:root {
  --ink: #0f1c24;
  --ink-soft: #3a4a54;
  --paper: #f3f6f4;
  --paper-2: #e7eeea;
  --mist: #c5d5ce;
  --accent: #0d7a6f;
  --accent-deep: #09564e;
  --accent-glow: rgba(13, 122, 111, 0.18);
  --line: rgba(15, 28, 36, 0.12);
  --shadow: 0 24px 60px rgba(15, 28, 36, 0.14);
  --font-display: "Fraunces", "Georgia", serif;
  --font-body: "Manrope", "Segoe UI", sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, monospace;
  --sidebar-w: 300px;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
}

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

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  color: var(--ink);
  font-family: var(--font-body);
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(13, 122, 111, 0.14), transparent 55%),
    radial-gradient(900px 500px at 100% 0%, rgba(45, 90, 120, 0.12), transparent 50%),
    linear-gradient(180deg, #eef3f0 0%, var(--paper) 40%, #e8efe9 100%);
  min-height: 100vh;
  line-height: 1.55;
}

a {
  color: inherit;
  text-decoration: none;
}

img {
  max-width: 100%;
  display: block;
}

.skip-link {
  position: absolute;
  left: -999px;
  top: 0;
  background: var(--ink);
  color: white;
  padding: 0.6rem 1rem;
  z-index: 1000;
}

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

/* ——— Landing ——— */
.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1.25rem clamp(1.25rem, 4vw, 3rem);
  position: sticky;
  top: 0;
  z-index: 50;
  backdrop-filter: blur(14px);
  background: rgba(243, 246, 244, 0.78);
  border-bottom: 1px solid transparent;
  transition: border-color 0.3s var(--ease), background 0.3s var(--ease);
}

.site-header.is-scrolled {
  border-bottom-color: var(--line);
  background: rgba(243, 246, 244, 0.92);
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 0.55rem;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.15rem;
  letter-spacing: -0.02em;
}

.brand span {
  color: var(--accent);
  font-weight: 500;
  font-size: 0.78rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-family: var(--font-body);
}

.nav-links {
  display: flex;
  gap: 1.25rem;
  align-items: center;
  font-size: 0.92rem;
  color: var(--ink-soft);
}

.nav-links a:hover {
  color: var(--accent-deep);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.85rem 1.35rem;
  border-radius: 999px;
  border: 1px solid transparent;
  font-family: var(--font-body);
  font-weight: 650;
  font-size: 0.95rem;
  cursor: pointer;
  transition: transform 0.25s var(--ease), background 0.25s var(--ease), box-shadow 0.25s var(--ease);
}

.btn:hover {
  transform: translateY(-1px);
}

.btn-primary {
  background: var(--ink);
  color: #f7fbf9;
  box-shadow: 0 10px 28px rgba(15, 28, 36, 0.22);
}

.btn-primary:hover {
  background: var(--accent-deep);
}

.btn-ghost {
  background: transparent;
  border-color: var(--line);
  color: var(--ink);
}

.btn-ghost:hover {
  border-color: var(--accent);
  background: var(--accent-glow);
}

.hero {
  position: relative;
  min-height: calc(100vh - 72px);
  display: grid;
  align-items: end;
  padding: clamp(2rem, 6vw, 5rem) clamp(1.25rem, 4vw, 3rem) clamp(3rem, 7vw, 5rem);
  overflow: hidden;
}

.hero::before {
  content: "";
  position: absolute;
  inset: 0;
  background:
    linear-gradient(120deg, rgba(15, 28, 36, 0.72) 0%, rgba(15, 28, 36, 0.35) 42%, rgba(15, 28, 36, 0.15) 100%),
    url("../assets/cover.jpg") center / cover no-repeat;
  transform: scale(1.04);
  animation: hero-drift 18s var(--ease) infinite alternate;
  z-index: 0;
}

@keyframes hero-drift {
  from { transform: scale(1.04) translate3d(0, 0, 0); }
  to { transform: scale(1.08) translate3d(-1.2%, -0.8%, 0); }
}

