/* ==========================================================================
   This Is the Way — Standalone Stylesheet
   No tufte.css dependency. Crimson Pro (Google Fonts) for body text,
   system-ui for navigation/UI elements.
   ========================================================================== */

/* --------------------------------------------------------------------------
   Fonts & custom properties
   -------------------------------------------------------------------------- */

:root {
  --sidebar-w:   280px;
  --content-max: 660px;
  --serif:       'Crimson Pro', Georgia, 'Times New Roman', serif;
  --sans:        -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;

  /* Light theme — Slate */
  --bg:          #f9f9f9;
  --sidebar-bg:  #f0f0f1;
  --text:        #1a1a1a;
  --muted:       #767676;
  --accent:      #4a5568;
  --link:        #2d3748;
  --active-bg:   #dde1e8;
  --active-text: #2d3748;
  --border:      #d4d8de;
  --progress:    #4a5568;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:          #18191a;
    --sidebar-bg:  #111213;
    --text:        #e8e8e8;
    --muted:       #909090;
    --accent:      #8fa3be;
    --link:        #8fa3be;
    --active-bg:   #22262d;
    --active-text: #a8bdd4;
    --border:      #2e3138;
    --progress:    #8fa3be;
  }
}

/* --------------------------------------------------------------------------
   Reset
   -------------------------------------------------------------------------- */

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

html {
  font-size: 18px;
}

body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--serif);
  line-height: 1.75;
  /* Critical for dark-mode rendering — prevents the "stroked" text effect */
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* --------------------------------------------------------------------------
   Skip link
   -------------------------------------------------------------------------- */

.skip-link {
  position: absolute;
  top: -100%;
  left: 1rem;
  z-index: 300;
  background: var(--accent);
  color: #fff;
  font-family: var(--sans);
  font-size: 0.85rem;
  padding: 0.5rem 1.1rem;
  border-radius: 0 0 4px 4px;
  text-decoration: none;
}
.skip-link:focus { top: 0; }

/* --------------------------------------------------------------------------
   Layout shell
   -------------------------------------------------------------------------- */

.layout {
  display: flex;
  min-height: 100vh;
}

/* --------------------------------------------------------------------------
   Sidebar
   -------------------------------------------------------------------------- */

.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  height: 100vh;
  position: sticky;
  top: 0;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  flex-shrink: 0;
  font-family: var(--sans);
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.sidebar::-webkit-scrollbar       { width: 4px; }
.sidebar::-webkit-scrollbar-track { background: transparent; }
.sidebar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.sidebar-header {
  padding: 1.4rem 1.4rem 1.2rem;
  border-bottom: 1px solid var(--border);
}

.sidebar-title {
  margin: 0 0 0.2rem;
  font-size: 0.9rem;
  font-weight: 600;
  line-height: 1.25;
  color: var(--text);
  letter-spacing: 0.01em;
}

.sidebar-author {
  margin: 0;
  font-size: 0.72rem;
  color: var(--muted);
  letter-spacing: 0.02em;
}

/* --------------------------------------------------------------------------
   Table of Contents
   -------------------------------------------------------------------------- */

.toc {
  list-style: none;
  margin: 0;
  padding: 0.5rem 0 1.5rem;
}

.toc li {
  margin: 0;
}

.toc a {
  display: block;
  padding: 0.27rem 1.4rem;
  font-size: 0.72rem;
  font-style: normal;
  font-weight: 400;
  color: var(--text);
  text-decoration: none;
  line-height: 1.5;
  opacity: 0.8;
  transition: background 0.1s, color 0.1s, opacity 0.1s;
}

.toc a:hover {
  background: var(--active-bg);
  opacity: 1;
}

.toc a.active {
  background: var(--active-bg);
  color: var(--active-text);
  font-weight: 600;
  opacity: 1;
}

/* Suppress italic rendering inside TOC links (e.g. Latin titles) */
.toc a em {
  font-style: normal;
}

.toc-intro a,
.toc-postscript a {
  opacity: 0.6;
}

.toc-divider {
  height: 1px;
  background: var(--border);
  margin: 0.5rem 1.4rem;
  opacity: 0.5;
}

/* --------------------------------------------------------------------------
   Sidebar toggle (mobile only)
   -------------------------------------------------------------------------- */

