/* ============================================================
   The Playbook — Tufte-inspired stylesheet
   Typography: Source Serif 4 (self-hosted)

   Principles:
   - ~65 characters per line
   - Sidenotes in the margin on desktop, inline on mobile
   - Warm off-white / warm dark
   - One typeface, three weights
   - Dark mode via prefers-color-scheme
   - Print-ready
   ============================================================ */

/* --- Font --- */

@font-face {
  font-family: 'Source Serif';
  src: url('../public/fonts/SourceSerif4-Regular.woff2') format('woff2');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'Source Serif';
  src: url('../public/fonts/SourceSerif4-Italic.woff2') format('woff2');
  font-weight: 400;
  font-style: italic;
  font-display: swap;
}

@font-face {
  font-family: 'Source Serif';
  src: url('../public/fonts/SourceSerif4-Semibold.woff2') format('woff2');
  font-weight: 600;
  font-style: normal;
  font-display: swap;
}

/* --- Reset & Base --- */

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

:root {
  /* Light mode */
  --bg: #fffff8;
  --bg-accent: #f8f4eb;
  --text: #111;
  --text-secondary: #555;
  --text-muted: #999;
  --link: #a00000;
  --link-hover: #700000;
  --border: #ddd;
  --code-bg: #f0ece3;
  --hr: #ccc;
  --nav-rule: #e0dcd4;

  /* Typography */
  --font-body: 'Source Serif', Georgia, 'Times New Roman', serif;
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
  --font-size: 1.125rem;
  --line-height: 1.65;
  --measure: 34em;
  --sidenote-width: 18em;
  --sidenote-gap: 1.5em;

  /* Layout */
  --page-max: calc(var(--measure) + var(--sidenote-width) + var(--sidenote-gap));
  --header-height: 3.5rem;
}

/* Dark palette: applied by system preference (auto) or manual toggle */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1a1a1a;
    --bg-accent: #242420;
    --text: #e0ddd5;
    --text-secondary: #aaa;
    --text-muted: #666;
    --link: #e8a0a0;
    --link-hover: #f0c0c0;
    --border: #333;
    --code-bg: #2a2a26;
    --hr: #3a3a36;
    --nav-rule: #333;
  }
}

/* Manual dark override (when system is light) */
:root[data-theme="dark"] {
  --bg: #1a1a1a;
  --bg-accent: #242420;
  --text: #e0ddd5;
  --text-secondary: #aaa;
  --text-muted: #666;
  --link: #e8a0a0;
  --link-hover: #f0c0c0;
  --border: #333;
  --code-bg: #2a2a26;
  --hr: #3a3a36;
  --nav-rule: #333;
}

/* --- Document --- */

html {
  font-size: 100%;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

@media (prefers-reduced-motion: no-preference) {
  html { scroll-behavior: smooth; }
}

/* --- Skip to Content --- */

.skip-to-content {
  position: absolute;
  left: -9999px;
  top: 0;
  padding: 0.5em 1em;
  background: var(--text);
  color: var(--bg);
  font-family: var(--font-sans);
  font-size: 0.85rem;
  z-index: 100;
  text-decoration: none;
}

.skip-to-content:focus {
  left: 0;
}

body {
  font-family: var(--font-body);
  font-size: var(--font-size);
  line-height: var(--line-height);
  color: var(--text);
  background-color: var(--bg);
}

/* ============================================================
   SITE HEADER — identical on every page
   ============================================================ */

.site-header {
  max-width: var(--page-max);
  margin: 0 auto;
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--nav-rule);
}

.site-header nav {
  display: flex;
  align-items: baseline;
  gap: 0;
  font-family: var(--font-sans);
  font-size: 0.8rem;
  letter-spacing: 0.02em;
  flex-wrap: wrap;
}

/* Theme toggle */
.theme-toggle {
  margin-left: auto;
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.2em 0.5em;
  font-family: var(--font-sans);
  font-size: 0.75rem;
  color: var(--text-muted);
  cursor: pointer;
  line-height: 1.4;
  white-space: nowrap;
}

.theme-toggle:hover {
  color: var(--text-secondary);
  border-color: var(--text-muted);
}

.site-header .nav-home {
  color: var(--text);
  text-decoration: none;
  font-weight: 600;
  margin-right: 2em;
  display: inline-flex;
  align-items: baseline;
  gap: 0.4em;
}

.site-header .nav-home:hover {
  color: var(--link);
}

.pb-mark {
  font-family: var(--font-body);
  font-style: italic;
  font-size: 1.1em;
  color: var(--link);
}

.site-header .nav-chapters {
  display: flex;
  gap: 1.5em;
  flex-wrap: wrap;
  list-style: none;
  padding: 0;
  margin: 0;
}

.site-header .nav-chapters li {
  margin: 0;
}

