:root {
  --indigo: #1B1740;
  --indigo-deep: #121033;
  --violet: #3A2E7A;
  --amber: #FFB347;
  --gold: #FFD86B;
  --cream: #FFF1CF;
  --parchment: #F6E8CA;
  --brass: #8E6528;
  --ink: #1C173C;
  --danger: #E8553D;
}

* {
  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: 100dvh;
  overflow: hidden;
  background: var(--indigo);
  color: var(--cream);
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  touch-action: none;
}

#game-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100dvh;
  display: block;
}

.hidden { display: none !important; }

.screen {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
  z-index: 10;
  overflow-y: auto;
  padding: env(safe-area-inset-top, 12px) env(safe-area-inset-right, 16px) env(safe-area-inset-bottom, 16px) env(safe-area-inset-left, 16px);
}
.screen.active { display: flex; }

/* Title & Ending */
.title-bg {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  z-index: -2;
}
.title-scrim {
  position: fixed;
  inset: 0;
  background: radial-gradient(circle at 50% 45%, rgba(27,23,64,0.28), rgba(14,11,34,0.78));
  z-index: -1;
}
.title-content, .ending-content {
  margin: auto;
  text-align: center;
  max-width: 680px;
  padding: 16px;
}
.title-logo {
  width: min(86vw, 540px);
  height: auto;
  filter: drop-shadow(0 8px 24px rgba(0,0,0,0.55));
}
.title-tagline {
  font-size: clamp(1rem, 2.5vw, 1.3rem);
  color: var(--cream);
  margin: 10px 0 24px;
  text-shadow: 0 2px 10px rgba(0,0,0,0.8);
}
.controls-hint {
  margin-top: 18px;
  font-size: 0.88rem;
  opacity: 0.85;
  text-shadow: 0 1px 6px rgba(0,0,0,0.9);
}

.btn-stack {
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
}

