/* TrackPro operator console — dense, fast, themeable. Brand color comes from
   window.TRACKPRO_CONFIG.theme.primary (see config.js).
   THEMING: every color in this file goes through a variable below. Dark is
   the default (:root); light overrides the SAME variables under
   :root[data-theme="light"]. Adding a theme = adding one variable block. */
:root {
  --primary: #1f6feb;
  --bg: #0d1117;
  --bg-2: #161b22;
  --bg-3: #1c2330;
  --border: #30363d;
  --text: #e6edf3;
  --muted: #8b949e;
  --ok: #2ea043;
  --warn: #d29922;
  --bad: #f85149;
  --moving: #2ea043;
  --stopped: #8b949e;
  --idle: #d29922;
  /* semantic tints: soft background + readable foreground per state */
  --ok-bg: #123c1c;   --ok-fg: #6fdd8b;
  --warn-bg: #3a2f11; --warn-fg: #e3b341;
  --bad-bg: #3d1518;  --bad-fg: #ffb4ae;
  --neutral-bg: #21262d;
  --sel-bg: #132c4d;                 /* selected list row / picked tile */
  --link: #58a6ff; --link-bg: #102237; --link-border: #23415f;
  --map-bg: #0b1220;                 /* map canvas behind tiles */
  --pin-border: #0b1220;             /* marker outline against tiles */
  --label-bg: rgba(13,17,23,.85);    /* map name-label bubble */
  --shadow-c: rgba(0,0,0,.5);        /* dropdown / floating panel shadow */
  --sheet: rgba(28,28,30,0.90);      /* device card - the phone app's sheet color */
}
:root[data-theme="light"] {
  --primary: #1f6feb;
  --bg: #e9ebee;      /* owner: the white was too white - modern light greys */
  --bg-2: #f3f4f6;
  --bg-3: #e2e5e9;
  --border: #cdd3da;
  --text: #111418;
  --muted: #59636e;
  --ok: #1a7f37;
  --warn: #9a6700;
  --bad: #d1242f;
  --moving: #1a7f37;
  --stopped: #848d97;
  --idle: #bf8700;
  --ok-bg: #dafbe1;   --ok-fg: #1a7f37;
  --warn-bg: #fff8c5; --warn-fg: #9a6700;
  --bad-bg: #ffebe9;  --bad-fg: #d1242f;
  --neutral-bg: #eef1f4;
  --sel-bg: #ddf4ff;
  --link: #0969da; --link-bg: #ddf4ff; --link-border: #b6e3ff;
  --map-bg: #e6e9ee;
  --pin-border: #ffffff;
  --label-bg: rgba(255,255,255,.92);
  --shadow-c: rgba(31,35,40,.2);
  --sheet: rgba(242,242,247,0.90);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.45 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: var(--bg); color: var(--text); overflow: hidden;
}
button {
  font: inherit; cursor: pointer; border: 1px solid var(--border);
  background: var(--bg-3); color: var(--text); border-radius: 8px; padding: 8px 12px;
}
button:hover:not(:disabled) { border-color: var(--primary); }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--primary); border-color: var(--primary); color: #fff; font-weight: 600; }
button.danger { background: var(--bad-bg); border-color: var(--bad); color: var(--bad-fg); }
button.danger:hover { background: var(--bad); color: #fff; }
input, select {
  font: inherit; background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; width: 100%;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }

/* ── Login ───────────────────────────────────────────────────────────── */
#login {
  position: fixed; inset: 0; display: grid; place-items: center; background: var(--bg);
}
#login form {
  width: 340px; background: var(--bg-2); border: 1px solid var(--border);
  border-radius: 14px; padding: 28px; display: grid; gap: 12px;
}
#login h1 { margin: 0 0 4px; font-size: 22px; }
#login .sub { margin: 0 0 8px; color: var(--muted); font-size: 13px; }
#login label { font-size: 12px; color: var(--muted); display: grid; gap: 4px; }
#login .err { color: var(--bad); font-size: 13px; min-height: 18px; }

/* ── App layout ──────────────────────────────────────────────────────── */
#app { position: fixed; inset: 0; display: grid; grid-template-rows: 48px 1fr; }
header {
  display: flex; align-items: center; gap: 0; padding: 0;
  background: var(--bg-2); border-bottom: 1px solid var(--border);
}
/* Owner: bigger TrackPro TEXT only - the icon and the 48px bar stay put. */
header .brand { font-weight: 700; font-size: 21.5px; color: var(--text); display: inline-flex; align-items: center; gap: 7px; cursor: pointer; padding: 0 10px 0 2px; user-select: none; }
header .brand b { color: var(--primary); }
.brand-ico { height: 26px; width: auto; display: block; }
#login .login-brand { margin: 0 0 4px; font-size: 22px; display: flex; align-items: center; gap: 9px; }
#login .login-ico { height: 34px; }
header .spacer { flex: 1; }

/* ⋮ kebab menu (all tools/actions live here, REA-style). Full-height hit
   area + the phone-blue hover, like every header control (§74). */
.hmenu { position: relative; align-self: stretch; display: flex; }
.hmenu-btn { background: none; border: none; color: var(--text); font-size: 24px; font-weight: 700; line-height: 1; padding: 0 13px; cursor: pointer; border-radius: 0; height: 100%; display: inline-flex; align-items: center; }
.hmenu-btn:hover { background: var(--nav-hover); }
.hmenu-dropdown { display: none; position: absolute; top: calc(100% + 8px); left: 0; min-width: 230px; background: var(--bg-2); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 8px 28px var(--shadow-c); overflow: hidden; z-index: 1000; }
.hmenu-dropdown.open { display: block; }
.hmenu-user { display: flex; align-items: center; gap: 10px; padding: 12px 14px; font-weight: 600; font-size: 13px; }
.hmenu-div { height: 1px; background: var(--border); }
.hmenu-item { display: flex; align-items: center; gap: 10px; width: 100%; padding: 10px 14px; border: none; background: none; color: var(--text); cursor: pointer; font-size: 14px; text-align: left; border-radius: 0; }
.hmenu-item:hover { background: var(--bg-3); }
.hmenu-item .badge-count { margin-left: auto; }
.hmi { width: 18px; text-align: center; flex-shrink: 0; }
/* Basemap + fullscreen: same full-height clickable band as the nav (§74).
   The icon keeps its size; only the hit area and hover changed. */
