:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f2f1ed;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #6f6d68;
  --hairline: #e1e0d9;
  --border: rgba(11, 11, 11, 0.1);
  --accent: #2a78d6;
  --good: #0ca30c;
  --critical: #d03b3b;
  --bar: #2a78d6;
  --bar-soft: #cde2fb;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 4px 16px rgba(0, 0, 0, 0.06);

  /* Incident types. The glyph on each marker carries the type too, so colour is never the only cue. */
  --t-collision: #e34948;
  --t-breakdown: #eda100;
  --t-hazard: #1baf7a;
  --t-closure: #4a3aa7;
  --t-slowdown: #2a78d6;
  --t-congestion: #eb6834;
  --t-roadworks: #898781;
  --t-other: #898781;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #242423;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #9a988f;
    --hairline: #2c2c2a;
    --border: rgba(255, 255, 255, 0.1);
    --accent: #3987e5;
    --bar: #3987e5;
    --bar-soft: #184f95;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 16px rgba(0, 0, 0, 0.4);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #242423;
  --ink: #ffffff;
  --ink-2: #c3c2b7;
  --muted: #9a988f;
  --hairline: #2c2c2a;
  --border: rgba(255, 255, 255, 0.1);
  --accent: #3987e5;
  --bar: #3987e5;
  --bar-soft: #184f95;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 16px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--page);
  color: var(--ink);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
}
button { font: inherit; color: inherit; cursor: pointer; }
h1, h2 { margin: 0; }

/* ---- Header ---- */
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--hairline);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { font-size: 17px; font-weight: 650; letter-spacing: -0.01em; }
.brand-mark {
  width: 24px; height: 24px; border-radius: 50%;
  background: var(--t-collision); color: #fff;
  display: grid; place-items: center; font-weight: 800; font-size: 14px;
}
.live { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12px; }
.live-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.live.on .live-dot { background: var(--good); box-shadow: 0 0 0 0 rgba(12, 163, 12, 0.5); animation: pulse 2s infinite; }
.live.off .live-dot { background: var(--critical); }
@keyframes pulse { 70% { box-shadow: 0 0 0 6px rgba(12, 163, 12, 0); } 100% { box-shadow: 0 0 0 0 rgba(12, 163, 12, 0); } }
@media (prefers-reduced-motion: reduce) { .live.on .live-dot { animation: none; } }

.top-stats { display: flex; gap: 18px; color: var(--ink-2); font-size: 13px; }
.top-stats b { color: var(--ink); font-weight: 650; font-size: 15px; margin-right: 4px; }

/* ---- Layout ---- */
.layout { flex: 1; display: flex; min-height: 0; }
.map { flex: 1; min-width: 0; background: var(--surface-2); }
.panel {
  width: 420px;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-left: 1px solid var(--hairline);
  min-height: 0;
}
@media (max-width: 820px) {
  body { height: auto; min-height: 100%; }
  .layout { flex-direction: column; }
  .map { height: 55vh; flex: none; }
  .panel { width: 100%; border-left: 0; border-top: 1px solid var(--hairline); }
  .top-stats { gap: 12px; }
}

/* ---- Tabs & filters ---- */
.tabs { display: flex; gap: 4px; padding: 10px 16px 0; border-bottom: 1px solid var(--hairline); }
.tabs button {
  background: none; border: 0; padding: 8px 10px 10px; color: var(--muted); font-weight: 550;
  border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--ink); }

.filters { padding: 12px 16px 4px; display: grid; gap: 10px; }
.seg { display: inline-flex; background: var(--surface-2); border-radius: 8px; padding: 2px; width: fit-content; }
.seg button { border: 0; background: none; padding: 5px 10px; border-radius: 6px; color: var(--ink-2); font-size: 13px; }
.seg button[aria-checked="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); font-weight: 550; }
.seg.small button { font-size: 12px; padding: 4px 8px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--hairline); background: var(--surface); border-radius: 999px;
  padding: 3px 10px 3px 4px; font-size: 12px; color: var(--ink-2);
}
.chip[aria-pressed="false"] { opacity: 0.5; }
.chip[aria-pressed="false"] .glyph { filter: grayscale(1); }
.chip .count { color: var(--muted); font-variant-numeric: tabular-nums; }

