/* =====================================================================
   Castle Quest 3D — First-Person Raycaster
   Single stylesheet. Match the cosmic look-and-feel from v2 + the index.
   ===================================================================== */

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
  width: 100%; height: 100%; overflow: hidden;
  background: #0f0a1f;
}
body {
  font-family: 'Fredoka', system-ui, sans-serif;
  color: #fff;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
  overscroll-behavior: none;
}

#game-container {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(ellipse at top, #3b2466 0%, transparent 60%),
    radial-gradient(ellipse at bottom, #1e1438 0%, transparent 60%),
    #0f0a1f;
}
#stage {
  position: relative;
  width: min(100vw, 100vh * 16 / 10);
  aspect-ratio: 16 / 10;
  max-height: 100vh;
  border-radius: 16px;
  overflow: hidden;
  box-shadow:
    0 30px 80px rgba(0,0,0,0.6),
    0 0 0 4px rgba(255,255,255,0.05),
    inset 0 0 0 2px rgba(255,255,255,0.08);
  background: #0a0820;
}

canvas#view {
  display: block;
  width: 100%;
  height: 100%;
  background: #0a0820;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

/* =================== HUD =================== */

.hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 5;
}

.hud-top {
  position: absolute;
  top: 12px; left: 12px; right: 12px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  flex-wrap: wrap;
}

.hearts {
  display: flex;
  gap: 4px;
  padding: 8px 10px;
  background: rgba(15, 10, 31, 0.55);
  border-radius: 999px;
  backdrop-filter: blur(8px);
}
.heart {
  width: 18px; height: 18px;
  background: #ff4d6d;
  clip-path: path('M9 16 L1.5 9 A4.5 4.5 0 1 1 9 4 A4.5 4.5 0 1 1 16.5 9 Z');
  transition: transform 0.2s;
}
.heart.empty { background: #444a5e; }
.heart.pulse { animation: heartPulse 0.4s ease; }
@keyframes heartPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.35); }
}

.tracker {
  background: rgba(15, 10, 31, 0.55);
  padding: 8px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
  backdrop-filter: blur(8px);
  text-align: right;
  max-width: 280px;
  line-height: 1.5;
}

