:root {
  --paper: #FBF6EC;
  --dot: #D5CAB6;
  --sky: #2BB3D9;
  --navy: #1F3FB5;
  --pink: #D6337A;
  --purple: #4B2A7B;
  --violet: #8E5BC8;
  --orange: #F47B20;
  --yellow: #F7D417;
  --rose: #F59BB5;
  --green: #2E8B3A;
  --ink: #2B2350;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: 'Baloo 2', 'Chalkboard SE', 'Comic Sans MS', ui-rounded, system-ui, sans-serif;
  color: var(--ink);
  background-color: var(--paper);
  background-image: radial-gradient(var(--dot) 1.3px, transparent 1.6px);
  background-size: 22px 22px;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }

.screen { position: fixed; inset: 0; display: none; }
.screen.active { display: flex; }

/* ---------- Boutons ---------- */
.btn-big {
  font-size: clamp(24px, 4vh, 36px);
  font-weight: 800;
  padding: .3em 1.4em;
  border: 4px solid var(--navy);
  border-radius: 999px;
  background: var(--yellow);
  box-shadow: 0 6px 0 var(--navy);
  transition: transform .08s, box-shadow .08s;
}
.btn-big:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--navy); }

.btn-small {
  font-size: clamp(17px, 2.4vh, 21px);
  font-weight: 700;
  padding: .25em 1em;
  border: 3px solid var(--purple);
  border-radius: 999px;
  background: #fff;
  box-shadow: 0 3px 0 var(--purple);
}
.btn-small:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--purple); }

/* ---------- Menu des voix ---------- */
.modal {
  position: fixed; inset: 0; z-index: 10;
  display: flex; align-items: center; justify-content: center;
  background: rgba(43, 35, 80, .35);
  padding: 16px;
}
.modal[hidden] { display: none; }
.modal-box {
  width: min(560px, 100%); max-height: 100%;
  display: flex; flex-direction: column; gap: 10px;
  background: var(--paper);
  border: 5px solid var(--pink); border-radius: 28px;
  padding: 18px 18px 20px;
  animation: pop .3s;
}
.modal-box h2 { margin: 0; color: var(--pink); font-size: clamp(26px, 4vh, 36px); text-align: center; }
.modal-hint { margin: 0; text-align: center; }
.all-langs { display: flex; align-items: center; gap: 8px; font-size: 17px; justify-content: center; }
.all-langs input { width: 22px; height: 22px; accent-color: var(--pink); }
.voice-list { overflow-y: auto; min-height: 0; flex: 1; display: flex; flex-direction: column; gap: 6px; padding: 2px; }
.voice-row {
  display: grid; grid-template-columns: 26px 1fr auto; grid-template-rows: auto auto;
  align-items: center; text-align: left;
  padding: 8px 12px;
  background: #fff; border: 3px solid #d9cfe8; border-radius: 16px;
}
.voice-row.on { border-color: var(--green); background: #EAF8EC; }
.v-check { grid-row: 1 / 3; color: var(--green); font-weight: 800; font-size: 20px; }
.v-name { font-weight: 800; font-size: 19px; }
.v-detail { grid-column: 2; font-size: 14px; color: var(--purple); }
.v-play { grid-row: 1 / 3; grid-column: 3; font-size: 20px; color: var(--pink); }
.v-empty { text-align: center; }
#voiceClose { align-self: center; }

/* ---------- Accueil ---------- */
#screen-title { overflow-y: auto; }
.title-wrap {
  margin: auto;
  padding: 24px 16px;
  display: flex; flex-direction: column; align-items: center; gap: 14px;
  text-align: center;
}
.big-title {
  margin: 0;
  font-size: clamp(44px, 9vw, 92px);
  line-height: .95;
  font-weight: 800;
  letter-spacing: 1px;
}
.big-title span { display: inline-block; text-shadow: 0 4px 0 rgba(43,35,80,.15); }
.big-title span:nth-child(6n+1) { color: var(--pink); transform: rotate(-4deg); }
.big-title span:nth-child(6n+2) { color: var(--sky); transform: rotate(3deg); }
.big-title span:nth-child(6n+3) { color: var(--orange); transform: rotate(-2deg); }
.big-title span:nth-child(6n+4) { color: var(--purple); transform: rotate(4deg); }
.big-title span:nth-child(6n+5) { color: var(--green); transform: rotate(-3deg); }
.big-title span:nth-child(6n) { color: var(--navy); transform: rotate(2deg); }
.ribbon {
  position: relative;
  margin: 4px 0 2px;
  padding: .15em 1.4em;
  font-size: clamp(18px, 2.8vh, 26px);
  font-weight: 800;
  color: #fff;
  background: var(--pink);
  transform: rotate(-2deg);
  box-shadow: 0 4px 0 #9c1f55;
}
.ribbon::before, .ribbon::after {
  content: ""; position: absolute; top: 8px; width: 22px; height: 100%;
  background: #9c1f55; z-index: -1;
}
.ribbon::before { left: -14px; clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%, 40% 50%); }
.ribbon::after { right: -14px; clip-path: polygon(0 0, 100% 0, 60% 50%, 100% 100%, 0 100%); }
.subtitle { margin: 0; font-size: clamp(18px, 2.6vh, 24px); max-width: 30em; }
.drawing {
  margin: 4px 0 10px;
  background: #fff;
  padding: 10px 10px 4px;
  border-radius: 6px;
  box-shadow: 0 6px 18px rgba(43,35,80,.18);
  transform: rotate(-2deg);
  position: relative;
}
.drawing::before {
  content: ''; position: absolute; top: -12px; left: 50%; width: 90px; height: 26px;
  transform: translateX(-50%) rotate(3deg);
  background: rgba(247, 212, 23, .55);
}
.drawing img { display: block; width: min(420px, 78vw); max-height: 34vh; object-fit: cover; border-radius: 3px; }
.drawing figcaption { font-size: 16px; color: var(--purple); padding-top: 2px; }

