*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --cream: #FFF8F0;
  --warm-white: #FFF3E0;
  --pizza-crust: #F4A83D;
  --pizza-cheese: #FFD54F;
  --pizza-sauce: #E53935;
  --pepperoni: #C62828;
  --knife-blade: #B0BEC5;
  --knife-handle: #6D4C41;
  --mouse-body: #9E9E9E;
  --mouse-ear: #F8BBD0;
  --mouse-nose: #F48FB1;
  --green-accent: #A5D6A7;
  --text-dark: #4E342E;
  --shadow: rgba(0, 0, 0, 0.1);
}

html, body {
  height: 100%;
  overflow: hidden;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

body {
  font-family: 'Nunito', 'Comic Sans MS', cursive, sans-serif;
  background: var(--cream);
  background-image:
    linear-gradient(45deg, rgba(244, 168, 61, 0.05) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(244, 168, 61, 0.05) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, rgba(244, 168, 61, 0.05) 75%),
    linear-gradient(-45deg, transparent 75%, rgba(244, 168, 61, 0.05) 75%);
  background-size: 40px 40px;
  background-position: 0 0, 0 20px, 20px -20px, -20px 0;
}

#game {
  position: relative;
}

/* Header */
#header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
  background: var(--warm-white);
  border-bottom: 3px solid var(--pizza-crust);
  font-size: 1.3rem;
  font-weight: 900;
  color: var(--text-dark);
  z-index: 10;
}

/* Page navigation */
#app {
  height: 100vh;
  height: 100dvh;
  max-width: 600px;
  margin: 0 auto;
  position: relative;
}

.page {
  display: none;
  flex-direction: column;
  height: 100%;
}

.page.active {
  display: flex;
}

/* Header info bar */
#header-info {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 1rem;
}

/* Mini pizza icon in header (square) */
.header-pizza-icon {
  width: 20px;
  height: 20px;
  background: var(--pizza-cheese);
  border: 2px solid var(--pizza-crust);
  border-radius: 4px;
  position: relative;
}

.header-pizza-icon::after {
  content: '';
  position: absolute;
  inset: 3px;
  background:
    radial-gradient(circle at 30% 35%, var(--pepperoni) 2px, transparent 2px),
    radial-gradient(circle at 70% 65%, var(--pepperoni) 2px, transparent 2px);
}

/* Mini mouse icon in header */
.header-mouse-icon {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-left: 8px;
}

.hm-ears {
  display: flex;
  gap: 8px;
  margin-bottom: -3px;
  z-index: 1;
}

.hm-ear {
  width: 7px;
  height: 7px;
  background: var(--mouse-body);
  border-radius: 50%;
}

.hm-body {
  width: 16px;
  height: 13px;
  background: var(--mouse-body);
  border-radius: 50% 50% 45% 45%;
}

/* Header right group */
#header-right {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* Lives display */
#lives-display {
  color: #E53935;
  font-size: 1.1rem;
  letter-spacing: 2px;
  margin-right: 4px;
}

/* Give up button */
#giveup-btn {
  background: none;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 8px;
  color: var(--text-dark);
  opacity: 0.4;
  transition: opacity 0.2s;
}

#giveup-btn:hover, #giveup-btn:active {
  opacity: 1;
  color: var(--pizza-sauce);
}

/* Settings button in header */
#settings-btn {
  background: none;
  border: none;
  font-size: 1.4rem;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 8px;
  color: var(--text-dark);
  opacity: 0.5;
  transition: opacity 0.2s;
}

#settings-btn:hover, #settings-btn:active {
  opacity: 1;
}

/* Settings page */
.settings-header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 20px;
  background: var(--warm-white);
  border-bottom: 3px solid var(--pizza-crust);
  font-size: 1.3rem;
  font-weight: 900;
  color: var(--text-dark);
}

#settings-back {
  background: none;
  border: none;
  font-family: inherit;
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text-dark);
  cursor: pointer;
  padding: 4px 8px;
}

.settings-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 24px;
  padding: 32px;
}

.settings-item {
  font-family: inherit;
  font-size: 1.1rem;
  font-weight: 700;
  padding: 14px 32px;
  border: 3px solid var(--pizza-crust);
  border-radius: 12px;
  background: var(--warm-white);
  color: var(--text-dark);
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  min-width: 200px;
  text-align: center;
}

