:root {
  color-scheme: light;
  --ink: #302b3f;
  --paper: #fff9e8;
  --cream: #f7dfac;
  --coral: #ef766b;
  --teal: #3a9d98;
  --machine-edge: #594a65;
}

* { box-sizing: border-box; }

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background: #f4d69e;
  color: var(--ink);
  font-family: "Trebuchet MS", "Arial Rounded MT Bold", system-ui, sans-serif;
  user-select: none;
  -webkit-user-select: none;
}

body {
  background:
    radial-gradient(circle at 12% 12%, rgba(255,255,255,.48) 0 2px, transparent 3px),
    linear-gradient(145deg, #f9e6b7, #eec98e);
  background-size: 28px 28px, auto;
}

button { font: inherit; }

.game-shell {
  width: min(1180px, 100%);
  height: 100dvh;
  min-height: 360px;
  margin: 0 auto;
  padding: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.topbar {
  min-height: 58px;
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 12px;
}

.brand { display: flex; align-items: baseline; gap: 8px; }
.brand-kicker {
  padding: 4px 6px;
  border: 2px solid var(--ink);
  border-radius: 7px;
  font-size: 10px;
  font-weight: 900;
  transform: rotate(-4deg);
}

h1 {
  margin: 0;
  font-size: clamp(23px, 4vw, 42px);
  line-height: .9;
  letter-spacing: -.06em;
}

.stats { display: flex; gap: 8px; }
.stat {
  min-width: 78px;
  padding: 5px 10px;
  border: 2px solid var(--ink);
  border-radius: 12px;
  background: rgba(255,249,232,.82);
  box-shadow: 0 3px 0 rgba(48,43,63,.16);
  text-align: center;
}
.stat span { display: block; font-size: 9px; font-weight: 900; letter-spacing: .1em; }
.stat strong { font-size: 20px; line-height: 1; }

.sound-button {
  width: 42px;
  height: 42px;
  border: 2px solid var(--ink);
  border-radius: 50%;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 3px 0 rgba(48,43,63,.18);
  cursor: pointer;
  font-size: 22px;
  font-weight: 900;
}
.sound-button:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(48,43,63,.18); }
.sound-button.muted { opacity: .5; }

.machine {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  border: 3px solid var(--machine-edge);
  border-radius: 26px;
  background: #fdf2d1;
  box-shadow: 0 8px 0 rgba(89,74,101,.2), 0 18px 40px rgba(96,62,46,.15);
  touch-action: none;
}

#gameCanvas {
  display: block;
  width: 100%;
  height: 100%;
  outline: none;
  cursor: ew-resize;
  touch-action: none;
}

.ladder {
  position: absolute;
  top: 14px;
  bottom: 15px;
  left: 50%;
  width: clamp(48px, 9.2%, 92px);
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  pointer-events: none;
}

.ladder-title {
  flex: 0 0 auto;
  padding: 4px 7px;
  border-radius: 8px;
  background: var(--ink);
  color: #fff9e8;
  font-size: clamp(7px, 1vw, 10px);
  font-weight: 900;
  letter-spacing: .12em;
}

.ladder-faces {
  width: 100%;
  min-height: 0;
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-evenly;
  padding: 4px 0;
}

.ladder-step {
  position: relative;
  display: grid;
  place-items: center;
  min-height: 0;
  flex: 1 1 0;
}
.ladder-step:not(:last-child)::after {
  content: "›";
  position: absolute;
  left: 50%;
  bottom: -3px;
  transform: translateX(-50%) rotate(90deg);
  color: rgba(48,43,63,.52);
  font-size: 13px;
  font-weight: 900;
}
.ladder-step img {
  display: block;
  width: clamp(20px, 4.2vh, 38px);
  height: clamp(20px, 4.2vh, 38px);
  filter: drop-shadow(0 2px 0 rgba(48,43,63,.13));
}
.ladder-step:last-child img { width: clamp(25px, 5vh, 45px); height: clamp(25px, 5vh, 45px); }

