:root {
  color-scheme: light dark;
  --bg: #f3f0e8;
  --page: #fffdf8;
  --ink: #201d18;
  --muted: #6d665b;
  --accent: #755a27;
  --rule: #d8d0c1;
  --box: #f3efe6;
  --box-edge: #b8aa8d;
  --shadow: rgba(30, 24, 16, .14);
  --max-text: 42rem;
}

:root[data-theme="dark"] {
  --bg: #11100e;
  --page: #1b1916;
  --ink: #e7e0d3;
  --muted: #aaa092;
  --accent: #d7b86f;
  --rule: #494137;
  --box: #24201a;
  --box-edge: #756344;
  --shadow: rgba(0, 0, 0, .45);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11100e;
    --page: #1b1916;
    --ink: #e7e0d3;
    --muted: #aaa092;
    --accent: #d7b86f;
    --rule: #494137;
    --box: #24201a;
    --box-edge: #756344;
    --shadow: rgba(0, 0, 0, .45);
  }
}

* { box-sizing: border-box; }

html {
  scroll-behavior: smooth;
  background: var(--bg);
  color: var(--ink);
  -webkit-text-size-adjust: 100%;
}

body.book-document {
  margin: 0;
  min-height: 100vh;
  background-color: var(--bg);
  background-image: var(--book-background-image, none);
  background-attachment: fixed;
  background-size: cover;
  color: var(--ink);
  font-family: Georgia, "Times New Roman", serif;
  font-size: 17px;
  line-height: 1.62;
}

body.book-document > #wrap,
body.book-document > .book,
body.book-document > section,
body.book-document > .page-reset {
  width: min(100%, calc(var(--max-text) + 3.6rem));
}

#wrap,
.book {
  max-width: var(--max-text);
  margin: 0 auto;
  padding: 0 1.8rem 6rem;
  background: color-mix(in srgb, var(--page) 96%, transparent);
  box-shadow: 0 0 36px var(--shadow);
}

body.book-document > section,
body.book-document > .page-reset {
  max-width: var(--max-text);
  margin-left: auto;
  margin-right: auto;
}

body.book-document > section {
  padding-left: 1.4rem;
  padding-right: 1.4rem;
}

a {
  color: var(--accent);
  text-decoration: none;
}

a:hover { text-decoration: underline; }

p {
  margin: 0 0 .85rem;
  text-align: justify;
  hyphens: auto;
}

h1, h2, h3, h4 {
  color: var(--ink);
  line-height: 1.28;
  text-wrap: balance;
}

h1 {
  margin: 2.7rem 0 1.15rem;
  padding: .55rem 0;
  border-top: 1px solid var(--rule);
  border-bottom: 1px solid var(--rule);
  color: var(--accent);
  font-size: 1.38rem;
  font-style: italic;
  font-variant: small-caps;
  font-weight: normal;
  letter-spacing: .075em;
  text-align: center;
}

h2 {
  margin: 1.9rem 0 .55rem;
  font-size: 1.08rem;
}

h3 {
  margin: 1.35rem 0 .45rem;
  font-size: 1rem;
}

blockquote {
  margin: 1.35rem 0 1.6rem;
  padding: .85rem 1rem;
  border-left: 3px solid var(--box-edge);
  background: var(--box);
  color: var(--ink);
}

blockquote p { text-align: left; }

blockquote.keyconcept {
  font-style: normal;
  border-left-color: var(--accent);
}

hr {
  border: 0;
  border-top: 1px solid var(--rule);
  margin: 2rem 0;
}

ul, ol { padding-left: 1.35rem; }
li { margin: 0 0 .4rem; }

table {
  width: 100%;
  border-collapse: collapse;
  margin: 1.25rem 0 1.65rem;
  font-size: .92rem;
}

th, td {
  padding: .45rem .55rem;
  border-bottom: 1px solid var(--rule);
  text-align: left;
  vertical-align: top;
}

th {
  border-bottom-color: var(--box-edge);
  background: var(--box);
}

