:root {
  --ink: #2b1d12; --jade: #c2520f; --jade2: #f07419; --coral: #2f9e44; --coral2: #e8323c; --cream: #fffaf0;
  --blue: #2f7fd1; --gold: #ffd23f;
  --light: #fff3dc; --dark: #f49a4c; --bs: 360px; --sw: 300px; --pad: 10px; --gap: 10px; --top: 0px;
  --fs: clamp(11px, min(2.2vh, 1.7vw) + 4px, 19px);
  font-size: var(--fs);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  font-family: "Segoe UI", Roboto, "Helvetica Neue", Arial, system-ui, sans-serif; color: var(--ink);
  background: #8fd0f5 url(img/bg.webp) center bottom / cover no-repeat; touch-action: none; user-select: none; -webkit-user-select: none;
}
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; }
.hidden { display: none !important; }

#main { position: fixed; inset: 0; padding: calc(var(--pad) + var(--top)) var(--pad) var(--pad); display: flex; flex-direction: row-reverse; gap: var(--gap); justify-content: center; align-items: center; }
#boardCol { width: var(--bs); height: var(--bs); flex: none; margin: var(--ring, 0px); }
#boardWrap { position: relative; width: 100%; height: 100%; border-radius: 12px; padding: 0; box-shadow: 0 0 0 calc(var(--bs) * .012) #fff, 0 0 0 calc(var(--bs) * .036) #2f9e44, 0 0 0 calc(var(--bs) * .04) #1d6b2c, 0 12px 28px rgba(20, 60, 30, .38); }
#board { position: relative; width: 100%; height: 100%; display: grid; grid-template-columns: repeat(8, 1fr); grid-template-rows: repeat(8, 1fr); border-radius: 8px; overflow: hidden; touch-action: none; }
.sq { position: relative; }
.sq.l { background: var(--light); }
.sq.d { background: var(--dark); }
.sq.last::before { content: ""; position: absolute; inset: 0; background: rgba(255, 222, 70, .55); }
.sq.sel::before { content: ""; position: absolute; inset: 0; background: rgba(120, 200, 90, .6); }
.sq.hint::before { content: ""; position: absolute; inset: 0; background: rgba(90, 170, 255, .55); animation: hintp 1s ease-in-out infinite alternate; }
@keyframes hintp { from { opacity: .45; } to { opacity: 1; } }
.sq.mv::after { content: ""; position: absolute; left: 36%; top: 36%; width: 28%; height: 28%; border-radius: 50%; background: rgba(29, 107, 44, .55); z-index: 3; pointer-events: none; }
.sq.cap::after { content: ""; position: absolute; inset: 5%; border-radius: 50%; border: calc(var(--bs) * .009) solid rgba(232, 50, 60, .85); z-index: 3; pointer-events: none; }
.sq.chk { background: radial-gradient(circle, #ff5a4a 0%, rgba(255, 90, 74, .55) 45%, transparent 75%) !important; }
.sq.hov { box-shadow: inset 0 0 0 calc(var(--bs) * .008) #fff; }
.sq.cur { box-shadow: inset 0 0 0 calc(var(--bs) * .01) var(--coral2); }
.coord { position: absolute; font-size: calc(var(--bs) * .028); font-weight: 700; opacity: .75; pointer-events: none; }
.coord.f { right: 4%; bottom: 2%; } .coord.r { left: 4%; top: 2%; }
.sq.l .coord { color: #c2520f; } .sq.d .coord { color: #fffaf0; }
#pieces { position: absolute; inset: 0; pointer-events: none; z-index: 2; }
.piece { position: absolute; left: 0; top: 0; width: 12.5%; height: 12.5%; transition: transform .2s cubic-bezier(.3, .8, .3, 1); will-change: transform; }
.piece.drag { transition: none; z-index: 10; filter: drop-shadow(0 6px 6px rgba(0, 0, 0, .3)); }
.piece.drag svg { transform: scale(1.15); }
.piece.lift svg { transform: translateY(-6%) scale(1.06); }
.piece.pop svg { animation: pop .3s ease-out; }
.piece.gone { animation: gone .3s ease-in forwards; }
@keyframes pop { 0% { transform: scale(.6); } 60% { transform: scale(1.15); } 100% { transform: scale(1); } }
@keyframes gone { to { transform: var(--t) scale(.2); opacity: 0; } }
.pc { width: 100%; height: 100%; display: block; transition: transform .12s; overflow: visible; }
.pc .o, .pc .g { stroke-width: 4; stroke-linejoin: round; }
.pc .bd { fill: none; stroke-width: 4.2; stroke-linecap: round; }
.pc .hl { fill: none; stroke-width: 3.4; stroke-linecap: round; }
.pw .o { fill: #fffaf0; stroke: #6b3d1c; } .pw .bd { stroke: #f47a1f; } .pw .g { fill: #f47a1f; stroke: #6b3d1c; } .pw .f { fill: #6b3d1c; } .pw .hl { stroke: #fff; }
.pb .o { fill: #43281a; stroke: #150a05; } .pb .bd { stroke: #ffa940; } .pb .g { fill: #ffa940; stroke: #150a05; } .pb .f { fill: #ffc56b; } .pb .hl { stroke: rgba(255, 255, 255, .35); }
.pc { filter: drop-shadow(0 2px 1px rgba(0, 0, 0, .25)); }

/* side panel */
#side { width: var(--sw); height: calc(var(--bs) + 2 * var(--ring, 0px)); flex: none; display: flex; flex-direction: column; gap: .45rem; min-height: 0; }
.pstrip { display: flex; align-items: center; gap: .4rem; background: rgba(255, 250, 240, .92); border-radius: 10px; padding: .3rem .55rem; min-height: 2.1rem; box-shadow: 0 2px 6px rgba(0, 0, 0, .12); border: 2px solid transparent; flex: none; }
.pstrip.turn { border-color: var(--coral); box-shadow: 0 0 0 3px rgba(255, 122, 89, .25); }
#stripTop { margin-left: 5.6rem; }
.dot { width: .9rem; height: .9rem; border-radius: 50%; flex: none; border: 2px solid var(--ink); }
.dot.w { background: #fff; } .dot.b { background: #43281a; }
.pname { font-weight: 800; white-space: nowrap; font-size: .92rem; }
.pname .you { background: var(--coral); color: #fff; border-radius: 6px; padding: 0 .35rem; margin-right: .2rem; font-size: .8rem; }
.caps { display: flex; flex-wrap: nowrap; align-items: center; overflow: hidden; margin-left: auto; }
.caps svg { width: 1.15rem; height: 1.15rem; margin-left: -.3rem; }
.adv { font-size: .8rem; font-weight: 800; margin-left: .25rem; color: var(--jade); }
#calfBox { display: flex; align-items: center; gap: .4rem; flex: none; }
#calf { width: 5.4rem; height: 5.4rem; object-fit: contain; flex: none; filter: drop-shadow(0 3px 3px rgba(0, 0, 0, .2)); }
#calf.bounce { animation: bounce .45s ease-out; }
@keyframes bounce { 30% { transform: translateY(-10%) scale(1.06); } 60% { transform: translateY(2%); } }
#bubble { position: relative; flex: 1; background: #fff; border-radius: 12px; padding: .45rem .6rem; font-size: .9rem; font-weight: 600; line-height: 1.35; box-shadow: 0 2px 8px rgba(0, 0, 0, .15); min-height: 2.6rem; display: flex; align-items: center; }
#bubble::before { content: ""; position: absolute; left: -.45rem; top: 50%; margin-top: -.45rem; border: .45rem solid transparent; border-right-color: #fff; border-left: 0; }
#bubble.pulse { animation: bpulse .3s ease-out; }
@keyframes bpulse { 40% { transform: scale(1.03); } }
#tools { display: grid; grid-template-columns: repeat(4, 1fr); gap: .35rem; flex: none; }
.tool { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: .1rem; background: var(--cream); border-radius: 10px; padding: .3rem .1rem; font-weight: 800; font-size: .8rem; box-shadow: 0 3px 0 #c9bfa6; min-height: 2.6rem; }
.tool svg { width: 1.2rem; height: 1.2rem; }
.tool.primary { background: var(--jade2); color: #fff; box-shadow: 0 3px 0 #a3470b; }
.tool:active { transform: translateY(2px); box-shadow: none; }
.tool:disabled { opacity: .45; }
#movesBox { flex: 1 1 auto; min-height: 2.6rem; background: rgba(255, 250, 240, .92); border-radius: 10px; padding: .3rem .5rem; display: flex; flex-direction: column; box-shadow: 0 2px 6px rgba(0, 0, 0, .12); }
.mhead { display: flex; align-items: center; justify-content: space-between; }
.mhead h3 { margin: 0; font-size: .85rem; color: var(--jade); }
.mini { background: var(--jade); color: #fff; border-radius: 8px; padding: .12rem .5rem; font-size: .75rem; font-weight: 700; }
#moves { list-style: none; margin: .2rem 0 0; padding: 0; overflow-y: auto; flex: 1; min-height: 0; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .85rem; display: grid; grid-template-columns: 1fr 1fr; align-content: start; column-gap: .5rem; touch-action: pan-y; }
#moves li { display: grid; grid-template-columns: 1.9rem 1fr 1fr; }
#moves .n { color: #7d8aa5; } #moves .m.cur { background: var(--gold); border-radius: 4px; padding: 0 .15rem; }
#keysLine { margin: 0; font-size: .72rem; color: var(--ink); background: rgba(255, 255, 255, .7); border-radius: 8px; padding: .15rem .4rem; text-align: center; flex: none; }

/* sound buttons */
#snd { position: fixed; top: calc(var(--pad) + 2px); left: var(--pad); display: flex; gap: .35rem; z-index: 60; }
#snd button { width: 2.4rem; height: 2.4rem; border-radius: 50%; background: var(--cream); box-shadow: 0 2px 0 #c9bfa6, 0 2px 6px rgba(0, 0, 0, .15); display: grid; place-items: center; color: var(--jade); }
#snd svg { width: 1.3rem; height: 1.3rem; }
#hdr { display: none; }

/* overlays */
.ovl { position: fixed; inset: 0; z-index: 40; display: flex; align-items: center; justify-content: center; padding: .6rem; background: rgba(20, 50, 80, .4); }
#boardWrap .ovl { position: absolute; z-index: 20; border-radius: 8px; }
.card { background: var(--cream); border-radius: 18px; padding: 1rem 1.2rem; box-shadow: 0 12px 40px rgba(0, 0, 0, .3); max-height: 94vh; overflow: auto; text-align: center; border: 3px solid var(--jade2); }
.card h2 { margin: .1rem 0 .5rem; font-size: 1.25rem; color: var(--jade); }
.card p { margin: .3rem 0; }
.btn { background: var(--jade2); color: #fff; font-weight: 900; border-radius: 14px; padding: .55rem 1.2rem; box-shadow: 0 4px 0 #a3470b; font-size: 1rem; }
.btn.ghost { background: #fff; color: var(--jade); box-shadow: 0 4px 0 #c9bfa6; }
.btn.small { padding: .35rem .8rem; font-size: .88rem; }
.btn.big { font-size: 1.35rem; padding: .65rem 1.4rem; width: 100%; margin-top: .4rem; }
.btn:active { transform: translateY(3px); box-shadow: none; }
.row2 { display: flex; gap: .5rem; justify-content: center; margin-top: .55rem; flex-wrap: wrap; }
#promoBtns { display: flex; gap: .4rem; justify-content: center; margin-bottom: .5rem; }
#promoBtns button { width: calc(var(--bs) * .16); height: calc(var(--bs) * .16); background: var(--light); border-radius: 12px; box-shadow: 0 3px 0 #c9bfa6; }
#promo .card { padding: .6rem .8rem; }
#promo h2 { font-size: 1rem; }

.startCard { display: flex; gap: 1.2rem; text-align: left; width: min(48rem, 96vw); align-items: center; }
.sLeft { flex: 1.1; text-align: center; }
.sRight { flex: 1; display: flex; flex-direction: column; gap: .3rem; }
.sCalf { width: 7.5rem; height: 7.5rem; object-fit: contain; }
.startCard h1 { margin: 0; font-size: 2.6rem; color: var(--jade); letter-spacing: .02em; line-height: 1.05; }
.startCard h1 small { display: block; font-size: .95rem; letter-spacing: .3em; color: var(--coral); text-transform: uppercase; }
.intro { font-size: .92rem; line-height: 1.5; margin: .4rem 0 .5rem !important; }
.keys { display: flex; flex-direction: column; gap: .25rem; font-size: .8rem; align-items: center; }
kbd { display: inline-block; min-width: 1.4rem; padding: 0 .25rem; border-radius: 5px; background: #fff; border: 1px solid #c9bfa6; border-bottom-width: 3px; font-family: inherit; font-size: .75rem; text-align: center; }
.lbl { font-weight: 800; font-size: .8rem; color: var(--jade); }
.seg { display: flex; background: #fff; border-radius: 12px; padding: .2rem; gap: .2rem; box-shadow: inset 0 0 0 2px #e6dcc4; }
.seg button { flex: 1; padding: .38rem .2rem; border-radius: 9px; font-weight: 800; font-size: .88rem; white-space: nowrap; }
.seg button.on { background: var(--jade); color: #fff; }
#levelStats { font-size: .78rem; color: #55627e; min-height: 1em; margin: .1rem 0 !important; }
.sRight .row2 { margin-top: .3rem; } .sRight .row2 .btn { flex: 1; padding: .45rem .5rem; font-size: .92rem; }

.endCard { display: flex; align-items: center; gap: 1rem; max-width: min(34rem, 94vw); }
.endCard img { width: 7rem; height: 7rem; object-fit: contain; flex: none; }
#endMenu { margin-top: .6rem; }
#endReason { font-weight: 700; } #endStats { font-size: .85rem; color: #55627e; }

.tutCard { display: flex; gap: 1rem; width: min(52rem, 96vw); text-align: left; align-items: stretch; }
.tutBoardBox { flex: none; width: min(62vh, 44vw); }
#tutBoard { width: 100%; aspect-ratio: 1; display: grid; grid-template-columns: repeat(8, 1fr); grid-template-rows: repeat(8, 1fr); border-radius: 8px; overflow: hidden; box-shadow: 0 0 0 4px var(--jade); }
#tutBoard .sq { cursor: pointer; }
#tutBoard .lab { position: absolute; left: 0; right: 0; bottom: 0; text-align: center; font-size: .55rem; font-weight: 800; color: var(--coral2); pointer-events: none; z-index: 4; }
#tutBoard .tp { position: absolute; inset: 0; pointer-events: none; }
.tutText { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.tutStep { font-size: .78rem; font-weight: 800; color: var(--coral2); }
#tutBody { flex: 1; overflow-y: auto; font-size: .9rem; line-height: 1.55; touch-action: pan-y; }
#tutBody p { margin: .25rem 0; } #tutBody b { color: var(--jade); }
.tutNav { display: flex; gap: .4rem; margin-top: .5rem; flex-wrap: wrap; }

#toast { position: fixed; left: 50%; bottom: 8%; transform: translateX(-50%); background: var(--ink); color: #fff; border-radius: 10px; padding: .4rem .9rem; z-index: 80; font-weight: 700; }
#fx { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 70; }

/* portrait: board on top, info below */
body.port #main { flex-direction: column; justify-content: flex-start; }
body.port #side { width: calc(var(--bs) + 2 * var(--ring, 0px)); height: auto; flex: 1 1 auto; display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: auto auto auto 1fr; }
body.port #stripTop { margin-right: 0; grid-row: 1; grid-column: 1; }
body.port #stripBot { grid-row: 1; grid-column: 2; }
body.port #calfBox { grid-row: 2; grid-column: 1 / 3; }
body.port #tools { grid-row: 3; grid-column: 1 / 3; }
body.port #movesBox { grid-row: 4; grid-column: 1 / 3; }
body.port #keysLine { display: none; }
body.port #hdr { display: block; position: fixed; top: calc(var(--pad) + .2rem); left: var(--pad); font-size: 1.5rem; font-weight: 900; color: var(--jade); letter-spacing: .02em; text-shadow: 0 2px 0 #fff; }
body.port #calf { width: 3.4rem; height: 3.4rem; }

/* short windows */
@media (max-height: 560px) {
  .startCard { width: min(46rem, 97vw); padding: .7rem .9rem; gap: .9rem; }
  .sCalf { width: 4.6rem; height: 4.6rem; }
  .startCard h1 { font-size: 1.9rem; }
  .intro { font-size: .82rem; }
  #calf { width: 3.3rem; height: 3.3rem; }
  #side { gap: .32rem; }
  .card { padding: .7rem .9rem; }
  .tutCard { padding: .6rem; }
}
@media (max-height: 420px) {
  .sCalf { display: none; }
  .keys { display: none; }
  .startCard h1 { font-size: 1.6rem; }
  .seg button { padding: .3rem .15rem; font-size: .8rem; }
  .btn.big { font-size: 1.1rem; padding: .45rem 1rem; }
  #calf { width: 2.7rem; height: 2.7rem; }
  #bubble { font-size: .8rem; padding: .3rem .45rem; min-height: 2.2rem; }
  .tool { min-height: 2.2rem; padding: .2rem .1rem; }
  .tool span { font-size: .72rem; }
  #keysLine { display: none; }
  .endCard img { width: 4.5rem; height: 4.5rem; }
}
@media (max-width: 600px) {
  .startCard { flex-direction: column; gap: .4rem; text-align: center; }
  .sRight { width: 100%; text-align: left; }
  .sCalf { width: 5rem; height: 5rem; }
  .startCard h1 { font-size: 2rem; }
  .tutCard { flex-direction: column; }
  .tutBoardBox { width: min(100%, 46vh); align-self: center; }
  .endCard { flex-direction: column; text-align: center; }
}

/* puzzles */
.puzPick { display: flex; align-items: center; gap: .3rem; background: #fff; border-radius: 12px; padding: .2rem; box-shadow: inset 0 0 0 2px #ecd9b8; }
.puzPick .arrow { width: 2.2rem; height: 2rem; border-radius: 9px; background: var(--jade2); color: #fff; font-size: 1.4rem; font-weight: 900; line-height: 1; flex: none; }
#puzName { flex: 1; text-align: center; font-weight: 800; font-size: .88rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#puzName .today { background: var(--gold); color: var(--ink); border-radius: 6px; padding: 0 .3rem; font-size: .75rem; }
#puzName .ok { color: #2e8b3a; }
.seg button { padding-left: .1rem; padding-right: .1rem; }

/* level picker (10 levels) */
.lvlPick { display: flex; align-items: center; gap: .3rem; background: #fff; border-radius: 12px; padding: .2rem; box-shadow: inset 0 0 0 2px #f5dcc0; }
.lvlPick .arrow { width: 2.2rem; height: 2.2rem; border-radius: 9px; background: var(--jade2); color: #fff; font-size: 1.4rem; font-weight: 900; line-height: 1; flex: none; }
.lvlPick .arrow:disabled { opacity: .35; }
.lvlMid { flex: 1; min-width: 0; text-align: center; }
.lvlTxt { font-weight: 800; font-size: .9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lvlTxt b { display: inline-block; min-width: 1.5rem; background: var(--coral); color: #fff; border-radius: 6px; padding: 0 .3rem; }
.lvlTxt i { font-style: normal; font-weight: 600; font-size: .75rem; color: #8a7360; }
.lvlTrack { height: .32rem; background: #f3e6d6; border-radius: 3px; margin: .2rem .3rem 0; overflow: hidden; }
#lvlBar { height: 100%; width: 30%; background: linear-gradient(90deg, #ffd23f, #f07419, #e8323c); border-radius: 3px; transition: width .2s; }
.startCard h1 { color: var(--jade2); text-shadow: 0 2px 0 #ffe2c4; }
.startCard h1 small { color: var(--coral); letter-spacing: .18em; }
#bubble { border: 2px solid #ffe2c4; }
.pstrip.turn { border-color: var(--jade2); box-shadow: 0 0 0 3px rgba(240, 116, 25, .25); }
.pname .you { background: var(--jade2); }
#moves .m.cur { background: #ffe08a; }
body.port #snd { left: auto; right: var(--pad); }