.instruction,
.status-pill {
  position: absolute;
  z-index: 4;
  left: 25%;
  transform: translateX(-50%);
  border: 2px solid var(--ink);
  background: rgba(255,249,232,.95);
  color: var(--ink);
  box-shadow: 0 4px 0 rgba(48,43,63,.16);
  pointer-events: none;
  transition: opacity .25s, transform .25s;
}

.instruction {
  top: 68px;
  width: min(270px, 40%);
  padding: 9px 12px;
  border-radius: 13px;
  text-align: center;
  font-size: clamp(9px, 1.3vw, 13px);
}
.instruction strong { display: block; font-size: 1.15em; }
.instruction.hidden { opacity: 0; transform: translate(-50%, -8px); }

.status-pill {
  left: 50%;
  bottom: 13px;
  padding: 5px 9px;
  border-radius: 999px;
  font-size: clamp(8px, 1.2vw, 11px);
  font-weight: 900;
  letter-spacing: .08em;
}
.status-pill.right { background: #d8f2e8; }

.game-over {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  background: rgba(48,43,63,.2);
  backdrop-filter: blur(3px);
  opacity: 0;
  visibility: hidden;
  transition: opacity .25s, visibility .25s;
}
.game-over.visible { opacity: 1; visibility: visible; }
.game-over-card {
  width: min(310px, 78%);
  padding: 22px;
  border: 3px solid var(--ink);
  border-radius: 24px;
  background: var(--paper);
  box-shadow: 0 9px 0 rgba(48,43,63,.2);
  text-align: center;
  transform: rotate(-1deg);
}
.game-over-kicker { font-size: 11px; font-weight: 900; letter-spacing: .15em; }
.game-over h2 { margin: 0; color: var(--coral); font-size: clamp(48px, 11vw, 76px); line-height: .95; letter-spacing: -.05em; }
.game-over p { margin: 14px 0; }
.game-over p span { font-size: 11px; font-weight: 900; letter-spacing: .12em; }
.game-over p strong { margin-left: 6px; font-size: 24px; }
.game-over button {
  padding: 11px 22px;
  border: 2px solid var(--ink);
  border-radius: 999px;
  background: #f4c95d;
  color: var(--ink);
  box-shadow: 0 4px 0 var(--ink);
  cursor: pointer;
  font-weight: 900;
}
.game-over button:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }

@media (max-width: 600px) {
  .game-shell { padding: max(6px, env(safe-area-inset-top)) 5px max(6px, env(safe-area-inset-bottom)); gap: 5px; }
  .topbar { min-height: 46px; gap: 6px; }
  .brand-kicker { display: none; }
  h1 { font-size: clamp(20px, 7vw, 29px); }
  .stats { gap: 4px; }
  .stat { min-width: 58px; padding: 4px 5px; border-radius: 9px; }
  .stat span { font-size: 7px; }
  .stat strong { font-size: 16px; }
  .sound-button { width: 34px; height: 34px; font-size: 18px; }
  .machine { border-width: 2px; border-radius: 17px; box-shadow: 0 5px 0 rgba(89,74,101,.2); }
  .ladder { top: 8px; bottom: 9px; width: 12%; }
  .ladder-title { padding: 3px 4px; font-size: 6px; }
  .ladder-step:not(:last-child)::after { font-size: 10px; bottom: -2px; }
  .instruction { top: 56px; width: 42%; padding: 7px; }
  .status-pill { bottom: 8px; padding: 4px 7px; letter-spacing: .03em; }
}

@media (max-height: 540px) {
  .game-shell { padding-top: 5px; padding-bottom: 5px; }
  .topbar { min-height: 38px; }
  h1 { font-size: 24px; }
  .stat { padding-top: 2px; padding-bottom: 2px; }
  .sound-button { width: 34px; height: 34px; }
  .instruction { top: 42px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
