:root {
  --bg: #f6f6f4;
  --surface: rgba(255, 255, 255, 0.92);
  --surface-solid: #ffffff;
  --text: #16181c;
  --muted: #6b6f76;
  --line: rgba(0, 0, 0, 0.08);
  --hover: rgba(0, 0, 0, 0.05);
  --accent: #0a84ff;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.08), 0 6px 24px rgba(0, 0, 0, 0.12);
  --t1: #0a7d43;
  --t23: #2dbb68;
  --t4: #2f7de1;
  --t5: #d18a2c;
  --radius: 14px;
  --gap: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16181c;
    --surface: rgba(36, 38, 43, 0.92);
    --surface-solid: #24262b;
    --text: #f2f2f3;
    --muted: #a0a4ab;
    --line: rgba(255, 255, 255, 0.1);
    --hover: rgba(255, 255, 255, 0.06);
    --accent: #3d9bff;
  --t1: #3ed58a;
  --t23: #a3e77f;
  --t4: #6aa8ff;
  --t5: #d9a35c;
    --t1: #3ed58a;
    --t23: #a3e77f;
    --t4: #6aa8ff;
    --t5: #d9a35c;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 6px 24px rgba(0, 0, 0, 0.45);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #16181c;
  --surface: rgba(36, 38, 43, 0.92);
  --surface-solid: #24262b;
  --text: #f2f2f3;
  --muted: #a0a4ab;
  --line: rgba(255, 255, 255, 0.1);
  --hover: rgba(255, 255, 255, 0.06);
  --accent: #3d9bff;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 6px 24px rgba(0, 0, 0, 0.45);
  color-scheme: dark;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

#map { position: fixed; inset: 0; }

svg { width: 20px; height: 20px; fill: currentColor; flex: none; }

button { font: inherit; color: inherit; }

/* ---------- Loading screen ---------- */
.loading {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-content: center;
  gap: 28px;
  background: var(--bg);
  transition: opacity 0.4s ease;
}
.loading.done { opacity: 0; pointer-events: none; }
.loading-text { margin: 0; text-align: center; color: var(--muted); font-size: 14px; }

/* Loader from Uiverse.io by Li-Deheng; one circle per lane type. */
.loader {
  display: flex;
  justify-content: center;
  align-items: center;
  --color: var(--muted);
  --animation: 2s ease-in-out infinite;
}
.loader .circle:nth-child(1) { --color: var(--t1); }
.loader .circle:nth-child(2) { --color: var(--t23); }
.loader .circle:nth-child(3) { --color: var(--t4); }
.loader .circle:nth-child(4) { --color: var(--t5); }
.loader .circle {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  width: 20px;
  height: 20px;
  border: solid 2px var(--color);
  border-radius: 50%;
  margin: 0 10px;
  background-color: transparent;
  animation: circle-keys var(--animation);
}
.loader .circle .dot {
  position: absolute;
  transform: translate(-50%, -50%);
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background-color: var(--color);
  animation: dot-keys var(--animation);
}
.loader .circle .outline {
  position: absolute;
  transform: translate(-50%, -50%);
  width: 20px;
  height: 20px;
  border-radius: 50%;
  animation: outline-keys var(--animation);
}
.loader .circle:nth-child(2), .loader .circle:nth-child(2) .dot { animation-delay: 0.3s; }
.loader .circle:nth-child(3), .loader .circle:nth-child(3) .dot { animation-delay: 0.6s; }
.loader .circle:nth-child(4), .loader .circle:nth-child(4) .dot { animation-delay: 0.9s; }
.loader .circle:nth-child(1) .outline { animation-delay: 0.9s; }
.loader .circle:nth-child(2) .outline { animation-delay: 1.2s; }
.loader .circle:nth-child(3) .outline { animation-delay: 1.5s; }
.loader .circle:nth-child(4) .outline { animation-delay: 1.8s; }
@keyframes circle-keys {
  0% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.5); opacity: 0.5; }
  100% { transform: scale(1); opacity: 1; }
}
@keyframes dot-keys {
  0% { transform: scale(1); }
  50% { transform: scale(0); }
  100% { transform: scale(1); }
}
@keyframes outline-keys {
  0% { transform: scale(0); outline: solid 20px var(--color); outline-offset: 0; opacity: 1; }
  100% { transform: scale(1); outline: solid 0 transparent; outline-offset: 20px; opacity: 0; }
}
@media (prefers-reduced-motion: reduce) {
  .loader *, .loader { animation: none !important; }
}

