* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text-1);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
  overflow-wrap: break-word;
}
a {
  color: var(--accent);
}
.layout { display: flex; height: 100vh; }
.sidebar {
  width: 340px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
  gap: 12px;
  overflow-y: auto;
  overflow-x: hidden;
  transition: width .2s ease, padding .2s ease;
}
/* Collapsed = narrow icon rail (desktop only). */
.sidebar.collapsed {
  width: 64px;
  padding: 16px 8px;
  margin-left: 0;
}
.sidebar.collapsed .brand { flex-direction: column; justify-content: center; gap: 8px; padding: 4px 0 12px; border-bottom: 1px solid var(--border); }
.sidebar.collapsed .brand-name,
.sidebar.collapsed .nav-label,
.sidebar.collapsed .user-name,
.sidebar.collapsed .theme-label,
.sidebar.collapsed .staff-label,
.sidebar.collapsed .footer-label,
.sidebar.collapsed .site-version { display: none; }
.sidebar.collapsed .nav-item,
.sidebar.collapsed .logout,
.sidebar.collapsed .legal { justify-content: center; padding: 10px 0; }
.sidebar.collapsed .legal svg { width: 18px; height: 18px; flex-shrink: 0; }
.sidebar.collapsed .staff { margin-top: 0; }
/* Theme switcher is hidden in the icon rail -
   available again when the sidebar is expanded. */
.sidebar.collapsed .theme-toggle { display: none; }
.sidebar.collapsed .user-card { justify-content: center; padding: 8px 0; background: transparent; }
.sidebar.collapsed .avatar { margin: 0; }
.sidebar.collapsed .sidebar-footer { align-items: stretch; }
.sidebar[hidden], .route-panel[hidden] { display: none; }

/* Mobile menu button + backdrop */
.mobile-menu-btn {
  display: none;
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 110;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 5px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-1);
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .25);
}
.mobile-menu-btn svg { width: 20px; height: 20px; }
.mobile-close-btn {
  display: none;
  background: none;
  border: none;
  color: var(--text-2);
  font-size: 18px;
  cursor: pointer;
  margin-left: auto;
  padding: 4px 8px;
  border-radius: 5px;
}
.mobile-close-btn:hover { color: var(--text-1); background: var(--surface-2); }
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .55);
  z-index: 90;
  opacity: 0;
  transition: opacity .2s ease;
}
body.sidebar-mobile-open .sidebar-backdrop { display: block; opacity: 1; }
body.auth-page .mobile-menu-btn { display: none; }

/* Route panel: replaces the sidebar when a bus/train is clicked, showing
   the service's calling pattern as a vertical timeline. */
