:root {
  --paper-bg: #FBF3E4;
  --card-bg: #FFFDF7;
  --forest-green: #3E7C4F;
  --forest-dark: #2F6140;
  --sun-amber: #F2A33C;
  --terracotta: #B0522A;
  --caramel: #8A5A3B;
  --ink: #3D3226;
  --ink-soft: #6B5D4E;
  --valid-green: #4CAF6D;
  --invalid-red: #C63D3D;
  --muted: #EFE4D0;
  --outline-w: 3px;
  --radius-wobble: 15px 25px 20px 10px;
  --shadow-out: 4px 4px 0 rgba(61, 50, 38, .25);
  --shadow-in: inset -2px -2px 6px rgba(0, 0, 0, .12);
  --space-sm: 8px;
  --space-md: 16px;
  --space-lg: 24px;
  --space-xl: 32px;
  --space-2xl: 48px;
  --font-display: "Baloo 2", "Comic Sans MS", system-ui, sans-serif;
  --font-hand: "Kalam", "Comic Sans MS", cursive;
}

@font-face {
  font-family: "Baloo 2";
  src: url("assets/fonts/Baloo2.ttf") format("truetype");
  font-weight: 100 800;
  font-display: swap;
}

@font-face {
  font-family: "Kalam";
  src: url("assets/fonts/Kalam-Regular.ttf") format("truetype");
  font-weight: 400;
  font-display: swap;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--paper-bg);
  color: var(--ink);
  font-family: var(--font-display);
  overflow-x: hidden;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

button {
  font-family: inherit;
  font-size: 1rem;
}

button:focus-visible,
[tabindex]:focus-visible {
  outline: 3px solid var(--sun-amber);
  outline-offset: 2px;
}

.btn {
  background: var(--forest-green);
  color: #fff;
  border: var(--outline-w) solid var(--forest-dark);
  border-radius: var(--radius-wobble);
  box-shadow: var(--shadow-out), var(--shadow-in);
  font-family: var(--font-display);
  font-weight: 800;
  padding: 12px 24px;
  min-height: 44px;
  min-width: 44px;
  cursor: pointer;
  transition: transform 200ms cubic-bezier(.34, 1.56);
}

.btn:hover {
  transform: translateY(-2px) rotate(-1deg);
}

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

.btn-big {
  font-size: 1.25rem;
  padding: 16px 40px;
}

.card {
  background: var(--card-bg);
  border: var(--outline-w) solid var(--caramel);
  border-radius: var(--radius-wobble);
  box-shadow: var(--shadow-out), var(--shadow-in);
  padding: var(--space-lg);
}

#fx-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 20;
}

.screen {
  display: none;
  min-height: 100vh;
  background: var(--paper-bg, #FBF3E4);
}

.screen.active {
  display: block;
}

.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
  background: rgba(61, 50, 38, .45);
}

.overlay.hidden {
  display: none;
}

#screen-level {
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: var(--space-md);
  height: 100vh;
}

#board {
  display: grid;
  aspect-ratio: 1;
  width: min(88vw, 52vh, 520px);
  margin: 0 auto;
  grid-template-columns: repeat(var(--board-size), 1fr);
  gap: 4px;
  padding: 10px;
  background: var(--board, var(--card-bg));
  border: var(--outline-w) solid var(--caramel);
  border-radius: var(--radius-wobble);
  box-shadow: var(--shadow-out), var(--shadow-in);
}

.cell {
  position: relative;
  border-radius: 18% 30% 22% 28%;
  display: grid;
  place-items: center;
  touch-action: none;
}

.cell svg {
  width: 100%;
  height: 100%;
}

.cell.valid {
  outline: 3px solid var(--valid-green);
  background: rgba(76, 175, 109, .15);
  animation: checkPulse .3s ease;
}

.cell.valid::after {
  content: "";
  position: absolute;
  width: 45%;
  height: 45%;
  border-left: 3px solid var(--valid-green);
  border-bottom: 3px solid var(--valid-green);
  transform: rotate(-45deg) translate(10%, -20%);
}

.cell.invalid {
  outline: 3px solid var(--invalid-red);
  background: rgba(198, 61, 61, .10);
  animation: shake .25s ease;
}

