/* =========================================================
   Design tokens
   ========================================================= */
:root {
  color-scheme: light dark;

  /* Typography */
  --font-sans: "Lexend Deca", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --text-xs: 0.75rem;
  --text-sm: 0.875rem;
  --text-md: 1rem;
  --text-lg: clamp(1.0625rem, 1rem + 0.3vw, 1.1875rem);
  --text-xl: clamp(1.25rem, 1.1rem + 0.6vw, 1.5rem);
  --text-2xl: clamp(1.5rem, 1.2rem + 1.4vw, 2.25rem);
  --text-display: clamp(2.25rem, 1.3rem + 4.2vw, 4.5rem);
  --leading-tight: 1.08;
  --leading-snug: 1.3;
  --leading-body: 1.65;
  --tracking-tight: -0.025em;
  --tracking-wide: 0.08em;

  /* Spacing (4px base) */
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.5rem;
  --space-6: 2rem;
  --space-7: 3rem;
  --space-8: 4rem;
  --space-9: 6rem;

  /* Shape and elevation */
  --radius-sm: 0.5rem;
  --radius-md: 0.875rem;
  --radius-lg: 1.5rem;
  --radius-full: 999px;
  --shadow-lg: 0 24px 60px -20px rgb(15 17 40 / 0.35);

  /* Motion */
  --duration-fast: 140ms;
  --duration-base: 240ms;
  --ease-out: cubic-bezier(0.2, 0.8, 0.2, 1);

  /* Layout */
  --container-max: 72rem;
  --gutter: clamp(1rem, 4vw, 2rem);

  /* Color: light */
  --color-bg: #f6f7fc;
  --color-bg-glow-1: rgb(99 102 241 / 0.14);
  --color-bg-glow-2: rgb(236 72 153 / 0.1);
  --color-surface: #ffffff;
  --color-surface-muted: #eef0fa;
  --color-text: #13152b;
  --color-text-muted: #4b5072;
  --color-border: #dcdff0;
  --color-primary: #4f46e5;
  --color-primary-hover: #4338ca;
  --color-on-primary: #ffffff;
  --color-accent: #db2777;
  --color-warning: #a35207;
  --color-warning-soft: #fdf1d6;
  --color-focus: #6366f1;
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #080a16;
    --color-bg-glow-1: rgb(129 140 248 / 0.16);
    --color-bg-glow-2: rgb(244 114 182 / 0.1);
    --color-surface: #11142a;
    --color-surface-muted: #181c38;
    --color-text: #eceefe;
    --color-text-muted: #a4a9cf;
    --color-border: #262b4d;
    --color-primary: #8b8dff;
    --color-primary-hover: #a5a7ff;
    --color-on-primary: #0b0c1f;
    --color-accent: #f472b6;
    --color-warning: #fbbf24;
    --color-warning-soft: rgb(251 191 36 / 0.12);
    --color-focus: #a5b4fc;
    --shadow-lg: 0 24px 60px -20px rgb(0 0 0 / 0.6);
  }
}

/* =========================================================
   Base
   ========================================================= */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
  scroll-padding-top: var(--space-5);
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  font-family: var(--font-sans);
  font-size: var(--text-md);
  line-height: var(--leading-body);
  color: var(--color-text);
  background-color: var(--color-bg);
  background-image:
    radial-gradient(60rem 40rem at 10% -10%, var(--color-bg-glow-1), transparent 60%),
    radial-gradient(50rem 36rem at 100% 0%, var(--color-bg-glow-2), transparent 60%);
  background-repeat: no-repeat;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

