/* =============================================================================
   Songon Analytics — design system
   Dark-first surfaces, disciplined signal palette, glanceable metrics,
   motion that only ever means change.

   The signal palette is the validated dark-mode categorical set from the
   dataviz reference (adjacent-pair CVD ΔE ≥ 8 on this surface). Series slots
   are assigned in fixed order and never cycled; status colors are reserved
   for state and always ship with an icon + label.
   ============================================================================= */

:root {
  color-scheme: dark;

  /* ---- Surfaces --------------------------------------------------------- */
  --an-plane:    #0d0d0d;   /* page background */
  --an-surface:  #1a1a19;   /* cards, chart surface */
  --an-raised:   #232322;   /* hover wash, inputs */
  --an-overlay:  rgba(13, 13, 13, 0.72);

  /* ---- Ink -------------------------------------------------------------- */
  --an-ink:        #ffffff;
  --an-ink-2:      #c3c2b7;
  --an-muted:      #898781;
  --an-grid:       #2c2c2a;   /* hairline gridlines */
  --an-baseline:   #383835;   /* axis / divider */
  --an-border:     rgba(255, 255, 255, 0.10);

  /* ---- Signal palette (fixed slot order — never cycled) ------------------ */
  --series-1: #3987e5;  /* blue */
  --series-2: #d95926;  /* orange */
  --series-3: #199e70;  /* aqua */
  --series-4: #c98500;  /* yellow */
  --series-5: #d55181;  /* magenta */
  --series-6: #008300;  /* green */
  --series-7: #9085e9;  /* violet */
  --series-8: #e66767;  /* red */

  /* ---- Status (reserved — never a series) -------------------------------- */
  --status-good:     #0ca30c;
  --status-warning:  #fab219;
  --status-serious:  #ec835a;
  --status-critical: #d03b3b;
  --delta-up:        #0ca30c;
  --delta-down:      #e66767;

  /* ---- Type ------------------------------------------------------------- */
  --an-font: system-ui, -apple-system, "Segoe UI", "Inter", sans-serif;
  --an-mono: "JetBrains Mono", ui-monospace, "SF Mono", monospace;

  /* ---- Geometry ---------------------------------------------------------- */
  --an-radius:    10px;
  --an-radius-sm: 6px;
  --an-gap:       16px;
  --an-max:       1360px;

  /* ---- Motion — only ever means change ----------------------------------
     Transitions exist solely on state that data or the user changes:
     value updates, filter swaps, hover/focus response, panel switches.
     Nothing ambient, nothing looping, nothing decorative. */
  --an-change: 240ms cubic-bezier(0.2, 0, 0, 1);
  --an-respond: 120ms cubic-bezier(0.2, 0, 0, 1);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  font-family: var(--an-font);
  background: var(--an-plane);
  color: var(--an-ink-2);
  font-size: 0.9375rem;
  line-height: 1.55;
  min-height: 100svh;
}

::selection { background: rgba(57, 135, 229, 0.35); }

a { color: inherit; text-decoration: none; }

button {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
  cursor: pointer;
}

:focus-visible {
  outline: 2px solid var(--series-1);
  outline-offset: 2px;
  border-radius: var(--an-radius-sm);
}

/* =============================================================================
   Shell — top bar + section nav
   ============================================================================= */

.an-topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  background: rgba(13, 13, 13, 0.88);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--an-border);
}

.an-topbar-inner {
  max-width: var(--an-max);
  margin: 0 auto;
  padding: 0 clamp(16px, 3vw, 32px);
  display: flex;
  align-items: center;
  gap: 24px;
  min-height: 56px;
}

.an-brand {
  display: flex;
  align-items: baseline;
  gap: 8px;
  color: var(--an-ink);
  font-weight: 700;
  letter-spacing: -0.01em;
  white-space: nowrap;
}
.an-brand .dot { color: var(--series-1); }
.an-brand small {
  font-weight: 500;
  color: var(--an-muted);
  font-size: 0.75rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.an-nav {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  scrollbar-width: none;
}
.an-nav::-webkit-scrollbar { display: none; }

.an-nav a {
  padding: 8px 14px;
  border-radius: 999px;
  color: var(--an-ink-2);
  font-size: 0.875rem;
  font-weight: 500;
  white-space: nowrap;
  transition: background var(--an-respond), color var(--an-respond);
}
.an-nav a:hover { background: var(--an-raised); color: var(--an-ink); }
.an-nav a.active {
  background: var(--an-surface);
  color: var(--an-ink);
  box-shadow: inset 0 0 0 1px var(--an-border);
}

.an-topbar-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 0.8125rem;
  color: var(--an-muted);
  white-space: nowrap;
}
.an-topbar-actions a:hover { color: var(--an-ink); }