.route-panel {
  width: 340px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  min-height: 0;
}
.route-panel-head {
  padding: 16px 16px 12px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 1;
}
.route-panel-head h2 {
  margin: 0 28px 4px 0;
  font-size: 17px;
  line-height: 1.3;
}
.route-panel-head p {
  margin: 0;
  font-size: 13px;
  color: var(--text-2);
}
.route-close {
  position: absolute;
  top: 10px;
  right: 10px;
  background: none;
  border: none;
  color: var(--text-2);
  font-size: 16px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 5px;
}
.route-close:hover { color: var(--text-1); background: var(--surface-2); }
#routeBadges { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
#routeBadges:empty { display: none; }
.rp-ridden {
  display: inline-block;
  padding: 1px 10px;
  border-radius: 5px;
  background: rgba(52, 208, 100, .16);
  border: 1px solid var(--accent);
  color: var(--accent);
  font-size: 12px;
  font-weight: 700;
  align-content: center;
}
.rp-log-btn {
  display: inline-block;
  padding: 5px 14px;
  border-radius: 5px;
  background: var(--accent);
  color: #08130b;
  font-weight: 800;
  font-size: 13px;
  text-decoration: none;
}
.route-timeline {
  list-style: none;
  margin: 0;
  padding: 12px 16px 24px 0;
  position: relative;
}
.route-timeline::before {
  content: "";
  position: absolute;
  top: 26px;
  bottom: 34px;
  left: 25px;
  width: 3px;
  background: var(--text-2);
  border-radius: 2px;
  opacity: 0.85;
}
.route-timeline .stop {
  position: relative;
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 9px 0 9px 44px;
}
.route-timeline .stop::before {
  content: "";
  position: absolute;
  left: 18px;
  top: 17px;
  width: 17px;
  height: 4px;
  border-radius: 2px;
  background: var(--text-2);
  opacity: 0.85;
}
.route-timeline .stop.first::before {
  left: 17px;
  top: 9px;
  width: 15px;
  height: 15px;
  border-radius: 4px;
  border: 2px solid var(--text-2);
  opacity: 1;
  box-shadow: inset 0 0 0 3px var(--surface), inset 0 0 0 6px var(--text-2);
}
.route-timeline .stop.now::before {
  background: var(--accent);
  opacity: 1;
}
.route-timeline .stop.now.first::before {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 3px var(--surface), inset 0 0 0 6px var(--accent);
}
.route-timeline .stop.now .stop-name { color: var(--accent); }
.stop-main { display: flex; flex-direction: column; min-width: 0; }
.stop-name { font-weight: 700; font-size: 15px; color: var(--text-1); }
.stop.st-cancelled .stop-name { text-decoration: line-through; }
.stop-plat { font-size: 13px; color: var(--text-2); }
.stop-live {
  display: inline-block;
  align-self: flex-start;
  margin-top: 4px;
  padding: 1px 10px;
  border-radius: 5px;
  background: rgba(52, 208, 100, .16);
  border: 1px solid var(--accent);
  color: var(--accent);
  font-size: 12px;
  font-weight: 700;
}
.bus-popup .bus-live-row { color: var(--accent); font-size: 13px; font-weight: 600; }
.bus-popup .bus-live-dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  margin-right: 6px;
}
.stop-times { display: flex; flex-direction: column; text-align: right; flex-shrink: 0; }
.stop-times .aimed { font-weight: 700; font-size: 15px; color: var(--text-1); font-variant-numeric: tabular-nums; }
.stop-times .actual { font-size: 15px; font-weight: 700; font-variant-numeric: tabular-nums; color: var(--text-2); }
.stop-times .actual.est { opacity: 0.5; }
.stop-times .actual.st-early { color: var(--accent); }
.stop-times .actual.st-on-time { color: #22c55e; }
.stop-times .actual.st-late-small { color: #f59e0b; }
.stop-times .actual.st-late, .stop-times .actual.st-cancelled { color: #ef4444; }
.stop-times .actual.st-very-late { color: #b91c1c; }
.st-loading .stop-name { color: var(--text-3); font-weight: 400; font-style: italic; }
/* Live position dot: sits on the timeline rail between the last-departed
   stop and the next stop. Static per render; repositioned on refresh. */
.route-timeline .rail-dot {
  position: absolute;
  left: 19px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--surface);
  border: 3px solid var(--accent);
  box-shadow: 0 0 0 1px var(--surface);
  z-index: 1;
  pointer-events: auto;
}
/* Departures panel: its own sidebar (like the route panel) listing
   live departures for a clicked stop. */
.dep-panel {
  width: 340px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  min-height: 0;
}
.dep-panel[hidden] { display: none; }
.dep-panel-head {
  padding: 16px 16px 12px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 1;
}
.dep-panel-head h2 {
  margin: 0 28px 4px 0;
  font-size: 17px;
  line-height: 1.3;
}
.dep-title-row { display: flex; align-items: center; gap: 8px; margin: 0 28px 4px 0; }
.dep-title-row h2 { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin: 0; }
.dep-edit-pencil {
  color: var(--text-3);
  text-decoration: none;
  font-size: 16px;
  line-height: 1;
  padding: 2px 6px;
  border-radius: 5px;
  flex-shrink: 0;
}
.dep-edit-pencil:hover { color: var(--text-1); background: var(--surface-2); }
.dep-edit-pencil[hidden] { display: none; }
.dep-panel-head p {
  margin: 0;
  font-size: 13px;
  color: var(--text-2);
}
.dep-list { padding: 4px 16px 24px; display: flex; flex-direction: column; }
.dep-filters {
  display: flex;
  gap: 16px;
  margin-top: 10px;
  font-size: 13px;
  color: var(--text-2);
}
.dep-filters[hidden] { display: none; }
.dep-filters > label { display: flex; align-items: center; gap: 6px; cursor: pointer; }
.dep-filters input[type="checkbox"] { width: 15px; height: 15px; margin: 0; accent-color: var(--accent); cursor: pointer; }
.dep-datetime { display: flex; align-items: flex-end; gap: 8px; margin-top: 10px; font-size: 13px; color: var(--text-2); flex-wrap: wrap; }
.dep-datetime label { display: flex; flex-direction: column; gap: 4px; }
.dep-datetime input[type="date"], .dep-datetime input[type="time"] { background: var(--surface-2); color: var(--text-1); border: 1px solid var(--border); border-radius: 5px; padding: 6px 8px; font-size: 13px; color-scheme: dark; }
html[data-theme="light"] .dep-datetime input[type="date"], html[data-theme="light"] .dep-datetime input[type="time"] { color-scheme: light; }
.dep-today-btn { background: var(--surface-2); color: var(--text-1); border: 1px solid var(--border); border-radius: 5px; padding: 6px 10px; font-size: 13px; cursor: pointer; }
.dep-today-btn:hover { border-color: var(--accent); }
.dep-source { padding: 10px 16px 16px; font-size: 12px; color: var(--text-3); border-top: 1px solid var(--border); }
.dep-source[hidden] { display: none; }
.dep-source a { color: var(--accent); }
.dep-passing, .dep-terminating { color: var(--text-2); font-style: italic; font-weight: initial;}
.dep-unit { color: var(--accent); font-weight: 700; }
a.dep-unit { text-decoration: none; }
a.dep-unit:hover { text-decoration: underline; }
a.dep-unit.dep-link { text-decoration: underline dotted; }
.train-unit-row { display: flex; align-items: center; gap: 8px; }
/* .train-livery-swatch uses shared ui-elements.css. */
.spider-legs { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.spider-legs line { stroke: rgba(255, 255, 255, 0.65); stroke-width: 2; }
.spider-dot { width: 20px; height: 20px; border-radius: 50%; border: 1px solid var(--bg); cursor: pointer; box-shadow: 0 1px 6px rgba(0, 0, 0, 0.5); color: #fff; font-size: 10px; font-weight: 700; line-height: 16px; text-align: center; text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8); }
.spider-dot:hover { border-color: #22c55e; }
.dep-row {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 14px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.dep-row:last-child { border-bottom: none; }
.dep-line {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}
.dep-times {
  font-weight: 800;
  font-size: 18px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  color: var(--text-1);
}
.dep-row.passing {
  opacity: 0.5;
  gap: 2px;
  padding: 5px 0;
}
.dep-times .dep-sch { color: var(--text-1); }
.dep-times .dep-arr { color: var(--text-3); margin: 0 2px; }
.dep-times .dep-exp { color: var(--text-1); }
.dep-plus {
  font-size: 11px;
  font-weight: 700;
  opacity: 0.75;
  margin-left: 1px;
  vertical-align: super;
}
.dep-exp { margin-left: auto; }
.dep-main { font-size: 16px; font-weight: 700; }
.dep-svc {
  color: var(--accent);
  white-space: nowrap;
  min-width: 44px;
  flex-shrink: 0;
}
a.dep-svc { text-decoration: none; }
a.dep-svc:hover { text-decoration: underline; }
.dep-dest {
  color: var(--text-1);
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.dep-log {
  margin-left: auto;
  flex-shrink: 0;
  padding: 5px 16px;
  border-radius: 5px;
  background: var(--accent);
  color: #08130b;
  font-weight: 800;
  font-size: 13px;
  text-decoration: none;
  white-space: nowrap;
}
.dep-log:hover { filter: brightness(1.08); }
.dep-meta {
  justify-content: space-between;
  font-size: 13px;
  color: var(--text-3);
}
.dep-vehicle {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  min-width: 0;
}
.dep-fleet, .dep-reg, .dep-operator, .dep-platform, .dep-train-status { color: var(--text-2); }
.dep-status { font-weight: 600; margin-left: auto; }
.dep-status.ontime { color: var(--accent); }
.dep-status.early { color: var(--accent); }
.dep-status.late { color: #f87171; }
.dep-sep { color: var(--text-3); opacity: 0.6; }
.dep-note { padding: 16px 0; color: var(--text-3); font-size: 14px; }
.dep-more { padding: 10px 0; color: var(--text-3); font-size: 13px; }
.brand { display: flex; align-items: center; gap: 10px; padding: 0px 3px 12px; border-bottom: 1px solid var(--border); }
.logo {
  background: var(--accent);
  color: #08130b;
  font-weight: 800;
  border-radius: 5px;
  padding: 4px 8px;
}
.brand-name { font-weight: 700; flex: 1; }
.collapse-btn { background: none; border: none; color: var(--text-2); font-size: 20px; cursor: pointer; border-radius: 5px; padding: 0 6px; }
.collapse-btn:hover { color: var(--text-1); background: var(--surface-2); }

.sidebar.collapsed .collapse-btn { transform: rotate(180deg); margin: 0; }
.nav { display: flex; flex-direction: column; gap: 4px; }
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border-radius: 5px;
  color: var(--text-2);
  text-decoration: none;
  font-weight: 600;
}
.nav-item svg { width: 18px; height: 18px; }
.nav-item:hover { background: var(--surface-2); color: var(--text-1); }
.nav-item.active { background: var(--surface-2); color: var(--accent); border: 1px solid var(--border); }
.nav-start { color: var(--text-1); }
.staff { margin-top: 8px; }
.staff-label, .footer-label { color: var(--text-3); font-size: 11px; text-transform: uppercase; letter-spacing: .08em; padding: 0 12px; }
.sidebar-footer { margin-top: auto; border-top: 1px solid var(--border); padding-top: 12px; display: flex; flex-direction: column; gap: 10px; }
.theme-toggle { display: flex; gap: 8px; padding: 0 8px; }
.theme-btn {
  flex: 1;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-2);
  border-radius: 5px;
  padding: 8px;
  cursor: pointer;
  font-weight: 700;
}
.theme-btn.active { color: var(--accent); border-color: var(--accent); }
.user-card {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface-2);
  border-radius: 5px;
  padding: 8px 10px;
  font-weight: 700;
}
.avatar { width: 28px; height: 28px; border-radius: 50%; background: var(--accent); display: inline-block; }
.logout-form { margin: 0; padding: 0; }
.logout { color: var(--danger); text-decoration: none; padding: 5px 12px; align-items: center; display: flex; gap: 4px; background: none; border: none; font: inherit; cursor: pointer; }
.sidebar .legal { color: var(--text-2); text-decoration: none; padding: 0 12px; font-size: 13px; display: flex; align-items: center; gap: 4px; }
.sidebar .legal svg { width: 18px; height: 18px; flex-shrink: 0; }
.sidebar .legal:hover { color: var(--text-1); }
.site-version {
  color: var(--text-3);
  font-size: 11px;
  padding: 0 12px;
  margin: 0;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.map-wrap { position: relative; flex: 1;  }
#map { width: 100%; height: 100%;   overflow: hidden; }
.layer-toggles {
  position: absolute;
  top: 22px;
  left: 22px;
  background: rgba(13, 20, 16, .9);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  color: var(--text-1);
  z-index: 10;
}
html[data-theme="light"] .layer-toggles { background: rgba(255, 255, 255, .92); }
html[data-theme="night"] .layer-toggles { background: rgba(10, 10, 11, .92); }
.layer-toggles { font-size: 13px; }
.layer-toggles > label { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.layer-toggles input[type="checkbox"] { width: 15px; height: 15px; margin: 0; accent-color: var(--accent); cursor: pointer; }
.maplibregl-ctrl-attrib { font-size: 11px; }
/* Zone maps panel + controls (viewer: pick the zone to show) */
.zone-btn {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-1);
  border-radius: 5px;
  padding: 6px 12px;
  cursor: pointer;
  font-weight: 700;
  font-size: 13px;
  white-space: nowrap;
}
.zone-btn:hover { filter: brightness(1.08); }
#zonesManage { margin-top: 2px; }
.zone-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 4px;
  border-bottom: 1px solid var(--border);
}
.zone-row.selected .zone-name { color: var(--accent); }
.zone-dot { width: 12px; height: 12px; border-radius: 3px; flex-shrink: 0; border: 1px solid var(--border); }
.zone-name {
  flex: 1;
  min-width: 0;
  background: none;
  border: none;
  color: var(--text-1);
  font-weight: 700;
  font-size: 14px;
  text-align: left;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.zone-name:hover { color: var(--accent); }
.zone-dim { color: var(--text-3); font-weight: 400; font-size: 12px; }
.zone-note { color: var(--text-3); font-size: 13px; }
/* Bustimes-style popup card, themed via vars. */
.bus-popup-card .maplibregl-popup-content {
  background: var(--surface) !important;
  color: var(--text-1);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 10px 12px;
  min-width: 180px;
  max-width: 250px;
  box-shadow: 0 4px 18px rgba(0, 0, 0, .35);
  font: 13px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
}
.bus-popup-card .maplibregl-popup-tip { border-top-color: var(--surface) !important; }
.bus-popup-card.maplibregl-popup-anchor-bottom .maplibregl-popup-tip { border-top-color: var(--surface) !important; }
.bus-popup-card.maplibregl-popup-anchor-top .maplibregl-popup-tip { border-bottom-color: var(--surface) !important; }
.bus-popup-card.maplibregl-popup-anchor-left .maplibregl-popup-tip { border-right-color: var(--surface) !important; }
.bus-popup-card.maplibregl-popup-anchor-right .maplibregl-popup-tip { border-left-color: var(--surface) !important; }
.bus-popup-card .maplibregl-popup-close-button {
  color: var(--text-2) !important;
  font-size: 16px;
  line-height: 1;
  padding: 6px 8px;
}
.bus-popup-card .maplibregl-popup-close-button:hover { color: var(--text-1) !important; }
.bus-popup .bus-row { margin: 1px 0; }
.bus-popup .bus-title { font-weight: 400; padding-right: 20px; }
.bus-popup a.bus-title-link { color: inherit; text-decoration: none; }
.bus-popup a.bus-title-link:hover { text-decoration: underline; }
.bus-popup a.bus-vehicle { color: var(--accent); text-decoration: none; font-weight: 600; }
.bus-popup a.bus-vehicle:hover { text-decoration: underline; }
.bus-popup .train-unit { color: var(--accent); font-weight: 600; }
.bus-popup a.train-unit { text-decoration: none; }
.bus-popup a.train-unit:hover { text-decoration: underline; }
.bus-popup .loading-ph { color: var(--text-3); font-style: italic; }
.bus-popup .train-unit-unknown { color: var(--text-3); font-style: italic; }
.bus-popup .ridden {
  display: inline-block;
  margin-top: 6px;
  padding: 1px 10px;
  border-radius: 5px;
  background: rgba(52, 208, 100, .16);
  border: 1px solid var(--accent);
  color: var(--accent);
  font-size: 12px;
  font-weight: 700;
}
.bus-popup .bus-log-btn {
  display: block;
  width: 100%;
  margin-top: 8px;
  padding: 7px 10px;
  border-radius: 5px;
  background: var(--accent);
  color: #08130b;
  font-weight: 800;
  font-size: 13px;
  text-align: center;
  text-decoration: none;
}
.bus-popup .bus-log-btn:hover { filter: brightness(1.08); }
/* Live bus markers, same pattern as bustimes.org: a rotated wrapper holds
   the livery-coloured service pill plus a direction arrow. */
.bus-marker {
  display: flex;
  align-items: center;
  cursor: pointer;
}
.vehicle-marker {
  border: 1px solid var(--surface);
  text-align: center;
  color: #000;
  fill: currentColor;
  text-anchor: middle;
  paint-order: stroke;
  stroke-width: 2px;
  background: #fff;
  width: 26px;
  height: 18px;
  margin: 1px;
  line-height: 16px;
  display: block;
  overflow: hidden;
}
svg.vehicle-marker {
  padding: 0;
}
.bus-marker .arrow,
.bus-arrow {
  width: 0;
  height: 0;
  border-top: 5px solid transparent;
  border-bottom: 5px solid transparent;
  border-left: 7px solid #fff;
  margin-left: -1px;
  filter: drop-shadow(0 0 1px rgba(0, 0, 0, .6));
  background: #000;
  border-left-color: #fff;
}
.bus-popup { font: 13px/1.4 system-ui, sans-serif; }
/* Live train markers: an SVG headcode pill; livery artwork and text
   colours arrive per request from the db UnitLivery row (no livery file
   involved); the arrow keeps the delay colour. */
.train-marker {
  display: flex;
  align-items: center;
  cursor: pointer;
}
.train-marker svg.vehicle-marker {
  width: 32px;
}
.train-marker .vehicle-marker text {
  font: 700 11px/1 system-ui, -apple-system, "Segoe UI", sans-serif;
}
.train-arrow {
  width: 0;
  height: 0;
  border-top: 9px solid transparent;
  border-bottom: 9px solid transparent;
  border-left: 7px solid #9ca3af;
  margin-left: 0;
  background: var(--surface);
}

/* ======================
   Responsive / mobile
   ====================== */

/* Scrollable tables on narrow screens */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border);
  border-radius: 5px;
  margin: 12px 0;
}
.table-wrap table { min-width: 640px; margin: 0; }
.table-wrap .diff-table,
.table-wrap .timetable { min-width: 640px; }

/* Better defaults for focus/touch */
button, a, input, select, textarea { touch-action: manipulation; }
button, .nav-item, .pill-btn, .log-save, .btn-secondary, .btn-danger, .layout-btn {
  min-height: 36px;
}
.nav-item, .pill-btn, .log-save, .btn-secondary, .btn-danger, .theme-btn { min-height: 40px; }

/* Reset any mobile transform when returning to desktop */
@media (min-width: 901px) {
  .sidebar, .route-panel, .dep-panel { transform: none !important; }
}

/* Desktop large: give the main layout room to breathe */
@media (min-width: 1600px) {
  .sidebar { width: 280px; }
  .sidebar.collapsed { width: 64px; margin-left: 0; }
  .route-panel { width: 300px; }
  .dep-panel { width: 340px; }
}

/* Page title base styles use shared ui-elements.css. */

/* Tablet and below */
@media (max-width: 900px) {
  html, body { font-size: 15px; }
  .layout { flex-direction: column; height: auto; min-height: 100vh; }
  .mobile-menu-btn { display: flex; }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 100vw;
    z-index: 100;
    transform: translateX(-110%);
    transition: transform .25s ease;
    border-right: 1px solid var(--border);
    box-shadow: 4px 0 24px rgba(0, 0, 0, .45);
  }
  .sidebar.mobile-open { transform: translateX(0); }
  .sidebar.collapsed { width: 100vw; margin-left: 0; padding: 16px 12px; transform: translateX(-110%); }
  .sidebar.collapsed.mobile-open { transform: translateX(0); }
  /* On mobile the drawer always shows full labels, even if collapsed on desktop. */
  .sidebar.collapsed .brand { flex-direction: row; justify-content: flex-start; padding: 0px 3px 12px; }
  .sidebar.collapsed .brand-name,
  .sidebar.collapsed .nav-label,
  .sidebar.collapsed .user-name,
  .sidebar.collapsed .theme-label,
  .sidebar.collapsed .staff-label,
  .sidebar.collapsed .footer-label,
  .sidebar.collapsed .site-version { display: revert; }
  .sidebar.collapsed .nav-item,
  .sidebar.collapsed .logout,
  .sidebar.collapsed .legal { justify-content: flex-start; padding: 9px 12px; }
  .sidebar.collapsed .legal { display: flex; font-size: 13px; }
  .sidebar.collapsed .theme-toggle { display: flex; flex-direction: row; padding: 0 8px; }
  .sidebar.collapsed .theme-btn { padding: 8px; }
  .sidebar.collapsed .user-card { justify-content: flex-start; padding: 8px 10px; background: var(--surface-2); }
  .collapse-btn { display: none; }
  .mobile-close-btn { display: inline-block; }

  .route-panel {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 100vw;
    z-index: 95;
    transform: translateX(-110%);
    transition: transform .25s ease;
    box-shadow: 4px 0 24px rgba(0, 0, 0, .45);
  }
  .route-panel[hidden] { display: none; }
  .route-panel.mobile-open { transform: translateX(0); }
  .dep-panel {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 100vw;
    z-index: 95;
    transform: translateX(-110%);
    transition: transform .25s ease;
    box-shadow: 4px 0 24px rgba(0, 0, 0, .45);
  }
  #depTitle, #depSub, #routeTitle, #routeSub {
    padding-left: 50px;
  }
  .dep-panel[hidden] { display: none; }
  .dep-panel.mobile-open { transform: translateX(0); }

  .map-wrap {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    z-index: 1;
  }
  .map-wrap ~ .route-panel, .layout > .route-panel { position: fixed; }

  .layer-toggles {
    top: auto;
    bottom: 18px;
    left: 14px;
    right: auto;
    border-radius: 5px;
    padding: 10px 14px;
    gap: 8px;
    font-size: 14px;
    max-width: calc(100vw - 28px);
  }

  .bus-popup-card .maplibregl-popup-content {
    max-width: calc(100vw - 40px);
    min-width: 160px;
  }

  .content > h1:first-of-type {
    min-height: 40px;
    padding-left: 52px;
    margin: 0 0 12px;
    text-align: right;
    display: block;
    font-size: 26px;
  }
  /* .layout h1.page-title mobile sizing handled by ui-elements.css. */
  .content {
    padding-top: 10px;
  }
  .crumbs { display: none; }
}

/* Small phones */
@media (max-width: 480px) {
  html, body { font-size: 15px; }
  .layer-toggles {
    bottom: 14px;
    left: 12px;
    padding: 8px 12px;
  }
  .route-panel-head h2, .dep-panel-head h2 { font-size: 16px; }
  .route-close { top: 8px; right: 8px; padding: 8px 12px; font-size: 18px; }
  .route-timeline .stop { padding-left: 40px; }
  .stop-name { font-size: 14px; }
  .stop-times .aimed, .stop-times .actual { font-size: 14px; }
  .content > h1:first-of-type { font-size: 22px; }
}

/* Bracket truncation (mobile): stop/destination names keep their full text
   in `title`, with each "(...)" group wrapped in a <span class="br"> by
   app.js. JS hides the rightmost groups one at a time only while the
   element actually overflows. These rules just make overflow measurable
   (single-line ellipsis) on narrow screens. */
@media (max-width: 900px) {
  .route-leg { min-width: 0; }
  .route-leg strong,
  .dep-dest,
  .compact-route,
  .route-timeline .stop-name {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .route-leg strong { flex: 1 1 auto; }
  .br[hidden] { display: none !important; }
  /* Defensive: force every container in the chain to actually shrink so the
     name element above is the box that overflows (measurable + ellipsis).
     Without this, an ancestor can absorb the overflow and the text gets
     clipped by the card edge with no ellipsis and no measurable overflow. */
  .content, .trip-list, .trip-card, .service-panel,
  .route-line, .route-line-stacked, .route-leg {
    min-width: 0;
    max-width: 100%;
  }
}

/* Progressive bracket hiding: as the viewport narrows, drop the rightmost
   "(...)" group first, then every group on very small screens. This is pure
   CSS so it applies even before/without the JS overflow refinement. */
@media (max-width: 640px) {
  .route-leg strong .br:last-of-type,
  .dep-dest .br:last-of-type,
  .compact-route .br:last-of-type,
  .route-timeline .stop-name .br:last-of-type {
    display: none;
  }
}
/* Second group: :nth-last-of-type(2) matches only when 2+ groups exist, so
   single-group names are untouched here. Drops e.g. "(Manchester Metrolink)"
   once "(To Manchester)" is already gone. */
@media (max-width: 600px) {
  .route-leg strong .br:nth-last-of-type(2),
  .dep-dest .br:nth-last-of-type(2),
  .compact-route .br:nth-last-of-type(2),
  .route-timeline .stop-name .br:nth-last-of-type(2) {
    display: none;
  }
}
@media (max-width: 430px) {
  .route-leg strong .br,
  .dep-dest .br,
  .compact-route .br,
  .route-timeline .stop-name .br {
    display: none;
  }
}
