/* ═══════════════════════════════════════════════════════
   STYLES.CSS — Main Stylesheet
   Lost in the Scroll: A Fairytale of JavaScript
   V2 cleanup
   ═══════════════════════════════════════════════════════ */

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

html {
  scroll-behavior: auto;
}

body {
  font-family: var(--font-body);
  font-size: var(--fs-body);
  color: var(--text-primary);
  background-color: var(--bg-primary);
  line-height: var(--lh-body);
  overflow-x: hidden;
  transition:
    background-color 0.4s var(--ease-dream),
    color 0.4s var(--ease-dream);
}

#smooth-wrapper {
  overflow: hidden;
}

#smooth-content {
  will-change: transform;
}

section {
  scroll-margin-top: 6rem;
}

h1,
h2,
h3 {
  font-family: var(--font-display);
  line-height: var(--lh-tight);
  color: var(--text-primary);
}

h1 {
  font-size: var(--fs-hero);
  font-style: italic;
}

h2 {
  font-size: var(--fs-chapter);
}

h3 {
  font-size: var(--fs-subhead);
}

p {
  font-size: var(--fs-body);
  color: var(--text-secondary);
  max-width: 65ch;
}

em {
  color: var(--text-accent);
  font-style: italic;
}

strong {
  color: var(--text-primary);
  font-weight: 600;
}

a {
  color: var(--text-link);
  text-decoration: none;
  border-bottom: 1px solid var(--border-faint);
  transition:
    color var(--dur-fast),
    border-color var(--dur-fast);
}

a:hover {
  color: var(--text-rose);
  border-color: var(--text-rose);
}

/* NAV */
.nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: var(--z-nav);
  padding: var(--space-sm) var(--space-md);
  display: flex;
  align-items: center;
  justify-content: space-between;
  transition:
    background var(--dur-med) var(--ease-dream),
    box-shadow var(--dur-med) var(--ease-dream),
    border-color var(--dur-med) var(--ease-dream);
}

.nav.scrolled {
  background: var(--overlay-mist);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  box-shadow: var(--shadow-soft);
  border-bottom: 1px solid var(--border-faint);
}

.nav__logo {
  font-family: var(--font-display);
  font-style: italic;
  font-size: 1.1rem;
  color: var(--text-accent);
  border: none;
  letter-spacing: 0.05em;
}

.nav__controls {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

.btn-theme {
  background: transparent;
  border: 1px solid var(--border-faint);
  color: var(--text-accent);
  cursor: pointer;
  border-radius: var(--radius-pill);
  padding: 0.4rem 1rem;
  font-family: var(--font-body);
  font-size: 0.85rem;
  letter-spacing: 0.06em;
  transition:
    background var(--dur-fast) var(--ease-dream),
    border-color var(--dur-fast) var(--ease-dream),
    color var(--dur-fast) var(--ease-dream),
    box-shadow var(--dur-fast) var(--ease-dream);
}

.btn-theme:hover {
  background: var(--overlay-veil);
  border-color: var(--border-accent);
}

.btn-theme:focus-visible {
  outline: 2px solid var(--border-accent);
  outline-offset: 3px;
  box-shadow: var(--focus-ring);
}

/* STORY NAV */
.story-nav {
  position: fixed;
  right: 1.25rem;
  top: 50%;
  transform: translateY(-50%);
  z-index: var(--z-nav);
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  padding: 0.75rem;
  border: 1px solid var(--border-faint);
  border-radius: var(--radius-lg);
  background: var(--bg-elevated);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  box-shadow: var(--shadow-soft);
}

.story-nav__link {
  display: inline-flex;
  align-items: center;
  justify-content: flex-end;
  min-width: 7.5rem;
  padding: 0.4rem 0.8rem;
  border: 1px solid transparent;
  border-radius: var(--radius-pill);
  color: var(--text-secondary);
  font-family: var(--font-body);
  font-size: 0.82rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  background: transparent;
  transition:
    color var(--dur-fast) var(--ease-dream),
    border-color var(--dur-fast) var(--ease-dream),
    background var(--dur-fast) var(--ease-dream),
    transform var(--dur-fast) var(--ease-dream);
}

.story-nav__link:hover {
  color: var(--text-accent);
  border-color: var(--border-faint);
  background: var(--overlay-veil);
  transform: translateX(-2px);
}

.story-nav__link.is-active {
  color: var(--text-on-accent);
  background: var(--text-accent);
  border-color: var(--border-accent);
}

/* CANVAS / EFFECTS */
#stars-canvas {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  opacity: 0.6;
}

