:root {
  color-scheme: light;
  --paper: #edf3e6;
  --ink: #1f241d;
  --muted: #5b6554;
  --line: #c2ccb8;
  --fragile-glass: #d4f0eb;
  --goal: #6fa33f;
  --goal-deep: #3f6825;
  --block-skin: #8b592c;
  --block-flesh: #85c84b;
  --control: #24301f;
  --control-text: #fffaf2;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
  margin: 0;
}

body {
  overflow: hidden;
  background: var(--paper);
  color: var(--ink);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button {
  border: 0;
  border-radius: 8px;
  font: inherit;
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  width: 100vw;
  height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  min-height: 86px;
  padding: 18px clamp(18px, 4vw, 44px);
  border-bottom: 1px solid var(--line);
  background: rgba(237, 243, 230, 0.94);
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.mark {
  width: 38px;
  height: 38px;
  border-radius: 40% 55% 48% 52%;
  background:
    radial-gradient(circle at 50% 50%, var(--block-flesh) 0 38%, transparent 39%),
    radial-gradient(circle at 42% 45%, #1d2419 0 4%, transparent 5%),
    radial-gradient(circle at 58% 55%, #1d2419 0 4%, transparent 5%),
    var(--block-skin);
  box-shadow: 0 4px 0 #24301f;
}

h1,
p {
  margin: 0;
}

h1 {
  font-size: clamp(1.45rem, 2.4vw, 2.1rem);
  line-height: 1;
  letter-spacing: 0;
}

.brand p {
  margin-top: 8px;
  color: var(--muted);
  font-size: 0.95rem;
}

.stats {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  color: var(--muted);
  font-size: 0.95rem;
}

.stats span {
  min-width: 78px;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  text-align: center;
  white-space: nowrap;
}

.stats strong {
  color: var(--ink);
}

.play-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 260px;
  min-height: 0;
}

.stage {
  position: relative;
  min-width: 0;
  min-height: 0;
  background:
    linear-gradient(90deg, rgba(31, 36, 29, 0.05) 1px, transparent 1px),
    linear-gradient(0deg, rgba(31, 36, 29, 0.05) 1px, transparent 1px),
    #dfe8d5;
  background-size: 32px 32px;
}

#gameCanvas {
  display: block;
  width: 100%;
  height: 100%;
}

.panel {
  display: grid;
  grid-template-rows: auto 160px auto;
  align-content: start;
  gap: 18px;
  padding: 24px;
  border-left: 1px solid var(--line);
  background: #ffffff;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.level-grid button {
  height: 38px;
  border: 1px solid var(--line);
  background: #f4f7ef;
  color: var(--ink);
  font-weight: 700;
}

.level-grid button[aria-current="true"] {
  border-color: var(--goal);
  background: var(--goal);
  color: white;
}

.dpad {
  display: grid;
  grid-template-columns: repeat(3, 54px);
  grid-template-rows: repeat(3, 46px);
  justify-content: center;
  align-content: center;
  gap: 8px;
}

.dir,
.actions button {
  background: var(--control);
  color: var(--control-text);
  font-weight: 800;
  box-shadow: 0 3px 0 #000;
}

.dir {
  font-size: 1.25rem;
}

.dir:active,
.actions button:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 #000;
}

.north {
  grid-column: 2;
  grid-row: 1;
}

.west {
  grid-column: 1;
  grid-row: 2;
}

.east {
  grid-column: 3;
  grid-row: 2;
}

.south {
  grid-column: 2;
  grid-row: 3;
}

.actions {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.actions button {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  height: 52px;
  line-height: 1.1;
  white-space: nowrap;
}

.action-label {
  font-size: 0.95rem;
}

.action-hint {
  color: rgba(255, 250, 242, 0.78);
  font-size: 0.72rem;
  font-weight: 700;
}

#nextBtn.next-peck-target {
  outline: 2px solid rgba(134, 201, 79, 0.68);
  outline-offset: 3px;
}

#nextBtn.is-pecked {
  animation: next-peck-pulse 360ms ease-in-out 5;
}

.kiwi-runner {
  --bird-start-x: 0px;
  --bird-start-y: 0px;
  --bird-end-x: 0px;
  --bird-end-y: 0px;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 30;
  width: 220px;
  height: 156px;
  pointer-events: none;
  opacity: 0;
  overflow: visible;
  transform: translate(var(--bird-start-x), var(--bird-start-y)) scale(0.68);
  transform-origin: 40% 84%;
}

.kiwi-runner.is-active {
  animation: kiwi-runner-path 3600ms cubic-bezier(0.2, 0.82, 0.2, 1) forwards;
}

