/* ═══════════════════════════════════════════════════════
   MathéMaster – CSS
   Cible : iPhone 13 (390×844 logical px, Safari / PWA)
   Principe : app-shell occupe exactement 100dvh,
   aucun scroll, le clavier est toujours visible.
═══════════════════════════════════════════════════════ */

/* ── Reset ─────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:       #0f172a;
  --card:     #1e293b;
  --card2:    #273348;
  --accent:   #6366f1;
  --accent-h: #818cf8;
  --green:    #22c55e;
  --red:      #f87171;
  --yellow:   #fbbf24;
  --orange:   #fb923c;
  --pink:     #f472b6;
  --text:     #f1f5f9;
  --muted:    #94a3b8;
  --r:        .9rem;

  /* Hauteurs fixes des zones permanentes */
  --h-header:  52px;
  --h-daily:   28px;
  /* Le clavier + footer occupent une hauteur connue */
  --h-numpad:  260px;  /* 4 rangées × ~56px + gaps + footer */
}

/* ── Base body ──────────────────────────────────────────── */
html, body {
  height: 100%;
  overflow: hidden;           /* pas de scroll sur le body */
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

/* ── Confettis ──────────────────────────────────────────── */
#confetti-canvas {
  position: fixed; inset: 0;
  pointer-events: none; z-index: 9999;
}

/* ═══════════════════════════════════════════════════════
   APP-SHELL : grille verticale stricte
═══════════════════════════════════════════════════════ */
.app-shell {
  /* dvh = dynamic viewport height (exclut la barre Safari) */
  height: 100dvh;
  display: grid;
  grid-template-rows:
    var(--h-header)   /* header */
    var(--h-daily)    /* barre quotidienne */
    1fr;              /* main = tout le reste */
  background: var(--bg);
  /* safe-area pour notch iPhone */
  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);
}

/* ── Header ─────────────────────────────────────────────── */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
  padding: 0 .9rem;
  background: var(--card);
  border-bottom: 1px solid rgba(255,255,255,0.06);
  /* hauteur imposée par la grille */
}

.nav-back {
  color: var(--accent-h);
  text-decoration: none;
  font-size: .78rem;
  font-weight: 700;
  white-space: nowrap;
  flex-shrink: 0;
}

.header-center {
  flex: 1;
  text-align: center;
  overflow: hidden;
}

.logo-text {
  font-size: .9rem;
  white-space: nowrap;
}
.logo-text strong { font-weight: 900; }
.logo-text em     { color: var(--accent-h); font-style: normal; font-size: .8rem; }

.header-right {
  display: flex;
  gap: .35rem;
  flex-shrink: 0;
}

.badge {
  background: var(--card2);
  border-radius: 2rem;
  padding: .2rem .6rem;
  font-size: .75rem;
  font-weight: 700;
  white-space: nowrap;
}

/* ── Barre quotidienne ──────────────────────────────────── */
.daily-strip {
  display: flex;
  align-items: center;
  gap: .6rem;
  padding: 0 .9rem;
  background: var(--bg);
  border-bottom: 1px solid rgba(255,255,255,0.04);
}

.daily-track {
  flex: 1;
  height: 6px;
  background: var(--card2);
  border-radius: 2rem;
  overflow: hidden;
}

