* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body {
  margin: 0; height: 100%;
  font-family: system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
}
body { display: flex; }

/* ---------------- Topbar (sólo visible en móvil) ---------------- */
#topbar {
  display: none;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: #1c1f24;
  color: #e6e6e6;
  border-bottom: 1px solid #2f343c;
  z-index: 1500;
  flex-shrink: 0;
}
#topbar h1 { margin: 0; font-size: 16px; flex: 1; letter-spacing: 0.5px; }
.brand-icon {
  height: 24px;
  width: auto;
  display: inline-block;
  vertical-align: middle;
  border-radius: 4px;
  margin-right: 6px;
}
#panel h1.panel-h1 {
  display: flex;
  align-items: center;
  gap: 8px;
}
#panel h1.panel-h1 .brand-icon { margin: 0; height: 22px; }
#panel-toggle {
  background: #2a2e35;
  color: #e6e6e6;
  border: 1px solid #3a3f47;
  border-radius: 6px;
  padding: 8px 14px;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  min-width: 44px;
  min-height: 44px;
}

#panel-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 1399;
  -webkit-tap-highlight-color: transparent;
}

/* ---------------- Panel lateral ---------------- */
#panel {
  width: 300px;
  flex: 0 0 auto;
  padding: 14px;
  background: #1c1f24;
  color: #e6e6e6;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
#panel h1.panel-h1 {
  margin: 0;
  font-size: 16px;
  letter-spacing: 0.5px;
  color: #f5f5f5;
}
#panel hr {
  border: none;
  border-top: 1px solid #2f343c;
  margin: 4px 0;
}
#panel label {
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 12px;
  color: #c8ccd2;
}
#panel select,
#panel input[type=number],
#panel input[type=range] {
  background: #2a2e35;
  color: #e6e6e6;
  border: 1px solid #3a3f47;
  border-radius: 3px;
  padding: 4px 6px;
  font: inherit;
}
#panel button {
  background: #2a2e35;
  color: #e6e6e6;
  border: 1px solid #3a3f47;
  border-radius: 3px;
  padding: 4px 10px;
  cursor: pointer;
  font: inherit;
}
#panel button:hover { background: #353a42; }
#panel button:disabled { opacity: 0.4; cursor: default; }

#panel > label:has(#compare) {
  flex-direction: row;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: #b8bdc4;
  cursor: pointer;
}

.pane-block {
  padding: 8px 10px;
  background: #23272e;
  border: 1px solid #2f343c;
  border-radius: 4px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.pane-title {
  margin: 0;
  font-size: 10px;
  color: #7d848e;
  text-transform: uppercase;
  letter-spacing: 1px;
  font-weight: 600;
}

/* ---------------- iOS-style toggle ---------------- */
.ios-toggle,
#panel label.ios-toggle {
  display: inline-flex;
  flex-direction: row;
  align-items: center;
  gap: 8px;
  padding: 2px 4px;
  font-size: 12px;
  color: #c8ccd2;
  cursor: pointer;
  user-select: none;
  align-self: flex-start;
}
.ios-toggle input { display: none; }
.ios-slider {
  position: relative;
  width: 28px;
  height: 16px;
  background: #ccc;
  border-radius: 16px;
  transition: background 0.2s;
  flex-shrink: 0;
}
.ios-slider::before {
  content: "";
  position: absolute;
  width: 12px; height: 12px;
  left: 2px; top: 2px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.2s;
  box-shadow: 0 1px 2px rgba(0,0,0,0.3);
}
.ios-toggle input:checked + .ios-slider { background: #34c759; }
.ios-toggle input:checked + .ios-slider::before { transform: translateX(12px); }

/* ---------------- Step + range + export ---------------- */
#step-controls {
  display: grid;
  grid-template-columns: auto auto auto 1fr;
  gap: 4px;
  align-items: center;
}
#step-controls #step { grid-column: 1 / -1; }
#step-label {
  grid-column: 1 / -1;
  font-size: 11px;
  color: #8d949e;
  text-align: center;
}

#range-controls { display: flex; gap: 8px; }
#range-controls label {
  flex: 1;
  flex-direction: row;
  align-items: center;
  gap: 4px;
  font-size: 11px;
}
#range-controls select { flex: 1; min-width: 0; }

