@font-face{font-family:Fredoka;font-style:normal;font-weight:400;font-display:swap;src:url('assets/fredoka-400.woff2') format('woff2')}
@font-face{font-family:Fredoka;font-style:normal;font-weight:600;font-display:swap;src:url('assets/fredoka-600.woff2') format('woff2')}

:root{
  --wood:#7a4a24;--wood-dark:#4d2c14;--cream:#f7e8c8;--plum:#3b1f33;
  --amber:#ffc061;--amber-deep:#e8903a;--ink:#2a1622;--radius:18px;
  --pad:clamp(6px,1.4vmin,14px);
  --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}
html,body{height:100%;margin:0}
body{
  font-family:Fredoka,system-ui,sans-serif;color:var(--cream);background:var(--plum);
  overflow:hidden;-webkit-text-size-adjust:100%;touch-action:manipulation;
}
button{font:inherit;color:inherit;border:0;background:none;cursor:pointer}
img{display:block;max-width:100%}
[hidden]{display:none!important}

/* Canonical rune: bottom-LEFT corner on every screen and in every dialog. */
.rune{position:fixed;left:calc(8px + var(--safe-l));bottom:calc(8px + var(--safe-b));width:46px;opacity:.9;pointer-events:none;z-index:60}
dialog .rune{position:static;left:auto;right:auto;bottom:auto;display:block;margin:12px 0 0;width:40px}

/* -------------------------------------------------------------- opening */
#opening{position:fixed;inset:0}
#opening picture{position:absolute;inset:0;display:block}
#opening img{width:100%;height:100%;object-fit:cover}
/* The finished three-second loop sits on top of the still in landscape only:
   it is muted, looping and inline, so it can start on its own. */
#opening video{position:absolute;inset:0;width:100%;height:100%;object-fit:cover;background:#3b1f33}
.opening-title{position:absolute;left:50%;top:7%;transform:translateX(-50%);width:min(94vw,900px);text-align:center;text-shadow:0 3px 14px #2a1622dd,0 1px 3px #2a1622cc}
.opening-title h1{margin:0;font-size:clamp(1.8rem,6.4vw,4rem);line-height:1.04}
.opening-title p{margin:6px 0 0;font-size:clamp(.9rem,2.6vw,1.4rem);font-weight:600}
#play{position:absolute;left:50%;bottom:calc(8% + var(--safe-b));transform:translateX(-50%);width:clamp(96px,17vw,144px);aspect-ratio:1;display:grid;place-items:center}
#play img{width:100%;height:100%;object-fit:contain;filter:drop-shadow(0 8px 14px #2a1622aa)}
#play:focus-visible{outline:4px solid #fff8e6;outline-offset:4px;border-radius:50%}

/* ------------------------------------------------------------- playfield */
#playfield{
  position:fixed;inset:0;display:grid;
  grid-template-columns:minmax(0,1fr) auto;
  grid-template-rows:auto minmax(0,1fr);
  gap:var(--pad);
  padding:calc(var(--pad) + var(--safe-t)) calc(var(--pad) + var(--safe-r)) calc(var(--pad) + var(--safe-b)) calc(var(--pad) + var(--safe-l));
  background:radial-gradient(120% 90% at 50% 0%,#5c3350 0%,var(--plum) 62%,#2b1526 100%);
}

/* ----------------------------------------------------------------- stage */
#stage{grid-area:1/1/3/2;position:relative;display:flex;flex-direction:column;min-height:0;border-radius:24px;overflow:hidden;background:#5c3350;box-shadow:0 14px 30px #1c0e1acc}
#bgPicture{position:absolute;inset:0;display:block;z-index:0}
#bg{width:100%;height:100%;object-fit:cover}

#controls{grid-area:1/1;justify-self:start;align-self:start;z-index:6;display:flex;gap:8px;padding:var(--pad)}
#controls button{flex:0 0 56px;width:56px;height:56px;min-width:56px;border-radius:50%;filter:drop-shadow(0 3px 2px #46301e66)}
#controls button img{width:100%;height:100%;object-fit:contain;pointer-events:none}
#controls button:active{transform:translateY(2px) scale(.96)}

#hud{
  grid-area:1/1;justify-self:end;align-self:start;z-index:6;display:flex;align-items:center;justify-content:flex-end;
  flex-wrap:wrap;gap:6px 10px;padding:var(--pad);max-width:calc(100% - 340px);
}
#levelName{margin:0;padding:6px 14px;border-radius:999px;background:#2a1622b8;font-size:clamp(.86rem,2.1vw,1.2rem);font-weight:600;white-space:nowrap}
#pips{display:flex;align-items:center;justify-content:flex-end;flex-wrap:wrap;gap:4px 8px;padding:6px;border-radius:14px;background:#3b1f33cc}
#pips ol{display:flex;gap:6px;margin:0;padding:0;list-style:none}
#pips li{position:relative;flex:0 0 auto;display:grid;place-items:center}
#levelPips li{width:30px;height:30px;font-size:.95rem;font-weight:600;color:#ffeccb;border-radius:50%;background:#4d2c14;box-shadow:inset 0 0 0 2px #b8792f}
#levelPips li[data-level='now']{background:var(--amber);color:var(--ink);box-shadow:0 0 0 3px #fff8e6,0 0 12px var(--amber)}
#levelPips li[data-level='done']{background:#7d5a24;color:#fff6e2}
#levelPips li[data-level='done']::after{content:'✓';position:absolute;right:-3px;bottom:-4px;font-size:.8rem;color:#9ce89a;text-shadow:0 1px 2px #2a1622}
#levelPips li[data-level='locked']{opacity:.5}
#roundPips li{width:34px;height:34px}
#roundPips img{width:100%;height:100%;object-fit:contain;pointer-events:none}
#roundPips li[data-star='on'] img{filter:drop-shadow(0 0 5px var(--amber))}

