* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  height: 100%;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: #1f2430;
}

#app {
  display: flex;
  height: 100vh;
}

.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(8, 10, 9, 0.75);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.1rem;
  z-index: 2000;
}

.loading-overlay[hidden] {
  display: none;
}

.loading-scene {
  position: relative;
  width: 220px;
  height: 170px;
  overflow: hidden;
}

.mountain-backdrop {
  position: absolute;
  left: 50%;
  bottom: -18px;
  transform: translateX(-50%);
  font-size: 9rem;
  line-height: 1;
  opacity: 0.4;
}

.hiker-track {
  position: absolute;
  inset: 0;
}

.hiker-runner {
  position: absolute;
  left: 50%;
  top: 66%;
  animation: climb-hiker 3s ease-in-out infinite;
}

.hiker-emoji {
  display: inline-block;
  font-size: 2rem;
  animation: runner-bounce 0.45s ease-in-out infinite;
}

/* El recorrido se mantiene cerca del centro (poco desplazamiento lateral) y
   sube en vertical, para no salirse de la silueta de la montaña del emoji. */
@keyframes climb-hiker {
  0% {
    transform: translate(-22px, 26px) rotate(-6deg);
    opacity: 0;
  }
  8% {
    opacity: 1;
  }
  50% {
    transform: translate(-4px, -14px) rotate(4deg);
  }
  92% {
    transform: translate(14px, -52px) rotate(-6deg);
    opacity: 1;
  }
  100% {
    transform: translate(14px, -52px) rotate(-6deg);
    opacity: 0;
  }
}

@keyframes runner-bounce {
  0%,
  100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-3px);
  }
}

.loading-text {
  color: #ffffff;
  font-size: 0.95rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  margin: 0;
  animation: text-pulse 1.6s ease-in-out infinite;
}

@keyframes text-pulse {
  0%,
  100% {
    opacity: 0.65;
  }
  50% {
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  .hiker-runner,
  .hiker-emoji,
  .loading-text {
    animation: none;
  }
}

#sidebar {
  width: 380px;
  min-width: 320px;
  background: #fafaf8;
  border-right: 1px solid #e2e2de;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sheet-handle {
  display: none;
}

.map-attribution {
  display: none;
}

.sidebar-content {
  flex: 1;
  min-height: 0;
  padding: 1.25rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

#map {
  flex: 1;
}

header h1 {
  margin: 0;
  font-size: 1.4rem;
}

.subtitle {
  margin: 0.2rem 0 0;
  font-size: 0.85rem;
  color: #666;
}

.header-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
}

.header-profile-btn[hidden] {
  display: none;
}

.header-profile-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  border: 1px solid #d8d8d2;
  background: white;
  border-radius: 999px;
  padding: 0.35rem 0.7rem;
  font-size: 0.78rem;
  font-weight: 600;
  color: #333;
  cursor: pointer;
  flex-shrink: 0;
}

.header-profile-btn:hover {
  border-color: #1f6f43;
}

#search-form,
#profile-defaults-form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.field label {
  font-size: 0.82rem;
  font-weight: 600;
  color: #333;
}

.field .opt {
  font-weight: 400;
  color: #888;
  font-size: 0.75rem;
}

input[type="number"],
input[type="text"],
input[type="email"],
input[type="password"],
select {
  padding: 0.45rem 0.55rem;
  border: 1px solid #ccc;
  border-radius: 6px;
  font-size: 0.9rem;
  background: white;
}

input[readonly] {
  background: #f0f0ec;
  color: #555;
}

.link-btn {
  background: none;
  border: none;
  padding: 0;
  color: #1f6f43;
  font-weight: 600;
  font-size: inherit;
  text-decoration: underline;
  cursor: pointer;
}

#login-form[hidden],
#register-form[hidden] {
  display: none;
}

#login-form,
#register-form,
#admin-invite-form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

#admin-invite-btn {
  align-self: flex-start;
  margin-bottom: 0.5rem;
}

#login-btn,
#register-btn {
  background: #1f6f43;
  color: white;
  padding: 0.65rem;
  font-size: 0.95rem;
  align-self: flex-start;
}

details.settings-details {
  border: 1px solid #e2e2de;
  border-radius: 8px;
  padding: 0.6rem 0.75rem;
}

details.settings-details summary {
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 700;
}

details.settings-details[open] summary {
  margin-bottom: 0.75rem;
}

