/* Layout: fixed sidebar nav + main panel; screens swap via .screen[hidden]. Telemetry/ops console aesthetic. */
  :root {
    --bg: #f4f6f8;
    --panel: #ffffff;
    --panel-2: #f0f2f5;
    --edge: #dde2e8;
    --edge-soft: #e7ebf0;
    --fg: #1a2230;
    --fg-dim: #5b6676;
    --fg-faint: #8a94a3;
    --accent: #1f7a8c;
    --accent-ink: #0f5160;
    --good: #2e9e6b;
    --good-bg: #e3f5ec;
    --warn: #c8871a;
    --warn-bg: #fbf0da;
    --crit: #d1495b;
    --crit-bg: #fbe4e7;
    --sidebar: #141b26;
    --sidebar-fg: #c3ccd8;
    --sidebar-dim: #7c8798;
    --sidebar-edge: #253040;
    --mono: ui-monospace, "SF Mono", "Cascadia Code", Menlo, Consolas, monospace;
    --sans: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
    --r: 10px;
    --r-sm: 7px;
  }
  @media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
      --bg: #0e141c;
      --panel: #161e29;
      --panel-2: #1b2531;
      --edge: #273340;
      --edge-soft: #202b38;
      --fg: #e4eaf1;
      --fg-dim: #9aa6b4;
      --fg-faint: #6b7686;
      --accent: #45b6cc;
      --accent-ink: #8fd8e6;
      --good: #43c88a;
      --good-bg: #16302a;
      --warn: #e0a84a;
      --warn-bg: #30280f;
      --crit: #e76a7a;
      --crit-bg: #331a20;
      --sidebar: #0a0f16;
      --sidebar-fg: #c3ccd8;
      --sidebar-dim: #6b7686;
      --sidebar-edge: #1a232f;
      color-scheme: dark;
    }
  }
  :root[data-theme="dark"] {
    --bg: #0e141c;
    --panel: #161e29;
    --panel-2: #1b2531;
    --edge: #273340;
    --edge-soft: #202b38;
    --fg: #e4eaf1;
    --fg-dim: #9aa6b4;
    --fg-faint: #6b7686;
    --accent: #45b6cc;
    --accent-ink: #8fd8e6;
    --good: #43c88a;
    --good-bg: #16302a;
    --warn: #e0a84a;
    --warn-bg: #30280f;
    --crit: #e76a7a;
    --crit-bg: #331a20;
    --sidebar: #0a0f16;
    --sidebar-fg: #c3ccd8;
    --sidebar-dim: #6b7686;
    --sidebar-edge: #1a232f;
    color-scheme: dark;
  }

  * { box-sizing: border-box; }
  body {
    background: var(--bg);
    color: var(--fg);
    font-family: var(--sans);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
  }
  h1, h2, h3 { text-wrap: balance; margin: 0; }
  .tnum { font-variant-numeric: tabular-nums; }

  /* ---- shell ---- */
  .shell { display: flex; min-height: 100vh; }

  .sidebar {
    width: 230px;
    flex-shrink: 0;
    background: var(--sidebar);
    border-right: 1px solid var(--sidebar-edge);
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    align-self: flex-start;
    height: 100vh;
  }
  .brand {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 18px 20px 16px;
    border-bottom: 1px solid var(--sidebar-edge);
  }
  .brand-mark {
    width: 30px; height: 30px; border-radius: 8px; flex-shrink: 0;
    background: linear-gradient(145deg, var(--accent), var(--accent-ink));
    display: grid; place-items: center;
  }
  .brand-mark svg { width: 17px; height: 17px; }
  .brand-name { font-weight: 650; font-size: 15px; color: #fff; letter-spacing: -0.01em; }
  .brand-sub { font-size: 10.5px; color: var(--sidebar-dim); letter-spacing: 0.08em; text-transform: uppercase; margin-top: 1px; }

  .nav { padding: 12px 10px; display: flex; flex-direction: column; gap: 2px; flex: 1; }
  .nav-label { font-size: 10px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--sidebar-dim); padding: 12px 12px 6px; }
  .nav-item {
    display: flex; align-items: center; gap: 11px;
    padding: 9px 12px; border-radius: var(--r-sm);
    color: var(--sidebar-fg); font-size: 13.5px; font-weight: 500;
    cursor: pointer; border: none; background: none; width: 100%; text-align: left;
    font-family: var(--sans); transition: background 0.12s, color 0.12s;
  }
  .nav-item svg { width: 17px; height: 17px; flex-shrink: 0; opacity: 0.85; }
  .nav-item:hover { background: var(--sidebar-edge); color: #fff; }
  .nav-item.active { background: color-mix(in srgb, var(--accent) 22%, transparent); color: #fff; }
  .nav-item.active svg { opacity: 1; color: var(--accent-ink); }
  .nav-badge { margin-left: auto; font-size: 10.5px; font-weight: 600; padding: 1px 7px; border-radius: 20px; background: var(--crit); color: #fff; }

  .nav-foot { padding: 12px 16px; border-top: 1px solid var(--sidebar-edge); display: flex; align-items: center; gap: 10px; }
  .avatar { width: 30px; height: 30px; border-radius: 50%; background: linear-gradient(135deg,#3a4a5e,#5a6b80); display: grid; place-items: center; color: #fff; font-size: 12px; font-weight: 600; flex-shrink: 0; }
  .nav-foot .who { font-size: 12.5px; color: var(--sidebar-fg); font-weight: 550; }
  .nav-foot .role { font-size: 10.5px; color: var(--sidebar-dim); }

  /* ---- main ---- */
  .main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
  .topbar {
    display: flex; align-items: center; gap: 16px;
    padding: 0 max(24px, env(safe-area-inset-right, 0px)) 0 24px;
    height: 60px; border-bottom: 1px solid var(--edge);
    background: var(--panel); position: sticky; top: 0; z-index: 20;
  }
  .page-title { font-size: 17px; font-weight: 650; letter-spacing: -0.01em; }
  .page-crumb { font-size: 12px; color: var(--fg-faint); }
  .worker-pill {
    margin-left: auto; display: inline-flex; align-items: center; gap: 7px;
    font-size: 12px; font-weight: 550; padding: 6px 12px; border-radius: 20px;
    background: var(--good-bg); color: var(--good); border: 1px solid color-mix(in srgb, var(--good) 25%, transparent);
  }
  .worker-pill .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--good); box-shadow: 0 0 0 0 color-mix(in srgb, var(--good) 60%, transparent); animation: pulse 2.4s infinite; }
  @keyframes pulse { 0%{box-shadow:0 0 0 0 color-mix(in srgb,var(--good) 55%,transparent);} 70%{box-shadow:0 0 0 7px transparent;} 100%{box-shadow:0 0 0 0 transparent;} }
  @media (prefers-reduced-motion: reduce){ .dot{animation:none;} .worker-pill .dot{animation:none;} }
  .theme-btn { background: var(--panel-2); border: 1px solid var(--edge); color: var(--fg-dim); width: 34px; height: 34px; border-radius: var(--r-sm); cursor: pointer; display: grid; place-items: center; }
  .theme-btn:hover { color: var(--fg); border-color: var(--fg-faint); }
  .theme-btn svg { width: 16px; height: 16px; }

  .content { padding: 22px 24px 48px; max-width: 1180px; width: 100%; }

  /* ---- stat row ---- */
  .stat-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 22px; }
  .stat {
    background: var(--panel); border: 1px solid var(--edge); border-radius: var(--r);
    padding: 15px 16px; position: relative; overflow: hidden;
  }
  .stat::before { content:""; position:absolute; left:0; top:0; bottom:0; width:3px; background: var(--chip, var(--accent)); }
  .stat .k { font-size: 11.5px; color: var(--fg-dim); font-weight: 550; letter-spacing: 0.02em; display:flex; align-items:center; gap:6px; }
  .stat .v { font-size: 30px; font-weight: 680; letter-spacing: -0.02em; margin-top: 6px; line-height: 1; }
  .stat .d { font-size: 11.5px; color: var(--fg-faint); margin-top: 6px; }
  .stat.s-good { --chip: var(--good); }
  .stat.s-warn { --chip: var(--warn); }
  .stat.s-crit { --chip: var(--crit); }

  /* ---- card ---- */
  .card { background: var(--panel); border: 1px solid var(--edge); border-radius: var(--r); overflow: hidden; }
  .card-head { display: flex; align-items: center; gap: 12px; padding: 14px 18px; border-bottom: 1px solid var(--edge-soft); }
  .card-head h2 { font-size: 14.5px; font-weight: 620; }
  .card-head .sub { font-size: 12px; color: var(--fg-faint); }
  .card-head .tools { margin-left: auto; display: flex; gap: 8px; }

  .btn { font-family: var(--sans); font-size: 12.5px; font-weight: 560; padding: 7px 13px; border-radius: var(--r-sm); cursor: pointer; border: 1px solid var(--edge); background: var(--panel-2); color: var(--fg); display: inline-flex; align-items: center; gap: 7px; }
  .btn:hover { border-color: var(--fg-faint); }
  .btn svg { width: 14px; height: 14px; }
  .btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
  .btn-primary:hover { background: var(--accent-ink); border-color: var(--accent-ink); }
  .btn-ghost { background: transparent; border-color: transparent; color: var(--fg-dim); padding: 6px 9px; }
  .btn-ghost:hover { background: var(--panel-2); color: var(--fg); }

  /* ---- table ---- */
  .tbl { width: 100%; border-collapse: collapse; font-size: 13px; }
  .tbl th { text-align: left; font-size: 10.5px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--fg-faint); font-weight: 600; padding: 10px 14px; border-bottom: 1px solid var(--edge-soft); background: var(--panel-2); }
  .tbl td { padding: 11px 14px; border-bottom: 1px solid var(--edge-soft); vertical-align: middle; }
  .tbl tr:last-child td { border-bottom: none; }
  .tbl tbody tr { cursor: pointer; }
  .tbl tbody tr:hover { background: var(--panel-2); }
  .dev-name { font-weight: 600; font-size: 13px; }
  .dev-plate { font-size: 11px; color: var(--fg-faint); font-family: var(--mono); margin-top: 1px; }
  .imei-cell { font-family: var(--mono); font-size: 12px; color: var(--fg-dim); }
  .imei-map { display: inline-flex; align-items: center; gap: 6px; font-size: 11px; color: var(--fg-faint); margin-top: 2px; }
  .imei-map svg { width: 11px; height: 11px; }

  .pill { display: inline-flex; align-items: center; gap: 5px; font-size: 11.5px; font-weight: 580; padding: 3px 9px; border-radius: 20px; white-space: nowrap; }
  .pill .pdot { width: 6px; height: 6px; border-radius: 50%; }
  .pill-good { background: var(--good-bg); color: var(--good); }
  .pill-good .pdot { background: var(--good); }
  .pill-warn { background: var(--warn-bg); color: var(--warn); }
  .pill-warn .pdot { background: var(--warn); }
  .pill-crit { background: var(--crit-bg); color: var(--crit); }
  .pill-crit .pdot { background: var(--crit); }
  .pill-idle { background: var(--panel-2); color: var(--fg-faint); }
  .pill-idle .pdot { background: var(--fg-faint); }

  .ago { font-size: 12px; color: var(--fg-dim); font-variant-numeric: tabular-nums; }
  .reply-code { font-family: var(--mono); font-size: 11.5px; padding: 2px 6px; border-radius: 5px; background: var(--panel-2); color: var(--fg-dim); }
  .reply-ok { color: var(--good); background: var(--good-bg); }
  .reply-bad { color: var(--crit); background: var(--crit-bg); }

  .toolbar { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--edge-soft); flex-wrap: wrap; }
  .search { display: flex; align-items: center; gap: 8px; background: var(--panel-2); border: 1px solid var(--edge); border-radius: var(--r-sm); padding: 7px 11px; flex: 1; min-width: 180px; max-width: 320px; }
  .search svg { width: 15px; height: 15px; color: var(--fg-faint); }
  .search input { border: none; background: none; outline: none; font-size: 13px; color: var(--fg); width: 100%; font-family: var(--sans); }
  .seg { display: inline-flex; border: 1px solid var(--edge); border-radius: var(--r-sm); overflow: hidden; }
  .seg button { font-family: var(--sans); font-size: 12px; font-weight: 540; padding: 6px 12px; border: none; background: var(--panel); color: var(--fg-dim); cursor: pointer; border-right: 1px solid var(--edge); }
  .seg button:last-child { border-right: none; }
  .seg button.on { background: var(--accent); color: #fff; }

  /* ---- tracker detail ---- */
  .detail-grid { display: grid; grid-template-columns: 1.6fr 1fr; gap: 18px; align-items: start; }
  .map-card { background: var(--panel); border: 1px solid var(--edge); border-radius: var(--r); overflow: hidden; }
  .map-canvas { position: relative; height: 360px; background:
      radial-gradient(circle at 30% 40%, color-mix(in srgb, var(--accent) 8%, transparent), transparent 60%),
      var(--panel-2);
      overflow: hidden; }
  .map-canvas svg.grid { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0.5; }
  .map-roads { position: absolute; inset: 0; width: 100%; height: 100%; }
  .marker { position: absolute; transform: translate(-50%, -100%); }
  .marker-pin { filter: drop-shadow(0 4px 6px rgba(0,0,0,0.3)); }
  .marker-ping { position: absolute; left: 50%; top: 100%; transform: translate(-50%, -50%); width: 46px; height: 46px; border-radius: 50%; background: color-mix(in srgb, var(--accent) 30%, transparent); animation: ping 2.6s ease-out infinite; }
  @keyframes ping { 0%{transform:translate(-50%,-50%) scale(0.3);opacity:0.7;} 100%{transform:translate(-50%,-50%) scale(1.4);opacity:0;} }
  @media (prefers-reduced-motion: reduce){ .marker-ping{animation:none; opacity:0.35;} }
  .map-attrib { position: absolute; bottom: 5px; right: 7px; font-size: 9.5px; color: var(--fg-faint); background: color-mix(in srgb, var(--panel) 80%, transparent); padding: 1px 6px; border-radius: 4px; }
  .map-ctrl { position: absolute; top: 12px; right: 12px; display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--edge); border-radius: var(--r-sm); overflow: hidden; }
  .map-ctrl button { width: 30px; height: 30px; border: none; background: var(--panel); color: var(--fg-dim); font-size: 16px; cursor: pointer; border-bottom: 1px solid var(--edge-soft); }
  .map-ctrl button:last-child { border-bottom: none; }
  .map-ctrl button:hover { background: var(--panel-2); color: var(--fg); }
  .map-legend { position: absolute; bottom: 12px; left: 12px; display: flex; gap: 12px; background: color-mix(in srgb, var(--panel) 88%, transparent); border: 1px solid var(--edge); border-radius: var(--r-sm); padding: 6px 10px; font-size: 11px; }
  .map-legend span { display: inline-flex; align-items: center; gap: 5px; color: var(--fg-dim); }
  .map-legend i { width: 8px; height: 8px; border-radius: 50%; }

  .kv-card { background: var(--panel); border: 1px solid var(--edge); border-radius: var(--r); }
  .kv-head { padding: 14px 16px 12px; border-bottom: 1px solid var(--edge-soft); }
  .kv-head .n { font-size: 15px; font-weight: 650; }
  .kv-head .meta { font-size: 11.5px; color: var(--fg-faint); font-family: var(--mono); margin-top: 3px; }
  .kv-list { padding: 6px 16px 14px; }
  .kv { display: flex; justify-content: space-between; gap: 12px; padding: 8px 0; border-bottom: 1px dashed var(--edge-soft); font-size: 13px; }
  .kv:last-child { border-bottom: none; }
  .kv .kk { color: var(--fg-dim); display: inline-flex; align-items: center; gap: 7px; }
  .kv .kk svg { width: 14px; height: 14px; opacity: 0.7; }
  .kv .vv { font-weight: 560; font-variant-numeric: tabular-nums; text-align: right; }
  .kv .vv small { color: var(--fg-faint); font-weight: 400; }

  .flow-card { margin-top: 18px; grid-column: 1 / -1; }
  .flow { display: flex; align-items: stretch; gap: 0; padding: 20px; flex-wrap: wrap; }
  .flow-node { flex: 1; min-width: 150px; background: var(--panel-2); border: 1px solid var(--edge); border-radius: var(--r-sm); padding: 13px 15px; }
  .flow-node .fn-label { font-size: 10.5px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--fg-faint); font-weight: 600; }
  .flow-node .fn-main { font-size: 13.5px; font-weight: 600; margin-top: 4px; }
  .flow-node .fn-sub { font-size: 11.5px; color: var(--fg-dim); margin-top: 3px; font-family: var(--mono); }
  .flow-arrow { display: grid; place-items: center; padding: 0 4px; color: var(--accent); flex: 0 0 auto; }
  .flow-arrow svg { width: 22px; height: 22px; }

  /* ---- log console ---- */
  .console { font-family: var(--mono); font-size: 12px; background: var(--sidebar); color: #c3ccd8; padding: 14px 16px; max-height: 320px; overflow-y: auto; }
  .log-line { display: flex; gap: 12px; padding: 3px 0; white-space: nowrap; }
  .log-ts { color: #6b7686; flex-shrink: 0; }
  .log-lvl { flex-shrink: 0; width: 46px; font-weight: 600; }
  .lvl-ok { color: #43c88a; }
  .lvl-info { color: #45b6cc; }
  .lvl-warn { color: #e0a84a; }
  .lvl-err { color: #e76a7a; }
  .log-msg { color: #aeb8c5; white-space: pre; }

  /* ---- settings form ---- */
  .form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
  .field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 15px; }
  .field label { font-size: 12px; font-weight: 580; color: var(--fg-dim); }
  .field .hint { font-size: 11px; color: var(--fg-faint); }
  .field input, .field select { font-family: var(--sans); font-size: 13px; padding: 9px 11px; border: 1px solid var(--edge); border-radius: var(--r-sm); background: var(--panel-2); color: var(--fg); outline: none; }
  .field input:focus, .field select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent); }
  .secret-field { position: relative; display: flex; }
  .secret-field input { flex: 1; font-family: var(--mono); letter-spacing: 1px; }
  .secret-field .reveal { position: absolute; right: 6px; top: 50%; transform: translateY(-50%); background: none; border: none; color: var(--fg-faint); cursor: pointer; padding: 4px; }
  .secret-field .reveal:hover { color: var(--fg); }
  .secret-field .reveal svg { width: 16px; height: 16px; }
  .test-row { display: flex; align-items: center; gap: 10px; margin-top: 2px; }
  .masked-note { display:inline-flex; align-items:center; gap:6px; font-size: 11px; color: var(--good); }
  .masked-note svg { width: 13px; height: 13px; }

  .card-foot { padding: 14px 18px; border-top: 1px solid var(--edge-soft); display: flex; gap: 10px; align-items: center; }
  .card-foot .spacer { flex: 1; }

  .section-title { font-size: 12.5px; font-weight: 650; letter-spacing: 0.02em; color: var(--fg); margin: 0 0 2px; display:flex; align-items:center; gap:8px; }
  .section-title svg { width:15px;height:15px; color: var(--accent); }

  .back-link { display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--fg-dim); cursor: pointer; background: none; border: none; font-family: var(--sans); padding: 0; margin-bottom: 14px; }
  .back-link:hover { color: var(--accent); }
  .back-link svg { width: 15px; height: 15px; }

  .trail-dot { fill: var(--accent); opacity: 0.55; }
  .trail-line { stroke: var(--accent); stroke-width: 2.5; fill: none; opacity: 0.4; stroke-dasharray: 5 4; }

  /* mobile menu */
  .menu-btn { display: none; background: var(--panel-2); border: 1px solid var(--edge); color: var(--fg-dim); width: 34px; height: 34px; border-radius: var(--r-sm); cursor: pointer; place-items: center; }
  .menu-btn svg { width: 18px; height: 18px; }
  .scrim { display: none; }

  @media (max-width: 860px) {
    .sidebar { position: fixed; z-index: 1200; transform: translateX(-100%); transition: transform 0.2s; box-shadow: 0 0 40px rgba(0,0,0,0.3); }
    .sidebar.open { transform: translateX(0); }
    .scrim.show { display: block; position: fixed; inset: 0; background: rgba(0,0,0,0.45); z-index: 1150; }
    .menu-btn { display: grid; }
    .stat-row { grid-template-columns: repeat(2, 1fr); }
    .detail-grid { grid-template-columns: 1fr; }
    .form-grid { grid-template-columns: 1fr; }
  }
  @media (max-width: 560px) {
    .content { padding: 16px max(16px, env(safe-area-inset-right,0px)) 40px max(16px, env(safe-area-inset-left,0px)); }
    .topbar { padding-left: max(16px, env(safe-area-inset-left,0px)); padding-right: max(16px, env(safe-area-inset-right,0px)); }
    .stat-row { grid-template-columns: 1fr 1fr; }
    .hide-sm { display: none; }
    .flow-arrow { transform: rotate(90deg); }
  }

/* ---- Leaflet live map overrides (real tiles replace the mock canvas) ---- */
.leaflet-map { height: 360px; width: 100%; }
.map-card .leaflet-map { border-bottom: 1px solid var(--edge-soft); }
.dash-map { height: 300px; }
/* Establish a stacking context so Leaflet's internal panes/controls (z-index up
   to ~1000) stay contained and can't render above the mobile nav. */
.leaflet-map { position: relative; z-index: 0; isolation: isolate; }
.leaflet-container { background: var(--panel-2); font: inherit; z-index: 0; }
:root[data-theme="dark"] .leaflet-tile,
:root:not([data-theme="light"]) .leaflet-tile {
  filter: brightness(0.82) invert(0.96) hue-rotate(180deg) saturate(0.7);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .leaflet-tile { filter: brightness(0.82) invert(0.96) hue-rotate(180deg) saturate(0.7); }
}
.leaflet-popup-content { font-family: var(--sans); font-size: 12.5px; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--sidebar); color:#fff; padding: 11px 16px; border-radius: var(--r-sm); font-size: 13px; z-index: 200; box-shadow: 0 8px 24px rgba(0,0,0,.3); opacity:0; transition:opacity .2s; }
.toast.show { opacity: 1; }
.toast.ok { border-left: 3px solid var(--good); }
.toast.bad { border-left: 3px solid var(--crit); }
a { color: inherit; text-decoration: none; }
