:root {
  --brand: #c8102e;
  --brand-dark: #8e0b20;
  --green: #1a7f37;
}
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
html,
body {
  height: 100%;
  font-family:
    -apple-system,
    Segoe UI,
    Roboto,
    Helvetica,
    Arial,
    sans-serif;
  overscroll-behavior: none;
}
/* mobile viewport-safe map height */
#map {
  position: fixed;
  inset: 0;
  height: 100%;
  height: 100dvh;
  width: 100%;
  /* Allow the browser's native pinch/double-tap gestures so the map can
     zoom on touch devices. `manipulation` still lets taps/drags work while
     not blocking multi-touch zoom the way `none` does. */
  touch-action: manipulation;
}
#map::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: env(safe-area-inset-bottom);
  z-index: 500;
}

/* ===== visible keyboard focus ===== */
:focus-visible {
  outline: 3px solid #2563eb;
  outline-offset: 2px;
}

/* ===== status badge (top-right) ===== */
.badge {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  right: 12px;
  z-index: 1000;
  background: rgba(0, 0, 0, 0.72);
  color: #fff;
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 12px;
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  gap: 6px;
  opacity: 0.85;
}
.badge .gps-info {
  display: none;
  color: #60a5fa;
  font-weight: 600;
}
.badge .gps-info.on {
  display: inline;
}
#updateTime {
  color: #8ef0a6;
  font-weight: 600;
}
.status-ball {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #e2e8f0;
}
.status-ball.live {
  background: #22c55e;
  animation: pulse 1.4s infinite;
}
@keyframes pulse {
  0% {
    opacity: 1;
  }
  50% {
    opacity: 0.3;
  }
  100% {
    opacity: 1;
  }
}
@keyframes userpulse {
  0% {
    transform: scale(0.6);
    opacity: 0.7;
  }
  70% {
    transform: scale(1.6);
    opacity: 0;
  }
  100% {
    opacity: 0;
  }
}

/* ===== side panel (desktop) ===== */
.panel {
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 1000;
  background: rgba(255, 255, 255, 0.97);
  border-radius: 10px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.25);
  width: 320px;
  max-height: calc(100% - 24px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
#panelBody {
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.panel header {
  padding: 12px 16px;
  color: #fff;
  background: linear-gradient(135deg, var(--brand), var(--brand-dark));
}
.panel header h1 {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 0.02em;
}
.panel header p {
  font-size: 11px;
  opacity: 0.9;
  margin-top: 2px;
}
.panel .collapse-toggle {
  display: none;
}

.stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  padding: 12px 12px 0;
}
.stat {
  background: #f5f6fa;
  border-radius: 8px;
  padding: 8px 10px;
}
.stat .v {
  font-size: 18px;
  font-weight: 700;
}
.stat .l {
  font-size: 10px;
  text-transform: uppercase;
  color: #777;
  letter-spacing: 0.04em;
}
.stat.routes .v {
  color: var(--brand);
}
.stat.buses .v {
  color: var(--green);
}
.stat.none .v {
  color: #6b7280;
}

.controls {
  padding: 10px 12px;
}
.controls label {
  font-size: 12px;
  color: #444;
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  cursor: pointer;
  min-height: 32px;
}
.controls input[type="checkbox"] {
  accent-color: var(--brand);
  width: 18px;
  height: 18px;
  flex: none;
}
.controls button,
.controls select {
  background: var(--brand);
  color: #fff;
  border: 0;
  border-radius: 6px;
  padding: 10px 12px;
  font-size: 14px;
  cursor: pointer;
  width: 100%;
  margin-bottom: 8px;
  -webkit-tap-highlight-color: transparent;
}
.controls button {
  font-weight: 600;
}
.controls button:hover {
  background: var(--brand-dark);
}
.controls button:active {
  transform: scale(0.98);
}
.controls button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.controls select {
  background: #444;
  font-size: 14px;
}
.controls button.locate-btn {
  background: #1d4ed8;
}
.controls button.locate-btn:hover {
  background: #1e40af;
}
.controls button.locate-btn:disabled {
  background: #93a4c9;
}
.controls button.locate-btn.active {
  background: #16a34a;
}
.controls button.locate-btn.active:hover {
  background: #15803d;
}

