/* Koda: chest screens. Loaded after style.css; use its :root tokens. */

/* Road chests: stop endless hop loop; show a clean tactile golden halo on unopened chests */
.chest-rung:not(.opened) .ico {
  animation: none;
}

.chest-button {
  min-width: var(--tap);
  min-height: var(--tap);
  border-radius: 20px;
  transition: transform 0.12s ease;
}

.chest-rung:not(.opened) .chest-button {
  background: #FFF7DC;
  border: 2px solid #FFE066;
  box-shadow: 0 4px 0 #E5A000;
}

.chest-rung:not(.opened) .chest-button:hover {
  background: #FFF2C2;
  transform: translateY(-2px);
  box-shadow: 0 6px 0 #E5A000;
}

.chest-rung:not(.opened) .chest-button:active {
  transform: translateY(4px);
  box-shadow: 0 0 0 #E5A000;
}

.chest-button:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 4px;
}

.chest-rung:not(.opened) .chest-button .ico {
  width: 3.25rem;
  height: 3.25rem;
}

/* Full-screen native <dialog> presentation */
.chest-dialog {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
  color: #FFFFFF;
  overflow: hidden;
}

.chest-dialog[open] {
  display: flex;
  align-items: center;
  justify-content: center;
}

.chest-dialog::backdrop {
  background: rgba(23, 33, 60, 0.92);
  animation: chest-backdrop-in 180ms ease-out both;
}

.chest-screen {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  justify-content: safe center;
  gap: 1rem;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  text-align: center;
  padding:
    calc(env(safe-area-inset-top) + 1.25rem)
    max(1.25rem, env(safe-area-inset-right))
    calc(env(safe-area-inset-bottom) + 1.5rem)
    max(1.25rem, env(safe-area-inset-left));
}

.chest-close {
  position: fixed;
  top: calc(env(safe-area-inset-top) + 0.75rem);
  right: calc(env(safe-area-inset-right) + 0.75rem);
  z-index: 10;
  width: var(--tap);
  height: var(--tap);
  min-width: var(--tap);
  min-height: var(--tap);
  padding: 0;
  border: 2px solid rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.1);
  color: #E2EAFF;
  box-shadow: none;
  display: inline-grid;
  place-items: center;
  transition: background-color 150ms ease, transform 100ms ease;
}

.chest-close svg {
  width: 1.25rem;
  height: 1.25rem;
}

.chest-close:hover {
  background: rgba(255, 255, 255, 0.18);
}

.chest-close:active:not(:disabled) {
  transform: scale(0.94);
  box-shadow: none;
}

.chest-close:focus-visible,
.chest-collect:focus-visible {
  outline: 3px solid #7DD3FC;
  outline-offset: 3px;
}

.chest-head {
  position: relative;
  z-index: 6;
  max-width: 22rem;
}

.chest-kicker {
  margin: 0 0 4px;
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: #9BB2E0;
}

.chest-title {
  margin: 0;
  font-size: clamp(1.5rem, 6vw, 2rem);
  font-weight: 900;
  line-height: 1.15;
  color: #FFFFFF;
}

.chest-title:focus {
  outline: none;
}

.chest-dialog.is-open .chest-title {
  color: #FFE066;
  animation: chest-title-pop 320ms cubic-bezier(.34, 1.56, .64, 1) both;
}

/* Chest illustration, burst, particles and rising gems */
.chest-stage {
  position: relative;
  width: clamp(180px, 34dvh, 240px);
  height: clamp(180px, 34dvh, 240px);
  display: grid;
  place-items: center;
  flex: none;
  margin-top: 0.5rem;
}

.chest-svg-wrap {
  position: relative;
  z-index: 3;
  width: 100%;
  height: 100%;
  animation: chest-enter 260ms cubic-bezier(.34, 1.45, .64, 1) both;
}

.chest-svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
}

.chest-rig {
  transform-origin: 120px 196px;
}

.chest-shadow {
  transform-origin: 120px 212px;
}

.chest-lid {
  transform-origin: 44px 116px;
  transition: transform 340ms cubic-bezier(.22, 1.45, .45, 1);
}

