:root {
  color-scheme: light;
  --ink: #111827;
  --muted: #5d6a7d;
  --line: rgba(48, 70, 95, 0.28);
  --glass: rgba(244, 249, 253, 0.84);
  --panel-shadow: rgba(24, 45, 68, 0.24);
  --blue: #2bb8ff;
}

body[data-theme="night"] {
  color-scheme: dark;
  --ink: #eef6ff;
  --muted: #b7c7d8;
  --line: rgba(144, 190, 230, 0.28);
  --glass: rgba(8, 17, 29, 0.76);
  --panel-shadow: rgba(0, 0, 0, 0.48);
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
  scrollbar-width: none;
}

body {
  margin: 0;
  min-height: 100vh;
  overflow-x: hidden;
  color: var(--ink);
  background: #f7fbff;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    "Microsoft YaHei", sans-serif;
}

html::-webkit-scrollbar,
body::-webkit-scrollbar,
#scroll-stage::-webkit-scrollbar,
.overview::-webkit-scrollbar {
  display: none;
}

#scroll-stage,
.overview {
  -ms-overflow-style: none;
  scrollbar-width: none;
}

body[data-theme="night"] {
  background: #04080f;
}

#scene {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  z-index: 0;
}

#scroll-stage {
  position: relative;
  z-index: 1;
}

.scroll-space {
  height: 1200vh;
}

.topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 8;
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 18px clamp(18px, 4vw, 54px);
  pointer-events: none;
}

.brand,
.nav a,
.eyebrow,
.meta-row {
  font-size: 11px;
  line-height: 1.3;
  letter-spacing: 0.18em;
  text-transform: uppercase;
}

.brand {
  color: rgba(17, 24, 39, 0.76);
  font-weight: 800;
}

body[data-theme="night"] .brand {
  color: rgba(238, 246, 255, 0.86);
  text-shadow: 0 0 18px rgba(43, 184, 255, 0.24);
}

.nav {
  display: flex;
  gap: 14px;
  margin-left: auto;
  pointer-events: auto;
}

.nav a {
  color: rgba(17, 24, 39, 0.72);
  text-decoration: none;
  padding: 9px 0;
  border-bottom: 1px solid transparent;
}

body[data-theme="night"] .nav a {
  color: rgba(238, 246, 255, 0.72);
}

.nav a:hover {
  color: var(--ink);
  border-color: rgba(43, 184, 255, 0.7);
}

.toolbar {
  display: flex;
  gap: 10px;
  align-items: center;
  pointer-events: auto;
}

.segmented {
  display: inline-flex;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.66);
  box-shadow: 0 12px 30px rgba(43, 72, 102, 0.12);
  backdrop-filter: blur(14px);
}

body[data-theme="night"] .segmented {
  background: rgba(6, 13, 23, 0.7);
  box-shadow: 0 12px 34px rgba(0, 0, 0, 0.34);
}

.segmented button {
  min-width: 48px;
  min-height: 34px;
  padding: 0 13px;
  border: 0;
  border-right: 1px solid var(--line);
  color: var(--muted);
  background: transparent;
  font: inherit;
  font-size: 12px;
  font-weight: 800;
  cursor: pointer;
}

.segmented button:last-child {
  border-right: 0;
}

.segmented button.is-active {
  color: #061523;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.96), rgba(202, 235, 255, 0.9));
}

body[data-theme="night"] .segmented button.is-active {
  color: #fff;
  background: linear-gradient(180deg, rgba(42, 126, 172, 0.78), rgba(12, 37, 66, 0.9));
  box-shadow: inset 0 0 18px rgba(43, 184, 255, 0.22);
}

.rail {
  position: fixed;
  right: 24px;
  top: 50%;
  z-index: 7;
  width: 2px;
  height: 36vh;
  transform: translateY(-50%);
  background: rgba(17, 24, 39, 0.22);
  cursor: grab;
  pointer-events: auto;
  touch-action: none;
}

.rail::before {
  content: "";
  position: absolute;
  inset: -10px -14px;
}

