/* Palette roles from the validated reference instance. Dark is a selected set of
   steps for the dark surface, not an automatic inversion. */
:root {
  color-scheme: light;

  --surface-1: #fcfcfb;
  --page: #f9f9f7;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --wash: rgba(11, 11, 11, 0.04);

  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;

  --delta-up: #006300;
  --delta-down: #d03b3b;

  --sans: system-ui, -apple-system, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface-1: #1a1a19;
    --page: #0d0d0d;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --wash: rgba(255, 255, 255, 0.06);

    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;

    --delta-up: #0ca30c;
    --delta-down: #d03b3b;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-1: #1a1a19;
  --page: #0d0d0d;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --wash: rgba(255, 255, 255, 0.06);

  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;

  --delta-up: #0ca30c;
  --delta-down: #d03b3b;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--sans);
  background: var(--page);
  color: var(--text-primary);
  font-size: 14px;
  line-height: 1.5;
}

.wrap { max-width: 1240px; margin: 0 auto; padding: 24px 20px 64px; }

/* ---------- header ---------- */

.masthead {
  display: flex;
  align-items: baseline;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 4px;
}

.masthead h1 { font-size: 20px; font-weight: 600; margin: 0; letter-spacing: -0.01em; }
.masthead .realm { color: var(--text-muted); font-size: 13px; }
.masthead .spacer { flex: 1; }

/* The sign-out button needs a form to POST from. display:contents keeps the button itself as the
   flex item, so it lines up with the theme toggle instead of sitting in a wrapper box. */
.masthead form { display: contents; }

.theme-toggle {
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-secondary);
  border-radius: 8px;
  padding: 5px 10px;
  cursor: pointer;
  font: inherit;
  font-size: 12px;
}
.theme-toggle:hover { background: var(--wash); }

.status-line { color: var(--text-muted); font-size: 12.5px; margin-bottom: 20px; }

/* ---------- stat tiles ---------- */

.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}

.tile {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
}
.tile .label { color: var(--text-secondary); font-size: 12px; }
/* Proportional figures: tabular-nums makes standalone values look loose. */
.tile .value { font-size: 24px; font-weight: 600; letter-spacing: -0.02em; margin-top: 2px; }
.tile .sub { color: var(--text-muted); font-size: 11.5px; }

/* ---------- filters: one row, above everything it scopes ---------- */

.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  padding: 12px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 16px;
}

.filters label { color: var(--text-secondary); font-size: 12px; margin-right: -4px; }

.filters input[type="search"],
.filters input[type="text"],
.filters select {
  font: inherit;
  font-size: 13px;
  padding: 6px 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--page);
  color: var(--text-primary);
  min-height: 32px;
}
.filters input[type="search"] { min-width: 170px; }

.range-presets { display: flex; gap: 4px; }
.range-presets button {
  font: inherit;
  font-size: 12.5px;
  padding: 6px 10px;
  min-height: 32px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--page);
  color: var(--text-secondary);
  cursor: pointer;
}
.range-presets button:hover { background: var(--wash); }
.range-presets button[aria-pressed="true"] {
  background: var(--text-primary);
  color: var(--surface-1);
  border-color: var(--text-primary);
  font-weight: 600;
}

/* ---------- tabs ---------- */

.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin-bottom: 18px; }
.tabs button {
  font: inherit;
  font-size: 13.5px;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-secondary);
  padding: 9px 14px;
  cursor: pointer;
  margin-bottom: -1px;
}
.tabs button:hover { color: var(--text-primary); }
.tabs button[aria-selected="true"] {
  color: var(--text-primary);
  border-bottom-color: var(--series-1);
  font-weight: 600;
}

.panel[hidden] { display: none; }

/* ---------- cards & charts ---------- */

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  margin-bottom: 16px;
}

