:root {
      --color-bg: #0a0a0a;
      --color-surface: #0d0d0d;
      --color-surface-2: #111111;
      --color-surface-3: #141414;
      --color-border: #1f1f1f;
      --color-border-2: #222222;
      --color-accent: #c2410c;
      --color-accent-hover: #d14a12;
      --color-accent-border: #7c2d12;
      --color-accent-soft: rgba(194, 65, 12, 0.12);
      --color-text: #ffffff;
      --color-muted: #999999;
      --color-label: #666666;
      --color-section: #444444;
      --on-accent: #fff7ed;
      --color-success: #22c55e;
      --color-danger: #ef4444;
      --color-warning: #f59e0b;
      --color-code-bg: #0e0e10;
      --radius: 12px;
      --radius-sm: 6px;
      --font: 'Inter', ui-sans-serif, -apple-system, BlinkMacSystemFont, "Helvetica Neue", Arial, sans-serif;
      --font-mono: 'JetBrains Mono', "SF Mono", "Fira Code", ui-monospace, Consolas, monospace;
      --sidebar-width: 288px;
      --sidebar-width-collapsed: 72px;
      --control-height: 2.5rem;
    }

    * { box-sizing: border-box; margin: 0; padding: 0; }
    body { font-family: var(--font); background: var(--color-bg); color: var(--color-text); line-height: 1.6; min-height: 100vh; -webkit-font-smoothing: antialiased; }
    a { color: var(--color-accent); text-decoration: none; }
    code { font-family: var(--font-mono); font-size: 0.8rem; color: var(--color-text); }

    /* Portal shell layout */
    .portal-shell { display: flex; min-height: 100vh; }

    /* Sidebar (desktop) */
    .sidebar {
      width: var(--sidebar-width); flex-shrink: 0; background: var(--color-surface);
      border-right: 1px solid var(--color-border); display: flex; flex-direction: column;
      justify-content: space-between;
      height: 100vh;
      position: sticky;
      top: 0;
      transition: width 0.25s cubic-bezier(0.4, 0, 0.2, 1);
      overflow: hidden;
      z-index: 100;
    }
    .portal-shell.sidebar-collapsed .sidebar { 
      width: var(--sidebar-width-collapsed); 
      justify-content: flex-start;
      /* Let the edge chevron paint into main content. overflow:hidden
         on .sidebar/.sidebar-top clips it regardless of z-index. */
      overflow: visible;
      z-index: 200;
    }
    
    .sidebar-top {
      padding: 32px 32px 0;
      display: flex;
      flex-direction: column;
      gap: 48px;
      overflow-y: auto;
      overflow-x: hidden;
      flex: 1;
      min-height: 0;
      transition: padding 0.25s cubic-bezier(0.4, 0, 0.2, 1), gap 0.25s cubic-bezier(0.4, 0, 0.2, 1);
      scrollbar-width: thin;
      scrollbar-color: transparent transparent;
    }
    .sidebar-top:hover {
      scrollbar-color: var(--color-border) transparent;
    }
    .sidebar-top::-webkit-scrollbar {
      width: 6px;
    }
    .sidebar-top::-webkit-scrollbar-track {
      background: transparent;
    }
    .sidebar-top::-webkit-scrollbar-thumb {
      background: transparent;
      border-radius: 3px;
    }
    .sidebar-top:hover::-webkit-scrollbar-thumb {
      background: var(--color-border);
    }
    .portal-shell.sidebar-collapsed .sidebar-top {
      /* Tighter left inset than expanded (32px → 12px). Icons stay
         flex-start so they ease left with padding, not with width. */
      padding: 32px 12px 0;
      overflow: visible;
    }
    
    .sidebar-brand {
      display: flex; 
      align-items: center; 
      gap: 12px;
      flex-shrink: 0;
      position: relative;
      /* Two-line brand text is taller than the 36px logo; lock the
         row so taking .brand-text out of flow does not lift the nav. */
      min-height: 44px;
    }
    .brand-logo-img {
      flex-shrink: 0;
      border-radius: 8px;
    }
    .portal-shell.sidebar-collapsed .sidebar-brand {
      /* Toggle is position:absolute; keep the brand from becoming its
         containing block so right is relative to the sidebar edge.
         Stay left-aligned so the logo does not slide as width animates. */
      position: static;
    }
    .brand-text {
      display: flex;
      flex-direction: column;
      gap: 2px;
      min-width: 0;
      transition: opacity 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    }
    .brand-title {
      font-size: 15px;
      font-weight: 700;
      letter-spacing: -0.02em;
      color: var(--color-text);
      white-space: nowrap;
    }
    .brand-sub {
      font-size: 11px;
      color: var(--color-label);
      font-weight: 500;
      white-space: nowrap;
    }
    .portal-shell.sidebar-collapsed .brand-text {
      opacity: 0;
      pointer-events: none;
      position: absolute;
    }
    .sidebar-toggle {
      background: none;
      border: none;
      color: var(--color-muted);
      cursor: pointer;
      padding: 6px;
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 6px;
      transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1), background 0.15s, color 0.15s, right 0.25s cubic-bezier(0.4, 0, 0.2, 1);
      margin-left: auto;
      flex-shrink: 0;
      position: relative;
    }
    .sidebar-toggle:hover {
      background: var(--color-surface-2);
      color: var(--color-text);
    }
    .portal-shell.sidebar-collapsed .sidebar-toggle {
      position: absolute;
      top: 24px;
      right: -14px;
      margin-left: 0;
      transform: rotate(180deg);
      background: var(--color-surface);
      border: 1px solid var(--color-border);
      /* Above .main / admin tabs (later in the DOM); below modals (300). */
      z-index: 250;
    }
    .nav-section + .nav-section,
    .nav-list .nav-section:not(:first-child) {
      margin-top: 18px;
    }
    .nav-list {
      display: flex;
      flex-direction: column;
      gap: 4px;
    }
    .nav-section {
      font-size: 11px;
      font-weight: 600;
      text-transform: uppercase;
      letter-spacing: 0.06em;
      color: var(--color-section);
      padding: 8px 12px 4px;
      transition: opacity 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    }
    .portal-shell.sidebar-collapsed .nav-section {
      opacity: 0;
      pointer-events: none;
      overflow: hidden;
      white-space: nowrap;
    }
    .nav-item {
      display: flex;
      align-items: center;
      gap: 12px;
      padding: 10px 12px;
      /* 10px+10px padding + 14px label line-height (1.6 × 14px). Keeps
         the row from shrinking when .nav-label is taken out of flow. */
      min-height: calc(20px + 1.6em);
      border-radius: 8px;
      color: var(--color-muted);
      background: transparent;
      border: none;
      cursor: pointer;
      font-family: var(--font);
      font-size: 14px;
      transition: background 0.15s, color 0.15s;
      text-decoration: none;
      position: relative;
      width: 100%;
      text-align: left;
    }
    .nav-item:hover {
      background: var(--color-surface-2);
      color: var(--color-text);
    }
    .nav-item.active {
      background: var(--color-accent-soft);
      color: var(--color-accent);
    }
    .nav-icon-img {
      flex-shrink: 0;
      display: block;
      width: 14px;
      height: 14px;
    }
    .nav-label {
      white-space: nowrap;
      transition: opacity 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    }
    .portal-shell.sidebar-collapsed .nav-label {
      opacity: 0;
      pointer-events: none;
      position: absolute;
    }
    .sidebar-footer {
      padding: 16px 20px 6px;
      border-top: 1px solid var(--color-border);
      display: flex;
      flex-direction: column;
      gap: 12px;
      flex-shrink: 0;
      margin-top: auto;
      transition: padding 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    }
    .portal-shell.sidebar-collapsed .sidebar-footer {
      padding: 16px 12px 6px;
    }
    .user-card {
      display: flex;
      align-items: center;
      gap: 12px;
      min-width: 0;
    }
    .user-details {
      display: flex;
      flex-direction: column;
      gap: 2px;
      min-width: 0;
      flex: 1;
      transition: opacity 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    }
    .portal-shell.sidebar-collapsed .user-card {
      justify-content: flex-start;
    }
    .user-avatar {
      flex-shrink: 0;
    }
    .portal-shell.sidebar-collapsed .user-details {
      opacity: 0;
      pointer-events: none;
      position: absolute;
    }
    .user-email {
      font-size: 13px;
      font-weight: 500;
      color: var(--color-text);
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .user-role {
      font-size: 11px;
      color: var(--color-label);
      white-space: nowrap;
    }
    .signout-btn {
      display: flex;
      align-items: center;
      gap: 8px;
      padding: 8px 12px;
      background: transparent;
      border: 1px solid var(--color-border);
      border-radius: 6px;
      color: var(--color-muted);
      cursor: pointer;
      font-family: var(--font);
      font-size: 13px;
      transition: all 0.15s;
      justify-content: center;
    }
    .signout-btn:hover {
      background: var(--color-surface-2);
      border-color: var(--color-border-2);
      color: var(--color-text);
    }
    a.signin-link {
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 8px;
      padding: 8px 12px;
      background: var(--color-accent);
      border: 1px solid var(--color-accent-border);
      border-radius: 6px;
      color: var(--on-accent);
      font-family: var(--font);
      font-size: 13px;
      font-weight: 600;
      text-decoration: none;
    }
    a.signin-link:hover {
      background: var(--color-accent-hover);
      color: var(--on-accent);
      text-decoration: none;
    }
    .portal-shell.sidebar-collapsed a.signin-link .signin-label {
      opacity: 0;
      position: absolute;
      pointer-events: none;
    }
    .portal-shell.sidebar-collapsed .signout-btn {
      padding: 8px;
      width: 40px;
    }
    .signout-label {
      white-space: nowrap;
      transition: opacity 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    }
    .portal-shell.sidebar-collapsed .signout-label {
      opacity: 0;
      pointer-events: none;
      position: absolute;
    }

    /* Mobile hamburger button (hidden on desktop) */
    .mobile-menu-btn {
      display: none;
      position: fixed;
      top: 16px;
      right: 16px;
      z-index: 200;
      background: var(--color-surface);
      border: 1px solid var(--color-border);
      border-radius: 8px;
      padding: 10px;
      color: var(--color-text);
      cursor: pointer;
    }
    .mobile-menu-btn:hover {
      background: var(--color-surface-2);
    }
    
    .nav-sub {
      display: none;
      flex-direction: column;
      gap: 2px;
      padding: 2px 0 6px 28px;
    }
    .nav-item--sub {
      min-height: 0;
      padding: 6px 10px;
      font-size: 13px;
      gap: 8px;
    }
    .nav-sub-badge {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      min-width: 1.1rem;
      height: 1.1rem;
      padding: 0 0.3rem;
      margin-left: auto;
      border-radius: 999px;
      font-size: 0.65rem;
      font-weight: 600;
      background: var(--color-border);
      color: var(--color-muted);
    }
    .nav-item--sub.active .nav-sub-badge {
      background: var(--color-accent);
      color: var(--on-accent);
    }

    /* Status + version: one compact row at the bottom of the sidebar. */
    .sidebar-meta {
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 0.45rem;
      margin: 0 16px 10px;
      padding: 0;
      border: none;
      background: none;
    }
    .system-status-inline {
      display: flex;
      align-items: center;
      gap: 0.3rem;
      margin: 0;
      padding: 0;
      border: none;
      background: none;
      border-radius: 0;
    }
    .status-dot {
      display: block;
      width: 5px;
      height: 5px;
      background: var(--color-success);
      border-radius: 50%;
      flex-shrink: 0;
    }
    .status-text {
      font-size: 0.65rem;
      line-height: 1;
      color: var(--color-muted);
      white-space: nowrap;
    }
    .version-indicator {
      font-size: 0.65rem;
      line-height: 1;
      color: var(--color-muted);
      margin: 0;
      padding: 0;
    }
    .portal-shell.sidebar-collapsed .sidebar-meta {
      opacity: 0;
      position: absolute;
      pointer-events: none;
    }

    /* Mobile drawer overlay */
    .mobile-drawer-overlay {
      display: none;
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: rgba(0, 0, 0, 0.7);
      z-index: 150;
      opacity: 0;
      pointer-events: none;
      transition: opacity 0.25s ease;
    }
    .mobile-drawer-overlay.active {
      opacity: 1;
      pointer-events: auto;
    }

    /* Mobile: drawer overlay mode */
    @media (max-width: 768px) {
      .nav-sub {
        display: flex;
      }
      .mobile-menu-btn {
        display: flex;
        align-items: center;
        justify-content: center;
      }
      body.modal-open .mobile-menu-btn {
        display: none;
      }
      .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        height: 100vh;
        transform: translateX(-100%);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        width: 280px;
        z-index: 160;
        display: flex;
        flex-direction: column;
      }
      .sidebar.mobile-open {
        transform: translateX(0);
      }
      .mobile-drawer-overlay {
        display: block;
      }
      .sidebar-top {
        flex: 1;
        padding: 20px;
        gap: 16px;
      }
      .sidebar-toggle {
        display: none;
      }
      .portal-shell.sidebar-collapsed .sidebar {
        width: 280px;
        transform: translateX(-100%);
        overflow: hidden;
        z-index: 160;
      }
      .portal-shell.sidebar-collapsed .sidebar.mobile-open {
        transform: translateX(0);
      }
      .portal-shell.sidebar-collapsed .sidebar-top,
      .portal-shell.sidebar-collapsed .brand-text,
      .portal-shell.sidebar-collapsed .nav-label,
      .portal-shell.sidebar-collapsed .user-details,
      .portal-shell.sidebar-collapsed .signout-label,
      .portal-shell.sidebar-collapsed .sidebar-meta {
        opacity: 1;
        pointer-events: auto;
        position: static;
      }
      .portal-shell.sidebar-collapsed .sidebar-top {
        padding: 20px;
        gap: 16px;
      }
      .portal-shell.sidebar-collapsed .nav-item {
        justify-content: flex-start;
        padding: 10px 12px;
      }
      .portal-shell.sidebar-collapsed .signout-btn {
        padding: 8px 12px;
        width: auto;
        justify-content: center;
      }
      .portal-shell.sidebar-collapsed .nav-section {
        opacity: 1;
        pointer-events: auto;
        overflow: visible;
      }
      .portal-shell.sidebar-collapsed .sidebar-footer {
        padding: 16px 20px 6px;
        align-items: stretch;
      }
    }

    .portal-toast {
      position: fixed;
      right: max(1.25rem, env(safe-area-inset-right));
      bottom: max(1.25rem, env(safe-area-inset-bottom));
      z-index: 400;
      max-width: min(22rem, calc(100vw - 2rem));
      padding: 0.75rem 1rem;
      border-radius: var(--radius);
      font-size: 0.875rem;
      font-weight: 500;
      line-height: 1.4;
      box-shadow: 0 16px 40px rgba(0, 0, 0, 0.5);
      pointer-events: none;
      animation: portal-toast-in 0.28s cubic-bezier(0.4, 0, 0.2, 1);
    }
    .portal-toast--success {
      background: #0d2818;
      border: 1px solid var(--color-success);
      color: var(--color-success);
    }
    .portal-toast--error {
      background: #2a0c0c;
      border: 1px solid var(--color-danger);
      color: var(--color-danger);
    }
    .portal-toast.is-leaving {
      animation: portal-toast-out 0.28s cubic-bezier(0.4, 0, 0.2, 1) forwards;
    }
    @keyframes portal-toast-in {
      from { opacity: 0; transform: translate3d(12px, 16px, 0); }
      to { opacity: 1; transform: translate3d(0, 0, 0); }
    }
    @keyframes portal-toast-out {
      from { opacity: 1; transform: translate3d(0, 0, 0); }
      to { opacity: 0; transform: translate3d(8px, 12px, 0); }
    }
    @media (prefers-reduced-motion: reduce) {
      .portal-toast,
      .portal-toast.is-leaving {
        animation: none;
      }
    }
    
    /* Form controls — single source for inputs and dropdowns */
    .field, .field-select,
    .form-field input, .form-field select {
      width: 100%;
      padding: 0.55rem 0.7rem;
      border-radius: var(--radius-sm);
      border: 1px solid var(--color-border);
      background-color: var(--color-bg);
      color: var(--color-text);
      font-family: var(--font);
      font-size: 0.875rem;
      line-height: 1.3;
      color-scheme: dark;
    }
    .field, .field-select,
    .form-field input, .form-field select {
      height: var(--control-height);
    }
    .field:hover, .field-select:hover,
    .form-field input:hover, .form-field select:hover {
      border-color: var(--color-border-2);
    }
    .field:focus, .field-select:focus,
    .form-field input:focus, .form-field select:focus {
      outline: none;
      border-color: var(--color-accent);
    }
    .form-field select, .field-select {
      appearance: none;
      -webkit-appearance: none;
      background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23999999' d='M2.15 4.2 6 8.05 9.85 4.2 10.9 5.25 6 10.15 1.1 5.25z'/%3E%3C/svg%3E");
      background-repeat: no-repeat;
      background-position: right 0.7rem center;
      background-size: 12px 12px;
      padding-right: 2rem;
      cursor: pointer;
    }
    .form-field select:disabled, .field-select:disabled {
      opacity: 0.5;
      cursor: not-allowed;
    }
    .form-field select option, .field-select option {
      background: var(--color-surface);
      color: var(--color-text);
    }
    .inline-form .field,
    .inline-form .field-select {
      width: auto;
      min-width: 9.5rem;
      max-width: 16rem;
      height: auto;
      padding: 0.35rem 0.6rem;
      border-radius: var(--radius-sm);
      font-size: 0.8rem;
    }
    .inline-form .field-select {
      min-width: 8.75rem;
      padding-right: 1.75rem;
      background-position: right 0.5rem center;
    }

    /* Custom select — open menu uses portal chrome, not the OS picker */
    .select {
      position: relative;
      display: block;
      width: 100%;
      height: var(--control-height);
    }
    .select--inline {
      display: inline-block;
      width: auto;
      min-width: 8.75rem;
      max-width: 16rem;
      height: auto;
      vertical-align: middle;
    }
    .select.is-enhanced > select.field-select {
      position: absolute;
      inset: 0;
      opacity: 0;
      pointer-events: none;
      width: 100%;
      height: 100%;
      margin: 0;
    }
    .select-trigger {
      display: flex;
      align-items: center;
      gap: 0.5rem;
      width: 100%;
      height: 100%;
      min-height: var(--control-height);
      padding: 0.55rem 0.7rem;
      border-radius: var(--radius-sm);
      border: 1px solid var(--color-border);
      background: var(--color-bg);
      color: var(--color-text);
      font-family: var(--font);
      font-size: 0.875rem;
      line-height: 1.3;
      text-align: left;
      cursor: pointer;
      box-sizing: border-box;
    }
    .select--inline .select-trigger {
      min-height: 0;
      height: auto;
      padding: 0.35rem 0.6rem;
      font-size: 0.8rem;
    }
    .select-trigger:hover {
      border-color: var(--color-border-2);
    }
    .select.is-open .select-trigger,
    .select-trigger:focus {
      outline: none;
      border-color: var(--color-accent);
    }
    .select-trigger:disabled {
      opacity: 0.5;
      cursor: not-allowed;
    }
    .select-trigger-label {
      flex: 1;
      min-width: 0;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .select-trigger-icon {
      flex-shrink: 0;
      display: flex;
      color: var(--color-muted);
      transition: transform 0.15s ease;
    }
    .select.is-open .select-trigger-icon {
      transform: rotate(180deg);
      color: var(--color-text);
    }
    .select-menu {
      display: none;
      position: fixed;
      z-index: 280;
      min-width: 8rem;
      max-width: min(24rem, 90vw);
      max-height: 16rem;
      overflow-y: auto;
      padding: 4px;
      background: var(--color-surface-2);
      border: 1px solid var(--color-border);
      border-radius: var(--radius-sm);
      box-shadow: 0 16px 40px rgba(0, 0, 0, 0.55);
      color-scheme: dark;
    }
    .select-menu.is-open {
      display: block;
    }
    .select-option {
      display: block;
      width: 100%;
      padding: 0.45rem 0.7rem;
      border: none;
      border-radius: 4px;
      background: transparent;
      color: var(--color-text);
      font-family: var(--font);
      font-size: 0.875rem;
      line-height: 1.35;
      text-align: left;
      cursor: pointer;
    }
    .select--inline .select-option {
      font-size: 0.8rem;
    }
    .select-option:hover,
    .select-option.is-active {
      background: var(--color-surface-3);
    }
    .select-option.is-selected {
      background: var(--color-accent-soft);
      color: var(--color-text);
    }
    .select-option.is-selected.is-active,
    .select-option.is-selected:hover {
      background: var(--color-accent);
      color: var(--on-accent);
    }
    .select-option:disabled {
      opacity: 0.45;
      cursor: not-allowed;
    }

