/* ===== DETECCIÓN Y ACTIVACIÓN ===== */
.ios-device {
  --safe-top: env(safe-area-inset-top, 20px);
  --safe-bottom: env(safe-area-inset-bottom, 20px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

/* ===== FIX CRÍTICO: SCROLL VERTICAL ===== */
.ios-device html {
  overflow-x: hidden;
  overflow-y: auto !important; /* CRÍTICO: permitir scroll */
  -webkit-overflow-scrolling: touch;
  max-width: 100vw;
  height: 100%;
}

.ios-device body {
  overflow-x: hidden;
  overflow-y: auto !important; /* CRÍTICO: permitir scroll */
  -webkit-overflow-scrolling: touch;
  max-width: 100vw;
  min-height: 100vh;
  position: relative;
  /* Prevenir bounce solo en horizontal */
  overscroll-behavior-x: none;
  overscroll-behavior-y: auto;
}

/* ===== FIX HEADER: CENTRADO Y SAFE AREA ===== */
.ios-device .header {
  position: fixed;
  top: 0;
  left: 50% !important;
  transform: translateX(-50%) !important;
  width: 100%;
  max-width: 414px !important;
  height: 60px;
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
  z-index: 1000;
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
}

/* Header en pantallas pequeñas */
@media (max-width: 413px) {
  .ios-device .header {
    left: 0 !important;
    right: 0 !important;
    transform: none !important;
    max-width: 100vw !important;
  }
}

/* Header auto-hide */
.ios-device .header.navbar-hidden {
  transform: translateX(-50%) translateY(-100%) !important;
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.ios-device .header.navbar-visible {
  transform: translateX(-50%) translateY(0) !important;
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

@media (max-width: 413px) {
  .ios-device .header.navbar-hidden {
    transform: translateY(-100%) !important;
  }

  .ios-device .header.navbar-visible {
    transform: translateY(0) !important;
  }
}

.ios-device .header .container {
  height: 100%;
  padding-top: var(--safe-top);
  padding-bottom: 10px;
}

/* ===== FIX MODALES: CENTRADO VERTICAL Y SCROLL ===== */
.ios-device .modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 9999;
  background-color: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  display: none;
  overflow: hidden;
}

.ios-device .modal.show {
  display: flex !important;
  justify-content: center;
  align-items: center !important;
  padding: 20px;
}

.ios-device .modal-content {
  position: relative !important;
  bottom: auto !important;
  left: auto !important;
  right: auto !important;
  top: auto !important;
  transform: none !important;
  margin: auto;
  border-radius: 20px !important;
  max-height: 85vh;
  max-width: 500px;
  width: 90%;
  overflow: hidden;
  background: var(--gradient-card);
  backdrop-filter: blur(30px);
  -webkit-backdrop-filter: blur(30px);
  border: 1px solid var(--border-primary);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
  /* Prevenir que se mueva */
  will-change: auto;
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
}

/* CRÍTICO: Modal body debe permitir scroll interno */
.ios-device .modal-body {
  max-height: calc(85vh - 70px);
  overflow-y: scroll !important;
  overflow-x: hidden !important;
  -webkit-overflow-scrolling: touch !important;
  overscroll-behavior-y: contain;
  padding: 20px;
  padding-bottom: calc(20px + var(--safe-bottom));
  position: relative;
  touch-action: pan-y !important;
  will-change: scroll-position;
  min-height: 0 !important;
}

/* Forzar scroll visible en modal-body */
.ios-device .modal-body::after {
  content: "";
  display: block;
  height: 1px;
  width: 1px;
}

/* Prevenir scroll del body cuando modal está abierto */
.ios-device body.modal-open {
  overflow: hidden !important;
  position: fixed !important;
  width: 100% !important;
  height: 100% !important;
  touch-action: none;
}

/* ===== FIX CONTAINERS Y CARDS ===== */
.ios-device .container {
  max-width: 100%;
  width: calc(100% - 40px);
  margin: 0 auto;
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

.ios-device .weather-card,
.ios-device .hourly-forecast,
.ios-device .daily-forecast,
.ios-device .historical-weather .weather-card {
  width: 100%;
  max-width: 100%;
  margin-left: 0;
  margin-right: 0;
  transform: translateZ(0);
  -webkit-transform: translateZ(0);
}

/* ===== FIX SCROLL HORIZONTAL EN HOURLY ===== */
.ios-device .hourly-scroll {
  overflow-x: auto !important;
  overflow-y: hidden !important;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
}

/* ===== FIX SECCIONES ===== */
.ios-device .current-weather {
  padding-top: 70px;
  width: 100%;
  max-width: 100vw;
  overflow-x: hidden;
}

.ios-device .historical-weather {
  width: 100%;
  max-width: 100vw;
  overflow-x: hidden;
  padding-bottom: var(--safe-bottom);
}

/* ===== FIX DETAIL ITEMS (CONTAINERS PRESION, HUMEDAD, ETC) ===== */
/* Forzar grid 2x2 exactamente como en Android */
.ios-device .weather-details {
  display: grid !important;
  grid-template-columns: repeat(2, 1fr) !important;
  grid-template-rows: auto auto !important; /* 2 filas */
  gap: 2px !important;
  background: var(--surface-light) !important;
  border-radius: 16px !important;
  overflow: hidden !important;
  box-shadow: var(--shadow-small) !important;
  /* Forzar que se mantenga el grid incluso con contenido dinámico */
  will-change: auto !important;
}

.ios-device .detail-item {
  display: flex !important;
  flex-direction: column !important;
  align-items: center !important;
  justify-content: center !important;
  gap: 4px !important;
  padding: 16px 12px !important;
  background: var(--surface-glass) !important;
  backdrop-filter: blur(10px) saturate(180%) !important;
  -webkit-backdrop-filter: blur(10px) saturate(180%) !important;
  text-align: center !important;
  min-height: 90px !important;
  /* Forzar que cada item ocupe 1 celda del grid */
  grid-column: span 1 !important;
  grid-row: span 1 !important;
  /* Forzar layout para contenido dinámico */
  width: 100% !important;
  height: 100% !important;
  box-sizing: border-box !important;
}

/* Forzar layout correcto para cada hijo del detail-item */
.ios-device .detail-item > * {
  width: 100% !important;
  text-align: center !important;
  display: block !important;
}

/* Bordes redondeados específicos para cada esquina */
.ios-device .detail-item:nth-child(1) {
  border-top-left-radius: 14px !important;
}

.ios-device .detail-item:nth-child(2) {
  border-top-right-radius: 14px !important;
}

.ios-device .detail-item:nth-child(3) {
  border-bottom-left-radius: 14px !important;
}

.ios-device .detail-item:nth-child(4) {
  border-bottom-right-radius: 14px !important;
}

/* Asegurar que el icono, label y valor se muestren correctamente */
.ios-device .detail-item i {
  color: var(--text-accent) !important;
  font-size: 16px !important;
  display: block !important;
  margin-bottom: 2px !important;
}

.ios-device .detail-item span:nth-child(2) {
  font-size: 11px !important;
  color: var(--text-muted) !important;
  font-weight: 400 !important;
  text-transform: uppercase !important;
  letter-spacing: 0.5px !important;
  display: block !important;
}

.ios-device .detail-item span:nth-child(3) {
  font-size: 17px !important;
  font-weight: 500 !important;
  color: var(--text-primary) !important;
  display: block !important;
}

/* ===== FIX INPUTS Y SELECTS ===== */
.ios-device input,
.ios-device select,
.ios-device textarea {
  font-size: 16px !important; /* Prevenir zoom */
  -webkit-appearance: none;
  appearance: none;
  border-radius: 12px;
}

.ios-device input[type="date"] {
  min-height: 44px;
}

/* ===== FIX BOTONES TÁCTILES ===== */
.ios-device button,
.ios-device .header-btn,
.ios-device .consult-btn {
  min-height: 44px;
  min-width: 44px;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0.1);
  cursor: pointer;
}

.ios-device button:active,
.ios-device .header-btn:active {
  opacity: 0.7;
  transform: scale(0.95);
  transition: transform 0.1s ease;
}

/* ===== FIX VIEWPORT HEIGHT ===== */
.ios-device {
  --vh: 1vh;
}

.ios-device body {
  min-height: calc(var(--vh, 1vh) * 100);
}

@supports (height: 100dvh) {
  .ios-device body {
    min-height: 100dvh;
  }
}

/* ===== FIX GRÁFICOS ===== */
.ios-device #graficoClima {
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
  width: 100%;
  max-width: 100%;
}

.ios-device .chart-container {
  overflow-x: hidden !important;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* ===== FIX SPLASH SCREEN ===== */
.ios-device .splash-screen {
  height: 100vh;
  height: -webkit-fill-available;
}

@supports (height: 100dvh) {
  .ios-device .splash-screen {
    height: 100dvh;
  }
}

/* ===== FIX FONDO PARA iOS ===== */
.ios-background-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: -1;
  background-size: cover;
  background-position: center center;
  background-repeat: no-repeat;
  will-change: background-image;
  transition: opacity 0.6s ease-in-out;
}

.ios-device body {
  background: transparent !important;
}

/* ===== MODAL INSTALACIÓN iOS - CENTRADO CORRECTO ===== */
.ios-device .ios-install-modal {
  display: none;
  position: fixed;
  z-index: 99999;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  opacity: 0;
  transition: opacity 0.3s ease;
  /* NO overflow-y aquí */
  overflow: hidden;
}

.ios-device .ios-install-modal.show {
  display: flex !important;
  justify-content: center;
  align-items: center !important; /* Centrado vertical forzado */
  opacity: 1;
  padding: 20px; /* Padding para que no toque los bordes */
}

.ios-device .ios-install-content {
  background: var(--gradient-card);
  backdrop-filter: blur(30px);
  -webkit-backdrop-filter: blur(30px);
  /* Bordes redondeados en todos los lados */
  border-radius: 20px !important;
  max-width: 90%;
  width: 380px;
  max-height: 85vh;
  overflow-y: auto; /* Scroll interno */
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border-primary);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
  /* Posicionamiento relativo */
  position: relative;
  margin: auto;
  /* Forzar que no se deforme */
  flex-shrink: 0;
}

/* Pasos de instalación */
.install-steps {
  display: flex;
  flex-direction: column;
  gap: 20px;
  margin-bottom: 20px;
}

.install-step {
  display: flex;
  gap: 16px;
  align-items: flex-start;
  padding: 16px;
  background: var(--surface-glass);
  border-radius: 12px;
  border: 1px solid var(--border-primary);
}

.step-number {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  background: linear-gradient(135deg, #007aff, #0051d5);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 16px;
}

.step-content h3 {
  font-size: 15px;
  font-weight: 600;
  margin: 0 0 6px 0;
}

.step-content p {
  font-size: 14px;
  margin: 0;
  line-height: 1.5;
}

.install-note {
  display: flex;
  gap: 12px;
  padding: 14px;
  background: rgba(0, 122, 255, 0.1);
  border: 1px solid rgba(0, 122, 255, 0.3);
  border-radius: 12px;
}

.install-note i {
  color: #007aff;
  font-size: 18px;
  flex-shrink: 0;
}

.install-note p {
  margin: 0;
  font-size: 13px;
  line-height: 1.5;
}

/* ===== OPTIMIZACIONES GENERALES ===== */
.ios-device * {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  box-sizing: border-box;
}

.ios-device input,
.ios-device textarea,
.ios-device .modal-body,
.ios-device .info-section {
  -webkit-user-select: text;
  user-select: text;
}

.ios-device button,
.ios-device a {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

/* ===== RESPONSIVE ===== */
@media (max-width: 414px) {
  .ios-device .modal-content,
  .ios-device .ios-install-content {
    max-width: 95%;
    width: 95%;
  }
}

@media (orientation: landscape) and (max-height: 500px) {
  .ios-device .modal-content,
  .ios-device .ios-install-content {
    max-height: 90vh;
    margin: 2vh auto;
  }

  .ios-device .install-steps {
    gap: 12px;
  }

  .ios-device .install-step {
    padding: 12px;
  }
}

/* ===== MODO STANDALONE (PWA instalada) ===== */
@media all and (display-mode: standalone) {
  .ios-device .install-btn {
    display: none !important;
  }
}

/* ===== ACCESIBILIDAD ===== */
@media (prefers-reduced-motion: reduce) {
  .ios-device *,
  .ios-device *::before,
  .ios-device *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* ===== FIX WEATHER ICON ===== */
.ios-device #weatherIcon {
  font-style: normal !important;
  display: inline-block !important;
}

/* ===== FIX BLUR EN iOS ===== */
.ios-device .weather-card,
.ios-device .modal-content,
.ios-device .ios-install-content,
.ios-device .detail-item {
  -webkit-backdrop-filter: blur(30px) saturate(180%);
  backdrop-filter: blur(30px) saturate(180%);
}

.ios-device .header {
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  backdrop-filter: blur(20px) saturate(180%);
}

/* ===== FIX BACKGROUND EN iOS ===== */
.ios-device body {
  background-size: cover !important;
  background-position: center center !important;
  background-repeat: no-repeat !important;
  /* En iOS, usar scroll en lugar de fixed */
  background-attachment: scroll !important;
}

/* Forzar repaint del background */
.ios-device body::after {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  z-index: -1;
  transition: none;
}

/* ===== ESTILOS PARA MODAL DE INSTALACIÓN iOS ===== */
.ios-install-modal {
  display: none;
  position: fixed;
  z-index: 99999;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  opacity: 0;
  transition: opacity 0.3s ease;
  overflow: hidden;
}

.ios-install-modal.show {
  display: flex !important;
  justify-content: center;
  align-items: center !important;
  opacity: 1;
  padding: 20px;
}

.ios-install-content {
  background: var(--gradient-card);
  backdrop-filter: blur(30px);
  -webkit-backdrop-filter: blur(30px);
  border-radius: 20px !important;
  max-width: 90%;
  width: 380px;
  max-height: 85vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border-primary);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
  position: relative;
  margin: auto;
  flex-shrink: 0;
}