/* The book builder's stylesheet, for the markup builder/Pages.roc writes; the
   Safari Ride book and the books book both use it, and the widget, draft-note
   and top-bar rules apply only where a book's look writes them. The prose keeps
   a reading measure, and code, tables and
   diagrams may run wider into the right margin. A narrow window takes the chapter
   list out of the way, and a printed page drops whatever only a screen can use. */
:root {
  color-scheme: light dark;
  --paper: #fbfaf6;
  --ink: #23221f;
  --quiet: #736e64;
  --rule: #e3e0d7;
  --accent: #8a4a1c;
  --shade: #f3f1ea;
  --draft: #fbefc6;
  --draft-ink: #5e4706;
  --code-literal: #4d6b33;
  --code-type: #2f5d7c;
  --prose: 'Iowan Old Style', Charter, 'Bitstream Charter', 'Sitka Text', Cambria, Georgia, serif;
  --plain: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --mono: ui-monospace, 'SF Mono', Menlo, Consolas, 'DejaVu Sans Mono', monospace;
  --measure: 39rem;
  --wide: 52rem;
  font-size: 18px;
}

@media screen and (prefers-color-scheme: dark) {
  :root {
    --paper: #1b1a18;
    --ink: #e4e1d8;
    --quiet: #9c978b;
    --rule: #38352f;
    --accent: #dba56d;
    --shade: #26241f;
    --draft: #443612;
    --draft-ink: #f0d98f;
    --code-literal: #a9c08a;
    --code-type: #8fb8d4;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--prose);
  line-height: 1.6;
  text-rendering: optimizeLegibility;
}

a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: 0.15em; }

/* The page */

body.chapter { display: grid; grid-template-columns: 15rem minmax(0, 1fr); }
body.chapter main { padding: 3rem 3rem 4rem 4rem; }
body.front main { max-width: calc(var(--measure) + 4rem); margin: 0 auto; padding: 4rem 2rem; }

article > * { max-width: var(--measure); }
article > table { max-width: var(--wide); }
article > figure.diagram { max-width: var(--measure); }

/* A code listing matches the prose, and widens only for the lines that need it. */
article > figure.listing { width: fit-content; min-width: min(100%, var(--measure)); max-width: var(--wide); }

.book-bar { display: none; }

/* The bar across the top of every page: the way back to the site, and the way
   to the code. It spans both columns of a chapter. */

