:root {
  color-scheme: light;
  --bg: #f4f3f0;
  --surface-1: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e2e1dc;
  --grid: #e8e7e2;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #77766f;
  --accent: #0b0b0b;
  --accent-ink: #fcfcfb;
  /* Platforms take the first three categorical slots (they validate all-pairs). */
  --youtube: #eb6834;
  --instagram: #2a78d6;
  --tiktok: #1baf7a;
  --good: #006300;
  --critical: #d03b3b;
  --warning-bg: #fdf1d6;
  --critical-bg: #fbe4e2;
  --radius: 10px;
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211;
    --surface-1: #1a1a19;
    --surface-2: #232321;
    --border: #2e2e2b;
    --grid: #2a2a27;
    --text-primary: #f0efec;
    --text-secondary: #c3c2b7;
    --text-muted: #8f8e86;
    --accent: #f0efec;
    --accent-ink: #121211;
    --youtube: #d95926;
    --instagram: #3987e5;
    --tiktok: #199e70;
    --good: #3fbf3f;
    --critical: #e66767;
    --warning-bg: #3a2f12;
    --critical-bg: #3a1b1a;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211;
  --surface-1: #1a1a19;
  --surface-2: #232321;
  --border: #2e2e2b;
  --grid: #2a2a27;
  --text-primary: #f0efec;
  --text-secondary: #c3c2b7;
  --text-muted: #8f8e86;
  --accent: #f0efec;
  --accent-ink: #121211;
  --youtube: #d95926;
  --instagram: #3987e5;
  --tiktok: #199e70;
  --good: #3fbf3f;
  --critical: #e66767;
  --warning-bg: #3a2f12;
  --critical-bg: #3a1b1a;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text-primary);
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--text-muted); }
.secondary { color: var(--text-secondary); }

/* --- top bar --- */
header.top {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 12px max(16px, calc((100vw - 1240px) / 2));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 17px; letter-spacing: -0.02em; margin-right: auto; }
.brand span { color: var(--text-muted); font-weight: 500; }
main { max-width: 1240px; margin: 0 auto; padding: 20px 16px 64px; display: grid; gap: 20px; }

button, select, input {
  font: inherit; color: inherit;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface-1); padding: 7px 12px;
}
button { cursor: pointer; }
button:hover { background: var(--surface-2); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.primary:hover { opacity: 0.88; }
button.link { border: 0; background: none; padding: 2px 4px; color: var(--text-secondary); text-decoration: underline; }
button:disabled { opacity: 0.5; cursor: default; }
:focus-visible { outline: 2px solid var(--instagram); outline-offset: 2px; }

.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.segmented button { border: 0; border-radius: 0; padding: 6px 11px; background: var(--surface-1); color: var(--text-secondary); }
.segmented button + button { border-left: 1px solid var(--border); }
.segmented button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }

.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  border-radius: 999px; padding: 5px 12px; color: var(--text-secondary);
}
.chip[aria-pressed="true"] { border-color: var(--text-primary); color: var(--text-primary); background: var(--surface-2); }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex: none; }

.banner { padding: 10px 14px; border-radius: var(--radius); background: var(--surface-2); border: 1px solid var(--border); }
.banner.error { background: var(--critical-bg); }
.banner[hidden] { display: none; }

/* --- cards --- */
.card {
  background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; min-width: 0;
}
.card h2 { margin: 0 0 2px; font-size: 15px; font-weight: 600; }
.card .sub { margin: 0 0 14px; color: var(--text-muted); font-size: 13px; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; }

.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.kpi .label { color: var(--text-secondary); font-size: 13px; }
.kpi .value { font-size: 30px; font-weight: 650; letter-spacing: -0.02em; margin: 2px 0; }
.kpi .delta { font-size: 13px; }
.kpi .split { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 10px; font-size: 12px; color: var(--text-secondary); }
.kpi .split span { display: inline-flex; align-items: center; gap: 5px; }
.up { color: var(--good); }
.down { color: var(--critical); }

.charts { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.chart-box { position: relative; height: 260px; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--text-secondary); margin-bottom: 8px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
details.as-table { margin-top: 10px; font-size: 12px; }
details.as-table summary { cursor: pointer; color: var(--text-muted); }
details.as-table .scroll { max-height: 220px; overflow: auto; margin-top: 6px; }

/* --- tables --- */
.table-wrap { overflow-x: auto; margin: 0 -18px; padding: 0 18px; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 10px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { font-weight: 500; color: var(--text-muted); font-size: 12px; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text-primary); }
th[aria-sort] { color: var(--text-primary); }
td.r, th.r { text-align: right; }
tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: var(--surface-2); }
.who { display: flex; align-items: center; gap: 10px; }
.avatar { width: 28px; height: 28px; border-radius: 50%; background: var(--surface-2); object-fit: cover; flex: none; }
.thumb { width: 36px; height: 64px; border-radius: 4px; background: var(--surface-2); object-fit: cover; flex: none; }
.title-cell { max-width: 340px; overflow: hidden; text-overflow: ellipsis; }
.title-cell a { text-decoration: none; }
.title-cell a:hover { text-decoration: underline; }
.status { font-size: 12px; }
.status.err { color: var(--critical); }
.bar-cell { width: 140px; }
.bar { height: 8px; border-radius: 4px; display: flex; gap: 2px; overflow: hidden; background: var(--surface-2); }
.bar i { display: block; height: 100%; }
td select { padding: 4px 6px; font-size: 13px; max-width: 170px; }

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

/* --- add-account dialog --- */
dialog {
  border: 1px solid var(--border); border-radius: 14px; padding: 22px;
  background: var(--surface-1); color: var(--text-primary); width: min(440px, calc(100vw - 32px));
}
dialog::backdrop { background: rgb(0 0 0 / 0.4); }
dialog h3 { margin: 0 0 14px; }
dialog .option { border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 10px; }
dialog .option p { margin: 4px 0 10px; color: var(--text-muted); font-size: 13px; }
dialog form.row { display: flex; gap: 8px; }
dialog form.row input { flex: 1; min-width: 0; }

/* --- public pages --- */
.center-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login { width: min(360px, 100%); display: grid; gap: 12px; }
.prose { max-width: 720px; margin: 0 auto; padding: 40px 16px 80px; }
.prose h1 { letter-spacing: -0.02em; }

@media (max-width: 900px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .charts { grid-template-columns: 1fr; }
}
@media (max-width: 520px) {
  .kpi .value { font-size: 24px; }
  .brand span { display: none; }
}