/* =============================================================================
   Page scaffold
   ============================================================================= */

.an-main {
  max-width: var(--an-max);
  margin: 0 auto;
  padding: clamp(20px, 3.5vw, 36px) clamp(16px, 3vw, 32px) 64px;
}

.an-page-head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 24px;
}
.an-page-head h1 {
  color: var(--an-ink);
  font-size: clamp(1.375rem, 2.4vw, 1.75rem);
  font-weight: 700;
  letter-spacing: -0.015em;
  line-height: 1.2;
}
.an-page-head .sub {
  margin-top: 4px;
  color: var(--an-muted);
  font-size: 0.875rem;
  max-width: 62ch;
}

.an-section-h {
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--an-muted);
  margin: 32px 0 12px;
}

/* =============================================================================
   Filter row — one row, above everything it scopes
   ============================================================================= */

.an-filter-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
}

.an-seg {
  display: inline-flex;
  background: var(--an-surface);
  border: 1px solid var(--an-border);
  border-radius: 999px;
  padding: 3px;
}
.an-seg button {
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--an-ink-2);
  transition: background var(--an-respond), color var(--an-respond);
}
.an-seg button:hover { color: var(--an-ink); }
.an-seg button[aria-pressed="true"] {
  background: var(--an-raised);
  color: var(--an-ink);
  box-shadow: inset 0 0 0 1px var(--an-border);
}

.an-select {
  appearance: none;
  background: var(--an-surface) 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='%23898781' stroke-width='2.4'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E") no-repeat right 12px center;
  border: 1px solid var(--an-border);
  border-radius: 999px;
  color: var(--an-ink);
  font: inherit;
  font-size: 0.8125rem;
  padding: 7px 34px 7px 14px;
  cursor: pointer;
}
.an-select:hover { background-color: var(--an-raised); }

.an-input {
  background: var(--an-surface);
  border: 1px solid var(--an-border);
  border-radius: 999px;
  color: var(--an-ink);
  font: inherit;
  font-size: 0.8125rem;
  padding: 7px 14px;
  min-width: 200px;
}
.an-input::placeholder { color: var(--an-muted); }
.an-input:focus { outline: 2px solid var(--series-1); outline-offset: 1px; }

/* Refetch keeps the frame: previous render held at reduced opacity. */
.is-refetching { opacity: 0.45; transition: opacity var(--an-change); pointer-events: none; }

/* =============================================================================
   Stat tiles — glanceable metrics
   ============================================================================= */

.an-kpis {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: var(--an-gap);
  margin-bottom: var(--an-gap);
}

.stat-tile {
  background: var(--an-surface);
  border: 1px solid var(--an-border);
  border-radius: var(--an-radius);
  padding: 16px 18px 14px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.stat-tile .label {
  font-size: 0.8125rem;
  color: var(--an-muted);
  font-weight: 500;
}
.stat-tile .value {
  color: var(--an-ink);
  font-size: clamp(1.5rem, 2.4vw, 1.875rem);
  font-weight: 650;
  letter-spacing: -0.01em;
  line-height: 1.15;
  /* proportional figures at display size — tabular is for columns only */
  font-variant-numeric: normal;
}
.stat-tile .delta {
  font-size: 0.78rem;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.stat-tile .delta.up   { color: var(--delta-up); }
.stat-tile .delta.down { color: var(--delta-down); }
.stat-tile .delta.flat { color: var(--an-muted); }
.stat-tile .delta small { color: var(--an-muted); font-weight: 400; }
.stat-tile .spark { margin-top: 8px; height: 32px; }
.stat-tile .spark svg { display: block; width: 100%; height: 100%; }

/* Hero figure — exactly one per view */
.an-hero-figure {
  color: var(--an-ink);
  font-size: clamp(3rem, 7vw, 4.25rem);
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1;
}

/* =============================================================================
   Viz cards
   ============================================================================= */

.an-grid {
  display: grid;
  gap: var(--an-gap);
  grid-template-columns: repeat(12, 1fr);
}
.an-col-12 { grid-column: span 12; }
.an-col-6  { grid-column: span 6; }
.an-col-4  { grid-column: span 4; }
@media (max-width: 980px) {
  .an-col-6, .an-col-4 { grid-column: span 12; }
}

.viz-card {
  background: var(--an-surface);
  border: 1px solid var(--an-border);
  border-radius: var(--an-radius);
  padding: 16px 18px 12px;
  min-width: 0;
  display: flex;
  flex-direction: column;
}
.viz-card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}
.viz-card-head h3 {
  color: var(--an-ink);
  font-size: 0.9375rem;
  font-weight: 600;
  letter-spacing: -0.005em;
}
.viz-card-head .hint {
  margin-top: 2px;
  font-size: 0.78rem;
  color: var(--an-muted);
}
.viz-card-actions { display: flex; gap: 6px; flex-shrink: 0; }
.viz-card-actions button {
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--an-muted);
  padding: 4px 8px;
  border-radius: var(--an-radius-sm);
  transition: background var(--an-respond), color var(--an-respond);
}
.viz-card-actions button:hover { background: var(--an-raised); color: var(--an-ink); }
.viz-card-actions button[aria-pressed="true"] { color: var(--series-1); }

