/* DH Digital client reporting dashboard.
 *
 * Chart series colours are the validated reference palette (slots 1-3),
 * which clear the CVD and normal-vision separation gates in both light and
 * dark mode. They are deliberately independent of each client's brand
 * colour: brand tints the chrome, never the data, so two series never
 * become indistinguishable because a client's brand happens to be blue.
 */

:root {
  color-scheme: light;

  --surface-0: #f6f6f4;
  --surface-1: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e0dfda;
  --border-strong: #c9c8c1;

  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #7a7974;

  --series-1: #2a78d6;  /* sessions */
  --series-2: #eb6834;  /* spend */
  --series-3: #1baf7a;  /* bookings */
  --series-4: #4a3aa7;  /* GEO, in channel chips only — never a chart series */

  --good: #008300;
  --bad: #e34948;
  --warn: #eda100;

  --brand: #2563eb;

  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 4px 12px rgba(0, 0, 0, .04);

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface-0: #121211;
    --surface-1: #1a1a19;
    --surface-2: #232322;
    --border: #333330;
    --border-strong: #4a4a45;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #8f8e86;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #9085e9;
    --good: #008300;
    --bad: #e66767;
    --warn: #c98500;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 12px rgba(0, 0, 0, .25);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-0: #121211;
  --surface-1: #1a1a19;
  --surface-2: #232322;
  --border: #333330;
  --border-strong: #4a4a45;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #8f8e86;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #9085e9;
  --bad: #e66767;
  --warn: #c98500;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--surface-0);
  color: var(--text-primary);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* Links use their own token, not --brand: a brand colour dark enough for
   white button text is usually too close to body copy to read as a link. */
a { color: var(--brand-link, var(--brand)); }

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

.topbar {
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  display: flex;
  align-items: center;
  gap: 24px;
  min-height: 60px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 650;
  text-decoration: none;
  color: var(--text-primary);
  font-size: 15px;
}

.brand-dot {
  width: 10px; height: 10px; border-radius: 3px;
  background: var(--brand);
  flex: none;
}

/* Client logos are drawn for the client's own site, which is nearly always
   a light background — most are dark ink and vanish on the dark surface.
   Sitting them on a white plate in dark mode keeps any client's logo
   legible without needing a second, light-mode-inverted asset from them. */
.brand img {
  height: 30px;
  width: auto;
  max-width: 150px;
  display: block;
  object-fit: contain;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) .brand img {
    background: #fff;
    padding: 4px 7px;
    border-radius: var(--radius-sm);
  }
}

:root[data-theme="dark"] .brand img {
  background: #fff;
  padding: 4px 7px;
  border-radius: var(--radius-sm);
}

.nav { display: flex; gap: 2px; margin-left: 8px; flex-wrap: wrap; }

.nav a {
  padding: 7px 13px;
  border-radius: var(--radius-sm);
  text-decoration: none;
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
}

.nav a:hover { background: var(--surface-2); color: var(--text-primary); }
.nav a.active { background: var(--brand); color: #fff; }

.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 12px; }

.user-chip { font-size: 13px; color: var(--text-muted); }

/* ---------------------------------------------------------------- layout */

.wrap { max-width: 1360px; margin: 0 auto; padding: 24px; }

.page-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px;
  flex-wrap: wrap;
  margin-bottom: 20px;
}

.page-head h1 { margin: 0 0 4px; font-size: 24px; font-weight: 650; letter-spacing: -.01em; }
.page-head .sub { color: var(--text-muted); font-size: 13px; }

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

.card > h2 {
  margin: 0 0 4px;
  font-size: 15px;
  font-weight: 650;
}

.card > .card-note {
  margin: 0 0 16px;
  color: var(--text-muted);
  font-size: 13px;
}

.grid { display: grid; gap: 20px; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); }

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

/* Flex rather than grid, specifically so the final row fills the width.
   An auto-fit grid leaves whatever does not divide evenly stranded — with
   nine tiles that put a single one alone under a full row, which reads as
   a mistake. Growing the last row's tiles to take up the slack makes any
   number of KPIs look deliberate. */
.kpis {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 20px;
}

.kpi {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  /* A wide basis caps the row at five tiles, so nine wrap as 5 + 4 rather
     than 7 + 2. Grow then stretches that second row to the full width — a
     modest, even expansion, instead of two tiles ballooning to half the
     page each. */
  flex: 1 1 240px;
  min-width: 200px;
}

/* Counts that would strand a nearly-empty final row get their own column
   count instead. Six tiles at five-per-row leaves one stretched across the
   whole width, which looks more like a bug than a layout; three and three
   is what it should be. Keyed off the child count with :has() rather than
   a class, so a block that gains or loses a metric re-flows on its own.

   Each rule is scoped to a width band rather than layered by specificity,
   because a :has() selector outranks a plain one and a later override
   would otherwise be ignored. */