/* ---- Glyph (type badge, used in chips, feed and on the map) ---- */
.glyph {
  --c: var(--t-other);
  width: 20px; height: 20px; border-radius: 50%;
  background: var(--c); color: #fff;
  display: inline-grid; place-items: center; flex: none;
  font: 800 11px/1 system-ui, sans-serif;
}
.glyph.dark-ink { color: #0b0b0b; }
.glyph.hollow { background: var(--surface); color: var(--c); border: 2px dashed var(--c); }

/* ---- Feed ---- */
.tab-body { flex: 1; overflow-y: auto; padding: 4px 16px 16px; min-height: 0; }
.feed { list-style: none; margin: 0; padding: 0; }
.item {
  display: grid; grid-template-columns: 20px 1fr auto; gap: 4px 10px;
  padding: 12px 8px; margin: 0 -8px; border-radius: 8px; cursor: pointer;
  border-bottom: 1px solid var(--hairline);
}
.item:hover, .item.selected { background: var(--surface-2); }
.item.cleared { opacity: 0.55; }
.item.flash { animation: flash 2.4s ease-out; }
@keyframes flash { 0% { background: color-mix(in srgb, var(--t-collision) 22%, transparent); } 100% { background: transparent; } }
.item .glyph { margin-top: 1px; }
.item .where { font-weight: 650; }
.item .time { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.item .what, .item .facts, .item .srcs, .item details { grid-column: 2 / 4; }
.item .what { color: var(--ink); }
.item .facts { color: var(--ink-2); font-size: 13px; }
.item .facts .delay { color: var(--ink); font-weight: 600; }
.item .srcs { color: var(--muted); font-size: 12px; }
.item details { font-size: 12px; color: var(--ink-2); }
.item summary { color: var(--muted); cursor: pointer; width: fit-content; }
.item blockquote { margin: 6px 0 0; padding-left: 8px; border-left: 2px solid var(--hairline); }
.item blockquote cite { display: block; font-style: normal; color: var(--muted); }
.timeline { list-style: none; margin: 6px 0 2px 4px; padding: 0 0 0 12px; border-left: 2px solid var(--hairline); display: grid; gap: 5px; }
.timeline li { display: grid; grid-template-columns: 38px 1fr; gap: 6px; position: relative; color: var(--ink-2); }
.timeline li::before {
  content: ''; position: absolute; left: -18px; top: 4px; width: 8px; height: 8px; border-radius: 50%;
  background: var(--muted); box-shadow: 0 0 0 2px var(--surface);
}
.timeline li.tl-reported::before { background: var(--accent); }
.timeline li.tl-clear::before { background: var(--good); }
.timeline li.tl-clear { color: var(--ink); font-weight: 550; }
.timeline time { color: var(--muted); font-variant-numeric: tabular-nums; }
.empty { color: var(--muted); padding: 24px 0; text-align: center; }
.summary { color: var(--ink-2); margin: 8px 0; }

.watch { display: flex; align-items: center; gap: 8px; margin: 6px 0 4px; font-size: 12px; color: var(--muted); flex-wrap: wrap; }
.watch input {
  flex: 1; min-width: 120px; background: var(--surface-2); border: 1px solid transparent; border-radius: 6px;
  padding: 5px 8px; color: var(--ink); font: inherit; font-size: 13px;
}
.watch input:focus { outline: none; border-color: var(--accent); }
.ghost { background: none; border: 1px solid var(--hairline); border-radius: 6px; padding: 4px 8px; font-size: 12px; color: var(--ink-2); }

/* ---- Stats ---- */
#tab-stats h2 { font-size: 14px; font-weight: 650; margin-top: 22px; }
#tab-stats > .seg { margin-top: 10px; }
.sub { color: var(--muted); font-size: 12px; margin: 2px 0 8px; }
.chart-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
.tile { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; }
.tile .label { color: var(--ink-2); font-size: 12px; }
.tile .value { font-size: 24px; font-weight: 650; letter-spacing: -0.01em; }
.tile .value small { font-size: 13px; font-weight: 500; color: var(--ink-2); margin-left: 3px; }
.note { color: var(--muted); font-size: 12px; margin-top: 18px; }
.chart svg { display: block; width: 100%; overflow: visible; }
.chart text { fill: var(--muted); font-size: 11px; font-family: system-ui, sans-serif; }
.chart .label { fill: var(--ink-2); font-size: 12px; }
.chart .value { fill: var(--ink); font-size: 12px; font-variant-numeric: tabular-nums; }
.chart .grid { stroke: var(--hairline); stroke-width: 1; }
.chart .base { stroke: var(--hairline); stroke-width: 1; }
.chart .bar { fill: var(--bar); }
.chart .hit { fill: transparent; }
.chart .hit:hover + .bar, .chart g:hover .bar { fill: color-mix(in srgb, var(--bar) 80%, var(--ink)); }

.chart details { margin: 4px 0 0; }
.chart summary { cursor: pointer; width: fit-content; }
.chart table { border-collapse: collapse; margin-top: 6px; font-size: 12px; font-variant-numeric: tabular-nums; }
.chart th, .chart td { padding: 2px 12px 2px 0; border-bottom: 1px solid var(--hairline); color: var(--ink-2); }

.tip {
  position: fixed; z-index: 2000; pointer-events: none;
  background: var(--surface); color: var(--ink); border: 1px solid var(--border); box-shadow: var(--shadow);
  border-radius: 8px; padding: 6px 9px; font-size: 12px; max-width: 240px;
}
.tip b { font-weight: 650; }

/* ---- Sources ---- */
.sources { border-top: 1px solid var(--hairline); padding: 10px 16px; font-size: 12px; color: var(--ink-2); }
.sources summary { cursor: pointer; color: var(--muted); }
.sources ul { list-style: none; padding: 0; margin: 8px 0 0; display: grid; gap: 8px; }
.sources li { display: grid; grid-template-columns: 10px 1fr; gap: 2px 8px; }
.sources .dot { width: 8px; height: 8px; border-radius: 50%; margin-top: 5px; background: var(--muted); }
.sources .dot.ok { background: var(--good); }
.sources .dot.err { background: var(--critical); }
.sources .name { color: var(--ink); font-weight: 550; }
.sources .detail { grid-column: 2; color: var(--muted); }

/* ---- Map ---- */
.leaflet-container { font: inherit; background: var(--surface-2); }
/* Quieten the busy OSM style so incidents stand out; invert it for dark mode. */
.osm-tiles { filter: saturate(0.35) contrast(0.95) brightness(1.03); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .osm-tiles { filter: invert(1) hue-rotate(180deg) saturate(0.3) brightness(0.85) contrast(0.9); }
}
:root[data-theme="dark"] .osm-tiles { filter: invert(1) hue-rotate(180deg) saturate(0.3) brightness(0.85) contrast(0.9); }
.marker { background: none; border: 0; }
.marker .glyph { width: 100%; height: 100%; box-shadow: 0 0 0 2px var(--surface), 0 1px 4px rgba(0, 0, 0, 0.35); font-size: 12px; }
.marker.cleared .glyph { opacity: 0.4; }
.marker.new .glyph { animation: ping 1.4s ease-out 3; }
@keyframes ping { 0% { box-shadow: 0 0 0 2px var(--surface), 0 0 0 2px var(--c); } 100% { box-shadow: 0 0 0 2px var(--surface), 0 0 0 16px transparent; } }
@media (prefers-reduced-motion: reduce) { .marker.new .glyph, .item.flash { animation: none; } }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.leaflet-popup-content { margin: 12px 14px; min-width: 240px; }
.leaflet-popup-content .item { border: 0; margin: 0; padding: 0; cursor: default; }
.leaflet-popup-content .item:hover { background: none; }
.leaflet-control-attribution { font-size: 10px; }

/* ---- Toasts ---- */
.toasts { position: fixed; left: 16px; bottom: 16px; z-index: 1500; display: grid; gap: 8px; max-width: min(380px, calc(100vw - 32px)); }
.toast {
  display: flex; gap: 10px; align-items: flex-start;
  background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow);
  border-radius: 10px; padding: 10px 12px; cursor: pointer; text-align: left;
}
.toast b { display: block; }
.toast span { color: var(--ink-2); font-size: 13px; }
