:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f0f2f6;
  --border: #e1e5ec;
  --text: #17202e;
  --muted: #667085;
  --accent: #2f7de1;
  --ok: #19a974;
  --warn: #e0a106;
  --bad: #e5484d;

  --role-repeater: #2f7de1;
  --role-companion: #19a974;
  --role-room_server: #e8871e;
  --role-sensor: #9b5de5;
  --role-unknown: #98a2b3;

  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --radius: 12px;
  --map-tile-filter: none;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117;
    --surface: #161b22;
    --surface-2: #1c232d;
    --border: #2a323d;
    --text: #e6edf3;
    --muted: #8b96a5;
    --accent: #58a6ff;
    --role-repeater: #58a6ff;
    --role-companion: #3fb950;
    --role-room_server: #f0883e;
    --role-sensor: #bc8cff;
    --role-unknown: #6e7681;
    --shadow: none;
    --map-tile-filter: invert(1) hue-rotate(180deg) brightness(.95) contrast(.9) saturate(.6);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-variant-numeric: tabular-nums;
}
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -.01em; }
h2 { font-size: 15px; }
a { color: var(--accent); }
.muted { color: var(--muted); font-weight: 400; }
.small { font-size: 12px; }
.empty { color: var(--muted); padding: 24px 0; text-align: center; margin: 0; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 1000;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px max(16px, calc((100vw - 1400px) / 2));
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand h1 { font-size: 18px; }
.brand .sub { margin: 0; color: var(--muted); font-size: 12px; }
.logo { width: 34px; height: 34px; flex: none; }
.logo path { stroke: var(--role-repeater); stroke-width: 1.6; fill: none; opacity: .7; }
.logo .l-rep { fill: var(--role-repeater); }
.logo .l-cmp { fill: var(--role-companion); }
.logo .l-room { fill: var(--role-room_server); }

.conn {
  display: flex; align-items: center; gap: 8px; flex: none;
  padding: 6px 12px; border-radius: 999px;
  background: var(--surface-2); border: 1px solid var(--border);
  font-size: 12px; color: var(--muted);
}
.conn .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); }
.conn[data-state="online"] .dot { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 25%, transparent); }
.conn[data-state="degraded"] .dot { background: var(--warn); }
.conn[data-state="offline"] .dot { background: var(--bad); }

/* ---------- layout ---------- */
main {
  display: flex; flex-direction: column; gap: 16px;
  padding: 16px max(16px, calc((100vw - 1400px) / 2)) 32px;
}
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 14px 16px; min-width: 0;
}
.card-head, .section-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  margin-bottom: 12px;
}
.card-head.wrap { flex-wrap: wrap; }
.section-head { justify-content: flex-start; align-items: baseline; }

/* ---------- KPIs ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 12px 14px;
}
.kpi .label { color: var(--muted); font-size: 12px; display: flex; align-items: center; gap: 6px; }
.kpi .value { font-size: 26px; font-weight: 650; margin-top: 2px; letter-spacing: -.02em; }
.kpi .hint { color: var(--muted); font-size: 12px; }

.role-dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; flex: none; background: var(--role-unknown); }
.role-dot.repeater { background: var(--role-repeater); }
.role-dot.companion { background: var(--role-companion); }
.role-dot.room_server { background: var(--role-room_server); }
.role-dot.sensor { background: var(--role-sensor); }

/* ---------- map + feed ---------- */
.grid-main { display: grid; grid-template-columns: minmax(0, 2fr) minmax(300px, 1fr); gap: 16px; }
.map-card { display: flex; flex-direction: column; }
#map { height: 520px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-2); z-index: 0; }
.leaflet-tile-pane { filter: var(--map-tile-filter); }
.toggles { display: flex; gap: 14px; font-size: 13px; color: var(--muted); }
.toggles label { display: flex; align-items: center; gap: 5px; cursor: pointer; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin-top: 10px; font-size: 12px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend .line { width: 18px; height: 3px; border-radius: 2px; display: inline-block; }

.leaflet-container { font: inherit; }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--surface); color: var(--text); }
.leaflet-popup-content { margin: 12px 14px; font-size: 13px; }
.leaflet-container a.leaflet-popup-close-button { color: var(--muted); }
.leaflet-tooltip.node-label {
  background: transparent; border: 0; box-shadow: none; padding: 0;
  color: var(--text); font-size: 11px; font-weight: 600;
  text-shadow: 0 0 3px var(--bg), 0 0 3px var(--bg), 0 0 3px var(--bg);
}
.leaflet-tooltip.node-label::before { display: none; }
.popup h4 { margin: 0 0 4px; font-size: 14px; }
.popup .meta { color: var(--muted); font-size: 12px; margin-bottom: 6px; }
.popup button { margin-top: 8px; }

.feed-card { display: flex; flex-direction: column; max-height: 600px; }
.feed { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.feed li { padding: 8px 2px; border-bottom: 1px solid var(--border); font-size: 13px; }
.feed li:last-child { border-bottom: 0; }
.feed li.new { animation: flash 1.2s ease-out; }
@keyframes flash { from { background: color-mix(in srgb, var(--accent) 14%, transparent); } to { background: transparent; } }
.feed .row1 { display: flex; align-items: center; gap: 8px; }
.feed .time { color: var(--muted); font-size: 12px; margin-left: auto; flex: none; }
.feed .row2 { color: var(--muted); font-size: 12px; margin-top: 2px; overflow-wrap: anywhere; }
.badge {
  display: inline-block; padding: 1px 7px; border-radius: 6px; font-size: 11px; font-weight: 600;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); flex: none;
}
.badge.ADVERT { color: var(--role-companion); border-color: color-mix(in srgb, var(--role-companion) 40%, transparent); }
.badge.GRP_TXT, .badge.GRP_DATA { color: var(--role-room_server); border-color: color-mix(in srgb, var(--role-room_server) 40%, transparent); }
.badge.TEXT_MSG, .badge.REQ, .badge.RESPONSE, .badge.ACK, .badge.PATH { color: var(--role-repeater); border-color: color-mix(in srgb, var(--role-repeater) 40%, transparent); }
.live-dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--bad); margin-left: 4px; vertical-align: middle; animation: pulse 2s infinite; }
@keyframes pulse { 50% { opacity: .3; } }

