:root {
  --bg: #eceff4;
  --ink: #0e1116;
  --muted: #5b6270;
  --faint: #8a91a0;
  --rule: #dfe3ea;
  --card: #ffffff;
  --grey: #eef0f4;
  --blue: #2b63e3;
  --run: #1f9d55;
  --idle: #d98a00;
  --stop: #c23a2b;
  --off: #8a91a0;
  --shadow: 0 6px 24px rgba(14, 17, 22, 0.14), 0 1px 3px rgba(14, 17, 22, 0.08);
  --radius: 22px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b0d11; --ink: #f2f4f7; --muted: #9aa2b1; --faint: #6b7280; --rule: #262b33;
    --card: #15181e; --grey: #1d2129; --blue: #3a70ef;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.5); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b0d11; --ink: #f2f4f7; --muted: #9aa2b1; --faint: #6b7280; --rule: #262b33;
  --card: #15181e; --grey: #1d2129; --blue: #3a70ef;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.5); color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: Manrope, -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  font-feature-settings: "tnum" 1, "lnum" 1;
  -webkit-font-smoothing: antialiased;
  line-height: 1.4;
}
button { font: inherit; color: inherit; }

#map { position: fixed; inset: 0; z-index: 0; background: var(--grey); }
.leaflet-container { font-family: inherit; }
.leaflet-control-zoom { display: none; }
.leaflet-tooltip { font-family: inherit; font-weight: 700; border-radius: 10px; border: 0; box-shadow: var(--shadow); }

/* ---------- Top: search bar + chips ---------- */
.top {
  position: fixed;
  z-index: 10;
  top: calc(env(safe-area-inset-top) + 12px);
  left: 12px;
  right: 12px;
  display: grid;
  gap: 8px;
  pointer-events: none;
}
.top > * { pointer-events: auto; }
.bar {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--card);
  border-radius: 999px;
  padding: 6px 6px 6px 18px;
  box-shadow: var(--shadow);
}
.brand { font-weight: 700; letter-spacing: -0.02em; font-size: 16px; white-space: nowrap; }
.search {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 15px;
  color: var(--ink);
  background: var(--grey);
  border: 0;
  border-radius: 999px;
  padding: 10px 14px;
  outline: none;
}
.pill { font-size: 12px; font-weight: 700; border-radius: 999px; padding: 8px 12px; background: var(--ink); color: var(--bg); white-space: nowrap; }
.pill.live { background: var(--run); color: #fff; }
.pill.sample { background: var(--idle); color: #fff; }
.sample {
  justify-self: start;
  background: var(--idle);
  color: #fff;
  font-weight: 700;
  font-size: 13px;
  border-radius: 999px;
  padding: 7px 14px;
  box-shadow: var(--shadow);
}
.chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chips button {
  flex: none;
  border: 0;
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 700;
  background: var(--card);
  color: var(--ink);
  box-shadow: var(--shadow);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.chips button[aria-selected="true"] { background: var(--ink); color: var(--bg); }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex: none; }

.locate {
  position: fixed;
  z-index: 10;
  right: 12px;
  bottom: calc(40vh + 14px);
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: 0;
  background: var(--card);
  box-shadow: var(--shadow);
  display: grid;
  place-items: center;
  cursor: pointer;
}
.locate svg { width: 20px; height: 20px; fill: none; stroke: var(--ink); stroke-width: 2.2; stroke-linecap: round; }

/* ---------- Sheets ---------- */
.sheet {
  position: fixed;
  z-index: 20;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--card);
  border-radius: 24px 24px 0 0;
  box-shadow: 0 -6px 30px rgba(14, 17, 22, 0.18);
  display: flex;
  flex-direction: column;
  padding: 0 18px calc(env(safe-area-inset-bottom) + 12px);
}
.grab { width: 40px; height: 5px; border-radius: 3px; background: var(--rule); margin: 10px auto 8px; flex: none; cursor: grab; touch-action: none; }
.list-sheet { height: 40vh; }
.detail-sheet {
  max-height: 64vh;
  transform: translateY(105%);
  transition: transform 0.32s cubic-bezier(0.2, 0.9, 0.25, 1);
  overflow: hidden;
}
.detail-sheet.open { transform: translateY(0); }
#detail-body { overflow-y: auto; overscroll-behavior: contain; padding-bottom: 8px; }
@media (prefers-reduced-motion: reduce) { .detail-sheet { transition: none; } }

@media (min-width: 900px) {
  .top { right: auto; width: 420px; }
  .sheet {
    left: 12px;
    right: auto;
    width: 420px;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
  }
  .list-sheet { top: 150px; bottom: 12px; height: auto; }
  .detail-sheet { top: 150px; bottom: 12px; max-height: none; transform: translateX(-110%); }
  .detail-sheet.open { transform: translateX(0); }
  .grab { display: none; }
  .sheet { padding-top: 16px; }
  .locate { bottom: 24px; right: 24px; }
}

.sheet-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding-bottom: 10px; }
.sheet-head h2 { margin: 0; font-size: 24px; font-weight: 600; letter-spacing: -0.035em; }
.aside { color: var(--muted); font-size: 13.5px; margin: 0; }
.stats { display: flex; gap: 14px; }
.stat { text-align: right; }
.stat b { display: block; font-size: 20px; letter-spacing: -0.03em; }
.stat span { font-size: 12px; color: var(--muted); }

