:root {
  --bg: #f6f6f4;
  --surface: #ffffff;
  --surface-2: #f0f0ed;
  --text: #17191c;
  --muted: #6b6f76;
  --border: #e2e2de;
  --accent: #d7182a;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 4px 16px rgba(0, 0, 0, .06);
  --radius: 12px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #101114;
    --surface: #17191d;
    --surface-2: #1f2227;
    --text: #eceef1;
    --muted: #949aa3;
    --border: #2a2e34;
    --accent: #ff4d5e;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 4px 16px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #101114; --surface: #17191d; --surface-2: #1f2227; --text: #eceef1;
  --muted: #949aa3; --border: #2a2e34; --accent: #ff4d5e; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { height: 100dvh; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 var(--font);
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}
button, select, input { font: inherit; color: inherit; }
.muted { color: var(--muted); }
[hidden] { display: none !important; }

/* Barra superior */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 1000;
}
.brand { display: flex; align-items: center; gap: 8px; color: inherit; text-decoration: none; font-size: 18px; letter-spacing: -.01em; }
.brand b { color: var(--accent); font-weight: 700; }
.clock { margin-left: auto; font-variant-numeric: tabular-nums; font-weight: 600; }
.sim { font-size: 12px; padding: 2px 8px; border-radius: 99px; background: var(--accent); color: #fff; }

/* Distribución */
.layout { flex: 1; display: flex; min-height: 0; }
.panel {
  width: 380px;
  flex-shrink: 0;
  overflow-y: auto;
  background: var(--bg);
  border-right: 1px solid var(--border);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.map-wrap { flex: 1; position: relative; min-width: 0; }
.panel > section, .favs, .card { min-width: 0; }
/* Mapa base: OSM aclarado; en modo oscuro se invierte manteniendo los tonos. */
.basemap { filter: saturate(.55) contrast(.95) brightness(1.03); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .basemap { filter: invert(1) hue-rotate(180deg) saturate(.4) brightness(.85) contrast(.9); }
}
:root[data-theme="dark"] .basemap { filter: invert(1) hue-rotate(180deg) saturate(.4) brightness(.85) contrast(.9); }
#map { position: absolute; inset: 0; background: var(--surface-2); }
.panel-foot { font-size: 12px; margin-top: auto; }
.panel-foot p { margin: 0 0 4px; }
.credit a { color: var(--text); font-weight: 600; text-decoration: none; border-bottom: 1px solid var(--border); }
.credit a:hover { border-color: var(--accent); }

.sheet-grip { display: none; }

/* Móvil: mapa a pantalla completa y recorridos en una hoja inferior arrastrable
   con tres posiciones (mínima, media y completa). Su altura vive en --sheet-h,
   que también usan el botón de avisos y los créditos del mapa. */
@media (max-width: 760px) {
  .layout { display: block; position: relative; }
  .map-wrap { position: absolute; inset: 0; }
  .panel {
    /* Por encima de los controles de Leaflet (z-index 1000) */
    position: absolute; left: 0; right: 0; bottom: 0; z-index: 1100;
    width: auto; height: var(--sheet-h, 45dvh);
    padding: 0 16px calc(16px + env(safe-area-inset-bottom));
    border-right: 0; border-top: 1px solid var(--border);
    border-radius: 18px 18px 0 0;
    box-shadow: 0 -6px 24px rgba(0, 0, 0, .18);
    transition: height .3s cubic-bezier(.2, .8, .2, 1);
    overscroll-behavior: contain;
  }
  .panel.dragging { transition: none; user-select: none; }
  .panel.is-peek { overflow: hidden; touch-action: none; }
  .sheet-grip {
    display: flex; justify-content: center;
    position: sticky; top: 0; z-index: 2;
    margin: 0 -16px -10px; padding: 10px 0 8px;
    background: var(--bg); border-radius: 18px 18px 0 0;
    cursor: grab; touch-action: none;
  }
  .sheet-grip span { width: 42px; height: 5px; border-radius: 3px; background: color-mix(in srgb, var(--muted) 45%, transparent); }
  .sheet-grip:focus-visible span { background: var(--accent); }
  .section-head { touch-action: none; }
  #feedInfo { display: none; }
  .next .mins { font-size: 28px; }
  .leaflet-bottom { bottom: var(--sheet-h, 0px); transition: bottom .3s cubic-bezier(.2, .8, .2, 1); }
  .dragging-sheet .leaflet-bottom, .dragging-sheet .fab { transition: none; }
  .legend { max-height: calc(100dvh - var(--sheet-h, 0px) - 120px); }
}

.section-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0; font-weight: 600; }

button.ghost, button.primary, button.icon {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 6px 10px;
  cursor: pointer;
}
button.ghost:hover, button.icon:hover { background: var(--surface-2); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.icon { padding: 4px 8px; line-height: 1; min-width: 32px; min-height: 32px; }

.add-form {
  display: grid; gap: 10px; padding: 12px; margin-bottom: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.add-form label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.add-form select { padding: 8px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text); }
.form-actions { display: flex; gap: 8px; }

/* Tarjetas de recorrido */
.favs { display: grid; gap: 12px; }
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.card.active { border-color: var(--accent); }
.card-head { display: flex; align-items: center; gap: 6px; padding: 12px 12px 8px; }
.card-title { flex: 1; min-width: 0; font-weight: 650; font-size: 16px; cursor: pointer; background: none; border: 0; padding: 0; text-align: left; }
.card-title .arrow { color: var(--muted); margin: 0 4px; font-weight: 400; }
.next { padding: 0 12px 10px; display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.next .mins { font-size: 34px; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.next .mins small { font-size: 15px; font-weight: 600; margin-left: 2px; }
.next .detail { color: var(--muted); font-size: 13px; }
.deps { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--border); }
.deps li {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 10px;
  padding: 7px 12px;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
}
.deps li + li { border-top: 1px solid var(--border); }
.deps li:hover { background: var(--surface-2); }
.deps .head { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.deps .t { font-weight: 600; }
.deps .in { color: var(--muted); font-size: 13px; min-width: 52px; text-align: right; }
.live-dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: #2fb344; margin-right: 4px; vertical-align: middle; }
.empty { padding: 10px 12px; color: var(--muted); font-size: 13px; border-top: 1px solid var(--border); }

.badge {
  display: inline-grid; place-items: center;
  min-width: 26px; height: 22px; padding: 0 6px;
  border-radius: 6px; font-size: 12px; font-weight: 700;
  background: var(--c); color: var(--t);
}

/* Leyenda */
.legend {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 8px;
  max-height: calc(100vh - 160px);
  overflow-y: auto;
  font: 13px/1.3 var(--font);
  min-width: 210px;
}
.legend-head { display: flex; align-items: center; justify-content: space-between; gap: 6px; padding: 2px 4px 6px; }
.legend-head strong { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.legend-head button { font-size: 12px; padding: 3px 7px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface-2); cursor: pointer; }
.legend label { display: flex; align-items: center; gap: 8px; padding: 4px; border-radius: 6px; cursor: pointer; user-select: none; }
.legend label:hover { background: var(--surface-2); }
.legend input { accent-color: var(--accent); margin: 0; }
.legend .lname { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 150px; }
.legend .count { margin-left: auto; color: var(--muted); font-variant-numeric: tabular-nums; font-size: 12px; }
.legend.collapsed .legend-body { display: none; }
/* En móvil, minimizada queda solo el botón «Líneas» */
@media (max-width: 760px) {
  .legend.collapsed { min-width: 0; padding: 4px; }
  .legend.collapsed .legend-head { padding: 0; }
  .legend.collapsed .legend-actions { display: none; }
}
.legend:not(.collapsed) .chev { display: inline-block; transform: rotate(180deg); }
.legend-toggle { border: 0; background: none; padding: 0; cursor: pointer; }

/* Trenes: círculo con el número y flecha de dirección */
.train-icon { background: none; border: 0; display: grid; place-items: center; }
.train {
  position: relative;
  min-width: 22px; height: 22px; padding: 0 3px;
  border-radius: 11px;
  background: var(--c);
  color: var(--t);
  border: 2px solid #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, .45);
  display: grid; place-items: center;
  font: 700 11px/1 var(--font);
  transition: transform .2s;
}
.train span { position: relative; }
/* La flecha gira alrededor del círculo según el rumbo */
.train .dir {
  position: absolute; inset: -11px;
  pointer-events: none;
  transition: transform .4s;
}
.train .dir::before {
  content: '';
  position: absolute; left: 50%; top: 0;
  transform: translateX(-50%);
  border: 6px solid transparent;
  border-top: 0;
  border-bottom: 9px solid var(--c);
  filter: drop-shadow(0 -1px 0 #fff) drop-shadow(0 0 1px rgba(0, 0, 0, .5));
}
.train.stopped .dir { opacity: .3; }
/* Parado en andén: onda que se expande desde el tren */
.train::after {
  content: '';
  position: absolute; inset: -2px;
  border-radius: inherit;
  border: 2px solid var(--c);
  opacity: 0;
  pointer-events: none;
}
.train.stopped::after { animation: dwell 1.5s ease-out infinite; }
@keyframes dwell {
  0% { transform: scale(1); opacity: .9; }
  100% { transform: scale(2.1); opacity: 0; }
}
@media (prefers-reduced-motion: reduce) {
  .train.stopped::after { animation: none; opacity: .6; transform: scale(1.4); }
}
.train.hl { transform: scale(1.35); animation: pulse 1.6s infinite; }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 var(--c), 0 1px 4px rgba(0,0,0,.45); }
  70% { box-shadow: 0 0 0 12px transparent, 0 1px 4px rgba(0,0,0,.45); }
  100% { box-shadow: 0 0 0 0 transparent, 0 1px 4px rgba(0,0,0,.45); }
}

.status {
  position: absolute; left: 50%; top: 12px; transform: translateX(-50%);
  background: var(--surface); border: 1px solid var(--border); border-radius: 99px;
  padding: 6px 14px; font-size: 13px; box-shadow: var(--shadow); z-index: 500;
}

/* Popups de Leaflet con los tokens del tema */
.leaflet-popup-content-wrapper, .leaflet-popup-tip, .leaflet-tooltip {
  background: var(--surface); color: var(--text); border-color: var(--border);
}
.leaflet-tooltip { box-shadow: var(--shadow); }
.leaflet-popup-content { margin: 12px 14px; font: 14px/1.4 var(--font); min-width: 220px; }
.leaflet-container a.leaflet-popup-close-button { color: var(--muted); }
.leaflet-control-attribution { font-size: 10px; }
.pop-title { font-weight: 700; font-size: 15px; margin-bottom: 6px; display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.pop-deps { list-style: none; margin: 6px 0; padding: 0; }
.pop-deps li { display: grid; grid-template-columns: auto 1fr auto; gap: 8px; align-items: center; padding: 3px 0; font-variant-numeric: tabular-nums; }
.pop-actions { display: flex; gap: 6px; margin-top: 8px; }
.pop-actions button { font-size: 12px; padding: 4px 8px; }
.stop-label { font-weight: 600; font-size: 12px; padding: 2px 6px; }

/* Pantallas táctiles: objetivos de toque más grandes */
@media (pointer: coarse) {
  .card-title { padding: 6px 0; }
  .legend-head button, .legend-toggle { min-height: 34px; padding: 4px 10px; }
  .legend label { padding: 8px 4px; }
  .deps li { padding-top: 10px; padding-bottom: 10px; }
}

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}
.noscript { position: absolute; inset: 0; padding: 24px; background: var(--bg); z-index: 2000; overflow: auto; }
.noscript h1 { font-size: 20px; margin-top: 0; }

/* Origen + «Más cercana» */
.with-action { display: flex; gap: 8px; }
.with-action select { flex: 1; min-width: 0; }
.with-action button { white-space: nowrap; }
.hint { margin: -4px 0 0; font-size: 12px; color: var(--muted); }
.hint.error { color: var(--accent); }

/* Ventana de horarios */
.sheet {
  width: min(520px, 100vw);
  max-height: min(86dvh, 820px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .3);
  overflow: hidden;
  flex-direction: column;
}
.sheet[open] { display: flex; }
.sheet::backdrop { background: rgba(10, 12, 16, .45); }
@media (max-width: 760px) {
  .sheet { margin: auto 0 0; max-width: 100vw; width: 100vw; max-height: 88dvh; border-radius: 16px 16px 0 0; border-bottom: 0; }
}
.tt-head { padding: 14px 16px 8px; border-bottom: 1px solid var(--border); }
.tt-title { display: flex; align-items: center; gap: 6px; }
.tt-title h2 { flex: 1; min-width: 0; font-size: 17px; text-transform: none; letter-spacing: 0; color: var(--text); font-weight: 650; }
.tt-days { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  border-radius: 99px; padding: 6px 12px; font-size: 14px; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px; min-height: 34px;
}
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
.date-chip input { border: 0; background: transparent; color: inherit; font: inherit; padding: 0; color-scheme: inherit; }
.tt-sub { margin: 8px 0 0; font-size: 13px; }
.tt-list { list-style: none; margin: 0; padding: 4px 0; overflow-y: auto; flex: 1; }
.tt-list li {
  display: grid;
  grid-template-columns: 98px auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  padding: 8px 16px;
  font-variant-numeric: tabular-nums;
  border-left: 4px solid transparent;
}
.tt-list li + li { border-top: 1px solid var(--border); }
.tt-list .times { font-weight: 600; }
.tt-list .times small { color: var(--muted); font-weight: 400; }
.tt-list .head { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tt-list .dur { color: var(--muted); font-size: 13px; }
.tt-list li.past { opacity: .42; }
.tt-list li.next {
  background: color-mix(in srgb, var(--accent) 12%, var(--surface));
  border-left-color: var(--accent);
  position: relative;
}
.tt-list li.next .times { color: var(--accent); font-size: 17px; }
.tt-list li.near { background: var(--surface-2); border-left-color: var(--muted); }
.tag {
  grid-column: 1 / -1;
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; font-weight: 700; color: var(--accent);
  text-transform: uppercase; letter-spacing: .05em;
}
.tt-list li.near .tag { color: var(--muted); }
.pulse-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); animation: blink 1.4s ease-in-out infinite; }
@keyframes blink { 0%, 100% { opacity: 1; transform: scale(1); } 50% { opacity: .35; transform: scale(.7); } }
@media (prefers-reduced-motion: reduce) { .pulse-dot { animation: none; } }
.tt-empty { padding: 24px 16px; color: var(--muted); text-align: center; }

/* Sin recorridos guardados */
.empty-state {
  display: grid; gap: 8px; justify-items: start;
  padding: 16px; border: 1px dashed var(--border); border-radius: var(--radius); background: var(--surface);
}
.empty-state p { margin: 0; }
.empty-title { font-weight: 650; font-size: 16px; }
.empty-state .small { font-size: 12px; }

/* Botón flotante de avisos */
.fab {
  --fab: #2563eb;
  position: absolute; right: 12px; bottom: 30px; z-index: 900;
  display: inline-flex; align-items: center; gap: 8px;
  min-height: 48px; padding: 0 16px 0 14px;
  border: 0; border-radius: 99px;
  background: var(--fab); color: #fff;
  font: 600 15px/1 var(--font);
  box-shadow: 0 4px 14px rgba(0, 0, 0, .3);
  cursor: pointer;
}
.fab:hover { filter: brightness(1.08); }
.fab-icon { width: 22px; height: 22px; flex: none; }
.fab-count {
  min-width: 22px; height: 22px; padding: 0 6px; border-radius: 11px;
  background: #fff; color: var(--fab); font-size: 13px; font-weight: 800;
  display: inline-grid; place-items: center;
}
.fab--danger { --fab: #d7182a; animation: fab-pulse 1.8s ease-out infinite; }
@keyframes fab-pulse {
  0% { box-shadow: 0 4px 14px rgba(0,0,0,.3), 0 0 0 0 rgba(215, 24, 42, .55); }
  70% { box-shadow: 0 4px 14px rgba(0,0,0,.3), 0 0 0 16px rgba(215, 24, 42, 0); }
  100% { box-shadow: 0 4px 14px rgba(0,0,0,.3), 0 0 0 0 rgba(215, 24, 42, 0); }
}
@media (prefers-reduced-motion: reduce) { .fab--danger { animation: none; } }
@media (max-width: 760px) {
  .fab { right: 10px; bottom: calc(var(--sheet-h, 0px) + 14px); min-height: 46px; transition: bottom .3s cubic-bezier(.2, .8, .2, 1); }
  .fab-label { display: none; }
}

/* Lista de avisos */
.alerts-list { list-style: none; margin: 0; padding: 4px 0; overflow-y: auto; flex: 1; }
.alerts-list li { padding: 14px 16px; display: grid; gap: 8px; border-left: 4px solid transparent; }
.alerts-list li + li { border-top: 1px solid var(--border); }
.alerts-list li.danger { border-left-color: var(--accent); background: color-mix(in srgb, var(--accent) 7%, var(--surface)); }
.alert-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.alert-cat {
  font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em;
  color: var(--muted); margin-right: 4px;
}
li.danger .alert-cat { color: var(--accent); }
.alert-text { margin: 0; white-space: pre-line; }
.alert-link { font-size: 14px; font-weight: 600; color: var(--accent); text-decoration: none; }
.alert-link:hover { text-decoration: underline; }
.alerts-empty { padding: 24px 16px; color: var(--muted); text-align: center; }

/* Aviso dentro de la tarjeta de un recorrido */
.card-alerts:empty { display: none; }
.card-alert {
  display: flex; align-items: center; gap: 8px; width: calc(100% - 24px);
  margin: 0 12px 10px; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface-2); color: var(--text);
  font: 500 13px/1.3 var(--font); text-align: left; cursor: pointer;
}
.card-alert svg { width: 16px; height: 16px; flex: none; color: #2563eb; }
.card-alert.danger { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, var(--surface)); }
.card-alert.danger svg { color: var(--accent); }
.card-alert span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
