:root {
  --bg: #0d1117;
  --surface: rgba(22, 27, 34, 0.94);
  --surface-2: #1c2330;
  --line: rgba(255, 255, 255, 0.08);
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #ffb400;
  --ok: #3ddc84;
  --warn: #ffd23f;
  --late: #ff8c42;
  --bad: #ff4d4d;
  --radius: 12px;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
  --mono: 'JetBrains Mono', ui-monospace, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text);
  font: 14px/1.4 Inter, system-ui, -apple-system, Segoe UI, sans-serif; -webkit-font-smoothing: antialiased; }
#map { position: absolute; inset: 0; }
button { font: inherit; color: inherit; cursor: pointer; }
a { color: var(--accent); }

/* ---------- barra superior ---------- */
.topbar { position: absolute; top: 12px; left: 12px; z-index: 5; display: flex; gap: 8px; align-items: center; }
.brand { display: flex; align-items: center; gap: 8px; height: 42px; padding: 0 14px; border-radius: var(--radius);
  background: var(--surface); box-shadow: var(--shadow); color: var(--accent); font-weight: 600; letter-spacing: .2px;
  backdrop-filter: blur(8px); }
.brand span { color: var(--text); font-size: 16px; }
.brand b { color: var(--accent); }
.search { position: relative; display: flex; align-items: center; gap: 8px; height: 42px; width: 320px; padding: 0 12px;
  border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); color: var(--muted); backdrop-filter: blur(8px); }
.search input { flex: 1; min-width: 0; height: 100%; border: 0; outline: 0; background: transparent; color: var(--text); font: inherit; }
.search:focus-within { outline: 1px solid rgba(255, 180, 0, .5); }
#search-results { position: absolute; top: 48px; left: 0; right: 0; margin: 0; padding: 6px; list-style: none;
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); max-height: 60vh; overflow: auto; }
#search-results li { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; cursor: pointer; color: var(--text); }
#search-results li[aria-selected="true"], #search-results li:hover { background: var(--surface-2); }
#search-results .sub { color: var(--muted); font-size: 12px; }
#search-results .empty { color: var(--muted); cursor: default; }

/* ---------- toolbar derecha ---------- */
.toolbar { position: absolute; top: 12px; right: 12px; z-index: 5; display: flex; gap: 8px; align-items: center; }
.stats { display: flex; align-items: center; gap: 10px; height: 42px; padding: 0 14px; border-radius: var(--radius);
  background: var(--surface); box-shadow: var(--shadow); color: var(--muted); font-size: 13px; white-space: nowrap; backdrop-filter: blur(8px); }
.stats b { color: var(--text); font-family: var(--mono); font-weight: 600; }
.stats .sep { width: 1px; height: 16px; background: var(--line); }
.stats .upd::before { content: ''; display: inline-block; width: 7px; height: 7px; margin-right: 6px; border-radius: 50%;
  background: var(--ok); box-shadow: 0 0 0 3px rgba(61, 220, 132, .2); animation: pulse 2s infinite; vertical-align: 1px; }
.stats.error .upd::before { background: var(--bad); animation: none; box-shadow: none; }
@keyframes pulse { 50% { box-shadow: 0 0 0 6px rgba(61, 220, 132, 0); } }
.btn-group { display: flex; gap: 8px; }
.tbtn { width: 42px; height: 42px; display: grid; place-items: center; border: 0; border-radius: var(--radius);
  background: var(--surface); box-shadow: var(--shadow); color: var(--text); }
.tbtn:hover, .tbtn[aria-expanded="true"] { color: var(--accent); }

/* ---------- filtros ---------- */
.filters { position: absolute; top: 62px; right: 12px; z-index: 6; width: 300px; padding: 14px 16px 16px; border-radius: var(--radius);
  background: var(--surface); box-shadow: var(--shadow); backdrop-filter: blur(8px); }
