/*
 * Critterature
 *
 * The page is the whole design. Paper is a cool proof-sheet white, ink is
 * blue-black, and the companion is made of the same ink as the words. The
 * one colour, proofreader's blue pencil, is kept for the controls.
 *
 * --paper must be a flat colour. The companion hides a dot he has taken by
 * painting over it in exactly this colour.
 */

@font-face {
  font-family: "Literata";
  src: url("../fonts/literata-latin-400-normal.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: block;
}
@font-face {
  font-family: "Literata";
  src: url("../fonts/literata-latin-400-italic.woff2") format("woff2");
  font-weight: 400;
  font-style: italic;
  font-display: block;
}

:root {
  --paper: #f3f5f2;
  --ink: #1b1e26;
  --pencil: #2450c4;
  --quiet: #676d7a;
  --rule: #d5d9d4;
  --panel: #fbfcfa;

  /* Set from JavaScript; these are only fallbacks. */
  --fs: 20px;
  --lh: 36px;

  --serif: "Literata", "Iowan Old Style", Georgia, serif;
  color-scheme: light dark;
}

/* The reader can choose the paper. With no choice made, it follows the device. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --paper: #14161a;
    --ink: #d6d9df;
    --pencil: #8eabff;
    --quiet: #8b909b;
    --rule: #2b2f36;
    --panel: #1b1e23;
  }
}
:root[data-theme="dark"] {
  --paper: #14161a;
  --ink: #d6d9df;
  --pencil: #8eabff;
  --quiet: #8b909b;
  --rule: #2b2f36;
  --panel: #1b1e23;
  color-scheme: dark;
}
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="sepia"] {
  --paper: #efe5cf;
  --ink: #33291b;
  --pencil: #8c4a1e;
  --quiet: #7b6b55;
  --rule: #d8cbae;
  --panel: #f6eedc;
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--serif);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  overscroll-behavior: none;
  overflow: hidden;            /* the page never scrolls; only the menu's lists do */
}

/*
 * What he says: a small bubble over the text, pointing down at him (or up, at the top of a page).
 * It is the page turned inside out: the colour of the ink, with the words and the rim in the
 * colour of the paper. The pointer is rounded where it leaves the bubble and at its tip.
 */
#say {
  position: absolute; z-index: 3; box-sizing: border-box; max-width: min(300px, calc(100% - 24px));
  padding: .75em 1.05em; border-radius: 20px;
  background: var(--ink); color: var(--paper);
  box-shadow: 0 0 0 2px var(--paper), 0 6px 20px rgba(0, 0, 0, .2);
  font-size: clamp(13px, calc(var(--fs, 20px) * .68), 17px); line-height: 1.38; text-align: left;
  opacity: 0; transition: opacity .16s ease; pointer-events: none;
}
#say.up { opacity: 1; }
#say p { margin: 0; text-indent: 0; }
#say b { font-weight: 600; }
#say i { opacity: .7; }
#say ol { margin: 0; padding: 0; list-style: none; }
#say ol.two { padding-left: 1.2em; list-style: decimal; }
#say-tip { position: absolute; left: var(--tip, 50%); width: 44px; height: 18px; margin-left: -22px; overflow: visible; }
#say-tip .body { fill: var(--ink); }
#say-tip .rim { fill: none; stroke: var(--paper); stroke-width: 2; stroke-linecap: butt; }
#say.over #say-tip { top: calc(100% + 1px); }
#say.under #say-tip { bottom: calc(100% + 1px); transform: rotate(180deg); }
@media (prefers-reduced-motion: reduce) { #say { transition: none; } }

/* The page at the old address, shown for a moment on the way to the new one. */
body.moved { display: grid; place-items: center; padding: 24px; text-align: center; }
body.moved h1 { margin: 0 0 10px; font-size: 26px; font-weight: 400; line-height: 1.25; }
body.moved p { margin: 0; color: var(--quiet); font-size: 17px; line-height: 1.5; }
body.moved a { color: inherit; text-underline-offset: 3px; }

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
button:focus-visible, #page:focus-visible, input:focus-visible {
  outline: 2px solid var(--pencil);
  outline-offset: 3px;
}
[hidden] { display: none !important; }

/* ---------- frame ---------- */

#app {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
}

