:root {
  color-scheme: light;
  --bg-top: #dff0ff;
  --bg-mid: #b8daf5;
  --bg-bottom: #f8fcff;
  --text-primary: #082f49;
  --text-secondary: #0c4a6e;
  --text-muted: #155e75;
  --surface-strong: rgba(246, 251, 255, 0.88);
  --surface-soft: rgba(233, 245, 255, 0.72);
  --surface-elevated: rgba(255, 255, 255, 0.72);
  --surface-border: rgba(14, 116, 144, 0.18);
  --shadow-strong: rgba(8, 47, 73, 0.18);
  --shadow-soft: rgba(8, 47, 73, 0.11);
  --accent-cyan: #0891b2;
  --accent-orange: #ea580c;
  --accent-indigo: #1d4ed8;
  --accent-green: #15803d;
  --alert-info: #0f766e;
  --alert-warning: #b45309;
  --alert-error: #b91c1c;
  --button-text: #0c4a6e;
}

body[data-theme="light"] {
  color-scheme: light;
  --bg-top: #eaf4ff;
  --bg-mid: #c7def8;
  --bg-bottom: #fff5ea;
  --text-primary: #102a43;
  --text-secondary: #1f4f72;
  --text-muted: #5f4a32;
  --surface-strong: rgba(255, 249, 242, 0.86);
  --surface-soft: rgba(255, 241, 226, 0.7);
  --surface-elevated: rgba(255, 253, 249, 0.76);
  --surface-border: rgba(194, 122, 53, 0.18);
  --shadow-strong: rgba(77, 39, 15, 0.17);
  --shadow-soft: rgba(77, 39, 15, 0.1);
  --button-text: #7c2d12;
}

body[data-theme="dark"] {
  color-scheme: dark;
  /* Keep dark mode in a twilight register so the 3D world stays readable. */
  --bg-top: #1a3249;
  --bg-mid: #3e5d79;
  --bg-bottom: #6a4a42;
  --text-primary: #f7fbff;
  --text-secondary: #deefff;
  --text-muted: #c4dced;
  --surface-strong: rgba(33, 50, 72, 0.68);
  --surface-soft: rgba(51, 72, 97, 0.54);
  --surface-elevated: rgba(39, 57, 80, 0.6);
  --surface-border: rgba(166, 222, 255, 0.24);
  --shadow-strong: rgba(6, 12, 22, 0.22);
  --shadow-soft: rgba(6, 12, 22, 0.14);
  --accent-cyan: #67e8f9;
  --accent-orange: #fb923c;
  --accent-indigo: #93c5fd;
  --accent-green: #4ade80;
  --button-text: #f5fbff;
}

@media (prefers-color-scheme: dark) {
  body[data-theme="system"] {
    color-scheme: dark;
    --bg-top: #183047;
    --bg-mid: #395672;
    --bg-bottom: #64443d;
    --text-primary: #f5fbff;
    --text-secondary: #dceeff;
    --text-muted: #c0d8ea;
    --surface-strong: rgba(31, 49, 70, 0.7);
    --surface-soft: rgba(48, 69, 92, 0.56);
    --surface-elevated: rgba(37, 54, 77, 0.62);
    --surface-border: rgba(150, 218, 255, 0.22);
    --shadow-strong: rgba(5, 12, 21, 0.2);
    --shadow-soft: rgba(5, 12, 21, 0.13);
    --accent-cyan: #67e8f9;
    --accent-orange: #fb923c;
    --accent-indigo: #93c5fd;
    --accent-green: #4ade80;
    --button-text: #f8fbff;
  }
}

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

html,
body {
  height: 100%;
}

body {
  font-family: "Avenir Next", "SF Pro Text", "Segoe UI", sans-serif;
  color: var(--text-primary);
  background:
    radial-gradient(circle at top, rgba(255, 255, 255, 0.22), transparent 40%),
    linear-gradient(180deg, var(--bg-top) 0%, var(--bg-mid) 48%, var(--bg-bottom) 100%);
  overflow-y: scroll;
  overflow-x: hidden;
  transition:
    background 320ms ease,
    color 320ms ease;
}

#scene-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  z-index: 0;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.letterbox-bar {
  position: fixed;
  left: 0;
  width: 100%;
  height: 0;
  z-index: 7;
  background: linear-gradient(180deg, rgba(2, 6, 23, 0.96), rgba(2, 6, 23, 0.84));
  transition: height 560ms cubic-bezier(0.19, 1, 0.22, 1);
  pointer-events: none;
}

#letterbox-top {
  top: 0;
}

