:root {
  --bg: #fff7ec;
  --bg2: #ffeeda;
  --card: #ffffff;
  --ink: #2b2340;
  --muted: #7a7090;
  --line: #eadfce;
  --primary: #7c5cff;
  --primary-ink: #ffffff;
  --ok: #22b55e;
  --ok-bg: #dcfce7;
  --bad: #e5484d;
  --bad-bg: #fde2e2;
  --star: #ffb703;
  --muet: #a9a9b3;
  --marie: #13a35a;
  --radius: 22px;
  --shadow: 0 6px 0 rgba(43, 35, 64, .10), 0 10px 24px rgba(43, 35, 64, .08);
  --font: 'Andika', 'Trebuchet MS', system-ui, sans-serif;
  --title: 'Fredoka', 'Andika', system-ui, sans-serif;
  color-scheme: light;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 18px;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
  overscroll-behavior: none;
}
input, textarea, select { font: inherit; -webkit-user-select: text; user-select: text; }
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; touch-action: manipulation; }
img { -webkit-user-drag: none; }

#app { min-height: 100%; display: flex; flex-direction: column; }

/* ---------- En-tête ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 10px;
  padding: calc(env(safe-area-inset-top) + 10px) max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  background: var(--bg);
}
.topbar h1 { flex: 1; margin: 0; font-family: var(--title); font-size: clamp(20px, 4vw, 30px); font-weight: 600; text-align: center; line-height: 1.1; }
.icon-btn {
  width: 52px; height: 52px; flex: 0 0 auto;
  border-radius: 16px; background: var(--card); box-shadow: var(--shadow);
  font-size: 26px; display: grid; place-items: center;
}
.icon-btn:active { transform: translateY(3px); box-shadow: 0 3px 0 rgba(43,35,64,.10); }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border-radius: 999px; background: var(--card); box-shadow: var(--shadow); font-weight: 700; font-family: var(--title); white-space: nowrap; }
.spacer { width: 52px; flex: 0 0 auto; }

main.screen {
  flex: 1; width: 100%; max-width: 1000px; margin: 0 auto;
  padding: 8px max(16px, env(safe-area-inset-right)) calc(24px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  display: flex; flex-direction: column; gap: 16px;
}
.center { align-items: center; text-align: center; }

/* ---------- Boutons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 10px;
  min-height: 60px; padding: 12px 26px; border-radius: 20px;
  background: var(--primary); color: var(--primary-ink);
  font-family: var(--title); font-size: 22px; font-weight: 600;
  box-shadow: 0 6px 0 rgba(0,0,0,.18);
}
.btn:active { transform: translateY(4px); box-shadow: 0 2px 0 rgba(0,0,0,.18); }
.btn.secondary { background: var(--card); color: var(--ink); box-shadow: var(--shadow); }
.btn.ok { background: var(--ok); }
.btn.small { min-height: 46px; font-size: 18px; padding: 8px 18px; border-radius: 16px; }
.btn[disabled] { opacity: .45; pointer-events: none; }
.row { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; align-items: center; }

/* ---------- Profils ---------- */
.hero-title { font-family: var(--title); font-size: clamp(34px, 8vw, 60px); font-weight: 700; margin: 10px 0 0; color: var(--primary); text-align: center; letter-spacing: .5px; }
.hero-sub { color: var(--muted); text-align: center; margin: 0; font-size: 20px; }
.profiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 18px; margin-top: 10px; }
.profile-card {
  background: var(--card); border-radius: 28px; box-shadow: var(--shadow);
  padding: 26px 16px; display: flex; flex-direction: column; align-items: center; gap: 8px;
}
.profile-card:active { transform: scale(.98); }
.profile-card .avatar { font-size: 84px; line-height: 1; }
.profile-card .name { font-family: var(--title); font-size: 32px; font-weight: 600; }
.profile-card .meta { color: var(--muted); }

/* ---------- Accueil (grille des jeux) ---------- */
.games { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 16px; }
.game-tile {
  position: relative; border-radius: 26px; padding: 20px 12px 16px; min-height: 160px;
  color: #fff; box-shadow: 0 7px 0 rgba(0,0,0,.16);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; text-align: center;
}
.game-tile:active { transform: translateY(4px); box-shadow: 0 3px 0 rgba(0,0,0,.16); }
.game-tile .emoji { font-size: 56px; line-height: 1; filter: drop-shadow(0 3px 0 rgba(0,0,0,.12)); }
.game-tile .label { font-family: var(--title); font-size: 21px; font-weight: 600; line-height: 1.1; text-shadow: 0 2px 0 rgba(0,0,0,.15); }
.parent-link { align-self: center; color: var(--muted); padding: 10px 16px; font-size: 16px; }

