:root {
  color-scheme: light;
  --bg: #f6f6f4;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e4e3de;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #74736d;
  --accent: #2a78d6;
  --accent-text: #ffffff;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --good: #0ca30c;
  --good-text: #006300;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --band: rgba(42, 120, 214, 0.07);
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 2px 8px rgba(0,0,0,.04);
  --radius: 16px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #242422;
    --border: #2f2f2c;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #9a998f;
    --accent: #3987e5;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --good-text: #3fc23f;
    --band: rgba(57, 135, 229, 0.10);
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #242422; --border: #2f2f2c;
  --text: #f0efec; --text-2: #c3c2b7; --muted: #9a998f; --accent: #3987e5;
  --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --series-4: #c98500;
  --good-text: #3fc23f; --band: rgba(57, 135, 229, 0.10); --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; letter-spacing: -0.01em; }
.topbar-right { display: flex; align-items: center; gap: 8px; }
.icon-btn {
  display: grid; place-items: center; width: 38px; height: 38px;
  border-radius: 50%; border: 1px solid var(--border); background: var(--surface); cursor: pointer;
}
.icon-btn.spin svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

main { max-width: 760px; margin: 0 auto; padding: 4px 16px calc(96px + env(safe-area-inset-bottom)); outline: none; }

.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
  display: flex; justify-content: space-around;
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
}
.tabbar a {
  flex: 1; max-width: 120px; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 6px 0; font-size: 11px; color: var(--muted); text-decoration: none; border-radius: 12px;
}
.tabbar svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.tabbar a.active { color: var(--accent); }

h1 { font-size: 24px; margin: 8px 0 4px; letter-spacing: -0.02em; }
h2 { font-size: 15px; margin: 0 0 10px; font-weight: 650; display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 2px 8px; }
h3 { font-size: 13px; margin: 14px 0 6px; color: var(--text-2); font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.est { font-size: 10.5px; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); border: 1px solid var(--border); border-radius: 6px; padding: 1px 5px; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; margin: 12px 0; box-shadow: var(--shadow);
}
.datebar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 6px 0 2px; }
.datebar .date { font-size: 20px; font-weight: 700; letter-spacing: -0.01em; text-align: center; flex: 1; }
.datebar button { width: 38px; height: 38px; border-radius: 50%; border: 1px solid var(--border); background: var(--surface); cursor: pointer; font-size: 18px; }
.datebar button:disabled { opacity: .35; cursor: default; }