.kiwi-runner__image {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  filter: drop-shadow(0 2px 0 rgba(31, 36, 29, 0.18));
}

.kiwi-runner.is-active .kiwi-runner__image {
  animation:
    kiwi-walk-bob 360ms ease-in-out 0ms 7,
    kiwi-peck 360ms ease-in-out 2520ms 5;
  transform-origin: 54% 72%;
}

.final-victory-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: center;
  pointer-events: none;
  opacity: 0;
  transform: scale(0.72);
}

.final-victory-overlay::before {
  content: "";
  position: absolute;
  width: min(72vmin, 760px);
  aspect-ratio: 1;
  border-radius: 999px;
  background:
    radial-gradient(circle, rgba(255, 244, 168, 0.48) 0 12%, transparent 48%),
    radial-gradient(circle, rgba(98, 214, 255, 0.28) 0 7%, transparent 54%);
  filter: blur(3px);
  opacity: 0;
  transform: scale(0.38);
}

.final-victory-text {
  position: relative;
  z-index: 2;
  display: inline-block;
  color: #fff4a8;
  font-size: clamp(4.5rem, 14vw, 12rem);
  font-weight: 900;
  letter-spacing: 0;
  line-height: 1;
  opacity: 0;
  text-align: center;
  -webkit-text-stroke: 2px rgba(36, 48, 31, 0.72);
  filter: drop-shadow(0 0 22px rgba(255, 221, 87, 0.72));
  text-shadow:
    0 4px 0 #24301f,
    0 8px 0 rgba(94, 53, 27, 0.52),
    0 9px 26px rgba(31, 36, 29, 0.38),
    0 0 34px rgba(255, 221, 87, 0.68),
    0 0 68px rgba(98, 214, 255, 0.42);
  transform: translateY(18px) scale(0.76);
}

.final-victory-text::after {
  content: "";
  position: absolute;
  inset: -16% -8%;
  background: linear-gradient(
    105deg,
    transparent 0 40%,
    rgba(255, 255, 255, 0.82) 48%,
    transparent 56% 100%
  );
  opacity: 0;
  transform: translateX(-72%) skewX(-12deg);
}

.final-victory-overlay.is-visible {
  animation: final-victory-pop 1800ms cubic-bezier(0.17, 0.84, 0.3, 1.28) forwards;
}

.final-victory-overlay.is-visible .final-victory-text {
  animation: final-victory-text-pop 2200ms cubic-bezier(0.15, 0.82, 0.18, 1.18) forwards;
}

.final-victory-overlay.is-visible::before {
  animation: final-victory-glow 1900ms ease-out forwards;
}

.final-victory-overlay.is-visible .final-victory-text::after {
  animation: final-victory-shine 1700ms ease-out 220ms forwards;
}

.final-fireworks-overlay {
  position: fixed;
  inset: 0;
  z-index: 55;
  pointer-events: none;
  overflow: hidden;
  opacity: 0;
}

.final-fireworks-overlay::before {
  content: "";
  position: absolute;
  inset: 0;
  background:
    radial-gradient(circle at 50% 44%, rgba(255, 244, 168, 0.3), transparent 34%),
    linear-gradient(180deg, rgba(31, 36, 29, 0.08), transparent 45%);
  opacity: 0;
}

.final-fireworks-overlay.is-visible {
  opacity: 1;
}

.final-fireworks-overlay.is-visible::before {
  animation: final-firework-screen-flash 1280ms ease-out forwards;
}

.final-firework-comet {
  position: absolute;
  left: var(--comet-x);
  top: 100%;
  width: 8px;
  height: 92px;
  border-radius: 999px;
  background: linear-gradient(180deg, #ffffff 0%, var(--comet-color) 24%, transparent 100%);
  filter: drop-shadow(0 0 10px var(--comet-color));
  opacity: 0;
  transform: translate3d(-50%, 0, 0) scaleY(0.18);
  transform-origin: center top;
}

.final-fireworks-overlay.is-visible .final-firework-comet {
  animation: final-firework-comet var(--comet-duration) cubic-bezier(0.13, 0.72, 0.12, 1)
    var(--comet-delay) forwards;
}

.final-firework-burst {
  position: absolute;
  left: var(--burst-x);
  top: var(--burst-y);
  width: 1px;
  height: 1px;
  opacity: 0;
  transform: translate(-50%, -50%) scale(var(--burst-scale));
}

.final-fireworks-overlay.is-visible .final-firework-burst {
  animation: final-firework-burst-fade 1700ms ease-out var(--burst-delay) forwards;
}

.final-firework-burst::before,
.final-firework-burst::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  border-radius: 999px;
  opacity: 0;
  transform: translate(-50%, -50%) scale(0.18);
}