.settings-details-body {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

details.advanced {
  border: 1px solid #e2e2de;
  border-radius: 6px;
  padding: 0.5rem 0.75rem;
}

details.advanced summary {
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 600;
}

details.advanced .field {
  margin-top: 0.6rem;
}

.location-field .coords {
  font-family: monospace;
  font-size: 0.85rem;
  background: #f0f0ec;
  padding: 0.3rem 0.5rem;
  border-radius: 4px;
}

.hint {
  font-size: 0.75rem;
  color: #777;
  margin: 0.2rem 0 0;
}

button {
  cursor: pointer;
  border: none;
  border-radius: 6px;
  padding: 0.5rem 0.75rem;
  font-size: 0.88rem;
  font-weight: 600;
}

#use-my-location {
  background: #eef2ff;
  color: #33459c;
  align-self: flex-start;
}

#search-btn {
  background: #1f6f43;
  color: white;
  padding: 0.65rem;
  font-size: 0.95rem;
}

#search-btn:disabled {
  background: #9bbfa9;
  cursor: wait;
}

.status {
  font-size: 0.85rem;
  color: #445;
}

.status.error {
  color: #a12424;
}

.results-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

/* El atributo "hidden" y ".results-list{display:flex}" tienen la misma
   especificidad CSS — sin esta regla, la clase gana y la lista no llega a
   ocultarse nunca al poner resultsEl.hidden = true (ver modo edición en
   app.js). Mismo motivo por el que .loading-overlay ya tiene su propia regla
   [hidden] más abajo. */
.results-list[hidden] {
  display: none;
}

.result-item {
  border: 1px solid #e2e2de;
  border-radius: 8px;
  padding: 0.6rem 0.75rem;
  cursor: pointer;
  background: white;
  transition: border-color 0.15s ease;
}

.result-item:hover {
  border-color: #1f6f43;
}

.result-item.out-of-range {
  opacity: 0.75;
  border-style: dashed;
}

.result-item--active {
  border-color: #1f6f43;
  border-width: 2px;
  box-shadow: 0 0 0 2px rgba(31, 111, 67, 0.15);
}

.range-divider {
  list-style: none;
  margin: 0.4rem 0 -0.1rem;
  padding: 0.3rem 0;
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: #896b1f;
  border-bottom: 1px dashed #d8c48a;
}

.result-header {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-weight: 600;
}

.result-index {
  background: #1f6f43;
  color: white;
  border-radius: 50%;
  width: 1.4rem;
  height: 1.4rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  flex-shrink: 0;
}

.result-name {
  flex: 1;
  font-size: 0.9rem;
}

.badge {
  background: #fff3cd;
  color: #7a5b00;
  font-size: 0.7rem;
  padding: 0.15rem 0.4rem;
  border-radius: 10px;
  font-weight: 600;
  white-space: nowrap;
}

.badge.difficulty-easy {
  background: #e3f5e8;
  color: #1f6f43;
}

.badge.difficulty-medium {
  background: #fff3cd;
  color: #7a5b00;
}

.badge.difficulty-hard {
  background: #fde2e1;
  color: #a3312c;
}

.fav-btn.is-fav {
  background: #fff3cd;
  color: #7a5b00;
}

.empty-hint {
  list-style: none;
  padding: 1rem 0.5rem;
  color: #777;
  font-size: 0.85rem;
  text-align: center;
}

.result-meta {
  display: flex;
  gap: 0.6rem;
  font-size: 0.8rem;
  color: #444;
  margin-top: 0.35rem;
  flex-wrap: wrap;
}

.elevation-profile {
  position: relative;
  margin-top: 0.45rem;
  cursor: crosshair;
}

.elevation-svg {
  width: 100%;
  height: 46px;
  display: block;
}

.elevation-crosshair {
  stroke: #898781;
  stroke-width: 1;
}

.elevation-tooltip {
  position: absolute;
  top: -2px;
  transform: translate(-50%, -100%);
  background: #1f2430;
  color: #ffffff;
  font-size: 0.68rem;
  font-weight: 600;
  padding: 0.15rem 0.45rem;
  border-radius: 4px;
  white-space: nowrap;
  pointer-events: none;
  z-index: 2;
}

.result-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 0.35rem;
  font-size: 0.75rem;
  color: #777;
  gap: 0.5rem;
}

.result-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.action-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.25rem 0.55rem;
  border: none;
  border-radius: 999px;
  background: #eef2ff;
  color: #33459c;
  text-decoration: none;
  font-family: inherit;
  font-size: 0.72rem;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  transition: background-color 0.15s ease;
}

.action-btn:hover {
  background: #dbe3ff;
}

