:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #14181f;
  --muted: #5b6472;
  --line: #e2e5ea;
  --chip: #e9ecf1;
  --accent: #2f6fde;
  --accent-text: #ffffff;
  --stage: #d9dce1;
  --warn: #c2410c;
  --ar: 4/3;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --surface: #171b22;
    --text: #e8ebf0;
    --muted: #97a0ad;
    --line: #262c36;
    --chip: #212731;
    --accent: #5b93f5;
    --accent-text: #0e1116;
    --stage: #1b2028;
    --warn: #f59e0b;
  }
}
/* pan-x pan-y = scrolling allowed, page pinch-zoom and double-tap zoom off */
* { box-sizing: border-box; touch-action: pan-x pan-y; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; }

.app {
  display: grid;
  grid-template-areas: "head" "stage" "panel";
  max-width: 960px;
  margin: 0 auto;
  padding-bottom: env(safe-area-inset-bottom);
}

/* Header + region tabs */
header {
  grid-area: head;
  padding: calc(10px + env(safe-area-inset-top)) 0 10px;
  min-width: 0;
}
.title-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px; gap: 12px;
}
h1 { font-size: 20px; margin: 0; letter-spacing: -0.01em; }
h1 small { font-weight: 500; color: var(--muted); font-size: 14px; margin-left: 6px; }
.icon-btn {
  width: 44px; height: 44px; border: 0; border-radius: 12px;
  background: transparent; display: grid; place-items: center; cursor: pointer;
}
.icon-btn:active { background: var(--chip); }
.icon-btn svg { width: 22px; height: 22px; }
.refresh {
  position: relative; width: auto; min-width: 44px; padding: 0 8px 0 10px;
  display: inline-flex; align-items: center; gap: 6px;
}
.refresh span { font-size: 13px; font-weight: 500; color: var(--muted); font-variant-numeric: tabular-nums; }
.refresh.stale span { color: var(--warn); font-weight: 600; }          /* the view is old: refresh */
.refresh.has-new::after {                                               /* a newer frame is probably out */
  content: ""; position: absolute; top: 8px; right: 6px; width: 9px; height: 9px;
  border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 2px var(--bg);
}
.spin svg { animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.tabs {
  display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none;
  padding: 10px 16px 2px; scroll-padding: 16px;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: none; border: 0; border-radius: 999px; padding: 9px 14px; min-height: 40px;
  background: var(--chip); cursor: pointer; font-weight: 500; white-space: nowrap;
}
.tab[aria-selected="true"] { background: var(--accent); color: var(--accent-text); }

/* Radar stage */
.stage-wrap { grid-area: stage; min-width: 0; }
.stage {
  position: relative; width: 100%; aspect-ratio: var(--ar);
  background: var(--stage); overflow: hidden;
  touch-action: pan-y;                     /* 1x: one finger scrolls the page */
  user-select: none; -webkit-user-select: none;
}
.stage.zoomed { touch-action: none; cursor: grab; }
.stage.dragging { cursor: grabbing; }
.zoom { position: absolute; inset: 0; transform-origin: 0 0; }
.zoom.anim { transition: transform .2s ease-out; }
/* every frame is its own already-loaded <img>; showing a frame never triggers a new request */
.frames { position: absolute; inset: 0; z-index: 0; }
.frames img { visibility: hidden; }
.frames img.prev { visibility: visible; z-index: 0; }   /* stays underneath until the new frame is painted */
.frames img.on { visibility: visible; z-index: 1; }
.stage img {
  position: absolute; inset: 0; width: 100%; height: 100%;
  object-fit: contain; user-select: none; -webkit-user-select: none;
  -webkit-touch-callout: none; pointer-events: none;
}
.stage img[src=""], .stage img:not([src]) { visibility: hidden; }
.zoom-reset {
  position: absolute; top: 8px; right: 8px; min-width: 44px; height: 36px; padding: 0 10px;
  border: 0; border-radius: 10px; cursor: pointer; font-weight: 600; touch-action: manipulation;
  background: color-mix(in srgb, var(--surface) 88%, transparent); box-shadow: 0 1px 4px rgba(0,0,0,.25);
}
.progress {
  position: absolute; left: 0; top: 0; height: 3px; width: 0;
  background: var(--accent); transition: width .15s, opacity .4s;
}
.msg {
  position: absolute; inset: 0; display: grid; place-items: center;
  padding: 24px; text-align: center; color: var(--muted);
}

/* Controls panel */
.panel { grid-area: panel; padding: 14px 16px 24px; min-width: 0; }
.time .row1 { display: flex; align-items: baseline; gap: 10px; white-space: nowrap; min-width: 0; }
.time .row2 { height: 24px; margin-top: 2px; }
.time .badge { display: inline-block; line-height: 18px; }
.time .big { font-size: 34px; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.time .sub { color: var(--muted); overflow: hidden; text-overflow: ellipsis; }
.badge {
  font-size: 12px; font-weight: 600; padding: 3px 8px; border-radius: 999px;
  background: var(--chip); color: var(--muted);
}
.badge.live { background: color-mix(in srgb, #1f9d55 18%, transparent); color: #1f9d55; }

/* Time bar: spans the whole 3 hours; tap or drag anywhere on it.
   --loaded = where the loaded part starts (dashed before it), --p = current frame. */
.scrub {
  --p: 1; --loaded: 0;
  position: relative; height: 44px; margin: 4px 0 0;
  cursor: pointer; touch-action: none; outline: none;
}
.scrub-track {
  position: absolute; left: 13px; right: 13px; top: 50%; height: 6px; margin-top: -3px;
}
.scrub-unloaded, .scrub-loaded, .scrub-fill { position: absolute; top: 0; bottom: 0; border-radius: 3px; }
.scrub-unloaded {
  left: 0; width: calc(100% * var(--loaded));
  background: repeating-linear-gradient(90deg, var(--line) 0 5px, transparent 5px 9px);
}
.scrub-loaded { left: calc(100% * var(--loaded)); right: 0; background: var(--line); }
.scrub-fill {
  left: calc(100% * var(--loaded)); width: calc(100% * max(0, var(--p) - var(--loaded)));
  background: var(--accent);
}
.scrub-thumb {
  position: absolute; top: 50%; left: calc(13px + (100% - 26px) * var(--p));
  width: 26px; height: 26px; margin: -13px 0 0 -13px; border-radius: 50%;
  background: var(--accent); border: 3px solid var(--surface); box-shadow: 0 1px 4px rgba(0,0,0,.3);
}
.scrub:focus-visible .scrub-thumb { outline: 2px solid var(--accent); outline-offset: 2px; }
.ends { display: flex; justify-content: space-between; color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }

.controls { display: flex; align-items: center; justify-content: center; gap: 18px; margin: 10px 0 14px; }
.ctl {
  width: 52px; height: 52px; border-radius: 50%; border: 0; background: var(--chip);
  display: grid; place-items: center; cursor: pointer;
}
.ctl svg { width: 24px; height: 24px; }
.ctl.primary { width: 68px; height: 68px; background: var(--accent); color: var(--accent-text); }
.ctl.primary svg { width: 30px; height: 30px; }
.ctl:active { transform: scale(.94); }

.group { margin-top: 14px; }
.label { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-bottom: 8px; }
.seg { display: flex; background: var(--chip); border-radius: 12px; padding: 3px; }
.seg button {
  flex: 1 1 auto; border: 0; background: transparent; padding: 9px 6px; border-radius: 9px;
  cursor: pointer; font-weight: 500; min-height: 40px; white-space: nowrap;
}
.seg button[aria-checked="true"] { background: var(--surface); box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 8px 14px; min-height: 40px; cursor: pointer; font-weight: 500;
}
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

footer { margin-top: 22px; color: var(--muted); font-size: 13px; }
footer a { color: inherit; }
.disclaimer { margin-top: 6px; font-size: 12px; }

/* Landscape phones / desktop: map left, controls right */
@media (min-aspect-ratio: 5/4) {
  .app {
    max-width: none; min-height: 100svh; gap: 0 8px;
    grid-template-columns: max-content minmax(280px, 380px);
    grid-template-rows: auto 1fr;
    grid-template-areas: "stage head" "stage panel";
    justify-content: center; align-items: start;
    padding: 8px max(8px, env(safe-area-inset-right)) 8px max(8px, env(safe-area-inset-left));
  }
  header { padding-top: 0; }
  .stage {
    width: min(calc((100svh - 16px) * var(--ar)), calc(100vw - 300px - 24px));
    border-radius: 10px;
  }
  .stage-wrap { position: sticky; top: 8px; }
}

/* Desktop: the controls use all the width next to the map, and the region tabs wrap instead of
   scrolling. The min-width keeps landscape phones exactly as above. */
@media (min-aspect-ratio: 5/4) and (min-width: 1000px) {
  .app {
    grid-template-columns: max-content minmax(300px, 1fr);
    justify-content: stretch;
  }
  .tabs { flex-wrap: wrap; overflow-x: visible; }
}
