:root {
  --bg: #f6f5f2;
  --panel: #ffffff;
  --text: #1c1c1a;
  --muted: #6b6a66;
  --line: #e4e2dc;
  --chip: #f1efea;
  --chip-2: #e8e6e0;
  --ok: #1f9d55;
  --late: #c97a00;
  --grave: #d23b2f;
  --info: #2f6fd2;
  --nodata: #9a9a9a;
  --accent: #1c1c1a;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 16px rgb(0 0 0 / 6%);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  /* Modo oscuro en negro 100 %: fondo y panel #000; tarjetas en grises muy oscuros para distinguirlas */
  :root:not([data-theme="light"]) {
    --bg: #000000; --panel: #000000; --text: #f2f2f2; --muted: #9a9a9a; --line: #262626; --chip: #141414; --chip-2: #1f1f1f;
    --ok: #3fbf74; --late: #f0a530; --grave: #f0645a; --info: #6ea2f5; --nodata: #77756f; --accent: #f2f2f2;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] { /* negro 100 % */
  --bg: #000000; --panel: #000000; --text: #f2f2f2; --muted: #9a9a9a; --line: #262626; --chip: #141414; --chip-2: #1f1f1f;
  --ok: #3fbf74; --late: #f0a530; --grave: #f0645a; --info: #6ea2f5; --nodata: #77756f; --accent: #f2f2f2;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; cursor: pointer; }
h1 { font-size: 22px; line-height: 1.15; margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 12.5px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 22px 0 10px; font-weight: 600; }
h3 { font-size: 15px; margin: 18px 0 4px; }
code { font-size: 0.9em; background: var(--chip); padding: 1px 4px; border-radius: 4px; }
em { font-style: normal; }

.app { display: grid; grid-template-columns: 420px 1fr; height: 100vh; }
/* Escritorio: título y tarjetas fijos; solo se desplaza la sección de líneas */
.panel { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.panel-top { flex: none; padding: 18px 20px 12px; }
.panel-body { flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 0 20px 24px; }
.mapwrap { position: relative; min-height: 0; }
#map { position: absolute; inset: 0; }

.head { display: flex; justify-content: space-between; gap: 12px; align-items: center; }
.brand { font-size: 30px; font-weight: 800; line-height: 1; letter-spacing: -0.035em; }
.upd { flex: none; font-size: 12px; padding: 3px 8px; border-radius: 999px; background: var(--chip); color: var(--muted); white-space: nowrap; }
.upd::before { content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--ok); margin-right: 6px; vertical-align: 1px; }
.upd.is-stale::before { background: var(--late); }
.upd.is-error::before { background: var(--grave); }

/* ── Tarjetas de compañía (resumen + pestañas) ── */
.ops { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 14px; }
.op { display: grid; gap: 1px; align-content: start; text-align: left; background: var(--chip); border: 2px solid transparent; border-radius: 12px; padding: 8px 12px 9px; min-width: 0; }
.op:hover { border-color: var(--line); }
.op[aria-selected="true"] { background: var(--panel); border-color: var(--accent); }
.op-name { font-size: 13px; font-weight: 700; display: flex; align-items: center; gap: 7px; white-space: nowrap; overflow: hidden; }
.op-name .sdot { margin-left: auto; }
/* Logo de la compañía: cuadrado; el de AVE sobre placa blanca para que se vea también en modo oscuro */
.op-logo { flex: none; display: block; width: 20px; height: 20px; border-radius: 4px; }
.op-logo.tile-tram { background: #008d78; padding: 2px; } /* logo del TRAM en blanco, sobre su verde corporativo */
.op-logo.wide { width: 31px; padding: 2px 3px; background: #fff; object-fit: contain; box-shadow: inset 0 0 0 1px rgb(0 0 0 / 8%); }
.op-main { display: flex; align-items: baseline; gap: 6px; }
.op-main b { font-size: 23px; line-height: 1.1; font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
.op-main span { font-size: 13px; color: var(--muted); }
/* Resumen en una sola línea (si no cabe, «…» y el texto completo al pasar el ratón) */
.op-sub { font-size: 12px; color: var(--muted); line-height: 1.35; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.op-sub em { color: var(--late); font-weight: 600; }
.op-sub em::before { content: " · "; color: var(--muted); font-weight: 400; }
.sdot { flex: none; width: 8px; height: 8px; border-radius: 50%; background: var(--nodata); }
.sdot[data-status="normal"] { background: var(--ok); }
.sdot[data-status="aviso"] { background: var(--info); }
.sdot[data-status="retrasos"] { background: var(--late); }
.sdot[data-status="grave"] { background: var(--grave); }

/* ── Puntos de estado de tren (pastillas, lista, leyenda) ── */
.d { display: inline-block; flex: none; width: 9px; height: 9px; border-radius: 50%; background: var(--nodata); }
.d.s0 { background: var(--ok); }
.d.s1 { background: var(--late); }
.d.s2 { background: var(--grave); }
.d.su { background: transparent; box-shadow: inset 0 0 0 1.5px var(--nodata); }

/* ── Pastilla de línea o corredor ── */
.groups { display: grid; gap: 6px; }
.grp { background: var(--chip); border-radius: 12px; overflow: hidden; }
/* Borde con outline (no box-shadow): se pinta por encima del contenido, así el fondo de la cabecera al pasar el ratón no lo tapa */
.grp.is-open { background: var(--chip); outline: 1px solid var(--line); outline-offset: -1px; }
.grp.is-focus { outline: 2px solid var(--accent); outline-offset: -2px; }
.grp-head { display: grid; grid-template-columns: 46px 1fr auto 14px; gap: 10px; align-items: center; width: 100%; padding: 10px 12px; background: none; border: 0; text-align: left; }
.grp-head.no-lead { grid-template-columns: 1fr auto 14px; }
.grp-head:hover { background: var(--chip-2); }
.grp-main { display: grid; gap: 5px; min-width: 0; }
.grp-title { font-size: 14.5px; font-weight: 650; }
.dots { display: flex; flex-wrap: wrap; gap: 3px; align-items: center; }
.dots .more { font-size: 11.5px; color: var(--muted); margin-left: 3px; }
.grp-fact { font-size: 13px; color: var(--muted); }
.grp-fact em { color: var(--grave); font-weight: 600; }
.chev { width: 8px; height: 8px; border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted); transform: rotate(45deg) translate(-2px, -2px); transition: transform 0.15s; }
.grp.is-open .chev { transform: rotate(225deg) translate(-2px, -2px); }
.badge { display: inline-grid; place-items: center; min-width: 42px; height: 24px; padding: 0 6px; border-radius: 6px; color: #fff; font-weight: 700; font-size: 13px; white-space: nowrap; }

.st { font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.st.normal { color: var(--ok); background: color-mix(in srgb, var(--ok) 14%, transparent); }
.st.retrasos { color: var(--late); background: color-mix(in srgb, var(--late) 16%, transparent); }
.st.grave { color: var(--grave); background: color-mix(in srgb, var(--grave) 15%, transparent); }
.st.aviso { color: var(--info); background: color-mix(in srgb, var(--info) 14%, transparent); }
.st.sin-trenes { color: var(--muted); background: transparent; }

/* Trenes desplegados */
.grp-body { padding: 0 12px 10px; }
.grp-details { margin: 0 0 6px; font-size: 12.5px; color: var(--muted); }
.trains { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--line); }
.tr { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: center; width: 100%; padding: 8px 2px; background: none; border: 0; border-bottom: 1px solid var(--line); text-align: left; }
.trains li:last-child .tr { border-bottom: 0; }
.tr:hover, .tr.is-sel { background: var(--chip-2); }
.tr.is-sel { box-shadow: inset 3px 0 0 var(--accent); padding-left: 8px; }
.tr-main { display: grid; min-width: 0; }
/* Texto normal (no flex) para que «origen TO destino» salte de línea entero, sin partir palabras ni etiquetas */
.tr-title { display: block; font-size: 14px; font-weight: 600; line-height: 1.5; min-width: 0; }
.tr-title .badge { min-width: 0; height: 19px; font-size: 11px; padding: 0 5px; margin-right: 6px; vertical-align: 1px; }
.tr-title .to { margin-right: 2px; }
.tr-title .dest { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tr-sub { display: flex; align-items: center; gap: 10px; min-width: 0; }
.tr-where { flex: 1 1 auto; min-width: 0; font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Ocupación: barra de 5 tramos */
.occ { flex: none; display: inline-flex; align-items: center; gap: 2px; vertical-align: middle; }
.occ-ico { width: 9px; height: 11px; margin-right: 3px; fill: var(--muted); }
.occ i { width: 8px; height: 10px; border-radius: 2px; background: color-mix(in srgb, var(--muted) 28%, transparent); }
.occ i.on { background: var(--info); }
.occ.is-high i.on { background: var(--late); }
.pop-occ { display: flex; align-items: center; gap: 8px; }
.pop-occ span { font-variant-numeric: tabular-nums; }
/* Indicador de estado del tren, como un panel de aeropuerto (fondo oscuro también en modo claro) */
.board { display: inline-block; white-space: nowrap; padding: 3px 7px; border-radius: 4px; background: #000000; font: 700 11.5px/1.2 ui-monospace, "SF Mono", Menlo, Consolas, monospace; letter-spacing: 0.06em; font-variant-numeric: tabular-nums; }
.board.s0 { color: #3fd57b; }
.board.s1 { color: #ffb02e; }
.board.s2 { color: #ff6359; }
.board.su { color: #9c9a94; }
.board, .to { box-shadow: inset 0 0 0 1px rgb(255 255 255 / 10%); } /* se distingue también sobre fondo oscuro */
.tr .board { min-width: 128px; text-align: center; } /* columna fija, como en un panel de salidas */
/* «TO» delante del destino, mismo lenguaje que el indicador */
.to { display: inline-block; flex: none; padding: 3px 5px; border-radius: 3px; background: #000000; color: #f4f3ef; font: 700 10px/1 ui-monospace, "SF Mono", Menlo, Consolas, monospace; letter-spacing: 0.08em; vertical-align: 2px; }

/* Bus Moventis: buscador, marcas, recorridos y próximas salidas */
.search { width: 100%; margin: 18px 0 2px; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--chip); color: var(--text); font: inherit; font-size: 14px; }
.search:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.brand-h { font-size: 12.5px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 16px 0 4px; }
.routes { display: flex; flex-wrap: wrap; gap: 6px; margin: 2px 0 8px; }
.route { border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: 4px 10px; font-size: 12.5px; text-align: left; }
.route.is-on { border-color: var(--accent); font-weight: 650; }
.deps { padding: 2px 2px 10px 10px; }
.deps-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.deps-list li { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: center; }
.deps-list .board { min-width: 96px; text-align: center; }
.deps-list .tr-title { font-weight: 550; font-size: 13.5px; }
.pop .deps-list { margin-top: 6px; }

/* Fila única con las líneas sin trenes */
.idle { display: flex; flex-wrap: wrap; gap: 5px; align-items: center; padding: 8px 2px 0; }
.idle > span:first-child { font-size: 12.5px; color: var(--muted); margin-right: 4px; }
.idle .badge { min-width: 0; height: 20px; font-size: 11.5px; opacity: 0.55; }
.idle-list { display: grid; gap: 3px; }
.idle-item { font-size: 13px; color: var(--muted); }
.idle-item em { color: var(--grave); font-weight: 600; }

/* ── Directo, avisos, carreteras ── */
.events, .alerts, .roads { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.events li { display: grid; grid-template-columns: 40px auto 1fr; gap: 8px; align-items: center; font-size: 13.5px; }
.events time, .alerts time { color: var(--muted); font-size: 12.5px; font-variant-numeric: tabular-nums; }
.events .badge { min-width: 0; height: 20px; font-size: 11.5px; }
.empty { color: var(--muted); font-size: 13.5px; }
.alerts li { background: var(--chip); border-radius: 10px; padding: 10px 12px; font-size: 13.5px; }
.alerts .badges { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; margin-bottom: 6px; }
.alerts .badge { min-width: 0; height: 20px; font-size: 11.5px; }
.alerts time { margin-left: auto; }
/* La columna de la etiqueta se ajusta a la más ancha (subgrid): todas alineadas y nunca en dos líneas */
.roads { grid-template-columns: max-content 1fr; column-gap: 10px; row-gap: 0; }
.roads li { display: grid; grid-column: 1 / -1; grid-template-columns: subgrid; align-items: start; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 14px; }

/* Etiqueta de carretera con el código de color de los hitos kilométricos españoles */
.road { display: inline-block; white-space: nowrap; padding: 2px 7px; border-radius: 4px; font-weight: 700; font-size: 12.5px; line-height: 1.4; letter-spacing: 0.01em; color: #fff; background: #4a4a48; box-shadow: inset 0 0 0 1.5px rgb(255 255 255 / 85%), 0 0 0 1px rgb(0 0 0 / 25%); }
.road.auto { background: #1f4e9c; }              /* azul: autopista o autovía */
.road.n { background: #d7261e; }                 /* rojo: red del Estado */
.road.r1 { background: #ee8419; }                /* naranja: autonómica 1.er orden */
.road.r2 { background: #17913b; }                /* verde: autonómica 2.º orden */
.road.r3 { background: #f6d21b; color: #161616; } /* amarillo: 3.er orden */
.road.eu { background: #17913b; border-radius: 6px; } /* verde: itinerario europeo */
.roads .road { text-align: center; } /* en la lista todas ocupan el ancho de la más larga, como una columna de señales */
.road-legend { display: flex; flex-wrap: wrap; gap: 6px 12px; margin-top: 14px; font-size: 12px; color: var(--muted); }
.road-legend > span { display: inline-flex; align-items: center; gap: 6px; }
.road-legend .road { font-size: 11px; padding: 1px 5px; }
.meta { color: var(--muted); font-size: 12.5px; }
.lvl-5, .lvl-4 { color: var(--grave); font-weight: 600; }
.lvl-3 { color: var(--late); font-weight: 600; }
.note { color: var(--muted); font-size: 13px; margin: 14px 0 0; }

.foot { margin-top: 28px; padding-top: 14px; border-top: 1px solid var(--line); font-size: 12.5px; color: var(--muted); }
.foot p { margin: 0 0 6px; }
.link { background: none; border: 0; padding: 0; color: var(--text); text-decoration: underline; font-size: 13px; }

/* ── Mapa ── */
.focus { position: absolute; top: 12px; left: 12px; display: flex; align-items: center; gap: 8px; max-width: calc(100% - 80px); background: var(--panel); border-radius: 999px; padding: 4px 5px 4px 6px; box-shadow: var(--shadow); font-size: 13px; font-weight: 600; }
.focus[hidden] { display: none; }
.focus-label { display: flex; align-items: center; gap: 8px; min-width: 0; }
.focus-label span:last-child { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.focus .badge { min-width: 0; height: 22px; font-size: 12px; }
.focus-x { flex: none; border: 0; background: var(--chip); width: 24px; height: 24px; border-radius: 50%; color: var(--muted); font-size: 12px; }
.focus-x:hover { color: var(--text); }
.legend { position: absolute; left: 12px; bottom: 28px; display: flex; gap: 12px; align-items: center; background: var(--panel); border-radius: 8px; padding: 6px 10px; font-size: 12px; box-shadow: var(--shadow); }
.legend[hidden] { display: none; }
.legend span { display: flex; align-items: center; gap: 5px; }
.maplibregl-popup-content { background: var(--panel); color: var(--text); border-radius: 10px; padding: 10px 12px; font-size: 13.5px; box-shadow: var(--shadow); }
.maplibregl-popup-tip { border-top-color: var(--panel) !important; border-bottom-color: var(--panel) !important; }
.maplibregl-popup-close-button { color: var(--muted); font-size: 16px; }
.pop h4 { margin: 0 18px 4px 0; font-size: 14px; display: flex; gap: 6px; align-items: center; }
.pop h4 .badge { flex: none; min-width: 0; height: 20px; font-size: 11.5px; }
.pop .dest { display: block; }
.pop .to { margin-right: 2px; }
.pop p { margin: 2px 0; color: var(--muted); }
.pop .board { margin: 2px 0; }

dialog { max-width: 560px; width: calc(100% - 32px); border: 0; border-radius: 14px; padding: 20px 24px; background: var(--panel); color: var(--text); box-shadow: var(--shadow); }
dialog::backdrop { background: rgb(0 0 0 / 35%); }
dialog p { margin: 4px 0 8px; font-size: 14.5px; }
dialog h2 { font-size: 20px; text-transform: none; letter-spacing: 0; color: var(--text); margin: 0 0 8px; }
.close { float: right; background: none; border: 0; font-size: 18px; color: var(--muted); }

/* Móvil: la pantalla en tres partes — mapa / selectores de transporte / líneas.
   Los selectores ocupan lo que necesitan (sin desplazamiento); mapa y líneas se reparten el resto. */
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) auto minmax(0, 1fr); height: 100vh; height: 100dvh; }
  .mapwrap { grid-row: 1; }
  .panel { grid-row: 2 / 4; display: grid; grid-template-rows: subgrid; padding: 0; border-right: 0; border-top: 1px solid var(--line); overflow: hidden; }
  .panel-top { overflow: visible; padding: 5px 12px 7px; }
  .panel-body { overflow-y: auto; overscroll-behavior: contain; padding: 0 12px 18px; }
  .panel-body > .tabpanel > h2:first-child, .panel-body > .tabpanel > .search:first-child { margin-top: 12px; }

  /* Selectores compactos: 2 columnas × 3 filas, resumen en una línea */
  .brand { font-size: 19px; }
  .upd { font-size: 11px; padding: 2px 7px; }
  .ops { margin-top: 6px; gap: 5px; }
  .op { padding: 5px 9px 6px; border-radius: 10px; gap: 0; }
  .op-name { font-size: 12px; gap: 6px; }
  .op-logo { width: 16px; height: 16px; }
  .op-logo.wide { width: 26px; }
  .op-main b { font-size: 18px; line-height: 1.15; }
  .op-main span { font-size: 11.5px; }
  .op-sub { font-size: 11px; }

  /* Mapa: leyenda y selección más pequeñas para no tapar */
  .legend { bottom: 6px; left: 8px; gap: 8px; padding: 4px 8px; font-size: 11px; }
  .focus { top: 8px; left: 8px; font-size: 12px; }
  .maplibregl-ctrl-top-right .maplibregl-ctrl { margin: 6px 6px 0 0; }
}

/* Buses en directo (posición estimada) */
.bus-live { display: flex; align-items: center; gap: 7px; margin: 8px 0 4px; font-size: 13px; color: var(--muted); }
.live-dot { flex: none; width: 8px; height: 8px; border-radius: 50%; background: #1fa34a; animation: live-pulse 1.6s ease-in-out infinite; }
@keyframes live-pulse { 50% { opacity: 0.35; } }
@media (prefers-reduced-motion: reduce) { .live-dot { animation: none; } }
