:root {
  --bg: #0c1116;
  --bg-2: #131a22;
  --bg-3: #1c2630;
  --line: #243140;
  --text: #d8e1ea;
  --muted: #7c8a99;
  --accent: #5dd0a8;
  --warn: #e7b450;
  --err: #e87474;
  --chat: #5dd0a8;
  --repeater: #6fb4ff;
  --room: #c79cff;
  --sensor: #ffb27a;
  --none: #7c8a99;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 8px 14px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}
.brand {
  font-weight: 600;
  letter-spacing: 0.2px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.brand .dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 8px var(--accent);
}
.tabs { display: flex; gap: 4px; margin-left: 16px; }
.tab {
  background: transparent;
  border: 1px solid transparent;
  color: var(--muted);
  padding: 5px 12px;
  border-radius: 6px;
  cursor: pointer;
  font: inherit;
}
.tab:hover { color: var(--text); }
.tab.active {
  background: var(--bg-3);
  border-color: var(--line);
  color: var(--text);
}
.status { margin-left: auto; display: flex; gap: 6px; align-items: center; }
.pill {
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--bg-3);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
}
.pill.ok { color: var(--accent); border-color: rgba(93,208,168,0.4); }
.pill.warn { color: var(--warn); border-color: rgba(231,180,80,0.4); }
.pill.err { color: var(--err); border-color: rgba(232,116,116,0.4); }

main {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: 1fr 340px;
  overflow: hidden;
}
/* The radar tab manages its own internal split (360px obs panel + 1fr map)
   inside view-radar; main keeps the 2-column outer layout. */
