:root {
  --grass-a: #5aad3a;
  --grass-b: #4e9a32;
  --grass-dark: #3d7a28;
  --soil: #6b4a2a;
  --sky-top: #87ceeb;
  --sky-bot: #b8e0f0;
  --house: #c45c26;
  --panel: #fff8e7;
  --panel-border: #c4a35a;
  --ink: #2a1f12;
  --accent: #f0a020;
  --danger: #d94040;
  --ok: #3a9a3a;
  --shadow: rgba(0, 0, 0, 0.25);
  --cell-w: 72px;
  --cell-h: 80px;
  --rows: 5;
  --cols: 9;
  --radius: 14px;
  font-family: "Nunito", system-ui, sans-serif;
}

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

html,
body {
  height: 100%;
  overflow: hidden;
  background: linear-gradient(180deg, #1a3a1a 0%, #0d2010 100%);
  color: var(--ink);
  user-select: none;
  -webkit-user-select: none;
}

#app {
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  position: relative;
}

/* ---------- Screens ---------- */
.screen {
  display: none;
  width: 100%;
  height: 100%;
  place-items: center;
}

.screen.active {
  display: grid;
}

/* ---------- Title ---------- */
#screen-title {
  background:
    radial-gradient(ellipse at 50% 20%, #ffe08a55 0%, transparent 50%),
    linear-gradient(180deg, #5a9e3a 0%, #2d6a20 40%, #1a4010 100%);
}

.title-card {
  text-align: center;
  background: var(--panel);
  border: 4px solid var(--panel-border);
  border-radius: 24px;
  padding: 2.5rem 3rem;
  box-shadow:
    0 12px 0 #8a6a30,
    0 20px 40px var(--shadow);
  max-width: 480px;
  animation: pop-in 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.title-sun {
  font-size: 4rem;
  animation: spin-slow 12s linear infinite;
  filter: drop-shadow(0 4px 8px #f0a02088);
}

h1 {
  font-size: 2.6rem;
  font-weight: 900;
  letter-spacing: -0.02em;
  color: #2d6a20;
  text-shadow: 0 2px 0 #c4e8a0;
  margin: 0.4rem 0;
}

.subtitle {
  color: #5a4a30;
  margin-bottom: 1.2rem;
  font-weight: 600;
}

.title-features {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
  margin-bottom: 1.5rem;
}

.title-features span {
  background: #e8f5d0;
  border: 2px solid #a8c870;
  border-radius: 999px;
  padding: 0.3rem 0.75rem;
  font-size: 0.85rem;
  font-weight: 700;
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-block;
  font-family: inherit;
  font-weight: 800;
  font-size: 1.05rem;
  border: none;
  border-radius: 12px;
  padding: 0.75rem 1.6rem;
  cursor: pointer;
  transition: transform 0.12s, box-shadow 0.12s, filter 0.12s;
  margin: 0.3rem;
}

.btn:hover {
  transform: translateY(-2px);
  filter: brightness(1.05);
}

.btn:active {
  transform: translateY(1px);
}

.btn-primary {
  background: linear-gradient(180deg, #f0c040, #e09020);
  color: #3a2000;
  box-shadow: 0 4px 0 #a06010;
}

.btn-primary:hover {
  box-shadow: 0 6px 0 #a06010;
}

.btn-ghost {
  background: #efe4c8;
  color: var(--ink);
  box-shadow: 0 3px 0 #c4a35a;
}

.btn-small {
  padding: 0.4rem 0.7rem;
  font-size: 1.1rem;
  background: #efe4c8;
  box-shadow: 0 2px 0 #c4a35a;
  border-radius: 10px;
}

/* ---------- How to ---------- */
#screen-howto {
  background: linear-gradient(180deg, #3a6a2a, #1a3010);
}

.panel {
  background: var(--panel);
  border: 4px solid var(--panel-border);
  border-radius: 20px;
  padding: 2rem 2.2rem;
  box-shadow: 0 10px 0 #8a6a30, 0 16px 32px var(--shadow);
  max-width: 560px;
  text-align: center;
  animation: pop-in 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.panel h2 {
  font-size: 1.8rem;
  color: #2d6a20;
  margin-bottom: 1rem;
}

.howto-list {
  text-align: left;
  margin: 0 auto 1.2rem;
  padding-left: 1.3rem;
  line-height: 1.55;
  color: #3a3020;
  font-weight: 600;
}

.howto-list li {
  margin-bottom: 0.45rem;
}

.howto-plants {
  display: grid;
  gap: 0.45rem;
  text-align: left;
  margin-bottom: 1.2rem;
}

.howto-plant {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  background: #f5edd8;
  border-radius: 10px;
  padding: 0.4rem 0.7rem;
  font-size: 0.9rem;
  font-weight: 600;
}

.howto-plant span {
  font-size: 1.4rem;
}

/* ---------- Game screen ---------- */
#screen-game {
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.6rem;
  background:
    linear-gradient(180deg, var(--sky-top) 0%, var(--sky-bot) 28%, transparent 28%),
    linear-gradient(180deg, #4a8a30 28%, #3a7020 100%);
  padding: 0.6rem;
}

#screen-game.active {
  display: flex;
}

/* ---------- HUD ---------- */
#hud {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  width: min(100%, calc(var(--cols) * var(--cell-w) + 70px + 24px));
  background: linear-gradient(180deg, #5a3a18, #3d2810);
  border: 3px solid #2a1808;
  border-radius: 16px;
  padding: 0.45rem 0.6rem;
  box-shadow: 0 4px 12px var(--shadow);
  flex-wrap: wrap;
  justify-content: space-between;
}

.hud-left,
.hud-right {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.sun-bank,
.lives {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  background: #1a1008;
  color: #ffe080;
  font-weight: 900;
  font-size: 1.25rem;
  padding: 0.35rem 0.7rem;
  border-radius: 12px;
  border: 2px solid #c4a35a;
  min-width: 72px;
}

.sun-icon {
  font-size: 1.4rem;
  animation: pulse 2s ease-in-out infinite;
}

.lives {
  color: #ffb0a0;
  min-width: 56px;
}

.seed-bar {
  display: flex;
  gap: 0.35rem;
  flex: 1;
  justify-content: center;
  flex-wrap: wrap;
}

.seed {
  width: 68px;
  background: linear-gradient(180deg, #efe4c0, #d8c890);
  border: 3px solid #8a7040;
  border-radius: 10px;
  padding: 0.25rem 0.2rem 0.35rem;
  cursor: pointer;
  text-align: center;
  transition: transform 0.1s, border-color 0.1s, filter 0.1s;
  position: relative;
  box-shadow: 0 3px 0 #6a5030;
}

.seed:hover:not(.disabled):not(.selected) {
  transform: translateY(-3px);
  filter: brightness(1.08);
}

.seed.selected {
  border-color: #f0c020;
  box-shadow: 0 0 0 3px #f0c02088, 0 3px 0 #6a5030;
  transform: translateY(-4px);
  background: linear-gradient(180deg, #fff8d0, #f0e0a0);
}

.seed.disabled {
  filter: grayscale(0.7) brightness(0.75);
  cursor: not-allowed;
  opacity: 0.7;
}

.seed-emoji {
  font-size: 1.55rem;
  line-height: 1.2;
  display: block;
}

.seed-name {
  font-size: 0.62rem;
  font-weight: 800;
  color: #3a2a10;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.seed-cost {
  font-size: 0.75rem;
  font-weight: 900;
  color: #8a5010;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.1rem;
}

.seed-cd {
  position: absolute;
  inset: 0;
  background: rgba(20, 10, 0, 0.55);
  border-radius: 7px;
  display: flex;
  align-items: flex-end;
  overflow: hidden;
  pointer-events: none;
}

.seed-cd-fill {
  width: 100%;
  background: rgba(240, 180, 40, 0.45);
  transition: height 0.1s linear;
}

.wave-info {
  color: #ffe8b0;
  font-weight: 700;
  font-size: 0.85rem;
  text-align: center;
  line-height: 1.2;
}

.wave-info strong {
  font-size: 1.3rem;
  color: #fff;
  display: block;
}

.wave-total {
  opacity: 0.7;
  font-size: 0.75rem;
}

/* ---------- Battlefield ---------- */
#battlefield-wrap {
  display: flex;
  align-items: stretch;
  border-radius: 12px;
  overflow: hidden;
  box-shadow:
    0 8px 24px var(--shadow),
    inset 0 0 0 3px #2a1808;
  border: 3px solid #2a1808;
}

#house-col {
  width: 56px;
  background: linear-gradient(90deg, #8a4a28, #6a3520);
  display: flex;
  align-items: center;
  justify-content: center;
  border-right: 3px solid #4a2810;
  position: relative;
}

.house {
  font-size: 2.2rem;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.4));
  animation: house-bob 3s ease-in-out infinite;
}

#battlefield {
  position: relative;
  width: calc(var(--cols) * var(--cell-w));
  height: calc(var(--rows) * var(--cell-h));
  background: repeating-linear-gradient(
    180deg,
    var(--grass-a) 0,
    var(--grass-a) var(--cell-h),
    var(--grass-b) var(--cell-h),
    var(--grass-b) calc(var(--cell-h) * 2)
  );
}

#game-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
}

#grid {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell-w));
  grid-template-rows: repeat(var(--rows), var(--cell-h));
  z-index: 2;
}

.cell {
  border: 1px solid transparent;
  cursor: pointer;
  transition: background 0.12s;
  position: relative;
}

.cell:hover {
  background: rgba(255, 255, 200, 0.18);
}

.cell.valid {
  background: rgba(100, 220, 80, 0.28);
  box-shadow: inset 0 0 0 2px rgba(80, 200, 60, 0.6);
}

.cell.invalid {
  background: rgba(220, 60, 40, 0.22);
}

.cell.occupied::after {
  content: "";
  /* visual handled by plant entity */
}

#entities,
#suns-layer,
#fx-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 3;
}