.firefly {
  position: absolute;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--star-glow);
  box-shadow: 0 0 8px 2px var(--star-glow);
  pointer-events: none;
}

.cursor-sparkle {
  position: fixed;
  pointer-events: none;
  z-index: 9999;
  width: 8px;
  height: 8px;
  background: var(--star-glow);
  border-radius: 50%;
  box-shadow: 0 0 6px var(--star-glow);
  transform: translate(-50%, -50%);
  transition: opacity 0.3s;
}

.cursor-sparkle--warm {
  background: var(--star-glow);
  box-shadow: 0 0 10px 2px var(--star-glow);
}

.restart-comet {
  z-index: 10000;
  mix-blend-mode: screen;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.95) 0%, var(--star-glow) 46%, rgba(232, 213, 163, 0) 75%);
  box-shadow: 0 0 18px 6px var(--star-glow);
}

/* BASE CHAPTERS */
.chapter {
  position: relative;
  z-index: var(--z-content);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: var(--space-xl) var(--space-md);
}

.chapter--centered {
  align-items: center;
  text-align: center;
}

.chapter__inner {
  max-width: 980px;
  margin: 0 auto;
  width: 100%;
}

.chapter__inner--center {
  text-align: center;
}

.chapter__heading {
  margin: 0;
  text-wrap: balance;
}

.chapter__body {
  display: grid;
  gap: var(--space-sm);
  max-width: 42ch;
  align-content: start;
}

.chapter__body > * {
  min-width: 0;
}

.chapter__body--center {
  margin: var(--space-md) auto 0;
  max-width: 55ch;
}

.chapter-label {
  font-family: var(--font-drop-cap);
  font-size: var(--fs-caption);
  letter-spacing: 0.25em;
  text-transform: uppercase;
  color: var(--text-accent);
  opacity: 0.8;
  margin-bottom: var(--space-xs);
  display: block;
}

.drop-cap::first-letter {
  font-family: var(--font-drop-cap);
  font-size: 4.5em;
  font-weight: 600;
  float: left;
  line-height: 0.75;
  margin: 0.1em 0.12em 0 0;
  color: var(--text-accent);
}

.ornament {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  margin: var(--space-md) 0;
  color: var(--text-accent);
}

.ornament__line {
  flex: 1;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--border-accent), transparent);
}

.ornament__symbol {
  font-family: var(--font-display);
  font-size: 1.4rem;
  font-style: italic;
  opacity: 0.7;
}

.ornament--end {
  margin-top: var(--space-xl);
}

.ornament--footer {
  max-width: 400px;
  margin: 0 auto var(--space-md);
}

/* HERO */
.hero {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  position: relative;
  overflow: hidden;
  padding: var(--space-xl) var(--space-md);
}

.hero__eyebrow {
  margin-bottom: var(--space-sm);
}

.hero__title {
  margin: 0;
  text-wrap: balance;
}

.hero__subtitle {
  font-family: var(--font-body);
  font-style: italic;
  font-size: var(--fs-subhead);
  color: var(--text-secondary);
  margin: var(--space-sm) 0 var(--space-md);
  letter-spacing: 0.05em;
}

.hero__desc {
  margin: var(--space-sm) auto 0;
  text-align: center;
  max-width: 50ch;
}

.hero__illustration {
  max-width: 340px;
  margin-top: var(--space-md);
}

.hero__scroll-cue {
  position: absolute;
  bottom: var(--space-lg);
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  color: var(--text-accent);
  font-family: var(--font-body);
  font-style: italic;
  font-size: var(--fs-caption);
  letter-spacing: 0.1em;
  opacity: 0.7;
  animation: floatCue 2.5s ease-in-out infinite;
}

