:root {
  --fondo: #f4f6f3;
  --superficie: #ffffff;
  --borde: #dfe4dc;
  --texto: #1d2a22;
  --suave: #5f6f64;
  --verde: #1f8a4c;
  --verde-oscuro: #166b3a;
  --verde-claro: #e5f4ea;
  --oro: #f2b705;
  --gris: #9aa59d;
  --azul: #2563eb;
  --rojo: #c0392b;
  --radio: 10px;
  --sombra: 0 2px 8px rgba(20, 40, 25, .08);
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
@supports (height: 100dvh) {
  html, body { height: 100dvh; }
}
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--fondo);
  color: var(--texto);
  display: flex;
  flex-direction: column;
  font-size: 15px;
  overflow: hidden;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}
h1 { font-size: 1.5rem; margin: 0 0 .25rem; }
h2 { font-size: 1rem; margin: 0 0 .5rem; display: flex; align-items: center; gap: .5rem; }
h3 { font-size: 1rem; margin: 0; }
button, input, select { font: inherit; }

button {
  border: 1px solid var(--verde);
  background: var(--verde);
  color: #fff;
  border-radius: 8px;
  padding: .5rem .9rem;
  cursor: pointer;
}
button:hover { background: var(--verde-oscuro); }
button:disabled { opacity: .6; cursor: progress; }
button.secundario { background: var(--superficie); color: var(--verde-oscuro); }
button.secundario:hover { background: var(--verde-claro); }
button.principal { font-weight: 600; }
button.enlace { background: none; border: none; color: var(--verde-oscuro); padding: 0; text-decoration: underline; }

input, select {
  border: 1px solid var(--borde);
  border-radius: 8px;
  padding: .45rem .6rem;
  background: var(--superficie);
  color: var(--texto);
  min-width: 0;
}
input:focus, select:focus, button:focus-visible { outline: 2px solid var(--verde); outline-offset: 1px; }

.ayuda { color: var(--suave); font-size: .85rem; margin: .25rem 0; }
.error { color: var(--rojo); font-size: .9rem; min-height: 1.2em; margin: .25rem 0; }
.fila { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.acciones { justify-content: flex-end; margin-top: .75rem; }
.opcion-camino {
  display: flex; align-items: flex-start; gap: .55rem;
  margin: .85rem 0 .2rem; cursor: pointer; line-height: 1.35;
}
.opcion-camino input { width: 1.15rem; height: 1.15rem; margin-top: .15rem; accent-color: var(--verde); flex: none; }
.interruptor {
  display: flex; align-items: center; gap: .65rem;
  min-height: 44px; margin: .15rem 0 .35rem; cursor: pointer;
}
.interruptor input {
  position: absolute; opacity: 0; width: 1px; height: 1px;
}
.pista {
  width: 2.75rem; height: 1.55rem; border-radius: 999px; flex: none;
  background: #c5cdc7; position: relative;
}
.pista::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 1.25rem; height: 1.25rem; border-radius: 50%; background: #fff;
  transition: transform .15s ease;
}
.interruptor input:checked + .pista { background: var(--verde); }
.interruptor input:checked + .pista::after { transform: translateX(1.2rem); }
.interruptor input:focus-visible + .pista { outline: 2px solid var(--azul); outline-offset: 2px; }
.peso { display: flex; flex-wrap: wrap; align-items: center; gap: .45rem .6rem; margin: .35rem 0 .15rem; }
.peso label { font-weight: 600; }
.peso input { width: 5.5rem; }
.peso .ayuda { flex-basis: 100%; margin: 0; }
.peso[hidden] { display: none !important; }

/* ---------- Barra superior ---------- */
.barra {
  display: flex;
  align-items: center;
  gap: .75rem 1rem;
  padding: max(.6rem, env(safe-area-inset-top)) 1rem .6rem;
  background: var(--superficie);
  border-bottom: 1px solid var(--borde);
  flex-wrap: wrap;
}
.marca { font-weight: 800; font-size: 1.3rem; color: var(--verde-oscuro); flex: none; }
.marca span { color: var(--oro); }
.pestanas {
  display: flex;
  gap: .25rem;
  flex: 1;
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: none;
}
.pestanas::-webkit-scrollbar { display: none; }
.pestanas button, .pestanas-dialogo button {
  background: none;
  border: none;
  color: var(--suave);
  border-radius: 8px;
  white-space: nowrap;
  flex: none;
}
.pestanas button:hover, .pestanas-dialogo button:hover { background: var(--verde-claro); }
.pestanas button.activa, .pestanas-dialogo button.activa { background: var(--verde-claro); color: var(--verde-oscuro); font-weight: 600; }
.cuenta { display: flex; gap: .5rem; align-items: center; margin-left: auto; min-width: 0; }
.cuenta span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 34vw; }

