/* ==========================================================
   Visor Zonas de Descanso · estilos
   ========================================================== */
:root {
  --blue: #0b4f9c;
  --blue-600: #0a4386;
  --blue-50: #eaf2fb;
  --red: #e4252e;
  --accent: #f07c00;
  --accent-600: #d36c00;
  --ink: #1b2430;
  --ink-2: #4a5566;
  --ink-3: #7b8696;
  --line: #e2e7ee;
  --bg: #f4f6f9;
  --card: #ffffff;
  --ok: #15803d;
  --warn: #b45309;
  --err: #b91c1c;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --sidebar-w: 360px;
  --topbar-h: 64px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  color: var(--ink);
  background: var(--bg);
  overflow: hidden;
}
code {
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 12px;
  background: var(--blue-50);
  color: var(--blue-600);
  padding: 1px 4px;
  border-radius: 4px;
}

/* ---------- Topbar ---------- */
.topbar {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 16px;
  background: #fff;
  border-bottom: 1px solid var(--line);
  position: relative;
  z-index: 1001;
}
.topbar::after {
  content: "";
  position: absolute;
  left: 0; right: 0; bottom: -1px;
  height: 3px;
  background: linear-gradient(90deg, var(--blue) 0 35%, var(--red) 35% 100%);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-logo { height: 46px; width: auto; border-radius: 4px; }
.brand-text { min-width: 0; }
.brand-text h1 {
  font-size: 17px; font-weight: 700; margin: 0; line-height: 1.2;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.brand-text p {
  margin: 2px 0 0; color: var(--ink-3); font-size: 12.5px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.topbar-actions { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.status {
  font-size: 12.5px; color: var(--ink-2);
  background: var(--bg); border: 1px solid var(--line);
  padding: 5px 10px; border-radius: 999px;
  max-width: 340px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.status.busy::before {
  content: ""; display: inline-block; width: 10px; height: 10px; margin-right: 6px;
  border: 2px solid var(--blue); border-right-color: transparent; border-radius: 50%;
  vertical-align: -1px; animation: spin .8s linear infinite;
}
.status.error {
  color: var(--err); border-color: #f5c2c2; background: #fff5f5;
  white-space: normal; max-width: 520px; line-height: 1.25; border-radius: 10px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Layout ---------- */
.layout {
  height: calc(100% - var(--topbar-h));
  display: flex;
}
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  overflow-y: auto;
  padding: 12px;
  display: flex; flex-direction: column; gap: 12px;
  border-right: 1px solid var(--line);
  background: var(--bg);
  transition: margin-left .25s ease;
}
.sidebar.hidden { margin-left: calc(-1 * var(--sidebar-w)); }
.map-wrap { position: relative; flex: 1; min-width: 0; }
#map { position: absolute; inset: 0; background: #dfe6ee; }

/* ---------- Cards ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
}
.card-title {
  font-size: 12px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase;
  color: var(--blue); margin: 0 0 10px;
}
.hint { color: var(--ink-3); font-size: 12.5px; margin: 0 0 8px; line-height: 1.45; }

/* ---------- Layer list ---------- */
.layer-list { display: flex; flex-direction: column; gap: 2px; margin-bottom: 8px; }
.layer-item {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 6px; border-radius: 6px; cursor: pointer;
}
.layer-item:hover { background: var(--bg); }
.layer-item input { accent-color: var(--blue); margin: 0; }
.layer-item .swatch {
  width: 12px; height: 12px; border-radius: 50%; flex-shrink: 0;
  border: 2px solid rgba(255,255,255,.9); box-shadow: 0 0 0 1px rgba(0,0,0,.25);
}
.layer-item .swatch.line { border-radius: 2px; height: 4px; border: 0; box-shadow: none; }
.layer-item .swatch.dash {
  width: 18px; height: 3px; border-radius: 0;
  background: repeating-linear-gradient(90deg, var(--c) 0 5px, transparent 5px 8px);
  background-size: 8px 100%;
  animation: dash-swatch .8s linear infinite;
}
@keyframes dash-swatch { to { background-position: 8px 0; } }

/* Conexiones estación → tramo: segmentado que avanza hacia el tramo */
.link-anim {
  stroke-dasharray: 8 6;
  stroke-linecap: butt;
  animation: dash-flow 0.9s linear infinite;
  filter: drop-shadow(0 0 2px rgba(0, 230, 118, .7));
}
@keyframes dash-flow { to { stroke-dashoffset: -14; } }
/* Línea recta provisoria (mientras llega el trazado por camino o si el ruteo no está disponible) */
.link-anim.link-recta { stroke-dasharray: 3 7; opacity: .75; filter: none; }

/* Pulso sobre el servicio prioritario */
.pulso-servicio {
  transform-box: fill-box; transform-origin: center;
  animation: pulso 1.6s ease-out infinite;
}
@keyframes pulso {
  0% { transform: scale(.6); opacity: 1; }
  100% { transform: scale(1.6); opacity: 0; }
}
@media (prefers-reduced-motion: reduce) {
  .link-anim, .layer-item .swatch.dash, .pulso-servicio { animation: none; }
}

.grupo-check { border: 0; margin: 0 0 6px; padding: 0; }
.grupo-check legend { font-size: 12px; color: var(--ink-2); margin-bottom: 4px; padding: 0; }
.grupo-check .check { margin-bottom: 4px; }
.dot-tipo { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.layer-item .name { flex: 1; min-width: 0; line-height: 1.35; overflow-wrap: anywhere; }
.layer-item .count {
  font-size: 11.5px; color: var(--ink-3); background: var(--bg);
  border-radius: 999px; padding: 1px 7px;
}
.layer-item .count.err { color: var(--err); background: #fff5f5; }
.layer-err {
  margin: -2px 6px 6px 46px; font-size: 12px; line-height: 1.35; color: var(--err); overflow-wrap: anywhere;
}
.layer-group-label {
  font-size: 11px; color: var(--ink-3); font-weight: 600; margin: 6px 0 2px 6px;
}

/* ---------- Form controls ---------- */
textarea, input[type="number"], input[type="search"] {
  width: 100%;
  font: inherit;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  background: #fff;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
textarea {
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 12.5px; resize: vertical; min-height: 100px;
}
textarea:focus, input:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(11, 79, 156, .15);
}
.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--ink-2); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 10px; align-items: end; }
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; margin-bottom: 12px; cursor: pointer; }
.check input { accent-color: var(--blue); }
.row { display: flex; gap: 8px; align-items: center; justify-content: space-between; margin-top: 8px; }

.btn {
  font: inherit; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid transparent; border-radius: 8px;
  padding: 8px 14px; cursor: pointer;
  transition: background .15s, opacity .15s, transform .05s;
}
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--blue); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--blue-600); }
.btn-accent { background: var(--accent); color: #fff; }
.btn-accent:hover:not(:disabled) { background: var(--accent-600); }
.btn-ghost { background: transparent; color: var(--ink-2); }
.btn-ghost:hover { background: var(--bg); }
.btn-block { width: 100%; padding: 10px 14px; }
.btn-sm { padding: 5px 10px; font-size: 12.5px; }
.btn-excel { background: #1d6f42; color: #fff; }
.btn-pdf { background: #c62828; color: #fff; }
.icon-only { padding: 6px; }

.file-btn {
  display: inline-flex; align-items: center; justify-content: center;
  font-weight: 600; font-size: 13px; color: var(--blue);
  border: 1px dashed #b6c6dc; border-radius: 8px; padding: 7px 10px;
  cursor: pointer; width: 100%;
  transition: background .15s;
}
.file-btn:hover { background: var(--blue-50); }
.file-btn.small { width: auto; font-size: 12.5px; }

[hidden] { display: none !important; }

/* ---------- Buscador y filtros ---------- */
.buscador-wrap { position: relative; margin-bottom: 6px; }
.buscador-icono { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--ink-3); pointer-events: none; }
#buscador { padding-left: 32px; font-size: 13.5px; }
.sugerencias {
  position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 20;
  list-style: none; margin: 0; padding: 4px;
  background: #fff; border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 10px 30px rgba(16, 24, 40, .18);
  max-height: 340px; overflow-y: auto;
}
.sugerencias li {
  display: flex; align-items: flex-start; gap: 8px;
  padding: 7px 8px; border-radius: 6px; cursor: pointer; line-height: 1.35;
}
.sugerencias li.activa, .sugerencias li[data-i]:hover { background: var(--blue-50); }
.sugerencias .sug-texto { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.sugerencias .sug-texto small { display: block; color: var(--ink-3); font-size: 11.5px; }
.sugerencias .sug-vacia { color: var(--ink-3); font-size: 12.5px; cursor: default; }
.sug-tipo {
  flex-shrink: 0; margin-top: 1px;
  font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em;
  padding: 2px 6px; border-radius: 4px; background: var(--bg); color: var(--ink-2);
}
.sug-tipo.t-ruta { background: #fdecea; color: #b42318; }
.sug-tipo.t-macrozona, .sug-tipo.t-region, .sug-tipo.t-comuna { background: var(--blue-50); color: var(--blue-600); }
.sug-tipo.t-propuesta { background: #fff4e5; color: #b45309; }
.sug-tipo.t-estacion { background: #f1f5f9; color: var(--ink-2); }

select {
  width: 100%; font: inherit; font-size: 13px; color: var(--ink);
  border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px; background: #fff;
}
select:focus { outline: none; border-color: var(--blue); box-shadow: 0 0 0 3px rgba(11, 79, 156, .15); }
select:disabled { background: var(--bg); color: var(--ink-3); }
.card .field + .field { margin-top: 8px; }
.card > .field:first-of-type { margin-top: 4px; }
.hint.aviso { color: var(--warn); margin: 8px 0 0; }
.hint.aviso:empty { display: none; }

.chips-filtro { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.chips-filtro:empty { display: none; }
.fchip {
  font: inherit; font-size: 12px; font-weight: 600;
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid #b6c6dc; background: var(--blue-50); color: var(--blue-600);
  border-radius: 999px; padding: 3px 10px; cursor: pointer;
}
.fchip:hover { background: #dbe8f7; }
.fchip.limpiar { background: #fff; border-color: var(--line); color: var(--ink-2); }

.contador { margin-top: 12px; border-top: 1px solid var(--line); padding-top: 10px; }
.contador-head { font-size: 12px; color: var(--ink-2); margin-bottom: 6px; line-height: 1.4; }
.stat { display: flex; align-items: center; gap: 8px; padding: 4px 0; font-size: 12.5px; }
.stat-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.stat-label { flex: 1; min-width: 0; }
.stat-num { white-space: nowrap; font-variant-numeric: tabular-nums; }
.stat-num b { font-size: 15px; color: var(--ink); }
.stat-num small { color: var(--ink-3); font-size: 11.5px; }

/* ---------- Tramos list ---------- */
.tramos-list { list-style: none; margin: 10px 0 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.tramos-list li {
  display: flex; align-items: flex-start; gap: 8px;
  padding: 7px 9px; border-radius: 6px; font-size: 12.5px; line-height: 1.4;
  background: var(--bg); cursor: pointer;
}
.tramos-list li:hover { background: var(--blue-50); }
.tramos-list .dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; margin-top: 4px; }
.tramos-list .t-body { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.tramos-list .t-sub { color: var(--ink-3); }
.tramos-list .t-warn { color: var(--warn); font-size: 12px; }
.tramos-list li.err { background: #fff5f5; color: var(--err); cursor: default; }
.tramos-list li.err .t-sub { color: var(--err); opacity: .8; }

/* ---------- Results panel ---------- */
.results {
  position: absolute; left: 12px; right: 12px; bottom: 12px;
  z-index: 1000;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(16, 24, 40, .18);
  display: flex; flex-direction: column;
  max-height: 55%;
  transition: max-height .25s ease;
}
.results.collapsed { max-height: 48px; }
.results.collapsed .results-body { display: none; }
.results.collapsed .results-toggle svg { transform: rotate(180deg); }
.results-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 7px 10px; border-bottom: 1px solid var(--line); flex-shrink: 0;
}
.results-toggle {
  font: inherit; font-weight: 700; color: var(--ink);
  background: none; border: 0; cursor: pointer;
  display: flex; align-items: center; gap: 6px; padding: 4px 6px; border-radius: 6px;
}
.results-toggle:hover { background: var(--bg); }
.results-toggle svg { transition: transform .2s; }
.results-actions { display: flex; align-items: center; gap: 6px; }
.results-actions input[type="search"] { width: 170px; padding: 5px 9px; font-size: 12.5px; }
.results-body { overflow: auto; flex: 1; min-height: 0; }

/* Indicadores del análisis */
.resumen { padding: 10px 10px 4px; }
.resumen:empty { display: none; }
.kpis { display: flex; flex-wrap: wrap; gap: 8px; }
.kpi {
  flex: 1 1 140px; min-width: 0;
  border: 1px solid var(--line); border-left: 4px solid var(--blue);
  border-radius: 8px; padding: 7px 10px; background: #fff; line-height: 1.3;
}
.kpi b { display: block; font-size: 20px; color: var(--ink); }
.kpi span { font-size: 12px; color: var(--ink-2); }
.kpi-ok { border-left-color: var(--ok); }
.kpi-medio { border-left-color: #f59f00; }
.kpi-brecha { border-left-color: var(--warn); background: #fffaf0; }
.kpi-brecha b { color: var(--warn); }
.kpi-tipos { flex: 2 1 260px; border-left-color: var(--linkc, #00e676); display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px; }
.kpi-tipos > span { width: 100%; }
.kpi-tipos b { display: inline; font-size: 15px; margin-left: 2px; }
.nota-metodo { margin: 8px 2px 2px; font-size: 12px; color: var(--ink-3); }
#trazadosEstado { color: var(--blue); font-weight: 600; }

.pill-tipo {
  display: inline-block; font-size: 11px; font-weight: 700; line-height: 1.3;
  padding: 1px 7px; border-radius: 999px; white-space: nowrap;
  color: var(--c); background: color-mix(in srgb, var(--c) 12%, #fff);
  border: 1px solid color-mix(in srgb, var(--c) 35%, #fff);
}

/* Tabla de resultados: una fila por propuesta */
#tablaResultados td { white-space: normal; vertical-align: top; }
#tablaResultados td small { display: block; color: var(--ink-3); font-size: 11.5px; margin-top: 2px; line-height: 1.3; }
#tablaResultados .col-prop { min-width: 240px; }
#tablaResultados .col-prop b { font-weight: 600; }
.caret {
  font: inherit; font-size: 12px; width: 20px; height: 20px; padding: 0; margin-right: 2px;
  border: 1px solid var(--line); border-radius: 4px; background: #fff; color: var(--ink-2); cursor: pointer;
}
.nombre-serv { display: block; margin-top: 3px; }
.km { font-size: 15px; white-space: nowrap; }
.km-mini { font-weight: 700; white-space: nowrap; }
td.muted, .muted { color: var(--ink-3); font-size: 12px; }
.fila-brecha { background: #fffaf0; }
.brecha-txt { color: var(--warn); font-weight: 600; }
tr.fila-detalle > td { background: #f8fafc; padding: 6px 10px 10px 34px; }
tr.fila-detalle:hover { background: transparent; }
.tabla-opciones { background: #fff; border: 1px solid var(--line); border-radius: 6px; font-size: 12px; }
.tabla-opciones thead th { position: static; background: #f1f5f9; }
.tabla-opciones tr.es-prioritario td { background: #ecfdf3; }
.tabla-opciones tr.es-prioritario td:first-child { box-shadow: inset 3px 0 0 var(--ok); }

.table-wrap { padding: 0 10px 10px; }
table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
thead th {
  position: sticky; top: 0; z-index: 1;
  background: #f8fafc; color: var(--ink-2);
  text-align: left; font-weight: 600; font-size: 11.5px;
  padding: 7px 8px; border-bottom: 1px solid var(--line); white-space: nowrap;
}
tbody td { padding: 6px 8px; border-bottom: 1px solid #f0f2f5; white-space: nowrap; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--blue-50); }
tbody tr.group-start td { border-top: 2px solid var(--line); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.badge {
  display: inline-block; padding: 1px 8px; border-radius: 999px;
  font-size: 11.5px; font-weight: 600; color: #fff;
}
.loc-in { color: var(--ok); font-weight: 600; }
.loc-out { color: var(--ink-3); }
a.gmaps {
  display: inline-flex; align-items: center; gap: 4px;
  color: var(--blue); text-decoration: none; font-weight: 600;
}
a.gmaps:hover { text-decoration: underline; }
.empty { padding: 18px; color: var(--ink-3); text-align: center; }

/* ---------- Leaflet tweaks ---------- */
.leaflet-popup-content-wrapper { border-radius: 10px; }
.leaflet-popup-content { margin: 12px 14px; font-family: inherit; font-size: 13px; min-width: 220px; }
.popup h3 { margin: 0 0 6px; font-size: 14px; display: flex; align-items: center; gap: 6px; }
.popup table { font-size: 12px; margin-bottom: 8px; }
.popup td { padding: 2px 6px 2px 0; border: 0; white-space: normal; }
.popup td:first-child { color: var(--ink-3); white-space: nowrap; }
.popup .links { display: flex; flex-wrap: wrap; gap: 6px; }
.popup .links a {
  font-size: 12px; font-weight: 600; text-decoration: none;
  background: var(--blue); color: #fff; padding: 4px 9px; border-radius: 6px;
}
.popup .links a.alt { background: var(--bg); color: var(--blue); border: 1px solid var(--line); }
.tramo-label {
  background: rgba(27, 36, 48, .85); color: #fff; border: 0; border-radius: 6px;
  font-weight: 600; font-size: 11.5px; box-shadow: none; padding: 2px 7px;
}
.tramo-label::before { display: none; }
.leaflet-control-layers { border-radius: 8px !important; font-family: inherit; }
.legend {
  background: rgba(255,255,255,.95); padding: 8px 10px; border-radius: 8px;
  box-shadow: var(--shadow); font-size: 12px; line-height: 1.7;
}
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; vertical-align: -1px; }
.legend i.line { width: 16px; height: 4px; border-radius: 2px; vertical-align: 2px; }

/* ---------- Responsive ---------- */
@media (max-width: 860px) {
  :root { --sidebar-w: 300px; }
  .sidebar { position: absolute; top: var(--topbar-h); bottom: 0; left: 0; z-index: 1002; box-shadow: var(--shadow); max-width: 100%; transition: none; }
  /* Sin animación en pantallas angostas: evita que el panel quede a medio ocultar */
  .sidebar.hidden { display: none; }
  .brand-text p { display: none; }
  .status { max-width: 220px; white-space: normal; line-height: 1.25; border-radius: 10px; }
  .results-actions input[type="search"] { width: 100px; }
}
@media (max-width: 520px) {
  .brand-text h1 { font-size: 14px; }
  .brand-logo { height: 38px; }
  .results { left: 6px; right: 6px; bottom: 6px; }
}