.hero__scroll-cue svg {
  width: 24px;
  height: 24px;
}

@keyframes floatCue {
  0%, 100% { transform: translateX(-50%) translateY(0); }
  50% { transform: translateX(-50%) translateY(8px); }
}

/* LAYOUT */
.two-col {
  display: grid;
  grid-template-columns: 1.1fr 0.9fr;
  gap: var(--space-lg);
  align-items: start;
  margin-top: var(--space-md);
}

.two-col > * {
  min-width: 0;
}

.two-col figure {
  justify-self: center;
  align-self: start;
  width: min(100%, 400px);
}

figure {
  margin: 0;
  display: grid;
  gap: 0.75rem;
  align-content: start;
}

figcaption {
  text-align: center;
  font-style: italic;
  color: var(--text-secondary);
  font-size: 0.85rem;
  max-width: 34ch;
  margin: 0 auto;
}

.illustration {
  width: 100%;
  max-width: 400px;
  aspect-ratio: 5 / 4;
  object-fit: contain;
  object-position: center;
  margin: 0 auto;
  display: block;
  padding: clamp(0.9rem, 2vw, 1.35rem);
  background: linear-gradient(180deg, var(--bg-elevated), var(--bg-card));
  border: 1px solid var(--border-faint);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
}

/* MIST */
.mist-bg {
  position: absolute;
  inset: 0;
  z-index: -1;
  overflow: hidden;
}

.mist-bg__layer {
  position: absolute;
  border-radius: 50%;
  filter: blur(80px);
  opacity: 1;
}

/* SPELL SCROLL */
.spell-scroll {
  background: var(--code-bg);
  border: 1px solid var(--border-faint);
  border-radius: var(--radius-md);
  padding: var(--space-md);
  font-family: "Courier New", monospace;
  font-size: var(--fs-code);
  line-height: 1.55;
  white-space: pre-wrap;
  color: var(--code-text);
  position: relative;
  overflow-x: auto;
  overflow-y: hidden;
  box-shadow: var(--shadow-soft);
  margin: var(--space-md) 0;
}

.spell-scroll::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0; 
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, var(--text-accent), var(--text-rose), var(--code-string));
}

.spell-scroll.is-typing::after {
  content: "";
  position: absolute;
  right: var(--space-md);
  bottom: var(--space-md);
  width: 0.62rem;
  height: 1.05em;
  background: var(--text-accent);
  border-radius: 1px;
  opacity: 0.95;
  animation: code-caret-blink 0.9s steps(1, end) infinite;
}

@keyframes code-caret-blink {
  0%, 49% {
    opacity: 0.95;
  }
  50%, 100% {
    opacity: 0;
  }
}

.spell-scroll .kw { color: var(--code-keyword); font-weight: 600; }
.spell-scroll .str { color: var(--code-string); }
.spell-scroll .fn { color: var(--code-function); }
.spell-scroll .cm { color: var(--code-comment); font-style: italic; opacity: 0.85; }
.spell-scroll .nm { color: var(--code-number); }

/* CARDS */
.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  grid-auto-rows: 1fr;
  gap: var(--space-md);
  margin-top: var(--space-lg);
  align-items: stretch;
}

.cards-grid--epilogue {
  max-width: 980px;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: clamp(1rem, 2vw, 1.5rem);
  margin-left: auto;
  margin-right: auto;
  margin-bottom: var(--space-lg);
}

.cards-grid--epilogue .concept-card {
  display: grid;
  justify-items: center;
  align-content: start;
  gap: 0.55rem;
  min-height: 220px;
  padding: clamp(1.1rem, 2.2vw, 1.5rem);
  background: linear-gradient(180deg, var(--bg-elevated), var(--bg-card));
}

.cards-grid--epilogue .concept-card__icon {
  width: clamp(1.8rem, 2.8vw, 2.25rem);
  height: clamp(1.8rem, 2.8vw, 2.25rem);
  margin-bottom: 0;
  line-height: 1;
}