.bustype {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 4px;
  margin: 4px 0 6px;
}
.bustype button {
  padding: 6px 4px;
  font-size: 12px;
  font-weight: 700;
  border: 1px solid #555;
  border-radius: 6px;
  background: #3a3a3a;
  color: #ddd;
  cursor: pointer;
}
.bustype button:hover {
  background: #4a4a4a;
}
.bustype button.active {
  background: var(--brand, #c8102e);
  color: #fff;
  border-color: var(--brand, #c8102e);
}
.bustype-hint {
  font-size: 11px;
  color: #b8b8c4;
  line-height: 1.35;
  margin: 0 0 8px;
}

.controls .search-wrap {
  font-size: 11px;
  color: #666;
  margin-bottom: 3px;
}
.controls input[type="text"] {
  width: 100%;
  padding: 10px 12px;
  font-size: 14px;
  border: 1px solid #ddd;
  border-radius: 6px;
  margin-bottom: 8px;
  background: #fff;
  color: #111;
}
.search-results {
  display: none;
  max-height: 180px;
  overflow-y: auto;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 6px;
  margin: -4px 0 8px;
}
.search-results div {
  padding: 8px 10px;
  font-size: 13px;
  cursor: pointer;
  border-bottom: 1px solid #f0f0f0;
}
.search-results div:hover,
.search-results div:focus-visible {
  background: #f5f6fa;
}
.search-results .sr-k {
  float: right;
  color: #888;
  font-size: 11px;
}
.search-results .sr-routetag {
  display: inline-block;
  background: var(--brand);
  color: #fff;
  font-size: 10px;
  padding: 1px 5px;
  border-radius: 3px;
  margin-right: 6px;
}
.popup-route {
  display: inline-block;
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 3px;
  margin-right: 4px;
}

.bus-highlight {
  animation: busPulse 1.1s ease-in-out infinite;
}
@keyframes busPulse {
  0%,
  100% {
    opacity: 1;
    filter: drop-shadow(0 0 3px rgba(255, 170, 0, 0.9))
      drop-shadow(0 0 8px rgba(255, 170, 0, 0.6));
  }
  50% {
    opacity: 0.55;
    filter: drop-shadow(0 0 8px rgba(255, 170, 0, 1))
      drop-shadow(0 0 16px rgba(255, 170, 0, 0.95));
  }
}
.bus-arrived-pulse {
  animation: busArrivedPulse 0.8s ease-in-out infinite;
}
@keyframes busArrivedPulse {
  0%,
  100% {
    opacity: 1;
    filter: drop-shadow(0 0 3px rgba(22, 163, 74, 0.9))
      drop-shadow(0 0 6px rgba(22, 163, 74, 0.7));
  }
  50% {
    opacity: 0.55;
    filter: drop-shadow(0 0 8px rgba(22, 163, 74, 1))
      drop-shadow(0 0 12px rgba(22, 163, 74, 0.9));
  }
}

.bus-stale {
  /* Keep the bus's own colour strong and visible, only slightly dimmed so
     a parked/stopped bus is still clearly identifiable but a touch muted. */
  filter: grayscale(0.2) brightness(0.7);
}
/* Dark/night mode: invert the map tiles and darken the UI chrome */
body.night .leaflet-tile-pane {
  filter: invert(0.9) hue-rotate(180deg) saturate(0.6) brightness(0.85);
}
body.night .leaflet-container {
  background: #0b1220;
}
body.night .panel,
body.night .leaflet-popup-content-wrapper,
body.night .leaflet-popup-tip {
  background: #1e293b;
}
body.night .leaflet-popup-content {
  color: #e2e8f0;
}
body.night .panel,
body.night .stats,
body.night .controls,
body.night .routes-list,
body.night .note,
body.night .foot {
  background: #111827;
  color: #e5e7eb;
  border-color: #1f2937;
}
body.night .search-wrap,
body.night .controls label {
  color: #cbd5e1;
}

/* ===== loading progress bar ===== */
.loading-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 2000;
  height: 3px;
  background: rgba(0, 0, 0, 0.12);
  overflow: hidden;
  opacity: 1;
  transition: opacity 0.4s ease;
}
.loading-bar.done {
  opacity: 0;
  pointer-events: none;
}
.loading-bar-fill {
  height: 100%;
  width: 0%;
  background: var(--brand);
  transition: width 0.3s ease;
}
.loading-bar-text {
  position: absolute;
  top: 6px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 11px;
  color: #555;
  white-space: nowrap;
  background: rgba(255, 255, 255, 0.9);
  padding: 2px 8px;
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);
}

