:root {
  --bg: #eef1f4;
  --surface: rgba(255, 255, 255, 0.94);
  --surface-solid: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --line: rgba(15, 23, 42, 0.09);
  --ok: #16a34a;
  --low: #d97706;
  --empty: #dc2626;
  --off: #94a3b8;
  --eb: #facc15;
  --me: #2563eb;
  --fav: #f59e0b;
  --accent: #0f172a;
  --accent-ink: #ffffff;
  --shadow: 0 6px 24px rgba(15, 23, 42, 0.14), 0 1px 3px rgba(15, 23, 42, 0.1);
  --sheet-h: 0px;
  --top-h: 110px;
  --peek: min(40dvh, 340px);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1220;
    --surface: rgba(17, 24, 39, 0.94);
    --surface-solid: #111827;
    --text: #e5e7eb;
    --muted: #94a3b8;
    --line: rgba(255, 255, 255, 0.09);
    --ok: #22c55e;
    --low: #f59e0b;
    --empty: #ef4444;
    --off: #64748b;
    --accent: #f1f5f9;
    --accent-ink: #0f172a;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.45), 0 1px 3px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1220;
  --surface: rgba(17, 24, 39, 0.94);
  --surface-solid: #111827;
  --text: #e5e7eb;
  --muted: #94a3b8;
  --line: rgba(255, 255, 255, 0.09);
  --ok: #22c55e;
  --low: #f59e0b;
  --empty: #ef4444;
  --off: #64748b;
  --accent: #f1f5f9;
  --accent-ink: #0f172a;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.45), 0 1px 3px rgba(0, 0, 0, 0.4);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }

#map { position: fixed; inset: 0; background: var(--bg); }

/* ---------- Level colours (shared by pins, list badges and stats) */
.lvl-ok { --c: var(--ok); }
.lvl-low { --c: var(--low); }
.lvl-empty { --c: var(--empty); }
.lvl-off { --c: var(--off); }

/* ---------- Top bar */
.topbar {
  position: fixed;
  z-index: 1000;
  top: calc(env(safe-area-inset-top) + 10px);
  left: 10px;
  right: 10px;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  pointer-events: none;
}
.topbar > * { pointer-events: auto; }

.seg {
  display: flex;
  padding: 4px;
  border-radius: 999px;
  background: var(--surface);
  box-shadow: var(--shadow);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  min-width: 0;
}
.seg button {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-weight: 650;
  font-size: 14px;
  padding: 10px 8px;
  border-radius: 999px;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
}
.seg button[aria-selected="true"] { background: var(--accent); color: var(--accent-ink); }

.live {
  display: flex;
  align-items: center;
  gap: 7px;
  min-height: 44px;
  padding: 0 14px;
  border: 0;
  border-radius: 999px;
  background: var(--surface);
  box-shadow: var(--shadow);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  font-size: 13px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.live .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--off); flex: none; }
.live.ok .dot { background: var(--ok); animation: beat 2s ease-out infinite; }
.live.warn .dot { background: var(--low); }
.live.bad .dot { background: var(--empty); }
.live.busy .dot { animation: blink 0.6s ease-in-out infinite alternate; }
@keyframes beat {
  0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--ok) 55%, transparent); }
  70%, 100% { box-shadow: 0 0 0 7px transparent; }
}
@keyframes blink { to { opacity: 0.25; } }

.chips {
  grid-column: 1 / -1;
  display: flex;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
  padding: 2px 2px 8px;
  margin: -2px -2px -8px;
}
.chips::-webkit-scrollbar { display: none; }
.chips button {
  border: 0;
  border-radius: 999px;
  padding: 8px 13px;
  background: var(--surface);
  box-shadow: var(--shadow);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  font-size: 13px;
  font-weight: 650;
  white-space: nowrap;
}
.chips button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }
.bolt { margin-right: 3px; }
body[data-mode="docks"] .chips { display: none; }

