* { box-sizing: border-box; }
:root { color: #493727; background: #dce3c7; font-family: system-ui, sans-serif; }
html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; overscroll-behavior: none; }
body { user-select: none; -webkit-user-select: none; }
button { font: inherit; color: inherit; cursor: pointer; -webkit-tap-highlight-color: transparent; }
button:focus-visible { outline: 3px solid #fff1b7; outline-offset: 2px; }
main { position: fixed; inset: 0; height: 100dvh; overflow: hidden; }
#stage {
  --glass-tint: #dcecc6e6;
  --chest-width: min(36vw, 510px);
  --edge: 10px;
  --play-center: calc((100% - var(--chest-width) - var(--edge)) / 2);
  --control-size: clamp(58px, 6vw, 77px);
  --controls-bottom: max(10px, env(safe-area-inset-bottom));

  position: relative; height: 100%; overflow: hidden;
  background: linear-gradient(#f0efdc20, #f0efdc34), var(--scene-image, url("assets/jungle.jpg")) center 56% / cover, #dce3c7;
}
#stage[data-scene="ocean"] { --glass-tint: #bdebe6e6; }
#stage[data-scene="clouds"] { --glass-tint: #d4e3f9e6; }
#stage[data-scene="arctic"] { --glass-tint: #dbeff8e6; }
#stage[data-scene="farm"] { --glass-tint: #f4e3b6e6; }
#play-controls {
  position: absolute; bottom: var(--controls-bottom);
  left: var(--play-center); translate: -50% 0;
  z-index: 10; display: flex; gap: 8px;
}
#play-controls button {
  display: grid; place-items: center; width: var(--control-size); aspect-ratio: 1;
  border-radius: 50%; padding: 0;
}
.glass-button {
  background: linear-gradient(155deg, #ffffff70, transparent 38%),
    linear-gradient(145deg, #ffffff38, transparent 54%, #ffffff40) var(--glass-tint);
  border: 1px solid #fffdf5bd; border-radius: 999px;
  box-shadow: inset 0 1px 3px #fffdf5eb, inset 0 -2px 4px #ffffff50,
    inset 0 -6px 12px #25453940, 0 4px 13px #163c3150;
  backdrop-filter: blur(12px) saturate(145%);
  -webkit-backdrop-filter: blur(12px) saturate(145%);
  touch-action: manipulation;
  transition: filter .16s, scale .16s;
}
.glass-button:not(:disabled):active { scale: .92; }
#play-controls img, #play-controls svg { width: 57%; height: 57%; pointer-events: none; }
#play-controls button:disabled { cursor: default; }
#play-controls button:disabled img { opacity: .48; }
#scene-picker { position: relative; }
#scene-options button {
  --stack-row: var(--grid-row);
  --stack-column: var(--grid-column);
  position: absolute; top: 50%; left: 50%; width: 46px;
  translate: -50% -50%;
  transform: scale(.5);
  opacity: 0;
  pointer-events: none;
  transition: transform .24s ease-in, opacity .18s ease-in, scale .16s;
}
#scene[aria-expanded="true"] + #scene-options button {
  transform: translate(calc(52px * var(--stack-column)),
    calc(-1 * (var(--control-size) / 2 + 29px + 52px * var(--stack-row)))) scale(1);
  opacity: 1;
  pointer-events: auto;
  transition: transform .32s cubic-bezier(.2, .8, .34, 1.25) var(--reveal-delay),
    opacity .2s ease-out var(--reveal-delay), scale .16s;
}
#scene[aria-expanded="true"] + #scene-options button:disabled {
  box-shadow: inset 0 0 0 2px #3158488c, 0 3px 10px #163c3140;
}
#board {
  position: absolute; top: 50%; left: var(--play-center);
  translate: -50% -50%; width: min(calc(100% - var(--chest-width) - var(--edge)), 860px);
  height: min(100%, 650px); isolation: isolate; transform-origin: 50% 78%;
}
.slot {
  position: absolute; background: transparent; border: 0; padding: 0; transform: translate(calc(-50% + var(--gallop-x, 0%)), calc(-50% + var(--gallop-y, 0%)));
  transition: transform .14s ease-out;
  touch-action: none; display: grid; place-items: center; pointer-events: none;
}
.slot::before { content: ""; position: absolute; inset: 27% 23%; pointer-events: auto; border-radius: 45%; }
.slot:focus-visible { outline-offset: -10px; }
.slot img, .tile img, #drag img { width: 100%; height: 100%; object-fit: contain; pointer-events: none; }
.slot.empty img { filter: brightness(0) saturate(100%); opacity: .22; }
#stage[data-scene="ocean"] .slot.head.empty img { translate: 0 16%; }
/* Short tails have extra transparent canvas on the right. Keep their tips
   exposed while the root stays behind the body. */
