:root {
  --bg-top: #fff2cf;
  --bg-bottom: #c8ecff;
  --panel: rgba(255, 252, 244, 0.88);
  --panel-strong: rgba(255, 255, 255, 0.84);
  --text: #2d3a3a;
  --muted: #68757a;
  --accent: #ff9f68;
  --accent-strong: #ff7d3c;
  --shadow: rgba(58, 65, 92, 0.14);
  --success: #81c75d;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Marker Felt", "Comic Sans MS", "Trebuchet MS", sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at top left, rgba(255, 255, 255, 0.98), transparent 30%),
    radial-gradient(circle at bottom right, rgba(255, 230, 150, 0.68), transparent 28%),
    linear-gradient(180deg, var(--bg-top), var(--bg-bottom));
}

.puzzle-app {
  width: min(1180px, calc(100% - 24px));
  margin: 0 auto;
  padding: 18px 0 28px;
  min-height: 100svh;
}

.puzzle-hero,
.control-panel,
.play-zone,
.board-panel,
.tray-card {
  background: var(--panel);
  border: 3px solid rgba(255, 255, 255, 0.72);
  border-radius: 28px;
  box-shadow: 0 16px 32px var(--shadow);
  backdrop-filter: blur(8px);
}

.puzzle-hero {
  padding: 20px 22px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
}

.eyebrow,
.small-label,
.control-card span {
  margin: 0;
  font-size: 0.95rem;
  color: var(--muted);
}

.control-card small {
  color: var(--muted);
  line-height: 1.3;
}

h1 {
  margin: 4px 0 8px;
  font-size: clamp(2.2rem, 4vw, 3.5rem);
  line-height: 0.95;
}

.intro {
  max-width: 54ch;
  margin: 0;
  font-size: 1.08rem;
  line-height: 1.35;
}

.hero-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.secondary-link,
.primary-button {
  border-radius: 999px;
  padding: 12px 18px;
  text-decoration: none;
  font: inherit;
  cursor: pointer;
}

.secondary-link {
  background: rgba(255, 255, 255, 0.72);
  color: var(--text);
}

.primary-button {
  border: 0;
  background: linear-gradient(180deg, var(--accent), var(--accent-strong));
  color: #fffdf7;
  font-size: 1.1rem;
  box-shadow: 0 10px 18px rgba(255, 125, 60, 0.22);
}

.control-panel {
  margin-top: 14px;
  padding: 10px;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.control-card {
  padding: 10px 12px;
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.88);
  display: grid;
  gap: 4px;
  align-content: center;
  min-height: 108px;
}

.play-zone {
  position: relative;
  margin-top: 10px;
  padding: 16px 18px 18px;
  background: var(--panel);
  border: 3px solid rgba(255, 255, 255, 0.72);
  box-shadow: 0 16px 32px var(--shadow);
  backdrop-filter: blur(8px);
  overflow: hidden;
}

.play-zone-top {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(320px, 420px) minmax(0, 1fr);
  gap: 12px;
  align-items: center;
  position: relative;
  z-index: 1;
}

.play-zone-main {
  margin-top: 8px;
  display: grid;
  grid-template-columns: 132px minmax(0, 1fr) 132px;
  gap: 18px;
  align-items: stretch;
  position: relative;
  z-index: 1;
}

.preview-card {
  padding: 0;
  border-radius: 0;
  background: transparent;
  display: grid;
  gap: 0;
}

.preview-card-model {
  justify-items: center;
  width: min(100%, 360px);
  margin: 0 auto;
}

.zone-copy {
  justify-self: start;
}

.zone-copy strong {
  display: block;
  font-size: clamp(1.4rem, 2.4vw, 2rem);
  line-height: 0.95;
}

.board-panel {
  padding: 0;
  display: grid;
  background: transparent;
  align-content: center;
  border: 0;
  box-shadow: none;
  backdrop-filter: none;
}

.tray-panel {
  display: grid;
  align-self: stretch;
}

.tray-card {
  padding: 0;
}

.tray-card-side {
  height: 100%;
  align-content: start;
  padding: 6px 2px;
  background: transparent;
  border: 0;
  box-shadow: none;
  backdrop-filter: none;
  border-radius: 0;
}

.board-header,
.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.board-count {
  font-size: 1.1rem;
  justify-self: end;
  text-align: right;
}

.puzzle-board {
  --piece-ratio: 1;
  margin-top: 0;
  display: grid;
  gap: 8px;
  width: min(100%, 820px, calc(100vw - 420px), calc(100svh - 270px));
  aspect-ratio: 3 / 2;
  max-width: 100%;
  margin-left: auto;
  margin-right: auto;
  align-items: stretch;
}