.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 0.45rem 1.5rem;
  border-bottom: 1px solid var(--rule);
  font: 0.72rem/1.4 var(--plain);
}
body.chapter > .top-bar { grid-column: 1 / -1; }
/* The same pill every app page has, from site/web/shared/home.js. */
.home-link { padding: 3px 8px; border-radius: 4px; background: rgba(24, 24, 24, 0.72); color: #fff; text-decoration: none; }
.repo-link { color: var(--quiet); text-decoration: none; }
.repo-link:hover { color: var(--accent); }

/* Headings */

h1, h2, h3 { line-height: 1.2; font-weight: 600; scroll-margin-top: 1.5rem; text-wrap: balance; }
h1 { font-size: 2.1rem; letter-spacing: -0.01em; margin: 0 0 1.5rem; }
h2 { font-size: 1.4rem; margin: 2.8rem 0 0.8rem; }
h3 { font-size: 1.1rem; margin: 2rem 0 0.6rem; }

/* A section links to itself, for a reader to copy. It keeps out of the way until
   the heading is pointed at, and a touch screen shows it faintly. */
.section-link { margin-left: 0.2em; color: var(--quiet); font-size: 0.75em; font-weight: 400; text-decoration: none; opacity: 0; }
h2:hover .section-link, .section-link:focus { opacity: 1; }
.section-link:hover { color: var(--accent); }
@media (hover: none) { .section-link { opacity: 0.5; } }

/* A chapter named in the prose is a cross-reference, so it reads as text with a
   faint dotted line under it until it is pointed at. */
.chapter-link { color: inherit; text-decoration: underline dotted var(--quiet); text-decoration-thickness: 1px; }
.chapter-link:hover, .chapter-link:focus { color: var(--accent); text-decoration-color: currentColor; }

.chapter-number {
  margin: 0 0 0.3rem;
  color: var(--quiet);
  font: 0.75rem/1.4 var(--plain);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

p { margin: 0 0 1rem; }
ul, ol { padding-left: 1.4rem; }
li { margin: 0.25rem 0; }

/* Code */

code { font-family: var(--mono); font-size: 0.82em; }
:not(pre) > code { background: var(--shade); border-radius: 2px; padding: 0.05em 0.12em; }

/* A line too long for the column wraps, so no block scrolls and none hides its end. */

pre {
  margin: 0 0 1.6rem;
  padding: 0.75rem 1rem;
  background: var(--shade);
  border-radius: 4px;
  font-size: 0.78rem;
  line-height: 1.5;
  tab-size: 4;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
pre code { font-size: inherit; }

/* copy.js pins a Copy button to each code block's top right corner, the essay
   surface's button in the book's colors. The pin has no height, so the block
   below it lies as it would without one. */

.copypin { position: relative; height: 0; }
.copybtn {
  position: absolute;
  top: 0.35rem;
  right: 0.35rem;
  padding: 0.15rem 0.45rem;
  border: 1px solid var(--rule);
  border-radius: 4px;
  background: var(--paper);
  color: var(--quiet);
  font: 600 0.72rem/1.2 var(--plain);
  cursor: pointer;
  opacity: 0.7;
}
.copybtn:hover, .copybtn:focus-visible { opacity: 1; color: var(--accent); border-color: var(--accent); }

/* roc-syntax-color marks the code's runs. A few quiet colors are enough to read by. */

.syn-comment { color: var(--quiet); }
.syn-string, .syn-number { color: var(--code-literal); }
.syn-keyword { color: var(--accent); }
.syn-type { color: var(--code-type); }
.syn-name { font-weight: 600; }

/* A listing's title sits inside the block it names, small and quiet. */

figure { margin: 1.6rem 0; }
figure.listing { background: var(--shade); border-radius: 4px; }
figure.listing figcaption { padding: 0.5rem 1rem 0; color: var(--quiet); font: 0.66rem/1.4 var(--mono); }
figure.listing pre { margin: 0; padding: 0.3rem 1rem 0.75rem; background: none; }

/* A figure's caption reads as a caption: under it, in the plain face. */

figure.widget figcaption, figure.diagram figcaption { margin-top: 0.5rem; color: var(--quiet); font: 0.76rem/1.45 var(--plain); }
figcaption code { background: none; padding: 0; }

/* Diagrams come from Graphviz. Its colours are attributes, so these rules win, and
   a diagram takes the book's type and follows the page into dark mode. A dashed box
   stands for a family and a bold one is the diagram's subject. */

svg.diagram { display: block; max-width: 100%; height: auto; }
svg.diagram-phone { display: none; }
svg.diagram text { fill: var(--ink); font-family: var(--plain); }
svg.diagram .graph > polygon { fill: none; stroke: none; }
svg.diagram .cluster polygon { fill: none; stroke: var(--rule); }
svg.diagram .cluster text { fill: var(--quiet); font-style: italic; }
svg.diagram .node polygon, svg.diagram .node ellipse { fill: var(--shade); stroke: var(--quiet); }
svg.diagram .node polyline { stroke: var(--quiet); }
svg.diagram .node [stroke-width] { stroke: var(--accent); }
svg.diagram .node [stroke-dasharray] { fill: none; }
svg.diagram .edge path { stroke: var(--quiet); }
svg.diagram .edge polygon { fill: var(--quiet); stroke: var(--quiet); }
svg.diagram .edge text { fill: var(--quiet); }

/* Widgets */

iframe.canvas-app { display: block; width: 100%; border: 0; border-radius: 4px; background: var(--shade); }

/* Tables */

table {
  margin: 0 0 1.6rem;
  border-collapse: collapse;
  font: 0.8rem/1.45 var(--plain);
  font-variant-numeric: tabular-nums;
}
th { text-align: left; border-bottom: 1px solid var(--quiet); }
td { border-bottom: 1px solid var(--rule); }
th, td { padding: 0.35rem 1rem 0.35rem 0; vertical-align: top; }
th:last-child, td:last-child { padding-right: 0; }
/* book.js marks a column of sentences as prose, and it wraps at a reading
   width, about sixty characters a line. */
th.prose, td.prose { width: 30em; }

/* Draft notes stay in sight, and they look like notes rather than the book. */

.draft-note { background: var(--draft); color: var(--draft-ink); font: 0.74rem/1.5 var(--plain); }
span.draft-note { padding: 0.1em 0.3em; border-radius: 3px; -webkit-box-decoration-break: clone; box-decoration-break: clone; }
p.draft-note { padding: 0.5rem 0.8rem; border-radius: 3px; }
.draft-note code { background: none; padding: 0; }
.draft-note::before { content: 'Draft note: '; font-weight: 600; }

/* The chapters beside the page */

.book-nav {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  padding: 3rem 1rem 2rem 1.5rem;
  border-right: 1px solid var(--rule);
  font: 0.74rem/1.35 var(--plain);
}
.book-nav a { display: block; padding: 0.22rem 0; color: var(--quiet); text-decoration: none; }
.book-nav a:hover { color: var(--accent); }
.book-nav .book-name { margin-bottom: 1rem; color: var(--ink); font: 600 1rem/1.3 var(--prose); }
.book-nav ol { list-style: none; margin: 0; padding: 0; }
.book-nav li { margin: 0; }
.book-nav .here > a { color: var(--ink); font-weight: 600; }
.book-nav .sections { margin: 0.1rem 0 0.5rem; padding-left: 1.7em; }
.number { display: inline-block; width: 1.7em; color: var(--quiet); font-variant-numeric: tabular-nums; font-weight: 400; text-indent: 0; }
a:has(> .number) { padding-left: 1.7em; text-indent: -1.7em; }

.book-nav li.part { margin-top: 0.9rem; color: var(--accent); font-weight: 700; }
.book-nav li.category { margin-top: 0.4rem; color: var(--quiet); font-style: italic; }

/* The table of contents */

ol.contents { max-width: 34rem; margin: 0 auto; padding: 0; list-style: none; font: 0.9rem/1.45 var(--plain); }
ol.contents > li { margin: 0 0 1rem; padding-left: 1.6rem; }
ol.contents > li.part { margin: 2rem 0 0.8rem; padding: 0 0 0.3rem; border-bottom: 1px solid var(--rule); color: var(--accent); font-size: 1.15rem; font-weight: 700; }
ol.contents > li.category { margin: 1.2rem 0 0.6rem; padding-left: 0.8rem; color: var(--quiet); font-size: 0.95rem; font-style: italic; }
ol.contents > li > a { display: block; color: var(--ink); font-weight: 600; text-decoration: none; }
ol.contents a:hover { color: var(--accent); }
ol.contents .sections { margin: 0.15rem 0 0; padding-left: 2.2em; list-style: none; }
ol.contents .sections li { margin: 0.1rem 0; }
ol.contents .sections a { color: var(--quiet); text-decoration: none; }

/* Previous and next */

.turn-page {
  display: flex;
  justify-content: space-between;
  gap: 2rem;
  max-width: var(--measure);
  margin-top: 3.5rem;
  padding-top: 1rem;
  border-top: 1px solid var(--rule);
  font: 0.85rem/1.3 var(--plain);
}
.turn-page a { text-decoration: none; }
.turn-page a[rel='next'] { text-align: right; }
.turn-page small { display: block; color: var(--quiet); font-size: 0.72rem; }

/* A narrow window puts the chapter first. A bar at the top leads to the book's
   front and down to the chapter list, which follows the page. */

@media (max-width: 60rem) {
  body.chapter { display: flex; flex-direction: column; }
  body.chapter main { padding: 1rem 1rem 2rem; }
  body.front main { padding: 2rem 1rem; }
  .top-bar { padding-inline: 1rem; }
  .book-nav { order: 2; position: static; height: auto; padding: 1rem; border-right: 0; border-top: 1px solid var(--rule); }
  .book-nav a { padding-block: 0.35rem; }
  .book-bar {
    display: flex;
    justify-content: space-between;
    max-width: none;
    margin: 0 0 1.5rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--rule);
    font: 0.8rem/1.4 var(--plain);
  }
  .book-bar a { text-decoration: none; }
}

/* A phone: the diagrams drawn for it, a widget as wide as the window, and code and
   tables that wrap, so nothing on the page scrolls sideways. */

@media (max-width: 45rem) {
  :root { font-size: 17px; }
  svg.diagram-phone { display: block; }
  svg.diagram-wide { display: none; }
  figure.widget { margin-inline: -1rem; }
  figure.widget figcaption { padding-inline: 1rem; }
  iframe.canvas-app { border-radius: 0; }
  figure.listing { margin-inline: -0.5rem; }
  figure.listing figcaption { padding-inline: 0.6rem; }
  figure.listing pre { padding-inline: 0.6rem; font-size: 0.72rem; tab-size: 2; }
  article > table { width: 100%; font-size: 0.76rem; }
  th, td { padding-right: 0.6rem; }
  th.prose, td.prose { width: auto; }
}

/* A printed book: black on white, with no navigation and nothing split that can
   stay whole. */

@media print {
  :root { font-size: 11pt; --paper: #fff; --ink: #000; --quiet: #444; --accent: #000; }
  body.chapter { display: block; }
  body.chapter main, body.front main { max-width: none; padding: 0; }
  .book-nav, .book-bar, .turn-page, .section-link, .copypin { display: none; }
  article > *, article > table, article > figure.diagram, article > figure.listing { max-width: 100%; }
  h1, h2, h3 { break-after: avoid; }
  figure, tr { break-inside: avoid; }
  a { text-decoration: none; }
}
