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

html, body {
  height: 100%;
  overflow: hidden;
  font-family: system-ui, sans-serif;
}

.stage {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #5b8e6e;
  background-image:
    linear-gradient(45deg, rgba(0, 0, 0, 0.05) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(0, 0, 0, 0.05) 25%, transparent 25%);
  background-size: 40px 40px;
  background-position: 0 0, 20px 20px;
}

.stage.snow {
  background: #cfe3f0;
  background-image:
    linear-gradient(45deg, rgba(255, 255, 255, 0.5) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(255, 255, 255, 0.5) 25%, transparent 25%),
    linear-gradient(to bottom, #eaf4fb, #cfe3f0);
  background-size: 40px 40px, 40px 40px, 100% 100%;
  background-position: 0 0, 20px 20px, 0 0;
}

.houses {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 2;
}

.houses.hidden {
  display: none;
}

.house {
  position: absolute;
  width: 150px;
  height: 120px;
  background: #f7efe0;
  border: 3px solid #4a3421;
  border-bottom: none;
  border-radius: 4px 4px 0 0;
  z-index: 2;
}

.house::before {
  content: '';
  position: absolute;
  top: -36px;
  left: -8px;
  width: 112%;
  height: 46px;
  background: #c0392b;
  clip-path: polygon(50% 0, 0 100%, 100% 100%);
  z-index: 1;
}

.house::after {
  content: '';
  position: absolute;
  bottom: -4px;
  left: 8px;
  width: 128px;
  height: 10px;
  background: #d7d7d7;
  border-radius: 2px;
}

.house .roof-lights,
.house .edge-lights {
  position: absolute;
  z-index: 3;
}

.house .roof-lights {
  top: -30px;
  left: 10px;
  width: 130px;
}

.house .edge-lights {
  top: -32px;
  left: 0;
}

.house i {
  position: absolute;
  top: -4px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: hsl(var(--hue), 100%, 60%);
  box-shadow: 0 0 8px 2px hsl(var(--hue), 100%, 60%);
  animation: blink 1s ease-in-out infinite;
}

.house .roof-lights i:nth-child(1) { left: 0; animation-delay: 0s; }
.house .roof-lights i:nth-child(2) { left: 22px; animation-delay: 0.15s; }
.house .roof-lights i:nth-child(3) { left: 44px; animation-delay: 0.3s; }
.house .roof-lights i:nth-child(4) { left: 66px; animation-delay: 0.45s; }
.house .roof-lights i:nth-child(5) { left: 88px; animation-delay: 0.6s; }
.house .roof-lights i:nth-child(6) { left: 110px; animation-delay: 0.75s; }

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.25; }
}

.stage.snow::after {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  background-image: radial-gradient(rgba(255, 255, 255, 0.9) 2px, transparent 2px);
  background-size: 70px 70px;
  opacity: 0.7;
  z-index: 0;
}

/* ---- HP HUD ---- */
.hud {
  position: absolute;
  top: 18px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(0, 0, 0, 0.55);
  padding: 8px 14px;
  border-radius: 10px;
  z-index: 10;
}

.hp-label {
  color: #fff;
  font-weight: 700;
  letter-spacing: 0.5px;
}

.hp-bar {
  width: 220px;
  height: 16px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 8px;
  overflow: hidden;
}

#hp-fill {
  height: 100%;
  width: 100%;
  background: #ff4757;
  border-radius: 8px;
  transition: width 0.2s linear;
}

.hp-text {
  color: #fff;
  font-variant-numeric: tabular-nums;
  font-size: 14px;
}

.boss-hud {
  top: 104px;
}

.boss-label {
  color: #ffd32a;
}

#boss-hp-fill {
  height: 100%;
  width: 100%;
  background: #ff9f43;
  border-radius: 8px;
  transition: width 0.2s linear;
}

.boss-hud.hidden {
  display: none;
}

/* ---- level banner ---- */
.top-left {
  position: absolute;
  top: 18px;
  left: 18px;
  display: flex;
  align-items: center;
  gap: 10px;
  z-index: 10;
}

.coins {
  color: #fff;
  font-weight: 700;
  background: rgba(0, 0, 0, 0.55);
  padding: 8px 14px;
  border-radius: 10px;
}

.shop-btn {
  font-size: 15px;
  font-weight: 700;
  color: #fff;
  background: #ff6b35;
  border: none;
  border-radius: 10px;
  padding: 9px 18px;
  cursor: pointer;
  box-shadow: 0 3px 0 #b3401a;
  transition: transform 0.1s ease, box-shadow 0.1s ease;
}

.shop-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 0 #b3401a;
}

.shop-btn:active {
  transform: translateY(1px);
  box-shadow: 0 1px 0 #b3401a;
}

.shop-btn.hidden {
  display: none;
}

