:root {
  --bg: #0c0b0f;
  --card: #151419;
  --border: rgba(255, 255, 255, 0.07);
  --text: #e8e6e1;
  --muted: #9a9790;
  --dim: #5c5a55;
  --accent: #b4a0d4;
  --red: #d4605a;
  --red-stroke: #c05550;
  --red-bg: rgba(212, 96, 90, 0.08);
  --grn: #5aab7a;
  --grn-stroke: #4a9a6a;
  --grn-bg: rgba(90, 171, 122, 0.08);
  --serif: 'Instrument Serif', Georgia, serif;
  --sans: 'DM Sans', -apple-system, sans-serif
}

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased
}

.pg {
  max-width: 640px;
  margin: 0 auto;
  padding: 48px 16px 100px
}

header {
  margin-bottom: 48px;
  opacity: 0;
  transform: translateY(16px);
  animation: up .7s ease forwards
}

h1 {
  font-family: var(--serif);
  font-weight: 400;
  font-size: clamp(1.8rem, 6vw, 2.8rem);
  line-height: 1.12;
  letter-spacing: -.02em;
  margin-bottom: 16px
}

h1 em {
  font-style: italic;
  color: var(--red)
}

.intro {
  font-size: .95rem;
  font-weight: 300;
  color: var(--muted);
  line-height: 1.75
}

.intro p {
  margin-bottom: 14px
}

.intro p:last-child {
  margin-bottom: 0;
  color: var(--dim);
  font-size: .85rem
}

.intro strong {
  color: var(--text);
  font-weight: 500
}

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

.legend {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  padding: 10px 0;
  margin-bottom: 10px;
  border-bottom: 1px solid var(--border);
  opacity: 0;
  animation: up .6s ease .15s forwards
}

.legend-i {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: .72rem;
  color: var(--dim);
  text-transform: uppercase;
  letter-spacing: .04em;
  font-weight: 500
}

.legend-s {
  width: 14px;
  height: 14px;
  border-radius: 4px;
  border: 2px solid
}

.legend-s.g {
  border-color: var(--grn-stroke);
  background: var(--grn-bg)
}

.legend-s.r {
  border-color: var(--red-stroke);
  background: var(--red-bg)
}

.legend-s.n {
  border-color: var(--dim);
  background: transparent
}

.dag-wrap {
  opacity: 0;
  transform: translateY(12px);
  animation: up .7s ease .25s forwards
}

svg#dag {
  width: 100%;
  height: auto;
  display: block
}

svg text {
  font-family: 'DM Sans', sans-serif
}

svg#dag rect {
  transition: stroke .25s, stroke-width .25s, fill .25s, height .2s ease
}

.pnl {
  margin-top: 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--card);
  padding: 20px;
  min-height: 70px;
  opacity: 0;
  transform: translateY(6px);
  transition: opacity .3s, transform .3s
}

.pnl.on {
  opacity: 1;
  transform: translateY(0)
}

.pn {
  font-family: var(--serif);
  font-size: 1.25rem;
  margin-bottom: 14px
}

.pr {
  margin-bottom: 14px
}

.pr-tag {
  font-size: .65rem;
  font-weight: 600;
  letter-spacing: .05em;
  text-transform: uppercase;
  padding: 3px 8px;
  border-radius: 4px;
  display: inline-block;
  margin-bottom: 0
}

.pr-tag-g {
  background: var(--grn-bg);
  color: var(--grn);
  border: 1px solid rgba(90, 171, 122, 0.25)
}

.pr-tag-r {
  background: var(--red-bg);
  color: var(--red);
  border: 1px solid rgba(212, 96, 90, 0.25)
}

.pr-body {
  margin-top: 0;
  padding: 10px 0 0 14px;
  border-left: 2px solid transparent;
  font-size: .88rem;
  line-height: 1.6;
  color: var(--muted);
  font-weight: 300
}

.pr-body-g {
  border-left-color: rgba(90, 171, 122, 0.35)
}

.pr-body-r {
  border-left-color: rgba(212, 96, 90, 0.35)
}

.pr-body strong {
  color: var(--text);
  font-weight: 500
}

.idle {
  color: var(--dim);
  font-size: .88rem;
  font-weight: 300;
  font-style: italic
}

.ft {
  margin-top: 40px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
  font-size: .78rem;
  color: var(--dim);
  font-weight: 300;
  line-height: 1.7;
  opacity: 0;
  animation: up .7s ease .4s forwards
}

.term {
  color: var(--accent);
  text-decoration: underline;
  text-decoration-color: rgba(180, 160, 212, 0.35);
  text-underline-offset: 2px;
  cursor: pointer;
  transition: text-decoration-color .2s
}