.chest-inner-glow {
  opacity: 0;
  transform-origin: 120px 114px;
  transform: scale(0.5);
  transition: opacity 220ms ease-out, transform 320ms cubic-bezier(.22, 1.4, .5, 1);
}

.chest-treasure-pile {
  opacity: 0;
  transform-origin: 120px 114px;
  transform: translateY(14px) scale(0.82);
  transition: opacity 180ms ease-out 70ms, transform 340ms cubic-bezier(.22, 1.45, .45, 1) 70ms;
}

/* Anticipation & shaking while waiting for the server response */
.chest-dialog.is-shaking .chest-rig {
  animation: chest-wobble 360ms ease-in-out 180ms infinite;
}

.chest-dialog.is-shaking .chest-lid {
  animation: chest-lid-rattle 360ms ease-in-out 180ms infinite;
}

.chest-dialog.is-shaking .chest-shadow {
  animation: chest-shadow-wobble 360ms ease-in-out 180ms infinite;
}

/* Lid open state */
.chest-dialog.is-open .chest-rig {
  animation: chest-pop-open 380ms cubic-bezier(.22, 1.4, .45, 1) both;
}

.chest-dialog.is-open .chest-lid {
  transform: translate(-4px, -12px) rotate(-32deg);
}

.chest-dialog.is-open .chest-inner-glow {
  opacity: 1;
  transform: scale(1);
}

.chest-dialog.is-open .chest-treasure-pile {
  opacity: 1;
  transform: translateY(0) scale(1);
}

/* Sunburst rays and expanding flash ring behind the chest */
.chest-burst {
  position: absolute;
  inset: -24%;
  z-index: 1;
  pointer-events: none;
  display: grid;
  place-items: center;
  opacity: 0;
  transform: scale(0.35);
}

.chest-burst svg {
  width: 100%;
  height: 100%;
  display: block;
}

.chest-ring {
  position: absolute;
  width: 58%;
  height: 58%;
  border-radius: 50%;
  border: 4px solid #FFE066;
  opacity: 0;
  transform: scale(0.35);
}

.chest-dialog.is-open .chest-burst {
  animation: chest-burst-in 620ms cubic-bezier(.16, 1, .3, 1) both;
}

.chest-dialog.is-open .chest-ring {
  animation: chest-ring-out 480ms cubic-bezier(.16, 1, .3, 1) both;
}

/* Burst particles erupting from the open chest mouth */
.chest-particles {
  position: absolute;
  left: 50%;
  top: 46%;
  z-index: 4;
  pointer-events: none;
}

.chest-particle {
  position: absolute;
  left: 0;
  top: 0;
  width: 10px;
  height: 10px;
  margin: -5px 0 0 -5px;
  background: var(--c, #FFE066);
  opacity: 0;
  animation: chest-particle 760ms cubic-bezier(.12, .85, .28, 1) var(--delay, 0ms) both;
}

.chest-particle.p-dot {
  width: 8px;
  height: 8px;
  margin: -4px 0 0 -4px;
  border-radius: 50%;
}

.chest-particle.p-diamond {
  border-radius: 2px;
}

.chest-particle.p-star {
  width: 14px;
  height: 14px;
  margin: -7px 0 0 -7px;
  clip-path: polygon(50% 0%, 63% 37%, 100% 50%, 63% 63%, 50% 100%, 37% 63%, 0% 50%, 37% 37%);
}

/* Gems rising out of the open chest */
.chest-rising-gems {
  position: absolute;
  left: 50%;
  top: 46%;
  z-index: 5;
  pointer-events: none;
}

.chest-rising-gem {
  position: absolute;
  left: -16px;
  top: -16px;
  width: 32px;
  height: 32px;
  opacity: 0;
  animation: chest-gem-rise 780ms cubic-bezier(.18, .9, .32, 1) var(--gdelay, 0ms) both;
}

.chest-rising-gem svg,
.chest-gem-icon svg,
.chest-fly-gem svg {
  display: block;
  width: 100%;
  height: 100%;
}

/* Revealed reward amount */
.chest-reward {
  position: relative;
  z-index: 6;
  min-height: 5.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.25rem;
}

.chest-gem-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.55rem;
  opacity: 0;
  transform: scale(0.75) translateY(10px);
}

