*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* ── Theme variables ── */
:root {
  --kmb: #e8192c;
  --ctb: #009846;
  --nlb: #0070c0;
  --radius: 12px;
  --panel-h: 52vh;
  --topbar-h: 45px;

  --bg: #1a1a2e;
  --surface: #16213e;
  --card: #0f3460;
  --card-header: rgba(255,255,255,0.05);
  --accent: #e94560;
  --text: #eaeaea;
  --subtext: #a0a8c0;
  --border: rgba(255,255,255,0.09);
  --map-bg: #1a2535;
  --scrollbar: rgba(255,255,255,0.15);
  --toast-bg: rgba(0,0,0,0.82);
  --toast-color: #fff;
  --sep-color: rgba(255,255,255,0.16);
  --search-bg: rgba(255,255,255,0.08);
  --search-border: rgba(255,255,255,0.18);
  --ctrl-shadow: 0 2px 10px rgba(0,0,0,0.45);
}

[data-theme="light"] {
  --bg: #eef2f7;
  --surface: #ffffff;
  --card: #f4f7fb;
  --card-header: rgba(0,0,0,0.04);
  --text: #1a2340;
  --subtext: #5a6480;
  --border: rgba(0,0,0,0.10);
  --map-bg: #d4dce8;
  --scrollbar: rgba(0,0,0,0.15);
  --toast-bg: rgba(20,20,50,0.88);
  --toast-color: #fff;
  --sep-color: rgba(0,0,0,0.13);
  --search-bg: rgba(0,0,0,0.06);
  --search-border: rgba(0,0,0,0.18);
  --ctrl-shadow: 0 2px 10px rgba(0,0,0,0.18);
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
}

/* ── Fixed Top Bar ── */
#topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--topbar-h);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 1200;
}
#topbar-spacer { height: var(--topbar-h); flex-shrink: 0; }

#title { display: flex; align-items: center; gap: 8px; }
#title-icon { font-size: 20px; }
#title-text { font-size: 16px; font-weight: 700; letter-spacing: 0.5px; }
#controls { display: flex; gap: 8px; }

.ctrl-btn {
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 5px 11px;
  font-size: 14px; cursor: pointer;
  transition: background 0.15s, color 0.15s, transform 0.1s;
  white-space: nowrap;
}
.ctrl-btn:hover { background: var(--accent); color: #fff; }
.ctrl-btn:active { transform: scale(0.95); }
.ctrl-btn:disabled { opacity: 0.5; cursor: default; }
#refresh-btn.spinning { animation: spin 0.7s linear infinite; }

/* ── Map wrap — fills remaining space between spacer and ETA panel ── */
#map-wrap {
  flex: 1;
  position: relative;
  overflow: hidden;
  min-height: 0;
}

/* ── Leaflet map fills map-wrap completely ── */
#map {
  position: absolute;
  inset: 0;
  z-index: 1;
}
.leaflet-container { background: var(--map-bg); }

/* ── Map vignette — soft dark ring on edges, transparent centre ── */
#map-vignette {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 420;
  background: radial-gradient(
    ellipse 68% 62% at 50% 48%,
    transparent 35%,
    rgba(0, 0, 0, 0.28) 100%
  );
}
[data-theme="light"] #map-vignette {
  background: radial-gradient(
    ellipse 68% 62% at 50% 48%,
    transparent 35%,
    rgba(0, 0, 0, 0.14) 100%
  );
}

/* ── Floating map control buttons ── */
.map-ctrl-btn {
  width: 40px; height: 40px;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  box-shadow: var(--ctrl-shadow);
  transition: background 0.15s, transform 0.1s;
  white-space: nowrap;
}
.map-ctrl-btn:hover { background: var(--accent); color: #fff; }
.map-ctrl-btn:active { transform: scale(0.93); }
.map-ctrl-btn:disabled { opacity: 0.5; cursor: default; }
.map-ctrl-btn.sm { width: 34px; height: 34px; font-size: 15px; }

/* ── Collapsible search (top-right of map-wrap) ── */
#floating-search {
  position: absolute;
  top: 10px; right: 10px;
  z-index: 900;
  display: flex;
  align-items: center;
}

