/* ---- Theme tokens ------------------------------------------------------- */
/* Light is the default; dark overrides the same variables. Minimal + flat:
   hairline borders, one restrained brand accent, no gradients or glows. */
:root {
  --bg: #eef0ea;
  --surface: rgba(255, 255, 255, 0.88);
  --surface-2: rgba(255, 255, 255, 0.66);
  --hairline: rgba(22, 27, 34, 0.10);
  --hairline-strong: rgba(22, 27, 34, 0.18);
  --text: #1b2129;
  --muted: #5d6675;
  --faint: #8b94a2;
  --shadow: 0 6px 22px -10px rgba(20, 30, 55, 0.30);
  --brand: #e2001a;
  --live: #1a9d54;
  --warn: #c77700;
  --err: #c2401f;
  --accent: #2b6cb0;
  --blur: blur(12px) saturate(115%);
}
[data-theme="dark"] {
  --bg: #0b0f17;
  --surface: rgba(18, 23, 33, 0.82);
  --surface-2: rgba(28, 35, 49, 0.62);
  --hairline: rgba(255, 255, 255, 0.10);
  --hairline-strong: rgba(255, 255, 255, 0.17);
  --text: #e9eef6;
  --muted: #98a1b2;
  --faint: #6b7484;
  --shadow: 0 10px 30px -12px rgba(0, 0, 0, 0.6);
  --brand: #ff2d42;
  --live: #34d399;
  --warn: #fbbf24;
  --err: #f87171;
  --accent: #5aa2e0;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, system-ui, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
#map { position: absolute; inset: 0; background: var(--bg); }

/* MapLibre controls -> match the chrome */
.maplibregl-ctrl-group {
  background: var(--surface) !important; border: 1px solid var(--hairline) !important;
  border-radius: 10px !important; overflow: hidden; backdrop-filter: var(--blur);
  box-shadow: var(--shadow) !important;
}
.maplibregl-ctrl-group button { background: transparent; }
.maplibregl-ctrl-group button + button { border-top: 1px solid var(--hairline); }
.maplibregl-ctrl-group button:hover { background: rgba(127, 127, 127, 0.10); }
[data-theme="dark"] .maplibregl-ctrl-icon { filter: invert(1) brightness(1.4); opacity: .85; }
.maplibregl-ctrl-attrib { background: var(--surface-2) !important; border-radius: 6px 0 0 0; backdrop-filter: blur(6px); }
.maplibregl-ctrl-attrib, .maplibregl-ctrl-attrib a { color: var(--faint) !important; font-size: 10px; }

/* ---- Top bar ------------------------------------------------------------ */
#topbar {
  position: absolute; top: 0; left: 0; right: 0; z-index: 5;
  display: flex; justify-content: flex-end; align-items: center;
  gap: 10px; padding: calc(env(safe-area-inset-top) + 11px) 14px 10px;
  pointer-events: none;
}
.topbar-right { display: flex; align-items: center; gap: 8px; pointer-events: auto; }

.icon-btn {
  -webkit-appearance: none; appearance: none; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  width: 34px; height: 34px; padding: 0; color: var(--muted);
  background: var(--surface); border: 1px solid var(--hairline);
  border-radius: 10px; backdrop-filter: var(--blur); box-shadow: var(--shadow);
  transition: color .15s, border-color .15s, transform .1s;
}
.icon-btn:hover { color: var(--text); border-color: var(--hairline-strong); }
.icon-btn:active { transform: scale(.94); }
.icon-btn .ic { display: block; }
.ic-sun { display: none; }
[data-theme="dark"] .ic-moon { display: none; }
[data-theme="dark"] .ic-sun { display: block; }

/* ---- Bottom sheet ------------------------------------------------------- */
#sheet {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 5;
  background: var(--surface); border-top: 1px solid var(--hairline);
  border-radius: 18px 18px 0 0; backdrop-filter: var(--blur);
  box-shadow: var(--shadow);
  padding: 9px 16px calc(env(safe-area-inset-bottom) + 16px);
  animation: sheetUp .35s cubic-bezier(.2, .8, .2, 1);
}
@keyframes sheetUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
@media (prefers-reduced-motion: reduce) {
  #sheet { animation: none; }
  .live-dot::after { animation: none; }
}
.grip {
  display: block; width: 40px; height: 5px; padding: 0; border: 0; border-radius: 3px;
  background: var(--hairline-strong); margin: 3px auto 11px; cursor: pointer;
  -webkit-appearance: none; appearance: none; transition: background .2s, transform .15s;
}
.grip:hover { background: rgba(127, 127, 127, .45); }
.grip:active { transform: scaleX(.85); }

.sheet-head { margin: 0 2px 12px; }
.sheet-title { margin: 0; font-size: 16px; font-weight: 650; letter-spacing: -.01em; color: var(--text); }
.sheet-title span { color: var(--muted); font-weight: 500; }
.sheet-sub { display: flex; align-items: center; gap: 7px; margin-top: 4px; font-size: 11.5px; color: var(--muted); }
.sub-dot { color: var(--faint); }
.incount b, .viewers b { color: var(--text); font-weight: 650; font-variant-numeric: tabular-nums; }
.viewers { color: var(--muted); }