/* ---------- Search ---------- */
.search {
  position: fixed;
  top: calc(env(safe-area-inset-top) + var(--gap));
  left: calc(env(safe-area-inset-left) + var(--gap));
  right: calc(env(safe-area-inset-right) + var(--gap) + 56px);
  max-width: 400px;
  display: flex;
  align-items: center;
  gap: 8px;
  height: 48px;
  padding: 0 6px 0 14px;
  border-radius: var(--radius);
  background: var(--surface);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  box-shadow: var(--shadow);
  z-index: 3;
}
.search-icon { color: var(--muted); width: 18px; height: 18px; }
.search input {
  flex: 1;
  min-width: 0;
  height: 100%;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 16px; /* prevents iOS zoom on focus */
}
.search input::placeholder { color: var(--muted); }
.search input::-webkit-search-cancel-button { display: none; }

.results {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  right: 0;
  margin: 0;
  padding: 6px;
  list-style: none;
  border-radius: var(--radius);
  background: var(--surface-solid);
  box-shadow: var(--shadow);
  max-height: min(60vh, 420px);
  overflow-y: auto;
}
.results button {
  display: block;
  width: 100%;
  padding: 10px 10px;
  border: 0;
  border-radius: 10px;
  background: none;
  text-align: left;
  cursor: pointer;
}
.results button:hover, .results button[aria-selected="true"] { background: var(--hover); }
.results .r-name { display: block; font-weight: 600; }
.results .r-addr { display: block; color: var(--muted); font-size: 13px; }
.results .r-empty { padding: 10px; color: var(--muted); }

/* ---------- Buttons ---------- */
.icon-btn {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 50%;
  background: none;
  color: var(--muted);
  cursor: pointer;
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.icon-btn.small svg { width: 18px; height: 18px; }

.controls {
  position: fixed;
  top: calc(env(safe-area-inset-top) + var(--gap));
  right: calc(env(safe-area-inset-right) + var(--gap));
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 2;
}
.fab {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border: 0;
  border-radius: var(--radius);
  background: var(--surface);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  box-shadow: var(--shadow);
  color: var(--text);
  cursor: pointer;
}
.fab:hover { color: var(--accent); }
.fab[aria-expanded="true"], .fab.active { color: var(--accent); }
.fab.labeled { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; }
.fab.labeled svg { width: 18px; height: 18px; }
.fab.labeled span { font-size: 10.5px; font-weight: 600; line-height: 1; }
.fab.waiting svg { animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.35; } }

.primary {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  height: 48px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}
.primary:disabled { opacity: 0.45; cursor: default; }

/* ---------- Sheets ---------- */
.sheet {
  position: fixed;
  left: calc(env(safe-area-inset-left) + var(--gap));
  bottom: calc(env(safe-area-inset-bottom) + var(--gap));
  width: 360px;
  max-width: calc(100vw - 2 * var(--gap));
  max-height: 70vh;
  overflow-y: auto;
  padding: 16px;
  border-radius: 18px;
  background: var(--surface);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  box-shadow: var(--shadow);
  z-index: 4;
}
.sheet-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  margin: -4px -6px 12px 0;
}
.sheet h2 { margin: 2px 0 0; font-size: 19px; font-weight: 700; letter-spacing: -0.01em; }
.muted { color: var(--muted); margin: 2px 0 0; }
.center { text-align: center; }
.small-text { font-size: 13px; margin-top: 8px; }

.legend { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; }
.legend li { display: grid; grid-template-columns: 36px 1fr; align-items: center; gap: 12px; }
.legend .swatch { height: 6px; border-radius: 3px; }
.legend .swatch.thin { height: 3px; }
.legend .swatch.dashed { background-size: 10px 100% !important; }
.legend .l-label { font-weight: 600; }
.legend .l-desc { display: block; font-weight: 400; color: var(--muted); font-size: 13px; }

.sheet-sub {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin: 18px 0 10px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}
.sheet-head + .sheet-sub { margin-top: 0; }
.sheet-sub span { text-transform: none; letter-spacing: 0; font-weight: 400; }
.legend .layer-row { display: contents; cursor: pointer; }
.legend li:has(.layer-row) { grid-template-columns: 36px 1fr auto; }
.legend.symbols li { grid-template-columns: 56px 1fr; }
.legend .symbol { width: 56px; height: 28px; }
.hint { margin: 14px 0 0; font-size: 13px; color: var(--muted); }