#search-expanded {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 5px 8px;
  box-shadow: var(--ctrl-shadow);
}
#search-expanded.hidden { display: none; }
#search-toggle-btn.hidden { display: none; }

#route-input {
  width: 155px;
  background: var(--search-bg);
  border: 1px solid var(--search-border);
  color: var(--text);
  border-radius: 8px;
  padding: 5px 10px;
  font-size: 15px;
  outline: none;
  transition: border-color 0.2s;
}
#route-input:focus { border-color: var(--accent); }
#route-input::placeholder { color: var(--subtext); }

/* ── Floating locate/favourite (bottom-right of map-wrap) ── */
#map-controls {
  position: absolute;
  bottom: 20px; right: 10px;
  z-index: 900;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* ── ETA Panel ── */
#eta-panel {
  background: var(--surface);
  border-top: 2px solid var(--accent);
  flex-shrink: 0;
  height: var(--panel-h);
  display: flex; flex-direction: column;
  z-index: 1100;
  transition: height 0.3s ease;
}
#eta-panel.hidden { height: 0; border-top: none; overflow: hidden; }

#panel-header {
  display: flex; align-items: flex-start; justify-content: space-between;
  padding: 10px 14px 8px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
#stop-info { flex: 1; min-width: 0; }

/* ── Stop name with marquee ── */
#stop-name {
  font-size: 17px; font-weight: 700; line-height: 1.3;
  overflow: hidden; white-space: nowrap;
  position: relative;
}
#stop-name .stop-name-text { display: inline-block; }
#stop-name.scrolling .stop-name-text {
  animation: marquee-stop 13s ease-in-out infinite;
}
@keyframes marquee-stop {
  0%,  18% { transform: translateX(0); }
  75%, 100% { transform: translateX(var(--marquee-shift, 0px)); }
}

#stop-meta { font-size: 12px; color: var(--subtext); margin-top: 3px; }
#panel-actions { display: flex; gap: 6px; margin-left: 10px; align-items: center; }
#refresh-btn, #close-btn { padding: 4px 9px; font-size: 15px; }

/* ── ETA list wrap ── */
#eta-list-wrap { flex: 1; overflow-y: auto; padding: 6px 12px 12px; }

#eta-empty {
  text-align: center;
  padding: 22px 0;
  color: var(--subtext);
  font-size: 15px;
}
#eta-empty.hidden { display: none; }

/* ── Group separator ── */
.group-sep {
  font-size: 11px; font-weight: 700;
  letter-spacing: 0.5px; text-transform: uppercase;
  color: var(--subtext);
  padding: 8px 2px 3px;
  border-top: 1px solid var(--sep-color);
  margin-top: 4px;
}
.group-sep:first-child { border-top: none; margin-top: 0; padding-top: 2px; }
.group-sep.fav-sep { color: #ffd700; border-top-color: rgba(255,215,0,0.3); }

/* ── Route card ── */
.route-group {
  margin-bottom: 8px;
  background: var(--card);
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--border);
}
.route-group.dim { opacity: 0.52; }

.route-header {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 11px;
  background: var(--card-header);
}
.route-badge {
  font-size: 14px; font-weight: 800;
  padding: 3px 8px; border-radius: 5px;
  color: #fff; letter-spacing: 0.3px; flex-shrink: 0;
}
.badge-kmb { background: var(--kmb); }
.badge-ctb { background: var(--ctb); }
.badge-nlb { background: var(--nlb); }

