:root {
  --bg:          #0a0e14;
  --surface:     #111824;
  --surface-2:   #16202e;
  --surface-3:   #1c2838;
  --border:      #243246;
  --border-soft: #1a2433;
  --text:        #e7eef6;
  --text-dim:    #8a99ad;
  --text-faint:  #5c6b80;
  --accent:      #34d6c6;
  --accent-dim:  #1e8e83;
  --accent-glow: rgba(52, 214, 198, 0.25);
  --warn:        #f4b740;
  --danger:      #ff6b8b;

  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 16px;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;

  --topbar-h: 60px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: "SF Mono", "JetBrains Mono", "Fira Code", ui-monospace, Menlo, monospace;

  --ease: cubic-bezier(0.16, 1, 0.3, 1);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
}

body {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---------- Top bar ---------- */
.topbar {
  height: var(--topbar-h);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: var(--space-6);
  padding: 0 var(--space-5);
  background: linear-gradient(180deg, var(--surface-2), var(--surface));
  border-bottom: 1px solid var(--border);
  position: relative;
  z-index: 1000;
}

.brand { display: flex; align-items: center; gap: var(--space-2); }
.brand-mark { display: inline-flex; align-items: center; }
.brand-mark svg { display: block; border-radius: 8px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.45); }
.brand-name { font-weight: 700; letter-spacing: 0.3px; }
.brand-sub {
  color: var(--text-dim);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 2px;
}

.tabs { display: flex; gap: var(--space-1); background: var(--bg); padding: 4px; border-radius: var(--r-md); border: 1px solid var(--border); }
.tab {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.3px;
  padding: 7px 18px;
  border-radius: var(--r-sm);
  cursor: pointer;
  transition: color 0.2s var(--ease), background 0.2s var(--ease);
}
.tab:hover { color: var(--text); }
.tab.is-active {
  color: var(--bg);
  background: var(--accent);
  box-shadow: 0 0 16px var(--accent-glow);
}

.status {
  margin-left: 0;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text-faint);
  display: flex;
  align-items: center;
  gap: var(--space-2);
}
.status::before {
  content: "";
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 8px var(--accent);
}
.status.is-stale::before { background: var(--warn); box-shadow: 0 0 8px var(--warn); }

/* collector-connection indicator (left of the refresh time) */
.conn-status {
  display: inline-flex; align-items: center; gap: var(--space-2);
  font-family: var(--mono); font-size: 12px; color: var(--text-faint);
  white-space: nowrap;
}
.conn-status::before {
  content: ""; width: 7px; height: 7px; border-radius: 50%;
  background: var(--text-faint); flex: 0 0 auto;
}
.conn-status.is-up { color: var(--text-dim); }
.conn-status.is-up::before { background: var(--accent); box-shadow: 0 0 8px var(--accent); }
.conn-status.is-down { color: var(--danger); }
.conn-status.is-down::before { background: var(--danger); box-shadow: 0 0 8px var(--danger); }

/* ---------- Auth (Login / user menu) ---------- */
.auth-area { display: flex; align-items: center; }

.login-btn {
  appearance: none;
  background: var(--accent);
  color: var(--bg);
  border: 0;
  border-radius: var(--r-sm);
  padding: 8px 18px;
  font: inherit; font-size: 13px; font-weight: 700; letter-spacing: 0.3px;
  cursor: pointer;
  box-shadow: 0 0 16px var(--accent-glow);
  transition: filter 0.15s var(--ease);
}
.login-btn:hover { filter: brightness(1.08); }

.user-menu {
  display: inline-flex; align-items: center; gap: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  padding: 7px 12px;
  font: inherit; color: var(--text);
  cursor: pointer;
  transition: border-color 0.15s var(--ease);
}
.user-menu:hover { border-color: var(--accent-dim); }
.user-name { font-size: 13px; font-weight: 650; max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.burger { display: inline-flex; flex-direction: column; gap: 3px; }
.burger span { display: block; width: 16px; height: 2px; border-radius: 2px; background: var(--text-dim); }
.user-menu:hover .burger span { background: var(--accent); }

.user-pop {
  position: absolute; right: 0; top: calc(100% + 8px);
  min-width: 160px; padding: 6px;
  background: rgba(17, 24, 36, 0.96);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  backdrop-filter: blur(10px);
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.5);
  z-index: 1100;
}
.user-pop[hidden] { display: none; }
.pop-item {
  display: block; width: 100%;
  appearance: none; border: 0; background: transparent;
  border-radius: var(--r-sm);
  padding: 9px 12px;
  font: inherit; font-size: 13px; color: var(--text); text-align: left;
  cursor: pointer;
  transition: background 0.15s var(--ease), color 0.15s var(--ease);
}
.pop-item:hover { background: var(--surface-2); color: var(--accent); }
.pop-item.is-busy { opacity: 0.5; pointer-events: none; }

/* Write actions are visible only to signed-in users (the backend enforces too).
   Send advert lives in the user menu, which only renders once signed in. */
