:root {
  --vh: 1vh;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: #efe8dc;
  font-family: Arial, "Helvetica Neue", sans-serif;
  touch-action: none;
}

button {
  padding: 0;
  border: 0;
  background: none;
  font: inherit;
}

.puzzle-stage {
  position: fixed;
  left: 50%;
  top: 50%;
  width: min(100vw, calc(var(--vh) * 100 * 1500 / 2667));
  aspect-ratio: 1500 / 2667;
  overflow: hidden;
  transform: translate(-50%, -50%);
  background: #f7efdf url("./assets/puzzle/bg.png") center / 100% 100% no-repeat;
  container-type: inline-size;
}

.puzzle-info {
  position: absolute;
  right: 8%;
  top: 4.5%;
  z-index: 30;
  width: 15%;
  aspect-ratio: 94 / 99;
}

.puzzle-info img,
.puzzle-start img {
  width: 100%;
  height: 100%;
  display: block;
}

.puzzle-board {
  position: absolute;
  left: 17%;
  top: 15.9%;
  z-index: 5;
  width: 66%;
  aspect-ratio: 529 / 847;
}

.puzzle-frame {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  z-index: 8;
}

.piece-layer {
  position: absolute;
  inset: 0;
  z-index: 12;
}

.puzzle-piece {
  position: absolute;
  z-index: 35;
  left: var(--x);
  top: var(--y);
  width: var(--piece-w);
  height: var(--piece-h);
  object-fit: contain;
  touch-action: none;
  cursor: grab;
  filter: drop-shadow(0 1.3cqw 0.9cqw rgba(84, 54, 30, 0.18));
  transform: rotate(var(--r));
  transition: filter 140ms ease;
  user-select: none;
  -webkit-user-drag: none;
  -webkit-touch-callout: none;
  will-change: left, top, transform;
}

.is-scrambling .puzzle-piece {
  pointer-events: none;
  transition:
    left 940ms cubic-bezier(0.2, 0.86, 0.22, 1),
    top 940ms cubic-bezier(0.2, 0.86, 0.22, 1),
    transform 940ms cubic-bezier(0.2, 0.86, 0.22, 1),
    filter 140ms ease;
}

.is-ready .puzzle-piece {
  pointer-events: none;
}

.puzzle-piece.is-dragging {
  z-index: 80;
  cursor: grabbing;
  filter: drop-shadow(0 2cqw 1.4cqw rgba(84, 54, 30, 0.24));
  transition: none;
}

.puzzle-piece.is-correct {
  z-index: 6;
}

/* 吸附 / 彈回待機位置的補間動畫 */
.puzzle-piece.is-snapping {
  transition:
    left 280ms cubic-bezier(0.2, 0.86, 0.22, 1),
    top 280ms cubic-bezier(0.2, 0.86, 0.22, 1),
    transform 280ms cubic-bezier(0.2, 0.86, 0.22, 1),
    filter 140ms ease;
}

.puzzle-start {
  position: absolute;
  left: 24%;
  bottom: 12%;
  z-index: 60;
  width: 52%;
  aspect-ratio: 310 / 99;
  filter: drop-shadow(0 1.1cqw 0 rgba(90, 44, 20, 0.18));
  transform-origin: 50% 50%;
}

.puzzle-start__end {
  display: none;
}

.is-ready .puzzle-start {
  animation: start-sway 1.3s ease-in-out infinite;
}

@keyframes start-sway {
  0%,
  100% {
    transform: translateX(-2.4%) rotate(-1.6deg);
  }
  50% {
    transform: translateX(2.4%) rotate(1.6deg);
  }
}

@media (prefers-reduced-motion: reduce) {
  .is-ready .puzzle-start {
    animation: none;
  }
}

.is-scrambling .puzzle-start {
  pointer-events: none;
}

/* 開始後：START 縮小改為「結束遊戲」，外觀比照 START 按鈕 */
.is-started .puzzle-start {
  left: 32%;
  bottom: 15%;
  width: 36%;
  aspect-ratio: 310 / 99;
  height: auto;
  filter: drop-shadow(0 1cqw 0.5cqw rgba(120, 55, 20, 0.32));
  animation: none;
  transform: none;
}

.is-started .puzzle-start__img {
  display: none;
}

.is-started .puzzle-start__end {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  border: 0.75cqw solid #fff4e4;
  border-radius: 999px;
  color: #fff;
  font-size: 4.6cqw;
  font-weight: 950;
  letter-spacing: 0.05em;
  text-shadow: 0 0.4cqw 0 rgba(150, 60, 15, 0.5);
  background:
    linear-gradient(rgba(255, 255, 255, 0.55), rgba(255, 255, 255, 0) 46%),
    linear-gradient(#f7a24d, #e8641f);
  box-shadow:
    inset 0 -0.9cqw 0 rgba(160, 70, 20, 0.34),
    inset 0 0.7cqw 0 rgba(255, 255, 255, 0.5);
}

.help-card {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 90;
  width: 68%;
  display: none;
  padding: 4.8% 5.4% 5.2%;
  border: 0.55cqw solid rgba(244, 119, 52, 0.22);
  border-radius: 4.5cqw;
  color: #794321;
  background:
    linear-gradient(rgba(255, 255, 255, 0.68), rgba(255, 255, 255, 0) 38%),
    #fff7dc;
  box-shadow:
    0 2.4cqw 0 rgba(169, 91, 39, 0.13),
    0 4cqw 7cqw rgba(95, 55, 26, 0.2);
  transform: translate(-50%, -50%);
}

.show-help .puzzle-stage::after,
.show-win .puzzle-stage::after,
.show-confirm .puzzle-stage::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 85;
  background: rgba(255, 247, 232, 0.34);
  backdrop-filter: blur(1.2px);
}

