:root {
  color-scheme: light;
  --bg: #f3f2ee;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #dedcd5;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #7a7973;
  --grid: #e6e4de;
  --accent: #2C5F8A;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948;
  --other: #a3a29c;
  --good: #0ca30c; --warning: #fab219; --serious: #ec835a; --critical: #d03b3b;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.04);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111110; --surface: #1a1a19; --surface-2: #232321; --border: #33332f;
    --text: #ffffff; --text-2: #c3c2b7; --muted: #8f8e86; --grid: #2b2b28;
    --accent: #5b9bd0;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --other: #6d6c66;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111110; --surface: #1a1a19; --surface-2: #232321; --border: #33332f;
  --text: #ffffff; --text-2: #c3c2b7; --muted: #8f8e86; --grid: #2b2b28;
  --accent: #5b9bd0;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --other: #6d6c66;
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
code { font: 12px ui-monospace, SFMono-Regular, Menlo, monospace; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.num { text-align: right; font-variant-numeric: tabular-nums; }

.top {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 14px max(16px, 2vw); background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 1000;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand h1 { margin: 0; font-size: 19px; letter-spacing: .2px; }
.brand .sub { color: var(--text-2); font-size: 12.5px; }
.top-right { display: flex; align-items: center; gap: 12px; }
.who { color: var(--text-2); font-size: 13px; }
.pill {
  display: inline-flex; align-items: center; gap: 7px; padding: 4px 10px; border-radius: 999px;
  background: var(--surface-2); border: 1px solid var(--border); font-size: 12.5px; color: var(--text-2);
}
.pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.pill.ok .dot { background: var(--good); box-shadow: 0 0 0 3px color-mix(in srgb, var(--good) 25%, transparent); }
.pill.bad .dot { background: var(--critical); }
.pill.stale .dot { background: var(--warning); }
button.ghost {
  background: transparent; color: var(--text-2); border: 1px solid var(--border);
  border-radius: 8px; padding: 5px 10px; font: inherit; font-size: 12.5px; cursor: pointer;
}
.ghost-link { color: var(--text-2); border: 1px solid var(--border); border-radius: 8px; padding: 5px 10px; font-size: 12.5px; text-decoration: none; }
.ghost-link:hover { color: var(--text); border-color: var(--muted); }
button.ghost:hover { color: var(--text); border-color: var(--muted); }

.wrap { width: min(1900px, 96vw); margin: 18px auto 40px; display: grid; gap: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: var(--shadow); padding: 14px 16px; min-width: 0;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 10px; flex-wrap: wrap; }
.card h2 { margin: 0; font-size: 14.5px; font-weight: 650; }

/* stat tiles */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; box-shadow: var(--shadow); }
.tile .lbl { color: var(--text-2); font-size: 12px; }
.tile .val { font-size: 26px; font-weight: 650; font-variant-numeric: tabular-nums; margin-top: 2px; }
.tile .val small { font-size: 14px; font-weight: 500; color: var(--text-2); margin-left: 3px; }
.tile .foot { color: var(--muted); font-size: 11.5px; margin-top: 2px; display: flex; gap: 6px; align-items: center; }
.status { display: inline-flex; align-items: center; gap: 5px; font-size: 11.5px; color: var(--text-2); }
.status i { width: 8px; height: 8px; border-radius: 2px; display: inline-block; }

/* map + side */
.grid-map { display: grid; grid-template-columns: minmax(0, 1fr) 380px; gap: 16px; }
.map-card { display: flex; flex-direction: column; }
#map { height: min(72vh, 760px); min-height: 420px; border-radius: 9px; border: 1px solid var(--border); background: var(--surface-2); }
.map-card .card-head { justify-content: flex-start; }
.map-card .card-head h2 { margin-right: auto; }
.layer-toggles { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12.5px; color: var(--text-2); }
.layer-toggles label { display: inline-flex; gap: 5px; align-items: center; cursor: pointer; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 18px; margin-top: 10px; font-size: 12px; color: var(--text-2); align-items: center; }
.legend .sw { display: inline-block; width: 11px; height: 11px; border-radius: 50%; margin-right: 6px; vertical-align: -1px; border: 2px solid var(--surface); box-shadow: 0 0 0 1px var(--border); }
.legend .ramp { display: inline-block; width: 90px; height: 6px; border-radius: 3px; margin: 0 6px; vertical-align: 1px; }
.side { display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.side .grow { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0; font-size: 13px; }
.kv dt { color: var(--text-2); }
.kv dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.nb-list { overflow: auto; max-height: 440px; flex: 1; }
.nb { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; padding: 7px 4px; border-bottom: 1px solid var(--grid); cursor: pointer; border-radius: 6px; }
.nb:hover { background: var(--surface-2); }
.nb .n { font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nb .d { color: var(--muted); font-size: 11.5px; }
.nb .snr { font-variant-numeric: tabular-nums; text-align: right; }
.bar { height: 4px; background: var(--surface-2); border-radius: 2px; grid-column: 1 / -1; overflow: hidden; }
.bar > span { display: block; height: 100%; border-radius: 2px; background: var(--s1); }

/* charts */
.charts { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.chart { height: 230px; position: relative; }
.chart svg { width: 100%; height: 100%; display: block; overflow: visible; }
.chart .empty { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: 13px; }
.axis text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.axis line.grid { stroke: var(--grid); stroke-width: 1; }
.axis line.base { stroke: var(--border); }
.series-legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--text-2); margin-top: 6px; }
.series-legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { background: transparent; border: 0; color: var(--text-2); padding: 4px 10px; font: inherit; font-size: 12px; cursor: pointer; }
.seg button.on { background: var(--surface-2); color: var(--text); font-weight: 600; }

/* feeds */
.feeds { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.5fr); gap: 16px; }
.send { display: grid; grid-template-columns: 1fr auto; gap: 6px 8px; margin-bottom: 12px; }
.send input { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font: inherit; min-width: 0; }
.send input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.send button { background: var(--accent); color: #fff; border: 0; border-radius: 8px; padding: 0 16px; font: inherit; font-weight: 600; cursor: pointer; }
.send button:disabled { opacity: .5; cursor: default; }
.send-meta { grid-column: 1 / -1; display: flex; justify-content: space-between; font-size: 11.5px; color: var(--muted); }
.send-meta .over { color: var(--critical); font-weight: 600; }
.send-status { grid-column: 1 / -1; font-size: 12px; min-height: 0; }
.send-status.err { color: var(--critical); }
.send-status.ok { color: var(--text-2); }
.admin-toggle { display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--text-2); padding: 3px 8px; border: 1px dashed var(--border); border-radius: 8px; cursor: pointer; }
.send.locked input, .send.locked button { opacity: .5; }
.msg.mine { background: color-mix(in srgb, var(--accent) 16%, var(--surface-2)); }
.table-wrap { overflow: auto; max-height: 420px; }
.tbl { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.tbl th { position: sticky; top: 0; background: var(--surface); text-align: left; color: var(--text-2); font-weight: 600; padding: 6px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.tbl td { padding: 5px 8px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
.tbl tr:hover td { background: var(--surface-2); }
.tbl.sortable th { cursor: pointer; user-select: none; }
.tbl.sortable th.asc::after { content: " \25B4"; }
.tbl.sortable th.desc::after { content: " \25BE"; }
.tag { display: inline-flex; align-items: center; gap: 6px; }
.tag i { width: 9px; height: 9px; border-radius: 2px; display: inline-block; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11.5px; color: var(--text-2); }
.msgs { max-height: 420px; overflow: auto; display: flex; flex-direction: column; gap: 8px; }
.msg { padding: 8px 10px; border-radius: 9px; background: var(--surface-2); }
.msg .h { display: flex; justify-content: space-between; gap: 10px; font-size: 11.5px; color: var(--muted); }
.msg .h b { color: var(--text); font-weight: 600; }
.msg .t { margin-top: 2px; word-wrap: break-word; }
.empty-note { color: var(--muted); font-size: 13px; padding: 20px 4px; text-align: center; }
#nodeSearch { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; font: inherit; font-size: 13px; width: min(320px, 60vw); }
.foot { text-align: center; padding: 6px; }

/* tooltip */
.tip {
  position: fixed; z-index: 5000; pointer-events: none; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: 12px;
  box-shadow: 0 6px 24px rgba(0,0,0,.18); min-width: 140px;
}
.tip .tt { color: var(--text-2); margin-bottom: 4px; }
.tip .row { display: flex; justify-content: space-between; gap: 14px; }
.tip .row i { display: inline-block; width: 9px; height: 9px; border-radius: 2px; margin-right: 6px; }
.tip .row b { font-variant-numeric: tabular-nums; }

/* leaflet theming */
.leaflet-container { font: inherit; background: var(--surface-2); }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--surface); color: var(--text); box-shadow: 0 6px 24px rgba(0,0,0,.25); }
.leaflet-popup-content { margin: 12px 14px; font-size: 12.5px; line-height: 1.5; }
.leaflet-popup-content h3 { margin: 0 0 4px; font-size: 14px; }
.leaflet-popup-content table { border-collapse: collapse; }
.leaflet-popup-content td { padding: 1px 12px 1px 0; }
.leaflet-popup-content td:first-child { color: var(--text-2); }
.leaflet-control-attribution { background: color-mix(in srgb, var(--surface) 80%, transparent) !important; color: var(--muted) !important; }
.leaflet-control-attribution a { color: var(--text-2) !important; }
.leaflet-bar a { background: var(--surface); color: var(--text); border-color: var(--border); }
.node-label { background: transparent; border: 0; box-shadow: none; color: var(--text); font-weight: 600; font-size: 11.5px; text-shadow: 0 0 3px var(--surface), 0 0 3px var(--surface), 0 0 3px var(--surface); }
.node-label::before { display: none; }

@media (max-width: 1200px) {
  .grid-map { grid-template-columns: 1fr; }
  .feeds { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .charts { grid-template-columns: 1fr; }
  .top { flex-wrap: wrap; }
  .who { display: none; }
  .wrap { width: auto; margin: 12px 16px 32px; }
  #map { height: 60vh; }
  .tile .val { font-size: 22px; }
}