body:not(.is-authed) #device-add-btn,
body:not(.is-authed) .detail-controls,
body:not(.is-authed) .tab-auth { display: none !important; }

/* Devices tab is admin-only (like Messages): the device list exposes public
   keys and poll config, so hide the tab + view unless signed in. */
body:not(.is-authed) .tab[data-tab="devices"] { display: none !important; }
body:not(.is-authed) #view-devices { display: none !important; }

/* right-aligned group: advert action, status, and the auth control */
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: var(--space-4); position: relative; }

/* far-right icon action (e.g. send advert), sits just before the status dot */
.topbar-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  background: var(--bg);
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  cursor: pointer;
  transition: color 0.15s var(--ease), border-color 0.15s var(--ease), background 0.15s var(--ease);
}
.topbar-action:hover { color: var(--accent); border-color: var(--accent-dim); }
.topbar-action:active { background: var(--surface-2); }
.topbar-action.is-busy { opacity: 0.5; pointer-events: none; }

/* ---------- Views ---------- */
main { flex: 1 1 auto; position: relative; min-height: 0; }
.view { position: absolute; inset: 0; display: none; }
.view.is-active { display: flex; }

/* ---------- Info page ---------- */
/* Full-width scroll container (scrollbar sits at the viewport's right edge)… */
.info-page {
  flex: 1 1 auto; overflow-y: auto; width: 100%;
  color: var(--text); font-size: 15px; line-height: 1.65;
}
/* …with the readable content kept at a fixed width, centred. */
.info-content { max-width: 820px; margin: 0 auto; padding: var(--space-6) var(--space-5); }
.info-page h1 { font-size: 28px; margin: 0 0 var(--space-4); letter-spacing: -0.01em; }
.info-page h2 { font-size: 21px; margin: var(--space-6) 0 var(--space-3); }
.info-page h3 { font-size: 17px; margin: var(--space-5) 0 var(--space-2); }
.info-page h4 {
  font-size: 13px; margin: var(--space-4) 0 var(--space-2);
  color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.04em;
}
.info-page p { margin: 0 0 var(--space-3); color: #cdd8e5; }
.info-page ul, .info-page ol { margin: 0 0 var(--space-3); padding-left: 1.4em; color: #cdd8e5; }
.info-page li { margin: 4px 0; }
.info-page a { color: var(--accent); text-decoration: none; }
.info-page a:hover { text-decoration: underline; }
.info-page strong { color: var(--text); }
.info-page hr { border: 0; border-top: 1px solid var(--border); margin: var(--space-5) 0; }
.info-page blockquote {
  margin: var(--space-3) 0; padding: var(--space-3) var(--space-4);
  border-left: 3px solid var(--accent-dim); background: var(--surface);
  border-radius: 0 var(--r-sm) var(--r-sm) 0; color: #cdd8e5;
}
.info-page pre {
  margin: var(--space-3) 0; padding: var(--space-3) var(--space-4); overflow-x: auto;
  background: var(--bg); border: 1px solid var(--border); border-radius: var(--r-sm);
}
.info-page code { font-family: var(--mono); font-size: 13px; color: var(--accent); }
.info-table { width: 100%; border-collapse: collapse; margin: var(--space-3) 0; font-size: 14px; }
.info-table th, .info-table td { text-align: left; padding: 8px 12px; border: 1px solid var(--border); }
.info-table th { background: var(--surface); color: var(--text-dim); font-weight: 600; }
.info-table td { color: #cdd8e5; }
.info-note { color: var(--text-dim); }

/* ---------- Map ---------- */
#map { position: absolute; inset: 0; background: #e9e6df; }
.leaflet-container { background: #e9e6df; font-family: var(--font); }
.leaflet-control-zoom a {
  background: var(--surface-2) !important;
  color: var(--text) !important;
  border-color: var(--border) !important;
}
.leaflet-control-zoom a:hover { background: var(--surface-3) !important; }
.leaflet-bar { border: 1px solid var(--border) !important; box-shadow: 0 4px 16px rgba(0,0,0,0.5) !important; }
.leaflet-control-attribution {
  background: rgba(10,14,20,0.7) !important;
  color: var(--text-faint) !important;
}
.leaflet-control-attribution a { color: var(--text-dim) !important; }

/* floating control bar — bottom-left of the map (Layers · Devices · time window) */
.map-bar {
  position: absolute; left: var(--space-4); bottom: var(--space-4);
  z-index: 500; display: flex; align-items: center; gap: var(--space-2);
}
.bar-group { position: relative; }
.map-btn {
  display: inline-flex; align-items: center; gap: 8px;
  background: rgba(17, 24, 36, 0.86);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: 9px 14px;
  font: inherit; font-size: 13px; font-weight: 600;
  cursor: pointer;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
  transition: color 0.15s var(--ease), border-color 0.15s var(--ease);
  white-space: nowrap;
  text-decoration: none;  /* also used as an <a> (PureTrack link) */
}
.map-btn:hover { color: var(--accent); border-color: var(--accent-dim); }
.map-btn svg { color: var(--accent); flex: 0 0 auto; }
.map-btn-favicon { flex: 0 0 auto; width: 16px; height: 16px; border-radius: 3px; }

/* shared popover above a bar button (Layers / Devices) */
.popover {
  position: absolute; left: 0; bottom: calc(100% + 8px);
  min-width: 190px; padding: 6px;
  background: rgba(17, 24, 36, 0.94);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  backdrop-filter: blur(10px);
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.5);
  display: flex; flex-direction: column; gap: 2px;
  max-height: 60vh; overflow-y: auto;
}
.popover[hidden] { display: none; }
.pop-title {
  font-size: 10px; text-transform: uppercase; letter-spacing: 1.5px;
  color: var(--text-faint); padding: 4px 8px 6px;
}

/* Layers options */
.layer-opt {
  display: flex; align-items: center; gap: 9px; width: 100%;
  appearance: none; border: 0; background: transparent;
  border-radius: var(--r-sm);
  padding: 8px 10px;
  font: inherit; font-size: 13px; color: var(--text-dim);
  text-align: left; cursor: pointer;
  transition: color 0.15s var(--ease), background 0.15s var(--ease);
}
.layer-opt:hover { background: var(--surface-2); color: var(--text); }
.layer-opt.is-active { color: var(--text); }
.layer-opt .swatch {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--border); flex: 0 0 auto;
}
.layer-opt.is-active .swatch { background: var(--accent); box-shadow: 0 0 8px var(--accent-glow); }
.layer-opt .tick { margin-left: auto; color: var(--accent); opacity: 0; }
.layer-opt.is-active .tick { opacity: 1; }

/* Devices popup rows (node + last-fix time) */
.dev-row { display: flex; align-items: center; gap: var(--space-3); padding: 6px 8px; font-size: 13px; }
.dev-row .dot { width: 11px; height: 11px; border-radius: 50%; border: 1.5px solid #fff3; flex: 0 0 auto; }
.dev-row .dr-name { font-weight: 600; }
.dev-row .dr-time { margin-left: auto; padding-left: var(--space-3); color: var(--text-faint); font-family: var(--mono); font-size: 10px; white-space: nowrap; }
.dev-row .dr-icon { display: inline-flex; width: 13px; height: 13px; flex: 0 0 auto; }
.dev-row .dr-icon svg { width: 13px; height: 13px; display: block; }

/* time-window control: the button shows the value, options open upward as a
   popover (same direction as Layers / Devices). */
.period { position: relative; display: inline-flex; align-items: center; }
.period-options {
  position: absolute; left: 0; bottom: calc(100% + 8px); min-width: 100%;
  display: none; flex-direction: column; gap: 4px; padding: 6px;
  background: rgba(17, 24, 36, 0.94);
  border: 1px solid var(--border); border-radius: var(--r-md);
  backdrop-filter: blur(10px); box-shadow: 0 12px 36px rgba(0, 0, 0, 0.5);
}
.period.is-open .period-options { display: flex; }
.period-opt {
  appearance: none; border: 1px solid var(--border); background: rgba(17, 24, 36, 0.86);
  color: var(--text-dim); font: inherit; font-size: 12px; font-weight: 600;
  padding: 8px 12px; border-radius: var(--r-md); cursor: pointer; white-space: nowrap;
  width: 100%; text-align: center;
  transition: color 0.15s var(--ease), border-color 0.15s var(--ease), background 0.15s var(--ease);
}
.period-opt:hover { color: var(--text); border-color: var(--accent-dim); }
.period-opt.is-active { color: var(--bg); background: var(--accent); border-color: var(--accent); }

/* node label under the latest point */
.node-label.leaflet-tooltip {
  background: rgba(10,14,20,0.82);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.4px;
  padding: 2px 8px;
  border-radius: 6px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.5);
  backdrop-filter: blur(4px);
}
.node-label.leaflet-tooltip::before { display: none; }

.leaflet-popup-content-wrapper, .leaflet-popup-tip {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
}
.leaflet-popup-content { font-size: 12px; line-height: 1.5; }
.leaflet-popup-content .muted { color: var(--text-faint); }


/* ---------- Devices ---------- */
#view-devices { display: none; }
/* grid-template-rows: minmax(0,1fr) bounds the row to the container height so the
   list and charts panels can scroll internally instead of overflowing. */
#view-devices.is-active {
  display: grid;
  grid-template-columns: 280px 1fr;
  grid-template-rows: minmax(0, 1fr);
}

.device-list {
  border-right: 1px solid var(--border);
  background: var(--surface);
  overflow-y: auto;
  padding: var(--space-3);
}
.device-item {
  width: 100%;
  display: grid;
  grid-template-columns: auto 1fr auto;
  column-gap: var(--space-3);
  align-items: center;
  text-align: left;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--r-md);
  padding: 8px var(--space-3);
  margin-bottom: 4px;
  cursor: pointer;
  color: var(--text);
  font: inherit;
  transition: background 0.18s var(--ease), border-color 0.18s var(--ease), transform 0.18s var(--ease);
}
.device-item:hover { background: var(--surface-2); transform: translateX(2px); }
.device-item.is-active {
  background: var(--surface-3);
  border-color: var(--accent-dim);
  box-shadow: inset 3px 0 0 var(--accent);
}
.device-item .dot {
  width: 12px; height: 12px; border-radius: 50%;
  border: 1.5px solid #fff3;
}
.device-item .dev-name { font-weight: 650; font-size: 14px; }
.device-item .dev-time {
  font-size: 11px; color: var(--text-dim); font-family: var(--mono);
  text-align: right; white-space: nowrap;
}

