/* ==========================================================================
   Polaris Starlight tokens. Extracted from the three brand images:
   blush paper background, peach-rose hair flowing through magenta into
   deep violet tips, coral sweater, champagne-white four-point sparkles.
   ========================================================================== */
:root {
  color-scheme: dark;
  /* Core set (dark default): plum night page, rose primary, violet
     secondary, peach links, champagne stars, blush-white text. */
  --page: #1d1024;            /* plum night: darkest violet of the hair tips */
  --surface: #271733;         /* raised plum */
  --surface-2: #332143;       /* hover/elevated plum */
  --border: rgb(245 168 184 / 13%);
  --border-strong: rgb(245 168 184 / 24%);
  --text: #f8e9f0;            /* blush white */
  --muted: #cca9bc;           /* dusty rose */
  --dim: #93748d;
  --accent: #f0619b;          /* starlight rose: hair mid-length */
  --accent-2: #a86bf5;        /* violet: hair-to-tip gradient end */
  --accent-fg: #2b0e1e;       /* ink on rose fills */
  --accent-muted: rgb(240 97 155 / 16%);
  --link: #f2a9be;            /* peach-rose: hair roots */
  --star: #f2d49b;            /* champagne: the sparkle highlights */
  --success: #7bd9a3;
  --danger: #f2707f;
  --warning: #f2c063;

  --text-primary: var(--text);
  --text-secondary: var(--muted);
  --text-muted: var(--dim);
  --border-subtle: var(--border);
  --accent-red: var(--danger);
  --accent-green: var(--success);
  --accent-cyan: var(--link);
  --bg-hover: var(--surface-2);
  --bg-elevated: var(--surface-2);
  --select-chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23cca9bc' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");

  --font-display: ui-serif, Georgia, "Times New Roman", serif;
  --radius-sm: 3px;
  --radius-md: 6px;
  --radius-lg: 8px;
}

/* Blush-paper light theme: the portrait background becomes the page. */
@media (prefers-color-scheme: light) {
  :root:not([data-theme]) {
    color-scheme: light;
    --page: #fbeff3;
    --surface: #fffafc;
    --surface-2: #f5e2e9;
    --border: rgb(51 24 42 / 10%);
    --border-strong: rgb(51 24 42 / 18%);
    --text: #33182a;
    --muted: #7a5468;
    --dim: #a08696;
    --accent: #c2276b;
    --accent-2: #7c3aed;
    --accent-fg: #fff5f8;
    --accent-muted: rgb(194 39 107 / 12%);
    --link: #a81c5c;
    --star: #a8741a;
    --success: #1e7a4c;
    --danger: #c22747;
    --warning: #9a6a10;
    --select-chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%237a5468' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  }
}

[data-theme="light"] {
  color-scheme: light;
  --page: #fbeff3;
  --surface: #fffafc;
  --surface-2: #f5e2e9;
  --border: rgb(51 24 42 / 10%);
  --border-strong: rgb(51 24 42 / 18%);
  --text: #33182a;
  --muted: #7a5468;
  --dim: #a08696;
  --accent: #c2276b;
  --accent-2: #7c3aed;
  --accent-fg: #fff5f8;
  --accent-muted: rgb(194 39 107 / 12%);
  --link: #a81c5c;
  --star: #a8741a;
  --success: #1e7a4c;
  --danger: #c22747;
  --warning: #9a6a10;
  --select-chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%237a5468' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
}

/* Base */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; }
body {
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--page);
  color: var(--text);
  margin: 0;
  min-height: 100vh;
}
button:focus-visible, a:focus-visible, [tabindex]:focus-visible, input:focus-visible {
  outline: 2px solid var(--link);
  outline-offset: 2px;
}

