/* ══════════════════════════════════════════════════════════════════════
   PulseAI v2 Design Tokens
   ----------------------------------------------------------------------
   Single source of truth for colors, spacing, typography. Every page
   below MUST consume these tokens — no hardcoded colors, no arbitrary
   spacing. Old `--background` / `--card` / `--primary` tokens are kept
   alive as ALIASES so legacy components don't break.
   ══════════════════════════════════════════════════════════════════════ */

:root {
  /* ── Surfaces ────────────────────────────────────────────── */
  --bg-base: 250 22% 5%;          /* near-black (existing) */
  --bg-card: 250 24% 8%;          /* one shade lighter */
  --bg-card-hover: 250 24% 11%;   /* two shades lighter */
  --bg-elevated: 250 24% 13%;     /* drawers / popovers */

  --border-subtle: 250 12% 100% / 0.08;     /* low-opacity white */
  --border-default: 250 12% 100% / 0.12;
  --border-strong:  250 12% 100% / 0.20;

  /* ── Text ────────────────────────────────────────────────── */
  --text-primary:   250 12% 100% / 0.96;    /* white */
  --text-secondary: 250 10% 64%;            /* ~#9ca3af */
  --text-tertiary:  250 8%  44%;            /* ~#6b7280 */
  --text-disabled:  250 8%  30%;

  /* ── Accents (the only chromatic colors allowed) ─────────── */
  --accent-primary:        270 85% 65%;     /* purple — CTAs, active states */
  --accent-primary-soft:   270 85% 65% / 0.16;
  --accent-primary-glow:   270 85% 65% / 0.25;

  --accent-secondary:      195 90% 55%;     /* teal — top border, focus rings, hover */
  --accent-secondary-soft: 195 90% 55% / 0.16;

  /* ── Semantic (status only — never decorative) ───────────── */
  --success: 142 71% 45%;
  --warning:  38 92% 50%;
  --error:    0 84% 60%;
  --info:    270 85% 65%;

  /* ── Spacing (strict 8px grid) ───────────────────────────── */
  --space-1:  4px;    /* hairline only — internal icon gaps */
  --space-2:  8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-6: 24px;
  --space-8: 32px;
  --space-12: 48px;
  --space-16: 64px;

  /* ── Radii ───────────────────────────────────────────────── */
  --radius-xs: 6px;
  --radius-sm: 8px;
  --radius:    12px;     /* default cards */
  --radius-lg: 16px;
  --radius-pill: 999px;

  /* ── Typography ──────────────────────────────────────────── */
  --font-family: 'Inter', -apple-system, system-ui, sans-serif;

  --fs-h1: 28px;          --lh-h1: 36px;          --fw-h1: 700;
  --fs-h2: 22px;          --lh-h2: 30px;          --fw-h2: 700;
  --fs-h3: 16px;          --lh-h3: 24px;          --fw-h3: 600;
  --fs-body: 14px;        --lh-body: 22px;        --fw-body: 400;
  --fs-caption: 13px;     --lh-caption: 20px;     --fw-caption: 400;
  --fs-section: 11px;     --lh-section: 16px;     --fw-section: 600;
  --tracking-section: 0.08em;

  /* ── Shadows ─────────────────────────────────────────────── */
  --shadow-sm:   0 1px 2px hsl(0 0% 0% / 0.20);
  --shadow-md:   0 4px 12px hsl(0 0% 0% / 0.25);
  --shadow-lg:   0 12px 32px hsl(0 0% 0% / 0.35);
  --shadow-glow: 0 0 0 1px hsl(var(--accent-primary) / 0.30),
                 0 8px 32px hsl(var(--accent-primary) / 0.20);

  /* ── Motion ──────────────────────────────────────────────── */
  --ease: cubic-bezier(0.4, 0, 0.2, 1);
  --dur-fast: 120ms;
  --dur-base: 180ms;
  --dur-slow: 260ms;

  /* ── Layout ──────────────────────────────────────────────── */
  --content-max-width: 1280px;
  --sidebar-width: 248px;
  --sidebar-width-collapsed: 64px;
  --header-height: 56px;

  /* ──────────────────────────────────────────────────────────
     LEGACY ALIASES (do not reference in new code — kept so old
     pages don't shatter while we migrate)
     ────────────────────────────────────────────────────────── */
  --background: var(--bg-base);
  --foreground: var(--text-primary);
  --card: var(--bg-card);
  --card-foreground: var(--text-primary);
  --popover: var(--bg-elevated);
  --popover-foreground: var(--text-primary);
  --primary: var(--accent-primary);
  --primary-foreground: 0 0% 100%;
  --secondary: var(--accent-secondary);
  --secondary-foreground: 0 0% 100%;
  --muted: 250 20% 12%;
  --muted-foreground: var(--text-secondary);
  --accent: var(--accent-secondary);
  --accent-foreground: 0 0% 100%;
  --destructive: var(--error);
  --destructive-foreground: 0 0% 100%;
  --border: var(--border-default);
  --input: var(--border-default);
  --ring: var(--accent-primary);
  --success-foreground: 0 0% 100%;
  --warning-foreground: 0 0% 10%;
  --info-foreground: 0 0% 100%;
  --shadow-card: var(--shadow-md);
  --gradient-primary: linear-gradient(135deg, hsl(var(--accent-primary)), hsl(280 85% 70%));
  --score-high: var(--success);
  --score-medium: var(--warning);
  --score-low: var(--error);
}

