:root {
  --bg: #0f0a21;
  --panel: rgba(18, 13, 40, 0.92);
  --border: rgba(114, 240, 232, 0.34);
  --ink: #f3f9ff;
  --muted: #a4b8d6;
  --cyan: #52f7e8;
  --gold: #ffd45e;
  --emerald: #4cf5a4;
  --crimson: #ff6284;
  --violet: #b678ff;
}

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

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: "Trebuchet MS", "Segoe UI", system-ui, -apple-system, sans-serif;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

[hidden] {
  display: none !important;
}

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

/* ─── TOP HUD ─── */
.hud {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: max(10px, env(safe-area-inset-top)) 16px 10px;
  background: linear-gradient(180deg, rgba(11, 7, 26, 0.9) 0%, rgba(11, 7, 26, 0.45) 70%, rgba(11, 7, 26, 0) 100%);
  pointer-events: none;
}

.hud-brand {
  display: flex;
  flex-direction: column;
}

.eyebrow-mini {
  font-size: 9px;
  font-weight: 800;
  letter-spacing: 0.16em;
  color: var(--cyan);
}

.brand-name {
  font-size: 18px;
  color: var(--ink);
  letter-spacing: 0.02em;
}

.hud-stats {
  display: flex;
  align-items: center;
  gap: 8px;
}

.stat-pill {
  background: rgba(20, 15, 46, 0.86);
  border: 1px solid rgba(114, 240, 232, 0.3);
  border-radius: 11px;
  padding: 4px 11px;
  min-width: 72px;
  text-align: center;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.32);
}

.stat-pill .label {
  display: block;
  font-size: 8.5px;
  letter-spacing: 0.16em;
  color: var(--muted);
  font-weight: 800;
}

.stat-pill b {
  display: block;
  font-size: 16px;
  font-variant-numeric: tabular-nums;
  line-height: 1.2;
}

.crystal-pill b {
  color: var(--cyan);
}

.hp-pill b {
  color: var(--emerald);
}

.combo-pill b {
  color: var(--gold);
}

/* ─── BOSS HEALTH BAR ─── */
.boss-bar-wrap {
  position: fixed;
  top: 66px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 13;
  width: min(420px, 86vw);
  padding: 6px 12px 8px;
  border-radius: 13px;
  background: rgba(24, 10, 34, 0.9);
  border: 1.5px solid rgba(255, 114, 152, 0.65);
  box-shadow: 0 10px 26px rgba(0, 0, 0, 0.5);
  pointer-events: none;
}

.boss-bar-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.1em;
  color: #ffe08a;
  margin-bottom: 5px;
}

.boss-bar-track {
  width: 100%;
  height: 9px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.14);
  overflow: hidden;
}

.boss-bar-fill {
  height: 100%;
  width: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, #ff4d7d 0%, #ffae42 100%);
  transition: width 0.08s linear;
}

.hud-tools {
  display: flex;
  align-items: center;
  gap: 6px;
  pointer-events: auto;
}

.tool-btn {
  height: 36px;
  min-width: 36px;
  padding: 0 10px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.24);
  background: rgba(24, 18, 56, 0.9);
  color: var(--ink);
  font-family: inherit;
  font-size: 12px;
  font-weight: 800;
  cursor: pointer;
}

.tool-btn.active {
  border-color: var(--cyan);
  background: rgba(22, 74, 86, 0.92);
  color: #dcfffa;
}

.tool-btn:hover {
  filter: brightness(1.12);
}

/* ─── FLOATING NOTICE & ACTION DOCK ─── */
.flash-note {
  position: fixed;
  top: 116px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 12;
  padding: 7px 18px;
  border-radius: 999px;
  background: rgba(15, 11, 36, 0.92);
  border: 1.5px solid var(--cyan);
  color: var(--gold);
  font-size: 14px;
  font-weight: 800;
  letter-spacing: 0.04em;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.16s ease;
  max-width: 90vw;
  text-align: center;
}

.flash-note.show {
  opacity: 1;
}

.action-dock {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: max(14px, env(safe-area-inset-bottom));
  z-index: 11;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  pointer-events: none;
}

.touch-hint {
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.14em;
  color: rgba(204, 238, 255, 0.72);
  background: rgba(12, 9, 28, 0.65);
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.12);
}

.ability-btns {
  margin-left: auto;
  display: flex;
  gap: 8px;
  pointer-events: auto;
}