#export-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}
#export-progress { flex: 1; height: 6px; }

#status {
  font-size: 11px;
  color: #7d848e;
  min-height: 1em;
}

/* ---------------- Mapas ---------------- */
#maps {
  flex: 1;
  display: flex;
  min-width: 0;
  min-height: 0;
}
.map-container { position: relative; flex: 1; min-width: 0; min-height: 0; }
.map-target { position: absolute; inset: 0; }

.map-time-label {
  position: absolute;
  top: 8px;
  left: 8px;
  z-index: 750;
  background: rgba(28, 31, 36, 0.85);
  color: #f5f5f5;
  border-radius: 6px;
  padding: 6px 10px;
  pointer-events: none;
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  font-size: 13px;
  line-height: 1.2;
}
.map-time-label .time-main {
  font-weight: 600;
  font-size: 14px;
  letter-spacing: 0.3px;
}
.map-time-label .time-sub {
  font-size: 11px;
  color: #b8bdc4;
  margin-top: 2px;
}

@media (max-width: 900px) {
  /* En móvil ya hay barra inferior con la fecha — ocultamos la flotante */
  .map-time-label { display: none; }
}

.map-attribution {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 750;
  background: rgba(255,255,255,0.85);
  border-radius: 4px;
  padding: 3px 7px;
  font-size: 10px;
  color: #222;
  pointer-events: auto;
  box-shadow: 0 1px 2px rgba(0,0,0,0.2);
  user-select: none;
  max-width: 60%;
}
.map-attribution a { color: #1565c0; text-decoration: none; }
.map-attribution a:hover { text-decoration: underline; }

.legend-overlay {
  position: absolute;
  left: 8px; right: 8px; bottom: 8px;
  display: flex;
  justify-content: center;
  gap: 6px;
  pointer-events: none;
  z-index: 800;
  flex-wrap: wrap;
  max-height: 40%;
  overflow: hidden;
}
.legend-overlay canvas {
  background: rgba(255,255,255,0.92);
  border-radius: 4px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
  max-width: 100%;
}

.city-label {
  background: transparent;
  border: none;
  box-shadow: none;
  padding: 0;
  font-size: 9px;
  font-weight: 500;
  color: #222;
  text-shadow: 0 0 2px #fff, 0 0 2px #fff, 0 0 2px #fff;
}
.leaflet-tooltip.city-label::before { display: none; }

.leaflet-image-layer.pixelated {
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.wind-icon { background: transparent; border: none; }
.wind-barb {
  width: 22px; height: 22px;
  font-size: 18px; line-height: 22px; text-align: center;
  color: #222; font-weight: bold;
}

/* ===========================================================
   MOBILE / TABLET (≤ 900px)
   =========================================================== */
@media (max-width: 900px) {
  body {
    flex-direction: column !important;
  }

  #topbar { display: flex !important; }
  #topbar h1 {
    font-size: 16px !important;
    font-weight: 600;
  }

  /* Panel SIEMPRE drawer overlay en este modo.
     - Arranca debajo del topbar (~56 px).
     - Termina encima de la step-bar (~100 px).
     Así nunca queda tapado por las dos barras fijas. */
  #panel {
    position: fixed !important;
    top: 56px !important;
    bottom: 100px !important;
    left: 0 !important;
    width: min(340px, 86vw) !important;
    height: auto !important;
    z-index: 1400 !important;
    padding-top: 14px !important;
    padding-bottom: 14px !important;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 4px 0 12px rgba(0,0,0,0.5);
  }
  #panel.open { transform: translateX(0); }
  #panel h1.panel-h1 { display: none !important; }

  #maps {
    width: 100% !important;
    flex: 1 !important;
    min-height: 0;
  }
  #maps.compare-on {
    flex-direction: column;
  }
  /* Importante: ambos mapas comparten el espacio disponible (sin min-height fijo),
     así el mapa B no se sale por debajo dejando la leyenda detrás de la step-bar. */
  #maps.compare-on .map-container {
    flex: 1 1 0;
    min-height: 0;
  }

  /* Inputs cómodos pero compactos para que todo el panel quepa sin scroll */
  #panel { gap: 7px !important; padding: 12px !important; padding-top: 12px !important; padding-bottom: 12px !important; }
  #panel select,
  #panel input[type=number],
  #panel button {
    font-size: 13px;
    padding: 6px 8px;
    min-height: 32px;
  }
  #panel button { padding: 6px 12px; }
  #panel label { font-size: 11px; gap: 2px; }
  .pane-block { padding: 6px 8px !important; gap: 4px !important; }
  .pane-title { font-size: 9px !important; }
  #panel hr { margin: 2px 0 !important; }

  .ios-slider { width: 32px; height: 18px; }
  .ios-slider::before { width: 14px; height: 14px; }
  .ios-toggle input:checked + .ios-slider::before { transform: translateX(14px); }
  .ios-toggle { font-size: 12px; padding: 0 !important; }

  #panel input[type=range] { height: 22px; }
  #step-label { font-size: 12px; }
  #range-controls { gap: 6px; }
  #export-controls { gap: 6px; }
  #status { font-size: 10px; }

  /* Desde/Hasta se apilan en vertical en móvil — los textos largos no caben en una fila */
  #range-controls { flex-direction: column; gap: 8px; }
  #range-controls label { width: 100%; }
  #range-controls select { width: 100%; min-width: 0; }

  /* Botones de export ocupan ancho completo y no se cortan */
  #export-controls { gap: 8px; }
  #export-controls button { flex: 1 1 calc(50% - 4px); }

  .legend-overlay { left: 4px; right: 4px; bottom: 4px; }
  .legend-overlay canvas { transform: scale(0.85); transform-origin: bottom center; }
  .city-label { font-size: 10px; }
}