body[data-theme="night"] .rail {
  background: rgba(218, 239, 255, 0.18);
}

.rail-dot {
  position: absolute;
  left: -4px;
  top: 0;
  width: 10px;
  height: 10px;
  border-radius: 999px;
  background: var(--blue);
  box-shadow: 0 0 18px rgba(43, 184, 255, 0.8);
  pointer-events: none;
}

.rail.is-dragging {
  cursor: grabbing;
}

.chapter {
  position: fixed;
  z-index: 4;
  opacity: 0;
  visibility: hidden;
  transition:
    opacity 260ms ease,
    transform 260ms ease,
    visibility 260ms ease;
}

.chapter.is-visible {
  opacity: 1;
  visibility: visible;
}

.chapter-intro {
  top: 50%;
  left: 50%;
  width: min(720px, calc(100vw - 44px));
  transform: translate(-50%, -52%);
  color: #172033;
  text-align: center;
  text-shadow: 0 1px 18px rgba(255, 255, 255, 0.92);
}

body[data-theme="night"] .chapter-intro {
  color: #eef6ff;
  text-shadow: 0 2px 24px rgba(0, 0, 0, 0.75);
}

.eyebrow {
  margin: 0 0 12px;
  color: rgba(13, 92, 145, 0.92);
  font-weight: 800;
}

body[data-theme="night"] .eyebrow {
  color: rgba(92, 211, 255, 0.92);
}

h1,
h2 {
  margin: 0;
  margin-inline: auto;
  max-width: 13ch;
  font-weight: 800;
  letter-spacing: 0;
}

h1 {
  font-size: clamp(42px, 7vw, 86px);
  line-height: 0.98;
}

h2 {
  font-size: clamp(34px, 5vw, 68px);
  line-height: 1;
}

.chapter p:not(.eyebrow),
.overview-lead {
  margin-left: auto;
  margin-right: auto;
  max-width: 650px;
  color: var(--muted);
  font-size: clamp(15px, 1.5vw, 18px);
  line-height: 1.8;
}

.book-panel {
  top: 50%;
  left: 50%;
  display: grid;
  grid-template-columns: minmax(170px, 260px) minmax(260px, 500px);
  gap: clamp(22px, 4vw, 58px);
  align-items: center;
  width: min(860px, calc(100vw - 44px));
  transform: translate(-50%, calc(-42% + var(--panel-shift, 0%)));
  pointer-events: none;
}

.book-cover {
  position: relative;
  aspect-ratio: 0.68;
  overflow: hidden;
  border: 2px solid rgba(255, 255, 255, 0.86);
  border-radius: 8px;
  background:
    linear-gradient(145deg, rgba(255, 255, 255, 0.08), rgba(255, 255, 255, 0)),
    var(--cover-image),
    linear-gradient(155deg, #111c2c, #265b88 45%, #d9efff);
  background-size: cover, cover, cover;
  background-position: center;
  box-shadow:
    0 34px 94px rgba(8, 24, 42, 0.34);
}

.book-cover span {
  display: none;
}

.book-copy {
  padding: clamp(18px, 3vw, 30px);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--glass);
  box-shadow: 0 24px 78px var(--panel-shadow);
  backdrop-filter: blur(18px);
  pointer-events: auto;
}

body[data-theme="night"] .book-copy {
  border-color: rgba(99, 184, 240, 0.32);
}

.book-copy h2 {
  max-width: 10ch;
}

.meta-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin: 20px 0 24px;
  color: rgba(17, 24, 39, 0.72);
  font-weight: 800;
}

body[data-theme="night"] .meta-row {
  color: rgba(218, 238, 255, 0.72);
}

.meta-row span {
  border-top: 1px solid var(--line);
  padding-top: 8px;
}

.primary-link {
  display: inline-flex;
  align-items: center;
  min-height: 42px;
  padding: 0 18px;
  border: 1px solid rgba(21, 76, 118, 0.24);
  border-radius: 999px;
  color: #063b5d;
  background: rgba(255, 255, 255, 0.86);
  text-decoration: none;
  font-weight: 800;
}