#letterbox-bottom {
  bottom: 0;
}

.letterbox-bar.active {
  height: clamp(14px, 3.2vh, 34px);
}

#loading-hud,
.alert-banner,
#replay-button,
.utility-button {
  border: 1px solid var(--surface-border);
  background: linear-gradient(135deg, var(--surface-strong), var(--surface-soft));
  backdrop-filter: blur(14px) saturate(1.08);
  box-shadow:
    0 20px 48px var(--shadow-strong),
    0 8px 22px var(--shadow-soft);
}

#loading-hud {
  position: fixed;
  inset: auto auto 1.2rem 1.2rem;
  z-index: 10;
  width: min(23rem, calc(100vw - 2.4rem));
  padding: 0.95rem 1rem 1rem;
  border-radius: 1rem;
  transition:
    opacity 380ms ease,
    transform 380ms ease,
    visibility 380ms ease;
}

#loading-hud.is-hidden {
  opacity: 0;
  transform: translateY(10px);
  visibility: hidden;
}

#loading-hud[hidden] {
  display: none;
}

.loading-kicker {
  font-size: 0.72rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--accent-indigo);
  margin-bottom: 0.28rem;
}

#loading-label {
  font-size: 0.95rem;
  color: var(--text-primary);
  margin-bottom: 0.2rem;
}

#loading-percent {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-bottom: 0.52rem;
}

.loading-track {
  height: 0.4rem;
  border-radius: 999px;
  background: rgba(14, 116, 144, 0.16);
}

#loading-fill {
  height: 100%;
  width: 0%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--accent-cyan), var(--accent-orange));
  transition: width 240ms cubic-bezier(0.22, 1, 0.36, 1);
}

.alert-banner {
  position: fixed;
  top: 1.2rem;
  right: 1.2rem;
  z-index: 11;
  width: min(25rem, calc(100vw - 2.4rem));
  border-radius: 1rem;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.85rem;
  align-items: start;
  padding: 0.9rem 1rem;
}

.alert-banner[hidden] {
  display: none;
}

.alert-banner[data-tone="info"] .alert-dot {
  background: var(--alert-info);
}

.alert-banner[data-tone="warning"] .alert-dot {
  background: var(--alert-warning);
}

.alert-banner[data-tone="error"] .alert-dot {
  background: var(--alert-error);
}

.alert-dot {
  width: 0.8rem;
  height: 0.8rem;
  border-radius: 999px;
  margin-top: 0.32rem;
  box-shadow: 0 0 0 0.24rem rgba(255, 255, 255, 0.12);
}

.alert-copy {
  min-width: 0;
}

#alert-title {
  font-size: 0.96rem;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 0.18rem;
}

#alert-body {
  font-size: 0.88rem;
  line-height: 1.45;
  color: var(--text-secondary);
}

#overlay {
  position: fixed;
  inset: 0;
  z-index: 3;
  display: grid;
  grid-template-columns: minmax(15rem, 18.5rem) minmax(0, 1fr) minmax(12.5rem, 15rem);
  grid-template-rows: auto minmax(0, 1fr);
  grid-template-areas:
    "top top top"
    "left . right";
  align-items: stretch;
  gap: clamp(0.9rem, 2vw, 1.5rem);
  width: 100%;
  min-height: 100vh;
  padding: clamp(1rem, 2vw, 2.1rem);
  pointer-events: none;
}

#overlay > * {
  pointer-events: auto;
}

.top-bar {
  grid-area: top;
  display: flex;
  justify-content: flex-end;
  width: 100%;
}

.top-bar-actions {
  display: flex;
  align-items: center;
  gap: 0.7rem;
}

.left-rail,
.right-rail {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.left-rail {
  grid-area: left;
  justify-content: space-between;
  max-width: 16.5rem;
  padding-block: clamp(0.2rem, 1vh, 0.8rem) 0.35rem;
}

.right-rail {
  grid-area: right;
  justify-self: end;
  justify-content: flex-end;
  align-items: stretch;
  width: 100%;
  max-width: 15rem;
  padding-block: clamp(5.2rem, 16vh, 9rem) 0.35rem;
}

.utility-button,
#replay-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 2.85rem;
  padding: 0.74rem 1rem;
  border-radius: 999px;
  color: var(--button-text);
  font-size: 0.92rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  cursor: pointer;
  transition:
    transform 180ms ease,
    box-shadow 180ms ease,
    border-color 180ms ease,
    background 180ms ease;
}

.utility-button {
  font-size: 0.84rem;
  padding-inline: 0.95rem;
}

