:root {
  color-scheme: light;
  --ground: #eef2f0;
  --surface: #ffffff;
  --sunk: #e5ebe8;
  --ink: #12201b;
  --ink-2: #48584f;
  --ink-3: #6b7c74;
  --rule: #d3dcd8;
  --grid: #e6ece9;
  --axis: #b7c3be;
  --accent: #0e6b5b;
  --accent-ink: #ffffff;
  --accent-soft: #d3eae3;
  --accent-wash: rgba(14, 107, 91, 0.08);
  /* chart pair validated with the dataviz palette checker (bars vs 7-day reference line) */
  --bar: #0e6b5b;
  --ref: #9aa6a1;
  --good: #1e7f45;
  --good-soft: #ddf0e3;
  --warn: #8f5c0c;
  --warn-soft: #f7e9cf;
  --crit: #b0302a;
  --crit-soft: #f8dedb;
  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-brand: "Archivo", "Arial Narrow", system-ui, sans-serif;
  --font-mono: ui-monospace, "Cascadia Mono", Consolas, "Liberation Mono", monospace;
}

/* Dark palette: applies when the OS is dark and the user hasn't picked Light,
   or when the user picked Dark (block below). Keep the two blocks in sync. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --ground: #0b1210;
    --surface: #131d1a;
    --sunk: #1a2622;
    --ink: #e3ebe7;
    --ink-2: #a3b3ac;
    --ink-3: #7f9189;
    --rule: #27352f;
    --grid: #1f2b27;
    --axis: #3a4a43;
    --accent: #4dbba4;
    --accent-ink: #04201a;
    --accent-soft: #163630;
    --accent-wash: rgba(77, 187, 164, 0.12);
    --bar: #3aa58f;
    --ref: #5f6f69;
    --good: #5cc486;
    --good-soft: #15301f;
    --warn: #e3a748;
    --warn-soft: #382a12;
    --crit: #f07169;
    --crit-soft: #3a1917;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --ground: #0b1210;
  --surface: #131d1a;
  --sunk: #1a2622;
  --ink: #e3ebe7;
  --ink-2: #a3b3ac;
  --ink-3: #7f9189;
  --rule: #27352f;
  --grid: #1f2b27;
  --axis: #3a4a43;
  --accent: #4dbba4;
  --accent-ink: #04201a;
  --accent-soft: #163630;
  --accent-wash: rgba(77, 187, 164, 0.12);
  --bar: #3aa58f;
  --ref: #5f6f69;
  --good: #5cc486;
  --good-soft: #15301f;
  --warn: #e3a748;
  --warn-soft: #382a12;
  --crit: #f07169;
  --crit-soft: #3a1917;
}

/* ------------------------------------------------------------ theme switch */
.theme-switch {
  display: inline-flex;
  gap: 2px;
  padding: 2px;
  border: 1px solid var(--rule);
  border-radius: 8px;
  background: var(--ground);
}
.theme-switch button {
  height: 26px;
  padding: 0 10px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--ink-2);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}
.theme-switch button:hover { color: var(--ink); }
.theme-switch button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.14);
}
.login-page .theme-switch { position: fixed; top: 16px; right: 16px; }

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--ground);
  color: var(--ink);
  font: 15px/1.5 var(--font-ui);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