#bar, #foot {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
  /* Lined up with the text column (--col is set from JavaScript), never closer
     to the edge than 16px or the device's safe area. */
  padding: 0 max(16px, env(safe-area-inset-right), calc((100% - var(--col, 100%)) / 2))
           0 max(16px, env(safe-area-inset-left), calc((100% - var(--col, 100%)) / 2));
  font-size: 15px;
  line-height: 1;
  color: var(--quiet);
}
#bar { height: 46px; padding-top: env(safe-area-inset-top); box-sizing: content-box; }
#foot { height: 48px; padding-bottom: env(safe-area-inset-bottom); box-sizing: content-box; }

#open-menu { justify-self: start; padding: 10px 0; }
.on-shelf #open-menu, .on-shelf #foot > * { visibility: hidden; }
#open-menu:hover, #close-menu:hover, .quiet:hover { color: var(--ink); }

#running-head, #folio { margin: 0; line-height: 1.5; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#running-head { font-style: italic; }
#folio { font-variant-numeric: tabular-nums; }

#prev, #next { font-size: 30px; line-height: 1; width: 56px; height: 44px; color: var(--quiet); }
#prev { justify-self: start; text-align: left; }
#next { justify-self: end; text-align: right; }
#prev:hover, #next:hover { color: var(--ink); }
#prev:disabled, #next:disabled { opacity: .25; cursor: default; }

/* ---------- the page ---------- */

#stage { position: relative; overflow: hidden; }

#page {
  position: absolute;
  inset: 0;
  overflow: hidden;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  cursor: default;
}

#ink { position: absolute; left: 0; top: 0; pointer-events: none; }

/*
 * One chapter is laid out as a row of columns, one column per page, and the
 * row is slid sideways to turn the page. Left, top, width, height and the
 * column gap are set from JavaScript so every page holds whole lines.
 */
#flow {
  position: absolute;
  column-fill: auto;
  font-family: var(--serif);
  font-size: var(--fs);
  line-height: var(--lh);
  text-align: left;
  hyphens: manual;
  -webkit-hyphens: manual;
  /* The terrain map knows single letters, so letters must not be fused. */
  font-variant-ligatures: none;
  font-feature-settings: "liga" 0, "clig" 0, "calt" 0;
  font-kerning: normal;
  font-synthesis: none;
  orphans: 2;
  widows: 2;
}

#flow p { margin: 0; text-indent: 1.4em; }
#flow .chapter-head + p,
#flow blockquote p,
#flow td p { text-indent: 0; }

#flow .chapter-head {
  break-inside: avoid;
  overflow: hidden;
  text-align: center;
}
#flow .chapter-head .numeral {
  margin: 0;
  text-indent: 0;
  color: var(--quiet);
  letter-spacing: .08em;
}
#flow .chapter-head h1 {
  margin: 0;
  font-size: 1.45em;
  font-weight: 400;
  line-height: calc(var(--lh) * 1.5);
  text-wrap: balance;
}

#flow blockquote { margin: var(--lh) 0 var(--lh) 1.5em; }
#flow blockquote.epigraph { font-style: italic; }
#flow cite { display: block; text-align: right; font-style: italic; }
#flow blockquote.epigraph cite { font-style: normal; }
#flow hr { border: 0; margin: 0; height: var(--lh); }
#flow ol, #flow ul { margin: var(--lh) 0; padding-left: 1.5em; }
#flow table { margin: var(--lh) 0; border-collapse: collapse; }
#flow td { padding: 0 1.2em 0 0; vertical-align: top; }
#flow p.sub { text-indent: 0; text-align: center; margin: var(--lh) 0 0; color: var(--quiet); letter-spacing: .08em; }
#flow p.sub + p { text-indent: 0; }
#flow b, #flow strong { font-weight: 400; font-variant-caps: all-small-caps; letter-spacing: .05em; }

/* ---------- the shelf ---------- */