.ctrl-btn { background: none; border: none; color: var(--text); border-radius: 0; padding: 0 13px; line-height: 1; cursor: pointer; height: 100%; display: inline-flex; align-items: center; align-self: stretch; }
.ctrl-btn:hover { background: var(--nav-hover); }

/* REA layout: the map fills the whole area; the unit list floats over it as an
   absolute panel that slides away on collapse (so the map never resizes/breaks). */
main { position: relative; min-height: 0; }
#sidebar {
  position: absolute; top: 0; left: 0; bottom: 0; width: 320px; z-index: 500;
  background: var(--bg-2); border-right: 1px solid var(--border);
  display: grid; grid-template-rows: auto 1fr; min-height: 0;
  transition: transform .3s ease; box-shadow: 2px 0 12px rgba(0,0,0,.35);
}
#app.nolist #sidebar { transform: translateX(-100%); }
#sidebar .search { padding: 10px; border-bottom: 1px solid var(--border); display: flex; gap: 8px; align-items: center; }
#sidebar .search input { flex: 1; width: auto; }
#sidebar .search button { white-space: nowrap; }
#unitlist { overflow-y: auto; }
.unit {
  display: grid; grid-template-columns: 10px 1fr auto; gap: 8px; align-items: center;
  padding: 10px 12px; border-bottom: 1px solid var(--border); cursor: pointer;
}
.unit:hover { background: var(--bg-3); }
.unit.selected { background: var(--bg-3); box-shadow: inset 3px 0 0 var(--primary); }
.unit .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--stopped); }
.unit .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.unit .sub { font-size: 12px; color: var(--muted); }
.unit .speed { font-variant-numeric: tabular-nums; font-size: 12px; color: var(--muted); }

#mapwrap { position: absolute; inset: 0; }
#map { position: absolute; inset: 0; background: var(--map-bg); }
.maplibregl-popup-content { background: var(--bg-2); color: var(--text); border: 1px solid var(--border); border-radius: 8px; }
.maplibregl-popup-tip { display: none; }

/* Basemap switcher (OSM / Google streets / satellite / hybrid) */
.maplibregl-ctrl-group.basemap-ctrl { background: var(--bg-2); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 1px 4px rgba(0,0,0,.4); overflow: hidden; }
.basemap-ctrl select { width: auto; background: var(--bg-2); color: var(--text); border: none; border-radius: 8px; padding: 6px 26px 6px 10px; font-size: 12px; font-weight: 600; cursor: pointer; }
.basemap-ctrl select:focus { outline: none; }
.basemap-ctrl select option { background: var(--bg-2); color: var(--text); }
/* The top-left map control (basemap switcher) sits under the floating panel and
   the panel-toggle tab — slide it right to clear both, less when collapsed. */
.maplibregl-ctrl-top-left { transition: transform .3s ease; transform: translateX(344px); }
#app.nolist .maplibregl-ctrl-top-left { transform: translateX(24px); }

/* Unit marker (DOM) — used for the history-replay cursor. Live units render as a
   clustered circle layer; clusters render as the HTML bubbles below. */
.mk { width: 16px; height: 16px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.5); cursor: pointer; }
.mk.moving { background: var(--moving); }
.mk.stopped { background: var(--stopped); }
.mk.idle { background: var(--idle); }
.mk.sel { width: 20px; height: 20px; box-shadow: 0 0 0 3px var(--primary); }

/* Cluster bubble (HTML marker) */
.mk-cluster {
  display: flex; align-items: center; justify-content: center;
  min-width: 30px; height: 30px; padding: 0 7px; border-radius: 16px;
  background: var(--primary); color: #fff; font-weight: 700; font-size: 13px;
  border: 2px solid #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.5); cursor: pointer;
  font-variant-numeric: tabular-nums;
}
.mk-cluster:hover { filter: brightness(1.1); }

/* Cluster contents panel */
#clusterList {
  position: absolute; top: 12px; left: 332px; width: 250px; max-height: calc(100% - 24px);
  overflow-y: auto; background: var(--bg-2); border: 1px solid var(--border);
  border-radius: 12px; z-index: 502; box-shadow: 0 4px 20px var(--shadow-c);
  transition: left .3s ease;
}
#app.nolist #clusterList { left: 12px; }
#clusterList .cl-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--bg-2); font-weight: 600; }
#clusterList .cl-actions { display: flex; gap: 4px; }
#clusterList .cl-actions button { padding: 2px 8px; }
.cl-row { display: grid; grid-template-columns: 12px 1fr auto; gap: 8px; align-items: center; padding: 8px 12px; border-bottom: 1px solid var(--border); cursor: pointer; }
.cl-row:last-child { border-bottom: 0; }
.cl-row:hover { background: var(--bg-3); }
.cl-dot { width: 10px; height: 10px; border-radius: 50%; }
.cl-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cl-sub { font-size: 12px; color: var(--muted); }

