:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f1f0ec;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #7a7873;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --accent: #2a78d6;
  --accent-wash: rgba(42, 120, 214, 0.10);
  --serious: #ec835a;
  --serious-wash: rgba(236, 131, 90, 0.16);
  --critical: #d03b3b;
  --warning: #fab219;
  --good: #0ca30c;
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.06), 0 4px 16px rgba(11, 11, 11, 0.04);
  --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 14px;
  line-height: 1.45;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #242422;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #9a988f;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --series-1: #3987e5;
    --series-2: #d95926;
    --accent: #6da7ec;
    --accent-wash: rgba(57, 135, 229, 0.16);
    --serious-wash: rgba(236, 131, 90, 0.20);
    --shadow: none;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #242422;
  --ink: #ffffff;
  --ink-2: #c3c2b7;
  --muted: #9a988f;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --series-1: #3987e5;
  --series-2: #d95926;
  --accent: #6da7ec;
  --accent-wash: rgba(57, 135, 229, 0.16);
  --serious-wash: rgba(236, 131, 90, 0.20);
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--page); color: var(--ink); }
body { min-height: 100vh; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, select { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; font-weight: 600; }
h1 { font-size: 22px; letter-spacing: -0.01em; }
h2 { font-size: 15px; }
.muted { color: var(--muted); }
.ink2 { color: var(--ink-2); }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.nowrap { white-space: nowrap; }
[hidden] { display: none !important; }

/* --- top bar ---------------------------------------------------------------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 24px;
  padding: 0 24px; height: 56px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 15px; white-space: nowrap; display: flex; align-items: center; gap: 8px; }
.brand-mark { width: 22px; height: 22px; border-radius: 6px; background: var(--series-1); display: inline-grid; place-items: center; color: #fff; font-size: 12px; }
.nav { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
.nav a {
  padding: 6px 12px; border-radius: 8px; color: var(--ink-2); white-space: nowrap; font-weight: 500;
}
.nav a:hover { background: var(--surface-2); text-decoration: none; }
.nav a[aria-current="page"] { background: var(--accent-wash); color: var(--ink); }
.userbox { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 13px; white-space: nowrap; }

main { max-width: 1320px; margin: 0 auto; padding: 20px 24px 64px; transition: opacity 0.15s; }
main.loading { opacity: 0.55; pointer-events: none; }

/* --- filters ------------------------------------------------------------------ */
.filters {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px;
  margin-bottom: 20px;
}
.control {
  height: 34px; padding: 0 10px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface);
}
select.control { padding-right: 28px; }
input.control[type="date"] { padding: 0 8px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface); height: 34px; }
.seg button { border: 0; background: none; padding: 0 12px; cursor: pointer; color: var(--ink-2); }
.seg button + button { border-left: 1px solid var(--border); }
.seg button[aria-pressed="true"] { background: var(--accent-wash); color: var(--ink); font-weight: 600; }
.btn {
  height: 34px; padding: 0 14px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--ink); font-weight: 500;
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn.primary { background: var(--series-1); border-color: transparent; color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { border-color: transparent; background: none; color: var(--ink-2); }
.btn.small { height: 28px; padding: 0 10px; font-size: 13px; }
.btn:disabled { opacity: 0.5; cursor: default; }

.combo { position: relative; }
.combo input { width: 220px; }
.combo-list {
  position: absolute; z-index: 30; top: 38px; left: 0; min-width: 280px; max-height: 320px; overflow: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow);
  padding: 4px; margin: 0; list-style: none;
}
.combo-list li { padding: 7px 10px; border-radius: 6px; cursor: pointer; display: flex; justify-content: space-between; gap: 16px; }
.combo-list li[aria-selected="true"], .combo-list li:hover { background: var(--surface-2); }
.chips { display: contents; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; height: 28px; padding: 0 6px 0 10px;
  border-radius: 14px; background: var(--accent-wash); font-size: 13px; white-space: nowrap;
}
.chip button { border: 0; background: none; cursor: pointer; color: var(--ink-2); width: 20px; height: 20px; border-radius: 10px; padding: 0; line-height: 1; }
.chip button:hover { background: var(--border); }

/* --- cards & tiles -------------------------------------------------------------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card + .card, .grid + .card, .card + .grid { margin-top: 16px; }
.card-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; padding: 16px 18px 8px; }
.card-head h2 { margin-right: auto; }
.card-body { padding: 8px 18px 18px; }
.card-note { padding: 0 18px 12px; color: var(--ink-2); max-width: 820px; }

.tiles { display: grid; grid-template-columns: 1.4fr repeat(3, 1fr); gap: 16px; margin-bottom: 16px; }
.tile { padding: 16px 18px; }
.tile .label { color: var(--ink-2); font-size: 13px; }
.tile .value { font-size: 26px; font-weight: 600; margin-top: 4px; letter-spacing: -0.01em; }
.tile.hero .value { font-size: 48px; line-height: 1.05; }
.tile .sub { color: var(--muted); font-size: 13px; margin-top: 4px; }

.legend { display: flex; gap: 16px; color: var(--ink-2); font-size: 13px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.swatch { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }

/* --- chart --------------------------------------------------------------------- */
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; overflow: visible; }
.chart .grid line { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; shape-rendering: crispEdges; }
.chart text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .hit { fill: transparent; cursor: pointer; outline: none; }
.chart .hit:hover, .chart .hit:focus-visible { fill: var(--surface-2); }
.chart .s1 { fill: var(--series-1); }
.chart .s2 { fill: var(--series-2); }
.chart .empty { fill: var(--muted); font-size: 13px; }