#shelf {
  position: absolute;
  inset: 0;
  background: var(--paper);
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 18px max(16px, calc((100% - 34rem) / 2)) 28px;
}
#shelf h1 { margin: 6px 0 2px; font-size: 30px; font-weight: 400; line-height: 1.2; }
#shelf .lede { margin: 0 0 14px; color: var(--quiet); font-style: italic; font-size: 17px; line-height: 1.5; }
#books { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--rule); }
#books li { border-bottom: 1px solid var(--rule); }
#books button {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 2px 16px;
  width: 100%;
  padding: 13px 4px 14px;
  text-align: left;
  line-height: 1.4;
}
#books button:hover .book-title { color: var(--pencil); }
.book-title { font-size: 20px; }
.book-place { align-self: baseline; color: var(--quiet); font-size: 14px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.book-author { grid-column: 1 / -1; color: var(--quiet); font-style: italic; font-size: 15px; }
.book-about { grid-column: 1 / -1; color: var(--quiet); font-size: 15px; }
.book-bar { grid-column: 1 / -1; height: 2px; margin-top: 7px; background: var(--rule); }
.book-bar i { display: block; height: 100%; background: var(--ink); }
#books .is-current .book-title { color: var(--pencil); }
#shelf-foot { margin: 16px 0 0; color: var(--quiet); font-size: 15px; line-height: 1.6; }
#shelf-foot p { margin: 0 0 6px; }
.linkish { color: var(--quiet); text-decoration: underline; text-underline-offset: 3px; font-size: 15px; padding: 6px 0; }
.linkish:hover { color: var(--ink); }
.code-form { display: flex; flex-wrap: wrap; gap: 8px; margin: 6px 0 0; }
.code-form input {
  font: inherit; font-size: 17px; letter-spacing: .08em; text-transform: uppercase;
  width: 9.5em; padding: 8px 10px; color: var(--ink); background: var(--panel);
  border: 1px solid var(--rule); border-radius: 8px;
}
.code-form button, .plain-button { padding: 8px 14px; border: 1px solid var(--rule); border-radius: 8px; font-size: 15px; }
.code-form button:hover, .plain-button:hover { border-color: var(--ink); }
#device-code { font-size: 22px; letter-spacing: .1em; font-variant-numeric: tabular-nums; }

/* ---------- end of chapter ---------- */

#doorstep {
  position: absolute;
  inset: 0;
  background: var(--paper);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 24px;
  text-align: center;
  overflow-y: auto;
}

#no-script {
  position: absolute;
  inset: 0;
  margin: 0;
  padding: 24px;
  display: grid;
  place-items: center;
  text-align: center;
  font-size: 18px;
  line-height: 1.6;
}
#heap { display: block; }
#doorstep-count {
  margin: 0;
  min-height: 1.6em;
  font-size: 19px;
  line-height: 1.5;
  opacity: 0;
  transition: opacity 0.5s ease;
}
#doorstep-count.shown { opacity: 1; }
/* Keep what was collected, or have it put back in the chapter. Shown with the count, once the little show is over. */
#doorstep-choice { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; opacity: 0; visibility: hidden; transition: opacity 0.5s ease; }
#doorstep-count.shown + #doorstep-choice { opacity: 1; visibility: visible; }
#doorstep-choice p { flex-basis: 100%; margin: 2px 0 0; max-width: 26em; margin-inline: auto; }
.primary-button { padding: 8px 16px; border: 1px solid var(--ink); border-radius: 8px; font-size: 15px; background: var(--ink); color: var(--paper); }
.primary-button:hover { opacity: .88; }
@media (prefers-reduced-motion: reduce) { #doorstep-count, #doorstep-choice { transition: none; } }

#notice {
  position: absolute;
  left: 50%;
  bottom: 10px;
  transform: translateX(-50%);
  margin: 0;
  padding: 9px 16px;
  max-width: calc(100% - 32px);
  background: var(--ink);
  color: var(--paper);
  font-size: 15px;
  line-height: 1.35;
  border-radius: 999px;
  text-align: center;
  pointer-events: none;
}
#notice.is-error { border-radius: 10px; pointer-events: auto; }

/* ---------- menu ---------- */

#scrim { position: fixed; inset: 0; background: rgb(10 12 18 / .32); }

#menu {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  width: min(24rem, 92vw);
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--rule);
  padding: max(20px, env(safe-area-inset-top)) 22px 0 max(22px, env(safe-area-inset-left));
  font-size: 16px;
  line-height: 1.45;
  overflow: hidden;
  /* What is drawn in ink on this panel, and what shows through it. A pressed card swaps them. */
  --fg: var(--ink);
  --bg: var(--panel);
}

/* The book, and how far through it the reader is. */
.menu-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
#menu-title { margin: 0; font-size: 27px; font-weight: 400; line-height: 1.15; letter-spacing: -.005em; text-wrap: balance; }
#menu-author { margin: 4px 0 0; color: var(--quiet); font-style: italic; }
#close-menu {
  flex: none; width: 38px; height: 38px; margin: -4px -8px 0 0; border-radius: 50%;
  display: grid; place-items: center; color: var(--quiet);
}
#close-menu svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }
#close-menu:hover { background: color-mix(in srgb, var(--ink) 8%, transparent); }
.menu-place { margin-top: 14px; }
#menu-where { margin: 0 0 6px; font-size: 14px; color: var(--quiet); }
.menu-bar { height: 3px; border-radius: 2px; background: var(--rule); overflow: hidden; }
.menu-bar i { display: block; height: 100%; width: 0; background: var(--ink); border-radius: 2px; }

