/*
 * AI Lumen Studio — Shell Layout
 * Copyright 2006 - Marchi Ventures, LLC - All Rights Reserved.
 *
 * CSS Grid-based shell with 14 configurable regions.
 * Regions are toggled via data-* attributes on #shell.
 */

/* -- SVG icon primitives -- */
svg.icon, .icon svg {
  display: inline-block;
  vertical-align: middle;
  flex-shrink: 0;
  pointer-events: none;
}

/* -- Reset & base -- */
*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: var(--lh-normal);
  color: var(--color-ink-primary);
  background: var(--color-bg-app);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ── Shell container ── */
#shell {
  display: grid;
  grid-template-areas:
    "nav  header   header"
    "nav  menubar  menubar"
    "nav  itoolbar itoolbar"
    "nav  toolbar  toolbar"
    "nav  content  inspector"
    "nav  drawer   inspector"
    "status status status";
  grid-template-columns:
    var(--shell-nav-width)
    1fr
    var(--shell-inspector-width);
  grid-template-rows:
    var(--shell-header-height)
    var(--shell-menubar-height)
    var(--shell-itoolbar-height)
    var(--shell-toolbar-height)
    1fr
    var(--shell-drawer-height)
    var(--shell-status-height);
  height: 100vh;
  overflow: hidden;
  transition:
    grid-template-columns var(--dur-normal) var(--ease-std),
    grid-template-rows    var(--dur-normal) var(--ease-std);
}

/* ── Nav collapsed ── */
#shell[data-nav="collapsed"] {
  --shell-nav-width: 56px;
}
#shell[data-nav="hidden"] {
  --shell-nav-width: 0px;
}

/* ── Inspector visible ── */
#shell[data-inspector="open"] {
  --shell-inspector-width: 300px;
}

/* ── Drawer open ── */
#shell[data-drawer="open"] {
  --shell-drawer-height: 280px;
}

/* ── Left nav sidebar ── */
#shell-nav {
  grid-area: nav;
  display: flex;
  flex-direction: column;
  background: var(--color-nav-bg);
  border-right: 1px solid var(--color-nav-border);
  overflow: hidden;
  overflow-y: auto;
  transition: width var(--dur-normal) var(--ease-std);
  z-index: var(--z-raised);
  scrollbar-width: thin;
}

/* ── Header region ── */
#shell-header {
  grid-area: header;
  display: flex;
  align-items: center;
  gap: var(--sp-4);
  padding: 0 var(--sp-6);
  background: var(--color-header-bg);
  border-bottom: 1px solid var(--color-header-border);
  z-index: var(--z-sticky); /* must exceed menubar z-index so dropdown overlaps */
  flex-shrink: 0;
  overflow: visible;   /* allow dropdown to extend beyond header bounds */
}

/* ── Main Menubar region ── */
#shell-menubar {
  grid-area: menubar;
  display: flex;
  align-items: stretch;
  background: var(--color-bg-surface);
  border-bottom: 1px solid var(--color-border);
  z-index: calc(var(--z-raised) + 5);
  flex-shrink: 0;
  min-height: var(--shell-menubar-height);
  padding: 0 var(--sp-2);
  position: relative;
}

/* ── Icon/Action Toolbar region ── */
#shell-itoolbar {
  grid-area: itoolbar;
  display: flex;
  align-items: center;
  background: var(--color-bg-raised);
  border-bottom: 1px solid var(--color-border);
  z-index: var(--z-raised);
  flex-shrink: 0;
  min-height: var(--shell-itoolbar-height);
  padding: 0 var(--sp-3);
  overflow-x: auto;
  scrollbar-width: none;
}
#shell-itoolbar::-webkit-scrollbar { display: none; }

/* Toolbar placement: left — itoolbar becomes extreme-left column, outside nav */
#shell[data-toolbar-placement="left"] {
  grid-template-areas:
    "itoolbar nav     header   header"
    "itoolbar nav     menubar  menubar"
    "itoolbar nav     toolbar  toolbar"
    "itoolbar nav     content  inspector"
    "itoolbar nav     drawer   inspector"
    "status   status  status   status";
  grid-template-columns:
    var(--shell-itoolbar-width)
    var(--shell-nav-width)
    1fr
    var(--shell-inspector-width);
  grid-template-rows:
    var(--shell-header-height)
    var(--shell-menubar-height)
    var(--shell-toolbar-height)
    1fr
    var(--shell-drawer-height)
    var(--shell-status-height);
}
#shell[data-toolbar-placement="left"] #shell-itoolbar {
  grid-area: itoolbar;
  position: static;
  flex-direction: column;
  align-items: stretch;   /* children fill full width */
  border-bottom: none;
  border-right: 1px solid var(--color-border);
  overflow: visible;      /* allow tooltips to escape the bar */
  padding: var(--sp-2) 0;
  min-height: 0;
}

