:root {
  color-scheme: light;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  --color-cancelled: #b3261e;
  --color-disturbance: #b3261e;
  --color-info: #1a5fb4;
  --color-ink: #17263c;
  --color-muted: #52647a;
  --color-border: #dce4ec;
  --color-bg: #eef3f8;
  --color-header: #12233c;
  --color-surface: #fff;
  --color-overlay: rgba(255, 255, 255, 0.94);
  --color-hover: #f0f4fa;
  --color-chip: #f7f9fc;
  --color-button: #eaf0f6;
  --color-divider: #eee;
  --color-input-border: #a7b5c6;
  --color-chip-border: #ccc;
  --color-accent: #1a5fb4;
  --color-accent-ink: #114587;
  --color-accent-line: #b7cde9;
  --color-selected: #edf4ff;
  --color-selected-hover: #f4f8ff;
  --color-card-hover-border: #7295c4;
  --color-disabled: #9db6d8;
  --color-info-bg: #e8f0fb;
  --color-danger-bg: #fbeaea;
  --color-danger-soft: #fff2f2;
  --color-warn: #9a3412;
  --color-live: #1b7f3a;
  --color-stale: #8a6d00;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --color-cancelled: #ff8a80;
  --color-disturbance: #ff8a80;
  --color-info: #6ea8fe;
  --color-ink: #e3eaf3;
  --color-muted: #9aaabd;
  --color-border: #2b3a4f;
  --color-bg: #0b121c;
  --color-header: #0a1422;
  --color-surface: #141f2e;
  --color-overlay: rgba(20, 31, 46, 0.94);
  --color-hover: #1d2b3f;
  --color-chip: #1a2738;
  --color-button: #23324a;
  --color-divider: #243349;
  --color-input-border: #44566e;
  --color-chip-border: #3a4b63;
  --color-accent: #2f6fd0;
  --color-accent-ink: #a9cbff;
  --color-accent-line: #34507a;
  --color-selected: #1b2f4d;
  --color-selected-hover: #20365a;
  --color-card-hover-border: #4a78b5;
  --color-disabled: #3b5273;
  --color-info-bg: #162a47;
  --color-danger-bg: #3a1c1f;
  --color-danger-soft: #3a1c1f;
  --color-warn: #fdba74;
  --color-live: #5bd17f;
  --color-stale: #e6c34d;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--color-bg);
  color: var(--color-ink);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.app-header {
  min-width: 0;
  padding: 0.8rem 1rem;
  background: var(--color-header);
  color: #fff;
}

.header-title {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 0.25rem 0.85rem;
}

.app-header h1 {
  margin: 0;
  font-size: 1.2rem;
  letter-spacing: -0.02em;
}

.header-information {
  margin-top: 0.65rem;
  padding-top: 0.75rem;
  border-top: 1px solid rgba(255, 255, 255, 0.25);
  color: #c7d7ea;
  font-size: 0.85rem;
  line-height: 1.5;
}

.header-information h2 {
  margin: 0 0 0.4rem;
  color: #fff;
  font-size: 1rem;
}

.header-information p {
  max-width: 70ch;
  margin: 0 0 0.6rem;
}

.header-information a {
  color: #fff;
  text-underline-offset: 0.15em;
}

.header-updates {
  display: flex;
  align-items: center;
  min-width: 0;
  height: 1.5em;
  font-size: 0.75rem;
  line-height: 1.5;
}

.refresh-countdown {
  flex: 0 0 8.5rem;
  height: 1.5em;
  color: #9fb0c9;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.header-notices {
  flex: 1;
  min-width: 0;
  height: 1.5em;
  overflow-x: auto;
  overflow-y: hidden;
  white-space: nowrap;
  scrollbar-width: none;
}

.header-notices::-webkit-scrollbar {
  display: none;
}

.header-notices:not(:has(> :not([hidden]))) {
  visibility: hidden;
}

.header-notices:focus-visible {
  outline: 1px solid #ffdada;
  outline-offset: 2px;
}

.header-notice {
  color: #ffdada;
}

.header-notice::before {
  content: ' | ';
  white-space: pre;
}

.banner {
  margin: 0.25rem 0 0;
  padding: 0.4rem 0.6rem;
  border-radius: 4px;
  font-size: 0.9rem;
}

.banner-cancelled,
.banner-disturbance {
  background: var(--color-danger-bg);
  color: var(--color-disturbance);
  border: 1px solid var(--color-disturbance);
}

.banner-info {
  background: var(--color-info-bg);
  color: var(--color-info);
  border: 1px solid var(--color-info);
}

.notice {
  transition: opacity 0.3s ease;
}

.notice-fading {
  opacity: 0;
}

.status-message + .status-message::before {
  content: ' | ';
  white-space: pre;
}

.app-layout {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 0.75rem;
  padding-right: max(0.75rem, env(safe-area-inset-right));
  padding-bottom: max(0.75rem, env(safe-area-inset-bottom));
  padding-left: max(0.75rem, env(safe-area-inset-left));
}

.map-panel {
  position: relative;
  isolation: isolate;
  min-height: 320px;
  height: clamp(320px, 62vh, 640px);
  height: clamp(320px, 62dvh, 640px);
}

#map {
  position: relative;
  z-index: 0;
  width: 100%;
  height: 100%;
  border-radius: 10px;
  overflow: hidden;
}