.filters h3 { margin: 12px 0 8px; font-size: 11px; font-weight: 600; letter-spacing: .8px; text-transform: uppercase; color: var(--muted); }
.filters h3:first-child { margin-top: 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 5px 10px; border: 1px solid var(--line); border-radius: 999px;
  background: transparent; font-size: 12.5px; }
.chip i { width: 9px; height: 9px; border-radius: 50%; }
.chip small { color: var(--muted); font-family: var(--mono); }
.chip.off { opacity: .4; }
.chip.off i { background: transparent !important; outline: 1px solid var(--muted); }
.seg { display: inline-flex; padding: 3px; border-radius: 9px; background: var(--surface-2); }
.seg button { border: 0; padding: 5px 14px; border-radius: 7px; background: transparent; color: var(--muted); }
.seg button.on { background: #2d3644; color: var(--text); }
.switch { display: flex; align-items: center; gap: 8px; margin: 6px 0; cursor: pointer; }
.switch input { accent-color: var(--accent); }
.legend-delay { display: none; flex-wrap: wrap; gap: 10px; margin-top: 12px; font-size: 12px; color: var(--muted); }
.legend-delay.show { display: flex; }
.legend-delay i { display: inline-block; width: 9px; height: 9px; margin-right: 5px; border-radius: 50%; }

/* ---------- panel de detalle ---------- */
.panel { position: absolute; z-index: 7; top: 66px; left: 12px; bottom: 36px; width: 370px; display: flex; flex-direction: column;
  border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); overflow: hidden; backdrop-filter: blur(8px);
  animation: slidein .18s ease-out; }