/* ---------- Vistas ---------- */
main { flex: 1; min-height: 0; min-width: 0; display: flex; overflow: hidden; }
.vista { display: none; flex: 1; min-height: 0; min-width: 0; }
.vista.activa { display: flex; }
.contenido {
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  padding: 1.5rem 1rem;
  overflow: auto;
  flex: 1;
  min-height: 0;
  min-width: 0;
}
.encabezado { display: flex; justify-content: space-between; align-items: flex-end; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }

/* ---------- Panel de búsqueda ---------- */
.panel {
  width: clamp(300px, 34vw, 420px);
  flex-shrink: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 1rem;
  background: var(--superficie);
  border-right: 1px solid var(--borde);
  min-width: 0;
  min-height: 0;
}
.paso { padding-bottom: 1rem; margin-bottom: 1rem; border-bottom: 1px solid var(--borde); }
.paso .num {
  display: inline-grid; place-items: center; flex: none;
  width: 1.5rem; height: 1.5rem; border-radius: 50%;
  background: var(--verde); color: #fff; font-size: .8rem;
}
#radio { flex: 1; accent-color: var(--verde); }
#radio-num { width: 4.5rem; }
#item-texto { flex: 1; }
#item-cantidad { width: 4.2rem; }

.items { list-style: none; padding: 0; margin: .75rem 0 0; }
.items li {
  display: flex; align-items: center; gap: .5rem;
  padding: .4rem .5rem; border-radius: 8px;
}
.items li:nth-child(odd) { background: var(--fondo); }
.items li span { flex: 1; overflow-wrap: anywhere; }
.items li input { width: 3.8rem; padding: .2rem .4rem; }
.items li button { background: none; border: none; color: var(--suave); padding: .2rem .4rem; }
.items li button:hover { color: var(--rojo); background: none; }
.items .vacia { color: var(--suave); font-style: italic; background: none !important; }

