/* DondeBus — Estilo "Accessible & Ethical" (UI/UX Pro Max).
   Alto contraste, tokens semánticos, light + dark, foco visible, 44px touch. */

:root {
  /* Paleta transporte (light) */
  --primary: #2563eb;
  --primary-strong: #1d4ed8;
  --on-primary: #ffffff;
  --secondary: #0891b2;
  --accent: #ea580c;
  --danger: #dc2626;
  --live: #059669;

  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-2: #f1f5fd;
  --fg: #0f172a;
  --muted-fg: #475569;
  --border: #e4ecfc;
  --ring: #2563eb;

  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow: 0 4px 16px rgba(15, 23, 42, 0.10);
  --shadow-lg: 0 -6px 28px rgba(15, 23, 42, 0.16);

  --r-sm: 10px; --r: 14px; --r-lg: 18px; --pill: 999px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --font: "Public Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --primary: #3b82f6;
    --primary-strong: #60a5fa;
    --secondary: #22d3ee;
    --accent: #fb923c;
    --live: #34d399;

    --bg: #0b1220;
    --surface: #131c31;
    --surface-2: #1b2740;
    --fg: #e7edf7;
    --muted-fg: #94a3b8;
    --border: #263349;
    --ring: #60a5fa;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow: 0 4px 16px rgba(0, 0, 0, 0.5);
    --shadow-lg: 0 -6px 28px rgba(0, 0, 0, 0.55);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  color: var(--fg);
  background: var(--bg);
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
}

:focus-visible { outline: 3px solid var(--ring); outline-offset: 2px; border-radius: 6px; }
button { font-family: inherit; }

.skip-link {
  position: absolute; left: -999px; top: 8px; z-index: 100;
  background: var(--primary); color: var(--on-primary); padding: 10px 14px; border-radius: 8px;
}
.skip-link:focus { left: 8px; }

#app { display: flex; flex-direction: column; height: 100dvh; }

/* ---- Barra superior ---- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(var(--safe-top) + 10px) 16px 10px;
  background: var(--surface); border-bottom: 1px solid var(--border); z-index: 20;
}
.brand { font-weight: 800; font-size: 18px; letter-spacing: -0.02em; display: flex; align-items: center; gap: 8px; }
.brand svg { width: 24px; height: 24px; color: var(--primary); }
.route-pill {
  display: flex; align-items: center; gap: 6px; min-height: 40px;
  background: var(--surface-2); color: var(--primary-strong); border: 1px solid var(--border);
  font-weight: 700; font-size: 13px; padding: 8px 12px; border-radius: var(--pill); cursor: pointer;
  transition: background 0.18s ease, transform 0.18s ease;
}
.route-pill:hover { background: var(--border); }
.route-pill:active { transform: scale(0.97); }

/* ---- Contenido / pestañas ---- */
main { flex: 1; position: relative; overflow: hidden; }
.tab { position: absolute; inset: 0; display: none; overflow: auto; -webkit-overflow-scrolling: touch; }
.tab.active { display: block; }

/* z-index:0 + isolation crean un contexto de apilado propio: así los mosaicos
   y controles internos de Leaflet (z-index 200–1000) quedan contenidos y NO se
   pintan por encima de la barra, las pestañas ni la hoja inferior. */
#map { position: absolute; inset: 0; z-index: 0; isolation: isolate; background: var(--surface-2); }
.leaflet-container { background: var(--surface-2); font: inherit; }

.fab {
  position: absolute; right: 16px; bottom: 18px; z-index: 15;
  width: 48px; height: 48px; border-radius: 50%; border: 1px solid var(--border);
  background: var(--surface); color: var(--primary); box-shadow: var(--shadow);
  display: grid; place-items: center; cursor: pointer; transition: transform 0.15s ease;
}
.fab:hover { color: var(--primary-strong); }
.fab:active { transform: scale(0.92); }

.map-status {
  position: absolute; left: 50%; top: 12px; transform: translateX(-50%);
  z-index: 15; background: var(--surface); box-shadow: var(--shadow); border: 1px solid var(--border);
  padding: 7px 14px; border-radius: var(--pill); font-size: 12.5px; font-weight: 600; color: var(--muted-fg);
  max-width: 82%; text-align: center; transition: opacity 0.3s;
}

/* Marcadores del mapa */
.bus-marker {
  width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center;
  color: #fff; border: 2.5px solid #fff; box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
}
.bus-marker svg { width: 17px; height: 17px; }
.user-dot {
  width: 16px; height: 16px; border-radius: 50%; background: var(--primary);
  border: 3px solid #fff; box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.25);
}

