:root {
  --bg: #0f1115;
  --surface: #181b22;
  --surface-2: #232732;
  --text: #f3f4f6;
  --text-dim: #9aa3b2;
  --border: #2a2f3a;
  --track: #2a2f3a;
  --grey: #4b5160;
  --light-green: #86efac;
  --green: #22c55e;
  --amber: #f59e0b;
  --red: #ef4444;
  --accent: #60a5fa;

  --circle-main: 220px;
  --circle-sm: 92px;
  --stroke-main: 16px;
  --stroke-sm: 9px;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  min-height: 100%;
  overscroll-behavior: contain;
}
body {
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

button { font-family: inherit; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px 4px;
}
.topbar h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 22px;
  cursor: pointer;
  padding: 8px;
  border-radius: 10px;
  line-height: 1;
}
.icon-btn:hover { color: var(--text); background: var(--surface); }

.main {
  padding: 12px 20px 40px;
  display: flex;
  flex-direction: column;
  gap: 28px;
  align-items: center;
}

/* ---------- Main bus block ---------- */
.main-bus {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: flex-start;
  gap: 22px;
  margin-top: 12px;
}
.main-bus-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  min-width: 0;
  max-width: 220px; /* keeps meta from making 3-up layout wrap */
}
.main-bus-item .meta {
  max-width: 100%;
  overflow-wrap: anywhere;
}
.main-bus .meta {
  text-align: center;
}
.main-bus .route {
  font-size: 20px;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.main-bus .stop {
  color: var(--text-dim);
  font-size: 14px;
  margin-top: 2px;
}
.main-bus .next {
  color: var(--text-dim);
  font-size: 13px;
  margin-top: 6px;
}

/* Per-count circle sizing for multi-main layouts.
   Label font size scales with circle so digits stay proportional.
   max-width on items + tighter gap so 2 or 3 mains sit in one row. */
.main-bus.count-2 { gap: 18px; }
.main-bus.count-2 .main-bus-item { max-width: 200px; }
.main-bus.count-2 .route { font-size: 17px; }
.main-bus.count-2 .stop { font-size: 13px; }

.main-bus.count-3 { gap: 14px; }
.main-bus.count-3 .main-bus-item { max-width: 160px; }
.main-bus.count-3 .route { font-size: 15px; }
.main-bus.count-3 .stop { font-size: 12px; }
.main-bus.count-3 .next { font-size: 11px; }

/* ---------- Secondaries ---------- */
.secondaries {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 14px;
  width: 100%;
  max-width: 480px;
}
.sec-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}
.sec-cell .route-tag {
  font-size: 13px;
  color: var(--text-dim);
  font-weight: 500;
}

/* ---------- Circle ---------- */
.circle-wrap {
  position: relative;
  display: inline-grid;
  place-items: center;
}
.circle-wrap.main {
  width: var(--circle-main);
  height: var(--circle-main);
}
.circle-wrap.sm {
  width: var(--circle-sm);
  height: var(--circle-sm);
}
.circle-wrap svg {
  width: 100%;
  height: 100%;
  /* SVG is NOT rotated — the fill <circle> rotates itself via a transform
     attribute so the text inside stays upright. */
}
.circle-wrap .track {
  stroke: var(--track);
  fill: none;
}
.circle-wrap .fill {
  fill: none;
  stroke-linecap: round;
  transition: stroke-dashoffset 600ms ease, stroke 400ms ease;
}
.circle-label {
  fill: var(--text);
  font-weight: 700;
  letter-spacing: -0.02em;
  font-family: inherit;
  pointer-events: none;
}

/* Color tiers, applied to the fill stroke.
   Breakpoints chosen so amber begins at 50% fill (8 min) and red at 70% fill (4.8 min). */
.tier-empty      .fill { stroke: var(--grey); }
.tier-mid        .fill { stroke: var(--green); }
.tier-near       .fill { stroke: var(--amber); }
.tier-imm        .fill { stroke: var(--red); }
.tier-imm-pulse  .fill { stroke: var(--red); animation: pulse 1.2s ease-in-out infinite; }
.tier-arr        .fill { stroke: var(--red); }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.55; }
}

/* Version tag — small, unobtrusive, bottom of page */
.version-tag {
  text-align: center;
  color: var(--text-dim);
  font-size: 11px;
  opacity: 0.6;
  padding: 8px 0 16px;
}

/* Status line */
.status {
  margin-top: 10px;
  color: var(--text-dim);
  font-size: 12px;
}

