/* ════════════════════════════════════════════════════════════
   NEN CODEX · ENHANCEMENTS LAYER
   Adds: scroll-reveal, per-chapter accents, reading progress,
         side rail, parallax kanji, micro-interactions, dividers
   ════════════════════════════════════════════════════════════ */

/* ─────────────────────────────────────────
   1 · PER-CHAPTER ACCENTS (via body[data-chapter])
   Each chapter swaps the dominant accent so sections feel
   distinct as the user scrolls.
   ───────────────────────────────────────── */
:root {
  --accent: var(--cyan);
  --accent-dim: rgba(0,212,255,0.15);
  --accent-soft: rgba(0,212,255,0.06);
  --accent-glow: 0 0 60px rgba(0,212,255,0.20);
}
body { transition: background 1.4s ease; }

body[data-chapter="2"] { --accent:#ff4757; --accent-dim:rgba(255,71,87,0.18); --accent-soft:rgba(255,71,87,0.06); --accent-glow:0 0 60px rgba(255,71,87,0.20); }
body[data-chapter="3"] { --accent:#ffc048; --accent-dim:rgba(255,192,72,0.18); --accent-soft:rgba(255,192,72,0.06); --accent-glow:0 0 60px rgba(255,192,72,0.20); }
body[data-chapter="4"] { --accent:#a55eea; --accent-dim:rgba(165,94,234,0.20); --accent-soft:rgba(165,94,234,0.06); --accent-glow:0 0 60px rgba(165,94,234,0.22); }
body[data-chapter="5"] { --accent:#d6dbe5; --accent-dim:rgba(214,219,229,0.18); --accent-soft:rgba(214,219,229,0.05); --accent-glow:0 0 60px rgba(214,219,229,0.16); }
body[data-chapter="6"] { --accent:#2ed573; --accent-dim:rgba(46,213,115,0.20); --accent-soft:rgba(46,213,115,0.06); --accent-glow:0 0 60px rgba(46,213,115,0.22); }
body[data-chapter="7"] { --accent:#00d4ff; --accent-dim:rgba(0,212,255,0.18); --accent-soft:rgba(0,212,255,0.06); --accent-glow:0 0 60px rgba(0,212,255,0.20); }
body[data-chapter="8"] { --accent:#ff6b9d; --accent-dim:rgba(255,107,157,0.20); --accent-soft:rgba(255,107,157,0.06); --accent-glow:0 0 60px rgba(255,107,157,0.22); }
body[data-chapter="9"] { --accent:#ffc048; --accent-dim:rgba(255,192,72,0.20); --accent-soft:rgba(255,192,72,0.06); --accent-glow:0 0 60px rgba(255,192,72,0.22); }
body[data-chapter="10"]{ --accent:#5ee8b3; --accent-dim:rgba(94,232,179,0.20); --accent-soft:rgba(94,232,179,0.06); --accent-glow:0 0 60px rgba(94,232,179,0.22); }
body[data-chapter="11"]{ --accent:#ff4757; --accent-dim:rgba(255,71,87,0.22); --accent-soft:rgba(255,71,87,0.07); --accent-glow:0 0 60px rgba(255,71,87,0.26); }

/* Soft per-chapter aurora that follows the accent.
   A fixed, animated radial behind everything. */
.aurora {
  position: fixed; inset: -10vh -10vw;
  z-index: -1;
  pointer-events: none;
  opacity: 0.55;
  transition: opacity 1.2s ease;
  background:
    radial-gradient(40vw 40vw at 18% 22%, var(--accent-soft), transparent 60%),
    radial-gradient(48vw 48vw at 86% 78%, var(--accent-soft), transparent 60%),
    radial-gradient(32vw 32vw at 50% 110%, var(--accent-soft), transparent 60%);
  filter: blur(20px) saturate(1.1);
  animation: auroraFloat 24s ease-in-out infinite alternate;
}
@keyframes auroraFloat {
  0%   { transform: translate3d(0,0,0)        scale(1); }
  50%  { transform: translate3d(2vw,-2vh,0)   scale(1.06); }
  100% { transform: translate3d(-2vw,2vh,0)   scale(1.02); }
}

/* Topbar reflects active chapter accent */
.topbar.scrolled { box-shadow: 0 8px 32px -16px var(--accent), 0 0 0 1px var(--accent-dim); }
.topbar__nav a:hover, .topbar__nav a.active {
  color: var(--accent);
  background: var(--accent-dim);
}
.topbar__logo { color: var(--accent); transition: color 0.6s ease; }
.topbar__logo-kanji { color: var(--accent); transition: color 0.6s ease; filter: drop-shadow(0 0 12px var(--accent-dim)); }

/* ─────────────────────────────────────────
   2 · READING PROGRESS BAR
   Fixed, super-thin bar above topbar; widens with scroll.
   ───────────────────────────────────────── */
.read-progress {
  position: fixed; top: 0; left: 0; right: 0;
  height: 2px; z-index: 1100;
  background: transparent;
  pointer-events: none;
}
.read-progress__fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, transparent, var(--accent) 30%, var(--accent) 70%, transparent);
  box-shadow: 0 0 12px var(--accent);
  transition: width 0.08s linear, background 0.6s ease;
}

/* ─────────────────────────────────────────
   3 · CHAPTER SIDE RAIL
   Vertical dots, fixed on the right (Arabic page: still right
   feels natural; RTL keeps it on the page edge).
   ───────────────────────────────────────── */
.side-rail {
  position: fixed;
  top: 50%; transform: translateY(-50%);
  left: 18px;            /* RTL: keep on the visual-left edge */
  z-index: 999;
  display: flex; flex-direction: column; gap: 14px;
  align-items: center;
  padding: 14px 8px;
  border-radius: 100px;
  background: rgba(6,6,11,0.55);
  backdrop-filter: blur(14px);
  border: 1px solid var(--border);
  opacity: 0; transform: translate(-30px,-50%);
  transition: opacity 0.6s ease, transform 0.6s cubic-bezier(.2,.7,.2,1);
}
.side-rail.visible { opacity: 1; transform: translate(0,-50%); }
.side-rail__dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--text-3); opacity: 0.45;
  position: relative;
  cursor: pointer;
  transition: all 0.3s;
}
.side-rail__dot::before {
  content: attr(data-label);
  position: absolute;
  left: 22px; top: 50%; transform: translateY(-50%) translateX(-6px);
  font-family: var(--font-ar);
  font-size: 11px;
  font-weight: 600;
  color: var(--text);
  background: rgba(6,6,11,0.92);
  border: 1px solid var(--accent-dim);
  padding: 5px 10px;
  border-radius: 6px;
  white-space: nowrap;
  opacity: 0; pointer-events: none;
  transition: opacity 0.25s, transform 0.25s;
  direction: rtl;
}
.side-rail__dot:hover { transform: scale(1.4); opacity: 1; background: var(--accent); }
.side-rail__dot:hover::before { opacity: 1; transform: translateY(-50%) translateX(0); }
.side-rail__dot.active {
  background: var(--accent);
  opacity: 1;
  box-shadow: 0 0 0 4px var(--accent-dim), 0 0 14px var(--accent);
  transform: scale(1.25);
}
.side-rail__num {
  font-family: var(--font-mono);
  font-size: 9px; font-weight: 700;
  color: var(--text-3);
  letter-spacing: 1px;
  margin-bottom: 4px;
}
@media (max-width: 880px) { .side-rail { display: none; } }

/* ─────────────────────────────────────────
   4 · BACK TO TOP
   ───────────────────────────────────────── */
.to-top {
  position: fixed; bottom: 24px;
  left: 24px;                /* keep on the same edge as side rail */
  z-index: 999;
  width: 44px; height: 44px;
  border-radius: 50%;
  background: rgba(6,6,11,0.7);
  border: 1px solid var(--accent-dim);
  color: var(--accent);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  backdrop-filter: blur(14px);
  opacity: 0; pointer-events: none;
  transform: translateY(20px);
  transition: opacity 0.4s, transform 0.4s, box-shadow 0.4s;
}
.to-top.visible { opacity: 1; pointer-events: auto; transform: translateY(0); }
.to-top:hover { box-shadow: 0 0 0 4px var(--accent-dim), 0 0 18px var(--accent); transform: translateY(-4px) scale(1.08); }
.to-top svg { width: 18px; height: 18px; }

/* ─────────────────────────────────────────
   5 · SCROLL REVEAL — primitive utility
   JS adds .reveal to anything that should animate in.
   Variants: .reveal-up, .reveal-left, .reveal-right, .reveal-scale
   .reveal-stagger > * gets a small per-index delay.
   ───────────────────────────────────────── */
.reveal { opacity: 0; will-change: opacity, transform; transition:
  opacity 0.9s cubic-bezier(.2,.7,.2,1),
  transform 0.9s cubic-bezier(.2,.7,.2,1),
  filter 0.9s cubic-bezier(.2,.7,.2,1);
  filter: blur(6px);
}
.reveal-up         { transform: translateY(40px); }
.reveal-left       { transform: translateX(-40px); }
.reveal-right      { transform: translateX(40px); }
.reveal-scale      { transform: scale(0.94); }
.reveal.is-in {
  opacity: 1; transform: none; filter: blur(0);
}
.reveal-stagger > * {
  opacity: 0; transform: translateY(20px) scale(0.98);
  transition: opacity 0.7s cubic-bezier(.2,.7,.2,1), transform 0.7s cubic-bezier(.2,.7,.2,1);
}
.reveal-stagger.is-in > * { opacity: 1; transform: none; }
.reveal-stagger.is-in > *:nth-child(1)  { transition-delay: 0.00s; }
.reveal-stagger.is-in > *:nth-child(2)  { transition-delay: 0.06s; }
.reveal-stagger.is-in > *:nth-child(3)  { transition-delay: 0.12s; }
.reveal-stagger.is-in > *:nth-child(4)  { transition-delay: 0.18s; }
.reveal-stagger.is-in > *:nth-child(5)  { transition-delay: 0.24s; }
.reveal-stagger.is-in > *:nth-child(6)  { transition-delay: 0.30s; }
.reveal-stagger.is-in > *:nth-child(7)  { transition-delay: 0.36s; }
.reveal-stagger.is-in > *:nth-child(8)  { transition-delay: 0.42s; }
.reveal-stagger.is-in > *:nth-child(9)  { transition-delay: 0.48s; }
.reveal-stagger.is-in > *:nth-child(10) { transition-delay: 0.54s; }
.reveal-stagger.is-in > *:nth-child(11) { transition-delay: 0.60s; }
.reveal-stagger.is-in > *:nth-child(12) { transition-delay: 0.66s; }

/* ─────────────────────────────────────────
   6 · HERO — extra life
   Animated grid pulse + radial sweep + kanji breathing.
   ───────────────────────────────────────── */
.hero {
  padding-top: clamp(80px, 14vh, 160px) !important;
  /* The original hero had a kanji + orbs already; we layer additional life. */
}
.hero__grid-bg {
  animation: gridPulse 8s ease-in-out infinite alternate;
}
@keyframes gridPulse {
  0%   { opacity: 0.7; transform: scale(1); }
  100% { opacity: 1.0; transform: scale(1.04); }
}
.hero__title {
  background: linear-gradient(90deg, #e8eaf0 0%, #e8eaf0 50%, var(--cyan) 50%, var(--cyan) 100%);
  background-size: 200% 100%;
  background-position: 0% 0%;
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: titleSheen 6s ease-in-out infinite;
}
.hero__title em { color: inherit; }
@keyframes titleSheen {
  0%, 60%, 100% { background-position: 0% 0%; }
  30% { background-position: -60% 0%; }
}

/* Subtle scanlines across hero — invokes the codex/terminal feel */
.hero::after {
  content: "";
  position: absolute; inset: 0;
  background-image: repeating-linear-gradient(
    to bottom,
    rgba(0,212,255,0.025) 0,
    rgba(0,212,255,0.025) 1px,
    transparent 1px,
    transparent 4px
  );
  mix-blend-mode: screen;
  pointer-events: none;
  mask-image: radial-gradient(ellipse 80% 60% at 50% 50%, black 30%, transparent 90%);
}

/* Hero TOC cards: stagger entry + magnetic feel */
.hero__toc { perspective: 1200px; }
.hero__toc-item {
  position: relative;
  overflow: hidden;
  transition: transform 0.4s cubic-bezier(.2,.7,.2,1), border-color 0.4s, background 0.4s, box-shadow 0.4s;
}
.hero__toc-item::before {
  content: "";
  position: absolute; inset: -1px;
  border-radius: inherit;
  background: conic-gradient(from var(--ang, 0deg), transparent, var(--cyan), transparent 40%);
  opacity: 0; transition: opacity 0.4s;
  -webkit-mask:
    linear-gradient(#000 0 0) content-box,
    linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
          mask-composite: exclude;
  padding: 1px;
  pointer-events: none;
}
.hero__toc-item:hover::before { opacity: 1; animation: spinBorder 2.6s linear infinite; }
@keyframes spinBorder { to { --ang: 360deg; } }
@property --ang { syntax: '<angle>'; initial-value: 0deg; inherits: false; }
.hero__toc-item:hover .hero__toc-num {
  background: var(--cyan); box-shadow: 0 0 20px var(--cyan);
  transform: rotate(-8deg) scale(1.1);
}
.hero__toc-num { transition: all 0.4s cubic-bezier(.2,.7,.2,1); }

/* ─────────────────────────────────────────
   7 · CHAPTER HEADERS — cinematic entrances
   Big kanji slides + scales in, number flares, line draws.
   ───────────────────────────────────────── */
.ch-header { padding-top: 140px !important; padding-bottom: 80px !important; }
.ch-header__bg-kanji {
  -webkit-text-stroke-color: var(--accent-dim) !important;
  transition: transform 1.2s cubic-bezier(.2,.7,.2,1), opacity 1.2s, -webkit-text-stroke-color 0.6s;
  transform: translate(-50%, -50%) scale(0.85) rotate(-2deg);
  opacity: 0;
}
.ch-header.in-view .ch-header__bg-kanji {
  transform: translate(-50%, -50%) scale(1) rotate(0deg);
  opacity: 1;
}
.ch-header__bg-kanji {
  animation: kanjiSway 14s ease-in-out infinite alternate;
}
@keyframes kanjiSway {
  0%   { filter: drop-shadow(0 0 0 transparent); }
  100% { filter: drop-shadow(0 0 30px var(--accent-soft)); }
}
.ch-header__label {
  color: var(--accent) !important;
  letter-spacing: 6px !important;
  opacity: 0; transform: translateY(-8px);
  transition: opacity 0.7s 0.1s, transform 0.7s 0.1s;
}
.ch-header.in-view .ch-header__label { opacity: 1; transform: none; }

.ch-header__num {
  color: var(--accent) !important;
  opacity: 0.18 !important;
  font-size: 96px !important;
  text-shadow: 0 0 30px var(--accent-dim);
  transform: translateY(20px) scale(0.9);
  transition: opacity 0.8s 0.2s, transform 0.8s 0.2s cubic-bezier(.2,.7,.2,1), color 0.6s;
}
.ch-header.in-view .ch-header__num { transform: none; }

.ch-header__title {
  opacity: 0; transform: translateY(16px);
  transition: opacity 0.8s 0.3s, transform 0.8s 0.3s cubic-bezier(.2,.7,.2,1);
}
.ch-header.in-view .ch-header__title { opacity: 1; transform: none; }
.ch-header__title em { color: var(--accent) !important; transition: color 0.6s; }

.ch-header__sub {
  opacity: 0; transform: translateY(8px);
  transition: opacity 0.7s 0.4s, transform 0.7s 0.4s;
}
.ch-header.in-view .ch-header__sub { opacity: 1; transform: none; }

.ch-header__line {
  width: 0 !important;
  background: linear-gradient(90deg, transparent, var(--accent), transparent) !important;
  height: 2px !important;
  box-shadow: 0 0 18px var(--accent);
  transition: width 1.2s 0.5s cubic-bezier(.2,.7,.2,1);
}
.ch-header.in-view .ch-header__line { width: 220px !important; }

/* Tiny corner ornaments on chapter headers */
.ch-header::before,
.ch-header::after {
  content: ""; position: absolute;
  width: 80px; height: 80px;
  border: 1px solid var(--accent-dim);
  pointer-events: none;
  opacity: 0; transition: opacity 1s 0.6s, transform 1s 0.6s;
}
.ch-header::before {
  top: 90px; right: 24px;
  border-bottom: none; border-left: none;
  transform: translate(20px, -20px);
}
.ch-header::after {
  bottom: 40px; left: 24px;
  border-top: none; border-right: none;
  transform: translate(-20px, 20px);
}
.ch-header.in-view::before,
.ch-header.in-view::after { opacity: 1; transform: none; }

/* ─────────────────────────────────────────
   8 · CHAPTER SEPARATORS — animated SVG line
   The .sep divs already exist; we redress them.
   ───────────────────────────────────────── */
.sep {
  position: relative;
  height: 80px;
  margin: 20px 0;
  display: flex; align-items: center; justify-content: center;
  background: none !important;
}
.sep::before, .sep::after {
  content: "";
  flex: 1;
  max-width: 30vw;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--accent-dim), transparent);
}
.sep > * { display: none; } /* hide any inner */
.sep::after { background: linear-gradient(-90deg, transparent, var(--accent-dim), transparent); }

/* Diamond mark in the middle of the separator */
.sep-mark {
  width: 14px; height: 14px;
  border: 1px solid var(--accent);
  transform: rotate(45deg);
  margin: 0 18px;
  position: relative;
  box-shadow: 0 0 12px var(--accent);
  animation: diamondPulse 3s ease-in-out infinite;
}
.sep-mark::before {
  content: ""; position: absolute; inset: 3px;
  background: var(--accent);
  opacity: 0.5;
}
@keyframes diamondPulse {
  0%, 100% { box-shadow: 0 0 6px var(--accent); transform: rotate(45deg) scale(1); }
  50%      { box-shadow: 0 0 22px var(--accent); transform: rotate(45deg) scale(1.18); }
}

/* ─────────────────────────────────────────
   9 · GENERIC CARD MICRO-INTERACTIONS
   Apply when .card-tilt is set (JS will tag known cards).
   ───────────────────────────────────────── */
.card-tilt {
  transform-style: preserve-3d;
  transition: transform 0.35s cubic-bezier(.2,.7,.2,1), box-shadow 0.35s, border-color 0.35s;
}
.card-tilt::after {
  content: "";
  position: absolute; inset: 0;
  border-radius: inherit;
  pointer-events: none;
  background: radial-gradient(360px circle at var(--mx,50%) var(--my,50%), var(--accent-soft), transparent 50%);
  opacity: 0; transition: opacity 0.4s;
}
.card-tilt:hover::after { opacity: 1; }
.card-tilt:hover {
  box-shadow: 0 16px 40px -16px var(--accent-dim), 0 0 0 1px var(--accent-dim);
}

/* Principle / sub / type / death / mafia / eq / qa cards — soft existing surfaces
   already styled; we just lift the accent on the host chapter. */
.princ-card:hover, .water-card:hover { border-color: var(--accent) !important; }

/* ─────────────────────────────────────────
   10 · TOPBAR — animated underline on hover
   ───────────────────────────────────────── */
.topbar__nav a {
  position: relative;
}
.topbar__nav a::after {
  content: "";
  position: absolute;
  left: 12px; right: 12px; bottom: 4px;
  height: 1px;
  background: var(--accent);
  transform: scaleX(0);
  transform-origin: left;
  transition: transform 0.35s cubic-bezier(.2,.7,.2,1);
}
.topbar__nav a:hover::after,
.topbar__nav a.active::after { transform: scaleX(1); }

/* ─────────────────────────────────────────
   11 · FLOWCHART — animate stroke draw + nodes float
   ───────────────────────────────────────── */
.flow-container { transition: border-color 0.6s, box-shadow 0.6s; }
.flow-container:hover { border-color: var(--accent-dim); box-shadow: 0 0 60px -20px var(--accent-dim); }
.flow-svg line { stroke-dasharray: 200; stroke-dashoffset: 200; }
.flow-svg.in-view line { animation: drawLine 1.2s 0.2s cubic-bezier(.2,.7,.2,1) forwards; }
@keyframes drawLine { to { stroke-dashoffset: 0; } }
/* Animate inner circles only — DO NOT touch <g> (it carries the translate). */
.flow-svg circle {
  transform-origin: center;
  transform-box: fill-box;
  opacity: 0;
  transition: fill 0.3s;
}
.flow-svg.in-view circle { animation: nodePop 0.6s cubic-bezier(.34,1.56,.64,1) forwards; }
.flow-svg.in-view circle:nth-of-type(2)  { animation-delay: .05s; }
.flow-svg.in-view circle:nth-of-type(3)  { animation-delay: .12s; }
.flow-svg.in-view circle:nth-of-type(4)  { animation-delay: .18s; }
.flow-svg.in-view circle:nth-of-type(5)  { animation-delay: .24s; }
.flow-svg.in-view circle:nth-of-type(6)  { animation-delay: .30s; }
.flow-svg.in-view circle:nth-of-type(7)  { animation-delay: .36s; }
.flow-svg.in-view circle:nth-of-type(8)  { animation-delay: .42s; }
.flow-svg.in-view circle:nth-of-type(9)  { animation-delay: .48s; }
.flow-svg.in-view circle:nth-of-type(10) { animation-delay: .54s; }
.flow-svg.in-view circle:nth-of-type(11) { animation-delay: .60s; }
@keyframes nodePop {
  0%   { opacity: 0; transform: scale(0.5); }
  60%  { opacity: 1; transform: scale(1.08); }
  100% { opacity: 1; transform: scale(1); }
}
.flow-svg g:hover circle { fill: var(--accent-soft); }
.flow-svg text { opacity: 0; transition: opacity 0.6s; }
.flow-svg.in-view text { opacity: 1; transition-delay: 0.6s; }

/* ─────────────────────────────────────────
   12 · TYPE TABS (Ch.2)
   Active tab grows; underline glow.
   ───────────────────────────────────────── */
.types-tab {
  position: relative;
  overflow: hidden;
}
.types-tab::before {
  content: "";
  position: absolute; inset: 0;
  background: radial-gradient(circle at 50% 120%, var(--tab-color, var(--accent)), transparent 60%);
  opacity: 0; transition: opacity 0.4s;
}
.types-tab:hover::before { opacity: 0.25; }
.types-tab.active { transform: translateY(-2px); box-shadow: 0 8px 24px -8px var(--tab-color, var(--accent)); }

.type-card { transition: box-shadow 0.6s; }
.type-card:hover { box-shadow: 0 20px 60px -30px var(--type-color, var(--accent)); }
.type-card__glow { animation: glowDrift 10s ease-in-out infinite alternate; }
@keyframes glowDrift {
  0%   { transform: translate(0,0) scale(1); opacity: 0.03; }
  100% { transform: translate(40px,-30px) scale(1.2); opacity: 0.06; }
}

/* ─────────────────────────────────────────
   13 · MISC — selection sparkle, code rain hooks
   ───────────────────────────────────────── */
::selection { background: var(--accent); color: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--accent-dim); }
::-webkit-scrollbar-thumb:hover { background: var(--accent); }

/* Print-safe: kill animations */
@media print {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001s !important;
    transition-duration: 0.001s !important;
  }
  .reveal, .reveal-stagger > * { opacity: 1; transform: none; filter: none; }
}