.device-detail { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.detail-header {
  flex: 0 0 auto;
  height: 56px;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 0 var(--space-5);
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.detail-title { font-size: 16px; font-weight: 700; }
.detail-key { font-family: var(--mono); font-size: 12px; color: var(--text-faint); }

/* device info panel above the charts */
.detail-info {
  flex: 0 0 auto;
  display: none;  /* hidden by default; click the node name to reveal */
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: var(--space-2) var(--space-5);
  padding: var(--space-3) var(--space-5);
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.detail-info.is-open:not(:empty) { display: grid; }
.detail-toggle {
  background: transparent;
  border: 0;
  padding: 0;
  cursor: pointer;
  color: var(--text);
  font: inherit;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.detail-toggle:hover { color: var(--accent); }
.info-item { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.info-k { font-size: 10px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--text-faint); }
.info-v { font-family: var(--mono); font-size: 12px; color: var(--text); word-break: break-all; }
.detail-sub { font-size: 12px; color: var(--text-dim); font-family: var(--mono); }

.range-select {
  margin-left: auto;
  display: flex;
  gap: 2px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  padding: 3px;
}
.range-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  padding: 5px 11px;
  border-radius: 6px;
  cursor: pointer;
  transition: color 0.15s var(--ease), background 0.15s var(--ease);
}
.range-btn:hover { color: var(--text); }
.range-btn.is-active { color: var(--bg); background: var(--accent); box-shadow: 0 0 12px var(--accent-glow); }

.charts-grid {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: var(--space-5);
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(360px, 1fr));
  gap: var(--space-5);
  align-content: start;
}
.chart-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: var(--space-4) var(--space-4) var(--space-3);
  min-height: 220px;
  display: flex;
  flex-direction: column;
  transition: border-color 0.2s var(--ease), box-shadow 0.2s var(--ease);
}
.chart-card:hover { border-color: var(--border); box-shadow: 0 10px 30px rgba(0,0,0,0.35); }
.chart-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: var(--space-3);
}
.chart-name { font-size: 13px; font-weight: 650; letter-spacing: 0.2px; }
.chart-last { font-family: var(--mono); font-size: 13px; color: var(--accent); }
.chart-canvas-wrap { position: relative; flex: 1 1 auto; min-height: 150px; }