#skipDemo{min-height:44px;padding:0 14px;font-size:.9rem}
#artRetry{min-height:56px;padding:0 14px;font-size:.85rem;background:linear-gradient(#915020,#8a5a22)}

#boardWrap{position:relative;z-index:2;flex:1 1 auto;min-height:0;display:grid;place-items:center;padding:calc(66px + var(--safe-t)) 6% 4px}
#board{
  width:min(100%,56vh,560px);aspect-ratio:1;display:grid;
  grid-template-columns:repeat(var(--cols,2),minmax(0,1fr));
  grid-template-rows:repeat(var(--rows,2),minmax(0,1fr));
  gap:2px;padding:10px;border-radius:20px;
  background:linear-gradient(#8a5527,#5e3617);
  box-shadow:inset 0 0 0 3px #3d2210,inset 0 0 30px #2a162299,0 14px 30px #1c0e1acc;
}
.slot{
  position:relative;display:block;width:100%;height:100%;padding:0;border-radius:6px;
  background:#f0dcb4;overflow:hidden;box-shadow:inset 0 0 0 1px #ffffff44;
}
.slot .ghost,.slot .fill,.slot .tagnum{
  position:absolute;inset:0;background-repeat:no-repeat;pointer-events:none;display:block;
}
.slot .ghost{opacity:.16}
.slot .fill{opacity:0;transition:opacity .12s}
.slot.is-filled{background:#f7e8c8}
.slot.is-filled .fill{opacity:1}
.slot.is-empty::after{content:'';position:absolute;inset:5px;border:2px dashed #b8792f99;border-radius:5px;pointer-events:none}
.slot.is-wrong{outline:4px solid var(--amber-deep);outline-offset:-4px}
.slot.over{outline:4px solid var(--amber);outline-offset:-4px}
.slot:focus-visible,#pieces button:focus-visible,#check:focus-visible,#controls button:focus-visible,#guide:focus-visible,.wood:focus-visible{outline:3px solid #fff8e6;outline-offset:2px}
.slot .tagnum{display:none}
#board.art-fallback .slot .tagnum{
  display:grid;place-items:center;opacity:1;color:#4d2c14;font-weight:600;font-size:clamp(.9rem,2.4vw,1.5rem);
}

#heroStrip{position:relative;z-index:2;flex:0 0 auto;display:flex;align-items:flex-end;justify-content:space-between;gap:8px;padding:0 10px 6px calc(56px + var(--safe-l))}
#buki{height:clamp(84px,17vh,190px);width:auto;pointer-events:none;filter:drop-shadow(0 8px 12px #2a1622cc)}
#guide{display:flex;align-items:center;gap:8px;max-width:min(48vw,420px);text-align:left;align-self:flex-end}
#guide img{width:clamp(46px,5.4vw,78px);height:auto;flex:0 0 auto;filter:drop-shadow(0 6px 10px #2a1622cc)}
#guide span{display:block;background:var(--cream);color:var(--ink);border-radius:16px;padding:8px 12px;font-size:clamp(.74rem,1.6vw,.98rem);line-height:1.3;box-shadow:0 8px 18px #1c0e1a99}
#guide:active img{transform:scale(.96)}

#hint{position:absolute;left:50%;top:calc(var(--safe-t) + 64px);transform:translateX(-50%);margin:0;padding:8px 16px;border-radius:999px;background:#2a1622e6;color:var(--amber);font-size:clamp(.8rem,2.1vw,1rem);text-align:center;max-width:min(92%,620px);z-index:7}

/* ----------------------------------------------------------------- tray */
#tray{
  grid-area:1/2/3/3;z-index:5;width:clamp(158px,19vw,246px);
  display:flex;flex-direction:column;gap:10px;padding:10px;border-radius:24px;min-height:0;
  background:#2a1622c4;box-shadow:0 14px 30px #1c0e1acc;
}
#reference{margin:0;display:flex;flex-direction:column;align-items:center;gap:4px;flex:0 0 auto}
#refImg{width:100%;max-width:150px;border-radius:12px;background:#7a4a2412;box-shadow:inset 0 0 0 3px #b8792f,0 4px 10px #1c0e1a99}
#reference figcaption{font-size:.72rem;font-weight:600;color:#ffdca8;text-align:center}
#pieces{
  list-style:none;margin:0;padding:0;flex:1 1 auto;min-height:0;align-content:start;
  display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:8px;overflow:hidden;
}
#pieces li{min-width:0}
#pieces button{
  position:relative;display:block;width:100%;aspect-ratio:var(--ar,1);padding:0;
  min-height:52px;
  border-radius:12px;background-repeat:no-repeat;touch-action:none;
  box-shadow:inset 0 0 0 3px #b8792fcc,0 3px 6px #1c0e1a99;
}
#pieces button::after{content:'';position:absolute;inset:0;background:#3b1f33;opacity:0;border-radius:12px;transition:opacity .12s;pointer-events:none}
#pieces button[data-state='placed']::after{opacity:.55}
#pieces button[data-state='held']{box-shadow:inset 0 0 0 3px var(--amber),0 0 16px #ffc061aa;transform:translateY(-2px)}
#pieces button[data-state='placed']::before{content:'✓';position:absolute;right:4px;bottom:2px;font-size:1rem;font-weight:600;color:#9ce89a;text-shadow:0 1px 3px #2a1622;z-index:2}
#pieces.is-dragging button{opacity:.72}
#pieces button.tap:active{transform:scale(.95)}

