:root {
  --fondo: #0d1117;
  --superficie: #141a23;
  --superficie-2: #1b2330;
  --borde: #263142;
  --texto: #e6edf3;
  --texto-2: #9aa7b6;
  --texto-3: #6b7888;
  --acento: #3ea8ff;
  --ok: #3fb950;
  --aviso: #d29922;
  --alarma: #ff453a;
  --renfe: #9b2c8f;
  --ouigo: #e5007d;
  --iryo: #e8402f;
  --otro: #8b949e;
  --radio: 10px;
  --fuente: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  height: 100dvh;
  background: var(--fondo);
  color: var(--texto);
  font: 14px/1.45 var(--fuente);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
button { font: inherit; color: inherit; }

/* ---------- Barra superior ---------- */
.barra {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 8px 16px;
  background: var(--superficie);
  border-bottom: 1px solid var(--borde);
  min-height: 52px;
  z-index: 1000;
}
.marca { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.logo {
  width: 26px; height: 26px; border-radius: 7px;
  background: linear-gradient(135deg, var(--acento), #1f6feb);
  box-shadow: 0 0 0 1px #ffffff1a inset;
}
.marca h1 { font-size: 16px; font-weight: 650; margin: 0; letter-spacing: -0.01em; }
.marca h1 span { color: var(--texto-2); font-weight: 450; }
.fuentes { display: flex; gap: 8px; flex: 1; overflow-x: auto; scrollbar-width: none; }
.fuente {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 10px; border-radius: 999px;
  background: var(--superficie-2); border: 1px solid var(--borde);
  color: var(--texto-2); font-size: 12px; white-space: nowrap;
}
.fuente b { color: var(--texto); font-weight: 600; font-variant-numeric: tabular-nums; }
.fuente .led { width: 7px; height: 7px; border-radius: 50%; background: var(--ok); }
.fuente.mal .led { background: var(--alarma); }
.fuente.viejo .led { background: var(--aviso); }
.acciones { display: flex; align-items: center; gap: 12px; flex-shrink: 0; }
.reloj { font: 600 15px var(--mono); font-variant-numeric: tabular-nums; color: var(--texto-2); }
.boton-sonido {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 12px; border-radius: 8px; cursor: pointer;
  background: var(--superficie-2); border: 1px solid var(--borde); color: var(--texto-2);
}
.boton-sonido:hover { border-color: var(--texto-3); }
.boton-sonido[aria-pressed="true"] { color: var(--texto); border-color: var(--acento); background: #3ea8ff1f; }
.icono-sonido {
  width: 14px; height: 14px; display: inline-block; background: currentColor;
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M3 9v6h4l5 5V4L7 9H3zm13.5 3A4.5 4.5 0 0 0 14 8v8a4.5 4.5 0 0 0 2.5-4z'/%3E%3C/svg%3E") center/contain no-repeat;
          mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M3 9v6h4l5 5V4L7 9H3zm13.5 3A4.5 4.5 0 0 0 14 8v8a4.5 4.5 0 0 0 2.5-4z'/%3E%3C/svg%3E") center/contain no-repeat;
}

/* ---------- Disposición ---------- */
.principal { flex: 1; display: flex; min-height: 0; }
.zona-mapa { position: relative; flex: 1; min-width: 0; }
#mapa { position: absolute; inset: 0; background: #0b0f14; }

.aviso-alarma {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%);
  z-index: 800; padding: 10px 18px; border-radius: var(--radio);
  background: var(--alarma); color: #fff; font-weight: 650;
  box-shadow: 0 8px 30px #ff453a55; cursor: pointer;
  animation: latido 1.2s ease-in-out infinite;
  max-width: calc(100% - 32px); text-align: center;
}
@keyframes latido { 50% { box-shadow: 0 8px 40px #ff453aaa; filter: brightness(1.12); } }
.conexion {
  position: absolute; bottom: 16px; left: 50%; transform: translateX(-50%);
  z-index: 800; padding: 8px 14px; border-radius: 8px;
  background: var(--aviso); color: #111; font-weight: 600;
}
.leyenda {
  position: absolute; left: 12px; bottom: 24px; z-index: 700;
  background: #141a23e6; border: 1px solid var(--borde); border-radius: var(--radio);
  padding: 8px 12px; font-size: 12px; color: var(--texto-2);
  display: grid; gap: 4px; backdrop-filter: blur(6px);
}
.leyenda div { display: flex; align-items: center; gap: 8px; }
.leyenda div[hidden] { display: none; }
.leyenda small { color: var(--texto-3); }
.pt { width: 12px; height: 12px; border-radius: 50%; display: inline-block; border: 2px solid #fff; }
.pt.op-renfe { background: var(--renfe); }
.pt.op-ouigo { background: var(--ouigo); }
.pt.op-iryo { background: var(--iryo); }
.pt.est { border-style: dashed; }
.pt.alarma-ej { background: transparent; border-color: var(--alarma); box-shadow: 0 0 0 3px #ff453a55; }

/* ---------- Panel lateral ---------- */
.panel {
  width: 380px; flex-shrink: 0; display: flex; flex-direction: column;
  background: var(--superficie); border-left: 1px solid var(--borde); min-height: 0;
}
.asa { display: none; }
.pestanas { display: flex; border-bottom: 1px solid var(--borde); padding: 0 8px; }
.pestanas button {
  flex: 1; background: none; border: 0; padding: 12px 8px; cursor: pointer;
  color: var(--texto-2); font-weight: 550; border-bottom: 2px solid transparent;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.pestanas button[aria-selected="true"] { color: var(--texto); border-bottom-color: var(--acento); }
.contador {
  min-width: 20px; padding: 0 6px; border-radius: 999px; font-size: 11px; line-height: 18px;
  background: var(--superficie-2); color: var(--texto-2); font-variant-numeric: tabular-nums;
}
.contador.activo { background: var(--alarma); color: #fff; }
.contenido { flex: 1; overflow-y: auto; padding: 12px; min-height: 0; }
.contenido[hidden] { display: none; }

.resumen { margin: 4px 4px 12px; color: var(--texto-2); }
.subtitulo { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--texto-3); margin: 20px 4px 8px; }
.lista { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }

.alarma {
  border: 1px solid #ff453a66; background: #ff453a14; border-radius: var(--radio);
  padding: 10px 12px; cursor: pointer;
}
.alarma:hover { background: #ff453a22; }
.alarma .dist { font: 700 20px var(--mono); color: var(--alarma); font-variant-numeric: tabular-nums; }
.alarma .dist small { font: 500 12px var(--fuente); color: var(--texto-2); margin-left: 4px; }
.alarma .par { display: grid; gap: 2px; margin-top: 4px; }
.alarma .meta { color: var(--texto-3); font-size: 12px; margin-top: 4px; }
.etiqueta {
  display: inline-block; padding: 0 6px; border-radius: 4px; font-size: 11px; font-weight: 650;
  line-height: 18px; color: #fff; vertical-align: 1px;
}
.etiqueta.op-renfe { background: var(--renfe); }
.etiqueta.op-ouigo { background: var(--ouigo); }
.etiqueta.op-iryo { background: var(--iryo); }
.etiqueta.op-otro { background: var(--otro); }
.etiqueta.suave { background: var(--superficie-2); color: var(--texto-2); border: 1px solid var(--borde); }

.historial li { font-size: 12px; color: var(--texto-2); padding: 6px 4px; border-bottom: 1px solid var(--borde); }
.historial li b { color: var(--texto); font-weight: 600; }
.historial .ini { color: var(--alarma); }
.historial .fin { color: var(--ok); }
.vacio { color: var(--texto-3); padding: 16px 4px; text-align: center; }

.buscar {
  width: 100%; padding: 9px 12px; border-radius: 8px; margin-bottom: 8px;
  background: var(--superficie-2); border: 1px solid var(--borde); color: var(--texto); font: inherit;
}
.buscar:focus { outline: 2px solid var(--acento); outline-offset: -1px; }
.totales { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.fila-tren {
  display: grid; grid-template-columns: auto 1fr auto; gap: 10px; align-items: center;
  padding: 8px 10px; border-radius: 8px; cursor: pointer; border: 1px solid transparent;
}
.fila-tren:hover { background: var(--superficie-2); }
.fila-tren.en-alarma { border-color: #ff453a66; background: #ff453a10; }
.fila-tren .num { font: 650 13px var(--mono); }
.fila-tren .ruta { color: var(--texto-2); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fila-tren .vel { font: 12px var(--mono); color: var(--texto-2); font-variant-numeric: tabular-nums; }

/* ---------- Ficha de tren ---------- */
.ficha .cab { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.ficha .cerrar { background: var(--superficie-2); border: 1px solid var(--borde); border-radius: 8px; padding: 4px 10px; cursor: pointer; }
.ficha h2 { font: 700 22px var(--mono); margin: 8px 0 2px; }
.ficha .trayecto { color: var(--texto-2); margin-bottom: 12px; }
.ficha dl { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0 0 12px; }
.ficha dt { color: var(--texto-3); }
.ficha dd { margin: 0; font-variant-numeric: tabular-nums; }
.ficha .aviso-est { background: #d299221a; border: 1px solid #d2992255; color: #e3b341; border-radius: 8px; padding: 8px 10px; font-size: 12px; margin-bottom: 12px; }
.ficha .alarma-ficha { background: #ff453a1a; border: 1px solid #ff453a66; color: #ff8a80; border-radius: 8px; padding: 8px 10px; margin-bottom: 12px; font-weight: 600; }
.paradas { list-style: none; margin: 0; padding: 0; font-size: 13px; }
.paradas li { display: grid; grid-template-columns: 1fr auto; gap: 8px; padding: 5px 0 5px 16px; border-left: 2px solid var(--borde); position: relative; }
.paradas li::before { content: ""; position: absolute; left: -5px; top: 11px; width: 8px; height: 8px; border-radius: 50%; background: var(--borde); }
.paradas li.siguiente { color: var(--acento); font-weight: 600; }
.paradas li.siguiente::before { background: var(--acento); }
.paradas .h { font-family: var(--mono); color: var(--texto-2); }

/* ---------- Ajustes ---------- */
.ajustes { display: grid; gap: 14px; }
.campo { display: grid; gap: 6px; border: 0; padding: 0; margin: 0; }
.campo legend { margin-bottom: 6px; color: var(--texto-2); }
.campo label { display: flex; gap: 8px; align-items: center; }
.campo input[type="range"] { width: 100%; accent-color: var(--alarma); }
.interruptor { display: flex; gap: 10px; align-items: center; cursor: pointer; }
.interruptor input, .campo input[type="radio"] { accent-color: var(--acento); width: 16px; height: 16px; }
.nota { margin-top: 20px; font-size: 12px; color: var(--texto-2); border-top: 1px solid var(--borde); padding-top: 12px; }
.nota b { color: var(--texto); }

/* ---------- Marcadores de tren ---------- */
.tren-icono { background: none; border: 0; }
.tren {
  position: relative; width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid #fff; box-shadow: 0 1px 4px #0009;
}
.tren.op-renfe { background: var(--renfe); }
.tren.op-ouigo { background: var(--ouigo); }
.tren.op-iryo { background: var(--iryo); }
.tren.op-otro { background: var(--otro); }
.tren.estimada { border-style: dashed; }
.tren.fuera { opacity: .55; width: 12px; height: 12px; margin: 2px; }
.tren.cer { width: 10px; height: 10px; margin: 3px; border-width: 1.5px; background: #c9a0c4; opacity: .85; }
.tren.cer .flecha { display: none; }
.pt.cer { width: 9px; height: 9px; background: #c9a0c4; border-width: 1.5px; margin: 0 1.5px; }
.etq-km-icono { background: none; border: 0; }
.etq-km {
  display: inline-flex; align-items: baseline; gap: 4px;
  /* Encima del tramo, para no tapar los nombres de los trenes (a su derecha). */
  transform: translate(-50%, calc(-100% - 14px));
  padding: 4px 9px; border-radius: 999px; white-space: nowrap;
  background: #fff; color: var(--alarma); border: 2px solid var(--alarma);
  font: 750 15px var(--mono); font-variant-numeric: tabular-nums;
  box-shadow: 0 2px 10px #0008; pointer-events: none;
}
.etq-km small { font: 600 11px var(--fuente); color: #8a1c15; }
.km-compacto .etq-km { font-size: 11px; padding: 1px 6px; border-width: 1.5px; }
.km-compacto .etq-km small { display: none; }
.tren .flecha {
  position: absolute; left: 50%; top: 50%; width: 0; height: 0;
  border-left: 4px solid transparent; border-right: 4px solid transparent; border-bottom: 7px solid #fff;
  transform: translate(-50%, -50%) rotate(var(--rumbo, 0deg)) translateY(-13px);
  filter: drop-shadow(0 0 1px #000);
}
.tren .etq {
  position: absolute; left: 18px; top: -3px; white-space: nowrap;
  font: 600 11px var(--fuente); color: #fff; padding: 1px 5px; border-radius: 4px;
  background: #0d1117cc; pointer-events: none; display: none;
}
.etiquetas-on .tren .etq { display: block; }
.tren.seleccionado { outline: 3px solid var(--acento); outline-offset: 2px; }
.tren.en-alarma::after {
  content: ""; position: absolute; inset: -9px; border-radius: 50%;
  border: 3px solid var(--alarma); animation: onda 1s ease-out infinite;
}
.tren.en-alarma .etq { display: block; background: var(--alarma); }
@keyframes onda { from { transform: scale(.6); opacity: 1; } to { transform: scale(1.5); opacity: 0; } }

/* ---------- Pantalla completa ---------- */
.boton-completa {
  width: 34px; height: 34px; padding: 0; cursor: pointer;
  background: var(--superficie); color: var(--texto);
  border: 1px solid var(--borde); border-radius: 8px;
  display: grid; place-items: center;
}
.boton-completa:hover { background: var(--superficie-2); }
.boton-completa::before {
  content: ""; width: 18px; height: 18px; background: currentColor;
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M4 9V4h5M20 9V4h-5M4 15v5h5M20 15v5h-5'/%3E%3C/svg%3E") center/contain no-repeat;
          mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M4 9V4h5M20 9V4h-5M4 15v5h5M20 15v5h-5'/%3E%3C/svg%3E") center/contain no-repeat;
}
.boton-completa[aria-pressed="true"] { border-color: var(--acento); color: var(--acento); }
.boton-completa[aria-pressed="true"]::before {
  -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M9 4v5H4M15 4v5h5M9 20v-5H4M15 20v-5h5'/%3E%3C/svg%3E");
          mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M9 4v5H4M15 4v5h5M9 20v-5H4M15 20v-5h5'/%3E%3C/svg%3E");
}
body.mapa-completo .barra,
body.mapa-completo .panel { display: none; }
body.mapa-completo .leaflet-top { top: env(safe-area-inset-top); }

.leaflet-container { font: inherit; background: #0b0f14; }
.leaflet-tooltip.tip-linea { background: #141a23; color: var(--texto); border: 1px solid var(--borde); box-shadow: none; }
.leaflet-tooltip.tip-linea::before { display: none; }
.leaflet-control-layers, .leaflet-bar a { background: var(--superficie); color: var(--texto); border-color: var(--borde); }
.leaflet-control-layers { border: 1px solid var(--borde) !important; border-radius: var(--radio) !important; }
.leaflet-control-layers-expanded { padding: 8px 12px; }
.leaflet-control-attribution { background: #0d1117bb !important; color: var(--texto-3); }
.leaflet-control-attribution a { color: var(--texto-2); }

/* ---------- Móvil ---------- */
@media (max-width: 820px) {
  .barra { gap: 10px; padding: 8px 12px; flex-wrap: wrap; }
  .marca h1 span, .boton-sonido .txt { display: none; }
  .fuentes { order: 3; flex-basis: 100%; }
  .principal { flex-direction: column; }
  .panel {
    width: 100%; height: 46vh; border-left: 0; border-top: 1px solid var(--borde);
    transition: height .2s ease;
  }
  .panel.plegado { height: 52px; }
  .panel.plegado .contenido { display: none; }
  .asa { display: block; height: 14px; background: none; border: 0; cursor: pointer; position: relative; }
  .asa::after { content: ""; position: absolute; left: 50%; top: 5px; width: 40px; height: 4px; margin-left: -20px; border-radius: 2px; background: var(--borde); }
  .leyenda { bottom: 34px; left: 8px; font-size: 11px; padding: 6px 10px; }
  .aviso-alarma {
    top: 8px; left: 8px; right: 56px; transform: none; max-width: none;
    padding: 6px 10px; font-size: 13px; line-height: 1.3; border-radius: 8px;
  }
  body.mapa-completo .aviso-alarma { top: calc(8px + env(safe-area-inset-top)); }
  .leaflet-control-attribution { font-size: 9px; }
  .contenido { padding: 12px 16px; }
}
