/* App shell: sidebar + topbar + main */
.app {
  display: grid;
  grid-template-columns: var(--side-w) 1fr;
  min-height: 100vh;
}

/* ─── Sidebar — base (gradient style) ─────────────────────────── */
.sidebar {
  background: linear-gradient(180deg, var(--side-bg-1), var(--side-bg-2));
  color: var(--side-fg);
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--side-border);
  position: sticky;
  top: 0;
  height: 100vh;
  overflow: hidden;
}

/* ─── Sidebar — flat style (Slate Pro): solid bg, hairline border ─── */
[data-theme="slate-pro"] .sidebar {
  background: var(--side-bg-1);
  border-right: 1px solid var(--side-border);
}

/* ─── Sidebar — paper style (Editorial): cream with grain ─── */
[data-theme="editorial"] .sidebar {
  background:
    radial-gradient(circle at 0 0, rgba(255,255,255,0.6) 0, transparent 50%),
    linear-gradient(180deg, var(--side-bg-1), var(--side-bg-2));
  background-color: var(--side-bg-1);
  border-right: 1px solid var(--side-border);
}

/* ─── Sidebar — matrix style (Midnight): pure black + neon scan line ─── */
[data-theme="midnight"] .sidebar {
  background: var(--side-bg-1);
  border-right: 1px solid var(--side-border);
  position: sticky;
}
[data-theme="midnight"] .sidebar::after {
  content: ''; position: absolute; right: 0; top: 0; bottom: 0; width: 1px;
  background: linear-gradient(180deg, transparent, rgba(34,211,238,0.5), transparent);
  opacity: 0.4;
}

/* ─── Brand block ─── */
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 18px 20px;
  border-bottom: 1px solid var(--side-border);
  position: relative;
}
.brand-mark {
  width: 32px; height: 32px;
  border-radius: 9px;
  background: linear-gradient(135deg, var(--brand-grad-from), var(--brand-grad-to));
  display: grid; place-items: center;
  color: #fff; font-weight: 700; font-size: 13px;
  font-family: var(--f-display);
  box-shadow: 0 4px 14px color-mix(in srgb, var(--brand-grad-from) 35%, transparent);
}
[data-theme="slate-pro"] .brand-mark { border-radius: 6px; box-shadow: none; }
[data-theme="editorial"] .brand-mark {
  background: var(--fg);
  color: var(--bg-elevated);
  box-shadow: none;
  border-radius: 6px;
}
[data-theme="midnight"] .brand-mark {
  box-shadow: 0 0 14px var(--brand-500), 0 0 0 1px rgba(34,211,238,0.4);
}

.brand-name {
  font-family: var(--f-display);
  font-weight: var(--h-weight);
  color: var(--side-fg);
  font-size: 15px;
  letter-spacing: var(--h-tracking);
}
[data-theme="slate-pro"] .brand-name,
[data-theme="aurora"] .brand-name,
[data-theme="aurora-dark"] .brand-name,
[data-theme="midnight"] .brand-name { color: #fff; }
[data-theme="slate-pro"] .brand-name { color: var(--fg); }
[data-theme="editorial"] .brand-name { color: var(--fg); }

.brand-tagline { font-size: 11px; color: var(--side-fg-muted); margin-top: 2px; letter-spacing: 0.03em; }

/* ─── Nav ─── */
.nav { flex: 1; overflow-y: auto; padding: 12px 12px 24px; scrollbar-width: thin; scrollbar-color: rgba(255,255,255,0.1) transparent; }
.nav::-webkit-scrollbar { width: 6px; }
.nav::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 3px; }
[data-theme="slate-pro"] .nav::-webkit-scrollbar-thumb,
[data-theme="editorial"] .nav::-webkit-scrollbar-thumb { background: var(--border-strong); }

.nav-group { margin-top: 18px; }
.nav-group:first-child { margin-top: 4px; }
.nav-group-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--side-fg-muted);
  padding: 0 12px 8px;
  font-weight: 700;
}
[data-theme="editorial"] .nav-group-label {
  font-family: var(--f-display);
  text-transform: none;
  letter-spacing: 0;
  font-style: italic;
  font-weight: 500;
  font-size: 11px;
  opacity: 0.7;
}

.nav-link {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px;
  border-radius: var(--r-md);
  color: var(--side-fg);
  font-size: 13px;
  font-weight: 500;
  text-decoration: none;
  position: relative;
  transition: background var(--t-fast) var(--ease), color var(--t-fast) var(--ease), transform var(--t-fast) var(--ease);
}
.nav-link:hover { background: var(--side-hover-bg); color: var(--side-active-fg); text-decoration: none; }
.nav-link svg { width: 16px; height: 16px; flex: 0 0 auto; opacity: 0.85; }

