:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --accent: #2a78d6;
  --meter-track: #cde2fb;
  --warn-bg: #fff6e0;
  --warn-border: #fab219;
  --error-bg: #fdecec;
  --error-border: #d03b3b;
  /* Status colors: fixed across themes, always paired with an icon and label. */
  --critical: #d03b3b;
  --serious: #ec835a;
  --warning: #fab219;
  --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --text: #ffffff;
    --text-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.1);
    --series-1: #3987e5;
    --series-2: #d95926;
    --accent: #3987e5;
    --meter-track: #184f95;
    --warn-bg: #2e2508;
    --error-bg: #2e1212;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --text: #ffffff;
  --text-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.1);
  --series-1: #3987e5;
  --series-2: #d95926;
  --accent: #3987e5;
  --meter-track: #184f95;
  --warn-bg: #2e2508;
  --error-bg: #2e1212;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--page);
  color: var(--text);
  font-size: 14px;
  line-height: 1.45;
}

a { color: var(--accent); }
code { font-size: 12px; }
.muted { color: var(--muted); }

.top, .filters, main {
  max-width: 1320px;
  margin: 0 auto;
  padding-left: 24px;
  padding-right: 24px;
}

.top {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 16px;
  padding-top: 24px;
  flex-wrap: wrap;
}

h1 { font-size: 24px; margin: 0; font-weight: 650; }
h2 { font-size: 15px; margin: 0; font-weight: 600; }
.sub { margin: 4px 0 0; color: var(--text-2); }

.actions { display: flex; gap: 8px; }

.btn {
  display: inline-flex;
  align-items: center;
  height: 34px;
  padding: 0 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  text-decoration: none;
  cursor: pointer;
}
.btn:hover { border-color: var(--axis); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }

.filters {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding-top: 16px;
  padding-bottom: 16px;
}

.presets { display: flex; gap: 6px; flex-wrap: wrap; }

.chip {
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-2);
  text-decoration: none;
}
.chip:hover { color: var(--text); border-color: var(--axis); }
.chip.active { background: var(--text); color: var(--surface); border-color: var(--text); font-weight: 600; }