#check{margin-top:auto;align-self:center;width:76px;height:76px;min-width:76px;border-radius:50%;filter:drop-shadow(0 4px 6px #2a162299);flex:0 0 auto}
#check img{width:100%;height:100%;object-fit:contain}
#check:disabled{opacity:.7;cursor:default}
#check.demo-ok{opacity:1;filter:drop-shadow(0 0 14px #8fe08a) drop-shadow(0 4px 6px #2a162299)}

/* ------------------------------------------------------------ drag ghost */
#dragGhost{
  position:fixed;z-index:140;pointer-events:none;width:84px;
  border-radius:12px;background-repeat:no-repeat;box-shadow:0 10px 20px #1c0e1acc,0 0 0 3px var(--amber);
  transform:translate(-50%,-50%) scale(1.06);opacity:.94;
}

/* --------------------------------------------------------------- motion */
#board.solved .fill{animation:piecePulse .6s ease-out}
@keyframes piecePulse{0%{transform:scale(1)}45%{transform:scale(1.06)}100%{transform:scale(1)}}
#board.mismatch{animation:boardShake .4s ease-in-out 1}
@keyframes boardShake{0%,100%{transform:translateX(0)}25%{transform:translateX(-7px)}75%{transform:translateX(7px)}}
/* Pausing freezes the hand and any running CSS motion. */
#game[data-state='paused'] #hand{animation-play-state:paused!important}
#hand{position:fixed;width:64px;pointer-events:none;transform-origin:70% 70%;filter:drop-shadow(0 4px 6px #2a1622aa);transition:left .35s ease,top .35s ease,transform .35s ease;z-index:130}