.error-toast {
  position: fixed;
  bottom: max(20px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  z-index: 2000;
  background: #dc2626;
  color: #fff;
  border-radius: 8px;
  padding: 10px 18px;
  font-size: 13px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
  display: none;
  max-width: 90vw;
  text-align: center;
}
.note {
  font-size: 11px;
  color: #888;
  padding: 0 12px 10px;
  line-height: 1.4;
}

.legend {
  padding: 0 12px 12px;
  font-size: 11px;
  color: #ccc;
}
.legend-item {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-bottom: 5px;
}
.lg-bus {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  border: 1px solid #fff;
  display: inline-block;
}
.lg-stop {
  width: 13px;
  height: 13px;
  border-radius: 50%;
  border: 2px solid #fff;
  display: inline-block;
}

.routes-list {
  overflow-y: auto;
  padding: 0 12px 12px;
  border-top: 1px solid #eee;
}
.routes-list h3 {
  font-size: 12px;
  color: #555;
  padding: 10px 0 6px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.route-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 2px;
  font-size: 13px;
  color: #333;
}
.route-row .dot {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  flex: none;
}
.route-row .cnt {
  margin-left: auto;
  font-weight: 600;
  color: var(--green);
  font-size: 12px;
}
.route-row .none {
  margin-left: auto;
  color: #999;
  font-size: 12px;
}

.foot {
  font-size: 10px;
  color: #aaa;
  padding: 8px 16px;
  border-top: 1px solid #eee;
  text-align: center;
  background: #fafafa;
}

/* ===== mobile: bottom-sheet panel ===== */
@media (max-width: 720px) {
  .panel {
    left: 0;
    right: 0;
    top: auto;
    bottom: 0;
    width: auto;
    max-height: 75dvh;
    border-radius: 14px 14px 0 0;
    box-shadow: 0 -6px 24px rgba(0, 0, 0, 0.35);
    transition: transform 0.28s ease;
  }
  /* collapsed: only the grabber row peeks above bottom safe area */
  .panel.collapsed {
    transform: translateY(calc(100% - 58px - env(safe-area-inset-bottom)));
  }
  .panel .collapse-toggle {
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: sticky;
    top: 0;
    z-index: 5;
    background: rgba(255, 255, 255, 0.97);
    padding: 0 16px;
    height: 42px;
    cursor: pointer;
    border-bottom: 1px solid #eee;
    -webkit-tap-highlight-color: transparent;
    border-radius: 14px 14px 0 0;
  }
  .panel .collapse-toggle .grabber {
    flex: none;
    width: 44px;
    height: 5px;
    border-radius: 3px;
    background: #cbd0d9;
  }
  .panel .collapse-toggle .ttl {
    font-size: 12px;
    color: #666;
    font-weight: 600;
  }
  .panel .collapse-toggle .arrow {
    color: #999;
    font-size: 14px;
    transition: transform 0.28s ease;
  }
  .panel.collapsed .collapse-toggle .arrow {
    transform: rotate(180deg);
  }
  .panel .collapse-toggle .chev {
    display: none;
  }

  .controls button {
    min-height: 44px;
  }
  .controls select {
    min-height: 44px;
  }
  .controls label {
    -webkit-tap-highlight-color: transparent;
  }

  .badge {
    top: max(10px, env(safe-area-inset-top));
  }
}

/* ===== bus ETA tooltip ===== */
.bus-eta-tip {
  border: 0;
  background: transparent;
  box-shadow: none;
}