.map-instruction {
  position: absolute;
  z-index: 500;
  bottom: 1.75rem;
  left: 0.75rem;
  max-width: calc(100% - 1.5rem);
  padding: 0.35rem 0.6rem;
  border-radius: 6px;
  background: var(--color-overlay);
  color: var(--color-ink);
  font-size: 0.8rem;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);
  pointer-events: none;
}

.map-controls {
  position: absolute;
  z-index: 1000;
  inset: 0.75rem 0.75rem 3.5rem 4rem;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.5rem;
  pointer-events: none;
}

.map-toolbar {
  display: flex;
  flex-shrink: 0;
  gap: 0.5rem;
  max-width: 100%;
}

.map-tool-button,
.map-tool-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  min-height: 44px;
  padding: 0.5rem 0.65rem;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  background: var(--color-surface);
  color: var(--color-ink);
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  pointer-events: auto;
}

.map-tool-button {
  box-shadow: 0 2px 8px rgba(18, 35, 60, 0.18);
  white-space: nowrap;
}

.map-tool-button[aria-expanded="true"],
.map-tool-button--filtered {
  border-color: var(--color-accent);
  background: var(--color-selected);
  color: var(--color-accent-ink);
}

.map-tool-count {
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
}

.map-tool-count:empty {
  display: none;
}

.map-tool-panel {
  width: 20rem;
  max-width: 100%;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 0.75rem;
  border: 1px solid var(--color-border);
  border-radius: 10px;
  background: var(--color-surface);
  box-shadow: 0 6px 24px rgba(18, 35, 60, 0.22);
  pointer-events: auto;
}

.map-tool-panel[hidden] {
  display: none;
}

.map-tool-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.map-tool-heading h2 {
  margin: 0;
  font-size: 1rem;
}

.map-tool-close {
  min-width: 44px;
  padding: 0;
  border: 0;
  background: var(--color-button);
  font-size: 1.3rem;
}

.map-tool-panel .line-filters {
  margin-top: 0.5rem;
  border-top: 1px solid var(--color-border);
}

.map-tool-panel .nearby-stops-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  width: 100%;
  margin-top: 0.5rem;
}

.map-tool-panel .stop-search-wrap {
  margin-top: 0.5rem;
}

.map-tool-panel .stop-search-results {
  position: static;
  margin-top: 0.35rem;
  max-height: 12rem;
  box-shadow: none;
}

.map-tool-panel .nearby-stops-list {
  flex-direction: column;
}

.nearby-stop-select {
  width: 100%;
  white-space: normal;
  overflow-wrap: anywhere;
}

.side-panel {
  display: flex;
  min-width: 0;
  flex-direction: column;
  gap: 0.75rem;
}

.arrivals-panel,
.live-buses-panel {
  min-width: 0;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 10px;
  padding: 0.85rem;
  box-shadow: 0 2px 10px rgba(18, 35, 60, 0.04);
}

