:root {
  --bg: #0b111c;
  --panel: #111a29;
  --panel-2: #172234;
  --line: #22304a;
  --text: #e6ecf5;
  --muted: #8c9ab3;
  --faint: #5d6b85;
  --green: #3ecf8e;
  --amber: #f2b33d;
  --red: #ff5a52;
  --blue: #5aa9ff;
  --grey: #8b95a7;
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  --sans: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --r: 10px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 14px/1.45 var(--sans); }
button { font: inherit; color: inherit; cursor: pointer; }
h2 { font-size: 15px; margin: 0; font-weight: 700; letter-spacing: .01em; }

.app { display: grid; grid-template-columns: 1fr 380px; height: 100vh; }
.main { display: grid; grid-template-rows: 1fr auto; min-width: 0; min-height: 0; }
.mapwrap { position: relative; min-height: 0; }
#map { position: absolute; inset: 0; background: #070b12; }

/* Barra superior sobre el mapa */
.topbar {
  position: absolute; z-index: 600; left: 12px; right: 12px; top: 12px;
  display: flex; flex-wrap: wrap; gap: 10px 14px; align-items: center;
  pointer-events: none;
}
.topbar > * { pointer-events: auto; }
.brand { display: flex; align-items: center; gap: 10px; background: rgba(11,17,28,.88); backdrop-filter: blur(6px); border: 1px solid var(--line); border-radius: var(--r); padding: 7px 12px; }
.logo { width: 20px; height: 20px; fill: none; stroke: var(--blue); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.name { font-weight: 700; font-size: 16px; }
.live { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12px; }
.live i { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); }
.live.on i { background: var(--green); box-shadow: 0 0 0 0 rgba(62,207,142,.6); animation: pulse 2s infinite; }
.live.demo i { background: var(--amber); }
.live.off i { background: var(--red); }
@keyframes pulse { 70% { box-shadow: 0 0 0 8px rgba(62,207,142,0); } 100% { box-shadow: 0 0 0 0 rgba(62,207,142,0); } }