/* Layout */
.stats-app-container { display: flex; min-height: 100vh; background: var(--page); color: var(--text); }
.stats-nav-rail {
  width: 240px; background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; flex-shrink: 0;
}
.stats-desktop-nav { display: flex; }
@media (max-width: 1023px) { .stats-desktop-nav { display: none; } }
.stats-nav-rail-header { padding: 20px 24px; border-bottom: 1px solid var(--border); min-height: 56px; display: flex; align-items: center; }
.stats-logo-container { display: flex; align-items: center; gap: 10px; }
.stats-logo-star { color: var(--star); flex-shrink: 0; }
.stats-logo-text { font-size: 14px; font-weight: 700; letter-spacing: 0.1em; color: var(--text); }
.stats-logo-subtext { font-size: 10px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.stats-nav-rail-menu { flex: 1; padding: 16px 12px; display: flex; flex-direction: column; gap: 4px; }
.stats-nav-rail-item {
  display: flex; align-items: center; gap: 12px; padding: 9px 12px;
  border: 0; border-radius: var(--radius-md); background: transparent;
  color: var(--muted); font-size: 13.5px; cursor: pointer; text-align: left; width: 100%;
  border-left: 2px solid transparent;
}
.stats-nav-rail-item:hover { background: var(--bg-hover); color: var(--text); }
.stats-nav-rail-item[data-active="true"] {
  background: var(--accent-muted); color: var(--text);
  border-left-color: var(--accent);
  border-radius: 0 var(--radius-md) var(--radius-md) 0;
}
.stats-nav-rail-item[data-active="true"] .stats-nav-rail-item-icon { color: var(--accent); }
.stats-nav-rail-footer { padding: 16px 24px; border-top: 1px solid var(--border); }
.stats-version-tag { font-size: 11px; color: var(--dim); }

.stats-mobile-drawer-overlay {
  position: fixed; inset: 0; background: rgb(0 0 0 / 50%); z-index: 40;
}
.stats-mobile-drawer {
  position: fixed; left: 0; top: 0; bottom: 0; width: 280px; z-index: 41;
  background: var(--surface); display: flex; flex-direction: column;
}
.stats-mobile-drawer-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--border);
}
.stats-mobile-nav { width: 100%; border-right: 0; flex: 1; }
.stats-drawer-close-btn {
  background: transparent; border: 0; color: var(--muted); cursor: pointer; padding: 4px;
}

.stats-main-pane { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.stats-top-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 0 24px; height: 56px; border-bottom: 1px solid var(--border);
  background: var(--surface); position: sticky; top: 0; z-index: 20;
}
.stats-top-bar-left { display: flex; align-items: center; gap: 12px; min-width: 0; }
.stats-page-title {
  font-family: var(--font-display); font-size: 22px; font-weight: 600;
  letter-spacing: 0.01em; margin: 0; white-space: nowrap;
}
.stats-mobile-menu-btn {
  display: none; background: transparent; border: 0; color: var(--muted); cursor: pointer; padding: 4px;
}
@media (max-width: 1023px) { .stats-mobile-menu-btn { display: block; } }
.stats-top-bar-right { display: flex; align-items: center; gap: 12px; }
.stats-top-bar-meta { display: flex; align-items: center; }
.stats-last-updated { font-size: 12px; color: var(--dim); white-space: nowrap; }
.stats-content-area { flex: 1; padding: 24px; }
.stats-content-inner { max-width: 1200px; margin: 0 auto; display: flex; flex-direction: column; gap: 24px; }
.stats-route-container { display: flex; flex-direction: column; gap: 24px; }

/* Wide displays: lift the 1200px cap so 27" 4K panels use the space. */
@media (min-width: 1536px) { .stats-content-inner { max-width: 1440px; } }
@media (min-width: 2200px) { .stats-content-inner { max-width: 1720px; } }

/* Phone: tighten chrome; tables become cards via the DataTable fallback. */
@media (max-width: 640px) {
  .stats-content-area { padding: 12px; }
  .stats-top-bar { padding: 0 12px; }
  .stats-last-updated { display: none; }
  .stats-page-title { font-size: 18px; }
  .stats-top-bar-right { gap: 8px; }
  .stats-drawer { position: fixed; inset: 0; width: 100%; border-left: 0; }
}

/* Token gate (DD-029: shell renders before the token is known) */
.stats-token-gate { display: flex; align-items: center; gap: 8px; }
.stats-token-input {
  background: var(--surface-2); border: 1px solid var(--border-strong);
  border-radius: var(--radius-md); color: var(--text);
  font-size: 13px; padding: 7px 10px; width: 210px;
}
.stats-token-input::placeholder { color: var(--dim); }

