/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: "Comic Sans MS", "Chalkboard SE", "Marker Felt", system-ui, sans-serif;
  background: #fff7e6;
  color: #333;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  touch-action: none;
}
#app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
}
#confetti-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 999;
}

/* ---------- Top bar (shared across modes) ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  background: #fff;
  border-bottom: 4px solid #ffd166;
  flex: 0 0 auto;
  z-index: 5;
}
.topbar-left, .topbar-right { display: flex; gap: 12px; }
.btn {
  font: inherit;
  font-size: 22px;
  font-weight: bold;
  padding: 14px 22px;
  min-height: 60px;
  min-width: 60px;
  border-radius: 16px;
  border: 4px solid #333;
  background: #fff;
  color: #333;
  cursor: pointer;
  box-shadow: 0 4px 0 #333;
  transition: transform 0.08s ease, box-shadow 0.08s ease;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #333; }
.btn-primary { background: #ff7a59; color: #fff; }
.btn-zoo     { background: #ffd166; }
.btn-newgame { background: #06d6a0; color: #fff; }

/* ---------- Home screen ---------- */
.home {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 30px;
  padding: 24px;
}
.home h1 {
  font-size: 60px;
  margin: 0;
  color: #ff7a59;
  text-shadow: 3px 3px 0 #ffd166;
}
.home-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 24px;
  width: min(700px, 92vw);
}
.home-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 28px 16px;
  border-radius: 24px;
  border: 5px solid #333;
  background: #fff;
  box-shadow: 0 6px 0 #333;
  cursor: pointer;
  transition: transform 0.08s ease, box-shadow 0.08s ease;
  min-height: 160px;
}
.home-card:active { transform: translateY(4px); box-shadow: 0 2px 0 #333; }
.home-card .emoji { font-size: 64px; line-height: 1; }
.home-card .label { font-size: 26px; font-weight: bold; }
.card-flip   { background: #ffe5d9; }
.card-build  { background: #d9f0ff; }
.card-balance{ background: #e5ffd9; }
.card-zoo    { background: #fff3b0; }

/* ---------- Game screen ---------- */
.game {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  padding: 16px;
  overflow: hidden;
}

/* ---------- Number tiles ---------- */
.num-tile {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 110px;
  height: 110px;
  border-radius: 22px;
  font-size: 70px;
  font-weight: bold;
  border: 5px solid #333;
  background: #fff;
  box-shadow: 0 5px 0 #333;
  color: #fff;
  text-shadow: 2px 2px 0 rgba(0,0,0,0.2);
  flex: 0 0 auto;
}
.num-tile.draggable { cursor: grab; touch-action: none; }
.num-tile.dragging  { cursor: grabbing; z-index: 50; }
.op-tile {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 70px;
  height: 110px;
  font-size: 60px;
  font-weight: bold;
  color: #555;
  flex: 0 0 auto;
}
/* Equals sign in the theme orange so the symmetry "a+b = b+a" pops.
   Plain "+" stays grey. */
.op-tile.op-eq { color: #ff7a59; }

/* Per-digit colors so each number is a distinct visual */
.num-0  { background: #999; }
.num-1  { background: #ef476f; }
.num-2  { background: #f78c6b; }
.num-3  { background: #ffd166; color: #333; text-shadow: none; }
.num-4  { background: #06d6a0; }
.num-5  { background: #118ab2; }
.num-6  { background: #073b4c; }
.num-7  { background: #9b5de5; }
.num-8  { background: #00bbf9; }
.num-9  { background: #00f5d4; color: #333; text-shadow: none; }
/* Two-digit results / distractors (pool tiles, result slot) */
.num-10 { background: #ff9f1c; }
.num-11 { background: #22577a; }
.num-12 { background: #e76f51; }
.num-13 { background: #b298dc; color: #333; text-shadow: none; }
.num-14 { background: #2a9d8f; }
.num-15 { background: #ff6b6b; }
.num-16 { background: #4a4e69; }
.num-17 { background: #ffafcc; color: #333; text-shadow: none; }
.num-18 { background: #1d3557; }

/* Abstract level — strip the colored chrome so the numeral stands on its
   own. Used by Flip It's Level 2 to push the child from the colour cue
   to the symbol itself. Wins over the per-digit .num-N rules via !important. */
.num-tile.abstract {
  background: transparent !important;
  border: none !important;
  box-shadow: none !important;
  color: #333 !important;
  text-shadow: none !important;
  width: auto;
  min-width: 80px;
}
.slot.abstract {
  border: 3px dashed #ccc;
  background: transparent;
}

/* Level picker chips, pinned to the top of the game area so they don't
   crowd the vertically-centered math content. The mode's .game wrapper
   sets position: relative, so absolute here anchors to that. */
.level-picker {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
  z-index: 10;
}
.level-chip {
  font: inherit;
  font-size: 18px;
  font-weight: bold;
  padding: 8px 18px;
  border-radius: 999px;
  border: 3px solid #333;
  background: #fff;
  color: #333;
  cursor: pointer;
  box-shadow: 0 3px 0 #333;
  transition: transform 0.08s ease, box-shadow 0.08s ease;
}
.level-chip:active { transform: translateY(2px); box-shadow: 0 1px 0 #333; }
.level-chip.active {
  background: #ff7a59;
  color: #fff;
}

/* ---------- Equation rows ---------- */
.equation-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  flex-wrap: nowrap;
}
.equation-half {
  display: flex;
  align-items: center;
  gap: 14px;
}

/* ---------- Dots under number tiles ---------- */
.dots-row {
  display: flex;
  gap: 6px;
  justify-content: center;
  margin-top: 10px;
  min-height: 26px;
  flex-wrap: wrap;
  max-width: 240px;
}
.dot {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid #333;
}

/* ---------- Build It: drop slots & pool ---------- */
.slots-row {
  display: flex;
  gap: 14px;
  margin-left: 14px;
}
.slot {
  width: 110px;
  height: 110px;
  border: 5px dashed #aaa;
  border-radius: 22px;
  background: rgba(255,255,255,0.6);
}
.slot.op-slot { width: 70px; }
.slot.filled { border-style: solid; border-color: #333; background: transparent; }
.slot.hover  { border-color: #06d6a0; background: rgba(6,214,160,0.15); }

.tile-pool {
  margin-top: 40px;
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  justify-content: center;
  padding: 20px;
  background: rgba(0,0,0,0.04);
  border-radius: 20px;
}

/* ---------- Balance It ---------- */
.balance-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  height: 100%;
}
.scale {
  position: relative;
  width: min(700px, 90vw);
  height: 280px;
}
.scale-pivot {
  position: absolute;
  left: 50%;
  bottom: 20px;
  width: 20px;
  height: 200px;
  background: #333;
  transform: translateX(-50%);
  border-radius: 6px;
}
.scale-beam {
  position: absolute;
  left: 50%;
  top: 60px;
  width: 100%;
  height: 14px;
  background: #333;
  border-radius: 8px;
  transform-origin: 50% 50%;
  transform: translateX(-50%) rotate(0deg);
  transition: transform 0.5s ease;
}
.scale-beam.tilt-left  { transform: translateX(-50%) rotate(-10deg); }
.scale-beam.tilt-right { transform: translateX(-50%) rotate(10deg); }
.tray {
  position: absolute;
  top: 80px;
  width: 220px;
  min-height: 140px;
  border: 5px solid #333;
  border-top: none;
  border-radius: 0 0 24px 24px;
  background: rgba(255,255,255,0.85);
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 10px;
  align-content: flex-start;
  justify-content: center;
  transition: top 0.5s ease;
}
.tray.left  { left: calc(50% - 230px); }
.tray.right { right: calc(50% - 230px); }
.tray-count {
  position: absolute;
  top: -50px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 36px;
  font-weight: bold;
  background: #fff;
  border: 4px solid #333;
  border-radius: 50%;
  width: 64px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 3px 0 #333;
}
.block {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: 3px solid #333;
  flex: 0 0 auto;
  box-shadow: 0 2px 0 #333;
}
.block.red  { background: #ef476f; }
.block.blue { background: #118ab2; }
.block.draggable { cursor: grab; touch-action: none; }
.block-pool {
  margin-top: 20px;
  padding: 16px;
  background: rgba(0,0,0,0.04);
  border-radius: 18px;
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: center;
  max-width: 90vw;
}

/* ---------- Try-again chip ---------- */
.try-again-chip {
  position: absolute;
  top: 30%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.5);
  background: #ffd166;
  border: 4px solid #333;
  border-radius: 30px;
  padding: 14px 26px;
  font-size: 28px;
  font-weight: bold;
  box-shadow: 0 4px 0 #333;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.3s cubic-bezier(.34,1.56,.64,1);
  z-index: 30;
}
.try-again-chip.show {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}

/* ---------- Animal pop reward ---------- */
.animal-pop {
  position: fixed;
  font-size: 140px;
  z-index: 100;
  pointer-events: none;
  transition: transform 0.9s cubic-bezier(.34,1.56,.64,1), opacity 0.9s ease;
}

/* ---------- Zoo screen ---------- */
.zoo {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 20px;
  overflow: hidden;
}
.zoo h2 {
  margin: 0 0 16px;
  font-size: 40px;
  color: #ff7a59;
  text-shadow: 2px 2px 0 #ffd166;
  text-align: center;
}
.zoo-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 14px;
  overflow-y: auto;
  padding: 10px;
  align-content: start;
}
.zoo-cell {
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  /* Emoji nearly fills the square — scales with cell size. */
  font-size: clamp(56px, 13vmin, 120px);
  line-height: 1;
  overflow: hidden;
  cursor: grab;
  touch-action: none;
  transition: transform 0.12s ease, filter 0.12s ease;
}
.zoo-cell:active { transform: scale(0.92); }
.zoo-cell.dragging { cursor: grabbing; }
/* Drop-target highlight: glow + slight lift, no background box */
.zoo-cell.hover {
  filter: drop-shadow(0 0 12px #06d6a0);
  transform: scale(1.1);
}
.zoo-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  font-weight: bold;
  color: #888;
  text-align: center;
  padding: 24px;
}
.zoo-count {
  text-align: center;
  font-size: 22px;
  margin-top: 12px;
  font-weight: bold;
  color: #555;
}

/* ---------- Drag transform helper ----------
   While being dragged, an element gets its inline `transform` rewritten on
   every pointermove. Any CSS animation or transition on the same element
   would clobber that on the next animation frame, so kill both here. */
.dragging {
  transform-origin: center;
  transition: none !important;
  animation: none !important;
  z-index: 100;
}

/* ---------- Mode title strip ---------- */
.mode-title {
  font-size: 28px;
  font-weight: bold;
  text-align: center;
  margin: 0 0 16px;
  color: #555;
}

/* ---------- Wiggle / attention animation ---------- */
@keyframes wiggle {
  0%, 100% { transform: rotate(0deg); }
  25%      { transform: rotate(-8deg); }
  75%      { transform: rotate(8deg); }
}

/* ---------- Home card for Stack It ---------- */
.card-stack { background: #d9e8ff; }

/* ---------- Rectangle Stack mode ---------- */
.rect-game {
  --unit: 22px;     /* 1 number-unit = this many px of height */
  --rect-w: 70px;   /* width of every rectangle */
}
.rect-progress-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 32px;
  margin-bottom: 8px;
  flex-wrap: wrap;
  justify-content: center;
}
.rect-progress-text {
  font-size: 18px;
  font-weight: bold;
  color: #555;
}
.rect-found-chips {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.rect-found-chip {
  font-size: 16px;
  font-weight: bold;
  padding: 4px 10px;
  border-radius: 999px;
  background: #06d6a0;
  color: #fff;
  border: 2px solid #333;
}

.rect-play {
  display: flex;
  gap: 60px;
  justify-content: center;
  align-items: flex-end;
  margin: 8px 0;
}
.rect-col {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}
.rect-col-label {
  font-size: 16px;
  font-weight: bold;
  color: #777;
  text-transform: uppercase;
  letter-spacing: 1px;
}
.rect-col-body {
  display: flex;
  align-items: flex-end;
  /* Slight border around to read as a "container" */
}

/* The stacking area — same height as target, dashed outline so she sees
   the goal even when the stack is empty/short. */
.rect-stack {
  width: var(--rect-w);
  height: calc(var(--rect-h, 1) * var(--unit));
  border: 3px dashed #aaa;
  border-radius: 8px;
  display: flex;
  flex-direction: column-reverse; /* first child = bottom of the stack */
  background: rgba(255, 255, 255, 0.5);
  box-sizing: content-box;
}

/* A single rectangle — height proportional to value */
.rect {
  width: var(--rect-w);
  height: calc(var(--rect-h, 1) * var(--unit));
  border: 3px solid #333;
  border-radius: 6px;
  box-shadow: 0 2px 0 #333;
  color: #fff;
  font-weight: bold;
  font-size: 22px;
  text-shadow: 1px 1px 0 rgba(0,0,0,0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  box-sizing: border-box;
}
/* Rectangles inside a stack: borderless top so adjacent pieces look fused */
.rect-stack .rect {
  border-radius: 0;
  border-width: 2px;
  box-shadow: none;
  margin: 0;
}
.rect-stack .rect:first-child { /* bottom piece in DOM = first appended */
  border-bottom-left-radius: 6px;
  border-bottom-right-radius: 6px;
}
.rect-stack .rect:last-child {  /* top of the stack visually */
  border-top-left-radius: 6px;
  border-top-right-radius: 6px;
}

/* Pool — horizontal row of pieces, aligned at the bottom so heights read */
.rect-pool {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  justify-content: center;
  padding: 14px 20px;
  background: rgba(0,0,0,0.04);
  border-radius: 16px;
  margin-top: 12px;
  flex-wrap: wrap;
  max-width: 95vw;
}
.rect.pool-piece  { cursor: grab; touch-action: none; }
.rect.placed-piece { cursor: grab; touch-action: none; }
.rect-pool .rect { box-shadow: 0 2px 0 #333; }

/* Evaluate button */
.rect-evaluate {
  margin-top: 14px;
  background: #ff7a59;
  color: #fff;
  font-size: 22px;
  padding: 12px 28px;
}
.rect-evaluate:disabled {
  background: #ccc;
  color: #888;
  cursor: not-allowed;
  box-shadow: 0 4px 0 #999;
  border-color: #999;
}
