:root {
  color-scheme: light;
  --bg: #f4f4f1;
  --surface-1: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e2e1dc;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #7c7b76;
  --grid: #e7e6e1;
  --accent: #2a78d6;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948; --other: #8e8d87;
  --good: #0ca30c; --warning: #fab219; --critical: #d03b3b;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.04);
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --bg: #121211; --surface-1: #1a1a19; --surface-2: #232321; --border: #2e2e2b;
    --text-primary: #ffffff; --text-secondary: #c3c2b7; --text-muted: #8f8e86; --grid: #2a2a27;
    --accent: #3987e5;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767; --other: #77766f;
    --shadow: 0 1px 2px rgba(0,0,0,.3);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface-1: #1a1a19; --surface-2: #232321; --border: #2e2e2b;
  --text-primary: #ffffff; --text-secondary: #c3c2b7; --text-muted: #8f8e86; --grid: #2a2a27;
  --accent: #3987e5;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767; --other: #77766f;
  --shadow: 0 1px 2px rgba(0,0,0,.3);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text-primary);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
h1, h2 { margin: 0; font-weight: 650; letter-spacing: -.01em; }
h2 { font-size: 15px; }
.sub { margin: 2px 0 12px; color: var(--text-muted); font-size: 13px; }
.muted { color: var(--text-muted); }

/* header */
.top {
  position: sticky; top: 0; z-index: 1000;
  display: flex; gap: 16px; align-items: center; justify-content: space-between; flex-wrap: wrap;
  padding: 12px max(16px, calc(50% - 720px));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--border);
}
.brand { display: flex; gap: 12px; align-items: center; }
.brand svg { width: 34px; height: 34px; fill: var(--accent); }
.brand svg .w1, .brand svg .w2 { fill: none; stroke: var(--accent); stroke-width: 2; }
.brand svg .w1 { opacity: .55; } .brand svg .w2 { opacity: .25; }
.brand h1 { font-size: 18px; }
.brand p { margin: 0; color: var(--text-muted); font-size: 12.5px; }
.controls { display: flex; gap: 8px; align-items: center; }
.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 3px; }
.seg button, .layers button, .nodes button {
  font: inherit; font-size: 13px; color: var(--text-secondary); background: none; border: 0;
  padding: 6px 10px; border-radius: 7px; cursor: pointer; white-space: nowrap;
}
.seg button.on { background: var(--surface-1); color: var(--text-primary); box-shadow: var(--shadow); font-weight: 600; }
.icon { width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2); cursor: pointer; display: grid; place-items: center; }
.icon svg { width: 18px; height: 18px; fill: var(--text-secondary); }

/* node chips */
.nodes { display: flex; gap: 8px; flex-wrap: wrap; padding: 14px max(16px, calc(50% - 720px)) 0; }
.nodes button {
  display: inline-flex; gap: 8px; align-items: center;
  background: var(--surface-1); border: 1px solid var(--border); border-radius: 999px; padding: 6px 12px;
}
.nodes button.on { border-color: var(--accent); color: var(--text-primary); font-weight: 600; box-shadow: 0 0 0 1px var(--accent) inset; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--other); flex: none; }
.dot.on { background: var(--good); box-shadow: 0 0 0 3px color-mix(in srgb, var(--good) 25%, transparent); }

main { padding: 14px max(16px, calc(50% - 720px)) 8px; }

/* KPIs */
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(165px, 1fr)); gap: 12px; margin-bottom: 14px; }
.kpi { background: var(--surface-1); border: 1px solid var(--border); border-radius: 14px; padding: 14px 16px; box-shadow: var(--shadow); }
.kpi .l { color: var(--text-muted); font-size: 12.5px; }
.kpi .v { font-size: 26px; font-weight: 680; letter-spacing: -.02em; margin-top: 2px; font-variant-numeric: tabular-nums; }
.kpi .v small { font-size: 14px; font-weight: 500; color: var(--text-secondary); margin-left: 2px; }
.kpi .h { color: var(--text-secondary); font-size: 12.5px; margin-top: 2px; }