/* ---- Buscar ---- */
.search-wrap { padding: 16px; max-width: 640px; margin: 0 auto; }
.search-box {
  display: flex; align-items: center; gap: 10px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--r); padding: 12px 14px; color: var(--muted-fg);
  transition: border-color 0.18s ease, box-shadow 0.18s ease;
}
.search-box:focus-within { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15); }
.search-box input { flex: 1; border: none; outline: none; font-size: 16px; color: var(--fg); background: transparent; }
.route-list { list-style: none; margin: 16px 0 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.route-item {
  display: flex; align-items: center; gap: 12px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--r); padding: 12px; cursor: pointer;
  min-height: 44px; transition: transform 0.15s ease, border-color 0.18s ease, background 0.18s ease;
  animation: fadeUp 0.4s both; animation-delay: calc(var(--i, 0) * 45ms);
}
.route-item:hover { border-color: var(--primary); background: var(--surface-2); }
.route-item:active { transform: scale(0.99); }
.route-badge { min-width: 54px; padding: 9px 10px; border-radius: var(--r-sm); color: #fff; font-weight: 800; font-size: 12px; text-align: center; }
.route-info { flex: 1; min-width: 0; }
.route-name { font-weight: 700; font-size: 15px; }
.route-meta { font-size: 12.5px; color: var(--muted-fg); margin-top: 2px; }
.chev { color: var(--muted-fg); opacity: 0.5; }

/* ---- Ajustes ---- */
.settings { padding: 16px; max-width: 640px; margin: 0 auto; }
.settings h2 { font-size: 12.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted-fg); margin: 18px 4px 8px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r); overflow: hidden; box-shadow: var(--shadow-sm); }
.row { display: flex; align-items: center; gap: 12px; padding: 14px; width: 100%; min-height: 52px; font-size: 15px; }
.row > svg { width: 20px; height: 20px; color: var(--primary); flex-shrink: 0; }
.row .row-label { flex: 1; }
.row-btn { background: none; border: none; cursor: pointer; color: var(--fg); font: inherit; text-align: left; }
.row-btn .row-label { color: var(--primary); font-weight: 600; }
.divider { height: 1px; background: var(--border); margin-left: 46px; }
.muted { color: var(--muted-fg); font-size: 14px; }
.note { font-size: 12.5px; color: var(--muted-fg); line-height: 1.6; margin: 14px 4px; }

/* Switch accesible */
.switch { position: relative; width: 46px; height: 28px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .track { position: absolute; inset: 0; background: var(--border); border-radius: var(--pill); transition: background 0.2s ease; }
.switch .track::before { content: ""; position: absolute; width: 22px; height: 22px; left: 3px; top: 3px; background: #fff; border-radius: 50%; box-shadow: var(--shadow-sm); transition: transform 0.2s ease; }
.switch input:checked + .track { background: var(--primary); }
.switch input:checked + .track::before { transform: translateX(18px); }
.switch input:focus-visible + .track { outline: 3px solid var(--ring); outline-offset: 2px; }

/* ---- Barra de pestañas ---- */
.tabbar {
  display: flex; background: var(--surface); border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom); z-index: 20;
}
.tabbtn {
  flex: 1; background: none; border: none; cursor: pointer; color: var(--muted-fg);
  display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 9px 0 7px;
  min-height: 56px; font-size: 11px; font-weight: 600; transition: color 0.18s ease;
}
.tabbtn svg { width: 24px; height: 24px; }
.tabbtn.active { color: var(--primary); }
.tabbtn:active { transform: scale(0.94); }

/* ---- Hoja inferior ---- */
.sheet-overlay {
  position: fixed; inset: 0; background: rgba(2, 6, 23, 0.45); z-index: 30;
  opacity: 0; pointer-events: none; transition: opacity 0.25s;
}
.sheet-overlay.open { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 31; margin: 0 auto; max-width: 640px;
  background: var(--surface); border-radius: var(--r-lg) var(--r-lg) 0 0; box-shadow: var(--shadow-lg);
  padding: 8px 18px calc(18px + var(--safe-bottom));
  transform: translateY(110%); transition: transform 0.32s cubic-bezier(0.22, 1, 0.36, 1);
  max-height: 72vh; overflow: auto;
}
.sheet.open { transform: translateY(0); }
.sheet-handle { width: 40px; height: 4px; border-radius: 2px; background: var(--border); margin: 6px auto 12px; }
.sheet-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.sheet-head h3 { margin: 0; font-size: 18px; letter-spacing: -0.01em; }
.sheet-head p { margin: 3px 0 0; font-size: 13px; }
.icon-btn { background: var(--surface-2); border: 1px solid var(--border); width: 40px; height: 40px; border-radius: 50%; cursor: pointer; color: var(--muted-fg); display: grid; place-items: center; }
.icon-btn svg { width: 16px; height: 16px; }
.sheet-body { margin-top: 16px; }

/* Lista de llegadas (ETAs) */
.arrival {
  display: flex; align-items: center; gap: 12px; padding: 13px 0; border-bottom: 1px solid var(--border);
  animation: fadeUp 0.4s both; animation-delay: calc(var(--i, 0) * 50ms);
}
.arrival:last-child { border-bottom: none; }
.arrival-badge { min-width: 48px; padding: 7px 8px; border-radius: var(--r-sm); color: #fff; font-weight: 800; font-size: 12px; text-align: center; }
.arrival-info { flex: 1; min-width: 0; }
.arrival-head { font-weight: 700; font-size: 14px; }
.tag-live, .tag-sched { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; margin-top: 2px; }
.tag-live { color: var(--live); font-weight: 600; }
.tag-sched { color: var(--muted-fg); }
.dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.arrival-eta { text-align: right; }
.arrival-eta b { font-size: 20px; color: var(--primary-strong); font-variant-numeric: tabular-nums; }
.arrival-eta small { display: block; font-size: 11px; color: var(--muted-fg); }
.empty { color: var(--muted-fg); text-align: center; padding: 26px 8px; font-size: 14px; line-height: 1.6; }

/* ---- Tarifas y detalle (parada / bus) ---- */
.fare-card {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--r);
  padding: 14px; margin-bottom: 16px;
}
.fare-head { display: flex; align-items: baseline; justify-content: space-between; font-weight: 700; font-size: 14px; margin-bottom: 10px; }
.muted-sm { color: var(--muted-fg); font-size: 12px; font-weight: 500; }
.tiers { display: flex; gap: 8px; }
.tier { flex: 1; text-align: center; background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-sm); padding: 10px 6px; }
.tier span { display: block; font-size: 11px; color: var(--muted-fg); margin-bottom: 3px; }
.tier b { font-size: 16px; color: var(--primary-strong); font-variant-numeric: tabular-nums; }
.dest-field { display: block; margin-top: 12px; font-size: 13px; font-weight: 600; }
.dest-field select {
  display: block; width: 100%; margin-top: 6px; padding: 11px 12px; font: inherit; font-size: 15px;
  color: var(--fg); background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-sm);
  -webkit-appearance: none; appearance: none;
}
.dest-field select:focus-visible { outline: 3px solid var(--ring); outline-offset: 2px; }
.sub-h { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.03em; color: var(--muted-fg); margin: 4px 0 8px; }