.term:hover {
  text-decoration-color: var(--accent)
}

/* Term modal (z-index 300 -- above chapter modal) */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s ease
}

.modal-overlay.open {
  opacity: 1;
  pointer-events: auto
}

.modal {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 28px 24px 24px;
  max-width: 480px;
  width: 100%;
  transform: translateY(12px) scale(0.97);
  transition: transform .25s ease;
  max-height: 80vh;
  overflow-y: auto
}

.modal-overlay.open .modal {
  transform: translateY(0) scale(1)
}

.modal-title {
  font-family: var(--serif);
  font-size: 1.2rem;
  margin-bottom: 12px;
  color: var(--text)
}

.modal-body {
  font-size: .88rem;
  line-height: 1.65;
  color: var(--muted);
  font-weight: 300
}

.modal-close {
  display: block;
  margin-top: 16px;
  padding: 8px 20px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--dim);
  font-family: var(--sans);
  font-size: .8rem;
  cursor: pointer;
  transition: border-color .2s, color .2s
}

.modal-close:hover {
  border-color: rgba(255, 255, 255, 0.2);
  color: var(--text)
}

.modal-nav {
  display: flex;
  gap: 6px;
  margin-bottom: 12px
}

.modal-nav-btn {
  width: 32px;
  height: 28px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--dim);
  font-size: 1rem;
  cursor: pointer;
  transition: border-color .2s, color .2s, opacity .2s;
  font-family: var(--sans);
  display: flex;
  align-items: center;
  justify-content: center
}

.modal-nav-btn:hover:not(:disabled) {
  border-color: rgba(255, 255, 255, 0.2);
  color: var(--text)
}

.modal-nav-btn:disabled {
  opacity: 0.25;
  cursor: default
}

/* Chapter modal (z-index 200) */
.ch-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: var(--bg);
  overflow-y: auto;
  opacity: 0;
  pointer-events: none;
  transition: opacity .3s ease
}

.ch-overlay.open {
  opacity: 1;
  pointer-events: auto
}

.ch-inner {
  max-width: 640px;
  margin: 0 auto;
  padding: 60px 24px 120px
}

.ch-close {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 210;
  width: 40px;
  height: 40px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--muted);
  font-size: 1.2rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color .2s, color .2s;
  opacity: 0;
  pointer-events: none
}

.ch-overlay.open .ch-close {
  opacity: 1;
  pointer-events: auto
}

.ch-close:hover {
  border-color: rgba(255, 255, 255, 0.2);
  color: var(--text)
}

.ch-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 210;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 14px 24px;
  background: linear-gradient(transparent, var(--bg) 40%);
  opacity: 0;
  pointer-events: none;
  transition: opacity .3s;
}

.ch-overlay.open .ch-nav {
  opacity: 1;
  pointer-events: auto
}

.ch-nav-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--muted);
  font-family: var(--sans);
  font-size: .8rem;
  font-weight: 400;
  cursor: pointer;
  transition: border-color .2s, color .2s;
  white-space: nowrap;
}

.ch-nav-btn:hover:not(:disabled) {
  border-color: rgba(255, 255, 255, 0.2);
  color: var(--text)
}

.ch-nav-btn:disabled {
  opacity: 0.25;
  cursor: default
}

.ch-nav-btn svg {
  width: 14px;
  height: 14px;
  flex-shrink: 0
}

.ch-nav-label {
  font-size: .72rem;
  color: var(--dim);
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ch-title {
  font-family: var(--serif);
  font-weight: 400;
  font-size: clamp(1.6rem, 4vw, 2.2rem);
  line-height: 1.2;
  margin-bottom: 28px;
  color: var(--text)
}

.ch-body {
  font-size: .92rem;
  line-height: 1.75;
  color: var(--muted);
  font-weight: 300
}

.ch-body p {
  margin-bottom: 16px
}

.ch-body strong {
  color: var(--text);
  font-weight: 500
}

.ch-body em {
  color: var(--text);
  font-style: italic
}

.ch-body h3 {
  font-family: var(--serif);
  font-weight: 400;
  font-size: 1.15rem;
  color: var(--text);
  margin: 28px 0 14px
}

.ch-link {
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
  text-decoration-color: rgba(180, 160, 212, 0.35);
  text-underline-offset: 2px;
  transition: text-decoration-color .2s
}

.ch-link:hover {
  text-decoration-color: var(--accent)
}

/* ToC floating button */
.toc-btn {
  position: fixed;
  bottom: 20px;
  left: 20px;
  z-index: 9999;
  width: 44px;
  height: 44px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--muted);
  font-size: 1.1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color .2s, color .2s, transform .2s, bottom .3s ease;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4)
}

