:root {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #222220;
  --border: rgba(255, 255, 255, 0.10);
  --ink: #f0efec;
  --ink-2: #c3c2b7;
  --muted: #898781;
  --accent: #6ea8fe;

  /* Paleta de estado (fija): nunca se usa sola, siempre con etiqueta o icono. */
  --st-none: #898781;
  --st-good: #0ca30c;
  --st-warning: #fab219;
  --st-serious: #ec835a;
  --st-critical: #d03b3b;

  --sidebar: 400px;
  --topbar: 64px;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--page); color: var(--ink); }
body { display: flex; flex-direction: column; overflow: hidden; font-size: 14px; }
button { font: inherit; color: inherit; }

/* ---------- Barra superior ---------- */
.topbar {
  display: flex; align-items: center; gap: 20px; min-height: var(--topbar);
  padding: 8px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; font-size: 16px; color: var(--ink); white-space: nowrap; }
.brand svg { color: var(--accent); }

.live { display: flex; align-items: center; gap: 6px; color: var(--ink-2); font-size: 12px; white-space: nowrap; }
.live .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--st-none); }
.live[data-state="live"] .dot { background: var(--st-good); animation: pulse 2s ease-in-out infinite; }
.live[data-state="error"] .dot { background: var(--st-critical); }
@keyframes pulse { 50% { opacity: .35; } }

.kpis { display: flex; gap: 24px; margin-left: auto; overflow-x: auto; scrollbar-width: none; }
.kpi { display: flex; flex-direction: column; min-width: 0; }
.kpi .label { font-size: 11px; color: var(--muted); white-space: nowrap; }
.kpi .value { font-size: 20px; font-weight: 600; line-height: 1.2; white-space: nowrap; }
.kpi .sub { font-size: 11px; color: var(--ink-2); white-space: nowrap; }

/* ---------- Layout ---------- */
.layout { flex: 1; display: flex; min-height: 0; }
.map-wrap { position: relative; flex: 1; min-width: 0; }
#map { position: absolute; inset: 0; }

.sidebar {
  width: var(--sidebar); display: flex; flex-direction: column; min-height: 0;
  background: var(--surface); border-left: 1px solid var(--border);
}

.tabs { display: flex; border-bottom: 1px solid var(--border); }
.tabs button {
  flex: 1 1 auto; padding: 12px 6px; background: none; border: 0; border-bottom: 2px solid transparent;
  color: var(--ink-2); cursor: pointer; font-size: 13px; white-space: nowrap;
}
.tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); }
.badge {
  display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--surface-2); color: var(--ink-2); font-size: 11px; line-height: 18px;
}

.panel { flex: 1; overflow-y: auto; padding: 8px 12px 16px; }
.panel[hidden] { display: none; }

.disclaimer { padding: 10px 12px; font-size: 11px; color: var(--muted); border-top: 1px solid var(--border); line-height: 1.4; }

/* ---------- Listas ---------- */
.section-title { margin: 14px 2px 6px; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.empty { padding: 24px 8px; color: var(--muted); text-align: center; line-height: 1.5; }

.item {
  display: block; width: 100%; text-align: left; padding: 10px 10px 10px 12px; margin: 6px 0;
  background: var(--surface-2); border: 1px solid var(--border); border-left: 3px solid var(--st-none);
  border-radius: 6px; cursor: pointer;
}
.item:hover { border-color: rgba(255, 255, 255, 0.22); }
.item:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.item .title { font-weight: 600; line-height: 1.3; }
.item .desc { margin-top: 3px; color: var(--ink-2); font-size: 12.5px; line-height: 1.4; }
.item .meta { margin-top: 5px; color: var(--muted); font-size: 11.5px; display: flex; gap: 8px; flex-wrap: wrap; }
.item.resolved { opacity: .6; }

.sev-baja { border-left-color: var(--st-none); }
.sev-media { border-left-color: var(--st-warning); }
.sev-alta { border-left-color: var(--st-serious); }
.sev-muy-alta { border-left-color: var(--st-critical); }

.pill { display: inline-flex; align-items: center; gap: 4px; font-size: 11px; font-weight: 600; color: var(--ink-2); }
.pill::before { content: ""; width: 8px; height: 8px; border-radius: 2px; background: currentColor; }
.pill.sev-baja { color: var(--st-none); }
.pill.sev-media { color: var(--st-warning); }
.pill.sev-alta { color: var(--st-serious); }
.pill.sev-muy-alta { color: var(--st-critical); }

.row { display: flex; align-items: center; gap: 8px; }
.vrow { display: grid; grid-template-columns: 22px 1fr auto; gap: 4px 10px; align-items: center; padding: 8px 10px; border-left-width: 1px; }
.vrow .plate { width: 22px; height: 22px; grid-row: span 2; }
.vrow .delay { font-weight: 600; font-size: 13px; white-space: nowrap; }
.vrow .sub { grid-column: 2 / 4; color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

#vehicle-search {
  width: 100%; margin: 6px 0 4px; padding: 9px 11px; border-radius: 6px;
  background: var(--page); color: var(--ink); border: 1px solid var(--border);
}

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0 4px; }
.chip {
  padding: 5px 10px; border-radius: 14px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--ink-2); cursor: pointer; font-size: 12px;
}
.chip[aria-pressed="true"] { background: var(--ink); color: var(--page); border-color: var(--ink); }