.chest-dialog.is-open .chest-gem-badge {
  animation: chest-reward-in 340ms cubic-bezier(.34, 1.56, .64, 1) 100ms both;
}

.chest-gem-icon {
  display: inline-block;
  width: 2.75rem;
  height: 2.75rem;
  flex: none;
}

.chest-amount {
  display: inline-block;
  font-size: clamp(2.5rem, 10vw, 3.25rem);
  font-weight: 900;
  line-height: 1;
  letter-spacing: -0.02em;
  color: #FFFFFF;
  font-variant-numeric: tabular-nums;
}

.chest-amount.is-landed {
  animation: chest-amount-land 260ms cubic-bezier(.34, 1.6, .64, 1);
}

.chest-unit {
  margin: 0;
  font-size: 1rem;
  font-weight: 800;
  color: #7DD3FC;
  opacity: 0;
}

.chest-dialog.is-open .chest-unit {
  animation: chest-fade-in 260ms ease-out 180ms both;
}

/* Primary Collect button */
.chest-actions {
  position: relative;
  z-index: 6;
  width: 100%;
  max-width: 19rem;
  min-height: 3.75rem;
  display: flex;
  justify-content: center;
  margin-top: 0.25rem;
}

.chest-collect {
  width: 100%;
  min-height: 3.5rem;
  padding: 0 24px;
  border: 2px solid #FFC800;
  border-radius: 16px;
  background: #FFC800;
  color: #17213C;
  font-size: 1.05rem;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  box-shadow: 0 4px 0 #C78A00;
  animation: chest-collect-in 300ms cubic-bezier(.34, 1.45, .64, 1) 180ms both;
}

.chest-collect:hover {
  background: #FFD43B;
  border-color: #FFD43B;
}

.chest-collect:active:not(:disabled) {
  transform: translateY(4px);
  box-shadow: 0 0 0 #C78A00;
}

/* Flying gems from chest reward to top-bar gem counter */
.chest-fly-layer {
  position: fixed;
  inset: 0;
  z-index: 60;
  pointer-events: none;
  overflow: hidden;
}

.chest-fly-gem {
  position: fixed;
  left: 0;
  top: 0;
  width: 1.75rem;
  height: 1.75rem;
  margin: -0.875rem 0 0 -0.875rem;
  opacity: 0;
  will-change: transform, opacity;
  animation: chest-gem-fly 460ms cubic-bezier(.25, .75, .4, 1) var(--delay, 0ms) both;
}

.stat-chip.gems.chest-gems-bump {
  animation: chest-gems-bump 420ms cubic-bezier(.34, 1.65, .64, 1);
}