/* Toolbar placement: right — itoolbar becomes extreme-right column, outside inspector */
#shell[data-toolbar-placement="right"] {
  grid-template-areas:
    "nav  header   header    itoolbar"
    "nav  menubar  menubar   itoolbar"
    "nav  toolbar  toolbar   itoolbar"
    "nav  content  inspector itoolbar"
    "nav  drawer   inspector itoolbar"
    "status status  status   status";
  grid-template-columns:
    var(--shell-nav-width)
    1fr
    var(--shell-inspector-width)
    var(--shell-itoolbar-width);
  grid-template-rows:
    var(--shell-header-height)
    var(--shell-menubar-height)
    var(--shell-toolbar-height)
    1fr
    var(--shell-drawer-height)
    var(--shell-status-height);
}
#shell[data-toolbar-placement="right"] #shell-itoolbar {
  grid-area: itoolbar;
  position: static;
  flex-direction: column;
  align-items: stretch;   /* children fill full width */
  border-bottom: none;
  border-left: 1px solid var(--color-border);
  overflow: visible;      /* allow tooltips to escape the bar */
  padding: var(--sp-2) 0;
  min-height: 0;
}

/* Vertical tool buttons for left/right placement */
#shell[data-toolbar-placement="left"] .itoolbar-tool,
#shell[data-toolbar-placement="right"] .itoolbar-tool {
  flex-direction: column;
  width: 100%;
  justify-content: center;
  padding: var(--sp-2) 4px;
  border-radius: 0;
}
/* The .itoolbar and .itoolbar-group containers must also stack vertically */
#shell[data-toolbar-placement="left"] #shell-itoolbar .itoolbar,
#shell[data-toolbar-placement="right"] #shell-itoolbar .itoolbar {
  flex-direction: column;
  align-items: stretch;
  flex: unset;  /* remove the flex:1 that causes horizontal expansion */
  width: 100%;
  gap: 0;
}
#shell[data-toolbar-placement="left"] #shell-itoolbar .itoolbar-group,
#shell[data-toolbar-placement="right"] #shell-itoolbar .itoolbar-group {
  flex-direction: column;
  align-items: stretch;
  width: 100%;
}
#shell[data-toolbar-placement="left"] .itoolbar-label,
#shell[data-toolbar-placement="right"] .itoolbar-label {
  font-size: 9px;
  text-align: center;
  line-height: 1.1;
  max-width: 44px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: normal;
  word-break: break-word;
}

/* Toolbar placement: bottom */
#shell[data-toolbar-placement="bottom"] {
  grid-template-areas:
    "nav  header   header"
    "nav  menubar  menubar"
    "nav  toolbar  toolbar"
    "nav  content  inspector"
    "nav  drawer   inspector"
    "nav  itoolbar itoolbar"
    "status status status";
  grid-template-rows:
    var(--shell-header-height)
    var(--shell-menubar-height)
    var(--shell-toolbar-height)
    1fr
    var(--shell-drawer-height)
    var(--shell-itoolbar-height)
    var(--shell-status-height);
}
#shell[data-toolbar-placement="bottom"] #shell-itoolbar {
  grid-area: itoolbar;
  border-top: 1px solid var(--color-border);
  border-bottom: none;
}

/* Toolbar display modes */
#shell[data-toolbar-mode="icon-only"] .itoolbar-label { display: none; }
#shell[data-toolbar-mode="label-only"] .itoolbar-icon { display: none; }

/* ── Toolbar / Breadcrumb region ── */
#shell-toolbar {
  grid-area: toolbar;
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: 0 var(--sp-6);
  background: var(--color-bg-surface);
  border-bottom: 1px solid var(--color-border);
  z-index: var(--z-raised);
  flex-shrink: 0;
  min-height: var(--shell-toolbar-height);
}

/* ── Main workspace (content) ── */
#shell-workspace {
  grid-area: content;
  overflow-y: auto;
  overflow-x: hidden;
  background: var(--color-bg-app);
  scrollbar-width: thin;
}