.tooltip {
  position: fixed; z-index: 50; pointer-events: none; min-width: 170px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 6px 24px rgba(0, 0, 0, 0.14);
  padding: 10px 12px; font-size: 13px;
}
.tooltip .tt-title { color: var(--ink-2); margin-bottom: 6px; }
.tooltip .tt-row { display: grid; grid-template-columns: 14px auto 1fr; align-items: center; gap: 8px; }
.tooltip .tt-row b { font-variant-numeric: tabular-nums; }
.tooltip .tt-row span:last-child { color: var(--ink-2); }
.tooltip .key { width: 12px; height: 2px; border-radius: 1px; }
.tooltip .tt-foot { margin-top: 6px; padding-top: 6px; border-top: 1px solid var(--border); color: var(--ink-2); }

/* --- tables ---------------------------------------------------------------------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--grid); vertical-align: top; }
th { font-weight: 500; color: var(--ink-2); font-size: 13px; white-space: nowrap; position: sticky; top: 0; background: var(--surface); }
th.num, td.num { text-align: right; }
th button { border: 0; background: none; padding: 0; cursor: pointer; color: inherit; font-weight: inherit; }
th[aria-sort] button::after { content: ""; margin-left: 4px; }
th[aria-sort="descending"] button::after { content: "↓"; }
th[aria-sort="ascending"] button::after { content: "↑"; }
tbody tr:hover { background: var(--surface-2); }
tr.clickable { cursor: pointer; }
td.wrap { max-width: 360px; }
.src { color: var(--muted); font-size: 12px; }
.table-foot { padding: 10px 10px 0; color: var(--muted); font-size: 13px; }

/* --- relation badges: icon + label, never colour alone -------------------------------- */
.badge {
  display: inline-flex; align-items: center; gap: 5px; height: 22px; padding: 0 8px; border-radius: 11px;
  font-size: 12px; font-weight: 500; white-space: nowrap; color: var(--ink); background: var(--surface-2);
}
.badge .ico { font-size: 12px; line-height: 1; }
.badge.declared { background: var(--accent-wash); }
.badge.declared .ico { color: var(--accent); }
.badge.undeclared { background: var(--serious-wash); }
.badge.undeclared .ico { color: var(--critical); }
.badge.noacct .ico { color: var(--muted); }
.badge.sev-error { background: rgba(208, 59, 59, 0.14); }
.badge.sev-warning { background: rgba(250, 178, 25, 0.18); }
.badge.sev-info { background: var(--surface-2); }

/* --- person ------------------------------------------------------------------------- */
.crumb { display: inline-block; margin-bottom: 10px; color: var(--ink-2); }
.person-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 4px; }
.aliases { color: var(--muted); margin-bottom: 16px; }
.users { display: flex; flex-direction: column; gap: 4px; }
.users div { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* --- data quality --------------------------------------------------------------------- */
.qgrid { display: grid; grid-template-columns: 320px 1fr; gap: 16px; align-items: start; }
.codes { padding: 8px; }
.codes button {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; width: 100%; text-align: left;
  border: 0; background: none; border-radius: 8px; padding: 8px 10px; cursor: pointer;
}
.codes button:hover { background: var(--surface-2); }
.codes button[aria-pressed="true"] { background: var(--accent-wash); }
.codes .help { grid-column: 1 / -1; color: var(--muted); font-size: 12px; }
.codes h3 { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; padding: 10px 10px 4px; }

/* --- login --------------------------------------------------------------------------- */
.login { max-width: 360px; margin: 12vh auto; padding: 28px; }
.login h1 { margin-bottom: 4px; }
.login form { display: grid; gap: 12px; margin-top: 20px; }
.login label { display: grid; gap: 4px; color: var(--ink-2); font-size: 13px; }
.login .control { height: 40px; }
.error { color: var(--critical); }

.upload { display: grid; gap: 12px; max-width: 560px; }
.result { white-space: pre-wrap; font-size: 13px; color: var(--ink-2); }

@media (max-width: 900px) {
  .tiles { grid-template-columns: 1fr 1fr; }
  .tile.hero, .tiles .tile:last-child:nth-child(even) { grid-column: 1 / -1; }
  .qgrid { grid-template-columns: 1fr; }
  .userbox .lastimport { display: none; }
}
@media (max-width: 600px) {
  .topbar { padding: 0 16px; gap: 12px; }
  main { padding: 16px 16px 48px; }
  .tile.hero .value { font-size: 40px; }
  .combo input { width: 100%; }
  .combo { flex: 1 1 100%; }
  .brand span:last-child { display: none; }
}