body[data-theme="night"] .primary-link {
  color: #eaf8ff;
  background: rgba(24, 82, 122, 0.58);
  border-color: rgba(89, 204, 255, 0.42);
}

.primary-link:hover {
  border-color: rgba(43, 184, 255, 0.72);
}

.overview {
  position: fixed;
  inset: 0;
  z-index: 5;
  display: block;
  padding: 76px clamp(18px, 4vw, 58px) 56px;
  opacity: 0;
  visibility: hidden;
  transform: translateY(18px);
  overflow-y: auto;
  overscroll-behavior: contain;
  pointer-events: none;
  transition:
    opacity 380ms ease,
    transform 380ms ease,
    visibility 380ms ease;
  background:
    linear-gradient(180deg, rgba(241, 247, 252, 0.82), rgba(219, 231, 242, 0.94)),
    radial-gradient(circle at 50% 20%, rgba(255, 255, 255, 0.98), transparent 40%);
  backdrop-filter: blur(16px);
}

.site-footer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 9;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 34px;
  padding: 8px clamp(18px, 4vw, 54px);
  border-top: 1px solid rgba(58, 56, 54, 0.78);
  background: rgba(8, 8, 8, 0.88);
  color: rgba(245, 242, 237, 0.42);
  font-size: 8px;
  font-weight: 800;
  letter-spacing: 0.22em;
  text-align: center;
  text-transform: uppercase;
  backdrop-filter: blur(14px);
  pointer-events: none;
}

body[data-theme="day"] .site-footer {
  border-top-color: rgba(72, 69, 62, 0.28);
  background: rgba(32, 31, 29, 0.82);
  color: rgba(245, 242, 237, 0.52);
}

body[data-theme="night"] .overview {
  background:
    linear-gradient(180deg, rgba(4, 8, 15, 0.78), rgba(5, 12, 22, 0.94)),
    radial-gradient(circle at 50% 20%, rgba(38, 95, 132, 0.28), transparent 42%);
}

.overview.is-visible {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
  pointer-events: auto;
}

.overview-inner {
  width: min(1180px, 100%);
  margin: 0 auto;
  padding-bottom: 48px;
}

.overview h2 {
  max-width: 650px;
  margin-inline: auto;
  text-align: left;
}

.archive-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-top: 28px;
}

.archive-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 24px;
}

.archive-tabs button,
.story-book,
.graph-chip {
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.72);
  font: inherit;
  font-size: 13px;
  font-weight: 800;
  cursor: pointer;
}

body[data-theme="night"] .archive-tabs button,
body[data-theme="night"] .story-book,
body[data-theme="night"] .graph-chip {
  background: rgba(8, 17, 29, 0.74);
}

.archive-tabs button {
  min-height: 38px;
  padding: 0 16px;
}

.archive-tabs button.is-active,
.story-book.is-active,
.graph-chip.is-active {
  color: var(--ink);
  border-color: rgba(43, 184, 255, 0.58);
  box-shadow: 0 0 22px rgba(43, 184, 255, 0.14);
}

.final-view {
  display: none;
  margin-top: 18px;
}

.final-view.is-active {
  display: block;
}

.storyline-layout {
  display: grid;
  grid-template-columns: 250px minmax(0, 1fr);
  gap: 16px;
  min-height: 430px;
}

.story-books {
  display: grid;
  align-content: start;
  gap: 10px;
}

.story-book {
  display: grid;
  grid-template-columns: 42px 1fr;
  gap: 10px;
  align-items: center;
  min-height: 72px;
  padding: 10px 12px;
  border-radius: 8px;
  text-align: left;
}

.story-book-cover {
  display: block;
  width: 42px;
  aspect-ratio: 0.72;
  border: 1px solid currentColor;
  border-radius: 6px;
  background-color: rgba(8, 18, 32, 0.72);
  background-position: center;
  background-size: cover;
  box-shadow: 0 10px 22px rgba(0, 0, 0, 0.24);
}