.settings-item:hover, .settings-item:active {
  background: var(--pizza-cheese);
  transform: scale(1.02);
}

.settings-danger {
  border-color: var(--pizza-sauce);
  color: var(--pizza-sauce);
}

.settings-danger:hover, .settings-danger:active {
  background: #FFEBEE;
}

.settings-version {
  font-size: 0.9rem;
  color: #aaa;
  margin-top: 16px;
}

/* Play area */
#play-area {
  flex: 1;
  display: flex;
  min-height: 0;
  position: relative;
}

#knife-zone {
  width: 15%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(90deg, rgba(244, 168, 61, 0.08), transparent);
  border-right: none;
  position: relative;
}

#knife {
  display: flex;
  flex-direction: column;
  align-items: center;
  transition: transform 0.15s ease;
}

#knife.swiping {
  transform: translateX(20px);
}

.knife-blade {
  width: 8px;
  height: 80px;
  background: linear-gradient(180deg, #CFD8DC, var(--knife-blade), #90A4AE);
  border-radius: 3px 3px 1px 1px;
  box-shadow: 1px 0 3px var(--shadow);
}

.knife-handle {
  width: 16px;
  height: 32px;
  background: linear-gradient(90deg, #5D4037, var(--knife-handle), #5D4037);
  border-radius: 3px;
  margin-top: -2px;
}

#pizza-area {
  width: 85%;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 16px;
  position: relative;
  z-index: 1;
}

/* Pizza */
.pizza {
  width: 120px;
  height: 120px;
  background: var(--pizza-cheese);
  border: 4px solid var(--pizza-crust);
  border-radius: 12px;
  position: relative;
  box-shadow: 0 4px 8px var(--shadow);
  transition: transform 0.3s ease, opacity 0.3s ease;
  overflow: hidden;
}

.pizza::before {
  content: '';
  position: absolute;
  inset: 8px;
  background:
    radial-gradient(circle at 25% 30%, var(--pepperoni) 8px, transparent 8px),
    radial-gradient(circle at 65% 20%, var(--pepperoni) 6px, transparent 6px),
    radial-gradient(circle at 45% 60%, var(--pepperoni) 7px, transparent 7px),
    radial-gradient(circle at 75% 70%, var(--pepperoni) 6px, transparent 6px),
    radial-gradient(circle at 20% 75%, var(--pepperoni) 5px, transparent 5px),
    radial-gradient(circle at 55% 85%, var(--pepperoni) 7px, transparent 7px);
}

.pizza::after {
  content: '';
  position: absolute;
  inset: 4px;
  background:
    radial-gradient(circle at 35% 45%, var(--pizza-sauce) 4px, transparent 4px),
    radial-gradient(circle at 80% 40%, var(--pizza-sauce) 3px, transparent 3px),
    radial-gradient(circle at 15% 55%, var(--pizza-sauce) 3px, transparent 3px),
    radial-gradient(circle at 60% 30%, var(--pizza-sauce) 4px, transparent 4px);
  opacity: 0.5;
}

/* Slices container (replaces a pizza when cut) */
.slice-group {
  display: flex;
  flex-direction: column;
  gap: 3px;
  width: 120px;
}

.slice {
  width: 120px;
  height: 9px;
  background: var(--pizza-cheese);
  border: 2px solid var(--pizza-crust);
  border-radius: 4px;
  position: relative;
  box-shadow: 0 1px 3px var(--shadow);
  transition: transform 0.4s ease, opacity 0.4s ease;
}

.slice::before {
  content: '';
  position: absolute;
  top: 1px;
  left: 10%;
  right: 10%;
  height: 3px;
  background: var(--pepperoni);
  border-radius: 2px;
  opacity: 0.7;
}

/* Bits (1/100 pizza — tiny squares) */
.bit-group {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  width: 120px;
  justify-content: center;
}

.bit {
  width: 10px;
  height: 10px;
  background: var(--pizza-cheese);
  border: 1px solid var(--pizza-crust);
  border-radius: 2px;
  box-shadow: 0 1px 2px var(--shadow);
  transition: transform 0.4s ease, opacity 0.4s ease;
}

.bit.distributing {
  animation: fly-to-mouse 0.5s ease-in forwards;
  z-index: 20;
}

/* Crumbs (1/1000 pizza — tiny dots) */
.crumb-group {
  display: flex;
  flex-wrap: wrap;
  gap: 1px;
  width: 120px;
  justify-content: center;
}

.crumb {
  width: 5px;
  height: 5px;
  background: var(--pizza-cheese);
  border: 1px solid var(--pizza-crust);
  border-radius: 50%;
  box-shadow: 0 1px 1px var(--shadow);
  transition: transform 0.4s ease, opacity 0.4s ease;
}

.crumb.distributing {
  animation: fly-to-mouse 0.5s ease-in forwards;
  z-index: 20;
}

/* Specks (1/10000 pizza — tiny dots) */
.speck-group {
  display: flex;
  flex-wrap: wrap;
  gap: 1px;
  width: 120px;
  justify-content: center;
}

.speck {
  width: 3px;
  height: 3px;
  background: var(--pizza-cheese);
  border: 0.5px solid var(--pizza-crust);
  border-radius: 50%;
  transition: transform 0.4s ease, opacity 0.4s ease;
}

.speck.distributing {
  animation: fly-to-mouse 0.5s ease-in forwards;
  z-index: 20;
}

/* Cut animation */
@keyframes slice-spread {
  0% { gap: 0; opacity: 0.7; }
  100% { gap: 3px; opacity: 1; }
}

.slice-group.cutting,
.bit-group.cutting,
.crumb-group.cutting,
.speck-group.cutting {
  animation: slice-spread 0.5s ease-out forwards;
}

/* Knife sweep animation */
@keyframes knife-sweep {
  0% { transform: translateX(0); }
  40% { transform: translateX(300px); }
  100% { transform: translateX(0); }
}

#knife.cutting {
  animation: knife-sweep 0.6s ease-in-out;
}

