* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #dff3ff;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  touch-action: none;
  overscroll-behavior: none;
}

#game {
  display: block;
  width: 100vw;
  height: 100vh;
  touch-action: none;
}

#hud {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  color: #234;
  font-size: 22px;
  font-weight: 800;
  background: rgba(255, 255, 255, 0.7);
  padding: 8px 20px;
  border-radius: 24px;
  pointer-events: none;
  z-index: 5;
}

#go-home-btn {
  position: fixed;
  right: max(16px, env(safe-area-inset-right));
  bottom: 306px;
  display: none;
  font-size: 15px;
  font-weight: 900;
  padding: 14px 18px;
  border-radius: 20px;
  border: none;
  background: #2ecc40;
  color: #06340c;
  box-shadow: 0 5px 0 #1e8f2c;
  z-index: 6;
  animation: go-home-pulse 1.3s ease-in-out infinite;
}

#go-home-btn.visible {
  display: block;
}

#go-home-btn:active {
  transform: translateY(3px);
  box-shadow: 0 2px 0 #1e8f2c;
}

@keyframes go-home-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.06); }
}

#go-home-overlay {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at 50% 55%, #1c3d2f 0%, #0a1a12 75%);
  z-index: 40;
}

#go-home-overlay.visible {
  display: flex;
}

#go-home-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  text-align: center;
  padding: 20px;
}

.home-house-pop {
  font-size: 96px;
  opacity: 0;
  transform: scale(0.2) translateY(20px);
  filter: drop-shadow(0 0 0 rgba(255, 220, 0, 0));
  transition: none;
}

.home-house-pop.show {
  animation: home-pop 0.7s cubic-bezier(0.34, 1.56, 0.64, 1) forwards,
             home-glow-pulse 1.2s ease-in-out 0.7s infinite;
}

@keyframes home-pop {
  0%   { opacity: 0; transform: scale(0.2) translateY(20px); filter: drop-shadow(0 0 0 rgba(255,220,0,0)); }
  60%  { opacity: 1; transform: scale(1.3) translateY(-10px); filter: drop-shadow(0 0 28px rgba(255,220,0,0.9)); }
  100% { opacity: 1; transform: scale(1) translateY(0);       filter: drop-shadow(0 0 14px rgba(255,220,0,0.5)); }
}

@keyframes home-glow-pulse {
  0%, 100% { filter: drop-shadow(0 0 14px rgba(255,220,0,0.4)); }
  50%       { filter: drop-shadow(0 0 32px rgba(255,220,0,0.9)); }
}

.home-line {
  font-size: 30px;
  font-weight: 900;
  color: #fff;
  text-shadow: 0 3px 0 rgba(0,0,0,0.4);
  margin: 0;
  opacity: 0;
  transform: scale(0.4);
}

.home-line.show {
  animation: cine-text-pop 0.4s ease forwards;
}

#wish-btn {
  display: none;
  font-size: 22px;
  font-weight: 900;
  padding: 20px 36px;
  border-radius: 30px;
  border: none;
  background: #ffd400;
  color: #402400;
  box-shadow: 0 6px 0 #c9a400;
  margin-top: 8px;
  animation: wish-pulse 0.9s ease-in-out infinite;
}

#wish-btn.visible {
  display: block;
}

#wish-btn:active {
  transform: translateY(3px);
  box-shadow: 0 3px 0 #c9a400;
}

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

#build-house-btn {
  position: fixed;
  right: max(16px, env(safe-area-inset-right));
  bottom: 246px;
  display: none;
  font-size: 15px;
  font-weight: 900;
  padding: 14px 18px;
  border-radius: 20px;
  border: none;
  background: #ffb020;
  color: #402400;
  box-shadow: 0 5px 0 #c9860a;
  z-index: 6;
  animation: build-house-pulse 1.1s ease-in-out infinite;
}

#build-house-btn.visible {
  display: block;
}

#build-house-btn:active {
  transform: translateY(3px);
  box-shadow: 0 2px 0 #c9860a;
}

@keyframes build-house-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.06); }
}

#action-buttons {
  position: fixed;
  right: max(16px, env(safe-area-inset-right));
  bottom: max(16px, env(safe-area-inset-bottom));
  display: none;
  grid-template-columns: repeat(2, 1fr);
  gap: 14px;
  z-index: 6;
}

