:root {
  --bg: #100708;
  --maroon: #420303;
  --gold: #e4c36a;
  --gold-deep: #8a6420;
  --emerald: #1ed98a;
  --cyan: #06b1c4;
  --stone: #9aaba4;
  --cream: #f6edd8;
  --ink: #14080b;
  --line: rgba(228, 195, 106, 0.38);
  --display: Georgia, "Times New Roman", serif;
  --ui: Roboto, "Segoe UI", sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html { height: 100%; }

body {
  margin: 0;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
  font-family: var(--ui);
  color: var(--cream);
  background:
    radial-gradient(120% 50% at 50% -10%, rgba(66, 3, 3, 0.9), transparent 52%),
    radial-gradient(70% 40% at 110% 110%, rgba(6, 177, 196, 0.12), transparent 46%),
    var(--bg);
}

[hidden] { display: none !important; }

button, a { -webkit-tap-highlight-color: transparent; }

button { font: inherit; color: inherit; }

img { max-width: 100%; display: block; }

.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 52px;
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  color: inherit;
  text-decoration: none;
}

.brand-mark {
  width: 36px;
  height: 36px;
  flex: 0 0 36px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  color: var(--ink);
  font-family: var(--display);
  font-weight: 700;
  background: radial-gradient(circle at 35% 30%, #fff4cc, var(--gold) 42%, var(--gold-deep));
  box-shadow: 0 0 0 2px rgba(6, 177, 196, 0.55), 0 0 14px rgba(228, 195, 106, 0.45);
}

.brand-copy {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.brand-copy strong {
  font-family: var(--display);
  font-size: 13px;
  letter-spacing: 0.22em;
  font-weight: 700;
}

.brand-copy em {
  font-style: normal;
  color: var(--stone);
  font-size: 11px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.crystal-pill {
  margin: 0;
  flex: 0 1 auto;
  max-width: 46%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  border: 1px solid rgba(30, 217, 138, 0.5);
  color: var(--emerald);
  border-radius: 999px;
  padding: 6px 10px;
  font-size: 12px;
  background: rgba(8, 28, 20, 0.55);
}

.flow-main {
  flex: 1;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  padding: 4px 14px 18px;
}

.sheet {
  width: min(640px, 100%);
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.eyebrow {
  margin: 0;
  color: var(--cyan);
  letter-spacing: 0.16em;
  text-transform: uppercase;
  font-size: 11px;
}

h1, h2, h3 {
  font-family: var(--display);
  font-weight: 700;
  margin: 0;
}

h1 { font-size: clamp(1.7rem, 7vw, 2.5rem); line-height: 1.05; }

h2 {
  color: var(--gold);
  font-size: clamp(1.15rem, 4.6vw, 1.45rem);
}

.lede, .prose p, .field-note {
  margin: 0;
  line-height: 1.5;
  font-size: 0.98rem;
}

.hero-frame {
  border-radius: 18px;
  overflow: hidden;
  border: 1px solid var(--line);
  box-shadow: 0 16px 30px rgba(0, 0, 0, 0.28);
}

.hero-frame img {
  width: 100%;
  aspect-ratio: 16 / 10;
  object-fit: cover;
}

.caption {
  margin: 0;
  color: var(--stone);
  font-size: 12px;
}

.stat-band {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.stat-card, .folio, .note-card, .legend li, .step {
  border: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(66, 3, 3, 0.48), rgba(12, 6, 8, 0.92));
  border-radius: 16px;
}

.stat-card { padding: 10px 12px; }

.stat-card span, .role {
  display: block;
  color: var(--stone);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-size: 10px;
}

.stat-card strong {
  display: block;
  margin-top: 2px;
  color: var(--emerald);
  font-size: 1.2rem;
  font-variant-numeric: tabular-nums;
}

.field-note {
  padding: 12px 14px;
  border-left: 3px solid var(--gold);
  background: rgba(228, 195, 106, 0.07);
  font-family: var(--display);
}

.split-shots {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.split-shots a {
  min-width: 0;
  color: var(--gold);
  text-decoration: none;
  font-size: 12px;
}

.split-shots img {
  width: 100%;
  aspect-ratio: 16 / 11;
  object-fit: cover;
  border-radius: 14px;
  border: 1px solid var(--line);
}

.split-shots span {
  display: block;
  margin-top: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.folio {
  display: grid;
  grid-template-columns: minmax(0, 104px) minmax(0, 1fr);
  gap: 10px;
  padding: 10px;
}

.folio.is-selected {
  border-color: rgba(30, 217, 138, 0.85);
  box-shadow: inset 0 0 0 1px rgba(30, 217, 138, 0.35), 0 10px 24px rgba(0, 0, 0, 0.25);
}

.folio img {
  width: 100%;
  height: 100%;
  min-height: 112px;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  border-radius: 12px;
}

.folio-body {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.folio-body p { margin: 0; line-height: 1.4; font-size: 0.92rem; }

.stat-list {
  margin: 0;
  padding-left: 16px;
  color: var(--stone);
  font-size: 0.82rem;
}

.stat-list li + li { margin-top: 2px; }

.status {
  min-height: 1.2em;
  color: var(--cyan);
  font-size: 12px;
}

.action {
  appearance: none;
  width: 100%;
  min-height: 48px;
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid rgba(228, 195, 106, 0.75);
  background: linear-gradient(180deg, rgba(228, 195, 106, 0.16), rgba(66, 3, 3, 0.4));
  color: var(--cream);
  text-decoration: none;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.action.primary {
  background: linear-gradient(180deg, #8fffd0, var(--emerald) 40%, #0c8f5a);
  color: #082116;
  border-color: #d8fff0;
  box-shadow: 0 0 18px rgba(30, 217, 138, 0.28);
}

.action.ghost { background: transparent; }

.action:disabled { opacity: 0.5; }

.action.is-quiet:disabled {
  opacity: 1;
  color: var(--emerald);
  border-color: rgba(30, 217, 138, 0.8);
}

.action:focus-visible, .arch-link:focus-visible, .brand:focus-visible {
  outline: 2px solid var(--emerald);
  outline-offset: 2px;
}

.link-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
}

.link-row a {
  color: var(--gold);
  text-decoration: none;
  border-bottom: 1px solid rgba(228, 195, 106, 0.45);
  min-height: 44px;
  display: inline-flex;
  align-items: center;
}

.timeline { display: flex; flex-direction: column; }

.step {
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr);
  gap: 8px;
  padding: 10px;
  border-radius: 0;
  border-bottom: none;
}

.step:first-child { border-radius: 16px 16px 0 0; }

.step:last-child { border-radius: 0 0 16px 16px; border-bottom: 1px solid var(--line); }

.step-node {
  width: 14px;
  height: 14px;
  margin: 4px auto 0;
  border-radius: 50%;
  background: var(--emerald);
  box-shadow: 0 0 10px var(--emerald);
  position: relative;
}

.step-node::after {
  content: "";
  position: absolute;
  left: 6px;
  top: 16px;
  width: 1px;
  height: 42px;
  background: rgba(228, 195, 106, 0.45);
}

.step:last-child .step-node::after { display: none; }

.step h3 { font-size: 1rem; margin-bottom: 4px; }

.step p { margin: 0; color: var(--cream); line-height: 1.4; }

.legend {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.legend li {
  display: grid;
  grid-template-columns: 54px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  padding: 8px;
}

.legend svg {
  width: 48px;
  height: 48px;
}

.legend p { margin: 2px 0 0; color: var(--stone); font-size: 0.88rem; }

.arch-nav {
  position: relative;
  flex: 0 0 auto;
  padding: 8px 6px calc(6px + env(safe-area-inset-bottom));
  background:
    radial-gradient(70% 120% at 50% -30%, rgba(228, 195, 106, 0.2), transparent 48%),
    linear-gradient(180deg, #3a151b, #12080a 58%);
  border-top: 1px solid var(--line);
}

.arch-nav::before {
  content: "";
  position: absolute;
  left: 14%;
  right: 14%;
  top: 0;
  height: 10px;
  border: 1px solid rgba(228, 195, 106, 0.4);
  border-top: none;
  border-radius: 0 0 40px 40px;
  pointer-events: none;
}

.arch-rail {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  min-height: 58px;
}

.arch-link {
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  color: var(--cream);
  text-decoration: none;
  padding: 4px 2px;
}

.arch-rune {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 1px solid rgba(228, 195, 106, 0.75);
  background: rgba(12, 6, 8, 0.72);
  display: grid;
  place-items: center;
  position: relative;
}

.arch-label {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: clamp(9px, 2.5vw, 11px);
  letter-spacing: 0.04em;
  color: var(--stone);
}

.arch-link.is-current .arch-rune {
  border-color: var(--emerald);
  background: rgba(30, 217, 138, 0.16);
  box-shadow: 0 0 12px rgba(30, 217, 138, 0.75);
}

.arch-link.is-current .arch-label { color: var(--emerald); }

.rune-lane::before {
  content: "";
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 9px solid var(--gold);
}

.rune-archive::before {
  content: "";
  width: 11px;
  height: 13px;
  border: 1.5px solid var(--gold);
  border-radius: 1px 4px 1px 1px;
}

.rune-trail::before {
  content: "";
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--gold);
  box-shadow: 6px 4px 0 var(--gold);
}

.rune-territory::before {
  content: "";
  width: 2px;
  height: 12px;
  background: var(--gold);
  box-shadow: -5px 0 0 var(--gold), 5px 0 0 var(--gold);
}

.rune-relic::before {
  content: "";
  width: 8px;
  height: 8px;
  background: var(--emerald);
  transform: rotate(45deg);
  box-shadow: 0 0 8px rgba(30, 217, 138, 0.8);
}

.run-shell {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.hud {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 4px 8px;
  padding: 2px 10px 4px;
}

.hud-metric { min-width: 0; }

.hud-metric span {
  display: block;
  color: var(--stone);
  font-size: 10px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.hud-metric strong {
  display: block;
  font-size: clamp(13px, 3.6vw, 16px);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.hud-tools {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.lives { display: inline-flex; gap: 4px; }

.lives i {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #3a2428;
  display: block;
}

.lives i.is-on {
  background: var(--emerald);
  box-shadow: 0 0 8px var(--emerald);
}

.key-pill {
  border: 1px solid rgba(228, 195, 106, 0.3);
  color: var(--stone);
  border-radius: 999px;
  padding: 4px 8px;
  font-size: 11px;
}

.key-pill.is-on {
  color: var(--ink);
  background: var(--gold);
  border-color: var(--gold);
}

.hud-tools button, .controls button {
  appearance: none;
  border: 1px solid var(--line);
  background: rgba(20, 8, 11, 0.8);
  color: var(--cream);
  border-radius: 12px;
  min-height: 36px;
  padding: 4px 8px;
}

.hud-tools button:last-child { margin-left: auto; }

.stage {
  position: relative;
  flex: 1;
  min-height: 0;
  touch-action: none;
}

.stage canvas {
  width: 100%;
  height: 100%;
  display: block;
}

.run-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  overflow: auto;
  padding: 12px;
  background: rgba(8, 4, 6, 0.62);
}

.overlay-card {
  margin: auto;
  width: min(100%, 380px);
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px;
  border-radius: 18px;
  border: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(48, 12, 16, 0.95), rgba(12, 6, 8, 0.96));
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.35);
}

.overlay-card p { margin: 0; line-height: 1.45; }

.overlay-best { color: var(--gold); font-size: 0.92rem; }

.overlay-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.controls {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
  padding: 6px 8px 8px;
}

.controls button {
  min-width: 0;
  min-height: 48px;
  white-space: nowrap;
  font-size: clamp(11px, 3.1vw, 14px);
  font-weight: 700;
}

body[data-mode="ready"] .controls,
body[data-mode="pause"] .controls,
body[data-mode="over"] .controls {
  opacity: 0.4;
  pointer-events: none;
}

@media (hover: hover) {
  .action:not(:disabled):hover { border-color: var(--emerald); }
}

@media (max-width: 340px) {
  .folio { grid-template-columns: minmax(0, 84px) minmax(0, 1fr); }
  .brand-copy em { display: none; }
}

@media (max-height: 560px) {
  .arch-label { display: none; }
  .arch-rail { min-height: 46px; }
  .hud-metric span { display: none; }
  .topbar { min-height: 44px; padding-top: calc(4px + env(safe-area-inset-top)); padding-bottom: 4px; }
  .brand-mark { width: 28px; height: 28px; flex-basis: 28px; }
  .controls button { min-height: 40px; }
}

@media (prefers-reduced-motion: no-preference) {
  .arch-link.is-current .arch-rune {
    animation: ember 2.6s ease-in-out infinite;
  }
}

@keyframes ember {
  50% { box-shadow: 0 0 16px rgba(30, 217, 138, 0.95); }
}