.control-menu {
  position: relative;
}

.control-menu summary {
  list-style: none;
}

.control-menu summary::-webkit-details-marker {
  display: none;
}

.control-menu-summary {
  min-width: 8.9rem;
  justify-content: space-between;
}

.control-menu-summary::after {
  content: "▾";
  margin-left: 0.55rem;
  font-size: 0.7rem;
  transform-origin: center;
  transition: transform 180ms ease;
}

.control-menu[open] .control-menu-summary::after {
  transform: rotate(180deg);
}

.control-menu-panel {
  position: absolute;
  top: calc(100% + 0.55rem);
  right: 0;
  display: grid;
  gap: 0.8rem;
  min-width: min(21rem, calc(100vw - 2rem));
  padding: 0.9rem;
  border-radius: 1rem;
  border: 1px solid var(--surface-border);
  background: linear-gradient(135deg, var(--surface-strong), var(--surface-soft));
  backdrop-filter: blur(16px) saturate(1.1);
  box-shadow:
    0 18px 42px var(--shadow-strong),
    0 8px 20px var(--shadow-soft);
}

body[data-resolved-theme="dark"] .control-menu-panel {
  background: linear-gradient(
    135deg,
    color-mix(in srgb, var(--surface-strong) 94%, rgba(255, 255, 255, 0.08)),
    color-mix(in srgb, var(--surface-soft) 88%, rgba(255, 255, 255, 0.05))
  );
}

.control-menu-section + .control-menu-section {
  border-top: 1px solid color-mix(in srgb, var(--surface-border) 90%, transparent);
  padding-top: 0.8rem;
}

.control-menu-label {
  font-size: 0.71rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
}

.control-menu-status {
  display: grid;
  gap: 0.48rem;
}

.control-menu-status-row {
  position: relative;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 0.8rem;
  padding: 0.58rem 0.7rem 0.58rem 1rem;
  border-radius: 0.88rem;
  background: color-mix(in srgb, var(--surface-elevated) 90%, transparent);
}

.control-menu-status-row::before {
  content: "";
  position: absolute;
  left: 0.42rem;
  top: 50%;
  width: 0.4rem;
  height: 0.4rem;
  border-radius: 999px;
  transform: translateY(-50%);
}

.control-menu-status-row[data-tone="quality"]::before {
  background: var(--accent-cyan);
}

.control-menu-status-row[data-tone="chapter"]::before {
  background: var(--accent-orange);
}

.control-menu-status-row[data-tone="scene"]::before {
  background: var(--accent-green);
}

.control-menu-status-row[data-tone="audio"]::before {
  background: var(--accent-indigo);
}

.control-menu-status-row dt {
  font-size: 0.76rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.control-menu-status-row dd {
  min-width: 0;
  text-align: right;
  font-size: 0.88rem;
  color: var(--text-primary);
}

.control-menu-actions {
  display: grid;
  gap: 0.6rem;
}

.control-menu-actions .utility-button,
.control-menu-actions #replay-button {
  width: 100%;
  justify-content: flex-start;
}

.utility-button-primary {
  background: linear-gradient(
    135deg,
    color-mix(in srgb, var(--accent-cyan) 24%, var(--surface-strong)),
    color-mix(in srgb, var(--accent-indigo) 18%, var(--surface-soft))
  );
}

.utility-button:hover,
#replay-button:hover {
  transform: translateY(-1px);
  border-color: rgba(14, 116, 144, 0.42);
  box-shadow:
    0 16px 28px var(--shadow-strong),
    0 8px 20px var(--shadow-soft);
}

.utility-button:active,
#replay-button:active {
  transform: translateY(0);
}

.utility-button:focus-visible,
#replay-button:focus-visible {
  outline: 2px solid color-mix(in srgb, var(--accent-cyan) 70%, white);
  outline-offset: 2px;
}

.brand {
  max-width: 13.4rem;
  padding-left: 0.2rem;
  opacity: 0.82;
  transition:
    opacity 560ms cubic-bezier(0.19, 1, 0.22, 1),
    transform 560ms cubic-bezier(0.19, 1, 0.22, 1),
    filter 560ms ease;
}

body[data-ride-phase="journey"] .brand {
  opacity: 0.16;
  transform: translateY(-0.72rem) scale(0.985);
  filter: blur(0.7px);
}

.kicker {
  display: inline-block;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  font-size: 0.7rem;
  color: var(--accent-indigo);
  margin-bottom: 0.58rem;
}