/* ── Light mode (semantic flip — keeps token names) ─────────── */
.light, [data-theme="light"] {
  --bg-base: 260 30% 98%;
  --bg-card: 0 0% 100%;
  --bg-card-hover: 250 22% 96%;
  --bg-elevated: 0 0% 100%;

  --border-subtle:  250 22% 10% / 0.08;
  --border-default: 250 22% 10% / 0.14;
  --border-strong:  250 22% 10% / 0.24;

  --text-primary:   250 22% 12%;
  --text-secondary: 250 12% 38%;
  --text-tertiary:  250 10% 52%;
  --text-disabled:  250 10% 70%;

  --accent-primary:      270 80% 56%;
  --accent-primary-soft: 270 80% 56% / 0.10;
  --accent-primary-glow: 270 80% 56% / 0.18;
  --accent-secondary:    195 85% 45%;
  --accent-secondary-soft: 195 85% 45% / 0.10;

  --shadow-sm: 0 1px 2px hsl(250 22% 10% / 0.06);
  --shadow-md: 0 4px 12px hsl(250 22% 10% / 0.08);
  --shadow-lg: 0 12px 32px hsl(250 22% 10% / 0.12);
}

/* ── v2 utility classes (used by new components only) ───────── */
.v2 {
  /* Resets */
  font-family: var(--font-family);
  font-size: var(--fs-body);
  line-height: var(--lh-body);
  color: hsl(var(--text-primary));
}

.v2-text-secondary { color: hsl(var(--text-secondary)); }
.v2-text-tertiary  { color: hsl(var(--text-tertiary)); }

/* Section labels (sidebar group titles, etc) */
.v2-section-label {
  font-size: var(--fs-section);
  font-weight: var(--fw-section);
  letter-spacing: var(--tracking-section);
  text-transform: uppercase;
  color: hsl(var(--text-tertiary));
  line-height: var(--lh-section);
}

/* Headings */
.v2-h1 { font-size: var(--fs-h1); line-height: var(--lh-h1); font-weight: var(--fw-h1); color: hsl(var(--text-primary)); margin: 0; }
.v2-h2 { font-size: var(--fs-h2); line-height: var(--lh-h2); font-weight: var(--fw-h2); color: hsl(var(--text-primary)); margin: 0; }
.v2-h3 { font-size: var(--fs-h3); line-height: var(--lh-h3); font-weight: var(--fw-h3); color: hsl(var(--text-primary)); margin: 0; }
.v2-caption { font-size: var(--fs-caption); line-height: var(--lh-caption); color: hsl(var(--text-secondary)); }

/* ── PageHeader ─────────────────────────────────────────────── */
.v2-page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
  flex-wrap: wrap;
  padding: var(--space-8) 0 var(--space-6);
  border-bottom: 1px solid hsl(var(--border-subtle));
  margin-bottom: var(--space-8);
}
.v2-page-header__main { display: flex; flex-direction: column; gap: var(--space-2); min-width: 0; }
.v2-page-header__title { font-size: var(--fs-h2); line-height: var(--lh-h2); font-weight: var(--fw-h2); color: hsl(var(--text-primary)); margin: 0; }
.v2-page-header__subtitle { font-size: var(--fs-caption); color: hsl(var(--text-secondary)); margin: 0; }
.v2-page-header__actions { display: flex; gap: var(--space-3); align-items: center; flex-shrink: 0; }

/* ── Card ───────────────────────────────────────────────────── */
.v2-card {
  background: hsl(var(--bg-card));
  border: 1px solid hsl(var(--border-subtle));
  border-radius: var(--radius);
  padding: var(--space-6);
  transition: border-color var(--dur-base) var(--ease), background var(--dur-base) var(--ease);
}
.v2-card:hover { border-color: hsl(var(--border-default)); }
.v2-card--interactive { cursor: pointer; }
.v2-card--interactive:hover { background: hsl(var(--bg-card-hover)); }
.v2-card--flat { background: transparent; border: 1px solid hsl(var(--border-subtle)); }
.v2-card__header { display: flex; justify-content: space-between; align-items: flex-start; gap: var(--space-3); margin-bottom: var(--space-4); }
.v2-card__title { font-size: var(--fs-h3); font-weight: var(--fw-h3); color: hsl(var(--text-primary)); margin: 0; }
.v2-card__subtitle { font-size: var(--fs-caption); color: hsl(var(--text-secondary)); margin: 0; }