.final-firework-burst::before {
  width: 132px;
  height: 132px;
  background:
    radial-gradient(circle, rgba(255, 255, 255, 0.96) 0 7%, transparent 8%),
    radial-gradient(circle at 50% 3%, var(--burst-color-a) 0 5px, transparent 6px),
    radial-gradient(circle at 84% 18%, var(--burst-color-b) 0 5px, transparent 6px),
    radial-gradient(circle at 98% 51%, var(--burst-color-c) 0 5px, transparent 6px),
    radial-gradient(circle at 79% 84%, var(--burst-color-a) 0 5px, transparent 6px),
    radial-gradient(circle at 50% 98%, var(--burst-color-b) 0 5px, transparent 6px),
    radial-gradient(circle at 17% 82%, var(--burst-color-c) 0 5px, transparent 6px),
    radial-gradient(circle at 2% 50%, var(--burst-color-a) 0 5px, transparent 6px),
    radial-gradient(circle at 18% 17%, var(--burst-color-b) 0 5px, transparent 6px);
  filter: drop-shadow(0 0 11px var(--burst-color-a));
}

.final-firework-burst::after {
  width: 184px;
  height: 184px;
  background: conic-gradient(
    from 8deg,
    transparent 0 8deg,
    var(--burst-color-a) 8deg 12deg,
    transparent 12deg 34deg,
    var(--burst-color-b) 34deg 39deg,
    transparent 39deg 66deg,
    var(--burst-color-c) 66deg 70deg,
    transparent 70deg 360deg
  );
  mask-image: radial-gradient(circle, transparent 0 37%, #000 39% 64%, transparent 66%);
  -webkit-mask-image: radial-gradient(circle, transparent 0 37%, #000 39% 64%, transparent 66%);
}

.final-fireworks-overlay.is-visible .final-firework-burst::before {
  animation: final-firework-core 1120ms ease-out var(--burst-delay) forwards;
}

.final-fireworks-overlay.is-visible .final-firework-burst::after {
  animation: final-firework-ring 1320ms ease-out var(--burst-delay) forwards;
}

.final-firework-spark {
  position: absolute;
  left: 0;
  top: -2px;
  width: var(--spark-length);
  height: 5px;
  border-radius: 999px;
  background: linear-gradient(90deg, transparent 0%, var(--spark-color) 52%, #ffffff 100%);
  box-shadow: 0 0 10px var(--spark-color);
  opacity: 0;
  transform-origin: left center;
}

.final-fireworks-overlay.is-visible .final-firework-spark {
  animation: final-firework-spark 1180ms cubic-bezier(0.14, 0.76, 0.18, 1) var(--spark-delay)
    forwards;
}

.final-confetti {
  position: absolute;
  left: var(--confetti-x);
  top: -10vh;
  width: 11px;
  height: 16px;
  border-radius: 3px;
  background: var(--confetti-color);
  box-shadow: 0 0 8px rgba(255, 255, 255, 0.48);
  opacity: 0;
  transform: translate3d(0, 0, 0) rotate(0deg);
}

.final-confetti.is-ribbon {
  width: 26px;
  height: 7px;
  border-radius: 999px;
}

.final-fireworks-overlay.is-visible .final-confetti {
  animation: final-confetti-fall var(--confetti-duration) cubic-bezier(0.16, 0.72, 0.22, 1)
    var(--confetti-delay) forwards;
}

.actions button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
  transform: none;
  box-shadow: 0 3px 0 #000;
}

@keyframes kiwi-runner-path {
  0% {
    opacity: 0;
    transform: translate(var(--bird-start-x), var(--bird-start-y)) scale(0.68);
  }
  8% {
    opacity: 1;
  }
  70% {
    opacity: 1;
    transform: translate(var(--bird-end-x), var(--bird-end-y)) scale(0.84);
  }
  100% {
    opacity: 1;
    transform: translate(var(--bird-end-x), var(--bird-end-y)) scale(0.84);
  }
}

@keyframes kiwi-walk-bob {
  0%,
  100% {
    transform: translateY(0) rotate(-1deg);
  }
  50% {
    transform: translateY(-6px) rotate(2deg);
  }
}

@keyframes kiwi-peck {
  0%,
  100% {
    transform: translateY(0) rotate(0deg);
  }
  45% {
    transform: translate(12px, -4px) rotate(-1deg);
  }
}

@keyframes next-peck-pulse {
  0%,
  100% {
    transform: translateY(0);
  }
  45% {
    transform: translateY(2px);
  }
}

@keyframes final-victory-pop {
  0% {
    opacity: 0;
    transform: scale(0.72);
  }
  18% {
    opacity: 1;
    transform: scale(1.08);
  }
  34% {
    transform: scale(0.97);
  }
  52%,
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes final-victory-text-pop {
  0% {
    opacity: 0;
    filter: blur(12px) drop-shadow(0 0 0 rgba(255, 221, 87, 0));
    transform: translateY(18px) scale(0.76);
  }
  16% {
    opacity: 1;
    filter: blur(0) drop-shadow(0 0 34px rgba(255, 221, 87, 0.88));
    transform: translateY(-8px) scale(1.12);
  }
  34% {
    transform: translateY(2px) scale(0.98);
  }
  52%,
  100% {
    opacity: 1;
    filter: blur(0) drop-shadow(0 0 24px rgba(255, 221, 87, 0.78));
    transform: translateY(0) scale(1);
  }
}

@keyframes final-victory-glow {
  0% {
    opacity: 0;
    transform: scale(0.38);
  }
  22% {
    opacity: 1;
  }
  100% {
    opacity: 0.42;
    transform: scale(1.06);
  }
}

@keyframes final-victory-shine {
  0%,
  22% {
    opacity: 0;
    transform: translateX(-72%) skewX(-12deg);
  }
  42% {
    opacity: 0.9;
  }
  100% {
    opacity: 0;
    transform: translateX(72%) skewX(-12deg);
  }
}

@keyframes final-firework-screen-flash {
  0% {
    opacity: 0;
  }
  14% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

@keyframes final-firework-comet {
  0% {
    opacity: 0;
    transform: translate3d(-50%, 0, 0) scaleY(0.18);
  }
  18% {
    opacity: 1;
  }
  78% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: translate3d(-50%, var(--comet-rise), 0) scaleY(1);
  }
}

@keyframes final-firework-burst-fade {
  0%,
  8% {
    opacity: 0;
  }
  12%,
  72% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

@keyframes final-firework-core {
  0% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.12) rotate(0deg);
  }
  12% {
    opacity: 1;
  }
  64% {
    opacity: 0.94;
    transform: translate(-50%, -50%) scale(1) rotate(18deg);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(1.18) rotate(34deg);
  }
}

@keyframes final-firework-ring {
  0% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.08) rotate(0deg);
  }
  16% {
    opacity: 0.86;
  }
  72% {
    opacity: 0.5;
    transform: translate(-50%, -50%) scale(1) rotate(34deg);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(1.16) rotate(58deg);
  }
}