.nav-link.active {
  background: var(--side-active-bg);
  color: var(--side-active-fg);
  font-weight: 600;
}
.nav-link.active svg { opacity: 1; }

/* Active "rule" — a left-edge bar in most themes, an underline in editorial */
.nav-link.active::before {
  content: '';
  position: absolute;
  left: -4px; top: 8px; bottom: 8px;
  width: 3px; border-radius: 2px;
  background: var(--side-active-rule);
}

[data-theme="editorial"] .nav-link.active::before {
  display: none;
}
[data-theme="editorial"] .nav-link.active {
  background: var(--side-active-bg);
  color: var(--side-active-fg);
}

[data-theme="midnight"] .nav-link.active {
  box-shadow: inset 0 0 0 1px rgba(34,211,238,0.25), 0 0 12px rgba(34,211,238,0.10);
}

[data-theme="slate-pro"] .nav-link {
  border-radius: 4px;
  padding: 8px 10px;
}
[data-theme="slate-pro"] .nav-link.active::before {
  background: var(--brand-600);
  width: 2px;
  left: -2px;
}

/* ─── Side foot (user widget) ─── */
.side-foot {
  padding: 12px 14px;
  border-top: 1px solid var(--side-border);
  display: flex; align-items: center; gap: 10px;
}
.avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent-cyan), var(--accent-indigo));
  color: #fff; font-weight: 600; font-size: 12px;
  display: grid; place-items: center;
  flex: 0 0 auto;
  font-family: var(--f-display);
}
[data-theme="slate-pro"] .avatar { border-radius: 6px; }
[data-theme="editorial"] .avatar {
  background: var(--fg);
  color: var(--bg-elevated);
  border-radius: 50%;
  font-family: var(--f-display);
  font-weight: 500;
}
[data-theme="midnight"] .avatar {
  box-shadow: 0 0 12px rgba(34,211,238,0.4);
}
.side-foot .who { font-size: 12px; color: var(--side-fg); line-height: 1.3; }
.side-foot .who strong { color: var(--side-active-fg); display: block; font-weight: 600; font-size: 13px; }
.logout-btn {
  background: transparent; border: 0; color: var(--side-fg-muted);
  padding: 6px; border-radius: 6px; margin-left: auto;
  transition: color var(--t-fast) var(--ease), background var(--t-fast) var(--ease);
}
.logout-btn:hover { color: var(--accent-rose); background: color-mix(in srgb, var(--accent-rose) 12%, transparent); }

/* ─── Top bar ─── */
.topbar {
  height: var(--top-h);
  display: flex; align-items: center;
  padding: 0 var(--sp-6);
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  gap: var(--sp-4);
  position: sticky; top: 0; z-index: 20;
  backdrop-filter: saturate(160%) blur(8px);
}
[data-theme="editorial"] .topbar { background: var(--bg); }
[data-theme="midnight"] .topbar { background: rgba(11,16,28,0.88); }

/* iOS Safari has bugs with backdrop-filter + position:sticky inside flex columns
   that can make the topbar render fully transparent. Force solid bg on mobile. */
@media (max-width: 760px) {
  .topbar { backdrop-filter: none; -webkit-backdrop-filter: none; background: var(--bg-elevated); }
  [data-theme="editorial"] .topbar { background: var(--bg-elevated); }
  [data-theme="midnight"] .topbar { background: var(--bg-elevated); }
}

.topbar h1 { font-size: 18px; }
.topbar .crumbs { color: var(--fg-subtle); font-size: 12px; margin-bottom: 2px; letter-spacing: 0.02em; }
[data-theme="editorial"] .topbar .crumbs { font-style: italic; }

.search-cmd {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 12px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  color: var(--fg-muted);
  font-family: var(--f-body);
  font-size: 13px;
  min-width: 280px;
  cursor: pointer;
  transition: border-color var(--t-fast) var(--ease), background var(--t-fast) var(--ease);
}
.search-cmd:hover { border-color: var(--border-strong); background: var(--bg-elevated); }
.search-cmd kbd {
  margin-left: auto;
  font-family: var(--f-mono);
  font-size: 11px;
  padding: 2px 6px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 5px;
  color: var(--fg-muted);
}

.theme-toggle, .icon-btn {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  width: 36px; height: 36px;
  border-radius: var(--r-md);
  display: grid; place-items: center;
  color: var(--fg-muted);
  transition: color var(--t-fast) var(--ease), background var(--t-fast) var(--ease), border-color var(--t-fast) var(--ease);
  position: relative;
}
.theme-toggle:hover, .icon-btn:hover { color: var(--fg); background: var(--bg-elevated); border-color: var(--border-strong); }

