:root {
  --bg: #0a0c10;
  --panel: #10141a;
  --panel-2: #161b23;
  --line: #222a35;
  --line-2: #2d3644;
  --text: #d7dde5;
  --muted: #7d8896;
  --dim: #4b5563;
  --ok: #4ade80;
  --warn: #fbbf24;
  --bad: #f87171;
  --bar-h: 44px;
  --mono: ui-monospace, "Cascadia Mono", "SF Mono", Consolas, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

button, input { font: inherit; color: inherit; }

#map {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: grab;
}
#map.dragging { cursor: grabbing; }
#map.hovering { cursor: pointer; }

/* top bar */
#bar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: calc(var(--bar-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 6px 0 14px;
  display: flex;
  align-items: center;
  gap: 14px;
  background: rgba(10, 12, 16, .9);
  border-bottom: 1px solid var(--line);
  z-index: 20;
  user-select: none;
}
.bar-left { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1; }
.brand { font-weight: 700; letter-spacing: .18em; font-size: 12px; }
.tag {
  font: 600 11px var(--mono);
  padding: 2px 6px;
  border: 1px solid var(--line-2);
  border-radius: 4px;
  color: var(--muted);
}
.map-name { color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.bar-stats { display: flex; align-items: center; gap: 16px; }
.stat { display: flex; align-items: baseline; gap: 5px; }
.stat em { font-style: normal; font-size: 10px; letter-spacing: .08em; color: var(--muted); }
.stat b { font: 600 12px var(--mono); min-width: 2ch; text-align: right; }

.status { display: flex; align-items: center; gap: 7px; font-size: 12px; }
.status i { width: 7px; height: 7px; border-radius: 50%; background: var(--dim); }
.status b { font-weight: 500; }
.status[data-s="live"] i { background: var(--ok); box-shadow: 0 0 0 3px rgba(74, 222, 128, .15); }
.status[data-s="connecting"] i, .status[data-s="waiting"] i { background: var(--warn); animation: blink 1.2s infinite; }
.status[data-s="lost"] i, .status[data-s="offline"] i, .status[data-s="error"] i { background: var(--bad); }
.status[data-s="closed"] i { background: var(--dim); }
@keyframes blink { 50% { opacity: .3; } }

.icon-btn {
  width: 34px; height: 34px;
  display: grid; place-items: center;
  border: 1px solid transparent;
  border-radius: 7px;
  background: transparent;
  cursor: pointer;
  padding: 0;
}
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn svg .fill { fill: currentColor; stroke: none; }
.icon-btn:hover { background: var(--panel-2); }
.icon-btn.on { color: #0a0c10; background: var(--text); }

/* side panel */
#panel {
  position: fixed;
  top: calc(var(--bar-h) + env(safe-area-inset-top));
  right: 0; bottom: 0;
  width: 272px;
  padding: 4px 0 calc(16px + env(safe-area-inset-bottom));
  background: var(--panel);
  border-left: 1px solid var(--line);
  overflow-y: auto;
  z-index: 15;
  transform: translateX(100%);
  transition: transform .18s ease;
  user-select: none;
}
#panel.open { transform: none; }
#panel section { padding: 12px 16px; border-bottom: 1px solid var(--line); }
#panel section:last-child { border-bottom: 0; }
#panel h3 {
  margin: 0 0 8px;
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--muted);
}

.row {
  display: flex;
  align-items: center;
  gap: 9px;
  min-height: 30px;
  cursor: pointer;
}
.row .lbl { flex: 1; }
.row .count { font: 11px var(--mono); color: var(--muted); }
.row kbd, .keys dt kbd { font: 10px var(--mono); color: var(--dim); }
.row canvas { width: 20px; height: 20px; flex: none; }