.feed-new {
  font: inherit; font-size: 12px; font-weight: 600; cursor: pointer;
  color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent);
  border-radius: 8px; padding: 6px 10px; margin-bottom: 8px; text-align: left;
}
.feed-new:hover { background: color-mix(in srgb, var(--accent) 16%, var(--surface)); }

/* ---------- pagination ---------- */
.pager {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px;
  padding-top: 10px; font-size: 12px; color: var(--muted);
}
.pager:empty { display: none; }
.feed-card .pager { border-top: 1px solid var(--border); margin-top: 2px; }
.pager-pages { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; }
.page-btn {
  font: inherit; font-size: 12px; min-width: 30px; height: 30px; padding: 0 8px;
  color: var(--text); background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 7px; cursor: pointer;
}
.page-btn:hover:not(:disabled) { border-color: var(--accent); }
.page-btn[aria-current="page"] { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.page-btn:disabled { opacity: .4; cursor: default; }
.page-btn:focus-visible, .feed-new:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.page-gap { padding: 0 2px; }
.table-foot { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px; }
.table-foot .pager { flex: 1; }
.page-size { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); padding-top: 10px; }
.page-size select {
  font: inherit; font-size: 12px; color: var(--text); background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 7px; padding: 4px 6px;
}

/* ---------- charts ---------- */
.chart { min-height: 200px; }
.uplot { font-family: inherit; }
.u-legend { font-size: 12px; color: var(--muted); }
.u-legend .u-marker { border-radius: 2px; }

/* ---------- observers ---------- */
.obs-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); gap: 12px; }
.obs { cursor: pointer; transition: border-color .15s; }
.obs:hover { border-color: var(--accent); }
.obs .top { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.obs h3 { font-size: 15px; overflow-wrap: anywhere; }
.obs .meta { color: var(--muted); font-size: 12px; margin-top: 2px; }
.state { font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px; flex: none; }
.state.online { background: color-mix(in srgb, var(--ok) 15%, transparent); color: var(--ok); }
.state.stale { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.state.offline, .state.unknown { background: color-mix(in srgb, var(--bad) 14%, transparent); color: var(--bad); }
.obs .metrics { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 12px 0 6px; }
.obs .metric .k { color: var(--muted); font-size: 11px; }
.obs .metric .v { font-weight: 600; font-size: 15px; }
.spark { height: 56px; }

/* ---------- table ---------- */
.filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
input[type="search"] {
  font: inherit; color: var(--text); background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; width: 220px; max-width: 100%;
}
input[type="search"]:focus { outline: 2px solid color-mix(in srgb, var(--accent) 50%, transparent); outline-offset: 1px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  font: inherit; font-size: 12px; color: var(--muted); background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 999px; padding: 4px 10px; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px;
}
.chip[aria-pressed="true"] { color: var(--text); border-color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, var(--surface)); }
.table-wrap { overflow-x: auto; margin: 0 -16px; padding: 0 16px; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { font-size: 12px; font-weight: 600; color: var(--muted); cursor: pointer; user-select: none; }
th.sorted::after { content: " ↓"; }
th.sorted.asc::after { content: " ↑"; }
th.num, td.num { text-align: right; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--surface-2); }
td .name { display: inline-flex; align-items: center; gap: 8px; font-weight: 550; }
td .key { color: var(--muted); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11px; margin-left: 6px; }
.snr-good { color: var(--ok); }
.snr-mid { color: var(--warn); }
.snr-bad { color: var(--bad); }

/* ---------- dialog ---------- */
dialog {
  width: min(640px, calc(100vw - 32px)); max-height: calc(100vh - 64px);
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text); padding: 0;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .3);
}
dialog::backdrop { background: rgba(10, 14, 20, .5); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); }
.dlg-head h3 { display: flex; align-items: center; gap: 8px; overflow-wrap: anywhere; }
#dlg-body { padding: 14px 16px 18px; }
#dlg-body h4 { margin: 16px 0 6px; font-size: 13px; color: var(--muted); font-weight: 600; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 14px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.icon-btn, .btn {
  font: inherit; color: var(--text); background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 8px; cursor: pointer;
}
.icon-btn { width: 30px; height: 30px; font-size: 18px; line-height: 1; }
.btn { padding: 5px 12px; font-size: 13px; }
.btn:hover, .icon-btn:hover { border-color: var(--accent); }
dialog table th { cursor: default; }

footer { text-align: center; color: var(--muted); font-size: 12px; padding: 0 16px 24px; }

/* ---------- responsive ---------- */
@media (max-width: 960px) {
  .grid-main { grid-template-columns: 1fr; }
  #map { height: 420px; }
  .feed-card { max-height: 420px; }
}
@media (max-width: 560px) {
  .brand .sub { display: none; }
  .kpi .value { font-size: 22px; }
  #map { height: 340px; }
  input[type="search"] { width: 100%; }
  .filters { width: 100%; }
}
@media (prefers-reduced-motion: reduce) {
  .feed li.new, .live-dot { animation: none; }
}
