:root {
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-8: 32px;
  --focus-ring: 0 0 0 3px rgba(77, 139, 255, 0.22);
}

.app-shell {
  min-height: 100vh;
}

.app-layout {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr);
}

.app-sidebar {
  position: sticky;
  top: 0;
  align-self: start;
  height: 100vh;
  border-right: 1px solid var(--border);
  background:
    radial-gradient(420px 220px at 0% -20%, rgba(77, 139, 255, 0.07), transparent 55%),
    linear-gradient(180deg, #1a1c20 0%, #16181b 100%);
  padding: var(--space-5) var(--space-4) var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.sidebar-head {
  position: relative;
  text-align: center;
}

.sidebar-brand {
  display: block;
  text-decoration: none;
  text-align: center;
}

.sidebar-logo-img {
  width: 100%;
  max-width: 144px;
  height: auto;
  display: block;
  margin: 0 auto;
}

.brand-main {
  color: var(--text);
}

.brand-accent {
  color: var(--brand);
}

.sidebar-caption {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  text-align: center;
  margin-top: 6px;
}

.sidebar-menu {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  flex: 1 1 auto;
}

.sidebar-toggle {
  display: none;
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border: 1px solid var(--border2);
  border-radius: 8px;
  background: rgba(36, 39, 45, 0.9);
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  cursor: pointer;
}

.sidebar-toggle:hover {
  border-color: var(--brand);
}

.topbar-brand {
  display: none;
  text-decoration: none;
  text-align: center;
  flex: 1;
}

.topbar-logo-img {
  height: 32px;
  width: auto;
  max-width: 220px;
  display: block;
  margin: 0 auto;
  object-fit: contain;
  background: transparent;
}

.login-logo-img {
  width: 100%;
  max-width: 240px;
  height: auto;
  display: block;
  margin: 0 auto 8px;
}

.topbar-row {
  display: none;
}

.sidebar-toggle-bar {
  display: block;
  width: 14px;
  height: 2px;
  border-radius: 2px;
  background: var(--text);
}

.sidebar-nav {
  margin-top: var(--space-2);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.sidebar-user-block {
  display: none;
}

.sidebar-separator {
  display: none;
}

.sidebar-link {
  height: 38px;
  border: 1px solid transparent;
  border-radius: 10px;
  display: flex;
  align-items: center;
  padding: 0 var(--space-3);
  color: var(--muted);
  text-decoration: none;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.01em;
  transition: border-color 0.15s ease, color 0.15s ease, background-color 0.15s ease;
}

.sidebar-link:hover {
  color: var(--text);
  border-color: var(--border2);
  background: rgba(23, 35, 58, 0.55);
}

.sidebar-link.is-active {
  color: var(--brand);
  border-color: rgba(77, 139, 255, 0.40);
  background: rgba(77, 139, 255, 0.08);
}

.sidebar-link-label {
  white-space: nowrap;
}

.sidebar-subsection {
  margin: var(--space-2) 0 var(--space-1);
  padding: 0 var(--space-3);
  /* Bumped 10px -> 11px — 10px fell below WCAG minimum readable size
     even accounting for the uppercase+letter-spacing compensation. */
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 700;
  opacity: 0.9;
}

.sidebar-link-sub {
  position: relative;
  padding-left: calc(var(--space-3) + 14px);
}

.sidebar-link-sub::before {
  content: "";
  position: absolute;
  left: var(--space-3);
  top: 50%;
  transform: translateY(-50%);
  width: 5px;
  height: 5px;
  border-radius: 999px;
  background: var(--border2);
  transition: background-color 0.15s ease;
}

.sidebar-link-sub:hover::before {
  background: var(--text);
}

.sidebar-link-sub.is-active::before {
  background: var(--brand);
}

/* Pricing link gets a subtle gold tint to match the topbar treatment.
   Hidden on desktop (the topbar already has it); shown on mobile where
   .top-actions collapses to display:none below 840px. */
.sidebar-link-pricing {
  display: none;
  margin-top: var(--space-3);
  color: var(--brand);
}
@media (max-width: 840px) {
  .sidebar-link-pricing { display: flex; }
}
.sidebar-link-pricing:hover {
  color: var(--brand);
  background: rgba(77, 139, 255, 0.08);
}
.sidebar-link-pricing.is-active {
  color: var(--brand);
  background: rgba(77, 139, 255, 0.1);
}

.sidebar-bottom-nav {
  margin-top: auto;
  padding-top: var(--space-3);
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.sidebar-footer {
  margin-top: auto;
  padding-top: var(--space-3);
  border-top: 1px solid var(--border);
  /* 10px -> 11px (WCAG minimum readable) */
  font-size: 11px;
  color: var(--muted);
  opacity: 0.55;
  line-height: 1.5;
  text-align: center;
}

.sidebar-copyright {
  margin-top: 4px;
  font-size: 10px;
  opacity: 0.9;
}

/* ── Sidebar icons ──────────────────────────────────────────── */
.sidebar-icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  margin-right: 10px;
  opacity: 0.7;
  transition: opacity 0.15s;
}
.sidebar-link:hover .sidebar-icon { opacity: 1; }
.sidebar-link.is-active .sidebar-icon { opacity: 1; color: var(--brand); }

/* Remove bullet dots — icons replace them */
.sidebar-link-sub::before { display: none; }
.sidebar-link-sub { padding-left: var(--space-3); }

/* ── Collapse toggle button ─────────────────────────────────── */
.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: center;
}
.sidebar-collapse-btn {
  position: absolute;
  right: -12px;
  top: 50%;
  transform: translateY(-50%);
  width: 24px;
  height: 24px;
  background: var(--surface2, #2A2D31);
  border: 1px solid var(--border2, #383c45);
  color: var(--muted);
  cursor: pointer;
  padding: 0;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s, background 0.15s, border-color 0.15s;
  z-index: 2;
}
.sidebar-collapse-btn:hover {
  color: var(--brand);
  background: var(--surface, #1e2025);
  border-color: var(--brand);
}
.sidebar-collapse-btn .sidebar-icon {
  width: 14px;
  height: 14px;
  margin: 0;
  opacity: 1;
  transition: transform 0.25s;
}

/* ── Collapsed state ────────────────────────────────────────── */
.app-layout {
  transition: grid-template-columns 0.25s ease;
}
.app-layout.sidebar-collapsed {
  grid-template-columns: 64px minmax(0, 1fr);
}
.app-layout.sidebar-collapsed .app-sidebar {
  padding: var(--space-5) var(--space-2) var(--space-4);
  overflow: hidden;
}
.app-layout.sidebar-collapsed .sidebar-logo-img {
  max-width: 36px;
}
.app-layout.sidebar-collapsed .sidebar-link-label,
.app-layout.sidebar-collapsed .sidebar-subsection,
.app-layout.sidebar-collapsed .sidebar-user-block,
.app-layout.sidebar-collapsed .sidebar-separator,
.app-layout.sidebar-collapsed .sidebar-footer-text,
.app-layout.sidebar-collapsed .sidebar-copyright,
.app-layout.sidebar-collapsed .sidebar-badge {
  display: none;
}
.app-layout.sidebar-collapsed .sidebar-icon {
  margin-right: 0;
}
.app-layout.sidebar-collapsed .sidebar-link {
  justify-content: center;
  padding: 0;
  width: 40px;
  height: 40px;
  margin: 1px auto;
  border-radius: 10px;
}
.app-layout.sidebar-collapsed .sidebar-link-sub {
  padding: 0;
}
.app-layout.sidebar-collapsed .sidebar-footer {
  text-align: center;
}
.app-layout.sidebar-collapsed .sidebar-collapse-btn {
  opacity: 1;
  position: static;
  transform: none;
  margin: 0 auto;
}
.app-layout.sidebar-collapsed .sidebar-collapse-btn .sidebar-icon {
  transform: rotate(180deg);
}
.app-layout.sidebar-collapsed .sidebar-head {
  flex-direction: column;
  gap: 8px;
}

.app-main {
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.app-topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  min-height: 60px;
  border-bottom: 1px solid var(--border2);
  background: linear-gradient(180deg, #1d2026 0%, #171a20 100%);
  padding: 0 var(--space-6);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
}

.top-search-wrap {
  position: relative;
  width: min(640px, 100%);
}

.top-search {
  width: 100%;
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.top-search-input {
  width: 100%;
  flex: 1 1 auto;
  min-width: 0;
  height: 44px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface) url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%234D8BFF' stroke-width='2' stroke-linecap='round' stroke-linejoin='round' opacity='0.7'><circle cx='11' cy='11' r='8'/><line x1='21' y1='21' x2='16.65' y2='16.65'/></svg>") no-repeat 14px center;
  color: var(--text);
  font-size: 14px;
  padding: 0 16px 0 40px;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.top-search-input:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(77, 139, 255, 0.18);
}

.top-search-input::placeholder { color: var(--muted); }

.suggest-list {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 16px 36px rgba(0,0,0,.45);
  overflow: hidden;
  z-index: 200;
  max-height: 360px;
  overflow-y: auto;
}
.suggest-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  border: 0;
  background: transparent;
  color: var(--text);
  text-decoration: none;
  padding: 9px 12px;
  cursor: pointer;
}
.suggest-item:hover,
.suggest-item.active { background: var(--surface2); }
.suggest-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.suggest-logo {
  width: 22px;
  height: 22px;
  border-radius: 4px;
  object-fit: contain;
  flex-shrink: 0;
  background: var(--surface);
  border: 1px solid var(--border);
}
.suggest-symbol {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: .2px;
  min-width: 56px;
  text-align: left;
  font-family: var(--mono);
}
.suggest-name {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 390px;
}
.suggest-open {
  font-size: 11px;
  color: var(--info);
  white-space: nowrap;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-wrap: wrap;
}

.inline-form-reset {
  margin: 0;
}

.top-actions .inline-form-reset {
  display: inline-flex;
}

.top-link {
  height: 32px;
  border: 1px solid transparent;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  padding: 0 var(--space-2);
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  text-decoration: none;
}

.top-link:hover {
  color: var(--text);
  border-color: var(--border2);
  background: var(--surface);
}

.top-link-btn {
  background: transparent;
  cursor: pointer;
  font: inherit;
  appearance: none;
}

.top-link-pricing {
  color: var(--brand);
  border-color: rgba(77, 139, 255, 0.35);
}
.top-link-pricing:hover {
  color: var(--brand);
  border-color: var(--brand);
  background: rgba(77, 139, 255, 0.08);
}
.top-link-pricing.is-active {
  color: var(--brand);
  border-color: var(--brand);
  background: rgba(77, 139, 255, 0.1);
}

/* ── Theme toggle button ───────────────────────────────────────── */
.theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px; height: 32px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: all .15s;
  padding: 0;
  flex-shrink: 0;
}
.theme-toggle:hover {
  border-color: var(--brand);
  color: var(--brand);
}
/* In dark mode: show sun icon, hide moon */
.theme-icon-dark { display: none; }
.theme-icon-light { display: block; }
/* In light mode: show moon icon, hide sun */
[data-theme="light"] .theme-icon-dark { display: block; }
[data-theme="light"] .theme-icon-light { display: none; }

.theme-toggle-sidebar {
  margin-bottom: 8px;
}

.app-content {
  flex: 1;
  min-width: 0;
  padding: var(--space-5) var(--space-6) 56px;
}

.disclaimer-bar {
  padding: 8px var(--space-6);
  font-size: 11px;
  color: var(--text-muted, #888);
  border-top: 1px solid var(--border, #e5e7eb);
  text-align: center;
}

@media (max-width: 840px) {
  .app-layout {
    grid-template-columns: 1fr;
  }

  /* Topbar: burger+brand row, then search below */
  .app-topbar {
    padding: var(--space-2) var(--space-3) var(--space-3);
    min-height: auto;
    flex-direction: column;
    align-items: stretch;
    gap: var(--space-2);
  }

  .topbar-row {
    display: flex;
    align-items: center;
    position: relative;
    min-height: 44px;
  }

  .sidebar-toggle {
    display: inline-flex;
    position: absolute;
    left: 0;
  }

  .topbar-brand {
    display: block;
    flex: 1;
    text-align: center;
  }

  .top-search-wrap {
    width: 100%;
  }

  .top-search-input {
    height: 42px;
    font-size: 14px;
  }

  .top-actions {
    display: none;
  }

  .suggest-name { max-width: 150px; }

  /* Drawer: slides in from left, 80vw wide */
  .app-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100dvh;
    width: 80vw;
    max-width: 320px;
    z-index: 300;
    border-right: 1px solid var(--border);
    border-bottom: 0;
    padding: var(--space-4) var(--space-4) var(--space-3);
    background: linear-gradient(180deg, #1a1c20 0%, #16181b 100%);
    transform: translateX(-100%);
    transition: transform 0.26s cubic-bezier(0.4, 0, 0.2, 1);
    overflow-y: auto;
  }

  .app-sidebar.nav-open {
    transform: translateX(0);
  }

  /* Backdrop over the right 20% */
  .app-sidebar::after {
    content: "";
    position: fixed;
    inset: 0;
    left: 80vw;
    background: rgba(0, 0, 0, 0.55);
    z-index: -1;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.26s ease;
  }

  .app-sidebar.nav-open::after {
    opacity: 1;
    pointer-events: auto;
  }

  /* Hide brand from inside drawer on mobile (it's in topbar) */
  .sidebar-head {
    display: none;
  }

  .sidebar-menu {
    display: flex;
    margin-top: 0;
    gap: var(--space-2);
  }

  .sidebar-nav {
    margin-top: 0;
    flex-direction: column;
    gap: 6px;
  }

  .sidebar-bottom-nav {
    margin-top: 0;
    padding-top: var(--space-2);
  }

  .sidebar-user-block {
    display: block;
  }

  .sidebar-user-toggle {
    width: 100%;
    height: 36px;
    border: 1px solid var(--border2);
    border-radius: 10px;
    background: rgba(20, 28, 40, 0.7);
    color: var(--text);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 var(--space-3);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
  }

  .sidebar-user-toggle:hover {
    border-color: var(--brand);
  }

  .sidebar-user-caret {
    color: var(--muted);
    font-size: 12px;
    line-height: 1;
    transition: transform 0.16s ease;
  }

  .sidebar-user-block.is-open .sidebar-user-caret {
    transform: rotate(180deg);
  }

  .sidebar-user-submenu {
    display: none;
    margin-top: 6px;
    padding-left: var(--space-2);
  }

  .sidebar-user-block.is-open .sidebar-user-submenu {
    display: block;
  }

  .sidebar-user-link {
    height: 34px;
    border: 1px solid var(--border);
    border-radius: 8px;
    display: flex;
    align-items: center;
    padding: 0 var(--space-3);
    color: var(--muted);
    font-size: 12px;
    font-weight: 600;
    text-decoration: none;
  }

  .sidebar-user-link-btn {
    width: 100%;
    background: transparent;
    cursor: pointer;
    font: inherit;
    text-align: left;
    appearance: none;
  }

  .sidebar-user-link:hover {
    color: var(--text);
    border-color: var(--border2);
    background: var(--surface);
  }

  .sidebar-separator {
    display: block;
    height: 1px;
    background: var(--border);
    margin: 2px 0 4px;
  }

  .sidebar-link {
    height: 36px;
    padding: 0 var(--space-3);
    justify-content: flex-start;
    font-size: 13px;
  }

  .sidebar-subsection {
    margin-top: var(--space-1);
  }

  .sidebar-link-sub {
    padding-left: calc(var(--space-3) + 12px);
  }

  .app-content {
    padding: var(--space-4) var(--space-4) 40px;
  }
}

/* ── Error pages (DSH-1) ───────────────────────────────────────────── */
.error-shell {
  min-height: calc(100vh - 200px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
}
.error-card {
  max-width: 480px;
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 48px 36px;
}
.error-code {
  font-size: 64px;
  font-weight: 800;
  color: var(--brand);
  letter-spacing: -0.04em;
  line-height: 1;
  margin-bottom: 8px;
}
.error-card h1 {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
  margin: 0 0 12px;
  letter-spacing: -0.01em;
}
.error-card p {
  font-size: 13px;
  color: var(--muted);
  line-height: 1.55;
  margin: 0 0 24px;
}
.error-actions {
  display: flex;
  justify-content: center;
}
.error-btn {
  display: inline-flex;
  align-items: center;
  height: 40px;
  padding: 0 20px;
  border: 1px solid var(--brand);
  border-radius: 10px;
  background: transparent;
  color: var(--brand);
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
  transition: background .15s;
}
.error-btn:hover {
  background: rgba(77, 139, 255, 0.1);
}

/* ── Toast notifications (DSH-9) ───────────────────────────────────── */
.toast-host {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 10px;
  pointer-events: none;
  max-width: calc(100vw - 48px);
}
.toast {
  pointer-events: auto;
  min-width: 240px;
  max-width: 360px;
  padding: 12px 16px;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border2);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  line-height: 1.4;
  box-shadow: 0 12px 32px rgba(0,0,0,0.4);
  cursor: pointer;
  opacity: 0;
  transform: translateX(20px);
  transition: opacity .2s ease-out, transform .2s ease-out;
}
.toast.is-visible {
  opacity: 1;
  transform: translateX(0);
}
.toast.is-leaving {
  opacity: 0;
  transform: translateX(20px);
}
.toast-success {
  border-color: rgba(34, 197, 94, 0.45);
  background: linear-gradient(180deg, rgba(34, 197, 94, 0.08) 0%, var(--surface) 100%);
}
.toast-error {
  border-color: rgba(239, 68, 68, 0.45);
  background: linear-gradient(180deg, rgba(239, 68, 68, 0.08) 0%, var(--surface) 100%);
}
.toast-info {
  border-color: rgba(56, 189, 248, 0.45);
  background: linear-gradient(180deg, rgba(56, 189, 248, 0.08) 0%, var(--surface) 100%);
}

@media (max-width: 640px) {
  .toast-host { bottom: 16px; right: 16px; left: 16px; max-width: none; }
  .toast { min-width: 0; max-width: none; }
}

/* ── Light theme overrides ─────────────────────────────────────────── */
[data-theme="light"] .app-sidebar {
  background:
    radial-gradient(420px 220px at 0% -20%, rgba(183, 142, 92, 0.06), transparent 55%),
    linear-gradient(180deg, #f8f9fb 0%, #f0f1f3 100%);
}
[data-theme="light"] .app-topbar {
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(12px);
}
[data-theme="light"] .top-search-input {
  background: var(--surface2);
  color: var(--text);
}
[data-theme="light"] .toast {
  box-shadow: 0 12px 32px rgba(0,0,0,0.1);
}
[data-theme="light"] .toast-success {
  border-color: rgba(22, 163, 74, 0.35);
  background: linear-gradient(180deg, rgba(22, 163, 74, 0.06) 0%, var(--surface) 100%);
}
[data-theme="light"] .toast-error {
  border-color: rgba(220, 38, 38, 0.35);
  background: linear-gradient(180deg, rgba(220, 38, 38, 0.06) 0%, var(--surface) 100%);
}
[data-theme="light"] .toast-info {
  border-color: rgba(59, 130, 196, 0.35);
  background: linear-gradient(180deg, rgba(59, 130, 196, 0.06) 0%, var(--surface) 100%);
}
@media (max-width: 840px) {
  [data-theme="light"] .app-sidebar {
    background: linear-gradient(180deg, #f8f9fb 0%, #f0f1f3 100%);
  }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  .app-layout,
  .app-sidebar,
  .app-sidebar::after,
  .toast {
    transition: none !important;
  }
}