/* Panel toggle tab (sits at the panel's right edge; slides to 0 when collapsed) */
#sidebarToggle {
  position: absolute; left: 320px; top: 12px; z-index: 501;
  width: 16px; height: 46px; padding: 0; line-height: 1; font-size: 12px;
  background: var(--bg-2); border: 1px solid var(--border); border-left: none;
  border-radius: 0 8px 8px 0; color: var(--muted);
  box-shadow: 2px 2px 8px rgba(0,0,0,.4); transition: left .3s ease;
}
#sidebarToggle:hover { color: var(--text); background: var(--bg-3); }
#app.nolist #sidebarToggle { left: 0; }

/* ── Detail panel ────────────────────────────────────────────────────── */
#detail {
  position: absolute; top: 12px; right: 12px; width: 300px; max-height: calc(100% - 92px);
  overflow-y: auto; background: var(--bg-2); border: 1px solid var(--border);
  border-radius: 12px; padding: 14px; display: grid; gap: 10px; z-index: 503;
}
#detail h2 { margin: 0; font-size: 16px; }
#detail .close { position: absolute; top: 10px; right: 10px; padding: 2px 8px; }
#detail .kv { display: grid; grid-template-columns: 84px 1fr; gap: 4px 8px; font-size: 13px; }
#detail .kv span:nth-child(odd) { color: var(--muted); }
#detail .row { display: flex; gap: 8px; flex-wrap: wrap; }
#detail .row button { flex: 1; min-width: 120px; }
.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge.moving { background: var(--ok-bg); color: var(--ok-fg); }
.badge.stopped { background: var(--neutral-bg); color: var(--muted); }
.badge.idle { background: var(--warn-bg); color: var(--warn-fg); }
.badge.online { background: var(--ok-bg); color: var(--ok-fg); }
.badge.offline { background: var(--bad-bg); color: var(--bad-fg); }

/* ── History replay bar ──────────────────────────────────────────────── */
#history {
  position: absolute; left: 332px; bottom: 12px; right: 324px; z-index: 502;
  background: var(--bg-2); border: 1px solid var(--border); border-radius: 12px;
  padding: 10px 14px; display: grid; gap: 8px; transition: left .3s ease;
}
#app.nolist #history { left: 12px; }
#history .controls { display: flex; gap: 8px; align-items: center; }
#history .controls input[type=datetime-local] { width: auto; }
#history .scrub { display: flex; gap: 10px; align-items: center; }
#history input[type=range] { flex: 1; }
#history .ts { font-variant-numeric: tabular-nums; color: var(--muted); min-width: 148px; text-align: right; }

#toast {
  position: absolute; bottom: 16px; left: 50%; transform: translateX(-50%);
  background: var(--bg-3); border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 16px; z-index: 600; max-width: 70%;
}
#toast.err { border-color: var(--bad); color: var(--bad-fg); }
#toast.ok { border-color: var(--ok); color: var(--ok-fg); }

/* ── Modal overlay (admin / password / edit) ─────────────────────────── */
#overlay { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: grid; place-items: center; z-index: 2000; }
#overlayCard {
  width: 640px; max-width: 94vw; max-height: 88vh; overflow-y: auto;
  background: var(--bg-2); border: 1px solid var(--border); border-radius: 14px; padding: 20px;
}
#overlayCard.narrow { width: 380px; }
#overlayCard h2 { margin: 0 0 12px; font-size: 18px; }
#overlayCard .modrow { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
#overlayCard .close { padding: 2px 10px; }
.tabs { display: flex; gap: 6px; margin-bottom: 14px; border-bottom: 1px solid var(--border); }
.tabs button { border: none; background: none; border-radius: 0; border-bottom: 2px solid transparent; padding: 8px 12px; color: var(--muted); }
.tabs button.active { color: var(--text); border-bottom-color: var(--primary); }
.formgrid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
.formgrid label { font-size: 12px; color: var(--muted); display: grid; gap: 4px; }
.formgrid .full { grid-column: 1 / -1; }
table.grid { width: 100%; border-collapse: collapse; font-size: 13px; }
table.grid th, table.grid td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.grid th { color: var(--muted); font-weight: 600; }
table.grid td .act { display: flex; gap: 6px; }
table.grid td button { padding: 4px 8px; font-size: 12px; }
table.grid select { width: auto; padding: 4px 6px; }
.hint { color: var(--muted); font-size: 12px; margin: 6px 0 0; }
.section { border-top: 1px solid var(--border); margin-top: 14px; padding-top: 14px; }
.badge-count { display: inline-block; margin-left: 6px; background: var(--bad); color: #fff; border-radius: 999px; font-size: 11px; font-weight: 700; padding: 0 6px; min-width: 16px; text-align: center; }
.sev-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; }
.sev-info { background: var(--muted); } .sev-warning { background: var(--warn); } .sev-critical { background: var(--bad); }
.alert-toast { position: absolute; top: 12px; left: 50%; transform: translateX(-50%); background: var(--warn-bg); color: var(--warn-fg); border: 1px solid var(--warn); border-radius: 8px; padding: 8px 14px; z-index: 520; }

/* ═══════════════════════════════════════════════════════════════════════
   Modular UI (REA-style): full-height panel, top nav, Leaflet map, popups.
   Appended after the legacy rules so these win where they overlap.
   ═══════════════════════════════════════════════════════════════════════ */

/* ── Header: top feature nav ─────────────────────────────────────────── */
/* Owner (§74): the CLICKABLE area of every header control spans the bar's
   FULL height - no dead space above/below a button, especially noticeable in
   fullscreen. The bar stays 48px; hover paints the phone app's blue.
   --nav-hover derives from the app's primary #007AFF. */