/* ── StatCard ───────────────────────────────────────────────── */
.v2-stat {
  background: hsl(var(--bg-card));
  border: 1px solid hsl(var(--border-subtle));
  border-radius: var(--radius);
  padding: var(--space-6);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  transition: border-color var(--dur-base) var(--ease), background var(--dur-base) var(--ease);
}
.v2-stat:hover { border-color: hsl(var(--border-default)); background: hsl(var(--bg-card-hover)); }
.v2-stat__row { display: flex; align-items: center; justify-content: space-between; }
.v2-stat__icon {
  width: 36px; height: 36px;
  border-radius: var(--radius-sm);
  background: hsl(var(--accent-primary-soft));
  color: hsl(var(--accent-primary));
  display: flex; align-items: center; justify-content: center;
}
.v2-stat__label { font-size: var(--fs-caption); color: hsl(var(--text-secondary)); margin: 0; }
.v2-stat__value { font-size: var(--fs-h1); line-height: 1.1; font-weight: var(--fw-h1); color: hsl(var(--text-primary)); margin: 0; letter-spacing: -0.01em; }
.v2-stat__delta { display: inline-flex; align-items: center; gap: 4px; font-size: var(--fs-caption); color: hsl(var(--text-secondary)); }
.v2-stat__delta--positive { color: hsl(var(--success)); }
.v2-stat__delta--negative { color: hsl(var(--error)); }
.v2-stat--empty .v2-stat__value { font-size: var(--fs-h3); font-weight: var(--fw-h3); color: hsl(var(--text-secondary)); }
.v2-stat__empty-cta {
  font-size: var(--fs-caption);
  color: hsl(var(--accent-primary));
  text-decoration: none;
  display: inline-flex; align-items: center; gap: var(--space-1);
}
.v2-stat__empty-cta:hover { text-decoration: underline; }

/* ── Buttons ────────────────────────────────────────────────── */
.v2-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  padding: 0 var(--space-4); height: 36px;
  border-radius: var(--radius-sm);
  font-family: var(--font-family);
  font-size: var(--fs-body); font-weight: 500;
  border: 1px solid transparent;
  cursor: pointer;
  transition: background var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
  white-space: nowrap;
  text-decoration: none;
}
.v2-btn:active:not(:disabled) { transform: translateY(0.5px); }
.v2-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.v2-btn--sm { height: 30px; font-size: var(--fs-caption); padding: 0 var(--space-3); }
.v2-btn--lg { height: 44px; padding: 0 var(--space-6); font-size: var(--fs-h3); }

.v2-btn--primary {
  background: hsl(var(--accent-primary));
  color: white;
  box-shadow: 0 0 0 1px hsl(var(--accent-primary) / 0.5), 0 4px 16px hsl(var(--accent-primary) / 0.25);
}
.v2-btn--primary:hover:not(:disabled) { background: hsl(270 85% 70%); box-shadow: var(--shadow-glow); }

.v2-btn--gradient {
  background: linear-gradient(135deg, hsl(var(--accent-primary)) 0%, hsl(280 85% 68%) 100%);
  color: white;
  box-shadow: 0 0 0 1px hsl(var(--accent-primary) / 0.5), 0 4px 24px hsl(var(--accent-primary) / 0.32);
  position: relative;
}
.v2-btn--gradient::before {
  content: ''; position: absolute; inset: 0; border-radius: inherit;
  background: radial-gradient(120% 120% at 50% -20%, hsl(var(--accent-secondary) / 0.4), transparent 60%);
  opacity: 0; transition: opacity var(--dur-base) var(--ease); pointer-events: none;
}
.v2-btn--gradient:hover::before { opacity: 1; }

.v2-btn--secondary {
  background: hsl(var(--bg-card));
  color: hsl(var(--text-primary));
  border-color: hsl(var(--border-default));
}
.v2-btn--secondary:hover:not(:disabled) { background: hsl(var(--bg-card-hover)); border-color: hsl(var(--border-strong)); }

.v2-btn--ghost {
  background: transparent;
  color: hsl(var(--text-secondary));
}
.v2-btn--ghost:hover:not(:disabled) { background: hsl(var(--bg-card-hover)); color: hsl(var(--text-primary)); }

.v2-btn--icon {
  width: 36px; padding: 0;
}

