/* metrics.bizgrips.com — dense operator UI.
   Palette matches finance.bizgrips.com (validated categorical order + status
   steps) so the two internal tools read as one system. */

:root {
  color-scheme: light;
  --page: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f7f8fa;
  --surface-3: #eef1f5;
  --ink: #14161c;
  --ink-2: #565b68;
  --ink-3: #8a8f9c;
  --line: #e5e8ee;
  --line-strong: #ccd2dd;
  --accent: #2a78d6;
  --accent-soft: #eaf1fb;
  --muted-series: #898781;
  --good: #0ca30c;
  --good-ink: #067a06;
  --good-soft: #e8f6e8;
  --warn: #d98a00;
  --warn-ink: #9a6200;
  --warn-soft: #fdf3e0;
  --bad: #d03b3b;
  --bad-ink: #b32a2a;
  --bad-soft: #fdecec;
  --grid: #e9ecf2;
  --axis: #ccd2dd;
  --shadow: 0 1px 2px rgba(20, 22, 28, .04), 0 4px 16px rgba(20, 22, 28, .05);
}

/* Dark tokens are declared under both scopes: the media query carries the OS
   setting, and :root[data-theme] carries an explicit toggle, which has to win
   in both directions. The :not() guard lets a light stamp beat OS-dark, and
   :where() keeps the media block's specificity below the toggle scope. */
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0c0d10;
    --surface: #16181d;
    --surface-2: #1c1f26;
    --surface-3: #232730;
    --ink: #f3f5f8;
    --ink-2: #a8adba;
    --ink-3: #7a808e;
    --line: #262a33;
    --line-strong: #333844;
    --accent: #3987e5;
    --accent-soft: #182636;
    --muted-series: #898781;
    --good: #0ca30c;
    --good-ink: #2cc32c;
    --good-soft: #15271a;
    --warn: #e8a838;
    --warn-ink: #e8a838;
    --warn-soft: #2b2214;
    --bad: #e05a5a;
    --bad-ink: #e87070;
    --bad-soft: #2d1919;
    --grid: #23262e;
    --axis: #333844;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 20px rgba(0, 0, 0, .35);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0c0d10;
  --surface: #16181d;
  --surface-2: #1c1f26;
  --surface-3: #232730;
  --ink: #f3f5f8;
  --ink-2: #a8adba;
  --ink-3: #7a808e;
  --line: #262a33;
  --line-strong: #333844;
  --accent: #3987e5;
  --accent-soft: #182636;
  --muted-series: #898781;
  --good: #0ca30c;
  --good-ink: #2cc32c;
  --good-soft: #15271a;
  --warn: #e8a838;
  --warn-ink: #e8a838;
  --warn-soft: #2b2214;
  --bad: #e05a5a;
  --bad-ink: #e87070;
  --bad-soft: #2d1919;
  --grid: #23262e;
  --axis: #333844;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 20px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 13.5px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

.tnum { font-variant-numeric: tabular-nums; }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap;
}

/* ------------------------------------------------------------------ chrome */