/* ---------- Choix du personnage ---------- */
#screen-choose { overflow-y: auto; }
.choose-wrap { margin: auto; padding: 24px 16px; text-align: center; }
.choose-wrap h2 { font-size: clamp(32px, 6vh, 56px); line-height: 1.1; margin: 0 0 18px; color: var(--pink); }
.cards { display: flex; gap: clamp(12px, 3vw, 32px); justify-content: center; flex-wrap: wrap; }
.char-card {
  width: clamp(140px, 21vw, 230px);
  background: #fff;
  border: 5px solid var(--navy);
  border-radius: 28px;
  padding: 12px 10px 8px;
  box-shadow: 0 8px 0 var(--navy);
  display: flex; flex-direction: column; align-items: center;
  transition: transform .12s;
}
.char-card:nth-child(2) { border-color: var(--orange); box-shadow: 0 8px 0 var(--orange); }
.char-card:nth-child(3) { border-color: var(--green); box-shadow: 0 8px 0 var(--green); }
.char-card:nth-child(4) { border-color: var(--violet); box-shadow: 0 8px 0 var(--purple); }
.char-card:hover { transform: translateY(-4px) rotate(-1deg); }
.char-card:active { transform: translateY(4px); }
.char-card .pic { width: 100%; aspect-ratio: 1; }
.char-card .pic svg { width: 100%; height: 100%; }
.char-card span { font-size: clamp(20px, 3vh, 28px); font-weight: 800; line-height: 1.15; padding: 4px 0; }

/* ---------- Jeu ---------- */
#screen-play.active {
  display: grid;
  grid-template-columns: minmax(0, 1.55fr) minmax(300px, 1fr);
  height: 100dvh;
}
.scene-wrap { position: relative; min-height: 0; min-width: 0; padding: 12px; display: flex; }
#scene { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; }
#scene svg { width: 100%; height: 100%; display: block; }

.panel {
  display: flex; flex-direction: column; justify-content: center; gap: clamp(6px, 1.4vh, 14px);
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  min-width: 0;
}
.hud { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.sleep { display: flex; flex-direction: column; line-height: 1; }
.sleep small { font-size: 14px; color: var(--purple); margin-top: 2px; }
.zzz { display: flex; gap: 4px; }
.zzz span {
  width: 34px; height: 34px; border-radius: 50%;
  display: grid; place-items: center;
  font-weight: 800; font-size: 20px;
  background: var(--sky); color: #fff;
  border: 3px solid var(--navy);
  transition: all .3s;
}
.zzz span.lost { background: transparent; color: #c9c0d8; border-color: #c9c0d8; transform: scale(.8); }
.zzz span.pop { animation: pop .5s; }
.hud-btns { display: flex; gap: 6px; }
.icon-btn {
  width: 46px; height: 46px; border-radius: 14px;
  border: 3px solid var(--purple); background: #fff;
  font-size: 22px; line-height: 1;
  box-shadow: 0 3px 0 var(--purple);
}
.icon-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--purple); }
.icon-btn.pulse { animation: pulse 1s infinite; background: var(--yellow); }

