:root {
  --bg: #fdf3e1;
  --bg2: #f8e2bd;
  --ink: #3b3040;
  --soft: #6d6070;
  --card: #fffaf0;
  --primary: #e0558a;
  --primary-dark: #a8336a;
  --yellow: #ffcf2e;
  --green: #3a9a5a;
  --red: #d64545;
  --light: #f6e7c8;
  --dark: #c49a6c;
  --line: #d9c3a0;
  --wc: #e0558a;
  --font: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  --bs: min(calc(100vw - 44px), calc(100dvh - 250px), 620px);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
html { background: var(--bg2); }
body {
  background: radial-gradient(circle at 50% 0%, #fff7e8, var(--bg) 60%, var(--bg2));
  background-attachment: fixed;
  color: var(--ink);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0.3em 0; }

button {
  font: inherit;
  color: inherit;
  min-height: 44px;
  min-width: 44px;
  border-radius: 14px;
  border: 2px solid #00000022;
  background: var(--card);
  padding: 6px 16px;
  cursor: pointer;
  box-shadow: 0 3px 0 #00000022;
  transition: transform 0.08s;
}
button:active { transform: translateY(2px); box-shadow: 0 1px 0 #00000022; }
button:focus-visible { outline: 4px solid #1b6fd1; outline-offset: 2px; }
button.primary { background: var(--primary); color: #fff; border-color: var(--primary-dark); box-shadow: 0 4px 0 var(--primary-dark); font-weight: bold; }
button.big { font-size: 1.25rem; padding: 12px 24px; min-width: 260px; }
button.small { font-size: 0.9rem; min-height: 38px; padding: 4px 12px; }
button.icon { font-size: 1.3rem; padding: 0; width: 46px; }
button.danger { color: var(--red); }
button.soft { background: #fff; }
.row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

.screen { min-height: 100dvh; display: flex; flex-direction: column; align-items: center; padding: 0 0 20px; }

.bar {
  width: 100%;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 8px max(10px, env(safe-area-inset-left));
  background: #ffffffaa;
  border-bottom: 3px solid var(--wc);
  position: sticky;
  top: 0;
  z-index: 5;
  backdrop-filter: blur(6px);
}
.bar h2 { text-align: center; font-size: 1.25rem; }
.bar .middle { display: flex; flex-direction: column; align-items: center; min-width: 0; overflow: hidden; }
.bar .middle #lv-name { font-weight: bold; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.bar .tools { display: flex; gap: 6px; }
.pill { background: var(--card); border-radius: 20px; padding: 4px 12px; font-weight: bold; border: 2px solid #00000018; white-space: nowrap; }
.counter { display: flex; gap: 12px; align-items: center; font-size: 0.95rem; color: var(--soft); white-space: nowrap; max-width: 100%; overflow: hidden; }
.counter .ct:empty { display: none; }
@media (max-width: 480px) { .counter { gap: 8px; font-size: 0.85rem; } .bar .tools { gap: 3px; } .bar .tools button.icon { width: 40px; } }
.counter .ct { display: inline-flex; align-items: center; gap: 4px; }
.counter svg { width: 22px; height: 22px; }

/* ---- title ---- */
#title { justify-content: center; gap: 18px; padding: 20px 16px; }
.hero { text-align: center; }
.hero-pip { width: 150px; height: 150px; margin: 0 auto; animation: bob 2.4s ease-in-out infinite; }
.hero-pip svg { width: 100%; height: 100%; }
.hero h1 { font-size: clamp(2.2rem, 8vw, 3.4rem); color: var(--primary-dark); letter-spacing: 1px; text-shadow: 0 3px 0 #fff; }
.tagline { font-size: 1.15rem; color: var(--soft); margin: 0; }
.menu { display: flex; flex-direction: column; gap: 12px; align-items: center; }
.grownups { max-width: 460px; color: var(--soft); font-size: 0.9rem; text-align: center; }
.grownups summary { cursor: pointer; padding: 8px; }
.switch { display: flex; gap: 8px; align-items: center; justify-content: center; margin: 10px 0; cursor: pointer; }
.switch input { width: 22px; height: 22px; }
@keyframes bob { 50% { transform: translateY(-8px); } }

/* ---- map ---- */
.map-note { color: var(--soft); margin: 10px 0 4px; font-weight: bold; }
.ladder { display: flex; flex-direction: column; width: min(520px, 94vw); border: 6px solid #7a5030; border-radius: 10px; overflow: visible; box-shadow: 0 6px 0 #00000022; }
.rung {
  position: relative;
  display: grid;
  grid-template-columns: 30px 86px 1fr auto;
  align-items: center;
  gap: 10px;
  text-align: left;
  border-radius: 0;
  border: none;
  box-shadow: none;
  padding: 8px 12px;
  min-height: 100px;
}
.rung.lt { background: var(--light); }
.rung.dk { background: var(--dark); }
.rung .rank { font-weight: bold; font-size: 1.3rem; color: #00000066; }
.rung .tile { width: 80px; height: 80px; border-radius: 16px; background: var(--wc); display: grid; place-items: center; box-shadow: inset 0 -5px 0 #00000025; }
.rung .tile svg { width: 66px; height: 66px; }
.rung .tile .lock { width: 40px; height: 40px; color: #ffffffcc; }
.rung .info b { display: block; font-size: 1.2rem; }
.rung .info small { color: #3b3040bb; font-size: 0.95rem; }
.rung.closed { cursor: default; }
.rung.closed .tile { background: #a79e94; }
.rung.closed .info { opacity: 0.6; }
.rung .medal { font-size: 1.8rem; }
.pip-token { position: absolute; right: -24px; top: 50%; width: 70px; height: 70px; transform: translateY(-50%); animation: bob 2s ease-in-out infinite; filter: drop-shadow(0 4px 2px #0003); pointer-events: none; }
.pip-token svg { width: 100%; height: 100%; }
@media (max-width: 560px) { .pip-token { right: 2px; width: 56px; height: 56px; } .rung { padding-right: 56px; } }

.academy { width: min(520px, 94vw); margin-top: 14px; display: flex; flex-direction: column; gap: 12px; }
.academy:empty { display: none; }
.rung.bonus { width: 100%; border-radius: 14px; border: 4px solid var(--wc); background: linear-gradient(135deg, #eef0ff, #dfe3ff); box-shadow: 0 5px 0 #00000022; }
.rung.bonus .rank { color: var(--wc); }
.rung.bonus.closed { background: #ece7df; border-color: #b9b1a6; }

/* ---- world ---- */
.stones { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 16px; width: min(760px, 94vw); padding: 20px 0; }
.stone { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 14px 8px; border-radius: 22px; background: var(--card); border: 3px solid var(--wc); }
.stone .num { width: 58px; height: 58px; border-radius: 50%; background: var(--wc); color: #fff; font-size: 1.6rem; font-weight: bold; display: grid; place-items: center; box-shadow: inset 0 -4px 0 #00000030; }
.stone .num .lock { width: 28px; height: 28px; color: #fff; }
.stone .lt { font-weight: bold; text-align: center; }
.stone.boss { background: linear-gradient(#fff6d6, #ffe9a6); }
.stone.closed { opacity: 0.5; border-color: #aaa; cursor: default; }
.stone.closed .num { background: #a79e94; }
.mini-stars { display: flex; gap: 2px; }
.mini-stars svg { width: 22px; height: 22px; }
.star.on path { fill: var(--yellow); stroke: #a87b00; }
.star.off path { fill: #eee4d4; stroke: #c8b9a3; }

/* ---- level ---- */
#level { gap: 8px; }
.say { display: flex; gap: 10px; align-items: center; width: min(620px, 94vw); margin-top: 8px; }
.say-pip { width: 58px; height: 58px; flex: none; }
.say-pip svg { width: 100%; height: 100%; }
.say p {
  position: relative; margin: 0; flex: 1; background: #fff; border-radius: 18px; padding: 10px 14px; font-size: 1.08rem; line-height: 1.35;
  border: 3px solid var(--wc); min-height: 3.2em; display: flex; align-items: center;
}
.say p::before { content: ""; position: absolute; left: -12px; top: 50%; margin-top: -9px; border: 9px solid transparent; border-right-color: var(--wc); border-left: 0; }
.say.good p { border-color: var(--green); background: #effbf1; }
.say.oops p { border-color: #e8833a; background: #fff3e8; }
.say.pop p { animation: pop 0.3s; }
@keyframes pop { 40% { transform: scale(1.03); } }

.board-wrap { padding: 10px; background: #7a5030; border-radius: 12px; box-shadow: 0 6px 0 #4d3020, 0 10px 20px #0002; touch-action: none; }
.board {
  --cols: 8; --rows: 8; --most: 8;
  position: relative; width: calc(var(--bs) * var(--cols) / var(--most)); height: calc(var(--bs) * var(--rows) / var(--most));
  user-select: none; -webkit-user-select: none; touch-action: none;
}
.board .squares { position: absolute; inset: 0; display: grid; grid-template-columns: repeat(var(--cols), 1fr); grid-template-rows: repeat(var(--rows), 1fr); }
.sq { position: relative; display: grid; place-items: center; }
.sq.light { background: var(--light); }
.sq.dark { background: var(--dark); }
.sq.water { background: repeating-radial-gradient(circle at 30% 40%, #6fc3e6 0 6px, #5ab4dc 6px 12px); }
.sq.water.dark { background: repeating-radial-gradient(circle at 70% 60%, #63b8de 0 6px, #4eaad4 6px 12px); }
.sq > svg { width: 78%; height: 78%; pointer-events: none; }
.sq .gem { animation: twinkle 1.8s ease-in-out infinite; filter: drop-shadow(0 2px 1px #0003); }
@keyframes twinkle { 50% { transform: scale(1.1) rotate(-4deg); } }
.sq .co { position: absolute; font-size: max(9px, calc(var(--bs) / var(--most) / 6)); font-weight: bold; opacity: 0.55; pointer-events: none; }
.sq .co.rk { left: 3px; top: 2px; }
.sq .co.fl { right: 4px; bottom: 1px; }
.sq.last::after { content: ""; position: absolute; inset: 0; background: #ffe45c66; }
.sq.sel::after { content: ""; position: absolute; inset: 0; background: #7ad0ff77; box-shadow: inset 0 0 0 4px #2a8fd6; }
.sq.focus::after { content: ""; position: absolute; inset: 0; box-shadow: inset 0 0 0 5px #2a8fd6; }
.sq.check::after { content: ""; position: absolute; inset: 0; background: radial-gradient(circle, #ff3b3bcc 20%, #ff3b3b00 75%); }
.sq.zone::before { content: ""; position: absolute; inset: 0; background: #8a6cd640; box-shadow: inset 0 0 0 2px #8a6cd680; }
.sq.danger::before { content: ""; position: absolute; inset: 0; background: repeating-linear-gradient(45deg, #ff4d4d40 0 6px, #ff4d4d10 6px 12px); }
.sq.dot::before { content: ""; position: absolute; width: 30%; height: 30%; border-radius: 50%; background: #2a8fd6aa; z-index: 3; }
.sq.ring::before { content: ""; position: absolute; inset: 6%; border-radius: 50%; border: 5px solid #2a8fd6aa; z-index: 3; }
.sq.ok::after { content: "✔"; position: absolute; inset: 0; display: grid; place-items: center; color: #fff; font-size: calc(var(--bs) / var(--most) / 2.2); background: #3a9a5acc; font-weight: bold; }
.sq.bad::after { content: ""; position: absolute; inset: 0; background: #ff3b3b66; }
.sq.nope::after { content: ""; position: absolute; inset: 0; background: #ff3b3b66; animation: nope 0.4s; }
.sq.hintsq::after { content: ""; position: absolute; inset: 4px; border-radius: 10px; box-shadow: 0 0 0 5px var(--yellow), 0 0 18px 6px var(--yellow); animation: pulse 0.6s infinite alternate; z-index: 4; }
.sq.sparkle::after { content: "✨"; position: absolute; inset: 0; display: grid; place-items: center; font-size: calc(var(--bs) / var(--most) / 1.8); animation: sparkle 0.6s forwards; z-index: 4; }
@keyframes nope { 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }
@keyframes pulse { to { opacity: 0.5; } }
@keyframes sparkle { from { transform: scale(0.4); opacity: 1; } to { transform: scale(1.6); opacity: 0; } }

.board .pieces { position: absolute; inset: 0; pointer-events: none; }
.piece { position: absolute; width: calc(100% / var(--cols)); height: calc(100% / var(--rows)); display: grid; place-items: center; z-index: calc(2 + var(--row, 0)); }
.piece svg { width: 90%; height: 90%; filter: drop-shadow(0 3px 1px #0000002a); }
.piece.lifted svg { transform: translateY(-4%) scale(1.06); }
.piece.moving { z-index: 20; }
.piece.dragging { transition: none; z-index: 30; }
/* 3D pieces are taller than a square: their foot sits low in it and the top rises over the square behind */
.piece img.pc3d { position: absolute; left: 0; bottom: 4%; width: 100%; height: auto; pointer-events: none; transition: transform 0.15s; }
.piece.lifted img.pc3d { transform: translateY(-6%) scale(1.06); }
.piece.dragging img, .piece.dragging svg { opacity: 0.35; }
/* the piece being dragged: a copy over the page, its foot under the finger */
.drag-ghost { position: fixed; z-index: 50; pointer-events: none; transform: translate(-50%, calc((var(--foot, 0.12) - 1) * 100%)); filter: drop-shadow(0 12px 8px #0005); }
.drag-ghost > * { display: block; width: 100%; height: auto; }

/* ---- the 3D board: tilted back in perspective, with its pieces standing up on it ---- */
#level.three { perspective: 1100px; perspective-origin: 50% 30%; --tilt3d: 48deg; }
#level.three .board-wrap {
  position: relative; transform: rotateX(var(--tilt3d)); transform-origin: 50% 70%; transform-style: preserve-3d;
  margin: -6% 0 12%; box-shadow: 0 26px 30px #0003;
}
#level.three .board-wrap::after {   /* the board's front edge */
  content: ""; position: absolute; left: 0; right: 0; top: 100%; height: 22px; border-radius: 0 0 10px 10px;
  background: linear-gradient(#5e3b22, #3a2414); transform-origin: top; transform: rotateX(-90deg);
}
#level.three .board, #level.three .board .squares, #level.three .board .sq, #level.three .board .pieces, #level.three .piece { transform-style: preserve-3d; }
#level.three .board .arrows { transform: translateZ(2px); }
/* the board, its grid and its squares lie in one plane, and the browser can't tell which a tap is on:
   lift each layer a hair above the one it sits on, so a tap always finds a square */
#level.three .board { transform: translateZ(0.5px); }
#level.three .board .sq { transform: translateZ(0.5px); }
/* the tilted board's near rows grow past its box: let taps through the bar beneath, except on its buttons */
#level.three .foot { pointer-events: none; position: relative; z-index: 2; }
#level.three .foot > * { pointer-events: auto; }
/* each picture stands up on its bottom edge, set in front of the square's centre so the foot lands there */
#level.three .piece img.pc3d {
  bottom: calc(50% - var(--drop) * 1%); height: calc(var(--ph) * 1%);
  transform-origin: 50% 100%; transform: rotateX(calc(-1 * var(--tilt3d)));
}
#level.three .piece.lifted img.pc3d { transform: rotateX(calc(-1 * var(--tilt3d))) translateY(-10%) scale(1.06); }
#level.three .piece svg.pc { transform-origin: 50% 95%; transform: rotateX(calc(-1 * var(--tilt3d))); }
#level.three .piece.lifted svg.pc { transform: rotateX(calc(-1 * var(--tilt3d))) translateY(-10%) scale(1.06); }
#level.three .sq > svg.gem, #level.three .sq > svg.rock { transform-origin: 50% 90%; transform: rotateX(calc(-1 * var(--tilt3d))); }
img.pc3d { object-fit: contain; display: block; }
.tray-pc img, .promo-btn img, .meet-pc img, .rung .tile img, .pick .pcw img, .counter img { width: 100%; height: 100%; object-fit: contain; }
.rung .tile img { width: 70px; height: 76px; }
.counter img { width: 22px; height: 26px; }
.title-pieces { display: flex; justify-content: center; gap: 6px; height: 92px; }
.title-pieces > * { width: 58px; height: 92px; object-fit: contain; }
#pieces-toggle.busy::after { content: " …"; }
.piece.dragging svg { filter: drop-shadow(0 10px 6px #0004); }
.piece.gone { z-index: 1; pointer-events: none; }
.board .arrows { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 25; }
.arrow { stroke-width: 0.16; stroke-linecap: round; opacity: 0.85; }
.arrow.hint { stroke: #2a8fd6; }
.arrow.bad { stroke: var(--red); }
.ah.hint { fill: #2a8fd6; }
.ah.bad { fill: var(--red); }
.arrow.guard { stroke: var(--green); }
.ah.guard { fill: var(--green); }
.quiz-btn { font-size: 1.2rem; font-weight: bold; min-width: 150px; padding: 10px 18px; }
.quiz-btn.yes { background: #e6f6ea; border-color: var(--green); color: #226b3a; }
.quiz-btn.no { background: #fdeaea; border-color: var(--red); color: #9b2c2c; }

.foot { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; min-height: 50px; align-items: center; }
.tray { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; }
.tray-pc { position: relative; width: min(62px, calc((100vw - 70px) / 6)); height: min(62px, calc((100vw - 70px) / 6)); padding: 2px; min-width: 0; }
.tray { flex-wrap: nowrap; }
.tray-pc svg { width: 100%; height: 100%; }
.tray-pc.on { background: #7ad0ff; border-color: #2a8fd6; }
.tray-pc .n { position: absolute; right: -6px; top: -6px; background: var(--primary); color: #fff; border-radius: 12px; font-size: 0.8rem; padding: 1px 7px; font-weight: bold; }
.tray-pc:disabled { opacity: 0.3; }

/* ---- modal ---- */
.modal { position: fixed; inset: 0; background: #3b304088; display: grid; place-items: center; z-index: 20; padding: 16px; animation: fade 0.2s; }
.card { background: var(--card); border-radius: 26px; padding: 20px 24px; max-width: 460px; width: 100%; text-align: center; box-shadow: 0 10px 0 #00000025; animation: rise 0.3s; max-height: 92dvh; overflow: auto; }
.card p { font-size: 1.1rem; line-height: 1.4; }
.card h2 { color: var(--primary-dark); font-size: 1.7rem; }
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(30px) scale(0.95); opacity: 0; } }
.stars-row { display: flex; justify-content: center; gap: 8px; margin: 8px 0; }
.big-star { width: 76px; height: 76px; animation: starIn 0.5s backwards cubic-bezier(.3, 1.6, .5, 1); }
.big-star svg { width: 100%; height: 100%; }
@keyframes starIn { from { transform: scale(0) rotate(-90deg); opacity: 0; } }
.checks { list-style: none; padding: 0; text-align: left; display: inline-block; margin: 6px 0 14px; }
.checks li { margin: 4px 0; font-size: 1.05rem; }
.checks li.ok { color: var(--green); }
.checks li.no { color: var(--soft); }
.meet-top { display: flex; align-items: center; justify-content: center; gap: 10px; }
.meet-pc { width: 80px; height: 80px; }
.meet-pc svg { width: 100%; height: 100%; }
.meet-board { margin: 6px auto; border: 5px solid #7a5030; border-radius: 6px; box-sizing: content-box; --bs: min(300px, 70vw); width: var(--bs); }
.promo-row { display: flex; justify-content: center; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.promo-btn { width: 80px; height: 80px; padding: 4px; }
.promo-btn svg { width: 100%; height: 100%; }
.badge-big { width: 150px; margin: 0 auto; }
.badge-big svg { width: 100%; }

/* ---- badges ---- */
.shelf { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 20px; width: min(720px, 94vw); padding: 24px 0; }
.shelf-item { display: flex; flex-direction: column; align-items: center; gap: 6px; text-align: center; }
.shelf-item svg { width: 110px; }
.shelf-item.dim b { color: var(--soft); }

/* ---- free play ---- */
.free { display: flex; flex-direction: column; align-items: center; gap: 10px; width: min(640px, 94vw); padding: 14px 0; }
.picks { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; }
.pick { display: flex; flex-direction: column; align-items: center; gap: 2px; width: 160px; padding: 12px 8px; border-width: 3px; }
.pick.on { border-color: var(--primary); background: #ffe6f0; }
.pick .emoji { font-size: 2.6rem; }
.pick .pcw { width: 56px; height: 56px; }
.pick .pcw svg { width: 100%; height: 100%; }
.pick small { color: var(--soft); }

/* ---- confetti ---- */
.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 30; overflow: hidden; }
.confetti i { position: absolute; top: -20px; width: 10px; height: 16px; border-radius: 3px; animation: fall linear forwards; }
@keyframes fall { to { transform: translate(var(--drift), 110vh) rotate(720deg); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01s !important; animation-iteration-count: 1 !important; transition-duration: 0.01s !important; }
}