@media (max-width: 380px) {
  #panel { width: 92vw !important; }
}

/* ---------- Mobile floating step host (recibe #step-controls movido) ---------- */
#mobile-step-host { display: none; }
@media (max-width: 900px) {
  #mobile-step-host {
    display: block;
    position: fixed;
    bottom: 0; left: 0; right: 0;
    z-index: 1450;
    background: linear-gradient(180deg, rgba(28,31,36,0.85) 0%, rgba(28,31,36,0.96) 30%);
    backdrop-filter: blur(8px) saturate(1.2);
    -webkit-backdrop-filter: blur(8px) saturate(1.2);
    color: #e6e6e6;
    padding: 10px 14px 8px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
    border-top: 1px solid rgba(255,255,255,0.08);
    box-shadow: 0 -6px 20px rgba(0,0,0,0.35);
  }
  #mobile-step-host #step-controls {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
    margin: 0;
  }
  /* Botones centrados en su fila */
  #mobile-step-host #step-controls > button {
    /* Usaremos un truco: los 3 botones van en una fila aparte */
  }
  #mobile-step-host .mobile-buttons-row {
    display: flex;
    justify-content: center;
    gap: 10px;
  }
  #mobile-step-host button {
    background: rgba(255,255,255,0.08);
    color: #f5f5f5;
    border: 1px solid rgba(255,255,255,0.12);
    border-radius: 999px;
    min-width: 36px; min-height: 30px;
    padding: 0 10px;
    font-size: 13px;
    cursor: pointer;
    transition: background 0.15s;
  }
  #mobile-step-host button:active { background: rgba(255,255,255,0.18); }
  #mobile-step-host #play {
    background: #4a90e2;
    border-color: #4a90e2;
    min-width: 42px;
    font-size: 14px;
  }
  #mobile-step-host #play:active { background: #357ab8; }

  #mobile-step-host input[type=range] {
    height: 32px;
    accent-color: #4a90e2;
    margin: 0 4px;
  }
  #mobile-step-host #step-label {
    font-size: 11.5px;
    color: #b8bdc4;
    text-align: center;
    letter-spacing: 0.3px;
    margin-top: -2px;
  }

  body { padding-bottom: 0; }
  /* El padding-bottom real lo fija JS según la altura medida del step-bar.
     Como fallback ponemos uno razonable. */
  #maps { padding-bottom: 110px; }
  .legend-overlay { bottom: 10px; }

  /* Atribución: más compacta y con wrap controlado en móvil */
  .map-attribution {
    font-size: 9px;
    padding: 2px 6px;
    max-width: 72%;
    line-height: 1.3;
  }
}