/* Character switcher — pill row of available characters */
.char-switcher {
  display: flex;
  gap: 6px;
  padding: 5px 7px;
  background: rgba(15, 10, 31, 0.55);
  border-radius: 999px;
  backdrop-filter: blur(8px);
  pointer-events: auto;
  flex-wrap: wrap;
  justify-content: center;
  max-width: 60vw;
}
.char-chip {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255,255,255,0.07);
  border: 2px solid rgba(255,255,255,0.18);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  cursor: pointer;
  position: relative;
  user-select: none;
  transition: transform 0.15s, border-color 0.2s, background 0.2s;
}
@media (max-width: 540px) {
  .char-chip { width: 34px; height: 34px; font-size: 17px; }
  .char-switcher { gap: 4px; padding: 4px 6px; }
}
.char-chip.locked {
  opacity: 0.4;
  filter: grayscale(0.6);
  cursor: not-allowed;
}
.char-chip.active {
  border-color: var(--chip-color, #ffe066);
  background: rgba(255, 224, 102, 0.18);
  box-shadow: 0 0 18px var(--chip-color, rgba(255,224,102,0.4));
  transform: translateY(-2px);
}
.char-chip .key {
  position: absolute;
  bottom: -4px;
  right: -4px;
  font-size: 9px;
  font-weight: 700;
  background: rgba(0,0,0,0.7);
  border: 1px solid rgba(255,255,255,0.3);
  padding: 1px 4px;
  border-radius: 999px;
  font-family: 'Fredoka', sans-serif;
}
.char-chip.locked .key { display: none; }
.tracker .boss {
  color: #ff4d6d;
  font-weight: 700;
  font-size: 13px;
  margin-top: 4px;
  display: none;
  animation: bossPulse 1s ease infinite;
}
.tracker .boss.show { display: block; }
@keyframes bossPulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

/* Character face / weapon — always visible at the bottom of the view.
   The snout/face SVG sits in the bottom of the container and stays on
   screen so you can tell who you're playing as. On attack, the per-character
   ATTACK_SVG swaps in for ~320ms (handled in JS) and a swing animation
   plays. After the animation, we revert to the rest face. */
.weapon {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 260px;
  height: 200px;
  pointer-events: none;
  filter: drop-shadow(0 -6px 12px rgba(0,0,0,0.5));
}
.weapon svg { width: 100%; height: 100%; display: block; }
.weapon.swing {
  animation: weaponSwing 0.32s ease-out;
}
@keyframes weaponSwing {
  0%   { transform: translateX(-50%) translateY(15px) rotate(-10deg); }
  30%  { transform: translateX(-50%) translateY(-22px) rotate(8deg); }
  100% { transform: translateX(-50%) translateY(0) rotate(0deg); }
}

/* Mobile: a touch smaller so the snout doesn't dominate small screens */
@media (max-height: 540px) {
  .weapon { width: 210px; height: 160px; }
}

/* Tiny crosshair */
.crosshair {
  position: absolute;
  top: 50%; left: 50%;
  width: 14px; height: 14px;
  margin: -7px 0 0 -7px;
  border: 2px solid rgba(255,255,255,0.55);
  border-radius: 50%;
  box-shadow:
    inset 0 0 4px rgba(255,255,255,0.4),
    0 0 4px rgba(0,0,0,0.5);
}
.crosshair::before, .crosshair::after {
  content: '';
  position: absolute;
  background: rgba(255,255,255,0.6);
}
.crosshair::before { left: 50%; top: -4px; width: 2px; height: 4px; margin-left: -1px; }
.crosshair::after  { left: 50%; bottom: -4px; width: 2px; height: 4px; margin-left: -1px; }

/* =================== Touch controls =================== */

.touch {
  position: absolute;
  inset: 0;
  pointer-events: none;
  display: none;
  z-index: 6;
}
.touch.show { display: block; }

.joystick {
  position: absolute;
  bottom: 24px; left: 24px;
  width: 130px; height: 130px;
  pointer-events: auto;
  touch-action: none;
}
.joystick-base {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: rgba(255,255,255,0.10);
  border: 2px solid rgba(255,255,255,0.28);
  backdrop-filter: blur(8px);
}
.joystick-base::after {
  content: '';
  position: absolute;
  inset: 18px;
  border-radius: 50%;
  border: 1px dashed rgba(255,255,255,0.22);
}
.joystick-knob {
  position: absolute;
  width: 54px; height: 54px;
  top: 50%; left: 50%;
  margin: -27px 0 0 -27px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, rgba(255,255,255,0.55), rgba(255,255,255,0.3) 60%, rgba(255,255,255,0.18));
  border: 2px solid rgba(255,255,255,0.55);
  box-shadow: 0 4px 14px rgba(0,0,0,0.35);
  pointer-events: none;
  transition: transform 0.18s cubic-bezier(.34,1.56,.64,1);
}
.joystick.active .joystick-knob { transition: none; }

.lookpad {
  position: absolute;
  top: 0; right: 0;
  width: 55%; height: 100%;
  pointer-events: auto;
  touch-action: none;
  display: flex;
  align-items: center;
  justify-content: center;
}
.lookpad-hint {
  font-size: 11px;
  opacity: 0.25;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  pointer-events: none;
  text-shadow: 0 1px 2px rgba(0,0,0,0.8);
}

.swat-btn {
  position: absolute;
  bottom: 30px; right: 24px;
  width: 90px; height: 90px;
  border-radius: 50%;
  border: 3px solid rgba(255,255,255,0.45);
  background: radial-gradient(circle at 30% 30%, rgba(255,224,102,0.65), rgba(255,140,66,0.45));
  color: #fff;
  font-family: 'Bowlby One', sans-serif;
  font-size: 22px;
  display: flex; align-items: center; justify-content: center;
  flex-direction: column;
  line-height: 1.1;
  box-shadow: 0 6px 18px rgba(0,0,0,0.4);
  pointer-events: auto;
  cursor: pointer;
  user-select: none;
}
.swat-btn span { font-size: 11px; opacity: 0.9; letter-spacing: 0.06em; margin-top: 2px; }
.swat-btn:active { transform: scale(0.92); }

/* =================== Toast =================== */