.arrivals-panel h2,
.live-buses-panel h2 {
  margin: 0 0 0.7rem;
  font-size: 1.05rem;
  letter-spacing: -0.01em;
}

.section-kicker {
  margin: 0 0 0.15rem;
  color: var(--color-muted);
  font-size: 0.76rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.line-filters {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.line-filter {
  display: flex;
  min-height: 44px;
  align-items: center;
  gap: 0.55rem;
  padding: 0.25rem;
  font-size: 0.9rem;
  cursor: pointer;
}

.line-filter input {
  width: 1.25rem;
  height: 1.25rem;
  flex-shrink: 0;
}

.stop-search {
  display: flex;
  gap: 0.4rem;
}

.stop-search-wrap {
  position: relative;
}

.stop-search input {
  min-width: 0;
  min-height: 44px;
  flex: 1;
  padding: 0.65rem;
  border: 1px solid var(--color-input-border);
  border-radius: 6px;
  background: var(--color-surface);
  color: var(--color-ink);
  font: inherit;
}

.stop-clear {
  min-width: 44px;
  min-height: 44px;
  padding: 0.55rem 0.75rem;
  border: none;
  border-radius: 6px;
  background: var(--color-button);
  color: var(--color-ink);
  font: inherit;
  cursor: pointer;
}

input:focus-visible,
button:focus-visible,
summary:focus-visible,
.line-filter:has(input:focus-visible),
.stop-search-results li:focus-visible,
.vehicle-card:focus-visible {
  outline: 3px solid var(--color-accent);
  outline-offset: 2px;
}

.stop-search-results {
  position: absolute;
  z-index: 1200;
  top: calc(100% + 0.2rem);
  right: 0;
  left: 0;
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 15rem;
  overflow-y: auto;
  border: 1px solid var(--color-border);
  border-radius: 7px;
  background: var(--color-surface);
  box-shadow: 0 8px 20px rgba(18, 35, 60, 0.18);
}

.stop-search-results:empty {
  display: none;
}

.stop-search-results li {
  display: flex;
  min-height: 44px;
  align-items: center;
  padding: 0.55rem 0.6rem;
  cursor: pointer;
  border-bottom: 1px solid var(--color-divider);
}

.stop-search-results li:hover,
.stop-search-results li[aria-selected="true"] {
  background: var(--color-hover);
}

.departure-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.7rem;
}

.departure-actions:has(.favorite-toggle[hidden]) {
  display: none;
}

.departure-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.5rem;
}

.departure-heading > div {
  min-width: 0;
  overflow-wrap: anywhere;
}

.stop-clear {
  flex-shrink: 0;
}

.commuter-subheading {
  margin: 0;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--color-muted);
}

.commuter-favorites {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  margin-top: 0.8rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--color-border);
}

.commuter-favorites[hidden] {
  display: none;
}

.favorite-stops-list,
.nearby-stops-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.nearby-stops-list {
  margin-top: 0.55rem;
}

.favorite-stop-item {
  display: flex;
  align-items: stretch;
  gap: 0.35rem;
  max-width: 100%;
  min-width: 0;
}

.favorite-stop-select,
.nearby-stop-select {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  padding: 0.5rem 0.65rem;
  border: 1px solid var(--color-chip-border);
  border-radius: 6px;
  background: var(--color-chip);
  color: var(--color-ink);
  font: inherit;
  text-align: left;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
}

.favorite-stop-select {
  white-space: nowrap;
}

.favorite-stop-select[aria-current="true"] {
  background: var(--color-selected);
  border-color: var(--color-accent);
  font-weight: 600;
}

.favorite-stop-remove {
  min-width: 44px;
  min-height: 44px;
  border: none;
  border-radius: 6px;
  background: var(--color-button);
  color: var(--color-ink);
  font: inherit;
  cursor: pointer;
}

.favorite-toggle,
.nearby-stops-button {
  flex: 1 1 10rem;
  min-height: 44px;
  padding: 0.55rem 0.75rem;
  border: none;
  border-radius: 6px;
  background: var(--color-accent);
  color: #fff;
  font: inherit;
  cursor: pointer;
}