.route-dest {
  font-size: 15px; font-weight: 600;
  flex: 1; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* ── Star button ── */
.star-btn {
  background: none; border: none;
  font-size: 18px; line-height: 1;
  cursor: pointer; padding: 0 2px;
  color: var(--subtext); flex-shrink: 0;
  transition: color 0.2s, transform 0.15s;
  filter: grayscale(1) opacity(0.5);
}
.star-btn:hover { transform: scale(1.25); filter: none; color: #ffd700; }
.star-btn.starred { filter: none; color: #ffd700; }

/* ── ETA rows ── */
.eta-rows { padding: 0 11px 8px; }
.eta-row {
  display: flex; align-items: center;
  padding: 5px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14px; gap: 6px;
}
.eta-row:last-child { border-bottom: none; }
.eta-seq { color: var(--subtext); width: 18px; flex-shrink: 0; font-size: 13px; }
.eta-time {
  font-weight: 700; font-size: 17px;
  flex: 1; text-align: right;
  transition: color 0.3s;
}
.eta-time.arriving { color: #f0ad4e; }
.eta-time.now { color: var(--accent); animation: pulse 1s infinite alternate; }
.eta-time.scheduled { color: #4fc3f7; }
[data-theme="light"] .eta-time.scheduled { color: #0070c0; }
.eta-time.dim-time { color: var(--subtext); font-weight: 400; font-size: 15px; }
.eta-remark {
  font-size: 12px; color: var(--subtext);
  max-width: 100px; text-align: right;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex-shrink: 0;
}

@keyframes pulse { from { opacity: 1; } to { opacity: 0.5; } }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Status bar ── */
#status-bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 3px 12px 5px;
  border-top: 1px solid var(--border);
  font-size: 11px; color: var(--subtext);
  flex-shrink: 0; gap: 8px;
}
#status-left { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#status-right { flex-shrink: 0; display: flex; align-items: center; gap: 5px; }
.s-live { display: inline-flex; align-items: center; gap: 3px; color: #4caf50; font-weight: 600; }
.s-live::before {
  content: ''; width: 6px; height: 6px; border-radius: 50%;
  background: #4caf50; animation: livepulse 1.4s ease-in-out infinite;
}
@keyframes livepulse { 0%,100%{ opacity:1; } 50%{ opacity:0.3; } }
.s-cached { color: #f0ad4e; font-weight: 600; }
.s-err { color: var(--accent); font-weight: 600; }

/* ── Toast ── */
#locate-status {
  position: fixed;
  bottom: calc(var(--panel-h) + 10px);
  left: 50%; transform: translateX(-50%);
  background: var(--toast-bg); color: var(--toast-color);
  font-size: 14px; padding: 7px 18px; border-radius: 20px;
  z-index: 1300; pointer-events: none;
  transition: opacity 0.3s; white-space: nowrap;
}
#locate-status.hidden { opacity: 0; }

/* ── Stop markers ── */
.stop-marker {
  width: 10px; height: 10px; border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 0 4px rgba(0,0,0,0.45);
  cursor: pointer; transition: transform 0.15s;
}
[data-theme="light"] .stop-marker { border-color: rgba(255,255,255,0.9); }
.stop-marker.kmb { background: var(--kmb); }
.stop-marker.ctb { background: var(--ctb); }
.stop-marker.nlb { background: var(--nlb); }
.stop-marker.multi { background: linear-gradient(135deg, var(--kmb) 50%, var(--ctb) 50%); }
.stop-marker.active {
  animation: stop-breathe 1.3s ease-in-out infinite;
  z-index: 999 !important;
}
@keyframes stop-breathe {
  0%, 100% { transform: scale(1.8); box-shadow: 0 0 10px rgba(233,69,96,0.85); }
     50%   { transform: scale(2.3); box-shadow: 0 0 20px rgba(233,69,96,1.0); }
}

/* ── Route search highlight ── */
.stop-marker.search-hit {
  background: #ffd700 !important;
  border-color: rgba(255,255,255,0.9);
  transform: scale(1.5);
  box-shadow: 0 0 8px rgba(255,215,0,0.85);
  z-index: 500;
}
.stop-marker.search-hit.active { transform: scale(2); }

/* Global search result markers (outside nearby radius) */
.stop-marker-search-global {
  width: 10px; height: 10px; border-radius: 50%;
  background: #ffd700;
  border: 2px solid rgba(255,255,255,0.85);
  box-shadow: 0 0 6px rgba(255,215,0,0.8);
  cursor: pointer;
}

.user-marker {
  width: 14px; height: 14px; background: #4fc3f7;
  border-radius: 50%; border: 3px solid #fff;
  box-shadow: 0 0 10px rgba(79,195,247,0.75);
}

/* scrollbar */
#eta-list-wrap::-webkit-scrollbar { width: 4px; }
#eta-list-wrap::-webkit-scrollbar-track { background: transparent; }
#eta-list-wrap::-webkit-scrollbar-thumb { background: var(--scrollbar); border-radius: 2px; }

@media (max-height: 600px) { :root { --panel-h: 45vh; } }