button, input { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

/* ------------------------------------------------------------ brand */
.wordmark { display: flex; flex-direction: column; gap: 2px; }
.wm-main {
  font-family: var(--font-brand);
  font-stretch: 125%;
  font-weight: 800;
  letter-spacing: 0.02em;
  font-size: 56px;
  line-height: 0.9;
}
.wm-sub {
  font-family: var(--font-brand);
  font-stretch: 110%;
  font-weight: 600;
  font-size: 12px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  opacity: 0.75;
}
.wordmark.small { flex-direction: row; align-items: baseline; gap: 12px; }
.wordmark.small .wm-main { font-size: 22px; }
.wordmark.small .wm-sub { font-size: 11px; color: var(--ink-3); opacity: 1; }

/* ------------------------------------------------------------ sign in */
.login { min-height: 100vh; display: grid; place-items: center; padding-inline: 20px; padding-block: 40px; }
.login-card {
  width: 100%;
  max-width: 380px;
  display: flex;
  flex-direction: column;
  gap: 28px;
  padding: 32px 28px;
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: 10px;
}
.login-card .wm-main { font-size: 40px; color: var(--accent); }
.login-form { display: flex; flex-direction: column; gap: 18px; }
.login-form h1 { margin: 0 0 4px; font-family: var(--font-brand); font-stretch: 112%; font-weight: 750; font-size: 28px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 13px; font-weight: 600; color: var(--ink-2); }
.field input {
  height: 44px;
  padding: 0 12px;
  border: 1px solid var(--rule);
  border-radius: 6px;
  background: var(--surface);
}
.field input:focus { border-color: var(--accent); outline: 2px solid var(--accent-soft); outline-offset: 0; }
button.primary {
  height: 44px;
  margin-top: 4px;
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 650;
  cursor: pointer;
}
button.primary:hover { filter: brightness(1.08); }
.notice { margin: 0; padding: 10px 12px; border-radius: 6px; font-size: 14px; }
.notice.err { background: var(--crit-soft); color: var(--crit); }
.notice.ok { background: var(--accent-soft); color: var(--ink); }

/* ------------------------------------------------------------ dashboard frame */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--rule);
}
.topbar-right { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.me { font-size: 13px; color: var(--ink-2); }
.flag {
  font-size: 12px;
  font-weight: 650;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--warn-soft);
  color: var(--warn);
}
button.ghost {
  height: 32px;
  padding: 0 12px;
  border: 1px solid var(--rule);
  border-radius: 6px;
  background: transparent;
  cursor: pointer;
  font-size: 13px;
}
button.ghost:hover { background: var(--sunk); }

.wrap { max-width: 1280px; margin: 0 auto; padding-inline: 20px; padding-block: 24px 64px; display: flex; flex-direction: column; gap: 20px; }
.page-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px 20px; flex-wrap: wrap; }
.page-head h1 { margin: 0; font-family: var(--font-brand); font-stretch: 112%; font-weight: 750; font-size: 26px; }
.meta { margin: 0; font-size: 13px; color: var(--ink-3); }
.meta .sep { margin: 0 6px; }
.meta .stale { color: var(--crit); font-weight: 600; }

/* ------------------------------------------------------------ tiles */
.tiles { display: grid; grid-template-columns: 1.6fr repeat(4, minmax(0, 1fr)); gap: 12px; }
.tile {
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: 8px;
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}
.tile h2 { margin: 0; font-size: 13px; font-weight: 600; color: var(--ink-2); }
.t-value { margin: 0; font-size: 34px; font-weight: 650; line-height: 1.1; letter-spacing: -0.01em; }
.tile.hero .t-value { font-size: 52px; }
.t-foot { margin: auto 0 0; font-size: 13px; color: var(--ink-2); }
.tile[data-state="crit"] { background: var(--crit-soft); border-color: var(--crit); }
.tile[data-state="warn"] { border-color: var(--warn); }

.state { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; }
.state i {
  display: inline-grid;
  place-items: center;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  font-style: normal;
  font-size: 10px;
  font-weight: 800;
  color: var(--surface);
}
.state.good i { background: var(--good); }
.state.warn i { background: var(--warn); }
.state.crit i { background: var(--crit); }
.state.neutral i { background: var(--ink-3); }
.state.good { color: var(--good); }
.state.warn { color: var(--warn); }
.state.crit { color: var(--crit); }
.state.neutral { color: var(--ink-2); }
.delta { font-weight: 650; }
.delta.up { color: var(--good); }
.delta.down { color: var(--ink); }

