:root {
  --bg: #f1f3f6;
  --panel: #ffffff;
  --card: #f7f8fa;
  --border: #dde1e6;
  --text: #1d2229;
  --muted: #6b7380;
  --accent: #1f6feb;
  --accent-soft: #e4eefc;
  --accent-text: #ffffff;
  --danger: #c9302c;
  --ok: #1a9e4b;
  --warn: #d98c00;
  --off: #9aa3ad;
  --shadow: 0 6px 24px rgba(0, 0, 0, .18);
  --panel-w: 360px;
  --drawer-w: 400px;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --panel: #161a20;
    --card: #1e232b;
    --border: #2b323c;
    --text: #e6e9ee;
    --muted: #8f99a6;
    --accent: #4c8dff;
    --accent-soft: #1c2c47;
    --off: #5b6572;
    --shadow: 0 6px 24px rgba(0, 0, 0, .55);
  }
  .leaflet-layer, .leaflet-control-zoom, .leaflet-control-layers, .leaflet-control-attribution, .leaflet-bar {
    filter: brightness(.85) contrast(1.05);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
}
h1 { font-size: 16px; margin: 0; letter-spacing: .02em; }
h2 { font-size: 15px; margin: 0; flex: 1; }
h3 { font-size: 12px; margin: 0 0 8px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); }

/* ---------------------------------------------------------------- top bar */
#topbar {
  display: flex; align-items: center; gap: 10px;
  padding: calc(6px + var(--sat)) 10px 6px 14px;
  background: var(--panel); border-bottom: 1px solid var(--border);
  min-height: 48px; z-index: 5;
}
.badges { display: flex; gap: 6px; flex: 1; overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
.badges::-webkit-scrollbar { display: none; }
.badge {
  display: inline-flex; align-items: center; gap: 7px; flex: 0 0 auto;
  padding: 6px 11px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--card); color: var(--text); font: inherit; font-size: 13px; cursor: pointer;
  min-height: 36px; white-space: nowrap;
}
.badge.active { background: var(--accent-soft); border-color: var(--accent); }
.badge .name { font-weight: 600; }
.badge .ago { color: var(--muted); font-size: 12px; }
.badge.inactive .name { text-decoration: line-through; color: var(--muted); }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--off); flex: 0 0 auto; }
.dot.online { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 25%, transparent); }
.dot.stale { background: var(--warn); }
.dot.offline { background: var(--danger); }

button { font: inherit; color: inherit; }
button.icon {
  width: 40px; height: 40px; border: 0; border-radius: 10px; background: transparent;
  font-size: 20px; cursor: pointer; color: var(--muted); flex: 0 0 auto;
}
button.icon:hover { background: var(--card); color: var(--text); }

/* ---------------------------------------------------------------- layout */
#main { flex: 1; min-height: 0; display: flex; }
#panel {
  width: var(--panel-w); flex: 0 0 var(--panel-w);
  background: var(--panel); border-right: 1px solid var(--border);
  overflow-y: auto; padding: 12px 14px; display: flex; flex-direction: column; gap: 12px;
}
#map { flex: 1; min-width: 0; min-height: 0; background: var(--bg); position: relative; z-index: 0; } /* own stacking context: Leaflet panes (z 400+) must stay under the drawer */
.handle { display: none; }

/* ---------------------------------------------------------------- quick filters */
.chips { display: flex; flex-wrap: wrap; gap: 6px; flex: 0 0 auto; }
.chips::-webkit-scrollbar { display: none; }
.chips button {
  flex: 0 0 auto; padding: 7px 12px; min-height: 36px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--card); cursor: pointer; font-size: 13px;
}
.chips button:hover { border-color: var(--muted); }
.chips button.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