/* Buttons */
.stats-button {
  border: 1px solid var(--border-strong); border-radius: var(--radius-md);
  padding: 7px 14px; font-size: 13px; cursor: pointer;
  background: var(--surface-2); color: var(--text);
  display: inline-flex; align-items: center; gap: 8px;
}
.stats-button:hover { border-color: var(--accent); }
.stats-button-primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border: 0; color: #fff; font-weight: 600;
}
[data-theme="light"] .stats-button-primary { color: var(--accent-fg); }
.stats-button-secondary { background: transparent; }
.stats-button:disabled { opacity: 0.55; cursor: default; }
.stats-icon-button {
  background: transparent; border: 0; color: var(--muted); cursor: pointer;
  padding: 6px; border-radius: var(--radius-md); display: inline-flex;
}
.stats-icon-button:hover { color: var(--text); background: var(--bg-hover); }

/* Panel */
.stats-panel {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg); overflow: auto;
}
.stats-panel-header {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 12px;
  padding: 16px 20px 0;
}
.stats-panel-title { font-size: 14px; font-weight: 650; margin: 0; }
.stats-panel-subtitle { font-size: 12.5px; color: var(--muted); margin: 4px 0 0; }
.stats-panel-actions { display: flex; gap: 8px; align-items: center; }
.stats-panel-body { padding: 16px 20px 20px; }