/* Shing effect */
@keyframes shing {
  0% { opacity: 1; transform: scaleX(0); }
  50% { opacity: 1; transform: scaleX(1); }
  100% { opacity: 0; transform: scaleX(1); }
}

.shing-effect {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.9), transparent);
  pointer-events: none;
  animation: shing 0.4s ease-out forwards;
  z-index: 5;
}

/* Distribute animation */
@keyframes fly-to-mouse {
  0% { transform: translate(0, 0) scale(1); opacity: 1; }
  70% { transform: translate(calc(var(--fly-x) * 0.7), calc(var(--fly-y) * 0.7)) scale(1); opacity: 1; }
  100% { transform: translate(var(--fly-x), var(--fly-y)) scale(0.3); opacity: 0; }
}

.pizza.distributing, .slice.distributing {
  animation: fly-to-mouse 0.5s ease-in forwards;
  z-index: 20;
}

/* Distributed pieces — already opacity:0 from fly-to-mouse forwards fill */

/* Mouse tray */
#mouse-tray {
  min-height: 20%;
  max-height: 35%;
  display: grid;
  justify-items: center;
  align-items: center;
  justify-content: center;
  align-content: center;
  gap: 8px;
  padding: 12px 16px;
  background: linear-gradient(0deg, rgba(244, 168, 61, 0.08), transparent);
  border-top: none;
  overflow-y: auto;
}

/* Subitizing patterns per mouse count */
#mouse-tray[data-count="1"] {
  grid-template-columns: 1fr;
}

#mouse-tray[data-count="2"] {
  grid-template-columns: repeat(2, auto);
}

#mouse-tray[data-count="3"] {
  grid-template-columns: repeat(2, auto);
}

#mouse-tray[data-count="4"] {
  grid-template-columns: repeat(2, auto);
}

#mouse-tray[data-count="5"] {
  grid-template-columns: repeat(3, auto);
}

#mouse-tray[data-count="6"] {
  grid-template-columns: repeat(3, auto);
}

#mouse-tray[data-count="7"] {
  grid-template-columns: repeat(3, auto);
}

#mouse-tray[data-count="8"] {
  grid-template-columns: repeat(4, auto);
}

#mouse-tray[data-count="9"] {
  grid-template-columns: repeat(3, auto);
}

#mouse-tray[data-count="10"] {
  grid-template-columns: repeat(4, auto);
}

/* Queued distribute — subtle mouse nudge */
@keyframes mouse-nudge {
  0% { transform: scale(1); }
  40% { transform: scale(1.05); }
  100% { transform: scale(1); }
}

.mouse.nudge {
  animation: mouse-nudge 0.2s ease-out;
}

/* Mouse */
.mouse {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 44px;
  transition: transform 0.3s ease;
}