.hero-content {
  position: relative;
  z-index: 1;
  max-width: 42rem;
  color: #f4f8f6;
  animation: rise 0.9s var(--ease) both;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(18px); }
  to { opacity: 1; transform: translateY(0); }
}

.hero-kicker {
  font-size: 0.78rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  opacity: 0.8;
  margin-bottom: 1rem;
}

.hero h1 {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: clamp(3.4rem, 10vw, 6.4rem);
  line-height: 0.92;
  letter-spacing: -0.04em;
}

.hero p {
  margin: 1.25rem 0 0;
  font-size: clamp(1.05rem, 2.2vw, 1.25rem);
  max-width: 34rem;
  opacity: 0.92;
}

.hero-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-top: 1.75rem;
}

.hero .btn-primary {
  background: #f7fbf9;
  color: var(--ink);
}

.hero .btn-primary:hover {
  background: #ffffff;
}

.hero .btn-ghost {
  border-color: rgba(247, 251, 249, 0.35);
  color: #f7fbf9;
}

.section {
  padding: clamp(3rem, 7vw, 5.5rem) clamp(1.25rem, 4vw, 3rem);
}

.section-head {
  max-width: 40rem;
  margin-bottom: 2rem;
}

.section-head h2 {
  margin: 0 0 0.6rem;
  font-family: var(--font-display);
  font-size: clamp(1.8rem, 4vw, 2.6rem);
  letter-spacing: -0.03em;
  line-height: 1.1;
}

.section-head p {
  margin: 0;
  color: var(--ink-soft);
}

.parts {
  display: grid;
  gap: 1.25rem;
}

@media (min-width: 860px) {
  .parts {
    grid-template-columns: repeat(3, 1fr);
  }
}

.part {
  padding: 1.5rem 0 0.5rem;
  border-top: 1px solid var(--line);
  animation: rise 0.8s var(--ease) both;
}

.part:nth-child(2) { animation-delay: 0.08s; }
.part:nth-child(3) { animation-delay: 0.16s; }

.part-label {
  font-size: 0.75rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 0.55rem;
}

.part h3 {
  margin: 0 0 0.55rem;
  font-family: var(--font-display);
  font-size: 1.55rem;
  letter-spacing: -0.02em;
}

.part p {
  margin: 0;
  color: var(--ink-soft);
  font-size: 0.98rem;
}

.toc-grid {
  display: grid;
  gap: 2rem;
}

@media (min-width: 900px) {
  .toc-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

.toc-col h3 {
  margin: 0 0 0.85rem;
  font-family: var(--font-display);
  font-size: 1.25rem;
  padding-bottom: 0.55rem;
  border-bottom: 1px solid var(--line);
}

.toc-col ol {
  list-style: none;
  margin: 0;
  padding: 0;
}

.toc-col li a {
  display: grid;
  grid-template-columns: 2.2rem 1fr;
  gap: 0.4rem;
  padding: 0.42rem 0;
  border-bottom: 1px solid rgba(15, 28, 36, 0.05);
  color: var(--ink-soft);
  font-size: 0.94rem;
  transition: color 0.2s ease, padding-left 0.2s var(--ease);
}

.toc-col li a:hover {
  color: var(--accent-deep);
  padding-left: 0.25rem;
}

.toc-col li .num {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  color: var(--accent);
  padding-top: 0.15rem;
}

.diagram-strip {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(220px, 280px);
  gap: 1rem;
  overflow-x: auto;
  padding-bottom: 0.5rem;
  scroll-snap-type: x mandatory;
}

.diagram-card {
  scroll-snap-align: start;
  border: 1px solid var(--line);
  overflow: hidden;
  background: white;
  box-shadow: var(--shadow);
  transition: transform 0.3s var(--ease);
}

.diagram-card:hover {
  transform: translateY(-4px);
}

.diagram-card img {
  aspect-ratio: 16 / 10;
  object-fit: contain;
  object-position: center;
  background: #f7faf8;
  padding: 0.65rem;
}

.diagram-card figcaption {
  padding: 0.7rem 0.85rem;
  font-size: 0.82rem;
  color: var(--ink-soft);
}

.site-footer {
  padding: 2rem clamp(1.25rem, 4vw, 3rem) 3rem;
  border-top: 1px solid var(--line);
  color: var(--ink-soft);
  font-size: 0.9rem;
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  justify-content: space-between;
}

/* ——— Reader ——— */
body.reader {
  background: #dfe7e2;
  overflow: hidden;
  height: 100vh;
}

.reader-shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh;
}