/* ---------- Resultados ---------- */
.resumen-resultados { margin: 0 0 .75rem; }
.tarjeta {
  border: 1px solid var(--borde);
  border-radius: var(--radio);
  padding: .75rem;
  margin-bottom: .6rem;
  cursor: pointer;
  background: var(--superficie);
}
.tarjeta:hover { box-shadow: var(--sombra); }
.tarjeta.mejor { border: 2px solid var(--oro); background: #fffbea; }
.tarjeta.parcial { opacity: .9; }
.tarjeta header { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.tarjeta header h3 { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.tarjeta .total { font-weight: 700; font-size: 1.1rem; margin-left: auto; white-space: nowrap; }
.tarjeta .meta { color: var(--suave); font-size: .85rem; margin: .2rem 0; }
.tarjeta .ahorro { color: var(--verde-oscuro); font-weight: 600; font-size: .9rem; margin: .3rem 0 0; }
.tarjeta .faltan { color: var(--rojo); font-size: .85rem; margin: .3rem 0 0; }
.tarjeta details { margin-top: .4rem; font-size: .88rem; }
.tarjeta summary { color: var(--verde-oscuro); cursor: pointer; }
.estrella { color: var(--oro); font-size: 1.3rem; line-height: 1; flex: none; }
.tarjeta.activa { outline: 2px solid var(--azul); }
.tabla-scroll { max-height: 320px; overflow: auto; -webkit-overflow-scrolling: touch; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .35rem .4rem; border-bottom: 1px solid var(--borde); vertical-align: top; overflow-wrap: break-word; }
th { color: var(--suave); font-weight: 600; font-size: .85rem; }
td:first-child { overflow-wrap: anywhere; }
td.num, th.num { text-align: right; }
th.num { max-width: 8rem; }
td.num { white-space: nowrap; }

.actualizaciones { font-size: .88rem; margin-top: 1rem; }
.actualizaciones summary { cursor: pointer; color: var(--suave); }
.actualizaciones ul { padding-left: 1.1rem; }
.actualizaciones li { margin-bottom: .4rem; }

/* ---------- Mapa ---------- */
.mapa-contenedor { flex: 1; position: relative; min-height: 300px; }
#mapa { position: absolute; inset: 0; }
.leyenda {
  position: absolute; left: .75rem; bottom: 1.5rem; z-index: 500;
  background: var(--superficie); border-radius: 8px; box-shadow: var(--sombra);
  padding: .4rem .6rem; display: flex; gap: .75rem; flex-wrap: wrap; font-size: .8rem;
}
.leyenda span { display: inline-flex; align-items: center; gap: .3rem; }
.punto { display: inline-grid; place-items: center; width: 14px; height: 14px; border-radius: 50%; font-style: normal; font-size: 10px; }
.punto.origen { background: var(--azul); border: 2px solid #fff; box-shadow: 0 0 0 1px var(--azul); }
.punto.mejor { background: var(--oro); color: #fff; }
.punto.completo { background: var(--verde); }
.punto.parcial { background: var(--gris); }
.punto.parada { background: var(--azul); }
.punto.ruta.auto { background: #1d4ed8; }
.punto.ruta.pie { background: #0f766e; }
.leyenda-ruta[hidden] { display: none !important; }
.leyenda-parada[hidden] { display: none !important; }

.pin-origen {
  width: 20px; height: 20px; border-radius: 50%;
  background: var(--azul); border: 3px solid #fff; box-shadow: 0 0 0 2px var(--azul), var(--sombra);
}
.pin {
  display: inline-block; transform: translate(-50%, -100%);
  background: var(--verde); color: #fff; font-weight: 700; font-size: 12px;
  padding: 3px 7px; border-radius: 12px; white-space: nowrap; box-shadow: var(--sombra);
  border: 2px solid #fff; position: relative;
}
.pin::after {
  content: ""; position: absolute; left: 50%; bottom: -7px; transform: translateX(-50%);
  border: 5px solid transparent; border-top-color: #fff;
}
.pin.parcial { background: var(--gris); }
.pin.parada { background: var(--azul); }
.pin.mejor { background: var(--oro); color: #3d2f00; font-size: 14px; padding: 4px 9px; z-index: 1000; }
.pin.mejor .estrella-pin { font-size: 17px; margin-right: 2px; }
.pin-gris { width: 10px; height: 10px; border-radius: 50%; background: var(--gris); border: 2px solid #fff; box-shadow: var(--sombra); }
.pin-gris.en-zona { background: var(--verde); }
.leaflet-div-icon { background: none; border: none; }
.popup { max-width: min(260px, calc(100vw - 4.5rem)); }
.popup h4 { margin: 0 0 .25rem; overflow-wrap: anywhere; }
.popup table { font-size: .82rem; }
.leaflet-popup-content { margin: 10px 12px; }
.leaflet-control-attribution { max-width: 55%; }

/* ---------- Recomendada y estadísticas ---------- */
.bloque {
  background: var(--superficie); border: 1px solid var(--borde); border-radius: var(--radio);
  padding: 1rem; margin-bottom: 1rem;
}
.bloque h2 { margin-bottom: .75rem; }
.seccion-lista { margin: 1.1rem 0 .15rem; font-size: .95rem; }
.bloque > .principal { margin-top: .9rem; }
.rejilla { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 1rem; }
.cifras { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: .75rem; margin-bottom: 1rem; }
.cifra { background: var(--superficie); border: 1px solid var(--borde); border-radius: var(--radio); padding: .75rem 1rem; }
.cifra .valor { font-size: 1.5rem; font-weight: 700; }
.cifra .rotulo { color: var(--suave); font-size: .85rem; }
.etiqueta { display: inline-block; background: var(--verde-claro); color: var(--verde-oscuro); border-radius: 999px; padding: .05rem .5rem; font-size: .75rem; font-weight: 600; margin-left: .3rem; }
.etiqueta.reponer { background: #fff1c2; color: #7a5b00; }
.vacio { text-align: center; color: var(--suave); padding: 2rem 1rem; }
.lista-anterior { border-bottom: 1px solid var(--borde); padding: .6rem 0; display: flex; gap: .75rem 1rem; align-items: center; flex-wrap: wrap; }
.lista-anterior:last-child { border-bottom: none; }
.lista-anterior div { flex: 1; min-width: min(100%, 12rem); }
.lista-anterior small { color: var(--suave); overflow-wrap: anywhere; }

/* ---------- Diálogo y avisos ---------- */
dialog {
  border: none; border-radius: 14px; padding: 1.25rem;
  width: min(400px, calc(100vw - 1.5rem - env(safe-area-inset-left) - env(safe-area-inset-right)));
  max-height: min(92dvh, 640px);
  overflow: auto;
  box-shadow: 0 10px 40px rgba(0, 0, 0, .25);
}
dialog::backdrop { background: rgba(20, 30, 25, .45); }
dialog label { display: flex; flex-direction: column; gap: .25rem; margin-top: .6rem; font-size: .9rem; }
.pestanas-dialogo { display: flex; gap: .25rem; margin-bottom: .5rem; }
#form-cuenta:not(.registro) .solo-registro { display: none; }

.avisos {
  position: fixed;
  right: max(1rem, env(safe-area-inset-right));
  left: max(1rem, env(safe-area-inset-left));
  bottom: max(1rem, env(safe-area-inset-bottom));
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: .5rem;
  width: min(360px, calc(100vw - 2rem));
  margin-left: auto;
}
.aviso {
  background: var(--texto); color: #fff; padding: .7rem .9rem; border-radius: 10px;
  box-shadow: var(--sombra); font-size: .9rem; animation: entrar .25s ease-out;
}
.aviso.error { background: var(--rojo); color: #fff; }
@keyframes entrar { from { transform: translateY(10px); opacity: 0; } }

/* ---------- Teléfono y tablet angosta ---------- */
@media (max-width: 899px) {
  .barra {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas: "marca cuenta" "pestanas pestanas";
    align-items: center;
    gap: .35rem .75rem;
    padding-left: max(.75rem, env(safe-area-inset-left));
    padding-right: max(.75rem, env(safe-area-inset-right));
  }
  .marca { grid-area: marca; }
  .cuenta { grid-area: cuenta; margin-left: 0; }
  .pestanas { grid-area: pestanas; width: 100%; padding-bottom: .15rem; }

  h1 { font-size: 1.3rem; }
  .contenido { padding: 1rem .75rem max(1rem, env(safe-area-inset-bottom)); }
  .rejilla { grid-template-columns: 1fr; }
  .cifras { grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); }

  input, select, textarea { font-size: 16px; }
  .pestanas button, .acciones button, #form-item button, #btn-ubicacion { min-height: 44px; }

  #vista-buscar.activa {
    flex-direction: column;
    overflow: hidden;
  }
  .mapa-contenedor {
    order: -1;
    flex: 0 0 auto;
    display: flex;
    flex-direction: column;
    width: 100%;
    height: auto;
    min-height: 0;
  }
  #mapa {
    position: relative;
    inset: auto;
    flex: 0 0 auto;
    height: clamp(190px, 36dvh, 420px);
    min-height: 190px;
  }
  .panel {
    width: 100%;
    flex: 1;
    border-right: none;
    border-top: 1px solid var(--borde);
    padding-bottom: max(1rem, env(safe-area-inset-bottom));
  }
  .leyenda {
    position: static;
    left: auto;
    right: auto;
    bottom: auto;
    max-width: none;
    border-radius: 0;
    box-shadow: none;
    border-top: 1px solid var(--borde);
    padding: .35rem .6rem;
  }
  #form-item { align-items: stretch; }
  #item-texto { flex: 1 1 12rem; }
  #form-item button { flex: 1 1 auto; }
  .acciones .principal { flex: 1 1 10rem; }

  .encabezado label { display: flex; flex-direction: column; gap: .25rem; }
  .encabezado select { width: 100%; }
}

/* Teléfono horizontal: el mapa al lado, no encima de una franja mínima. */
@media (max-width: 899px) and (max-height: 480px) {
  .barra {
    display: flex;
    flex-wrap: nowrap;
    gap: .35rem .6rem;
    padding-top: max(.35rem, env(safe-area-inset-top));
    padding-bottom: .35rem;
  }
  .pestanas { width: auto; flex: 1; }
  .cuenta { margin-left: auto; }
  #vista-buscar.activa { flex-direction: row; }
  .mapa-contenedor {
    order: 0;
    display: block;
    flex: 1 1 auto;
    width: auto;
    height: auto;
    min-height: 0;
  }
  #mapa { position: absolute; inset: 0; height: auto; min-height: 0; }
  .leyenda {
    position: absolute;
    left: .4rem;
    right: .4rem;
    bottom: .35rem;
    border-radius: 8px;
    box-shadow: var(--sombra);
    border-top: none;
  }
  .panel {
    width: min(300px, 48vw);
    flex: none;
    border-top: none;
    border-right: 1px solid var(--borde);
  }
}

#vista-buscar.activa.sin-mapa {
  display: block;
  overflow: auto;
}
#vista-buscar.activa.sin-mapa .mapa-contenedor { display: none; }
#vista-buscar.activa.sin-mapa .panel {
  width: min(720px, 100%);
  max-width: 720px;
  margin-inline: auto;
  border: none;
  overflow: visible;
  flex: none;
}

@media (max-width: 420px) {
  .marca { font-size: 1.15rem; }
  .pestanas button { padding: .4rem .45rem; font-size: .85rem; }
  .cuenta span { max-width: 26vw; }
  .paso h2 { align-items: flex-start; }
  #item-texto { flex-basis: 100%; }
  .tarjeta .total { font-size: 1rem; }
  .cifra .valor { font-size: 1.25rem; }
}