#action-buttons.visible {
  display: grid;
}

.action-btn {
  position: relative;
  width: 92px;
  height: 92px;
  border-radius: 24px;
  border: none;
  background: rgba(255, 255, 255, 0.85);
  box-shadow: 0 5px 0 rgba(0, 0, 0, 0.2);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 0;
  overflow: hidden;
}

.action-btn:active {
  transform: translateY(3px);
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.2);
}

.action-icon {
  font-size: 38px;
  line-height: 1;
}

.action-label {
  font-size: 13px;
  font-weight: 800;
  color: #234;
  margin-top: 4px;
}

.action-cd {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  font-size: 32px;
  font-weight: 900;
  color: #fff;
  background: rgba(20, 30, 40, 0.6);
  border-radius: 24px;
}

.action-btn.on-cooldown .action-cd {
  display: flex;
}

#combat-toast {
  position: fixed;
  top: 28%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.6);
  font-size: 26px;
  font-weight: 900;
  color: #fff;
  text-shadow: 0 3px 0 rgba(0, 0, 0, 0.4);
  background: rgba(20, 30, 40, 0.55);
  padding: 10px 26px;
  border-radius: 20px;
  opacity: 0;
  pointer-events: none;
  z-index: 7;
  white-space: nowrap;
}

#combat-toast.show {
  animation: toast-pop 1.1s ease forwards;
}

@keyframes toast-pop {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.6); }
  20% { opacity: 1; transform: translate(-50%, -50%) scale(1.1); }
  35% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  80% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -50%) scale(1); }
}

#gameover-overlay {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 20px;
  background: rgba(10, 15, 20, 0.92);
  color: #fff;
  z-index: 25;
}

#gameover-overlay.visible {
  display: flex;
}

#gameover-overlay h1 {
  font-size: 38px;
  letter-spacing: 2px;
  margin: 0 0 14px;
  text-shadow: 0 3px 0 rgba(0, 0, 0, 0.4);
}

#gameover-stats {
  font-size: 22px;
  font-weight: 700;
  line-height: 1.7;
  margin: 0 0 26px;
  opacity: 0.9;
  white-space: pre-line;
}

#restart-btn {
  font-size: 24px;
  font-weight: 800;
  padding: 18px 40px;
  border-radius: 36px;
  border: none;
  background: #3ddc4a;
  color: #06340c;
  box-shadow: 0 6px 0 #2a9e34;
}

#restart-btn:active {
  transform: translateY(3px);
  box-shadow: 0 3px 0 #2a9e34;
}

#help-btn {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  left: max(10px, env(safe-area-inset-left));
  height: 46px;
  padding: 0 18px;
  border-radius: 23px;
  border: none;
  background: rgba(255, 255, 255, 0.85);
  color: #234;
  font-size: 15px;
  font-weight: 900;
  white-space: nowrap;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.2);
  z-index: 31;
}

#help-btn:active {
  transform: translateY(2px);
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.2);
}

#pause-btn {
  position: fixed;
  top: max(66px, calc(env(safe-area-inset-top) + 56px));
  left: max(10px, env(safe-area-inset-left));
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.85);
  color: #234;
  font-size: 20px;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.2);
  z-index: 31;
  display: none;
}

#pause-btn.visible {
  display: block;
}

#pause-btn:active {
  transform: translateY(2px);
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.2);
}

#fullscreen-btn {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  right: max(10px, env(safe-area-inset-right));
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.85);
  color: #234;
  font-size: 20px;
  font-weight: 900;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.2);
  z-index: 31;
}

#fullscreen-btn:active {
  transform: translateY(2px);
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.2);
}

#help-overlay {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 12px;
  background: rgba(10, 15, 20, 0.92);
  z-index: 40;
}

#help-overlay.visible {
  display: flex;
}

#help-card {
  background: #12202c;
  color: #fff;
  border-radius: 24px;
  padding: 24px 32px;
  width: 96vw;
  max-width: 96vw;
  max-height: 90vh;
  overflow-y: auto;
  text-align: center;
  box-shadow: 0 10px 0 rgba(0, 0, 0, 0.3);
}