.custom { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.custom label { color: var(--text-2); display: flex; gap: 6px; align-items: center; }

input[type="date"], input[type="search"], select {
  height: 34px;
  padding: 0 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

main { padding-bottom: 40px; }

.banner {
  padding: 12px 16px;
  border-radius: var(--radius);
  margin-bottom: 16px;
  border: 1px solid;
}
.banner.error { background: var(--error-bg); border-color: var(--error-border); }
.banner.warn { background: var(--warn-bg); border-color: var(--warn-border); }

.kpis {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.tile, .card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.tile { padding: 14px 16px; }
.tile .label { color: var(--text-2); font-size: 13px; }
.tile .value { font-size: 28px; font-weight: 600; margin-top: 2px; }
.tile .note { color: var(--muted); font-size: 12px; margin-top: 2px; }

.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
  margin-bottom: 16px;
}
.grid .wide { grid-column: 1 / -1; }
section.card { margin-bottom: 16px; }

.card { padding: 16px; margin: 0; min-width: 0; }
figcaption p, .card-head p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }
.card-head { margin-bottom: 10px; }
.card-head.row { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; flex-wrap: wrap; }

.legend { display: flex; gap: 16px; margin: 10px 0 4px; color: var(--text-2); font-size: 12px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.key { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }
.key.line { height: 2px; width: 14px; border-radius: 1px; }
.key.s1 { background: var(--series-1); }
.key.s2 { background: var(--series-2); }

.chart { position: relative; width: 100%; }

.table-wrap { overflow: auto; max-height: 440px; }
.table-wrap.tall { max-height: 620px; }

table.data { width: 100%; border-collapse: collapse; font-size: 13px; }
table.data th, table.data td {
  padding: 7px 10px;
  border-bottom: 1px solid var(--grid);
  text-align: left;
  vertical-align: top;
}
table.data th {
  position: sticky;
  top: 0;
  background: var(--surface);
  color: var(--text-2);
  font-weight: 600;
  white-space: nowrap;
  z-index: 1;
}
table.sortable th { cursor: pointer; user-select: none; }
table.sortable th[aria-sort="ascending"]::after { content: " ▲"; font-size: 10px; }
table.sortable th[aria-sort="descending"]::after { content: " ▼"; font-size: 10px; }
table.data tbody tr:hover { background: color-mix(in srgb, var(--accent) 6%, transparent); }
.num { text-align: right !important; font-variant-numeric: tabular-nums; white-space: nowrap; }
.nowrap { white-space: nowrap; }
td.reason { min-width: 220px; }
td.empty { color: var(--muted); text-align: center; padding: 24px; }

.rate { display: inline-flex; align-items: center; gap: 8px; }
.meter { width: 48px; height: 6px; border-radius: 3px; background: var(--meter-track); overflow: hidden; }
.meter > span { display: block; height: 100%; background: var(--series-2); border-radius: 3px; }

.table-filters { display: flex; gap: 8px; flex-wrap: wrap; }
.table-filters input { width: 260px; }

.foot { font-size: 12px; margin-top: 8px; }

/* ---- Loading ---- */
.loading-card { max-width: 640px; margin: 24px 0; padding: 24px; }
.loading-card h2 { font-size: 18px; }
.progress { height: 8px; border-radius: 4px; background: var(--meter-track); overflow: hidden; margin: 14px 0 8px; }
.progress > span { display: block; height: 100%; background: var(--accent); border-radius: 4px; transition: width 0.4s ease; }
.loading-card .hint { margin-top: 14px; }
.loading-card #failed { margin-top: 14px; }

/* ---- Settings ---- */
main.settings, .settings-page .top { max-width: 920px; }
.banner.ok { background: color-mix(in srgb, var(--series-1) 10%, var(--surface)); border-color: var(--series-1); }
.banner ul { margin: 6px 0 0; padding-left: 18px; }
.settings .card { margin-bottom: 16px; }
.fields { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px 16px; }
.field { display: flex; flex-direction: column; gap: 6px; color: var(--text-2); font-size: 13px; }
.span-2 { grid-column: span 2; }
.span-4 { grid-column: 1 / -1; }
.field input, .field select, .field textarea, .url-row input {
  width: 100%;
  height: 36px;
  padding: 0 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--page);
  color: var(--text);
  font: inherit;
  font-size: 14px;
}
.field textarea { height: auto; padding: 8px 10px; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.check { display: flex; align-items: center; gap: 8px; color: var(--text); font-size: 14px; }
.field .check { font-size: 13px; color: var(--text-2); }
.field .check input, .check input { width: 16px; height: 16px; margin: 0; flex: none; }
.settings-page .top { padding-bottom: 16px; }
.hint { color: var(--muted); font-size: 12px; margin: 4px 0 0; }
.form-actions { display: flex; justify-content: flex-end; margin-bottom: 16px; }
.send-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 16px; }
.send-actions form, .url-row form { margin: 0; }
.settings h3 { font-size: 14px; margin: 8px 0 0; }
.url-row { display: flex; gap: 8px; margin-top: 8px; }
.url-row input { flex: 1; min-width: 0; }
.mono { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 12px !important; }
.status { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; border: 1px solid var(--border); }
.status-sent { color: var(--text); }
.status-sent::before { content: "✓ "; color: var(--series-1); }
.status-failed::before { content: "✕ "; color: var(--critical); }
.status-skipped::before { content: "– "; color: var(--muted); }

@media (max-width: 700px) {
  .fields { grid-template-columns: minmax(0, 1fr); }
  .span-2, .span-4 { grid-column: 1 / -1; }
  .url-row { flex-wrap: wrap; }
}

/* ---- Needs attention ---- */
.sev { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; color: var(--text); white-space: nowrap; }
.sev svg { flex: none; }
.sev-critical svg { color: var(--critical); }
.sev-attention svg { color: var(--serious); }
.sev-watch svg { color: var(--warning); }

.sev-counts { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.sev-counts > span { display: inline-flex; align-items: center; gap: 6px; }
.sev-counts .sev { font-weight: 500; color: var(--text-2); }

.attention-card { margin-bottom: 16px; }
table.attention td { padding-top: 10px; padding-bottom: 10px; }
table.attention ul { margin: 0; padding: 0; list-style: none; }
table.attention li + li { margin-top: 4px; }
ul.issues { min-width: 220px; }
ul.focus { min-width: 260px; color: var(--text-2); }
ul.focus li::before { content: "→ "; color: var(--muted); }
.th-sub { font-weight: 400; color: var(--muted); font-size: 11px; }
.small { font-size: 12px; }
.all-clear, .more { color: var(--text-2); margin: 4px 0 0; }
.attention-wrap { max-height: none; }

.rules { margin-top: 16px; }
.rules ul { margin: 8px 0 0; padding-left: 18px; color: var(--text-2); }
.rules li + li { margin-top: 4px; }

@media print {
  :root { color-scheme: light; }
  body { background: #fff; font-size: 11px; }
  .no-print { display: none !important; }
  .card, .tile { border-color: #ccc; break-inside: avoid; }
  table.attention tr { break-inside: avoid; }
  .top, .filters, main { max-width: none; padding-left: 0; padding-right: 0; }
  a { color: inherit; text-decoration: none; }
}

@media (max-width: 860px) {
  .grid { grid-template-columns: minmax(0, 1fr); }
  .top, .filters, main { padding-left: 16px; padding-right: 16px; }
  .table-filters input { width: 100%; }
}