.viz-body { position: relative; min-height: 0; }
.viz-body svg { display: block; width: 100%; height: auto; }

/* Legend — always present for ≥2 series; swatch carries identity, text wears ink */
.viz-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 16px;
  margin-bottom: 8px;
  font-size: 0.78rem;
  color: var(--an-ink-2);
}
.viz-legend .key { display: inline-flex; align-items: center; gap: 6px; }
.viz-legend .key .swatch-line { width: 14px; height: 2px; border-radius: 1px; }
.viz-legend .key .swatch-rect { width: 10px; height: 10px; border-radius: 3px; }

/* Tooltip — values lead, labels follow; line keys, not boxes */
.viz-tip {
  position: fixed;
  z-index: 60;
  pointer-events: none;
  background: #232322;
  border: 1px solid var(--an-border);
  border-radius: var(--an-radius-sm);
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.5);
  padding: 8px 10px;
  font-size: 0.78rem;
  min-width: 130px;
  opacity: 0;
  transition: opacity var(--an-respond);
}
.viz-tip.show { opacity: 1; }
.viz-tip .tip-title {
  color: var(--an-muted);
  margin-bottom: 4px;
  font-size: 0.72rem;
}
.viz-tip .tip-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 1px 0;
}
.viz-tip .tip-row .k {
  width: 12px;
  height: 2px;
  border-radius: 1px;
  flex-shrink: 0;
}
.viz-tip .tip-row .v {
  color: var(--an-ink);
  font-weight: 650;
  margin-left: auto;
  font-variant-numeric: tabular-nums;
}
.viz-tip .tip-row .n { color: var(--an-ink-2); }

/* Table twin — every chart's WCAG-clean equivalent */
.viz-table-wrap { overflow-x: auto; }
.viz-table, .an-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8125rem;
}
.viz-table th, .an-table th {
  text-align: left;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--an-muted);
  padding: 8px 10px;
  border-bottom: 1px solid var(--an-baseline);
  white-space: nowrap;
}
.viz-table td, .an-table td {
  padding: 7px 10px;
  border-bottom: 1px solid var(--an-grid);
  color: var(--an-ink-2);
}
.viz-table td.num, .an-table td.num,
.viz-table th.num, .an-table th.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.viz-table tr:hover td, .an-table tbody tr:hover td { background: var(--an-raised); }

.hidden { display: none !important; }

/* =============================================================================
   Spreadsheet grid
   ============================================================================= */

.sheet-card {
  background: var(--an-surface);
  border: 1px solid var(--an-border);
  border-radius: var(--an-radius);
  overflow: hidden;
}
.sheet-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--an-baseline);
}
.sheet-status {
  margin-left: auto;
  font-size: 0.78rem;
  color: var(--an-muted);
  font-variant-numeric: tabular-nums;
}
.sheet-scroll { overflow: auto; max-height: min(62svh, 640px); }

