/* =========================================
   1. CONFIGURACIÓN GLOBAL
   ========================================= */
:root {
  --gap-base: 16px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent; /* Evita efecto gris al tocar (Apple) */
}

/* Prevención de selección de texto (botones y textos) */
button,
h1, h2, h3,
h1 span,
.name-slot,
.name-slot h3,
.word-display,
.mnemo-active {
  user-select: none;
  -webkit-user-select: none;
}

html {
  height: 100vh;
  height: -webkit-fill-available; /* iOS antiguo */
  height: 100dvh; /* Estándar moderno */
  overflow: hidden;
}

body {
  width: 100%;
  height: 100vh;
  height: -webkit-fill-available; /* iOS antiguo */
  height: 100dvh; /* Estándar moderno */
  overflow: hidden;
  margin: 0;
  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);
  
  /* 2. Contenedor relativo para centrar #root desde el centro */
  position: relative;
  
  /* 3. Estética y comportamiento */
  touch-action: manipulation;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: linear-gradient(135deg, #4169E1 0%, #1e3a8a 50%, #0f172a 100%);
  overscroll-behavior-y: contain;
  position: fixed;
  box-sizing: border-box;
}

@media (orientation: landscape) {
  body {
    justify-content: flex-start;
    padding-top: env(safe-area-inset-top, 5px);
    padding-bottom: env(safe-area-inset-bottom, 5px);
  }
}

#root {
  height: 100vh;
  height: -webkit-fill-available; /* iOS antiguo */
  height: 100dvh; /* Dynamic Viewport: se encoge solo si sale el teclado */
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  overflow: hidden;
  align-items: center;
  touch-action: pan-y; /* Android: permite scroll vertical, evita gestos laterales accidentales */
  width: 100%;
  box-sizing: border-box;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  transform-origin: center center;
}

/* =========================================
   2. CONTENEDOR PRINCIPAL
   ========================================= */
#app {
  width: 90% !important;
  max-width: 450px !important;
  max-height: 85vh;
  height: auto;
  min-height: 0;
  margin: 10px auto 2px auto !important;
  background: white;
  padding: 25px;
  border-radius: 20px;
  box-shadow: 0 10px 25px rgba(0,0,0,0.1);
  position: relative;
  z-index: 10;
  text-align: center;
  flex-shrink: 0;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  overflow-y: auto;
}

#app h2 {
  font-size: 3rem;
  margin: 0 0 15px;
  color: #4169E1;
  font-weight: 700;
}

/* =========================================
   5. SLOT DE IMAGEN
   ========================================= */
.image-slot {
  width: 100%;
  max-height: 250px;
  height: auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto;
  overflow: hidden;
  flex-shrink: 0;
}

#app img,
.responsive-image {
  max-height: 100%;
  width: 100%;
  object-fit: contain; /* Lógica clave: ajusta sin estirar */
  flex-shrink: 1; /* Permite que la imagen se achique si el teclado ocupa espacio */
  height: auto;
  image-rendering: auto; /* Nítida en Retina / Super AMOLED */
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  -webkit-user-drag: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  pointer-events: none;
}

.image-slot.hidden img {
  visibility: hidden;
  opacity: 0;
}

/* =========================================
   6. NOMBRE Y CONTROLES
   ========================================= */
