/* ---------------------------------------------------------------- tokens */

:root {
  --bg:        #ffffff;
  --panel:     #fbfbfc;
  --raised:    #ffffff;
  --border:    #e4e4e7;
  --border-2:  #d4d4d8;
  --text:      #18181b;
  --muted:     #55555f;
  --faint:     #74747f;
  --accent:    #0d7c6d;
  --accent-bg: #0d7c6d14;
  --warn:      #b45309;
  --shadow:    0 1px 2px #18181b0d, 0 4px 12px #18181b0a;
  --radius:    10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:        #0b0b0d;
    --panel:     #111114;
    --raised:    #17171b;
    --border:    #26262b;
    --border-2:  #34343a;
    --text:      #f4f4f5;
    --muted:     #bcbcc6;
    --faint:     #90909b;
    --accent:    #2dd4bf;
    --accent-bg: #2dd4bf1f;
    --warn:      #fbbf24;
    --shadow:    0 1px 2px #0006, 0 4px 16px #0004;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  display: grid;
  grid-template-columns: min(400px, 40vw) 1fr;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }

/* ----------------------------------------------------------------- panel */

.panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
}

/*
  Drag handle for the mobile bottom sheet. Hidden on desktop, where the panel
  is a plain grid column and has nothing to drag.
*/
.panel__grab {
  display: none;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 22px;
  padding: 0;
  border: 0;
  background: none;
  touch-action: none;
}
.panel__grab-bar {
  width: 40px;
  height: 4px;
  border-radius: 999px;
  background: var(--border-2);
  transition: background .12s;
}
.panel__grab:active .panel__grab-bar { background: var(--faint); }

.panel__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px 12px;
  border-bottom: 1px solid var(--border);
}

.panel__head h1 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.tabs { display: flex; gap: 2px; padding: 2px; background: var(--bg); border: 1px solid var(--border); border-radius: 8px; }

.tab {
  padding: 4px 10px;
  border: 0;
  border-radius: 6px;
  background: none;
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
}
.tab.is-active { background: var(--raised); color: var(--text); box-shadow: var(--shadow); }

.view { display: none; flex-direction: column; min-height: 0; flex: 1; }
.view.is-active { display: flex; }

/* ----------------------------------------------------------------- query */

.query { padding: 14px 16px; display: grid; gap: 8px; position: relative; }

.query__points { display: grid; gap: 8px; }

.field { position: relative; display: flex; align-items: center; }

.field__dot {
  position: absolute;
  left: 13px;
  width: 9px; height: 9px;
  border-radius: 50%;
  pointer-events: none;
}
.field__dot--from { background: var(--accent); }
.field__dot--to   { background: var(--text); }

.field input {
  width: 100%;
  padding: 10px 38px 10px 32px;
  background: var(--raised);
  border: 1px solid var(--border-2);
  border-radius: var(--radius);
  outline: none;
}
.field input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-bg); }