.level-banner {
  position: absolute;
  top: 70px;
  left: 50%;
  transform: translateX(-50%);
  color: #fff;
  font-weight: 700;
  letter-spacing: 1px;
  background: rgba(0, 0, 0, 0.55);
  padding: 6px 14px;
  border-radius: 8px;
  z-index: 10;
}

/* ---- menu ---- */
.top-right {
  position: absolute;
  top: 18px;
  right: 18px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
  z-index: 15;
}

.menu-btn {
  font-size: 15px;
  font-weight: 700;
  color: #fff;
  background: #57606f;
  border: none;
  border-radius: 10px;
  padding: 9px 18px;
  cursor: pointer;
  box-shadow: 0 3px 0 #2f3542;
  transition: transform 0.1s ease, box-shadow 0.1s ease;
}

.menu-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 0 #2f3542;
}

.menu-btn:active {
  transform: translateY(1px);
  box-shadow: 0 1px 0 #2f3542;
}

.menu-dropdown {
  display: flex;
  flex-direction: column;
  background: rgba(20, 20, 20, 0.95);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
}

.menu-dropdown.hidden {
  display: none;
}

.menu-item {
  font-size: 15px;
  font-weight: 600;
  color: #fff;
  background: transparent;
  border: none;
  padding: 12px 22px;
  cursor: pointer;
  text-align: left;
  white-space: nowrap;
}

.menu-item:hover {
  background: rgba(255, 255, 255, 0.1);
}

/* ---- entities ---- */
#player,
.enemy {
  position: absolute;
  top: 50%;
  left: 50%;
  will-change: transform;
  z-index: 3;
}

.shadow {
  position: absolute;
  left: 50%;
  bottom: -8px;
  width: 34px;
  height: 10px;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.25);
  border-radius: 50%;
}

#hero {
  display: block;
  width: 80px;
  height: auto;
  image-rendering: pixelated;
  transform-origin: bottom center;
  will-change: transform;
}

#hero.walking {
  animation: walk 0.3s steps(4, end) infinite;
}

#hero.hit {
  animation: hurt 0.3s ease;
}

@keyframes walk {
  0%   { transform: translateY(0) translateX(0) rotate(-2deg); }
  25%  { transform: translateY(-5px) translateX(2px) rotate(0deg); }
  50%  { transform: translateY(0) translateX(0) rotate(2deg); }
  75%  { transform: translateY(-5px) translateX(-2px) rotate(0deg); }
  100% { transform: translateY(0) translateX(0) rotate(-2deg); }
}

@keyframes hurt {
  0%, 100% { filter: none; }
  30% { filter: invert(1); transform: translateX(-4px); }
  60% { filter: none; transform: translateX(4px); }
}

.enemy-hud {
  position: absolute;
  left: 50%;
  bottom: 100%;
  margin-bottom: 6px;
  transform: translateX(-50%);
  width: 84px;
  background: rgba(0, 0, 0, 0.55);
  border-radius: 6px;
  padding: 2px;
}

.enemy-bar {
  width: 100%;
  height: 8px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 4px;
  overflow: hidden;
}

.enemy-hp-fill {
  height: 100%;
  width: 100%;
  background: #ffd32a;
  border-radius: 4px;
  transition: width 0.2s linear;
}

.enemy-img {
  display: block;
  width: 90px;
  height: auto;
  image-rendering: pixelated;
  transform-origin: bottom center;
  will-change: transform;
}

.enemy-img.attacking {
  animation: lunge 0.35s ease;
}

.enemy-img.hit {
  animation: hurt 0.3s ease;
}