.toast {
  position: absolute;
  top: 18%;
  left: 50%;
  transform: translateX(-50%);
  padding: 10px 18px;
  background: rgba(15, 10, 31, 0.85);
  border: 1px solid rgba(255,255,255,0.18);
  border-radius: 12px;
  font-weight: 600;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.3s, transform 0.3s;
  white-space: nowrap;
  backdrop-filter: blur(10px);
  z-index: 10;
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* =================== Overlays =================== */

.overlay {
  position: absolute;
  inset: 0;
  background: rgba(15, 10, 31, 0.94);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 20;
  padding: 24px;
  padding-top: max(24px, env(safe-area-inset-top));
  padding-bottom: max(24px, env(safe-area-inset-bottom));
  pointer-events: auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.overlay.show { display: flex; }
.overlay-card {
  max-width: 600px;
  width: 100%;
  text-align: center;
}
.title {
  font-family: 'Bowlby One', sans-serif;
  font-size: clamp(2.4rem, 8vw, 4rem);
  line-height: 1;
  letter-spacing: -0.02em;
  background: linear-gradient(180deg, #ffe066 0%, #ff8c42 60%, #ff4d6d 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-bottom: 8px;
  text-shadow: 0 8px 30px rgba(255, 140, 66, 0.3);
}
.title.small { font-size: clamp(2rem, 6vw, 3rem); }
.subtitle {
  font-size: clamp(0.9rem, 2vw, 1.05rem);
  opacity: 0.75;
  margin-bottom: 24px;
  font-weight: 500;
  letter-spacing: 0.05em;
}
.blurb {
  font-size: 1rem;
  margin-bottom: 22px;
  opacity: 0.9;
  line-height: 1.55;
}
.blurb em {
  color: #ffe066;
  font-style: normal;
  font-weight: 600;
}

.big-btn {
  font-family: 'Bowlby One', sans-serif;
  font-size: 18px;
  padding: 14px 32px;
  border-radius: 999px;
  background: linear-gradient(180deg, #ffe066 0%, #ff8c42 100%);
  color: #1a0f30;
  border: none;
  cursor: pointer;
  box-shadow: 0 10px 30px rgba(255, 140, 66, 0.4),
              inset 0 -3px 0 rgba(0,0,0,0.2);
  transition: transform 0.15s, box-shadow 0.15s;
  letter-spacing: 0.03em;
}
.big-btn:hover { transform: translateY(-2px); box-shadow: 0 14px 32px rgba(255, 140, 66, 0.55); }
.big-btn:active { transform: translateY(0); }

.help-text {
  margin-top: 18px;
  font-size: 12px;
  opacity: 0.65;
  line-height: 1.6;
}
.kbd {
  background: rgba(255,255,255,0.12);
  border: 1px solid rgba(255,255,255,0.2);
  padding: 2px 8px;
  border-radius: 6px;
  font-family: 'Fredoka', monospace;
  font-size: 11px;
  margin: 0 2px;
}

/* Show touch controls / hide desktop help on touch devices */
@media (hover: none) and (pointer: coarse) {
  .touch { display: block; }
  .help-text.desktop-help { display: none; }
  .help-text.touch-help { display: block; }
}
.help-text.touch-help { display: none; }

/* Compact overlay for short / narrow screens */
@media (max-height: 720px), (max-width: 540px) {
  .overlay.show { align-items: flex-start; }
  .title { font-size: clamp(1.9rem, 7vw, 2.6rem); margin-bottom: 4px; }
  .subtitle { margin-bottom: 14px; font-size: 0.85rem; }
  .blurb { font-size: 0.92rem; margin-bottom: 16px; }
  .tracker { font-size: 11px; max-width: 200px; }
  .help-text { font-size: 10px; }
}

/* =================== Intro cinematic =================== */
.overlay.intro {
  background: linear-gradient(180deg, #1a0a2e 0%, #4a2a68 55%, #7a4a3a 100%);
  padding: 0;
  overflow: hidden;
}
.intro-stage {
  position: relative;
  width: 100%; height: 100%;
  overflow: hidden;
}
.intro-stage::before {
  content: '';
  position: absolute;
  inset: 0 0 50% 0;
  background-image:
    radial-gradient(1px 1px at 12% 25%, #fff, transparent),
    radial-gradient(2px 2px at 32% 55%, #fff, transparent),
    radial-gradient(1px 1px at 55% 18%, #fff, transparent),
    radial-gradient(2px 2px at 72% 42%, #ffe066, transparent),
    radial-gradient(1px 1px at 88% 28%, #fff, transparent),
    radial-gradient(1px 1px at 22% 70%, #fff, transparent);
  opacity: 0.35;
  pointer-events: none;
}
.intro-stage::after {
  content: '';
  position: absolute;
  bottom: 0; left: 0; right: 0;
  height: 28%;
  background:
    linear-gradient(180deg, transparent 0%, rgba(20,8,40,0.45) 60%, #1a0a2e 100%);
  pointer-events: none;
}
.intro-animals {
  position: absolute;
  bottom: 28%; left: 0; right: 0;
  display: flex; justify-content: space-around; align-items: flex-end;
  padding: 0 6%;
  z-index: 4;
}
.intro-animal {
  font-size: clamp(36px, 7vw, 64px);
  display: inline-block;
  animation: introDance 0.65s ease-in-out infinite alternate;
  line-height: 1;
}
.intro-animal.i1 { animation-delay: 0s; }
.intro-animal.i2 { animation-delay: 0.10s; }
.intro-animal.i3 { animation-delay: 0.18s; }
.intro-animal.i4 { animation-delay: 0.05s; }
.intro-animal.i5 { animation-delay: 0.22s; }
.intro-animal.i6 { animation-delay: 0.12s; }
@keyframes introDance {
  0%   { transform: translateY(0) rotate(-8deg); }
  100% { transform: translateY(-14px) rotate(8deg); }
}
.intro-stage.phase-scatter .intro-animal {
  animation: introScatter 1.4s forwards cubic-bezier(.36,.07,.19,.97);
}
.intro-stage.phase-scatter .intro-animal.i1 { --dx: -55vw; --dy: -120vh; --rot: -540deg; }
.intro-stage.phase-scatter .intro-animal.i2 { --dx: -28vw; --dy: -135vh; --rot: 720deg; }
.intro-stage.phase-scatter .intro-animal.i3 { --dx:  -8vw; --dy: -150vh; --rot: -360deg; }
.intro-stage.phase-scatter .intro-animal.i4 { --dx:  12vw; --dy: -140vh; --rot: 540deg; }
.intro-stage.phase-scatter .intro-animal.i5 { --dx:  36vw; --dy: -120vh; --rot: -720deg; }
.intro-stage.phase-scatter .intro-animal.i6 { --dx:  55vw; --dy: -130vh; --rot: 450deg; }
@keyframes introScatter {
  to {
    transform: translate(var(--dx), var(--dy)) rotate(var(--rot)) scale(0.4);
    opacity: 0.05;
  }
}
.intro-wolf {
  position: absolute;
  bottom: 22%; left: -25%;
  font-size: clamp(90px, 16vw, 160px);
  z-index: 5;
  opacity: 0;
  line-height: 1;
  filter: drop-shadow(0 6px 14px rgba(0,0,0,0.6));
}
.intro-stage.phase-wolf-enter .intro-wolf {
  animation: introWolfEnter 1.1s forwards ease-out;
}
.intro-stage.phase-wolf-blow .intro-wolf {
  animation: introWolfBlow 0.7s ease-in-out;
}
@keyframes introWolfEnter {
  0%   { left: -25%; opacity: 0; transform: scale(0.55); }
  100% { left: 12%;  opacity: 1; transform: scale(1); }
}
@keyframes introWolfBlow {
  0%, 100% { transform: scale(1); }
  35%      { transform: scale(1.5) rotate(-6deg); }
  65%      { transform: scale(0.9) rotate(4deg); }
}
.intro-puff {
  position: absolute;
  bottom: 30%; left: 22%; right: 0;
  height: 80px;
  background: linear-gradient(90deg,
    rgba(255,255,255,0.0) 0%,
    rgba(255,255,255,0.6) 15%,
    rgba(255,255,255,0.2) 45%,
    transparent 100%);
  transform: scaleX(0);
  transform-origin: left center;
  opacity: 0;
  z-index: 3;
  border-radius: 50%;
  filter: blur(6px);
}
.intro-stage.phase-wolf-blow .intro-puff {
  animation: introPuff 0.9s forwards ease-out;
}
@keyframes introPuff {
  0%   { transform: scaleX(0); opacity: 0; }
  25%  { transform: scaleX(1); opacity: 0.95; }
  100% { transform: scaleX(1.4); opacity: 0; }
}
.intro-text {
  position: absolute;
  top: 30%; left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  opacity: 0;
  max-width: 86%;
  z-index: 6;
  pointer-events: none;
}
.intro-text h2 {
  font-family: 'Bowlby One', sans-serif;
  font-size: clamp(20px, 4.5vw, 30px);
  background: linear-gradient(180deg, #ffe066 0%, #ff8c42 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-bottom: 14px;
  line-height: 1.15;
  text-shadow: 0 4px 20px rgba(255,140,66,0.35);
}
.intro-text p {
  font-size: clamp(13px, 2.5vw, 15px);
  color: rgba(255,255,255,0.92);
  line-height: 1.55;
  margin: 0 auto;
  max-width: 36em;
}
.intro-stage.phase-text .intro-text {
  animation: introTextFade 0.7s forwards;
}
@keyframes introTextFade { to { opacity: 1; } }
.intro-skip {
  position: absolute;
  top: max(16px, env(safe-area-inset-top));
  right: 16px;
  padding: 8px 16px;
  background: rgba(255,255,255,0.12);
  border: 1px solid rgba(255,255,255,0.3);
  border-radius: 999px;
  color: #fff;
  font-family: 'Fredoka', sans-serif;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  z-index: 10;
  backdrop-filter: blur(6px);
}
.intro-skip:hover { background: rgba(255,255,255,0.22); }
.intro-continue {
  position: absolute;
  bottom: 8%; left: 50%;
  transform: translateX(-50%);
  opacity: 0;
  pointer-events: none;
  z-index: 8;
}
.intro-stage.phase-text .intro-continue {
  animation: introTextFade 0.7s 0.5s forwards;
  pointer-events: auto;
}

/* =================== Victory dance background =================== */
.victory-dance {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 0;
}
.victory-dance .vd-animal {
  position: absolute;
  font-size: clamp(36px, 8vw, 70px);
  line-height: 1;
  opacity: 0.85;
  animation: vdBob 0.7s ease-in-out infinite alternate, vdDrift 14s linear infinite;
  filter: drop-shadow(0 6px 14px rgba(0,0,0,0.45));
}
.victory-dance .vd-animal.v1 { left: 8%;  top: 25%; animation-delay: 0s,    0s; }
.victory-dance .vd-animal.v2 { left: 26%; top: 60%; animation-delay: 0.15s, -3s; }
.victory-dance .vd-animal.v3 { left: 44%; top: 18%; animation-delay: 0.10s, -6s; }
.victory-dance .vd-animal.v4 { left: 60%; top: 55%; animation-delay: 0.20s, -9s; }
.victory-dance .vd-animal.v5 { left: 76%; top: 28%; animation-delay: 0.05s, -2s; }
.victory-dance .vd-animal.v6 { left: 90%; top: 65%; animation-delay: 0.25s, -5s; }
@keyframes vdBob {
  0%   { transform: translateY(0) rotate(-8deg); }
  100% { transform: translateY(-12px) rotate(8deg); }
}
@keyframes vdDrift {
  0%   { margin-left: 0;   margin-top: 0;   }
  50%  { margin-left: 20px; margin-top: 8px; }
  100% { margin-left: 0;   margin-top: 0;   }
}
.victory-dance::before {
  content: '';
  position: absolute;
  inset: 0;
  background-image:
    radial-gradient(2px 2px at 14% 24%, #ffe066, transparent),
    radial-gradient(2px 2px at 38% 60%, #ff8ab0, transparent),
    radial-gradient(1px 1px at 64% 16%, #fff, transparent),
    radial-gradient(2px 2px at 82% 70%, #b8e0ee, transparent),
    radial-gradient(1px 1px at 50% 80%, #ffe066, transparent),
    radial-gradient(2px 2px at 22% 88%, #fff, transparent);
  opacity: 0.6;
  animation: vdTwinkle 3s ease-in-out infinite alternate;
}
@keyframes vdTwinkle {
  from { opacity: 0.25; }
  to   { opacity: 0.85; }
}
