:root {
  color-scheme: dark;
  --ink: #271506;
  --paper-ink: #44200a;
  --gold: #ffd667;
  --gold-dark: #a96c22;
  --wood: #8e4b21;
  --wood-dark: #3a1a0c;
  --wood-light: #c2762e;
  --brass: #edbd4c;
  --brass-shadow: #7b4916;
  --cream: #ffe5a7;
  --pink: #ff5ea8;
  --cyan: #42f3ff;
  --green: #6dff8b;
  --void: #0f0b16;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--void);
}

body {
  min-height: 100vh;
  margin: 0;
  overflow-x: hidden;
  color: var(--cream);
  font-family: "Trebuchet MS", Verdana, sans-serif;
  background:
    linear-gradient(180deg, rgba(9, 7, 16, 0.82), rgba(33, 12, 23, 0.94)),
    repeating-linear-gradient(90deg, rgba(66, 243, 255, 0.08) 0 2px, transparent 2px 36px),
    repeating-linear-gradient(0deg, rgba(255, 214, 103, 0.06) 0 2px, transparent 2px 36px),
    #130f1d;
}

body::before {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  content: "";
  opacity: 0.28;
  background-image:
    linear-gradient(45deg, transparent 47%, rgba(255, 94, 168, 0.35) 48% 52%, transparent 53%),
    linear-gradient(-45deg, transparent 47%, rgba(66, 243, 255, 0.28) 48% 52%, transparent 53%);
  background-size: 72px 72px, 96px 96px;
  image-rendering: pixelated;
}