/* ── Badge ──────────────────────────────────────────────────── */
.v2-badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 10px;
  font-size: var(--fs-section); font-weight: 600;
  border-radius: var(--radius-pill);
  letter-spacing: 0.02em;
  background: hsl(var(--bg-card-hover));
  color: hsl(var(--text-secondary));
  border: 1px solid hsl(var(--border-subtle));
  white-space: nowrap;
}
.v2-badge--primary { background: hsl(var(--accent-primary-soft)); color: hsl(var(--accent-primary)); border-color: hsl(var(--accent-primary) / 0.25); }
.v2-badge--success { background: hsl(var(--success) / 0.14); color: hsl(var(--success)); border-color: hsl(var(--success) / 0.25); }
.v2-badge--warning { background: hsl(var(--warning) / 0.14); color: hsl(var(--warning)); border-color: hsl(var(--warning) / 0.25); }
.v2-badge--error   { background: hsl(var(--error) / 0.14);   color: hsl(var(--error));   border-color: hsl(var(--error) / 0.25); }
.v2-badge--founder { background: linear-gradient(135deg, hsl(var(--accent-primary) / 0.16), hsl(var(--accent-secondary) / 0.16)); color: hsl(var(--accent-primary)); border: 1px solid hsl(var(--accent-primary) / 0.30); }

/* ── DataTable ──────────────────────────────────────────────── */
.v2-table-wrap { overflow-x: auto; border: 1px solid hsl(var(--border-subtle)); border-radius: var(--radius); background: hsl(var(--bg-card)); }
.v2-table { width: 100%; border-collapse: collapse; font-size: var(--fs-body); }
.v2-table thead th {
  text-align: left;
  padding: var(--space-3) var(--space-4);
  font-size: var(--fs-caption); font-weight: 600;
  color: hsl(var(--text-secondary));
  background: hsl(var(--bg-card-hover) / 0.5);
  border-bottom: 1px solid hsl(var(--border-subtle));
  position: sticky; top: 0;
}
.v2-table thead th[data-sortable] { cursor: pointer; user-select: none; }
.v2-table thead th[data-sortable]:hover { color: hsl(var(--text-primary)); }
.v2-table tbody td { padding: var(--space-4); border-top: 1px solid hsl(var(--border-subtle)); color: hsl(var(--text-primary)); }
.v2-table tbody tr { transition: background var(--dur-fast) var(--ease); }
.v2-table tbody tr:hover { background: hsl(var(--bg-card-hover)); }
.v2-table tbody tr[data-clickable] { cursor: pointer; }
.v2-table__pagination { display: flex; justify-content: space-between; align-items: center; padding: var(--space-3) var(--space-4); border-top: 1px solid hsl(var(--border-subtle)); font-size: var(--fs-caption); color: hsl(var(--text-secondary)); }

/* ── EmptyState ─────────────────────────────────────────────── */
.v2-empty {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center;
  padding: var(--space-16) var(--space-8);
  gap: var(--space-4);
  background: hsl(var(--bg-card));
  border: 1px dashed hsl(var(--border-default));
  border-radius: var(--radius);
}
.v2-empty__icon {
  width: 56px; height: 56px;
  border-radius: var(--radius);
  background: hsl(var(--accent-primary-soft));
  color: hsl(var(--accent-primary));
  display: flex; align-items: center; justify-content: center;
}
.v2-empty__title { font-size: var(--fs-h3); font-weight: var(--fw-h3); color: hsl(var(--text-primary)); margin: 0; }
.v2-empty__desc { font-size: var(--fs-caption); color: hsl(var(--text-secondary)); margin: 0; max-width: 420px; }

/* ── LoadingSkeleton ────────────────────────────────────────── */
@keyframes v2-shimmer {
  0%   { background-position: -200% 0; }
  100% { background-position: 200% 0; }
}
.v2-skeleton {
  background: linear-gradient(
    90deg,
    hsl(var(--bg-card-hover)) 0%,
    hsl(var(--bg-elevated)) 50%,
    hsl(var(--bg-card-hover)) 100%
  );
  background-size: 200% 100%;
  animation: v2-shimmer 1.4s linear infinite;
  border-radius: var(--radius-sm);
}
.v2-skeleton--text { height: 14px; width: 100%; border-radius: 4px; }
.v2-skeleton--text-sm { height: 12px; width: 60%; border-radius: 4px; }
.v2-skeleton--title { height: 20px; width: 40%; }
.v2-skeleton--card { height: 88px; }
.v2-skeleton--row { height: 44px; margin-bottom: var(--space-2); }

/* ── Tabs ────────────────────────────────────────────────────── */
.v2-tabs { display: flex; gap: var(--space-1); border-bottom: 1px solid hsl(var(--border-subtle)); margin-bottom: var(--space-6); }
.v2-tab {
  padding: var(--space-3) var(--space-4);
  font-size: var(--fs-body); font-weight: 500;
  color: hsl(var(--text-secondary));
  background: transparent; border: none; cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: color var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease);
}
.v2-tab:hover { color: hsl(var(--text-primary)); }
.v2-tab--active { color: hsl(var(--text-primary)); border-bottom-color: hsl(var(--accent-primary)); }