header.top {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 11px 20px;
  background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 60;
}
.brand { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.brand .mark { font-weight: 800; letter-spacing: -.02em; font-size: 16px; }
.brand .mark b { color: var(--accent); }
.brand .sub { color: var(--ink-3); font-size: 12px; }
.top-right { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.sheet-link {
  color: var(--ink-2); font-size: 12px; max-width: 260px; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; text-decoration: none;
  border: 1px solid var(--line-strong); border-radius: 999px; padding: 2px 10px;
}
.sheet-link:hover { color: var(--accent); border-color: var(--accent); }
.freshness { color: var(--ink-3); font-size: 12px; white-space: nowrap; }
.freshness b { color: var(--ink-2); font-weight: 600; }

.chip {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 11.5px; font-weight: 600; padding: 2px 8px;
  border-radius: 999px; border: 1px solid transparent; white-space: nowrap;
}
.chip.stale { background: var(--warn-soft); color: var(--warn-ink); border-color: var(--warn); }
.chip.mock { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }

button {
  font: inherit; color: var(--ink); background: var(--surface);
  border: 1px solid var(--line-strong); border-radius: 7px;
  padding: 5px 11px; cursor: pointer;
}
button:hover:not(:disabled) { background: var(--surface-2); }
button:focus-visible, input:focus-visible, select:focus-visible, summary:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover:not(:disabled) { filter: brightness(1.07); }

/* ---------------------------------------------------------------- controls */

/* --header-h is measured from the real header, so the controls bar stays put
   however the header wraps (it wraps to two lines on a phone). */
.controls {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px;
  padding: 10px 20px; background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky; top: var(--header-h, 47px); z-index: 50;
}
.ctl { display: flex; align-items: center; gap: 6px; }
.ctl > label { color: var(--ink-3); font-size: 11.5px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .04em; }

.seg { display: inline-flex; border: 1px solid var(--line-strong); border-radius: 7px; overflow: hidden; }
.seg button {
  border: 0; border-radius: 0; padding: 4px 9px; font-size: 12.5px;
  background: var(--surface); color: var(--ink-2);
  border-left: 1px solid var(--line);
}
.seg button:first-child { border-left: 0; }
.seg button[aria-pressed="true"] { background: var(--accent); color: #fff; font-weight: 600; }

input[type="search"], input[type="date"], select {
  font: inherit; font-size: 13px; color: var(--ink); background: var(--surface);
  border: 1px solid var(--line-strong); border-radius: 7px; padding: 4px 9px;
}
input[type="search"] { width: 190px; }

/* campaign multi-select */
.multi { position: relative; }
.multi > summary {
  list-style: none; cursor: pointer; user-select: none;
  border: 1px solid var(--line-strong); border-radius: 7px; padding: 5px 11px;
  background: var(--surface); font-size: 13px; white-space: nowrap;
}
.multi > summary::-webkit-details-marker { display: none; }
.multi > summary::after { content: " ▾"; color: var(--ink-3); }
.multi[open] > summary { border-color: var(--accent); }
.multi-panel {
  position: absolute; top: calc(100% + 5px); left: 0; z-index: 70;
  min-width: 280px; max-height: 320px; overflow: auto;
  background: var(--surface); border: 1px solid var(--line-strong);
  border-radius: 9px; box-shadow: var(--shadow); padding: 7px;
}
.multi-panel label {
  display: flex; align-items: center; gap: 8px;
  padding: 5px 7px; border-radius: 6px; cursor: pointer; font-size: 13px;
}
.multi-panel label:hover { background: var(--surface-2); }
.multi-panel hr { border: 0; border-top: 1px solid var(--line); margin: 6px 2px; }

/* -------------------------------------------------------------------- main */

main { padding: 16px 20px 48px; max-width: 1900px; }

.warnings {
  margin: 0 0 14px; padding: 9px 13px; border-radius: 9px;
  background: var(--warn-soft); border: 1px solid var(--warn);
  color: var(--warn-ink); font-size: 12.5px;
}
.warnings ul { margin: 4px 0 0; padding-left: 18px; }

/* KPI strip */
.kpi-caption {
  color: var(--ink-3); font-size: 11.5px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .05em; margin-bottom: 6px;
}
.kpis {
  display: grid; gap: 10px; margin-bottom: 16px;
  grid-template-columns: repeat(auto-fit, minmax(132px, 1fr));
}
.kpi {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 13px; box-shadow: var(--shadow);
}
.kpi .k-label {
  color: var(--ink-3); font-size: 11px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .05em;
}
.kpi .k-value { font-size: 22px; font-weight: 650; letter-spacing: -.02em; margin-top: 3px; }
.kpi .k-sub { color: var(--ink-3); font-size: 11.5px; margin-top: 1px; }
.kpi.is-good .k-value { color: var(--good-ink); }
.kpi.is-warn .k-value { color: var(--warn-ink); }
.kpi.is-bad  .k-value { color: var(--bad-ink); }

/* ------------------------------------------------------------- leaderboard */

.board { background: var(--surface); border: 1px solid var(--line);
  border-radius: 11px; box-shadow: var(--shadow); overflow: hidden; }
.board-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 9px 13px; border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.tabs { display: inline-flex; gap: 3px; }
.tabs button {
  border: 0; background: transparent; color: var(--ink-2);
  padding: 5px 12px; border-radius: 7px; font-size: 13px;
}
.tabs button[aria-selected="true"] { background: var(--accent-soft); color: var(--accent); font-weight: 650; }
.legend-goal { color: var(--ink-3); font-size: 11.5px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.legend-goal i { display: inline-block; width: 9px; height: 9px; border-radius: 3px; margin-right: 4px; vertical-align: -1px; }
.legend-goal .g-good { background: var(--good); }
.legend-goal .g-warn { background: var(--warn); }
.legend-goal .g-bad { background: var(--bad); }

.table-wrap { overflow: auto; max-height: calc(100vh - 300px); }
table.board-table { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 13px; }
table.board-table th, table.board-table td {
  padding: 7px 11px; border-bottom: 1px solid var(--line); white-space: nowrap;
}
table.board-table thead th {
  position: sticky; top: 0; z-index: 5;
  background: var(--surface-2); color: var(--ink-2);
  font-size: 11.5px; font-weight: 650; text-transform: uppercase; letter-spacing: .03em;
  border-bottom: 1px solid var(--line-strong); cursor: pointer; user-select: none;
}
table.board-table thead th:hover { color: var(--ink); }
table.board-table thead th .arrow { color: var(--accent); font-size: 10px; margin-left: 3px; }
table.board-table tbody tr:nth-child(even) { background: var(--surface-2); }
table.board-table tbody tr:hover { background: var(--accent-soft); }
table.board-table tbody tr.is-paused { opacity: .55; }
table.board-table td.num, table.board-table th.num { text-align: right; font-variant-numeric: tabular-nums; }

/* Adset and campaign names share long prefixes ("Broad - US, Men, 21-64…"), so
   the distinguishing part is at the END and an ellipsis hides exactly what you
   need. Both columns are drag-resizable; widths come from these custom
   properties, set on the table from persisted state. */
table.board-table { --name-w: 300px; --campaign-w: 210px; }
table.board-table td.name { max-width: var(--name-w); }
table.board-table th.resizable { position: relative; }
.col-resizer {
  position: absolute; top: 0; right: 0; width: 9px; height: 100%;
  cursor: col-resize; user-select: none; touch-action: none;
}
.col-resizer::after {
  content: ""; position: absolute; top: 20%; right: 4px;
  width: 1px; height: 60%; background: var(--line-strong);
}
.col-resizer:hover::after, .col-resizer.dragging::after {
  background: var(--accent); width: 2px; top: 0; height: 100%;
}
body.is-resizing { cursor: col-resize; user-select: none; }

.col-sticky { position: sticky; left: 0; z-index: 4; background: var(--surface); }
thead .col-sticky { z-index: 6; background: var(--surface-2); }
tbody tr:nth-child(even) .col-sticky { background: var(--surface-2); }
tbody tr:hover .col-sticky { background: var(--accent-soft); }

.adset-link {
  color: var(--ink); font-weight: 600; text-decoration: none;
  display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  max-width: 100%;
}
.adset-link:hover { color: var(--accent); text-decoration: underline; }
.name-cell { display: flex; align-items: center; gap: 2px; min-width: 0; }
.name-cell .adset-link { flex: 0 1 auto; }
.campaign-cell {
  color: var(--ink-3); font-size: 12px; max-width: var(--campaign-w);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.pill {
  display: inline-block; font-size: 10.5px; font-weight: 700; letter-spacing: .04em;
  padding: 1px 7px; border-radius: 999px; border: 1px solid;
}
.pill.active { color: var(--good-ink); border-color: var(--good); background: var(--good-soft); }
.pill.paused { color: var(--ink-2); border-color: var(--line-strong); background: var(--surface-3); }
.pill.unknown { color: var(--ink-3); border-color: var(--line); background: transparent; }
.badge-new {
  display: inline-block; margin-left: 6px; font-size: 10px; font-weight: 800;
  letter-spacing: .06em; padding: 1px 6px; border-radius: 4px;
  background: var(--accent-soft); color: var(--accent); border: 1px solid var(--accent);
}

td.g-good { color: var(--good-ink); background: var(--good-soft); font-weight: 600; }
td.g-warn { color: var(--warn-ink); background: var(--warn-soft); font-weight: 600; }
td.g-bad  { color: var(--bad-ink);  background: var(--bad-soft);  font-weight: 600; }
td.nil { color: var(--ink-3); }

.spark { display: block; }

.empty {
  padding: 44px 24px; text-align: center; color: var(--ink-3);
}
.empty h3 { margin: 0 0 6px; color: var(--ink-2); font-size: 15px; }
.empty p { margin: 0; font-size: 13px; }

/* ---------------------------------------------------------------- detail */

.detail-head {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 16px; margin-bottom: 14px; flex-wrap: wrap;
}
.detail-head h2 { margin: 2px 0 3px; font-size: 20px; letter-spacing: -.02em; }
.detail-head .meta { color: var(--ink-3); font-size: 12.5px; }
.back-link {
  color: var(--accent); text-decoration: none; font-size: 13px; font-weight: 600;
}
.back-link:hover { text-decoration: underline; }

/* A fixed 2x2 above tablet width — auto-fit produced a lopsided 3 + 1. */
.chart-grid { display: grid; gap: 12px; margin: 16px 0; grid-template-columns: 1fr; }
@media (min-width: 940px) {
  .chart-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
.card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 11px; box-shadow: var(--shadow); padding: 12px 14px 8px;
}
.card h3 { margin: 0 0 1px; font-size: 13.5px; font-weight: 650; }
.card .card-sub { color: var(--ink-3); font-size: 11.5px; margin-bottom: 8px; }
.chart-host { position: relative; }
.chart-host svg { display: block; width: 100%; height: auto; }

.legend { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 4px;
  color: var(--ink-2); font-size: 11.5px; }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.legend i { display: inline-block; width: 11px; height: 3px; border-radius: 2px; }
.legend i.sq { width: 9px; height: 9px; border-radius: 2px; }

.tip {
  position: absolute; pointer-events: none; z-index: 20;
  background: var(--surface); border: 1px solid var(--line-strong);
  border-radius: 8px; box-shadow: var(--shadow); padding: 6px 9px;
  font-size: 12px; white-space: nowrap; opacity: 0; transition: opacity .08s;
}
.tip.on { opacity: 1; }
.tip .tip-date { color: var(--ink-3); font-size: 11px; margin-bottom: 2px; }
.tip .tip-row { display: flex; align-items: center; gap: 6px; justify-content: space-between; }
.tip .tip-row b { font-variant-numeric: tabular-nums; }
.tip i { display: inline-block; width: 9px; height: 9px; border-radius: 2px; }

.daily-table-wrap { overflow: auto; max-height: 460px; }
table.daily { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 12.5px; }
table.daily th, table.daily td { padding: 5px 9px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.daily thead th {
  position: sticky; top: 0; background: var(--surface-2); color: var(--ink-2);
  font-size: 11px; font-weight: 650; text-transform: uppercase; letter-spacing: .03em;
  border-bottom: 1px solid var(--line-strong); text-align: right;
}
table.daily thead th:first-child { text-align: left; }
table.daily td { text-align: right; font-variant-numeric: tabular-nums; }
table.daily td:first-child { text-align: left; font-variant-numeric: normal; }
table.daily tbody tr:nth-child(even) { background: var(--surface-2); }

.loading { padding: 60px; text-align: center; color: var(--ink-3); }

@media print {
  header.top, .controls, .board-head .tabs { position: static; }
}

/* ===================================================================== */
/* Mobile. A 17-column leaderboard can't be a table on a phone, so below  */
/* 760px each adset becomes a card and sorting moves to an explicit       */
/* control (there are no column headers left to tap).                     */
/* ===================================================================== */

.mobile-sort { display: none; align-items: center; gap: 6px; }
.mobile-sort select { flex: 1 1 auto; min-width: 0; }
.sort-dir { padding: 6px 10px; line-height: 1; }

.cards { display: flex; flex-direction: column; gap: 9px; padding: 11px; }
.acard {
  border: 1px solid var(--line); border-radius: 11px;
  background: var(--surface); padding: 11px 12px;
  text-decoration: none; color: inherit; display: block;
}
.acard:active { background: var(--accent-soft); }
.acard.is-paused { opacity: .6; }
.acard-top { display: flex; justify-content: space-between; gap: 9px; align-items: flex-start; }
/* Names wrap in full here — no ellipsis, so near-identical names stay distinct. */
.acard-name { font-weight: 650; font-size: 14.5px; line-height: 1.3; overflow-wrap: anywhere; }
.acard-campaign { color: var(--ink-3); font-size: 12px; margin-top: 2px; overflow-wrap: anywhere; }
.acard-spark { margin: 8px 0 2px; }
.acard-spark svg { width: 100%; height: auto; display: block; }
.acard-metrics {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1px; margin-top: 9px;
  background: var(--line); border: 1px solid var(--line); border-radius: 8px; overflow: hidden;
}
.acard-metric { background: var(--surface); padding: 6px 9px; }
.acard-metric .m-label {
  color: var(--ink-3); font-size: 10.5px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .04em;
}
.acard-metric .m-value { font-size: 15px; font-variant-numeric: tabular-nums; margin-top: 1px; }
.acard-metric.g-good .m-value { color: var(--good-ink); font-weight: 650; }
.acard-metric.g-warn .m-value { color: var(--warn-ink); font-weight: 650; }
.acard-metric.g-bad  .m-value { color: var(--bad-ink);  font-weight: 650; }
.acard-metric.nil .m-value { color: var(--ink-3); }
/* Presets with an odd column count (Funnel has 7) would leave a bare gap in the
   last row — the final tile spans instead. */
.acard-metric:last-child:nth-child(odd) { grid-column: 1 / -1; }

@media (max-width: 760px) {
  body { font-size: 14px; }

  header.top { padding: 9px 13px; flex-wrap: wrap; gap: 7px; }
  .brand { flex: 1 1 100%; }
  .brand .mark { font-size: 15px; }
  .top-right { flex: 1 1 100%; justify-content: space-between; flex-wrap: wrap; gap: 7px; }
  .sheet-link { max-width: 100%; order: 3; flex: 1 1 100%; }

  .controls { padding: 9px 13px; gap: 8px; }
  .ctl { flex-wrap: wrap; }
  /* Search, campaign and the date range each take a full row; the two date
     inputs then share that row instead of orphaning the arrow onto its own. */
  .ctl:last-child, .multi, .multi > summary, .ctl-range { flex: 1 1 100%; }
  /* Grid, not flex: a native date input won't shrink below its intrinsic width
     in a nowrap row, which pushed the page into horizontal scroll at 375px. */
  .ctl-range {
    display: grid; grid-template-columns: 1fr 1fr; gap: 6px; align-items: center;
  }
  .ctl-range > label { grid-column: 1 / -1; }
  .ctl-range > span { display: none; }            /* the arrow separator */
  .ctl-range input[type="date"] { width: 100%; min-width: 0; }
  .ctl-range > button { grid-column: 1 / -1; }    /* Clear */
  input[type="search"] { width: 100%; }
  .multi-panel { min-width: 0; width: 100%; }
  .seg { overflow-x: auto; max-width: 100%; }

  /* Comfortable touch targets. */
  button, .seg button, select, input[type="search"], input[type="date"], .multi > summary {
    min-height: 38px;
  }
  .seg button { padding: 6px 13px; }
  /* The label row is the real tap target for a campaign, not the 13px box. */
  .multi-panel label { min-height: 42px; padding: 8px 10px; }
  .multi-panel input[type="checkbox"] { width: 18px; height: 18px; flex: 0 0 auto; }

  main { padding: 11px 13px 40px; }
  .kpi-caption { margin-bottom: 5px; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .kpi { padding: 8px 10px; }
  .kpi .k-value { font-size: 19px; }

  .board-head { padding: 9px 11px; gap: 9px; }
  .tabs { width: 100%; overflow-x: auto; }
  .tabs button { flex: 1 0 auto; min-height: 38px; }
  .legend-goal { font-size: 11px; }
  .mobile-sort { display: flex; width: 100%; }

  /* The table and its resize grips are desktop-only. The :has() guard keeps
     the empty states visible, since they render into the same container. */
  .table-wrap:has(table.board-table) { display: none; }
  .col-resizer { display: none; }

  .detail-head h2 { font-size: 17px; }
  .chart-grid { gap: 10px; }
  .card { padding: 10px 11px 7px; }
  .daily-table-wrap { max-height: 60vh; }
}

/* Cards are the mobile-only view; the table takes over above the breakpoint. */
@media (min-width: 761px) {
  .cards { display: none; }
}

@media (max-width: 380px) {
  .kpis { grid-template-columns: 1fr; }
  .acard-metrics { grid-template-columns: 1fr; }
}