/* ---------- Add slot ---------- */
.add-slot {
  display: grid;
  place-items: center;
  width: var(--circle-sm);
  height: var(--circle-sm);
  border-radius: 50%;
  border: 2px dashed var(--border);
  color: var(--text-dim);
  font-size: 32px;
  background: transparent;
  cursor: pointer;
}
.add-slot:hover { color: var(--text); border-color: var(--text-dim); }

/* ---------- Modal ---------- */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.55);
  backdrop-filter: blur(4px);
  display: grid; place-items: center;
  z-index: 50;
  padding: 20px;
}
.modal.hidden { display: none; }
.modal-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  width: 100%;
  max-width: 520px;
  max-height: 90vh;
  display: flex; flex-direction: column;
  overflow: hidden;
}
.modal-card.small { max-width: 380px; }
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.modal-head h2 { margin: 0; font-size: 18px; }
.modal-body {
  padding: 16px;
  overflow-y: auto;
  display: flex; flex-direction: column;
  gap: 14px;
}
.modal-foot {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  display: flex; justify-content: flex-end;
}

.field {
  display: flex; flex-direction: column;
  gap: 4px;
}
.field span { font-size: 13px; color: var(--text-dim); }
.field input[type="text"], .field input[type="password"] {
  background: var(--surface-2);
  border: 2px solid var(--border);
  color: var(--text);
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 15px;
  outline: none;
}
/* Empty text inputs glow a touch lighter, signalling "fillable"; settle back
   to the standard fill once typed in. */
.field input[type="text"]:placeholder-shown {
  background: rgba(255, 255, 255, 0.07);
}
.field input:focus { border-color: var(--accent); }
.field small { color: var(--text-dim); font-size: 11px; }

h3 { margin: 4px 0 0; font-size: 14px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.05em; }
.hint { margin: 0; font-size: 12px; color: var(--text-dim); }

.bus-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 12px; }

/* Each bus is a vertical card: Main pill + remove on top, then stop field,
   then bus-number field. Works cleanly on a narrow phone. */
.bus-row {
  background: var(--surface-2);
  border-radius: 12px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.bus-row-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.bus-row .field { position: relative; display: flex; flex-direction: column; gap: 4px; }

/* Main toggle — compact checkbox + label */
.main-toggle {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  user-select: none;
}
.main-toggle input[type="checkbox"] {
  width: 16px;
  height: 16px;
  margin: 0;
  cursor: pointer;
  accent-color: var(--accent);
}
.main-toggle span {
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
}
.bus-row .remove-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  width: 20px;
  height: 20px;
  font-size: 14px;
  line-height: 1;
  padding: 0;
  cursor: pointer;
}
/* No press/hover color change — deletion is instant, so a red flash would just
   break the illusion that the row vanishes the moment you tap. */

/* Stop search field */
.stopSearch {
  background: var(--bg);
  border: 2px solid var(--border);
  color: var(--text);
  padding: 11px 12px;
  border-radius: 9px;
  font-size: 15px;
  width: 100%;
}
.stopSearch:placeholder-shown { background: rgba(255, 255, 255, 0.07); } /* lighter while empty */
.stopSearch:focus { border-color: var(--accent); outline: none; }
.stop-code-hint { font-size: 12px; color: var(--text-dim); padding-left: 10px; }

/* Example/placeholder text — lighter + italic so it never reads as pre-filled */
input::placeholder {
  color: var(--text-dim);
  opacity: 0.55;
  font-style: italic;
}

/* Autocomplete dropdown — absolutely positioned so it overlays (doesn't push
   rows) and can flip ABOVE the field when the field sits low on screen. */
.ac-menu {
  position: absolute;
  left: 0;
  right: 0;
  top: calc(100% + 2px); /* default: drop below the input */
  z-index: 30;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  border: 2px solid var(--border);
  border-radius: 9px;
  overflow: hidden;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
}
.ac-menu.flip-up {
  top: auto;
  bottom: calc(100% + 2px); /* flipped: rise above the input */
}
.ac-item {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: "name code" "road road";
  gap: 2px 8px;
  text-align: left;
  background: transparent;
  border: none;
  border-bottom: 1px solid var(--border);
  padding: 10px 12px;
  cursor: pointer;
}
.ac-item:last-child { border-bottom: none; }
.ac-item:hover { background: var(--surface); }
.ac-item:active { background: var(--surface-2); }
.ac-name { grid-area: name; color: var(--text); font-weight: 600; font-size: 14px; }
.ac-code { grid-area: code; color: var(--text-dim); font-size: 13px; }
.ac-road { grid-area: road; color: var(--text-dim); font-size: 12px; }