/* Three leaves: where you are in the book, who is with you, and how the page looks. */
.tabs { display: flex; gap: 22px; margin-top: 18px; border-bottom: 1px solid var(--rule); }
.tabs button {
  padding: 8px 0 9px; margin-bottom: -1px; font-size: 16px; color: var(--quiet);
  border-bottom: 2px solid transparent;
}
.tabs button:hover { color: var(--ink); }
.tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--ink); }

.pane {
  flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain;
  margin: 0 -22px 0 calc(-1 * max(22px, env(safe-area-inset-left)));
  padding: 16px 22px 18px max(22px, env(safe-area-inset-left));
}
.pane[hidden] { display: none; }
.pane h3 { margin: 0 0 8px; font-size: 15px; font-weight: 400; font-style: italic; color: var(--quiet); }
.pane h3.spaced { margin-top: 26px; }
.pane p { margin: 0; }
.aside { color: var(--quiet); font-size: 14px; line-height: 1.4; }

/* Contents. */
#pane-contents { display: flex; flex-direction: column; padding-top: 8px; }
#pane-contents[hidden] { display: none; }
#chapters { list-style: none; margin: 0 -10px; padding: 0; flex: 1 1 auto; }
#chapters button {
  display: grid;
  grid-template-columns: 3.6em 1fr auto;
  gap: 8px;
  align-items: baseline;
  width: 100%;
  padding: 9px 10px;
  text-align: left;
  border-radius: 8px;
}
#chapters button:hover { background: color-mix(in srgb, var(--ink) 7%, transparent); }
#chapters .num { color: var(--quiet); font-size: 14px; font-variant-numeric: tabular-nums; }
#chapters .mark { color: var(--quiet); font-size: 13px; font-style: italic; }
#chapters [aria-current="true"] { background: var(--ink); color: var(--paper); }
#chapters [aria-current="true"] .num, #chapters [aria-current="true"] .mark { color: inherit; opacity: .7; }
.pane-foot { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--rule); }
.pane-foot .quiet { font-size: 14px; color: var(--quiet); }

/* Companion: the two of them, as cards. The one with you is in ink. */
.critters { display: grid; gap: 8px; }
.critters button {
  display: grid; grid-template-columns: 58px 1fr; gap: 14px; align-items: center;
  padding: 10px 14px 10px 10px; text-align: left;
  border: 1px solid var(--rule); border-radius: 14px;
}
.critters button:hover { border-color: var(--ink); }
.critters button[aria-pressed="true"] { --fg: var(--paper); --bg: var(--ink); background: var(--ink); border-color: var(--ink); color: var(--paper); }
.critters .portrait { width: 58px; height: 58px; }
.critters button[aria-pressed="false"] .portrait { opacity: .55; }
.critters .who { display: grid; gap: 1px; }
.critters .who b { font-weight: 400; font-size: 19px; line-height: 1.2; }
.critters .who span { font-size: 14px; line-height: 1.35; opacity: .72; }
#critter-about { margin: 12px 0 6px; }
#menu .fg { fill: var(--fg); }
#menu .bg { fill: var(--bg); }
#menu .line { fill: none; stroke: var(--fg); stroke-linecap: round; }
#menu .tail { stroke-width: 17; }
#menu .legs { stroke-width: 13.6; }
#menu .rleg { stroke-width: 20; }
#menu .rwhisk { stroke-width: 3; }
#menu .wbrim { stroke-width: 5.06; }

/* What the Linerabbit translates into. */
.field { display: grid; gap: 6px; padding: 9px 0 3px; }
.field[hidden] { display: none; }
.field label { font-size: 16px; }
.field select, .field input {
  font: inherit; font-size: 16px; color: var(--ink); background: var(--panel);
  border: 1px solid var(--rule); border-radius: 10px; padding: 9px 10px; width: 100%;
}
.field select:focus-visible, .field input:focus-visible { outline: 2px solid var(--pencil); outline-offset: 1px; }
#pile-case .piece svg { display: block; width: 100%; height: 100%; }
#pile-case .piece svg text { font-family: inherit; fill: var(--ink); }