.cards-grid--epilogue .concept-card h3 {
  font-size: clamp(1.08rem, 1.8vw, 1.3rem);
  color: var(--text-primary);
}

.cards-grid--epilogue .concept-card p {
  max-width: 28ch;
  margin: 0;
  font-size: clamp(0.96rem, 1.3vw, 1.04rem);
  line-height: 1.55;
  color: var(--text-secondary);
}

.concept-card {
  display: grid;
  grid-template-rows: auto auto 1fr;
  align-content: start;
  gap: 0.6rem;
  background: var(--bg-card);
  border: 1px solid var(--border-faint);
  border-radius: var(--radius-lg);
  padding: var(--space-md) var(--space-lg);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  box-shadow: var(--shadow-card);
  position: relative;
  overflow: hidden;
  transition:
    box-shadow var(--dur-med) var(--ease-dream),
    transform var(--dur-med) var(--ease-fairy),
    border-color var(--dur-fast) var(--ease-dream);
  height: 100%;
}

.concept-card:hover {
  box-shadow: var(--shadow-glow);
  transform: translateY(-4px);
  border-color: var(--border-accent);
}

.concept-card__icon {
  width: 3rem;
  height: 3rem;
  margin-bottom: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.concept-card__icon img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
  filter: var(--card-icon-filter);
  transition: filter var(--dur-fast) var(--ease-dream);
}

.concept-card h3,
.concept-card p {
  margin: 0;
}

.concept-card p {
  max-width: none;
}

.concept-card--center {
  text-align: center;
  justify-items: center;
}

/* SHOWPIECE */
.showpiece {
  position: relative;
  height: 440vh;
  z-index: var(--z-content);
}

.showpiece__sticky {
  position: relative;
  height: 100svh;
  min-height: 100svh;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  padding: clamp(1rem, 2.5vw, 2rem);
}

.showpiece__stage {
  position: relative;
  width: min(760px, 92vw);
  min-height: min(86svh, 780px);
  margin: 0 auto;
}

.showpiece__step {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: clamp(1.25rem, 2.4vw, 2rem);
  gap: var(--space-sm);
}

.showpiece__step > * {
  max-width: 100%;
}

.showpiece__title {
  font-size: clamp(2rem, 5vw, 3rem);
}

.showpiece__text {
  max-width: 45ch;
  margin: 1rem auto 0;
  text-align: center;
}

.showpiece__code {
  width: min(100%, 400px);
  margin: 1rem auto 0;
  text-align: left;
}

.showpiece__emoji {
  font-size: 3rem;
  margin-top: 1rem;
  display: block;
}

/* PROGRESS */
.reading-progress {
  position: fixed;
  top: 0;
  left: 0;
  height: 3px;
  background: linear-gradient(90deg, var(--text-accent), var(--text-rose));
  z-index: calc(var(--z-nav) + 1);
  width: 0%;
  transition: none;
}

/* BLOCKQUOTE */
blockquote {
  border-left: 3px solid var(--border-accent);
  padding-left: var(--space-md);
  margin: var(--space-md) 0;
  font-style: italic;
  font-size: 1.2em;
  color: var(--text-secondary);
  position: relative;
}

blockquote::before {
  content: '"';
  font-family: var(--font-display);
  font-size: 4em;
  color: var(--border-faint);
  position: absolute;
  left: -0.1em;
  top: -0.2em;
  line-height: 1;
}

/* BADGES / KEYWORDS / BUTTONS */
.keyword {
  color: var(--text-accent);
  font-family: "Courier New", monospace;
  font-size: 0.9em;
  background: var(--overlay-veil);
  padding: 0.1em 0.4em;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-faint);
}

.epilogue__actions {
  display: flex;
  justify-content: center;
  margin-top: clamp(1.5rem, 4vw, 2.5rem);
  position: relative;
  z-index: 1;
}

.btn-restart {
  background: transparent;
  border: 1px solid var(--border-accent);
  color: var(--text-accent);
  border-radius: var(--radius-pill);
  padding: 0.6rem 1.4rem;
  font-family: var(--font-body);
  font-size: 0.95rem;
  letter-spacing: 0.06em;
  cursor: pointer;
  transition:
    background var(--dur-fast) var(--ease-dream),
    color var(--dur-fast) var(--ease-dream),
    transform var(--dur-fast) var(--ease-dream),
    box-shadow var(--dur-fast) var(--ease-dream);
}