.slot.tail:is([data-animal="polar-bear"], [data-animal="sheep"], [data-animal="pig"]) img {
  translate: 17% 0;
}
.slot.tail[data-animal="chicken"] img { translate: 8% 0; }
#laid-egg {
  --egg-fall: clamp(27px, 10dvh, 75px);
  position: absolute; z-index: 0; left: 70%; top: 55%;
  width: clamp(16px, 5%, 34px); aspect-ratio: 4 / 5;
  border-radius: 52% 52% 48% 48% / 64% 64% 36% 36%;
  background: radial-gradient(at 34% 28%, #fffdf4, #f6dfaf 65%, #cca679);
  box-shadow: 0 2px 4px #3f352050; opacity: 0; pointer-events: none;
}
.slot.target { filter: drop-shadow(0 0 4px #fff6ba) drop-shadow(0 0 6px #fff6ba); }
.slot.empty.target img { opacity: .42; }
#chest {
  --tray-top: 27.5%;
  --tray-bottom: calc(4% + 5px);
  --shortcut-height: clamp(30px, 5dvh, 44px);
  --pieces-top: calc(var(--tray-top) + var(--shortcut-height) + 4px);
  --scroll-offset: 7%;
  --scroll-width: clamp(44px, 5vw, 64px);
  position: absolute; right: var(--edge);
  top: var(--edge);
  bottom: var(--edge); width: var(--chest-width);
  background: url("assets/chest.png") center / 100% 100% no-repeat;
  filter: drop-shadow(0 8px 11px #2b241a55);
  transition: filter .18s ease-out;
}
#chest.return-target {
  filter: drop-shadow(0 0 7px #ffe2a2) drop-shadow(0 0 3px #fff0b8) drop-shadow(0 8px 11px #2b241a55);
}
#part-shortcuts {
  position: absolute; top: var(--tray-top); left: 8%; right: var(--scroll-offset);
  display: grid; grid-template-columns: repeat(4, clamp(34px, 4.6vw, 48px));
  justify-content: center; gap: 4px; height: var(--shortcut-height);
}
#part-shortcuts button {
  display: grid; place-items: center; padding: 0; border: 0;
  background: transparent; touch-action: manipulation;
  filter: drop-shadow(0 1px 1px #34271966);
}
#part-shortcuts button:disabled { opacity: .42; filter: none; cursor: default; }
#part-shortcuts img {
  width: min(88%, 40px); height: calc(var(--shortcut-height) * .92);
  object-fit: contain; pointer-events: none;
}
#tray {
  position: absolute; inset: var(--pieces-top) 0 var(--tray-bottom);
  padding-inline: 8% calc(var(--scroll-offset) + var(--scroll-width) + 5px);
  display: grid; grid-auto-rows: max-content; align-content: start; gap: 1px;
  overflow: hidden;
  touch-action: none;
}
#tray .part-group {
  display: grid; grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-auto-rows: clamp(48px, 9dvh, 90px); gap: 1px 2px;
}
#tray .part-group + .part-group { margin-top: clamp(18px, 4dvh, 34px); }
#tray-scroll {
  --clasp-height: calc(var(--scroll-width) * 120 / 148);
  --clasp-art: url("assets/chest-clasp.png") center / 100% 100% no-repeat;
  --clasp-shadow: drop-shadow(0 2px 1px #3f251d80);
  position: absolute; z-index: 1; top: var(--pieces-top); right: var(--scroll-offset);
  height: calc(100% - var(--pieces-top) - var(--tray-bottom)); width: var(--scroll-width);
  margin: 0; writing-mode: vertical-lr; direction: ltr; appearance: none; -webkit-appearance: none;
  background: url("assets/scroll-track.svg") center / 50% 100% no-repeat;
  cursor: ns-resize; touch-action: none;
}
#tray-scroll:focus-visible { outline: 2px solid #fff1b7; outline-offset: 1px; }
#tray-scroll::-webkit-slider-runnable-track {
  width: 100%; height: 100%;
  background: transparent;
}
#tray-scroll::-webkit-slider-thumb {
  appearance: none; -webkit-appearance: none; width: var(--scroll-width); height: var(--clasp-height);
  border: 0; background: var(--clasp-art);
  filter: var(--clasp-shadow);
}
#tray-scroll::-moz-range-track {
  width: 100%; height: 100%; background: transparent;
}
#tray-scroll::-moz-range-thumb {
  width: var(--scroll-width); height: var(--clasp-height); border: 0;
  background: var(--clasp-art);
  filter: var(--clasp-shadow);
}
.tile {
  min-width: 0; min-height: 0; padding: 1px; background: transparent; border: 1px solid transparent;
  border-radius: 10px; touch-action: none;
}
.tile img { display: block; filter: drop-shadow(0 2px 2px #42281960); }
.tile[aria-pressed="true"] { background: #fff3bd5c; border-color: #fff0ac; }
#drag {
  position: fixed; z-index: 30; pointer-events: none; transform: translate(-50%, -50%);
  width: clamp(82px, 14vw, 160px); aspect-ratio: 1; filter: drop-shadow(0 6px 5px #24362755);
}
.celebrate { animation: dance 2s ease-in-out; }
@keyframes dance {
  0%, 100% { rotate: 0deg; transform: translateY(0); }
  12%, 48%, 80% { rotate: -5deg; transform: translateY(-5px); }
  30%, 65%, 90% { rotate: 5deg; transform: translateY(-9px); }
}
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
@media (orientation: portrait) {
  #stage { --chest-width: 44vw; }
  #board { height: min(62%, 480px); }

  #tray { gap: 0; }
}
@media (orientation: portrait) and (max-width: 600px) {
  #stage { --play-height: 48%; --play-center: 50%; --controls-bottom: calc(100% - var(--play-height) + 8px); }
  #board {
    top: env(safe-area-inset-top); translate: -50% 0;
    width: 100%; height: calc(var(--play-height) - env(safe-area-inset-top));
  }
  #chest {
    top: var(--play-height); bottom: max(10px, env(safe-area-inset-bottom));
    left: max(10px, env(safe-area-inset-left)); right: max(10px, env(safe-area-inset-right));
    width: auto;
  }
}

.gallop { z-index: 4; animation: gallop-around 1.65s linear; }
/* Move pieces toward the body, then give them a shared lift each stride. */
.gallop .slot { animation: gallop-bounce .27s ease-in-out infinite; }
.gallop .head { --gallop-x: 9%; --gallop-y: 3%; }
.gallop .tail { --gallop-x: -8%; }
.gallop .back { --gallop-x: -5%; --gallop-y: -5%; }
.gallop .front { --gallop-x: 6%; --gallop-y: -5%; }
.gallop .back img, .gallop .front img {
  transform-origin: 50% 33%;
  animation: gallop-stride .27s ease-in-out infinite;
}
.gallop .front img { animation-delay: -.135s; }
@keyframes gallop-around {
  0%, 100% { transform: translateX(0); }
  49% { transform: translateX(var(--exit-left)); }
  49.01% { transform: translateX(var(--enter-right)); }
}
@keyframes gallop-bounce {
  0%, 100% { translate: 0 0; }
  50% { translate: 0 -7px; }
}
@keyframes gallop-stride {
  0%, 100% { rotate: -9deg; }
  50% { rotate: 9deg; }
}

#welcome {
  width: min(92vw, 720px); max-height: calc(100dvh - 24px); padding: 0;
  border: 2px solid #fff3b4; border-radius: 28px; color: #fff7da;
  background: linear-gradient(135deg, #183822ee 7%, #254d36a6 58%, #163923c7),
    url("assets/jungle.jpg") center / cover, #254d36;
  box-shadow: 0 15px 60px #14271fcc, inset 0 0 30px #162c2eb3;
}
#welcome::backdrop { background: #20262460; backdrop-filter: grayscale(1) brightness(.7); }
.welcome-content {
  display: grid; grid-template-columns: 1fr .85fr; grid-template-rows: auto auto;
  align-items: center; gap: 8px;
  padding: clamp(10.667px, 2.667vw, 24px) clamp(16px, 4vw, 36px);
}
.welcome-copy { text-align: center; z-index: 1; }
#game-title {
  margin: 0; color: #ffde67; font-family: ui-rounded, "Arial Rounded MT Bold", system-ui, sans-serif;
  font-weight: 1000; font-size: clamp(2.6rem, 8.4vw, 5.4rem); line-height: .85;
  letter-spacing: -.075em;
  text-shadow: 0 .06em #ab4a27, 0 .115em #532919, 0 .15em .13em #102b18;
  -webkit-text-stroke: .012em #fff2a6; transform: rotate(-5deg);
}
.logo-word { display: block; white-space: nowrap; }
.logo-word span { display: inline-block; transform: translateY(var(--lift, 0)) rotate(var(--tilt, 0deg)); }
.logo-word span:nth-child(3n + 1) { --tilt: -6deg; --lift: .02em; }
.logo-word span:nth-child(3n + 2) { --tilt: 5deg; --lift: -.025em; }
.logo-word span:nth-child(3n) { --tilt: -3deg; --lift: .035em; }
.logo-word span:nth-child(4n + 1) { color: #ffe585; }
.logo-word + .logo-word { color: #ffb75d; font-size: .65em; margin-top: .16em; transform: rotate(3deg); }
#welcome-description { font-size: clamp(1rem, 2.5vw, 1.22rem); font-weight: 650; margin: 20px 0 0; }
#welcome-animal {
  position: relative; align-self: stretch; grid-column: 2; grid-row: 1 / 3;
  min-height: clamp(130px, 40dvh, 285px); transform: rotate(-12deg) scale(.95);
  filter: drop-shadow(0 6px 5px #142b1ec0);
}
#welcome-animal img { position: absolute; object-fit: contain; transform: translate(-50%, -50%); }
#start {
  justify-self: center; width: min(292px, 100%); aspect-ratio: 40 / 13; min-height: 78px; padding: 5px 49px 5px 12px;
  border: 0; background: url("assets/start-sign.svg") center / 100% 100% no-repeat;
  rotate: -4deg; filter: drop-shadow(0 5px 4px #102617a6);
  animation: sway-sign 1.65s ease-in-out infinite;
  color: #fff6db; text-shadow: 0 2px 1px #4d210e;
  font-size: 1.45rem; font-weight: 850; touch-action: manipulation;
}
#start:focus-visible { outline-offset: 0; }
#start:active { scale: .96; }
@keyframes sway-sign {
  0%, 100% { rotate: -4deg; }
  50% { rotate: 3deg; }
}
#tutorial {
  position: absolute; z-index: 12; top: max(10px, env(safe-area-inset-top));
  left: var(--play-center); translate: -50% 0; width: min(90%, 338px);
  padding: 9px 12px; border: 1px solid #fff4cb; border-radius: 16px;
  background: #fff6dcf5; box-shadow: 0 4px 20px #29362755;
  display: grid; grid-template-columns: 66px 1fr; align-items: center; gap: 4px 8px;
}
#tutorial[hidden] { display: none; }
#tutorial-tip { margin: 0; line-height: 1.25; font-weight: 650; }
.tutorial-actions { grid-column: 2; display: flex; justify-content: flex-end; gap: 6px; }
.tutorial-actions button {
  min-height: 36px; padding: 4px 12px; font-size: .875rem; font-weight: 750;
}
#next-tip {
  border-color: #586f5f; color: #183f31;
}
.tutorial-actions button:focus-visible { outline-color: #315743; }
#tutorial-demo {
  height: 48px; position: relative; display: flex; align-items: center;
  justify-content: center; border-radius: 8px; background: #ead8ac;
}
#tutorial-demo img { min-width: 0; width: 24%; height: 100%; object-fit: contain; }
[data-tip="drag"] #tutorial-demo img { position: absolute; width: 50%; }
[data-tip="drag"] #tutorial-demo img:first-child { left: 4px; filter: brightness(0); opacity: .25; }
[data-tip="drag"] #tutorial-demo img:last-child { right: 2px; animation: show-drag 1.8s ease-in-out infinite; }
[data-tip="scroll"] #tutorial-demo::before {
  position: absolute; content: ""; width: 10px; height: 90%; border-radius: 9px; background: #875837;
}
[data-tip="scroll"] #tutorial-demo img { width: 29px; z-index: 1; animation: show-scroll 1.8s ease-in-out infinite alternate; }
[data-tip="shortcuts"] #part-shortcuts, [data-tip="scroll"] #tray-scroll {
  outline: 2px solid #fff2a3; outline-offset: 2px; border-radius: 10px;
}
[data-tip="reset"] #tutorial-demo img, [data-tip="mute"] #tutorial-demo img, [data-tip="egg"] #tutorial-demo img { width: 32px; }
[data-tip="scene"] #tutorial-demo svg { width: 32px; height: 32px; }
[data-tip="scene"] #scene, [data-tip="reset"] #reset, [data-tip="mute"] #mute, [data-tip="egg"] #lay-egg { outline: 2px solid #fff2a3; outline-offset: 2px; }
@keyframes show-drag {
  0%, 15% { transform: translateX(0); }
  65%, 85% { transform: translateX(-27px); }
  100% { transform: translateX(0); }
}
@keyframes show-scroll { from { transform: translateY(-11px); } to { transform: translateY(11px); } }

@media (orientation: portrait) and (max-width: 600px) {
  #stage { --control-size: clamp(46px, 6dvh, 58px); }
  .welcome-content { grid-template-columns: 1fr; }
  #welcome-animal { grid-column: 1; grid-row: 2; min-height: clamp(105px, 31dvh, 245px); }
}
@media (orientation: landscape) and (max-height: 450px) {
  #stage { --control-size: 46px; }
}
@media (orientation: portrait) and (max-width: 600px) and (max-height: 420px),
       (orientation: landscape) and (max-height: 215px) {
  #scene-options button {
    --stack-row: var(--short-row);
    --stack-column: var(--short-column);
  }
}
@media (prefers-reduced-motion: reduce) {
  .celebrate, .gallop, .gallop .slot, .gallop .slot img, #start, #stage #tutorial-demo img { animation: none; }
  .slot { transition: none; }
  .glass-button, #scene[aria-expanded] + #scene-options button, #chest { transition: none; }
}