.pixel-sky {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background-image:
    radial-gradient(circle at 12% 16%, #fff6b8 0 1px, transparent 2px),
    radial-gradient(circle at 82% 20%, #7dfff2 0 1px, transparent 2px),
    radial-gradient(circle at 25% 78%, #ff85bd 0 1px, transparent 2px),
    radial-gradient(circle at 74% 70%, #e6f6ff 0 1px, transparent 2px),
    radial-gradient(circle at 50% 42%, #b7ff88 0 1px, transparent 2px);
  background-size: 86px 86px, 131px 131px, 113px 113px, 149px 149px, 173px 173px;
  image-rendering: pixelated;
}

.screen-lines {
  position: fixed;
  inset: 0;
  z-index: 4;
  pointer-events: none;
  opacity: 0.11;
  background: repeating-linear-gradient(180deg, rgba(255, 255, 255, 0.24) 0 1px, transparent 1px 4px);
}

.game-shell {
  position: relative;
  z-index: 1;
  display: grid;
  min-height: 100vh;
  grid-template-rows: auto auto minmax(280px, 1fr) auto;
  gap: clamp(10px, 2vh, 22px);
  align-items: center;
  width: min(1120px, 100%);
  margin: 0 auto;
  padding: clamp(14px, 3vw, 30px);
}

.hud {
  display: flex;
  justify-content: flex-end;
}

.timer {
  min-width: 104px;
  padding: 10px 14px;
  border: 3px solid #f8da74;
  border-radius: 6px;
  color: #fffdcf;
  background: #1c1121;
  box-shadow:
    0 0 0 3px #4b2434,
    0 0 24px rgba(66, 243, 255, 0.32);
  font: 700 1.25rem/1 "Courier New", monospace;
  text-align: center;
  text-shadow: 2px 2px 0 #000;
}

.timer.is-low {
  color: #ffd2d2;
  border-color: #ff5e5e;
  animation: timer-pulse 0.9s steps(2, end) infinite;
}

.riddle-scroll {
  position: relative;
  width: min(900px, 96vw);
  margin: 0 auto;
  isolation: isolate;
}

.parchment {
  display: block;
  width: 100%;
  height: auto;
  filter: drop-shadow(0 22px 20px rgba(0, 0, 0, 0.42));
  image-rendering: auto;
}

.riddle-copy {
  position: absolute;
  inset: 17% 14% 18%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  color: var(--paper-ink);
  text-align: center;
  text-shadow: 0 1px 0 rgba(255, 237, 178, 0.65);
}

.riddle-copy h1 {
  margin: 0 0 clamp(4px, 1vw, 12px);
  font-family: "Brush Script MT", "Segoe Script", "Comic Sans MS", cursive;
  font-size: clamp(1.6rem, 4.8vw, 3.9rem);
  font-weight: 700;
  line-height: 0.95;
}

.riddle-copy p {
  margin: 0;
  font-family: "Segoe Print", "Bradley Hand", "Comic Sans MS", cursive;
  font-size: clamp(0.98rem, 2.5vw, 1.85rem);
  font-weight: 700;
  line-height: 1.42;
}

.treasure-zone {
  position: relative;
  display: grid;
  place-items: center;
  min-height: clamp(280px, 43vw, 460px);
  padding: 0 0 18px;
  perspective: 900px;
}

.milky-way {
  position: absolute;
  inset: -38% 0 -8%;
  width: 100%;
  height: 146%;
  opacity: 0;
  transition: opacity 500ms ease;
  pointer-events: none;
  mix-blend-mode: screen;
}

body.is-open .milky-way {
  opacity: 1;
}

.chest {
  position: relative;
  width: min(560px, 86vw);
  aspect-ratio: 2.18 / 1;
  transform-style: preserve-3d;
  filter:
    drop-shadow(0 20px 0 rgba(0, 0, 0, 0.2))
    drop-shadow(0 28px 24px rgba(0, 0, 0, 0.48));
  image-rendering: pixelated;
}

.chest::before {
  position: absolute;
  left: 10%;
  right: 10%;
  bottom: 4%;
  height: 13%;
  border-radius: 50%;
  content: "";
  background: rgba(0, 0, 0, 0.36);
  filter: blur(4px);
}

.inner-light {
  position: absolute;
  left: 12%;
  right: 12%;
  top: 37%;
  z-index: 1;
  height: 23%;
  opacity: 0;
  background:
    linear-gradient(90deg, transparent, rgba(255, 248, 184, 0.9), transparent),
    repeating-linear-gradient(90deg, transparent 0 7px, rgba(66, 243, 255, 0.78) 7px 13px);
  box-shadow:
    0 0 24px rgba(255, 255, 184, 0.78),
    0 0 66px rgba(66, 243, 255, 0.58);
  clip-path: polygon(6% 30%, 94% 30%, 100% 100%, 0 100%);
  transition: opacity 260ms ease 160ms;
}

.chest-lid {
  position: absolute;
  left: 6%;
  right: 6%;
  top: 3%;
  z-index: 2;
  height: 47%;
  border: 7px solid var(--wood-dark);
  border-bottom-width: 9px;
  border-radius: 46% 46% 7px 7px / 82% 82% 7px 7px;
  transform-origin: 50% 100%;
  background:
    linear-gradient(90deg, transparent 0 10%, rgba(0, 0, 0, 0.2) 10% 13%, transparent 13% 87%, rgba(0, 0, 0, 0.2) 87% 90%, transparent 90%),
    repeating-linear-gradient(0deg, rgba(255, 223, 98, 0.08) 0 7px, transparent 7px 16px),
    linear-gradient(180deg, #d18134 0%, var(--wood) 58%, #5a260f 100%);
  box-shadow:
    inset 0 9px 0 rgba(255, 213, 105, 0.34),
    inset 0 -12px 0 rgba(47, 19, 8, 0.32),
    0 8px 0 #1f0c06;
  transition: transform 900ms cubic-bezier(0.2, 0.82, 0.2, 1.08), filter 500ms ease;
}

.lid-rim {
  position: absolute;
  left: -5%;
  right: -5%;
  bottom: -12px;
  height: 18px;
  border: 5px solid var(--brass-shadow);
  border-radius: 4px;
  background: linear-gradient(180deg, #ffe586, var(--brass) 55%, var(--brass-shadow));
  box-shadow: 0 4px 0 #2c1007;
}

.lid-band,
.body-band {
  position: absolute;
  z-index: 3;
  width: 9%;
  border: 5px solid var(--brass-shadow);
  background:
    repeating-linear-gradient(0deg, transparent 0 13px, rgba(255, 250, 165, 0.35) 13px 17px),
    linear-gradient(90deg, #8a5517, #ffe17d 45%, #bd7a21 100%);
  box-shadow: inset -5px 0 0 rgba(0, 0, 0, 0.22);
}

.lid-band {
  top: -8%;
  bottom: -15%;
  border-radius: 28px 28px 4px 4px;
}

.lid-band-left,
.body-band-left {
  left: 18%;
}

.lid-band-right,
.body-band-right {
  right: 18%;
}

.chest-body {
  position: absolute;
  left: 3%;
  right: 3%;
  bottom: 8%;
  z-index: 3;
  height: 50%;
  border: 8px solid var(--wood-dark);
  border-radius: 6px;
  background:
    linear-gradient(90deg, rgba(0, 0, 0, 0.2) 0 4%, transparent 4% 96%, rgba(0, 0, 0, 0.2) 96%),
    repeating-linear-gradient(180deg, rgba(255, 221, 106, 0.08) 0 10px, transparent 10px 26px),
    linear-gradient(180deg, var(--wood-light) 0%, var(--wood) 50%, #5b270f 100%);
  box-shadow:
    inset 0 8px 0 rgba(255, 219, 126, 0.28),
    inset 0 -13px 0 rgba(35, 12, 5, 0.36);
}

.body-band {
  top: -8px;
  bottom: -8px;
}

.lock-plate {
  position: absolute;
  left: 50%;
  top: 23%;
  width: 19%;
  aspect-ratio: 1 / 0.95;
  border: 5px solid var(--brass-shadow);
  border-radius: 6px;
  transform: translateX(-50%);
  background:
    linear-gradient(135deg, rgba(255, 255, 210, 0.65), transparent 30%),
    linear-gradient(180deg, #ffe282, var(--brass) 55%, #9b641e);
  box-shadow:
    0 7px 0 #2a1006,
    inset -6px -7px 0 rgba(90, 45, 7, 0.22);
}

.lock-hole {
  position: absolute;
  left: 50%;
  top: 52%;
  z-index: 4;
  width: 4%;
  aspect-ratio: 1 / 1.5;
  border-radius: 50% 50% 4px 4px;
  transform: translateX(-50%);
  background: #211009;
  box-shadow: 0 -11px 0 -2px #211009;
}

.chest-feet {
  position: absolute;
  left: 7%;
  right: 7%;
  bottom: 2%;
  z-index: 2;
  height: 11%;
  background:
    linear-gradient(90deg, var(--wood-dark) 0 14%, transparent 14% 86%, var(--wood-dark) 86% 100%);
}

.chest.is-shaking {
  animation: chest-shake 420ms steps(2, end);
}

.chest.is-open .chest-lid {
  transform: translateY(-31%) rotateX(67deg);
  filter: brightness(1.22);
}

.chest.is-open .inner-light {
  opacity: 1;
}

.reward {
  position: absolute;
  bottom: -4px;
  display: grid;
  gap: 7px;
  place-items: center;
  opacity: 0;
  transform: translateY(12px) scale(0.96);
  transition: opacity 420ms ease, transform 420ms ease;
}

.reward.is-visible {
  opacity: 1;
  transform: translateY(0) scale(1);
}

.reward p {
  margin: 0;
  padding: 6px 10px;
  border: 2px solid rgba(255, 214, 103, 0.8);
  border-radius: 6px;
  color: #fff3bf;
  background: rgba(22, 11, 22, 0.82);
  font-weight: 800;
  text-align: center;
  text-shadow: 2px 2px 0 #000;
}

.candy-bar {
  position: relative;
  display: grid;
  place-items: center;
  width: min(270px, 68vw);
  height: 68px;
  border: 4px solid #f6f0ff;
  border-radius: 6px;
  color: white;
  overflow: hidden;
  background:
    linear-gradient(120deg, transparent 0 14%, rgba(255, 255, 255, 0.32) 14% 18%, transparent 18% 100%),
    linear-gradient(90deg, #17388f, #263eb8 48%, #4c2b92 100%);
  box-shadow:
    0 8px 0 rgba(0, 0, 0, 0.35),
    0 0 22px rgba(66, 243, 255, 0.52);
  font: 900 1.7rem/1 "Trebuchet MS", Verdana, sans-serif;
  letter-spacing: 0;
  text-shadow: 3px 3px 0 #0d1644;
}

.candy-bar::before,
.candy-bar::after {
  position: absolute;
  top: 0;
  width: 26px;
  height: 100%;
  content: "";
  background:
    repeating-linear-gradient(45deg, rgba(255, 255, 255, 0.95) 0 5px, rgba(255, 255, 255, 0.55) 5px 10px),
    #d7e9ff;
}

.candy-bar::before {
  left: 0;
}

.candy-bar::after {
  right: 0;
}

.answer-panel {
  width: min(620px, 100%);
  margin: 0 auto;
  padding: 14px;
  border: 3px solid #5a2d44;
  border-radius: 8px;
  background: rgba(13, 8, 18, 0.86);
  box-shadow:
    inset 0 0 0 2px rgba(255, 214, 103, 0.18),
    0 12px 28px rgba(0, 0, 0, 0.35);
}

.answer-panel label {
  display: block;
  margin: 0 0 8px;
  color: #ffd667;
  font-weight: 900;
  text-transform: uppercase;
  text-shadow: 2px 2px 0 #000;
}

.answer-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
}

.answer-row input,
.answer-row button {
  min-height: 48px;
  border-radius: 6px;
  font: 900 1rem/1 "Courier New", monospace;
}

.answer-row input {
  width: 100%;
  border: 3px solid #ad6e35;
  color: #1b0d05;
  background: #fff1be;
  box-shadow: inset 4px 4px 0 rgba(83, 38, 12, 0.18);
  padding: 0 12px;
  outline: none;
}

.answer-row input:focus {
  border-color: var(--cyan);
  box-shadow:
    inset 4px 4px 0 rgba(83, 38, 12, 0.18),
    0 0 0 3px rgba(66, 243, 255, 0.25);
}

.answer-row button {
  border: 3px solid #f9df81;
  color: #2d1607;
  background:
    linear-gradient(180deg, #fff49d, var(--brass) 68%, #b36e1c);
  box-shadow: 0 5px 0 #5d3210;
  padding: 0 20px;
  cursor: pointer;
}

.answer-row button:active {
  transform: translateY(3px);
  box-shadow: 0 2px 0 #5d3210;
}

.answer-row button:disabled,
.answer-row input:disabled {
  cursor: default;
  filter: grayscale(0.4);
  opacity: 0.76;
}

.feedback {
  min-height: 1.35em;
  margin: 9px 0 0;
  color: #d8fff8;
  font-weight: 800;
  text-align: center;
  text-shadow: 2px 2px 0 #000;
}

.feedback.is-error {
  color: #ffd1d1;
}

@keyframes chest-shake {
  0%,
  100% {
    transform: translate(0, 0) rotate(0);
  }

  12% {
    transform: translate(-13px, 2px) rotate(-2deg);
  }

  24% {
    transform: translate(11px, -1px) rotate(2deg);
  }

  36% {
    transform: translate(-9px, 0) rotate(-1deg);
  }

  48% {
    transform: translate(7px, 2px) rotate(1deg);
  }

  64% {
    transform: translate(-5px, -1px) rotate(-1deg);
  }

  82% {
    transform: translate(3px, 1px) rotate(1deg);
  }
}

@keyframes timer-pulse {
  50% {
    box-shadow:
      0 0 0 3px #4b2434,
      0 0 30px rgba(255, 94, 94, 0.66);
  }
}

@media (max-width: 720px) {
  .game-shell {
    grid-template-rows: auto auto auto auto;
    gap: 10px;
    padding: 12px;
  }

  .hud {
    justify-content: center;
  }

  .riddle-scroll {
    width: min(100%, 640px);
  }

  .riddle-copy {
    inset: 14% 12% 16%;
  }

  .riddle-copy p {
    line-height: 1.32;
  }

  .treasure-zone {
    min-height: 270px;
  }

  .answer-row {
    grid-template-columns: 1fr;
  }

  .answer-row button {
    width: 100%;
  }
}

@media (max-width: 430px) {
  .riddle-copy {
    inset: 12% 10% 14%;
  }

  .riddle-copy h1 {
    font-size: 1.45rem;
  }

  .riddle-copy p {
    font-size: 0.88rem;
  }

  .treasure-zone {
    min-height: 235px;
  }

  .reward {
    bottom: -8px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 1ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 1ms !important;
  }
}