.company-pill {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 6px 12px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: var(--r-pill);
  font-size: 12px;
  color: var(--fg-muted);
}
.company-pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--brand-500); }
[data-theme="slate-pro"] .company-pill { border-radius: 4px; }

/* ─── Main ─── */
.main { min-width: 0; display: flex; flex-direction: column; }
.page { padding: var(--sp-6) var(--sp-8); flex: 1; width: 100%; margin: 0; }
.page-header { display: flex; align-items: flex-end; justify-content: space-between; gap: var(--sp-4); margin-bottom: var(--sp-6); flex-wrap: wrap; }
.page-header h1 { font-size: 26px; }
.page-header .sub { color: var(--fg-muted); font-size: 13px; margin-top: 4px; max-width: 60ch; }
[data-theme="editorial"] .page-header h1 { font-size: 32px; }
[data-theme="editorial"] .page-header .sub { font-style: italic; font-size: 14px; }

@media (max-width: 1100px) {
  .app { grid-template-columns: var(--side-w-rail) 1fr; }
  .sidebar.expanded { position: fixed; width: var(--side-w); z-index: 50; }
  .sidebar:not(.expanded) .brand-name, .sidebar:not(.expanded) .brand-tagline,
  .sidebar:not(.expanded) .nav-group-label, .sidebar:not(.expanded) .nav-link span,
  .sidebar:not(.expanded) .side-foot .who, .sidebar:not(.expanded) .logout-btn { display: none; }
  .sidebar:not(.expanded) .nav-link { justify-content: center; padding: 10px; }
  .sidebar:not(.expanded) .brand { justify-content: center; padding: 18px 0; }
}
@media (max-width: 760px) {
  /* Drop grid on mobile: iOS Safari sometimes treats a position:fixed grid
     item as if it still occupies a row, which pushes .main 100vh down. */
  .app { display: block; }
  .sidebar { position: fixed; top: 0; left: 0; transform: translateX(-100%); width: var(--side-w); height: 100vh; z-index: 50; transition: transform var(--t-med) var(--ease); }
  .sidebar.expanded { transform: translateX(0); }
  .sidebar.expanded .brand-name, .sidebar.expanded .brand-tagline,
  .sidebar.expanded .nav-group-label, .sidebar.expanded .nav-link span,
  .sidebar.expanded .side-foot .who, .sidebar.expanded .logout-btn { display: initial; }
  .sidebar.expanded .nav-link { justify-content: flex-start; padding: 9px 12px; }
  .menu-toggle { display: grid !important; }
  .page { padding: var(--sp-4); }

  /* Compact topbar so the row actually fits the viewport */
  .topbar { padding: 0 12px; gap: 8px; }
  .topbar h1 { font-size: 15px; line-height: 1.25; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 55vw; }
  .topbar .crumbs { font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 55vw; }
  .topbar .spacer { display: none; }

  /* Title wrapper (the unclassed <div> holding crumbs+h1) — let it shrink */
  .topbar > div:not(.theme-picker):not(.spacer) { min-width: 0; }

  /* Search becomes an icon-only button */
  .search-cmd { min-width: 0; width: 36px; height: 36px; padding: 0; justify-content: center; flex: 0 0 auto; }
  .search-cmd span, .search-cmd kbd { display: none; }

  /* Theme picker collapses to its swatch */
  .theme-picker > button { padding: 0; width: 36px; height: 36px; justify-content: center; }
  .theme-picker > button [data-theme-label], .theme-picker > button > svg { display: none; }
  .theme-picker .menu { right: -8px; min-width: 220px; }

  /* Company pill: dot only */
  .company-pill { padding: 0; width: 28px; height: 28px; justify-content: center; gap: 0; border-radius: 50%; }
  .company-pill > :not(.dot) { display: none; }

  /* Tables inside cards must scroll, not break out */
  .card-body { overflow-x: auto; }

  /* Flash toasts: full-width strip near top */
  .flash-stack { top: 70px; right: 12px; left: 12px; }
  .flash { min-width: 0; max-width: none; }
}

@media (max-width: 480px) {
  .topbar .crumbs { display: none; }
  .topbar h1 { font-size: 14px; max-width: 60vw; }
  .company-pill { display: none; }
  .page-header h1 { font-size: 20px; }
  .page-header { gap: 8px; margin-bottom: var(--sp-4); }
}

.menu-toggle { display: none; }

.side-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.35); z-index: 40; display: none; }
.side-backdrop.show { display: block; }