/* ---------------------------------------------------------------- position card */
.card { background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; }
.card:empty { display: none; }
.pos-head { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.pos-head .ago { font-weight: 400; color: var(--muted); }
.pos-mini { display: none; margin-left: auto; font-variant-numeric: tabular-nums; }
.pos-main { display: flex; align-items: baseline; gap: 12px; margin-top: 8px; flex-wrap: wrap; }
.pos-speed { font-size: 28px; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; }
.pos-speed small { font-size: 13px; font-weight: 500; color: var(--muted); margin-left: 3px; }
.pos-course { font-size: 15px; color: var(--muted); }
.pos-coords { margin-top: 6px; font-variant-numeric: tabular-nums; }
.pos-coords a { color: inherit; text-decoration: none; border-bottom: 1px dotted var(--muted); }
.pos-meta { margin-top: 4px; color: var(--muted); font-size: 12px; }
.pos-warn { margin-top: 6px; color: var(--warn); font-size: 12px; }

/* ---------------------------------------------------------------- telemetry tiles */
.tiles { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.tiles:empty { display: none; }
.tile { background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 9px 12px; min-width: 0; }
.tile .k { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.tile .v { font-size: 18px; font-weight: 600; margin-top: 2px; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.tile .v small { font-size: 12px; font-weight: 500; color: var(--muted); margin-left: 2px; }
.tile .s { font-size: 12px; color: var(--muted); margin-top: 1px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile.on .v { color: var(--ok); }
.tile.off .v { color: var(--muted); }
.tile.bad .v { color: var(--danger); }
.bars { display: inline-flex; align-items: flex-end; gap: 2px; height: 16px; margin-right: 6px; vertical-align: -2px; }
.bars i { width: 4px; background: var(--border); border-radius: 1px; }
.bars i:nth-child(1) { height: 5px; } .bars i:nth-child(2) { height: 8px; }
.bars i:nth-child(3) { height: 11px; } .bars i:nth-child(4) { height: 14px; } .bars i:nth-child(5) { height: 16px; }
.bars i.on { background: var(--ok); }
.bars.weak i.on { background: var(--warn); }

/* ---------------------------------------------------------------- track summary */
.summary { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 13px; align-items: center; }
.summary:empty { display: none; }
.summary b { font-weight: 600; font-variant-numeric: tabular-nums; }
.summary button { margin-left: auto; padding: 5px 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--card); cursor: pointer; font-size: 12px; }

/* ---------------------------------------------------------------- drawer */
#scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 20; }
body.drawer-open #scrim { opacity: 1; pointer-events: auto; }
#drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(var(--drawer-w), 100vw);
  background: var(--panel); box-shadow: var(--shadow); z-index: 21;
  transform: translateX(105%); transition: transform .22s ease; overflow-y: auto;
  display: flex; flex-direction: column; padding-top: var(--sat);
}
body.drawer-open #drawer { transform: none; }
#drawer header {
  display: flex; align-items: center; gap: 8px; padding: 10px 10px 10px 16px;
  border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--panel); z-index: 1;
}
#drawer section { padding: 14px 16px; border-bottom: 1px solid var(--border); }
#drawer section.row, #drawer .row { display: flex; gap: 8px; }
#drawer .row button, #drawer .row form { flex: 1; }
#drawer .row form button { width: 100%; }
#drawer section .row { margin-top: 10px; }

label { display: block; margin: 8px 0; font-size: 13px; }
label.check { display: flex; align-items: center; gap: 8px; min-height: 36px; }
label.check input { margin: 0; width: 18px; height: 18px; }
input:not([type=checkbox]), select {
  width: 100%; margin-top: 4px; padding: 8px 10px; min-height: 40px; font: inherit; color: inherit;
  border: 1px solid var(--border); border-radius: 8px; background: var(--panel);
}
.inline-input { display: flex; align-items: center; gap: 6px; margin-top: 4px; }
.inline-input input { width: 100px; margin: 0; }
#drawer button:not(.icon) {
  padding: 9px 12px; min-height: 40px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--card); cursor: pointer;
}
#drawer button:not(.icon):hover { border-color: var(--muted); }
button.primary { background: var(--accent) !important; color: var(--accent-text) !important; border-color: var(--accent) !important; width: 100%; margin-top: 6px; }