.mouse-ears {
  display: flex;
  gap: 16px;
  margin-bottom: -6px;
  z-index: 1;
}

.mouse-ear {
  width: 14px;
  height: 14px;
  background: var(--mouse-body);
  border-radius: 50%;
  position: relative;
}

.mouse-ear::after {
  content: '';
  position: absolute;
  inset: 3px;
  background: var(--mouse-ear);
  border-radius: 50%;
}

.mouse-body {
  width: 36px;
  height: 30px;
  background: var(--mouse-body);
  border-radius: 50% 50% 45% 45%;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Eyes */
.mouse-eyes {
  display: flex;
  gap: 8px;
  position: absolute;
  top: 6px;
}

.mouse-eye {
  width: 5px;
  height: 5px;
  background: #212121;
  border-radius: 50%;
}

/* Nose */
.mouse-nose {
  width: 6px;
  height: 5px;
  background: var(--mouse-nose);
  border-radius: 50%;
  position: absolute;
  top: 14px;
}

/* Whiskers */
.mouse-whiskers {
  position: absolute;
  top: 15px;
  width: 100%;
}

.mouse-whisker {
  position: absolute;
  width: 12px;
  height: 1px;
  background: #757575;
}

.mouse-whisker.left-top { left: -4px; top: -3px; transform: rotate(-15deg); }
.mouse-whisker.left-bottom { left: -4px; top: 1px; transform: rotate(15deg); }
.mouse-whisker.right-top { right: -4px; top: -3px; transform: rotate(15deg); }
.mouse-whisker.right-bottom { right: -4px; top: 1px; transform: rotate(-15deg); }

/* Fed mouse */
.mouse.fed .mouse-body {
  background: #BDBDBD;
}

.mouse.fed .mouse-eye {
  width: 7px;
  height: 3px;
  border-radius: 0 0 50% 50%;
  background: #212121;
}

.mouse.fed::after {
  content: '😋';
  font-size: 10px;
  margin-top: -2px;
}

/* Eating animation */
@keyframes nom-nom {
  0%, 100% { transform: scale(1); }
  25% { transform: scale(1.15) rotate(-3deg); }
  50% { transform: scale(1.05); }
  75% { transform: scale(1.15) rotate(3deg); }
}

.mouse.eating {
  animation: nom-nom 0.4s ease;
}

/* Crying mouse */
@keyframes cry-shake {
  0%, 100% { transform: translateY(0); }
  20% { transform: translateY(-3px) rotate(-2deg); }
  40% { transform: translateY(0) rotate(2deg); }
  60% { transform: translateY(-2px) rotate(-1deg); }
  80% { transform: translateY(0) rotate(1deg); }
}

.mouse.crying {
  animation: cry-shake 0.6s ease infinite;
}

.mouse.crying .mouse-eye {
  width: 7px;
  height: 4px;
  border-radius: 50% 50% 0 0;
  background: #212121;
  position: relative;
}

.mouse.crying::after {
  content: '😢';
  font-size: 10px;
  margin-top: -2px;
}

/* Tear drops */
@keyframes tear-fall {
  0% { opacity: 1; transform: translateY(0) scale(1); }
  100% { opacity: 0; transform: translateY(12px) scale(0.5); }
}

.mouse-tear {
  position: absolute;
  width: 4px;
  height: 5px;
  background: #64B5F6;
  border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%;
  animation: tear-fall 0.8s ease-in infinite;
  pointer-events: none;
}

.mouse-tear.left { top: 10px; left: 6px; }
.mouse-tear.right { top: 10px; right: 6px; animation-delay: 0.4s; }

/* Failure overlay */
#failure {
  position: absolute;
  inset: 0;
  background: rgba(255, 240, 240, 0.92);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 100;
  opacity: 0;
  transition: opacity 0.5s ease;
  pointer-events: none;
}

#failure.visible {
  opacity: 1;
  pointer-events: auto;
}

#failure.hidden {
  display: none;
}

.failure-text {
  font-size: 2.5rem;
  font-weight: 900;
  color: var(--pizza-sauce);
  text-shadow: 2px 2px 0 rgba(0,0,0,0.08);
  animation: bounce-in 0.6s ease;
}

.failure-sub {
  font-size: 1.1rem;
  color: var(--text-dark);
  margin-top: 12px;
  opacity: 0.8;
}

.failure-tap {
  font-size: 1rem;
  color: var(--text-dark);
  margin-top: 24px;
  opacity: 0.5;
}