/* ---------- Niveaux ---------- */
.levels { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 14px; }
.level-card {
  background: var(--card); border-radius: 22px; box-shadow: var(--shadow); padding: 16px 18px; text-align: left;
  display: flex; flex-direction: column; gap: 6px; border: 3px solid transparent;
}
.level-card:active { transform: translateY(3px); }
.level-card .lv-num { font-family: var(--title); color: var(--muted); font-size: 15px; font-weight: 600; text-transform: uppercase; letter-spacing: .5px; }
.level-card .lv-title { font-family: var(--title); font-size: 21px; font-weight: 600; line-height: 1.15; }
.level-card .lv-desc { color: var(--muted); font-size: 16px; }
.level-card .lv-stars { font-size: 22px; letter-spacing: 2px; }
.level-card.locked { opacity: .5; }
.level-card.locked .lv-title::before { content: '🔒 '; }
.stars-off { filter: grayscale(1); opacity: .35; }

/* ---------- Carte générique ---------- */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.muted { color: var(--muted); }
.big-q { font-family: var(--title); font-weight: 600; font-size: clamp(44px, 11vw, 84px); text-align: center; line-height: 1.1; margin: 0; }
.progress { display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; }
.progress span { width: 16px; height: 16px; border-radius: 50%; background: var(--line); }
.progress span.ok { background: var(--ok); }
.progress span.bad { background: var(--bad); }
.progress span.cur { outline: 3px solid var(--primary); outline-offset: 2px; }

.answer-box {
  min-width: 180px; min-height: 84px; padding: 8px 24px; border-radius: 22px;
  background: var(--card); box-shadow: inset 0 0 0 4px var(--line);
  font-family: var(--title); font-size: 56px; font-weight: 600; display: grid; place-items: center;
}
.answer-box.ok { box-shadow: inset 0 0 0 5px var(--ok); background: var(--ok-bg); }
.answer-box.bad { box-shadow: inset 0 0 0 5px var(--bad); background: var(--bad-bg); }
.shake { animation: shake .4s; }
@keyframes shake { 20%,60% { transform: translateX(-10px); } 40%,80% { transform: translateX(10px); } }
.pop { animation: pop .35s; }
@keyframes pop { 50% { transform: scale(1.12); } }