#help-card h2 {
  font-size: 28px;
  letter-spacing: 2px;
  margin: 0 0 16px;
}

#help-list {
  list-style: none;
  margin: 0 0 20px;
  padding: 0;
  text-align: left;
}

#help-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 18px;
  font-weight: 600;
  line-height: 1.4;
  padding: 10px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
}

#help-list li:last-child {
  border-bottom: none;
}

.help-icon {
  font-size: 24px;
  flex-shrink: 0;
  width: 32px;
  text-align: center;
}

#help-close-btn {
  font-size: 20px;
  font-weight: 800;
  padding: 14px 36px;
  border-radius: 30px;
  border: none;
  background: #3ddc4a;
  color: #06340c;
  box-shadow: 0 5px 0 #2a9e34;
}

#help-close-btn:active {
  transform: translateY(3px);
  box-shadow: 0 2px 0 #2a9e34;
}

#house-builder-overlay {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(120, 190, 230, 0.97);
  z-index: 40;
}

#house-builder-overlay.visible {
  display: flex;
}

#house-builder-card {
  text-align: center;
  color: #123;
  max-width: 520px;
  width: 100%;
}

#house-builder-card h2 {
  font-size: 24px;
  letter-spacing: 1px;
  margin: 0 0 8px;
  text-shadow: 0 2px 0 rgba(255, 255, 255, 0.5);
}

#builder-hint {
  font-size: 18px;
  font-weight: 700;
  margin: 0 0 14px;
}

#house-blueprint {
  position: relative;
  width: 260px;
  height: 210px;
  margin: 0 auto 18px;
}

.house-body {
  position: absolute;
  left: 30px;
  top: 90px;
  width: 200px;
  height: 110px;
  background: rgba(255, 255, 255, 0.5);
  border: 3px dashed rgba(20, 40, 60, 0.4);
  border-radius: 4px;
}

.drop-zone {
  position: absolute;
  border-radius: 6px;
  background: rgba(20, 40, 60, 0.12);
}

#zone-roof {
  left: 15px;
  top: 6px;
  width: 230px;
  height: 78px;
  clip-path: polygon(50% 0%, 4% 100%, 96% 100%);
}

#zone-chimney {
  right: 46px;
  top: 14px;
  width: 22px;
  height: 36px;
}

#zone-window {
  left: 52px;
  top: 108px;
  width: 40px;
  height: 40px;
}

#zone-door {
  left: 148px;
  top: 132px;
  width: 40px;
  height: 68px;
}

#piece-tray {
  display: flex;
  justify-content: center;
  align-items: flex-end;
  gap: 26px;
  min-height: 70px;
}

.drag-piece {
  touch-action: none;
  cursor: grab;
}

.drag-piece.dragging {
  position: fixed;
  z-index: 60;
  cursor: grabbing;
}

.drag-piece.placed {
  pointer-events: none;
}

.piece-roof {
  width: 92px;
  height: 44px;
  background: #b5482a;
  clip-path: polygon(50% 0%, 2% 100%, 98% 100%);
}

.piece-chimney {
  width: 22px;
  height: 36px;
  background: #7a7a7a;
  border-radius: 2px;
}

.piece-window {
  width: 40px;
  height: 40px;
  background: #6fc3ff;
  border: 4px solid #fff;
  box-sizing: border-box;
}

.piece-door {
  width: 40px;
  height: 68px;
  background: #6b3f1d;
  border-radius: 6px 6px 0 0;
}

#intro-overlay {
  position: fixed;
  inset: 0;
  background: #12202c;
  color: white;
  z-index: 20;
  overflow: hidden;
}

#intro-overlay.hidden {
  display: none;
}

.intro-stage {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 20px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.4s ease;
}

.intro-stage.active {
  opacity: 1;
  pointer-events: auto;
}

.skip-hint {
  position: absolute;
  bottom: max(16px, env(safe-area-inset-bottom));
  font-size: 15px;
  font-weight: 700;
  opacity: 0.5;
  margin: 0;
  letter-spacing: 1px;
}

/* ---- Stage 0: start screen ---- */
#stage-start {
  gap: 28px;
}

