:root {
  --wm-cream: #fff8e6;
  --wm-cream-warm: #f6e7c8;
  --wm-wood: #c98f5a;
  --wm-wood-dark: #8a552c;
  --wm-gold: #f2c141;
  --wm-ink: #3d3226;
  --wm-plum: #8d6197;
  --world-water: #6eaeb1;
  --world-coral: #df826d;
  --safe-top: max(10px, env(safe-area-inset-top));
  --safe-right: max(10px, env(safe-area-inset-right));
  --safe-bottom: max(10px, env(safe-area-inset-bottom));
  --safe-left: max(10px, env(safe-area-inset-left));
}

* { box-sizing: border-box; }
html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; }
body {
  font-family: "Trebuchet MS", "Arial Rounded MT Bold", system-ui, sans-serif;
  color: var(--wm-ink);
  background: #eac47d;
  touch-action: manipulation;
  user-select: none;
}
button { font: inherit; color: inherit; -webkit-tap-highlight-color: transparent; }
button:focus-visible { outline: 5px solid #fff; outline-offset: 3px; box-shadow: 0 0 0 9px var(--wm-plum); }
.hidden { display: none !important; }
.shell { position: fixed; inset: 0; isolation: isolate; overflow: hidden; }
.scene { position: absolute; inset: 0; }
.scene > picture, .scene > picture img { position: absolute; inset: 0; width: 100%; height: 100%; }
.scene > picture img { object-fit: cover; }
.rune {
  position: fixed; z-index: 100; left: var(--safe-left); bottom: var(--safe-bottom);
  width: clamp(50px, 6vw, 76px); pointer-events: none;
  filter: drop-shadow(0 4px 6px rgba(75,45,22,.28));
}

/* Opening story */
.intro::after { content: ""; position: absolute; inset: 55% 0 0; background: linear-gradient(transparent, rgba(74,43,20,.19)); pointer-events: none; }
.play-btn {
  position: absolute; z-index: 3; left: 50%; bottom: max(28px, calc(env(safe-area-inset-bottom) + 18px));
  width: clamp(88px, 9vw, 112px); height: clamp(88px, 9vw, 112px); transform: translateX(-50%);
  border: 4px solid #8a552c; border-radius: 50%; color: #fff8e6; font-size: 44px; line-height: 1;
  background: linear-gradient(#d9ad76, #a96c3a); box-shadow: inset 0 6px rgba(255,255,255,.4), inset 0 -8px #74421f, 0 10px 22px rgba(58,34,17,.3);
  cursor: pointer;
}
.play-btn:active { transform: translateX(-50%) translateY(4px) scale(.96); box-shadow: inset 0 3px rgba(255,255,255,.3), inset 0 -4px #74421f, 0 5px 10px rgba(58,34,17,.24); }

/* Game shell */
.game { padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left); }
.game-content { position: relative; z-index: 2; height: 100%; display: grid; grid-template-rows: 72px minmax(0,1fr); gap: 9px; }
.hud { display: grid; grid-template-columns: minmax(150px,1fr) auto minmax(200px,1fr); align-items: center; gap: 10px; }
.progress, .level-path {
  display: flex; align-items: center; justify-content: center; gap: 7px; min-height: 54px; padding: 6px 12px;
  border: 3px solid rgba(138,85,44,.72); border-radius: 28px; background: rgba(255,248,230,.88);
  box-shadow: 0 5px 13px rgba(79,49,25,.2);
}
.progress { justify-self: start; }
.progress-dot { width: 28px; height: 28px; border: 3px solid #9b6a3d; border-radius: 50%; background: #f6e7c8; box-shadow: inset 0 3px rgba(255,255,255,.55); }
.progress-dot.done { background: var(--wm-gold); box-shadow: inset 0 3px rgba(255,255,255,.6), 0 0 9px rgba(242,193,65,.6); }
.level-node { width: 34px; height: 34px; display: grid; place-items: center; border: 3px solid #9b6a3d; border-radius: 50%; background: #ead4ac; font-weight: 900; opacity: .68; }
.level-node.done { background: #f3d477; opacity: .9; }
.level-node.current { width: 42px; height: 42px; background: var(--wm-gold); opacity: 1; transform: translateY(-2px); box-shadow: 0 0 10px rgba(242,193,65,.8); }
.hud-controls { justify-self: end; display: flex; gap: 8px; }
.wood-btn {
  min-width: 56px; min-height: 56px; width: 58px; height: 58px; border: 3px solid var(--wm-wood-dark); border-radius: 50%;
  background: linear-gradient(#dcb17c, #ad7040); box-shadow: inset 0 4px rgba(255,255,255,.45), inset 0 -5px #774622, 0 5px 10px rgba(70,42,21,.22);
  color: #fff8e6; font-size: 27px; font-weight: 900; cursor: pointer;
}
.wood-btn:active { transform: translateY(3px) scale(.96); box-shadow: inset 0 2px rgba(255,255,255,.3), inset 0 -2px #774622, 0 2px 5px rgba(70,42,21,.2); }

.play-grid { min-height: 0; display: grid; grid-template-columns: minmax(155px,20%) minmax(430px,1fr) minmax(150px,20%); align-items: center; gap: 12px; }
.guide-zone, .hero-zone { min-width: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.speech {
  position: relative; width: min(100%, 250px); min-height: 86px; display: flex; align-items: center; justify-content: center;
  padding: 13px 16px; border: 4px solid #bc864e; border-radius: 44% 40% 45% 42% / 42% 46% 40% 45%;
  background: rgba(255,248,230,.96); box-shadow: 0 7px 16px rgba(69,43,22,.2); text-align: center; font-size: clamp(15px,1.45vw,21px); font-weight: 900; line-height: 1.12;
}
.speech::after { content: ""; position: absolute; left: 44%; bottom: -18px; width: 30px; height: 28px; background: #fff8e6; border-left: 4px solid #bc864e; border-bottom: 4px solid #bc864e; transform: rotate(-28deg) skew(-8deg); border-bottom-left-radius: 20px; }
.speech.glow { animation: speechGlow .8s ease-in-out 2; }
.guide { position: relative; z-index: 1; width: clamp(128px,13vw,185px); max-height: 190px; object-fit: contain; cursor: pointer; filter: drop-shadow(0 7px 8px rgba(58,35,18,.25)); animation: float 3.5s ease-in-out infinite; }
.guide:focus-visible { outline: 5px solid #fff; outline-offset: 3px; border-radius: 42%; box-shadow: 0 0 0 9px var(--wm-plum); }
.hero { width: min(100%, 250px); max-height: 330px; object-fit: contain; filter: drop-shadow(0 9px 8px rgba(58,35,18,.25)); transform-origin: 50% 75%; animation: breathe 3.6s ease-in-out infinite; }
.hero.happy { animation: happy .75s ease; }
.hero.oops { animation: wobble .55s ease; }

.board {
  position: relative; min-width: 0; min-height: 0; height: min(100%, 590px); padding: 15px 18px 12px;
  border: 5px solid var(--wm-wood-dark); border-radius: 34px; background: rgba(255,248,230,.93);
  box-shadow: inset 0 0 0 7px rgba(201,143,90,.35), 0 12px 25px rgba(65,39,20,.25);
  display: grid; grid-template-rows: auto minmax(0,1fr) auto auto; gap: 8px; overflow: hidden;
}
.prompt { min-height: 32px; text-align: center; font-size: clamp(18px,2vw,26px); font-weight: 900; line-height: 1.05; }
.math-stage { min-height: 0; display: flex; align-items: center; justify-content: center; gap: clamp(8px,2vw,24px); padding: 2px; }
.group-wrap { display: flex; flex-direction: column; align-items: center; gap: 5px; }
.group-label { min-height: 28px; color: #80542f; font-size: clamp(24px,3vw,40px); font-weight: 900; }
.object-group { min-width: 105px; min-height: 112px; max-width: 230px; padding: 10px; display: flex; flex-wrap: wrap; align-content: center; justify-content: center; gap: 4px; border: 3px dashed rgba(151,96,49,.55); border-radius: 26px; background: rgba(246,231,200,.72); }
.shell-object { width: clamp(38px,4.2vw,61px); height: clamp(38px,4.2vw,61px); object-fit: contain; filter: drop-shadow(0 3px 2px rgba(74,45,23,.2)); }
.pebble { width: clamp(34px,4vw,56px); height: clamp(26px,3vw,43px); border: 3px solid #9e5e46; border-radius: 50%; background: radial-gradient(circle at 38% 28%, #f5b293, #da7e67 62%, #b75d4b); box-shadow: inset 0 3px rgba(255,255,255,.38), 0 3px 3px rgba(79,44,27,.2); }
.pebble.added { animation: pebbleArrive .58s ease both; }
.math-sign { color: #87552e; font-size: clamp(34px,5vw,64px); font-weight: 900; }
.change-arrow { font-size: clamp(42px,5vw,66px); color: var(--world-coral); }
.change-object { transform: translateY(-8px); animation: join 1.2s ease-in-out infinite; }

.mats { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 12px; width: 100%; }
.mat { min-width: 120px; min-height: 104px; padding: 11px; border: 5px solid #c89a63; border-radius: 50%; background: repeating-radial-gradient(circle at center,#f6e5c0 0 8px,#ead1a2 9px 11px); display: flex; flex-wrap: wrap; align-content: center; justify-content: center; gap: 5px; transition: transform .18s, box-shadow .18s; }
.selectable-mat { cursor: pointer; }
.selectable-mat.selected, .choice-card.selected { transform: translateY(-4px) scale(1.03); border-color: var(--wm-gold); box-shadow: 0 0 0 6px rgba(242,193,65,.32), 0 7px 12px rgba(77,45,22,.2); }
.equal-options { display: grid; grid-template-columns: repeat(3,minmax(105px,1fr)); gap: 8px; width: 100%; }
.choice-card { min-height: 120px; padding: 8px; border: 4px solid #b77a45; border-radius: 24px; background: #fff8e6; cursor: pointer; transition: transform .18s, box-shadow .18s; }
.choice-card .tiny-mats { display: flex; justify-content: center; gap: 4px; }
.choice-card .mat { min-width: 58px; min-height: 68px; border-width: 3px; padding: 5px; gap: 2px; }
.choice-card .pebble { width: 22px; height: 17px; border-width: 2px; }
.hint { animation: hint 1s ease-in-out infinite !important; }

.answers { min-height: 76px; display: flex; align-items: center; justify-content: center; gap: 10px; }
.answer-card {
  width: clamp(64px,7vw,88px); height: clamp(64px,7vw,88px); border: 4px solid #a86a38; border-radius: 24px;
  background: linear-gradient(#fff8e6,#f0d9ad); box-shadow: inset 0 4px rgba(255,255,255,.75), inset 0 -5px rgba(138,85,44,.18), 0 5px 9px rgba(65,39,19,.18);
  font-size: clamp(30px,4vw,46px); font-weight: 900; cursor: pointer; transition: transform .16s, box-shadow .16s;
}
.answer-card.selected { transform: translateY(-5px) scale(1.05); border-color: var(--wm-gold); box-shadow: 0 0 0 6px rgba(242,193,65,.36), 0 8px 12px rgba(65,39,19,.2); }
.submit-row { min-height: 72px; display: flex; align-items: center; justify-content: center; }
.confirm-btn {
  width: 70px; height: 70px; border: 0; border-radius: 50%; background: url('ui/button-check-normal.webp') center/contain no-repeat;
  filter: drop-shadow(0 5px 4px rgba(58,36,18,.25)); cursor: pointer;
}
.confirm-btn:disabled { background-image: url('ui/button-check-disabled.webp'); filter: grayscale(.82) saturate(.24); opacity: .52; cursor: default; }
.confirm-btn:not(:disabled):active { background-image: url('ui/button-check-pressed.webp'); transform: translateY(3px) scale(.96); }
.confirm-btn.demo-focus { animation: hint .85s ease-in-out infinite; }
body[data-state="menu"] .confirm-btn,
body[data-state="level-complete"] .confirm-btn,
body[data-state="game-complete"] .confirm-btn,
body[data-state="menu"] .skip-demo,
body[data-state="level-complete"] .skip-demo,
body[data-state="game-complete"] .skip-demo { visibility: hidden; }
.skip-demo { position: absolute; z-index: 4; right: 14px; bottom: 14px; min-width: 52px; min-height: 52px; border: 3px solid #8a552c; border-radius: 50%; background: #d8ad78; color: #fff8e6; font-size: 25px; font-weight: 900; cursor: pointer; }
.feedback { position: absolute; z-index: 5; left: 50%; top: 46%; transform: translate(-50%,-50%) scale(.9); min-width: 180px; padding: 14px 20px; border: 4px solid #b77a45; border-radius: 28px; background: #fff8e6; box-shadow: 0 10px 25px rgba(60,37,19,.26); text-align: center; font-size: 23px; font-weight: 900; opacity: 0; pointer-events: none; }
.feedback.show { animation: feedback .9s ease both; }

/* Overlays */
.overlay { position: fixed; z-index: 50; inset: 0; display: grid; place-items: center; padding: 18px; background: rgba(62,39,21,.48); backdrop-filter: blur(4px); }
.panel { position: relative; width: min(92vw,720px); max-height: 92vh; overflow: hidden; padding: 24px; border: 6px solid #8a552c; border-radius: 38px; background: rgba(255,248,230,.98); box-shadow: inset 0 0 0 8px rgba(201,143,90,.35), 0 18px 45px rgba(45,28,14,.38); text-align: center; }
.panel h2 { margin: 0 0 8px; font-size: clamp(28px,4vw,46px); }
.panel p { margin: 5px 0 10px; font-size: clamp(16px,2.2vw,23px); font-weight: 800; }
.level-menu { display: flex; justify-content: center; gap: 9px; margin: 18px 0; }
.menu-level { width: 64px; height: 64px; border: 4px solid #8a552c; border-radius: 50%; background: #f2c141; font-size: 28px; font-weight: 900; cursor: pointer; }
.menu-level:disabled { filter: grayscale(1); opacity: .42; cursor: default; }
.panel-actions { display: flex; justify-content: center; gap: 13px; }
.panel-actions .wood-btn { width: 70px; height: 70px; }
.highfive { width: min(68vw,370px); max-height: 38vh; object-fit: contain; filter: drop-shadow(0 8px 7px rgba(76,44,19,.22)); }
.reward-star { width: clamp(58px,8vw,88px); filter: drop-shadow(0 5px 5px rgba(84,52,22,.22)); }
.finale .panel { border-color: var(--wm-gold); background: radial-gradient(circle at 50% 32%,#fffdf0,#f9e0ad 68%,#e8bc70); }
.confetti { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }
.confetti i { position: absolute; top: -20px; width: 10px; height: 18px; border-radius: 4px; background: var(--wm-gold); animation: fall 3s linear infinite; }

@keyframes float { 0%,100%{transform:translateY(0) rotate(-1deg)} 50%{transform:translateY(-7px) rotate(1deg)} }
@keyframes breathe { 0%,100%{transform:scale(1)} 50%{transform:scale(1.025)} }
@keyframes happy { 0%,100%{transform:translateY(0) scale(1)} 45%{transform:translateY(-18px) scale(1.06)} }
@keyframes wobble { 0%,100%{transform:rotate(0)} 25%{transform:rotate(-4deg)} 65%{transform:rotate(4deg)} }
@keyframes hint { 0%,100%{filter:drop-shadow(0 0 0 rgba(242,193,65,0))} 50%{filter:drop-shadow(0 0 12px #f2c141)} }
@keyframes speechGlow { 50%{box-shadow:0 0 0 8px rgba(242,193,65,.32),0 7px 16px rgba(69,43,22,.2)} }
@keyframes feedback { 0%{opacity:0;transform:translate(-50%,-50%) scale(.8)} 20%,75%{opacity:1;transform:translate(-50%,-50%) scale(1)} 100%{opacity:0;transform:translate(-50%,-58%) scale(1)} }
@keyframes join { 0%,100%{transform:translateY(-8px)} 50%{transform:translate(-10px,4px)} }
@keyframes pebbleArrive { 0%{opacity:0;transform:translateY(-22px) scale(.7)} 70%{opacity:1;transform:translateY(3px) scale(1.08)} 100%{opacity:1;transform:none} }
@keyframes fall { to{transform:translateY(105vh) rotate(450deg)} }

@media (max-width: 760px), (orientation: portrait) {
  .game { padding: max(6px,env(safe-area-inset-top)) max(7px,env(safe-area-inset-right)) max(6px,env(safe-area-inset-bottom)) max(7px,env(safe-area-inset-left)); }
  .game-content { grid-template-rows: 105px minmax(0,1fr); gap: 5px; }
  .hud { grid-template-columns: 1fr auto; grid-template-rows: 54px 46px; gap: 4px 7px; }
  .hud-controls { grid-column: 2; grid-row: 1; }
  .progress { grid-column: 1; grid-row: 1; justify-self: start; min-height: 50px; padding: 5px 8px; gap: 4px; }
  .progress-dot { width: 23px; height: 23px; border-width: 2px; }
  .level-path { grid-column: 1 / 3; grid-row: 2; min-height: 42px; padding: 3px 8px; gap: 5px; }
  .level-node { width: 30px; height: 30px; font-size: 13px; }
  .level-node.current { width: 36px; height: 36px; }
  .wood-btn { width: 48px; height: 48px; min-width: 48px; min-height: 48px; font-size: 21px; border-width: 2px; }
  .play-grid { display: grid; grid-template-columns: minmax(0,1fr) 78px; grid-template-rows: 92px minmax(0,1fr); gap: 4px; align-items: center; }
  .guide-zone { grid-column: 1 / 3; grid-row: 1; flex-direction: row; justify-content: center; align-items: center; gap: 4px; }
  .speech { order: 2; width: min(70vw,280px); min-height: 68px; padding: 9px 12px; font-size: 15px; }
  .speech::after { left: -15px; bottom: 17px; transform: rotate(45deg); width: 25px; height: 25px; }
  .guide { order: 1; width: 78px; height: 78px; }
  .board { grid-column: 1 / 3; grid-row: 2; height: 100%; padding: 9px 9px 7px; border-width: 4px; border-radius: 26px; grid-template-rows: auto minmax(0,1fr) auto auto; gap: 3px; }
  .prompt { min-height: 23px; font-size: 17px; }
  .hero-zone { position: fixed; z-index: 4; right: max(8px,env(safe-area-inset-right)); bottom: max(5px,env(safe-area-inset-bottom)); pointer-events: none; }
  .hero { width: 92px; height: 92px; }
  .math-stage { gap: 5px; }
  .object-group { min-width: 82px; min-height: 78px; max-width: 148px; padding: 5px; border-radius: 19px; }
  .shell-object { width: clamp(29px,8vw,42px); height: clamp(29px,8vw,42px); }
  .math-sign { font-size: 33px; }
  .answers { min-height: 58px; gap: 7px; padding-right: 64px; }
  .answer-card { width: 58px; height: 58px; border-radius: 19px; font-size: 28px; }
  .submit-row { min-height: 58px; padding-right: 64px; }
  .confirm-btn { width: 58px; height: 58px; }
  .mats { gap: 5px; }
  .mat { min-width: 90px; min-height: 72px; padding: 6px; border-width: 4px; }
  .pebble { width: 31px; height: 23px; border-width: 2px; }
  .equal-options { gap: 5px; grid-template-columns: repeat(3,1fr); }
  .choice-card { min-height: 92px; padding: 4px; border-radius: 17px; border-width: 3px; }
  .choice-card .mat { min-width: 38px; min-height: 52px; padding: 3px; }
  .choice-card .pebble { width: 16px; height: 12px; }
  .panel { width: min(96vw,620px); padding: 18px 14px; }
  .highfive { width: min(82vw,330px); max-height: 34vh; }
  .rune { width: 50px; }
}

@media (max-height: 700px) and (orientation: portrait) {
  .game-content { grid-template-rows: 96px minmax(0,1fr); }
  .hud { grid-template-rows: 49px 42px; }
  .play-grid { grid-template-rows: 76px minmax(0,1fr); }
  .guide { width: 65px; height: 65px; }
  .speech { min-height: 58px; font-size: 13px; }
  .hero { width: 72px; height: 72px; }
  .object-group { min-height: 66px; }
  .shell-object { width: 29px; height: 29px; }
  .mat { min-height: 62px; }
  .answers { min-height: 51px; }
  .answer-card { width: 50px; height: 50px; min-width: 50px; min-height: 50px; font-size: 24px; border-width: 3px; }
  .submit-row { min-height: 51px; }
  .confirm-btn { width: 56px; height: 56px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; scroll-behavior: auto !important; }
}
