/**
 * MI FOUNDATION — Sistema de diseño base para Mi Mapa Interno
 * Este archivo se carga ANTES que style.css y menu.css
 * Define tokens, z-index scale, layout fixes y utilidades globales
 *
 * IMPORTANTE: No borrar ni reordenar. Todo el sistema depende de estas variables.
 */

/* ===== 1. DESIGN TOKENS — Z-INDEX SCALE ===== */
:root {
  /* Z-index: escala ordenada de menor a mayor. USAR SOLO ESTAS. */
  --z-base: 1;
  --z-above: 10;
  --z-header: 100;
  --z-menu: 200;
  --z-dropdown: 300;
  --z-sticky: 400;
  --z-floating-btn: 500;
  --z-notification: 600;
  --z-overlay: 700;
  --z-modal: 800;
  --z-modal-content: 801;
  --z-toast: 900;
  --z-tooltip: 950;
  --z-max: 999;

  /* Spacing scale — reemplaza vw sueltos */
  --space-2xs: 2px;
  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 12px;
  --space-lg: 16px;
  --space-xl: 24px;
  --space-2xl: 32px;
  --space-3xl: 48px;
  --space-4xl: 64px;

  /* Breakpoint reference (para documentación, CSS no puede usar vars en media queries) */
  /* --bp-mobile: 700px;    Mobile-first breakpoint */
  /* --bp-tablet: 1024px;   Tablet breakpoint (NUEVO) */
  /* --bp-desktop: 1500px;  Wide desktop cap */

  /* Typography scale con clamp() — fluid sin saltos bruscos */
  --text-xs: clamp(11px, 0.73vw, 11px);
  --text-sm: clamp(12px, 0.8vw, 12px);
  --text-base: clamp(14px, 1vw, 15px);
  --text-md: clamp(15px, 1.1vw, 16.5px);
  --text-lg: clamp(16px, 1.2vw, 18px);
  --text-xl: clamp(18px, 1.5vw, 22.5px);
  --text-2xl: clamp(20px, 1.73vw, 26px);
  --text-3xl: clamp(24px, 2vw, 30px);
  --text-4xl: clamp(28px, 3vw, 45px);

  /* Touch target mínimo para móvil */
  --touch-min: 44px;

  /* Viewport height real (se actualiza via JS para iOS) */
  --vh: 1vh;

  /* Modal transition */
  --modal-duration: 0.3s;
}


/* ===== 2. VIEWPORT HEIGHT FIX (iOS address bar) ===== */

/* dvh con fallback para navegadores viejos */
@supports (height: 100dvh) {
  :root {
    --vh-full: 100dvh;
  }
}
@supports not (height: 100dvh) {
  :root {
    --vh-full: calc(var(--vh, 1vh) * 100);
  }
}


/* ===== 3. LAYOUT FIX — SCROLL ARCHITECTURE ===== */

/*
 * Arquitectura de scroll corregida:
 *
 *   <body>
 *     <div id="page">          ← viewport container, NO scroll
 *       <nav id="menu">        ← fixed sidebar/top
 *       <div id="content">     ← ESTE es el que scrollea
 *     </div>
 *   </body>
 *
 * Regla: SOLO #content scrollea. Nada más.
 */

html {
  overflow: hidden;
  height: 100%;
}

body {
  overflow: hidden;
  height: 100%;
  /* Compensar scrollbar al abrir modal (prevents page shift) */
  scrollbar-gutter: stable;
}

/* Safe area insets para PWA */
@supports (padding: max(0px)) {
  body {
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }
}

#page {
  position: relative;
  height: var(--vh-full, 100vh);
  width: 100%;
  overflow: hidden;
}

/* #content scroll fix — este es el contenedor que DEBE scrollear */
#content {
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  scroll-behavior: smooth;
}
/* Padding inferior para que el contenido no quede detrás de la bottom nav */
@media (max-width: 700px) {
  #content {
    padding-bottom: calc(14vw + env(safe-area-inset-bottom, 0px) + 8px);
  }
}

/* Red de seguridad: touch targets mínimos en móvil */
@media (max-width: 700px) {
  button, [role="button"], a.boton_ppal, .btn, input[type="submit"] {
    min-height: var(--touch-min, 44px);
  }
}

/* Cuando hay modal abierto, impedir scroll en #content */
body.mi-modal-open #content {
  overflow: hidden;
}


/* ===== 4. MODAL SYSTEM — CSS ===== */

/*
 * Sistema de modales unificado.
 * Clases: .mi-modal, .mi-modal-overlay, .mi-modal-box, .mi-modal-close
 *
 * Uso en HTML:
 *   <div class="mi-modal" id="mi_modal_xxx" role="dialog" aria-modal="true">
 *     <div class="mi-modal-overlay"></div>
 *     <div class="mi-modal-box">
 *       <button class="mi-modal-close" aria-label="Cerrar">
 *         <i class="fas fa-times"></i>
 *       </button>
 *       <!-- contenido -->
 *     </div>
 *   </div>
 *
 * También funciona con los modales existentes (.modal + .modal-box)
 */

/* Overlay */
.mi-modal,
.modal {
  display: none;
  position: fixed;
  inset: 0;
  z-index: var(--z-overlay);
  background: rgba(0, 0, 0, 0.85);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  /* Centrado con flexbox en vez de margin */
  display: none;
  align-items: flex-start;
  justify-content: center;
  padding: var(--space-xl) var(--space-lg);
}