/* Shared fixed reader controls */
.theme-toggle {
  position: fixed;
  top: .8rem;
  right: .8rem;
  z-index: 1000;
  border: 1px solid var(--box-edge);
  border-radius: 999px;
  padding: .48rem .82rem;
  background: color-mix(in srgb, var(--box) 94%, transparent);
  color: var(--ink);
  box-shadow: 0 2px 10px var(--shadow);
  font: 600 .78rem/1 system-ui, -apple-system, "Segoe UI", sans-serif;
  cursor: pointer;
}

.theme-toggle:hover { filter: brightness(1.04); }

.jump-top,
a.jumptop {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  z-index: 999;
  display: inline-block;
  padding: .45rem .7rem;
  border-radius: 999px;
  background: color-mix(in srgb, var(--ink) 86%, transparent);
  color: var(--page);
  font: 600 .75rem/1 system-ui, -apple-system, "Segoe UI", sans-serif;
  text-decoration: none;
}

/* Covers and title pages */
header.cover,
.title-page,
section.titlepage {
  text-align: center;
  padding: 4rem 0 3rem;
  border: 0;
}

header.cover img {
  width: min(70%, 20rem);
  margin: 0 auto 1.3rem;
  box-shadow: 0 4px 24px var(--shadow);
}

h1.booktitle,
.title-page h1,
section.titlepage h1 {
  margin: .65rem 0 .45rem;
  padding: 0;
  border: 0;
  color: var(--ink);
  font-size: clamp(2rem, 6vw, 2.8rem);
  font-style: normal;
  font-variant: normal;
  font-weight: normal;
  letter-spacing: .015em;
  text-align: center;
}

.booksub,
.title-page .sub,
section.titlepage h3 {
  margin: 0 0 .65rem;
  color: var(--muted);
  font-size: 1.08rem;
  font-style: italic;
  text-align: center;
}

.byline,
.title-page .desc,
section.titlepage p {
  max-width: 34rem;
  margin: .35rem auto 1rem;
  color: var(--muted);
  text-align: center;
}

section.dedication {
  padding-top: 3rem;
  padding-bottom: 3rem;
  text-align: center;
}

section.dedication h2 { display: none; }
section.dedication p { text-align: center; font-style: italic; }

/* Chapters */
section {
  padding-top: 2.5rem;
  padding-bottom: .2rem;
}

section.chapter,
section.backmatter,
.chapter {
  scroll-margin-top: 1rem;
}

section.chapter > h1,
section.backmatter > h1,
.ch-title {
  margin: 2.2rem 0 2.2rem;
  padding: .65rem 0;
  border-top: 1px solid var(--rule);
  border-bottom: 1px solid var(--rule);
  color: var(--accent);
  font-size: 1.28rem;
  font-style: italic;
  font-variant: small-caps;
  font-weight: normal;
  letter-spacing: .075em;
  line-height: 1.3;
  text-align: center;
}

.ch-body { padding-bottom: 1rem; }

h1.part {
  margin-top: 3.2rem;
  border: 0;
  color: var(--ink);
  font-size: 1.6rem;
}

/* Tables of contents */
nav#TOC,
div.toc,
.toc {
  margin: 1rem 0 3rem;
  padding: 1.15rem 1.3rem;
  border: 1px solid var(--rule);
  border-radius: .35rem;
  background: var(--box);
}

nav#TOC ul {
  list-style: none;
  padding-left: 0;
}

nav#TOC ul ul { padding-left: 1rem; }

.toc > a {
  display: block;
  padding: .25rem 0;
}

div.tocline {
  display: flex;
  align-items: baseline;
  overflow: hidden;
  white-space: nowrap;
  line-height: 1.75;
}

div.tocline a {
  display: contents;
  color: var(--ink);
}

span.tt { flex: 0 0 auto; padding-right: .5rem; }
span.nn { display: inline-block; width: 2rem; text-align: right; padding-right: .5rem; }
span.dots { flex: 1 1 0; overflow: hidden; min-width: 0; color: var(--muted); }
span.dots::before {
  content: ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .";
  letter-spacing: 3px;
  white-space: nowrap;
}
span.pn { flex: 0 0 auto; padding-left: .5rem; min-width: 2rem; text-align: right; }