@media (min-width: 1180px) {
  .kpis:has(.kpi:nth-child(6):last-child) .kpi {
    flex-basis: calc(33.333% - 8px);   /* 6 -> 3 + 3, not 5 + 1 */
  }
  .kpis:has(.kpi:nth-child(7):last-child) .kpi,
  .kpis:has(.kpi:nth-child(8):last-child) .kpi {
    flex-basis: calc(25% - 9px);       /* 7 -> 4 + 3, 8 -> 4 + 4 */
  }
  .kpis:has(.kpi:nth-child(11):last-child) .kpi,
  .kpis:has(.kpi:nth-child(12):last-child) .kpi {
    flex-basis: calc(25% - 9px);       /* 11 -> 4+4+3, 12 -> 4+4+4 */
  }
}

/* Three-up territory. Four tiles would break 3 + 1, so they pair off. */
@media (max-width: 1179px) and (min-width: 821px) {
  .kpis .kpi { flex-basis: calc(33.333% - 8px); }
  .kpis:has(.kpi:nth-child(4):last-child) .kpi { flex-basis: calc(50% - 6px); }
}

@media (max-width: 820px) and (min-width: 521px) {
  .kpis .kpi { flex-basis: calc(50% - 6px); }
  /* An odd count leaves the last tile half-width beside a gap. Letting it
     take the whole row reads as intended rather than left over. */
  .kpis:has(.kpi:nth-child(odd):last-child) .kpi:last-child {
    flex-basis: 100%;
  }
}

@media (max-width: 520px) {
  .kpis .kpi { flex-basis: 100%; }
}

.kpi-label {
  font-size: 12px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .04em;
  font-weight: 600;
  margin-bottom: 6px;
}

.kpi-value {
  font-size: 25px;
  font-weight: 650;
  letter-spacing: -.02em;
  font-variant-numeric: tabular-nums;
  line-height: 1.15;
}

.kpi-value.na { color: var(--text-muted); font-size: 19px; font-weight: 500; }

.kpi-meta {
  margin-top: 6px;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.kpi-note { font-size: 11px; color: var(--text-muted); margin-top: 4px; }

.delta { font-weight: 600; font-variant-numeric: tabular-nums; }
.delta.up { color: var(--good); }
.delta.down { color: var(--bad); }
.delta.flat, .delta.na { color: var(--text-muted); }
.delta-vs { color: var(--text-muted); font-weight: 400; }

/* ---------------------------------------------------------------- charts */

.chart { width: 100%; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }

.legend {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 12px;
  font-size: 13px;
  color: var(--text-secondary);
}

.legend-item { display: flex; align-items: center; gap: 6px; }
.legend-swatch { width: 11px; height: 11px; border-radius: 3px; flex: none; }

.chart-tooltip {
  position: absolute;
  pointer-events: none;
  background: var(--surface-1);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 9px 11px;
  font-size: 12px;
  box-shadow: var(--shadow);
  z-index: 30;
  min-width: 130px;
  opacity: 0;
  transition: opacity .1s;
}

.chart-tooltip.visible { opacity: 1; }
.chart-tooltip .tt-date { font-weight: 650; margin-bottom: 5px; }
.chart-tooltip .tt-row { display: flex; align-items: center; gap: 6px; justify-content: space-between; }
.chart-tooltip .tt-label { display: flex; align-items: center; gap: 5px; color: var(--text-secondary); }
.chart-tooltip .tt-value { font-variant-numeric: tabular-nums; font-weight: 600; }

.chart-shell { position: relative; }

.empty-chart {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 220px;
  color: var(--text-muted);
  font-size: 14px;
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-sm);
  text-align: center;
  padding: 20px;
}

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

table { width: 100%; border-collapse: collapse; font-size: 14px; }

thead th {
  text-align: right;
  padding: 8px 10px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-muted);
  font-weight: 650;
  border-bottom: 1px solid var(--border-strong);
  white-space: nowrap;
}

thead th:first-child, tbody td:first-child { text-align: left; }

tbody td {
  padding: 9px 10px;
  border-bottom: 1px solid var(--border);
  text-align: right;
  font-variant-numeric: tabular-nums;
}

tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--surface-2); }

.table-scroll { overflow-x: auto; margin: 0 -20px; padding: 0 20px; }

/* ------------------------------------------------- sortable/filterable */

.table-tools {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}

.table-filter {
  font-size: 13.5px;
  padding: 6px 10px;
  min-width: 210px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface-1);
  color: var(--text-primary);
}

