:root {
  --pink: #ff7fb0;
  --pink-dark: #e85a93;
  --pink-light: #ffe1ec;
  --cream: #fffaf3;
  --ink: #5a3550;
  --muted: #9a7590;
  --gold: #f7c84a;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #f6e3d9;
  font-family: 'M PLUS Rounded 1c', system-ui, sans-serif;
  color: var(--ink);
  user-select: none;
  -webkit-user-select: none;
  overscroll-behavior: none;
  touch-action: none;
}

#game, #game canvas { position: fixed; inset: 0; display: block; }

#fx { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; }
#fx-layer { position: fixed; inset: 0; pointer-events: none; overflow: hidden; }

/* +10 qui s'envole avec des petits coeurs et étoiles */
.pop-score {
  position: absolute; transform: translate(-50%, -50%);
  font-weight: 800; font-size: 26px; color: #ff6fa5;
  -webkit-text-stroke: 1.5px #fff; paint-order: stroke fill;
  text-shadow: 0 2px 0 rgba(232, 90, 147, 0.5);
  animation: pop-score 0.9s cubic-bezier(.2, 1.4, .4, 1) forwards;
  will-change: transform, opacity;
}
@keyframes pop-score {
  0% { transform: translate(-50%, -50%) scale(0.3); opacity: 0; }
  20% { transform: translate(-50%, -90%) scale(1.25); opacity: 1; }
  45% { transform: translate(-50%, -110%) scale(1); }
  100% { transform: translate(-50%, -220%) scale(0.9); opacity: 0; }
}
.pop-bit {
  position: absolute; transform: translate(-50%, -50%);
  font-size: 18px; color: var(--c, #ff8fb6);
  animation: pop-bit 0.8s ease-out forwards;
  will-change: transform, opacity;
}
@keyframes pop-bit {
  0% { transform: translate(-50%, -50%) scale(0.2) rotate(0); opacity: 1; }
  100% { transform: translate(calc(-50% + var(--dx)), calc(-50% + var(--dy))) scale(1) rotate(var(--r)); opacity: 0; }
}

/* carte de déblocage d'un niveau */
#unlock {
  position: fixed; left: 50%; top: calc(16% + var(--safe-t)); z-index: 25;
  display: flex; align-items: center; gap: 14px;
  padding: 14px 22px 14px 16px; border-radius: 24px;
  background: linear-gradient(135deg, #fff8e6, #ffe3ef);
  border: 3px solid #fff; overflow: hidden;
  box-shadow: 0 8px 0 rgba(232, 90, 147, 0.35), 0 0 24px rgba(255, 210, 120, 0.7);
  transform: translate(-50%, -50%) scale(0); opacity: 0; pointer-events: none;
  visibility: hidden;
  will-change: transform, opacity;   /* animé par le compositeur, sans repeindre l'ombre à chaque image */
}
#unlock.show { visibility: visible; animation: unlock-in 3.4s cubic-bezier(.2, 1.4, .4, 1) forwards; }
.unlock-icon { font-size: 46px; }
#unlock.show .unlock-icon { animation: unlock-bob 0.6s ease-in-out infinite alternate; }
.unlock-text { display: flex; flex-direction: column; text-align: left; }
.unlock-text small { font-weight: 800; color: var(--pink-dark); font-size: 13px; }
.unlock-text b { font-size: 26px; font-weight: 800; color: #d8901a; line-height: 1.1; }
.unlock-text span { font-size: 12px; color: var(--muted); }
.unlock-shine {
  position: absolute; top: 0; bottom: 0; width: 60px; left: 0;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.9), transparent);
  transform: translateX(-80px) skewX(-20deg);
}
#unlock.show .unlock-shine { animation: unlock-shine 1.2s 0.4s ease-in-out 2; }
@keyframes unlock-in {
  0% { transform: translate(-50%, -50%) scale(0) rotate(-10deg); opacity: 0; }
  12% { transform: translate(-50%, -50%) scale(1.15) rotate(2deg); opacity: 1; }
  20% { transform: translate(-50%, -50%) scale(1) rotate(0); }
  85% { transform: translate(-50%, -50%) scale(1); opacity: 1; }
  100% { transform: translate(-50%, -80%) scale(0.9); opacity: 0; }
}
@keyframes unlock-bob { from { transform: translateY(0) rotate(-6deg); } to { transform: translateY(-5px) rotate(6deg); } }
@keyframes unlock-shine { to { transform: translateX(380px) skewX(-20deg); } }
#confetti-box { position: fixed; inset: 0; z-index: 26; display: none; pointer-events: none; }
#confetti-box.go { display: block; }
.confetti {
  position: absolute; top: -20px; width: 9px; height: 14px; border-radius: 2px;
  background: var(--c);
}
.confetti.star { width: 12px; height: 12px; border-radius: 0; clip-path: polygon(50% 0, 62% 38%, 100% 50%, 62% 62%, 50% 100%, 38% 62%, 0 50%, 38% 38%); }
#confetti-box.go .confetti { animation: confetti var(--d, 2s) cubic-bezier(.25, .6, .4, 1) both; will-change: transform; }
.glyph-warm { position: fixed; left: 0; top: 0; font-size: 46px; opacity: 0.01; pointer-events: none; z-index: -1; }
@keyframes confetti {
  from { transform: translate(0, 0) rotate(0); opacity: 1; }
  to { transform: translate(var(--dx), 105vh) rotate(var(--r)); opacity: 0.9; }
}