@keyframes slidein { from { transform: translateX(-12px); opacity: 0; } }
.panel-grip { display: none; }
#panel-body { flex: 1; overflow: auto; }
.p-head { position: relative; padding: 16px 18px 14px; background: linear-gradient(135deg, var(--pc, #333) 0%, color-mix(in srgb, var(--pc, #333) 55%, #000) 100%); }
.p-head .close { position: absolute; top: 10px; right: 10px; width: 30px; height: 30px; border: 0; border-radius: 8px;
  background: rgba(0, 0, 0, .25); display: grid; place-items: center; }
.p-head .close:hover { background: rgba(0, 0, 0, .45); }
.p-badges { display: flex; gap: 6px; align-items: center; margin-bottom: 4px; }
.badge { display: inline-block; white-space: nowrap; padding: 2px 8px; border-radius: 6px; font-size: 12px; font-weight: 600; background: rgba(0, 0, 0, .28); }
.badge.line { font-family: var(--mono); }
.p-num { font: 600 28px/1.1 var(--mono); letter-spacing: .5px; }
.p-num small { font: 500 13px Inter, system-ui, sans-serif; opacity: .8; letter-spacing: 0; }
.p-sub { margin-top: 2px; font-size: 13px; opacity: .85; }
.p-route { display: grid; grid-template-columns: 1fr auto 1fr; gap: 10px; align-items: center; padding: 16px 18px; border-bottom: 1px solid var(--line); }
.p-route .st { font-size: 15px; font-weight: 600; line-height: 1.2; }
.p-route .st small { display: block; margin-top: 3px; color: var(--muted); font-size: 11px; font-weight: 500; letter-spacing: .6px; text-transform: uppercase; }
.p-route .st.to { text-align: right; }
.p-route .arrow { color: var(--muted); }
.p-progress { padding: 0 18px 16px; border-bottom: 1px solid var(--line); }
.bar { position: relative; height: 4px; margin: 10px 0 6px; border-radius: 2px; background: #2d3644; }
.bar > span { position: absolute; inset: 0 auto 0 0; border-radius: 2px; background: var(--pc, var(--accent)); }
.bar > i { position: absolute; top: 50%; width: 12px; height: 12px; margin: -6px 0 0 -6px; border-radius: 50%; background: #fff;
  box-shadow: 0 0 0 3px var(--pc, var(--accent)); }
.bar-labels { display: flex; justify-content: space-between; color: var(--muted); font-size: 12px; font-family: var(--mono); }
.p-grid { display: grid; grid-template-columns: 1fr 1fr; border-bottom: 1px solid var(--line); }
.p-grid > div { padding: 12px 18px; border-top: 1px solid var(--line); }
.p-grid > div:nth-child(odd) { border-right: 1px solid var(--line); }
.p-grid > div:nth-child(-n+2) { border-top: 0; }
.p-grid label { display: block; margin-bottom: 3px; color: var(--muted); font-size: 11px; font-weight: 500; letter-spacing: .6px; text-transform: uppercase; }
.p-grid .v { font: 600 16px/1.2 var(--mono); }
.p-grid .v.txt { font-family: inherit; font-size: 14px; }
.delay-ok { color: var(--ok); } .delay-warn { color: var(--warn); } .delay-late { color: var(--late); } .delay-bad { color: var(--bad); }
.p-actions { display: flex; gap: 8px; padding: 12px 18px; border-bottom: 1px solid var(--line); }
.p-actions button { flex: 1; display: inline-flex; align-items: center; justify-content: center; gap: 6px; height: 36px;
  border: 1px solid var(--line); border-radius: 9px; background: var(--surface-2); font-size: 13px; }
.p-actions button.on { border-color: var(--accent); color: var(--accent); }
.p-stops { padding: 8px 18px 18px; }
.p-stops h4 { margin: 8px 0 10px; font-size: 11px; font-weight: 600; letter-spacing: .8px; text-transform: uppercase; color: var(--muted); }
.stop { position: relative; display: grid; grid-template-columns: 18px 1fr auto; gap: 10px; align-items: start; padding: 5px 0; cursor: pointer; }
.stop::before { content: ''; position: absolute; left: 8px; top: 0; bottom: 0; width: 2px; background: #2d3644; }
.stop:first-child::before { top: 12px; } .stop:last-child::before { bottom: calc(100% - 12px); }
.stop.passed::before { background: var(--pc, var(--accent)); }
.stop .dot { position: relative; z-index: 1; width: 10px; height: 10px; margin: 4px 0 0 4px; border-radius: 50%; background: var(--surface); border: 2px solid #4a5566; }
.stop.passed .dot { background: var(--pc, var(--accent)); border-color: var(--pc, var(--accent)); }
.stop.next .dot { width: 14px; height: 14px; margin: 2px 0 0 2px; border-color: #fff; background: var(--pc, var(--accent)); box-shadow: 0 0 0 4px rgba(255,255,255,.12); }
.stop .nm { font-size: 13.5px; }
.stop.passed .nm { color: var(--muted); }
.stop.next .nm { font-weight: 600; }
.stop .tm { font: 500 12.5px var(--mono); color: var(--muted); text-align: right; }
.stop.next .tm { color: var(--text); }
.p-note { padding: 14px 18px; color: var(--muted); font-size: 12.5px; }
.p-prov { padding: 9px 18px; font-size: 12.5px; border-bottom: 1px solid var(--line); }
.p-prov.real { color: var(--muted); }
.p-prov.est { background: rgba(255, 180, 0, .12); color: var(--text); border-left: 3px solid var(--accent); }
.p-prov.est b { display: block; color: var(--accent); margin-bottom: 2px; }
.p-prov small { display: block; margin-top: 4px; color: var(--muted); }
.est-tag { color: var(--accent); font-weight: 600; font-size: 11.5px; }
.p-list .item { display: grid; grid-template-columns: auto 1fr auto; gap: 10px; align-items: center; padding: 10px 18px; border-bottom: 1px solid var(--line); cursor: pointer; }
.p-list .item:hover { background: var(--surface-2); }
.p-list .item b { font-family: var(--mono); }

/* ---------- pie ---------- */
.attrib { position: absolute; bottom: 6px; left: 12px; z-index: 4; font-size: 11px; color: var(--muted);
  background: rgba(13, 17, 23, .7); padding: 3px 8px; border-radius: 6px; }
.maplibregl-ctrl-attrib { background: rgba(13, 17, 23, .7) !important; color: var(--muted); font-size: 10.5px; }
.maplibregl-ctrl-attrib a { color: var(--muted); }
.maplibregl-ctrl-group { background: var(--surface) !important; box-shadow: var(--shadow) !important; border-radius: 10px !important; }
.maplibregl-ctrl-group button + button { border-top-color: var(--line) !important; }
.maplibregl-ctrl-group button .maplibregl-ctrl-icon { filter: invert(1) brightness(.85); }
.maplibregl-popup-content { background: var(--surface); color: var(--text); border-radius: 8px; padding: 6px 10px; box-shadow: var(--shadow); font-size: 12.5px; }
.maplibregl-popup-tip { border-top-color: var(--surface) !important; border-bottom-color: var(--surface) !important; }
.hover-pop b { font-family: var(--mono); }
.hover-pop .sub { color: var(--muted); }

/* ---------- foto y material ---------- */
.p-photo { position: relative; margin: 0; aspect-ratio: 16 / 9; background: #0a0e14; overflow: hidden; }
.p-photo img { display: block; width: 100%; height: 100%; object-fit: cover; }
.p-photo.loading { display: grid; place-items: center; color: var(--muted); font-size: 12px;
  background: linear-gradient(90deg, #121821 25%, #1a2230 50%, #121821 75%); background-size: 200% 100%; animation: shimmer 1.4s infinite linear; }
@keyframes shimmer { to { background-position: -200% 0; } }
.p-photo figcaption { position: absolute; left: 0; right: 0; bottom: 0; padding: 18px 10px 6px; font-size: 10.5px; color: #d0d7de;
  background: linear-gradient(transparent, rgba(0,0,0,.75)); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.p-photo figcaption a { color: #fff; }
.ph-exact { position: absolute; top: 10px; left: 10px; padding: 3px 8px; border-radius: 6px; background: var(--accent); color: #111; font-size: 11px; font-weight: 700; }
.ph-exact.ph-ref { background: rgba(17, 17, 17, .72); color: #fff; font-weight: 600; }
.ph-count { position: absolute; top: 10px; right: 10px; padding: 2px 7px; border-radius: 6px; background: rgba(0,0,0,.55); font: 500 11px var(--mono); }
.ph-nav { position: absolute; top: 50%; width: 34px; height: 34px; margin-top: -17px; border: 0; border-radius: 50%; background: rgba(0,0,0,.5);
  color: #fff; font-size: 22px; line-height: 1; display: grid; place-items: center; opacity: .85; }
.ph-nav:hover { opacity: 1; background: rgba(0,0,0,.7); }
.ph-nav.prev { left: 8px; } .ph-nav.next { right: 8px; }
.p-model { padding: 12px 18px 4px; border-bottom: 1px solid var(--line); }
.p-model h4 { margin: 0 0 8px; font-size: 11px; font-weight: 600; letter-spacing: .8px; text-transform: uppercase; color: var(--muted); }
.p-model dl { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0 0 10px; font-size: 13px; }
.p-model dt { color: var(--muted); }
.p-model dd { margin: 0; }
.legend-rail { display: flex; flex-wrap: wrap; gap: 10px; margin: 0 0 8px 24px; font-size: 12px; color: var(--muted); }
.legend-rail i { display: inline-block; width: 14px; height: 3px; margin-right: 5px; border-radius: 2px; vertical-align: 3px; }
body.on-photo .stats, body.on-photo .brand, body.on-photo .search, body.on-photo .tbtn { background: rgba(13,17,23,.88); }

.toast { position: absolute; z-index: 9; top: 66px; left: 50%; transform: translateX(-50%); padding: 8px 14px; border-radius: 999px;
  background: var(--surface); box-shadow: var(--shadow); font-size: 13px; font-weight: 600; pointer-events: none; }

/* ---------- leyenda ---------- */
.legend { position: absolute; z-index: 4; right: 54px; bottom: 12px; width: 236px; max-height: calc(100dvh - 90px); overflow: auto;
  padding: 12px 14px 10px; border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); backdrop-filter: blur(8px); font-size: 12.5px; }
.legend h4 { margin: 10px 0 6px; font-size: 10.5px; font-weight: 600; letter-spacing: .8px; text-transform: uppercase; color: var(--muted); }
.legend h4:first-child { margin-top: 0; }
.legend ul { margin: 0; padding: 0; list-style: none; display: grid; gap: 4px; }
.legend li { display: flex; align-items: center; gap: 8px; }
.legend li svg { flex: none; }
.legend ul.closures li { display: block; color: #f0b429; }
.legend .hint { margin: 4px 0 0; color: var(--muted); font-size: 11.5px; }
.legend-close { position: absolute; top: 4px; right: 6px; width: 26px; height: 26px; border: 0; border-radius: 6px; background: transparent;
  color: var(--muted); font-size: 18px; line-height: 1; }
.legend-close:hover { color: var(--text); }

/* ---------- móvil ---------- */
@media (max-width: 420px) {
  /* En pantallas estrechas el % de puntualidad cede su sitio a los botones. */
  .stats > span:nth-child(3), .stats > span:nth-child(4) { display: none; }
}
@media (max-width: 720px) {
  .topbar { left: 8px; right: 8px; top: max(8px, env(safe-area-inset-top)); }
  .brand span { display: none; }
  .search { flex: 1; width: auto; }
  .toolbar { top: calc(max(8px, env(safe-area-inset-top)) + 50px); right: 8px; left: 8px; justify-content: space-between; }
  .stats { height: 36px; font-size: 12px; gap: 8px; padding: 0 10px; }
  .tbtn { width: 36px; height: 36px; }
  .btn-group { gap: 6px; }
  .stats { min-width: 0; flex: 0 1 auto; overflow: hidden; }
  .btn-group { flex: none; }
  .toast { top: calc(max(8px, env(safe-area-inset-top)) + 96px); }
  .filters { top: calc(max(8px, env(safe-area-inset-top)) + 94px); left: 8px; right: 8px; width: auto; max-height: calc(100dvh - 180px); overflow: auto; }
  .legend { z-index: 6; top: calc(max(8px, env(safe-area-inset-top)) + 94px); right: 8px; bottom: auto; width: 250px; max-height: calc(100dvh - 180px); }
  .search input { font-size: 16px; } /* evita el zoom automático de iOS */
  .maplibregl-ctrl-bottom-right { bottom: env(safe-area-inset-bottom); }
  /* Ficha inferior arrastrable: medio abierta (peek) o completa (full). */
  .panel { top: auto; left: 0; right: 0; bottom: 0; width: auto; height: 88dvh; max-height: none; border-radius: 16px 16px 0 0;
    padding-bottom: env(safe-area-inset-bottom); animation: none; transition: transform .25s cubic-bezier(.2,.8,.2,1);
    transform: translateY(calc(88dvh - var(--peek, 46dvh))); touch-action: pan-y; }
  .panel.full { transform: translateY(0); }
  .panel.dragging { transition: none; }
  .panel:not(.full) #panel-body { overflow: hidden; }
  .panel-grip { display: block; position: absolute; top: 0; left: 0; right: 0; height: 22px; z-index: 3; margin: 0; background: none; cursor: grab; touch-action: none; }
  .panel-grip::after { content: ''; position: absolute; top: 7px; left: 50%; width: 40px; height: 4px; margin-left: -20px; border-radius: 2px; background: rgba(255,255,255,.45); }
  .p-head { touch-action: none; padding-top: 20px; }
  .p-head .close { top: 16px; }
  .p-photo { aspect-ratio: 2 / 1; }
  .p-grid .v { font-size: 15px; }
  .p-actions button { height: 42px; }
  .attrib { display: none; }
}