.action-btn svg {
  flex-shrink: 0;
}

.trailapi-marker-icon {
  background: none;
  border: none;
}

.trailapi-marker {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 2px solid #ffffff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
  color: #ffffff;
  font-size: 0.65rem;
  font-weight: 700;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.45);
}

/* Manejador de arrastre en modo edición: más grande que los marcadores de
   inicio/fin normales, para que quede claro que aquí sí se puede arrastrar. */
.trailapi-edit-handle {
  display: block;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #ffffff;
  border: 4px solid #1f6f43;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.45);
  cursor: grab;
}

.edit-panel {
  border: 1px solid #e2e2de;
  border-radius: 8px;
  padding: 0.75rem;
  background: white;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

/* Misma razón que .results-list[hidden]: sin esto, la regla display:flex de
   arriba (misma especificidad que [hidden]) ganaría y el panel se vería
   incluso con el atributo hidden puesto. */
.edit-panel[hidden] {
  display: none;
}

.edit-panel h2 {
  margin: 0;
  font-size: 1rem;
}

.edit-panel-actions {
  display: flex;
  gap: 0.5rem;
}

.onboarding-actions {
  display: flex;
  justify-content: center;
  gap: 1rem;
  margin-top: 0.25rem;
}

.onboarding-actions button {
  padding: 0.65rem 1.25rem;
}

#edit-cancel-btn {
  background: #f0f0ec;
  color: #444;
}

/* Login/registro: modal centrado con el mapa visible (atenuado) detrás — a
   diferencia de .profile-screen (pantalla completa), aquí el contenido es
   compacto (2-3 campos) y no hace falta ocultar el resto de la app. */
.auth-backdrop {
  position: fixed;
  inset: 0;
  z-index: 1600;
  background: rgba(12, 18, 14, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.auth-backdrop[hidden] {
  display: none;
}

.auth-modal {
  background: #fff;
  border-radius: 12px;
  width: 100%;
  max-width: 400px;
  max-height: min(600px, 100%);
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}

.auth-modal-header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 1.1rem 1.25rem;
  border-bottom: 1px solid #e2e2de;
}

.auth-modal-header h2 {
  margin: 0;
  font-size: 1.1rem;
}

.auth-modal-close {
  background: none;
  border: none;
  padding: 0.25rem;
  font-size: 1.1rem;
  line-height: 1;
  color: #666;
  cursor: pointer;
  flex-shrink: 0;
}

.auth-modal-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 1.25rem;
  padding-bottom: max(1.25rem, env(safe-area-inset-bottom, 0px));
}

/* Pantalla propia (no un panel dentro de la hoja inferior de #sidebar): a
   pantalla completa y fija, funciona igual en escritorio y en móvil sin
   depender del hueco de 80dvh de la hoja inferior — ver .sidebar-content. */
.profile-screen {
  position: fixed;
  inset: 0;
  z-index: 1500;
  background: #fff;
  display: flex;
  flex-direction: column;
}

.profile-screen[hidden] {
  display: none;
}

.profile-screen-header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 1rem 1.25rem;
  padding-top: max(1rem, env(safe-area-inset-top, 0px));
  border-bottom: 1px solid #e2e2de;
}

.profile-screen-header h2 {
  margin: 0;
  font-size: 1.1rem;
}

.profile-back-btn {
  background: #f0f0ec;
  color: #444;
  border: none;
  border-radius: 6px;
  padding: 0.5rem 0.75rem;
  font-size: 0.9rem;
  cursor: pointer;
  flex-shrink: 0;
}

.profile-screen-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 1.25rem;
  padding-bottom: max(1.25rem, env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.profile-section + .profile-section {
  border-top: 1px solid #e2e2de;
  padding-top: 1rem;
}

.profile-section h3 {
  margin: 0 0 0.4rem;
  font-size: 0.9rem;
}

.profile-routes-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.profile-route-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.4rem 0.5rem;
  border: 1px solid #e2e2de;
  border-radius: 6px;
  font-size: 0.85rem;
}

.profile-route-item .profile-route-name {
  font-weight: 600;
  display: block;
}

.profile-route-item .profile-route-meta {
  color: #666;
  font-size: 0.8rem;
}

.profile-route-actions {
  display: flex;
  gap: 0.3rem;
  flex-shrink: 0;
}

.admin-toggle-label {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  flex-shrink: 0;
  font-size: 0.8rem;
  color: #444;
  white-space: nowrap;
}