.view {
  position: relative;
  display: none;
  overflow: hidden;
}
.view.active { display: block; }
.leaflet { height: 100%; width: 100%; background: #0a0e13; }

.map-controls {
  position: absolute;
  top: 12px; right: 12px;
  background: rgba(19,26,34,0.92);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 12px;
  display: flex; flex-direction: column; gap: 6px;
  font-size: 12px;
  color: var(--text);
  z-index: 500;
  backdrop-filter: blur(6px);
  min-width: 200px;
}
.ctl-row { display: flex; align-items: center; gap: 8px; color: var(--muted); }
.ctl-row select, .ctl-row input[type=range] {
  background: var(--bg-3); color: var(--text);
  border: 1px solid var(--line); border-radius: 4px;
  padding: 1px 4px; font: inherit;
}

.heatmap-legend {
  position: absolute;
  bottom: 12px; left: 12px;
  background: rgba(19,26,34,0.92);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 12px;
  display: flex; gap: 12px; align-items: center;
  font-size: 12px;
  color: var(--muted);
  z-index: 500;
  backdrop-filter: blur(6px);
}
.heatmap-legend select, .heatmap-legend button {
  background: var(--bg-3);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 3px 8px;
  font: inherit;
}
.heatmap-legend button { cursor: pointer; }
.heatmap-legend button:hover { border-color: var(--accent); }

.sidebar {
  background: var(--bg-2);
  border-left: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.sidebar-head {
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.sidebar-head .filter { text-transform: none; letter-spacing: 0; color: var(--text); display: flex; align-items: center; gap: 5px; font-size: 13px; }

.feed {
  margin: 0; padding: 0;
  list-style: none;
  overflow-y: auto;
  flex: 1 1 auto;
}
.feed li {
  padding: 7px 12px;
  border-bottom: 1px solid var(--line);
  font-size: 12px;
  display: grid;
  grid-template-columns: 60px 1fr auto;
  gap: 6px 10px;
}
.feed li.flash { animation: flash 1.4s ease-out; }
@keyframes flash {
  from { background: rgba(93,208,168,0.18); }
  to { background: transparent; }
}
.feed .ptype { color: var(--muted); font-family: ui-monospace, Menlo, monospace; }
.feed .ptype.advert { color: var(--accent); }
.feed .origin { color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.feed .meta { color: var(--muted); font-family: ui-monospace, Menlo, monospace; text-align: right; }
.feed li.advert .ptype { color: var(--accent); }

/* Marker popups */
.leaflet-popup-content-wrapper {
  background: var(--bg-2);
  color: var(--text);
  border: 1px solid var(--line);
}
.leaflet-popup-content {
  margin: 8px 12px;
  font-size: 12px;
  line-height: 1.5;
}
.leaflet-popup-tip { background: var(--bg-2); }
.popup-title { font-weight: 600; margin-bottom: 4px; }
.popup-role {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 3px;
  font-size: 11px;
  margin-left: 6px;
}
.popup-meta { color: var(--muted); font-family: ui-monospace, monospace; font-size: 11px; }

.role-chat { background: var(--chat); color: #0a1a14; }
.role-repeater { background: var(--repeater); color: #06162a; }
.role-room { background: var(--room); color: #1a0a2a; }
.role-sensor { background: var(--sensor); color: #2a1606; }
.role-none, .role-unknown { background: var(--none); color: #0a1116; }

/* Custom div icons */
.node-marker {
  width: 14px; height: 14px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.5);
  box-shadow: 0 0 6px rgba(0,0,0,0.6);
}
.node-marker.chat { background: var(--chat); }
.node-marker.repeater { background: var(--repeater); }
.node-marker.room { background: var(--room); }
.node-marker.sensor { background: var(--sensor); }
.node-marker.none, .node-marker.unknown { background: var(--none); }
.node-marker.pulse { animation: pulse 1.2s ease-out; }
@keyframes pulse {
  0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(93,208,168,0.7); }
  100% { transform: scale(1); box-shadow: 0 0 0 14px rgba(93,208,168,0); }
}

/* --- Radar --- */
.view-radar { display: none; }
.view-radar.active { display: grid; grid-template-columns: 360px 1fr; }
body:not(.radar-active) .view-radar.active { grid-template-columns: 1fr; }
body:not(.radar-active) .obs-panel { display: none; }

.obs-panel {
  background: var(--bg-2);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}
.obs-head {
  padding: 9px 12px;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.obs-count {
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 1px 8px;
  text-transform: none;
  letter-spacing: 0;
  color: var(--text);
}
.obs-msgbody {
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
  line-height: 1.4;
  max-height: 30vh;
  overflow-y: auto;
}
.obs-msgbody .obs-empty { color: var(--muted); }
.obs-msgbody .obs-text { color: var(--text); white-space: pre-wrap; word-break: break-word; margin: 4px 0; }
.obs-msgbody .obs-sender { color: var(--accent); font-weight: 600; }
.obs-msgbody .obs-row { display: flex; justify-content: space-between; gap: 8px; color: var(--muted); font-size: 11px; margin-top: 4px; }
.obs-msgbody .obs-row b { color: var(--text); font-weight: 500; }
.obs-tablewrap { overflow-y: auto; flex: 1 1 auto; }
.obs-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 11px;
}
.obs-table th, .obs-table td {
  padding: 5px 8px;
  text-align: left;
  border-bottom: 1px solid var(--line);
}
.obs-table th {
  position: sticky; top: 0;
  background: var(--bg-3);
  color: var(--muted);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  font-size: 10px;
}
.obs-table tbody tr { cursor: pointer; }
.obs-table tbody tr:hover { background: var(--bg-3); }
.obs-table tbody tr.focused { background: rgba(255,201,85,0.15); }
.obs-table td.observer { color: var(--text); font-weight: 500; max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.obs-table td.path { font-family: ui-monospace, monospace; color: var(--muted); max-width: 100px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Channel dropdown */
.dd-label { display: flex; gap: 6px; align-items: center; color: var(--text); }
.dd-label select {
  background: var(--bg-3);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 3px 6px;
  font: inherit;
}
.dd-label select option:disabled { color: var(--muted); }

.radar-legend {
  position: absolute;
  bottom: 12px; left: 12px;
  background: rgba(19,26,34,0.92);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  display: flex; flex-direction: column; gap: 5px;
  font-size: 12px;
  color: var(--text);
  z-index: 500;
  backdrop-filter: blur(6px);
  max-width: 360px;
}
.rl-row { display: flex; align-items: center; gap: 8px; color: var(--muted); }
.rl-dot { width: 12px; height: 12px; border-radius: 50%; display: inline-block; }
.rl-dot.rl-origin { background: #ff5577; box-shadow: 0 0 8px #ff5577; }
.rl-dot.rl-hop { background: #ffc955; }
.rl-dot.rl-observer { background: #5dd0a8; border: 2px solid #5dd0a8; background: transparent; }
.rl-line { display: inline-block; width: 22px; height: 2px; background: linear-gradient(90deg, #ff5577, #5dd0a8); }
.rl-circle { display: inline-block; width: 14px; height: 14px; border-radius: 50%; box-sizing: border-box; }
.rl-circle.rl-circle-hop { border: 2px solid rgba(255,201,85,0.7); background: rgba(255,201,85,0.15); }
.rl-circle.rl-circle-obs { border: 2px dashed rgba(93,208,168,0.7); background: rgba(93,208,168,0.10); }
.rl-msg {
  margin-top: 6px;
  color: var(--text);
  font-style: italic;
  white-space: pre-wrap;
  word-break: break-word;
  border-top: 1px solid var(--line);
  padding-top: 6px;
}

.radar-marker { border: 2px solid rgba(255,255,255,0.5); border-radius: 50%; box-shadow: 0 0 8px rgba(0,0,0,0.6); }
.radar-marker.origin { background: #ff5577; width: 22px; height: 22px; box-shadow: 0 0 18px #ff5577; animation: pulse-origin 1.8s infinite; }
.radar-marker.hop { background: #ffc955; width: 12px; height: 12px; }
.radar-marker.observer { background: rgba(93,208,168,0.2); border-color: #5dd0a8; width: 16px; height: 16px; }
@keyframes pulse-origin {
  0% { box-shadow: 0 0 0 0 rgba(255,85,119,0.7); }
  100% { box-shadow: 0 0 0 24px rgba(255,85,119,0); }
}

/* Public-channel message rows */
.feed.msgs li {
  grid-template-columns: 1fr;
  gap: 2px;
  cursor: pointer;
  padding: 9px 12px;
}
.feed.msgs li:hover { background: var(--bg-3); }
.feed.msgs li.selected { background: rgba(255,201,85,0.12); border-left: 2px solid #ffc955; }
.feed.msgs .msg-head { display: flex; justify-content: space-between; color: var(--muted); font-size: 11px; }
.feed.msgs .msg-sender { color: var(--text); font-weight: 500; }
.feed.msgs .msg-body { color: var(--text); white-space: pre-wrap; word-break: break-word; font-size: 13px; line-height: 1.35; }
.feed.msgs .msg-meta { color: var(--muted); font-size: 11px; }