.cell.invalid::after {
  content: "";
  position: absolute;
  width: 55%;
  height: 55%;
  background:
    linear-gradient(45deg, transparent 43%, var(--invalid-red) 43% 57%, transparent 57%),
    linear-gradient(-45deg, transparent 43%, var(--invalid-red) 43% 57%, transparent 57%);
}

#piece-tray {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: var(--space-lg);
  min-height: 120px;
}

.tray-piece {
  width: 96px;
  height: 96px;
  cursor: grab;
  transition: transform 200ms cubic-bezier(.34, 1.56);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.tray-piece.selected {
  outline: 3px solid var(--sun-amber);
  border-radius: var(--radius-wobble);
  background: rgba(242, 163, 60, .15);
}

.tray-piece:active {
  cursor: grabbing;
}

.ghost-piece {
  position: fixed;
  pointer-events: none;
  z-index: 50;
  transform: scale(1.05);
  filter: drop-shadow(0 6px 6px rgba(61, 50, 38, .35));
}

.ghost-grid {
  display: grid;
  width: 100%;
  height: 100%;
}

.ghost-grid .tile {
  width: 100%;
  height: 100%;
}

.tile {
  width: 100%;
  height: 100%;
}

.hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-md);
  padding: var(--space-sm) var(--space-md);
}

.btn-icon {
  width: 48px;
  height: 48px;
  background: var(--card-bg);
  border: var(--outline-w) solid var(--caramel);
  border-radius: var(--radius-wobble);
  box-shadow: var(--shadow-out), var(--shadow-in);
  cursor: pointer;
  display: grid;
  place-items: center;
  padding: 8px;
}

.obj-pips {
  display: flex;
  gap: var(--space-sm);
}

.obj-pip {
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--card-bg);
  border: 2px solid var(--caramel);
  border-radius: 999px;
  padding: 2px 10px 2px 4px;
  font-weight: 700;
}

.obj-pip .tile {
  width: 28px;
  height: 28px;
}

.world-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: var(--space-lg);
  padding: var(--space-lg);
  max-width: 720px;
  margin: 0 auto;
}

.world-card {
  background: var(--card-bg);
  border: var(--outline-w) solid var(--caramel);
  border-radius: var(--radius-wobble);
  box-shadow: var(--shadow-out), var(--shadow-in);
  padding: var(--space-lg);
  cursor: pointer;
  font-family: var(--font-display);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-sm);
}

#map-path {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: var(--space-md);
  max-width: 640px;
  margin: 0 auto;
  padding: var(--space-lg);
}

.node {
  position: relative;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: var(--board, var(--card-bg));
  border: var(--outline-w) solid var(--caramel);
  box-shadow: var(--shadow-out), var(--shadow-in);
  cursor: pointer;
  display: grid;
  place-items: center;
  font-weight: 800;
  padding: 0;
}

.node .node-food {
  width: 26px;
  height: 26px;
}

.node .node-num {
  position: absolute;
  bottom: 1px;
  right: 4px;
  font-size: .6rem;
  line-height: 1;
}

.node-stars {
  position: absolute;
  top: 1px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 1px;
}

.node-stars .tile {
  width: 9px;
  height: 9px;
}

.node-perfect {
  position: absolute;
  bottom: 1px;
  left: 3px;
  width: 12px;
  height: 12px;
  fill: var(--sun-amber);
}

.hud-level {
  font-size: 1.05rem;
  margin: 0;
  line-height: 1.2;
}

.node:nth-child(4n+1) {
  margin-left: 28px;
}

.node:nth-child(4n+3) {
  margin-right: 28px;
}

.map-path-v1 .node:nth-child(4n+2) {
  margin-left: 28px;
}

.map-path-v1 .node:nth-child(4n+4) {
  margin-right: 28px;
}

.map-path-v2 .node:nth-child(4n+2) {
  margin-right: 28px;
}

.map-path-v2 .node:nth-child(4n+4) {
  margin-left: 28px;
}

.map-decor {
  display: flex;
  justify-content: center;
  gap: var(--space-md);
  padding: var(--space-sm) 0;
}

