* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  background: #1c1c1c;
  /* 100% d'abord (repli pour les navigateurs sans dvh), puis 100dvh : suit la hauteur
     RÉELLEMENT visible de Safari iPad (barre d'outils dynamique), contrairement à 100%/
     100vh qui restent figés sur la plus grande hauteur possible même quand la barre est
     affichée — cause du blocage où les flèches se retrouvaient hors écran après la
     fermeture du panneau (2026-08-26). */
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  font-family: system-ui, -apple-system, sans-serif;
}

#game-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  height: 100dvh;
  /* env(safe-area-inset-*) exige viewport-fit=cover (index.html) pour renvoyer une
     valeur non nulle sur Safari iPad ; max() garde au moins le padding d'origine
     sur les appareils sans zone sécurisée. */
  padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  gap: 12px;
}

#canvas-wrap {
  position: relative;
  width: 100%;
  max-width: 960px;
  /* Grandit pour occuper l'espace vertical restant une fois #touch-controls déduit,
     mais min-height:0 lui permet aussi de RÉTRÉCIR sous sa taille intrinsèque — sans
     ça, un flex-item ne rétrécit jamais sous le contenu qu'il contient. C'est ce qui
     permet au canvas de se réduire en paysage plutôt que de pousser les commandes
     hors de l'écran. */
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

#game-canvas {
  /* max-width/max-height (plutôt que width:100%) laissent le navigateur choisir la
     dimension la plus contraignante avec aspect-ratio : le canvas rétrécit en largeur
     ET en hauteur selon l'espace réellement disponible, jamais l'inverse. */
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: 960 / 540;
  background: #7ec850;
  border: 2px solid #333;
  border-radius: 4px;
  touch-action: none;
  display: block;
}

#interact-prompt {
  position: absolute;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.75);
  color: #fff;
  padding: 8px 14px;
  border-radius: 6px;
  font-size: 14px;
  max-width: 90%;
  text-align: center;
  white-space: nowrap;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  /* La visibilité ne bascule qu'à la fin du fondu (délai = durée du fondu) pour un
     retrait doux ; à l'apparition, elle bascule sans délai. */
  transition: opacity 0.15s ease, visibility 0s linear 0.15s;
}

#interact-prompt.visible {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transition: opacity 0.15s ease, visibility 0s linear 0s;
}

#touch-controls {
  display: none;
  /* Rangée horizontale : pavé directionnel à gauche, bouton E à droite, comme le
     bouton d'action d'une manette — plus jamais empilé au-dessus des flèches. */
  flex-direction: row;
  align-items: center;
  gap: 20px;
  flex-shrink: 0;
  user-select: none;
}

#dpad {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

#touch-row {
  display: flex;
  gap: 8px;
}

.touch-btn {
  width: 56px;
  height: 56px;
  border-radius: 8px;
  border: none;
  background: rgba(255, 255, 255, 0.85);
  color: #222;
  font-size: 22px;
  touch-action: none;
}

.touch-btn:active {
  background: rgba(255, 255, 255, 1);
}

#btn-interact {
  /* Emplacement réservé en PERMANENCE dans le flux de #touch-controls (jamais
     display:none, jamais retiré/inséré) — sa taille (56x56px, comme .touch-btn) est
     toujours comptée dans la largeur de la rangée, donc #dpad ne bouge jamais d'un
     pixel quand E apparaît ou disparaît. Seuls opacity/visibility/pointer-events
     basculent, pour un simple fondu. */
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: rgba(70, 180, 110, 0.92);
  color: #fff;
  font-weight: bold;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.15s ease, visibility 0s linear 0.15s;
}

#btn-interact.visible {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transition: opacity 0.15s ease, visibility 0s linear 0s;
}

@media (pointer: coarse) {
  #touch-controls {
    display: flex;
  }
}

#interaction-panel {
  display: none;
  /* Défense en profondeur : display:none retire déjà l'élément de la détection
     tactile, mais un pointer-events explicite évite qu'une future modification de
     ce fichier ne réintroduise silencieusement une couche qui bloque les boutons
     (leçon du bogue [hidden]/display:flex corrigé dans multivers/ le 2026-08-25). */
  pointer-events: none;
  position: fixed;
  inset: 0;
  z-index: 10;
  align-items: center;
  justify-content: center;
}

#interaction-panel.visible {
  display: flex;
  pointer-events: auto;
}

#panel-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
}

#panel-box {
  position: relative;
  background: #fff;
  color: #222;
  border-radius: 8px;
  padding: 20px 24px;
  width: 360px;
  max-width: 90vw;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

#panel-box h2 {
  margin: 0 0 8px;
  font-size: 20px;
}

#panel-box p {
  margin: 0 0 16px;
  line-height: 1.4;
}

#panel-close {
  padding: 8px 16px;
  border: none;
  border-radius: 6px;
  background: #2255cc;
  color: #fff;
  font-size: 14px;
}