.wax-btn {
  background: linear-gradient(180deg, #FDF2D6, #E7CFA0);
  color: var(--ink);
  border: 3px solid var(--brass);
  border-radius: 14px;
  padding: 12px 26px;
  font-size: 1.05rem;
  font-weight: 700;
  min-width: 220px;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(0,0,0,0.45);
  transition: transform 0.1s ease, filter 0.12s ease;
}
.wax-btn:hover { filter: brightness(1.06); transform: translateY(-1px); }
.wax-btn:active { transform: translateY(1px) scale(0.98); }
.wax-btn.primary {
  background: linear-gradient(180deg, #FFE285, #F4A736);
  border-color: #6B4411;
}
.wax-btn:disabled { opacity: 0.45; cursor: not-allowed; }

/* Hearth Hub */
#screen-hearth, #screen-valley, #screen-results {
  background: radial-gradient(circle at 50% 25%, #2D245E, #141030);
}
.hearth-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 1020px;
  width: 100%;
  margin: 8px auto 14px;
  gap: 12px;
}
.hearth-header h2, .section-title {
  margin: 0;
  color: var(--gold);
  letter-spacing: 0.04em;
  font-size: clamp(1.2rem, 3vw, 1.8rem);
}
.sub-badge { font-size: 0.92rem; color: var(--cream); opacity: 0.85; }
.ember-pill {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(255, 179, 71, 0.16);
  border: 2px solid var(--amber);
  border-radius: 999px;
  padding: 6px 16px;
  font-weight: 700;
  color: var(--gold);
}
.ember-pill img { width: 24px; height: 24px; object-fit: contain; }

.hearth-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 14px;
  max-width: 1020px;
  width: 100%;
  margin: auto;
}
.hearth-card {
  background: rgba(246, 232, 202, 0.95);
  color: var(--ink);
  border: 3px solid var(--brass);
  border-radius: 16px;
  padding: 14px 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(0,0,0,0.35);
  transition: transform 0.12s ease;
}
.hearth-card:hover, .hearth-card:active { transform: translateY(-2px); }
.hearth-card img { width: 78px; height: 78px; object-fit: contain; }
.hearth-card strong { font-size: 0.98rem; text-align: center; }
.hearth-card span { font-size: 0.8rem; color: #4A3E68; text-align: center; }

.hearth-footer {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 12px;
  margin: 14px auto 8px;
}

/* Valley Map */
.valley-wrap {
  max-width: 1080px;
  width: 100%;
  margin: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.section-title { text-align: center; }
.beacons-strip {
  display: flex;
  gap: 10px;
  overflow-x: auto;
  padding: 6px 4px;
  justify-content: safe center;
}
.beacon-tile {
  flex: 0 0 112px;
  background: rgba(255,255,255,0.06);
  border: 2px solid rgba(255,216,107,0.28);
  border-radius: 14px;
  padding: 8px 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  color: var(--cream);
  cursor: pointer;
}
.beacon-tile.selected {
  border-color: var(--gold);
  background: rgba(255,179,71,0.22);
}
.beacon-tile.locked { opacity: 0.4; cursor: not-allowed; }
.beacon-tile img { width: 64px; height: 78px; object-fit: contain; }
.beacon-tile span { font-size: 0.82rem; font-weight: 700; }

.valley-panels {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(290px, 1fr));
  gap: 14px;
}
.parchment-panel {
  background: var(--parchment);
  color: var(--ink);
  border: 4px solid var(--brass);
  border-radius: 16px;
  padding: 16px 20px;
  box-shadow: 0 10px 28px rgba(0,0,0,0.45);
}
.parchment-panel h3 { margin: 0 0 8px; color: var(--indigo-deep); }
.meta-text { margin: 0 0 10px; line-height: 1.5; font-size: 0.96rem; }
.small-note { margin: 0; font-size: 0.84rem; color: #4A3E68; }
.pacts-list { display: flex; flex-direction: column; gap: 6px; }
.pact-row { display: flex; align-items: center; gap: 8px; font-size: 0.88rem; cursor: pointer; }
.mult-lbl { margin: 10px 0 0; font-weight: 700; color: #8A4B08; }

/* Gameplay HUD */
#hud {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 20;
  padding: env(safe-area-inset-top, 10px) env(safe-area-inset-right, 12px) env(safe-area-inset-bottom, 10px) env(safe-area-inset-left, 12px);
}
.hud-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
}
.hud-flame {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(18, 16, 51, 0.62);
  padding: 6px 12px 6px 8px;
  border-radius: 14px;
  border: 1.5px solid rgba(255, 216, 107, 0.35);
}
.bulb-wrap { position: relative; width: 38px; height: 48px; }
.bulb-frame { width: 100%; height: 100%; object-fit: contain; }
.bulb-fill {
  position: absolute;
  left: 9px; bottom: 9px;
  width: 20px; height: 24px;
  object-fit: contain;
  transform-origin: bottom center;
}
.hud-big { font-size: clamp(0.95rem, 2.4vw, 1.35rem); font-weight: 800; color: var(--cream); text-shadow: 0 2px 6px rgba(0,0,0,0.8); }
.hud-sub { font-size: clamp(0.72rem, 1.8vw, 0.9rem); color: var(--gold); text-shadow: 0 1px 4px rgba(0,0,0,0.8); }
.hud-clock { text-align: center; }
.boss-box { margin-top: 6px; width: min(62vw, 340px); margin-inline: auto; }
.boss-name { font-size: 0.82rem; font-weight: 700; color: #FFB4A2; margin-bottom: 3px; }
.boss-track { height: 12px; background: rgba(18,16,51,0.85); border: 1.5px solid var(--danger); border-radius: 99px; overflow: hidden; }
.boss-fill { height: 100%; width: 100%; background: linear-gradient(90deg, #E8553D, #FFB347); }

.icon-btn {
  pointer-events: auto;
  width: 46px; height: 46px;
  background: rgba(18,16,51,0.72);
  border: 2px solid var(--amber);
  border-radius: 12px;
  padding: 6px;
  cursor: pointer;
}
.icon-btn img { width: 100%; height: 100%; object-fit: contain; }

.hud-bottom {
  position: fixed;
  left: 12px; right: 12px;
  bottom: max(10px, env(safe-area-inset-bottom, 10px));
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.hud-upgrades { display: flex; flex-wrap: wrap; gap: 6px; }
.up-badge {
  display: flex; align-items: center; gap: 4px;
  background: rgba(18,16,51,0.72);
  border: 1.5px solid rgba(255,216,107,0.45);
  border-radius: 8px;
  padding: 2px 6px 2px 3px;
  font-size: 0.72rem; font-weight: 700; color: var(--gold);
}
.up-badge img { width: 22px; height: 22px; object-fit: contain; }
.xp-row { display: flex; align-items: center; gap: 8px; }
.xp-lbl { font-weight: 800; font-size: 0.88rem; color: var(--gold); min-width: 48px; text-shadow: 0 1px 4px #000; }
.xp-track { flex: 1; height: 12px; background: rgba(18,16,51,0.8); border: 1.5px solid var(--brass); border-radius: 99px; overflow: hidden; }
.xp-fill { height: 100%; width: 0%; background: linear-gradient(90deg, #FFB347, #FFF1CF); transition: width 0.12s linear; }

/* Mobile Touch Controls */
.touch-controls {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 25;
}
.joystick-zone {
  position: absolute;
  left: 0; top: 20%; bottom: 48px; width: 52%;
  pointer-events: auto;
}
.joystick-base {
  position: absolute;
  width: 112px; height: 112px;
  margin-left: -56px; margin-top: -56px;
  border-radius: 50%;
  background: rgba(255, 241, 207, 0.12);
  border: 2px solid rgba(255, 216, 107, 0.45);
}
.joystick-knob {
  position: absolute;
  left: 50%; top: 50%;
  width: 48px; height: 48px;
  margin-left: -24px; margin-top: -24px;
  border-radius: 50%;
  background: rgba(255, 179, 71, 0.75);
  border: 2px solid var(--cream);
}
.touch-actions {
  position: absolute;
  right: max(16px, env(safe-area-inset-right, 16px));
  bottom: max(58px, calc(env(safe-area-inset-bottom, 16px) + 46px));
  display: flex;
  gap: 14px;
  pointer-events: auto;
}
.touch-btn {
  width: 74px; height: 74px;
  border-radius: 50%;
  border: 3px solid var(--gold);
  background: rgba(27, 23, 64, 0.72);
  color: var(--cream);
  font-weight: 800;
  font-size: 0.78rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 14px rgba(0,0,0,0.5);
}
.touch-btn.active {
  background: rgba(255, 179, 71, 0.85);
  color: var(--ink);
  transform: scale(0.95);
}
.flare-btn { border-color: #FFB347; }

/* Results */
.results-card {
  margin: auto;
  max-width: 560px;
  width: 100%;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}
.postcard-img {
  width: min(82vw, 360px);
  border-radius: 10px;
  border: 4px solid #FFF8E7;
  box-shadow: 0 6px 18px rgba(0,0,0,0.35);
}
.postcard-cap { font-style: italic; margin: 0; font-size: 1rem; }
.res-stats { font-weight: 700; font-size: 0.95rem; color: #5C3A0E; line-height: 1.6; }

/* Modals */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(12, 10, 30, 0.78);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 16px;
}
.modal-box {
  max-width: 820px;
  width: 100%;
  max-height: 88dvh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.cards-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
}
.up-card {
  background: #FFF7E4;
  border: 3px solid var(--brass);
  border-radius: 14px;
  padding: 14px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  text-align: center;
  cursor: pointer;
}
.up-card.evo { background: linear-gradient(180deg, #FFF3B0, #FFD86B); border-color: #B45309; }
.up-card img { width: 64px; height: 64px; object-fit: contain; }
.up-card strong { font-size: 1rem; color: var(--ink); }
.up-card p { margin: 0; font-size: 0.84rem; color: #3A3058; }

.modal-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid rgba(142,101,40,0.22);
  font-size: 0.92rem;
}
.modal-row button { min-width: 120px; padding: 8px 12px; font-size: 0.88rem; }

@media (max-width: 640px) {
  .hearth-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .hearth-card img { width: 58px; height: 58px; }
  .wax-btn { min-width: 160px; padding: 10px 18px; font-size: 0.96rem; }
}