.detail-card { background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--r); padding: 4px 14px; margin-bottom: 16px; }
.detail-row { display: flex; align-items: center; justify-content: space-between; padding: 13px 0; border-bottom: 1px solid var(--border); }
.detail-row:last-child { border-bottom: none; }
.detail-label { font-size: 14px; font-weight: 600; }
.detail-value { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--muted-fg); }
.detail-value .big { font-size: 22px; color: var(--primary-strong); font-variant-numeric: tabular-nums; }

.occ { display: inline-flex; gap: 3px; }
.occ-seg { width: 14px; height: 8px; border-radius: 2px; background: var(--border); }
.occ-seg.on.lvl1 { background: var(--live); }
.occ-seg.on.lvl2 { background: var(--accent); }
.occ-seg.on.lvl3 { background: var(--danger); }
.occ-txt.lvl1 { color: var(--live); }
.occ-txt.lvl2 { color: var(--accent); }
.occ-txt.lvl3 { color: var(--danger); }
.hint-sm { font-size: 12px; color: var(--muted-fg); margin: 8px 0 0; line-height: 1.5; }

/* Planificador de viaje (subís / bajás / parada más cercana) */
.trip-field { margin-bottom: 12px; }
.trip-label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.trip-controls { display: flex; gap: 8px; }
.fare-card select {
  flex: 1; width: 100%; min-height: 44px; padding: 10px 12px; font: inherit; font-size: 15px;
  color: var(--fg); background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-sm);
}
.fare-card select:focus-visible { outline: 3px solid var(--ring); outline-offset: 2px; }
.chip-btn {
  display: inline-flex; align-items: center; gap: 5px; white-space: nowrap;
  background: var(--primary); color: #fff; border: none; border-radius: var(--r-sm);
  padding: 0 14px; min-height: 44px; font: inherit; font-size: 13px; font-weight: 700; cursor: pointer;
}
.chip-btn svg { width: 16px; height: 16px; }
.chip-btn:active { transform: scale(0.96); }
.chip-btn.loading { opacity: 0.6; pointer-events: none; }
.fare-result { margin-top: 8px; padding-top: 12px; border-top: 1px solid var(--border); }
.fare-result .muted-sm { display: block; margin-bottom: 8px; font-weight: 600; color: var(--fg); }

@keyframes fadeUp { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.001ms !important; animation-delay: 0 !important; transition-duration: 0.001ms !important; }
}