#profile-save-btn {
  background: #1f6f43;
  color: white;
  padding: 0.65rem;
  font-size: 0.95rem;
  align-self: flex-start;
}

@media (max-width: 760px) {
  #app {
    display: block;
    position: relative;
    overflow: hidden;
    /* 100vh no se reduce cuando el navegador tiene su propia barra inferior
       (p.ej. Vivaldi) — se queda "midiendo" una pantalla más alta que la
       zona realmente visible, y eso empujaba la hoja inferior por debajo de
       lo que se ve. 100dvh sigue el viewport visible real; se deja 100vh
       antes como reserva para navegadores sin soporte. */
    height: 100vh;
    height: 100dvh;
  }

  #map {
    position: absolute;
    inset: 0;
    z-index: 1;
    height: 100%;
    width: 100%;
  }

  /* El panel se convierte en una hoja inferior a todo lo ancho y hasta el
     borde de la pantalla (como el buscador de Google Maps en móvil), con
     solo la manija visible por defecto y desplegable hacia arriba al
     tocar o deslizar la manija (ver sheet-handle en app.js). Solo se
     respeta el hueco de sistema (env(safe-area-inset-bottom), p.ej. la
     barra de gestos de iOS) — sin ese hueco, no se añade margen extra. */
  #sidebar {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    bottom: env(safe-area-inset-bottom, 0px);
    z-index: 10;
    width: 100%;
    min-width: 0;
    max-height: 80vh;
    max-height: 80dvh;
    border-right: none;
    border-top: 1px solid #e2e2de;
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.2);
  }

  .sheet-handle {
    display: flex;
    flex-direction: column;
    align-items: center;
    flex-shrink: 0;
    gap: 0.4rem;
    width: 100%;
    background: none;
    border: none;
    padding: 0.85rem 0 0.7rem;
    cursor: grab;
    touch-action: none;
  }

  .sheet-handle-bar {
    width: 42px;
    height: 4px;
    border-radius: 999px;
    background: #cfcfc8;
  }

  .sheet-handle-label {
    font-size: 0.78rem;
    font-weight: 600;
    color: #555;
  }

  /* El contenido se despliega/oculta variando su altura máxima según
     --sheet-progress (0 = colapsado, 1 = expandido). app.js actualiza esa
     variable al vuelo mientras se arrastra la manija, así el panel sigue
     al dedo en tiempo real en vez de saltar de golpe entre los dos
     estados; la transición de abajo solo entra al soltar (se desactiva
     durante el arrastre, ver .sheet-dragging), para animar el ajuste al
     estado más cercano. */
  .sidebar-content {
    max-height: calc(var(--sheet-progress, 0) * 80dvh);
    opacity: var(--sheet-progress, 0);
    padding-top: calc(var(--sheet-progress, 0) * 1.25rem);
    padding-bottom: calc(var(--sheet-progress, 0) * 1.25rem);
    transition:
      max-height 0.32s cubic-bezier(0.22, 0.8, 0.2, 1),
      opacity 0.25s ease,
      padding 0.32s ease;
  }

  #sidebar.sheet-dragging .sidebar-content {
    transition: none;
  }

  .map-attribution {
    display: block;
    margin-top: 0.2rem;
    padding-top: 0.7rem;
    border-top: 1px solid #e2e2de;
    font-size: 0.68rem;
    line-height: 1.45;
    color: #888;
    text-align: center;
  }

  .map-attribution a {
    color: #667;
  }

  /* Se duplica arriba, dentro del panel, porque el control de atribución
     de Leaflet vive pegado a la esquina del mapa y en móvil queda tapado
     por la barra propia del navegador (p.ej. Vivaldi) o por esta misma
     hoja inferior. */
  .leaflet-control-attribution {
    display: none;
  }

  /* Objetivos táctiles más grandes (mínimo recomendado ~44px de alto) —
     en escritorio con ratón no hace falta tanto margen. */
  input[type="number"],
  select,
  .header-profile-btn,
  #use-my-location {
    padding: 0.6rem 0.7rem;
    font-size: 1rem;
  }

  #search-btn {
    padding: 0.75rem;
    font-size: 1rem;
  }

  .action-btn {
    padding: 0.45rem 0.7rem;
    font-size: 0.8rem;
  }

  /* Con hasta 5 botones (GPX/Maps/OSM/Dar la vuelta/Favorito) no cabe todo
     en una fila junto al texto de fuente/distancia — se pasan a su propia
     línea, con envoltura libre en vez de recortarse. */
  .result-footer {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
  }

  .result-actions {
    width: 100%;
  }
}