.brand h1 {
  font-size: clamp(1.72rem, 3.5vw, 2.7rem);
  letter-spacing: -0.03em;
  line-height: 0.92;
  text-wrap: balance;
  color: var(--text-primary);
  text-shadow: 0 10px 28px rgba(255, 255, 255, 0.12);
}

.brand p {
  margin-top: 0.52rem;
  color: var(--text-secondary);
  font-size: 0.84rem;
  max-width: 12.6rem;
  line-height: 1.45;
  opacity: 0.82;
  transition:
    opacity 420ms ease,
    transform 420ms ease;
}

body[data-ride-phase="journey"] .brand p {
  opacity: 0;
  transform: translateY(-0.4rem);
}

.story-panel {
  width: min(100%, 17.2rem);
  max-width: 17.2rem;
  border-radius: 1.4rem;
  padding: 1rem 1rem 1.08rem;
  position: relative;
  overflow: hidden;
  border: 1px solid color-mix(in srgb, var(--surface-border) 54%, rgba(255, 255, 255, 0.28));
  background: linear-gradient(
    180deg,
    color-mix(in srgb, var(--surface-strong) 43%, transparent),
    color-mix(in srgb, var(--surface-soft) 26%, transparent)
  );
  backdrop-filter: blur(20px) saturate(1.08);
  box-shadow:
    0 12px 28px color-mix(in srgb, var(--shadow-strong) 74%, transparent),
    0 4px 14px color-mix(in srgb, var(--shadow-soft) 78%, transparent),
    inset 0 1px 0 rgba(255, 255, 255, 0.14);
  transition:
    transform 620ms cubic-bezier(0.19, 1, 0.22, 1),
    opacity 620ms ease,
    filter 620ms ease,
    box-shadow 620ms ease;
}

body[data-resolved-theme="dark"] .story-panel {
  background: linear-gradient(
    180deg,
    color-mix(in srgb, var(--surface-strong) 62%, transparent),
    color-mix(in srgb, var(--surface-soft) 48%, transparent)
  );
  box-shadow:
    0 12px 28px color-mix(in srgb, var(--shadow-strong) 66%, transparent),
    0 4px 14px color-mix(in srgb, var(--shadow-soft) 64%, transparent),
    inset 0 1px 0 rgba(255, 255, 255, 0.18);
}

.story-panel::before {
  content: "";
  position: absolute;
  inset: 0 auto 0 0;
  width: 0.14rem;
  background: linear-gradient(180deg, var(--accent-cyan), rgba(255, 255, 255, 0));
  opacity: 0.72;
}

.story-panel::after {
  content: "";
  position: absolute;
  inset: auto 0 0 0;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.26), transparent);
}

.story-panel.is-swapping {
  transform: translateY(8px) scale(0.992);
  opacity: 0.38;
  filter: blur(2.5px);
}

.story-step {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--accent-cyan);
  margin-bottom: 0.52rem;
}

.story-panel h2 {
  font-size: clamp(1.3rem, 2vw, 1.72rem);
  line-height: 1.08;
  margin-bottom: 0.58rem;
  color: var(--text-primary);
}

.story-panel p {
  color: var(--text-secondary);
  line-height: 1.52;
}

.progress-track {
  margin-top: 0.88rem;
  height: 0.28rem;
  border-radius: 999px;
  background: rgba(14, 116, 144, 0.12);
}

.progress-fill {
  height: 100%;
  width: 0%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--accent-cyan), var(--accent-orange));
  transition: width 240ms cubic-bezier(0.22, 1, 0.36, 1);
}

.assist-panel {
  display: block;
  width: min(100%, 14.6rem);
  max-width: 14.6rem;
  align-self: flex-end;
}

.assist-card {
  min-width: 0;
  border-radius: 1.18rem;
  padding: 0.92rem 0.94rem 0.98rem;
  border: 1px solid color-mix(in srgb, var(--surface-border) 48%, rgba(255, 255, 255, 0.24));
  background: linear-gradient(
    180deg,
    color-mix(in srgb, var(--surface-strong) 38%, transparent),
    color-mix(in srgb, var(--surface-soft) 22%, transparent)
  );
  backdrop-filter: blur(18px) saturate(1.06);
  box-shadow:
    0 10px 24px color-mix(in srgb, var(--shadow-strong) 66%, transparent),
    0 4px 12px color-mix(in srgb, var(--shadow-soft) 70%, transparent),
    inset 0 1px 0 rgba(255, 255, 255, 0.12);
}