/* iOS-style switch */
.switch { position: relative; width: 44px; height: 26px; flex: none; }
.switch input { position: absolute; inset: 0; margin: 0; opacity: 0; cursor: pointer; }
.switch .track {
  position: absolute;
  inset: 0;
  border-radius: 999px;
  background: var(--line);
  box-shadow: inset 0 0 0 1px var(--line);
  transition: background 0.2s;
  pointer-events: none;
}
.switch .track::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
  transition: transform 0.2s;
}
.switch input:checked + .track { background: #34c759; }
.switch input:checked + .track::after { transform: translateX(18px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Segmented control (Appearance) */
.segmented {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 2px;
  padding: 2px;
  border-radius: 10px;
  background: var(--line);
}
.segmented button {
  height: 32px;
  border: 0;
  border-radius: 8px;
  background: none;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}
.segmented button[aria-checked="true"] { background: var(--surface-solid); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15); font-weight: 600; }

.fineprint { margin: 16px 0 0; font-size: 12px; color: var(--muted); }

.sheet-handle { display: none; }

@media (max-width: 600px) {
  /* Directions starts half height so the route stays visible; the handle expands it. */
  #directions { max-height: 46vh; transition: max-height 0.25s ease; }
  #directions.expanded { max-height: 88vh; }
  .sheet-handle {
    display: block;
    width: 100%;
    height: 18px;
    margin: -10px 0 4px;
    border: 0;
    background: none;
    cursor: pointer;
  }
  .sheet-handle::before {
    content: "";
    display: block;
    width: 36px;
    height: 5px;
    margin: 0 auto;
    border-radius: 3px;
    background: var(--line);
    box-shadow: inset 0 0 0 10px color-mix(in srgb, var(--muted) 40%, transparent);
  }
  .sheet {
    left: 0;
    right: 0;
    bottom: 0;
    width: auto;
    max-width: none;
    border-radius: 18px 18px 0 0;
    padding-bottom: calc(env(safe-area-inset-bottom) + 16px);
  }
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 40px);
  transform: translateX(-50%);
  max-width: calc(100vw - 2 * var(--gap));
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  font-weight: 500;
  box-shadow: var(--shadow);
  z-index: 5;
}

/* ---------- Directions ---------- */
.dir-fields { display: flex; align-items: center; gap: 6px; }
.dir-inputs { flex: 1; min-width: 0; display: grid; gap: 6px; }
.dir-field {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 44px;
  padding: 0 4px 0 12px;
  border-radius: 12px;
  background: var(--hover);
}
.dir-field input {
  flex: 1;
  min-width: 0;
  height: 100%;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 16px;
}
.dir-field input::placeholder { color: var(--muted); }
.dir-field input::-webkit-search-cancel-button { display: none; }
.dir-dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.dir-dot.from { background: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
.dir-dot.to { background: #ff3b30; }
.results.inline { position: static; margin: 0; box-shadow: none; border: 1px solid var(--line); max-height: 240px; }
.dir-status { margin: 12px 0 0; color: var(--muted); font-size: 14px; }
.dir-status.busy::before {
  content: "";
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-right: 8px;
  border-radius: 50%;
  border: 2px solid var(--muted);
  border-top-color: transparent;
  vertical-align: -1px;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.dir-summary { display: flex; align-items: baseline; gap: 10px; margin-top: 16px; }
#dir-time { font-size: 26px; font-weight: 700; letter-spacing: -0.02em; }
.dir-headline { margin: 2px 0 10px; font-weight: 600; color: var(--t1); }
.mixbar { display: flex; height: 10px; border-radius: 5px; overflow: hidden; gap: 2px; }
.mix, .warnings, .legs { list-style: none; margin: 10px 0 0; padding: 0; display: grid; gap: 8px; }
.mix li { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.mix .mix-val { margin-left: auto; color: var(--muted); font-variant-numeric: tabular-nums; }
.warnings li { display: grid; grid-template-columns: 20px 1fr; gap: 10px; align-items: start; font-size: 14px; }
.warnings svg { width: 18px; height: 18px; color: #f5a524; margin-top: 1px; }
.warnings .l-desc, .legs .l-desc { display: block; color: var(--muted); font-size: 13px; }
.legs li { display: grid; grid-template-columns: 10px 1fr; gap: 12px; align-items: start; font-size: 14px; }
.legs .pop-dot { margin-top: 5px; }
.legs b, .warnings b { font-weight: 600; }
#dir-result .primary { margin-top: 16px; }
.route-start {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  border: 3px solid #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
}

/* ---------- MapLibre overrides ---------- */
.maplibregl-ctrl-geolocate { display: none !important; }
.maplibregl-ctrl-bottom-right .maplibregl-ctrl-group { display: none; }
.maplibregl-ctrl-attrib.maplibregl-compact {
  background: var(--surface);
  color: var(--muted);
}
.maplibregl-ctrl-attrib a { color: inherit; }

.maplibregl-popup-content {
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--surface-solid);
  color: var(--text);
  box-shadow: var(--shadow);
  font: inherit;
  min-width: 180px;
}
.maplibregl-popup-tip { display: none; }
.maplibregl-popup-close-button { display: none; }
.pop-type { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.pop-dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.pop-name { margin-top: 2px; }
.pop-meta { margin-top: 6px; color: var(--muted); font-size: 13px; }
.pop-dir { margin-top: 6px; font-size: 13px; font-weight: 600; }
.pop-dir.oneway::before { content: "→ "; }