/* Selección de alertas */
.item.selected { outline: 2px solid var(--accent); outline-offset: -2px; background: rgba(110, 168, 254, 0.08); }
.item .check {
  margin-left: auto; width: 20px; height: 20px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center;
  background: var(--accent); color: #0d0d0d; font-size: 12px; font-weight: 700;
}
.vrow .check { display: inline-flex; width: 16px; height: 16px; margin: 0 4px 0 0; font-size: 10px; vertical-align: 1px; }
.alerts-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 2px 2px 4px; min-height: 32px; }
.switch { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; color: var(--ink-2); cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch .track { position: relative; width: 36px; height: 20px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--border); transition: background .15s; }
.switch .track::after { content: ""; position: absolute; top: 2px; left: 2px; width: 14px; height: 14px; border-radius: 50%; background: var(--ink-2); transition: transform .15s; }
.switch input:checked + .track { background: var(--accent); border-color: var(--accent); }
.switch input:checked + .track::after { transform: translateX(16px); background: #0d0d0d; }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn-ghost { padding: 6px 12px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface-2); color: var(--ink); cursor: pointer; font-size: 13px; }
.btn-ghost:hover { border-color: rgba(255, 255, 255, 0.3); }
.hint { margin: 2px 2px 8px; font-size: 12px; line-height: 1.4; color: var(--muted); }
.focus-banner {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%); z-index: 2;
  display: flex; align-items: center; gap: 12px; padding: 6px 6px 6px 14px; font-size: 13px; white-space: nowrap;
}
.focus-banner[hidden] { display: none; }
.focus-banner .btn { padding: 6px 12px; }

/* Fuentes */
.src { padding: 10px 2px; border-bottom: 1px solid var(--border); }
.src .name { font-weight: 600; }
.src .stat { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 3px; font-size: 12px; color: var(--ink-2); }
.src .err { margin-top: 3px; font-size: 12px; color: var(--st-serious); }
.src a { color: var(--muted); font-size: 11.5px; word-break: break-all; }
.status { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; }
.status::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.status.ok { color: var(--st-good); }
.status.ko { color: var(--st-critical); }
.status.off, .status.wait { color: var(--st-none); }