/* ── Modal / Drawer ─────────────────────────────────────────── */
.v2-modal-backdrop {
  position: fixed; inset: 0;
  background: hsl(var(--bg-base) / 0.7);
  backdrop-filter: blur(8px);
  z-index: 1000;
  display: flex; align-items: center; justify-content: center;
  animation: v2-fade-in var(--dur-base) var(--ease);
}
@keyframes v2-fade-in { from { opacity: 0 } to { opacity: 1 } }
.v2-modal {
  background: hsl(var(--bg-elevated));
  border: 1px solid hsl(var(--border-default));
  border-radius: var(--radius-lg);
  padding: var(--space-8);
  width: min(560px, 90vw);
  max-height: 90vh; overflow-y: auto;
  box-shadow: var(--shadow-lg);
}
.v2-drawer {
  position: fixed; top: 0; right: 0; bottom: 0;
  width: min(560px, 95vw);
  background: hsl(var(--bg-elevated));
  border-left: 1px solid hsl(var(--border-default));
  z-index: 1001;
  overflow-y: auto;
  animation: v2-slide-in var(--dur-slow) var(--ease);
}
@keyframes v2-slide-in { from { transform: translateX(100%) } to { transform: translateX(0) } }

/* ── Inputs (used in PageHeader filters etc.) ───────────────── */
.v2-input, .v2-select {
  background: hsl(var(--bg-card));
  border: 1px solid hsl(var(--border-default));
  color: hsl(var(--text-primary));
  border-radius: var(--radius-sm);
  padding: 0 var(--space-3);
  height: 36px;
  font-family: var(--font-family);
  font-size: var(--fs-body);
  transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}
.v2-input:focus, .v2-select:focus { outline: none; border-color: hsl(var(--accent-primary)); box-shadow: 0 0 0 3px hsl(var(--accent-primary) / 0.16); }

/* ── Layout helpers ─────────────────────────────────────────── */
.v2-page { max-width: var(--content-max-width); margin: 0 auto; padding: 0 var(--space-8) var(--space-12); }
.v2-stack { display: flex; flex-direction: column; }
.v2-row { display: flex; align-items: center; }
/* Grids: minmax(0, 1fr) lets columns actually shrink below min-content
   (otherwise large cards or charts blow the grid past its container). */
.v2-grid { display: grid; gap: var(--space-4); min-width: 0; }
.v2-grid > * { min-width: 0; }
.v2-grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.v2-grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.v2-grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.v2-grid-6040 { grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); }

@media (max-width: 960px) {
  .v2-grid-4, .v2-grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .v2-grid-2, .v2-grid-6040 { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 600px) {
  .v2-grid-4, .v2-grid-3, .v2-grid-2 { grid-template-columns: minmax(0, 1fr); }
}

/* ══════════════════════════════════════════════════════════════════════
   v2 Sidebar
   ══════════════════════════════════════════════════════════════════════ */
.v2-sidebar {
  position: fixed; top: 0; left: 0; bottom: 0;
  width: var(--sidebar-width);
  background: hsl(var(--bg-base));
  border-right: 1px solid hsl(var(--border-subtle));
  display: flex; flex-direction: column;
  z-index: 30;
  transition: width var(--dur-base) var(--ease), transform var(--dur-base) var(--ease);
  overflow-x: hidden;
}
.v2-sidebar::before {
  /* subtle teal hairline at top per spec — accent-secondary used only here + focus rings + hover */
  content: ''; position: absolute; top: 0; left: 0; right: 0; height: 1px;
  background: linear-gradient(90deg, transparent, hsl(var(--accent-secondary) / 0.5), transparent);
  pointer-events: none;
}
.v2-sidebar.collapsed { width: var(--sidebar-width-collapsed); }

.v2-sidebar__header {
  padding: var(--space-4); display: flex; align-items: center; gap: var(--space-3);
  border-bottom: 1px solid hsl(var(--border-subtle));
  height: var(--header-height); flex-shrink: 0;
}
.v2-sidebar__logo {
  width: 32px; height: 32px; border-radius: var(--radius-sm);
  background: linear-gradient(135deg, hsl(var(--accent-primary)), hsl(280 85% 70%));
  display: flex; align-items: center; justify-content: center;
  color: white; flex-shrink: 0;
}
.v2-sidebar__logo-text {
  font-size: var(--fs-h3); font-weight: 700; color: hsl(var(--text-primary));
  white-space: nowrap;
}
.v2-sidebar.collapsed .v2-sidebar__logo-text,
.v2-sidebar.collapsed .v2-sidebar__group-label,
.v2-sidebar.collapsed .v2-sidebar__item-label,
.v2-sidebar.collapsed .v2-sidebar__group-chevron { display: none; }

.v2-sidebar__nav {
  flex: 1; overflow-y: auto; overflow-x: hidden;
  padding: var(--space-3) var(--space-2);
  display: flex; flex-direction: column;
}
.v2-sidebar__nav::-webkit-scrollbar { width: 4px; }
.v2-sidebar__nav::-webkit-scrollbar-thumb { background: hsl(var(--border-default)); border-radius: 2px; }

.v2-sidebar__group {
  display: flex; flex-direction: column;
  margin-top: var(--space-3);
}
.v2-sidebar__group:first-child { margin-top: 0; }
.v2-sidebar__group-label {
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--space-2) var(--space-3);
  font-size: var(--fs-section); font-weight: var(--fw-section);
  letter-spacing: var(--tracking-section);
  text-transform: uppercase;
  color: hsl(var(--text-tertiary));
  background: transparent; border: none; cursor: pointer; width: 100%; text-align: left;
  transition: color var(--dur-fast) var(--ease);
}
.v2-sidebar__group-label:hover { color: hsl(var(--text-secondary)); }
.v2-sidebar__group-chevron { display: flex; align-items: center; transition: transform var(--dur-base) var(--ease); }
.v2-sidebar__group--open .v2-sidebar__group-chevron { transform: rotate(90deg); }
.v2-sidebar__group-items {
  display: flex; flex-direction: column; gap: 1px;
  max-height: 0; overflow: hidden;
  transition: max-height var(--dur-slow) var(--ease);
}
.v2-sidebar__group--open .v2-sidebar__group-items { max-height: 800px; }

