:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface-1: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e2e1dc;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #7a7974;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #8a5cd1;
  --grid: #e8e7e3;
  --good: #0ca30c;
  --good-ink: #006300;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --missing: #a3a29d;
  --shadow: 0 1px 2px rgb(0 0 0 / .04), 0 2px 8px rgb(0 0 0 / .04);
  --radius: 12px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "Cascadia Code", "SFMono-Regular", Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211; --surface-1: #1a1a19; --surface-2: #232321; --border: #33332f;
    --text-primary: #f0efec; --text-secondary: #c3c2b7; --text-muted: #8e8d86;
    --accent: #3987e5; --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --series-4: #c98500; --series-5: #9b74e0;
    --grid: #2b2b28; --good-ink: #5fd35f; --missing: #6b6a65; --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface-1: #1a1a19; --surface-2: #232321; --border: #33332f;
  --text-primary: #f0efec; --text-secondary: #c3c2b7; --text-muted: #8e8d86;
  --accent: #3987e5; --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --series-4: #c98500; --series-5: #9b74e0;
  --grid: #2b2b28; --good-ink: #5fd35f; --missing: #6b6a65; --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text-primary); font: 14px/1.5 var(--font); -webkit-font-smoothing: antialiased; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0; line-height: 1.25; }
h2 { font-size: 15px; margin: 0 0 12px; }
h3 { font-size: 13px; margin: 0 0 8px; color: var(--text-secondary); font-weight: 600; }
code, .mono { font-family: var(--mono); font-size: 12px; }

.topbar { position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 20px; padding: 10px 24px;
  background: color-mix(in srgb, var(--surface-1) 92%, transparent); backdrop-filter: blur(8px); border-bottom: 1px solid var(--border); }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; color: var(--text-primary); }
.brand:hover { text-decoration: none; }
.brand svg { width: 26px; height: 26px; }
.brand svg rect { fill: var(--accent); }
.brand svg path { stroke: #fff; stroke-width: 3; fill: none; stroke-linecap: round; stroke-linejoin: round; }
nav { display: flex; gap: 4px; flex: 1; }
nav a { padding: 6px 12px; border-radius: 8px; color: var(--text-secondary); font-weight: 500; }
nav a:hover { background: var(--surface-2); text-decoration: none; }
nav a.active { background: var(--surface-2); color: var(--text-primary); }

main { max-width: 1280px; margin: 0 auto; padding: 24px; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head .sub { color: var(--text-secondary); margin-top: 4px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grid { display: grid; gap: 16px; }
.grid > * { min-width: 0; }
.grid.kpis { grid-template-columns: repeat(auto-fill, minmax(min(170px, 100%), 1fr)); }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(min(420px, 100%), 1fr)); }
.panel { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); min-width: 0; }
#view > :is(.panel, .grid, .callout) + :is(.panel, .grid, .callout) { margin-top: 16px; }
.panel-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.panel-head h2 { margin: 0; }
.muted { color: var(--text-muted); }
.secondary { color: var(--text-secondary); }
.small { font-size: 12px; }

.kpi .label { color: var(--text-secondary); font-size: 12px; }
.kpi .value { font-size: 24px; font-weight: 650; letter-spacing: -.01em; font-variant-numeric: tabular-nums; margin-top: 2px; }
.kpi .foot { margin-top: 4px; font-size: 12px; color: var(--text-muted); display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }

button, .btn { font: inherit; border: 1px solid var(--border); background: var(--surface-1); color: var(--text-primary);
  padding: 7px 14px; border-radius: 8px; cursor: pointer; font-weight: 500; }
button:hover { background: var(--surface-2); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
button.primary:hover { filter: brightness(1.06); }
button.danger { color: var(--critical); }
button.ghost { border-color: transparent; background: transparent; }
button:disabled { opacity: .55; cursor: progress; }
input, select, textarea { font: inherit; color: var(--text-primary); background: var(--surface-1); border: 1px solid var(--border);
  border-radius: 8px; padding: 7px 10px; min-width: 0; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input[type="url"] { flex: 1 1 320px; }
textarea { width: 100%; font-family: var(--mono); font-size: 12px; }
label.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-secondary); }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; }