/* ---------- Pavé numérique ---------- */
.numpad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; width: 100%; max-width: 380px; margin: 0 auto; }
.numpad button {
  height: 68px; border-radius: 18px; background: var(--card); box-shadow: var(--shadow);
  font-family: var(--title); font-size: 32px; font-weight: 600;
}
.numpad button:active { transform: translateY(3px); box-shadow: 0 2px 0 rgba(43,35,64,.10); }
.numpad .go { background: var(--ok); color: #fff; }
.numpad .del { background: #ffe1c7; }

/* ---------- Clavier lettres ---------- */
.keyboard { display: flex; flex-direction: column; gap: 7px; width: 100%; max-width: 760px; margin: 0 auto; }
.keyboard .krow { display: flex; gap: 6px; justify-content: center; }
.keyboard button {
  flex: 1 1 0; max-width: 64px; height: 56px; border-radius: 12px;
  background: var(--card); box-shadow: 0 4px 0 rgba(43,35,64,.12);
  font-size: 24px; font-weight: 700; padding: 0;
}
.keyboard button:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(43,35,64,.12); }
.keyboard .acc { background: #efe9ff; }
.keyboard .wide { max-width: 130px; flex-grow: 2; font-size: 18px; }
.keyboard .space { max-width: 320px; flex-grow: 6; font-size: 16px; color: var(--muted); }
.keyboard .del { background: #ffe1c7; }
.keyboard .go { background: var(--ok); color: #fff; }
@media (max-width: 480px) {
  .keyboard button { height: 48px; font-size: 19px; border-radius: 9px; }
  .keyboard .krow { gap: 4px; }
}

/* ---------- Fin de partie ---------- */
.finish-stars { font-size: clamp(60px, 16vw, 110px); letter-spacing: 6px; }
.finish-stars span { display: inline-block; animation: starIn .5s both; }
.finish-stars span:nth-child(2) { animation-delay: .2s; }
.finish-stars span:nth-child(3) { animation-delay: .4s; }
@keyframes starIn { from { transform: scale(0) rotate(-90deg); opacity: 0; } to { transform: none; opacity: 1; } }
.badge-new { display: inline-flex; gap: 8px; align-items: center; background: #fff3c4; padding: 10px 16px; border-radius: 16px; font-weight: 700; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 14px 22px; border-radius: 16px; z-index: 100;
  font-size: 18px; box-shadow: 0 10px 30px rgba(0,0,0,.25); max-width: calc(100% - 32px); text-align: center;
  animation: toastIn .25s;
}
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 10px); } }

/* ---------- Choix (QCM) ---------- */
.choices { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.choice {
  background: var(--card); border-radius: 20px; box-shadow: var(--shadow); padding: 14px 10px;
  display: flex; flex-direction: column; align-items: center; gap: 4px; font-size: 20px; min-height: 80px; justify-content: center;
}
.choice .ce { font-size: 54px; line-height: 1.1; }
.choice.ok { background: var(--ok-bg); box-shadow: inset 0 0 0 4px var(--ok); }
.choice.bad { background: var(--bad-bg); box-shadow: inset 0 0 0 4px var(--bad); }
.choice:active { transform: translateY(3px); }

/* ---------- Lecture colorée ---------- */
.reading {
  font-family: 'Andika', var(--font); font-size: clamp(34px, 7.5vw, 58px); line-height: 1.7;
  text-align: center; word-spacing: .25em; padding: 18px 10px;
}
.reading .w { display: inline-block; border-radius: 12px; padding: 0 4px; }
.reading .w:active, .reading .w.speaking { background: #fff2b8; }
.reading .mu { color: var(--muet); }
.reading .ma { color: var(--marie); border-bottom: 4px solid currentColor; border-radius: 0 0 14px 14px; padding: 0 1px; margin: 0 1px; }
.reading.story { font-size: clamp(28px, 5.6vw, 44px); line-height: 1.6; }
.reading.small { font-size: clamp(22px, 4.6vw, 32px); line-height: 1.65; text-align: left; word-spacing: .1em; }
.legend { display: flex; gap: 18px; justify-content: center; flex-wrap: wrap; color: var(--muted); font-size: 16px; }
.legend b { font-size: 20px; }

/* ---------- Grilles (sudoku, mots fléchés, mots mêlés) ---------- */
.grid-wrap { width: 100%; display: flex; justify-content: center; }
.board { display: grid; background: var(--ink); gap: 2px; padding: 4px; border-radius: 14px; touch-action: none; }
.cell {
  background: #fff; display: grid; place-items: center; aspect-ratio: 1;
  font-family: var(--title); font-weight: 600; position: relative; overflow: hidden;
}
.sudoku .cell { font-size: var(--fs, 28px); }
.sudoku .cell.given { background: #f3efe7; color: var(--ink); }
.sudoku .cell.user { color: var(--primary); }
.sudoku .cell.peer { background: #f1ecff; }
.sudoku .cell.same { background: #e0d6ff; }
.sudoku .cell.sel { background: #c9b8ff; }
.sudoku .cell.wrong { color: var(--bad); background: var(--bad-bg); }
.sudoku .cell.bt { border-top: 3px solid var(--ink); }
.sudoku .cell.bl { border-left: 3px solid var(--ink); }
.pad { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.pad button { width: 62px; height: 62px; border-radius: 16px; background: var(--card); box-shadow: var(--shadow); font-family: var(--title); font-size: 30px; font-weight: 600; }
.pad button:active { transform: translateY(3px); }
.pad .del { background: #ffe1c7; }

.fleches .cell { font-size: var(--fs, 26px); }
.fleches .cell.void { background: #e9e1d4; }
.fleches .cell.clue { background: #ffe9b8; font-family: var(--font); font-weight: 700; color: #6a4a00; padding: 1px; display: flex; flex-direction: column; }
.fleches .clue-part { flex: 1; width: 100%; display: flex; align-items: center; justify-content: center; position: relative; line-height: 1.05; text-align: center; font-size: var(--cfs, 11px); overflow: hidden; }
.fleches .clue-part + .clue-part { border-top: 1px solid #d9b860; }
.fleches .clue-part .em { font-size: calc(var(--fs, 26px) * .95); }
.fleches .clue-part .arr { position: absolute; right: 1px; bottom: 0; font-size: 11px; color: #a06c00; }
.fleches .cell.inword { background: #efe9ff; }
.fleches .cell.cur { background: #c9b8ff; }
.fleches .clue-part.active { background: #ffd36b; }
.fleches .cell.wrong { color: var(--bad); }
.fleches .cell.right { color: var(--ok); }
.clue-banner { display: flex; align-items: center; gap: 14px; justify-content: center; min-height: 72px; font-size: 22px; font-weight: 700; text-align: center; }
.clue-banner .em { font-size: 46px; }

.meles .cell { font-size: var(--fs, 24px); background: #fff; transition: background .1s; }
.meles .cell.drag { background: #ffe58a; }
.meles .board { position: relative; }
.wordlist { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.wordlist span { background: var(--card); box-shadow: var(--shadow); border-radius: 14px; padding: 6px 12px; font-weight: 700; font-size: 18px; }
.wordlist span.found { text-decoration: line-through; opacity: .5; box-shadow: none; background: var(--ok-bg); }

/* ---------- Créatures ---------- */
.draw-area { position: relative; width: 100%; max-width: 620px; margin: 0 auto; aspect-ratio: 1; background: #fff; border-radius: 24px; box-shadow: var(--shadow); overflow: hidden; touch-action: none; }
.draw-area canvas { width: 100%; height: 100%; display: block; touch-action: none; }
.palette { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.palette .sw { width: 44px; height: 44px; border-radius: 50%; box-shadow: 0 3px 0 rgba(0,0,0,.15); border: 3px solid #fff; }
.palette .sw.sel { outline: 4px solid var(--primary); }
.palette .tool { width: 50px; height: 50px; border-radius: 14px; background: var(--card); box-shadow: var(--shadow); font-size: 24px; }
.palette .tool.sel { outline: 4px solid var(--primary); }
.types { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.types button { padding: 8px 12px; border-radius: 14px; background: var(--card); box-shadow: var(--shadow); font-size: 17px; font-weight: 700; }
.types button.sel { outline: 4px solid var(--primary); }
.text-input { width: 100%; max-width: 420px; height: 58px; border-radius: 16px; border: 3px solid var(--line); padding: 0 16px; font-size: 22px; background: #fff; color: var(--ink); }

.creature-card {
  width: min(92vw, 380px); border-radius: 26px; padding: 12px; margin: 0 auto;
  background: linear-gradient(160deg, var(--t1, #ffd166), var(--t2, #f77f00));
  box-shadow: 0 14px 40px rgba(0,0,0,.25); color: #2b2340;
}
.creature-card .inner { background: #fffaf0; border-radius: 18px; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.creature-card .head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.creature-card .cname { font-family: var(--title); font-size: 26px; font-weight: 700; line-height: 1.1; }
.creature-card .pv { font-family: var(--title); font-weight: 700; color: #c1121f; white-space: nowrap; }
.creature-card .pic { width: 100%; aspect-ratio: 1; border-radius: 14px; background: #fff; object-fit: contain; border: 4px solid var(--t1, #ffd166); }
.creature-card .ctype { display: inline-block; padding: 3px 10px; border-radius: 999px; background: var(--t1); font-weight: 700; font-size: 15px; }
.creature-card .desc { font-size: 15px; font-style: italic; color: #5b5370; }
.creature-card .atk { display: flex; justify-content: space-between; gap: 8px; border-top: 2px dashed #e7dcc6; padding-top: 6px; }
.creature-card .atk b { font-family: var(--title); font-size: 18px; }
.creature-card .atk small { display: block; color: #6b6380; font-size: 13px; }
.creature-card .dmg { font-family: var(--title); font-weight: 700; font-size: 22px; }
.creature-card .foot { display: flex; justify-content: space-between; font-size: 13px; color: #6b6380; }
.album { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; }
.album button { background: var(--card); border-radius: 18px; box-shadow: var(--shadow); padding: 8px; display: flex; flex-direction: column; gap: 4px; align-items: center; }
.album img { width: 100%; aspect-ratio: 1; object-fit: contain; border-radius: 12px; background: #fff; }
.album b { font-family: var(--title); }
.loader { width: 70px; height: 70px; border-radius: 50%; border: 8px solid var(--line); border-top-color: var(--primary); animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.magic { font-size: 70px; animation: float 1.6s ease-in-out infinite; }
@keyframes float { 50% { transform: translateY(-14px) rotate(8deg); } }

/* ---------- Espace parent ---------- */
.parent section { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; display: flex; flex-direction: column; gap: 12px; }
.parent h2 { margin: 0; font-family: var(--title); font-size: 24px; }
.parent h3 { margin: 6px 0 0; font-family: var(--title); font-size: 19px; }
.parent label { display: flex; flex-direction: column; gap: 6px; font-weight: 700; }
.parent .inline { flex-direction: row; align-items: center; gap: 10px; }
.parent select, .parent input[type=text], .parent input[type=password], .parent input[type=number] { height: 50px; border-radius: 14px; border: 2px solid var(--line); padding: 0 12px; font-size: 18px; background: #fff; color: var(--ink); }
.parent input[type=range] { width: 100%; }
.parent input[type=checkbox] { width: 26px; height: 26px; }
.stat-table { width: 100%; border-collapse: collapse; font-size: 16px; }
.stat-table th, .stat-table td { padding: 8px 6px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.stat-table th { color: var(--muted); font-weight: 700; font-size: 14px; }
.errs { color: var(--bad); font-size: 15px; }
.pin-dots { display: flex; gap: 14px; justify-content: center; font-size: 34px; }

.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 90; }

@media (max-width: 520px) {
  body { font-size: 17px; }
  .games { grid-template-columns: repeat(2, 1fr); gap: 12px; }
  .game-tile { min-height: 140px; }
  .game-tile .emoji { font-size: 46px; }
  .game-tile .label { font-size: 18px; }
  .icon-btn { width: 46px; height: 46px; font-size: 22px; }
  .spacer { width: 46px; }
  .numpad button { height: 60px; }
}
