:root {
  --bg: #f1f5f9;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --accent: #2563eb;
  --node: #9333ea;
  --accent-text: #ffffff;
  --good: #16a34a;
  --ok: #ca8a04;
  --weak: #ea580c;
  --bad: #dc2626;
  --seg-bg: #e2e8f0;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a;
    --card: #1e293b;
    --text: #f1f5f9;
    --muted: #94a3b8;
    --border: #334155;
    --accent: #3b82f6;
    --node: #c084fc;
    --seg-bg: #334155;
    --good: #22c55e;
    --ok: #eab308;
    --weak: #f97316;
    --bad: #ef4444;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
header {
  position: sticky; top: 0; z-index: 1000;
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 16px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}
.title { font-weight: 700; }
.headRight { display: flex; align-items: center; gap: 8px; }
.helpLink {
  display: inline-flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; border-radius: 50%;
  background: var(--seg-bg); color: var(--text); font-weight: 700; text-decoration: none;
}
main { max-width: 720px; margin: 0 auto; padding: 12px 16px; display: grid; gap: 12px; }
.card { background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 14px; min-width: 0; }
.muted { color: var(--muted); font-weight: 400; }

button {
  font: inherit; border: 0; border-radius: 10px; padding: 10px 14px; cursor: pointer;
  min-height: 44px;
}
button.primary { background: var(--accent); color: var(--accent-text); font-weight: 600; }
button.secondary { background: var(--seg-bg); color: var(--text); }
button.danger { background: transparent; color: var(--bad); border: 1px solid var(--bad); }
button:disabled { opacity: .45; cursor: default; }

.connect { display: flex; gap: 8px; flex-wrap: wrap; }
.connect button { flex: 1 1 140px; }

.pill { font-size: 13px; padding: 4px 10px; border-radius: 999px; font-weight: 600; }
.pill.off { background: var(--seg-bg); color: var(--muted); }
.pill.on { background: var(--good); color: #fff; }
.pill.wait { background: var(--ok); color: #fff; }

.liveHead { display: flex; gap: 12px; align-items: baseline; font-weight: 700; font-size: 18px; }
.liveHead #liveAge { margin-left: auto; font-size: 14px; }
.metrics { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 12px 0; }
.metric { border-radius: 10px; padding: 10px; background: var(--seg-bg); border-left: 6px solid var(--border); min-width: 0; }
.metric .label { font-size: 13px; font-weight: 600; }
.metric .value { font-size: clamp(24px, 8vw, 36px); font-weight: 800; font-variant-numeric: tabular-nums; line-height: 1.15; white-space: nowrap; }
.metric .value small { font-size: 14px; font-weight: 600; color: var(--muted); margin-left: 3px; }
.metric .sub { font-size: 14px; color: var(--muted); font-variant-numeric: tabular-nums; }
.metric.good { border-left-color: var(--good); }
.metric.ok { border-left-color: var(--ok); }
.metric.weak { border-left-color: var(--weak); }
.metric.bad { border-left-color: var(--bad); }
.stats { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 14px; }
.lastEvent { margin-top: 8px; font-size: 14px; }

.rowLabel { font-weight: 600; margin: 4px 0 6px; }
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px; background: var(--seg-bg); padding: 4px; border-radius: 12px; margin-bottom: 10px; }
.seg button { background: transparent; color: var(--text); padding: 8px 4px; }
.seg button.active { background: var(--accent); color: var(--accent-text); font-weight: 700; }
.buttons { display: flex; gap: 8px; flex-wrap: wrap; }
.buttons button { flex: 1 1 auto; }

.selects { display: flex; gap: 8px; margin-bottom: 10px; }
select {
  flex: 1; min-width: 0; font: inherit; padding: 10px; min-height: 44px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
}
.rxList { display: grid; gap: 6px; max-height: 420px; overflow-y: auto; }
.rxItem { border-left: 4px solid var(--border); padding: 6px 10px; background: var(--seg-bg); border-radius: 8px; min-width: 0; }
.rxItem.good { border-left-color: var(--good); }
.rxItem.ok { border-left-color: var(--ok); }
.rxItem.weak { border-left-color: var(--weak); }
.rxItem.bad { border-left-color: var(--bad); }
.rxItem .rxHead { display: flex; justify-content: space-between; gap: 8px; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.rxItem .rxText { font-weight: 600; overflow-wrap: anywhere; }
.rxItem .rxHex { font: 11px/1.3 ui-monospace, Consolas, monospace; color: var(--muted); overflow-wrap: anywhere; }

.note { display: flex; gap: 8px; margin: 8px 0; }
.note input {
  flex: 1; min-width: 0; font: inherit; padding: 10px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
}
#map { height: 300px; border-radius: 10px; margin-top: 8px; }

.logHead { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 8px; }
.tableWrap { overflow-x: auto; margin-top: 8px; max-height: 360px; overflow-y: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
th, td { text-align: left; padding: 6px 6px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { position: sticky; top: 0; background: var(--card); }
td.ev-pong { color: var(--good); font-weight: 600; }
td.ev-timeout { color: var(--bad); font-weight: 600; }
td.ev-note { color: var(--accent); font-weight: 600; }
details { margin-top: 8px; }
pre { font-size: 12px; max-height: 200px; overflow: auto; background: var(--bg); padding: 8px; border-radius: 8px; white-space: pre-wrap; word-break: break-all; }