.an-sheet {
  border-collapse: separate;
  border-spacing: 0;
  width: 100%;
  font-size: 0.8125rem;
}
.an-sheet thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--an-raised);
  color: var(--an-ink-2);
  font-weight: 600;
  font-size: 0.75rem;
  padding: 0;
  border-bottom: 1px solid var(--an-baseline);
  border-right: 1px solid var(--an-grid);
  white-space: nowrap;
}
.an-sheet thead th button {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  padding: 8px 10px;
  font: inherit;
  color: inherit;
}
.an-sheet thead th button:hover { color: var(--an-ink); }
.an-sheet thead th .dir { color: var(--series-1); font-size: 0.7rem; }
.an-sheet thead th.col-letter {
  text-align: center;
  color: var(--an-muted);
  font-family: var(--an-mono);
  font-size: 0.68rem;
  padding: 3px 0;
  border-bottom: 1px solid var(--an-grid);
}
.an-sheet td {
  padding: 6px 10px;
  border-bottom: 1px solid var(--an-grid);
  border-right: 1px solid var(--an-grid);
  color: var(--an-ink-2);
  white-space: nowrap;
  max-width: 340px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.an-sheet td.num { text-align: right; font-variant-numeric: tabular-nums; }
.an-sheet td.rowno {
  color: var(--an-muted);
  font-family: var(--an-mono);
  font-size: 0.7rem;
  text-align: right;
  background: var(--an-raised);
  position: sticky;
  left: 0;
  z-index: 1;
}
.an-sheet tbody tr:hover td { background: #21211f; }
.an-sheet tbody tr:hover td.rowno { background: #262624; }

.sheet-summary {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 20px;
  padding: 10px 14px;
  border-top: 1px solid var(--an-baseline);
  font-size: 0.78rem;
  color: var(--an-muted);
}
.sheet-summary b { color: var(--an-ink); font-weight: 650; font-variant-numeric: tabular-nums; }

/* =============================================================================
   Status & quality
   ============================================================================= */

/* Status pill — icon + label always; color never carries state alone */
.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid var(--an-border);
  background: var(--an-raised);
  color: var(--an-ink-2);
  white-space: nowrap;
}
.status-pill .ic { font-size: 0.8rem; line-height: 1; }
.status-pill.good     .ic { color: var(--status-good); }
.status-pill.warning  .ic { color: var(--status-warning); }
.status-pill.serious  .ic { color: var(--status-serious); }
.status-pill.critical .ic { color: var(--status-critical); }

.meter {
  height: 8px;
  border-radius: 999px;
  background: #103154;           /* lighter step of the fill's own ramp */
  overflow: hidden;
}
.meter > span {
  display: block;
  height: 100%;
  border-radius: 999px;
  background: var(--series-1);
  transition: width var(--an-change);   /* motion = the value changed */
}
.meter.warn > span    { background: var(--status-warning); }
.meter.warn           { background: #4a3708; }
.meter.serious > span { background: var(--status-serious); }
.meter.serious        { background: #4a2415; }
.meter.crit > span    { background: var(--status-critical); }
.meter.crit           { background: #451313; }

.quality-check {
  background: var(--an-surface);
  border: 1px solid var(--an-border);
  border-radius: var(--an-radius);
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.quality-check .row1 {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.quality-check .name { color: var(--an-ink); font-weight: 600; font-size: 0.875rem; }
.quality-check .nums {
  font-size: 0.78rem;
  color: var(--an-muted);
  font-variant-numeric: tabular-nums;
}
.quality-check .nums b { color: var(--an-ink); }

/* =============================================================================
   Gamified signals — earned by data, shown once, never looping
   ============================================================================= */

.ach-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: var(--an-gap);
}
.ach-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: var(--an-surface);
  border: 1px solid var(--an-border);
  border-radius: 999px;
  padding: 6px 14px 6px 8px;
  font-size: 0.8125rem;
  color: var(--an-ink-2);
}
.ach-chip .medal {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 0.8rem;
  background: var(--an-raised);
}
.ach-chip b { color: var(--an-ink); font-weight: 650; }
.ach-chip.earned { border-color: rgba(201, 133, 0, 0.5); }
.ach-chip.earned .medal { background: rgba(201, 133, 0, 0.18); }

/* Entrance on data arrival — a one-shot change signal, not ambience */
@keyframes an-enter {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: none; }
}
.an-enter { animation: an-enter var(--an-change) both; }

/* Value-change flash on live cells: fired by JS only when a number changes */
@keyframes an-changed {
  0%   { background: rgba(57, 135, 229, 0.25); }
  100% { background: transparent; }
}
.an-changed { animation: an-changed 700ms var(--an-respond, ease-out) both; }

.an-empty {
  padding: 32px;
  text-align: center;
  color: var(--an-muted);
  font-size: 0.875rem;
}

.an-error {
  padding: 14px 16px;
  border: 1px solid rgba(208, 59, 59, 0.4);
  background: rgba(208, 59, 59, 0.08);
  color: var(--an-ink-2);
  border-radius: var(--an-radius);
  font-size: 0.875rem;
}

/* Embed mode — the page chrome disappears, the chart is the page */
body.embed-mode { background: var(--an-surface); }
body.embed-mode .an-topbar, body.embed-mode .no-embed { display: none; }
body.embed-mode .an-main { max-width: none; padding: 12px 16px; }

.embed-snippet {
  background: var(--an-plane);
  border: 1px solid var(--an-border);
  border-radius: var(--an-radius-sm);
  font-family: var(--an-mono);
  font-size: 0.72rem;
  color: var(--an-ink-2);
  padding: 10px 12px;
  overflow-x: auto;
  white-space: pre;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