.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--border); background: var(--surface-1); border-radius: 999px; padding: 4px 12px; font-size: 13px; cursor: pointer; color: var(--text-secondary); }
.chip.active { border-color: var(--accent); color: var(--text-primary); background: color-mix(in srgb, var(--accent) 12%, var(--surface-1)); }

.badge { display: inline-flex; align-items: center; gap: 4px; border-radius: 6px; padding: 1px 7px; font-size: 11.5px; font-weight: 600;
  border: 1px solid var(--border); color: var(--text-secondary); background: var(--surface-2); white-space: nowrap; }
.badge .dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.badge.observed { color: var(--text-primary); }
.badge.computed { color: var(--text-secondary); border-style: solid; }
.badge.estimated { color: var(--text-secondary); border-style: dashed; background: transparent; }
.badge.missing { color: var(--text-muted); border-style: dotted; background: transparent; }
.badge.status-ok { color: var(--good-ink); }
.badge.status-warn { color: var(--text-primary); }
.badge.status-bad { color: var(--critical); }
.sev { display: inline-flex; align-items: center; gap: 6px; font-variant-numeric: tabular-nums; font-weight: 600; }
.sev i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; }
.na { color: var(--text-muted); font-style: italic; }
.src-dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 4px; vertical-align: 0; }

table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th { text-align: left; font-size: 12px; font-weight: 600; color: var(--text-secondary); padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
td { padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
td.num, th.num { text-align: right; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }
.table-wrap { overflow-x: auto; margin: 0 -4px; }
.up { color: var(--good-ink); }
.down { color: var(--critical); }

.chart-box { position: relative; height: 260px; }
.chart-box.short { height: 200px; }
.legend-note { font-size: 12px; color: var(--text-muted); margin-top: 8px; display: flex; gap: 12px; flex-wrap: wrap; }
.legend-note .ln { display: inline-block; width: 18px; height: 0; border-top: 2px solid var(--text-secondary); vertical-align: middle; margin-right: 4px; }
.legend-note .ln.dash { border-top-style: dashed; }

.event { border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; background: var(--surface-1); }
.event + .event { margin-top: 10px; }
.event .meta { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; font-size: 12px; color: var(--text-muted); }
.event .title { font-weight: 650; margin: 4px 0; }
.event p { margin: 6px 0 0; color: var(--text-secondary); }
.empty { padding: 28px; text-align: center; color: var(--text-muted); }
.callout { border: 1px solid var(--border); border-left: 3px solid var(--accent); background: var(--surface-2); padding: 10px 12px; border-radius: 8px; color: var(--text-secondary); font-size: 13px; }
.callout.warn { border-left-color: var(--warning); }
.candidates label { display: flex; gap: 10px; padding: 8px; border: 1px solid var(--border); border-radius: 8px; cursor: pointer; align-items: flex-start; }
.candidates label + label { margin-top: 6px; }
.candidates label:has(input:checked) { border-color: var(--accent); }

.switch { position: relative; display: inline-block; width: 36px; height: 20px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; background: var(--border); border-radius: 999px; transition: .15s; cursor: pointer; }
.switch span::before { content: ""; position: absolute; width: 16px; height: 16px; left: 2px; top: 2px; background: #fff; border-radius: 50%; transition: .15s; }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::before { transform: translateX(16px); }

#toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(20px); opacity: 0; transition: .2s;
  background: var(--text-primary); color: var(--surface-1); padding: 10px 16px; border-radius: 8px; max-width: calc(100vw - 32px); pointer-events: none; }
#toast.show { opacity: 1; transform: translateX(-50%); }
dialog { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface-1); color: var(--text-primary); max-width: min(760px, calc(100vw - 32px)); width: 100%; }
dialog::backdrop { background: rgb(0 0 0 / .35); }
dialog pre { max-height: 50vh; overflow: auto; background: var(--surface-2); padding: 10px; border-radius: 8px; font-size: 12px; }
dialog img { max-width: 100%; border-radius: 8px; border: 1px solid var(--border); }
details summary { cursor: pointer; color: var(--text-secondary); font-size: 13px; }

@media (max-width: 720px) {
  .topbar { padding: 10px 16px; gap: 10px; }
  .brand span { display: none; }
  main { padding: 16px; }
  .grid.two { grid-template-columns: 1fr; }
  .hide-sm { display: none; }
  .kpi .value { font-size: 20px; }
}