.zone-line { display: flex; align-items: center; justify-content: space-between; font-size: clamp(20px, 3vh, 28px); font-weight: 800; color: var(--pink); }
.progress { display: flex; gap: 6px; }
.progress i { width: 16px; height: 16px; border-radius: 50%; border: 3px solid var(--pink); display: block; }
.progress i.done { background: var(--pink); }
.progress i.now { background: var(--yellow); }

.calc {
  display: flex; align-items: center; justify-content: center; flex-wrap: wrap;
  gap: .18em;
  font-size: clamp(38px, 7.5vh, 68px);
  font-weight: 800;
  line-height: 1.1;
  background: #fff;
  border: 4px solid var(--navy);
  border-radius: 22px;
  padding: .12em .3em;
  min-height: 1.6em;
}
.calc .n1 { color: var(--navy); }
.calc .n2 { color: var(--violet); }
.calc .n3 { color: var(--orange); }
.calc .op { color: var(--ink); opacity: .7; }
.calc .answer {
  min-width: 1.6em; text-align: center;
  border: 4px dashed var(--pink); border-radius: 14px;
  color: var(--pink);
  padding: 0 .1em;
}
.calc .answer.filled { border-style: solid; color: var(--green); border-color: var(--green); }
.calc.shake { animation: shake .45s; }
.calc.good .answer { background: #DDF5DF; }

.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: clamp(6px, 1.2vh, 12px); }
.keypad button {
  height: clamp(50px, 9vh, 80px);
  font-size: clamp(28px, 5vh, 44px);
  font-weight: 800;
  border-radius: 18px;
  background: #fff;
  border: 4px solid var(--navy);
  box-shadow: 0 5px 0 var(--navy);
  transition: transform .06s, box-shadow .06s;
}
.keypad button:nth-child(3n+1) { border-color: var(--sky); box-shadow: 0 5px 0 #1d8fb0; }
.keypad button:nth-child(3n+2) { border-color: var(--violet); box-shadow: 0 5px 0 var(--purple); }
.keypad button:nth-child(3n) { border-color: var(--orange); box-shadow: 0 5px 0 #b4520c; }
.keypad button:active { transform: translateY(4px); box-shadow: 0 1px 0 var(--navy); }
.keypad .k-del { background: #FDE3EE; border-color: var(--pink) !important; box-shadow: 0 5px 0 #9c1f55 !important; }
.keypad .k-ok { background: #DDF5DF; border-color: var(--green) !important; box-shadow: 0 5px 0 #1d5e26 !important; color: var(--green); }
.keypad .k-ok.ready { animation: pulse 1s infinite; background: #9EE3A6; }
.panel.busy .keypad { opacity: .55; pointer-events: none; }

/* Bulle d'aide : des étoiles à compter */
.help {
  position: absolute; left: 50%; bottom: 18px; transform: translateX(-50%);
  width: min(94%, 640px);
  background: #fff; border: 4px solid var(--yellow); border-radius: 24px;
  padding: 10px 14px;
  display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 10px;
  box-shadow: 0 8px 20px rgba(43,35,80,.2);
}
.help[hidden] { display: none; }
.help .grp { display: grid; grid-template-columns: repeat(5, auto); gap: 2px; }
.help .grp svg { width: clamp(18px, 3.2vh, 28px); height: clamp(18px, 3.2vh, 28px); }
.help .sym { font-size: 32px; font-weight: 800; }
.help .close { position: absolute; top: -16px; right: -10px; width: 36px; height: 36px; border-radius: 50%; border: 3px solid var(--yellow); background: #fff; font-size: 18px; }

/* Carte d'histoire */
.card-overlay {
  position: absolute; inset: 12px;
  display: flex; align-items: center; justify-content: center;
  background: rgba(251, 246, 236, .55);
  border-radius: 22px;
  padding: 16px;
}
.card-overlay[hidden] { display: none; }
.card {
  max-width: 560px;
  background: #fff;
  border: 5px solid var(--pink);
  border-radius: 28px;
  padding: 18px 22px 22px;
  text-align: center;
  box-shadow: 0 10px 0 rgba(214, 51, 122, .25);
  animation: pop .4s;
}
.card p { font-size: clamp(19px, 3vh, 26px); margin: 0 0 16px; line-height: 1.3; }

/* ---------- Animations des scènes ---------- */
.actor { transition: transform 1s ease-in-out; }
.hop.hopping { animation: hop .5s ease-in-out 2; }
.hop.tiptoe { animation: tiptoe .7s ease-in-out 3; }
.guard .eye-open { display: none; }
.guard.awake .eye-open { display: inline; }
.guard.awake .eyes-closed { display: none; }
.guard.awake .zfloat { display: none; }
.guard .mouth { transform-box: fill-box; transform-origin: center; animation: snore 3.2s ease-in-out infinite; }
.guard .belly { transform-box: fill-box; transform-origin: bottom; animation: breathe 3.2s ease-in-out infinite; }
.guard.stir { animation: stir .5s ease-in-out 2; transform-box: fill-box; transform-origin: bottom; }
.guard .z { font-family: 'Baloo 2', sans-serif; font-weight: 800; fill: var(--navy); font-size: 26px; animation: zfloat 3.2s ease-in infinite; opacity: 0; }
.guard .z2 { animation-delay: .5s; font-size: 22px; }
.guard .z3 { animation-delay: 1s; font-size: 30px; }
.flame { transform-box: fill-box; transform-origin: bottom; animation: flicker .6s ease-in-out infinite alternate; }
#bars { transition: transform 1.4s ease-in; }
#lock { transition: transform .9s ease-in, opacity .9s; }
.burst { opacity: 0; transform-box: fill-box; transform-origin: center; }
.burst.on { animation: burst 1.6s ease-out forwards; }

@keyframes hop { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-22px); } }
@keyframes tiptoe { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-8px); } }
@keyframes snore { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.8, 2.2); } }
@keyframes breathe { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.04, 1.06); } }
@keyframes stir { 0%, 100% { transform: rotate(0); } 25% { transform: rotate(-4deg); } 75% { transform: rotate(4deg); } }
@keyframes zfloat { 0% { opacity: 0; transform: translate(0, 0); } 30% { opacity: 1; } 100% { opacity: 0; transform: translate(14px, -28px); } }
@keyframes flicker { from { transform: scale(1, 1); } to { transform: scale(.9, 1.12); } }
@keyframes burst { 0% { opacity: 0; transform: scale(.3); } 30% { opacity: 1; transform: scale(1.1); } 100% { opacity: 0; transform: scale(1.4) translateY(-40px); } }
@keyframes pop { 0% { transform: scale(.6); opacity: 0; } 70% { transform: scale(1.06); } 100% { transform: scale(1); opacity: 1; } }
@keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.08); } }
@keyframes shake { 0%, 100% { transform: translateX(0); } 20%, 60% { transform: translateX(-10px); } 40%, 80% { transform: translateX(10px); } }