@keyframes final-firework-spark {
  0% {
    opacity: 0;
    transform: rotate(var(--spark-angle)) translateX(0) scaleX(0.12);
  }
  12% {
    opacity: 1;
  }
  68% {
    opacity: 1;
    transform: rotate(var(--spark-angle)) translateX(var(--spark-distance)) scaleX(1);
  }
  100% {
    opacity: 0;
    transform: rotate(var(--spark-angle)) translateX(var(--spark-distance)) scaleX(0.18);
  }
}

@keyframes final-confetti-fall {
  0% {
    opacity: 0;
    transform: translate3d(0, 0, 0) rotate(0deg);
  }
  8%,
  74% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: translate3d(var(--confetti-drift), var(--confetti-fall), 0)
      rotate(var(--confetti-rotate));
  }
}

@media (max-width: 760px) {
  body {
    overflow: auto;
  }

  .app-shell {
    min-height: 100vh;
    height: auto;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .stats {
    gap: 6px;
    justify-content: flex-start;
    font-size: 0.85rem;
  }

  .stats span {
    min-width: 0;
    padding: 7px 8px;
  }

  .play-layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(420px, 60vh) auto;
    width: 100%;
    max-width: 100vw;
    min-width: 0;
  }

  .panel {
    grid-template-columns: minmax(0, 1fr);
    min-width: 0;
    width: 100%;
    max-width: 100%;
    padding: clamp(14px, 4vw, 20px);
    border-top: 1px solid var(--line);
    border-left: 0;
  }

  .level-grid,
  .dpad,
  .actions {
    min-width: 0;
    max-width: 100%;
  }

  .actions {
    gap: 8px;
  }

  .actions button,
  .level-grid button {
    min-width: 0;
  }

  .actions button {
    padding-inline: 6px;
  }

  .action-hint {
    font-size: 0.68rem;
  }

}