.ability-btn {
  min-width: 84px;
  height: 52px;
  padding: 6px 12px;
  border-radius: 13px;
  border: 2px solid rgba(114, 240, 232, 0.5);
  background: rgba(18, 16, 46, 0.9);
  color: var(--ink);
  font-family: inherit;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.4);
}

.ability-btn span {
  font-size: 12px;
  font-weight: 900;
}

.ability-btn small {
  font-size: 10px;
  color: var(--muted);
  font-weight: 800;
}

.ability-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.nova-btn.ready {
  border-color: var(--gold);
  background: linear-gradient(180deg, rgba(160, 108, 22, 0.95), rgba(92, 52, 12, 0.95));
  box-shadow: 0 0 22px rgba(255, 212, 94, 0.55);
  animation: pulseGlow 1.1s ease-in-out infinite;
}

.nova-btn.ready small {
  color: #fff6c9;
}

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

/* ─── OVERLAYS ─── */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 14px;
  background: radial-gradient(circle at 50% 32%, rgba(28, 18, 62, 0.78), rgba(9, 6, 22, 0.94));
  backdrop-filter: blur(6px);
  overflow-y: auto;
}

.menu-card {
  width: min(560px, 100%);
  background: var(--panel);
  border: 2px solid var(--border);
  border-radius: 20px;
  padding: 20px 20px 18px;
  text-align: center;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.62);
}

.menu-card.compact {
  width: min(440px, 100%);
}

.menu-hero {
  position: relative;
  width: 240px;
  height: 102px;
  margin: 0 auto 6px;
}

.hero-img {
  position: absolute;
  object-fit: contain;
  filter: drop-shadow(0 8px 14px rgba(0, 0, 0, 0.55));
}

.hero-guardian {
  width: 74px;
  left: 16px;
  bottom: 4px;
  animation: floatY 1.8s ease-in-out infinite;
}

.hero-crystal {
  width: 66px;
  left: 88px;
  top: 0;
  animation: floatY 2.1s ease-in-out 0.2s infinite;
}

.hero-golem {
  width: 82px;
  right: 12px;
  bottom: 2px;
  animation: floatY 1.9s ease-in-out 0.4s infinite;
}

@keyframes floatY {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}

.eyebrow {
  font-size: 10px;
  font-weight: 900;
  letter-spacing: 0.2em;
  color: var(--cyan);
  margin-bottom: 4px;
}

.menu-title {
  font-size: clamp(28px, 5vw, 40px);
  color: var(--ink);
  line-height: 1.02;
  text-shadow: 0 4px 16px rgba(82, 247, 232, 0.35);
}

.menu-sub {
  font-size: 14px;
  font-weight: 900;
  letter-spacing: 0.24em;
  color: var(--gold);
  margin-bottom: 8px;
}

.overlay-h2 {
  font-size: clamp(24px, 4.5vw, 32px);
  color: var(--gold);
  margin-bottom: 8px;
}

.menu-desc {
  font-size: 13.5px;
  line-height: 1.45;
  color: #d6e4f8;
  margin-bottom: 14px;
}

.menu-desc strong {
  color: var(--gold);
}

.selector-block {
  margin-bottom: 12px;
}

.sel-label {
  display: block;
  font-size: 9.5px;
  font-weight: 900;
  letter-spacing: 0.18em;
  color: var(--muted);
  margin-bottom: 6px;
}

.grade-row,
.topic-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.sel-btn {
  padding: 9px 6px;
  border-radius: 10px;
  border: 2px solid rgba(255, 255, 255, 0.15);
  background: rgba(28, 22, 62, 0.86);
  color: var(--ink);
  font-family: inherit;
  font-size: 12px;
  font-weight: 800;
  cursor: pointer;
  transition: transform 0.1s ease, border-color 0.12s ease, background 0.12s ease;
}

.sel-btn.selected {
  border-color: var(--cyan);
  background: linear-gradient(180deg, rgba(24, 92, 108, 0.96), rgba(16, 56, 74, 0.96));
  box-shadow: 0 0 14px rgba(82, 247, 232, 0.32);
}

.menu-cta-row {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-top: 14px;
}

.primary-btn,
.secondary-btn {
  min-height: 48px;
  padding: 12px 20px;
  border-radius: 13px;
  font-family: inherit;
  font-size: 15px;
  font-weight: 900;
  letter-spacing: 0.03em;
  cursor: pointer;
  transition: transform 0.1s ease, filter 0.1s ease;
}

