/* ==========================================================
   DIGITAL TWIN STAGE (twin.js)
   The journey becomes two columns: chapters on the left, a sticky
   WebGL stage on the right (a sticky band at the top on tablet and
   phone). Everything here is scoped to .twin-layout / .twin-stage.
   The layout only turns on once twin.js confirms WebGL is running
   (.is-twin); without it the chapters stay a single full-width column.
   ========================================================== */

.twin-layout {
  display: block;
  margin-top: 8px;
}
.twin-stage {
  display: none;
}

/* Instrument palette: matches PALETTE in twin.js */
.twin-stage {
  --tw-bg: #f1f3ef;
  --tw-ink: #121418;
  --tw-ink2: #444951;
  --tw-edge: #2340ff;
  --tw-accent: #ffb800;
  --tw-on-accent: #121418;
  --tw-rule: rgba(35, 64, 255, 0.3);
  --tw-grid: rgba(35, 64, 255, 0.07);
  --tw-panel: rgba(255, 255, 255, 0.82);
  --tw-glow: rgba(35, 64, 255, 0.1);
}
:root[data-theme="dark"] .twin-stage {
  --tw-bg: #080a0f;
  --tw-ink: #ece6d6;
  --tw-ink2: #b9b2a3;
  --tw-edge: #8da0ff;
  --tw-rule: rgba(131, 151, 255, 0.34);
  --tw-grid: rgba(131, 151, 255, 0.07);
  --tw-panel: rgba(8, 10, 15, 0.74);
  --tw-glow: rgba(91, 115, 255, 0.16);
}

/* ---- Wide: text left, twin right ---- */
.story-body:has(.twin-layout.is-twin) {
  max-width: none;
}
.story-body:has(.twin-layout.is-twin) > .story-section:not(.twin-journey) {
  max-width: 780px;
}
.twin-layout.is-twin {
  display: grid;
  grid-template-columns: minmax(0, 0.92fr) minmax(0, 1.08fr);
  gap: clamp(28px, 3.6vw, 56px);
  align-items: start;
}
.twin-copy {
  min-width: 0;
}
.twin-layout.is-twin .twin-copy .chart-pair {
  grid-template-columns: minmax(0, 1fr);
}
.twin-layout.is-twin .twin-stage {
  display: block;
  position: sticky;
  top: calc(var(--header-h) + 22px);
  height: min(calc(100svh - var(--header-h) - 44px), 720px);
  min-height: 360px;
  overflow: hidden;
  isolation: isolate;
  border: 1px solid var(--tw-rule);
  border-radius: var(--radius);
  background: var(--tw-bg);
  color: var(--tw-ink);
  contain: layout paint;
}

.tw-backdrop {
  position: absolute;
  inset: 0;
  z-index: 0;
  background:
    radial-gradient(120% 90% at 50% 8%, var(--tw-glow), transparent 62%),
    linear-gradient(var(--tw-grid) 1px, transparent 1px) 0 0 / 28px 28px,
    linear-gradient(90deg, var(--tw-grid) 1px, transparent 1px) 0 0 / 28px 28px;
}
.tw-canvas {
  position: absolute;
  inset: 0;
  z-index: 1;
  display: block;
  width: 100%;
  height: 100%;
  opacity: 0;
  transition: opacity 0.7s ease;
}
.twin-stage[data-state="ready"] .tw-canvas {
  opacity: 1;
}

/* Corner registration marks */
.twin-stage::before,
.twin-stage::after {
  content: "";
  position: absolute;
  z-index: 4;
  width: 14px;
  height: 14px;
  pointer-events: none;
  border: 0 solid var(--tw-edge);
  opacity: 0.8;
}
.twin-stage::before {
  right: 8px;
  bottom: 8px;
  border-width: 0 1.5px 1.5px 0;
}
.twin-stage::after {
  left: 8px;
  bottom: 8px;
  border-width: 0 0 1.5px 1.5px;
}

