:root {
  color-scheme: light;
  /* SolarSquare palette, carried over from the Referral Dashboards project so
     the two read as one family. */
  --bg: #f5f4f0;
  --surface: #ffffff;
  --surface-2: #faf9f6;
  --border: rgba(0, 0, 0, .07);
  --border-strong: rgba(0, 0, 0, .16);
  --text: #1c1c1a;
  --text-2: #55534d;
  --text-3: #8a8780;
  --accent: #1a56db;
  --accent-soft: #dbeafe;
  --good: #166534;
  --good-soft: #dcfce7;
  --bad: #991b1b;
  --bad-soft: #fee2e2;
  --amber: #92400e;
  --amber-soft: #fef3c7;
  --solar: #f59e0b;
  --shadow: 0 1px 2px rgba(28, 28, 26, .04), 0 1px 3px rgba(28, 28, 26, .06);
  --radius: 10px;
}
* { box-sizing: border-box; }
/* An explicit `display` on an element beats the UA stylesheet's [hidden] rule,
   so tab panels need this or they all show at once. */
[hidden] { display: none !important; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
h1, h2 { margin: 0; font-weight: 600; letter-spacing: -.01em; }

/* ---- top bar ---------------------------------------------------------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; flex-wrap: wrap;
  padding: 13px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 0 rgba(0,0,0,.02);
}
.topbar::before {
  content: ""; position: absolute; left: 0; right: 0; top: 0; height: 3px;
  background: linear-gradient(90deg, var(--accent) 0%, var(--solar) 55%, var(--good) 100%);
}
body { position: relative; }
.brand { display: flex; align-items: center; gap: 12px; }
.logo-img { height: 26px; width: auto; flex: none; }
.topbar h1 { font-size: 16px; letter-spacing: -.015em; }
.sub { margin: 1px 0 0; font-size: 12px; color: var(--text-2); }
.stamp { display: flex; align-items: center; gap: 10px; font-size: 12px; }
.muted { color: var(--text-3); }
.badge {
  padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 600;
  letter-spacing: .03em; text-transform: uppercase;
  background: var(--accent-soft); color: var(--accent);
  border: 1px solid transparent;
}
.badge.sample { background: #fff4e0; color: #9a5b00; border-color: #f0d9b0; }
/* ---- loading ---------------------------------------------------------- */
.loading, .load-error { padding: 60px 24px; color: var(--text-2); text-align: center; }
.load-error { color: var(--bad); white-space: pre-wrap; text-align: left; max-width: 760px; margin: 0 auto; }
.dots::after { content: ""; animation: dots 1.2s steps(4, end) infinite; }
@keyframes dots { 0% { content: ""; } 25% { content: "."; } 50% { content: ".."; } 75% { content: "..."; } }

/* ---- filters ---------------------------------------------------------- */
.filters {
  display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px;
  padding: 12px 24px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
}
.filter { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.filter > label {
  font-size: 10px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .05em; color: var(--text-3);
}
.filter select {
  font: inherit; font-size: 13px; padding: 5px 8px;
  border: 1px solid var(--border-strong); border-radius: 7px;
  background: var(--surface); color: var(--text); min-width: 108px;
}
.grow-end { margin-left: auto; }
.btn-reset {
  font: inherit; font-size: 13px; padding: 6px 12px; cursor: pointer;
  border: 1px solid var(--border-strong); border-radius: 7px;
  background: var(--surface); color: var(--text-2);
}
.btn-reset:hover { border-color: var(--accent); color: var(--accent); }

/* ---- multi-select dropdown -------------------------------------------- */
.ms { position: relative; }
.ms-btn {
  font: inherit; font-size: 13px; padding: 5px 26px 5px 9px; cursor: pointer;
  border: 1px solid var(--border-strong); border-radius: 7px;
  background: var(--surface); color: var(--text);
  min-width: 132px; max-width: 210px; text-align: left;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.ms-btn::after {
  content: ""; position: absolute; right: 10px; top: 50%; pointer-events: none;
  width: 0; height: 0; margin-top: -2px;
  border-left: 4px solid transparent; border-right: 4px solid transparent;
  border-top: 5px solid var(--text-3);
}
.ms-btn:hover { border-color: var(--accent); }
.ms-btn.active { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.ms-panel {
  position: absolute; z-index: 40; top: calc(100% + 4px); left: 0;
  width: 250px; max-height: 300px; display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: 8px; box-shadow: 0 6px 18px rgba(16, 24, 40, .12);
}
.ms-search {
  font: inherit; font-size: 13px; margin: 8px; padding: 5px 8px;
  border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface); color: var(--text);
}
.ms-list { overflow-y: auto; padding: 0 4px 6px; }
.ms-opt {
  display: flex; align-items: center; gap: 8px; cursor: pointer;
  padding: 5px 8px; border-radius: 5px; font-size: 13px; color: var(--text);
}
.ms-opt:hover { background: var(--surface-2); }
.ms-opt input { margin: 0; accent-color: var(--accent); }
.ms-opt .ms-n { margin-left: auto; color: var(--text-3); font-size: 11px; }
.ms-foot {
  border-top: 1px solid var(--border); padding: 6px 10px;
  display: flex; justify-content: space-between; font-size: 12px;
}
.ms-foot button {
  font: inherit; font-size: 12px; background: none; border: none;
  color: var(--accent); cursor: pointer; padding: 0;
}
.ms-empty { padding: 10px; color: var(--text-3); font-size: 12px; }

/* ---- KPIs ------------------------------------------------------------- */
.kpis {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px; padding: 16px 24px 4px;
}
.kpi {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px 14px 12px 16px;
  box-shadow: var(--shadow); position: relative; overflow: hidden;
}
.kpi::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px;
  background: var(--accent); opacity: .85;
}
.kpi.k-good::before { background: var(--good); }
.kpi.k-warn::before { background: var(--solar); }
.kpi.k-flat::before { background: var(--text-3); }
.kpi .k-value { color: var(--text); }
.kpi .k-label {
  font-size: 10px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .05em; color: var(--text-3);
}
.kpi .k-value {
  font-size: 24px; font-weight: 650; letter-spacing: -.02em;
  margin-top: 3px; font-variant-numeric: tabular-nums;
}
.kpi .k-note { font-size: 11px; color: var(--text-2); margin-top: 2px; }

/* ---- tabs ------------------------------------------------------------- */
.tabs {
  display: flex; gap: 2px; padding: 12px 24px 0; flex-wrap: wrap;
  border-bottom: 1px solid var(--border);
}
.tab {
  font: inherit; font-size: 13px; font-weight: 500; cursor: pointer;
  padding: 8px 14px; border: none; background: none; color: var(--text-2);
  border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 650; }
.tabs { background: var(--surface); }

/* ---- panels ----------------------------------------------------------- */
.panel {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px; padding: 16px 24px 24px;
}
@media (max-width: 900px) { .panel { grid-template-columns: 1fr; } }
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow);
  min-width: 0;
}
.card.wide { grid-column: 1 / -1; }
.card-head { margin-bottom: 12px; }
.card-head h2 { font-size: 14px; }
.card-head p { margin: 4px 0 0; font-size: 12px; color: var(--text-2); max-width: 74ch; }
.chart { height: 280px; width: 100%; }
.chart.tall { height: 380px; }