.primary-btn {
  flex: 1;
  border: 0;
  background: linear-gradient(180deg, #8afef2 0%, #30d8cb 100%);
  color: #0b1a24;
  box-shadow: 0 5px 0 #147a74, 0 12px 24px rgba(0, 0, 0, 0.4);
}

.primary-btn:hover:not(:disabled) {
  filter: brightness(1.06);
  transform: translateY(-1px);
}

.primary-btn:disabled {
  opacity: 0.55;
  cursor: wait;
}

.secondary-btn {
  width: 100%;
  border: 1.5px solid rgba(255, 255, 255, 0.24);
  background: rgba(30, 24, 66, 0.9);
  color: var(--ink);
}

.back-home {
  padding: 13px 14px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  color: var(--muted);
  text-decoration: none;
  font-size: 12.5px;
  font-weight: 800;
  white-space: nowrap;
}

.back-home:hover {
  color: var(--ink);
  border-color: rgba(255, 255, 255, 0.45);
}

.controls-foot {
  margin-top: 12px;
  font-size: 11px;
  color: var(--muted);
}

/* ─── 3-QUESTION CRYSTAL FORGE OVERLAY ─── */
.forge-card {
  width: min(560px, 100%);
  background: var(--panel);
  border: 2px solid rgba(82, 247, 232, 0.5);
  border-radius: 20px;
  padding: 20px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.65);
}

.forge-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
}

.forge-eyebrow {
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.16em;
  color: var(--gold);
}

.forge-pips {
  display: flex;
  gap: 6px;
}

.pip {
  width: 24px;
  height: 10px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.16);
  border: 1px solid rgba(255, 255, 255, 0.24);
}

.pip.current {
  background: var(--cyan);
  box-shadow: 0 0 10px rgba(82, 247, 232, 0.7);
}

.pip.correct {
  background: var(--emerald);
  border-color: #a8ffd6;
}

.pip.wrong {
  background: var(--crimson);
  border-color: #ff9fb3;
}

.forge-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 12px;
}

.cat-pill {
  font-size: 10px;
  font-weight: 900;
  letter-spacing: 0.14em;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(182, 120, 255, 0.22);
  border: 1px solid rgba(182, 120, 255, 0.55);
  color: #e6ccff;
  text-transform: uppercase;
}

.tier-preview {
  font-size: 11px;
  color: var(--muted);
  font-weight: 700;
}

.forge-question {
  font-size: clamp(19px, 3.4vw, 25px);
  line-height: 1.28;
  color: #ffffff;
  margin-bottom: 16px;
}

.forge-choices {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.choice-btn {
  min-height: 56px;
  padding: 12px 14px;
  border-radius: 13px;
  border: 2px solid rgba(132, 210, 255, 0.34);
  background: linear-gradient(180deg, rgba(35, 28, 78, 0.95), rgba(22, 17, 54, 0.95));
  color: var(--ink);
  font-family: inherit;
  font-size: 16px;
  font-weight: 800;
  cursor: pointer;
  text-align: center;
  transition: transform 0.1s ease, border-color 0.12s ease, background 0.12s ease;
}

.choice-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  border-color: var(--cyan);
}

.choice-btn.is-correct {
  border-color: var(--emerald);
  background: linear-gradient(180deg, rgba(24, 108, 76, 0.96), rgba(14, 66, 48, 0.96));
  color: #e8fff4;
}

.choice-btn.is-wrong {
  border-color: var(--crimson);
  background: linear-gradient(180deg, rgba(124, 28, 52, 0.95), rgba(74, 16, 32, 0.95));
  color: #ffdce5;
}

.choice-btn:disabled {
  cursor: default;
}