/* Bus-number field (tap to reveal chip grid). Always TRANSPARENT with a solid
   border — visually distinct from the lighter text-input fields. The chip
   selector is a button, not something you type into. */
.svc-field {
  background: transparent;
  border: 2px solid var(--border);
  color: var(--text);
  padding: 11px 12px;
  border-radius: 9px;
  font-size: 15px;
  text-align: left;
  cursor: pointer;
  width: 100%;
}
.svc-field:disabled { cursor: not-allowed; }
/* "Bus number" prompt — matched to the input placeholder grey (text-dim at 55%,
   on the text only so the border stays solid). */
.svc-field.svc-placeholder {
  font-style: italic;
  color: rgba(154, 163, 178, 0.55); /* = var(--text-dim) at 55% */
}
/* Unlocked but still empty → lighten the fill so it reads as "fillable" (like
   the empty input fields). Locked (:disabled) stays transparent and recedes. */
.svc-field.svc-placeholder:not(:disabled) {
  background: rgba(255, 255, 255, 0.07);
}
/* Manual bus-number entry (shown instead of the chip grid at big interchanges) */
.svc-input {
  background: var(--bg);
  border: 2px solid var(--border);
  color: var(--text);
  padding: 11px 12px;
  border-radius: 9px;
  font-size: 15px;
  width: 100%;
}
.svc-input:placeholder-shown { background: rgba(255, 255, 255, 0.07); } /* lighter while empty */
.svc-input:focus { border-color: var(--accent); outline: none; }
/* Live soft-validation warning — small red italic, bottom of the bus's card */
.svc-warning {
  color: var(--red);
  font-style: italic;
  font-size: 12px;
  padding-left: 2px;
}
.chip-grid {
  display: flex;
  flex-wrap: wrap; /* wrap-and-grow for big interchanges */
  gap: 8px;
  margin-top: 8px;
}
.chip {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 0 12px;
  height: 40px;
  min-width: 44px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.chip:hover { border-color: var(--accent); color: var(--accent); }
/* Pressed feedback — fills with the accent while held, for a snappy tap feel */
.chip:active {
  background: var(--accent);
  border-color: var(--accent);
  color: #0b1220;
}

/* "Add return stop" — subtle text link (advanced/optional feature, kept quiet) */
.add-return-btn {
  align-self: flex-start;
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 13px;
  font-weight: 500;
  text-decoration: underline;
  padding: 2px;
  cursor: pointer;
}
.add-return-btn:hover { color: var(--accent); }

/* Small uppercase header above the return field once it's shown */
.return-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-dim);
}
.remove-return-btn {
  align-self: flex-start;
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 12px;
  text-decoration: underline;
  cursor: pointer;
  padding: 2px;
}
/* No red flash on press — keep it a quiet grey link. */
.return-code-hint { font-size: 12px; color: var(--text-dim); padding-left: 10px; }

/* Reverse-direction button inside the arrivals popup */
.reverse-btn {
  width: 100%;
  min-height: 44px;
  background: var(--accent);
  color: #0b1220;
  border: none;
  border-radius: 9px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  margin-bottom: 4px;
}
.reverse-btn:active { filter: brightness(0.9); }

/* Utility: hide toggled elements. Placed after the component rules above so it
   wins on equal specificity; .modal.hidden stays more specific and unaffected. */
.hidden { display: none; }
/* `.bus-row .field` sets display:flex at higher specificity than `.hidden`, so
   the return field wouldn't actually hide without this more-specific rule. */
.bus-row .field.hidden { display: none; }

.primary-btn {
  background: var(--accent);
  color: #0b1220;
  border: none;
  padding: 10px 16px;
  border-radius: 10px;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
}
.primary-btn:hover { filter: brightness(1.05); }

/* Detail body */
.arrival-row {
  display: flex; justify-content: space-between;
  padding: 8px 4px;
  border-bottom: 1px solid var(--border);
  font-size: 15px;
}
.arrival-row:last-child { border-bottom: none; }
.arrival-row .when { color: var(--text); font-weight: 600; }
.arrival-row .ago  { color: var(--text-dim); font-size: 12px; }
.arrival-row .no-data { color: var(--text-dim); font-weight: 400; font-style: italic; }

/* Larger screen tweaks */
@media (min-width: 600px) {
  :root {
    --circle-main: 260px;
    --circle-sm: 104px;
  }
}
