/**
 * CSS Fallbacks y compatibilidad de navegadores
 * Incluye fixes específicos para Safari, iOS y navegadores legacy
 */

/* ============================================
   VIEWPORT UNITS Y ALTURA MÓVIL
   ============================================ */

:root {
  /* Fallback para viewport height en iOS */
  --vh: 1vh; /* Se actualiza con JS */
  
  /* Safe area insets para dispositivos con notch */
  --safe-area-top: env(safe-area-inset-top, 0);
  --safe-area-bottom: env(safe-area-inset-bottom, 0);
  --safe-area-left: env(safe-area-inset-left, 0);
  --safe-area-right: env(safe-area-inset-right, 0);
}

/* Altura completa de pantalla con fallbacks */
.min-screen {
  min-height: 100vh; /* Fallback */
  min-height: calc(var(--vh) * 100); /* iOS fix */
}

/* Soporte para nuevas viewport units */
@supports (height: 100dvh) {
  .min-screen {
    min-height: 100dvh; /* Dynamic viewport height */
  }
}

@supports (height: 100svh) {
  .min-screen-small {
    min-height: 100svh; /* Small viewport height */
  }
}

/* Safe areas para dispositivos con notch */
.header {
  padding-top: var(--safe-area-top);
}

.footer {
  padding-bottom: var(--safe-area-bottom);
}

.container-safe {
  padding-left: max(1rem, var(--safe-area-left));
  padding-right: max(1rem, var(--safe-area-right));
}

/* ============================================
   INPUTS Y FORMULARIOS MÓVILES
   ============================================ */

/* Prevenir zoom en iOS cuando se enfoca un input */
input, select, textarea, button {
  font-size: 16px; /* Mínimo para evitar zoom en iOS */
}

/* Text size adjust para prevenir escalado automático */
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

/* Touch targets mínimos de 44px */
.btn, button, 
.btn-link, a[role="button"],
input[type="button"], input[type="submit"], input[type="reset"] {
  min-height: 44px;
  min-width: 44px;
  touch-action: manipulation; /* Prevenir double-tap zoom */
}

/* Links táctiles */
a {
  min-height: 44px;
  display: inline-block;
  touch-action: manipulation;
}

/* Inputs táctiles */
input, select, textarea {
  min-height: 44px;
  touch-action: manipulation;
}

/* ============================================
   FLEXBOX GAP FALLBACKS
   ============================================ */

/* Fallback para flex gap en Safari < 14.1 */
.no-flexgap .flex-gap > * {
  margin-right: 12px;
  margin-bottom: 12px;
}

.no-flexgap .flex-gap > *:last-child {
  margin-right: 0;
}

.no-flexgap .flex-gap-sm > * {
  margin-right: 8px;
  margin-bottom: 8px;
}

.no-flexgap .flex-gap-lg > * {
  margin-right: 16px;
  margin-bottom: 16px;
}

/* Grid gap fallbacks */
.no-flexgap .grid-gap {
  margin: -12px -12px 0 0;
}

.no-flexgap .grid-gap > * {
  margin: 12px 12px 0 0;
}

/* ============================================
   STICKY POSITIONING FIXES
   ============================================ */

/* Fix para sticky en iOS - evitar transforms en ancestros */
.sticky-container {
  /* No usar transform, filter, o perspective aquí */
  position: relative;
}

.sticky-element {
  position: sticky;
  top: 0;
  /* Backup para navegadores sin soporte */
  position: -webkit-sticky;
}

/* Fix para overflow hidden en contenedores sticky */
.sticky-parent {
  overflow: visible; /* No usar hidden/auto */
}

/* ============================================
   SCROLL BEHAVIOR Y OVERSCROLL
   ============================================ */

/* Prevenir rebote en iOS */
html, body {
  overscroll-behavior-y: contain;
  -webkit-overflow-scrolling: touch; /* Smooth scrolling en iOS legacy */
}

/* Smooth scroll con fallback */
@supports (scroll-behavior: smooth) {
  html {
    scroll-behavior: smooth;
  }
}

/* ============================================
   WEBKIT SPECIFIC FIXES
   ============================================ */

/* Fix para inputs en Safari */
input[type="search"] {
  -webkit-appearance: none;
  appearance: none;
}

/* Fix para select en Safari */
select {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath fill='none' stroke='%23343a40' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M2 5l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  background-size: 16px;
  padding-right: 40px;
}

/* Fix para buttons en Safari */
button, input[type="button"], input[type="submit"], input[type="reset"] {
  -webkit-appearance: none;
  appearance: none;
  border-radius: 4px;
}

/* ============================================
   FOCUS VISIBLE POLYFILL
   ============================================ */

/* Focus visible para navegadores sin soporte */
.js-focus-visible :focus:not(.focus-visible) {
  outline: none;
}

.focus-visible {
  outline: 2px solid var(--primary, #0066cc);
  outline-offset: 2px;
}

/* ============================================
   PICTURE ELEMENT FALLBACKS
   ============================================ */

/* Fallback para navegadores sin soporte de picture */
picture img {
  width: 100%;
  height: auto;
}

/* Object-fit fallback */
.object-cover {
  object-fit: cover;
}

/* Para IE/Edge legacy */
@supports not (object-fit: cover) {
  .object-cover {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }
  
  .object-cover-container {
    position: relative;
    overflow: hidden;
  }
}

/* ============================================
   ASPECT RATIO FALLBACKS
   ============================================ */

/* Aspect ratio con fallback */
.aspect-ratio-16-9 {
  aspect-ratio: 16 / 9;
}

/* Fallback para navegadores sin aspect-ratio */
@supports not (aspect-ratio: 16 / 9) {
  .aspect-ratio-16-9 {
    position: relative;
    width: 100%;
    height: 0;
    padding-bottom: 56.25%; /* 9/16 * 100% */
  }
  
  .aspect-ratio-16-9 > * {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }
}

/* ============================================
   CSS GRID FALLBACKS
   ============================================ */

/* Fallback para CSS Grid */
@supports not (display: grid) {
  .grid {
    display: flex;
    flex-wrap: wrap;
  }
  
  .grid-item {
    flex: 1 1 300px; /* Ancho mínimo */
    margin: 8px;
  }
}

/* ============================================
   CUSTOM PROPERTIES FALLBACKS
   ============================================ */

/* Fallbacks para custom properties críticas */
.btn-primary {
  background-color: #0066cc; /* Fallback */
  background-color: var(--primary, #0066cc);
  color: white;
  color: var(--primary-contrast, white);
}

.text-muted {
  color: #666;
  color: var(--fg-muted, #666);
}

/* ============================================
   PRINT STYLES
   ============================================ */

@media print {
  /* Optimizaciones para impresión */
  .no-print {
    display: none !important;
  }
  
  a[href]:after {
    content: " (" attr(href) ")";
  }
  
  .btn {
    border: 1px solid #000;
    background: transparent !important;
    color: #000 !important;
  }
}