@media (prefers-color-scheme: dark) {
  body::before {
    content: "";
    position: fixed;
    inset: 0;
    z-index: -1;
    pointer-events: none;
    opacity: 0.55;
    background-image:
      radial-gradient(1px 1px at 12% 22%, #fff, transparent),
      radial-gradient(1px 1px at 28% 68%, #fff, transparent),
      radial-gradient(1.5px 1.5px at 44% 12%, #fff, transparent),
      radial-gradient(1px 1px at 63% 42%, #fff, transparent),
      radial-gradient(1.5px 1.5px at 78% 18%, #fff, transparent),
      radial-gradient(1px 1px at 86% 74%, #fff, transparent),
      radial-gradient(1px 1px at 52% 88%, #fff, transparent),
      radial-gradient(1px 1px at 6% 82%, #fff, transparent);
  }
}

main {
  flex: 1;
}

h1,
h2,
h3,
p,
dl,
dd {
  margin: 0;
}

a {
  color: inherit;
}

:focus-visible {
  outline: 3px solid var(--color-focus);
  outline-offset: 3px;
}

.skip-link {
  position: absolute;
  left: var(--space-4);
  top: var(--space-4);
  z-index: 10;
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-sm);
  background: var(--color-primary);
  color: var(--color-on-primary);
  font-weight: 600;
  text-decoration: none;
  transform: translateY(-200%);
}

.skip-link:focus {
  transform: none;
}

.container {
  width: 100%;
  max-width: var(--container-max);
  margin-inline: auto;
  padding-inline: var(--gutter);
}

/* =========================================================
   Components
   ========================================================= */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: 2.75rem;
  padding: 0 var(--space-5);
  border: 1px solid transparent;
  border-radius: var(--radius-full);
  font: inherit;
  font-weight: 600;
  line-height: 1;
  text-decoration: none;
  cursor: pointer;
  transition:
    background-color var(--duration-fast) var(--ease-out),
    border-color var(--duration-fast) var(--ease-out),
    transform var(--duration-fast) var(--ease-out);
}

.btn:active {
  transform: translateY(1px);
}

.btn--primary {
  background: var(--color-primary);
  color: var(--color-on-primary);
}

.btn--primary:hover {
  background: var(--color-primary-hover);
}

.btn--ghost {
  background: transparent;
  color: var(--color-text);
  border-color: var(--color-border);
}

.btn--ghost:hover {
  background: var(--color-surface-muted);
}

.btn--sm {
  min-height: 2.5rem;
  padding-inline: var(--space-4);
  font-size: var(--text-sm);
}

/* Sound toggle: the icon shows waves when on and a cross when off. */
[data-action="sound"] .sound-icon__on,
[data-action="sound"][aria-pressed="true"] .sound-icon__off {
  display: none;
}

[data-action="sound"][aria-pressed="true"] .sound-icon__on {
  display: inline;
}

.btn--lg {
  min-height: 3.25rem;
  padding-inline: var(--space-6);
  font-size: var(--text-lg);
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: 0.375rem 0.875rem;
  border-radius: var(--radius-full);
  background: var(--color-warning-soft);
  color: var(--color-warning);
  font-size: var(--text-sm);
  font-weight: 600;
}

.badge__dot {
  width: 0.5rem;
  height: 0.5rem;
  border-radius: 50%;
  background: currentColor;
  animation: pulse 1.8s var(--ease-out) infinite;
}

@keyframes pulse {
  from {
    box-shadow: 0 0 0 0 color-mix(in srgb, currentColor 55%, transparent);
  }
  to {
    box-shadow: 0 0 0 0.5rem transparent;
  }
}

.text-gradient {
  background: linear-gradient(100deg, var(--color-primary), var(--color-accent));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

kbd {
  display: inline-block;
  min-width: 1.5rem;
  padding: 0.0625rem 0.4rem;
  border: 1px solid var(--color-border);
  border-bottom-width: 2px;
  border-radius: 0.375rem;
  background: var(--color-surface);
  color: var(--color-text);
  font: inherit;
  font-size: var(--text-xs);
  font-weight: 600;
  text-align: center;
}

/* =========================================================
   Layout: header, hero, footer
   ========================================================= */
.site-header__inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  padding-block: var(--space-5);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-lg);
  font-weight: 600;
  letter-spacing: var(--tracking-tight);
  text-decoration: none;
}

.brand__mark {
  width: 1.75rem;
  height: 1.75rem;
}

.nav-link {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  min-height: 2.5rem;
  padding: 0 var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-full);
  font-size: var(--text-sm);
  font-weight: 500;
  text-decoration: none;
  transition: background-color var(--duration-fast) var(--ease-out);
}

.nav-link:hover {
  background: var(--color-surface-muted);
}

.hero {
  padding-block: clamp(2.5rem, 8vw, 6rem) var(--space-8);
}

.hero__title {
  max-width: 16ch;
  margin-top: var(--space-5);
  font-size: var(--text-display);
  font-weight: 600;
  line-height: var(--leading-tight);
  letter-spacing: var(--tracking-tight);
  text-wrap: balance;
}

.hero__lede {
  max-width: 38rem;
  margin-top: var(--space-5);
  font-size: var(--text-lg);
  color: var(--color-text-muted);
  text-wrap: pretty;
}

.hero__actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
  margin-top: var(--space-6);
}

.site-footer {
  border-top: 1px solid var(--color-border);
}

.site-footer__inner {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: var(--space-2) var(--space-5);
  padding-block: var(--space-6);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

/* =========================================================
   Game section
   ========================================================= */
.game-section {
  padding-bottom: var(--space-9);
}

.section-head {
  margin-bottom: var(--space-5);
}

.section-head h2 {
  font-size: var(--text-2xl);
  font-weight: 600;
  line-height: var(--leading-snug);
  letter-spacing: var(--tracking-tight);
}

.section-head p {
  max-width: 40rem;
  margin-top: var(--space-2);
  color: var(--color-text-muted);
}

/* The game always renders in a dark "space" theme, so it re-scopes the color tokens. */
.game-shell {
  --color-text: #f1f2ff;
  --color-text-muted: #b3b8de;
  --color-border: rgb(255 255 255 / 0.12);
  --color-surface: rgb(14 16 38 / 0.9);
  --color-surface-muted: rgb(255 255 255 / 0.05);
  --color-primary: #8b8dff;
  --color-primary-hover: #a5a7ff;
  --color-on-primary: #0b0c1f;
  --color-focus: #c7d2fe;
  --game-hp: #ff5d7a;
  --game-xp-1: #5eead4;
  --game-xp-2: #8b8dff;
  --game-gold: #ffd166;

  color-scheme: dark;
  position: relative;
  isolation: isolate;
  height: clamp(28rem, 74vh, 46rem);
  height: clamp(28rem, 74svh, 46rem);
  overflow: hidden;
  border: 1px solid rgb(255 255 255 / 0.08);
  border-radius: var(--radius-lg);
  background: #070914;
  box-shadow: var(--shadow-lg);
  color: var(--color-text);
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.game-shell::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  box-shadow: inset 0 0 90px rgb(255 60 100 / 0.55);
  opacity: 0;
  transition: opacity 220ms ease-out;
}

.game-shell.is-hurt::after {
  opacity: 1;
  transition: none;
}

.game-canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.game-canvas:focus-visible {
  outline: 2px solid rgb(199 210 254 / 0.35);
  outline-offset: -2px;
}

/* HUD */
.hud {
  position: absolute;
  inset: 0 0 auto;
  display: grid;
  gap: var(--space-2);
  padding: var(--space-3);
  pointer-events: none;
  transition: opacity var(--duration-base) var(--ease-out);
}

.game-shell[data-state="menu"] .hud {
  visibility: hidden;
  opacity: 0;
}

.xp-bar {
  position: relative;
  height: 0.875rem;
  overflow: hidden;
  border: 1px solid rgb(255 255 255 / 0.1);
  border-radius: var(--radius-full);
  background: rgb(255 255 255 / 0.08);
}

.xp-bar__fill {
  position: absolute;
  inset: 0;
  transform: scaleX(0);
  transform-origin: left;
  background: linear-gradient(90deg, var(--game-xp-1), var(--game-xp-2));
  transition: transform 120ms linear;
}

.xp-bar__label {
  position: relative;
  display: block;
  font-size: 0.625rem;
  font-weight: 700;
  line-height: 0.75rem;
  letter-spacing: var(--tracking-wide);
  text-align: center;
  text-shadow: 0 1px 2px rgb(0 0 0 / 0.7);
}

.hud__row {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: var(--space-3);
}

.hp {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
  font-size: var(--text-xs);
  font-weight: 600;
}

.hp-bar {
  flex: 1;
  max-width: 10rem;
  height: 0.625rem;
  overflow: hidden;
  border-radius: var(--radius-full);
  background: rgb(255 255 255 / 0.1);
}

.hp-bar__fill {
  display: block;
  height: 100%;
  transform-origin: left;
  background: var(--game-hp);
  transition: transform 150ms var(--ease-out);
}

.hp__text {
  color: var(--color-text-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.hud__timer {
  font-size: var(--text-xl);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 2px 8px rgb(0 0 0 / 0.6);
}

.hud__right {
  display: flex;
  align-items: center;
  justify-self: end;
  gap: var(--space-3);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 2.5rem;
  height: 2.5rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-full);
  background: rgb(255 255 255 / 0.06);
  color: var(--color-text);
  cursor: pointer;
  pointer-events: auto;
  transition: background-color var(--duration-fast) var(--ease-out);
}

.icon-btn:hover {
  background: rgb(255 255 255 / 0.14);
}

@media (max-width: 30rem) {
  .hp__text {
    display: none;
  }
}

/* Toast */
.toast {
  position: absolute;
  top: 5.25rem;
  left: 50%;
  max-width: calc(100% - 2rem);
  padding: var(--space-2) var(--space-4);
  border: 1px solid rgb(255 209 102 / 0.45);
  border-radius: var(--radius-full);
  background: var(--color-surface);
  color: var(--game-gold);
  font-size: var(--text-sm);
  font-weight: 600;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -0.5rem);
  transition:
    opacity var(--duration-base) var(--ease-out),
    transform var(--duration-base) var(--ease-out);
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* Overlays */
.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding: var(--space-4);
  overflow-y: auto;
  background: rgb(5 6 18 / 0.62);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  animation: overlay-in var(--duration-base) var(--ease-out);
}

.overlay[hidden] {
  display: none;
}

.overlay--start {
  background: radial-gradient(closest-side, rgb(5 6 18 / 0.75), rgb(5 6 18 / 0.1));
  -webkit-backdrop-filter: none;
  backdrop-filter: none;
}

@keyframes overlay-in {
  from {
    opacity: 0;
  }
}

.panel {
  display: grid;
  justify-items: center;
  gap: var(--space-3);
  width: 100%;
  max-width: 30rem;
  margin: auto;
  padding: clamp(1.25rem, 4vw, 2rem);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  background: var(--color-surface);
  box-shadow: 0 30px 80px -30px rgb(0 0 0 / 0.8);
  text-align: center;
}

.panel--wide {
  max-width: 48rem;
}

.panel__eyebrow {
  color: var(--color-primary);
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: var(--tracking-wide);
  text-transform: uppercase;
}

.panel__title {
  font-size: var(--text-2xl);
  font-weight: 600;
  line-height: var(--leading-snug);
  letter-spacing: var(--tracking-tight);
  text-wrap: balance;
}

.panel__text {
  max-width: 36ch;
  color: var(--color-text-muted);
  text-wrap: pretty;
}

.panel__meta {
  color: var(--color-text-muted);
  font-size: var(--text-sm);
}

.panel__actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: var(--space-3);
  margin-top: var(--space-2);
}

.panel > .btn--lg {
  margin-top: var(--space-2);
}

/* Upgrade choices */
.choices {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
  gap: var(--space-3);
  width: 100%;
  margin-top: var(--space-2);
}

.choice {
  position: relative;
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-areas:
    "icon name"
    "icon tag"
    "desc desc";
  align-content: start;
  align-items: center;
  gap: var(--space-1) var(--space-3);
  padding: var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface-muted);
  color: var(--color-text);
  font: inherit;
  text-align: left;
  cursor: pointer;
  transition:
    border-color var(--duration-fast) var(--ease-out),
    background-color var(--duration-fast) var(--ease-out),
    transform var(--duration-fast) var(--ease-out);
}

.choice:hover,
.choice:focus-visible {
  border-color: var(--color-primary);
  background: rgb(139 141 255 / 0.12);
  transform: translateY(-2px);
}

.choice-icon {
  grid-area: icon;
  display: grid;
  place-items: center;
  width: 3rem;
  height: 3rem;
  border-radius: var(--radius-sm);
  background: rgb(255 255 255 / 0.06);
  font-size: 1.75rem;
  line-height: 1;
}

.choice-name {
  grid-area: name;
  font-weight: 600;
  line-height: var(--leading-snug);
}

.choice-tag {
  grid-area: tag;
  justify-self: start;
  padding: 0.0625rem 0.5rem;
  border-radius: var(--radius-full);
  background: rgb(94 234 212 / 0.14);
  color: var(--game-xp-1);
  font-size: var(--text-xs);
  font-weight: 600;
}

.choice--mastery .choice-tag {
  background: rgb(255 209 102 / 0.14);
  color: var(--game-gold);
}

.choice--mastery .choice-icon {
  box-shadow: inset 0 0 0 1px rgb(255 209 102 / 0.45);
}

.choice-desc {
  grid-area: desc;
  margin-top: var(--space-2);
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  line-height: 1.5;
}

.choice-key {
  position: absolute;
  top: var(--space-2);
  right: var(--space-3);
  color: var(--color-text-muted);
  font-size: var(--text-xs);
}

@media (hover: none) {
  .choice-key {
    display: none;
  }
}

/* Results */
.new-best {
  color: var(--game-gold);
  font-weight: 600;
}

.stats {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--space-2);
  width: 100%;
  margin-block: var(--space-2);
}

.stats div {
  padding: var(--space-3);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface-muted);
}

.stats dt {
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  letter-spacing: var(--tracking-wide);
  text-transform: uppercase;
}

.stats dd {
  font-size: var(--text-xl);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* Controls legend */
.controls {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2) var(--space-5);
  margin: var(--space-4) 0 0;
  padding: 0;
  list-style: none;
  color: var(--color-text-muted);
  font-size: var(--text-sm);
}

.controls li {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.controls li > span {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
}

/* =========================================================
   Accessibility preferences
   ========================================================= */
@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }

  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
