/* ============================================================
   book reader — a side-to-side, paginated reading page for
   Project entries with format: book. Adapted from Robin Sloan's
   "Perfect Edition" template (MIT), restyled in reference's own
   type and color system instead of the original's Vollkorn/pink:
   Source Serif 4 for body text, Instrument Serif for the title
   page and chapter headings (matching the site's other display
   headlines), the sitewide ink/accent tokens, no italics.
   This page is intentionally its own document — a fully
   immersive reading view — so it does not load style.css.
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=Source+Serif+4:ital,opsz,wght@0,8..60,400..600;1,8..60,400&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Instrument+Serif&display=swap');

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

:root {
  --base-unit: 5vh;
  --column-width: 50vw;
}

@supports (width: clamp(28px, 5vh, 48px)) {
  :root {
    --base-unit: clamp(28px, 5vh, 48px);
    --column-width: clamp(480px, 50vw, 640px);
  }
}

:root {
  --base-unit-half: calc(var(--base-unit) / 2.0);
  --body-text-font-stack: 'Source Serif 4', ui-serif, Georgia, serif;
  --display-font-stack: 'Instrument Serif', var(--body-text-font-stack);
  --font-size: var(--base-unit-half);
  --line-height: 1.5rem;
  --vertical-padding: var(--base-unit);
  --column-height: calc(100vh - calc(var(--vertical-padding) * 4.0));
  --column-height: calc(100svh - calc(var(--vertical-padding) * 4.0));
  --column-gap: var(--base-unit);
  --page-bg: #ffffff;
  --shade-color-destination: rgba(255, 255, 255, 0);
  --shade-color-origin: rgba(255, 255, 255, 0.82);
  --accent: #2358ff;
  --selection-color: rgba(35, 88, 255, 0.25);
  --ink: #201d16;
  --ink-soft: #55503f;
  --hairline: rgba(32, 29, 22, 0.14);
}

::-moz-selection { background: var(--selection-color); }
::selection { background: var(--selection-color); }

button {
  touch-action: manipulation;
  background: none;
  background-color: transparent;
  border: none;
  outline: none;
  font-family: inherit;
}

html.book-reader {
  scroll-behavior: auto;
  overflow: hidden; /* the key: the page itself never scrolls vertically */
  font-size: var(--font-size);
  font-feature-settings: "kern", "liga";
}

body.book-reader {
  height: 100vh;
  overflow-x: auto;
  overflow-y: hidden;
  margin: 0;
  padding: var(--vertical-padding) 0 0 0;
  background-color: var(--page-bg);
  font-family: var(--body-text-font-stack);
  font-size: 1rem;
  font-weight: 400;
  font-style: normal;
  text-align: left;
  line-height: var(--line-height);
  color: var(--ink);
}

/* no italics anywhere, including markdown *emphasis* */
.book-reader em, .book-reader i, .book-reader cite { font-style: normal; }

/* -- the title page's full-screen color (see paintWash in the template) -- */

.book-reader__wash {
  position: fixed;
  inset: 0;
  background: var(--cover-bg, var(--page-bg));
  opacity: 0;
  pointer-events: none;
  z-index: 0;
}

.is-on-cover .book-reader__home { color: var(--cover-ink); }
.is-on-cover button.toc-button { background: transparent; border-color: var(--cover-ink); color: var(--cover-ink); }

/* -- a quiet way back to the rest of the site -- */

.book-reader__home {
  position: fixed;
  top: var(--base-unit-half);
  left: var(--base-unit-half);
  padding: 0.3em 0;
  font-family: var(--body-text-font-stack);
  font-size: 0.9rem;
  font-weight: 500;
  letter-spacing: 0.02em;
  text-decoration: none;
  color: var(--ink-soft);
  z-index: 100000;
}

.book-reader__home:hover { color: var(--accent); }

/* -- title page + tutorial (each is its own full page) -- */

div.book div.tutorial {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--line-height);
  break-after: column;
  width: var(--column-width);
  height: calc(var(--column-height) - 1px);
  overflow: hidden;
  text-align: center;
}