/* Keyframes (transform and opacity only) */
@keyframes chest-backdrop-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes chest-enter {
  from { opacity: 0; transform: scale(0.55) translateY(28px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

@keyframes chest-wobble {
  0%, 100% { transform: translate(0, 0) rotate(0deg) scale(1, 1); }
  20% { transform: translate(-5px, -3px) rotate(-4.5deg) scale(1.03, 0.96); }
  45% { transform: translate(5px, -5px) rotate(4.5deg) scale(0.97, 1.04); }
  70% { transform: translate(-4px, -2px) rotate(-3deg) scale(1.02, 0.98); }
  88% { transform: translate(3px, 0) rotate(2deg) scale(1, 1); }
}

@keyframes chest-lid-rattle {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  25% { transform: translateY(-3px) rotate(-2.5deg); }
  55% { transform: translateY(-4px) rotate(2deg); }
  80% { transform: translateY(-1px) rotate(-1deg); }
}

@keyframes chest-shadow-wobble {
  0%, 100% { transform: scaleX(1); }
  45% { transform: scaleX(0.92); }
}

@keyframes chest-pop-open {
  0% { transform: scale(1, 1) translateY(0); }
  24% { transform: scale(1.08, 0.89) translateY(6px); }
  58% { transform: scale(0.96, 1.05) translateY(-10px); }
  100% { transform: scale(1, 1) translateY(0); }
}

@keyframes chest-burst-in {
  0% { opacity: 0; transform: scale(0.3) rotate(-14deg); }
  45% { opacity: 0.95; transform: scale(calc(var(--burst-scale, 1) * 1.08)) rotate(8deg); }
  100% { opacity: 0.7; transform: scale(var(--burst-scale, 1)) rotate(18deg); }
}

@keyframes chest-ring-out {
  0% { opacity: 0.9; transform: scale(0.35); }
  100% { opacity: 0; transform: scale(calc(var(--burst-scale, 1) * 1.65)); }
}

@keyframes chest-particle {
  0% { opacity: 0; transform: translate3d(0, 0, 0) rotate(45deg) scale(0.3); }
  16% { opacity: 1; }
  75% { opacity: 1; }
  100% { opacity: 0; transform: translate3d(var(--dx), var(--dy), 0) rotate(calc(45deg + var(--rot))) scale(var(--s, 1)); }
}

@keyframes chest-gem-rise {
  0% { opacity: 0; transform: translate3d(0, 12px, 0) scale(0.3) rotate(0deg); }
  20% { opacity: 1; }
  58% { opacity: 1; transform: translate3d(var(--gx), var(--gy), 0) scale(1.05) rotate(var(--grot)); }
  100% { opacity: 0; transform: translate3d(calc(var(--gx) * 1.15), calc(var(--gy) - 22px), 0) scale(0.75) rotate(calc(var(--grot) * 1.3)); }
}

@keyframes chest-title-pop {
  from { opacity: 0.5; transform: scale(0.86); }
  to { opacity: 1; transform: scale(1); }
}

@keyframes chest-reward-in {
  from { opacity: 0; transform: scale(0.72) translateY(10px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

@keyframes chest-amount-land {
  0% { transform: scale(1); }
  45% { transform: scale(1.16); }
  100% { transform: scale(1); }
}

@keyframes chest-fade-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes chest-collect-in {
  from { opacity: 0; transform: scale(0.9) translateY(12px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

@keyframes chest-gem-fly {
  0% { opacity: 0; transform: translate3d(var(--sx), var(--sy), 0) scale(0.65); }
  14% { opacity: 1; }
  46% { opacity: 1; transform: translate3d(var(--mx), var(--my), 0) scale(1.08); }
  88% { opacity: 1; transform: translate3d(var(--tx), var(--ty), 0) scale(0.82); }
  100% { opacity: 0; transform: translate3d(var(--tx), var(--ty), 0) scale(0.35); }
}

@keyframes chest-gems-bump {
  0% { transform: scale(1); }
  40% { transform: scale(1.22); }
  100% { transform: scale(1); }
}

/* Reduced motion: skip animations and show the final open chest and amount directly */
@media (prefers-reduced-motion: reduce) {
  .chest-button,
  .chest-dialog::backdrop,
  .chest-dialog *,
  .chest-fly-gem,
  .stat-chip.gems.chest-gems-bump {
    animation: none !important;
    transition: none !important;
  }

  .chest-dialog.is-open .chest-lid {
    transform: translate(-4px, -12px) rotate(-32deg);
  }

  .chest-dialog.is-open .chest-inner-glow {
    opacity: 1;
    transform: scale(1);
  }

  .chest-dialog.is-open .chest-treasure-pile {
    opacity: 1;
    transform: none;
  }

  .chest-dialog.is-open .chest-burst {
    opacity: 0.65;
    transform: scale(var(--burst-scale, 1));
  }

  .chest-dialog.is-open .chest-ring,
  .chest-particles,
  .chest-rising-gems {
    display: none;
  }

  .chest-dialog.is-open .chest-gem-badge,
  .chest-dialog.is-open .chest-unit,
  .chest-dialog.is-open .chest-collect {
    opacity: 1;
    transform: none;
  }
}