.table-count { font-size: 12.5px; font-variant-numeric: tabular-nums; }

th.sortable {
  cursor: pointer;
  user-select: none;
  position: relative;
  padding-right: 10px;
}

th.sortable:hover { color: var(--text-primary); }
th.sortable:focus-visible { outline: 2px solid var(--brand); outline-offset: -2px; }

/* Reserve the arrow's space permanently so sorting never shifts a column's
   width and nudges the whole table sideways. */
th.sortable::after {
  content: "↕";
  margin-left: 5px;
  opacity: .35;
  font-size: 10px;
}

th.sorted-asc::after { content: "↑"; opacity: 1; color: var(--brand); }
th.sorted-desc::after { content: "↓"; opacity: 1; color: var(--brand); }

@media print {
  .table-tools { display: none !important; }
  th.sortable::after { content: "" !important; }
}

td.name, th.name { max-width: 340px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.muted { color: var(--text-muted); }

/* Period-on-period change beneath a table value. Small and quiet — it is
   supporting context for the number above it, not a second metric. */
.cell-delta {
  font-size: 11px;
  font-weight: 600;
  margin-top: 2px;
  font-variant-numeric: tabular-nums;
  line-height: 1.2;
}

.cell-delta.up { color: var(--good); }
.cell-delta.down { color: var(--bad); }
.cell-delta.flat, .cell-delta.muted { color: var(--text-muted); font-weight: 500; }

.chan {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-weight: 500;
}

.chan-dot { width: 9px; height: 9px; border-radius: 3px; flex: none; }

/* ---------------------------------------------------------------- forms */

.controls {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

select, input[type=text], input[type=email], input[type=password],
input[type=date], input[type=number], input[type=color], input[type=file] {
  font: inherit;
  font-size: 14px;
  padding: 7px 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface-1);
  color: var(--text-primary);
}

input[type=color] { padding: 2px; width: 44px; height: 34px; }

label { font-size: 13px; color: var(--text-secondary); font-weight: 500; }

.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 12px; }
.field-row { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }
.field-row .field { flex: 1; min-width: 170px; margin-bottom: 0; }

.btn {
  font: inherit;
  font-size: 14px;
  font-weight: 550;
  padding: 8px 15px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface-1);
  color: var(--text-primary);
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
  white-space: nowrap;
}

.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:hover { filter: brightness(1.08); background: var(--brand); }
.btn-sm { padding: 5px 10px; font-size: 13px; }

/* --------------------------------------------------------------- alerts */

.flash {
  padding: 11px 15px;
  border-radius: var(--radius-sm);
  margin-bottom: 10px;
  font-size: 14px;
  border: 1px solid;
  display: flex;
  gap: 9px;
  align-items: flex-start;
}

.flash-success { background: rgba(0, 131, 0, .09); border-color: rgba(0, 131, 0, .3); }
.flash-error { background: rgba(227, 73, 72, .09); border-color: rgba(227, 73, 72, .35); }
.flash-warning { background: rgba(237, 161, 0, .1); border-color: rgba(237, 161, 0, .35); }

.notice {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--warn);
  border-radius: var(--radius-sm);
  padding: 13px 15px;
  font-size: 13.5px;
  color: var(--text-secondary);
  margin-bottom: 20px;
}

.notice strong { color: var(--text-primary); }

.pill {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: .03em;
}

.pill-ok { background: rgba(0, 131, 0, .13); color: var(--good); }
.pill-off { background: var(--surface-2); color: var(--text-muted); }
.pill-err { background: rgba(227, 73, 72, .13); color: var(--bad); }

/* --------------------------------------------------------------- footer */

.coverage {
  font-size: 12px;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  padding-top: 14px;
  margin-top: 8px;
}

.coverage code { font-family: var(--mono); font-size: 11px; }

/* ---------------------------------------------------------------- login */

.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px;
  width: 100%;
  max-width: 380px;
  box-shadow: var(--shadow);
}

.login-card h1 { margin: 0 0 6px; font-size: 20px; }
.login-card .sub { color: var(--text-muted); font-size: 13.5px; margin-bottom: 22px; }
.login-card input { width: 100%; }
.login-card .btn { width: 100%; text-align: center; margin-top: 6px; }

/* --------------------------------------------------------------- print */

@media print {
  .topbar, .controls, .btn, form { display: none !important; }
  body { background: #fff; }
  .card { break-inside: avoid; box-shadow: none; }
}

@media (max-width: 700px) {
  .wrap { padding: 16px; }
  .topbar { padding: 10px 16px; }
  .kpi-value { font-size: 21px; }
}