.puzzle-burst {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 30;
}

.puzzle-flake {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 22px;
  height: 22px;
  margin: -11px 0 0 -11px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, rgba(255, 255, 255, 0.98), var(--flake-core, #ffd86b) 58%, var(--flake-edge, #ff9f68) 100%);
  box-shadow:
    0 0 0 4px rgba(255, 255, 255, 0.18),
    0 10px 18px rgba(128, 168, 190, 0.24);
  opacity: 0;
  animation: flake-burst 1.2s ease-out forwards;
  will-change: transform, opacity;
}

.puzzle-board.is-complete {
  animation: puzzle-win-glow 1s ease-in-out 2;
}

.puzzle-slot,
.piece-button {
  aspect-ratio: var(--piece-ratio);
  border-radius: 18px;
  overflow: hidden;
  position: relative;
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.72);
}

.puzzle-slot {
  background:
    linear-gradient(180deg, rgba(255, 250, 240, 0.95), rgba(240, 230, 210, 0.95)),
    repeating-linear-gradient(45deg, rgba(255, 183, 121, 0.12), rgba(255, 183, 121, 0.12) 12px, rgba(255, 255, 255, 0.12) 12px, rgba(255, 255, 255, 0.12) 24px);
  border: 0;
  padding: 0;
}

.puzzle-slot.is-correct {
  box-shadow: inset 0 0 0 4px rgba(129, 199, 93, 0.65);
}

.puzzle-slot.has-piece {
  cursor: default;
}

.piece-tray {
  --piece-ratio: 1;
  margin-top: 0;
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  align-content: start;
  justify-items: center;
  min-height: 100%;
}

.piece-tray-slot {
  width: min(100%, 108px);
  aspect-ratio: var(--piece-ratio);
  border-radius: 16px;
}

.piece-tray-slot.is-empty {
  background: transparent;
  box-shadow: none;
}

.piece-tray-zigzag .piece-tray-slot:nth-child(even) {
  transform: translateX(12px);
}

.piece-button {
  border: 0;
  padding: 0;
  background: #fffaf4;
  cursor: grab;
  transition: transform 160ms ease, box-shadow 160ms ease;
  width: 100%;
}

.piece-button:hover,
.piece-button:focus-visible {
  transform: translateY(-2px) scale(1.02);
}

.piece-button.is-locked {
  opacity: 0.45;
  cursor: not-allowed;
}

.piece-button.dragging {
  opacity: 0.45;
}

.piece-image {
  width: 100%;
  height: 100%;
  background-repeat: no-repeat;
  background-size: cover;
}

.mini-preview {
  width: min(100%, 320px);
  aspect-ratio: 3 / 2;
  border-radius: 20px;
  background-image: url("assets/scene-garden.svg");
  background-size: cover;
  background-position: center;
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.72);
}

.puzzle-success {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(255, 249, 238, 0.42);
  backdrop-filter: blur(4px);
  z-index: 25;
}

.puzzle-success-card {
  min-width: min(100%, 320px);
  padding: 22px 20px;
  border-radius: 28px;
  display: grid;
  gap: 12px;
  justify-items: center;
  background: rgba(255, 251, 243, 0.97);
  box-shadow: 0 24px 42px rgba(58, 65, 92, 0.2);
  animation: puzzle-success-pop 360ms ease-out;
}

.puzzle-success-card strong {
  font-size: clamp(1.6rem, 3vw, 2.2rem);
  line-height: 0.95;
  text-align: center;
}

.puzzle-success-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
}

.puzzle-replay-button {
  margin-top: 4px;
}

.puzzle-next-button {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  border: 0;
  cursor: pointer;
}

.puzzle-next-thumb {
  width: 42px;
  height: 42px;
  border-radius: 14px;
  background-image: url("assets/scene-castle.svg");
  background-size: cover;
  background-position: center;
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.72);
  flex-shrink: 0;
}

.slot-remove {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 28px;
  height: 28px;
  border: 0;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: rgba(255, 251, 243, 0.96);
  color: #8f5c43;
  font: inherit;
  line-height: 1;
  box-shadow: 0 8px 14px rgba(58, 65, 92, 0.14);
  cursor: pointer;
}

.cursor-piece {
  --piece-ratio: 1;
  position: fixed;
  top: 0;
  left: 0;
  width: min(20vw, 132px);
  aspect-ratio: var(--piece-ratio);
  pointer-events: none;
  transform: translate(-50%, -50%) scale(1.04);
  z-index: 20;
  border-radius: 18px;
  overflow: hidden;
  box-shadow:
    0 16px 26px rgba(58, 65, 92, 0.18),
    inset 0 0 0 3px rgba(255, 255, 255, 0.72);
}