.map-decor .tile {
  width: 36px;
  height: 36px;
  opacity: .8;
}

.world-art {
  display: flex;
  gap: 4px;
}

.world-art .tile {
  width: 28px;
  height: 28px;
}

.world-card.locked {
  opacity: .5;
  cursor: not-allowed;
  background: var(--muted);
  filter: grayscale(.4);
}

.world-card.locked .world-hint {
  font-family: var(--font-hand);
  font-size: .8rem;
  color: var(--ink-soft);
}

.world-card .world-lock-icon {
  width: 20px;
  height: 20px;
}

.world-actions {
  display: flex;
  justify-content: center;
  gap: var(--space-md);
  padding: var(--space-lg);
}

.btn .btn-icon-inline {
  width: 22px;
  height: 22px;
  vertical-align: -4px;
  margin-right: 6px;
}

.obj-pip.softened .obj-count {
  color: var(--invalid-red);
  text-decoration: underline wavy var(--invalid-red);
  font-weight: 800;
}

.motion-select {
  font-family: inherit;
  font-weight: 700;
  padding: 6px 8px;
  border: 2px solid var(--caramel);
  border-radius: var(--radius-wobble);
  background: var(--card-bg);
  color: var(--ink);
  cursor: pointer;
  min-height: 44px;
}

.node.locked {
  opacity: .45;
  cursor: not-allowed;
  background: var(--muted);
}

.node.current {
  animation: bob 1.2s ease-in-out infinite;
  outline: 3px solid var(--accent, var(--sun-amber));
}

@keyframes bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}

@keyframes checkPulse {
  0% { transform: scale(.9); }
  100% { transform: scale(1); }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

.animal-pop {
  position: absolute;
  width: 96px;
  height: 96px;
  pointer-events: none;
  z-index: 40;
}

.hand-font {
  font-family: var(--font-hand);
}

.title-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-lg);
  min-height: 100vh;
  padding: var(--space-xl);
}

.title-art {
  display: flex;
  gap: var(--space-md);
}

.title-art .tile {
  width: 72px;
  height: 72px;
}

.win-animal {
  width: 96px;
  height: 96px;
}

.logo {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(2.4rem, 8vw, 4.5rem);
  color: var(--forest-green);
  text-shadow: 3px 3px 0 var(--card-bg), 6px 6px 0 rgba(61, 50, 38, .25);
  margin: 0;
}

.tagline {
  font-size: 1.1rem;
  color: var(--ink-soft);
  margin: 0;
}

.screen-title {
  font-family: var(--font-display);
  font-weight: 800;
  text-align: center;
  padding: var(--space-lg) var(--space-md) 0;
  margin: 0;
}

.win-stars {
  display: flex;
  justify-content: center;
  gap: var(--space-sm);
}

.win-stars .tile {
  width: 56px;
  height: 56px;
}

.prompt-card,
.pause-card,
.settings-card,
.win-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-md);
  max-width: 380px;
  width: min(90vw, 380px);
}

.prompt-card h3,
.pause-card h3,
.settings-card h3,
.win-card h3 {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 800;
}

.settings-card {
  align-items: flex-start;
}

.toggle,
.vol {
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  cursor: pointer;
}

.toggle input {
  width: 22px;
  height: 22px;
  accent-color: var(--forest-green);
}

.help-card {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
  max-width: 420px;
  width: min(90vw, 420px);
  max-height: 80vh;
  overflow-y: auto;
}

.help-card h3 {
  margin: 0;
}

.help-card h4 {
  margin: var(--space-sm) 0 4px;
  color: var(--forest-dark);
}

.help-card p {
  margin: 0;
  color: var(--ink-soft);
}

#powerup-shelf {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: var(--space-sm);
  min-height: 72px;
  padding: 0 var(--space-md);
}

.powerup-btn {
  width: 64px;
  height: 64px;
  background: var(--card-bg);
  border: var(--outline-w) solid var(--caramel);
  border-radius: var(--radius-wobble);
  box-shadow: var(--shadow-out), var(--shadow-in);
  cursor: pointer;
  position: relative;
  padding: 8px;
  transition: transform 150ms cubic-bezier(.34, 1.56);
}

