:root {
  --bg: #ffffff;
  --fg: #111418;
  --muted: #5a6370;
  --primary: #1f5fbf;
  --border: #e6e8eb;
  --elev: rgba(255, 255, 255, 0.85);
  --shadow: 0 2px 8px rgba(16, 24, 40, 0.06);
  --radius: 12px;
  --maxw: 78ch;
  --lh: 1.7;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d0f12;
    --fg: #e6e9ef;
    --muted: #a0a7b3;
    --primary: #5aa0ff;
    --border: #1f2937;
    --elev: rgba(13, 15, 18, 0.7);
    --shadow: 0 2px 10px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }
html { font-size: 16px; }
body {
  margin: 0;
  color: var(--fg);
  background: var(--bg);
  font-family: ui-serif, Georgia, Cambria, "Times New Roman", Times, serif;
  line-height: var(--lh);
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.skip-link { position: absolute; left: -10000px; top: auto; width: 1px; height: 1px; overflow: hidden; }
.skip-link:focus { left: 1rem; top: 1rem; width: auto; height: auto; padding: .5rem .75rem; background: #ffd; border: 1px solid #cc0; z-index: 1000; }

.site-header { position: sticky; top: 0; z-index: 10; padding: 2rem 1rem .75rem; background: var(--elev); -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px); border-bottom: 1px solid var(--border); box-shadow: var(--shadow); }
.title { margin: 0 0 .25rem; font-size: 1.875rem; letter-spacing: .2px; }
.subtitle { margin: 0; color: var(--muted); font-size: 1rem; }

.container { max-width: var(--maxw); margin: 0 auto; padding: 1rem; }

.toc { margin: 1rem auto 1.25rem; padding: .5rem 1rem; border: 1px solid var(--border); border-radius: var(--radius); background: #fafbfc; }
.toc__list { list-style: none; margin: 0; padding: 0; display: flex; gap: .5rem; flex-wrap: wrap; }
.toc__list a { display: inline-block; padding: .35rem .65rem; border: 1px solid var(--border); border-radius: .6rem; background: #fff; color: var(--fg); text-decoration: none; }
.toc__list a:hover { background: #f3f5f7; }

.reader { margin: 1.25rem 0 2rem; font-size: 1.05rem; }
.chapter { display: none; }
.chapter__title { margin: 0 0 .75rem; font-size: 1.25rem; }
.chapter__content { hanging-punctuation: first; }
.chapter__content p { margin: .75rem 0; text-align: justify; hyphens: auto; }
.chapter__content p:first-of-type::first-letter { float: left; font-size: 2.6rem; line-height: 1; padding: .2rem .35rem 0 0; font-weight: 600; }

.chapter:target { display: block; }
#ch1 { display: block; }
@supports selector(:has(*)) {
  .reader:has(:target) .chapter { display: none; }
  .reader:has(:target) :target { display: block; }
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

@media print {
  .toc, .site-header, .skip-link { display: none !important; }
  .container { max-width: none; padding: 0; }
  body { color: #000; }
  .chapter { display: block; }
}

/* Enhancements */
@media (min-width: 768px) { html { font-size: 17px; } }
@media (min-width: 1024px) { html { font-size: 18px; } }

.chapter__title { font-size: 1.35rem; letter-spacing: .2px; }
.chapter__content p { margin: .85rem 0; }
.chapter__content p + p { text-indent: 1.1em; }
.chapter__content p:first-of-type { text-indent: 0; }

:where(a, button, [tabindex]:not([tabindex="-1"])):focus-visible {
  outline: 3px solid var(--primary);
  outline-offset: 2px;
  border-radius: 6px;
}

a, .back-to-top, .site-header { transition: background-color .2s ease, color .2s ease, box-shadow .2s ease, transform .2s ease; }

.back-to-top {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--elev);
  border: 1px solid var(--border);
  color: var(--fg);
  text-decoration: none;
  box-shadow: var(--shadow);
  opacity: .85;
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}
.back-to-top:hover, .back-to-top:focus-visible { opacity: 1; transform: translateY(-2px); }

@media (prefers-reduced-motion: reduce) {
  .back-to-top, .site-header { transition: none; }
}

@media print { .back-to-top { display: none !important; } }


/* Theme toggle (no JS) */
:root { color-scheme: light dark; }
.theme-toggle {
  position: absolute;
  width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.site-header {
  display: flex; align-items: center; gap: .75rem;
}
.site-header .title { flex: 0 0 auto; }
.site-header .subtitle { flex: 1 1 auto; }
.theme-button {
  width: 2.25rem; height: 2.25rem; flex: 0 0 auto;
  border-radius: 999px; border: 1px solid var(--border);
  background: var(--elev); color: var(--fg);
  display: grid; place-items: center; cursor: pointer;
  box-shadow: var(--shadow);
}
.theme-button::after { content: "🌙"; font-size: 1rem; line-height: 1; }
:root:has(#theme-toggle:checked) .theme-button::after { content: "☀️"; }
.theme-button:hover, .theme-button:focus-visible { transform: translateY(-1px); }

/* Force theme via checkbox state */
:root:has(#theme-toggle:not(:checked)) {
  /* Light theme forced */
  --bg: #ffffff; --fg: #111418; --muted: #5a6370; --primary: #1f5fbf; --border: #e6e8eb; --elev: rgba(255,255,255,.85); --shadow: 0 2px 8px rgba(16,24,40,.06);
  color-scheme: light;
}
:root:has(#theme-toggle:checked) {
  /* Dark theme forced */
  --bg: #0d0f12; --fg: #e6e9ef; --muted: #a0a7b3; --primary: #5aa0ff; --border: #1f2937; --elev: rgba(13,15,18,.7); --shadow: 0 2px 10px rgba(0,0,0,.35);
  color-scheme: dark;
}

@media print { .theme-button { display: none !important; } }

.site-footer {
    text-align: center;
    padding: 2rem 1rem;
    margin-top: 3rem;
    color: var(--muted);
    font-size: 0.9rem;
    border-top: 1px solid var(--border);
}

.site-footer p {
    margin: 0;
}