.site-header .nav-chapters a {
  color: var(--text-muted);
  text-decoration: none;
  white-space: nowrap;
}

.site-header .nav-chapters a:hover {
  color: var(--text-secondary);
}

.site-header .nav-chapters a[aria-current="page"] {
  color: var(--text);
  font-weight: 600;
}

/* ============================================================
   ARTICLE — the reading area, same frame on every page
   ============================================================ */

article {
  max-width: var(--page-max);
  margin: 0 auto;
  padding: 3rem 1.5rem 4rem;
  position: relative;
  min-height: calc(100vh - var(--header-height) - 8rem);
}

/* --- Page Header (every page has one) --- */

.page-header {
  margin-bottom: 2.5rem;
}

.page-label {
  font-family: var(--font-sans);
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  margin-bottom: 0.75rem;
}

.page-header h1 {
  font-size: 2.4rem;
  line-height: 1.15;
  font-weight: 400;
  font-style: italic;
  margin: 0 0 0.5rem;
  max-width: var(--measure);
}

[role="doc-subtitle"] {
  font-size: 1.15rem;
  font-style: normal;
  color: var(--text-secondary);
  max-width: var(--measure);
  margin: 0;
  line-height: 1.5;
}

/* Cluster subtitles in design-rules */
.cluster-subtitle {
  font-size: 1.15rem;
  font-style: normal;
  color: var(--text-secondary);
  margin-top: -0.5rem;
  margin-bottom: 1rem;
}

/* --- Headings --- */

h1 {
  font-size: 2.4rem;
  line-height: 1.15;
  margin-top: 2rem;
  margin-bottom: 1rem;
  font-weight: 400;
  font-style: italic;
}

h2 {
  font-size: 1.5rem;
  line-height: 1.3;
  margin-top: 3rem;
  margin-bottom: 0.8rem;
  font-weight: 400;
  font-style: italic;
}

h3 {
  font-size: 1.1rem;
  line-height: 1.4;
  margin-top: 2rem;
  margin-bottom: 0.5rem;
  font-weight: 600;
  font-style: normal;
}

/* --- Paragraphs --- */

p {
  max-width: var(--measure);
  margin-bottom: 1.2em;
}

/* --- Links --- */

a {
  color: var(--link);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}

a:hover {
  color: var(--link-hover);
}

/* --- Focus indicators --- */

:focus-visible {
  outline: 2px solid var(--link);
  outline-offset: 2px;
  border-radius: 2px;
}

/* --- Blockquotes --- */

blockquote {
  max-width: var(--measure);
  margin: 2em 0;
  padding-left: 1.5em;
  border-left: 3px solid var(--hr);
  font-style: italic;
  color: var(--text-secondary);
}

blockquote p {
  margin-bottom: 0.6em;
}

/* --- Lists --- */

ul, ol {
  max-width: var(--measure);
  margin-bottom: 1.2em;
  padding-left: 1.5em;
}

li {
  margin-bottom: 0.4em;
}

li > ul, li > ol {
  margin-top: 0.4em;
  margin-bottom: 0;
}

/* --- Horizontal Rules --- */

hr {
  max-width: var(--measure);
  border: none;
  border-top: 1px solid var(--hr);
  margin: 3rem 0;
}

/* --- Code --- */

code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  background: var(--code-bg);
  padding: 0.15em 0.35em;
  border-radius: 3px;
}

pre {
  max-width: var(--measure);
  overflow-x: auto;
  background: var(--code-bg);
  padding: 1em 1.2em;
  border-radius: 4px;
  margin-bottom: 1.5em;
  line-height: 1.5;
}

pre code {
  background: none;
  padding: 0;
  border-radius: 0;
  font-size: 0.85rem;
}

/* --- Tables --- */

table {
  max-width: var(--measure);
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 1.5em;
  font-size: 0.95rem;
}

thead {
  border-bottom: 2px solid var(--text);
}

th {
  text-align: left;
  padding: 0.5em 0.8em;
  font-weight: 600;
}