/* Celebration overlay */
#celebration {
  position: absolute;
  inset: 0;
  background: rgba(255, 248, 240, 0.92);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 100;
  opacity: 0;
  transition: opacity 0.5s ease;
  pointer-events: none;
}

#celebration.visible {
  opacity: 1;
  pointer-events: auto;
}

#celebration.hidden {
  display: none;
}

.celebration-text {
  font-size: 2.5rem;
  font-weight: 900;
  color: var(--pizza-crust);
  text-shadow: 2px 2px 0 rgba(0,0,0,0.08);
  animation: bounce-in 0.6s ease;
}

.celebration-sub {
  font-size: 1.1rem;
  color: var(--text-dark);
  margin-top: 16px;
  opacity: 0.7;
}

@keyframes bounce-in {
  0% { transform: scale(0.3); opacity: 0; }
  50% { transform: scale(1.1); }
  70% { transform: scale(0.95); }
  100% { transform: scale(1); opacity: 1; }
}

/* Confetti */
@keyframes confetti-fall {
  0% { transform: translateY(-20px) rotate(0deg); opacity: 1; }
  100% { transform: translateY(100vh) rotate(720deg); opacity: 0; }
}

.confetti {
  position: absolute;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  animation: confetti-fall 1.5s ease-in forwards;
  z-index: 101;
  pointer-events: none;
}

/* Swipe hint arrows */
@keyframes hint-pulse {
  0%, 100% { opacity: 0.3; transform: translateY(0); }
  50% { opacity: 0.6; transform: translateY(8px); }
}

.swipe-hint {
  position: absolute;
  font-size: 2rem;
  opacity: 0.3;
  animation: hint-pulse 2s ease-in-out infinite;
  pointer-events: none;
  color: var(--text-dark);
}

/* Game over overlay */
#game-over {
  position: absolute;
  inset: 0;
  background: rgba(255, 240, 240, 0.92);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 100;
  opacity: 0;
  transition: opacity 0.5s ease;
  pointer-events: none;
}

#game-over.visible {
  opacity: 1;
  pointer-events: auto;
}

#game-over.hidden {
  display: none;
}

/* Fraction display on celebration */
#fraction-display {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin: 20px 0 8px;
}

.fraction {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.fraction-top, .fraction-bottom {
  display: flex;
  flex-direction: row;
  gap: 4px;
  justify-content: center;
  padding: 6px 8px;
}

.fraction-line {
  width: 100%;
  height: 3px;
  background: var(--text-dark);
  border-radius: 2px;
}

.fraction-equals {
  font-size: 2rem;
  font-weight: 900;
  color: var(--text-dark);
}

.fraction-result {
  font-size: 2rem;
  font-weight: 900;
  color: var(--pizza-crust);
}

/* Mini pizza in fraction */
.mini-pizza {
  width: 22px;
  height: 22px;
  background: var(--pizza-cheese);
  border: 2px solid var(--pizza-crust);
  border-radius: 4px;
}

/* Mini mouse in fraction */
.mini-mouse-icon {
  display: flex;
  flex-direction: column;
  align-items: center;
  transform: scale(0.8);
}

.mini-mouse-icon .hm-ears {
  display: flex;
  gap: 8px;
  margin-bottom: -3px;
  z-index: 1;
}

.mini-mouse-icon .hm-ear {
  width: 7px;
  height: 7px;
  background: var(--mouse-body);
  border-radius: 50%;
}

.mini-mouse-icon .hm-body {
  width: 16px;
  height: 13px;
  background: var(--mouse-body);
  border-radius: 50% 50% 45% 45%;
}

/* Responsive */
@media (max-width: 400px) {
  .pizza {
    width: 90px;
    height: 90px;
  }

  .slice-group {
    width: 90px;
  }

  .slice {
    width: 90px;
    height: 7px;
  }

  .mouse {
    width: 36px;
  }

  .mouse-body {
    width: 28px;
    height: 24px;
  }

  .mouse-ear {
    width: 11px;
    height: 11px;
  }

  .mouse-ears {
    gap: 10px;
  }

  #header {
    font-size: 1.1rem;
    padding: 8px 14px;
  }
}

@media (min-width: 601px) {
  .pizza {
    width: 140px;
    height: 140px;
  }

  .slice-group {
    width: 140px;
  }

  .slice {
    width: 140px;
    height: 11px;
  }
}