.field__pick {
  position: absolute;
  right: 5px;
  display: grid;
  place-items: center;
  width: 30px; height: 30px;
  border: 0;
  border-radius: 7px;
  background: none;
  color: var(--faint);
}
.field__pick:hover { background: var(--accent-bg); color: var(--accent); }
.field.is-picking .field__pick { background: var(--accent); color: #fff; }

/*
  Swap rides inside the first field as a second icon button, so the pair of
  endpoints stays a plain two-row stack. It sits outermost, with the pick
  button shifted left to make room.
*/
.swap {
  position: absolute;
  right: 5px;
  display: grid;
  place-items: center;
  width: 30px; height: 30px;
  border: 0;
  border-radius: 7px;
  background: none;
  color: var(--faint);
}
.swap:hover { background: var(--accent-bg); color: var(--accent); }

.field:has(.swap) input { padding-right: 70px; }
.field:has(.swap) .field__pick { right: 37px; }

.opts { display: flex; align-items: center; gap: 8px; }
.opts label { display: flex; align-items: center; gap: 5px; font-size: 13px; color: var(--muted); white-space: nowrap; }
.opts select {
  padding: 5px 6px;
  background: var(--raised);
  border: 1px solid var(--border);
  border-radius: 7px;
}

.go {
  margin-left: auto;
  padding: 8px 18px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  font-size: 14px;
}
.go:hover { filter: brightness(1.08); }
.go:disabled { opacity: .55; cursor: progress; }
/* A failed attempt shouldn't wear the "primary action" colour — it reads as
   a retry, outlined in the warning tone. */
.go.is-error {
  background: transparent;
  color: var(--warn);
  box-shadow: inset 0 0 0 1px var(--warn);
}

/* Results dim rather than disappear while a fresh answer is on its way:
   the panel keeps its height and the user keeps their place. */
.results.is-loading { opacity: .55; transition: opacity .12s; }

/* ------------------------------------------------------------- suggest */

.suggest {
  position: absolute;
  top: calc(100% + 4px);
  left: 0; right: 0;
  z-index: 30;
  max-height: 260px;
  overflow-y: auto;
  margin: 0;
  padding: 4px;
  list-style: none;
  background: var(--raised);
  border: 1px solid var(--border-2);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.suggest li { padding: 8px 9px; border-radius: 7px; font-size: 14px; }
.suggest li:hover, .suggest li.is-on { background: var(--accent-bg); }
.suggest small { display: block; margin-top: 1px; color: var(--faint); font-size: 12.5px; font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------- results */

.results { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 0 12px 16px; }

.hint { margin: 10px 4px; color: var(--muted); font-size: 13.5px; }
.hint strong { color: var(--text); font-weight: 600; }

.jr {
  margin-bottom: 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--raised);
  overflow: hidden;
  transition: border-color .12s;
}
.jr:hover { border-color: var(--border-2); }
.jr.is-on { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }

/*
  The whole card head is one button: the chain of bus numbers is the part
  people actually read, so it has to be tappable, not just the duration row.
*/
.jr__top {
  display: grid;
  gap: 7px;
  width: 100%;
  min-height: 44px;
  padding: 11px 13px;
  border: 0;
  background: none;
  text-align: left;
}

.jr__head { display: flex; align-items: baseline; gap: 8px; }

.jr__time { font-size: 21px; font-weight: 650; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.jr__unit { font-size: 13px; color: var(--muted); }
.jr__meta { margin-left: auto; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }

.jr__chev {
  flex: none;
  align-self: center;
  color: var(--faint);
  line-height: 0;
  transition: rotate .16s;
}
.jr.is-on .jr__chev { rotate: 180deg; color: var(--accent); }

/*
  Up to three transfers means up to seven chips, so the chain wraps. Give the
  rows their own gap and keep separators from breaking mid-hop, otherwise the
  wrapped block reads as noise.
*/
.chain { display: flex; flex-wrap: wrap; align-items: center; row-gap: 6px; column-gap: 5px; }
.chain__sep, .badge { flex: none; }

.badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 9px;
  border-radius: 6px;
  background: var(--bus, var(--accent));
  color: #fff;
  font-size: 13.5px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.badge--walk {
  background: var(--bg);
  border: 1px solid var(--border-2);
  color: var(--text);
  font-weight: 500;
}
.chain__sep { color: var(--faint); font-size: 12px; }

.est { margin-left: 4px; color: inherit; opacity: .7; font-weight: 500; }
.warnbar .est { margin: 0; color: var(--warn); opacity: 1; font-weight: 700; }

.jr__steps { display: none; padding: 0 13px 12px; border-top: 1px solid var(--border); }
.jr.is-on .jr__steps { display: block; }

.step { display: grid; grid-template-columns: 66px 1fr; gap: 12px; padding: 12px 0; }
.step + .step { border-top: 1px dashed var(--border); }
.step__lead { display: flex; flex-direction: column; align-items: flex-start; gap: 5px; }
.step__mins { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.step__where { font-size: 14px; line-height: 1.45; overflow-wrap: anywhere; }
.step__where b { font-weight: 600; }
.step__arrow { color: var(--faint); }
.step__note { margin-top: 4px; font-size: 13px; color: var(--warn); }
.step__detail { margin-top: 4px; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }

.warnbar {
  margin: 4px 4px 10px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-left: 3px solid var(--warn);
  border-radius: 8px;
  background: var(--raised);
  color: var(--muted);
  font-size: 13px;
  line-height: 1.5;
}
.warnbar b { color: var(--warn); }

/* -------------------------------------------------------------- routes */

.routes__head { display: grid; gap: 8px; padding: 14px 16px 10px; border-bottom: 1px solid var(--border); }
.routes__head input {
  padding: 8px 11px;
  background: var(--raised);
  border: 1px solid var(--border-2);
  border-radius: var(--radius);
  outline: none;
}
.routes__head input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-bg); }

.seg { display: flex; gap: 2px; padding: 2px; background: var(--bg); border: 1px solid var(--border); border-radius: 8px; }
.seg__btn { flex: 1; padding: 6px; border: 0; border-radius: 6px; background: none; color: var(--muted); font-size: 13px; }
.seg__btn.is-active { background: var(--raised); color: var(--text); box-shadow: var(--shadow); }

.routes__count { margin: 0; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }

.routes { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 8px 12px 16px; }

.rt {
  display: grid;
  grid-template-columns: 54px 1fr 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 7px 6px;
  border-bottom: 1px solid var(--border);
}
.rt__no { font-weight: 650; font-variant-numeric: tabular-nums; }

/* Keep the unit beside its input instead of wrapping under it. */
.rt label { display: flex; align-items: baseline; gap: 5px; min-width: 0; }

.rt input {
  width: 100%;
  padding: 5px 7px;
  background: var(--raised);
  border: 1px solid var(--border);
  border-radius: 7px;
  outline: none;
  font-variant-numeric: tabular-nums;
}
.rt input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-bg); }
.rt__unit { flex: none; font-size: 12px; color: var(--muted); }
.rt__save {
  padding: 5px 10px;
  border: 1px solid var(--border-2);
  border-radius: 7px;
  background: var(--raised);
  font-size: 13px;
}
.rt__save:hover { border-color: var(--accent); color: var(--accent); }
.rt.is-observed .rt__no::after { content: " ✓"; color: var(--accent); font-size: 11px; }
.rt.is-dirty { background: var(--accent-bg); }
.rt__note { grid-column: 1 / -1; margin: 0; font-size: 12.5px; color: var(--muted); }

