* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.hidden {
  display: none !important;
}

/* Prevent zoom on double-click */
html,
body {
  touch-action: manipulation;
  overscroll-behavior: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  height: 100%;
}

body {
  font-family: "Open Sans", sans-serif;
  font-optical-sizing: auto;
  background: linear-gradient(160deg, #2d2a26 0%, #1f1d1a 40%, #252320 100%);
  min-height: 100vh;
  color: #e8e4df;
  letter-spacing: 0.03em;
  margin: 0;
  padding: 0;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

/* While playing, the page is locked to the viewport: no scrolling means
   edge swipes can't rubber-band the page or summon scrollbars mid-game */
body.snl-playing {
  overflow: hidden;
  height: 100vh;
  height: 100dvh;
}

body.snl-playing .small-header {
  display: none;
}

body.snl-playing .game-container {
  min-height: 0;
  height: 100vh;
  height: 100dvh;
}

/* Animated background - warm earth tones */
body::before {
  content: "";
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background:
    radial-gradient(circle at 15% 85%, rgba(139, 168, 134, 0.08) 0%, transparent 45%),
    radial-gradient(circle at 85% 15%, rgba(194, 139, 115, 0.06) 0%, transparent 40%),
    radial-gradient(circle at 50% 50%, rgba(168, 162, 150, 0.05) 0%, transparent 50%);
  pointer-events: none;
  z-index: 0;
}

.container {
  position: relative;
  z-index: 1;
}

/* Full-width dungeon backdrop (level-1 mood): while playing, the stone wall
   fills the ENTIRE viewport so the dungeon isn't boxed inside the canvas. The
   playfield canvas stays a limited centred column and reads as a lit shaft cut
   into this wider wall. */
body.snl-playing::before {
  background:
    /* warm torch glow pooling near the top where the exit sits */
    radial-gradient(65% 42% at 50% 9%, rgba(255, 190, 110, 0.12) 0%, transparent 72%),
    /* faint dressed-stone courses */
    repeating-linear-gradient(180deg,
      rgba(0, 0, 0, 0.12) 0px, rgba(0, 0, 0, 0.12) 1px,
      transparent 1px, transparent 44px),
    /* vertical stone gradient: warmer near the exit, darker in the depths */
    linear-gradient(180deg, #2c2114 0%, #241c14 34%, #33271a 64%, #1a130b 100%);
  opacity: 1;
}

/* Level 2 — Sunken Sewers: damp teal-green stone shaft */
body.snl-playing.snl-level-2::before {
  background:
    radial-gradient(60% 40% at 50% 9%, rgba(120, 220, 180, 0.10) 0%, transparent 72%),
    repeating-linear-gradient(180deg,
      rgba(0, 0, 0, 0.12) 0px, rgba(0, 0, 0, 0.12) 1px,
      transparent 1px, transparent 44px),
    linear-gradient(180deg, #1c322c 0%, #21382f 30%, #16302a 62%, #0a1a17 100%);
}

/* Level 3 — Broken Gate: moonlit night-blue wall opening to sky */
body.snl-playing.snl-level-3::before {
  background:
    radial-gradient(70% 40% at 50% 8%, rgba(180, 205, 255, 0.12) 0%, transparent 74%),
    repeating-linear-gradient(180deg,
      rgba(0, 0, 0, 0.12) 0px, rgba(0, 0, 0, 0.12) 1px,
      transparent 1px, transparent 44px),
    linear-gradient(180deg, #141d33 0%, #2a3242 34%, #232a37 64%, #12161f 100%);
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Congratulations Modal */
.congratulations-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  padding: 20px;
  animation: fadeIn 0.3s ease-out;
}

.congratulations-content {
  background: linear-gradient(145deg, rgba(55, 52, 48, 0.6) 0%, rgba(40, 38, 35, 0.8) 100%);
  backdrop-filter: blur(20px);
  border-radius: 20px;
  padding: 36px;
  max-width: 380px;
  width: 100%;
  text-align: center;
  border: 1px solid rgba(139, 168, 134, 0.15);
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5);
  animation: scaleIn 0.4s ease;
}

@keyframes scaleIn {
  from {
    opacity: 0;
    transform: scale(0.9);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

/* The freed hamster, celebrating above the escape stats. Rides in on a hop,
   then keeps bobbing gently. Only shown on the win modal, never on game over. */
.escape-hamster {
  display: block;
  width: auto;
  height: 132px;
  margin: -8px auto 4px;
  filter: drop-shadow(0 10px 18px rgba(0, 0, 0, 0.45));
  transform-origin: 50% 100%;
  animation:
    hamsterPopIn 0.5s cubic-bezier(0.2, 1.5, 0.4, 1) 0.15s backwards,
    hamsterCheer 2.2s ease-in-out 0.65s infinite;
}

/* Warm glow behind the hamster so it reads against the dark modal */
.congratulations-content:not(.game-over) {
  background:
    radial-gradient(64% 40% at 50% 16%, rgba(217, 164, 65, 0.22) 0%, transparent 70%),
    linear-gradient(145deg, rgba(55, 52, 48, 0.6) 0%, rgba(40, 38, 35, 0.8) 100%);
}

@keyframes hamsterPopIn {
  from { opacity: 0; transform: translateY(26px) scale(0.6); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes hamsterCheer {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  25%      { transform: translateY(-9px) rotate(-3deg); }
  50%      { transform: translateY(0) rotate(0deg); }
  75%      { transform: translateY(-5px) rotate(3deg); }
}

@media (prefers-reduced-motion: reduce) {
  .escape-hamster { animation: none; }
}

/* Short viewports (landscape phones): the hamster must not push Play Again
   off screen, so shrink it rather than let the modal overflow */
@media (max-height: 620px) {
  .escape-hamster {
    height: 84px;
    margin-top: 0;
  }
}

@media (max-height: 480px) {
  .escape-hamster {
    height: 60px;
  }
}

.congratulations-content h1 {
  font-size: 1.4rem;
  margin-bottom: 24px;
  color: #a8b5a0;
  text-shadow: 0 2px 20px rgba(168, 181, 160, 0.3);
}

/* Stats Display */
.stats-display {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 20px;
}

.stat-item {
  background: rgba(0, 0, 0, 0.25);
  border-radius: 10px;
  padding: 14px 8px;
  border: 1px solid rgba(255, 255, 255, 0.05);
}

.stat-label {
  font-size: 0.65rem;
  color: rgba(255, 255, 255, 0.45);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 4px;
}

.stat-value {
  font-size: 1.3rem;
  font-weight: 700;
  color: #fff;
}

.stat-value.combo {
  color: #c28b73;
}

.difficulty-info {
  margin-bottom: 20px;
  padding: 12px;
  background: rgba(139, 168, 134, 0.08);
  border-radius: 10px;
  border: 1px solid rgba(139, 168, 134, 0.15);
}

.difficulty-info p {
  font-size: 0.9rem;
  margin: 0;
  color: #a8b5a0;
}

.play-again-btn {
  padding: 12px 40px;
  background: linear-gradient(135deg, #c28b73 0%, #a67260 100%);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 20px rgba(166, 114, 96, 0.3);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.play-again-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 28px rgba(166, 114, 96, 0.4);
}

/* Game Over Modal Styling */
.congratulations-content.game-over h1 {
  color: #d4847a;
  text-shadow: 0 2px 20px rgba(212, 132, 122, 0.3);
}

/* Game Container */
.game-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  max-width: 500px;
  width: 100%;
  text-align: center;
  margin-inline: auto;
  min-height: 100vh;
  padding: 10px;
  position: relative;
  z-index: 1;
}

/* Sound Toggle — floats over the canvas, top-right above the finish line */
.sound-toggle,
.home-link {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 60;
  width: 36px;
  height: 36px;
  padding: 7px;
  color: rgba(139, 168, 134, 0.75);
  background: rgba(20, 16, 12, 0.55);
  border: 1px solid rgba(139, 168, 134, 0.25);
  border-radius: 8px;
  cursor: pointer;
}

.home-link {
  right: auto;
  left: 10px;
}

.sound-toggle:hover,
.home-link:hover {
  background: rgba(35, 28, 20, 0.75);
}

.sound-toggle svg,
.home-link img {
  display: block;
  width: 100%;
  height: 100%;
}

.home-link img {
  object-fit: contain;
}

.sound-toggle .icon-sound-off,
.sound-toggle.is-muted .icon-sound-on {
  display: none;
}

.sound-toggle.is-muted .icon-sound-off {
  display: block;
}

.sound-toggle.is-muted {
  color: #6a7a8a;
}

/* Combo Display */
.combo-display {
  position: absolute;
  top: 70px;
  left: 50%;
  transform: translateX(-50%);
  padding: 8px 18px;
  background: linear-gradient(135deg, rgba(194, 139, 115, 0.2) 0%, rgba(166, 114, 96, 0.12) 100%);
  border-radius: 16px;
  border: 1px solid rgba(194, 139, 115, 0.3);
  opacity: 0;
  transition: opacity 0.3s ease;
  z-index: 100;
}

.combo-display.active {
  opacity: 1;
  animation: comboPulse 0.5s ease-out;
}

@keyframes comboPulse {
  0% { transform: translateX(-50%) scale(0.8); }
  50% { transform: translateX(-50%) scale(1.1); }
  100% { transform: translateX(-50%) scale(1); }
}

.combo-text {
  font-size: 0.85rem;
  font-weight: 700;
  color: #c28b73;
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

.combo-multiplier {
  font-size: 1.1rem;
  font-weight: 800;
  color: #fff;
  margin-left: 6px;
}

/* Game Board */
.game-board {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  width: 100%;
  max-width: 480px;
}

#gameCanvas {
  background: rgba(0, 0, 0, 0.12);
  border: 1px solid rgba(139, 168, 134, 0.12);
  box-shadow:
    0 4px 24px rgba(0, 0, 0, 0.3),
    inset 0 0 40px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  /* The world's shape is chosen per run by JS (chooseLogicalHeight), which
     sets --snl-aspect and aspect-ratio inline so the playfield fills a tall
     phone instead of letterboxing. CSS just fits that shape into the space:
     sized by width, but never taller than the viewport leaves room for.
     --snl-chrome is the vertical space reserved for everything else, and
     the values here are the 400x533 fallback before JS runs. */
  --snl-chrome: 110px;
  --snl-aspect: 0.75;
  width: min(100%, 480px, calc((100vh - var(--snl-chrome)) * var(--snl-aspect)));
  width: min(100%, 480px, calc((100dvh - var(--snl-chrome)) * var(--snl-aspect)));
  aspect-ratio: 400 / 533;
  height: auto;
  outline: none;
  /* Without this the browser may start a page scroll from a drag on the
     canvas; once scrolling begins, touch events turn cancelable=false and
     preventDefault() in the handlers gets ignored (console interventions) */
  touch-action: none;
}

/* Full-width dungeon backdrop canvas — the real dungeon rendered edge-to-edge.
   The playfield is aspect-locked to 400x533 so it can't fill a tall phone; the
   backdrop covers the WHOLE viewport instead (JS paints extra wall above and
   below the playfield) so there are no flat bands. Sits behind the playfield. */
#bgCanvas {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  z-index: 0;
  pointer-events: none;
  display: none;
}

body.snl-playing #bgCanvas {
  display: block;
}

/* While playing, the playfield canvas is transparent so the full-width dungeon
   shows through and around it — hazards, tower and mouse float over the
   continuous dungeon with no boxed frame. */
body.snl-playing #gameCanvas {
  background: transparent;
  border-color: transparent;
  box-shadow: none;
}

/* Swipe Gesture Hint */
.swipe-hint {
  position: absolute;
  bottom: 130px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 72px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.4s ease;
  z-index: 50;
}

.swipe-hint.visible {
  opacity: 1;
}

.swipe-arrow {
  width: 52px;
  height: 52px;
  color: rgba(255, 255, 255, 0.55);
  filter: drop-shadow(0 2px 8px rgba(0, 0, 0, 0.5));
}

.swipe-arrow-left {
  animation: swipeLeft 1.3s ease-in-out infinite;
}

.swipe-arrow-right {
  animation: swipeRight 1.3s ease-in-out infinite;
}

@keyframes swipeLeft {
  0%, 100% { transform: translateX(0); opacity: 0.3; }
  50% { transform: translateX(-16px); opacity: 0.9; }
}

@keyframes swipeRight {
  0%, 100% { transform: translateX(0); opacity: 0.3; }
  50% { transform: translateX(16px); opacity: 0.9; }
}

/* On-screen move buttons (transparent key caps) — a bottom-right cluster placed
   OUTSIDE the canvas, in the right margin beside the board's lower corner.
   Shown on desktop; hidden on mobile (<=768px) where the player swipes. */
.key-controls {
  position: absolute;
  left: 100%;        /* start just past the board's right edge */
  bottom: 24px;
  margin-left: 14px;
  display: flex;
  gap: 10px;
  z-index: 55;
  /* Only the caps catch taps, not the gap between them */
  pointer-events: none;
}

.key-controls .key-cap {
  pointer-events: auto;
}

.key-cap {
  width: 52px;
  height: 52px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  /* Transparent fill, just an outlined key cap around the arrow glyph */
  color: rgba(242, 232, 213, 0.55);
  background: transparent;
  border: 1.5px solid rgba(242, 232, 213, 0.35);
  border-radius: 10px;
  box-shadow: none;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  transition: transform 0.06s ease, color 0.15s ease, border-color 0.15s ease;
}

.key-cap svg {
  width: 30px;
  height: 30px;
  display: block;
  filter: drop-shadow(0 1px 3px rgba(0, 0, 0, 0.7));
}

.key-cap:hover {
  color: rgba(242, 232, 213, 0.85);
  border-color: rgba(242, 232, 213, 0.55);
}

.key-cap:active,
.key-cap.pressed {
  transform: translateY(1px) scale(0.94);
  color: #f6ecd6;
  border-color: rgba(255, 236, 190, 0.7);
}

/* Floating Numbers */
.floating-number {
  position: absolute;
  font-size: 1.4rem;
  font-weight: 800;
  pointer-events: none;
  animation: floatUp 1s ease-out forwards;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.6);
  z-index: 1000;
}

.floating-number.positive {
  color: #8ba886;
}

.floating-number.negative {
  color: #d4847a;
}

.floating-number.combo {
  color: #c28b73;
  font-size: 1.1rem;
}

@keyframes floatUp {
  0% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
  50% {
    opacity: 1;
    transform: translateY(-25px) scale(1.15);
  }
  100% {
    opacity: 0;
    transform: translateY(-50px) scale(0.9);
  }
}

/* Screen Flash */
.screen-flash {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  z-index: 999;
  opacity: 0;
}

.screen-flash.snake {
  background: radial-gradient(circle, rgba(194, 139, 115, 0.2) 0%, transparent 65%);
  animation: flashOut 0.3s ease-out;
}

.screen-flash.ladder {
  background: radial-gradient(circle, rgba(139, 168, 134, 0.2) 0%, transparent 65%);
  animation: flashOut 0.3s ease-out;
}

@keyframes flashOut {
  0% { opacity: 1; }
  100% { opacity: 0; }
}

/* Confetti */
.confetti-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1999;
  overflow: hidden;
}

.confetti {
  position: absolute;
  width: 10px;
  height: 10px;
  opacity: 0;
}

/* Bottom Controls */
.bottom-controls {
  display: flex;
  flex-direction: column;
  gap: 4px;
  width: 100%;
  margin-top: 4px;
}

.dice-row {
  display: flex;
  justify-content: space-between;
  width: 100%;
  max-width: 480px;
  background: rgba(0, 0, 0, 0.2);
  padding: 2px;
  border: 1px solid rgba(255, 255, 255, 0.05);
  touch-action: none;
}

.dice-face {
  width: calc(100% / 6);
  height: 48px;
  background: transparent;
  border: none;
  border-radius: 6px;
  display: grid;
  grid-template-columns: repeat(3, 12px);
  grid-template-rows: repeat(3, 12px);
  place-content: center;
  color: rgba(255, 255, 255, 0.2);
  cursor: default;
  transition: all 0.25s ease;
  opacity: 0.4;
}

.dice-face .pip {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: currentColor;
  place-self: center;
}

/* Pip layouts (row / column on a 3x3 grid) */
.dice-face[data-value="1"] .pip:nth-child(1) { grid-area: 2 / 2; }

.dice-face[data-value="2"] .pip:nth-child(1) { grid-area: 1 / 3; }
.dice-face[data-value="2"] .pip:nth-child(2) { grid-area: 3 / 1; }

.dice-face[data-value="3"] .pip:nth-child(1) { grid-area: 1 / 3; }
.dice-face[data-value="3"] .pip:nth-child(2) { grid-area: 2 / 2; }
.dice-face[data-value="3"] .pip:nth-child(3) { grid-area: 3 / 1; }

.dice-face[data-value="4"] .pip:nth-child(1) { grid-area: 1 / 1; }
.dice-face[data-value="4"] .pip:nth-child(2) { grid-area: 1 / 3; }
.dice-face[data-value="4"] .pip:nth-child(3) { grid-area: 3 / 1; }
.dice-face[data-value="4"] .pip:nth-child(4) { grid-area: 3 / 3; }

.dice-face[data-value="5"] .pip:nth-child(1) { grid-area: 1 / 1; }
.dice-face[data-value="5"] .pip:nth-child(2) { grid-area: 1 / 3; }
.dice-face[data-value="5"] .pip:nth-child(3) { grid-area: 2 / 2; }
.dice-face[data-value="5"] .pip:nth-child(4) { grid-area: 3 / 1; }
.dice-face[data-value="5"] .pip:nth-child(5) { grid-area: 3 / 3; }

.dice-face[data-value="6"] .pip:nth-child(1) { grid-area: 1 / 1; }
.dice-face[data-value="6"] .pip:nth-child(2) { grid-area: 1 / 3; }
.dice-face[data-value="6"] .pip:nth-child(3) { grid-area: 2 / 1; }
.dice-face[data-value="6"] .pip:nth-child(4) { grid-area: 2 / 3; }
.dice-face[data-value="6"] .pip:nth-child(5) { grid-area: 3 / 1; }
.dice-face[data-value="6"] .pip:nth-child(6) { grid-area: 3 / 3; }

.dice-face.active {
  opacity: 1;
  color: #8ba886;
  background: rgba(139, 168, 134, 0.12);
}

/* Responsive */
@media (max-width: 768px) {
  .game-container {
    padding: 8px;
  }

  #gameCanvas {
    --snl-chrome: 100px;
  }

  .dice-face {
    height: 44px;
    grid-template-columns: repeat(3, 11px);
    grid-template-rows: repeat(3, 11px);
  }

  .dice-face .pip {
    width: 7px;
    height: 7px;
  }

  /* Mobile uses swipe — hide the on-screen keys */
  .key-controls {
    display: none;
  }
}

@media (max-width: 375px) {
  #gameCanvas {
    --snl-chrome: 90px;
  }

  .dice-face {
    height: 38px;
    grid-template-columns: repeat(3, 10px);
    grid-template-rows: repeat(3, 10px);
  }

  .dice-face .pip {
    width: 6px;
    height: 6px;
  }
}

@media (max-width: 320px) {
  #gameCanvas {
    --snl-chrome: 80px;
  }

  .dice-face {
    height: 34px;
    grid-template-columns: repeat(3, 9px);
    grid-template-rows: repeat(3, 9px);
  }

  .dice-face .pip {
    width: 5px;
    height: 5px;
  }
}