.search input {
  width: 210px; background: rgba(11,17,28,.88); border: 1px solid var(--line); color: var(--text);
  border-radius: var(--r); padding: 8px 12px; font: 13px var(--sans); outline: none;
}
.search input:focus { border-color: var(--blue); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; width: 100%; }
.chip {
  display: inline-flex; gap: 6px; align-items: center; padding: 5px 10px; border-radius: 999px;
  background: rgba(11,17,28,.88); border: 1px solid var(--line); font-size: 12px; font-weight: 600; color: var(--muted);
}
.chip b { font-family: var(--mono); font-weight: 700; color: var(--text); }
.chip.on { background: #e6ecf5; color: #0b111c; border-color: #e6ecf5; }
.chip.on b { color: #0b111c; }
.chip[disabled] { opacity: .45; cursor: not-allowed; }

/* Ficha del tren */
.card {
  max-height: calc(100% - 210px); overflow: auto;
  position: absolute; z-index: 700; left: 12px; top: 128px; width: 300px; max-width: calc(100% - 24px);
  background: rgba(11,17,28,.96); border: 1px solid var(--line); border-left: 3px solid var(--red);
  border-radius: var(--r); padding: 14px 16px; box-shadow: 0 10px 30px rgba(0,0,0,.45);
}
.card.c-green { border-left-color: var(--green); }
.card.c-amber { border-left-color: var(--amber); }
.card h3 { margin: 0; font-size: 15px; }
.card .route { color: var(--muted); font-size: 12px; margin: 2px 0 10px; }
.card dl { display: grid; grid-template-columns: auto 1fr; gap: 5px 14px; margin: 0; font-size: 13px; }
.card dt { color: var(--muted); }
.card dd { margin: 0; text-align: right; font-family: var(--mono); font-size: 12.5px; }
.card dd.txt { font-family: var(--sans); font-size: 13px; }
.card .x { position: absolute; top: 8px; right: 10px; background: none; border: 0; color: var(--muted); font-size: 18px; line-height: 1; }
.card dl.extra { margin-top: 5px; }
.card dl.extra[data-collapsed] { display: none; }
.card s.sched { color: var(--faint); font-size: 11px; margin-left: 4px; }
.card-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.card .share { margin-top: 10px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 5px 10px; font-size: 12px; color: var(--muted); }

/* KPIs y leyenda */
.kpis {
  position: absolute; z-index: 600; left: 12px; bottom: 12px; display: flex; gap: 22px;
  background: rgba(11,17,28,.9); border: 1px solid var(--line); border-radius: var(--r); padding: 10px 14px;
}
.kpis div { display: flex; flex-direction: column; }
.kpis b { font-family: var(--mono); font-size: 22px; line-height: 1.1; }
.kpis span { color: var(--muted); font-size: 11px; }
.red { color: var(--red); }

.legend {
  position: absolute; z-index: 600; right: 12px; bottom: 12px; font-size: 11.5px; color: var(--muted);
  background: rgba(11,17,28,.9); border: 1px solid var(--line); border-radius: var(--r); padding: 8px 12px; display: grid; gap: 3px;
}
.legend div { display: flex; align-items: center; gap: 8px; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.dot.green { background: var(--green); } .dot.amber { background: var(--amber); }
.dot.red { background: var(--red); } .dot.grey { background: var(--grey); width: 7px; height: 7px; }
.dash { width: 16px; border-top: 2px dashed var(--red); display: inline-block; }

/* Paneles */
.panel { background: var(--panel); border-left: 1px solid var(--line); }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 14px 16px 0; }
.sub { margin: 4px 16px 10px; color: var(--faint); font-size: 12px; }
.tabs { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 16px 10px; }
.tabs.small { padding: 0; }
.tabs button { background: var(--panel-2); border: 1px solid var(--line); border-radius: 7px; padding: 4px 9px; font-size: 12px; color: var(--muted); }
.tabs button span { font-family: var(--mono); color: var(--faint); margin-left: 3px; }
.tabs button.on { background: #e6ecf5; color: #0b111c; border-color: #e6ecf5; }
.tabs button.on span { color: #0b111c; }

.incidents { border-left: 0; border-top: 1px solid var(--line); max-height: 34vh; display: flex; flex-direction: column; }
.inc-list { overflow: auto; padding: 0 16px 14px; display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 10px; }
.inc { background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--r); padding: 10px 12px; cursor: pointer; }
.inc:hover { border-color: #33476b; }
.inc .t { display: flex; gap: 8px; align-items: flex-start; }
.inc .t i { margin-top: 5px; flex: none; }
.inc .t h4 { margin: 0; font-size: 13px; flex: 1; }
.inc .meta { text-align: right; font-size: 11px; color: var(--muted); white-space: nowrap; }
.inc .meta b { display: block; color: var(--amber); }
.inc.grave .meta b { color: var(--red); }
.inc p { margin: 6px 0 0 18px; color: var(--muted); font-size: 12.5px; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; white-space: pre-line; }
.inc.open p { -webkit-line-clamp: unset; display: block; }
.inc .reg { margin: 6px 0 0 18px; color: var(--faint); font-size: 11px; }
.empty { color: var(--faint); font-size: 13px; padding: 4px 0 10px; }

.side { display: flex; flex-direction: column; min-height: 0; }
.arr-list { list-style: none; margin: 0; padding: 0 10px; overflow: auto; flex: 1; }
.arr {
  display: grid; grid-template-columns: 58px 1fr auto; gap: 10px; align-items: center;
  padding: 9px 8px; border-bottom: 1px solid var(--line); cursor: pointer; border-radius: 6px;
}
.arr:hover, .arr.sel { background: var(--panel-2); }
.arr .d { font-family: var(--mono); font-weight: 700; font-size: 15px; color: var(--red); border-right: 2px solid currentColor; padding-right: 8px; text-align: right; }
.arr .d.amber { color: var(--amber); }
.arr h4 { margin: 0; font-size: 12.5px; font-weight: 600; }
.arr h4 small { color: var(--faint); font-family: var(--mono); font-weight: 500; margin-left: 4px; }
.arr .w { color: var(--faint); font-size: 11.5px; }
.arr .h { text-align: right; font-family: var(--mono); font-size: 12.5px; }
.arr .h s { display: block; color: var(--faint); font-size: 11px; }
.more { margin: 8px 16px; background: none; border: 0; color: var(--blue); text-align: left; padding: 0; font-size: 12.5px; }
.foot { padding: 10px 16px 14px; color: var(--faint); font-size: 11px; border-top: 1px solid var(--line); }
.foot a { color: var(--muted); display: block; margin-top: 3px; }

/* Etiquetas del mapa */
.leaflet-tooltip.tlabel {
  background: rgba(11,17,28,.92); border: 1px solid var(--line); color: var(--text); box-shadow: none;
  font: 600 10.5px var(--mono); padding: 1px 5px; border-radius: 4px;
}
.leaflet-tooltip.tlabel::before { display: none; }
.leaflet-tooltip.tlabel .r { color: var(--red); } .leaflet-tooltip.tlabel .a { color: var(--amber); }
.leaflet-tooltip.slabel { background: none; border: 0; box-shadow: none; color: var(--faint); font: 500 11px var(--sans); padding: 0; }
.leaflet-tooltip.slabel::before { display: none; }
.leaflet-container { font-family: var(--sans); }
.leaflet-control-attribution { background: rgba(11,17,28,.7) !important; color: var(--faint) !important; }
.leaflet-control-attribution a { color: var(--muted) !important; }
.leaflet-bar a { background: var(--panel) !important; color: var(--text) !important; border-color: var(--line) !important; }
.leaflet-top.leaflet-right { top: 128px; }

/* Botones auxiliares */
.search-btn { display: none; }
.recenter { width: 34px; height: 34px; display: grid; place-items: center; background: var(--panel); border: 1px solid var(--line) !important; border-radius: 8px !important; cursor: pointer; }
.recenter svg { width: 18px; height: 18px; fill: none; stroke: var(--text); stroke-width: 1.8; stroke-linecap: round; }
.recenter:hover { background: var(--panel-2); }
.sheet { display: none; }

/* ───────── Móvil y tableta vertical: mapa a pantalla completa + panel inferior deslizable ───────── */
@media (max-width: 1000px) {
  html, body { height: 100%; overflow: hidden; overscroll-behavior: none; }
  body { position: fixed; inset: 0; }
  .app { display: block; height: 100dvh; }
  .main { display: block; height: 100%; }
  .mapwrap { position: absolute; inset: 0; height: auto; min-height: 0; }
  .side, .incidents { display: none; }

  /* Barra superior compacta */
  .topbar {
    left: 0; right: 0; top: 0; gap: 8px; padding: calc(8px + env(safe-area-inset-top)) 10px 8px;
    background: linear-gradient(to bottom, rgba(7,11,18,.96) 60%, rgba(7,11,18,0));
    flex-wrap: wrap;
  }
  .brand { flex: 1; min-width: 0; padding: 6px 10px; gap: 8px; }
  .name { font-size: 15px; }
  .live { min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .search-btn {
    display: grid; place-items: center; width: 42px; height: 42px; flex: none;
    background: rgba(11,17,28,.92); border: 1px solid var(--line); border-radius: var(--r);
  }
  .search-btn svg { width: 19px; height: 19px; fill: none; stroke: var(--text); stroke-width: 2; stroke-linecap: round; }
  .search { display: none; width: 100%; order: 3; }
  .search input { width: 100%; font-size: 16px; padding: 10px 12px; }
  .topbar.searching .search { display: block; }
  .topbar.searching .chips { display: none; }
  .chips {
    order: 4; flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; margin: 0 -10px; padding: 0 10px 2px;
    -webkit-mask-image: linear-gradient(to right, transparent 0, #000 10px, #000 calc(100% - 24px), transparent);
            mask-image: linear-gradient(to right, transparent 0, #000 10px, #000 calc(100% - 24px), transparent);
  }
  .chips::-webkit-scrollbar { display: none; }
  .chip { flex: none; padding: 7px 11px; font-size: 12.5px; }

  /* Controles del mapa */
  .leaflet-control-zoom { display: none; }
  .leaflet-top.leaflet-right { top: calc(112px + env(safe-area-inset-top)); }
  .leaflet-bottom { bottom: var(--sheet-visible, 0px); transition: bottom .25s ease; }
  .leaflet-control-attribution { font-size: 9.5px; max-width: 70vw; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

  /* Ficha del tren: flota sobre el panel */
  .card {
    top: auto; left: 8px; right: 8px; width: auto; bottom: calc(var(--sheet-visible, 0px) + 8px);
    max-height: calc(100dvh - var(--sheet-visible, 0px) - 140px); padding: 12px 14px; z-index: 900;
  }
  .card dl { gap: 4px 12px; font-size: 13px; }
  .card .x { width: 36px; height: 36px; top: 4px; right: 4px; font-size: 22px; }
  .card .share { padding: 8px 12px; font-size: 12.5px; }
  body.sheet-open .card { display: none; }
  body.card-open .tl-fab { display: none; }

  /* Panel inferior */
  .sheet {
    display: flex; flex-direction: column; position: fixed; z-index: 1000; left: 0; right: 0; bottom: 0;
    background: var(--panel); border-top: 1px solid var(--line); border-radius: 16px 16px 0 0;
    box-shadow: 0 -10px 30px rgba(0,0,0,.45); transition: transform .28s cubic-bezier(.2,.8,.2,1);
    will-change: transform;
  }
  .sh-grip { flex: none; touch-action: none; padding: 0 12px; user-select: none; -webkit-user-select: none; }
  .sh-handle { display: block; width: 100%; height: 22px; background: none; border: 0; padding: 0; }
  .sh-handle span { display: block; width: 40px; height: 5px; border-radius: 3px; background: #3a4a66; margin: 8px auto 0; }
  .sh-peek .kpis {
    position: static; display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px;
    background: none; border: 0; padding: 4px 4px 10px; border-radius: 0;
  }
  .sh-peek .kpis b { font-size: 20px; }
  .sh-tabs { display: flex; gap: 6px; padding-bottom: 10px; }
  .sh-tabs button {
    flex: 1; background: var(--panel-2); border: 1px solid var(--line); border-radius: 9px; padding: 8px 6px;
    font-size: 13px; font-weight: 600; color: var(--muted);
  }
  .sh-tabs button span { font-family: var(--mono); color: var(--faint); margin-left: 2px; }
  .sh-tabs button.on { background: #e6ecf5; color: #0b111c; border-color: #e6ecf5; }
  .sh-tabs button.on span { color: #0b111c; }
  .sh-body {
    flex: 1; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
    border-top: 1px solid var(--line); padding-bottom: calc(80px + env(safe-area-inset-bottom));
  }
  .sh-pane .panel-head { padding-top: 12px; }
  .sh-pane .tabs:not(.small) { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .sh-pane .tabs:not(.small)::-webkit-scrollbar { display: none; }
  .sh-pane .tabs button { flex: none; padding: 6px 10px; font-size: 12.5px; }
  .sh-pane .arr-list { overflow: visible; padding: 0 6px; }
  .arr { grid-template-columns: 54px 1fr auto; padding: 11px 6px; }
  .arr h4 { font-size: 13px; }
  .sh-pane .inc-list { grid-template-columns: 1fr; overflow: visible; }
  .sh-pane .legend { position: static; background: none; border: 0; padding: 12px 16px; font-size: 13px; gap: 8px; }
  .sh-pane .foot { border-top: 0; }
  .sh-info { margin: 14px 16px 0; color: var(--muted); font-size: 13px; }
  .more { padding: 10px 0; }

}

@media (max-width: 1000px) and (max-height: 520px) {
  /* Móvil en horizontal: panel lateral más bajo */
  .sh-peek .kpis { display: none; }
  .chips { display: none; }
  .leaflet-top.leaflet-right { top: 64px; }
}

/* Selector de mapa base */
.leaflet-control-layers { background: var(--panel) !important; color: var(--text); border: 1px solid var(--line) !important; border-radius: 8px !important; }
.leaflet-control-layers-toggle { filter: invert(1) hue-rotate(180deg) brightness(1.2); width: 32px !important; height: 32px !important; }
.leaflet-control-layers-expanded { padding: 8px 10px; font-size: 12.5px; }

/* ───────── Trenalito ───────── */
.tl-fab {
  position: fixed; z-index: 1100; right: 20px; bottom: 20px; display: flex; align-items: center; gap: 8px;
  background: var(--blue); color: #06101f; border: 0; border-radius: 999px; padding: 11px 18px 11px 14px;
  font-weight: 700; font-size: 14px; box-shadow: 0 8px 24px rgba(0,0,0,.45);
}
.tl-fab svg { width: 22px; height: 22px; fill: #06101f; }
.tl-fab svg circle { fill: var(--blue); }
.tl-fab:hover { filter: brightness(1.08); }
body.tl-open .tl-fab { display: none; }

.tl {
  position: fixed; z-index: 1200; top: 0; right: 0; bottom: 0; width: 400px; max-width: 100vw;
  display: flex; flex-direction: column; background: #0e1625; border-left: 1px solid var(--line);
  box-shadow: -12px 0 32px rgba(0,0,0,.45); animation: tlIn .18s ease-out;
}
@keyframes tlIn { from { transform: translateX(24px); opacity: 0; } to { transform: none; opacity: 1; } }
.tl[hidden] { display: none; }
.tl-head { display: flex; align-items: flex-start; gap: 6px; padding: 14px 14px 12px 16px; border-bottom: 1px solid var(--line); }
.tl-head > div { flex: 1; }
.tl-head h2 { font-size: 16px; }
.tl-head p { margin: 2px 0 0; color: var(--faint); font-size: 12px; }
.tl-icon { background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; width: 32px; height: 32px; font-size: 17px; line-height: 1; color: var(--muted); }
.tl-icon:hover { color: var(--text); }

.tl-log { flex: 1; overflow: auto; padding: 14px 14px 6px; display: flex; flex-direction: column; gap: 10px; }
.tl-msg { max-width: 88%; padding: 9px 12px; border-radius: 12px; font-size: 13.5px; line-height: 1.5; }
.tl-msg p { margin: 0 0 6px; } .tl-msg p:last-child { margin-bottom: 0; }
.tl-msg ul { margin: 4px 0 6px; padding-left: 18px; } .tl-msg li { margin: 2px 0; }
.tl-msg.user { align-self: flex-end; background: var(--blue); color: #06101f; border-bottom-right-radius: 4px; }
.tl-msg.assistant { align-self: flex-start; background: var(--panel-2); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.tl-msg.error { border-color: rgba(255,90,82,.5); color: #ffb4ae; }
.tl-train { font: 700 12px var(--mono); background: rgba(90,169,255,.14); color: var(--blue); border: 1px solid rgba(90,169,255,.35); border-radius: 5px; padding: 0 5px; }
.tl-train:hover { background: rgba(90,169,255,.28); }
.tl-dots { display: inline-flex; gap: 4px; padding: 4px 0; }
.tl-dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: tlDot 1s infinite ease-in-out; }
.tl-dots i:nth-child(2) { animation-delay: .15s; } .tl-dots i:nth-child(3) { animation-delay: .3s; }
@keyframes tlDot { 0%, 80%, 100% { opacity: .25; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-3px); } }

.tl-sugs { display: flex; flex-wrap: wrap; gap: 6px; padding: 4px 14px 8px; }
.tl-sugs button { background: none; border: 1px solid var(--line); color: var(--muted); border-radius: 999px; padding: 5px 10px; font-size: 12px; text-align: left; }
.tl-sugs button:hover { border-color: var(--blue); color: var(--text); }

.tl-form { display: flex; gap: 8px; align-items: flex-end; padding: 10px 14px; border-top: 1px solid var(--line); }
.tl-form textarea {
  flex: 1; resize: none; background: var(--panel-2); color: var(--text); border: 1px solid var(--line); border-radius: 10px;
  padding: 9px 11px; font: 14px/1.4 var(--sans); outline: none; max-height: 120px;
}
.tl-form textarea:focus { border-color: var(--blue); }
.tl-form button { width: 40px; height: 40px; border-radius: 10px; border: 0; background: var(--blue); color: #06101f; font-size: 16px; }
.tl-form button:disabled { opacity: .4; cursor: wait; }
.tl-note { margin: 0; padding: 0 14px 12px; color: var(--faint); font-size: 11px; }

@media (max-width: 1000px) {
  .tl { width: 100vw; border-left: 0; }
  .tl-fab { right: 12px; padding: 12px; bottom: calc(var(--sheet-visible, 0px) + 12px); transition: bottom .25s ease; }
  body.sheet-full .tl-fab { display: none; }
  .tl-fab span { display: none; }
  .tl-form { padding-bottom: max(10px, env(safe-area-inset-bottom)); }
}