#suns-layer {
  z-index: 10;
  pointer-events: none;
}

/* ---------- Entities ---------- */
.entity {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  transform: translate(-50%, -50%);
  transition: none;
  filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.35));
  z-index: 4;
  line-height: 1;
  pointer-events: none;
}

.entity.plant {
  font-size: 2.1rem;
  z-index: 5;
  animation: plant-idle 2.4s ease-in-out infinite;
}

.entity.plant.wall {
  font-size: 2rem;
  animation: none;
}

.entity.zombie {
  font-size: 2.15rem;
  z-index: 6;
  transition: left 0.05s linear;
}

.entity.zombie.eating {
  animation: chomp 0.35s ease-in-out infinite;
}

.entity.zombie.slowed {
  filter: drop-shadow(0 0 6px #60c0ff) drop-shadow(0 2px 3px rgba(0, 0, 0, 0.35));
}

.entity.zombie.boss {
  font-size: 2.6rem;
  filter: drop-shadow(0 0 8px #ff4040aa) drop-shadow(0 2px 3px rgba(0, 0, 0, 0.4));
}

.entity.pea {
  font-size: 0.95rem;
  z-index: 7;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.3));
}

.entity.pea.frost {
  filter: drop-shadow(0 0 4px #60d0ff);
}

.hp-bar {
  position: absolute;
  bottom: -6px;
  left: 50%;
  transform: translateX(-50%);
  width: 36px;
  height: 5px;
  background: #1a1008;
  border-radius: 3px;
  overflow: hidden;
  border: 1px solid #0006;
}

.hp-bar > i {
  display: block;
  height: 100%;
  background: linear-gradient(90deg, #60d040, #a0e060);
  border-radius: 2px;
  transition: width 0.15s;
}

.hp-bar.low > i {
  background: linear-gradient(90deg, #e04030, #f08040);
}

/* ---------- Suns ---------- */
.sun {
  position: absolute;
  width: 44px;
  height: 44px;
  font-size: 1.8rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  pointer-events: auto;
  transform: translate(-50%, -50%);
  animation: sun-bob 1.2s ease-in-out infinite;
  filter: drop-shadow(0 0 8px #ffd040) drop-shadow(0 2px 4px rgba(0, 0, 0, 0.3));
  z-index: 20;
  transition: transform 0.15s, opacity 0.2s;
}

.sun:hover {
  transform: translate(-50%, -50%) scale(1.15);
}

.sun.collecting {
  animation: none;
  transition: left 0.45s ease-in, top 0.45s ease-in, opacity 0.45s, transform 0.45s;
  opacity: 0;
  pointer-events: none;
}

/* ---------- FX ---------- */
.fx {
  position: absolute;
  transform: translate(-50%, -50%);
  pointer-events: none;
  z-index: 15;
  font-weight: 900;
  font-size: 1rem;
  color: #fff;
  text-shadow: 0 1px 3px #000a;
  animation: float-up 0.8s ease-out forwards;
}

.fx.boom {
  font-size: 2.5rem;
  animation: boom 0.55s ease-out forwards;
}

.fx.damage {
  color: #ff6060;
  font-size: 0.9rem;
}

.fx.sun-pop {
  color: #ffe040;
}

.mower {
  position: absolute;
  left: -8px;
  transform: translateY(-50%);
  font-size: 1.6rem;
  z-index: 8;
  filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.4));
  transition: left 0.8s linear;
  pointer-events: none;
}

.mower.used {
  opacity: 0.25;
  filter: grayscale(1);
}

.mower.racing {
  z-index: 12;
}

/* ---------- Shovel bar ---------- */
#shovel-bar {
  display: flex;
  align-items: center;
  gap: 1rem;
  width: min(100%, calc(var(--cols) * var(--cell-w) + 70px + 24px));
}

.shovel-btn {
  font-family: inherit;
  font-weight: 800;
  font-size: 0.9rem;
  background: linear-gradient(180deg, #d0c0a0, #a89070);
  border: 3px solid #6a5030;
  border-radius: 10px;
  padding: 0.45rem 0.9rem;
  cursor: pointer;
  box-shadow: 0 3px 0 #4a3820;
  color: var(--ink);
  transition: transform 0.1s, border-color 0.1s;
}

.shovel-btn:hover {
  transform: translateY(-2px);
}

.shovel-btn.selected {
  border-color: #e04030;
  box-shadow: 0 0 0 3px #e0403088, 0 3px 0 #4a3820;
  background: linear-gradient(180deg, #f0d0c0, #d0a090);
}

.progress-wrap {
  flex: 1;
}

.progress-label {
  font-size: 0.75rem;
  font-weight: 800;
  color: #e8f5d0;
  text-shadow: 0 1px 2px #0008;
  margin-bottom: 0.2rem;
}

.progress-track {
  height: 12px;
  background: #1a1008;
  border-radius: 8px;
  border: 2px solid #4a3820;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #60a040, #c0e060);
  border-radius: 6px;
  transition: width 0.2s linear;
}

/* ---------- Overlays ---------- */
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(10, 20, 8, 0.72);
  display: none;
  place-items: center;
  z-index: 100;
  backdrop-filter: blur(3px);
}

.overlay.active {
  display: grid;
}

.end-emoji {
  font-size: 3.5rem;
  margin-bottom: 0.3rem;
}

.end-stats {
  display: flex;
  gap: 1rem;
  justify-content: center;
  margin: 0.8rem 0 1.2rem;
  flex-wrap: wrap;
}

.end-stats .stat {
  background: #f5edd8;
  border-radius: 10px;
  padding: 0.5rem 0.9rem;
  font-weight: 700;
  font-size: 0.9rem;
}

.end-stats .stat b {
  display: block;
  font-size: 1.3rem;
  color: #2d6a20;
}

/* ---------- Cursor plant ghost ---------- */
body.placing,
body.shoveling {
  cursor: crosshair;
}

/* ---------- Animations ---------- */
@keyframes pop-in {
  from {
    opacity: 0;
    transform: scale(0.85) translateY(12px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

@keyframes spin-slow {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

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

@keyframes plant-idle {
  0%,
  100% {
    transform: translate(-50%, -50%) rotate(-2deg);
  }
  50% {
    transform: translate(-50%, -52%) rotate(2deg);
  }
}

@keyframes chomp {
  0%,
  100% {
    transform: translate(-50%, -50%) scale(1);
  }
  50% {
    transform: translate(-50%, -50%) scale(1.08) rotate(-4deg);
  }
}

@keyframes sun-bob {
  0%,
  100% {
    transform: translate(-50%, -50%) rotate(-8deg);
  }
  50% {
    transform: translate(-50%, calc(-50% - 4px)) rotate(8deg);
  }
}

@keyframes float-up {
  from {
    opacity: 1;
    transform: translate(-50%, -50%);
  }
  to {
    opacity: 0;
    transform: translate(-50%, calc(-50% - 40px));
  }
}

@keyframes boom {
  0% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(0.4);
  }
  40% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1.4);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(1.8);
  }
}

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

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  :root {
    --cell-w: 56px;
    --cell-h: 64px;
  }

  .seed {
    width: 56px;
  }

  .seed-emoji {
    font-size: 1.3rem;
  }

  .entity.plant,
  .entity.zombie {
    font-size: 1.7rem;
  }

  .entity.zombie.boss {
    font-size: 2.1rem;
  }

  #house-col {
    width: 42px;
  }

  .house {
    font-size: 1.6rem;
  }
}

@media (max-width: 640px) {
  :root {
    --cell-w: 42px;
    --cell-h: 52px;
  }

  .title-card {
    padding: 1.5rem;
    margin: 1rem;
  }

  h1 {
    font-size: 1.9rem;
  }

  .seed {
    width: 48px;
    padding: 0.15rem;
  }

  .seed-name {
    display: none;
  }

  .entity.plant,
  .entity.zombie {
    font-size: 1.35rem;
  }

  #hud {
    gap: 0.35rem;
  }
}
