:root {
  --bg: #0f1720;
  --panel: #16212c;
  --panel-2: #1d2a37;
  --line: #2a3a4a;
  --text: #e8eef4;
  --muted: #8ea1b3;
  --accent: #3aa0ff;
  --green: #2fbf71;
  --amber: #f0b429;
  --red: #e5484d;
  --grey: #6b7c8c;
  --radius: 12px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body { display: flex; flex-direction: column; overflow: hidden; -webkit-tap-highlight-color: transparent; }
button { font: inherit; color: inherit; }
h2, h3 { margin: 0; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 0.8rem; }

/* top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: #0b1219; border-bottom: 1px solid var(--line); flex: none;
}
.brand { font-size: 1.05rem; display: flex; gap: 8px; align-items: baseline; }
.brand-nexion { font-weight: 700; letter-spacing: 0.02em; }
.brand-sep { color: var(--muted); }
.top-right { position: relative; }
.link-btn {
  display: flex; align-items: center; gap: 8px; padding: 7px 12px; border-radius: 999px;
  background: var(--panel-2); border: 1px solid var(--line); cursor: pointer; min-height: 36px;
}
.link-btn .dot { width: 12px; height: 12px; border-radius: 50%; background: var(--grey); box-shadow: 0 0 0 3px rgba(255,255,255,0.06); }
.link-btn[data-color="green"] .dot { background: var(--green); }
.link-btn[data-color="amber"] .dot { background: var(--amber); }
.link-btn[data-color="red"] .dot { background: var(--red); }
.link-pop {
  position: absolute; right: 0; top: calc(100% + 8px); z-index: 30; width: min(300px, calc(100vw - 32px));
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.4);
}
.link-pop p { margin: 0 0 6px; }
.text-btn { background: none; border: none; color: var(--accent); padding: 6px 0; cursor: pointer; }

/* layout: map left, radio right; stacked on phones */
.layout { flex: 1; display: grid; grid-template-columns: 1fr minmax(320px, 420px); min-height: 0; }
.map-pane { position: relative; min-height: 0; }
#map { position: absolute; inset: 0; }
.radio-pane {
  background: var(--panel); border-left: 1px solid var(--line); padding: 16px; overflow-y: auto;
  display: flex; flex-direction: column; gap: 14px;
}
@media (max-width: 760px) {
  body { overflow: auto; }
  .layout { grid-template-columns: 1fr; grid-template-rows: 46vh auto; }
  .radio-pane { border-left: none; border-top: 1px solid var(--line); overflow: visible; }
}

.stale {
  position: absolute; left: 12px; bottom: 28px; z-index: 5; padding: 4px 10px; border-radius: 999px;
  background: rgba(11,18,25,0.8); color: var(--muted); font-size: 0.8rem;
}

/* marker */
.marker { display: flex; flex-direction: column; align-items: center; cursor: pointer; }
.marker .pin {
  width: 22px; height: 22px; border-radius: 50%; background: var(--accent); border: 3px solid #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,0.45);
}
.marker .tag {
  margin-top: 4px; padding: 2px 8px; border-radius: 6px; background: #0b1219; color: #fff; font-size: 0.78rem;
  font-weight: 600; white-space: nowrap;
}
.marker[data-source="static"] .pin { background: #7c5cff; }
.marker[data-source="last_known"] .pin { background: var(--grey); border-style: dashed; }
.marker[data-source="last_known"] .tag::after { content: " · last known"; font-weight: 400; color: var(--muted); }
.marker[data-source="static"] .tag::after { content: " · fixed position"; font-weight: 400; color: var(--muted); }

/* sheets over the map */
.sheet {
  position: absolute; z-index: 10; top: 12px; left: 12px; width: min(380px, calc(100% - 24px));
  max-height: calc(100% - 24px); overflow-y: auto; background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px; box-shadow: 0 10px 30px rgba(0,0,0,0.45);
}
.sheet-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.sheet-head h2 { flex: 1; font-size: 1.05rem; }
.close, .back {
  width: 34px; height: 34px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel-2);
  cursor: pointer; font-size: 1.2rem; line-height: 1;
}
.device-list { list-style: none; margin: 0; padding: 0; }
.device-list li button {
  width: 100%; display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; text-align: left;
  padding: 10px 12px; margin-bottom: 6px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--panel-2); cursor: pointer;
}
.device-list .name { font-weight: 600; }
.device-list .addr { color: var(--muted); font-size: 0.82rem; grid-column: 1; }
.device-list .state { grid-row: 1 / span 2; grid-column: 2; align-self: center; }
.pill {
  display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 0.8rem; margin: 0;
  background: var(--panel-2); border: 1px solid var(--line); color: var(--muted);
}
.pill[data-s="online"], .pill[data-s="ok"], .pill[data-s="green"] { color: var(--green); border-color: rgba(47,191,113,0.4); }
.pill[data-s="starting"], .pill[data-s="degraded"], .pill[data-s="amber"] { color: var(--amber); border-color: rgba(240,180,41,0.4); }
.pill[data-s="offline"], .pill[data-s="down"], .pill[data-s="red"] { color: var(--red); border-color: rgba(229,72,77,0.4); }
.statusline { font-family: ui-monospace, monospace; font-size: 0.82rem; color: var(--muted); }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; margin: 12px 0; }
.tile { background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 10px; }
.tile .t { font-size: 0.75rem; color: var(--muted); }
.tile .v { font-size: 1.4rem; font-weight: 600; margin: 4px 0 2px; }
.tile .v small { font-size: 0.8rem; color: var(--muted); font-weight: 400; margin-left: 3px; }
.tile .a { font-size: 0.72rem; color: var(--muted); }
.tile .light { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 6px; background: var(--grey); }
.tile .light[data-l="green"] { background: var(--green); }
.tile .light[data-l="amber"] { background: var(--amber); }
.tile .light[data-l="red"] { background: var(--red); }
.identity { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0 0 10px; font-size: 0.9rem; }
.identity dt { color: var(--muted); }
.identity dd { margin: 0; word-break: break-word; }
.dim { opacity: 0.55; }