/* traits de vitesse et petit nuage de poussière au changement de voie */
.dash {
  position: absolute; width: 90px; height: 70px; transform: translate(-50%, -50%) scaleX(var(--dir, 1));
  will-change: transform, opacity;
}
.dash i {
  position: absolute; left: 0; height: 6px; border-radius: 6px;
  background: linear-gradient(90deg, rgba(255,255,255,0), rgba(255,255,255,1));
  box-shadow: 0 0 8px rgba(255, 190, 220, 0.8);
  animation: dash-line 0.32s ease-out forwards;
}
.dash i:nth-child(1) { top: 12px; width: 60px; }
.dash i:nth-child(2) { top: 32px; width: 80px; animation-delay: 0.03s; }
.dash i:nth-child(3) { top: 52px; width: 50px; animation-delay: 0.06s; }
.dash b {
  position: absolute; bottom: -18px; width: 22px; height: 22px; border-radius: 50%;
  background: radial-gradient(circle, rgba(255,250,240,0.9), rgba(255,250,240,0));
  animation: dash-puff 0.45s ease-out forwards;
}
.dash b:nth-of-type(1) { left: 54px; }
.dash b:nth-of-type(2) { left: 34px; animation-delay: 0.05s; }
@keyframes dash-line { from { transform: translateX(30px) scaleX(0.3); opacity: 1; } to { transform: translateX(-10px) scaleX(1); opacity: 0; } }
@keyframes dash-puff { from { transform: scale(0.3); opacity: 1; } to { transform: translate(-18px, -10px) scale(1.8); opacity: 0; } }

/* défaite : flash rouge puis l'écran s'assombrit */
#death-dim {
  position: fixed; inset: 0; pointer-events: none; opacity: 0;
  background:
    radial-gradient(ellipse at 50% 60%, rgba(60, 0, 8, 0.15), rgba(25, 0, 4, 0.85) 75%),
    rgba(10, 0, 3, 0.35);
  transition: opacity 0.2s;
}
#death-dim.on { opacity: 1; transition: opacity 1.4s ease-in; }
.hit-flash { position: absolute; inset: 0; background: rgba(220, 20, 40, 0.5); animation: hit-flash 0.45s ease-out forwards; }
@keyframes hit-flash { from { opacity: 1; } to { opacity: 0; } }