.rings { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.ring { position: relative; aspect-ratio: 1; max-width: 150px; margin: 0 auto; width: 100%; }
.ring svg { width: 100%; height: 100%; }
.ring-track { fill: none; stroke: var(--surface-2); stroke-width: 8; }
.ring-fill { fill: none; stroke: var(--series-1); stroke-width: 8; stroke-linecap: round; transition: stroke-dasharray .6s ease; }
.ring.good .ring-fill { stroke: var(--good); }
.ring.moderate .ring-fill { stroke: var(--warning); }
.ring.low .ring-fill { stroke: var(--critical); }
.ring-text { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; }
.ring-value { font-size: clamp(20px, 6vw, 28px); font-weight: 700; letter-spacing: -0.02em; line-height: 1.1; }
.ring-label { font-size: 11.5px; color: var(--text-2); font-weight: 600; }
.ring-sub { font-size: 10.5px; color: var(--muted); }

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(138px, 1fr)); gap: 8px; }
.tile { background: var(--surface-2); border-radius: 12px; padding: 10px 12px; min-width: 0; }
.tile .k { font-size: 12px; color: var(--text-2); display: flex; justify-content: space-between; gap: 4px; }
.tile .v { font-size: 22px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.2; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile .v small { font-size: 13px; font-weight: 500; color: var(--text-2); margin-left: 2px; }
.tile .d { font-size: 12px; color: var(--muted); }

.notes { list-style: none; padding: 0; margin: 10px 0 0; display: grid; gap: 6px; }
.notes li { display: flex; gap: 8px; align-items: baseline; font-size: 13.5px; color: var(--text-2); }
.notes .dot { width: 8px; height: 8px; border-radius: 50%; flex: none; transform: translateY(-1px); }
.dot.up { background: var(--good); } .dot.down { background: var(--critical); } .dot.flat { background: var(--muted); }
.status { display: inline-flex; align-items: center; gap: 5px; font-size: 12.5px; font-weight: 600; }
.status::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.status.good { color: var(--good-text); } .status.moderate { color: #a86d00; } .status.low { color: var(--critical); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .status.moderate { color: var(--warning); } }

.zones { display: grid; gap: 6px; margin-top: 4px; }
.zone { display: grid; grid-template-columns: 92px 1fr 52px; gap: 8px; align-items: center; font-size: 12.5px; }
.zone .bar-bg { height: 10px; background: var(--surface-2); border-radius: 5px; overflow: hidden; }
.zone .bar-fg { height: 100%; background: var(--series-1); border-radius: 5px; }
.zone .n { text-align: right; color: var(--text-2); font-variant-numeric: tabular-nums; }

.chart { position: relative; width: 100%; touch-action: pan-y; }
.chart svg { display: block; overflow: visible; }
.chart .axis text { fill: var(--muted); font-size: 10.5px; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.chart .line { fill: none; stroke: var(--series-1); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .area { fill: var(--series-1); opacity: .10; }
.chart .dot { fill: var(--series-1); stroke: var(--surface); stroke-width: 2; }
.chart .band { fill: var(--band); }
.chart .refline { stroke: var(--text-2); stroke-width: 1; stroke-dasharray: 4 4; }
.chart .reflabel { fill: var(--text-2); font-size: 10.5px; }
.chart .cross { stroke: var(--text-2); stroke-width: 1; }
.chart .hover-dot { fill: var(--series-1); stroke: var(--surface); stroke-width: 2; }
.chart .bar { fill: var(--series-1); }
.chart .bar.s2 { fill: var(--series-2); } .chart .bar.s3 { fill: var(--series-3); } .chart .bar.s4 { fill: var(--series-4); }
.chart .bar-hover { fill: var(--surface-2); }
.chart .stage-deep { fill: var(--series-1); } .chart .stage-rem { fill: var(--series-2); }
.chart .stage-light { fill: var(--series-3); } .chart .stage-awake { fill: var(--series-4); }
.chart-tip {
  position: absolute; pointer-events: none; z-index: 5;
  background: var(--text); color: var(--bg); border-radius: 8px; padding: 6px 9px;
  font-size: 12px; line-height: 1.35; white-space: nowrap;
}
.chart-empty { padding: 28px 8px; text-align: center; color: var(--muted); font-size: 13px; background: var(--surface-2); border-radius: 12px; }

.legend { display: flex; flex-wrap: wrap; gap: 12px; font-size: 12px; color: var(--text-2); margin-top: 8px; }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }

.seg { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: transparent; padding: 5px 12px; border-radius: 8px; cursor: pointer; font-size: 13px; color: var(--text-2); }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); font-weight: 600; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 10px 16px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface); cursor: pointer; font-weight: 600; text-decoration: none; color: var(--text); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--critical); }
.btn:disabled { opacity: .5; cursor: default; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.form { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.form label { display: grid; gap: 4px; font-size: 12.5px; color: var(--text-2); }
.form input, .form select, .login input {
  padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg); width: 100%;
}

.banner { border-radius: 12px; padding: 10px 12px; font-size: 13.5px; margin: 10px 0; background: var(--surface-2); color: var(--text-2); }
.banner.warn { border-left: 4px solid var(--warning); }
.banner.err { border-left: 4px solid var(--critical); }
.banner.ok { border-left: 4px solid var(--good); }

.login { max-width: 360px; margin: 12vh auto 0; text-align: center; }
.login form { display: grid; gap: 10px; margin-top: 18px; }

table.diag { width: 100%; border-collapse: collapse; font-size: 12.5px; }
table.diag th, table.diag td { text-align: left; padding: 7px 6px; border-bottom: 1px solid var(--border); vertical-align: top; }
table.diag th { color: var(--text-2); font-weight: 600; }
table.diag td.err { color: var(--critical); word-break: break-word; }
details summary { cursor: pointer; color: var(--text-2); font-size: 12.5px; }
pre { background: var(--surface-2); padding: 8px; border-radius: 8px; font-size: 11px; overflow-x: auto; max-height: 260px; }
.list { display: grid; gap: 8px; }
.ex { display: flex; justify-content: space-between; gap: 8px; background: var(--surface-2); border-radius: 12px; padding: 10px 12px; font-size: 13.5px; }
.loading { padding: 60px 0; text-align: center; color: var(--muted); }
.grid2 { display: grid; grid-template-columns: 1fr; gap: 0 12px; }
@media (min-width: 720px) { .grid2 { grid-template-columns: 1fr 1fr; } }