.card-head { display: flex; align-items: baseline; gap: 10px; margin-bottom: 2px; }
.card-head h2 { font-size: 14.5px; font-weight: 600; margin: 0; }
.card-head .spacer { flex: 1; }
.card-sub { color: var(--text-muted); font-size: 12px; margin: 0 0 12px; }

.table-toggle {
  font: inherit;
  font-size: 12px;
  border: 1px solid var(--border);
  background: var(--page);
  color: var(--text-secondary);
  border-radius: 7px;
  padding: 4px 9px;
  cursor: pointer;
}
.table-toggle:hover { background: var(--wash); }

/* Height includes the x-axis band so the axis is never cut off. */
.chart-box { position: relative; height: 300px; }
.chart-box.tall { height: 340px; }
.chart-box.short { height: 190px; }

.small-multiples {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 16px;
}

/* Refetch holds the previous render — no skeleton, no layout jump. */
.loading { opacity: 0.45; transition: opacity 120ms ease; }

.legend { display: flex; flex-wrap: wrap; gap: 14px; margin-top: 12px; }
.legend-item { display: flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--text-secondary); }
.legend-key { width: 14px; height: 2px; border-radius: 1px; flex: none; }
.legend-key.block { height: 10px; width: 10px; border-radius: 2px; }

/* ---------- tables ---------- */

.table-scroll { overflow-x: auto; }

table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}
table.data th, table.data td { text-align: left; padding: 7px 10px; white-space: nowrap; }
table.data th {
  color: var(--text-secondary);
  font-weight: 600;
  font-size: 12px;
  border-bottom: 1px solid var(--axis);
  position: sticky;
  top: 0;
  background: var(--surface-1);
}
table.data th.sortable { cursor: pointer; user-select: none; }
table.data th.sortable:hover { color: var(--text-primary); }
table.data td { border-bottom: 1px solid var(--grid); }
table.data tbody tr:hover { background: var(--wash); }
table.data .num { text-align: right; }
table.data .name-cell { font-weight: 600; }
table.data tbody tr.clickable { cursor: pointer; }

.delta { font-variant-numeric: tabular-nums; }
.delta.up { color: var(--delta-up); }
.delta.down { color: var(--delta-down); }

.muted { color: var(--text-muted); }
.empty { color: var(--text-muted); padding: 28px 4px; text-align: center; }

.chip {
  display: inline-block;
  padding: 1px 7px;
  border-radius: 999px;
  border: 1px solid var(--border);
  font-size: 11.5px;
  color: var(--text-secondary);
  margin: 0 4px 4px 0;
}

/* ---------- compare picker ---------- */

.picker { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 4px; }
.picker select { min-width: 220px; }
.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 6px 3px 9px;
  font-size: 12.5px;
  background: var(--page);
}
.pill .key { width: 10px; height: 10px; border-radius: 2px; flex: none; }
.pill button {
  border: none;
  background: none;
  color: var(--text-muted);
  cursor: pointer;
  font: inherit;
  line-height: 1;
  padding: 2px 4px;
  min-width: 24px;
  min-height: 24px;
}
.pill button:hover { color: var(--text-primary); }

/* ---------- tooltip ---------- */

#tooltip {
  position: fixed;
  pointer-events: none;
  z-index: 20;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 12.5px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.14);
  opacity: 0;
  transition: opacity 90ms ease;
  max-width: 280px;
}
#tooltip.show { opacity: 1; }
#tooltip .tt-title { color: var(--text-muted); font-size: 11.5px; margin-bottom: 5px; }
#tooltip .tt-row { display: flex; align-items: center; gap: 7px; margin-top: 3px; }
#tooltip .tt-key { width: 12px; height: 2px; border-radius: 1px; flex: none; }
/* Values lead, labels follow. */
#tooltip .tt-value { font-weight: 600; font-variant-numeric: tabular-nums; }
#tooltip .tt-name { color: var(--text-secondary); }

a.link { color: var(--series-1); text-decoration: none; }
a.link:hover { text-decoration: underline; }
