:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #eef2f6;
  --text: #0f1b2a;
  --muted: #536274;
  --line: #d9e0e7;
  --brand: #0b2e4f;
  --accent: #0369a1;
  --alto: #b42318;
  --alto-bg: #fde7e4;
  --medio: #b45309;
  --medio-bg: #fdf0dc;
  --bajo: #2f6f4f;
  --bajo-bg: #e3f2ea;
  --neutro-bg: #eceff3;
  --shadow: 0 8px 28px rgba(15, 27, 42, .16), 0 1px 3px rgba(15, 27, 42, .12);
  --r: 14px;
  color-scheme: light;
}
:root[data-theme="dark"] {
    --bg: #08131f;
    --surface: #0f1d2c;
    --surface-2: #162839;
    --text: #e7eef5;
    --muted: #93a4b6;
    --line: #22384d;
    --brand: #7cc4f5;
    --accent: #5bb8f0;
    --alto: #ff8a7a;
    --alto-bg: #3a1714;
    --medio: #fbbf62;
    --medio-bg: #36260e;
    --bajo: #7dd3a4;
    --bajo-bg: #12301f;
    --neutro-bg: #1b2b3b;
    --shadow: 0 10px 30px rgba(0, 0, 0, .5);
    color-scheme: dark;}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #08131f;
    --surface: #0f1d2c;
    --surface-2: #162839;
    --text: #e7eef5;
    --muted: #93a4b6;
    --line: #22384d;
    --brand: #7cc4f5;
    --accent: #5bb8f0;
    --alto: #ff8a7a;
    --alto-bg: #3a1714;
    --medio: #fbbf62;
    --medio-bg: #36260e;
    --bajo: #7dd3a4;
    --bajo-bg: #12301f;
    --neutro-bg: #1b2b3b;
    --shadow: 0 10px 30px rgba(0, 0, 0, .5);
    color-scheme: dark;}
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
button { font: inherit; color: inherit; cursor: pointer; }
h1 { font-size: 1.32rem; line-height: 1.2; margin: .2rem 0 .4rem; letter-spacing: -.01em; text-wrap: balance; }
h2 { font-size: 1.15rem; margin: 0 0 .5rem; }
h3 { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 1.1rem 0 .4rem; }
.small, .hint { font-size: .82rem; color: var(--muted); }
.lead { color: var(--muted); margin: 0 0 1rem; }

#map { position: fixed; inset: 0; background: var(--bg); }

/* Barra superior */
.topbar {
  position: fixed; top: max(10px, env(safe-area-inset-top)); left: 12px; right: 12px;
  display: flex; gap: 8px; align-items: center; pointer-events: none; z-index: 5;
}
.topbar > * { pointer-events: auto; }
.brand {
  display: inline-flex; align-items: center; gap: 8px; text-decoration: none; color: var(--text);
  background: var(--surface); padding: 6px 12px 6px 7px; border-radius: 999px; box-shadow: var(--shadow); font-size: .95rem; white-space: nowrap;
}
.brand b { color: var(--brand); }
.tema {
  flex: none; width: 38px; height: 38px; border: 0; border-radius: 50%; background: var(--surface); color: var(--text);
  box-shadow: var(--shadow); display: grid; place-items: center;
}
.estado {
  display: inline-flex; align-items: center; gap: 8px; border: 0; border-radius: 999px; padding: 8px 12px;
  background: var(--alto-bg); color: var(--alto); font-weight: 600; font-size: .82rem; box-shadow: var(--shadow);
  min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.estado span:last-child { overflow: hidden; text-overflow: ellipsis; }
.pulse { width: 8px; height: 8px; border-radius: 50%; background: currentColor; flex: none; animation: pulse 1.8s infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 currentColor; } 70% { box-shadow: 0 0 0 7px transparent; } 100% { box-shadow: 0 0 0 0 transparent; } }