.story-book strong {
  display: block;
  color: var(--ink);
  line-height: 1.25;
}

.story-book span:last-child {
  display: block;
  margin-top: 3px;
  font-size: 11px;
  color: var(--muted);
}

.story-stage {
  min-width: 0;
}

.story-canvas {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(8, 18, 32, 0.72);
  cursor: grab;
  user-select: none;
  touch-action: pan-y;
  scrollbar-width: none;
}

.story-canvas::-webkit-scrollbar {
  display: none;
}

.story-canvas.is-dragging {
  cursor: grabbing;
}

body[data-theme="day"] .story-canvas {
  background: rgba(10, 32, 54, 0.84);
}

.story-svg {
  display: block;
  transform-origin: 0 0;
}

.story-node {
  cursor: pointer;
}

.story-node:hover .story-dot,
.story-node.is-active .story-dot {
  fill: #fff;
  stroke-width: 3;
}

.story-label {
  fill: #dbe7f6;
  font-size: 12px;
  font-weight: 700;
}

.story-label-small {
  fill: #a9bed6;
  font-size: 10.5px;
}

.story-branch-label {
  font-size: 10px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.story-detail {
  min-height: 108px;
  margin-top: 12px;
  padding: 15px 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--glass);
  box-shadow: 0 14px 44px var(--panel-shadow);
}

.story-detail h3 {
  margin: 5px 0 7px;
  color: var(--ink);
  font-size: 20px;
}

.story-detail p {
  margin: 0;
  color: var(--muted);
  line-height: 1.65;
}

.story-detail-meta {
  color: var(--blue);
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.graph-wrap {
  position: relative;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background:
    radial-gradient(circle at 50% 42%, rgba(43, 184, 255, 0.14), transparent 42%),
    rgba(8, 18, 32, 0.82);
}

.relation-graph {
  display: block;
  width: 100%;
  height: 430px;
  cursor: grab;
  touch-action: none;
  user-select: none;
}

.graph-chips {
  position: absolute;
  z-index: 2;
  top: 12px;
  left: 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
}

.graph-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 28px;
  padding: 0 10px;
  font-size: 11px;
}

.graph-chip-dot {
  width: 9px;
  height: 9px;
  border-radius: 999px;
}

.graph-tip {
  position: absolute;
  right: 14px;
  bottom: 12px;
  color: rgba(219, 231, 246, 0.7);
  font-size: 12px;
  pointer-events: none;
}

.archive-card {
  display: block;
  min-height: 162px;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.82);
  box-shadow: 0 14px 44px rgba(65, 91, 118, 0.16);
  color: inherit;
  text-decoration: none;
  transition: border-color 160ms ease, box-shadow 160ms ease, transform 160ms ease;
}

a.archive-card:hover {
  border-color: rgba(43, 184, 255, 0.68);
  box-shadow: 0 18px 54px rgba(43, 184, 255, 0.18);
  transform: translateY(-2px);
}

body[data-theme="night"] .archive-card {
  background: rgba(8, 17, 29, 0.72);
}

.archive-thumb {
  display: block;
  width: 54px;
  aspect-ratio: 0.68;
  float: right;
  margin-left: 12px;
  border-radius: 5px;
  background-size: cover;
  background-position: center;
  box-shadow: 0 10px 26px rgba(0, 0, 0, 0.22);
}

.archive-card strong {
  display: block;
  margin-bottom: 8px;
  color: var(--ink);
  font-size: 19px;
}

.archive-card p {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.7;
}

.archive-card-link {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  margin-top: 14px;
  color: var(--accent);
  font-size: 13px;
  font-weight: 900;
}

.archive-card.is-empty {
  opacity: 0.56;
}

