/* ===================================================================
   TypstRacer — early-2000s Flash-game aesthetic
   Glossy panels, chunky beveled buttons, bright friendly colors,
   Verdana/Trebuchet type. No neon, no glow.
   =================================================================== */

/* ── Variables ─────────────────────────────────────────────── */
:root {
  --sky-1: #cfe8ff;
  --sky-2: #9cc7f0;

  --panel: #ffffff;
  --panel-alt: #f3f7fb;
  --ink: #1a2f45;
  --ink-soft: #4a6076;

  --blue: #2f7fd6;
  --blue-d: #1f5fa8;
  --green: #4caf2f;
  --green-d: #368a1f;
  --orange: #ff8a1e;
  --orange-d: #d96f0c;
  --gold: #e0a400;
  --purple: #8a4fd0;

  --correct: #2e9e2e;
  --incorrect: #d63a2f;
  --untyped: #a6b4c2;
  --extra: #d96f0c;

  --color-incorrect: var(--incorrect);
  /* used by showError() in index.html */

  --border: #bcd0e2;
  --shadow: 0 3px 0 rgba(0, 0, 0, .12), 0 6px 14px rgba(20, 50, 90, .18);

  --ui: 'Trebuchet MS', 'Verdana', 'Segoe UI', sans-serif;
  --mono: 'Consolas', 'Courier New', monospace;
}

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

html,
body {
  height: 100%;
}