/* Powered by FraGoTe (esquina superior derecha) */
.powered {
  position: fixed; z-index: 7; top: max(10px, env(safe-area-inset-top)); right: 12px;
  display: inline-flex; align-items: center; gap: 8px; text-decoration: none;
  background: #fff; color: #0b2e4f; border-radius: 999px; padding: 6px 12px 6px 9px; box-shadow: var(--shadow);
}
.powered img { display: block; height: 19px; width: auto; }
.powered > span { display: flex; flex-direction: column; line-height: 1.05; }
.powered small { font-size: .62rem; color: #536274; letter-spacing: .02em; }
.powered b { font-size: .82rem; letter-spacing: .04em; }
.fg-a { color: #003b7a; }
.fg-b { color: #00b8c4; }
.powered:hover { filter: brightness(.97); }
/* el resto de la barra superior deja sitio a la insignia */
.topbar { right: 150px; }
@media (max-width: 899px) {
  .topbar { flex-wrap: wrap; right: 140px; }
  .estado { order: 3; }
  nav.capas { top: calc(max(10px, env(safe-area-inset-top)) + 94px); }
}
@media (min-width: 900px) {
  .maplibregl-ctrl-top-right { top: 62px !important; }
}

/* Capas */
.capas {
  position: fixed; z-index: 4; top: calc(max(10px, env(safe-area-inset-top)) + 46px); left: 12px; right: 12px;
  display: flex; flex-wrap: wrap; gap: 6px; align-items: flex-start; pointer-events: none;
}
.capas > * { pointer-events: auto; }
.chips { display: flex; gap: 4px; background: var(--surface); border-radius: 999px; padding: 4px; box-shadow: var(--shadow); overflow-x: auto; scrollbar-width: none; max-width: 100%; }
.chips::-webkit-scrollbar { display: none; }
.chips button {
  border: 0; background: transparent; padding: 6px 11px; border-radius: 999px; font-size: .82rem; font-weight: 600; color: var(--muted); white-space: nowrap;
}
.chips button[aria-checked="true"], .chips button[aria-pressed="true"] { background: var(--brand); color: var(--surface); }
.leyenda {
  flex-basis: 100%; max-width: 330px; background: var(--surface); border-radius: 12px; box-shadow: var(--shadow); padding: 8px 10px; font-size: .76rem;
  max-height: calc(100dvh - 170px); overflow-y: auto;
}
.leyenda:empty { display: none; }
.leyenda .tit { font-weight: 700; margin-bottom: 4px; display: flex; justify-content: space-between; gap: 8px; }
.leyenda .tit a { font-weight: 500; }
.leyenda .fila { display: flex; align-items: flex-start; gap: 8px; margin: 4px 0; line-height: 1.25; }
.leyenda .fila small { display: block; color: var(--muted); font-size: .68rem; }
.leyenda .simb { flex: none; width: 30px; min-height: 14px; display: flex; align-items: center; justify-content: center; gap: 2px; }
.leyenda .sws { display: flex; gap: 2px; }
.leyenda .sws .sw { width: 9px; }
.leyenda .sw { width: 16px; height: 11px; border-radius: 3px; flex: none; border: 1px solid rgba(0,0,0,.12); }
.leyenda .dot { width: 10px; height: 10px; border-radius: 50%; flex: none; box-sizing: border-box; }
.leyenda .raya { width: 22px; height: 0; flex: none; }
.leyenda .llama { flex: none; display: block; }
.leyenda .explica { margin: 0 0 4px; color: var(--muted); line-height: 1.3; }
.leyenda .acercate { margin: 6px 0 0; font-style: italic; }
.leyenda .sec { margin: 8px 0 2px; padding-top: 6px; border-top: 1px solid var(--line); font-weight: 700; font-size: .7rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.leyenda > .sec:first-child { margin-top: 0; padding-top: 0; border-top: 0; }

@media (max-width: 899px) {
  .leyenda:not(.ver) { display: none; }
  .maplibregl-ctrl-top-right .maplibregl-ctrl-group:not(.ctrl-ubicacion) { display: none; }
  /* en móvil solo queda el botón de ubicación: debajo de las dos filas de botones */
  .maplibregl-ctrl-top-right { top: calc(max(10px, env(safe-area-inset-top)) + 180px) !important; }
}
@media (min-width: 900px) { .solo-movil { display: none; } }

/* Panel: hoja inferior en móvil, tarjeta lateral en escritorio */
.panel {
  position: fixed; z-index: 6; left: 0; right: 0; bottom: 0; height: 88dvh;
  background: var(--surface); border-radius: 20px 20px 0 0; box-shadow: var(--shadow);
  transform: translateY(calc(88dvh - var(--peek, 42dvh))); transition: transform .28s cubic-bezier(.2,.8,.2,1);
  display: flex; flex-direction: column; padding-bottom: env(safe-area-inset-bottom);
}
.panel.abierto { transform: translateY(0); }
.panel.min { --peek: 132px; }
.handle { border: 0; background: transparent; padding: 10px 0 4px; display: flex; justify-content: center; flex: none; }
.handle span { width: 42px; height: 5px; border-radius: 3px; background: var(--line); }
.panel-body { overflow-y: auto; padding: 4px 18px 24px; overscroll-behavior: contain; flex: 1; }

@media (min-width: 900px) {
  .capas { left: 432px; right: 172px; top: max(10px, env(safe-area-inset-top)); }
  .topbar { right: auto; width: 404px; }
  .panel {
    top: calc(max(10px, env(safe-area-inset-top)) + 50px); left: 12px; bottom: 12px; right: auto; width: 404px; height: auto;
    border-radius: var(--r); transform: none !important;
  }
  .handle { display: none; }
  .panel-body { padding-top: 16px; }
}

.cta {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; width: 100%;
  border: 0; border-radius: 12px; padding: 13px 16px; background: var(--brand); color: var(--surface); font-weight: 700; font-size: 1rem;
}
.cta:hover { filter: brightness(1.08); }
.cta:disabled { opacity: .6; cursor: progress; }
.cta.secundario { background: var(--surface-2); color: var(--text); }
.cta.chico { width: auto; padding: 9px 13px; font-size: .88rem; }
.fila-botones { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.volver { border: 0; background: none; color: var(--accent); padding: 0; font-weight: 600; font-size: .88rem; margin-bottom: 6px; }

/* Resultado */
.lugar { color: var(--muted); font-size: .85rem; margin: 0; }
.nivel { display: flex; gap: 12px; align-items: center; border-radius: 12px; padding: 12px 14px; margin: 10px 0 4px; }
.nivel .n { font-size: 1.25rem; font-weight: 800; letter-spacing: -.01em; }
.nivel p { margin: 2px 0 0; font-size: .85rem; }
.nivel.alto { background: var(--alto-bg); color: var(--alto); }
.nivel.medio { background: var(--medio-bg); color: var(--medio); }
.nivel.bajo { background: var(--bajo-bg); color: var(--bajo); }
.nivel .ic { width: 38px; height: 38px; border-radius: 50%; background: currentColor; flex: none; display: grid; place-items: center; }
.nivel .ic svg { color: var(--surface); }

.senales { list-style: none; padding: 0; margin: 8px 0 0; }
.senal { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; padding: 10px 0; border-top: 1px solid var(--line); }
.senal .tag { grid-row: span 2; align-self: start; font-size: .7rem; font-weight: 800; text-transform: uppercase; letter-spacing: .04em; padding: 3px 7px; border-radius: 6px; min-width: 64px; text-align: center; }
.senal .tag.alto { background: var(--alto-bg); color: var(--alto); }
.senal .tag.medio { background: var(--medio-bg); color: var(--medio); }
.senal .tag.bajo { background: var(--bajo-bg); color: var(--bajo); }
.senal .tag.neutro { background: var(--neutro-bg); color: var(--muted); }
.senal b { font-size: .92rem; }
.senal p { margin: 0; font-size: .85rem; color: var(--muted); }
.senal p a { white-space: nowrap; }

.consejos { background: var(--surface-2); border-radius: 12px; padding: 4px 14px; margin-top: 12px; }
.consejos ul { margin: 0 0 10px; padding-left: 18px; font-size: .88rem; }
.consejos li { margin: 4px 0; }

.resumen { margin-top: 18px; }
.estado-card { background: var(--surface-2); border-radius: 12px; padding: 12px 14px; font-size: .86rem; }
.estado-card .vencido { color: var(--medio); font-weight: 600; }
.quebradas-lista { list-style: none; padding: 0; margin: 6px 0 0; }
.quebradas-lista button { display: flex; width: 100%; justify-content: space-between; gap: 8px; text-align: left; border: 0; border-top: 1px solid var(--line); background: none; padding: 9px 0; font-size: .88rem; }
.quebradas-lista button span:last-child { color: var(--muted); white-space: nowrap; }

/* Modelo */
.modelo-stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin: 10px 0; }
.stat { background: var(--surface-2); border-radius: 10px; padding: 8px 10px; }
.stat .v { font-size: 1.1rem; font-weight: 800; }
.stat .k { font-size: .74rem; color: var(--muted); }
.perfil { width: 100%; height: 90px; display: block; margin: 6px 0; }
.toggles { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0; }
.toggles label { display: inline-flex; align-items: center; gap: 6px; background: var(--surface-2); border-radius: 999px; padding: 5px 10px; font-size: .8rem; cursor: pointer; }
.aviso { border-left: 3px solid var(--medio); background: var(--medio-bg); padding: 8px 10px; border-radius: 0 8px 8px 0; font-size: .8rem; margin: 10px 0; color: var(--text); }
.cargando { display: flex; align-items: center; gap: 10px; color: var(--muted); padding: 14px 0; }
.spin { width: 18px; height: 18px; border-radius: 50%; border: 2.5px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed; z-index: 10; left: 50%; transform: translateX(-50%); bottom: calc(44dvh + 10px);
  background: var(--text); color: var(--surface); padding: 9px 14px; border-radius: 10px; font-size: .86rem; box-shadow: var(--shadow); max-width: calc(100% - 32px);
}
@media (min-width: 900px) { .toast { bottom: 24px; left: calc(50% + 210px); } }

.hist-flot {
  position: fixed; z-index: 8; left: 50%; transform: translateX(-50%); bottom: calc(44dvh + 10px); width: min(420px, calc(100% - 24px));
  background: var(--surface); color: var(--text); border-radius: 14px; box-shadow: var(--shadow); padding: 10px 12px; font-size: .84rem;
}
.hist-flot .fila-h { display: flex; align-items: center; gap: 8px; }
.hist-flot .fila-h b { flex: 1; }
.hist-flot .cerrar { border: 0; background: var(--surface-2); border-radius: 50%; width: 28px; height: 28px; font-size: 16px; line-height: 1; }
.hist-flot .deslizador { margin: 6px 0 2px; }
.hist-flot .opciones { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
@media (min-width: 900px) { .hist-flot { bottom: 24px; left: calc(50% + 210px); } }

dialog { border: 0; border-radius: 16px; padding: 0; max-width: min(560px, calc(100% - 24px)); max-height: 86dvh; background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(8, 19, 31, .5); }
.dlg { padding: 20px 22px; }
.dlg ul { padding-left: 18px; font-size: .9rem; }
.dlg li { margin: 5px 0; }
.dlg p { font-size: .9rem; }
.fuentes { font-size: .84rem !important; }
.tel { display: flex; gap: 8px; flex-wrap: wrap; }
.tel a { background: var(--alto-bg); color: var(--alto); text-decoration: none; padding: 6px 10px; border-radius: 8px; font-weight: 700; }

.visitas { display: inline-flex; align-items: center; gap: 6px; margin: 2px 0 0; font-size: .82rem; color: var(--muted); }
.visitas b { color: var(--text); font-variant-numeric: tabular-nums; }
.punto-vivo { width: 8px; height: 8px; margin: 0 3px; border-radius: 50%; background: #16a34a; box-shadow: 0 0 0 0 rgba(22,163,74,.5); animation: vivo 2s ease-out infinite; }
@keyframes vivo { 70% { box-shadow: 0 0 0 6px rgba(22,163,74,0); } 100% { box-shadow: 0 0 0 0 rgba(22,163,74,0); } }
@media (prefers-reduced-motion: reduce) { .punto-vivo { animation: none; } }

/* Historia */
.eventos { list-style: none; padding: 0; margin: 0; }
.evento { border-top: 1px solid var(--line); padding: 10px 0; }
.evento.activo .ev-cab { background: var(--surface-2); }
.ev-cab { display: flex; gap: 12px; align-items: center; width: 100%; text-align: left; border: 0; background: none; padding: 6px; border-radius: 10px; }
.ev-cab > span:last-child { display: flex; flex-direction: column; }
.ev-anio { font-weight: 800; font-size: 1.05rem; color: var(--brand); min-width: 64px; }
.evento details { font-size: .86rem; }
.evento summary, .consejos summary { cursor: pointer; color: var(--accent); font-weight: 600; padding: 4px 0; }
.evento p { margin: 6px 0; }
.deslizador { display: flex; align-items: center; gap: 8px; margin: 10px 0 4px; font-size: .84rem; }
.deslizador input { flex: 1; }

/* Ventanas emergentes del mapa */
.maplibregl-popup-content { background: var(--surface) !important; color: var(--text); border-radius: 12px !important; box-shadow: var(--shadow) !important; padding: 12px 14px !important; font-size: .84rem; }
.maplibregl-popup-tip { border-top-color: var(--surface) !important; border-bottom-color: var(--surface) !important; }
.maplibregl-popup-close-button { color: var(--muted); font-size: 18px; }
.pop p { margin: 6px 0; }
.pop b { font-size: .9rem; }

/* MapLibre */
.maplibregl-ctrl-group { background: var(--surface) !important; box-shadow: var(--shadow) !important; border-radius: 10px !important; }
.maplibregl-ctrl-group button + button { border-top-color: var(--line) !important; }
[data-theme="dark"] .maplibregl-ctrl-icon { filter: invert(1); }
.maplibregl-ctrl-attrib { font-size: 10px; }
.ctrl-ubicacion button { display: grid; place-items: center; color: var(--text); }
.ctrl-ubicacion button.buscando svg { animation: pulso-ubic 1s ease-in-out infinite; color: #2563eb; }
@keyframes pulso-ubic { 50% { opacity: .35; } }
.maplibregl-ctrl-bottom-right { bottom: 132px; }
@media (min-width: 900px) { .maplibregl-ctrl-bottom-right { bottom: 0; } }
.maplibregl-ctrl-top-right { top: 120px; }
@media (min-width: 900px) { .maplibregl-ctrl-top-right { top: 60px; } }

@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
.nivel-rio b { min-width: 3.4em; text-align: right; font-size: 1rem; }
.nivel-rio input { accent-color: #2171b5; }

/* aportes ciudadanos */
.titulo-vista { font-size: 1.15rem; margin: 4px 0 6px; }
.aporte-lugar { background: var(--surface-2); border-radius: 12px; padding: 10px 12px; font-size: .88rem; margin: 10px 0; border-left: 4px solid #0d9488; }
.aporte-lugar .cta { margin-top: 8px; }
.form-aporte { display: grid; gap: 12px; }
.form-aporte fieldset { border: 0; padding: 0; margin: 0; }
.form-aporte legend, .form-aporte > label { font-weight: 600; font-size: .9rem; }
.form-aporte > label { display: grid; gap: 4px; }
.form-aporte [hidden] { display: none; }
.form-aporte .small { font-weight: 400; }
.form-aporte select, .form-aporte textarea, .form-aporte input[type="number"] { font: inherit; font-weight: 400; color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 9px 10px; width: 100%; }
.form-aporte input[type="file"] { font: inherit; font-size: .85rem; font-weight: 400; max-width: 100%; }
.opciones { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.opciones label { display: inline-flex; align-items: center; gap: 6px; background: var(--surface-2); border-radius: 999px; padding: 7px 12px; font-size: .88rem; font-weight: 400; cursor: pointer; }
.opciones label:has(input:checked) { background: #0d9488; color: #fff; }
.ap-prev { max-width: 100%; max-height: 220px; border-radius: 10px; object-fit: cover; }
.ap-error { color: var(--alto); background: var(--alto-bg); border-radius: 10px; padding: 8px 12px; margin: 0; font-size: .88rem; }
.trampa { position: absolute; left: -9999px; width: 1px; height: 1px; opacity: 0; }
.pop-foto { display: block; width: 100%; max-height: 180px; object-fit: cover; border-radius: 8px; margin: 6px 0; }
.barra-volver { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 6px; }
.barra-volver .volver { margin-bottom: 0; }
.btn-aportar { display: inline-flex; align-items: center; gap: 6px; border: 0; border-radius: 999px; padding: 7px 12px; background: #0d9488; color: #fff; font: inherit; font-size: .84rem; font-weight: 600; cursor: pointer; white-space: nowrap; }
.btn-aportar:hover { filter: brightness(1.08); }