/* Metric cluster */
.stats-metric-cluster { display: flex; flex-direction: column; gap: 12px; }
.stats-metric-primary-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px;
}
.stats-metric-secondary-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 12px;
}
.stats-metric-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-md); padding: 14px 16px;
}
.stats-metric-card.primary { border-top: 2px solid var(--accent); }
.stats-metric-card.secondary { padding: 10px 14px; }
.stats-metric-label { font-size: 11.5px; color: var(--muted); margin-bottom: 6px; }
.stats-metric-value {
  font-family: var(--font-display); font-size: 26px; font-weight: 600;
  line-height: 1.1; font-variant-numeric: tabular-nums;
}
.stats-metric-card.secondary .stats-metric-value { font-size: 18px; }
.stats-metric-value.hero {
  background: linear-gradient(120deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text; background-clip: text; color: transparent;
}

/* Tables */
.stats-table-wrapper { width: 100%; }
.stats-table-container { overflow-x: auto; }
.stats-table-desktop-only { overflow-x: auto; }
.stats-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.stats-table-th {
  text-align: left; font-weight: 600; font-size: 11.5px; color: var(--muted);
  padding: 8px 12px; border-bottom: 1px solid var(--border-strong); white-space: nowrap;
}
.stats-table-td { padding: 9px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
.stats-table-tr:last-child .stats-table-td { border-bottom: 0; }
.stats-table-tr-clickable { cursor: pointer; }
.stats-table-tr-clickable:hover .stats-table-td { background: var(--bg-hover); }
.stats-text-right { text-align: right; font-variant-numeric: tabular-nums; }
.stats-text-left { text-align: left; }
.stats-table-empty { padding: 24px; text-align: center; color: var(--dim); font-size: 13px; }
.stats-table-mobile-only { display: none; }
@media (max-width: 767px) {
  .stats-table-mobile-only { display: block; }
  .stats-table-desktop-only { display: none; }
}
.stats-table-mobile-list { display: flex; flex-direction: column; gap: 8px; }
.stats-mobile-card {
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius-md); padding: 12px 14px; font-size: 13px;
}
.stats-mobile-card-row { display: flex; justify-content: space-between; gap: 8px; padding: 2px 0; }
.stats-mobile-card-key { color: var(--muted); }

/* Status pills */
.stats-status-pill {
  display: inline-block; font-size: 11.5px; font-weight: 600; padding: 2px 9px;
  border-radius: 999px; border: 1px solid var(--border-strong); white-space: nowrap;
}
.stats-status-pill[data-variant="success"] { color: var(--success); border-color: var(--success); }
.stats-status-pill[data-variant="danger"] { color: var(--danger); border-color: var(--danger); }
.stats-status-pill[data-variant="warning"] { color: var(--warning); border-color: var(--warning); }
.stats-status-pill[data-variant="info"] { color: var(--link); border-color: var(--link); }

/* Skeleton / empty / error */
.stats-skeleton {
  border-radius: var(--radius-sm);
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--border-strong) 50%, var(--surface-2) 75%);
  background-size: 200% 100%; animation: stats-shimmer 1.4s infinite linear;
}
@keyframes stats-shimmer { to { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) { .stats-skeleton { animation: none; } }
.stats-boundary-skeleton { display: flex; flex-direction: column; gap: 8px; }
.stats-empty-state {
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  padding: 36px 20px; color: var(--dim); text-align: center;
}
.stats-empty-state-icon { color: var(--star); }
.stats-empty-state-message { font-size: 13px; margin: 0; max-width: 420px; }
.stats-error-state { padding: 24px; text-align: center; }
.stats-error-state-title { font-size: 14px; margin: 0 0 8px; }
.stats-error-state-message { font-size: 12.5px; color: var(--muted); }
.stats-error-state-btn { margin-top: 12px; }

/* Charts */
.stats-chart-box { position: relative; height: 260px; }
.stats-chart-box.tall { height: 320px; }
.stats-chart-legend { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 12px; color: var(--muted); }

/* Decision drawer */
.stats-drawer-overlay { position: fixed; inset: 0; background: rgb(0 0 0 / 55%); z-index: 50; }
.stats-drawer {
  position: fixed; right: 0; top: 0; bottom: 0; width: min(560px, 100%); z-index: 51;
  background: var(--surface); border-left: 1px solid var(--border-strong);
  display: flex; flex-direction: column;
}
.stats-drawer-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--border);
}
.stats-drawer-title { font-family: var(--font-display); font-size: 18px; margin: 0; }
.stats-drawer-body { overflow-y: auto; padding: 16px 20px; display: flex; flex-direction: column; gap: 16px; }
.stats-kv-grid { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; font-size: 13px; }
.stats-kv-key { color: var(--muted); }
.stats-kv-val { font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.stats-json-block {
  background: var(--page); border: 1px solid var(--border); border-radius: var(--radius-md);
  padding: 12px 14px; font-size: 12px; overflow-x: auto; margin: 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.stats-note {
  font-size: 12.5px; color: var(--muted); background: var(--surface-2);
  border: 1px solid var(--border); border-radius: var(--radius-md); padding: 10px 14px;
}

/* Request details drawer (Info): status card, stat tiles, payload blocks. */
.stats-drawer-header-left { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.stats-drawer-id { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }
.stats-drawer-status-card { background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-md); padding: 14px 16px; }
.stats-drawer-status-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.stats-drawer-model { font-size: 15px; font-weight: 650; }
.stats-drawer-provider { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.stats-drawer-metrics-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 10px; }
.stats-drawer-metric-card { background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-md); padding: 10px 14px; }
.stats-drawer-metric-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.stats-drawer-metric-value { font-size: 17px; font-weight: 600; font-variant-numeric: tabular-nums; margin-top: 6px; overflow-wrap: anywhere; }
.stats-drawer-metric-sub { font-size: 11.5px; color: var(--dim); margin-top: 4px; }
.stats-json-block-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.stats-json-block-title { font-size: 12px; font-weight: 600; color: var(--muted); }
.stats-json-actions { display: flex; align-items: center; gap: 8px; }
.stats-json-actions .stats-button { padding: 4px 10px; font-size: 12px; }
.stats-json-block-content { margin: 0; overflow-x: auto; font-family: inherit; font-size: 12px; }

/* Section heading star */
.stats-section-star { color: var(--star); vertical-align: -2px; margin-right: 8px; }

/* Proposals staging form */
.stats-staging-form { display: flex; flex-wrap: wrap; gap: 12px; align-items: end; }
.stats-staging-form label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.stats-staging-form input, .stats-staging-form select {
  border: 1px solid var(--border-strong); border-radius: var(--radius-md);
  padding: 7px 10px; font-size: 13px; background: var(--surface-2); color: var(--text);
  min-width: 140px;
}
.stats-staging-notice, .stats-staging-error { margin: 10px 0 0; font-size: 12.5px; }
.stats-staging-notice { color: var(--success); }
.stats-staging-error { color: var(--danger); }

/* Utility */
.stats-muted { color: var(--muted); }
.stats-mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