.forge-feedback {
  margin-top: 14px;
  padding: 12px 14px;
  border-radius: 13px;
  background: rgba(16, 44, 48, 0.88);
  border: 1.5px solid var(--emerald);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.forge-feedback.miss {
  background: rgba(54, 20, 34, 0.9);
  border-color: #ff8ea4;
}

.forge-feedback strong {
  font-size: 14px;
  color: var(--gold);
}

.forge-feedback p {
  font-size: 13px;
  color: #e4f2ff;
}

.forge-next-btn {
  margin-top: 4px;
}

/* ─── ACCURACY-SCALED UPGRADE PICKER ─── */
.upgrade-card {
  width: min(760px, 100%);
  background: var(--panel);
  border: 2px solid var(--border);
  border-radius: 22px;
  padding: 22px 20px;
  text-align: center;
  box-shadow: 0 26px 64px rgba(0, 0, 0, 0.7);
}

.upgrade-tier-badge {
  display: inline-block;
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0.14em;
  padding: 6px 14px;
  border-radius: 999px;
  margin-bottom: 8px;
}

.upgrade-tier-badge.gold {
  background: linear-gradient(180deg, rgba(255, 224, 114, 0.25), rgba(204, 134, 20, 0.25));
  border: 1.5px solid var(--gold);
  color: #ffe88f;
}

.upgrade-tier-badge.rare {
  background: rgba(76, 245, 164, 0.18);
  border: 1.5px solid var(--emerald);
  color: #b4ffdc;
}

.upgrade-tier-badge.base {
  background: rgba(82, 247, 232, 0.16);
  border: 1.5px solid var(--cyan);
  color: #c2fffa;
}

.upgrade-title {
  font-size: clamp(22px, 4vw, 30px);
  color: var(--ink);
  margin-bottom: 6px;
}

.upgrade-sub {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 18px;
}

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

.upg-btn {
  text-align: left;
  padding: 16px 14px;
  border-radius: 15px;
  border: 2px solid rgba(114, 240, 232, 0.45);
  background: linear-gradient(180deg, rgba(30, 26, 70, 0.96), rgba(18, 15, 44, 0.96));
  color: var(--ink);
  font-family: inherit;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 8px;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}

.upg-btn:hover {
  transform: translateY(-4px);
}

.upg-btn.tier-gold {
  border-color: var(--gold);
  background: linear-gradient(180deg, rgba(72, 48, 18, 0.96), rgba(34, 22, 12, 0.96));
  box-shadow: 0 0 22px rgba(255, 212, 94, 0.28);
}

.upg-btn.tier-rare {
  border-color: var(--emerald);
  background: linear-gradient(180deg, rgba(18, 66, 54, 0.96), rgba(12, 34, 34, 0.96));
  box-shadow: 0 0 18px rgba(76, 245, 164, 0.22);
}

.upg-slot {
  font-size: 9.5px;
  font-weight: 900;
  letter-spacing: 0.16em;
  color: var(--cyan);
}

.upg-btn.tier-gold .upg-slot {
  color: var(--gold);
}

.upg-btn.tier-rare .upg-slot {
  color: var(--emerald);
}

.upg-icon {
  font-size: 28px;
}

.upg-name {
  font-size: 18px;
  font-weight: 900;
  color: #ffffff;
}

.upg-detail {
  font-size: 12.5px;
  line-height: 1.4;
  color: #d6e6f8;
}

.pause-actions {
  display: grid;
  gap: 8px;
}

.res-stats {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}

.res-box {
  padding: 10px 8px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.14);
}

.res-box span {
  display: block;
  font-size: 9.5px;
  letter-spacing: 0.14em;
  color: var(--muted);
  font-weight: 800;
}

.res-box strong {
  display: block;
  font-size: 20px;
  color: var(--gold);
  margin-top: 2px;
}

/* ─── MOBILE RESPONSIVE (390x844 ZERO OVERFLOW) ─── */
@media (max-width: 720px) {
  .hud {
    flex-wrap: wrap;
    padding: max(8px, env(safe-area-inset-top)) 10px 8px;
    gap: 6px;
  }

  .hud-brand {
    display: none;
  }

  .hud-stats {
    flex: 1;
    gap: 4px;
  }

  .stat-pill {
    min-width: 54px;
    padding: 3px 6px;
    flex: 1;
  }

  .stat-pill b {
    font-size: 13px;
  }

  .tool-btn {
    height: 32px;
    min-width: 32px;
    padding: 0 7px;
    font-size: 11px;
  }

  .grade-row,
  .topic-row {
    grid-template-columns: repeat(2, 1fr);
  }

  .forge-choices {
    grid-template-columns: 1fr;
  }

  .upgrade-grid {
    grid-template-columns: 1fr;
  }

  .touch-hint {
    display: none;
  }

  .ability-btn {
    min-width: 76px;
    height: 46px;
  }

  .boss-bar-wrap {
    top: 92px;
  }

  .flash-note {
    top: 144px;
    font-size: 12px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    transition-duration: 0.001ms !important;
  }
}