/* ---------- Locate button */
.fab {
  position: fixed;
  z-index: 1000;
  right: 12px;
  bottom: calc(var(--sheet-h) + 12px);
  width: 48px;
  height: 48px;
  border: 0;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--surface-solid);
  color: var(--text);
  box-shadow: var(--shadow);
}
.fab.active { color: var(--me); }
.fab.following { background: var(--me); color: #fff; }
.fab.following path { fill: currentColor; }

/* ---------- You are here + facing direction */
.me-marker { width: 22px; height: 22px; pointer-events: none; }
.me-dot {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: var(--me);
  border: 3px solid #fff;
  box-shadow: 0 0 0 1px rgba(15, 23, 42, 0.15), 0 2px 6px rgba(15, 23, 42, 0.35);
}
.me-cone {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 150px;
  height: 150px;
  margin: -75px 0 0 -75px;
  background: conic-gradient(from -32deg, color-mix(in srgb, var(--me) 60%, transparent) 0deg 64deg, transparent 64deg);
  -webkit-mask-image: radial-gradient(circle closest-side, #000 10%, transparent 100%);
  mask-image: radial-gradient(circle closest-side, #000 10%, transparent 100%);
  opacity: 0;
  transition: opacity 0.3s;
}
.me-marker.has-heading .me-cone { opacity: 1; }
body[data-sheet="full"] .fab { opacity: 0; pointer-events: none; }
.fab.active path { fill: currentColor; }

/* ---------- Bottom sheet */
.sheet {
  position: fixed;
  z-index: 1001;
  left: 0;
  right: 0;
  bottom: 0;
  height: var(--peek);
  display: flex;
  flex-direction: column;
  background: var(--surface-solid);
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -6px 30px rgba(0, 0, 0, 0.16);
  padding-bottom: env(safe-area-inset-bottom);
  transition: height 0.25s ease;
}
.sheet[data-state="full"] { height: calc(100dvh - var(--top-h) - 20px); }
.sheet[data-state="detail"] { height: auto; max-height: 78dvh; }
.handle {
  flex: none;
  height: 26px;
  border: 0;
  background: transparent;
  display: grid;
  place-items: center;
  touch-action: none;
}
.handle::before {
  content: "";
  width: 40px;
  height: 5px;
  border-radius: 3px;
  background: var(--muted);
  opacity: 0.35;
}

.list-view, .detail-view {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}
.list-view { padding: 0 12px 12px; }
.list-view[hidden], .detail-view[hidden] { display: none; }

.search {
  position: sticky;
  top: 0;
  z-index: 1;
  padding-bottom: 6px;
  background: var(--surface-solid);
}
.search input {
  width: 100%;
  height: 42px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  font-size: 16px;
  outline: none;
}
.search input:focus { border-color: var(--me); }

.section-title {
  margin: 12px 4px 2px;
  font-size: 12px;
  font-weight: 750;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}
.hint { margin: 10px 4px 0; font-size: 13px; color: var(--muted); }

.list { list-style: none; margin: 0; padding: 0; }
.list .empty { padding: 18px 4px; color: var(--muted); }
.row {
  display: grid;
  grid-template-columns: 34px 1fr auto;
  gap: 10px;
  align-items: center;
  width: 100%;
  padding: 9px 4px;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: transparent;
  text-align: left;
}
.row:active { background: var(--bg); }
.row.dim .mini, .row.dim .row-main { opacity: 0.55; }
.mini {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--c);
  color: #fff;
  font-size: 13px;
  font-weight: 750;
  font-variant-numeric: tabular-nums;
}
.row-main { min-width: 0; }
.row-name, .row-sub { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-name { font-weight: 620; }
.row-sub { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.row .fv { color: var(--fav); }
.row-dist { text-align: right; font-size: 14px; font-weight: 650; font-variant-numeric: tabular-nums; white-space: nowrap; }
.row-dist small { display: block; font-size: 12px; font-weight: 500; color: var(--muted); }

/* ---------- Install prompt */
.install {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 6px;
  margin: 4px 0 6px;
  padding: 10px 6px 10px 12px;
  border: 1px solid color-mix(in srgb, var(--me) 25%, transparent);
  border-radius: 14px;
  background: color-mix(in srgb, var(--me) 9%, var(--surface-solid));
}
.install[hidden] { display: none; }
.install-text { min-width: 0; }
.install-text b { display: block; font-size: 14px; }
.install-text span { display: block; font-size: 13px; color: var(--muted); }
.share-ic { vertical-align: -3px; color: var(--me); }
.install-btn {
  border: 0;
  border-radius: 10px;
  padding: 9px 14px;
  background: var(--me);
  color: #fff;
  font-size: 14px;
  font-weight: 700;
}
.install-x {
  width: 34px;
  height: 34px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  font-size: 22px;
  line-height: 1;
}

/* ---------- Station detail */
.detail-view { padding: 0 16px 16px; }
.d-head { display: grid; grid-template-columns: 40px 1fr 40px; gap: 10px; align-items: start; }
.d-title { min-width: 0; }
.d-title h2 { margin: 6px 0 3px; font-size: 18px; line-height: 1.2; }
.d-title p { margin: 0; font-size: 13px; color: var(--muted); }
.icon-btn {
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--bg);
  font-size: 20px;
  line-height: 1;
}
.icon-btn.star.on { color: var(--fav); }
.d-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 14px 0 12px; }
.stat {
  padding: 12px 6px 10px;
  border: 2px solid transparent;
  border-radius: 14px;
  background: var(--bg);
  text-align: center;
}
.stat b { display: block; font-size: 30px; line-height: 1.05; font-variant-numeric: tabular-nums; }
.stat span { font-size: 12px; font-weight: 650; color: var(--muted); }
.stat.hl { border-color: var(--c); }
.stat.zero b { color: var(--muted); }
.d-note { margin: 0 0 10px; padding: 10px 12px; border-radius: 12px; font-size: 14px; font-weight: 600; }
.d-note.warn { background: color-mix(in srgb, var(--low) 15%, transparent); }
.d-note.bad { background: color-mix(in srgb, var(--empty) 14%, transparent); }
.d-meta { list-style: none; margin: 0 0 14px; padding: 0; display: grid; gap: 5px; font-size: 13px; color: var(--muted); }
.d-meta strong { color: var(--text); font-weight: 600; }
.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 48px;
  border-radius: 14px;
  font-weight: 700;
  text-decoration: none;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn-alt {
  display: block;
  margin-top: 6px;
  padding: 8px;
  text-align: center;
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
  text-decoration: none;
}

/* ---------- Toast */
.toast {
  position: fixed;
  z-index: 2000;
  left: 50%;
  top: calc(var(--top-h) + 16px);
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  padding: 10px 14px;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 14px;
  font-weight: 600;
  box-shadow: var(--shadow);
}
.toast[hidden] { display: none; }

/* ---------- Map controls */
.maplibregl-ctrl-bottom-right, .maplibregl-ctrl-bottom-left { bottom: var(--sheet-h); transition: bottom 0.25s ease; }
.maplibregl-ctrl-top-right { top: 10px; }
.maplibregl-ctrl-attrib { font-size: 10px; }

/* ---------- Wide screens: list becomes a side panel */
@media (min-width: 900px) {
  .topbar { right: auto; width: 400px; }
  .sheet, .sheet[data-state] {
    left: 10px;
    right: auto;
    top: calc(var(--top-h) + 20px);
    bottom: 10px;
    width: 400px;
    height: auto;
    max-height: none;
    border-radius: 20px;
    padding-bottom: 0;
    transition: none;
  }
  .handle { display: none; }
  .list-view { padding-top: 12px; }
  .detail-view { padding-top: 14px; }
  .fab, body[data-sheet="full"] .fab { bottom: 24px; right: 24px; opacity: 1; pointer-events: auto; }
  .maplibregl-ctrl-bottom-right, .maplibregl-ctrl-bottom-left { bottom: 0; }
  .toast { left: calc(50% + 205px); }
}
