:root {
  --paper: #f4ead2;
  --paper-deep: #ead9b4;
  --ink: #27284d;
  --ink-soft: #3d3e6b;
  --red: #e8472b;
  --red-deep: #c23318;
  --mustard: #f2a93b;
  --teal: #16917b;
  --teal-bright: #2fc4a7;
  --violet: #7b4ba0;
  --ice: #a9dcf5;
  --leaf: #4a8f3c;
  --leaf-bright: #6cb84f;
  --grass: #dbe3b3;
  --grass-alt: #d2dca6;
  --cell: 56px;
  --frame: 5px;
}

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

html {
  height: 100%;
}

body {
  min-height: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: 'Sometype Mono', monospace;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

.halftone {
  position: fixed;
  inset: 0;
  background-image: radial-gradient(rgba(39, 40, 77, 0.10) 1px, transparent 1.6px);
  background-size: 14px 14px;
  pointer-events: none;
  z-index: 0;
}

.grain {
  position: fixed;
  inset: -50%;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2'/%3E%3C/filter%3E%3Crect width='200' height='200' filter='url(%23n)' opacity='0.5'/%3E%3C/svg%3E");
  opacity: 0.07;
  mix-blend-mode: multiply;
  pointer-events: none;
  z-index: 50;
  animation: grain-shift 0.9s steps(3) infinite;
}

@keyframes grain-shift {
  0% { transform: translate(0, 0); }
  33% { transform: translate(2%, -1%); }
  66% { transform: translate(-1%, 2%); }
  100% { transform: translate(0, 0); }
}

.flash {
  position: fixed;
  inset: 0;
  background: radial-gradient(ellipse at center, transparent 30%, rgba(232, 71, 43, 0.55));
  opacity: 0;
  pointer-events: none;
  z-index: 40;
  transition: opacity 0.5s;
}

.flash.on {
  opacity: 1;
  transition: opacity 0.05s;
}

.stage {
  position: relative;
  z-index: 1;
  max-width: 1020px;
  margin: 0 auto;
  padding: 22px 18px 36px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* ---------- masthead ---------- */

.masthead {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.title {
  font-family: 'Shrikhand', cursive;
  font-size: clamp(2.4rem, 7vw, 4.2rem);
  line-height: 0.95;
  color: var(--red);
  letter-spacing: 0.01em;
  text-shadow: 4px 4px 0 var(--ink);
  transform: rotate(-1.5deg);
  position: relative;
  display: inline-block;
}

.sticker {
  position: absolute;
  top: -0.35em;
  right: -0.62em;
  font-size: 0.52em;
  color: var(--ink);
  background: var(--mustard);
  border: 4px solid var(--ink);
  border-radius: 50%;
  width: 1.5em;
  height: 1.5em;
  display: grid;
  place-items: center;
  text-shadow: none;
  transform: rotate(12deg);
  box-shadow: 3px 3px 0 var(--red-deep);
}

.tagline {
  margin-top: 10px;
  font-size: 0.82rem;
  font-style: italic;
  letter-spacing: 0.06em;
  color: var(--ink-soft);
  transform: rotate(-1.5deg);
}

.level-chips {
  display: flex;
  gap: 7px;
  flex-wrap: wrap;
}

.chip {
  font-family: 'Sometype Mono', monospace;
  font-weight: 700;
  font-size: 0.95rem;
  width: 38px;
  height: 38px;
  border: 3px solid var(--ink);
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
  box-shadow: 3px 3px 0 var(--ink);
  transition: transform 0.08s, box-shadow 0.08s;
}

.chip:hover:not(:disabled) {
  transform: translate(-1px, -1px);
  box-shadow: 4px 4px 0 var(--ink);
}

.chip:active:not(:disabled) {
  transform: translate(3px, 3px);
  box-shadow: 0 0 0 var(--ink);
}

.chip.done {
  background: var(--teal);
  color: var(--paper);
}

.chip.current {
  background: var(--red);
  color: var(--paper);
  transform: rotate(-6deg) scale(1.12);
}

.chip:disabled {
  opacity: 0.35;
  cursor: not-allowed;
  box-shadow: none;
}

/* ---------- board ---------- */

.board-zone {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.hud {
  width: 100%;
  max-width: 640px;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 10px;
}

.hud-level {
  font-weight: 700;
  font-size: 0.95rem;
  letter-spacing: 0.08em;
  background: var(--ink);
  color: var(--paper);
  padding: 4px 12px;
  transform: rotate(-0.8deg);
  box-shadow: 3px 3px 0 var(--mustard);
}

.hud-moves {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--ink-soft);
}

.board-wrap {
  position: relative;
  padding: 14px;
  background: var(--paper-deep);
  border: var(--frame) solid var(--ink);
  box-shadow: 12px 12px 0 var(--red);
}

.board-wrap.dead {
  animation: shake 0.45s;
}

@keyframes shake {
  0%, 100% { transform: translate(0, 0); }
  15% { transform: translate(-7px, 3px) rotate(-0.7deg); }
  35% { transform: translate(6px, -4px) rotate(0.7deg); }
  55% { transform: translate(-5px, -3px); }
  75% { transform: translate(4px, 3px); }
}

.board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell));
  grid-auto-rows: var(--cell);
}