.name-slot {
  min-height: 2.5rem;
  margin: 10px 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.name-slot h3 {
  font-size: 1.8rem;
  color: #333;
  margin: 0;
}

.name-slot.hidden h3 {
  visibility: hidden;
}

.controls {
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.row {
  display: flex;
  gap: 10px;
  justify-content: center;
}

button {
  flex: 1;
  padding: 12px 15px;
  font-size: 1rem;
  border: none;
  border-radius: 8px;
  background: #4169E1;
  color: white;
  cursor: pointer; /* Surface Pen, trackpad: indica elemento interactivo */
  transition: all 0.2s ease;
  touch-action: manipulation; /* Elimina delay 300ms en iOS/Android antiguos */
}

button:active {
  transform: translateY(1px);
}

/* =========================================
   7. RESPONSIVE - TABLETS Y PANTALLAS GRANDES (iPad y Tablets Android)
   ========================================= */
@media (min-width: 768px) and (max-width: 1024px) {
  #app {
    width: 95% !important;
    max-width: 95% !important;
    padding: 28px;
  }
  .image-slot {
    height: 280px;
  }
  #app h2 {
    font-size: 3.5rem;
    margin: 0 0 20px;
  }
  h1 {
    font-size: 2.2rem;
    margin: 10px 0 12px 0;
  }
  h1 span {
    font-size: 1.3rem;
    margin-top: 6px;
  }
  .name-slot {
    margin: 12px 0;
  }
  .name-slot h3 {
    font-size: 2rem;
  }
  .controls {
    margin-top: 10px;
    gap: 12px;
  }
  button {
    padding: 14px 18px;
    font-size: 1.1rem;
  }
  .bottom-menu {
    padding: 14px;
    width: 95%;
    max-width: 95%;
  }
  .bottom-menu button {
    height: 48px;
    font-size: 0.6875rem;
  }
  .bottom-menu .buttons {
    gap: 6px;
  }
  #area-retos {
    min-height: 70px;
    margin: 5px 0;
  }
}

/* iPad Pro, Tablets grandes y pantallas muy grandes */
@media (min-width: 1024px) {
  #app {
    width: 95% !important;
    max-width: 95% !important;
    padding: 32px;
  }
  .image-slot {
    height: 320px;
  }
  #app h2 {
    font-size: 4rem;
    margin: 0 0 24px;
  }
  h1 {
    font-size: 2.4rem;
    margin: 12px 0 15px 0;
  }
  h1 span {
    font-size: 1.4rem;
    margin-top: 8px;
  }
  .name-slot {
    margin: 15px 0;
  }
  .name-slot h3 {
    font-size: 2.2rem;
  }
  .controls {
    margin-top: 12px;
    gap: 14px;
  }
  button {
    padding: 16px 20px;
    font-size: 1.2rem;
  }
  .bottom-menu {
    padding: 16px;
    width: 95%;
    max-width: 95%;
  }
  .bottom-menu button {
    height: 52px;
    font-size: 0.75rem;
  }
  .bottom-menu .buttons {
    gap: 7px;
  }
  #area-retos {
    min-height: 80px;
    margin: 6px 0;
  }
}

/* =========================================
   8. RESPONSIVE - MÓVILES GRANDES
   ========================================= */
@media (max-width: 600px) and (min-width: 376px) {
  #app {
    padding: 14px;
    width: 95% !important;
    max-width: 95% !important;
  }
  .image-slot {
    height: 180px;
  }
  #app h2 {
    font-size: 2.5rem;
    margin: 0 0 12px;
  }
  h1 {
    font-size: 1.5rem;
    margin: 8px 0 10px 0;
  }
  h1 span {
    font-size: 1rem;
    margin-top: 6px;
  }
  .name-slot {
    margin: 8px 0;
  }
  .name-slot h3 {
    font-size: 1.5rem;
  }
  .controls {
    margin-top: 6px;
    gap: 8px;
  }
  .bottom-menu {
    padding: 9px;
  }
  .bottom-menu button {
    height: 38px;
    font-size: 0.5625rem;
  }
  #area-retos {
    min-height: 60px;
  }
}

/* =========================================
   9. RESPONSIVE - MÓVILES MEDIANOS (iPhone estándar)
   ========================================= */
@media (max-width: 414px) and (min-width: 376px) {
  #app {
    padding: 12px;
    width: 95% !important;
    max-width: 95% !important;
  }
  .image-slot {
    height: 160px;
  }
  #app h2 {
    font-size: 2.3rem;
    margin: 0 0 10px;
  }
  h1 {
    font-size: 1.4rem;
    margin: 6px 0 8px 0;
  }
  .name-slot {
    margin: 6px 0;
  }
  .name-slot h3 {
    font-size: 1.4rem;
  }
  .controls {
    margin-top: 5px;
    gap: 8px;
  }
  button {
    padding: 9px 10px;
    font-size: 0.85rem;
  }
}

/* =========================================
   10. RESPONSIVE - MÓVILES PEQUEÑOS (iPhone SE, etc.)
   ========================================= */