/* ---------- Truck rows ---------- */
.rows { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; overscroll-behavior: contain; }
.row {
  display: grid;
  grid-template-columns: 44px 1fr auto;
  gap: 2px 12px;
  align-items: center;
  padding: 11px 6px;
  border-bottom: 1px solid var(--rule);
  cursor: pointer;
  border-radius: 12px;
}
.row:hover, .row.sel { background: var(--grey); }
.row .av { grid-row: 1 / span 2; width: 44px; height: 44px; border-radius: 14px; background: var(--grey); display: grid; place-items: center; }
.row .av svg { width: 30px; height: 30px; transform: rotate(-90deg); }
.row .no { font-weight: 700; font-size: 15.5px; letter-spacing: -0.01em; }
.row .sub { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .right { grid-row: 1 / span 2; grid-column: 3; text-align: right; }
.row .spd { font-weight: 700; font-size: 17px; letter-spacing: -0.03em; }
.row .spd small { font-size: 11px; color: var(--muted); font-weight: 600; margin-left: 2px; }
.fuel-mini { display: flex; align-items: center; gap: 5px; justify-content: flex-end; font-size: 12px; color: var(--muted); font-weight: 600; margin-top: 3px; }
.fuel-mini i { width: 34px; height: 5px; border-radius: 3px; background: var(--rule); overflow: hidden; display: block; }
.fuel-mini i b { display: block; height: 100%; background: var(--run); border-radius: 3px; }
.fuel-mini i b.low { background: var(--stop); }
.empty { padding: 22px 6px; color: var(--muted); }

/* ---------- Truck marker (top-down, rotates with heading) ---------- */
.tm { position: relative; width: 44px; height: 44px; }
.tm .body { position: absolute; inset: 0; display: grid; place-items: center; transition: transform 0.6s ease; }
.tm svg { width: 40px; height: 40px; filter: drop-shadow(0 2px 3px rgba(14, 17, 22, 0.4)); }
.tm .ring { position: absolute; inset: -6px; border-radius: 50%; border: 3px solid var(--blue); background: rgba(43, 99, 227, 0.12); }
@media (prefers-reduced-motion: no-preference) {
  .tm .ring { animation: pulse 1.6s ease-out infinite; }
}
@keyframes pulse { 0% { transform: scale(0.8); opacity: 1; } 100% { transform: scale(1.35); opacity: 0; } }
.leaflet-marker-icon { transition: transform 1.2s linear; }
.leaflet-zoom-anim .leaflet-marker-icon { transition: none; }

/* ---------- Detail ---------- */
.d-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; padding: 4px 0 12px; }
.d-head h2 { margin: 0; font-size: 28px; font-weight: 600; letter-spacing: -0.04em; }
.icon-btn { border: 0; background: var(--grey); width: 36px; height: 36px; border-radius: 50%; cursor: pointer; font-size: 15px; }
.tag { font-size: 12px; font-weight: 700; border-radius: 999px; padding: 6px 11px; color: #fff; white-space: nowrap; }
.d-actions { display: flex; gap: 8px; align-items: center; }
.d-tiles { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.tile { border-radius: 18px; padding: 14px 16px; background: var(--grey); min-height: 98px; display: flex; flex-direction: column; justify-content: space-between; }
.tile .label { font-size: 13px; color: var(--muted); }
.tile .num { font-size: 32px; font-weight: 600; letter-spacing: -0.04em; line-height: 1; margin-top: 10px; }
.tile .num small { font-size: 13px; letter-spacing: 0; margin-left: 3px; font-weight: 600; }
.tile.blue { background: var(--blue); color: #fff; }
.tile.blue .label { color: rgba(255, 255, 255, 0.75); }
.gauge { height: 8px; border-radius: 4px; background: var(--rule); margin-top: 10px; overflow: hidden; }
.gauge b { display: block; height: 100%; background: var(--run); border-radius: 4px; }
.gauge b.low { background: var(--stop); }
.flags { display: flex; flex-wrap: wrap; gap: 6px; margin: 14px 0 4px; }
.flag { font-size: 12.5px; font-weight: 700; padding: 6px 11px; border-radius: 999px; background: var(--grey); color: var(--muted); }
.flag.on { background: var(--run); color: #fff; }
.flag.alert { background: var(--stop); color: #fff; }
.where { display: flex; gap: 10px; align-items: flex-start; padding: 12px 0; border-bottom: 1px solid var(--rule); font-weight: 600; font-size: 14.5px; }
.where .pin { width: 10px; height: 10px; border-radius: 50%; background: var(--ink); margin-top: 6px; flex: none; }
.d-sec { margin: 18px 0 2px; font-size: 13px; color: var(--muted); font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; }
.kv { margin: 0; display: grid; grid-template-columns: 1fr; }
.kv div { display: flex; justify-content: space-between; gap: 12px; padding: 9px 0; border-bottom: 1px solid var(--rule); font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-weight: 600; text-align: right; overflow-wrap: anywhere; }
.kv dd.na { color: var(--faint); font-weight: 500; }
details { margin-top: 16px; }
details summary { cursor: pointer; color: var(--muted); font-weight: 700; font-size: 13.5px; }
.raw { font-size: 12px; background: var(--grey); border-radius: 14px; padding: 12px; overflow: auto; max-height: 240px; margin: 8px 0 0; }

.s-RUNNING { background: var(--run); } .s-IDLE { background: var(--idle); }
.s-STOP { background: var(--stop); } .s-INACTIVE, .s-OTHER { background: var(--off); }