/* A switch: the words, and a little track with a dot in it. */
.switch { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 9px 0; cursor: pointer; }
.switch[hidden] { display: none; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch i { flex: none; position: relative; width: 44px; height: 26px; border-radius: 13px; background: var(--rule); transition: background .15s; }
.switch i::after { content: ""; position: absolute; top: 4px; left: 4px; width: 18px; height: 18px; border-radius: 50%; background: var(--panel); transition: transform .15s; }
.switch input:checked + i { background: var(--ink); }
.switch input:checked + i::after { transform: translateX(18px); }
.switch input:focus-visible + i { outline: 2px solid var(--pencil); outline-offset: 2px; }

/* What they have given the reader, set out like specimens: the piece itself, large, and how many. */
#pile-case { display: grid; grid-template-columns: repeat(auto-fit, minmax(84px, 1fr)); gap: 8px; margin-bottom: 10px; }
#pile-case[hidden] { display: none; }
#pile-case div { border: 1px solid var(--rule); border-radius: 14px; padding: 6px 4px 12px; text-align: center; }
#pile-case .piece { display: block; height: 54px; font-size: 60px; line-height: 54px; overflow: hidden; }
#pile-case .piece.low { line-height: 14px; }
#pile-case .piece.high { line-height: 62px; }
#pile-case .piece.mid { line-height: 44px; }
#pile-case b { display: block; font-weight: 400; font-size: 24px; line-height: 1.2; font-variant-numeric: tabular-nums; }
#pile-case small { display: block; font-size: 14px; color: var(--quiet); }

/* Page. */
.stepper { display: flex; align-items: center; gap: 12px; }
.stepper button { flex: none; width: 54px; height: 44px; border: 1px solid var(--rule); border-radius: 10px; line-height: 1; }
.stepper button:hover { border-color: var(--ink); }
.stepper button:disabled { opacity: .35; cursor: default; }
.stepper .a-small { font-size: 15px; }
.stepper .a-large { font-size: 25px; }
#size-scale { flex: 1; display: flex; align-items: center; justify-content: space-between; }
#size-scale i { width: 5px; height: 5px; border-radius: 50%; background: var(--rule); }
#size-scale i.on { width: 11px; height: 11px; background: var(--ink); }

.papers { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.papers button { display: grid; justify-items: center; gap: 6px; padding: 8px 0 7px; border: 1px solid transparent; border-radius: 12px; font-size: 14px; color: var(--quiet); }
.papers button:hover { color: var(--ink); }
.papers button[aria-pressed="true"] { border-color: var(--ink); color: var(--ink); }
.swatch { width: 46px; height: 46px; border-radius: 50%; display: grid; place-items: center; font-style: normal; font-size: 17px; border: 1px solid var(--rule); }
.swatch.light { background: #f3f5f2; color: #1b1e26; }
.swatch.sepia { background: #efe5cf; color: #33291b; }
.swatch.dark { background: #14161a; color: #d6d9df; }
.swatch.auto { background: linear-gradient(115deg, #f3f5f2 50%, #14161a 50%); color: #7d828c; }
#device-idle { margin-top: 10px; }
#device-code { margin-top: 10px; }

/* The name of the place, at the foot. */
.menu-foot {
  flex: none; margin: 0 -22px 0 calc(-1 * max(22px, env(safe-area-inset-left)));
  padding: 10px 22px max(10px, env(safe-area-inset-bottom)) max(22px, env(safe-area-inset-left));
  border-top: 1px solid var(--rule);
}
.menu-foot .wordmark { display: block; width: 128px; height: 36px; opacity: .85; }
.menu-foot .wordmark text { font-family: inherit; font-size: 200px; }
#menu .w-tail { stroke-width: 14.4; }
#menu .w-legs { stroke-width: 11.5; }
.about { margin-top: 28px; padding-top: 14px; border-top: 1px solid var(--rule); display: grid; gap: 6px; }
.about p:empty { display: none; }

@media (prefers-reduced-motion: no-preference) {
  #menu { animation: menu-in .16s ease-out; }
  @keyframes menu-in { from { transform: translateX(-14px); opacity: 0; } }
}

/* ---------- a phone on its side, or any very short window ---------- */

@media (max-height: 480px) {
  #bar { height: 34px; }
  #foot { height: 36px; }
  #prev, #next { height: 36px; font-size: 26px; }
  #doorstep { gap: 4px; padding: 6px 24px; }
  #doorstep-count { font-size: 17px; }
  #shelf { padding-top: 8px; }
  #shelf h1 { font-size: 24px; margin-top: 0; }
}