.toc-btn:hover {
  border-color: rgba(255, 255, 255, 0.2);
  color: var(--text);
  transform: scale(1.05)
}

.toc-btn.above-nav {
  bottom: 72px
}

/* ToC modal */
.toc-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 200;
  display: flex;
  align-items: flex-end;
  justify-content: flex-start;
  padding: 24px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s ease
}

.toc-overlay.open {
  opacity: 1;
  pointer-events: auto
}

.toc-panel {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 24px;
  max-width: 380px;
  width: 100%;
  max-height: 70vh;
  overflow-y: auto;
  transform: translateY(20px) scale(0.97);
  transition: transform .3s cubic-bezier(0.34, 1.56, 0.64, 1)
}

.toc-overlay.open .toc-panel {
  transform: translateY(0) scale(1)
}

.toc-heading {
  font-family: var(--serif);
  font-size: 1.1rem;
  color: var(--text);
  margin-bottom: 16px
}

.toc-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: opacity .15s
}

.toc-item:last-child {
  border-bottom: none
}

.toc-item:hover {
  opacity: 0.8
}

.toc-item-text {
  flex: 1;
  min-width: 0
}

.toc-item-title {
  font-size: .85rem;
  font-weight: 400;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis
}

.toc-item-sub {
  font-size: .72rem;
  color: var(--dim);
  font-weight: 300;
  margin-top: 2px
}

.toc-progress-wrap {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  position: relative
}

.toc-progress-wrap svg {
  width: 100%;
  height: 100%;
  transform: rotate(-90deg)
}

.toc-progress-wrap circle {
  fill: none;
  stroke-width: 3
}

.toc-progress-bg {
  stroke: var(--border)
}

.toc-progress-fg {
  stroke: var(--accent);
  stroke-linecap: round;
  transition: stroke-dashoffset .3s
}

.toc-pct {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: .6rem;
  font-weight: 500;
  color: var(--dim)
}

/* Peek */
.peek {
  position: fixed;
  right: -90px;
  bottom: 80px;
  width: 120px;
  height: 140px;
  z-index: 50;
  cursor: pointer;
  transition: right .5s cubic-bezier(0.34, 1.56, 0.64, 1), opacity .3s;
  opacity: 0;
  pointer-events: none
}

.peek.show {
  right: -30px;
  opacity: 1;
  pointer-events: auto
}

.peek:hover {
  right: -20px
}

.peek img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: right bottom;
  filter: grayscale(0.3) brightness(0.85);
  transition: filter .2s
}

.peek:hover img {
  filter: grayscale(0) brightness(1)
}

.peek-placeholder {
  width: 100%;
  height: 100%;
  background: radial-gradient(ellipse 60% 45% at 50% 30%, var(--dim) 0%, transparent 100%);
  border-radius: 50% 50% 40% 40%;
  opacity: 0.5
}

.essay-link {
  color: var(--accent);
  cursor: pointer;
  text-decoration: none;
  border-bottom: 1px solid rgba(180, 160, 212, 0.3);
  transition: border-color .2s
}

.essay-link:hover {
  border-color: var(--accent)
}

@keyframes up {
  to {
    opacity: 1;
    transform: translateY(0)
  }
}

@keyframes fadeIn {
  from {
    opacity: 0
  }

  to {
    opacity: 1
  }
}

@media(max-width:600px) {
  .pg {
    padding: 36px 16px 80px
  }

  .pnl {
    padding: 18px 16px
  }

  .toc-btn {
    bottom: 14px;
    left: 14px
  }

  .toc-btn.above-nav {
    bottom: 66px
  }
}

.ch-body .ref {
  font-size: .8rem;
  color: var(--dim);
  line-height: 1.5;
  margin-bottom: 8px;
  padding-left: 16px;
  border-left: 1px solid var(--border)
}

/* Bibliography references */
.bib-ref {
  cursor: pointer;
  color: var(--accent);
  text-decoration: none;
  font-size: .75em;
  vertical-align: super;
  line-height: 0;
}

.bib-ref:hover {
  text-decoration: underline;
}

.bib-entry,
.ch-body .ref {
  transition: background-color .5s ease;
}

.bib-entry.highlight,
.ch-body .ref.highlight {
  background-color: rgba(180, 160, 212, 0.15);
}

/* Node descriptions */
.node-desc {
  font-size: .88rem;
  line-height: 1.6;
  color: var(--muted);
  font-weight: 300;
  margin-bottom: 14px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--border);
}

.node-desc strong {
  color: var(--text);
  font-weight: 500;
}