.cursor-piece .piece-image {
  width: 100%;
  height: 100%;
}

.hidden {
  display: none;
}

.size-picker,
.image-picker-shell,
.image-picker {
  display: grid;
  gap: 6px;
}

.image-picker-shell {
  grid-template-columns: 34px minmax(0, 1fr) 34px;
  grid-template-rows: 1fr;
  align-items: center;
}

.size-picker {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.size-choice,
.image-choice {
  border: 0;
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.82);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}

.size-choice {
  padding: 10px 8px;
  font-size: 1.1rem;
}

.size-choice.active,
.image-choice.active {
  box-shadow: inset 0 0 0 3px rgba(255, 159, 104, 0.45);
  background: rgba(255, 245, 229, 0.95);
}

.image-choice {
  min-width: 112px;
  padding: 8px;
  display: flex;
  align-items: center;
  gap: 8px;
  text-align: left;
  flex-shrink: 0;
}

.image-picker {
  display: flex;
  align-items: stretch;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 2px;
  scroll-behavior: smooth;
}

.image-picker::-webkit-scrollbar {
  height: 6px;
}

.image-picker::-webkit-scrollbar-thumb {
  border-radius: 999px;
  background: rgba(255, 159, 104, 0.45);
}

.image-scroll-button {
  border: 0;
  border-radius: 16px;
  width: 38px;
  height: 56px;
  padding: 0;
  display: grid;
  place-items: center;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.98), rgba(255, 241, 226, 0.96)),
    rgba(255, 255, 255, 0.95);
  color: #945d36;
  font: inherit;
  font-size: 0;
  box-shadow:
    0 12px 20px rgba(255, 159, 104, 0.16),
    inset 0 0 0 1px rgba(255, 255, 255, 0.8);
  cursor: pointer;
}

.image-scroll-button::before {
  content: "";
  width: 12px;
  height: 12px;
  border-top: 4px solid currentColor;
  border-right: 4px solid currentColor;
  transform: rotate(45deg);
}

#imageScrollUp::before {
  transform: rotate(-135deg);
  margin-left: 4px;
}

#imageScrollDown::before {
  transform: rotate(45deg);
  margin-right: 4px;
}

.image-choice.is-locked {
  opacity: 0.5;
  cursor: not-allowed;
}

.image-choice-thumb {
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  border-radius: 12px;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.72);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@keyframes puzzle-win-glow {
  0%,
  100% {
    transform: scale(1);
    filter: drop-shadow(0 0 0 rgba(255, 211, 112, 0));
  }

  50% {
    transform: scale(1.02);
    filter: drop-shadow(0 0 18px rgba(255, 211, 112, 0.45));
  }
}

@keyframes puzzle-success-pop {
  0% {
    transform: scale(0.88);
    opacity: 0;
  }

  100% {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes flake-burst {
  0% {
    transform: translate(0, 0) scale(0.3) rotate(0deg);
    opacity: 0;
  }

  20% {
    opacity: 1;
  }

  100% {
    transform: translate(var(--burst-x), var(--burst-y)) scale(1.05) rotate(180deg);
    opacity: 0;
  }
}

.scene-garden-thumb { background-image: url("assets/scene-garden.svg"); }
.scene-castle-thumb { background-image: url("assets/scene-castle.svg"); }
.scene-sea-thumb { background-image: url("assets/scene-sea.svg"); }

@media (max-width: 980px) {
  .control-panel,
  .play-zone-main {
    grid-template-columns: 1fr;
  }

  .play-zone-top {
    grid-template-columns: 1fr;
    justify-items: center;
    text-align: center;
  }

  .zone-copy,
  .board-count {
    justify-self: center;
    text-align: center;
  }

  .puzzle-board {
    width: min(100%, 520px, calc(100svh - 360px));
  }

  .tray-panel {
    display: block;
  }

  .tray-card-side {
    height: auto;
  }

  .piece-tray {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    justify-items: stretch;
    min-height: auto;
  }

  .piece-tray-slot {
    width: 100%;
  }

  .piece-tray-zigzag .piece-tray-slot:nth-child(even) {
    transform: none;
  }
}

@media (max-width: 640px) {
  .puzzle-app {
    width: min(100% - 14px, 1180px);
    padding-top: 10px;
  }

  .puzzle-hero {
    padding: 16px;
    flex-direction: column;
    align-items: stretch;
  }

  .hero-actions,
  .board-header,
  .section-head {
    flex-direction: column;
    align-items: stretch;
  }

  .puzzle-board {
    width: min(100%, 360px, calc(100svh - 390px));
  }

  .piece-tray {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .image-picker-shell {
    grid-template-columns: 30px minmax(0, 1fr) 30px;
  }
}