.sidebar {
  background: rgba(243, 246, 244, 0.96);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sidebar-top {
  padding: 1rem 1rem 0.75rem;
  border-bottom: 1px solid var(--line);
}

.sidebar-top .brand {
  margin-bottom: 0.75rem;
}

.search {
  width: 100%;
  border: 1px solid var(--line);
  background: white;
  border-radius: 10px;
  padding: 0.65rem 0.8rem;
  font: inherit;
  color: var(--ink);
}

.search:focus {
  outline: 2px solid var(--accent-glow);
  border-color: var(--accent);
}

.toc-scroll {
  overflow: auto;
  padding: 0.75rem 0.5rem 1.5rem;
  flex: 1;
}

.toc-group {
  margin-bottom: 0.85rem;
}

.toc-group-title {
  font-size: 0.7rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--accent);
  padding: 0.45rem 0.65rem;
  font-weight: 700;
}

.toc-item {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  font: inherit;
  color: var(--ink-soft);
  padding: 0.45rem 0.65rem;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.88rem;
}

.toc-item:hover,
.toc-item.active {
  background: var(--accent-glow);
  color: var(--accent-deep);
}

.toc-item .num {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  margin-right: 0.4rem;
  opacity: 0.8;
}

.reader-main {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-width: 0;
  min-height: 0;
}

.reader-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.7rem 1rem;
  background: rgba(243, 246, 244, 0.9);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(10px);
}

.reader-title {
  font-family: var(--font-display);
  font-size: 1rem;
  letter-spacing: -0.02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.toolbar-controls {
  display: flex;
  align-items: center;
  gap: 0.45rem;
}

.icon-btn {
  border: 1px solid var(--line);
  background: white;
  width: 2.2rem;
  height: 2.2rem;
  border-radius: 8px;
  cursor: pointer;
  font: inherit;
  color: var(--ink);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.icon-btn:hover:not(:disabled) {
  border-color: var(--accent);
  background: var(--accent-glow);
}

.icon-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.page-indicator {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  color: var(--ink-soft);
  min-width: 5.5rem;
  text-align: center;
}

.page-stage {
  overflow: auto;
  padding: 1.25rem;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.page-frame {
  width: min(860px, 100%);
  background: white;
  box-shadow: var(--shadow);
  border: 1px solid rgba(15, 28, 36, 0.08);
  overflow: hidden;
  animation: page-in 0.35s var(--ease);
}

@keyframes page-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.page-frame img {
  width: 100%;
  height: auto;
}

.reader-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  padding: 0.65rem 1rem;
  border-top: 1px solid var(--line);
  background: rgba(243, 246, 244, 0.92);
  font-size: 0.85rem;
  color: var(--ink-soft);
}

.menu-toggle {
  display: none;
}

@media (max-width: 900px) {
  .reader-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(86vw, 320px);
    z-index: 80;
    transform: translateX(-105%);
    transition: transform 0.3s var(--ease);
    box-shadow: var(--shadow);
  }

  body.sidebar-open .sidebar {
    transform: translateX(0);
  }

  .menu-toggle {
    display: inline-flex;
  }

  .sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(15, 28, 36, 0.35);
    z-index: 70;
  }

  body.sidebar-open .sidebar-backdrop {
    display: block;
  }

  .nav-links .hide-sm {
    display: none;
  }
}
