:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-2: #273449;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --ok: #22c55e;
  --warn: #f59e0b;
  --bad: #ef4444;
  --radius: 12px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

button, input, select, textarea { font: inherit; }

button {
  background: var(--accent);
  color: #0b1220;
  border: 0;
  border-radius: 10px;
  padding: 10px 16px;
  font-weight: 600;
  cursor: pointer;
}
button.secondary { background: var(--panel-2); color: var(--text); }
button.danger { background: var(--bad); color: #fff; }
button:disabled { opacity: .5; cursor: default; }

input, select, textarea {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid #334155;
  border-radius: 8px;
  padding: 9px 10px;
}

label { display: block; font-size: 13px; color: var(--muted); margin: 10px 0 4px; }

.card {
  background: var(--panel);
  border-radius: var(--radius);
  padding: 16px;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--bad); min-height: 1.2em; }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  background: var(--panel-2);
}
.badge.ok { background: rgba(34,197,94,.2); color: var(--ok); }
.badge.warn { background: rgba(245,158,11,.2); color: var(--warn); }
.badge.bad { background: rgba(239,68,68,.2); color: var(--bad); }
.badge.info { background: rgba(56,189,248,.2); color: var(--accent); }

/* ---------- Login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-wrap .card { width: 100%; max-width: 360px; }
.login-wrap h1 { margin: 0 0 4px; font-size: 22px; }
.login-wrap button { width: 100%; margin-top: 16px; }

/* ---------- App del oficial ---------- */
.officer { display: flex; flex-direction: column; height: 100dvh; }
.officer header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; background: var(--panel);
}
.officer header strong { font-size: 16px; }
.officer #map { flex: 1; min-height: 200px; }
.officer .sheet { padding: 16px; background: var(--panel); display: grid; gap: 12px; }
.officer .target-name { font-size: 22px; font-weight: 700; margin: 0; }
.officer .big { width: 100%; padding: 16px; font-size: 18px; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { background: var(--panel-2); border-radius: 10px; padding: 8px; text-align: center; }
.stat b { display: block; font-size: 20px; }
.stat span { font-size: 12px; color: var(--muted); }
.banner { padding: 10px 12px; border-radius: 10px; font-weight: 600; }
.banner.ok { background: rgba(34,197,94,.18); color: var(--ok); }
.banner.warn { background: rgba(245,158,11,.18); color: var(--warn); }
.banner.bad { background: rgba(239,68,68,.18); color: var(--bad); }
.banner.info { background: rgba(56,189,248,.15); color: var(--accent); }
.progress { height: 10px; background: var(--panel-2); border-radius: 999px; overflow: hidden; }
.progress > div { height: 100%; background: var(--ok); transition: width .5s; }
.progress.indeterminate > div { width: 30%; animation: slide 1.4s infinite ease-in-out; }
@keyframes slide { 0% { margin-left: -30%; } 100% { margin-left: 100%; } }

/* Botón SOS: se llena mientras se mantiene presionado */
.sos-btn {
  position: relative; overflow: hidden; background: var(--bad); color: #fff;
  font-weight: 800; letter-spacing: 1px; padding: 10px 18px; border-radius: 999px;
  touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.sos-btn span { position: relative; z-index: 1; }
.sos-btn::before {
  content: ''; position: absolute; inset: 0; background: #7f1d1d;
  transform: scaleX(0); transform-origin: left;
}
.sos-btn.holding::before { transform: scaleX(1); transition: transform 2s linear; }

.overlay {
  position: fixed; inset: 0; z-index: 2000; display: grid; place-items: center; padding: 16px;
  background: rgba(15, 23, 42, .92);
}
.overlay.sos { z-index: 2100; background: rgba(127, 29, 29, .95); }
.overlay.safety { animation: flash 1.5s infinite; }
@keyframes flash { 0%, 100% { background: rgba(15, 23, 42, .94); } 50% { background: rgba(120, 53, 15, .94); } }
.overlay-box { width: 100%; max-width: 420px; text-align: center; display: grid; gap: 12px; }
.overlay-box h2 { margin: 0; font-size: 30px; }
.overlay-box p { margin: 0; font-size: 18px; }
.overlay-icon { font-size: 64px; }
.overlay .big { width: 100%; padding: 18px; font-size: 20px; }
.ok-btn { background: var(--ok); color: #052e16; }

/* ---------- Administración ---------- */
.admin header {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 16px; background: var(--panel);
}
.admin header h1 { font-size: 18px; margin: 0; }
.tabs { display: flex; gap: 4px; flex-wrap: wrap; flex: 1; }
.tabs button { background: transparent; color: var(--muted); padding: 8px 12px; }
.tabs button.active { background: var(--panel-2); color: var(--text); }
.admin main { padding: 16px; }
.split { display: grid; grid-template-columns: 1fr 380px; gap: 16px; }
.admin #adminMap, .admin #roundMap { height: calc(100vh - 110px); min-height: 420px; border-radius: var(--radius); }
.admin #roundMap { height: 420px; }
.side { display: grid; gap: 16px; align-content: start; max-height: calc(100vh - 110px); overflow: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 8px; border-bottom: 1px solid #334155; }
th { color: var(--muted); font-weight: 500; }
tr.clickable { cursor: pointer; }
tr.clickable:hover { background: var(--panel-2); }
.list-item { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid #334155; }
.list-item:last-child { border-bottom: 0; }
.list-item button { padding: 6px 10px; font-size: 13px; }
.settings-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 4px 16px; }
.hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
.filters { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }
.filters > div { min-width: 170px; }
.filter-actions { display: flex; gap: 8px; }
.table-scroll { overflow-x: auto; }
.table-scroll td { white-space: nowrap; }
.hour-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.hour {
  background: var(--panel); border-radius: 10px; padding: 10px; border-left: 5px solid #475569;
  font-size: 13px; line-height: 1.45; min-height: 92px;
}
.hour.clickable { cursor: pointer; }
.hour.clickable:hover { background: var(--panel-2); }
.hour b { font-size: 16px; }
.hour .st { font-weight: 600; }
.st-cumplida { border-color: var(--ok) !important; background: var(--ok); }
.st-incompleta { border-color: var(--warn) !important; background: var(--warn); }
.st-no_realizada, .st-atrasada { border-color: var(--bad) !important; background: var(--bad); }
.st-en_curso, .st-pendiente { border-color: var(--accent) !important; background: var(--accent); }
.st-futura, .st-programada, .st-sin_datos { border-color: #475569 !important; background: #475569; }
.hour[class*="st-"] { background: var(--panel); }
.hour.st-cumplida .st { color: var(--ok); }
.hour.st-incompleta .st { color: var(--warn); }
.hour.st-no_realizada .st, .hour.st-atrasada .st { color: var(--bad); }
.hour.st-en_curso .st, .hour.st-pendiente .st { color: var(--accent); }
.alert-bar { display: grid; gap: 8px; padding: 12px 16px 0; }
.alert-item {
  display: flex; gap: 12px; align-items: flex-start; justify-content: space-between;
  background: rgba(239,68,68,.15); border: 2px solid var(--bad); border-radius: var(--radius);
  padding: 12px 14px; animation: pulse 1.6s infinite;
}
.alert-item strong { color: var(--bad); font-size: 16px; }
.alert-item pre { margin: 6px 0 0; font: inherit; font-size: 13px; white-space: pre-wrap; color: var(--text); }
.alert-item button { background: var(--bad); color: #fff; white-space: nowrap; }
@keyframes pulse { 0%, 100% { box-shadow: 0 0 0 0 rgba(239,68,68,.5); } 50% { box-shadow: 0 0 0 6px rgba(239,68,68,0); } }
.legend { display: flex; flex-wrap: wrap; gap: 14px; margin-top: 12px; font-size: 13px; color: var(--muted); }
.legend i { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.big-number { font-size: 28px; font-weight: 700; }
.coords { color: var(--accent); font-size: 13px; text-decoration: none; }
.coords:hover { text-decoration: underline; }

@media (max-width: 900px) {
  .split { grid-template-columns: 1fr; }
  .admin #adminMap { height: 55vh; }
  .side { max-height: none; }
}
.alert-item.sos { background: rgba(239,68,68,.4); border-width: 3px; }
.alert-item.sos strong { color: #fff; font-size: 18px; }
.route-select { width: auto; max-width: 320px; font-weight: 600; }
.check-list { display: grid; gap: 4px; max-height: 240px; overflow: auto; background: var(--bg); border: 1px solid #334155; border-radius: 8px; padding: 8px; }
.check-list label { display: flex; gap: 8px; align-items: center; margin: 0; color: var(--text); font-size: 14px; }
.check-list input { width: auto; }
.route-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; }
.route-card { background: var(--panel); border-radius: var(--radius); padding: 14px; cursor: pointer; border-left: 5px solid #475569; }
.route-card:hover { background: var(--panel-2); }
.route-card .big-number { font-size: 24px; }

/* Justificación de rondas */
.justify-box { text-align: left; max-height: 100%; overflow: auto; gap: 8px; }
.justify-box h2 { text-align: center; font-size: 26px; }
.justify-box label { margin: 6px 0 0; }
.reason-list { display: grid; gap: 6px; }
.reason-list label {
  display: flex; gap: 10px; align-items: center; margin: 0; padding: 12px; font-size: 16px; color: var(--text);
  background: var(--panel); border: 2px solid transparent; border-radius: 10px; cursor: pointer;
}
.reason-list label:has(input:checked) { border-color: var(--accent); background: var(--panel-2); }
.reason-list input { width: auto; margin: 0; }
.st-justificada { border-color: #a78bfa !important; background: #a78bfa; }
.hour.st-justificada .st { color: #a78bfa; }
.badge.purple { background: rgba(167,139,250,.2); color: #a78bfa; }
.tabs .count { background: var(--bad); color: #fff; border-radius: 999px; padding: 0 7px; font-size: 12px; margin-left: 4px; }
#justifyOverlay { background: var(--bg); place-items: start center; overflow: auto; }

/* ---------- Análisis (tablero) ---------- */
/* Colores por función: estados (resultado de la hora) y una sola escala azul para magnitudes.
   Validados contra la superficie del panel (#1e293b) con el validador de paletas. */
.viz {
  --viz-surface: #1e293b;
  --viz-grid: #2f3d52;
  --viz-axis: #475569;
  --viz-muted: #94a3b8;
  --st-cumplida: #0ca30c;
  --st-justificada: #9085e9;
  --st-incompleta: #fab219;
  --st-no_realizada: #d03b3b;
  --seq: #3987e5;
}
.viz-filters { display: flex; flex-wrap: wrap; gap: 10px 14px; align-items: flex-end; margin-bottom: 8px; }
.viz-presets { display: flex; gap: 2px; background: var(--panel); border-radius: 10px; padding: 3px; }
.viz-presets button { background: transparent; color: var(--muted); padding: 7px 11px; font-size: 14px; }
.viz-presets button:hover { background: var(--panel-2); color: var(--text); }
.viz-presets button.on { background: var(--panel-2); color: var(--text); font-weight: 700; }
.viz-field { display: grid; gap: 4px; margin: 0; font-size: 12px; color: var(--muted); }
.viz-field input, .viz-field select { width: auto; min-width: 150px; padding: 7px 9px; }
#anBody { transition: opacity .2s; }
#anBody.loading { opacity: .45; pointer-events: none; }
.viz-tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.viz-tile { background: var(--panel); border-radius: var(--radius); padding: 14px 16px; }
.viz-tile .v { font-size: 30px; font-weight: 700; line-height: 1.1; }
.viz-tile .l { font-size: 13px; color: var(--muted); margin-top: 4px; }
.viz-tile .s { font-size: 12px; color: var(--muted); margin-top: 2px; }
.viz-card { background: var(--panel); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 16px; min-width: 0; }
.viz-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px 14px; margin-bottom: 10px; }
.viz-head .hint { margin: 0; }
.viz-csv { margin-left: auto; padding: 5px 10px; font-size: 13px; }
.viz-grid { display: grid; grid-template-columns: 3fr 2fr; gap: 16px; }
.viz-grid > .viz-card { margin-bottom: 16px; }
@media (max-width: 1100px) { .viz-grid { grid-template-columns: 1fr; } }
.viz-chart { width: 100%; overflow-x: auto; }
.viz-chart svg { display: block; }
.viz-chart text { fill: var(--viz-muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.viz-legend { display: flex; flex-wrap: wrap; gap: 12px; font-size: 13px; color: var(--muted); }
.viz-legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; }
.viz-hit { fill: transparent; cursor: pointer; }
.viz-col:hover .seg { filter: brightness(1.18); }
.viz-cell { stroke: var(--viz-surface); stroke-width: 2px; cursor: pointer; }
.viz-cell:hover, .viz-cell:focus { stroke: var(--text); outline: none; }
.viz-tip {
  position: fixed; z-index: 3000; pointer-events: none; background: #0b1220; border: 1px solid #334155; border-radius: 8px;
  padding: 8px 10px; font-size: 13px; min-width: 150px; box-shadow: 0 6px 20px rgba(0,0,0,.35);
}
.viz-tip .t { color: var(--muted); font-size: 12px; margin-bottom: 4px; }
.viz-tip .r { display: flex; align-items: center; gap: 8px; justify-content: space-between; }
.viz-tip .r span { color: var(--muted); }
.viz-tip .r b { color: var(--text); font-variant-numeric: tabular-nums; }
.viz-tip .k { display: inline-block; width: 12px; height: 2px; margin-right: 6px; vertical-align: middle; }
.viz-table { font-size: 13px; }
.viz-table th { white-space: nowrap; cursor: pointer; user-select: none; }
.viz-table th.sorted::after { content: ' ▾'; }
.viz-table th.sorted.asc::after { content: ' ▴'; }
.viz-table td { white-space: nowrap; font-variant-numeric: tabular-nums; }
.viz-table tbody tr { cursor: pointer; }
.viz-table tbody tr:hover { background: var(--panel-2); }
.viz-table tr.sel { background: rgba(57,135,229,.18); }
.viz-meter { display: inline-flex; align-items: center; gap: 8px; }
.viz-meter span { display: inline-block; width: 90px; height: 8px; background: var(--viz-grid); border-radius: 4px; overflow: hidden; }
.viz-meter span i { display: block; height: 100%; background: var(--seq); border-radius: 4px; }
.viz-hbar { display: grid; grid-template-columns: minmax(120px, 2fr) 3fr auto; gap: 10px; align-items: center; padding: 5px 0; font-size: 13px; }
.viz-hbar .bar { height: 10px; background: var(--viz-grid); border-radius: 0 4px 4px 0; }
.viz-hbar .bar i { display: block; height: 100%; background: var(--seq); border-radius: 0 4px 4px 0; }
.viz-hbar .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.viz-hbar .val { font-variant-numeric: tabular-nums; color: var(--text); }
.viz-empty { color: var(--muted); font-size: 14px; padding: 16px 0; }
.viz-status { display: inline-flex; align-items: center; gap: 5px; }
.an-cards { margin-bottom: 16px; }
.an-cards .route-card.sel { background: var(--panel-2); box-shadow: 0 0 0 2px var(--accent) inset; }
.an-cards .route-card:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.brand { display: inline-block; font-size: 13px; font-weight: 700; color: var(--accent); letter-spacing: .5px; }
.login-wrap .brand { display: block; margin: -4px 0 6px; font-size: 15px; }
.brand-logo.big { display: block; width: 170px; max-width: 70%; height: auto; margin: 0 auto 10px; }
.brand-logo.small { height: 40px; width: auto; flex: 0 0 auto; }
.brand-row { display: flex; align-items: center; gap: 10px; min-width: 0; }
.officer header .brand-logo.small { height: 46px; }
.login-wrap h1, .login-wrap .brand { text-align: center; }
.login-wrap form > .muted:first-of-type { text-align: center; }
.powered { display: flex; align-items: center; justify-content: center; gap: 8px; margin-top: 18px; font-size: 11px; letter-spacing: 2px; text-transform: uppercase; color: var(--muted); }
.powered img { height: 30px; width: auto; }
.powered.footer { padding: 8px 16px 16px; }
.license-bar { display: grid; gap: 6px; background: var(--bg); border: 1px solid #334155; border-left: 5px solid var(--accent); border-radius: 8px; padding: 10px 12px; margin: 10px 0 12px; }
.license-bar.full { border-left-color: var(--warn); }
.license-bar .hint { margin: 0; }
.license-meter { display: block; height: 8px; background: #334155; border-radius: 4px; overflow: hidden; }
.license-meter i { display: block; height: 100%; background: var(--accent); border-radius: 4px; }
.license-bar.full .license-meter i { background: var(--warn); }
