* { 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 {
  --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;
}
#play-controls {
  position: absolute; bottom: var(--controls-bottom);
  left: calc(var(--play-center) - var(--control-size) / 2);
  z-index: 10; display: flex; gap: 8px;
}
#play-controls button {
  display: grid; place-items: center; width: var(--control-size); aspect-ratio: 1;
  background: #fff6df99; border: 1px solid #786f504d; border-radius: 50%;
  box-shadow: 0 3px 9px #323e2c20;
  touch-action: manipulation;
}
#play-controls img { width: 57%; height: 57%; pointer-events: none; }
#reset:disabled { opacity: .5; cursor: default; }
#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(-50%, -50%);
  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; }
.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);
}
#chest.return-target {
  filter: drop-shadow(0 0 6px #ffe2a2) 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 button::before {
  content: ""; width: min(78%, 34px); height: calc(var(--shortcut-height) * .85);
  background: #5c5851a0;
  -webkit-mask: var(--part-icon) center / contain no-repeat;
  mask: var(--part-icon) center / contain no-repeat;
}
#part-shortcuts button:active::before { background: #37342fcc; }
#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 {
  position: relative; margin-top: 4px; padding-top: 8px;
}
#tray .part-group + .part-group::before {
  content: ""; position: absolute; top: 2px; inset-inline: 7%; height: 3px;
  background: linear-gradient(90deg, transparent, #653c2670 10% 90%, transparent) top / 100% 2px no-repeat,
    linear-gradient(90deg, transparent, #ffd49d80 10% 90%, transparent) bottom / 100% 1px no-repeat;
  rotate: -.8deg; pointer-events: none;
}
#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: linear-gradient(90deg, transparent 25%, #633e24ad 25% 75%, transparent 75%);
  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;
  }
}
@media (prefers-reduced-motion: reduce) { .celebrate { animation: none; } }