body[data-resolved-theme="dark"] .assist-card {
  background: linear-gradient(
    180deg,
    color-mix(in srgb, var(--surface-strong) 58%, transparent),
    color-mix(in srgb, var(--surface-soft) 44%, transparent)
  );
  box-shadow:
    0 10px 24px color-mix(in srgb, var(--shadow-strong) 62%, transparent),
    0 4px 12px color-mix(in srgb, var(--shadow-soft) 58%, transparent),
    inset 0 1px 0 rgba(255, 255, 255, 0.16);
}

.assist-kicker {
  font-size: 0.64rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--accent-indigo);
  margin-bottom: 0.46rem;
}

.assist-focus {
  display: grid;
  gap: 0.34rem;
}

.assist-label {
  font-size: 0.69rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.assist-value {
  font-size: 0.9rem;
  line-height: 1.5;
  color: var(--text-primary);
}

.assist-list {
  display: grid;
  gap: 0.62rem;
  margin-top: 0.78rem;
  padding-top: 0.74rem;
  border-top: 1px solid color-mix(in srgb, var(--surface-border) 84%, transparent);
}

.assist-item {
  display: grid;
  gap: 0.3rem;
}

.assist-item + .assist-item {
  padding-top: 0.72rem;
  border-top: 1px solid color-mix(in srgb, var(--surface-border) 68%, transparent);
}

.assist-item .assist-label {
  margin: 0;
}

.assist-detail {
  font-size: 0.82rem;
  line-height: 1.46;
  color: var(--text-secondary);
}

.hint {
  max-width: 12.8rem;
  font-size: 0.72rem;
  line-height: 1.46;
  color: var(--text-muted);
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.08);
  opacity: 0.55;
  padding-right: 0.12rem;
}

body[data-resolved-theme="dark"] .hint {
  color: color-mix(in srgb, var(--text-muted) 94%, white 6%);
  opacity: 0.76;
}

#scroll-spacer {
  position: relative;
  z-index: 1;
  height: 520vh;
}

.noscript-warning {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  z-index: 12;
  max-width: 24rem;
  font-size: 0.88rem;
  padding: 0.82rem 0.92rem;
  border-radius: 0.9rem;
  color: #fef2f2;
  background: rgba(127, 29, 29, 0.96);
  border: 1px solid rgba(252, 165, 165, 0.44);
}

@media (max-width: 860px) {
  .alert-banner {
    left: 1rem;
    right: 1rem;
    width: auto;
  }

  #overlay {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto;
    grid-template-areas:
      "top"
      "left"
      "right";
    gap: 0.75rem;
    align-content: start;
  }

  .top-bar {
    justify-content: flex-start;
  }

  .top-bar-actions {
    width: 100%;
    justify-content: flex-start;
  }

  .control-menu {
    justify-self: start;
  }

  .control-menu-panel {
    left: 0;
    right: auto;
  }

  .utility-button,
  #replay-button {
    width: auto;
    min-width: 0;
    justify-content: flex-start;
  }

  .left-rail,
  .right-rail,
  .story-panel,
  .brand,
  .assist-panel,
  .hint {
    max-width: none;
  }

  .left-rail,
  .right-rail {
    width: 100%;
    padding-block: 0;
    gap: 0.75rem;
    justify-content: flex-start;
  }

  .right-rail {
    justify-self: stretch;
    align-items: stretch;
  }

  .brand {
    padding-left: 0;
  }

  .story-panel,
  .assist-panel {
    width: 100%;
    max-width: none;
  }

  .hint {
    max-width: 22rem;
  }
}

@media (max-width: 560px) {
  #overlay {
    padding: 0.95rem;
  }

  #loading-hud {
    inset: auto 0.95rem 0.95rem 0.95rem;
    width: auto;
  }

  .brand h1 {
    font-size: clamp(1.92rem, 12vw, 3rem);
  }

  .story-panel {
    padding: 1rem;
  }

  .control-menu-panel {
    min-width: min(18.5rem, calc(100vw - 1.9rem));
  }

  .top-bar-actions {
    gap: 0.5rem;
  }
}

@media (orientation: landscape) and (max-height: 720px) {
  #overlay {
    grid-template-columns: minmax(13.2rem, 16rem) minmax(0, 1fr) minmax(11.4rem, 13.4rem);
    grid-template-rows: auto 1fr;
    grid-template-areas:
      "top top top"
      "left . right";
    gap: 0.9rem;
  }

  .left-rail {
    max-width: 14.5rem;
  }

  .brand {
    max-width: 12rem;
  }

  .story-panel {
    width: min(100%, 14.8rem);
  }

  .assist-panel {
    width: min(100%, 13.4rem);
  }

  .hint {
    max-width: 11.6rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