.show-win .puzzle-stage::after,
.show-confirm .puzzle-stage::after {
  background: rgba(60, 34, 14, 0.42);
  backdrop-filter: blur(2px);
}

.show-help .help-card {
  display: block;
}

.help-title {
  color: #f47734;
  font-size: 4.1cqw;
  font-weight: 950;
  line-height: 1;
  text-align: center;
  text-shadow: 0 0.28cqw 0 white;
  margin-bottom: 1.6cqw;
}

.help-steps {
  margin: 0;
  padding: 0;
  display: grid;
  gap: 1.15cqw;
  list-style: none;
}

.help-steps li {
  min-height: 6.6cqw;
  display: flex;
  align-items: center;
  padding: 1.45cqw 1.8cqw;
  border-radius: 2.2cqw;
  background: rgba(255, 255, 255, 0.62);
  box-shadow: inset 0 0 0 0.25cqw rgba(244, 119, 52, 0.12);
  color: #794321;
  font-size: 2.35cqw;
  font-weight: 850;
  line-height: 1.24;
}

.help-close {
  position: absolute;
  right: -2.8cqw;
  top: -2.8cqw;
  width: 7.2cqw;
  aspect-ratio: 1;
  border-radius: 50%;
  color: white;
  background:
    linear-gradient(rgba(255, 255, 255, 0.35), rgba(255, 255, 255, 0) 45%),
    #f47734;
  box-shadow: 0 0.9cqw 0 rgba(174, 80, 30, 0.24);
  font-size: 4.4cqw;
  font-weight: 950;
  line-height: 1;
}

/* 完成彈窗 / 結束確認彈窗（共用外觀） */
.win-card,
.confirm-card {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 90;
  width: 72%;
  display: none;
  padding: 6.5% 6% 6%;
  text-align: center;
  border: 0.55cqw solid rgba(244, 119, 52, 0.22);
  border-radius: 5cqw;
  color: #794321;
  background:
    linear-gradient(rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 0) 40%),
    #fff7dc;
  box-shadow:
    0 2.4cqw 0 rgba(169, 91, 39, 0.13),
    0 4cqw 8cqw rgba(60, 34, 14, 0.4);
  transform: translate(-50%, -50%);
  animation: pop-in 260ms cubic-bezier(0.2, 1.2, 0.32, 1) both;
}

.show-win .win-card,
.show-confirm .confirm-card {
  display: block;
}

@keyframes pop-in {
  0% {
    transform: translate(-50%, -50%) scale(0.82);
    opacity: 0;
  }
  100% {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
  }
}

.win-emoji {
  font-size: 11cqw;
  line-height: 1;
  margin-bottom: 1cqw;
}

.win-title {
  color: #f47734;
  font-size: 5cqw;
  font-weight: 950;
  line-height: 1.1;
  text-shadow: 0 0.3cqw 0 white;
}

.win-sub {
  margin-top: 1.6cqw;
  font-size: 3cqw;
  font-weight: 850;
  color: #9a6134;
}

.win-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-top: 5cqw;
  padding: 2.8cqw 9cqw;
  border-radius: 999px;
  color: #fff;
  font-size: 4cqw;
  font-weight: 950;
  letter-spacing: 0.08em;
  text-decoration: none;
  text-shadow: 0 0.35cqw 0 rgba(150, 70, 20, 0.45);
  background:
    linear-gradient(rgba(255, 255, 255, 0.35), rgba(255, 255, 255, 0) 52%),
    linear-gradient(#ffb15f, #f2803a);
  box-shadow:
    inset 0 0.4cqw 0 rgba(255, 255, 255, 0.45),
    0 1.1cqw 0 rgba(150, 70, 20, 0.32);
}

.win-btn:active {
  transform: translateY(0.4cqw);
}

.confirm-title {
  color: #f47734;
  font-size: 4.4cqw;
  font-weight: 950;
  line-height: 1.15;
  text-shadow: 0 0.3cqw 0 white;
}

.confirm-sub {
  margin-top: 1.6cqw;
  font-size: 2.9cqw;
  font-weight: 850;
  color: #9a6134;
}

.confirm-actions {
  margin-top: 5cqw;
  display: flex;
  gap: 3cqw;
  justify-content: center;
}

.confirm-btn {
  flex: 1;
  max-width: 40%;
  padding: 2.6cqw 0;
  border-radius: 999px;
  font-size: 3.4cqw;
  font-weight: 950;
  letter-spacing: 0.06em;
  cursor: pointer;
}

.confirm-btn--cancel {
  color: #9a6134;
  background: #ffeccb;
  box-shadow: inset 0 0 0 0.4cqw rgba(244, 119, 52, 0.28);
}

.confirm-btn--ok {
  color: #fff;
  text-shadow: 0 0.35cqw 0 rgba(150, 70, 20, 0.45);
  background:
    linear-gradient(rgba(255, 255, 255, 0.35), rgba(255, 255, 255, 0) 52%),
    linear-gradient(#ff8f6b, #ef5a3a);
  box-shadow:
    inset 0 0.4cqw 0 rgba(255, 255, 255, 0.4),
    0 1cqw 0 rgba(150, 55, 30, 0.3);
}

.confirm-btn:active {
  transform: translateY(0.4cqw);
}