.v2-sidebar__item {
  display: flex; align-items: center; gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  color: hsl(var(--text-secondary));
  background: transparent; border: 1px solid transparent;
  cursor: pointer; width: 100%; text-align: left;
  font-size: var(--fs-body); font-weight: 500;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease);
  white-space: nowrap;
}
.v2-sidebar__item:hover {
  background: hsl(var(--accent-secondary-soft));
  color: hsl(var(--text-primary));
}
.v2-sidebar__item--active {
  background: hsl(var(--accent-primary-soft));
  color: hsl(var(--accent-primary));
  border-color: hsl(var(--accent-primary) / 0.20);
}
.v2-sidebar__item--active:hover {
  background: hsl(var(--accent-primary-soft));
  color: hsl(var(--accent-primary));
}
.v2-sidebar__item-icon { display: flex; align-items: center; flex-shrink: 0; width: 20px; }
.v2-sidebar__item-label { overflow: hidden; text-overflow: ellipsis; }

.v2-sidebar__pinned {
  margin-bottom: var(--space-3);
  padding-bottom: var(--space-3);
  border-bottom: 1px solid hsl(var(--border-subtle));
}

.v2-sidebar__footer {
  padding: var(--space-3); display: flex; align-items: center; gap: var(--space-2);
  border-top: 1px solid hsl(var(--border-subtle));
  flex-shrink: 0;
}
.v2-sidebar__collapse-btn {
  margin-left: auto;
  width: 28px; height: 28px;
  border-radius: var(--radius-sm);
  background: transparent; border: 1px solid hsl(var(--border-subtle));
  color: hsl(var(--text-secondary));
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: all var(--dur-fast) var(--ease);
}
.v2-sidebar__collapse-btn:hover { background: hsl(var(--bg-card-hover)); color: hsl(var(--text-primary)); }

/* ══════════════════════════════════════════════════════════════════════
   v2 App Header (top bar)
   ══════════════════════════════════════════════════════════════════════ */
.v2-app-header {
  position: sticky; top: 0; z-index: 20;
  height: var(--header-height);
  background: hsl(var(--bg-base) / 0.85);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid hsl(var(--border-subtle));
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 var(--space-6);
  gap: var(--space-4);
}
.v2-app-header__left { display: flex; align-items: center; gap: var(--space-3); min-width: 0; flex: 1; }
.v2-app-header__right { display: flex; align-items: center; gap: var(--space-2); flex-shrink: 0; }

.v2-search {
  position: relative;
  max-width: 360px; width: 100%;
}
.v2-search__input {
  width: 100%;
  background: hsl(var(--bg-card));
  border: 1px solid hsl(var(--border-subtle));
  color: hsl(var(--text-primary));
  border-radius: var(--radius-sm);
  height: 36px; padding: 0 var(--space-3) 0 36px;
  font-family: var(--font-family); font-size: var(--fs-body);
  transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}
.v2-search__input::placeholder { color: hsl(var(--text-tertiary)); }
.v2-search__input:focus { outline: none; border-color: hsl(var(--accent-secondary)); box-shadow: 0 0 0 3px hsl(var(--accent-secondary) / 0.16); }
.v2-search__icon {
  position: absolute; left: 12px; top: 50%; transform: translateY(-50%);
  color: hsl(var(--text-tertiary)); pointer-events: none; display: flex;
}