@media (max-width: 1100px) {
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .tile.hero { grid-column: 1 / -1; }
}
@media (max-width: 460px) {
  .tiles { grid-template-columns: minmax(0, 1fr); }
  .tile.hero .t-value { font-size: 44px; }
}

/* ------------------------------------------------------------ panels & charts */
.panel { background: var(--surface); border: 1px solid var(--rule); border-radius: 8px; padding: 16px 18px; min-width: 0; }
.panel-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px 20px; flex-wrap: wrap; margin-bottom: 10px; }
.panel-head h2 { margin: 0; font-size: 16px; font-weight: 650; }
.panel-sub { margin: 2px 0 0; font-size: 13px; color: var(--ink-3); }
.split { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 3fr); gap: 20px; }
@media (max-width: 1000px) { .split { grid-template-columns: minmax(0, 1fr); } }

.legend { list-style: none; display: flex; gap: 16px; margin: 4px 0 0; padding: 0; font-size: 13px; color: var(--ink-2); }
.legend li { display: inline-flex; align-items: center; gap: 6px; }
.key { display: inline-block; flex: none; }
.key.k-today { width: 10px; height: 12px; border-radius: 3px 3px 0 0; background: var(--bar); }
.key.k-avg { width: 16px; height: 2px; border-radius: 1px; background: var(--ref); }

.chart { position: relative; min-height: 250px; }
.chart svg { display: block; overflow: visible; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .axis { stroke: var(--axis); stroke-width: 1; }
.chart .tick { fill: var(--ink-3); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .val { fill: var(--ink); font-size: 12px; font-weight: 650; }
.chart .bar { fill: var(--bar); }
.chart .bar.partial { fill: var(--bar); opacity: 0.45; }
.chart .ref-line { fill: none; stroke: var(--ref); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .hover-band { fill: var(--accent-wash); }
.chart .hit { fill: transparent; cursor: default; }

.tip {
  position: absolute;
  z-index: 2;
  pointer-events: none;
  min-width: 170px;
  padding: 8px 10px;
  border-radius: 6px;
  background: var(--surface);
  border: 1px solid var(--rule);
  box-shadow: 0 6px 18px rgba(10, 20, 17, 0.14);
  font-size: 13px;
}
.tip-title { font-weight: 650; margin-bottom: 4px; }
.tip-row { display: flex; align-items: center; gap: 8px; }
.tip-row .tip-k { color: var(--ink-2); flex: 1; }
.tip-row b { font-variant-numeric: tabular-nums; font-weight: 650; }

details.as-table { margin-top: 10px; font-size: 13px; }
details.as-table summary { cursor: pointer; color: var(--ink-2); }
details.as-table .tbl { margin-top: 8px; max-height: 320px; overflow-y: auto; }

/* ------------------------------------------------------------ tables */
.tbl { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th {
  text-align: left;
  font-size: 11px;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-3);
  padding: 8px 10px;
  border-bottom: 1px solid var(--rule);
  white-space: nowrap;
}
td { padding: 9px 10px; border-bottom: 1px solid var(--grid); vertical-align: top; }
tr:last-child td { border-bottom: 0; }
th.num, td.num { text-align: right; font-variant-numeric: tabular-nums; }
td.mono { font-family: var(--font-mono); font-size: 12.5px; white-space: nowrap; }
td.nowrap { white-space: nowrap; }
td .sub { display: block; font-size: 12px; color: var(--ink-3); font-family: var(--font-ui); }
td.err { color: var(--ink-2); font-size: 12.5px; min-width: 130px; }
.empty { display: flex; align-items: center; gap: 8px; padding: 14px 4px; color: var(--ink-2); }

.pill {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 650;
  white-space: nowrap;
}
.pill.good { background: var(--good-soft); color: var(--good); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.crit { background: var(--crit-soft); color: var(--crit); }
.pill.accent { background: var(--accent-soft); color: var(--accent); }
.pill.neutral { background: var(--sunk); color: var(--ink-2); }