.fireball {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 28px;
  height: 28px;
  margin: -14px 0 0 -14px;
  background: radial-gradient(circle at 35% 35%, #ffe29a, #ff9f1a 60%, #ff5e00);
  border-radius: 50%;
  box-shadow: 0 0 14px 4px rgba(255, 140, 20, 0.7);
  z-index: 8;
  will-change: transform;
}

.player-fireball {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 22px;
  height: 22px;
  margin: -11px 0 0 -11px;
  background: radial-gradient(circle at 35% 35%, #fff7d6, #ffb347 55%, #ff6b1a);
  border-radius: 50%;
  box-shadow: 0 0 12px 4px rgba(255, 120, 40, 0.8);
  z-index: 9;
  will-change: transform;
}

.spear {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 44px;
  height: 10px;
  margin: -5px 0 0 -22px;
  background: linear-gradient(to right, #8a7a5c 0%, #d9cdb4 30%, #ffffff 60%, #d9cdb4 80%);
  border-radius: 3px;
  box-shadow: 0 0 8px 2px rgba(255, 255, 255, 0.6);
  z-index: 8;
  will-change: transform;
}

.spear::before {
  content: '';
  position: absolute;
  left: -12px;
  top: -4px;
  width: 0;
  height: 0;
  border-top: 9px solid transparent;
  border-bottom: 9px solid transparent;
  border-right: 14px solid #e8e8e8;
  filter: drop-shadow(0 0 3px rgba(255, 255, 255, 0.7));
}

.spear::after {
  content: '';
  position: absolute;
  right: -8px;
  top: 1px;
  width: 8px;
  height: 8px;
  background: #b8a88a;
  border-radius: 2px;
}

@keyframes lunge {
  0%   { transform: translateY(0); }
  40%  { transform: translateY(-18px); }
  100% { transform: translateY(0); }
}

/* ---- game over ---- */
.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.7);
  z-index: 20;
}

.overlay.hidden {
  display: none;
}

.overlay-card {
  color: #fff;
  font-size: 42px;
  font-weight: 800;
  letter-spacing: 2px;
  text-shadow: 0 3px 0 rgba(0, 0, 0, 0.6);
  text-align: center;
}

.afk-card {
  max-width: min(560px, 90vw);
}

.afk-sub {
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 0.5px;
  text-shadow: none;
  margin-top: 8px;
}

.overlay-title {
  margin-bottom: 24px;
}

.overlay-buttons {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.next-btn {
  display: inline-block;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 1px;
  color: #fff;
  background: #ff6b35;
  border: none;
  border-radius: 10px;
  padding: 12px 28px;
  cursor: pointer;
  box-shadow: 0 4px 0 #b3401a;
  transition: transform 0.1s ease, box-shadow 0.1s ease;
}

.next-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 0 #b3401a;
}

.next-btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 #b3401a;
}

.next-btn.hidden {
  display: none;
}

.replay-btn {
  background: #5c7cfa;
  box-shadow: 0 4px 0 #3b5bdb;
}

.replay-btn:hover {
  box-shadow: 0 6px 0 #3b5bdb;
}

.replay-btn:active {
  box-shadow: 0 1px 0 #3b5bdb;
}

/* ---- shop ---- */.shop-card {
  width: min(620px, 94vw);
  max-height: 92vh;
  display: flex;
  flex-direction: column;
  background: #000;
  border: 4px solid #fff;
  border-radius: 8px;
  padding: 14px;
  color: #fff;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
  font-family: 'Courier New', monospace;
}

.shop-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  border-bottom: 2px solid #fff;
  padding-bottom: 10px;
}

.shop-header h2 {
  font-size: 24px;
  letter-spacing: 1px;
}

.shop-coins {
  font-weight: 700;
  color: #ffd32a;
}

.close-btn {
  font-size: 16px;
  font-weight: 700;
  color: #fff;
  background: transparent;
  border: 2px solid #fff;
  border-radius: 6px;
  width: 34px;
  height: 34px;
  cursor: pointer;
}

.close-btn:hover {
  background: #ff4757;
  border-color: #ff4757;
}

.shop-body {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
}

.shopkeeper {
  width: 120px;
  height: 120px;
  object-fit: cover;
  border: 2px solid #fff;
  margin: 0 auto;
  flex-shrink: 0;
}

.upgrades-box {
  flex: 1;
  min-height: 0;
  background: #000;
  border: 3px solid #fff;
  border-radius: 6px;
  padding: 12px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.upgrade-btn {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  text-align: left;
  color: #fff;
  background: #111;
  border: 2px solid #3a3a3a;
  border-radius: 8px;
  padding: 10px 12px;
  cursor: pointer;
  transition: transform 0.1s ease, border-color 0.1s ease, background 0.1s ease;
}

.upgrade-btn:hover {
  border-color: #ffd32a;
  background: #1c1c1c;
  transform: translateY(-2px);
}

.upgrade-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.upgrade-btn strong {
  font-size: 16px;
}

.upgrade-btn .cost {
  color: #ffd32a;
  font-weight: 700;
}

.upgrade-btn .sold {
  color: #2ed573;
  font-weight: 700;
}

.upgrade-btn.locked {
  opacity: 0.6;
}

/* ---- level select ---- */
.levels-card {
  width: min(520px, 92vw);
  background: #222;
  border-radius: 16px;
  padding: 18px;
  color: #fff;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
}

.levels-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.levels-header h2 {
  font-size: 24px;
}

.levels-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

.level-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 16px 8px;
  border-radius: 12px;
  border: 2px solid #444;
  background: #333;
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.1s ease, border-color 0.1s ease;
}

.level-btn:hover {
  border-color: #ffd32a;
  transform: translateY(-2px);
}

.level-btn .level-name {
  font-size: 13px;
  font-weight: 600;
  color: #ffd32a;
}

.level-btn.locked {
  opacity: 0.55;
  cursor: not-allowed;
}

.level-btn.locked:hover {
  border-color: #444;
  transform: none;
}

.level-btn.locked .lock {
  font-size: 20px;
}