#levelup {
  position: fixed; left: 50%; top: calc(17% + var(--safe-t));
  transform: translate(-50%, -50%);
  text-align: center; pointer-events: none; opacity: 0;
  white-space: nowrap;
}
#levelup { visibility: hidden; will-change: transform, opacity; }
#levelup.show { visibility: visible; animation: levelup 1.4s cubic-bezier(.2, 1.3, .4, 1) forwards; }
.lu-title {
  font-size: clamp(34px, 8vw, 58px); font-weight: 800; line-height: 1;
  background: linear-gradient(90deg, #ff7fb0, #ffd23f, #7fe0b0, #6fc3ff, #c39bff);
  -webkit-background-clip: text; background-clip: text; color: transparent;
  -webkit-text-stroke: 2px #fff;
}
.lu-name { margin-top: 6px; font-size: clamp(18px, 4.5vw, 28px); font-weight: 800; color: #fff; text-shadow: 0 2px 0 var(--pink-dark), 0 0 10px rgba(255, 255, 255, 0.8); }
@keyframes levelup {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.2) rotate(-8deg); }
  18% { opacity: 1; transform: translate(-50%, -50%) scale(1.15) rotate(3deg); }
  30% { transform: translate(-50%, -50%) scale(1) rotate(0); }
  70% { opacity: 1; transform: translate(-50%, -55%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -80%) scale(0.9); }
}

#loading {
  position: fixed; inset: 0; display: grid; place-items: center;
  font-size: 22px; font-weight: 800; color: var(--pink-dark);
}
body.ready #loading { display: none; }

.hidden { display: none !important; }

button { font-family: inherit; cursor: pointer; }

/* ---------- Écrans ---------- */
.screen {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding: calc(16px + var(--safe-t)) calc(16px + var(--safe-r)) calc(16px + var(--safe-b)) calc(16px + var(--safe-l));
  background: rgba(255, 230, 240, 0.25);
  touch-action: auto;
}
#screen-over { background: none; }   /* laisse voir l'écran assombri de la défaite */
#screen-menu { background: none; justify-content: flex-end; padding-right: max(6vw, 16px); }

/* Illustration de l'héroïne sur l'écran d'accueil */
#menu-chara {
  position: absolute; left: max(4vw, 12px); bottom: 2vh;
  height: min(92vh, 640px); aspect-ratio: 400 / 560;
  pointer-events: none;
  animation: chara-in 0.6s cubic-bezier(.2, 1.4, .4, 1);
}
#menu-chara img { pointer-events: auto; cursor: pointer; -webkit-tap-highlight-color: transparent; user-select: none; -webkit-user-select: none; }
#menu-chara img.boop { animation: chara-boop 0.25s ease-out; }
@keyframes chara-boop { 30% { transform: scale(1.04, 0.95) translateY(2%); } 70% { transform: scale(0.98, 1.03); } }
#menu-chara img { position: relative; width: 100%; height: 100%; display: block; filter: drop-shadow(0 6px 10px rgba(90, 40, 70, 0.25)); }
.chara-shadow {
  position: absolute; left: 22%; right: 22%; bottom: 0.5%; height: 4%;
  background: radial-gradient(closest-side, rgba(60, 30, 50, 0.35), rgba(60, 30, 50, 0));
}
@keyframes chara-in { from { transform: translateY(30px) scale(0.9); opacity: 0; } to { transform: none; opacity: 1; } }

.panel {
  position: relative;
  width: min(380px, 100%);
  max-height: 100%;
  overflow-y: auto;
  padding: 26px 24px 22px;
  background: rgba(255, 250, 243, 0.92);
  border: 3px solid #fff;
  border-radius: 28px;
  box-shadow: 0 10px 0 rgba(232, 90, 147, 0.25), 0 18px 40px rgba(90, 53, 80, 0.2);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex; flex-direction: column; gap: 12px;
  text-align: center;
  animation: pop 0.35s cubic-bezier(.2, 1.4, .4, 1);
}
.panel.small { width: min(330px, 100%); }