/* ── Right inspector panel ── */
#shell-inspector {
  grid-area: inspector;
  overflow-y: auto;
  background: var(--color-bg-surface);
  border-left: 1px solid var(--color-border);
  overflow: hidden;
  transition: width var(--dur-normal) var(--ease-std);
}
#shell[data-inspector!="open"] #shell-inspector {
  display: none;
}

/* ── Bottom drawer ── */
#shell-drawer {
  grid-area: drawer;
  background: var(--color-bg-surface);
  border-top: 1px solid var(--color-border);
  overflow: hidden;
  transition: height var(--dur-normal) var(--ease-std);
}
#shell[data-drawer!="open"] #shell-drawer {
  display: none;
}

/* ── Status strip (footer) ── */
#shell-status {
  grid-area: status;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--sp-4);
  background: var(--brand-primary);
  color: rgba(255,255,255,0.9);
  font-size: var(--text-xs);
  gap: var(--sp-4);
  flex-shrink: 0;
}

/* ── Overlay layers ── */
#shell-float-layer {
  position: fixed;
  inset: 0;
  z-index: var(--z-float);
  pointer-events: none;
}
#shell-float-layer > * { pointer-events: auto; }

#shell-modal-layer {
  position: fixed;
  inset: 0;
  z-index: var(--z-modal);
  display: none;
}
#shell-modal-layer.is-open {
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.5);
  backdrop-filter: blur(2px);
}

#shell-toast-layer {
  position: fixed;
  bottom: var(--sp-6);
  right: var(--sp-6);
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column-reverse;
  gap: var(--sp-2);
  pointer-events: none;
}
#shell-toast-layer > * { pointer-events: auto; }

/* ── Nav anatomy ── */
.nav-brand {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-4) var(--sp-4);
  height: var(--shell-header-height);
  border-bottom: 1px solid var(--color-nav-border);
  flex-shrink: 0;
  text-decoration: none;
}
.nav-brand-logo {
  width: 28px;
  height: 28px;
  object-fit: contain;
  flex-shrink: 0;
}
.nav-brand-wordmark {
  font-size: var(--text-md);
  font-weight: var(--fw-semibold);
  color: var(--color-ink-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.nav-brand-wordmark span { font-weight: var(--fw-normal); }

.nav-context-label {
  font-size: var(--text-xs);
  color: var(--color-ink-muted);
  padding: var(--sp-2) var(--sp-4) 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.nav-scroll {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: var(--sp-2) 0;
  scrollbar-width: thin;
}

.nav-section {
  margin-bottom: var(--sp-3);
}
.nav-section-label {
  display: block;
  padding: var(--sp-3) var(--sp-4) var(--sp-1);
  font-size: var(--text-xs);
  font-weight: var(--fw-semibold);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--color-nav-section-fg);
  white-space: nowrap;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-4);
  font-size: var(--text-sm);
  font-weight: var(--fw-medium);
  color: var(--color-nav-item-fg);
  text-decoration: none;
  border-radius: 0;
  cursor: pointer;
  transition: background var(--dur-fast) var(--ease-std);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-height: 32px;
}
.nav-item:hover {
  background: var(--color-nav-item-hover);
}
.nav-item.active {
  background: var(--color-nav-item-active-bg);
  color: var(--color-nav-item-active-fg);
}
.nav-item.active .nav-item-icon {
  opacity: 1;
}
.nav-item-icon {
  width: 18px;
  flex-shrink: 0;
  font-size: var(--text-md);
  opacity: 0.7;
  display: flex;
  align-items: center;
  justify-content: center;
}
.nav-item-label { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.nav-item-arrow {
  font-size: var(--text-xs);
  opacity: 0.5;
  transition: transform var(--dur-fast) var(--ease-std);
}
.nav-item[aria-expanded="true"] .nav-item-arrow {
  transform: rotate(90deg);
}
.nav-item.indent-1 { padding-left: 28px; }
.nav-item.indent-2 { padding-left: 42px; }

.nav-footer {
  flex-shrink: 0;
  border-top: 1px solid var(--color-nav-border);
  padding: var(--sp-3) var(--sp-3);
}
.nav-search {
  position: relative;
}
.nav-search input {
  width: 100%;
  padding: var(--sp-2) var(--sp-3) var(--sp-2) var(--sp-8);
  background: var(--color-bg-sunken);
  border: 1px solid var(--color-border-input);
  border-radius: var(--radius-sm);
  font-size: var(--text-sm);
  color: var(--color-ink-primary);
  outline: none;
  transition: border-color var(--dur-fast);
}
.nav-search input:focus { border-color: var(--color-border-focus); }
.nav-search::before {
  content: "⌕";
  position: absolute;
  left: 8px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--color-ink-muted);
  font-size: var(--text-md);
  pointer-events: none;
}

/* ── Collapsed nav (rail only) ── */
#shell[data-nav="collapsed"] .nav-brand-wordmark,
#shell[data-nav="collapsed"] .nav-section-label,
#shell[data-nav="collapsed"] .nav-item-label,
#shell[data-nav="collapsed"] .nav-item-arrow,
#shell[data-nav="collapsed"] .nav-context-label,
#shell[data-nav="collapsed"] .nav-footer { display: none; }
#shell[data-nav="collapsed"] .nav-item {
  justify-content: center;
  padding: var(--sp-3) var(--sp-2);
}
#shell[data-nav="collapsed"] .nav-item-icon {
  width: 22px;
  opacity: 0.8;
}