.start-title {
  font-size: 52px;
  font-weight: 900;
  margin: 0;
  letter-spacing: 4px;
  color: #ffd400;
  text-shadow: 0 6px 0 rgba(0,0,0,0.4);
}

.start-sub {
  font-size: 26px;
  font-weight: 700;
  margin: 0;
  opacity: 0.85;
}

#start-btn {
  width: 260px;
  height: 90px;
  border-radius: 45px;
  border: none;
  background: #2ecc40;
  color: #06340c;
  font-size: 36px;
  font-weight: 900;
  box-shadow: 0 8px 0 #1e8f2c;
  animation: say67-idle 1.4s ease-in-out infinite;
}

.fs-arrow-hint {
  position: absolute;
  top: 64px;
  right: max(12px, env(safe-area-inset-right, 0px));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  color: #ffd400;
  animation: fs-hint-bounce 0.7s ease-in-out infinite alternate;
  pointer-events: none;
}

.fs-arrow-text {
  font-size: 13px;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  text-shadow: 0 1px 4px rgba(0,0,0,0.6);
}

.fs-arrow-icon {
  font-size: 26px;
  line-height: 1;
}

@keyframes fs-hint-bounce {
  from { transform: translateY(0); }
  to   { transform: translateY(-8px); }
}

.start-hint {
  font-size: 16px;
  font-weight: 600;
  opacity: 0.7;
  margin: 0;
  max-width: 340px;
  text-align: center;
  line-height: 1.5;
}

.start-hint strong {
  font-size: 20px;
  opacity: 1;
}

#start-btn:active {
  transform: translateY(4px);
  box-shadow: 0 4px 0 #1e8f2c;
  animation: none;
}

/* ---- Stage 1: chase scene ---- */
.chase-track {
  position: relative;
  width: 100%;
  height: 90px;
}

.muncher,
.ghost {
  position: absolute;
  top: 50%;
  width: 46px;
  height: 46px;
  transform: translateY(-50%) translateX(-80px);
  animation: chase-move 2.6s linear infinite;
}

.muncher {
  border-radius: 50%;
  background: #ffd400;
  z-index: 3;
  animation: chase-move 2.6s linear 0.36s infinite, chomp 0.3s ease-in-out infinite;
}

.ghost {
  border-radius: 20px 20px 0 0;
  z-index: 2;
}

.ghost-red {
  background: #ff4136;
  animation-delay: 0s;
}

.ghost-blue {
  background: #0074d9;
  animation-delay: 0.18s;
  z-index: 1;
}

.ghost::after {
  content: '';
  position: absolute;
  bottom: -7px;
  left: 0;
  width: 100%;
  height: 7px;
  background: inherit;
  clip-path: polygon(0 0, 100% 0, 100% 100%, 83% 25%, 66% 100%, 50% 25%, 33% 100%, 16% 25%, 0 100%);
}

.ghost .eye {
  position: absolute;
  top: 14px;
  width: 8px;
  height: 8px;
  background: white;
  border-radius: 50%;
}

.ghost .eye:first-child { left: 10px; }
.ghost .eye:last-child { left: 26px; }

@keyframes chase-move {
  0% { transform: translateY(-50%) translateX(-80px); }
  100% { transform: translateY(-50%) translateX(calc(100vw + 80px)); }
}

@keyframes chomp {
  0%, 100% { clip-path: polygon(50% 50%, 100% 8%, 100% 0%, 0% 0%, 0% 100%, 100% 100%, 100% 92%); }
  50% { clip-path: polygon(50% 50%, 100% 46%, 100% 0%, 0% 0%, 0% 100%, 100% 100%, 100% 54%); }
}

/* ---- Stage 2: lore text ---- */
#stage-lore {
  gap: 22px;
}

.lore-full {
  font-size: 30px;
  font-weight: 800;
  max-width: 680px;
  margin: 0;
  text-shadow: 0 3px 0 rgba(0, 0, 0, 0.25);
}

.lore-full strong {
  color: #ffd400;
  font-size: 1.15em;
}

.lore-instruction {
  font-size: 22px;
  font-weight: 700;
  max-width: 560px;
  margin: 0;
  opacity: 0.9;
}