/* ----------------------------------------------------------------- map */

#map { height: 100%; background: var(--panel); }
.leaflet-container { font: inherit; background: var(--panel); }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(.92) contrast(.9); }
}

.pin {
  display: grid;
  place-items: center;
  width: 22px; height: 22px;
  border: 2.5px solid var(--bg);
  border-radius: 50%;
  box-shadow: 0 1px 6px #0005;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
}
.pin--from { background: var(--accent); }
.pin--to   { background: var(--text); }

.leaflet-popup-content { margin: 9px 12px; font-size: 13px; }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--raised); color: var(--text); }

/* -------------------------------------------------------------- mobile */

/*
  On a phone the panel becomes a bottom sheet over a full-height map. A fixed
  split gave the results list about a third of the screen, which a journey with
  three transfers cannot fit into. Snap points (peek / half / full) let the
  reader trade map for detail as needed; --sheet-y is written by app.js.
*/
@media (max-width: 860px) {
  body { grid-template-columns: 1fr; grid-template-rows: 1fr; }

  #map { grid-row: 1; height: 100%; }

  .panel {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 1200;
    height: 92vh;
    height: 92svh;
    border-right: 0;
    border-top: 1px solid var(--border);
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -6px 28px #00000026;
    translate: 0 var(--sheet-y, 0px);
    transition: translate .28s cubic-bezier(.32, .72, 0, 1);
  }
  .panel.is-dragging { transition: none; }

  .panel__grab { display: flex; }

  .panel__head { padding-top: 4px; }

  /* Room for the home indicator, and for the sheet's own rounded lip. */
  .results, .routes { padding-bottom: calc(24px + env(safe-area-inset-bottom)); }

  /* Suggestions can outgrow a peeking sheet, so cap them to what is visible. */
  .suggest { max-height: 40svh; }
}

@media (max-width: 420px) {
  /*
    Two labelled selects and a button do not fit on one phone-width line —
    the labels used to wrap mid-word. Give the selects the row and the button
    a full-width tap target underneath.
  */
  .opts { flex-wrap: wrap; }
  .go { flex: 1 0 100%; margin-left: 0; padding: 10px 18px; }

  .jr__head { flex-wrap: wrap; }
  .jr__time { font-size: 19px; }
  .jr__meta { order: 2; flex-basis: 100%; margin-left: 0; }
  .jr__chev { order: 1; margin-left: auto; }
  .badge { padding: 2px 7px; font-size: 13px; }

  .step { grid-template-columns: 56px 1fr; gap: 10px; padding: 14px 0; }

  .rt { grid-template-columns: 44px 1fr 1fr auto; gap: 6px; }
}

@media (prefers-reduced-motion: reduce) {
  .panel, .jr__chev { transition: none; }
}