/* Command-palette dropdown that opens below the input */
.v2-search__panel {
  position: absolute; top: calc(100% + 6px); left: 0; right: 0;
  background: hsl(var(--bg-elevated));
  border: 1px solid hsl(var(--border-default));
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  z-index: 50;
  max-height: 420px; overflow-y: auto;
  padding: var(--space-2);
  animation: v2-fade-in var(--dur-fast) var(--ease);
}
.v2-search__result {
  display: flex; align-items: center; gap: var(--space-3);
  width: 100%; padding: var(--space-2) var(--space-3);
  background: transparent; border: 1px solid transparent;
  border-radius: var(--radius-sm);
  cursor: pointer; text-align: left;
  font-size: var(--fs-body); color: hsl(var(--text-primary));
  transition: background var(--dur-fast) var(--ease);
}
.v2-search__result:hover,
.v2-search__result--active {
  background: hsl(var(--accent-primary-soft));
  border-color: hsl(var(--accent-primary) / 0.2);
}
.v2-search__result-icon {
  width: 20px; flex-shrink: 0; color: hsl(var(--text-secondary)); display: flex;
}
.v2-search__result--active .v2-search__result-icon { color: hsl(var(--accent-primary)); }
.v2-search__result-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.v2-search__result-section {
  font-size: var(--fs-caption); color: hsl(var(--text-tertiary));
  padding: 2px 8px; border-radius: var(--radius-pill);
  background: hsl(var(--bg-card-hover));
  flex-shrink: 0;
}
.v2-search__empty {
  padding: var(--space-6); text-align: center;
  color: hsl(var(--text-secondary)); display: flex; flex-direction: column; align-items: center; gap: var(--space-2);
}
.v2-search__hint {
  display: flex; gap: var(--space-4); justify-content: center;
  padding: var(--space-2) var(--space-3); margin-top: var(--space-2);
  border-top: 1px solid hsl(var(--border-subtle));
  font-size: var(--fs-caption); color: hsl(var(--text-tertiary));
}
.v2-search__hint kbd {
  display: inline-block;
  padding: 1px 6px; margin-right: 3px;
  background: hsl(var(--bg-card));
  border: 1px solid hsl(var(--border-subtle));
  border-radius: 4px;
  font-family: var(--font-family);
  font-size: 10px; color: hsl(var(--text-secondary));
}
.v2-search { position: relative; } /* ensure panel anchors below */

/* User-menu avatar trigger */
.v2-user-menu { position: relative; }
.v2-user-menu__trigger {
  width: 36px; height: 36px; border-radius: 50%;
  background: linear-gradient(135deg, hsl(var(--accent-primary)), hsl(280 85% 70%));
  color: white; font-weight: 600; font-size: 13px;
  border: 1px solid hsl(var(--border-subtle));
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: box-shadow var(--dur-fast) var(--ease);
}
.v2-user-menu__trigger:hover { box-shadow: 0 0 0 3px hsl(var(--accent-primary) / 0.20); }
.v2-user-menu__panel {
  position: absolute; top: calc(100% + 8px); right: 0;
  width: 240px;
  background: hsl(var(--bg-elevated));
  border: 1px solid hsl(var(--border-default));
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: var(--space-3);
  z-index: 100;
  animation: v2-fade-in var(--dur-fast) var(--ease);
}
.v2-user-menu__head {
  display: flex; gap: var(--space-3); align-items: center;
  padding: var(--space-3); border-radius: var(--radius-sm);
  background: hsl(var(--bg-card));
  margin-bottom: var(--space-2);
}
.v2-user-menu__head-name { font-size: var(--fs-body); font-weight: 600; color: hsl(var(--text-primary)); margin: 0; line-height: 1.2; }
.v2-user-menu__head-email { font-size: var(--fs-caption); color: hsl(var(--text-secondary)); margin: 2px 0 0; line-height: 1.2; word-break: break-all; }
.v2-user-menu__divider { height: 1px; background: hsl(var(--border-subtle)); margin: var(--space-2) 0; }
.v2-user-menu__item {
  display: flex; align-items: center; gap: var(--space-3);
  padding: var(--space-2) var(--space-3); width: 100%;
  background: transparent; border: none; cursor: pointer;
  border-radius: var(--radius-sm); color: hsl(var(--text-primary));
  font-size: var(--fs-body); text-align: left;
  transition: background var(--dur-fast) var(--ease);
}
.v2-user-menu__item:hover { background: hsl(var(--bg-card-hover)); }
.v2-user-menu__item--danger { color: hsl(var(--error)); }

