:root {
  --bg: #ffffff; --bg-soft: #f4f4f2; --line: #e2e2df;
  --text: #1b1b1b; --muted: #6b6b6b; --link: #1d4ed8;
  --police: #1d4ed8; --fire: #b91c1c; --ems: #047857; --traffic: #b45309; --other: #6d28d9;
  --flash: #fff7d6; --live: #16a34a; --chip: #ececea; --chip-on: #1b1b1b; --chip-on-text: #fff;
  --tabs-h: 58px;
  color-scheme: light;
}
body.dark {
  --bg: #15171b; --bg-soft: #1d2025; --line: #2b2f36;
  --text: #e4e4e0; --muted: #97978f; --link: #6ea8fe;
  --police: #6ea8fe; --fire: #f47c7c; --ems: #43c393; --traffic: #e3aa4a; --other: #b39ddb;
  --flash: #3b3a26; --live: #3fbf6f; --chip: #262a31; --chip-on: #e4e4e0; --chip-on-text: #15171b;
  color-scheme: dark;
}

* { box-sizing: border-box; margin: 0; }
html, body { height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column;
  height: 100dvh; overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }

/* ------------------------------------------------------------- header --- */
header {
  flex: 0 0 auto;
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
}
.brand strong { font-size: 17px; }
.brand .sub { color: var(--muted); margin-left: 8px; font-size: 14px; }
.hdr-right { display: flex; align-items: center; gap: 14px; font-size: 13px; color: var(--muted); }
#live-indicator .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: #bbb; margin-right: 6px; }
#live-indicator.on { color: var(--live); } #live-indicator.on .dot { background: var(--live); }
#live-indicator.err { color: var(--fire); } #live-indicator.err .dot { background: var(--fire); }
#theme-toggle { padding: 6px 4px; color: var(--muted); }

/* --------------------------------------------------------------- views --- */
main { flex: 1 1 auto; min-height: 0; display: flex; }
.view { display: none; flex: 1 1 auto; min-width: 0; min-height: 0; flex-direction: column; }
.view.active { display: flex; }

/* ------------------------------------------------------------- filters --- */
.filters {
  flex: 0 0 auto; display: flex; gap: 6px; padding: 10px 12px;
  overflow-x: auto; scrollbar-width: none; border-bottom: 1px solid var(--line);
}
.filters::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto; padding: 6px 12px; border-radius: 999px;
  background: var(--chip); font-size: 13.5px; font-weight: 500; min-height: 36px;
}
.chip.active { background: var(--chip-on); color: var(--chip-on-text); }
.chip-closed { margin-left: auto; }

/* ---------------------------------------------------------------- feed --- */
#feed { flex: 1 1 auto; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.empty { color: var(--muted); padding: 48px 32px; text-align: center; }
#statusbar {
  flex: 0 0 auto; font-size: 12px; color: var(--muted); padding: 6px 16px;
  border-top: 1px solid var(--line); white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
#statusbar .warn { color: var(--fire); }

.card { padding: 14px 16px; border-bottom: 1px solid var(--line); }
.card.flash { animation: flash 3s; }
@keyframes flash { 0% { background: var(--flash); } 100% { background: transparent; } }
.card.closed { opacity: .55; }
.card-head { cursor: pointer; }
.card-head:active { opacity: .7; }
.cat { font-size: 11px; font-weight: 700; letter-spacing: .6px; color: var(--other); }
.card.police .cat { color: var(--police); } .card.fire .cat { color: var(--fire); }
.card.ems .cat { color: var(--ems); } .card.traffic .cat { color: var(--traffic); }
.card-top { display: flex; justify-content: space-between; gap: 12px; align-items: baseline; }
.card-title { font-size: 16px; font-weight: 600; }
.card-time { font-size: 12px; color: var(--muted); white-space: nowrap; }
.card-addr { color: var(--muted); font-size: 14px; margin-top: 2px; }
.units-line { font-size: 14px; margin-top: 6px; }
.units-line .lbl { color: var(--muted); }
.u-st { color: var(--muted); }
.st-enroute { color: var(--police); } .st-onscene { color: var(--ems); }
.st-cleared { color: var(--muted); } .st-dispatched { color: var(--traffic); }
.last-upd { font-size: 14px; margin-top: 6px; }
.last-upd time { color: var(--muted); font-size: 12px; margin-right: 8px; font-variant-numeric: tabular-nums; }
.card-meta { display: flex; gap: 14px; margin-top: 8px; font-size: 13px; color: var(--link); }