/* radio panel */
.radio-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.radio-head h2 { font-size: 1.1rem; }
.radio-meta { display: flex; align-items: center; gap: 10px; }
.bars { display: inline-flex; align-items: flex-end; gap: 2px; height: 18px; }
.bars i { width: 4px; background: var(--line); border-radius: 1px; }
.bars i:nth-child(1) { height: 20%; } .bars i:nth-child(2) { height: 40%; } .bars i:nth-child(3) { height: 60%; }
.bars i:nth-child(4) { height: 80%; } .bars i:nth-child(5) { height: 100%; }
.bars i.on { background: var(--text); }
.bars.low i.on { background: var(--amber); }
.rx { display: flex; align-items: center; gap: 10px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.rx-label { font-size: 0.85rem; }
.meter { flex: 1; height: 8px; border-radius: 4px; background: #0b1219; overflow: hidden; }
.meter span { display: block; height: 100%; width: 0; background: var(--green); transition: width 80ms linear; }
.group-title { font-size: 0.85rem; color: var(--muted); font-weight: 500; }
.group { list-style: none; margin: -6px 0 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.group li {
  display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 12px;
  border-radius: 10px; border: 1px solid var(--line); background: var(--panel-2);
}
.group li .n { font-weight: 600; }
.group li .id { color: var(--muted); font-size: 0.8rem; margin-left: 6px; }
.group li[data-a="rx"] { border-color: var(--green); }
.group li[data-a="tx"] { border-color: var(--red); }
.group li[data-a="down"] { opacity: 0.6; }
.group .act { font-size: 0.8rem; color: var(--muted); }
.group li[data-a="rx"] .act { color: var(--green); }
.group li[data-a="tx"] .act { color: var(--red); }

.ptt-wrap { margin-top: auto; display: flex; flex-direction: column; align-items: center; gap: 8px; padding-top: 8px; }
.ptt {
  width: min(100%, 300px); aspect-ratio: 1 / 1; max-height: 260px; border-radius: 50%; border: 4px solid var(--line);
  background: radial-gradient(circle at 50% 40%, #23384c, #172533); display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 6px; cursor: pointer; user-select: none; -webkit-user-select: none;
  touch-action: none; transition: background 120ms, border-color 120ms, transform 80ms;
}
.ptt #ptt-label { font-size: 1.35rem; font-weight: 700; }
.ptt-sub { font-size: 0.9rem; color: var(--muted); min-height: 1.2em; }
.ptt:disabled { cursor: not-allowed; opacity: 0.55; }
.ptt[data-state="pending"] { border-color: var(--amber); }
.ptt[data-state="talking"] { background: radial-gradient(circle at 50% 40%, #c4383d, #8f2226); border-color: #ff8a8d; transform: scale(0.98); }
.ptt[data-state="talking"] .ptt-sub { color: #ffe1e2; }
.ptt[data-state="busy"] { border-color: var(--amber); }
.ptt[data-state="busy"] #ptt-label { color: var(--amber); }
.ptt[data-state="denied"] { border-color: var(--red); }
.ptt[data-state="lost"] { border-color: var(--red); }
.ptt[data-state="lost"] #ptt-label { color: var(--red); }

/* join overlay */
.join {
  position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; padding: 16px;
  background: rgba(8, 12, 17, 0.82); backdrop-filter: blur(4px);
}
.join[hidden] { display: none; }
.join-card {
  max-width: 420px; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 24px;
  text-align: center;
}
.join-card h1 { font-size: 1.3rem; margin: 0 0 8px; }
.join-card p { color: var(--muted); margin: 0 0 20px; }
.join-btn {
  width: 100%; padding: 14px; border-radius: 12px; border: none; background: var(--accent); color: #05121f;
  font-weight: 700; font-size: 1.05rem; cursor: pointer; margin-bottom: 8px;
}

/* roles: PIN button, visitor countdown, owner panel */
.top-right { display: flex; align-items: center; gap: 8px; }
.chip {
  display: inline-flex; align-items: center; min-height: 36px; padding: 6px 12px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text); text-decoration: none;
  font-size: 0.9rem; cursor: pointer; white-space: nowrap;
}
.chip[hidden] { display: none; }
.chip-accent { background: var(--accent); border-color: var(--accent); color: #05121f; font-weight: 700; }
#talk-chip { cursor: default; font-variant-numeric: tabular-nums; }
#talk-chip.low { border-color: var(--amber); color: var(--amber); }
.link-pop .text-btn { display: inline-block; text-decoration: none; }
.owner-sheet { width: min(420px, calc(100% - 24px)); }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 0.85rem; color: var(--muted); margin-bottom: 10px; }
.field input {
  font: inherit; color: var(--text); background: #0b1219; border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 12px;
}
.primary-btn, .danger-btn {
  width: 100%; padding: 12px; border-radius: 10px; border: none; font-weight: 700; cursor: pointer; font-size: 1rem;
}
.primary-btn { background: var(--accent); color: #05121f; }
.primary-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.danger-btn { background: transparent; border: 1px solid var(--red); color: var(--red); margin-top: 12px; }
.pending { text-align: center; margin: 12px 0; padding: 12px; border: 1px solid var(--accent); border-radius: 12px; }
.pending[hidden] { display: none; }
.pending-pin { font-size: 2.6rem; font-weight: 700; letter-spacing: 0.25em; font-variant-numeric: tabular-nums; margin: 4px 0 10px; }
.pending-qr { display: flex; justify-content: center; }
.pending-qr svg { width: min(240px, 70vw); height: auto; border-radius: 8px; }
.owner-msg { color: var(--amber); font-size: 0.9rem; }
.visitor-list { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.visitor-list li {
  display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 10px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--panel-2); font-size: 0.9rem;
}
.visitor-list .left { font-variant-numeric: tabular-nums; color: var(--muted); margin-left: 6px; }
.visitor-list button { background: none; border: 1px solid var(--line); border-radius: 8px; color: var(--red); padding: 4px 10px; cursor: pointer; }
.ptt-signin { display: block; text-align: center; text-decoration: none; }
@media (max-width: 760px) { .brand { font-size: 0.95rem; } .chip { padding: 6px 10px; } #link-text { display: none; } }

/* phones: keep the top bar on one line, never wider than the screen */
.topbar { min-width: 0; }
.brand { min-width: 0; flex: 1 1 auto; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-right { flex: 0 0 auto; }
@media (max-width: 480px) {
  .topbar { padding-left: 12px; padding-right: 12px; gap: 8px; }
  .brand { font-size: 0.85rem; flex-direction: column; align-items: flex-start; gap: 0; line-height: 1.15; }
  .brand-sep { display: none; }
  .top-right { gap: 6px; }
  .chip { padding: 5px 9px; font-size: 0.82rem; min-height: 32px; }
  .link-btn { padding: 7px 9px; min-height: 32px; }
}