.btn-restart:hover {
  background: var(--text-accent);
  color: var(--text-on-accent);
  transform: translateY(-1px);
  box-shadow: var(--shadow-soft);
}

.btn-restart.is-restarting {
  pointer-events: none;
  box-shadow: 0 0 0 1px var(--border-accent), 0 0 16px 2px var(--star-glow);
}

/* FOOTER */
.footer {
  text-align: center;
  padding: var(--space-xl) var(--space-md);
  border-top: 1px solid var(--border-faint);
  position: relative;
  z-index: var(--z-content);
}

.footer p {
  margin: 0 auto;
  text-align: center;
  font-style: italic;
  color: var(--text-secondary);
  font-size: var(--fs-caption);
}

.footer__meta {
  margin-top: 0.5rem !important;
  opacity: 0.6;
}

/* SKIP */
.skip-link {
  position: absolute;
  top: -100%;
  left: 1rem;
  background: var(--text-accent);
  color: var(--text-on-accent);
  padding: 0.5rem 1rem;
  border-radius: var(--radius-sm);
  font-weight: 600;
  z-index: 9999;
  border: none;
  transition: top 0.1s;
}

.skip-link:focus {
  top: 1rem;
}

/* SCROLLBAR */
::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-track { background: var(--scrollbar-track); }
::-webkit-scrollbar-thumb {
  background: var(--scrollbar-thumb);
  border-radius: var(--radius-pill);
}

::selection {
  background: var(--selection-bg);
  color: var(--selection-text);
}

:focus-visible {
  outline: 2px solid var(--border-accent);
  outline-offset: 3px;
  border-radius: var(--radius-sm);
  box-shadow: var(--focus-ring);
}

/* RESPONSIVE */
@media (max-width: 980px) {
  .story-nav {
    display: none;
  }

  .cards-grid--epilogue {
    max-width: 760px;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 768px) {
  .chapter {
    padding: var(--space-lg) var(--space-sm);
  }

  .hero {
    padding-top: calc(var(--space-xl) + 3rem);
  }

  .two-col {
    grid-template-columns: 1fr;
    gap: var(--space-md);
    align-items: start;
  }

  .chapter__body {
    max-width: none;
  }

  .spell-scroll {
    font-size: 0.8rem;
  }

  .showpiece__step {
    padding: var(--space-md);
  }

  .showpiece__stage {
    min-height: 88svh;
  }

  .showpiece {
    height: 520vh;
  }

  .cards-grid--epilogue {
    max-width: 560px;
    grid-template-columns: 1fr;
    margin-bottom: var(--space-md);
  }

  .cards-grid--epilogue .concept-card {
    min-height: 0;
  }
}

/* REDUCED MOTION */
@media (prefers-reduced-motion: reduce) {
  .hero__scroll-cue,
  .firefly,
  .cursor-sparkle {
    animation: none;
    display: none;
  }

  .spell-scroll.is-typing::after {
    animation: none;
  }
}
.cards-grid--epilogue .concept-card {
  opacity: 0.3;
  transform: translateY(18px) scale(0.97);
  filter: blur(1.8px);
  transition:
    opacity 0.45s ease,
    transform 0.45s ease,
    filter 0.45s ease,
    box-shadow var(--dur-med) var(--ease-dream),
    border-color var(--dur-fast) var(--ease-dream);
}

/* Hover/focus reveals epilogue cards with a soft fade-in. */
.cards-grid--epilogue .concept-card:hover,
.cards-grid--epilogue .concept-card:focus-within {
  opacity: 1 !important;
  transform: translateY(0) scale(1) !important;
  filter: blur(0) !important;
  box-shadow: 0 10px 30px rgba(201, 146, 47, 0.25);
}

@media (hover: none) {
  .cards-grid--epilogue .concept-card {
    opacity: 1;
    transform: none;
    filter: none;
  }
}