.card-body { display: none; margin-top: 10px; }
.card.open .card-body { display: block; }
.section-lbl { font-size: 11px; font-weight: 700; letter-spacing: .6px; color: var(--muted); margin: 10px 0 4px; }
.upd { display: flex; gap: 10px; font-size: 14px; padding: 4px 0; }
.upd time { color: var(--muted); font-size: 12px; flex: 0 0 auto; padding-top: 2px; font-variant-numeric: tabular-nums; }

.clip { display: flex; gap: 12px; align-items: center; padding: 8px 0; border-top: 1px solid var(--line); }
.clip:first-of-type { border-top: 0; }
.clip .play {
  flex: 0 0 auto; width: 40px; height: 40px; border-radius: 50%;
  background: var(--chip); display: flex; align-items: center; justify-content: center;
}
.clip.playing .play { background: var(--chip-on); color: var(--chip-on-text); }
.clip .play svg { width: 16px; height: 16px; fill: currentColor; }
.clip .body { flex: 1 1 auto; min-width: 0; }
.clip .ct { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.clip .tr { font-size: 13.5px; margin-top: 2px; }
.clip .bar { height: 3px; background: var(--line); margin-top: 6px; border-radius: 2px; overflow: hidden; display: none; }
.clip.playing .bar { display: block; }
.clip .bar i { display: block; height: 100%; width: 0; background: var(--link); }
.card-actions { margin-top: 10px; }
.btn {
  display: inline-block; padding: 10px 16px; border-radius: 8px; min-height: 40px;
  background: var(--chip-on); color: var(--chip-on-text); font-weight: 600; font-size: 14px;
}
.btn-ghost { background: var(--chip); color: var(--text); }

/* ----------------------------------------------------------------- map --- */
#view-map { position: relative; }
#map { flex: 1 1 auto; background: var(--bg-soft); }
.leaflet-container { font: inherit; }
.dark-tiles { filter: invert(1) hue-rotate(180deg) brightness(.82) contrast(.92) saturate(.6); }
body.dark .leaflet-control-layers, body.dark .leaflet-control-zoom a, body.dark .leaflet-control-attribution {
  background: var(--bg-soft); color: var(--text); border-color: var(--line);
}
body.dark .leaflet-control-layers-toggle { filter: invert(.85); }
body.dark .leaflet-control-attribution a { color: var(--muted); }
.leaflet-control-zoom a { width: 40px; height: 40px; line-height: 40px; font-size: 20px; }

#sheet {
  position: absolute; left: 10px; right: 10px; bottom: 10px; z-index: 1000;
  background: var(--bg); border: 1px solid var(--line); border-radius: 14px;
  padding: 14px 16px; box-shadow: 0 8px 30px rgba(0,0,0,.35);
}
.sheet-cat { font-size: 11px; font-weight: 700; letter-spacing: .6px; color: var(--muted); }
.sheet-title { font-size: 17px; font-weight: 600; margin-top: 2px; }
.sheet-sub { font-size: 14px; color: var(--muted); margin-top: 2px; }
.sheet-actions { display: flex; gap: 10px; margin-top: 12px; }

/* ---------------------------------------------------------------- tabs --- */
#tabs {
  flex: 0 0 auto; display: flex;
  border-top: 1px solid var(--line); background: var(--bg);
  padding-bottom: env(safe-area-inset-bottom);
}
.tab {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 0 6px; font-size: 12px; color: var(--muted); position: relative; min-height: var(--tabs-h);
}
.tab svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.tab.active { color: var(--text); }
.tab .badge {
  position: absolute; top: 6px; left: calc(50% + 8px);
  background: var(--fire); color: #fff; font-size: 10px; font-weight: 700;
  min-width: 17px; height: 17px; border-radius: 9px; padding: 0 5px; line-height: 17px; text-align: center;
}
.tab .badge:empty { display: none; }

/* ------------------------------------------------------------- desktop --- */
@media (min-width: 900px) {
  #tabs { display: none; }
  .view { display: flex; }
  #view-feed { flex: 0 0 480px; border-right: 1px solid var(--line); }
  #view-map { flex: 1 1 auto; }
  #sheet { left: auto; right: 20px; bottom: 20px; width: 340px; }
}