.lore-instruction strong {
  color: #ffd400;
}

#say67-btn {
  width: 150px;
  height: 150px;
  border-radius: 50%;
  border: none;
  background: #ffd400;
  color: #12202c;
  font-size: 58px;
  font-weight: 900;
  box-shadow: 0 8px 0 #c9a400;
  animation: say67-idle 1.4s ease-in-out infinite;
}

#say67-btn:active {
  transform: translateY(4px) scale(0.96);
  box-shadow: 0 4px 0 #c9a400;
  animation: none;
}

@keyframes say67-idle {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.06); }
}

/* ---- Stage 3: cinematic summon (the 67 gets ready to chase you) ---- */
#stage-title {
  background: radial-gradient(circle at 50% 55%, #1c2f3d 0%, #0a1218 75%);
  overflow: hidden;
}

.cine-green {
  position: relative;
  opacity: 0;
  transform: scale(0.25) translateY(30px);
}

.cine-green canvas {
  display: block;
}

.cine-glow-67 {
  position: absolute;
  top: -6px;
  right: -34px;
  font-size: 34px;
  font-weight: 900;
  color: #2ecfff;
  text-shadow: 0 0 12px rgba(46, 207, 255, 0.9);
}

#stage-title.active .cine-green {
  animation: cine-pop 0.7s cubic-bezier(0.34, 1.56, 0.64, 1) forwards,
             cine-glow-pulse 1s ease-in-out 0.7s infinite;
}

@keyframes cine-pop {
  0% {
    opacity: 0;
    transform: scale(0.2) translateY(30px);
    filter: drop-shadow(0 0 0 rgba(255, 40, 40, 0));
  }
  60% {
    opacity: 1;
    transform: scale(1.3) translateY(-12px);
    filter: drop-shadow(0 0 20px rgba(255, 40, 40, 0.9));
  }
  100% {
    opacity: 1;
    transform: scale(1) translateY(0);
    filter: drop-shadow(0 0 10px rgba(255, 40, 40, 0.6));
  }
}

@keyframes cine-glow-pulse {
  0%, 100% { filter: drop-shadow(0 0 10px rgba(255, 40, 40, 0.5)); }
  50% { filter: drop-shadow(0 0 22px rgba(255, 40, 40, 0.9)); }
}

.cine-line {
  font-size: 32px;
  font-weight: 900;
  margin: 10px 0 0;
  opacity: 0;
  transform: scale(0.4);
  text-shadow: 0 3px 0 rgba(0, 0, 0, 0.4);
}

.cine-line.show {
  animation: cine-text-pop 0.4s ease forwards;
}

.cine-line-run {
  color: #ff4136;
  font-size: 44px;
}

.cine-line-run.show {
  animation: cine-text-pop 0.4s ease forwards, cine-shake 0.4s ease 0.4s;
}

@keyframes cine-text-pop {
  0% { opacity: 0; transform: scale(0.3); }
  60% { opacity: 1; transform: scale(1.2); }
  100% { opacity: 1; transform: scale(1); }
}

@keyframes cine-shake {
  0%, 100% { transform: translate(0, 0) scale(1); }
  20% { transform: translate(-6px, 3px) scale(1); }
  40% { transform: translate(5px, -4px) scale(1); }
  60% { transform: translate(-5px, 4px) scale(1); }
  80% { transform: translate(6px, -2px) scale(1); }
}

.flash-overlay {
  position: absolute;
  inset: 0;
  background: white;
  opacity: 0;
  pointer-events: none;
}

.flash-overlay.flash {
  animation: flash-pop 0.35s ease-out;
}

@keyframes flash-pop {
  0% { opacity: 0; }
  15% { opacity: 1; }
  100% { opacity: 0; }
}

#portrait-overlay {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #111;
  color: white;
  z-index: 30;
  text-align: center;
  padding: 20px;
}

.rotate-icon {
  font-size: 60px;
  margin-bottom: 16px;
  animation: spin 1.6s ease-in-out infinite;
}

@keyframes spin {
  0%, 100% { transform: rotate(0deg); }
  50% { transform: rotate(90deg); }
}

@media (orientation: portrait) {
  #portrait-overlay {
    display: flex;
  }
}