/* ---- tables ----------------------------------------------------------- */
.table-wrap { overflow-x: auto; }
.table-wrap.scroll { max-height: 340px; overflow-y: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 12.5px; }
table.data th {
  text-align: left; font-weight: 600; font-size: 10px; text-transform: uppercase;
  letter-spacing: .04em; color: var(--text-3); padding: 6px 8px;
  border-bottom: 1px solid var(--border); position: sticky; top: 0;
  background: var(--surface); cursor: pointer; white-space: nowrap;
}
table.data th:hover { color: var(--accent); }
table.data th.num, table.data td.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data td { padding: 6px 8px; border-bottom: 1px solid var(--border); }
table.data tbody tr:hover { background: var(--surface-2); }
.pos { color: var(--good); } .neg { color: var(--bad); }
.bar-cell { position: relative; }
.bar-cell span { position: relative; z-index: 1; }
.bar-cell i {
  position: absolute; left: 0; top: 3px; bottom: 3px; border-radius: 3px;
  background: var(--accent-soft); z-index: 0;
}

/* ---- footer ----------------------------------------------------------- */
.foot {
  grid-column: 1 / -1; display: flex; justify-content: space-between; gap: 16px;
  flex-wrap: wrap; padding: 4px 24px 28px; font-size: 11.5px; color: var(--text-3);
}
.warn { color: #9a5b00; }
.card-head .caveat {
  margin-top: 6px; padding: 6px 9px; border-radius: 6px;
  background: var(--surface-2); border-left: 2px solid #e0862c;
  color: var(--text-2); font-size: 11.5px;
}

/* ---- empty state ------------------------------------------------------ */
.panel.is-empty .card { display: none; }
.empty-state {
  grid-column: 1 / -1; padding: 48px 24px; text-align: center;
  background: var(--surface); border: 1px dashed var(--border-strong);
  border-radius: var(--radius); color: var(--text-2); font-size: 14px;
}
.empty-state span {
  display: block; margin-top: 8px; font-size: 12.5px; color: var(--text-3);
  max-width: 52ch; margin-inline: auto;
}

/* ---- headline finding under a chart ----------------------------------- */
.finding {
  margin: 12px 0 0; padding: 10px 13px; border-radius: 8px;
  background: var(--accent-soft); color: var(--text);
  font-size: 13px; line-height: 1.55;
}
.finding strong { font-weight: 650; }


/* ---- drill-down cells -------------------------------------------------- */
table.data td.drill { cursor: pointer; position: relative; }
table.data td.drill:hover {
  background: var(--accent-soft); color: var(--accent); font-weight: 600;
}
table.data td.drill::after {
  content: "93"; position: absolute; right: 3px; opacity: 0;
  font-size: 10px; color: var(--accent);
}
table.data td.drill:hover::after { opacity: .7; }
table.data td.drilled { background: var(--good); color: #fff; }

/* ---- refreshed stamp --------------------------------------------------- */
.refreshed { text-align: right; line-height: 1.25; }
.r-label {
  display: block; font-size: 9.5px; font-weight: 600; letter-spacing: .06em;
  text-transform: uppercase; color: var(--text-3);
}
.r-value { font-size: 12.5px; color: var(--text); font-variant-numeric: tabular-nums; }

/* ---- date presets ------------------------------------------------------ */
.presets { display: flex; gap: 0; border: 1px solid var(--border-strong); border-radius: 7px; overflow: hidden; }
.presets button {
  font: inherit; font-size: 12.5px; padding: 5px 10px; cursor: pointer;
  border: none; border-right: 1px solid var(--border);
  background: var(--surface); color: var(--text-2); white-space: nowrap;
}
.presets button:last-child { border-right: none; }
.presets button:hover { background: var(--accent-soft); color: var(--accent); }
.presets button.on { background: var(--accent); color: #fff; font-weight: 600; }
.date-input {
  font: inherit; font-size: 13px; padding: 4px 8px;
  border: 1px solid var(--border-strong); border-radius: 7px;
  background: var(--surface); color: var(--text);
}

/* ---- toggle ------------------------------------------------------------ */
.toggle {
  display: inline-flex; align-items: center; gap: 7px; cursor: pointer;
  font: inherit; font-size: 12.5px; font-weight: 600;
  padding: 4px 11px 4px 5px; border-radius: 999px;
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text-2);
}
.toggle .knob {
  width: 26px; height: 15px; border-radius: 999px; background: var(--border-strong);
  position: relative; transition: background .15s ease;
}
.toggle .knob::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 11px; height: 11px;
  border-radius: 50%; background: #fff; transition: transform .15s ease;
}
.toggle[aria-checked="true"] { border-color: var(--good); color: var(--good); }
.toggle[aria-checked="true"] .knob { background: var(--good); }
.toggle[aria-checked="true"] .knob::after { transform: translateX(11px); }

/* ---- table emphasis ---------------------------------------------------- */
table.data tbody tr.total-row {
  background: var(--accent-soft); font-weight: 650;
}
table.data tbody tr.total-row:hover { background: var(--accent-soft); }
table.data tbody tr.total-row td { border-bottom: 2px solid var(--accent); }
table.data td.metric-strong { font-weight: 650; color: var(--accent); }
table.data td.metric-muted { color: var(--text-3); }


/* ---- link out to the domain-restricted sheet --------------------------- */
.sheet-link {
  margin: 10px 0 0; padding: 10px 13px; border-radius: 8px;
  background: var(--good-soft); border-left: 3px solid var(--good);
  font-size: 13px;
}
.sheet-link a { color: var(--good); font-weight: 650; text-decoration: none; }
.sheet-link a:hover { text-decoration: underline; }
.sheet-link span { color: var(--text-2); font-size: 12px; display: block; margin-top: 2px; }

/* ---- centred metric table (Referrer Activation) ------------------------ */
.table-wrap.centered table.data th {
  text-align: center; white-space: normal; line-height: 1.25;
  vertical-align: bottom; padding: 7px 6px; min-width: 62px;
}
.table-wrap.centered table.data td.num { text-align: center; }
.table-wrap.centered table.data th:first-child,
.table-wrap.centered table.data td:first-child {
  text-align: left; white-space: nowrap; position: sticky; left: 0;
  background: var(--surface); z-index: 1;
}
.table-wrap.centered table.data tbody tr.total-row td:first-child {
  background: var(--accent-soft);
}
.table-wrap.centered table.data tbody tr:hover td:first-child { background: var(--surface-2); }
/* The bar fill reads as a left-anchored magnitude, which fights a centred
   number -- drop it to a soft tint on this table. */
.table-wrap.centered table.data td.bar-cell i { opacity: .45; }

/* ======================================================================== */
/* Mobile                                                                    */
/* ======================================================================== */

/* Tables stay wide and scroll sideways; everything else stacks. Give the
   scroll a visible edge so it is obvious there is more to the right. */
.table-wrap { -webkit-overflow-scrolling: touch; }

@media (max-width: 760px) {
  .topbar { padding: 10px 14px; gap: 10px; align-items: flex-start; }
  .topbar h1 { font-size: 15px; }
  .sub { display: none; }
  .logo-img { height: 22px; }
  .stamp { gap: 8px; }
  .refreshed { text-align: left; }

  .filters {
    padding: 10px 14px; gap: 10px;
    position: static;            /* a sticky bar eats half a phone screen */
  }
  .filter { width: 100%; }
  .grow-end { margin-left: 0; }
  .presets { width: 100%; flex-wrap: wrap; }
  .presets button { flex: 1 1 30%; text-align: center; padding: 7px 6px; }
  .date-input { width: 100%; }
  .ms-btn { min-width: 0; max-width: none; width: 100%; }
  .ms-panel { width: min(320px, calc(100vw - 32px)); }
  .btn-reset { width: 100%; padding: 9px 12px; }
  .toggle { width: 100%; justify-content: center; padding: 8px 12px; }

  .kpis {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px; padding: 12px 14px 4px;
  }
  .kpi { padding: 10px 10px 10px 13px; }
  .kpi .k-value { font-size: 20px; }
  .kpi .k-label { font-size: 9px; }
  .kpi .k-note { font-size: 10px; }

  /* Tabs scroll horizontally rather than wrapping to three cramped lines. */
  .tabs {
    padding: 8px 14px 0; flex-wrap: nowrap; overflow-x: auto;
    scrollbar-width: none;
  }
  .tabs::-webkit-scrollbar { display: none; }
  .tab { padding: 8px 11px; font-size: 12.5px; white-space: nowrap; }

  .panel { padding: 12px 14px 20px; gap: 10px; }
  .card { padding: 12px; }
  .card-head h2 { font-size: 13.5px; }
  .card-head p { font-size: 11.5px; }
  .chart { height: 240px; }
  .chart.tall { height: 280px; }
  .finding { font-size: 12px; padding: 9px 11px; }

  table.data { font-size: 12px; }
  .table-wrap.scroll { max-height: 420px; }
  /* The sticky first column is what makes a 12-column table usable on a
     phone -- the cluster name stays put while the metrics scroll. */
  .table-wrap.centered table.data th:first-child,
  .table-wrap.centered table.data td:first-child {
    box-shadow: 1px 0 0 var(--border);
  }

  .foot { padding: 4px 14px 22px; }
}

@media (max-width: 420px) {
  .kpis { grid-template-columns: 1fr; }
  .presets button { flex: 1 1 45%; }
}

/* ---- collapsible filter bar (mobile only) ------------------------------ */
.filter-toggle { display: none; }

@media (max-width: 760px) {
  .filter-toggle {
    display: flex; align-items: center; gap: 9px; width: 100%;
    font: inherit; font-size: 13px; text-align: left; cursor: pointer;
    padding: 11px 14px; border: none;
    border-bottom: 1px solid var(--border);
    background: var(--surface); color: var(--text);
  }
  .ft-icon { color: var(--accent); font-size: 14px; }
  .ft-text { font-weight: 650; }
  .ft-summary {
    margin-left: auto; color: var(--text-2); font-size: 11.5px;
    text-align: right; overflow: hidden; text-overflow: ellipsis;
    white-space: nowrap; max-width: 60%;
  }
  .filters.collapsed { display: none; }
  .filters.open { display: flex; }
  /* Three presets per row reads better than two at phone widths. */
  .presets button { flex: 1 1 30%; }
}

/* ---- sticky header / first column stacking ----------------------------- */
/* The sticky header had no z-index while the sticky first column's cells had
   z-index 1, so data rows painted OVER the header as you scrolled. Four
   explicit layers fix it: header corner > header row > first column > body. */
table.data thead th { z-index: 3; }
.table-wrap.centered table.data thead th:first-child { z-index: 4; }
.table-wrap.centered table.data tbody td:first-child { z-index: 2; }
table.data tbody td { position: relative; z-index: 0; }

/* Opaque backgrounds, or scrolled rows show through the gaps. */
table.data thead th {
  background: var(--surface);
  box-shadow: inset 0 -1px 0 var(--border-strong);
}
.table-wrap.centered table.data thead th:first-child { background: var(--surface); }

/* Header needs room now that labels wrap to two lines. */
.table-wrap.centered table.data thead th { padding-top: 9px; padding-bottom: 9px; }