/* Cuando visible */
.mi-modal.is-open,
.modal[style*="display: block"],
.modal[style*="display:block"] {
  display: flex !important;
}

/* Box del modal */
.mi-modal-box,
.modal-box {
  position: relative;
  z-index: var(--z-modal-content);
  width: 90%;
  max-width: 620px;
  margin: auto;
  padding: var(--space-2xl);
  background: rgba(20, 16, 8, 0.95);
  border: 1px solid var(--mi-gold, #cc9e75);
  border-radius: var(--mi-radius-lg, 16px);
  animation: mi-modal-enter var(--modal-duration) cubic-bezier(0.34, 1.56, 0.64, 1);
  max-height: calc(var(--vh-full, 100vh) - var(--space-3xl) * 2);
  overflow-y: auto;
}

.claro .mi-modal-box,
.claro .modal-box {
  background: rgba(255, 255, 255, 0.97);
  border-color: var(--mi-gold, #cc9e75);
  color: var(--mi-dark, #0c0a06);
}

/* Botón cerrar — SIEMPRE tocable, siempre visible */
.mi-modal-close,
.close-modal {
  position: absolute;
  top: var(--space-md);
  right: var(--space-md);
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  width: var(--touch-min);
  height: var(--touch-min);
  min-width: var(--touch-min);
  min-height: var(--touch-min);
  padding: 0;
  margin: 0;
  border: none;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  cursor: pointer;
  color: var(--mi-white, #f0ebe3);
  font-size: 18px;
  transition: all 0.2s ease;
  -webkit-tap-highlight-color: transparent;
}

.close-modal i {
  /* Override del CSS original — quitar position: absolute */
  position: static !important;
  font-size: 18px !important;
  color: inherit !important;
}

.mi-modal-close:hover,
.mi-modal-close:focus-visible,
.close-modal:hover,
.close-modal:focus-visible {
  background: rgba(255, 255, 255, 0.2);
  outline: 2px solid var(--mi-gold, #cc9e75);
  outline-offset: 2px;
}

.mi-modal-close:active,
.close-modal:active {
  transform: scale(0.95);
}

/* Animación de entrada */
@keyframes mi-modal-enter {
  from {
    opacity: 0;
    transform: translateY(-20px) scale(0.97);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* Animación de salida (se añade via JS) */
.mi-modal.is-closing .mi-modal-box,
.modal.is-closing .modal-box {
  animation: mi-modal-exit var(--modal-duration) ease forwards;
}

@keyframes mi-modal-exit {
  from {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
  to {
    opacity: 0;
    transform: translateY(10px) scale(0.97);
  }
}

/* Modal loading state */
.mi-modal-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 200px;
  color: var(--mi-gray, #9a8e80);
}

.mi-modal-loading::after {
  content: '';
  width: 32px;
  height: 32px;
  border: 3px solid rgba(255, 255, 255, 0.1);
  border-top-color: var(--mi-gold, #cc9e75);
  border-radius: 50%;
  animation: mi-spin 0.6s linear infinite;
}

@keyframes mi-spin {
  to { transform: rotate(360deg); }
}


/* ===== 5. RESPONSIVE FIXES — TABLET BREAKPOINT ===== */

/* Tablet (700px - 1024px): zona muerta arreglada */
@media (min-width: 701px) and (max-width: 1024px) {
  body {
    font-size: clamp(14px, 1.8vw, 16px);
  }

  .modal-box,
  .mi-modal-box {
    width: 85%;
    max-width: 580px;
    padding: var(--space-xl);
  }
}

/* Mobile */
@media (max-width: 700px) {
  .modal-box,
  .mi-modal-box {
    width: calc(100% - var(--space-lg) * 2);
    max-width: none;
    margin: var(--space-lg) auto;
    padding: var(--space-xl) var(--space-lg);
  }

  .mi-modal,
  .modal {
    padding: var(--space-sm);
  }
}


/* ===== 6. TOAST / NOTIFICATIONS ===== */

.mi-toast {
  position: fixed;
  top: var(--space-xl);
  right: var(--space-xl);
  z-index: var(--z-toast);
  padding: var(--space-md) var(--space-xl);
  background: var(--mi-dark-2, #141008);
  border: 1px solid var(--mi-gold, #cc9e75);
  border-radius: var(--mi-radius-md, 12px);
  color: var(--mi-white, #f0ebe3);
  font-size: var(--text-sm);
  animation: mi-toast-in 0.3s ease;
  pointer-events: auto;
  max-width: 350px;
}

.mi-toast.is-leaving {
  animation: mi-toast-out 0.3s ease forwards;
}

@keyframes mi-toast-in {
  from { opacity: 0; transform: translateX(20px); }
  to { opacity: 1; transform: translateX(0); }
}

@keyframes mi-toast-out {
  from { opacity: 1; transform: translateX(0); }
  to { opacity: 0; transform: translateX(20px); }
}

@media (max-width: 700px) {
  .mi-toast {
    right: var(--space-md);
    left: var(--space-md);
    max-width: none;
  }
}


/* ===== 7. UTILITY: VISUALLY HIDDEN (accesibility) ===== */

.mi-sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