input[type="checkbox"] {
  appearance: none;
  flex: none;
  width: 28px; height: 16px;
  margin: 0;
  border-radius: 8px;
  background: var(--line-2);
  position: relative;
  cursor: pointer;
  transition: background .15s;
}
input[type="checkbox"]::after {
  content: "";
  position: absolute;
  top: 2px; left: 2px;
  width: 12px; height: 12px;
  border-radius: 50%;
  background: var(--muted);
  transition: transform .15s, background .15s;
}
input[type="checkbox"]:checked { background: #e2e8f0; }
input[type="checkbox"]:checked::after { transform: translateX(12px); background: var(--bg); }

.btn {
  height: 30px;
  padding: 0 12px;
  border: 1px solid var(--line-2);
  border-radius: 6px;
  background: var(--panel-2);
  cursor: pointer;
}
.btn:hover { border-color: var(--dim); }
.btn.primary { background: var(--text); color: var(--bg); border-color: var(--text); font-weight: 600; }
.btn.wide { width: 100%; margin-top: 8px; }

.legend { display: flex; flex-wrap: wrap; gap: 4px 12px; margin-top: 8px; font-size: 11.5px; color: var(--muted); }
.legend i { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; vertical-align: 0; }

.keys { display: grid; grid-template-columns: auto 1fr; gap: 3px 10px; margin: 10px 0 0; font-size: 11.5px; }
.keys dt { font: 11px var(--mono); color: var(--text); }
.keys dd { margin: 0; color: var(--muted); }

.field { display: grid; gap: 5px; }
.field span { font-size: 11.5px; color: var(--muted); }
.field input {
  height: 32px;
  padding: 0 10px;
  border: 1px solid var(--line-2);
  border-radius: 6px;
  background: var(--bg);
  outline: none;
}
.field input:focus { border-color: var(--muted); }

/* map controls */
#controls {
  position: fixed;
  left: calc(12px + env(safe-area-inset-left));
  bottom: calc(40px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 4px;
  background: rgba(16, 20, 26, .92);
  border: 1px solid var(--line);
  border-radius: 10px;
  z-index: 10;
}
#compass { transition: transform .1s linear; }
#compass .north { color: #f87171; }
.icon-btn.on #compass .north { color: #dc2626; }

/* overlays */
#overlay {
  position: fixed;
  inset: calc(var(--bar-h) + env(safe-area-inset-top)) 0 0 0;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(10, 12, 16, .6);
  z-index: 12;
}
#overlay[hidden], #banner[hidden], #tip[hidden], #radial[hidden], #toast[hidden] { display: none; }
.card {
  width: min(340px, 100%);
  padding: 20px;
  background: var(--panel);
  border: 1px solid var(--line-2);
  border-radius: 12px;
}
.card h2 { margin: 0 0 6px; font-size: 15px; font-weight: 600; display: flex; align-items: center; gap: 10px; }
.card p { margin: 0 0 14px; color: var(--muted); }
.card p:last-child { margin-bottom: 0; }
.card form { display: grid; gap: 10px; }
.card .err { color: var(--bad); font-size: 12px; }
.card .actions { display: flex; gap: 8px; }
.card code { font: 12px var(--mono); color: var(--text); word-break: break-all; }
.spin {
  width: 14px; height: 14px;
  border: 2px solid var(--line-2);
  border-top-color: var(--text);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

#banner {
  position: fixed;
  top: calc(var(--bar-h) + 10px + env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  padding: 6px 12px;
  border: 1px solid rgba(248, 113, 113, .4);
  border-radius: 6px;
  background: rgba(40, 14, 16, .92);
  color: #fecaca;
  font-size: 12px;
  white-space: nowrap;
  z-index: 11;
}

#tip {
  position: fixed;
  max-width: 260px;
  padding: 9px 11px;
  background: rgba(16, 20, 26, .97);
  border: 1px solid var(--line-2);
  border-radius: 8px;
  font-size: 12px;
  pointer-events: none;
  z-index: 30;
}
.tt-h { display: flex; align-items: center; gap: 7px; font-weight: 600; font-size: 13px; }
.tt-h i { width: 8px; height: 8px; border-radius: 2px; flex: none; }
.tt-s { color: var(--muted); margin: 1px 0 6px; font-size: 11.5px; }
.tt-r { display: flex; justify-content: space-between; gap: 14px; }
.tt-r span { color: var(--muted); }
.tt-r b { font: 500 12px var(--mono); }
.tt-d { margin: 2px 0 4px; }
.tt-c { margin: 6px 0 0; padding: 6px 0 0; border-top: 1px solid var(--line); list-style: none; display: grid; gap: 2px; }
.tt-c li { display: flex; gap: 6px; align-items: center; }
.tt-c li i { width: 6px; height: 6px; border-radius: 50%; flex: none; }
.tt-c .more { color: var(--muted); }

/* radial ping menu */
#radial {
  position: fixed;
  width: 0; height: 0;
  z-index: 25;
}
#radial button {
  position: absolute;
  width: 78px; height: 34px;
  margin: -17px 0 0 -39px;
  display: flex; align-items: center; justify-content: center; gap: 7px;
  border: 1px solid var(--line-2);
  border-left: 3px solid var(--c);
  border-radius: 7px;
  background: rgba(16, 20, 26, .97);
  cursor: pointer;
  animation: pop .12s ease-out both;
}
#radial button:hover, #radial button:focus-visible { background: var(--panel-2); border-color: var(--c); outline: none; }
#radial button b { font-weight: 600; font-size: 12px; }
#radial kbd { font: 10px var(--mono); color: var(--dim); }
#radial .r-n { left: 0; top: -46px; }
#radial .r-s { left: 0; top: 46px; }
#radial .r-e { left: 72px; top: 0; }
#radial .r-w { left: -72px; top: 0; }
#radial .r-c {
  position: absolute;
  width: 10px; height: 10px;
  margin: -5px 0 0 -5px;
  border: 2px solid var(--text);
  border-radius: 50%;
}
@keyframes pop { from { opacity: 0; transform: scale(.85); } }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(18px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  padding: 7px 12px;
  background: var(--panel-2);
  border: 1px solid var(--line-2);
  border-radius: 6px;
  font-size: 12px;
  z-index: 30;
}

@media (max-width: 720px) {
  #bar { gap: 8px; padding-left: 10px; }
  .brand { display: none; }
  .bar-stats { gap: 10px; }
  .stat em { display: none; }
  .status b { display: none; }
  #panel {
    top: auto;
    left: 0;
    width: auto;
    height: 62vh;
    border-left: 0;
    border-top: 1px solid var(--line);
    border-radius: 12px 12px 0 0;
    transform: translateY(100%);
  }
  #banner { white-space: normal; text-align: center; width: max-content; max-width: 90vw; }
}