/* ---------- Victoire ---------- */
#screen-win { overflow: hidden; }
.win-wrap { margin: auto; padding: 16px; display: flex; flex-direction: column; align-items: center; gap: 10px; width: 100%; height: 100%; justify-content: center; }
.win-scene { width: min(100%, 900px); flex: 0 1 auto; min-height: 0; max-height: 68vh; display: flex; }
.win-scene svg { width: 100%; height: 100%; max-height: 68vh; }
.win-wrap h2 { margin: 0; font-size: clamp(28px, 5vh, 52px); color: var(--pink); text-align: center; }
.confetti { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.confetti svg { position: absolute; top: -40px; width: 30px; height: 30px; animation: fall linear forwards; }
@keyframes fall { to { transform: translateY(110vh) rotate(540deg); } }

/* ---------- Mises en page ---------- */
@media (orientation: portrait), (max-aspect-ratio: 1/1) {
  #screen-play.active { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) auto; }
  .panel { padding-top: 0; max-width: 620px; width: 100%; justify-self: center; }
}
@media (max-height: 520px) and (orientation: landscape) {
  .zone-line { display: none; }
  .sleep small { display: none; }
  .scene-wrap { padding: 6px; }
  .panel { padding: 6px 12px; }
  .icon-btn { width: 36px; height: 36px; font-size: 17px; border-radius: 10px; }
  .zzz span { width: 26px; height: 26px; font-size: 15px; border-width: 2px; }
  .hud-btns { gap: 4px; }
}
@media (max-width: 420px) {
  .zzz span { width: 28px; height: 28px; font-size: 16px; }
  .icon-btn { width: 40px; height: 40px; font-size: 19px; }
}
@media (prefers-reduced-motion: reduce) {
  .actor { transition-duration: .2s; }
  .guard .z, .guard .mouth, .guard .belly, .flame { animation: none; }
}