.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 10px; font-size: 13px; }
.stats:empty { display: none; }
.stats:not(:empty) + #stats-empty { display: none; }
.stats .k { color: var(--muted); }
.stats .v { text-align: right; font-variant-numeric: tabular-nums; }
.stats .wide { grid-column: 1 / -1; }

#telemetry dl { display: grid; grid-template-columns: auto 1fr; gap: 3px 12px; margin: 0; font-size: 12px; color: var(--text); }
#telemetry dt { color: var(--muted); }
#telemetry dd { margin: 0; overflow-wrap: anywhere; font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------------- map bits */
.leaflet-popup-content { font-size: 12px; line-height: 1.35; margin: 10px 12px; }
.leaflet-popup-content dl { display: grid; grid-template-columns: auto 1fr; gap: 1px 8px; margin: 6px 0 0; }
.leaflet-popup-content dt { color: var(--muted); }
.leaflet-popup-content dd { margin: 0; }
.pos-marker { position: relative; }
.pos-marker::before, .pos-marker::after {
  content: ""; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); border-radius: 50%;
}
.pos-marker::after { width: 14px; height: 14px; background: var(--accent); border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.4); }
.pos-marker.online::before { width: 14px; height: 14px; background: var(--accent); opacity: .5; animation: pulse 2s ease-out infinite; }
@keyframes pulse { 0% { transform: translate(-50%,-50%) scale(1); opacity: .5; } 100% { transform: translate(-50%,-50%) scale(3.2); opacity: 0; } }

/* ---------------------------------------------------------------- login */
body.login-page { align-items: center; justify-content: center; }
.login-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  padding: 24px 26px; width: min(360px, 92vw);
}
.login-card h1 { margin-bottom: 12px; font-size: 18px; }
.login-card button { width: 100%; margin-top: 10px; padding: 10px; min-height: 44px; border-radius: 8px; border: 1px solid var(--border); background: var(--card); cursor: pointer; }
.login-card .error { margin: 0 0 10px; }

/* ---------------------------------------------------------------- mobile */
@media (max-width: 800px) {
  #topbar { padding-left: 12px; gap: 8px; }
  h1 { font-size: 15px; }
  #main { flex-direction: column; }
  #map { order: 1; }
  #panel {
    order: 2; width: auto; flex: 0 0 auto; max-height: 48dvh; border-right: 0; border-top: 1px solid var(--border);
    padding: 8px 12px calc(10px + var(--sab)); gap: 10px; position: relative;
    transition: max-height .2s ease;
  }
  .handle {
    display: block; position: sticky; bottom: calc(-10px - var(--sab)); margin: -4px -12px calc(-10px - var(--sab));
    padding: 8px 0 calc(8px + var(--sab)); border: 0; background: var(--panel); cursor: pointer; width: calc(100% + 24px);
  }
  .handle span { display: block; width: 44px; height: 5px; border-radius: 3px; background: var(--border); margin: 0 auto; }
  body.panel-min #panel { max-height: none; flex: 0 0 auto; }
  body.panel-min #panel > :not(.chips):not(.handle):not(#pos) { display: none; }
  body.panel-min #pos .pos-main, body.panel-min #pos .pos-coords, body.panel-min #pos .pos-meta { display: none; }
  body.panel-min #pos { padding: 8px 12px; }
  body.panel-min .pos-mini { display: inline; }
  .chips { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .tiles { grid-template-columns: repeat(3, 1fr); }
  .tile .v { font-size: 16px; }
  #drawer {
    top: auto; left: 0; right: 0; width: auto; max-height: 88dvh; border-radius: 16px 16px 0 0;
    transform: translateY(105%); padding-top: 0; padding-bottom: var(--sab);
  }
  #drawer header { border-radius: 16px 16px 0 0; }
}
@media (max-width: 380px) { .tiles { grid-template-columns: repeat(2, 1fr); } }