/* Live/status badge (the single status indicator, moved here from the top bar) */
.live {
  display: inline-flex; align-items: center; gap: 6px; flex: 0 0 auto;
  font-size: 10.5px; font-weight: 700; letter-spacing: .6px; text-transform: uppercase; color: var(--live);
}
.live-dot { position: relative; width: 6px; height: 6px; border-radius: 50%; background: var(--live); }
.live-dot::after { content: ""; position: absolute; inset: -3px; border-radius: 50%; border: 1px solid var(--live); opacity: .5; animation: pulse 2s ease-out infinite; }
.live.loading { color: var(--warn); } .live.loading .live-dot { background: var(--warn); }
.live.error { color: var(--err); } .live.error .live-dot { background: var(--err); }
.live.loading .live-dot::after, .live.error .live-dot::after { display: none; }
@keyframes pulse { 0% { transform: scale(.5); opacity: .6; } 100% { transform: scale(1.7); opacity: 0; } }

/* Collapsible drawer body (tap the grip to toggle) */
#sheet.collapsed .sheet-body { display: none; }
#sheet.collapsed .sheet-head { margin-bottom: 2px; }
.total-note { font-size: 11px; color: var(--warn); }

/* ---- Filter chips ------------------------------------------------------- */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  --c: #768390;
  display: inline-flex; align-items: center; gap: 8px; min-height: 38px;
  padding: 8px 12px 8px 10px; border-radius: 10px; cursor: pointer; user-select: none;
  font-size: 13px; font-weight: 500; line-height: 1; color: var(--text);
  border: 1px solid var(--hairline); background: var(--surface-2);
  transition: background .15s, border-color .15s, opacity .15s, transform .08s;
  -webkit-appearance: none; appearance: none;
}
.chip:hover { border-color: var(--hairline-strong); }
.chip.on { background: color-mix(in srgb, var(--c) 12%, var(--surface-2)); border-color: color-mix(in srgb, var(--c) 42%, var(--hairline)); }
.chip.off { opacity: .45; }
.chip.off .sw { filter: saturate(.3) brightness(.95); }
.chip .sw {
  width: 13px; height: 13px; background: var(--c); flex: 0 0 auto;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .35), 0 0 0 1px rgba(0, 0, 0, .10);
}
.chip .sw.ci { border-radius: 50%; }
.chip .sw.sq { border-radius: 3px; }
.chip .sw.di { border-radius: 2px; transform: rotate(45deg) scale(.82); }
.chip .cnt { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 12px; min-width: 1.3em; text-align: right; }
.chip.on .cnt { color: var(--text); }
.chip .cnt.zero { opacity: .4; }
.chip:active { transform: scale(.97); }

/* ---- Meta row + recenter ------------------------------------------------ */
.meta { display: flex; align-items: center; justify-content: space-between; margin-top: 14px; font-size: 13px; color: var(--muted); }
.total b { color: var(--text); font-size: 15px; font-weight: 650; font-variant-numeric: tabular-nums; }
.ghost-btn {
  -webkit-appearance: none; appearance: none; cursor: pointer; font: inherit; font-size: 12px; font-weight: 500; color: var(--text);
  background: var(--surface-2); border: 1px solid var(--hairline); padding: 7px 12px; border-radius: 8px;
  transition: background .15s, border-color .15s, transform .08s;
}
.ghost-btn:hover { border-color: var(--hairline-strong); }
.ghost-btn:active { transform: scale(.96); }

/* ---- Legend key --------------------------------------------------------- */
.legend-key {
  display: flex; align-items: center; gap: 8px 15px; flex-wrap: wrap;
  margin-top: 13px; padding-top: 11px; border-top: 1px solid var(--hairline);
  font-size: 11px; color: var(--muted);
}
.legend-key span { display: inline-flex; align-items: center; gap: 6px; }
.legend-key .k-line { width: 16px; height: 0; border-top: 2px solid var(--muted); opacity: .7; }
.legend-key .k-stop { width: 6px; height: 6px; border-radius: 50%; background: #fff; box-shadow: 0 0 0 1px rgba(90, 100, 120, .6); }
.legend-key .k-note { margin-left: auto; color: var(--faint); }
.note { margin-top: 10px; font-size: 10.5px; color: var(--faint); line-height: 1.45; }

/* ---- Vehicle popup ------------------------------------------------------ */
.veh-popup .maplibregl-popup-content {
  background: var(--surface); color: var(--text);
  border: 1px solid var(--hairline-strong); border-radius: 12px; backdrop-filter: var(--blur);
  padding: 10px 12px; box-shadow: var(--shadow);
  display: flex; gap: 10px; align-items: flex-start; min-width: 150px;
}
.veh-popup .maplibregl-popup-tip { border-top-color: var(--hairline-strong); border-bottom-color: var(--hairline-strong); }
.veh-popup .pline {
  font-weight: 700; font-size: 13px; line-height: 1; flex: 0 0 auto;
  padding: 6px 8px; border-radius: 7px; min-width: 22px; text-align: center;
}
.veh-popup .pbody { font-size: 13px; line-height: 1.38; }
.veh-popup .ptype { color: var(--faint); font-size: 9.5px; font-weight: 600; text-transform: uppercase; letter-spacing: .5px; margin-bottom: 2px; }
.veh-popup .pbody b { font-weight: 650; }
.veh-popup .pdest { color: var(--muted); font-size: 12px; }
.veh-popup .pd { font-size: 12px; margin-top: 3px; font-weight: 600; }
.veh-popup .pd.late { color: var(--warn); }
.veh-popup .pd.ontime { color: var(--live); }
.veh-popup .pd.early { color: var(--accent); }

/* ---- Wider screens: floating card on the left --------------------------- */
@media (min-width: 720px) {
  #sheet {
    left: 14px; right: auto; bottom: 14px; width: 326px;
    border-radius: 14px; border: 1px solid var(--hairline);
  }
  .grip { display: none; }
}