@keyframes pop { from { transform: scale(0.85); opacity: 0; } to { transform: none; opacity: 1; } }

.title {
  margin: 0;
  font-size: 46px; font-weight: 800; line-height: 1;
  color: var(--pink);
  text-shadow: 3px 3px 0 #fff, 5px 5px 0 rgba(232, 90, 147, 0.3);
  letter-spacing: 1px;
}
.title span { display: block; font-size: 30px; color: var(--gold); text-shadow: 2px 2px 0 #fff, 4px 4px 0 rgba(200, 140, 30, 0.3); }
.subtitle { margin: -4px 0 6px; color: var(--muted); font-size: 14px; letter-spacing: 4px; }

h2 { margin: 0 0 4px; font-size: 26px; color: var(--pink-dark); }

label { font-size: 14px; color: var(--muted); text-align: left; margin-bottom: -6px; padding-left: 6px; }

input {
  font: inherit; font-size: 20px; font-weight: 800;
  color: var(--ink);
  padding: 12px 16px;
  border-radius: 16px;
  border: 3px solid var(--pink-light);
  background: #fff;
  outline: none;
  text-align: center;
  user-select: text; -webkit-user-select: text;
}
input:focus { border-color: var(--pink); }
input.shake { animation: shake 0.4s; border-color: var(--pink-dark); }
@keyframes shake { 20%, 60% { transform: translateX(-6px); } 40%, 80% { transform: translateX(6px); } }
.error { min-height: 0; color: var(--pink-dark); font-size: 14px; margin-top: -6px; }
.error:empty { display: none; }

.btn {
  font-size: 17px; font-weight: 800;
  padding: 12px 16px;
  border-radius: 999px;
  border: 0;
  color: var(--pink-dark);
  background: var(--pink-light);
  box-shadow: 0 4px 0 #f6bfd4;
  transition: transform 0.08s, box-shadow 0.08s;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #f6bfd4; }
.btn.primary {
  font-size: 22px; padding: 14px 18px;
  color: #fff;
  background: linear-gradient(180deg, #ff9cc4, var(--pink));
  box-shadow: 0 5px 0 var(--pink-dark);
}
.btn.primary:active { box-shadow: 0 2px 0 var(--pink-dark); }
.row { display: flex; gap: 10px; }
.row .btn { flex: 1; }

.icon-btn {
  width: 46px; height: 46px;
  border-radius: 50%;
  border: 3px solid #fff;
  background: rgba(255, 250, 243, 0.9);
  font-size: 20px;
  box-shadow: 0 3px 0 rgba(232, 90, 147, 0.35);
  display: grid; place-items: center;
  padding: 0;
}
/* Boutons du coin supérieur droit */
#top-btns {
  position: fixed; z-index: 30;
  top: calc(12px + var(--safe-t)); right: calc(12px + var(--safe-r));
  display: flex; gap: 8px;
}
#top-btns svg { width: 22px; height: 22px; fill: none; stroke: var(--pink-dark); stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
#top-btns svg .fill { fill: var(--pink-dark); stroke-width: 1.2; }
#top-btns .icon-btn { color: var(--pink-dark); transition: transform 0.1s; }
#top-btns .icon-btn:active { transform: scale(0.9); }
#btn-pause { display: none; }
body.in-run #btn-pause { display: grid; }
.ico-muted, .ico-exit, body.is-muted .ico-sound, body.is-fullscreen .ico-enter { display: none; }
body.is-muted .ico-muted, body.is-fullscreen .ico-exit { display: block; }
body.no-fullscreen #btn-fs { display: none; }

/* ---------- Choix du niveau ---------- */
.level-label { font-size: 14px; color: var(--muted); text-align: left; margin-bottom: -6px; padding-left: 6px; }
.level-picker { display: flex; align-items: center; gap: 8px; }
.lv-arrow {
  flex: none; width: 42px; height: 42px; border-radius: 50%; border: 0;
  display: grid; place-items: center;
  background: var(--pink-light); box-shadow: 0 3px 0 #f6bfd4;
  transition: transform 0.08s, box-shadow 0.08s;
}
.lv-arrow svg { width: 22px; height: 22px; fill: none; stroke: var(--pink-dark); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
.lv-arrow:active { transform: translateY(2px) scale(0.94); box-shadow: 0 1px 0 #f6bfd4; }
.level-card {
  flex: 1; min-width: 0; position: relative;
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px; border-radius: 18px;
  border: 3px solid var(--pink); background: #fff3f8; box-shadow: 0 3px 0 #f6bfd4;
  color: var(--ink); text-align: left;
}
.level-card .lv-icon { position: relative; font-size: 30px; line-height: 1; }
.level-card .lv-lock { position: absolute; right: -8px; bottom: -6px; font-size: 16px; }
.level-card .lv-text { display: flex; flex-direction: column; min-width: 0; }
.level-card b { font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.level-card small { font-size: 11.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.level-card.locked { background: #f6f0f2; border-color: #e6d6dc; box-shadow: 0 3px 0 #e6d6dc; }
.level-card.locked #lv-icon { filter: grayscale(0.8); opacity: 0.55; }
.level-card.locked b { color: var(--muted); }
.level-card.loading::after {
  content: ''; position: absolute; right: 10px; top: 50%; width: 16px; height: 16px; margin-top: -8px;
  border-radius: 50%; border: 3px solid var(--pink-light); border-top-color: var(--pink);
  animation: spin 0.8s linear infinite;
}
.level-card.slide-r { animation: lv-in-r 0.25s ease-out; }
.level-card.slide-l { animation: lv-in-l 0.25s ease-out; }
@keyframes lv-in-r { from { transform: translateX(18px); opacity: 0.3; } }
@keyframes lv-in-l { from { transform: translateX(-18px); opacity: 0.3; } }
.level-card.shake { animation: shake 0.4s; }
.level-dots { display: flex; justify-content: center; gap: 6px; margin-top: -4px; }
.level-dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--pink-light); }
.level-dots i.on { background: var(--pink); }
.btn.primary.locked { background: linear-gradient(180deg, #d8ccd2, #bfb0b8); box-shadow: 0 5px 0 #a8989f; }

/* chargement d'un niveau */
#level-loading {
  position: fixed; inset: 0; z-index: 40;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  background: rgba(255, 240, 246, 0.85); backdrop-filter: blur(4px);
  font-weight: 800; color: var(--pink-dark); font-size: 18px;
}
.spinner {
  width: 48px; height: 48px; border-radius: 50%;
  border: 5px solid var(--pink-light); border-top-color: var(--pink);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* onglets du tableau des scores */
.tabs { display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; }
.tab {
  border: 0; border-radius: 999px; padding: 6px 12px;
  font-weight: 800; font-size: 13px; color: var(--pink-dark); background: var(--pink-light);
}
.tab.on { background: var(--pink); color: #fff; }

/* ---------- Aide ---------- */
.help { list-style: none; padding: 0; margin: 0; text-align: left; display: flex; flex-direction: column; gap: 10px; }
.help li { display: flex; gap: 12px; align-items: center; font-size: 15px; }
.keys {
  flex: 0 0 64px; text-align: center;
  font-weight: 800; color: var(--pink-dark);
  background: var(--pink-light); border-radius: 10px; padding: 6px 4px;
}
.tip { font-size: 14px; line-height: 1.5; color: var(--muted); margin: 4px 0; text-align: left; }
.tip b { color: var(--ink); }

/* ---------- Scores ---------- */
.scores { width: 100%; border-collapse: collapse; font-size: 16px; }
.scores th { font-size: 12px; color: var(--muted); font-weight: 500; padding: 4px; }
.scores td { padding: 8px 6px; border-top: 2px dashed var(--pink-light); }
.scores .rank { width: 42px; font-weight: 800; }
.scores .name { text-align: left; font-weight: 800; max-width: 150px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.scores .pts { text-align: right; font-weight: 800; color: var(--pink-dark); }
.scores tr.me td { background: #fff3c4; }
.scores .empty { text-align: center; color: var(--muted); padding: 20px 0; line-height: 1.6; }

/* ---------- Fin de partie ---------- */
.big-score { font-size: 54px; font-weight: 800; color: var(--pink); line-height: 1; text-shadow: 3px 3px 0 #fff; }
.big-score small { font-size: 20px; color: var(--muted); }
.over-best { color: var(--muted); font-size: 15px; }
.over-msg { margin: 0; font-weight: 800; color: var(--ink); min-height: 1.2em; }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; }
.score-box {
  position: absolute;
  top: calc(12px + var(--safe-t)); left: calc(12px + var(--safe-l));
  padding: 8px 16px 10px;
  background: rgba(255, 250, 243, 0.85);
  border: 3px solid #fff;
  border-radius: 20px;
  box-shadow: 0 4px 0 rgba(232, 90, 147, 0.3);
  min-width: 130px;
}
.hud-name { font-size: 12px; color: var(--muted); font-weight: 800; }
.score { font-size: 32px; font-weight: 800; color: var(--pink); line-height: 1.05; }
.score small { font-size: 15px; color: var(--muted); }
.best { font-size: 12px; color: var(--muted); }
.hud-line { font-size: 13px; font-weight: 800; color: var(--muted); }
.candy-row { display: flex; align-items: center; gap: 5px; margin: 3px 0 2px; font-weight: 800; font-size: 15px; color: var(--pink-dark); }
.candy-ico { font-size: 15px; }
.candy-bar { flex: 1; height: 8px; min-width: 50px; border-radius: 99px; background: var(--pink-light); overflow: hidden; }
#hud-candybar { height: 100%; width: 0%; border-radius: 99px; background: linear-gradient(90deg, #ffd23f, var(--pink)); transition: width 0.2s; }
.over-detail { font-size: 14px; font-weight: 800; color: var(--muted); margin-top: -4px; }
#toast {
  position: absolute; left: 50%; top: 22%;
  transform: translate(-50%, 0);
  font-size: 28px; font-weight: 800; color: #fff;
  text-shadow: 0 3px 0 var(--pink-dark), 0 0 12px rgba(232, 90, 147, 0.6);
  white-space: nowrap; opacity: 0;
}
#toast.show { animation: toast 1.6s ease-out; }
@keyframes toast {
  0% { opacity: 0; transform: translate(-50%, 20px) scale(0.8); }
  15% { opacity: 1; transform: translate(-50%, 0) scale(1.05); }
  75% { opacity: 1; transform: translate(-50%, 0) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -20px) scale(1); }
}

/* ---------- Téléphone (portrait) ---------- */
@media (max-aspect-ratio: 1/1) {
  #menu-chara { left: 50%; bottom: auto; top: calc(2vh + var(--safe-t)); height: min(50vh, 520px); transform: translateX(-50%); animation: none; }
  #screen-menu { justify-content: center; align-items: flex-end; padding-right: calc(16px + var(--safe-r)); }
  #screen-menu .panel { padding: 18px 20px 18px; gap: 10px; }
  .title { font-size: 38px; }
  .title span { font-size: 24px; display: inline; margin-left: 8px; }
}
@media (max-height: 520px) and (min-aspect-ratio: 1/1) {
  .panel { padding: 14px 18px; gap: 8px; }
  .title { font-size: 32px; }
  .title span { font-size: 22px; }
  .btn.primary { font-size: 18px; padding: 10px; }
}