@media (max-width: 820px) {
  .topbar {
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 12px;
    padding: 12px 16px 6px;
  }

  .brand {
    order: 1;
  }

  .toolbar {
    order: 2;
    gap: 6px;
    margin-left: auto;
  }

  .nav {
    order: 3;
    width: 100%;
    margin-left: 0;
    gap: 18px;
  }

  .nav a {
    padding: 4px 0;
  }

  /* The topbar wraps to two rows here; give it a soft backing so scrolled
     archive cards don't run underneath the links. */
  .topbar {
    background: linear-gradient(180deg, rgba(238, 232, 220, 0.92) 55%, rgba(238, 232, 220, 0));
  }

  body[data-theme="night"] .topbar {
    background: linear-gradient(180deg, rgba(4, 8, 15, 0.9) 55%, rgba(4, 8, 15, 0));
  }

  .overview {
    padding-top: 112px;
  }

  .segmented button {
    min-width: 42px;
    padding: 0 9px;
  }

  .rail {
    right: 14px;
  }

  .chapter-intro {
    top: 50%;
  }

  .book-panel {
    grid-template-columns: 120px minmax(0, 1fr);
    gap: 14px;
    width: calc(100vw - 30px);
  }

  .book-copy {
    padding: 16px;
  }

  .book-copy h2 {
    font-size: 32px;
  }

  .book-copy p {
    font-size: 14px;
    line-height: 1.55;
  }

  .meta-row {
    margin: 14px 0;
  }

  .archive-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .storyline-layout {
    grid-template-columns: 1fr;
  }

  .story-books {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 520px) {
  .topbar {
    flex-wrap: wrap;
  }

  .brand {
    width: 100%;
  }

  .brand {
    width: auto;
  }

  .book-panel {
    top: auto;
    bottom: 46px;
    grid-template-columns: 76px minmax(0, 1fr);
    gap: 10px;
    align-items: end;
    width: calc(100vw - 32px);
    transform: translate(-50%, var(--panel-shift, 0%));
  }

  .book-cover {
    border-width: 1px;
  }

  .book-copy {
    padding: 12px 14px;
  }

  .book-copy h2 {
    font-size: 24px;
  }

  #book-description {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 4;
    overflow: hidden;
  }

  .meta-row {
    margin: 8px 0;
  }

  .archive-grid {
    grid-template-columns: 1fr;
  }

  .story-books {
    grid-template-columns: 1fr;
  }
}

/* Phones held sideways: little vertical room, so tighten the chrome and the panel. */
@media (max-height: 520px) and (orientation: landscape) {
  .topbar {
    flex-wrap: nowrap;
    padding: 8px 16px;
  }

  .nav {
    order: 2;
    width: auto;
    margin-left: auto;
  }

  .toolbar {
    order: 3;
    margin-left: 0;
  }

  .segmented button {
    min-height: 30px;
  }

  .rail {
    height: 50vh;
  }

  .overview {
    padding-top: 60px;
  }

  .book-panel {
    top: 54%;
    bottom: auto;
    grid-template-columns: minmax(84px, 22vh) minmax(0, 1fr);
    gap: 14px;
    align-items: center;
    width: min(620px, calc(100vw - 72px));
    transform: translate(-50%, calc(-50% + var(--panel-shift, 0%)));
  }

  .book-copy {
    padding: 12px 16px;
  }

  .book-copy h2 {
    font-size: 24px;
  }

  #book-description {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 3;
    overflow: hidden;
  }

  .meta-row {
    margin: 8px 0;
  }
}

/* Without WebGL (or after a lost context) the archive still works as a 2D page. */
body.no-webgl #scene {
  background:
    radial-gradient(ellipse at 50% 38%, rgba(43, 184, 255, 0.22), transparent 55%),
    radial-gradient(ellipse at 50% 100%, rgba(215, 155, 58, 0.12), transparent 60%),
    #04080f;
}

body.no-webgl[data-theme="day"] #scene {
  background:
    radial-gradient(ellipse at 50% 38%, rgba(43, 184, 255, 0.18), transparent 55%),
    #eee8dc;
}

.noscript-note {
  position: fixed;
  inset: auto 16px 50px;
  z-index: 10;
  padding: 14px 16px;
  border-radius: 8px;
  background: rgba(8, 17, 29, 0.9);
  color: #eef6ff;
}

.noscript-note a {
  color: #5edcff;
}

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }

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