.sidebar-toggle {
  display: none;
  position: fixed;
  top: 0.75rem;
  left: 0.75rem;
  z-index: 200;
  align-items: center;
  gap: 0.4rem;
  font-family: var(--sans);
  font-size: 0.78rem;
  background: var(--sidebar-bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.35rem 0.8rem;
  border-radius: 4px;
  cursor: pointer;
  line-height: 1;
  box-shadow: 0 1px 4px rgba(0,0,0,0.1);
}

/* --------------------------------------------------------------------------
   Content area
   -------------------------------------------------------------------------- */

.content {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  position: relative;
}

/* --------------------------------------------------------------------------
   Reading progress bar
   -------------------------------------------------------------------------- */

.progress-bar {
  position: fixed;
  top: 0;
  left: var(--sidebar-w);
  right: 0;
  height: 2px;
  background: var(--progress);
  transform-origin: left center;
  transform: scaleX(0);
  z-index: 100;
  transition: transform 0.08s linear;
  pointer-events: none;
}

/* --------------------------------------------------------------------------
   Chapter articles
   -------------------------------------------------------------------------- */

.chapter {
  max-width: var(--content-max);
  margin: 0 auto;
  padding: 4rem 2.5rem 1.5rem;
}

.chapter[hidden] {
  display: none;
}

/* Intro cover */
.chapter-cover {
  text-align: center;
  padding: 2rem 0 2.5rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 2.5rem;
}

.chapter-cover img {
  width: 96px;
  height: auto;
  display: block;
  margin: 0 auto 1.4rem;
  opacity: 0.8;
}

.chapter-cover h1 {
  margin: 0 0 0.4rem;
  font-family: var(--serif);
  font-size: 2.8rem;
  font-weight: 400;
  font-style: italic;
  line-height: 1.1;
  color: var(--text);
}

.chapter-cover .byline {
  margin: 0;
  font-family: var(--sans);
  font-size: 0.85rem;
  color: var(--muted);
  letter-spacing: 0.04em;
}

/* Drop cap on the intro's first paragraph */
#intro .chapter-body > p:first-child::first-letter {
  font-size: 4.5rem;
  line-height: 0.72;
  float: left;
  margin-right: 0.08em;
  margin-top: 0.06em;
  color: var(--accent);
  font-weight: 400;
}

/* Chapter heading */
.chapter-heading {
  margin: 0 0 2rem;
}

.chapter-number {
  display: block;
  font-family: var(--sans);
  font-size: 0.65rem;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  margin-bottom: 0.5rem;
}

.chapter-title {
  margin: 0;
  font-family: var(--serif);
  font-size: 2rem;
  font-weight: 400;
  font-style: italic;
  line-height: 1.2;
  color: var(--text);
  padding-bottom: 1.4rem;
  border-bottom: 1px solid var(--border);
}

/* --------------------------------------------------------------------------
   Prose
   -------------------------------------------------------------------------- */

.chapter p {
  margin: 0 0 1.2rem;
  font-size: 1rem;
  line-height: 1.8;
  color: var(--text);
}

.chapter blockquote {
  margin: 1.5rem 0;
  padding: 0 0 0 1.4rem;
  border-left: 2px solid var(--border);
  color: var(--muted);
}

.chapter blockquote p {
  font-size: 0.97rem;
  margin-bottom: 0.5rem;
}

.chapter blockquote p:last-child {
  margin-bottom: 0;
}

.chapter a {
  color: var(--link);
  text-decoration: underline;
  text-decoration-color: color-mix(in srgb, var(--link) 35%, transparent);
  text-underline-offset: 2px;
  transition: text-decoration-color 0.1s;
}

.chapter a:hover {
  text-decoration-color: var(--link);
}

.chapter img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 2rem auto;
  border-radius: 2px;
}

.chapter ol,
.chapter ul {
  margin: 0 0 1.2rem;
  padding-left: 1.5rem;
}

.chapter li {
  font-size: 1rem;
  line-height: 1.75;
  margin-bottom: 0.35rem;
}

.chapter hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 2rem auto;
  width: 36%;
}

.chapter em {
  font-style: italic;
}

.chapter strong {
  font-weight: 600;
}

/* --------------------------------------------------------------------------
   Chapter footer navigation
   -------------------------------------------------------------------------- */

.chapter-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: var(--content-max);
  margin: 0 auto;
  padding: 1.5rem 2.5rem 3.5rem;
  border-top: 1px solid var(--border);
  gap: 1rem;
}

.nav-btn {
  font-family: var(--sans);
  font-size: 0.78rem;
  background: none;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.45rem 1.1rem;
  border-radius: 3px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
}

.nav-btn:hover:not(:disabled) {
  background: var(--active-bg);
  border-color: var(--accent);
  color: var(--accent);
}

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

.chapter-counter {
  font-family: var(--sans);
  font-size: 0.68rem;
  color: var(--muted);
  text-align: center;
  flex: 1;
  letter-spacing: 0.03em;
}

/* --------------------------------------------------------------------------
   Mobile
   -------------------------------------------------------------------------- */

@media (max-width: 740px) {
  .sidebar-toggle {
    display: flex;
  }

  .sidebar {
    position: fixed;
    left: calc(-1 * var(--sidebar-w));
    top: 0;
    height: 100%;
    z-index: 190;
    transition: left 0.22s ease;
  }

  .sidebar.open {
    left: 0;
    box-shadow: 4px 0 28px rgba(0,0,0,0.25);
  }

  .sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 180;
  }

  .sidebar-overlay.visible {
    display: block;
  }

  .progress-bar {
    left: 0;
  }

  .chapter {
    padding: 3.5rem 1.25rem 1rem;
  }

  .chapter-nav {
    padding: 1.25rem 1.25rem 2.5rem;
  }

  .chapter-cover h1 { font-size: 2rem; }
  .chapter-title    { font-size: 1.6rem; }
}

@media (max-width: 400px) {
  .chapter     { padding: 3rem 1rem 1rem; }
  .chapter-nav { padding: 1rem 1rem 2rem; }
  .chapter-cover h1 { font-size: 1.7rem; }
  .chapter-title    { font-size: 1.4rem; }
}