:root { --nav-hover: rgba(0, 122, 255, 0.22); }
:root[data-theme="light"] { --nav-hover: rgba(0, 122, 255, 0.14); }
.topnav { display: flex; gap: 0; margin-left: 0; align-self: stretch; align-items: stretch; }
/* §79: buttons sit shoulder to shoulder (no dead space), full-height hit
   areas, REAL text color (not grey), no borders anywhere - hover is the
   blue tint, and the ACTIVE tab is unmistakably solid blue. */
.nav-btn {
  background: none; border: none; border-radius: 0; color: var(--text);
  padding: 0 12px; cursor: pointer; font-size: 14.5px; font-weight: 600; white-space: nowrap;
  display: inline-flex; align-items: center; gap: 7px; height: 100%;
}
.nav-btn:hover { background: var(--nav-hover); }
.nav-btn.active { color: #fff; background: var(--primary); }
.nav-btn.active:hover { background: var(--primary); }
.nvi { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: 0 0 auto; }
.nvi-lg { width: 18px; height: 18px; }
.nvi-bold { stroke-width: 2.6; }
@media (max-width: 720px) { .nav-btn { padding: 0 9px; } .nav-btn > span { display: none; } .nav-btn .badge-count { display: inline-flex; } }

/* ── Full-height left panel ──────────────────────────────────────────── */
#panel {
  position: absolute; top: 10px; bottom: 14px; left: 10px; width: 340px; z-index: 500;
  background: var(--bg-2); border: 1px solid var(--border); border-radius: 14px;
  box-shadow: 0 8px 30px var(--shadow-c);
  display: flex; flex-direction: column; overflow: hidden;
  transition: transform .2s ease;
}
#panel.collapsed { transform: translateX(calc(-100% - 12px)); }
#panelSearch, #panelDetail { display: flex; flex-direction: column; min-height: 0; flex: 1; overflow: hidden; }
#panelSearch.hidden, #panelDetail.hidden { display: none; }

.panel-filters { padding: 12px; border-bottom: 1px solid var(--border); display: flex; flex-direction: column; gap: 8px; }
.pf-row { display: flex; gap: 8px; }
.pf-select { flex: 1; }
.pf-row .primary { width: 40px; flex: 0 0 auto; padding: 8px 0; }
.pf-search { position: relative; }
.pf-search input { padding-right: 34px; }
.pf-mag { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
.pf-hint { font-size: 12px; color: var(--muted); }

#unitlist { overflow-y: auto; flex: 1; padding: 6px; }
#panel .unit {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 9px; cursor: pointer;
}
#panel .unit:hover { background: var(--bg-3); }
#panel .unit.selected { background: var(--sel-bg); box-shadow: inset 3px 0 0 var(--primary); }
#panel .unit .dot { width: 10px; height: 10px; border-radius: 50%; flex: 0 0 auto; background: var(--stopped); }
/* Owner (§77): the dot answers "is it talking?" - green within 24h, red not.
   (#panel-scoped to out-rank the ID-specificity base rule above.) */
#panel .unit .dot.on, .cl-item .dot.on { background: var(--moving); }
#panel .unit .dot.off, .cl-item .dot.off { background: var(--bad); }
#panel .unit .dot.moving { background: var(--moving); }
#panel .unit .dot.idle { background: var(--idle); }
#panel .unit .u-main { min-width: 0; flex: 1; }
#panel .unit .u-name { display: flex; align-items: baseline; gap: 8px; min-width: 0; font-weight: 600; }
#panel .unit .u-sub { font-size: 12px; color: var(--muted); }
/* Name + motion word ("Godwin ABJ-892-PP  moving 26km/h") - the name
   truncates, the motion word never does. moving green / idle amber /
   parked grey; speed rides only with moving. */