@media (max-width: 375px) {
  #app {
    padding: 10px;
    width: 95% !important;
    max-width: 95% !important;
  }
  .image-slot {
    height: 140px;
  }
  #app h2 {
    font-size: 2rem;
    margin: 0 0 8px;
  }
  h1 {
    font-size: 1.2rem;
    margin: 5px 0 6px 0;
  }
  h1 span {
    font-size: 0.9rem;
    margin-top: 4px;
  }
  .name-slot {
    min-height: 2rem;
    margin: 6px 0;
  }
  .name-slot h3 {
    font-size: 1.2rem;
  }
  .controls {
    gap: 6px;
    margin-top: 5px;
  }
  .row {
    gap: 6px;
  }
  button {
    padding: 8px 8px;
    font-size: 0.8rem;
  }
  .bottom-menu {
    padding: 8px;
    width: calc(100% - 16px);
  }
  .bottom-menu button {
    height: 36px;
    font-size: 0.5rem;
  }
  .bottom-menu .buttons {
    gap: 3px;
  }
  #area-retos {
    min-height: 50px;
  }
}

/* =========================================
   11. RESPONSIVE - PANTALLAS MUY PEQUEÑAS
   ========================================= */
@media (max-width: 320px) {
  #app {
    padding: 8px;
    width: 95% !important;
    max-width: 95% !important;
  }
  .image-slot {
    height: 120px;
  }
  #app h2 {
    font-size: 1.8rem;
    margin: 0 0 6px;
  }
  h1 {
    font-size: 1.1rem;
    margin: 4px 0 5px 0;
  }
  h1 span {
    font-size: 0.85rem;
  }
  .name-slot {
    margin: 5px 0;
  }
  .name-slot h3 {
    font-size: 1.1rem;
  }
  .controls {
    margin-top: 4px;
    gap: 5px;
  }
  button {
    padding: 7px;
    font-size: 0.75rem;
  }
  .bottom-menu button {
    height: 32px;
    font-size: 0.4375rem;
  }
  .bottom-menu .buttons {
    gap: 2px;
  }
  #area-retos {
    min-height: 45px;
  }
}

/* =========================================
   12. ORIENTACIÓN HORIZONTAL (LANDSCAPE)
   ========================================= */
@media (orientation: landscape) and (max-height: 600px) {
  body {
    overflow-y: auto;
  }
  
  #root {
    padding: 5px env(safe-area-inset-right, 10px) 5px env(safe-area-inset-left, 10px);
    justify-content: flex-start;
    min-height: auto;
  }
  
  h1 {
    font-size: 1.2rem;
    margin: 3px 0 5px 0;
  }
  
  h1 span {
    font-size: 0.9rem;
    margin-top: 2px;
  }
  
  #app {
    padding: 10px !important;
    margin: 3px auto 3px auto !important;
    max-width: 400px !important;
  }
  
  #app h2 {
    font-size: 2rem;
    margin: 0 0 8px;
  }
  
  .image-slot {
    height: 120px !important;
  }
  
  .name-slot {
    margin: 5px 0;
    min-height: 1.8rem;
  }
  
  .name-slot h3 {
    font-size: 1.1rem;
  }
  
  .controls {
    margin-top: 4px;
    gap: 6px;
  }
  
  button {
    padding: 6px 8px;
    font-size: 0.75rem;
  }
  
  #area-retos {
    min-height: 40px;
    margin: 2px 0;
  }
  
  .bottom-menu {
    padding: 6px;
    margin: 2px auto;
  }
  
  .bottom-menu button {
    height: 32px;
    font-size: 0.4375rem;
  }
  
  .bottom-menu .buttons {
    gap: 3px;
  }
}

