/* ------------------------------------------------------------------ */
/* Reglages generaux                                                   */
/* ------------------------------------------------------------------ */

:root {
  --fond: #0b132b;
  --fond-clair: #1c2541;
  --fond-carte: #16203d;
  --trait: #2f3d63;
  --texte: #f2f4ff;
  --texte-doux: #9fb0d8;
  --accent: #f72585;
  --accent-doux: #ff7ab3;
  --bleu: #5bc0be;
  --rayon: 18px;

  /* Marges de securite iOS : encoche en haut, barre gestuelle en bas. */
  --haut: env(safe-area-inset-top, 0px);
  --bas: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--fond);
  color: var(--texte);
  font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', Helvetica, Arial, sans-serif;
  font-size: 17px;
  line-height: 1.45;
}

body {
  /* Un jeu ne doit ni rebondir, ni selectionner du texte, ni zoomer au double appui. */
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  text-size-adjust: 100%;
  -webkit-text-size-adjust: 100%;
}

img { display: block; max-width: 100%; }

/* ------------------------------------------------------------------ */
/* Cadre du jeu                                                        */
/* ------------------------------------------------------------------ */

.jeu {
  position: relative;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.ecran {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: calc(var(--haut) + 20px) 18px calc(var(--bas) + 18px);
  min-height: 100vh;
  min-height: 100dvh;
}

/* Bloc d'actions : toujours dans la moitie basse, a portee du pouce. */
.zone-pouce {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.chargement {
  margin: auto;
  padding: 40px;
  text-align: center;
  color: var(--texte-doux);
}

/* ------------------------------------------------------------------ */
/* Boutons et champs                                                   */
/* ------------------------------------------------------------------ */

.bouton {
  min-height: 64px;
  width: 100%;
  padding: 16px 22px;
  border: none;
  border-radius: var(--rayon);
  font-family: inherit;
  font-size: 19px;
  font-weight: 700;
  color: #ffffff;
  background: var(--accent);
  box-shadow: 0 6px 0 #b4176a;
  transition: transform 0.06s ease, box-shadow 0.06s ease;
  cursor: pointer;
}

.bouton:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 #b4176a;
}

.bouton--discret {
  min-height: 56px;
  font-size: 17px;
  font-weight: 600;
  color: var(--texte-doux);
  background: transparent;
  border: 2px solid var(--trait);
  box-shadow: none;
}

.bouton--discret:active {
  transform: none;
  background: var(--fond-clair);
}

.bouton:disabled {
  opacity: 0.45;
  box-shadow: none;
}

.label {
  display: block;
  font-size: 15px;
  font-weight: 600;
  color: var(--texte-doux);
}

.champ {
  width: 100%;
  min-height: 60px;
  padding: 16px;
  border: 2px solid var(--trait);
  border-radius: var(--rayon);
  background: var(--fond-clair);
  color: var(--texte);
  font-family: inherit;
  /* 17px minimum : en dessous, Safari iOS zoome tout seul a la saisie. */
  font-size: 17px;
  -webkit-user-select: text;
  user-select: text;
}

.champ:focus { outline: 2px solid var(--bleu); border-color: var(--bleu); }
.champ--erreur { border-color: var(--accent); }

.champ--code {
  font-family: ui-monospace, Menlo, monospace;
  font-size: 15px;
  word-break: break-all;
  resize: none;
}

.erreur {
  min-height: 20px;
  margin: 0;
  font-size: 15px;
  color: var(--accent-doux);
}

/* ------------------------------------------------------------------ */
/* Entetes                                                             */
/* ------------------------------------------------------------------ */

.entete { flex: 0 0 auto; }

.entete__titre {
  margin: 0;
  font-size: 30px;
  line-height: 1.15;
  letter-spacing: -0.5px;
}

.entete__sous-titre {
  margin: 6px 0 0;
  color: var(--texte-doux);
  font-size: 16px;
}

/* ------------------------------------------------------------------ */
/* Menu des niveaux                                                    */
/* ------------------------------------------------------------------ */

.cartes {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.carte {
  display: grid;
  grid-template-columns: 76px 1fr auto;
  align-items: center;
  gap: 14px;
  min-height: 96px;
  padding: 12px 14px;
  border: 2px solid var(--trait);
  border-radius: var(--rayon);
  background: var(--fond-carte);
  color: inherit;
  font-family: inherit;
  text-align: left;
  cursor: pointer;
}

.carte:active { background: var(--fond-clair); }

.carte--verrouillee {
  opacity: 0.45;
  filter: grayscale(1);
  cursor: default;
}

.carte--terminee { border-color: var(--bleu); }

.carte__vignette {
  width: 76px;
  height: 58px;
  object-fit: cover;
  border-radius: 12px;
}

.carte__texte { min-width: 0; }

.carte__rang {
  font-size: 13px;
  font-weight: 700;
  color: var(--texte-doux);
  letter-spacing: 2px;
}

.carte__titre {
  margin: 2px 0 0;
  font-size: 19px;
  line-height: 1.2;
}

.carte__resume {
  margin: 3px 0 0;
  font-size: 14px;
  color: var(--texte-doux);
}

.carte__etat {
  margin: 4px 0 0;
  font-size: 13px;
  font-weight: 700;
  color: var(--bleu);
}

.carte--verrouillee .carte__etat { color: var(--texte-doux); }

.carte__action {
  font-size: 15px;
  font-weight: 700;
  color: var(--accent-doux);
}

.pied { padding-top: 6px; }

/* ------------------------------------------------------------------ */
/* Intro                                                               */
/* ------------------------------------------------------------------ */

.ecran--intro { gap: 14px; }

/* Element retire du flux : sert a enchainer les trois temps de l'intro. */
.est-cache { display: none !important; }

/* Scene animee : chaque sprite est un element separe, remplacable seul. */
.scene {
  position: relative;
  flex: 0 0 auto;
  height: 150px;
  border-radius: var(--rayon);
  overflow: hidden;
  background: var(--fond-clair) center / cover no-repeat;
}

.sprite {
  position: absolute;
  width: 48px;
  height: 48px;
  /* Les images pixel art ne doivent pas etre lissees par le navigateur. */
  image-rendering: pixelated;
}

.sprite--avion {
  top: 16px;
  left: 0;
  animation: traverse 7s linear infinite;
}

.sprite--coeur {
  top: 50px;
  right: 24px;
  animation: battre 1.1s ease-in-out infinite;
}

.sprite--danseur {
  bottom: 10px;
  left: 26px;
  animation: danser 0.7s ease-in-out infinite alternate;
}

@keyframes traverse {
  from { transform: translateX(-60px); }
  to { transform: translateX(calc(100vw + 20px)); }
}

@keyframes battre {
  0%, 100% { transform: scale(1); }
  30% { transform: scale(1.25); }
  45% { transform: scale(1.05); }
}

@keyframes danser {
  from { transform: translateY(0) rotate(-8deg); }
  to { transform: translateY(-10px) rotate(8deg); }
}

/* Texte d'introduction qui remonte, incline en perspective. */
.recit {
  position: relative;
  flex: 1 1 auto;
  min-height: 200px;
  overflow: hidden;
  perspective: 260px;
  perspective-origin: 50% 0%;
  /* Fondu vers le haut, comme un generique de film. */
  -webkit-mask-image: linear-gradient(to bottom, transparent 0%, #000 30%, #000 100%);
  mask-image: linear-gradient(to bottom, transparent 0%, #000 30%, #000 100%);
}

.recit__perspective {
  position: absolute;
  inset: 0;
  transform: rotateX(42deg);
  transform-origin: 50% 100%;
}

.recit__piste {
  will-change: transform;
  text-align: center;
}

.recit__ligne {
  margin: 0 0 4px;
  font-size: 19px;
  font-weight: 700;
  color: #ffd166;
}

.intro__passer { flex: 0 0 auto; }

.intro__formulaire { gap: 8px; }

/* Zone de remplacement, visible tant qu'un element n'est pas construit. */
.zone-a-venir {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  min-height: 120px;
  padding: 16px;
  border: 2px dashed var(--trait);
  border-radius: var(--rayon);
  color: var(--texte-doux);
  font-family: ui-monospace, Menlo, monospace;
  font-size: 13px;
  letter-spacing: 1px;
  text-align: center;
}

.zone-a-venir--grande { min-height: 180px; }

.chantier__message {
  margin: 0;
  color: var(--texte-doux);
  font-size: 15px;
}

/* ------------------------------------------------------------------ */
/* Niveau 2 : memoire et mot mystere                                   */
/* ------------------------------------------------------------------ */

.ecran--memo { gap: 12px; }

.entete--compacte {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.entete__titre--petit { font-size: 22px; }

.memo__compteur {
  margin: 0;
  font-size: 14px;
  color: var(--texte-doux);
}

.memo__consigne {
  margin: 0;
  text-align: center;
  font-size: 14px;
  color: var(--texte-doux);
}

/* Le mot a reconstituer, une case par caractere. */
.mot {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
}

.mot__case {
  width: 34px;
  height: 46px;
  padding: 0;
  border: 2px solid var(--trait);
  border-radius: 10px;
  background: var(--fond-carte);
  color: var(--texte);
  font-family: inherit;
  font-size: 21px;
  font-weight: 800;
}

/* Case donnee d'office (apostrophe, tiret, espace) : jamais cliquable. */
.mot__case--fixe {
  width: 18px;
  border-color: transparent;
  background: transparent;
  color: var(--texte-doux);
}

.mot__case--remplie { border-color: var(--bleu); }

.mot--faux { animation: trembler 0.5s ease; }

@keyframes trembler {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-9px); }
  40% { transform: translateX(9px); }
  60% { transform: translateX(-5px); }
  80% { transform: translateX(5px); }
}

/* La grille de cartes. */
.memo__grille {
  display: grid;
  gap: 8px;
}

.memo__carte {
  position: relative;
  aspect-ratio: 1;
  padding: 0;
  border: none;
  border-radius: 12px;
  background: transparent;
  transform-style: preserve-3d;
  -webkit-transform-style: preserve-3d;
  transition: transform 0.35s ease;
}

.memo__carte.est-retournee { transform: rotateY(180deg); }

.memo__carte.est-trouvee { box-shadow: 0 0 0 2px var(--bleu); }

.memo__face {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  overflow: hidden;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

.memo__face img { width: 100%; height: 100%; object-fit: cover; }

.memo__face--avant { transform: rotateY(180deg); }

/* La lettre revelee par une paire trouvee, posee sur le paysage. */
.memo__lettre {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(11, 19, 43, 0.5);
  color: #ffffff;
  font-size: 30px;
  font-weight: 800;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.8);
}

/* La reserve de lettres debloquees. */
.reserve {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  min-height: 64px;
}

.reserve__lettre {
  width: 64px;
  height: 64px;
  padding: 0;
  border: 2px solid var(--accent);
  border-radius: 12px;
  background: var(--fond-carte);
  color: #ffffff;
  font-family: inherit;
  font-size: 24px;
  font-weight: 800;
}

.reserve__lettre:disabled {
  border-color: var(--trait);
  color: var(--texte-doux);
  opacity: 0.6;
}

/* ------------------------------------------------------------------ */
/* Niveau 3 : la course                                                */
/* ------------------------------------------------------------------ */

.ecran--course { gap: 10px; }

.course__entete {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.jauge {
  height: 12px;
  border: 1px solid var(--trait);
  border-radius: 6px;
  background: var(--fond-carte);
  overflow: hidden;
}

.jauge__remplissage {
  width: 0;
  height: 100%;
  background: linear-gradient(90deg, var(--bleu), var(--accent));
  transition: width 0.12s linear;
}

.course__mesures {
  display: flex;
  justify-content: space-between;
  font-size: 15px;
  font-weight: 700;
}

.course__chrono {
  font-family: ui-monospace, Menlo, monospace;
  color: var(--bleu);
}

.course__record {
  margin: 0;
  font-size: 13px;
  color: var(--texte-doux);
}

/* La scene : trois couches de decor qui defilent a des vitesses differentes. */
.course__scene {
  position: relative;
  flex: 0 0 auto;
  height: 240px;
  border-radius: var(--rayon);
  background: var(--fond-clair);
  overflow: hidden;
}

.couche {
  position: absolute;
  left: 0;
  right: 0;
  background-repeat: repeat-x;
  background-size: auto 100%;
  image-rendering: pixelated;
}

.couche--loin { top: 0; height: 62%; opacity: 0.55; }
.couche--milieu { top: 18%; height: 62%; opacity: 0.85; }
.couche--sol { bottom: 0; height: 26%; }

/* Le coureur : un corps anime et une tete posee par-dessus, images separees. */
.coureur {
  position: absolute;
  left: 15%;
  bottom: 16%;
  width: 74px;
  transition: transform 0.08s ease;
}

.coureur--rebond { transform: translateY(-5px); }

.coureur__corps {
  display: block;
  width: 100%;
  image-rendering: pixelated;
}

.coureur__tete {
  position: absolute;
  left: 50%;
  bottom: 100%;
  width: 52px;
  transform: translate(-50%, 9px);
  image-rendering: pixelated;
}

.banderole {
  position: absolute;
  right: -6%;
  bottom: 22%;
  width: 46%;
  image-rendering: pixelated;
  transition: transform 0.12s linear;
}

/* Le gros bouton rond, seul geste du niveau. */
.course__commande { align-items: center; }

.bouton-rond {
  width: 150px;
  height: 150px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: radial-gradient(circle at 50% 30%, var(--accent-doux), var(--accent) 65%);
  color: #ffffff;
  font-family: inherit;
  font-size: 24px;
  font-weight: 800;
  letter-spacing: 1px;
  box-shadow: 0 8px 0 #b4176a;
  transition: transform 0.05s ease, box-shadow 0.05s ease;
}

.bouton-rond:active {
  transform: translateY(6px);
  box-shadow: 0 2px 0 #b4176a;
}

.bouton-rond:disabled { opacity: 0.5; box-shadow: none; }

.panneau--echec { text-align: center; }

/* ------------------------------------------------------------------ */
/* Niveau 4 : mots fleches                                             */
/* ------------------------------------------------------------------ */

.ecran--mf { gap: 10px; }

.mf__indices {
  font-size: 14px;
  color: var(--texte-doux);
}

.mf__bas { gap: 8px; }

.grille {
  display: grid;
  gap: 2px;
  width: 100%;
}

.case {
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1;
  padding: 0;
  border: 1px solid var(--trait);
  border-radius: 4px;
  background: var(--fond-carte);
  color: var(--texte);
  font-family: inherit;
  font-size: 20px;
  font-weight: 800;
}

.case--noire {
  background: #070c1c;
  border-color: #070c1c;
}

/* Case definition : texte minuscule dans la case, texte en grand sous la grille. */
.case--definition {
  position: relative;
  padding: 2px;
  background: var(--fond-clair);
  color: var(--texte-doux);
  font-size: 7.5px;
  font-weight: 600;
  line-height: 1.05;
  text-align: center;
  overflow: hidden;
}

.case__fleche {
  position: absolute;
  font-size: 9px;
  color: var(--accent-doux);
}

.case__fleche--droite {
  top: 50%;
  right: 1px;
  transform: translateY(-50%);
}

.case__fleche--bas {
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
}

.case--mot { background: #223163; }

.case--courante {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 2px var(--accent);
}

.case--fausse {
  background: var(--accent);
  color: #ffffff;
  animation: clignoter 0.5s steps(1) infinite;
}

@keyframes clignoter {
  0%, 49% { opacity: 1; }
  50%, 100% { opacity: 0.25; }
}

/* La definition du mot choisi, juste au-dessus du clavier. */
.definition {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 46px;
  margin: 0;
  padding: 0 4px;
  font-size: 16px;
  font-weight: 700;
  text-align: center;
}

/* Clavier AZERTY maison : il remplace le clavier iOS. */
.clavier {
  display: flex;
  flex-direction: column;
  gap: 5px;
  /* Il deborde un peu des marges de l'ecran pour offrir de plus grosses touches. */
  margin: 0 -10px;
}

.clavier__rangee {
  display: flex;
  gap: 4px;
  justify-content: center;
}

.touche {
  flex: 1 1 0;
  min-width: 0;
  height: 52px;
  padding: 0;
  border: none;
  border-radius: 8px;
  background: var(--fond-clair);
  color: var(--texte);
  font-family: inherit;
  font-size: 18px;
  font-weight: 700;
}

.touche:active { background: var(--accent); }

.touche--large {
  flex: 2.4 1 0;
  background: var(--trait);
  font-size: 13px;
}

/* ------------------------------------------------------------------ */
/* Victoire et fin                                                     */
/* ------------------------------------------------------------------ */

.ecran--victoire { justify-content: center; text-align: center; }

.victoire__haut {
  margin: auto 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.victoire__titre { margin: 0; font-size: 34px; }

.victoire__message { margin: 0; font-size: 18px; color: var(--texte-doux); }

.victoire__detail {
  margin: 0;
  font-family: ui-monospace, Menlo, monospace;
  font-size: 16px;
  color: var(--bleu);
}

.confettis {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 5;
}

/* ------------------------------------------------------------------ */
/* Panneau du code de sauvegarde                                       */
/* ------------------------------------------------------------------ */

.voile {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: flex-end;
  background: rgba(5, 9, 22, 0.75);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
}

.panneau {
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 22px 18px calc(var(--bas) + 22px);
  border-radius: 24px 24px 0 0;
  background: var(--fond-clair);
}

.panneau__titre { margin: 0; font-size: 22px; }
.panneau__texte { margin: 0; font-size: 15px; color: var(--texte-doux); }
.panneau__separateur { width: 100%; border: none; border-top: 1px solid var(--trait); margin: 6px 0; }

.panneau__message {
  min-height: 20px;
  margin: 0;
  font-size: 15px;
  color: var(--bleu);
  text-align: center;
}

.panneau__message--erreur { color: var(--accent-doux); }

/* ------------------------------------------------------------------ */
/* Mode debug                                                          */
/* ------------------------------------------------------------------ */

.mode-debug .jeu::before {
  content: 'DEBUG';
  position: fixed;
  top: calc(var(--haut) + 4px);
  right: 8px;
  z-index: 30;
  padding: 2px 8px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 1px;
}

/* ------------------------------------------------------------------ */
/* Accessibilite                                                       */
/* ------------------------------------------------------------------ */

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