/* ---------- Panel de capas sobre el mapa ---------- */
.card { background: rgba(26, 26, 25, 0.94); border: 1px solid var(--border); border-radius: 8px; backdrop-filter: blur(6px); }
.layers { position: absolute; top: 12px; left: 12px; width: 230px; max-height: calc(100% - 24px); overflow-y: auto; font-size: 13px; }
.layers-toggle { display: block; width: 100%; padding: 9px 12px; text-align: left; background: none; border: 0; font-weight: 600; cursor: pointer; }
.layers-toggle::after { content: "▾"; float: right; color: var(--muted); }
.layers-toggle[aria-expanded="false"]::after { content: "▸"; margin-left: 12px; }
.layers.collapsed { width: auto; }
.layers-body { padding: 0 12px 10px; }
.layers-body[hidden] { display: none; }
.layers h4 { margin: 8px 0 4px; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.layers label { display: flex; align-items: center; gap: 8px; padding: 3px 0; cursor: pointer; color: var(--ink-2); }
.layers label .count { margin-left: auto; color: var(--muted); font-size: 11.5px; }
.legend { display: grid; grid-template-columns: 18px 1fr; gap: 4px 8px; align-items: center; color: var(--ink-2); font-size: 12px; }
.legend i { width: 10px; height: 10px; border-radius: 50%; justify-self: center; }
.legend svg { justify-self: center; }
.legend .plate { width: 18px; height: 18px; }

/* ---------- Ficha del elemento seleccionado ---------- */
.detail {
  position: absolute; top: 84px; right: 12px; z-index: 3; width: 320px; max-height: calc(100% - 150px); overflow-y: auto;
  padding: 12px 14px; font-size: 13px;
}
.detail[hidden] { display: none; }
.detail-close {
  position: absolute; top: 4px; right: 6px; padding: 4px 8px; border: 0; background: none;
  color: var(--ink-2); font-size: 22px; line-height: 1; cursor: pointer;
}
.detail-close:hover { color: var(--ink); }
.pop h3 { margin: 0 28px 6px 0; font-size: 15px; }
.pop dl { display: grid; grid-template-columns: auto 1fr; gap: 3px 10px; margin: 0; }
.pop dt { color: var(--muted); }
.pop dd { margin: 0; }
.pop .note { margin-top: 8px; color: var(--muted); font-size: 11.5px; line-height: 1.45; }
.pop a { color: var(--accent); }

.maplibregl-ctrl-attrib { background: rgba(13, 13, 13, 0.7) !important; color: var(--muted); }
.maplibregl-ctrl-attrib a { color: var(--ink-2); }
/* Plegada en el móvil: MapLibre la pone en negro sobre blanco; aquí, clara sobre oscuro. */
.maplibregl-ctrl-attrib.maplibregl-compact { color: var(--ink-2); background: rgba(13, 13, 13, 0.85) !important; }
.maplibregl-ctrl-attrib-button { background-color: var(--ink-2) !important; }
@media (max-width: 860px) { .maplibregl-ctrl-attrib { font-size: 10px; } }

/* ---------- Aviso de cobertura ---------- */
.notice {
  width: calc(100% - 32px); max-width: 540px; padding: 20px 22px;
  background: var(--surface); color: var(--ink); border: 1px solid var(--border); border-radius: 10px;
}
.notice::backdrop { background: rgba(0, 0, 0, 0.6); }
.notice h2 { margin: 0 0 10px; font-size: 18px; }
.notice p, .notice li { color: var(--ink-2); line-height: 1.5; }
.notice ul { padding-left: 18px; margin: 8px 0; }
.notice li { margin: 6px 0; }
.notice strong { color: var(--ink); }
.notice a { color: var(--accent); }
.notice .actions { display: flex; justify-content: flex-end; margin: 16px 0 0; }
.btn { padding: 9px 18px; border: 0; border-radius: 6px; background: var(--accent); color: #0d0d0d; font-weight: 600; cursor: pointer; }
.btn:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
.link { padding: 0; border: 0; background: none; color: var(--ink-2); font-size: inherit; text-decoration: underline; cursor: pointer; }

/* ---------- Página de aviso legal y fuentes ---------- */
body.doc { overflow: auto; display: block; }
.doc .brand { text-decoration: none; }
.doc .back { margin-left: auto; color: var(--ink-2); font-size: 13px; }
.doc-body { max-width: 980px; margin: 0 auto; padding: 24px 16px 64px; line-height: 1.55; color: var(--ink-2); }
.doc-body h1 { color: var(--ink); font-size: 26px; margin: 8px 0 12px; }
.doc-body h2 { color: var(--ink); font-size: 18px; margin: 32px 0 8px; }
.doc-body strong { color: var(--ink); }
.doc-body a { color: var(--accent); }
.doc-body .muted { color: var(--muted); font-size: 13px; }
.doc-body .toc { font-size: 13px; }
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 8px; }
.doc-body table { width: 100%; border-collapse: collapse; font-size: 13px; }
.doc-body th, .doc-body td { padding: 8px 10px; text-align: left; vertical-align: top; border-bottom: 1px solid var(--border); }
.doc-body th { color: var(--ink); background: var(--surface); font-weight: 600; }
.doc-body tr:last-child td { border-bottom: 0; }
/* En el móvil las tablas pasan a fichas apiladas (legal.html pone data-label en cada celda). */
@media (max-width: 640px) {
  .doc-body thead { display: none; }
  .doc-body tr { display: block; padding: 10px 12px; border-bottom: 1px solid var(--border); }
  .doc-body tr:last-child { border-bottom: 0; }
  .doc-body td { display: block; padding: 2px 0; border: 0; }
  .doc-body td:first-child { padding-bottom: 4px; color: var(--ink); font-weight: 600; }
  .doc-body td[data-label]:not(:first-child)::before { content: attr(data-label) ": "; color: var(--muted); }
}

.sheet-handle { display: none; }

/* ---------- Móvil ----------
   El mapa ocupa toda la pantalla y el panel lateral pasa a ser una hoja que sube desde abajo
   (sólo pestañas, media pantalla o entera; ver app.js). Los indicadores van en una fila que
   se desliza de lado. */
@media (max-width: 860px) {
  :root { --peek: 65px; --sheet-half: 55%; }  /* --sheet-half = SHEET_HALF de app.js */
  body { height: 100dvh; }

  .topbar { flex-wrap: wrap; gap: 2px 12px; min-height: 0; padding: 6px 12px 4px; }
  .brand { font-size: 15px; }
  .live { margin-left: auto; }
  .kpis {
    order: 3; width: calc(100% + 24px); margin: 0 -12px; padding: 0 12px 2px; gap: 18px;
    -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 32px), transparent);
    mask-image: linear-gradient(to right, #000 calc(100% - 32px), transparent);
  }
  .kpi { flex: none; }
  .kpi .label { font-size: 10.5px; }
  .kpi .value { font-size: 16px; }
  .kpi .sub { display: none; }

  .layout { position: relative; }
  .map-wrap { position: absolute; inset: 0 0 var(--peek); }
  .sidebar {
    position: absolute; left: 0; right: 0; bottom: 0; z-index: 5; width: auto; overflow: hidden;
    height: var(--peek); min-height: var(--peek); max-height: 100%;
    border-left: 0; border-top: 1px solid var(--border); border-radius: 14px 14px 0 0;
    box-shadow: 0 -6px 24px rgba(0, 0, 0, 0.45); transition: height .25s ease;
  }
  .sidebar[data-sheet="half"] { height: var(--sheet-half); }
  .sidebar[data-sheet="full"] { height: 100%; border-radius: 0; }
  .sidebar.dragging { transition: none; }
  .sheet-handle {
    display: flex; flex: none; align-items: center; justify-content: center; width: 100%; height: 20px;
    padding: 0; border: 0; background: none; cursor: grab; touch-action: none;
  }
  .sheet-handle span { width: 40px; height: 4px; border-radius: 2px; background: var(--muted); }
  .tabs { flex: none; }
  .tabs button { height: 43px; padding: 0 6px; }
  .panel { overscroll-behavior: contain; }
  .disclaimer { padding: 6px 12px; font-size: 10.5px; line-height: 1.35; }

  .layers { top: 10px; left: 10px; width: 270px; max-width: calc(100% - 64px); max-height: calc(100% - 20px); }
  .layers.collapsed { width: auto; }
  .focus-banner { top: auto; bottom: 10px; left: 10px; right: 10px; transform: none; justify-content: space-between; }
  .detail { top: auto; bottom: 10px; left: 8px; right: 8px; width: auto; max-height: 45%; }
  /* Con el aviso de selección abajo, la ficha y los controles del mapa suben por encima. */
  .map-wrap:has(> .focus-banner:not([hidden])) .detail { bottom: 64px; }
  .map-wrap:has(> .focus-banner:not([hidden])) .maplibregl-ctrl-bottom-left,
  .map-wrap:has(> .focus-banner:not([hidden])) .maplibregl-ctrl-bottom-right { bottom: 54px; }
}
