:root {
  --bg: #eef0f3;
  --glass: rgba(255, 255, 255, 0.78);
  --glass-border: rgba(255, 255, 255, 0.6);
  --text: #111418;
  --muted: #5d6673;
  --line: rgba(17, 20, 24, 0.08);
  --ok: #1a9e55;
  --late: #e07b00;
  --bad: #d7263d;
  --accent: #0a84ff;
  --shadow: 0 10px 40px rgba(16, 24, 40, 0.18);
  --radius: 22px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #0f1115;
    --glass: rgba(28, 31, 38, 0.8);
    --glass-border: rgba(255, 255, 255, 0.08);
    --text: #f3f5f8;
    --muted: #9aa4b2;
    --line: rgba(255, 255, 255, 0.08);
    --shadow: 0 10px 40px rgba(0, 0, 0, 0.45);
  }
}
:root[data-theme='dark'] {
  --bg: #0f1115;
  --glass: rgba(28, 31, 38, 0.8);
  --glass-border: rgba(255, 255, 255, 0.08);
  --text: #f3f5f8;
  --muted: #9aa4b2;
  --line: rgba(255, 255, 255, 0.08);
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text); font-family: Inter, system-ui, -apple-system, 'Segoe UI', sans-serif; -webkit-font-smoothing: antialiased; }
#map { position: fixed; inset: 0; }
.hidden { display: none !important; }

.glass {
  background: var(--glass);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(22px) saturate(1.6);
  -webkit-backdrop-filter: blur(22px) saturate(1.6);
  box-shadow: var(--shadow);
  border-radius: var(--radius);
}
.eyebrow { font-size: 11px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }

/* Cabecera */
.top { position: fixed; top: max(12px, env(safe-area-inset-top)); left: 16px; right: 16px; max-width: 440px; padding: 10px 12px 10px 10px; display: flex; align-items: center; justify-content: space-between; gap: 10px; z-index: 3; }
.station-row { display: flex; align-items: center; gap: 10px; min-width: 0; }
.station-icon { width: 40px; height: 40px; border-radius: 12px; background: var(--text); display: grid; place-items: center; flex: none; }
.station-icon svg { width: 22px; height: 22px; fill: var(--bg); }
.station-text { display: flex; flex-direction: column; min-width: 0; }
#station-select { appearance: none; border: 0; background: transparent; color: var(--text); font: 700 19px/1.2 Inter, system-ui, sans-serif; padding: 0 18px 0 0; max-width: 220px; cursor: pointer; background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%); background-position: right 6px top 10px, right 1px top 10px; background-size: 5px 5px; background-repeat: no-repeat; text-overflow: ellipsis; }
#station-select option { color: #111; }
.pill { display: flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; color: var(--muted); white-space: nowrap; }
.pill .dot { width: 8px; height: 8px; border-radius: 50%; background: #9aa4b2; }
.pill.ok .dot { background: var(--ok); box-shadow: 0 0 0 0 rgba(26, 158, 85, 0.6); animation: pulse 2s infinite; }
.pill.err .dot { background: var(--bad); }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(26, 158, 85, 0.55); } 70% { box-shadow: 0 0 0 8px rgba(26, 158, 85, 0); } 100% { box-shadow: 0 0 0 0 rgba(26, 158, 85, 0); } }