body {
  font-family: var(--ui);
  color: var(--ink);
  background:
    radial-gradient(circle at 20% 0%, #ffffff 0%, transparent 45%),
    linear-gradient(180deg, var(--sky-1) 0%, var(--sky-2) 100%);
  background-attachment: fixed;
  min-height: 100vh;
  padding: 10px;
}

/* ── Container ──────────────────────────────────────────────── */
.container {
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* ── Header ─────────────────────────────────────────────────── */
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  background: linear-gradient(180deg, #ffffff, #e9f2fb);
  border: 2px solid var(--border);
  border-radius: 12px;
  padding: 10px 16px;
  box-shadow: var(--shadow);
}

.logo-section h1 {
  font-size: 1.7rem;
  font-weight: 800;
  color: var(--blue-d);
  letter-spacing: .5px;
  text-shadow: 1px 1px 0 #fff;
}

.logo-section p {
  font-size: .8rem;
  color: var(--ink-soft);
  margin-top: 2px;
}

.badge {
  display: inline-block;
  font-size: .6rem;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(180deg, var(--orange), var(--orange-d));
  border-radius: 8px;
  padding: 2px 8px;
  vertical-align: middle;
  margin-left: 6px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .5);
}

/* ── Stats bar ──────────────────────────────────────────────── */
.stats-bar {
  display: flex;
  gap: 8px;
}

.stat-item {
  background: linear-gradient(180deg, #ffffff, var(--panel-alt));
  border: 2px solid var(--border);
  border-radius: 10px;
  padding: 6px 14px;
  min-width: 66px;
  text-align: center;
  box-shadow: inset 0 1px 0 #fff;
}

.stat-label {
  font-size: .6rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--ink-soft);
  font-weight: 700;
}

.stat-val {
  font-size: 1.15rem;
  font-weight: 800;
  color: var(--ink);
}

.stat-val.highlight-cyan {
  color: var(--blue);
}

.stat-val.purple {
  color: var(--purple);
}

/* ── Controls bar ───────────────────────────────────────────── */
.controls-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  background: linear-gradient(180deg, #ffffff, #eaf2fb);
  border: 2px solid var(--border);
  border-radius: 12px;
  padding: 8px 14px;
  box-shadow: var(--shadow);
}

.controls-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.group-label {
  font-size: .85rem;
  color: var(--ink-soft);
  font-weight: 600;
}

.btn-select-group {
  display: flex;
  gap: 4px;
}

.select-btn {
  font-family: var(--ui);
  font-size: .8rem;
  font-weight: 700;
  color: var(--blue-d);
  background: linear-gradient(180deg, #ffffff, #dfeaf6);
  border: 2px solid var(--border);
  border-radius: 8px;
  padding: 5px 12px;
  cursor: pointer;
}

.select-btn:hover {
  background: linear-gradient(180deg, #ffffff, #cfe0f2);
}

.select-btn.active {
  color: #fff;
  background: linear-gradient(180deg, var(--blue), var(--blue-d));
  border-color: var(--blue-d);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .4);
}

/* ── Chunky glossy buttons ──────────────────────────────────── */
.btn {
  font-family: var(--ui);
  font-size: .85rem;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(180deg, #8fa5b8, #6c8194);
  border: 2px solid rgba(0, 0, 0, .15);
  border-radius: 10px;
  padding: 7px 16px;
  cursor: pointer;
  text-shadow: 0 1px 1px rgba(0, 0, 0, .25);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .45), 0 3px 0 rgba(0, 0, 0, .18);
  transition: transform .05s;
}

.btn:hover {
  filter: brightness(1.06);
}

.btn:active {
  transform: translateY(2px);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .35);
}

.btn-primary {
  background: linear-gradient(180deg, var(--green), var(--green-d));
  border-color: var(--green-d);
}

/* ── Game grid: formula on top, editor + preview below ──────── */
.game-grid {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.play-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

@media (max-width: 760px) {
  .play-row {
    grid-template-columns: 1fr;
  }
}

/* ── Shared panel ───────────────────────────────────────────── */
.panel {
  background: var(--panel);
  border: 2px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
}

.panel.focus-active {
  border-color: var(--orange);
  box-shadow: 0 0 0 3px rgba(255, 138, 30, .25), var(--shadow);
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  background: linear-gradient(180deg, var(--blue), var(--blue-d));
  color: #fff;
  padding: 6px 12px;
}

.panel-title {
  font-size: .85rem;
  font-weight: 800;
  text-shadow: 0 1px 1px rgba(0, 0, 0, .3);
}

.header-note {
  font-size: .72rem;
  color: rgba(255, 255, 255, .85);
}

/* ── Rendered target formula (TOP, large) ───────────────────── */
.rendered-preview {
  min-height: 110px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px 16px;
  background:
    linear-gradient(180deg, #ffffff, #f5f9fd),
    repeating-linear-gradient(45deg, rgba(0, 0, 0, .015) 0 10px, transparent 10px 20px);
}

/* ── Editor (typing) ────────────────────────────────────────── */
.editor-body {
  position: relative;
  flex: 1;
  min-height: 120px;
  background: var(--panel-alt);
}

.focus-alert {
  position: absolute;
  inset: 0;
  z-index: 10;
  background: rgba(243, 247, 251, .92);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: opacity .5s;
}

.focus-alert.hidden {
  opacity: 0;
  pointer-events: none;
}

.focus-alert-hint {
  font-size: .9rem;
  color: var(--ink-soft);
}

.countdown {
  font-family: var(--ui);
  font-size: 3.5rem;
  font-weight: 800;
  line-height: 1;
  color: var(--blue-d);
}

.editor-textarea,
.editor-overlay {
  position: absolute;
  inset: 0;
  padding: 10px 12px;
  font-family: var(--mono);
  font-size: 1.05rem;
  line-height: 1.6;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  border: none;
}

/* Hard mode (default): plain, visible textarea; overlay hidden. */
.editor-textarea {
  resize: none;
  background: transparent;
  color: var(--ink);
  caret-color: var(--orange-d);
  outline: none;
  z-index: 2;
}

.editor-textarea::placeholder {
  color: var(--untyped);
}

.editor-overlay {
  z-index: 1;
  pointer-events: none;
  overflow-y: auto;
  display: none;
}

/* Learning mode: hide the textarea's own glyphs AND its placeholder (the
   ghost overlay is the guide here), and show the colour overlay. */
.editor-body.ghost .editor-textarea {
  color: transparent;
  caret-color: transparent;
}

.editor-body.ghost .editor-textarea::placeholder {
  color: transparent;
}

.editor-body.ghost .editor-overlay {
  display: block;
}

/* Character states (Normal mode overlay) */
.char {
  position: relative;
}

.char.untyped {
  color: var(--untyped);
}

.char.correct {
  color: var(--correct);
}

.char.incorrect {
  color: var(--incorrect);
  background: rgba(214, 58, 47, .12);
  border-radius: 2px;
}

.char.extra {
  color: var(--extra);
  background: rgba(217, 111, 12, .14);
  border-radius: 2px;
}

.caret {
  position: absolute;
  left: 0;
  top: 12%;
  height: 76%;
  width: 2px;
  background: var(--orange-d);
  animation: blink .9s step-end infinite;
}

@keyframes blink {
  50% {
    opacity: 0;
  }
}

/* ── Live preview (right) ───────────────────────────────────── */
.draft-body {
  flex: 1;
  min-height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  background: linear-gradient(180deg, #ffffff, #f5f9fd);
  overflow: auto;
}

.draft-placeholder {
  font-size: .85rem;
  color: var(--ink-soft);
  font-style: italic;
  text-align: center;
}

.render-note {
  font-size: .8rem;
  color: var(--ink-soft);
  font-style: italic;
  text-align: center;
  overflow-wrap: anywhere;
}

/* ── Spinner / loader ───────────────────────────────────────── */
.loader-container {
  text-align: center;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 4px solid #d5e2ee;
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: spin .8s linear infinite;
  margin: 0 auto 8px;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.loading-text {
  font-size: .8rem;
  color: var(--ink-soft);
}

/* ── Cheat sheet ────────────────────────────────────────────── */
.cheat-sheet-panel .panel-header {
  background: linear-gradient(180deg, var(--gold), #b98600);
}

.sheet-search {
  font-family: var(--mono);
  font-size: .8rem;
  border: 2px solid rgba(0, 0, 0, .15);
  border-radius: 8px;
  padding: 4px 10px;
  outline: none;
  background: #fff;
  color: var(--ink);
  width: 180px;
}

.sheet-body {
  max-height: 360px;
  overflow-y: auto;
  padding: 10px 12px;
  background: var(--panel-alt);
}

.sheet-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 6px;
}

.sheet-item {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 9px;
  box-shadow: 0 1px 0 rgba(0, 0, 0, .05);
}

.sheet-math {
  font-size: .9rem;
  font-weight: 700;
  color: var(--ink);
}

.sheet-typst {
  font-family: var(--mono);
  font-size: .78rem;
  color: var(--blue-d);
  background: #e7f0fa;
  border-radius: 4px;
  padding: 1px 5px;
  display: inline-block;
  margin: 2px 0;
}

.sheet-desc {
  font-size: .72rem;
  color: var(--ink-soft);
}

/* ── Victory modal ──────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(20, 50, 90, .55);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
}

.modal-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

.modal-content {
  width: 90%;
  max-width: 440px;
  background: linear-gradient(180deg, #ffffff, #eaf2fb);
  border: 3px solid var(--gold);
  border-radius: 16px;
  padding: 26px;
  text-align: center;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .35);
}

.victory-title {
  font-size: 1.5rem;
  font-weight: 800;
  color: var(--green-d);
  text-shadow: 1px 1px 0 #fff;
  margin-bottom: 4px;
}

.victory-subtitle {
  font-size: 1rem;
  font-weight: 700;
  color: var(--blue-d);
  margin-bottom: 4px;
  text-align: left;
}

#nextRaceBtn {
  margin: 4px 0 14px;
}

.victory-explain {
  font-size: .85rem;
  line-height: 1.5;
  color: var(--ink);
  text-align: justify;
}

.victory-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 20px;
}

.v-stat {
  background: #fff;
  border: 2px solid var(--border);
  border-radius: 10px;
  padding: 12px;
}

.v-label {
  font-size: .65rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--ink-soft);
  font-weight: 700;
}

.v-val {
  font-size: 1.5rem;
  font-weight: 800;
  color: var(--ink);
  margin-top: 4px;
}

.v-val.highlight-cyan {
  color: var(--blue);
}

.v-val.purple {
  color: var(--purple);
}

/* ── Footer ─────────────────────────────────────────────────── */
footer {
  text-align: center;
  padding: 12px;
  font-size: .78rem;
  color: var(--ink-soft);
}

footer a {
  color: var(--blue-d);
  font-weight: 700;
  text-decoration: none;
}

footer a:hover {
  text-decoration: underline;
}

/* ── Site nav / footer (shared across pages) ────────────────── */
.site-nav {
  max-width: 1100px;
  margin: 0 auto 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 6px 4px;
}

.site-nav .brand {
  font-weight: 800;
  font-size: 1.15rem;
  color: var(--blue-d);
  text-decoration: none;
}

.nav-links {
  display: flex;
  gap: 14px;
}

.nav-links a {
  color: var(--ink-soft);
  text-decoration: none;
  font-weight: 700;
  font-size: .9rem;
}

.nav-links a:hover {
  color: var(--blue-d);
  text-decoration: underline;
}

.site-footer {
  text-align: center;
  padding: 16px;
  font-size: .8rem;
  color: var(--ink-soft);
}

.site-footer a {
  color: var(--blue-d);
  font-weight: 700;
  text-decoration: none;
}

.site-footer a:hover {
  text-decoration: underline;
}

.footer-links {
  margin-top: 6px;
}

/* ── Content pages (guide / about / privacy) ────────────────── */
.content {
  max-width: 820px;
  margin: 0 auto;
  background: var(--panel);
  border: 2px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 24px 28px;
}

.content h1 {
  color: var(--blue-d);
  font-size: 1.7rem;
  margin-bottom: 12px;
}

.content h2 {
  color: var(--ink);
  font-size: 1.15rem;
  margin: 22px 0 8px;
}

.content p {
  line-height: 1.6;
  margin-bottom: 12px;
  color: var(--ink);
}

.content ul {
  margin: 0 0 12px 22px;
}

.content li {
  line-height: 1.6;
  margin-bottom: 6px;
  color: var(--ink);
}

.content a {
  color: var(--blue-d);
  font-weight: 700;
}

.content code {
  font-family: var(--mono);
  font-size: .9em;
  background: #eef2f7;
  border-radius: 4px;
  padding: 1px 5px;
  color: var(--ink);
}

.ref-table {
  width: 100%;
  border-collapse: collapse;
  margin: 6px 0 12px;
  font-size: .9rem;
}

.ref-table th,
.ref-table td {
  text-align: left;
  padding: 7px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.ref-table th {
  color: var(--ink-soft);
  font-size: .78rem;
  text-transform: uppercase;
  letter-spacing: .5px;
}

.cta {
  margin-top: 18px;
  font-weight: 700;
}

/* ── Side-banner ad rails (game page only; wide screens only) ── */
.stage {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  gap: 64px;
  max-width: 1220px;
  margin: 0 auto;
}

.stage .container {
  margin: 0;
}

.ad-rail {
  display: none;
  /* hidden by default (mobile/tablet) */
  flex: 0 0 160px;
  position: sticky;
  top: 12px;
  min-height: 600px;
}

@media (min-width: 1200px) {
  .ad-rail {
    display: block;
  }

  /* only when there's room beside the game */
}

/* Content pages sitting inside .stage (e.g. guide with ad rails): the auto
   margin would eat flex free space, so pin it. */
.stage .content {
  margin: 0;
}

/* ── Landing page ───────────────────────────────────────────── */
.hero {
  text-align: center;
  padding: 6px 0 4px;
}

.hero .tagline {
  max-width: 560px;
  margin: 10px auto 16px;
  font-size: 1.05rem;
}

.btn-play {
  display: inline-block;
  font-size: 1.15rem;
  padding: 12px 36px;
}

.content a.btn-play,
.content a.btn-play:hover {
  color: #fff;
  text-decoration: none;
}

/* Mini re-creation of the game layout: target on top, input + preview below. */
.demo {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin: 18px 0 8px;
}

.demo-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

@media (max-width: 600px) {
  .demo-row {
    grid-template-columns: 1fr;
  }
}

.demo-render {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px 16px;
  min-height: 92px;
  background: linear-gradient(180deg, #ffffff, #f5f9fd);
}

.demo-svg {
  max-width: 100%;
  height: auto;
  max-height: 96px;
}

.demo-svg-target {
  max-height: 150px;
}

.demo-input {
  display: flex;
  align-items: center;
  min-height: 92px;
  padding: 12px 16px;
  background: var(--panel-alt);
}

.demo-input code {
  background: none;
  padding: 0;
  font-size: 1.05rem;
  color: var(--ink);
}

.demo-caption {
  font-size: .9rem;
  color: var(--ink-soft);
  text-align: center;
  margin: 4px auto 0;
  max-width: 620px;
}

.demo-gif-wrap {
  text-align: center;
  margin: 18px 0 8px;
}

.demo-gif {
  max-width: 100%;
  height: auto;
  border-radius: 12px;
  border: 2px solid var(--border);
  box-shadow: var(--shadow);
}

.demo-gif-wrap figcaption {
  font-size: .85rem;
  color: var(--ink-soft);
  margin-top: 6px;
}