/* -------------------------------------------------------------- dialogs */
dialog{border:0;border-radius:22px;padding:18px;max-width:min(92vw,620px);max-height:88vh;overflow:auto;background:var(--cream);color:var(--ink);box-shadow:0 18px 40px #1c0e1acc}
dialog::backdrop{background:#2a1622cc}
dialog h2{margin:0 0 8px;font-size:clamp(1.2rem,3.4vw,1.7rem);color:var(--wood-dark)}
dialog p{margin:8px 0;line-height:1.4}
.menu-label{margin:14px 0 6px;font-weight:600;color:var(--wood-dark)}
.level-menu{display:flex;flex-wrap:wrap;gap:8px}
.stage-btn{min-width:104px}
.stage-btn:disabled{opacity:.45;filter:grayscale(.4)}
.stage-btn.is-current{box-shadow:inset 0 0 0 3px var(--amber),0 3px 0 #4d2c14}
.dialog-actions{display:flex;flex-wrap:wrap;gap:8px;margin-top:12px}
.wood{min-height:56px;padding:0 18px;border-radius:14px;background:linear-gradient(#915020,#7a4a24);color:#fff6e2;font-weight:600;box-shadow:inset 0 2px 0 #ffffff33,0 3px 0 #4d2c14}
.wood:active{transform:translateY(2px)}
#helpSteps{margin:0;padding:0;list-style:none;display:flex;flex-wrap:wrap;gap:12px}
#helpSteps li{display:flex;align-items:center;gap:8px;flex:1 1 240px;min-width:0}
#helpSteps li>span:last-child{font-size:.95rem}
.step-art{flex:0 0 auto;width:72px;height:56px;display:grid;place-items:center;border-radius:12px;background:#7a4a2412;box-shadow:inset 0 0 0 3px #b8792f}
.step-check{width:48px;height:48px;object-fit:contain;flex:0 0 auto}
.mini-piece{width:34px;height:34px;border-radius:4px;box-shadow:inset 0 0 0 3px var(--amber)}
.mini-board{display:grid;grid-template-columns:repeat(2,20px);grid-template-rows:repeat(2,20px);gap:2px}
.mini-slot{border-radius:3px;background-size:200% 200%;box-shadow:inset 0 0 0 1px #b8792f}
.mini-slot.is-ghost{background:#f3e0ba;border:1px dashed #b8792f}
.mini-slot.swapped{box-shadow:inset 0 0 0 2px var(--amber-deep)}
.narration{display:flex;gap:8px;margin:12px 0 4px;flex-wrap:wrap}
#helpText{font-size:.92rem;background:#7a4a2412;padding:10px 12px;border-radius:12px}
#rewardArt{width:100%;max-height:38vh;object-fit:contain;border-radius:14px;background:#7a4a2412}

/* ----------------------------------------------------------- responsive */
/* Phone / portrait: the five controls stay in one row of 56px targets, the
   guide stays compact, the square board is always fully visible, and the tray
   lays the pieces beside a small preview so every crop shape and a 56px touch
   target both survive. The landscape rules above stay as they are. */
@media (orientation:portrait){
  #playfield{grid-template-columns:minmax(0,1fr);grid-template-rows:auto auto minmax(0,1fr) auto;gap:6px}
  #controls{grid-area:1/1;padding:0;justify-self:stretch;justify-content:space-between;gap:6px}
  #controls button{flex:0 0 56px;width:56px;height:56px;min-width:56px}
  #hud{grid-area:2/1;padding:0;justify-self:stretch;max-width:100%;justify-content:space-between;gap:4px 6px}
  #stage{grid-area:3/1/4/2;flex-direction:column-reverse;border-radius:20px}
  /* The board is a square that fits whatever room the stage has left, so it is
     never clipped and never stretched out of shape. */
  #boardWrap{padding:2px;container-type:size}
  #board{width:min(100%,42vh,420px)}
  @supports (width:1cqh){#board{width:min(100%,100cqh,420px)}}
  #heroStrip{padding:0 2px 2px;justify-content:space-between}
  #buki{height:clamp(50px,8.4vh,92px)}
  #guide img{width:clamp(34px,9vw,52px)}
  #guide span{font-size:clamp(.68rem,2.8vw,.9rem);padding:5px 9px}
  #hint{position:static;left:auto;top:auto;bottom:auto;transform:none;margin:2px auto 0;max-width:96%}
  /* Tray: the little picture and the green check share the first row, the
     pieces take the second in as many columns as the puzzle needs. */
  #tray{
    grid-area:4/1;width:auto;display:grid;align-items:center;gap:6px 8px;
    grid-template-columns:auto minmax(0,1fr) auto;grid-template-rows:auto auto;
    padding:8px 10px calc(6px + var(--safe-b)) calc(52px + var(--safe-l));
  }
  #reference{grid-area:1/1;flex-direction:row;align-items:center;gap:6px}
  #refImg{max-width:48px}
  #reference figcaption{font-size:11px;max-width:64px;line-height:1.05}
  #pieces{grid-area:2/1/3/4;flex:0 0 auto;gap:6px;justify-content:start;grid-template-columns:repeat(var(--tray-cols,2),auto)}
  #pieces button{width:auto;height:var(--tray-tile,64px);min-height:0;aspect-ratio:var(--ar,1)}
  #check{grid-area:1/3;justify-self:end;margin:0;width:64px;height:64px;min-width:64px}
}
@media (max-width:430px){
  #levelPips li{width:24px;height:24px;font-size:.78rem}
  #roundPips li{width:26px;height:26px}
  #levelName{font-size:.8rem;padding:4px 9px}
  #pips{gap:3px 5px;padding:4px}
  #pips ol{gap:5px}
}
@media (orientation:portrait) and (max-height:700px){
  #refImg{max-width:44px}
  #reference figcaption{font-size:11px;max-width:64px}
  #buki{height:clamp(46px,7.6vh,80px)}
  #guide img{width:clamp(30px,8vw,46px)}
}
@media (orientation:landscape) and (max-height:600px){
  #board{width:min(100%,52vh,440px)}
  #buki{height:clamp(70px,14vh,130px)}
  #check{width:64px;height:64px;min-width:64px}
  #refImg{max-width:110px}
}
/* Compact landscape (e.g. 752x393): the five 56px controls keep the left
   corner, so the HUD stacks the level name above ONE row of compact pips. That
   keeps it ~215px wide (inside the stage minus the 324px controls) and under
   the 66px the board reserves. The demo skip button leaves the flow entirely. */
@media (orientation:landscape) and (max-width:900px) and (max-height:600px){
  #hud{position:relative;flex-direction:column;align-items:flex-end;flex-wrap:nowrap;gap:3px;padding:0;max-width:calc(100% - 324px)}
  #levelName{font-size:.82rem;padding:4px 10px}
  #pips{flex-wrap:nowrap;gap:3px 6px;padding:4px}
  #pips ol{flex:0 0 auto;gap:5px}
  #levelPips li{width:24px;height:24px;font-size:.78rem}
  #roundPips li{width:26px;height:26px}
  /* Skip the demo below the controls and to the left of the HUD, clear of the board. */
  #skipDemo{position:fixed;top:76px;left:14px;right:auto;margin-top:0;padding:0 12px;font-size:.8rem;white-space:nowrap}
  /* Tray: an 84px column keeps the flat 3:2 crop with a 56px touch target, so
     the six pieces fit three rows (168px + gaps) without clipping. */
  #tray{width:192px;padding:8px}
  #refImg{max-width:64px}
  #pieces button{min-height:56px}
}
@media (max-width:480px){
  #rewardArt{max-height:150px}
  #rewardActions{display:grid;grid-template-columns:1fr 1fr;gap:8px}
  #rewardActions .wood{min-height:56px;padding:0 10px;font-size:.86rem;line-height:1.15}
}
@media (prefers-reduced-motion:reduce){
  *{animation-duration:.001ms!important;animation-iteration-count:1!important;transition-duration:.001ms!important}
}