td {
  padding: 0.5em 0.8em;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

/* ============================================================
   SIDENOTES (Tufte's marginal notes)
   ============================================================ */

.sidenote {
  float: right;
  clear: right;
  margin-right: calc(-1 * (var(--sidenote-width) + var(--sidenote-gap)));
  width: var(--sidenote-width);
  font-size: 0.8rem;
  line-height: 1.45;
  color: var(--text-secondary);
  position: relative;
}

.sidenote {
  counter-increment: sidenote-counter;
}

.sidenote::before {
  content: counter(sidenote-counter);
  font-size: 0.6rem;
  position: relative;
  top: -0.4em;
  padding-right: 0.3em;
  font-weight: 600;
}

/* Toggle for mobile sidenotes */
.sidenote-toggle {
  display: none;
}

.sidenote-toggle:checked + .sidenote {
  display: block;
  float: none;
  width: 100%;
  margin: 0.8em 0 0.8em 1.5em;
  padding: 0.5em 0.8em;
  background: var(--bg-accent);
  border-radius: 4px;
}

/* ============================================================
   CHAPTER LIST (cover page)
   ============================================================ */

.chapter-list {
  max-width: var(--measure);
  list-style: none;
  padding: 0;
  margin: 2rem 0;
}

.chapter-list li {
  margin-bottom: 1.5em;
  padding-left: 0;
}

.chapter-list .chapter-number {
  font-family: var(--font-sans);
  font-size: 0.7rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  display: block;
  margin-bottom: 0.15em;
}

.chapter-list a {
  font-size: 1.25rem;
  font-style: italic;
  text-decoration: none;
  color: var(--text);
}

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

.chapter-list .chapter-desc {
  display: block;
  font-size: 0.9rem;
  color: var(--text-secondary);
  margin-top: 0.2em;
  line-height: 1.45;
}

/* ============================================================
   CHAPTER NAVIGATION (bottom of every page)
   ============================================================ */

.chapter-nav {
  max-width: var(--measure);
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-top: 4rem;
  padding-top: 2rem;
  border-top: 1px solid var(--hr);
  font-family: var(--font-sans);
  font-size: 0.85rem;
}

.chapter-nav a {
  color: var(--text-secondary);
  text-decoration: none;
}

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

.chapter-nav .prev::before {
  content: "\2190\00a0";
}

.chapter-nav .next::after {
  content: "\00a0\2192";
}

/* Placeholder to keep flex layout when only one link */
.chapter-nav .spacer {
  flex: 1;
}

/* ============================================================
   FOOTER — identical on every page
   ============================================================ */

.site-footer {
  max-width: var(--page-max);
  margin: 0 auto;
  padding: 2rem 1.5rem;
  font-family: var(--font-sans);
  font-size: 0.75rem;
  color: var(--text-muted);
  border-top: 1px solid var(--nav-rule);
}

.site-footer a {
  color: var(--text-muted);
}

.site-footer a:hover {
  color: var(--text-secondary);
}

/* --- Emphasis --- */

strong {
  font-weight: 600;
}

em {
  font-style: italic;
}

/* ============================================================
   RESPONSIVE
   ============================================================ */

/* Wide: sidenotes in margin */
@media (min-width: 1080px) {
  article {
    padding-left: 1.5rem;
    padding-right: calc(var(--sidenote-width) + var(--sidenote-gap) + 1.5rem);
  }
}

/* Narrow: sidenotes collapse */
@media (max-width: 1079px) {
  .sidenote {
    display: none;
    float: none;
    width: 100%;
    margin: 0.8em 0 0.8em 1.5em;
    padding: 0.5em 0.8em;
    background: var(--bg-accent);
    border-radius: 4px;
  }

  .sidenote-toggle {
    display: inline;
    cursor: pointer;
    color: var(--link);
    font-size: 0.65rem;
    position: relative;
    top: -0.3em;
    font-weight: 600;
  }

  article {
    padding: 2rem 1rem 3rem;
  }

  h1, .page-header h1 {
    font-size: 1.8rem;
  }

  h2 {
    font-size: 1.3rem;
  }
}

/* Small screens */
@media (max-width: 600px) {
  :root {
    --font-size: 1rem;
  }

  article {
    padding: 1.5rem 0.8rem 2rem;
  }

  h1, .page-header h1 {
    font-size: 1.6rem;
  }

  .site-header nav {
    font-size: 0.75rem;
  }

  .site-header .nav-chapters {
    gap: 1em;
  }

  .chapter-nav {
    flex-direction: column;
    gap: 0.8em;
    align-items: flex-start;
  }
}

/* ============================================================
   PRINT
   ============================================================ */

@media print {
  :root {
    --bg: #fff;
    --text: #000;
    --text-secondary: #333;
    --link: #000;
  }

  body {
    font-size: 11pt;
    line-height: 1.5;
  }

  .site-header,
  .site-footer,
  .chapter-nav,
  .sidenote-toggle {
    display: none;
  }

  .sidenote {
    display: block;
    float: right;
    clear: right;
    margin-right: -4cm;
    width: 3.5cm;
    font-size: 8pt;
  }

  article {
    max-width: none;
    padding: 0;
    margin: 0 4cm 0 0;
  }

  a {
    color: #000;
    text-decoration: none;
  }

  a[href^="http"]::after {
    content: " (" attr(href) ")";
    font-size: 0.8em;
    color: #555;
  }

  h1, h2, h3 {
    page-break-after: avoid;
  }

  p, blockquote {
    orphans: 3;
    widows: 3;
  }

  pre {
    page-break-inside: avoid;
    border: 1px solid #ddd;
  }
}