div.book div.tutorial h2 {
  flex-grow: 0;
  height: auto;
  margin: 0 0 var(--base-unit-half);
  break-before: avoid-column;
  font-family: var(--display-font-stack);
  font-weight: 400;
  font-size: 2.25rem;
}

div.book div.tutorial p {
  flex-grow: 0;
  margin: 0;
  text-indent: 0;
}

div.book div.tutorial .caption {
  display: block;
  margin-bottom: 0.15em;
  font-family: var(--body-text-font-stack);
  font-size: 0.75rem;
  font-weight: 500;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

div.book div.tutorial small {
  font-size: 0.85rem;
  font-weight: 500;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

/* -- controls: the toc button, fixed bottom-center -- */

button.toc-button {
  position: fixed;
  display: flex;
  flex-direction: column;
  justify-content: center;
  bottom: 0;
  left: 50vw;
  cursor: pointer;
  width: calc(var(--base-unit) * 2.0);
  height: var(--base-unit);
  transform: translateX(-50%) translateY(-50%);
  background-color: var(--page-bg);
  border: 1px solid var(--ink);
  color: inherit;
  z-index: 100000;
}

button.toc-button:hover {
  border-color: var(--accent);
  color: var(--accent);
}

button.toc-button svg {
  flex-grow: 1;
  fill: currentColor;
}

/* -- table of contents overlay -- */

nav {
  position: fixed;
  top: calc(var(--vertical-padding) + var(--base-unit));
  left: calc(50vw - var(--column-width) * 0.45);
  width: calc(var(--column-width) * 0.9);
  height: calc(var(--column-height) - var(--base-unit));
  overflow: hidden;
  background-color: var(--page-bg);
  border: 1px solid var(--accent);
  box-shadow: 0 4px calc(var(--base-unit-half) * 1.5) rgba(3, 3, 43, 0.12);
  transition: opacity 0.15s;
  text-align: left;
  z-index: 10000;
}

nav.visible { pointer-events: auto; opacity: 1.0; }
nav.invisible { pointer-events: none; opacity: 0.0; }

nav div.toc {
  position: relative;
  width: calc(var(--column-width) * 0.9);
  height: calc(var(--column-height) - var(--base-unit) * 2.5);
  overflow-x: hidden;
  overflow-y: auto;
  padding: var(--base-unit);
}

nav div.close-button {
  position: fixed;
  top: calc(var(--vertical-padding) + var(--base-unit) + var(--base-unit-half));
  right: calc(50vw + var(--base-unit-half) - var(--column-width) * 0.45);
  width: var(--base-unit-half);
  height: var(--base-unit-half);
  stroke: var(--ink-soft);
  stroke-width: 1px;
  cursor: pointer;
}

nav div.close-button:hover { stroke: var(--accent); }

nav div.controls {
  position: fixed;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: center;
  gap: var(--base-unit-half);
  top: calc(var(--vertical-padding) + var(--column-height) - var(--base-unit) * 2.0);
  left: calc(50vw - var(--column-width) * 0.45);
  width: calc(var(--column-width) * 0.9);
  height: calc(var(--base-unit) * 2.0);
  background-color: var(--page-bg);
  border-top: 1px solid var(--hairline);
  z-index: 100000;
}

nav div.controls button {
  flex-shrink: 0;
  cursor: pointer;
  width: var(--base-unit);
  height: auto;
  padding: 0;
  border: 1px solid var(--hairline);
  font-family: var(--body-text-font-stack);
  text-align: center;
  color: var(--ink-soft);
}

nav div.controls button:hover { border-color: var(--accent); color: var(--accent); }

nav h1 {
  width: 100%;
  margin: 0 0 var(--base-unit) 0;
  padding: 0 0 var(--base-unit-half);
  border-bottom: 1px solid var(--hairline);
  font-family: var(--title-font, var(--display-font-stack));
  font-weight: 400;
  font-size: 1.75rem;
  line-height: 1.15;
}

nav ol {
  width: 100%;
  margin: 0 0 var(--line-height) 0;
  padding: 0;
  list-style-type: none;
}

nav ol li { margin: 0 0 calc(var(--line-height) * 0.85) 0; }
nav ol li:last-of-type { margin: 0; }

/* Reused from the rest of the site: nothing at rest, then a hand-drawn
   accent-colored stroke draws in under the title on hover. */
nav ol li a {
  position: relative;
  display: inline-block;
  font-size: 1.0625rem;
  line-height: var(--line-height);
  text-decoration: none;
  color: var(--ink);
}

nav ol li a::after {
  content: "";
  position: absolute;
  left: -0.03em;
  right: -0.03em;
  bottom: -0.05em;
  height: 0.22em;
  background: var(--accent);
  -webkit-mask: url("data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20200%2020'%20preserveAspectRatio='none'%3E%3Cpath%20d='M2%2010%20C%2020%204,%2042%2013,%2072%208%20C%20102%203,%20132%2012,%20162%207%20C%20176%205,%20189%208,%20198%205%20L%20198.5%209.5%20C%20187%2013,%20174%2010.5,%20160%2013%20C%20130%2018,%20102%209.5,%2072%2013.5%20C%2042%2018.5,%2020%2011.5,%201.5%2015%20Z'/%3E%3C/svg%3E") no-repeat 0 0 / 100% 100%;
          mask: url("data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20200%2020'%20preserveAspectRatio='none'%3E%3Cpath%20d='M2%2010%20C%2020%204,%2042%2013,%2072%208%20C%20102%203,%20132%2012,%20162%207%20C%20176%205,%20189%208,%20198%205%20L%20198.5%209.5%20C%20187%2013,%20174%2010.5,%20160%2013%20C%20130%2018,%20102%209.5,%2072%2013.5%20C%2042%2018.5,%2020%2011.5,%201.5%2015%20Z'/%3E%3C/svg%3E") no-repeat 0 0 / 100% 100%;
  clip-path: inset(0 100% 0 0);
  transition: clip-path 0.3s cubic-bezier(0.3, 0.7, 0.3, 1);
  pointer-events: none;
}

nav ol li a:hover::after, nav ol li a:focus-visible::after { clip-path: inset(0 0 0 0); }

/* -- the little ribbon marking your saved place -- */

div.bookmark {
  position: absolute;
  display: none;
  top: calc(var(--vertical-padding) * -2.5);
  left: calc(var(--column-width) / 2.0);
  width: var(--base-unit);
  height: calc(var(--base-unit) * 2.0);
  transform: translateX(-50%);
  fill: var(--bookmark, var(--accent));
  z-index: 100000;
}

/* ============================================================
   the text itself
   ============================================================ */

.book h2 {
  margin: calc(var(--line-height) * 5.0) 0 calc(var(--line-height) * 2.0) 0;
  padding: 0;
  break-before: column;
  font-family: var(--display-font-stack);
  font-weight: 400;
  font-size: 2.5rem;
  line-height: 1.1;
  color: var(--ink);
}

.book a, .book a:link, .book a:visited { color: var(--accent); }
.book a:hover, .book a:active { color: var(--ink); }

.book p {
  margin: 0;
  padding: 0;
  text-indent: var(--base-unit-half);
}

.book span.nobr { white-space: nowrap; }

/* A dropped capital opens every chapter: the first paragraph after a
   chapter heading (chapter headings carry ids; the "How to Read" and title pages do not). Same treatment as the Annual Letter's opening. */
.book > h2[id] + p { text-indent: 0; }

.book > h2[id] + p::first-letter {
  float: left;
  padding-right: 0.08em;
  margin-top: 0.04em;
  font-family: var(--body-text-font-stack);
  font-weight: 600;
  font-size: 4.3em;
  line-height: 0.78;
  color: var(--ink);
}

.book p.spacetime { margin-top: var(--line-height); text-indent: 0; }

.book small {
  font-size: 0.9rem;
  font-weight: 500;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

.book img, .book picture {
  display: block;
  break-before: column;
  break-after: column;
  width: var(--column-width);
  height: var(--column-height);
  max-height: var(--column-height);
  overflow: hidden;
  object-fit: contain;
}

.book hr {
  break-after: avoid-column;
  width: 40%;
  margin: var(--base-unit) auto;
  border: none;
  border-bottom: 1px solid var(--hairline);
}

p.last-page { break-before: column; }
p.last-page::after { display: block; height: var(--column-height); content: ""; }

.book blockquote {
  break-before: avoid-column;
  margin: 0;
  padding: var(--base-unit-half) var(--base-unit);
  border-left: 2px solid var(--accent);
  color: var(--ink-soft);
}

/* -- book block layout: CSS columns, one page-width per "page" -- */

div.book {
  position: relative;
  column-gap: var(--base-unit);
  column-fill: auto;
  column-count: auto;
  column-width: var(--column-width);
  width: var(--column-width);
  height: var(--column-height);
  margin: var(--base-unit-half);
}

div.book div.title {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: var(--column-width);
  height: var(--column-height);
  padding: var(--base-unit-half);
  overflow: hidden;
  text-align: center;
  color: var(--cover-ink, var(--ink));
}

div.book div.title h1 {
  flex-grow: 0;
  height: auto;
  max-width: 100%;
  margin: 0;
  padding: 0;
  font-family: var(--title-font, var(--display-font-stack));
  font-weight: 400;
  font-size: calc(clamp(2.25rem, 5vw, 4rem) * var(--cover-scale, 1));
  line-height: 1.05;
  letter-spacing: -0.01em;
  /* no mid-word breaks: fitTitle() in the template shrinks the type instead */
  overflow-wrap: normal;
  word-break: normal;
  hyphens: manual;
  text-wrap: balance;
  color: var(--cover-ink, var(--ink));
}

/* title alignment: left / center / right (vertically it stays centered) */
div.book div.title[data-align="left"]   { align-items: flex-start; text-align: left;  padding-inline: var(--base-unit); }
div.book div.title[data-align="right"]  { align-items: flex-end;   text-align: right; padding-inline: var(--base-unit); }

/* the author runs along the bottom of the page, in the title's own font;
   left / center / right is chosen in the admin (default right) */
div.book div.title .book-reader__author {
  position: absolute;
  left: var(--base-unit);
  right: var(--base-unit);
  bottom: var(--base-unit-half);
  margin: 0;
  text-indent: 0;
  font-family: var(--title-font, var(--display-font-stack));
  font-size: 1.05rem;
  font-weight: 400;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  text-align: right;
  color: var(--cover-ink, var(--ink));
}
div.book div.title[data-author-align="left"]   .book-reader__author { text-align: left; }
div.book div.title[data-author-align="center"] .book-reader__author { text-align: center; }

/* the book's one-line description: its own plain page after the title page */
div.book div.book-note {
  break-before: column;
  break-after: column;
  width: var(--column-width);
  height: calc(var(--column-height) - 1px);
  overflow: hidden;
}

img.cover {
  break-before: column;
  break-after: column;
  width: var(--column-width);
  height: var(--column-height);
  max-height: var(--column-height);
  object-fit: contain;
}

div.book::before {
  position: fixed;
  top: 0;
  left: 0;
  width: calc((100vw - var(--column-width) - var(--base-unit)) / 2.0);
  height: 100vh;
  background: linear-gradient(to right, var(--shade-color-origin) 0%, var(--shade-color-origin) 95%, var(--shade-color-destination) 100%);
  content: "";
  z-index: 1000;
}

div.book::after {
  position: fixed;
  top: 0;
  right: 0;
  width: calc((100vw - var(--column-width) - var(--base-unit)) / 2.0);
  height: 100vh;
  background: linear-gradient(to left, var(--shade-color-origin) 0%, var(--shade-color-origin) 95%, var(--shade-color-destination) 100%);
  content: "";
  z-index: 1000;
}

/* -- responsive -- */

@media screen and (max-width: 640px) {
  :root { --column-width: calc(100vw - var(--base-unit)); }
  div.book::before, div.book::after { display: none; }
  .book-reader__home { font-size: 0.8rem; }
}

@media screen and (min-width: 640px) {
  div.book { margin-left: calc(50vw - var(--column-width) / 2.0); }
}

@media screen and (min-height: 1000px) {
  :root { --vertical-padding: calc(var(--base-unit) * 2.0); }
  div.bookmark { top: calc(var(--vertical-padding) * -1.5); }
}

@media (prefers-reduced-motion: reduce) {
  nav, nav ol li a::after { transition: none; }
}