/* cards */
.card { background: var(--surface-1); border: 1px solid var(--border); border-radius: 16px; padding: 16px; box-shadow: var(--shadow); min-width: 0; }
.grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; margin-top: 14px; }
.card.wide { grid-column: 1 / -1; }
@media (max-width: 1100px) { .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 720px) { .grid { grid-template-columns: minmax(0, 1fr); } .kpi .v { font-size: 22px; } }
.chart { position: relative; height: 240px; }
.chart.tall { height: 280px; }
.empty { position: absolute; inset: 0; display: grid; place-items: center; color: var(--text-muted); font-size: 13px; text-align: center; pointer-events: none; }

/* map */
.mapcard { padding: 0; overflow: hidden; }
.maphead { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; padding: 12px 16px; }
.layers { display: flex; gap: 6px; flex-wrap: wrap; }
.layers button { border: 1px solid var(--border); background: var(--surface-2); }
.layers button.on { background: color-mix(in srgb, var(--accent) 16%, var(--surface-1)); border-color: color-mix(in srgb, var(--accent) 55%, var(--border)); color: var(--text-primary); }
#map { height: min(62vh, 560px); min-height: 340px; background: var(--surface-2); }
.maplegend { display: flex; gap: 16px; flex-wrap: wrap; align-items: center; padding: 10px 16px; font-size: 12.5px; color: var(--text-secondary); border-top: 1px solid var(--border); }
.maplegend .ramp { display: inline-block; width: 120px; height: 8px; border-radius: 4px; vertical-align: middle; margin: 0 6px; }
.maplegend i.sw { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 5px; vertical-align: -1px; }
.maplegend i.ln { display: inline-block; width: 18px; height: 0; border-top: 2px solid var(--accent); margin-right: 5px; vertical-align: 3px; }
.maplegend i.ln.dash { border-top-style: dashed; }
.leaflet-container { font: inherit; }
.leaflet-tooltip { background: var(--surface-1); color: var(--text-primary); border: 1px solid var(--border); box-shadow: var(--shadow); border-radius: 8px; }
.leaflet-tooltip-top:before { border-top-color: var(--surface-1); }
.rx-marker { width: 18px; height: 18px; border-radius: 50%; background: var(--accent); border: 3px solid var(--surface-1); box-shadow: 0 0 0 2px var(--accent); }
.rx-marker.online { animation: pulse 2.4s infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 2px var(--accent), 0 0 0 0 color-mix(in srgb, var(--accent) 50%, transparent); } 100% { box-shadow: 0 0 0 2px var(--accent), 0 0 0 18px transparent; } }

/* heat grid */
.heatgrid { display: grid; grid-template-columns: 34px repeat(24, minmax(0, 1fr)); gap: 2px; font-size: 11px; color: var(--text-muted); }
.heatgrid div { height: 22px; border-radius: 3px; }
.heatgrid .lab { background: none; display: flex; align-items: center; }
.heatgrid .hl { background: none; text-align: center; height: auto; }

/* tables */
.tbl { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
.tbl th { text-align: left; font-weight: 600; color: var(--text-muted); font-size: 12px; padding: 6px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.tbl td { padding: 7px 8px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
.tbl td.num, .tbl th.num { text-align: right; }
.tbl tr:last-child td { border-bottom: 0; }
.tblwrap { overflow-x: auto; }
.tag { display: inline-flex; align-items: center; gap: 6px; }
.tag i { width: 8px; height: 8px; border-radius: 2px; display: inline-block; }
.status { display: inline-flex; gap: 6px; align-items: center; }

/* feed */
.feed { list-style: none; margin: 0; padding: 0; max-height: 330px; overflow: auto; font-size: 13px; }
.feed li { display: grid; grid-template-columns: 58px minmax(0, 1fr) auto; gap: 8px; padding: 6px 2px; border-bottom: 1px solid var(--grid); align-items: center; }
.feed li:last-child { border-bottom: 0; }
.feed .t { color: var(--text-muted); font-variant-numeric: tabular-nums; }
.feed .r { color: var(--text-secondary); font-variant-numeric: tabular-nums; text-align: right; }
.feed li.new { animation: flash 1.2s ease-out; }
@keyframes flash { from { background: color-mix(in srgb, var(--accent) 18%, transparent); } to { background: transparent; } }

footer { padding: 16px max(16px, calc(50% - 720px)) 32px; color: var(--text-secondary); font-size: 13px; }
footer p { margin: 4px 0; max-width: 900px; }

/* OSM tiles, dimmed for dark mode */
:root[data-theme="dark"] .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(.85) contrast(.9) saturate(.6); }
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(.85) contrast(.9) saturate(.6); }
}