.empty, .hint {
  color: var(--text-dim);
  font-size: 13px;
  padding: var(--space-5);
  text-align: center;
}
.charts-grid .hint {
  grid-column: 1 / -1;
  margin-top: 10vh;
  font-size: 14px;
}
.charts-grid .hint .big { display:block; font-size: 40px; margin-bottom: var(--space-3); opacity: 0.4; }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--surface-3); border-radius: 6px; border: 2px solid var(--surface); }
::-webkit-scrollbar-thumb:hover { background: var(--border); }

/* ---- Device management ---- */
.device-list-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px; font-size: 12px; letter-spacing: .08em;
  text-transform: uppercase; color: #8a99ad;
}
.btn {
  background: #1b2738; color: #e7eef6; border: 1px solid #2b3a4f;
  border-radius: 8px; padding: 7px 14px; font: inherit; cursor: pointer;
}
.btn:hover { border-color: #3a4d68; }
.btn-primary { background: #2563eb; border-color: #2563eb; }
.btn-primary:hover { background: #1d4ed8; }
.btn-add { padding: 2px 10px; font-size: 18px; line-height: 1; }
.detail-controls { display: inline-flex; align-items: center; gap: var(--space-3); margin-left: var(--space-3); }

/* labelled toggle switch (Collect / PureTrack) */
.switch { display: inline-flex; align-items: center; gap: 7px; cursor: pointer; user-select: none; }
.switch-input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.switch-track {
  position: relative; width: 34px; height: 18px; flex: 0 0 auto;
  background: var(--surface-3); border: 1px solid var(--border);
  border-radius: 999px;
  transition: background 0.18s var(--ease), border-color 0.18s var(--ease);
}
.switch-thumb {
  position: absolute; top: 1px; left: 1px; width: 14px; height: 14px;
  border-radius: 50%; background: var(--text-dim);
  transition: transform 0.18s var(--ease), background 0.18s var(--ease);
}
.switch-input:checked + .switch-track { background: var(--accent-dim); border-color: var(--accent-dim); }
.switch-input:checked + .switch-track .switch-thumb { transform: translateX(16px); background: var(--accent); }
.switch-input:focus-visible + .switch-track { box-shadow: 0 0 0 2px var(--accent-glow); }
.switch-text { font-size: 11px; font-weight: 600; letter-spacing: 0.3px; color: var(--text-dim); }
.switch-input:checked ~ .switch-text { color: var(--text); }

/* delete (text) button in the device detail header */
.btn-del {
  appearance: none; background: transparent; color: var(--danger);
  border: 1px solid var(--border); border-radius: var(--r-sm);
  padding: 6px 12px; font: inherit; font-size: 12px; font-weight: 600;
  cursor: pointer;
  transition: background 0.15s var(--ease), border-color 0.15s var(--ease);
}
.btn-del:hover { background: rgba(255, 107, 139, 0.12); border-color: var(--danger); }

/* edit (text) button in the device detail header */
.btn-edit {
  appearance: none; background: transparent; color: var(--text-dim);
  border: 1px solid var(--border); border-radius: var(--r-sm);
  padding: 6px 12px; font: inherit; font-size: 12px; font-weight: 600;
  cursor: pointer;
  transition: color 0.15s var(--ease), border-color 0.15s var(--ease);
}
.btn-edit:hover { color: var(--accent); border-color: var(--accent-dim); }

/* read-only name/public-key block at the top of the edit modal */
.edit-meta { display: flex; flex-direction: column; gap: var(--space-3); }
.device-item.is-disabled { opacity: .5; }
.icon-btn {
  background: transparent; border: none; color: #8a99ad; cursor: pointer;
  font-size: 13px; padding: 2px 5px; border-radius: 6px;
}
.icon-btn:hover { background: #1b2738; color: #e7eef6; }
.icon-danger:hover { color: #ff6b6b; }

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(6, 11, 18, .66);
  display: flex; align-items: center; justify-content: center; z-index: 1000;
}
.modal-backdrop[hidden] { display: none; }
.modal-card {
  width: min(420px, 92vw); background: #111b29; border: 1px solid #243246;
  border-radius: 14px; padding: 22px; display: flex; flex-direction: column; gap: 14px;
  box-shadow: 0 24px 60px rgba(0,0,0,.5);
}
.modal-title { margin: 0; font-size: 17px; color: #e7eef6; }
.field { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: #cdd8e5; }
.field[hidden], .field-check[hidden] { display: none; }
.field em { color: #6f8097; font-style: normal; }
.field input {
  background: #0c1420; border: 1px solid #243246; border-radius: 8px;
  padding: 9px 11px; color: #e7eef6; font: inherit;
}
.field input:focus { outline: none; border-color: #2563eb; }
.field-check { display: flex; align-items: center; gap: 8px; font-size: 13px; color: #cdd8e5; }
.modal-error {
  margin: 0; color: #ff8a8a; font-size: 12.5px; background: #2a1416;
  border: 1px solid #5a2226; border-radius: 8px; padding: 8px 10px;
}
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 4px; }

/* ---------- Icon picker (device forms) ---------- */
.field-color input[type="color"] {
  width: 56px; height: 34px; padding: 2px; cursor: pointer;
  background: #0c1420; border: 1px solid #243246; border-radius: 8px;
}
.icon-picker { --icon-color: var(--accent); display: flex; flex-wrap: wrap; gap: 6px; }
.icon-opt {
  display: grid; place-items: center; width: 40px; height: 40px;
  background: #0c1420; border: 1px solid #243246; border-radius: 9px;
  color: var(--icon-color); cursor: pointer; transition: border-color .12s, box-shadow .12s;
}
.icon-opt:hover { border-color: var(--accent-dim); }
.icon-opt.is-active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent), 0 0 10px var(--accent-glow); }
.icon-opt .icon-none { color: var(--text-faint); font-size: 18px; line-height: 1; }

/* ---------- Map node icon marker ---------- */
.map-pin-wrap { background: none; border: 0; }
.map-pin {
  width: 100%; height: 100%; display: grid; place-items: center;
  filter: drop-shadow(0 0 1px #0b1622) drop-shadow(0 1px 2px rgba(0,0,0,.55));
}
.map-pin svg { display: block; }

/* Uniform monochrome device badge: a neutral circle with a light glyph inside.
   Used on map markers, the Devices list and the Links table. */
.icon-badge {
  display: inline-flex; align-items: center; justify-content: center;
  flex: 0 0 auto; border-radius: 50%;
  background: rgba(28, 40, 56, 0.82);
  border: 1px solid rgba(255, 255, 255, 0.16);
  color: #e7eef6;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
}
.icon-badge svg { display: block; }

/* ---------- Mobile / responsive ---------- */
html { -webkit-text-size-adjust: 100%; }

@media (max-width: 768px) {
  :root { --topbar-h: 54px; }

  /* Compact header so brand + tabs + actions fit one row */
  .topbar { gap: var(--space-3); padding: 0 var(--space-3); }
  .brand-name { font-size: 15px; }
  .brand-mark svg { width: 26px; height: 26px; }
  .tabs { gap: 2px; padding: 3px; }
  .tab { padding: 6px 12px; font-size: 12px; }
  .status { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  /* Devices: stack the list above the detail/charts instead of side-by-side */
  #view-devices.is-active {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(110px, 34vh) minmax(0, 1fr);
  }
  .device-list { border-right: 0; border-bottom: 1px solid var(--border); }
  .device-detail { min-height: 0; }
  .detail-header {
    height: auto; min-height: 52px; flex-wrap: wrap;
    padding: var(--space-2) var(--space-3); gap: var(--space-2);
  }
  .range-select { margin-left: 0; flex-wrap: wrap; }
  .detail-info { grid-template-columns: 1fr; padding: var(--space-3); }
  .charts-grid { grid-template-columns: 1fr; padding: var(--space-3); gap: var(--space-3); }
  .chart-card { min-height: 200px; }

  /* lift the control bar clear of the (wrapping) Leaflet attribution; wrap if tight */
  .map-bar { left: var(--space-2); bottom: 34px; right: var(--space-2); flex-wrap: wrap; }
  .map-btn { padding: 8px 11px; }
  .popover { max-height: 50vh; }
  .leaflet-control-attribution { font-size: 10px; }
}

@media (max-width: 420px) {
  /* On very small screens keep the live/stale dot but drop the timestamp text */
  .status { font-size: 0; gap: 0; }
  .topbar { gap: var(--space-2); }
}

/* ---- Messages tab (packets table) ---- */
/* Full-width scroll container like .info-page, with a wider content column
   to fit the dense packets table. */
.packets-page { flex: 1 1 auto; overflow-y: auto; width: 100%;
  padding: var(--space-4) var(--space-3) var(--space-6); }
.packets-content { max-width: 1480px; margin: 0 auto; }
.packets-page h1 { font-size: 22px; margin: 0 0 var(--space-2); letter-spacing: -0.01em; }
.packets-summary { color: var(--text-dim); font-size: 14px; font-weight: 400; margin-left: 6px; }

/* Filter bar: server-side filters + client-side Grouping / LIVE toggles. */
.packets-toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2);
  margin: 0 0 var(--space-3); }
.pk-input { background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--r-sm);
  color: var(--text); font: inherit; font-size: 13px; padding: 7px 10px; min-width: 150px; }
.pk-input::placeholder { color: var(--text-faint); }
.pk-input:focus { outline: none; border-color: var(--accent-dim); box-shadow: 0 0 0 2px var(--accent-glow); }
.pk-toggles { display: flex; gap: var(--space-2); margin-left: auto; }
/* Segmented time-window selector: same chip look as the toggles, tighter gap */
.pk-windows { display: flex; gap: 4px; }
.pk-toggle, .pk-window { appearance: none; cursor: pointer; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: var(--r-sm); color: var(--text-dim);
  font: inherit; font-size: 12px; font-weight: 600; letter-spacing: 0.03em;
  padding: 7px 12px; transition: color 0.15s, border-color 0.15s, background 0.15s; }
.pk-window { padding: 7px 11px; }  /* a touch tighter than the toggles */
.pk-toggle:hover, .pk-window:hover { border-color: var(--accent-dim); color: var(--text); }
.pk-toggle.is-on, .pk-window.is-on { background: rgba(52, 214, 198, 0.08);
  border-color: var(--accent-dim); color: var(--accent); }

/* Links tab: direct vs route (indirect) edge tag. Route matches the map's
   violet route-links colour; route rows get a faint violet left accent. */
.link-kind { display: inline-block; font-size: 10.5px; font-weight: 700;
  letter-spacing: 0.04em; text-transform: uppercase; padding: 2px 7px;
  border-radius: 999px; border: 1px solid var(--border); color: var(--text-dim); }
.link-kind-route { color: #a78bfa; border-color: rgba(139, 92, 246, 0.5);
  background: rgba(139, 92, 246, 0.12); }
.is-route-link { box-shadow: inset 3px 0 0 rgba(139, 92, 246, 0.55); }
/* LIVE gets a status dot: accent while following the tick, faint when paused */
.pk-toggle-live::before { content: ""; display: inline-block; width: 7px; height: 7px;
  border-radius: 50%; margin-right: 6px; background: var(--text-faint); vertical-align: 1px; }
.pk-toggle-live.is-on::before { background: var(--accent); box-shadow: 0 0 6px var(--accent-glow); }

/* Table: dense monospace-ish rows with subtle separators; the wrapper scrolls
   horizontally so nowrap cells never break the page layout. */
.packets-table-wrap { overflow-x: auto; border: 1px solid var(--border-soft); border-radius: var(--r-md);
  background: var(--surface); }
.packets-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.packets-table th, .packets-table td { padding: 6px 10px; text-align: left; vertical-align: top;
  white-space: nowrap; border-bottom: 1px solid var(--border-soft); }
.packets-table thead th { position: sticky; top: 0; z-index: 1; background: var(--surface-2);
  color: var(--text-dim); font-weight: 600; font-size: 11px; text-transform: uppercase;
  letter-spacing: 0.04em; }
.packets-table tbody tr:last-child td { border-bottom: none; }
.packets-row:hover { background: var(--surface-2); }
.packets-row.is-expandable { cursor: pointer; }
.packets-row.is-open td { background: rgba(122, 79, 221, 0.15); }
/* Leg grouping: a bold rule before each message (packet hash), a thinner one
   before each separate reception of the same message. */
.packets-row.is-msg-start td { border-top: 2px solid var(--border); }
.packets-row.is-recv-start td { border-top: 1px solid var(--border); }
/* Expanded per-hop legs share the open summary's highlight, so the whole
   expanded group reads as one coloured block; legs keep dimmed text. */
.packets-row.is-leg td { background: rgba(122, 79, 221, 0.15); color: var(--text-dim); }
.packets-table .col-group { width: 36px; padding-right: 2px; text-align: right; }
.packets-table .col-time { color: var(--text-dim); font-variant-numeric: tabular-nums; }
.packets-table .col-observer { color: var(--text); font-weight: 600; }
.packets-table .col-type { color: var(--text-dim); }
.packets-table .col-details { white-space: normal; min-width: 220px; max-width: 420px;
  word-break: break-word; }
/* +N badge in the left gutter: duplicate sightings collapsed under this row */
.pk-badge { display: inline-block; min-width: 24px; padding: 1px 6px; border-radius: 999px;
  background: var(--surface-3); border: 1px solid var(--border); color: var(--accent);
  font-family: var(--mono); font-size: 11px; font-weight: 600; text-align: center; }
.pk-hex { font-family: var(--mono); font-size: 12px; }
.pk-hex.pk-known { color: var(--text); }          /* white = resolved to a known device */
.pk-hex.pk-unknown { color: var(--warn); }         /* amber = unknown / unresolved node */
.col-type.is-origin-unknown { color: var(--warn); }
.pk-route { font-size: 11px; font-weight: 600; letter-spacing: 0.02em; }
.pk-route-direct { color: var(--accent); }   /* teal = point-to-point */
.pk-route-zerohop { color: #7ee787; }         /* green = zero-hop (direct, line-of-sight) */
.pk-route-flood { color: var(--warn); }       /* amber = flooded */
/* Node cells click to seed the matching Src/Dst/Originator/Observer filter. */
.pk-clickable { cursor: pointer; }
.pk-clickable:hover .pk-hex { text-decoration: underline; }
/* Link-quality traffic lights (SNR/RSSI columns) */
.pk-sig-good { color: var(--accent); }
.pk-sig-mid { color: var(--warn); }
.pk-sig-bad { color: var(--danger); }
.pk-details { color: var(--text); }
.pk-muted { color: var(--text-faint); }
/* Expanded sightings: slightly recessed sub-rows under the grouped packet */
.packets-sighting td { background: rgba(0, 0, 0, 0.18); color: var(--text-dim); }
.packets-sighting .col-observer { font-weight: 400; }
.pk-signal { font-family: var(--mono); font-size: 12px; color: var(--text-dim); }
.packets-empty td { text-align: center; color: var(--text-faint); padding: 28px;
  white-space: normal; }
.muted { color: var(--text-dim); }

/* Messages tab is admin-only: hidden unless the body carries .is-authed (set
   by auth.js on login; cleared on logout via reload). Works for both the tab
   button and the view itself, so a logged-out user can't reach the feed. */
body:not(.is-authed) .tab[data-tab="messages"] { display: none !important; }
body:not(.is-authed) #view-messages { display: none !important; }
body:not(.is-authed) .tab[data-tab="links"] { display: none !important; }
body:not(.is-authed) #view-links { display: none !important; }

/* ---- Channels tab (read-only group-chat viewer) ---- */
body:not(.is-authed) .tab[data-tab="channels"] { display: none !important; }
body:not(.is-authed) #view-channels { display: none !important; }
#view-channels.is-active { display: grid; grid-template-columns: 300px 1fr;
  grid-template-rows: minmax(0, 1fr); }
/* Left pane: channel list */
.ch-list-pane { display: flex; flex-direction: column; min-height: 0;
  border-right: 1px solid var(--border); background: var(--surface); }
.ch-list-head { display: flex; align-items: center; padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border-soft); }
.ch-list-head h2 { margin: 0; font-size: 15px; font-weight: 700; color: var(--text); }
.ch-list { flex: 1; overflow-y: auto; padding: var(--space-2); }
.ch-item { display: grid; grid-template-columns: 40px 1fr; gap: var(--space-3);
  align-items: center; padding: var(--space-3); border-radius: var(--r-md); cursor: pointer;
  border: 1px solid transparent; }
.ch-item:hover { background: var(--surface-2); }
.ch-item.is-active { background: var(--surface-3); border-color: var(--accent-dim);
  box-shadow: inset 3px 0 0 var(--accent); }
.ch-ava { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center;
  background: var(--surface-3); color: var(--text-dim); font-size: 16px; flex: none; }
.ch-meta { min-width: 0; }
.ch-row1 { display: flex; align-items: baseline; gap: var(--space-2); }
.ch-name { font-weight: 600; color: var(--text); white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; }
.ch-time { margin-left: auto; flex: none; font-size: 11px; color: var(--text-faint);
  font-family: var(--mono); }
.ch-preview { font-size: 12.5px; color: var(--text-dim); white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; }
/* Right pane: messages */
.ch-msg-pane { display: flex; flex-direction: column; min-height: 0; background: var(--bg); }
.ch-msg-head { display: flex; align-items: center; gap: var(--space-3);
  padding: var(--space-3) var(--space-4); border-bottom: 1px solid var(--border-soft);
  background: var(--surface); }
.ch-title { margin: 0; font-size: 15px; font-weight: 700; color: var(--text); white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; }
.ch-windows { display: flex; gap: 4px; margin-left: auto; flex: none; }
.ch-window { appearance: none; cursor: pointer; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: var(--r-sm); color: var(--text-dim);
  font: inherit; font-size: 12px; font-weight: 600; letter-spacing: 0.03em; padding: 7px 11px;
  transition: color 0.15s, border-color 0.15s, background 0.15s; }
.ch-window:hover { border-color: var(--accent-dim); color: var(--text); }
.ch-window.is-on { background: rgba(52, 214, 198, 0.08); border-color: var(--accent-dim);
  color: var(--accent); }
.ch-messages { flex: 1; overflow-y: auto; padding: var(--space-4) var(--space-5); display: flex;
  flex-direction: column; gap: var(--space-2); }
.ch-msg { display: grid; grid-template-columns: 34px 1fr; gap: var(--space-3);
  align-items: start; max-width: 680px; }
.ch-msg .ch-ava { width: 34px; height: 34px; color: var(--bg); font-weight: 700; font-size: 14px; }
.ch-bubble { background: var(--surface); border: 1px solid var(--border-soft);
  border-radius: var(--r-lg); border-top-left-radius: 4px; padding: var(--space-2) var(--space-3); }
.ch-bubble .ch-head { display: flex; align-items: baseline; gap: var(--space-3); margin-bottom: 2px; }
.ch-sender { font-weight: 700; font-size: 13px; }
.ch-mtime { margin-left: auto; font-size: 11px; color: var(--text-faint); font-family: var(--mono); }
.ch-text { color: var(--text); font-size: 14px; line-height: 1.35; white-space: pre-wrap;
  word-break: break-word; }
.ch-empty { margin: auto; color: var(--text-faint); text-align: center; padding: var(--space-6); }
@media (max-width: 768px) {
  #view-channels.is-active { grid-template-columns: 1fr; grid-template-rows: 38vh minmax(0, 1fr); }
  .ch-list-pane { border-right: 0; border-bottom: 1px solid var(--border); }
}
.links-empty { padding: var(--space-4); text-align: center; color: var(--text-faint); }
#view-links .col-from .dot { display: inline-block; width: 8px; height: 8px;
  border-radius: 50%; margin-right: 6px; vertical-align: middle; }
/* Links on/off switch inside the Devices popover. */
.links-switch { display: flex; align-items: center; gap: var(--space-2); width: 100%;
  padding: 5px 6px; margin: 2px 0 6px; background: none; border: 0; cursor: pointer;
  color: var(--text-dim); font: inherit; font-size: 12px; text-align: left; }
.links-switch:hover, .links-switch.is-on { color: var(--text); }
.links-switch-track { position: relative; width: 30px; height: 16px; flex: 0 0 auto;
  border-radius: 999px; background: var(--surface-3); border: 1px solid var(--border);
  transition: background 150ms ease, border-color 150ms ease; }
.links-switch-thumb { position: absolute; top: 1px; left: 1px; width: 12px; height: 12px;
  border-radius: 50%; background: var(--text-faint); transition: transform 150ms ease, background 150ms ease; }
.links-switch.is-on .links-switch-track { background: rgba(52, 214, 198, 0.25); border-color: var(--accent-dim); }
.links-switch.is-on .links-switch-thumb { transform: translateX(14px); background: var(--accent); }
/* Link arcs are hover-only (tooltip); suppress the click/focus outline the
   browser draws around the selected SVG path. */
.link-arc, .link-arc:focus, .link-arc:focus-visible { outline: none; }