/* ---- Caption, rail, readout ---- */
.tw-cap {
  position: absolute;
  z-index: 3;
  top: 12px;
  left: 12px;
  right: 132px; /* clear of the step rail (8 ticks at most) */
  display: flex;
  align-items: stretch;
  gap: 0;
  font-family: var(--f-mono);
  font-size: 10.5px;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  pointer-events: none;
}
.tw-cap-n {
  display: inline-flex;
  align-items: center;
  padding: 4px 7px;
  background: var(--tw-accent);
  color: var(--tw-on-accent);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.tw-cap-t {
  display: inline-flex;
  align-items: center;
  padding: 4px 9px;
  min-width: 0;
  background: var(--tw-panel);
  border: 1px solid var(--tw-rule);
  border-left: 0;
  color: var(--tw-ink);
  /* wrap at spaces, never inside a word, and never clip to an ellipsis */
  line-height: 1.3;
  white-space: normal;
  overflow-wrap: normal;
  word-break: normal;
  hyphens: manual;
  max-width: 34ch;
  backdrop-filter: blur(6px);
}
.tw-rail {
  position: absolute;
  z-index: 3;
  top: 16px;
  right: 14px;
  display: flex;
  gap: 4px;
  pointer-events: none;
}
.tw-rail i {
  position: relative;
  width: 10px;
  height: 3px;
  background: var(--tw-rule);
  overflow: hidden;
}
.tw-rail i.is-done {
  background: var(--tw-edge);
}
.tw-rail i.is-on {
  background: var(--tw-rule);
}
.tw-rail i.is-on::after {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--tw-accent);
  transform-origin: left;
  transform: scaleX(var(--p, 0));
}

.tw-hud {
  position: absolute;
  z-index: 3;
  left: 12px;
  bottom: 12px;
  display: grid;
  gap: 2px;
  min-width: 0;
  max-width: calc(100% - 52px);
  font-family: var(--f-mono);
  font-size: 10.5px;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  pointer-events: none;
}
.tw-hud:empty {
  display: none;
}
.tw-hud-row {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  padding: 3px 8px;
  background: var(--tw-panel);
  border-left: 2px solid var(--tw-rule);
  backdrop-filter: blur(6px);
}
.tw-hud-row span {
  color: var(--tw-ink2);
}
.tw-hud-row b {
  color: var(--tw-ink);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.tw-hud-row.is-hot {
  border-left-color: var(--tw-accent);
}

/* ---- Pinned 3D labels ---- */
.tw-labels {
  position: absolute;
  inset: 0;
  z-index: 2;
  overflow: hidden;
  pointer-events: none;
}
.tw-label {
  position: absolute;
  left: 0;
  top: 0;
  padding: 2px 6px;
  font-family: var(--f-mono);
  font-size: 10px;
  line-height: 1.25;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  white-space: nowrap;
  color: var(--tw-ink);
  background: var(--tw-panel);
  border: 1px solid var(--tw-rule);
  opacity: 0;
  transition: opacity 0.25s ease;
  will-change: transform, opacity;
}
.tw-label--accent {
  background: var(--tw-accent);
  border-color: transparent;
  color: var(--tw-on-accent);
  font-weight: 600;
}
.tw-label--dim {
  background: transparent;
  border-color: transparent;
  color: var(--tw-ink2);
  padding-inline: 0;
}

/* ---- Loading ---- */
.tw-boot {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  font-family: var(--f-mono);
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--tw-ink2);
  pointer-events: none;
}
.tw-boot-dot {
  width: 8px;
  height: 8px;
  background: var(--tw-accent);
  transform: rotate(45deg);
  animation: tw-pulse 1.1s ease-in-out infinite;
}
@keyframes tw-pulse {
  50% {
    opacity: 0.25;
    transform: rotate(45deg) scale(0.7);
  }
}

/* ---- Tablet and phone: sticky band at the top, text scrolls beneath ---- */
@media (max-width: 1080px) {
  .twin-layout.is-twin {
    display: flex;
    flex-direction: column;
    gap: 0;
  }
  .twin-layout.is-twin .twin-stage {
    order: -1;
    align-self: stretch;
    top: var(--header-h);
    z-index: 6;
    height: 42svh;
    min-height: 250px;
    max-height: 460px;
    margin: 0 calc(var(--gutter) * -1) 22px;
    border-width: 0 0 1px;
    border-radius: 0;
  }
  .tw-cap {
    top: 8px;
    left: 8px;
    right: 96px;
  }
  .tw-rail {
    top: 12px;
    right: 10px;
    gap: 3px;
  }
  .tw-rail i {
    width: 7px; /* 8 ticks stay clear of the caption */
  }
  .tw-hud {
    left: 8px;
    bottom: 8px;
    font-size: 9.5px;
  }
  .tw-hud-row:nth-child(n + 4) {
    display: none;
  }
  .tw-label {
    font-size: 9px;
    padding: 1px 4px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .tw-boot-dot {
    animation: none;
  }
  .tw-canvas {
    transition: none;
  }
  .tw-label {
    transition: none;
  }
}

/* Capture flash (step 8 of the AMR twin) */
.twin-stage.is-flash {
  box-shadow: inset 0 0 0 2px var(--tw-accent), inset 0 0 70px rgba(255, 184, 0, 0.4);
}

/* Leader from a displaced label back to its anchor */
.tw-leader {
  position: absolute;
  left: 0;
  top: 0;
  height: 1px;
  transform-origin: 0 0;
  background: var(--tw-edge);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}