.daily-fill {
  height: 100%; width: 0%;
  background: linear-gradient(90deg, var(--green), var(--accent));
  border-radius: 2rem;
  transition: width .6s ease, background .4s;
}
.daily-fill.warn   { background: linear-gradient(90deg, var(--yellow), var(--orange)); }
.daily-fill.danger { background: linear-gradient(90deg, var(--red), #dc2626); }

.daily-label {
  font-size: .7rem;
  color: var(--muted);
  white-space: nowrap;
}

/* ── Main (zone flexible) ───────────────────────────────── */
.app-main {
  overflow: hidden;   /* pas de scroll ici non plus */
  position: relative;
}

/* ── Écrans ─────────────────────────────────────────────── */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* ─────────────────────────────────────────────────────────
   ÉCRAN ACCUEIL
───────────────────────────────────────────────────────── */
.screen-welcome {
  justify-content: center;
  gap: 1.1rem;
  padding: 1.5rem;
  text-align: center;
  background: var(--bg);
}

.welcome-emoji {
  font-size: 3.5rem;
  animation: float 3s ease-in-out infinite;
}
@keyframes float {
  0%,100% { transform: translateY(0); }
  50%      { transform: translateY(-8px); }
}

.welcome-title { font-size: 1.8rem; font-weight: 900; }

.welcome-sub { color: var(--muted); font-size: .88rem; line-height: 1.7; }

.welcome-hint {
  display: inline-block;
  background: var(--card2);
  border-radius: 2rem;
  padding: .18rem .75rem;
  font-size: .78rem;
  color: var(--accent-h);
  margin-top: .3rem;
}

.btn-start {
  padding: .9rem 2.2rem;
  font-size: 1.05rem;
  font-weight: 800;
  background: linear-gradient(135deg, var(--accent), var(--pink));
  color: #fff;
  border: none;
  border-radius: 3rem;
  cursor: pointer;
  box-shadow: 0 4px 18px #6366f150;
  transition: transform .15s;
  text-align: center;
}
.btn-start:active { transform: scale(.97); }

/* ─────────────────────────────────────────────────────────
   ÉCRAN JEU
   Grille verticale : zone haute flexible + clavier fixe bas
───────────────────────────────────────────────────────── */
.screen-game {
  justify-content: space-between;
  background: var(--bg);
  padding: 0;
}

/* Zone haute : question, réponse, feedback, historique */
.game-top {
  flex: 1;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: .5rem;
  padding: .6rem 1rem .3rem;
  min-height: 0; /* important pour que flex ne déborde pas */
}

/* Question */
.question-block {
  width: 100%;
  text-align: center;
}

.question-text {
  font-size: clamp(2.2rem, 13vw, 3.4rem);
  font-weight: 900;
  line-height: 1;
  letter-spacing: -1px;
}

.question-text.pop {
  animation: popIn .25s cubic-bezier(.34,1.56,.64,1) both;
}
@keyframes popIn {
  0%   { transform: scale(.5) rotate(-4deg); opacity: 0; }
  100% { transform: scale(1) rotate(0deg);   opacity: 1; }
}

/* Affichage réponse */
.answer-display-wrap {
  width: 100%;
  text-align: center;
}

.answer-display {
  display: inline-block;
  min-width: 110px;
  padding: .4rem 1rem;
  font-size: 1.8rem;
  font-weight: 900;
  background: var(--card2);
  border: 2.5px solid var(--accent);
  border-radius: .7rem;
  color: var(--text);
  letter-spacing: 2px;
  transition: border-color .2s, box-shadow .2s;
}

.answer-display::after {
  content: '|';
  animation: blink 1s step-end infinite;
  margin-left: 2px;
  color: var(--accent-h);
}
.answer-display.no-cursor::after { display: none; }
@keyframes blink { 0%,100%{opacity:1} 50%{opacity:0} }

.answer-display.correct { border-color: var(--green); box-shadow: 0 0 0 3px #22c55e28; }
.answer-display.wrong   {
  border-color: var(--red); box-shadow: 0 0 0 3px #f8717128;
  animation: shake .3s ease;
}
@keyframes shake {
  0%,100%{transform:translateX(0)} 20%{transform:translateX(-7px)}
  40%{transform:translateX(7px)}   60%{transform:translateX(-4px)}
  80%{transform:translateX(4px)}
}

/* Feedback */
.feedback {
  font-size: .9rem;
  font-weight: 700;
  text-align: center;
  width: 100%;
  min-height: 1.3rem;
  line-height: 1.4;
  padding: 0 .5rem;
}
.feedback.correct { color: var(--green); }
.feedback.wrong   { color: var(--red); }

/* Historique + bouton suivant sur la même ligne */
.bottom-row {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: .75rem;
  flex-wrap: wrap;
  min-height: 2rem;
}

.history-row {
  font-size: 1.15rem;
  letter-spacing: .08rem;
}

.btn-next {
  padding: .45rem 1.2rem;
  font-size: .85rem;
  font-weight: 700;
  background: var(--card2);
  color: var(--text);
  border: 2px solid var(--accent);
  border-radius: 2rem;
  cursor: pointer;
  transition: background .15s, transform .1s;
  white-space: nowrap;
}
.btn-next:active { transform: scale(.95); background: var(--accent); }

/* ── Zone basse : clavier ───────────────────────────────── */
.game-bottom {
  width: 100%;
  background: var(--card);
  border-top: 1px solid rgba(255,255,255,0.06);
  padding: .5rem .75rem .4rem;
  display: flex;
  flex-direction: column;
  gap: .4rem;
  flex-shrink: 0; /* ne rétrécit jamais */
}

/* ═══════════════════════════════════════════════════════
   CLAVIER NUMÉRIQUE
═══════════════════════════════════════════════════════ */
.numpad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: .4rem;
  width: 100%;
}

.nk {
  position: relative;
  /* hauteur auto via aspect-ratio pour s'adapter à tous les iPhones */
  aspect-ratio: 2 / 1;
  max-height: 56px;
  font-size: 1.35rem;
  font-weight: 800;
  color: var(--text);
  background: var(--card2);
  border: 2px solid transparent;
  border-radius: var(--r);
  cursor: pointer;
  user-select: none;
  transition: transform .08s, background .1s;
  overflow: hidden;
  box-shadow: 0 3px 0 #0005, inset 0 1px 0 #ffffff0c;
  touch-action: manipulation; /* supprime le 300ms delay iOS */
}

/* Couleurs décalées par chiffre */
.nk:nth-child(1) { border-color: #6366f145; }
.nk:nth-child(2) { border-color: #22d3ee45; }
.nk:nth-child(3) { border-color: #f472b645; }
.nk:nth-child(4) { border-color: #fb923c45; }
.nk:nth-child(5) { border-color: #fbbf2445; }
.nk:nth-child(6) { border-color: #22c55e45; }
.nk:nth-child(7) { border-color: #818cf845; }
.nk:nth-child(8) { border-color: #38bdf845; }
.nk:nth-child(9) { border-color: #e879f945; }

.nk:active, .nk.pressed {
  transform: translateY(2px) scale(.95);
  box-shadow: 0 1px 0 #0005;
  background: #334155;
}

/* 0 occupe 2 colonnes */
.nk-wide { grid-column: span 2; }

/* Effacer */
.nk-del {
  background: #3b2a2a;
  border-color: var(--red) !important;
  font-size: 1rem;
}
.nk-del:active, .nk-del.pressed { background: #4d3333; }

/* OK — pleine largeur, un peu plus haut */
.nk-ok {
  grid-column: 1 / -1;
  aspect-ratio: auto;
  height: 48px;
  max-height: 48px;
  font-size: 1rem;
  font-weight: 900;
  letter-spacing: 1px;
  background: linear-gradient(135deg, var(--green), #16a34a);
  border-color: var(--green) !important;
  box-shadow: 0 3px 0 #14532d, 0 0 12px #22c55e30;
  color: #fff;
}
.nk-ok:active, .nk-ok.pressed {
  box-shadow: 0 1px 0 #14532d;
}

/* Ripple */
.nk .ripple {
  position: absolute; border-radius: 50%;
  background: #ffffff22;
  transform: scale(0);
  animation: ripple .35s linear;
  pointer-events: none;
}
@keyframes ripple { to { transform: scale(4); opacity: 0; } }

.numpad.disabled .nk { opacity: .3; pointer-events: none; }

/* ── Footer du jeu ──────────────────────────────────────── */
.game-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
  width: 100%;
}

.stat-text { font-size: .72rem; color: var(--muted); }

.btn-ghost {
  padding: .25rem .7rem;
  background: transparent;
  color: var(--muted);
  border: 1px solid #ffffff12;
  border-radius: 2rem;
  font-size: .72rem;
  cursor: pointer;
  transition: color .2s, border-color .2s;
  touch-action: manipulation;
}
.btn-ghost:active { color: var(--red); border-color: var(--red); }

/* ─────────────────────────────────────────────────────────
   ÉCRAN LIMITE
───────────────────────────────────────────────────────── */
.screen-limit {
  justify-content: center;
  gap: 1rem;
  padding: 2rem;
  text-align: center;
  background: var(--bg);
}
.limit-emoji  { font-size: 3rem; }
.limit-title  { font-size: 1.4rem; font-weight: 800; }
.limit-sub    { color: var(--muted); line-height: 1.7; font-size: .9rem; }
.final-score  { font-size: 1.1rem; font-weight: 700; color: var(--accent-h); }

/* ─────────────────────────────────────────────────────────
   ADAPTATIONS IPHONE (hauteurs très réduites)
   iPhone SE : 667px, iPhone 13 mini : 780px
───────────────────────────────────────────────────────── */
@media (max-height: 700px) {
  :root { --h-header: 46px; --h-daily: 24px; }
  .nk       { max-height: 46px; font-size: 1.15rem; }
  .nk-ok    { height: 42px; max-height: 42px; }
  .game-top { gap: .3rem; padding: .4rem .75rem .2rem; }
  .question-text { font-size: clamp(1.9rem, 11vw, 2.8rem); }
  .answer-display { font-size: 1.5rem; padding: .3rem .8rem; }
  .feedback { font-size: .82rem; }
}

@media (max-height: 600px) {
  .nk    { max-height: 40px; font-size: 1rem; }
  .nk-ok { height: 38px; max-height: 38px; }
}

/* ─────────────────────────────────────────────────────────
   LARGEUR : iPhone Pro Max (430px) et plus petits (375px)
───────────────────────────────────────────────────────── */
@media (max-width: 375px) {
  .nk { font-size: 1.15rem; }
  .game-bottom { padding: .4rem .6rem .3rem; }
}