/* Container shift to make room for sidebar.
   Body uses display:flex (#app) so the shell is a flex child — it must
   explicitly grow + clamp to prevent its content (esp. wide cards/tables)
   from blowing out the viewport horizontally. */
.v2-app-shell {
  display: flex; flex-direction: column;
  flex: 1 1 auto;
  min-width: 0;            /* lets nested overflow:auto containers actually scroll */
  margin-left: var(--sidebar-width);
  width: calc(100% - var(--sidebar-width));
  max-width: calc(100vw - var(--sidebar-width));
  min-height: 100vh;
  transition: margin-left var(--dur-base) var(--ease), width var(--dur-base) var(--ease), max-width var(--dur-base) var(--ease);
}
.v2-app-shell.sidebar-collapsed {
  margin-left: var(--sidebar-width-collapsed);
  width: calc(100% - var(--sidebar-width-collapsed));
  max-width: calc(100vw - var(--sidebar-width-collapsed));
}
/* Page content lives inside .v2-app-shell — must respect its width and let
   internal scroll containers (tables, charts) scroll inside, not push out. */
.v2-app-shell #page-content,
.v2-app-shell .page-container {
  width: 100%;
  max-width: 100%;
  min-width: 0;
}

@media (max-width: 768px) {
  .v2-sidebar {
    transform: translateX(-100%);
    width: min(280px, 86vw);
    box-shadow: 8px 0 32px hsl(0 0% 0% / 0.5);
  }
  .v2-sidebar.mobile-open { transform: translateX(0); }
  .v2-app-shell,
  .v2-app-shell.sidebar-collapsed {
    margin-left: 0;
    width: 100%;
    max-width: 100vw;
  }
  .v2-search__input { font-size: 16px; } /* >=16px prevents iOS zoom on focus */
  .v2-app-header { padding: 0 var(--space-3); }
  .v2-page { padding: 0 var(--space-3) var(--space-8); }
  .v2-page-header { padding: var(--space-6) 0 var(--space-4); }
  .v2-page-header__title { font-size: 22px; line-height: 28px; }
}

/* ══════════════════════════════════════════════════════════════════════
   Universal responsive primitives — apply to ALL pages, legacy + v2.
   ══════════════════════════════════════════════════════════════════════ */

/* Use the dynamic viewport unit so the address bar collapsing on mobile
   doesn't leave a gap below the app. Falls back to 100vh on browsers
   that don't support dvh. */
html, body { height: 100%; }
@supports (height: 100dvh) {
  html, body { min-height: 100dvh; }
  .v2-app-shell { min-height: 100dvh; }
}

/* Kill any horizontal scrollbar — common cause is wide tables or
   absolutely-positioned chart legends. We let .v2-table-wrap handle
   its own internal scroll instead. */
html, body { overflow-x: hidden; max-width: 100vw; }

/* Safe-area insets for notched devices (iPhone X+, foldables) */
@supports (padding: max(0px)) {
  .v2-app-header { padding-left: max(var(--space-6), env(safe-area-inset-left)); padding-right: max(var(--space-6), env(safe-area-inset-right)); }
  .v2-page { padding-left: max(var(--space-8), env(safe-area-inset-left)); padding-right: max(var(--space-8), env(safe-area-inset-right)); padding-bottom: max(var(--space-12), env(safe-area-inset-bottom)); }
  @media (max-width: 768px) {
    .v2-app-header { padding-left: max(var(--space-3), env(safe-area-inset-left)); padding-right: max(var(--space-3), env(safe-area-inset-right)); }
    .v2-page { padding-left: max(var(--space-3), env(safe-area-inset-left)); padding-right: max(var(--space-3), env(safe-area-inset-right)); }
  }
}

/* Mobile sidebar backdrop overlay — uses the existing .sidebar-overlay
   element rendered by the router. Click-outside closes the drawer. */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: hsl(var(--bg-base) / 0.65);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 25;
  animation: v2-fade-in var(--dur-base) var(--ease);
}
@media (max-width: 768px) {
  .v2-sidebar.mobile-open ~ .sidebar-overlay { display: block; }
}

/* PWA standalone mode — pad the top so content clears the iOS status bar */
@media all and (display-mode: standalone) {
  .v2-app-header { padding-top: env(safe-area-inset-top); height: calc(var(--header-height) + env(safe-area-inset-top, 0px)); }
}

/* Touch targets — minimum 44px on mobile per Apple HIG / WCAG */
@media (max-width: 768px) and (pointer: coarse) {
  .v2-btn, .v2-sidebar__item, .v2-search__result, .v2-user-menu__item {
    min-height: 44px;
  }
  .v2-btn--icon { min-width: 44px; }
}

/* Hide the chatbot bubble on tiny phones so it doesn't overlap content */
@media (max-width: 480px) {
  #chatbot-fab, .chatbot-fab { transform: scale(0.85); }
}