/* Figures */
img {
  max-width: 100%;
  height: auto;
}

.figblock,
.figure,
p:has(> img) {
  margin: 1.55rem 0 .45rem;
  text-align: center;
}

.figblock img,
.figure img,
p > img {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 0 auto;
  border-radius: 3px;
}

.figcap,
.fig-caption {
  margin: .4rem 0 1.85rem;
  padding: 0 .25rem;
  color: var(--muted);
  font-size: .86rem;
  font-style: italic;
  line-height: 1.45;
  text-align: left;
}

.figcap strong,
.fig-label {
  color: var(--ink);
  font-style: normal;
  font-weight: bold;
}

.sep {
  margin: 2.4rem auto;
  color: var(--muted);
  text-align: center;
  letter-spacing: .12em;
}

/* Pandoc utility classes */
code { white-space: pre-wrap; }
span.smallcaps { font-variant: small-caps; }
div.columns { display: flex; gap: min(4vw, 1.5em); }
div.column { flex: auto; overflow-x: auto; }
div.hanging-indent { margin-left: 1.5em; text-indent: -1.5em; }
ul.task-list[class] { list-style: none; }
ul.task-list li input[type="checkbox"] {
  width: .8em;
  margin: 0 .8em .2em -1.6em;
  vertical-align: middle;
  font-size: inherit;
}
.display.math { display: block; margin: .5rem auto; text-align: center; }

@media (max-width: 700px) {
  body.book-document { font-size: 16px; }

  #wrap,
  .book {
    padding: 0 1rem 4rem;
    box-shadow: none;
  }

  body.book-document > section {
    padding-left: 1rem;
    padding-right: 1rem;
  }

  header.cover,
  .title-page,
  section.titlepage {
    padding-top: 3rem;
  }

  .theme-toggle {
    top: .55rem;
    right: .55rem;
  }

  table {
    display: block;
    overflow-x: auto;
  }
}

@media print {
  :root {
    --bg: #fff;
    --page: #fff;
    --ink: #000;
    --muted: #555;
    --accent: #333;
    --rule: #bbb;
    --box: #f4f4f4;
  }

  body.book-document { background: #fff; }

  #wrap,
  .book {
    box-shadow: none;
    max-width: none;
  }

  .theme-toggle,
  .jump-top,
  a.jumptop {
    display: none !important;
  }
}
/* --- Canonical shared book layout: based on A Brief History of Almost Everywhere --- */
body.book-document {
  background-position: center top;
  background-repeat: no-repeat;
  background-size: cover;
}

#wrap {
  width: min(100%, calc(var(--max-text) + 3.6rem));
  max-width: calc(var(--max-text) + 3.6rem);
  min-height: 100vh;
  margin: 0 auto;
  padding: 0 1.8rem 6rem;
  background: color-mix(in srgb, var(--page) 97%, transparent);
  box-shadow: 0 0 36px var(--shadow);
}

#wrap > header.cover {
  max-width: var(--max-text);
  margin: 0 auto;
}

#wrap > nav#TOC,
#wrap > h1,
#wrap > h2,
#wrap > h3,
#wrap > p,
#wrap > blockquote,
#wrap > table,
#wrap > ul,
#wrap > ol,
#wrap > .figblock,
#wrap > .figure,
#wrap > section.dedication,
#wrap > span[id] {
  max-width: var(--max-text);
  margin-left: auto;
  margin-right: auto;
}

#wrap > nav#TOC {
  margin-top: 1rem;
  margin-bottom: 3rem;
}

#wrap > h1.part {
  margin-top: 3.4rem;
}

#wrap > h1:not(.booktitle):not(.part) {
  margin-top: 2.9rem;
}

#wrap > section.dedication {
  padding: 2.2rem 0 2.6rem;
}

@media (max-width: 700px) {
  #wrap {
    width: 100%;
    padding: 0 1rem 4rem;
  }
}
