/* Terraforming Mars — How to Play. Dark theme only, no external assets. */

:root {
  color-scheme: dark;
  /* surfaces */
  --space: #0a0f1f;
  --space-2: #111a35;
  --panel: #16203f;
  --line: #2b3a63;
  --ink: #eef1f8;
  --muted: #9ca8c7;
  /* game colours */
  --mars: #e0652c;
  --mars-2: #9c3d17;
  --ocean: #22b8b0;
  --green: #5fc35d;
  --gold: #f3bb4a;
  --steel: #8d97aa;
  --ti: #8fd3ff;
  --energy: #f7dc2a;
  --heat: #ff5b33;
  --prod: #6e4527; /* brown production box */
  --bad: #ff6b6b;
  --bronze: #c27a44;
  --silver: #cfd6e0;
  --p1: #c4b5fd; /* player marker colours (abstract players) */
  --p2: #f9a8d4;
  --p3: #e2e8f0;
  /* motion */
  --out: cubic-bezier(.22, 1, .36, 1);
  --back: cubic-bezier(.34, 1.56, .64, 1); /* slight overshoot for landings and pops */
  --gutter: 16px;
  --bar-h: calc(104px + env(safe-area-inset-bottom));
  --dir: 1; /* +1 moving forward, -1 moving back (set by app.js) */
  --i: 0;   /* current step index (set by app.js), drives the sky parallax */
}

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

html, body { height: 100%; margin: 0; overflow: hidden; }