/* Tren seleccionado */
.focus { position: fixed; top: calc(max(12px, env(safe-area-inset-top)) + 72px); left: 16px; right: 16px; max-width: 440px; padding: 14px; z-index: 3; }
.focus-head { display: flex; align-items: center; gap: 10px; }
.focus-title { flex: 1; min-width: 0; }
.dest { font-weight: 700; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.meta { font-size: 13px; color: var(--muted); margin-top: 2px; }
.icon-btn { width: 32px; height: 32px; border-radius: 50%; border: 0; background: var(--line); color: var(--text); font-size: 14px; cursor: pointer; flex: none; }
.focus-main { display: flex; align-items: flex-end; justify-content: space-between; margin: 12px 2px 12px; gap: 12px; }
.eta { font-size: 44px; font-weight: 800; letter-spacing: -0.03em; font-variant-numeric: tabular-nums; line-height: 1; margin-top: 4px; }
.focus-stats { display: flex; gap: 16px; text-align: right; }
.focus-stats span { display: block; font-weight: 700; font-size: 17px; font-variant-numeric: tabular-nums; }
.focus-stats small { color: var(--muted); font-size: 11px; }
.seg { display: grid; grid-template-columns: 1fr 1fr; background: var(--line); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { border: 0; background: transparent; color: var(--text); font: 600 13px Inter, system-ui, sans-serif; padding: 8px; border-radius: 9px; cursor: pointer; }
.seg button.active { background: var(--glass); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.12); }

.badge { display: inline-flex; align-items: center; justify-content: center; min-width: 44px; height: 28px; padding: 0 8px; border-radius: 8px; color: #fff; font-weight: 800; font-size: 14px; letter-spacing: 0.01em; flex: none; text-shadow: 0 1px 1px rgba(0, 0, 0, 0.25); }

.recenter { position: fixed; right: 16px; bottom: calc(46vh + 12px); z-index: 3; border: 0; padding: 10px 14px; font: 600 13px Inter, system-ui, sans-serif; color: var(--accent); cursor: pointer; border-radius: 999px; }

/* Botón de relieve 3D */
.relief-btn { position: fixed; right: 16px; top: max(12px, env(safe-area-inset-top)); z-index: 3; width: 46px; height: 46px; border-radius: 14px; border: 0; cursor: pointer; display: grid; place-items: center; padding: 0; }
.relief-btn svg { width: 24px; height: 24px; fill: var(--muted); }
.relief-btn.on { background: var(--text); }
.relief-btn.on svg { fill: var(--bg); }
@media (max-width: 520px) { .relief-btn { top: calc(max(12px, env(safe-area-inset-top)) + 70px); } }

/* Diagnóstico (?debug=1) */
.debug-box { position: fixed; right: 8px; bottom: 30px; z-index: 5; background: rgba(0, 0, 0, 0.75); color: #9be7b4; font: 600 11px ui-monospace, Consolas, monospace; padding: 6px 9px; border-radius: 8px; max-width: 70vw; }

/* Hoja inferior */
.sheet { position: fixed; left: 16px; right: 16px; bottom: max(12px, env(safe-area-inset-bottom)); max-width: 440px; max-height: 42vh; display: flex; flex-direction: column; padding: 8px 6px 8px; z-index: 2; }
.sheet-handle { width: 38px; height: 5px; border-radius: 3px; background: var(--line); margin: 0 auto 6px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 0 10px; }
.sheet-head h2 { margin: 4px 0 6px; font-size: 18px; font-weight: 800; letter-spacing: -0.01em; }
.alerts-chip { font-size: 12px; font-weight: 700; color: var(--late); background: rgba(224, 123, 0, 0.12); padding: 4px 9px; border-radius: 999px; cursor: pointer; }
.alert-box { margin: 0 8px 6px; padding: 10px 12px; font-size: 12.5px; line-height: 1.4; background: rgba(224, 123, 0, 0.1); border-radius: 12px; max-height: 120px; overflow: auto; }
.alert-box p { margin: 0 0 6px; }
.departures { list-style: none; margin: 0; padding: 0 4px; overflow-y: auto; }
.dep { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 10px; padding: 10px 8px; border-radius: 14px; cursor: pointer; }
.dep + .dep { border-top: 1px solid var(--line); }
.dep:hover { background: var(--line); }
.dep.selected { background: rgba(10, 132, 255, 0.12); }
.dep .info { min-width: 0; }
.dep .to { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dep .sub { font-size: 12.5px; color: var(--muted); margin-top: 2px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.dep .ontime { color: var(--ok); font-weight: 600; }
.dep .late { color: var(--late); font-weight: 600; }
.dep .cancel { color: var(--bad); font-weight: 700; }
.dep .right { text-align: right; }
.dep .mins { font-size: 20px; font-weight: 800; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.dep .mins small { font-size: 12px; font-weight: 600; color: var(--muted); margin-left: 2px; }
.btn3d { margin-top: 4px; border: 0; background: var(--text); color: var(--bg); font: 700 11.5px Inter, system-ui, sans-serif; padding: 5px 9px; border-radius: 999px; cursor: pointer; }
.btn3d[disabled] { background: var(--line); color: var(--muted); cursor: default; }
.livedot { width: 7px; height: 7px; border-radius: 50%; background: var(--ok); display: inline-block; }
.platform { font-weight: 700; color: var(--text); }
.credits { margin: 6px 10px 2px; font-size: 10.5px; color: var(--muted); }
.empty { padding: 18px 10px; color: var(--muted); font-size: 14px; }

/* Etiqueta flotante del tren seleccionado */
/* Recuadro compacto sobre el tren elegido */
.train-tag { position: relative; display: inline-flex; gap: 6px; align-items: center; width: max-content; max-width: 280px; background: rgba(17, 20, 24, 0.92); color: #fff; font: 700 12px Inter, system-ui, sans-serif; padding: 4px 9px 4px 4px; border-radius: 10px; white-space: nowrap; box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3); pointer-events: none; }
.train-tag .line { color: #fff; font-weight: 800; font-size: 11px; padding: 2px 6px; border-radius: 6px; text-shadow: 0 1px 1px rgba(0, 0, 0, 0.3); }
.train-tag b { font-weight: 800; color: #9be7b4; }
.train-tag .sub { font-size: 10px; font-weight: 600; color: #c9d1db; background: rgba(255, 255, 255, 0.14); padding: 1px 6px; border-radius: 999px; }
.train-tag::after { content: ''; position: absolute; left: 50%; bottom: -5px; transform: translateX(-50%); border: 5px solid transparent; border-top-color: rgba(17, 20, 24, 0.92); border-bottom: 0; }

/* Viendo un tren: el mapa 3D manda. La lista se oculta y la ficha del tren baja. */
body.viewing .sheet { display: none; }
body.viewing .focus { top: auto; bottom: max(12px, env(safe-area-inset-bottom)); }
body.viewing .recenter { bottom: 210px; }

@media (min-width: 900px) {
  body.viewing .sheet { display: flex; }
  body.viewing .focus { top: calc(max(12px, env(safe-area-inset-top)) + 72px); bottom: auto; }
  .sheet { bottom: 16px; max-height: calc(100vh - 340px); }
  body.viewing .sheet { max-height: calc(100vh - 380px); }
  .recenter { bottom: 24px; }
}
@media (max-width: 380px) {
  .eta { font-size: 36px; }
  .pill span#live-text { display: none; }
}
.maplibregl-ctrl-attrib { font-size: 10px; }