.favorite-toggle {
  border: 1px solid var(--color-accent);
  background: var(--color-selected);
  color: var(--color-accent-ink);
}

.favorite-toggle[hidden] {
  display: none;
}

.favorite-toggle:disabled,
.nearby-stops-button:disabled {
  background: var(--color-disabled);
  cursor: not-allowed;
}

.vehicle-list,
.departure-list {
  display: grid;
  gap: 0.5rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

.vehicle-card {
  display: grid;
  width: 100%;
  gap: 0.4rem;
  padding: 0.65rem 0.7rem;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  background: var(--color-surface);
  color: var(--color-ink);
  font: inherit;
  text-align: left;
}

button.vehicle-card {
  cursor: pointer;
}

.vehicle-card--selected {
  border-color: var(--color-info);
  background: var(--color-selected);
  box-shadow: inset 0 0 0 1px var(--color-info);
}

.vehicle-card--expanded {
  border-bottom: 0;
  border-radius: 8px 8px 0 0;
  box-shadow: inset 1px 0 var(--color-info), inset -1px 0 var(--color-info), inset 0 1px var(--color-info);
}

.vehicle-card-main,
.departure-main {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 0.55rem;
}

.vehicle-destination,
.departure-destination {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.9rem;
}

.vehicle-time,
.departure-time {
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  font-weight: 700;
}

.vehicle-time-label {
  margin-right: 0.35rem;
  color: var(--color-muted);
  font-size: 0.7rem;
  font-weight: 500;
  text-transform: uppercase;
}

.eta-delay {
  margin-left: 0.35rem;
  color: var(--color-disturbance);
  font-size: 0.75rem;
  font-weight: 600;
}

.vehicle-card-next {
  font-size: 0.87rem;
}

.vehicle-card-meta,
.departure-meta {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 0.2rem 0.7rem;
  color: var(--color-muted);
  font-size: 0.78rem;
  font-variant-numeric: tabular-nums;
}

.departure-card {
  padding: 0.7rem 0.15rem;
  border-bottom: 1px solid var(--color-border);
}

.departure-card:last-child {
  border-bottom: 0;
}

.departure-time {
  font-size: 1.1rem;
}

.departure-meta {
  justify-content: flex-start;
  margin-top: 0.3rem;
}

.departure-message {
  margin: 0.5rem 0 0;
  padding: 0.4rem 0.5rem;
  border-radius: 5px;
  background: var(--color-danger-soft);
  color: var(--color-disturbance);
  font-size: 0.82rem;
}

@media (hover: hover) {
  button.vehicle-card:hover {
    border-color: var(--color-card-hover-border);
    background: var(--color-selected-hover);
  }

  button.vehicle-card--selected:hover {
    border-color: var(--color-info);
    background: var(--color-selected);
  }
}

.line-badge {
  display: inline-block;
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
  font-weight: 600;
  font-size: 0.8rem;
}

.hint {
  margin: 0.3rem 0;
  color: var(--color-muted);
  font-size: 0.86rem;
}

.status-live {
  color: var(--color-live);
  font-weight: 600;
}

.status-stale {
  color: var(--color-stale);
  font-weight: 600;
}

.vehicle-icon-wrapper {
  background: none;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
}

.vehicle-icon-wrapper--selected .vehicle-marker {
  transform: scale(1.2);
  filter: drop-shadow(0 0 5px var(--color-info)) drop-shadow(0 0 2px var(--color-info));
}

.vehicle-icon-wrapper--hover::before {
  content: '';
  z-index: -1;
  position: absolute;
  inset: -6px;
  border: 3px solid #f59e0b;
  border-radius: 50%;
  background: rgba(245, 158, 11, 0.25);
  box-shadow: 0 0 0 2px #fff, 0 0 10px rgba(245, 158, 11, 0.9);
  pointer-events: none;
}

.vehicle-marker {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border: 3px solid var(--vehicle-color);
  border-radius: 50%;
  background: var(--color-surface);
  color: #1a1a1a;
  box-shadow: 0 0 0 1px #fff, 0 0 0 2px rgba(0, 0, 0, 0.55), 0 1px 4px rgba(0, 0, 0, 0.6);
  transition: transform 0.15s ease;
}

:root[data-theme="dark"] .vehicle-marker {
  color: #fff;
}

.vehicle-bus {
  width: 22px;
  height: 22px;
}

/* Rotate only the edge indicator; the bus stays upright. */
.vehicle-heading {
  position: absolute;
  inset: -3px;
  pointer-events: none;
}

.vehicle-direction {
  position: absolute;
  top: -6px;
  left: 9px;
  width: 12px;
  height: 10px;
  fill: var(--vehicle-color);
  stroke: #fff;
  stroke-width: 1.5;
  stroke-linejoin: round;
}

.vehicle-card--stale,
.vehicle-marker--stale {
  opacity: 0.45;
}

.vehicle-next-stop-path {
  filter: drop-shadow(0 0 1px #17263c);
  animation: next-stop-flow 1s linear infinite;
}

@keyframes next-stop-flow {
  to { stroke-dashoffset: -12; }
}

@media (prefers-reduced-motion: reduce) {
  .notice,
  .vehicle-marker {
    transition: none;
  }

  .vehicle-next-stop-path {
    animation: none;
  }
}

.information-toggle,
.theme-toggle {
  min-width: 44px;
  min-height: 32px;
  padding: 0.2rem 0.6rem;
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: 6px;
  background: transparent;
  color: #fff;
  font: inherit;
  font-size: 0.8rem;
  cursor: pointer;
}

.information-toggle {
  display: grid;
  place-items: center;
}

.information-toggle:hover,
.information-toggle[aria-expanded="true"],
.theme-toggle:hover {
  background: rgba(255, 255, 255, 0.12);
}

.information-toggle:focus-visible,
.theme-toggle:focus-visible,
.header-information a:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 3px;
}

/* OSM tiles are light-only, so the dark theme inverts the tile pane only;
   markers, routes and tooltips live in other panes and keep their colors. */
:root[data-theme="dark"] .leaflet-tile-pane {
  filter: invert(1) hue-rotate(180deg) brightness(0.95) contrast(0.9);
}

:root[data-theme="dark"] .leaflet-container {
  background: #1b1f26;
}

:root[data-theme="dark"] .leaflet-tooltip {
  border-color: var(--color-border);
  background: var(--color-surface);
  color: var(--color-ink);
}

:root[data-theme="dark"] .leaflet-tooltip-top::before {
  border-top-color: var(--color-surface);
}

:root[data-theme="dark"] .leaflet-bar a {
  border-bottom-color: var(--color-border);
  background: var(--color-surface);
  color: var(--color-ink);
}

:root[data-theme="dark"] .leaflet-bar a:hover {
  background: var(--color-hover);
}

:root[data-theme="dark"] .leaflet-control-attribution {
  background: var(--color-overlay);
  color: var(--color-muted);
}

:root[data-theme="dark"] .leaflet-control-attribution a {
  color: var(--color-accent-ink);
}

@media (min-width: 900px) and (min-height: 600px) {
  body {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
  }

  .app-layout {
    display: grid;
    min-height: 0;
    grid-template-columns: minmax(0, 1fr) clamp(340px, 28vw, 400px);
  }

  .map-panel {
    height: auto;
    min-height: 0;
  }

  .side-panel {
    display: flex;
    order: 1;
    min-width: 0;
    min-height: 0;
    flex-direction: column;
    gap: 0.75rem;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding-right: 0.25rem;
  }

  .side-panel > * {
    flex-shrink: 0;
  }

  .live-buses-panel {
    flex: 1 1 auto;
    min-height: 12rem;
    overflow-y: auto;
    overscroll-behavior: contain;
  }
}

@media (max-width: 480px) {
  .app-header {
    padding: 0.65rem 0.75rem;
  }

  .app-layout {
    gap: 0.6rem;
    padding-top: 0.6rem;
  }

  .arrivals-panel,
  .live-buses-panel {
    padding: 0.65rem;
  }

  .side-panel {
    gap: 0.6rem;
  }
}