/* ── Header anatomy ── */
.header-title {
  font-size: var(--text-xl);
  font-weight: var(--fw-bold);
  color: var(--color-ink-primary);
  white-space: nowrap;
  flex-shrink: 0;
  margin: 0;
}
.header-spacer { flex: 1; }
.header-controls {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  flex-shrink: 0;
}

/* Header icon action buttons (messages/alerts/notifications) */
.header-icon-btn {
  position: relative;
  background: none;
  border: none;
  cursor: pointer;
  font-size: var(--icon-md);
  color: var(--color-ink-muted);
  padding: var(--sp-1) var(--sp-2);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--dur-fast), color var(--dur-fast);
}
.header-icon-btn:hover {
  background: var(--color-bg-hover);
  color: var(--color-ink-primary);
}
.header-icon-btn .badge-dot {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 8px;
  height: 8px;
  border-radius: var(--radius-full);
  background: var(--color-danger);
  border: 2px solid var(--color-header-bg);
  display: none;
}
.header-icon-btn[data-has-badge="true"] .badge-dot { display: block; }

/* User menu */
.user-menu {
  position: relative;
}
.avatar {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-full);
  background: var(--brand-primary);
  color: #fff;
  font-size: var(--text-xs);
  font-weight: var(--fw-semibold);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border: none;
  flex-shrink: 0;
  transition: box-shadow var(--dur-fast);
}
.avatar:hover { box-shadow: 0 0 0 3px var(--color-bg-hover); }
.avatar[aria-expanded="true"] { box-shadow: 0 0 0 3px var(--brand-accent); }