.u-nm { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.u-mo { flex: 0 0 auto; font-size: 12px; font-weight: 700; font-variant-numeric: tabular-nums; }
.u-mo.moving { color: var(--moving); }
.u-mo.idle { color: var(--idle); }
.u-mo.parked { color: var(--stopped); }

/* ── Left panel: tabs, scope chips, expandable entities ──────────────── */
.pf-tabs { display: flex; gap: 4px; }
.pf-tab {
  flex: 1; background: none; border: none; border-bottom: 2px solid transparent;
  color: var(--muted); padding: 6px 2px 8px; border-radius: 0; font-size: 13px; cursor: pointer;
}
.pf-tab:hover { color: var(--text); }
.pf-tab.active { color: var(--text); border-bottom-color: var(--primary); font-weight: 600; }
.pf-chips { display: flex; gap: 6px; }
.pf-chip {
  background: var(--bg-3); border: 1px solid var(--border); color: var(--muted);
  border-radius: 999px; padding: 3px 10px; font-size: 12px; cursor: pointer;
}
.pf-chip.active { background: var(--sel-bg); border-color: var(--primary); color: var(--text); }
.pf-sect { color: var(--muted); font-size: 11.5px; text-transform: uppercase; letter-spacing: .4px; padding: 8px 8px 3px; }
.pf-ent { display: flex; align-items: center; gap: 10px; padding: 8px; border-radius: 8px; cursor: pointer; }
.pf-ent:hover { background: var(--bg-3); }
.pf-ent-ico { font-size: 16px; flex: 0 0 auto; }
.pf-chev { color: var(--muted); flex: 0 0 auto; }
.pf-entbody { margin: 0 0 6px 14px; padding-left: 8px; border-left: 2px solid var(--border); }
.pf-subsearch { padding: 4px 8px 6px; }
.pf-subsearch input { width: 100%; font-size: 13px; padding: 6px 10px; }
.pf-more {
  display: block; width: calc(100% - 16px); margin: 6px 8px; padding: 7px 0;
  background: var(--bg-3); border: 1px dashed var(--border); color: var(--muted);
  border-radius: 8px; font-size: 12.5px; cursor: pointer;
}
.pf-more:hover { color: var(--text); border-color: var(--primary); }

/* ── Floating device card (the phone sheet, web edition) ─────────────── */
/* The phone app's bottom sheet, faithfully (§77): flush to the bottom edge,
   top corners rounded, 90% solid sheet color, the grab handle, 10px side
   padding. Slide-up entrance like the phone. */
#unitCard {
  position: absolute; left: 50%; bottom: 0; transform: translateX(-50%) translateY(30px);
  width: 580px; max-width: calc(100% - 8px); z-index: 650;
  background: var(--sheet); border: 1px solid var(--border); border-bottom: none;
  border-radius: 14px 14px 0 0;
  box-shadow: 0 -6px 30px var(--shadow-c); padding: 0 10px 16px;
  opacity: 0; transition: transform .28s cubic-bezier(.33,1,.68,1), opacity .28s ease;
}
#unitCard.open { transform: translateX(-50%) translateY(0); opacity: 1; }
#unitCard.hidden { display: none; }
.uc-grab { display: flex; justify-content: center; padding: 3px 0 5px; cursor: pointer; }
.uc-handle { width: 228px; max-width: 60%; height: 5px; border-radius: 3px; background: #8e8e93; }
.uc-head { display: flex; align-items: center; gap: 8px; }
.uc-name { display: flex; align-items: baseline; gap: 10px; min-width: 0; font-weight: 700; font-size: 16px; }
.uc-name .u-mo { font-size: 13px; }
.uc-status { font-size: 13px; font-weight: 700; }
.uc-on { color: var(--moving); }
.uc-off { color: var(--bad); }
.uc-x { background: none; border: none; color: var(--muted); font-size: 14px; cursor: pointer; padding: 4px 6px; }
.uc-x:hover { color: var(--text); }
.uc-addr { color: var(--muted); font-size: 13px; height: 18px; margin-top: 1px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.uc-pills { display: flex; align-items: center; gap: 8px; margin-top: 5px; height: 28px; }
.uc-pill {
  display: inline-flex; align-items: center; gap: 4px; background: none;
  border: 1px solid var(--primary); color: var(--primary); border-radius: 999px;
  padding: 0 11px; height: 26px; font-size: 12px; font-weight: 700; cursor: pointer;
}
.uc-pill.on { background: var(--primary); color: #fff; }
.uc-coords { background: none; border: none; color: var(--primary); font-size: 13.5px; font-weight: 700; cursor: pointer; font-variant-numeric: tabular-nums; }
.uc-muted { color: var(--muted); font-size: 13px; }
.uc-grid { display: flex; justify-content: space-between; margin-top: 7px; }
.uc-cell { width: 32%; display: flex; flex-direction: column; }
.uc-grid .uc-cell:nth-child(2), .uc-actions .uc-slot-mid .uc-cell { align-items: center; text-align: center; }
.uc-grid .uc-cell:nth-child(3) { align-items: flex-end; text-align: right; }
.uc-head .spacer, .uc-pills .spacer { flex: 1; }
.uc-k { color: var(--muted); font-size: 11px; white-space: nowrap; }
.uc-v { font-weight: 600; font-size: 13px; margin-top: 1px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.uc-good { color: var(--moving); }
.uc-bad { color: var(--bad); }
.uc-copy { background: none; border: none; color: var(--text); font-size: 10px; font-weight: 600; cursor: pointer; padding: 0; }
.uc-copy:hover { color: var(--primary); }
.uc-actions { display: flex; justify-content: space-between; align-items: center; margin-top: 9px; }
.uc-slot { width: 32%; display: flex; gap: 6px; }
.uc-slot-mid { justify-content: center; }
.uc-slot-end { justify-content: flex-end; }
.uc-btn {
  position: relative; display: inline-flex; align-items: center; justify-content: center;
  background: var(--bg-3); border: 1px solid var(--muted); color: var(--text);
  border-radius: 999px; height: 32px; padding: 0 14px; font-size: 12px; font-weight: 700; cursor: pointer;
}
.uc-btn:hover:not(:disabled) { border-color: var(--primary); }
.uc-danger { background: #7f1d1d; color: #fff; }
.uc-release { background: #ffd60a; color: #111; }
.uc-q { display: block; font-size: 9px; line-height: 10px; font-weight: 600; opacity: .8; text-align: center; }
.uc-spin {
  width: 11px; height: 11px; border: 2px solid #1113; border-top-color: #111;
  border-radius: 50%; margin-left: 7px; animation: ucspin .8s linear infinite;
}
.uc-spin-light { border-color: #fff3; border-top-color: #fff; }
@keyframes ucspin { to { transform: rotate(360deg); } }
.uc-note { min-height: 0; margin-top: 6px; text-align: center; font-size: 12px; font-weight: 600; line-height: 16px; }
.uc-note:empty { display: none; }
.uc-note-ok { color: var(--moving); }
.uc-note-danger { color: var(--bad); }
.uc-menu {
  position: absolute; right: 12px; top: 64px; z-index: 60; min-width: 220px;
  background: var(--bg-2); border: 1px solid var(--muted); border-radius: 10px;
  box-shadow: 0 8px 28px var(--shadow-c); overflow: hidden;
}
.uc-menu button {
  display: flex; width: 100%; align-items: center; gap: 9px; background: none; border: none;
  border-bottom: 1px solid var(--border); color: var(--text); padding: 11px 14px; font-size: 14px; cursor: pointer; border-radius: 0;
}
.uc-menu button:last-child { border-bottom: none; }
.uc-menu button:hover { background: var(--bg-3); }
.uc-dlg-backdrop {
  position: absolute; inset: -400px -60px -14px; z-index: 70; background: rgba(0,0,0,.45);
  display: flex; align-items: center; justify-content: center; border-radius: 14px;
}
.uc-dlg { background: var(--bg-2); border: 1px solid var(--border); border-radius: 12px; padding: 18px; width: 340px; max-width: 90%; }
.uc-dlg-title { font-weight: 700; font-size: 16px; }
.uc-dlg-text { color: var(--muted); font-size: 13.5px; margin: 6px 0 14px; }
.uc-dlg-row { display: flex; gap: 8px; }
.uc-dlg-row button { flex: 1; }
.uc-dlg-danger { background: #7f1d1d; border-color: #7f1d1d; color: #fff; }

/* ── Panel collapse arrow ────────────────────────────────────────────── */
#panelToggle {
  position: absolute; top: 22px; left: 356px; z-index: 600;
  background: var(--bg-2); border: 1px solid var(--border); color: var(--muted);
  width: 26px; height: 30px; border-radius: 8px; cursor: pointer; transition: left .2s ease;
  box-shadow: 0 2px 8px var(--shadow-c);
}
#panelToggle:hover { color: var(--text); background: var(--bg-3); }
#panel.collapsed ~ #panelToggle { left: 8px; }

/* ── Map + Leaflet dark theming ──────────────────────────────────────── */
#mapwrap { position: absolute; inset: 0; z-index: 0; }
#map { position: absolute; inset: 0; background: var(--map-bg); }
.tp-marker { background: none; border: none; }
.tp-dot { display: block; width: 14px; height: 14px; border-radius: 50%; border: 3px solid var(--pin-border); box-shadow: 0 0 0 1px #0006; }
.tp-marker.sel .tp-dot { box-shadow: 0 0 0 4px #1f6feb88; width: 16px; height: 16px; }
.tp-mlabel { background: var(--label-bg); color: var(--text); border: 1px solid var(--border); border-radius: 6px; padding: 1px 6px; font-size: 11px; font-weight: 600; box-shadow: none; }
.tp-mlabel::before { display: none; }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--bg-2); color: var(--text); border: 1px solid var(--border); box-shadow: 0 8px 28px var(--shadow-c); }
.leaflet-popup-content { margin: 12px 14px; font: inherit; }
.leaflet-container a.leaflet-popup-close-button { color: var(--muted); }
.leaflet-control-layers, .leaflet-bar { background: var(--bg-2) !important; border: 1px solid var(--border) !important; color: var(--text); }
.leaflet-bar a { background: var(--bg-2); color: var(--text); border-bottom-color: var(--border); }
.leaflet-bar a:hover { background: var(--bg-3); }
.leaflet-control-layers-expanded { color: var(--text); padding: 8px 10px; }
.tp-pop .tp-pop-name { font-weight: 700; font-size: 15px; margin-bottom: 6px; }
.tp-pop .tp-pop-badges { display: flex; gap: 6px; margin-bottom: 6px; }
.tp-pop .tp-pop-row { display: flex; justify-content: space-between; gap: 14px; font-size: 13px; margin: 2px 0; }
.tp-pop .tp-pop-row span:first-child { color: var(--muted); }
.tp-pop .tp-pop-row a { color: var(--link); }
.tp-pop .tp-pop-detail { display: inline-block; margin-top: 8px; color: var(--link); text-decoration: none; font-weight: 600; font-size: 13px; }

/* ── Cluster list (floats at click point) ────────────────────────────── */
#clusterListPanel {
  position: absolute; z-index: 700; width: 250px; max-height: 320px; overflow: hidden;
  background: var(--bg-2); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 10px 34px var(--shadow-c);
  display: flex; flex-direction: column;
}
#clusterListPanel .cl-head { display: flex; align-items: center; justify-content: space-between; padding: 9px 12px; border-bottom: 1px solid var(--border); font-weight: 600; font-size: 13px; }
#clusterListPanel .close-btn { cursor: pointer; color: var(--muted); font-size: 18px; line-height: 1; }
#clusterListPanel .close-btn:hover { color: var(--text); }
#clusterListPanel .cl-search { padding: 8px 10px; border-bottom: 1px solid var(--border); }
#clusterListPanel .cl-search input { padding: 6px 8px; font-size: 12px; }
#clusterListBody { overflow-y: auto; }
.cl-item { display: flex; align-items: center; gap: 8px; padding: 8px 12px; cursor: pointer; }
.cl-item:hover { background: var(--bg-3); }
.cl-item .dot { width: 9px; height: 9px; border-radius: 50%; flex: 0 0 auto; background: var(--stopped); }
.cl-item .dot.moving { background: var(--moving); } .cl-item .dot.idle { background: var(--idle); }
.cl-item .cl-name { flex: 1; min-width: 0; display: flex; align-items: baseline; gap: 8px; }
.cl-item .cl-sub { color: var(--muted); font-size: 11px; white-space: nowrap; }

/* alert severity dot (alerts list) */
.sev-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; background: var(--muted); }
.sev-dot.sev-critical { background: var(--bad); } .sev-dot.sev-warning { background: var(--warn); } .sev-dot.sev-info { background: var(--primary); }

/* ── Iteration 2: cluster colour, toggle tab, basemap switcher, icons ──── */

/* Cluster bubble → blue with white number/border (restore previous look) */
.marker-cluster { background: rgba(31,111,235,.30) !important; }
.marker-cluster div {
  background: var(--primary) !important; color: #fff !important;
  border: 2px solid #fff; font-weight: 700; font-variant-numeric: tabular-nums;
}

/* Panel show/hide toggle → the previous tab at the panel's right edge */
#panelToggle {
  position: absolute; left: 340px; top: 12px; z-index: 600;
  width: 16px; height: 46px; padding: 0; line-height: 1; font-size: 12px;
  background: var(--bg-2); border: 1px solid var(--border); border-left: none;
  border-radius: 0 8px 8px 0; color: var(--muted);
  box-shadow: 2px 2px 8px rgba(0,0,0,.4); transition: left .2s ease;
  display: grid; place-items: center;
}
#panelToggle:hover { color: var(--text); background: var(--bg-3); }
#panel.collapsed ~ #panelToggle { left: 0; }

/* Header basemap switcher — inline, hover to drop down (next to Fullscreen) */
.basemap-ctrl { position: relative; display: inline-flex; align-self: stretch; }
.basemap-menu {
  display: none; position: absolute; top: calc(100% + 6px); right: 0; min-width: 132px;
  background: var(--bg-2); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 8px 28px var(--shadow-c); overflow: hidden; z-index: 1000; padding: 4px;
}
.basemap-ctrl.open .basemap-menu { display: block; }
.bm-opt { display: flex; align-items: center; gap: 9px; width: 100%; text-align: left; background: none; border: none; color: var(--text); padding: 9px 11px; border-radius: 7px; cursor: pointer; font-size: 13px; font-weight: 500; }
.bm-opt:hover { background: var(--bg-3); }
.bm-opt.active { color: #fff; background: var(--primary); font-weight: 600; }

/* Emoji icon marker (tinted by status) */
.tp-pin {
  display: flex; align-items: center; justify-content: center;
  width: 26px; height: 26px; border-radius: 50%; border: 2px solid var(--pin-border);
  box-shadow: 0 1px 4px rgba(0,0,0,.55); font-size: 15px; line-height: 1;
}
.tp-marker.tp-ico.sel .tp-pin { box-shadow: 0 0 0 4px #1f6feb88; }

/* Icon picker (add/edit device forms) */
.icon-picker { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.icon-opt {
  width: 34px; height: 34px; display: grid; place-items: center; font-size: 18px;
  border: 1px solid var(--border); border-radius: 8px; cursor: pointer; background: var(--bg-3); user-select: none;
}
.icon-opt:hover { border-color: var(--primary); }
.icon-opt.sel { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(31,111,235,.35); background: var(--sel-bg); }

/* ── Iteration 3: overlays become full PAGES (own page, not a card over map) ── */
#overlay {
  position: fixed; top: 48px; left: 0; right: 0; bottom: 0;
  background: var(--bg); display: block; overflow-y: auto; z-index: 900;
  place-items: initial;
}
#overlayCard {
  /* Owner: side gaps 50% smaller than the old 1100px cap - the content
   * grows toward each edge by half the old margin (calc keeps that true at
   * every viewport; small screens stay full-width). */
  width: 100%; max-width: calc(550px + 50vw); min-height: 100%; margin: 0 auto;
  background: transparent; border: none; border-radius: 0;
  padding: 22px 26px; max-height: none;
}
#overlayCard.narrow { width: 100%; max-width: 460px; }
#overlayCard h2 { font-size: 20px; }
#overlayCard .close { font-size: 16px; padding: 4px 12px; }

/* Popup: comprehensive detail rows + "zoom to unit" */
.tp-pop { min-width: 210px; }
.tp-pop .tp-pop-detail { cursor: pointer; }

/* Fix: basemap dropdown must stay open while the cursor crosses the gap from
   the button to the menu (a transparent bridge keeps :hover alive). */
.basemap-ctrl::after { content: ''; position: absolute; top: 100%; left: -10px; right: -10px; height: 12px; }
.basemap-menu { top: calc(100% + 8px); }
.basemap-ctrl:hover .basemap-menu, .basemap-menu:hover { display: block; }

/* ── Iteration 3: icon markers (typed/colour/image) + rich icon picker ── */
.tp-pin2 {
  position: relative; display: flex; align-items: center; justify-content: center;
  width: 28px; height: 28px; border-radius: 50%; border: 2px solid var(--pin-border);
  box-shadow: 0 1px 4px rgba(0,0,0,.55); font-size: 15px; line-height: 1;
}
.tp-pin2 .tp-g { pointer-events: none; }
.tp-imgpin { background: #fff; padding: 0; overflow: hidden; }
.tp-imgpin img { width: 100%; height: 100%; object-fit: cover; border-radius: 50%; display: block; }
.tp-marker.tp-ico.sel .tp-pin2 { box-shadow: 0 0 0 4px rgba(31,111,235,.55); }
.tp-pin2 svg { display: block; }

/* Icon picker */
.icon-picker { display: flex; flex-direction: column; gap: 8px; margin-top: 4px; }
.ipk-preview { display: flex; align-items: center; gap: 8px; min-height: 30px; font-size: 12px; }
.ipk-grid { display: flex; flex-wrap: wrap; gap: 6px; }
.ipk-tile {
  position: relative; width: 38px; height: 38px; display: grid; place-items: center;
  border: 1px solid var(--border); border-radius: 8px; background: var(--bg-3); cursor: pointer; font-size: 19px;
}
.ipk-tile:hover { border-color: var(--primary); }
.ipk-pop {
  display: none; position: absolute; bottom: calc(100% + 6px); left: 50%; transform: translateX(-50%); z-index: 10;
  background: var(--bg-2); border: 1px solid var(--border); border-radius: 10px; padding: 8px;
  box-shadow: 0 8px 24px var(--shadow-c); grid-template-columns: repeat(4, 22px); gap: 7px;
}
.ipk-pop::before { content: ''; position: absolute; top: 100%; left: 0; right: 0; height: 8px; }
.ipk-tile:hover .ipk-pop { display: grid; }
.ipk-c { width: 22px; height: 22px; border-radius: 50%; cursor: pointer; border: 2px solid transparent; }
.ipk-c:hover { border-color: #fff; }
.ipk-cust { display: grid; place-items: center; width: 22px; height: 22px; cursor: pointer; position: relative; font-size: 14px; }
.ipk-cust input[type=color] { position: absolute; inset: 0; opacity: 0; cursor: pointer; padding: 0; border: 0; }
.ipk-upload, .ipk-none { font-size: 15px; }

/* ── Iteration 4: SVG icon picker + Follow-unit toggle ─────────────────── */
.ipk-label { font-size: 12px; color: var(--muted); display: block; margin-bottom: 2px; }
.ipk-tile svg { pointer-events: none; }
.ipk-tile { width: 46px; height: 40px; }
.ipk-tile .ipk-glyph { display: grid; place-items: center; width: 100%; height: 100%; }
.ipk-pop { grid-template-columns: repeat(4, 22px); }
/* Follow-unit toggle (in the detail actions row) */
.pd-actions button.on { background: var(--primary); border-color: var(--primary); color: #fff; font-weight: 600; }

/* ── Iteration 5: emoji tiles restored + Follow-unit full-width row ────── */
.ipk-tile { width: 38px; height: 38px; font-size: 19px; }
/* Follow unit: its own full-width row, sitting directly ABOVE History/Trips */
.pd-follow { margin-top: auto; border-top: 1px solid var(--border); padding: 12px 12px 0; }
.pd-follow button { width: 100%; }
.pd-follow button.on { background: var(--primary); border-color: var(--primary); color: #fff; font-weight: 600; }
.pd-actions { margin-top: 0; border-top: none; }

/* ── Interactive count dropdowns (Manage) - ONE component, used everywhere a
   count opens a searchable list (units / groups / users / rules). ────────── */
.ddBtn {
  background: none; border: none; color: var(--link); cursor: pointer;
  font: inherit; font-weight: 700; font-variant-numeric: tabular-nums;
  padding: 2px 8px; border-radius: 6px;
}
.ddBtn:hover { background: var(--bg-3); text-decoration: underline; }
.ddPanel {
  position: fixed; z-index: 2600; width: 470px; max-width: 92vw;
  background: var(--bg-2); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 12px 34px var(--shadow-c);
  display: flex; flex-direction: column; overflow: hidden;
}
.ddPanel .dd-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 9px 12px; border-bottom: 1px solid var(--border); font-weight: 600; font-size: 13px;
}
.ddPanel .dd-search { padding: 8px 10px; border-bottom: 1px solid var(--border); }
.ddPanel .dd-search input { padding: 7px 9px; font-size: 13px; }
.ddPanel .dd-body { overflow-y: auto; max-height: 340px; }
.ddRow {
  display: flex; align-items: center; gap: 9px; padding: 8px 12px;
  border-bottom: 1px solid var(--border); font-size: 13px;
}
.ddRow:last-child { border-bottom: 0; }
.ddRow.click { cursor: pointer; }
.ddRow.click:hover { background: var(--bg-3); }
.ddRow .dd-ico { width: 20px; text-align: center; flex-shrink: 0; }
.ddRow .dd-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.ddRow .dd-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ddRow .dd-sub { font-size: 11.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
/* Edit | Show on map: well separated so nobody fat-fingers the wrong one */
.ddRow .dd-act { display: flex; gap: 14px; flex-shrink: 0; }
.ddRow .dd-act button { padding: 4px 10px; font-size: 12px; }

/* Row ⋮ menu: every table row's secondary actions live here, keeping rows
   clean (one visible action max) and destructive choices one level deep. */
.rmBtn { padding: 2px 9px; font-size: 15px; line-height: 1.2; }
.ddMenu {
  position: fixed; z-index: 2600; min-width: 150px;
  background: var(--bg-2); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 10px 28px var(--shadow-c); overflow: hidden; padding: 4px;
}
.ddMenu button {
  display: block; width: 100%; text-align: left; background: none; border: none;
  border-radius: 7px; padding: 8px 11px; font-size: 13px; color: var(--text); cursor: pointer;
}
.ddMenu button:hover { background: var(--bg-3); }
.ddMenu button.danger { color: var(--bad-fg); background: none; }
.ddMenu button.danger:hover { background: var(--bad-bg); }

/* Entity names are LINKS - clicking opens the thing (no "Associations" verb) */
.nameBtn {
  background: none; border: none; padding: 0; font: inherit; font-weight: 600;
  color: var(--text); cursor: pointer; text-align: left;
}
.nameBtn:hover { color: var(--link); text-decoration: underline; }

/* Count buttons: visibly clickable at rest, not only on hover */
.ddBtn { text-decoration: underline; text-decoration-style: dotted; text-underline-offset: 3px; }
.ddBtn:hover { text-decoration-style: solid; }
.ddBtn .ok-n { color: var(--ok-fg); font-weight: 700; }
.ddBtn .bad-n { color: var(--bad-fg); font-weight: 700; }

/* Sortable column headers */
th.sortable { cursor: pointer; user-select: none; white-space: nowrap; }
th.sortable:hover { color: var(--text); }
th.sortable .arrow { font-size: 10px; margin-left: 3px; }

/* Pager under paginated lists */
.pager { display: flex; align-items: center; gap: 10px; margin-top: 10px; font-size: 13px; color: var(--muted); }
.pager button { padding: 4px 12px; }

/* Summary pills on an entity page (counts as chips - premium at-a-glance) */
.pillrow { display: flex; gap: 8px; flex-wrap: wrap; margin: 2px 0 10px; }
.pill {
  display: inline-flex; align-items: center; gap: 6px; padding: 5px 12px;
  border: 1px solid var(--border); border-radius: 999px; background: var(--bg-2);
  font-size: 12.5px; font-weight: 600; color: var(--text); cursor: pointer;
}
.pill:hover { border-color: var(--primary); }
.pill b { font-variant-numeric: tabular-nums; }