.powerup-btn.armed {
  outline: 3px solid var(--accent, var(--sun-amber));
  transform: translateY(-4px);
}

.powerup-count {
  position: absolute;
  top: -6px;
  right: -6px;
  background: var(--sun-amber);
  border: 2px solid var(--ink);
  border-radius: 999px;
  font-weight: 800;
  font-size: .8rem;
  min-width: 22px;
  height: 22px;
  display: grid;
  place-items: center;
  padding: 0 4px;
}

.banner {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  background: var(--card-bg);
  border: var(--outline-w) solid var(--caramel);
  border-radius: var(--radius-wobble);
  box-shadow: var(--shadow-out), var(--shadow-in);
  padding: var(--space-sm) var(--space-md);
  z-index: 45;
}

.banner .tile {
  width: 44px;
  height: 44px;
}

.banner p {
  margin: 0;
}

.food-pick-grid {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-sm);
  justify-content: center;
}

.food-pick {
  width: 56px;
  height: 56px;
  background: var(--card-bg);
  border: 2px solid var(--caramel);
  border-radius: 18px;
  cursor: pointer;
  padding: 6px;
}

.ach-toast {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  background: var(--card-bg);
  border: var(--outline-w) solid var(--sun-amber);
  border-radius: var(--radius-wobble);
  box-shadow: var(--shadow-out), var(--shadow-in);
  padding: var(--space-sm) var(--space-md);
  z-index: 60;
}

.ach-toast .tile {
  width: 40px;
  height: 40px;
}

.trophy-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  max-height: 55vh;
  overflow-y: auto;
}

.trophy-row {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  background: var(--card-bg);
  border: 2px solid var(--caramel);
  border-radius: var(--radius-wobble);
  padding: var(--space-sm);
  font-weight: 700;
}

.trophy-row .trophy-icon {
  width: 32px;
  height: 32px;
}

.trophy-row.locked {
  opacity: .45;
}

.vol input {
  flex: 1;
  accent-color: var(--sun-amber);
}

#screen-level .animal-pop {
  position: absolute;
}

.star-lit {
  fill: var(--sun-amber);
  filter: drop-shadow(0 0 6px rgba(242, 163, 60, .8));
}

.bubble {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: var(--space-md);
  background: var(--card-bg);
  border: var(--outline-w) solid var(--caramel);
  border-radius: var(--radius-wobble);
  box-shadow: var(--shadow-out), var(--shadow-in);
  padding: var(--space-md) var(--space-lg);
  z-index: 45;
  max-width: min(92vw, 560px);
}

.bubble .bubble-animal {
  width: 64px;
  height: 64px;
  flex: none;
}

.bubble p {
  margin: 0;
  font-size: 1.05rem;
  color: var(--ink);
}

.bubble .btn {
  flex: none;
}

.btn-link {
  background: none;
  border: none;
  color: var(--terracotta);
  font-family: var(--font-hand);
  font-size: .95rem;
  text-decoration: underline;
  cursor: pointer;
  min-height: 44px;
  min-width: 44px;
}

.cell.hint {
  outline: 3px solid var(--sun-amber);
  background: rgba(242, 163, 60, .25);
  animation: hintPulse 1s ease-in-out infinite;
}

.tray-piece.tray-dim {
  opacity: .35;
  cursor: not-allowed;
}

@keyframes hintPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.08); }
}

@media (max-width: 480px) {
  .bubble {
    flex-direction: column;
    text-align: center;
    bottom: 8px;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation: none !important;
    transition: none !important;
  }
}

@media (max-width: 480px) {
  #piece-tray {
    gap: var(--space-md);
  }
  .tray-piece {
    width: 72px;
    height: 72px;
  }
  .hud-world {
    display: none;
  }
  .obj-pip {
    font-size: .8rem;
  }
  #screen-level {
    gap: var(--space-sm);
  }
}

@media (orientation: landscape) and (max-height: 500px) {
  #screen-level {
    grid-template-rows: auto 1fr;
    grid-template-columns: 1fr 1fr;
  }
  #board {
    width: min(60vh, 60vw);
  }
}