body {
  background: radial-gradient(120% 80% at 70% 0%, var(--space-2), var(--space) 60%);
  color: var(--ink);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

.defs { position: absolute; width: 0; height: 0; overflow: hidden; }

/* ---------- Starfield: two layers, one drifting, one twinkling with parallax ---------- */
.sky { position: fixed; inset: 0; overflow: hidden; pointer-events: none; }
.sky::before, .sky::after {
  content: "";
  position: absolute;
  inset: -300px;
  background-repeat: repeat;
}
.sky::before {
  background-image:
    radial-gradient(1px 1px at 20px 40px, #fff9 50%, transparent 52%),
    radial-gradient(1px 1px at 130px 90px, #fff6 50%, transparent 52%),
    radial-gradient(1.5px 1.5px at 210px 170px, #fff8 50%, transparent 52%),
    radial-gradient(1px 1px at 80px 220px, #fff5 50%, transparent 52%);
  background-size: 260px 260px;
  animation: sky-drift 160s linear infinite;
  will-change: transform;
}
.sky::after {
  background-image:
    radial-gradient(2px 2px at 60px 120px, #fff 50%, transparent 55%),
    radial-gradient(1.5px 1.5px at 300px 40px, #ffe9c7 50%, transparent 55%),
    radial-gradient(2px 2px at 340px 310px, #cde8ff 50%, transparent 55%);
  background-size: 420px 420px;
  transform: translateX(calc(var(--i) * -18px));
  transition: transform 1.2s var(--out);
  animation: sky-twinkle 7s ease-in-out infinite;
}
@keyframes sky-drift { to { transform: translate(-260px, -130px); } }
@keyframes sky-twinkle { 50% { opacity: .45; } }

/* ---------- Steps ---------- */
.step {
  position: fixed;
  inset: 0 0 var(--bar-h) 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px var(--gutter) 12px;
  overflow-x: hidden;
  overflow-y: auto;
  visibility: hidden;
}
.step.is-current, .step.is-leaving { visibility: visible; }
/* A #step-N deep link focuses the (scrollable) section; a full-screen ring would be noise. */
.step:focus-visible { outline: none; }
.step.is-leaving { pointer-events: none; }

/* auto margins centre the pair but never clip it when the copy is taller than the screen */
.art { margin-top: auto; height: min(42dvh, 340px); flex: none; }
.copy { margin-bottom: auto; max-width: 60ch; }

.art svg { display: block; width: 100%; height: 100%; overflow: visible; }

@media (min-width: 700px) {
  :root { --bar-h: calc(84px + env(safe-area-inset-bottom)); }
}

@media (min-width: 900px) {
  .step {
    flex-direction: row;
    align-items: center;
    gap: clamp(32px, 4vw, 64px);
    padding: 32px clamp(32px, 6vw, 96px);
  }
  .art { flex: 1.15; height: min(72dvh, 560px); margin: 0; }
  .copy { flex: 1; margin: 0; }
}

/* Step transition: art and copy travel different distances, giving a parallax slide. */
.step.is-entering .art { animation: in-art .75s var(--out) both; }
.step.is-entering .copy { animation: in-copy .85s var(--out) both; }
.step.is-leaving .art { animation: out-art .45s ease-in both; }
.step.is-leaving .copy { animation: out-copy .45s ease-in both; }
@keyframes in-art { from { transform: translateX(calc(var(--dir) * 10vw)) scale(.94); opacity: 0; } }
@keyframes in-copy { from { transform: translateX(calc(var(--dir) * 30vw)); opacity: 0; } }
@keyframes out-art { to { transform: translateX(calc(var(--dir) * -10vw)) scale(.94); opacity: 0; } }
@keyframes out-copy { to { transform: translateX(calc(var(--dir) * -30vw)); opacity: 0; } }

/* ---------- Copy ---------- */
.kicker {
  margin: 0 0 4px;
  color: var(--mars);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: .12em;
  text-transform: uppercase;
}
.copy h2 {
  margin: 0 0 10px;
  font-size: clamp(1.75rem, 1.1rem + 2.6vw, 3rem);
  line-height: 1.08;
  letter-spacing: -.01em;
}
.copy p { margin: 0 0 10px; }
.details, .checklist {
  margin: 12px 0 0;
  padding: 0;
  list-style: none;
  color: var(--muted);
  font-size: 14px;
}
.details li { position: relative; padding-left: 18px; margin-bottom: 6px; }
.details li::before {
  content: "";
  position: absolute;
  left: 2px;
  top: .55em;
  width: 7px;
  height: 7px;
  border-radius: 2px;
  background: var(--mars);
  transform: rotate(45deg);
}
.details strong, .checklist strong { color: var(--ink); }
.checklist li { position: relative; padding-left: 26px; margin-bottom: 5px; }
.checklist li::before {
  content: "";
  position: absolute;
  left: 0;
  top: .2em;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--green) center / 10px no-repeat
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10'%3E%3Cpath d='M1.5 5.2 4 7.5 8.5 2.5' fill='none' stroke='%230a0f1f' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
}

.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }

button { font: inherit; }
.btn {
  min-height: 44px;
  min-width: 44px;
  padding: 0 18px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--panel);
  color: var(--ink);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: transform .2s var(--back), background .2s;
}
.btn:hover { background: #1d2a52; }
.btn:active { transform: scale(.96); }
.btn:disabled { opacity: .35; cursor: default; transform: none; }
.btn-primary { background: var(--mars); border-color: var(--mars); color: #1a0b04; }
.btn-primary:hover { background: #ef7a42; }
:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }

/* ---------- Bottom bar ---------- */
.bar {
  position: fixed;
  inset: auto 0 0 0;
  height: var(--bar-h);
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  align-content: start;
  column-gap: 10px;
  row-gap: 6px;
  padding: 10px var(--gutter) env(safe-area-inset-bottom);
  background: linear-gradient(to bottom, #0a0f1f00, #0a0f1fee 28%);
}
.meta { display: flex; flex-direction: column; align-items: center; gap: 5px; min-width: 0; }
.counter { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.dots { display: flex; gap: 4px; }
.dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--line);
  transition: transform .35s var(--back), background .35s;
}
.dot.is-done { background: var(--mars-2); }
.dot.is-on { background: var(--gold); transform: scale(1.5); }
.credit {
  grid-column: 1 / -1;
  margin: 0;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.35;
  text-align: center;
}
.credit a { color: var(--ink); }

/* ---------- SVG vocabulary ---------- */
/* CSS-animated SVG parts scale/rotate around their own centre. Scoped to fx/amb
   classes because transform-origin would also shift D3's transform attributes. */
[class*="fx-"], [class*="amb-"] { transform-box: fill-box; transform-origin: center; }
.t { fill: var(--ink); font-size: 13px; font-weight: 600; }
.ts { fill: var(--muted); font-size: 11px; font-weight: 500; }
.tl { font-size: 18px; font-weight: 800; }
.tc { text-anchor: middle; }
.te { text-anchor: end; }
.dk { fill: #1a1206; }
.panel { fill: var(--panel); stroke: var(--line); stroke-width: 1.5; }
.track { fill: #0d1530; stroke: var(--line); stroke-width: 1.5; }
.c-mars { fill: var(--mars); color: var(--mars); }
.c-mars2 { fill: var(--mars-2); color: var(--mars-2); }
.c-ocean { fill: var(--ocean); color: var(--ocean); }
.c-green { fill: var(--green); color: var(--green); }
.c-gold { fill: var(--gold); color: var(--gold); }
.c-steel { fill: var(--steel); color: var(--steel); }
.c-ti { fill: var(--ti); color: var(--ti); }
.c-energy { fill: var(--energy); color: var(--energy); }
.c-heat { fill: var(--heat); color: var(--heat); }
.c-prod { fill: var(--prod); color: var(--prod); }
.c-bad { fill: var(--bad); color: var(--bad); }
.c-bronze { fill: var(--bronze); color: var(--bronze); }
.c-silver { fill: var(--silver); color: var(--silver); }
.c-p1 { fill: var(--p1); color: var(--p1); }
.c-p2 { fill: var(--p2); color: var(--p2); }
.c-p3 { fill: var(--p3); color: var(--p3); }
.c-ink { fill: var(--ink); color: var(--ink); }
.c-muted { fill: var(--muted); color: var(--muted); }
.ln { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.dash { stroke-dasharray: 4 4; }
/* Number badges sitting on a chip/gauge edge: dark ring to separate them, digit centred on the shape. */
.badge-ring { stroke: var(--space); stroke-width: 2; }
.badge-num { font-size: 13px; font-weight: 800; dominant-baseline: central; }

/* ---------- Main-sequence animations ----------
   Animations only run inside the active step. Each element sets its own
   --d (delay) and optionally --dur, --fx/--fy/--fs/--fr (fly-in offsets).
   fill-mode "both" holds the start pose during the delay and the end pose after. */
.is-active [class*="fx-"] {
  animation-duration: var(--dur, .7s);
  animation-delay: var(--d, 0s);
  animation-fill-mode: both;
  animation-timing-function: var(--back);
}
.is-active .fx-pop { animation-name: pop; }
.is-active .fx-fly { animation-name: fly; }
.is-active .fx-drop { animation-name: drop; animation-timing-function: linear; --dur: .9s; }
.is-active .fx-fade { animation-name: fade; animation-timing-function: ease; }
.is-active .fx-flash { animation-name: flash; animation-timing-function: var(--out); }
.is-active .fx-out { animation-name: out; animation-timing-function: ease-in; }
.is-active .fx-dim { animation-name: dim; animation-timing-function: ease; }
.is-active .fx-shake { animation-name: shake; animation-timing-function: ease-in-out; }
.is-active .fx-burst { animation-name: burst; animation-timing-function: var(--out); --dur: .9s; }
.is-active .fx-draw { animation-name: draw; animation-timing-function: var(--out); }
.is-active .fx-grow-x { animation-name: grow-x; animation-timing-function: var(--out); }
.is-active .fx-grow-y { animation-name: grow-y; animation-timing-function: var(--out); }
.is-active .fx-flip-out { animation-name: flip-out; animation-timing-function: ease-in-out; }
.is-active .fx-flip-in { animation-name: flip-in; animation-timing-function: ease-in-out; }
.is-active .fx-slide { animation-name: slide; }
.fx-grow-x { transform-origin: left center; }
.fx-grow-y { transform-origin: center bottom; }
.fx-draw { stroke-dasharray: 100; } /* pair with pathLength="100" */

@keyframes pop { 0% { transform: scale(0); opacity: 0; } 50% { opacity: 1; } }
@keyframes fly {
  0% { transform: translate(var(--fx, 0), var(--fy, 0)) scale(var(--fs, 1)) rotate(var(--fr, 0deg)); opacity: 0; }
  25% { opacity: 1; }
}
/* falls, squashes on landing, bounces once */
@keyframes drop {
  0% { transform: translateY(var(--fy, -90px)); opacity: 0; animation-timing-function: cubic-bezier(.55, 0, 1, .45); }
  12% { opacity: 1; }
  52% { transform: translateY(0) scale(1.18, .82); animation-timing-function: var(--out); }
  70% { transform: translateY(-12px) scale(.94, 1.06); animation-timing-function: cubic-bezier(.55, 0, 1, .45); }
  84% { transform: translateY(0) scale(1.06, .94); animation-timing-function: var(--out); }
  100% { transform: none; }
}
@keyframes fade { from { opacity: 0; } }
/* visible for a while, then gone: used for values that get replaced */
@keyframes flash {
  0% { transform: scale(0); opacity: 0; }
  12% { transform: scale(1.18); opacity: 1; }
  22% { transform: scale(1); }
  82% { transform: scale(1); opacity: 1; }
  100% { transform: scale(.8); opacity: 0; }
}
/* moves from its resting place to an offset, e.g. energy cubes into the heat box */
@keyframes slide { to { transform: translate(var(--sx, 0), var(--sy, 0)); } }
@keyframes out { to { transform: scale(0); opacity: 0; } }
@keyframes dim { to { opacity: .28; } }
@keyframes shake { 0%, 100% { transform: none; } 20%, 60% { transform: translateX(-6px); } 40%, 80% { transform: translateX(6px); } }
@keyframes burst {
  0% { transform: scale(.2) rotate(0deg); opacity: 0; }
  15% { opacity: 1; }
  100% { transform: scale(2.6) rotate(25deg); opacity: 0; }
}
@keyframes draw { from { stroke-dashoffset: 100; } }
@keyframes grow-x { from { transform: scaleX(0); } }
@keyframes grow-y { from { transform: scaleY(0); } }
@keyframes flip-out { 0% { transform: none; } 50%, 100% { transform: scaleX(0); } }
@keyframes flip-in { 0%, 50% { transform: scaleX(0); } 100% { transform: none; } }

/* ---------- Ambient loops (on wrapper groups, so they never fight an fx-* transform) ---------- */
.is-active [class*="amb-"] {
  animation-duration: var(--adur, 4s);
  animation-delay: var(--ad, 0s);
  animation-iteration-count: infinite;
  animation-timing-function: ease-in-out;
}
.is-active .amb-float { animation-name: float; }
.is-active .amb-pulse { animation-name: pulse; }
.is-active .amb-glow { animation-name: glow; }
.is-active .amb-twinkle { animation-name: twinkle; }
.is-active .amb-sway { animation-name: sway; }
.is-active .amb-spin { animation-name: spin; animation-timing-function: linear; --adur: 90s; }
.is-active .amb-spin-r { animation-name: spin; animation-direction: reverse; animation-timing-function: linear; }
@keyframes float { 50% { transform: translateY(-5px); } }
@keyframes pulse { 50% { transform: scale(1.07); } }
@keyframes glow { 50% { opacity: .35; } }
@keyframes twinkle { 50% { transform: scale(.5); opacity: .25; } }
@keyframes sway { 50% { transform: rotate(-5deg); } }
@keyframes spin { to { transform: rotate(360deg); } }

/* HTML lists can use the same fx classes (setup checklist). */
.checklist li.fx-fly { --fx: -24px; --dur: .6s; }

/* ---------- Reduced motion: jump straight to the end pose, no loops ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 1ms !important;
    animation-delay: 0s !important;
    animation-iteration-count: 1 !important;
    transition-duration: 1ms !important;
  }
}
.is-active .amb-breathe { animation-name: breathe; --adur: 2.6s; }
@keyframes breathe { 50% { opacity: .72; } }