.cell {
  width: var(--cell);
  height: var(--cell);
  animation: pop-in 0.3s cubic-bezier(0.34, 1.56, 0.64, 1) both;
  animation-delay: var(--delay);
}

@keyframes pop-in {
  from { transform: scale(0); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.cell.floor {
  background: var(--paper);
  position: relative;
}

.cell.floor.alt {
  background: #efe2c2;
}

.cell.floor::after {
  content: '';
  position: absolute;
  inset: 0;
  margin: auto;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: rgba(39, 40, 77, 0.16);
}

.cell.wall {
  background:
    repeating-linear-gradient(45deg, transparent 0 7px, rgba(244, 234, 210, 0.09) 7px 13px),
    var(--ink);
  border: 2px solid var(--ink);
  box-shadow: inset 3px 3px 0 rgba(244, 234, 210, 0.18), inset -3px -3px 0 rgba(0, 0, 0, 0.3);
}

.cell.exit {
  position: relative;
}

.cell.exit::after {
  display: none;
}

.cell.exit svg {
  position: absolute;
  inset: 6%;
  width: 88%;
  height: 88%;
  animation: exit-pulse 1.6s ease-in-out infinite;
}

.cell.bush {
  position: relative;
  background: var(--grass);
}

.cell.bush.alt {
  background: var(--grass-alt);
}

.cell.bush::after {
  display: none;
}

.cell.bush svg {
  position: absolute;
  inset: 2%;
  width: 96%;
  height: 96%;
  transform-origin: 50% 88%;
  animation: bush-sway 3.4s ease-in-out infinite;
}

.cell.bush:nth-child(odd) svg {
  animation-delay: -1.7s;
}

@keyframes bush-sway {
  0%, 100% { transform: rotate(-2.5deg); }
  50% { transform: rotate(2.5deg); }
}

@keyframes exit-pulse {
  0%, 100% { filter: drop-shadow(0 0 0 rgba(47, 196, 167, 0)); transform: scale(1); }
  50% { filter: drop-shadow(0 0 9px rgba(47, 196, 167, 0.9)); transform: scale(1.05); }
}

/* ---------- tokens ---------- */

.token {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--cell);
  height: var(--cell);
  display: grid;
  place-items: center;
  transition: transform 0.16s cubic-bezier(0.34, 1.3, 0.64, 1);
  will-change: transform;
  pointer-events: none;
}

.token[hidden] {
  display: none;
}

.token svg {
  width: 88%;
  height: 88%;
  transition: transform 0.15s;
}

.token.flip svg {
  transform: scaleX(-1);
}

.token.player {
  z-index: 5;
  animation: token-drop 0.4s 0.35s cubic-bezier(0.34, 1.56, 0.64, 1) backwards;
}

.token.monster {
  z-index: 6;
  animation: token-drop 0.4s 0.5s cubic-bezier(0.34, 1.56, 0.64, 1) backwards;
}

.token.monster2 {
  z-index: 7;
  animation: token-drop 0.4s 0.6s cubic-bezier(0.34, 1.56, 0.64, 1) backwards;
}

@keyframes token-drop {
  from { opacity: 0; scale: 2.2; }
  to { opacity: 1; scale: 1; }
}

.token.freeze {
  z-index: 3;
  animation: token-drop 0.4s 0.7s cubic-bezier(0.34, 1.56, 0.64, 1) backwards;
}

.token.freeze svg {
  width: 68%;
  height: 68%;
  animation: ice-bob 1.5s ease-in-out infinite;
  filter: drop-shadow(0 0 6px rgba(169, 220, 245, 0.9));
}

@keyframes ice-bob {
  0%, 100% { transform: translateY(0) rotate(-5deg); }
  50% { transform: translateY(-4px) rotate(5deg); }
}

.token.monster.frozen svg {
  filter: saturate(0.15) brightness(1.35) drop-shadow(0 0 7px #6db9e8);
  animation: shiver 0.4s infinite;
}

.token.monster.frozen::after {
  content: '❄';
  position: absolute;
  top: 0;
  right: 4px;
  font-size: calc(var(--cell) * 0.3);
  color: #2b8fc4;
  text-shadow: 0 0 5px #fff;
  animation: ice-bob 1.5s ease-in-out infinite;
}

@keyframes shiver {
  0%, 100% { translate: 0 0; }
  25% { translate: -1.5px 0; }
  75% { translate: 1.5px 0; }
}

.token.monster.lunge svg {
  animation: lunge 0.3s;
}

@keyframes lunge {
  40% { transform: scale(1.45) rotate(-6deg); }
}

.token.player.bump {
  animation: bump 0.22s;
}

@keyframes bump {
  35% { translate: 0 -3px; scale: 0.92 1.08; }
}

.token.player.gone svg {
  animation: vanish 0.45s forwards;
}

@keyframes vanish {
  to { transform: scale(0) rotate(260deg); opacity: 0; }
}

.print {
  position: absolute;
  width: var(--cell);
  height: var(--cell);
  display: grid;
  place-items: center;
  pointer-events: none;
  z-index: 4;
}

.print::before {
  content: '';
  width: 26%;
  height: 26%;
  border-radius: 50% 50% 50% 4px;
  background: rgba(232, 71, 43, 0.5);
  animation: print-fade 0.8s forwards;
}

@keyframes print-fade {
  to { opacity: 0; transform: scale(0.4); }
}

.hint {
  max-width: 560px;
  text-align: center;
  font-size: 0.85rem;
  font-style: italic;
  color: var(--ink-soft);
  min-height: 2.2em;
}

.hint b {
  color: var(--red-deep);
}

/* ---------- controls ---------- */

.controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.dpad {
  display: grid;
  grid-template-areas: '. up .' 'left wait right' '. down .';
  gap: 6px;
}

.key {
  width: 52px;
  height: 46px;
  font-size: 1rem;
  font-family: inherit;
  background: var(--paper);
  color: var(--ink);
  border: 3px solid var(--ink);
  box-shadow: 0 4px 0 var(--ink);
  cursor: pointer;
  transition: transform 0.06s, box-shadow 0.06s;
}

.key:active {
  transform: translateY(4px);
  box-shadow: 0 0 0 var(--ink);
}

.key[data-dir='up'] { grid-area: up; }
.key[data-dir='left'] { grid-area: left; }
.key[data-dir='right'] { grid-area: right; }
.key[data-dir='down'] { grid-area: down; }

.key-wait {
  grid-area: wait;
  background: var(--mustard);
  font-weight: 700;
}

.actions {
  display: flex;
  gap: 9px;
  flex-wrap: wrap;
  justify-content: center;
}

.btn {
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  padding: 8px 14px;
  background: var(--paper);
  color: var(--ink);
  border: 3px solid var(--ink);
  box-shadow: 4px 4px 0 var(--ink);
  cursor: pointer;
  transition: transform 0.07s, box-shadow 0.07s;
}

.btn:hover {
  transform: translate(-1px, -1px);
  box-shadow: 5px 5px 0 var(--ink);
}

.btn:active {
  transform: translate(4px, 4px);
  box-shadow: 0 0 0 var(--ink);
}

.btn kbd {
  font-family: inherit;
  font-size: 0.7rem;
  background: var(--ink);
  color: var(--paper);
  padding: 1px 5px;
  margin-left: 4px;
}

.btn.off {
  opacity: 0.45;
  text-decoration: line-through;
}

.legend {
  font-size: 0.75rem;
  color: var(--ink-soft);
  letter-spacing: 0.04em;
}

/* ---------- overlay ---------- */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(39, 40, 77, 0.55);
  display: grid;
  place-items: center;
  z-index: 60;
  padding: 20px;
}

.overlay[hidden] {
  display: none;
}

.card {
  background: var(--paper);
  border: 5px solid var(--ink);
  box-shadow: 14px 14px 0 var(--red);
  padding: 34px 38px 30px;
  max-width: 470px;
  width: 100%;
  text-align: center;
  transform: rotate(-1.2deg);
  animation: card-in 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
  position: relative;
}

@keyframes card-in {
  from { transform: rotate(-1.2deg) scale(0.6) translateY(40px); opacity: 0; }
}

.card-kicker {
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.28em;
  color: var(--teal);
  margin-bottom: 6px;
}

.card-title {
  font-family: 'Shrikhand', cursive;
  font-size: clamp(1.8rem, 6vw, 2.6rem);
  line-height: 1;
  color: var(--red);
  text-shadow: 3px 3px 0 var(--ink);
  margin-bottom: 14px;
}

.card.good .card-title {
  color: var(--teal);
}

.card-body {
  font-size: 0.88rem;
  line-height: 1.55;
  color: var(--ink-soft);
  margin-bottom: 22px;
}

.card-body ul {
  text-align: left;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 9px;
}

.card-body li {
  padding-left: 1.4em;
  position: relative;
}

.card-body li::before {
  content: '▸';
  position: absolute;
  left: 0;
  color: var(--red);
  font-weight: 700;
}

.card-body b {
  color: var(--ink);
}

.card-buttons {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
}

.btn.primary {
  background: var(--red);
  color: var(--paper);
  box-shadow: 4px 4px 0 var(--ink);
}

.btn.primary:hover {
  box-shadow: 5px 5px 0 var(--ink);
}

/* ---------- confetti ---------- */

.confetti-piece {
  position: fixed;
  top: -20px;
  width: 10px;
  height: 16px;
  z-index: 55;
  animation: confetti-fall var(--t) linear var(--d) forwards;
  pointer-events: none;
}

@keyframes confetti-fall {
  to {
    transform: translateY(110vh) rotate(720deg);
  }
}

/* ---------- responsive ---------- */

@media (max-width: 640px) {
  .stage {
    padding: 14px 10px 28px;
  }

  .masthead {
    flex-direction: column;
    align-items: center;
    text-align: center;
  }

  .board-wrap {
    padding: 8px;
    box-shadow: 7px 7px 0 var(--red);
  }

  .legend {
    display: none;
  }
}