/* Landscape para tablets/iPad */
@media (orientation: landscape) and (min-width: 768px) and (max-height: 600px) {
  #app {
    width: 95% !important;
    max-width: 95% !important;
    padding: 20px !important;
  }
  
  .image-slot {
    height: 180px !important;
  }
  
  #app h2 {
    font-size: 3rem;
    margin: 0 0 12px;
  }
  
  h1 {
    font-size: 1.8rem;
    margin: 6px 0 10px 0;
  }
  
  h1 span {
    font-size: 1.1rem;
  }
  
  .name-slot {
    margin: 8px 0;
  }
  
  .name-slot h3 {
    font-size: 1.8rem;
  }
  
  .controls {
    margin-top: 8px;
    gap: 10px;
  }
  
  button {
    padding: 12px 14px;
    font-size: 1rem;
  }
  
  .bottom-menu {
    padding: 12px;
    max-width: 500px;
  }
  
  .bottom-menu button {
    height: 42px;
    font-size: 0.625rem;
  }
  
  .bottom-menu .buttons {
    gap: 5px;
  }
  
  #area-retos {
    min-height: 60px;
    margin: 4px 0;
  }
}

/* =========================================
   13. ESPACIO DE RETOS (RESERVADO)
   ========================================= */
#area-retos {
  width: 100%;
  height: 45px;           /* Altura fija que NUNCA cambia */
  min-height: 45px; 
  max-height: 45px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0 auto;         /* Pegado al cuadro blanco y a la barra negra */
  padding: 0;
  overflow: hidden;       /* Evita que cualquier contenido mueva el resto */
  background: transparent;
  flex-shrink: 0;         /* Impide que el flexbox lo aplaste */
}
/* El placeholder es invisible pero mantiene el espacio del área de retos */
.btn-reto-placeholder {
  width: 90%;
  height: 45px;           /* Misma altura que el contenedor */
  min-height: 45px;
  display: block;         /* Mantiene el espacio */
  border-radius: 8px;
  border: none;
  visibility: hidden;     /* Invisible pero mantiene el espacio */
  opacity: 0;             /* Invisible pero mantiene el espacio */
  background: transparent;
  pointer-events: none;   /* No interfiere con clics */
}

/* El botón activo */
.btn-reto-active {
  width: 90%;            /* Mismo ancho que el bloque de números de abajo */
  max-width: 420px;      /* Mismo max-width que .bottom-menu */
  height: 38px;          /* Un poco menor que el contenedor para dejar aire */
  background: #4169E1;
  color: white;
  border: none;
  border-radius: 8px;
  font-weight: bold;
  cursor: pointer;
  animation: pulse-blue 2s infinite; /* El brillo que mencionamos */
  /* Centrar el texto siempre */
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  margin: 0 auto;        /* Centrado horizontal */
  box-sizing: border-box;
}

/* Animación de brillo para que resalte en tus videos de TikTok */
@keyframes pulse-blue {
  0% { box-shadow: 0 0 0 0 rgba(65, 105, 225, 0.7); }
  70% { box-shadow: 0 0 0 10px rgba(65, 105, 225, 0); }
  100% { box-shadow: 0 0 0 0 rgba(65, 105, 225, 0); }
}

/* Estilos para resaltado mnemotécnico */
.mnemo-active { 
    color: #00FF00 !important; 
    font-weight: bold; 
    text-shadow: 0 0 5px #00FF00;
}

.word-display { 
    font-size: 2.5rem; 
    letter-spacing: 2px; 
}

/* Sensor: Pantalla corta (iPhone SE 2/3, Huawei antiguos, alto < 670px) */
@media (max-height: 670px) {
  h1 { font-size: 1.1rem !important; margin: 4px 0 !important; }
  h1 span { display: none; } /* Oculta el subtítulo para ganar espacio */
  .image-slot { max-height: 160px !important; }
  #app img { max-height: 150px !important; }
  .btn-reto-active { height: 35px !important; margin-top: 5px !important; }
}

/* Evitar flash blanco en modo oscuro (Android/Google Play) */
@media (prefers-color-scheme: dark) {
  /* Ajustes mínimos: el fondo ya usa gradiente; theme-color en HTML evita destello */
}

/* =========================================
   SURFACE / TABLET PC - Proporción 3:2 y teclado físico
   ========================================= */
@media (min-width: 768px) and (orientation: landscape) {
  #app,
  .card-container {
    max-width: 600px;
    margin: 0 auto;
  }
  .image-slot {
    max-height: 40dvh;
  }
}

/* Elimina "sticky hover" en pantallas táctiles (Surface, tabletas) */
@media (hover: hover) {
  button:hover {
    filter: brightness(1.1);
  }
}
@media (hover: none) {
  button:hover {
    background: inherit;
  }
}