.user-menu-dropdown {
  position: absolute;
  top: calc(100% + var(--sp-2));
  right: 0;
  min-width: 240px;
  background: var(--color-bg-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  z-index: calc(var(--z-modal) - 10);
  padding: var(--sp-2) 0;
  display: none;
}
.user-menu-dropdown.is-open { display: block; }

.user-menu-header {
  padding: var(--sp-3) var(--sp-4);
  border-bottom: 1px solid var(--color-border);
  margin-bottom: var(--sp-1);
}
.user-menu-name {
  font-size: var(--text-sm);
  font-weight: var(--fw-semibold);
  color: var(--color-ink-primary);
}
.user-menu-email {
  font-size: var(--text-xs);
  color: var(--color-ink-muted);
}

.user-menu-section-label {
  display: block;
  font-size: var(--text-xs);
  font-weight: var(--fw-semibold);
  color: var(--color-ink-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: var(--sp-2) var(--sp-4) var(--sp-1);
}
.user-menu-item,
.user-menu-theme-btn,
.user-menu-locale-btn {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  width: 100%;
  padding: var(--sp-2) var(--sp-4);
  background: none;
  border: none;
  text-align: left;
  font-size: var(--text-sm);
  color: var(--color-ink-secondary);
  cursor: pointer;
  text-decoration: none;
  transition: background var(--dur-fast);
}
.user-menu-item:hover,
.user-menu-theme-btn:hover,
.user-menu-locale-btn:hover {
  background: var(--color-bg-hover);
  color: var(--color-ink-primary);
}
.user-menu-theme-btn.active,
.user-menu-locale-btn.active {
  color: var(--brand-primary);
  font-weight: var(--fw-medium);
}
.user-menu-sep {
  border: none;
  border-top: 1px solid var(--color-border);
  margin: var(--sp-1) 0;
}

/* ── User menu submenus (Theme / Language) ── */
.user-submenu-wrap {
  position: relative;
}
.user-submenu-trigger {
  justify-content: space-between;
}
.user-submenu-arrow {
  font-size: var(--text-xs);
  opacity: 0.6;
  margin-left: var(--sp-2);
  pointer-events: none;
}
/* ‹ default (submenu opens LEFT — user menu lives on the right edge) */
.user-submenu-arrow::before { content: "\2039"; }
/* › when submenu is flipped right */
.user-submenu-wrap:has(.user-submenu.opens-right) .user-submenu-arrow::before { content: "\203A"; }

/* Default: opens to the left of the user-menu-dropdown */
.user-submenu {
  position: absolute;
  top: -4px;
  right: 100%;
  margin-right: 2px;
  min-width: 180px;
  background: var(--color-bg-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  z-index: var(--z-tooltip);
  padding: var(--sp-2) 0;
  display: none;
}
.user-submenu.is-open { display: block; }
/* Flipped right when left edge would overflow the viewport */
.user-submenu.opens-right {
  right: auto;
  left: 100%;
  margin-right: 0;
  margin-left: 2px;
}

/* ── Toolbar anatomy ── */
.breadcrumb {
  display: flex;
  align-items: center;
  gap: var(--sp-1);
  font-size: var(--text-sm);
  color: var(--color-ink-muted);
  flex: 1;
  overflow: hidden;
}
.breadcrumb a {
  color: var(--color-ink-link);
  text-decoration: none;
}
.breadcrumb a:hover { text-decoration: underline; }
.breadcrumb-sep {
  color: var(--color-ink-muted);
  margin: 0 var(--sp-1);
}
.breadcrumb-current {
  color: var(--color-ink-secondary);
  font-weight: var(--fw-medium);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.toolbar-actions {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  flex-shrink: 0;
}

/* ── Inspector anatomy ── */
.inspector-header {
  padding: var(--sp-4);
  border-bottom: 1px solid var(--color-border);
  font-size: var(--text-sm);
  font-weight: var(--fw-semibold);
}
.inspector-body {
  padding: var(--sp-4);
  overflow-y: auto;
}
.inspector-section {
  margin-bottom: var(--sp-6);
}
.inspector-section-label {
  font-size: var(--text-xs);
  font-weight: var(--fw-semibold);
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--color-ink-muted);
  margin-bottom: var(--sp-3);
}

/* ── Drawer anatomy ── */
.drawer-header {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-2) var(--sp-4);
  border-bottom: 1px solid var(--color-border);
  font-size: var(--text-sm);
  font-weight: var(--fw-semibold);
  cursor: pointer;
}
.drawer-body {
  padding: var(--sp-4);
  overflow-y: auto;
  height: calc(var(--shell-drawer-height) - 40px);
}

/* ── Status strip anatomy ── */
.status-item {
  display: flex;
  align-items: center;
  gap: var(--sp-1);
  font-size: var(--text-xs);
  color: rgba(255,255,255,0.85);
}
.status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #4ade80;
  flex-shrink: 0;
}
.status-dot.warning { background: #fbbf24; }
.status-dot.error   { background: #f87171; }

/* ── Content page wrapper ── */
.page-content {
  padding: var(--sp-6);
  max-width: 1400px;
}
.page-header {
  margin-bottom: var(--sp-6);
}
.page-title {
  font-size: var(--text-2xl);
  font-weight: var(--fw-bold);
  color: var(--color-ink-primary);
  margin: 0 0 var(--sp-1);
  line-height: var(--lh-tight);
}
.page-subtitle {
  font-size: var(--text-md);
  color: var(--color-ink-muted);
  margin: 0;
}

/* ── Responsive ── */
@media (max-width: 900px) {
  #shell {
    grid-template-areas:
      "header  header"
      "menubar menubar"
      "toolbar toolbar"
      "content content"
      "status  status";
    grid-template-columns: 0 1fr;
    grid-template-rows:
      var(--shell-header-height)
      var(--shell-menubar-height)
      var(--shell-toolbar-height)
      1fr
      var(--shell-status-height);
  }
  #shell-nav { display: none; }
  #shell-itoolbar { display: none; }
  #shell[data-nav="open"] {
    grid-template-columns: var(--shell-nav-width) 1fr;
  }
  #shell[data-nav="open"] #shell-nav { display: flex; }
}
