:root {
  --game-bg: radial-gradient(circle at 50% 30%, #291b4a 0%, #18102e 55%, #0b0717 100%);
  --chest-wood-light: #9c663b;
  --chest-wood-mid: #70421e;
  --chest-wood-dark: #4e2b0e;
  --chest-metal-bright: #ffe082;
  --chest-metal-mid: #e6b800;
  --chest-metal-dark: #a87d00;
  --chest-gem-color: #e91e63;
}

/* ========================================================
   Stage & Room Atmosphere
   ======================================================== */
.chest-field {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.room-backdrop {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 1;
}

.ambient-dust {
  position: absolute;
  inset: 0;
  background-image:
    radial-gradient(2px 2px at 20% 30%, rgba(255, 235, 150, 0.5) 50%, transparent 100%),
    radial-gradient(2.5px 2.5px at 75% 25%, rgba(255, 215, 0, 0.4) 50%, transparent 100%),
    radial-gradient(2px 2px at 45% 70%, rgba(255, 255, 255, 0.3) 50%, transparent 100%),
    radial-gradient(3px 3px at 85% 65%, rgba(255, 200, 100, 0.45) 50%, transparent 100%),
    radial-gradient(2px 2px at 15% 75%, rgba(255, 240, 200, 0.4) 50%, transparent 100%);
  animation: dust-float 12s ease-in-out infinite alternate;
}

@keyframes dust-float {
  0% { transform: translateY(0) scale(1); opacity: 0.6; }
  50% { transform: translateY(-15px) scale(1.04); opacity: 0.9; }
  100% { transform: translateY(-28px) scale(0.98); opacity: 0.7; }
}

.magic-light-rays {
  position: absolute;
  bottom: 30%;
  left: 50%;
  width: 320px;
  height: 380px;
  transform: translateX(-50%);
  background: conic-gradient(
    from 180deg at 50% 100%,
    transparent 20deg,
    rgba(255, 220, 100, 0.22) 50deg,
    transparent 75deg,
    rgba(255, 240, 160, 0.28) 100deg,
    transparent 125deg,
    rgba(255, 220, 100, 0.2) 155deg,
    transparent 180deg
  );
  filter: blur(14px);
  opacity: 0;
  transition: opacity 0.6s ease;
  pointer-events: none;
  z-index: 2;
}

.magic-light-rays.active {
  opacity: 1;
  animation: light-ray-pulse 3s ease-in-out infinite alternate;
}

@keyframes light-ray-pulse {
  0% { opacity: 0.6; transform: translateX(-50%) scale(0.95); }
  100% { opacity: 1; transform: translateX(-50%) scale(1.05); }
}

/* ========================================================
   Stage Pedestal
   ======================================================== */
.chest-stage {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 5;
  width: 100%;
  max-width: 480px;
  margin-top: 20px;
}

.pedestal-shadow {
  position: absolute;
  bottom: -28px;
  width: min(85vw, 340px);
  height: 48px;
  background: radial-gradient(ellipse at center, rgba(0, 0, 0, 0.6) 0%, rgba(0, 0, 0, 0) 70%);
  border-radius: 50%;
  z-index: 4;
}

.pedestal-base {
  position: absolute;
  bottom: -16px;
  width: min(82vw, 320px);
  height: 28px;
  background: linear-gradient(180deg, #3d276b 0%, #20133c 100%);
  border: 2px solid rgba(255, 215, 0, 0.3);
  border-radius: 50%;
  box-shadow: 0 0 25px rgba(186, 104, 200, 0.35);
  z-index: 4;
}

/* ========================================================
   Chest Box & Structure
   ======================================================== */
.chest-wrapper {
  position: relative;
  z-index: 10;
}

.chest-btn {
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 10px;
  margin: 0;
  outline: none;
  display: block;
}

.chest-btn:focus-visible .chest-box {
  outline: 4px solid var(--chest-metal-bright);
  outline-offset: 8px;
  border-radius: 20px;
}

.chest-box {
  position: relative;
  width: min(86vw, 310px);
  height: 220px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  transition: transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.chest-box.rattle {
  animation: chest-wobble 0.4s ease-in-out;
}

@keyframes chest-wobble {
  0%, 100% { transform: rotate(0deg) scale(1); }
  20% { transform: rotate(-3deg) scale(1.02); }
  40% { transform: rotate(3deg) scale(1.02); }
  60% { transform: rotate(-2deg); }
  80% { transform: rotate(2deg); }
}

/* Interior of Chest */
.chest-interior {
  position: absolute;
  bottom: 22px;
  width: 250px;
  height: 90px;
  background: radial-gradient(ellipse at center, #261118 0%, #15070c 80%, #0d0407 100%);
  border-radius: 8px 8px 14px 14px;
  box-shadow: inset 0 8px 18px rgba(0, 0, 0, 0.85);
  z-index: 6;
}

/* Internal Glow */
.chest-light-glow {
  position: absolute;
  bottom: 50px;
  left: 50%;
  width: 190px;
  height: 80px;
  transform: translateX(-50%);
  background: radial-gradient(ellipse at center, rgba(255, 235, 100, 0.8) 0%, rgba(255, 190, 30, 0.3) 60%, transparent 100%);
  filter: blur(8px);
  opacity: 0;
  transition: opacity 0.4s ease;
  z-index: 8;
  pointer-events: none;
}

.chest-light-glow.active {
  opacity: 1;
}

/* ========================================================
   Lid Styling & Art
   ======================================================== */
.chest-lid {
  position: absolute;
  bottom: 95px;
  width: 270px;
  height: 86px;
  background: linear-gradient(180deg, var(--chest-wood-light) 0%, var(--chest-wood-mid) 50%, var(--chest-wood-dark) 100%);
  border: 4px solid var(--chest-metal-mid);
  border-radius: 46px 46px 6px 6px;
  box-shadow:
    inset 0 4px 6px rgba(255, 255, 255, 0.25),
    inset 0 -6px 8px rgba(0, 0, 0, 0.4),
    0 10px 24px rgba(0, 0, 0, 0.45);
  transform-origin: top center;
  transition: transform 0.45s cubic-bezier(0.34, 1.56, 0.64, 1);
  z-index: 12;
}

.lid-handle {
  position: absolute;
  top: -12px;
  left: 50%;
  transform: translateX(-50%);
  width: 54px;
  height: 18px;
  border: 5px solid var(--chest-metal-bright);
  border-bottom: none;
  border-radius: 12px 12px 0 0;
  box-shadow: 0 -2px 6px rgba(0, 0, 0, 0.3);
}

.lid-strap {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 24px;
  background: linear-gradient(90deg, var(--chest-metal-dark), var(--chest-metal-bright) 50%, var(--chest-metal-mid));
  border-left: 1px solid rgba(255, 255, 255, 0.4);
  border-right: 1px solid rgba(0, 0, 0, 0.3);
}

.strap-left { left: 42px; }
.strap-right { right: 42px; }

.lid-rivets {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.lid-rivets::before,
.lid-rivets::after {
  content: '';
  position: absolute;
  width: 6px;
  height: 6px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 0 2px #000;
  top: 14px;
}
.lid-rivets::before { left: 51px; }
.lid-rivets::after { right: 51px; }

/* Lid State Variations */
.lid-ajar {
  transform: translateY(-26px) rotate(-14deg);
}

.lid-open {
  transform: translateY(-84px) rotate(-62deg) scaleY(0.92);
}

/* ========================================================
   Base / Chest Body
   ======================================================== */
.chest-base {
  position: relative;
  width: 260px;
  height: 105px;
  background: linear-gradient(180deg, var(--chest-wood-mid) 0%, var(--chest-wood-dark) 100%);
  border: 4px solid var(--chest-metal-mid);
  border-radius: 0 0 16px 16px;
  box-shadow:
    inset 0 4px 6px rgba(255, 255, 255, 0.15),
    inset 0 -10px 14px rgba(0, 0, 0, 0.5),
    0 14px 30px rgba(0, 0, 0, 0.4);
  z-index: 10;
}

.base-strap {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 24px;
  background: linear-gradient(90deg, var(--chest-metal-dark), var(--chest-metal-bright) 50%, var(--chest-metal-mid));
  border-left: 1px solid rgba(255, 255, 255, 0.4);
  border-right: 1px solid rgba(0, 0, 0, 0.3);
}

.escutcheon-plate {
  position: absolute;
  top: 4px;
  left: 50%;
  transform: translateX(-50%);
  width: 48px;
  height: 38px;
  background: radial-gradient(circle, var(--chest-metal-bright) 0%, var(--chest-metal-dark) 100%);
  border: 2px solid var(--chest-metal-bright);
  border-radius: 6px 6px 14px 14px;
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.35);
}

/* ========================================================
   Hasp, Lock & Golden Key
   ======================================================== */
.chest-hasp {
  position: absolute;
  top: 100px;
  left: 50%;
  transform: translateX(-50%);
  width: 28px;
  height: 34px;
  background: linear-gradient(180deg, var(--chest-metal-bright) 0%, var(--chest-metal-dark) 100%);
  border: 2px solid var(--chest-metal-bright);
  border-radius: 4px 4px 12px 12px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
  z-index: 15;
  transition: transform 0.3s ease, opacity 0.3s ease;
}

.chest-lock {
  position: absolute;
  top: 114px;
  left: 50%;
  transform: translateX(-50%);
  width: 46px;
  height: 52px;
  z-index: 20;
  transition: transform 0.35s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.35s ease;
  filter: drop-shadow(0 4px 10px rgba(0, 0, 0, 0.45));
}

.lock-shackle {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 26px;
  height: 22px;
  border: 5px solid var(--chest-metal-bright);
  border-bottom: none;
  border-radius: 14px 14px 0 0;
  transition: transform 0.25s ease;
}

.lock-body {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 34px;
  background: radial-gradient(circle at 50% 30%, var(--chest-metal-bright) 0%, var(--chest-metal-dark) 100%);
  border: 2px solid #fff;
  border-radius: 8px;
  box-shadow: inset 0 2px 4px rgba(255, 255, 255, 0.5);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.lock-gem {
  width: 10px;
  height: 10px;
  background: var(--chest-gem-color);
  border-radius: 50%;
  box-shadow: 0 0 6px var(--chest-gem-color);
  margin-bottom: 2px;
}

.lock-keyhole {
  width: 5px;
  height: 8px;
  background: #111;
  border-radius: 2px 2px 3px 3px;
}

/* Golden Key */
.golden-key {
  position: absolute;
  top: 100px;
  right: 28px;
  font-size: 38px;
  line-height: 1;
  z-index: 25;
  filter: drop-shadow(0 0 10px #ffe57f);
  animation: key-float 2.2s ease-in-out infinite alternate;
  transition: transform 0.5s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.4s ease;
}

@keyframes key-float {
  0% { transform: translateY(0) rotate(5deg) scale(1); }
  100% { transform: translateY(-8px) rotate(-8deg) scale(1.08); }
}

.golden-key.key-turning {
  transform: translate(-100px, 18px) rotate(360deg) scale(0.9);
}

.golden-key.hidden-key {
  opacity: 0;
  pointer-events: none;
}

/* Lock Opened State */
.lock-unlocked .lock-shackle {
  transform: translateX(-50%) translateY(-10px);
}

.lock-unlocked {
  transform: translateX(-50%) translateY(32px) rotate(25deg) scale(0.7);
  opacity: 0;
}

/* ========================================================
   Prize / Toy Inside the Chest
   ======================================================== */
.chest-prize-slot {
  position: absolute;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  width: 180px;
  height: 180px;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 8;
  pointer-events: none;
}

.chest-prize {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.toy-visual {
  font-size: 96px;
  line-height: 1;
  filter: drop-shadow(0 8px 18px rgba(0, 0, 0, 0.4)) drop-shadow(0 0 20px rgba(255, 235, 100, 0.6));
  transform-origin: bottom center;
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.toy-bounce-in {
  animation: prize-burst 0.65s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

@keyframes prize-burst {
  0% {
    transform: translateY(60px) scale(0.2) rotate(-15deg);
    opacity: 0;
  }
  60% {
    transform: translateY(-90px) scale(1.35) rotate(10deg);
    opacity: 1;
  }
  80% {
    transform: translateY(-70px) scale(0.95) rotate(-4deg);
  }
  100% {
    transform: translateY(-80px) scale(1.15) rotate(0deg);
    opacity: 1;
  }
}

.toy-idle {
  transform: translateY(-80px) scale(1.15);
  animation: toy-bob 2.4s ease-in-out infinite alternate;
}

@keyframes toy-bob {
  0% { transform: translateY(-80px) scale(1.15) rotate(0deg); }
  50% { transform: translateY(-88px) scale(1.18) rotate(3deg); }
  100% { transform: translateY(-76px) scale(1.13) rotate(-3deg); }
}

.toy-react-jump {
  transform: translateY(-80px) scale(1.15);
  animation: toy-jump-wiggle 0.45s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes toy-jump-wiggle {
  0% { transform: translateY(-80px) scale(1.15); }
  35% { transform: translateY(-115px) scale(1.35) rotate(12deg); }
  70% { transform: translateY(-75px) scale(1.05) rotate(-10deg); }
  100% { transform: translateY(-80px) scale(1.15) rotate(0deg); }
}

/* Toy Speech / Reaction Bubble */
.toy-bubble {
  position: absolute;
  top: -55px;
  left: 50%;
  transform: translateX(-50%);
  background: #ffffff;
  color: #1a1a1a;
  font-weight: 800;
  font-size: 19px;
  padding: 6px 14px;
  border-radius: 20px;
  border: 3px solid #ff9800;
  box-shadow: 0 6px 14px rgba(0, 0, 0, 0.35);
  white-space: nowrap;
  animation: bubble-pop-in 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  z-index: 30;
  pointer-events: none;
}

.toy-bubble::after {
  content: '';
  position: absolute;
  bottom: -9px;
  left: 50%;
  transform: translateX(-50%);
  border-width: 9px 8px 0;
  border-style: solid;
  border-color: #ff9800 transparent transparent;
  display: block;
  width: 0;
}

@keyframes bubble-pop-in {
  0% { transform: translateX(-50%) scale(0.3) translateY(15px); opacity: 0; }
  100% { transform: translateX(-50%) scale(1) translateY(0); opacity: 1; }
}

/* ========================================================
   Theme Variations
   ======================================================== */
/* 1. Emerald Forest */
.chest-box.theme-emerald {
  --chest-wood-light: #2e7d32;
  --chest-wood-mid: #1b5e20;
  --chest-wood-dark: #0f3d13;
  --chest-metal-bright: #ffe082;
  --chest-metal-mid: #d4af37;
  --chest-metal-dark: #8c6b12;
  --chest-gem-color: #00e676;
}

/* 2. Starry Amethyst */
.chest-box.theme-amethyst {
  --chest-wood-light: #512da8;
  --chest-wood-mid: #311b92;
  --chest-wood-dark: #1a0c5c;
  --chest-metal-bright: #e0e0e0;
  --chest-metal-mid: #b0bec5;
  --chest-metal-dark: #546e7a;
  --chest-gem-color: #e040fb;
}

/* 3. Royal Ruby Holiday */
.chest-box.theme-ruby {
  --chest-wood-light: #c2185b;
  --chest-wood-mid: #880e4f;
  --chest-wood-dark: #4a0026;
  --chest-metal-bright: #fff9c4;
  --chest-metal-mid: #ffd54f;
  --chest-metal-dark: #ff8f00;
  --chest-gem-color: #ffeb3b;
}

/* ========================================================
   Confetti & Sparkles
   ======================================================== */
.confetti-container,
.sparkles-container {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 40;
}

.confetti-piece {
  position: absolute;
  width: 14px;
  height: 18px;
  border-radius: 3px;
  animation: confetti-fall linear forwards;
}

.confetti-ribbon {
  border-radius: 50% 50% 0 0;
  width: 10px;
  height: 24px;
}

@keyframes confetti-fall {
  0% {
    transform: translate(0, 0) rotate(0deg);
    opacity: 1;
  }
  100% {
    transform: translate(var(--end-x, 0), var(--end-y, 450px)) rotate(var(--rot, 720deg));
    opacity: 0;
  }
}

.sparkle-burst {
  position: absolute;
  width: 16px;
  height: 16px;
  background: radial-gradient(circle, #ffffff 0%, #ffeb3b 60%, transparent 100%);
  border-radius: 50%;
  animation: sparkle-twinkle 0.7s cubic-bezier(0.25, 1, 0.5, 1) forwards;
}

@keyframes sparkle-twinkle {
  0% { transform: translate(0, 0) scale(0.2); opacity: 1; }
  100% { transform: translate(var(--sx, 0), var(--sy, 0)) scale(1.4); opacity: 0; }
}

/* ========================================================
   Action Controls Bar
   ======================================================== */
.stage-controls {
  position: relative;
  z-index: 50;
  margin-top: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 58px;
}

.action-pill-btn {
  background: linear-gradient(135deg, #ff9800 0%, #f57c00 100%);
  color: #ffffff;
  font-family: inherit;
  font-size: 19px;
  font-weight: 800;
  padding: 12px 28px;
  border: 3px solid #ffe082;
  border-radius: 999px;
  box-shadow: 0 8px 20px rgba(245, 124, 0, 0.45);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 0.2s ease;
  animation: pulse-gentle 2s ease-in-out infinite;
}

.action-pill-btn:hover {
  transform: scale(1.06);
  box-shadow: 0 10px 26px rgba(245, 124, 0, 0.6);
}

.action-pill-btn:active {
  transform: scale(0.96);
}

.action-pill-btn:focus-visible {
  outline: 4px solid #fff;
  outline-offset: 4px;
}

@keyframes pulse-gentle {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.04); }
}

/* ========================================================
   Responsive Compact & Landscape Support
   ======================================================== */
@media (max-height: 560px) {
  .chest-stage {
    margin-top: 6px;
    transform: scale(0.82);
    transform-origin: center bottom;
  }
  .stage-controls {
    margin-top: 10px;
    min-height: 44px;
  }
  .action-pill-btn {
    font-size: 16px;
    padding: 8px 20px;
  }
}

@media (max-height: 420px) {
  .chest-stage {
    margin-top: 0;
    transform: scale(0.68);
    transform-origin: center bottom;
  }
  .stage-controls {
    margin-top: 4px;
    min-height: 38px;
  }
  .action-pill-btn {
    font-size: 14px;
    padding: 6px 16px;
  }
}

/* ========================================================
   Accessibility & Prefers-Reduced-Motion
   ======================================================== */
@media (prefers-reduced-motion: reduce) {
  .ambient-dust,
  .magic-light-rays.active,
  .golden-key,
  .toy-idle,
  .toy-bounce-in,
  .toy-react-jump,
  .action-pill-btn,
  .confetti-piece,
  .sparkle-burst,
  .chest-box.rattle {
    animation: none;
  }
  .chest-prize {
    transform: translateY(-80px) scale(1.15);
  }
  .chest-box,
  .chest-lid,
  .chest-lock,
  .golden-key,
  .toy-visual {
    transition: none;
  }
}
