/* Reader theme ("G · Reader"). Loaded after LaTeXML.css and ltx-book.css. */

/* ---------- Tokens ---------- */
/* One paper family: light --bg is also the home's --paper (site/theme/home.css; site.py --self-test checks). */
:root {
  --bg: #f7f6f2; --text: #1c1d21; --muted: #565961; --panel: #eeece6;
  --border: #e3e0d8; --accent: #1f3f8f; --soft: #eef2ff;
  --serif: 'Newsreader', 'Hiragino Mincho ProN', 'Yu Mincho', 'Noto Serif JP', serif;
  /* Headings: Japanese in Shippori Mincho, as on the home (decisions.md). */
  --serif-head: 'Newsreader', 'Shippori Mincho', 'Hiragino Mincho ProN', 'Yu Mincho', 'Noto Serif JP', serif;
  --sans: Inter, 'Hiragino Sans', 'Yu Gothic', 'Noto Sans JP', sans-serif;
  --bar-h: 56px;
  --toc-w: 280px;
  color-scheme: light;
}
:root[data-theme="sepia"] {
  --bg: #f2eadb; --text: #2c2619; --muted: #62553f; --panel: #e9dfcb;
  --border: #dccfb7; --accent: #2a4787; --soft: #e6dac3;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg: #1f2023; --text: #dddbd5; --muted: #a19e97; --panel: #28292d;
    --border: #36373c; --accent: #9fbde6; --soft: #2a2f3a;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1f2023; --text: #dddbd5; --muted: #a19e97; --panel: #28292d;
  --border: #36373c; --accent: #9fbde6; --soft: #2a2f3a;
  color-scheme: dark;
}

/* ---------- Base ---------- */
html { scroll-padding-top: calc(var(--bar-h) + 12px); -webkit-text-size-adjust: 100%; }
@media (prefers-reduced-motion: no-preference) { html { scroll-behavior: smooth; } }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: var(--sans); line-height: 1.5;
}
a { color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

.r-skip {
  position: absolute; left: 8px; top: -100px; z-index: 100;
  padding: 10px 14px; background: var(--accent); color: var(--bg);
  border-radius: 8px; font-weight: 600; text-decoration: none;
}
.r-skip:focus { top: 8px; }

/* Chrome buttons */
.r-bar button, .r-settings button {
  font: inherit; color: inherit; background: none; cursor: pointer;
  min-width: 44px; min-height: 44px; border: 1px solid transparent; border-radius: 8px;
}
.r-bar button:hover { background: var(--panel); }

/* ---------- Top bar ---------- */
.r-bar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 4px;
  min-height: var(--bar-h); padding: 0 8px;
  background: var(--bg); border-bottom: 1px solid var(--border);
  font-family: var(--sans); font-size: 15px;
}
/* The TOC lives on the right, so its toggle sits at the right end of the bar, after aA. */
.r-toc-toggle { order: 1; display: inline-flex; align-items: center; justify-content: center; }
.r-toc-toggle svg { width: 20px; height: 20px; }
.r-titles {
  flex: 1; min-width: 0; display: flex; align-items: baseline; gap: 10px;
  white-space: nowrap; overflow: hidden;
}
.r-book {
  color: var(--text); font-weight: 600; text-decoration: none;
  overflow: hidden; text-overflow: ellipsis; flex-shrink: 1; min-width: 0;
}
.r-home { display: inline-flex; align-self: center; flex-shrink: 0; color: var(--muted); }
.r-home:hover { color: var(--text); }
.r-crumb { color: var(--muted); overflow: hidden; text-overflow: ellipsis; min-width: 0; flex-shrink: 2; }
.r-book::before, .r-crumb::before { content: "/"; margin-right: 10px; color: var(--border); }
.r-lang { display: flex; }
.r-lang a {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 44px; min-height: 44px; padding: 0 6px; border-radius: 8px;
  color: var(--muted); text-decoration: none;
}
.r-lang a:hover { background: var(--panel); }
.r-settings-toggle { font-weight: 600; }
.r-search-toggle { display: inline-flex; align-items: center; justify-content: center; }
.r-settings-toggle[aria-expanded="true"], .r-toc-toggle[aria-expanded="true"], .r-search-toggle[aria-expanded="true"] { color: var(--accent); }

.r-progress { position: absolute; left: 0; right: 0; bottom: -1px; height: 2px; pointer-events: none; }
.r-progress-bar { height: 100%; width: 0; background: var(--accent); }

/* ---------- Settings popover ---------- */
.r-settings {
  position: absolute; top: calc(100% + 6px); right: 8px; z-index: 40;
  width: min(300px, calc(100vw - 16px)); box-sizing: border-box;
  padding: 12px; display: grid; gap: 12px;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 10px 30px rgb(0 0 0 / 0.15);
}
.r-settings[hidden] { display: none; }
.r-settings fieldset { margin: 0; padding: 0; border: 0; display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.r-settings legend { padding: 0 0 6px; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.r-settings button { flex: 1; padding: 0 8px; border-color: var(--border); background: var(--panel); }
.r-settings button[aria-pressed="true"] { border-color: var(--accent); background: var(--soft); color: var(--accent); font-weight: 600; }
.r-size-value { min-width: 3.5em; text-align: center; font-variant-numeric: tabular-nums; }

/* ---------- Search panel (search.js) ---------- */
.r-search {
  position: absolute; top: calc(100% + 6px); right: 8px; z-index: 40;
  width: min(32rem, calc(100vw - 16px)); box-sizing: border-box;
  max-height: calc(100vh - var(--bar-h) - 24px); overflow-y: auto; padding: 12px;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 10px 30px rgb(0 0 0 / 0.15);
}
.r-search[hidden] { display: none; }
.r-search input {
  width: 100%; box-sizing: border-box; min-height: 44px; padding: 0 12px;
  font: inherit; font-size: 16px; color: inherit; background: var(--panel);
  border: 1px solid var(--border); border-radius: 8px;
}
.r-search-status { margin: 0; color: var(--muted); }
.r-search-status:not(:empty) { margin-top: 10px; }
.r-search-results { list-style: none; margin: 0; padding: 0; }
.r-search-results a {
  display: block; margin-top: 4px; padding: 8px 10px; border-radius: 8px;
  color: var(--text); text-decoration: none; overflow-wrap: anywhere;
}
.r-search-results a:hover, .r-search-results a:focus-visible { background: var(--panel); }
.r-search-title { display: block; font-weight: 600; }
.r-search-section { display: block; color: var(--accent); font-size: 14px; }
.r-search-excerpt { display: block; margin-top: 2px; color: var(--muted); font-size: 14px; line-height: 1.45; }
.r-search-excerpt mark { background: var(--soft); color: var(--text); font-weight: 600; }

/* ---------- Layout ---------- */
.r-layout {
  display: grid; grid-template-columns: minmax(0, 1fr) var(--toc-w);
  align-items: start;
}
:root[data-toc="closed"] .r-layout { grid-template-columns: minmax(0, 1fr); }
:root[data-toc="closed"] .r-toc { display: none; }

/* ---------- TOC ---------- */
.r-toc {
  position: sticky; top: var(--bar-h);
  max-height: calc(100vh - var(--bar-h)); overflow-y: auto; box-sizing: border-box;
  order: 2; padding: 16px 8px 32px; border-left: 1px solid var(--border);
  font-family: var(--sans); font-size: 14px; line-height: 1.35;
}
.r-toc ol { list-style: none; margin: 0; padding: 0; }
.r-toc a {
  display: block; position: relative;
  padding: 8px 10px; border-radius: 8px; color: var(--text); text-decoration: none;
}
/* Chapter titles (text plus inline math) wrap as one flow; the number hangs in the left padding.
   Entries without a number (bibliography) align with the titles. */
.r-toc-chapters > li > a { padding-left: calc(10px + 2.2em); }
.r-toc a:hover { background: var(--panel); }
.r-toc a[aria-current="page"] { background: var(--soft); color: var(--accent); font-weight: 600; }
.r-num { position: absolute; left: 10px; color: var(--muted); font-variant-numeric: tabular-nums; }
.r-toc a[aria-current="page"] .r-num { color: inherit; }
/* A \part: an unlinked heading over its chapters. */
.r-toc-part { position: relative; padding: 14px 10px 4px calc(10px + 2.2em); color: var(--muted); font-size: 12px; font-weight: 600; }
.r-toc-part .r-num { top: 14px; }
.r-toc-sections { margin: 2px 0 6px 18px !important; border-left: 1px solid var(--border); }
.r-toc-sections a { padding: 6px 10px; color: var(--muted); font-size: 13px; border-radius: 0 8px 8px 0; }
.r-toc-sections a[aria-current="location"] { color: var(--accent); box-shadow: inset 2px 0 0 var(--accent); }

/* ---------- Main column ---------- */
.r-main { min-width: 0; padding: 24px 20px 48px; }
.r-article {
  min-width: 0; max-width: 70ch; margin: 0 auto;
  font-family: var(--serif); font-size: var(--r-size, 19px); line-height: 1.7;
}
:root[data-face="sans"] .r-article { font-family: var(--sans); }
.r-article .ltx_title { font-family: var(--serif-head); font-weight: 600; line-height: 1.25; }
:root[data-face="sans"] .r-article .ltx_title { font-family: var(--sans); }
.r-article .ltx_title_chapter, .r-article .ltx_title_appendix,
.r-article .ltx_title_bibliography { font-size: 2em; margin: 0.6em 0 0.8em; }
.r-article .ltx_title_chapter .ltx_tag, .r-article .ltx_title_appendix .ltx_tag {
  color: var(--muted); font-size: 0.5em; font-weight: 500; text-transform: uppercase; letter-spacing: 0.06em;
}
.r-article .ltx_title_section { font-size: 1.35em; margin-top: 2em; }
.r-article .ltx_title_subsection { font-size: 1.12em; margin-top: 1.6em; }
.r-article .ltx_title_section .ltx_tag, .r-article .ltx_title_subsection .ltx_tag { color: var(--muted); }
.r-article .ltx_p { margin: 0.8em 0; }
/* Inline MathML cannot wrap; a formula wider than the column scrolls within its paragraph.
   Run-in titles make the first paragraph of a theorem or proof inline, so those blocks scroll too. */
.r-article .ltx_p, .r-article .ltx_theorem, .r-article .ltx_proof { overflow-x: auto; overflow-y: hidden; }
/* LaTeXML sets list items nowrap with an inline-block body; keep that body within the column. */
.r-article .ltx_item > .ltx_para { max-width: 100%; white-space: normal; overflow-x: auto; overflow-y: hidden; }
.r-article img, .r-article object, .r-article svg { max-width: 100%; }

/* Theorem-like blocks */
.r-article .ltx_theorem {
  margin: 1.4em 0; padding: 0.4em 1.1em;
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
}
/* ag-cs's boxed aside 「複素関数論を知っている読者へ」: framed like its PDF box */
.r-article .ltx_theorem_complexaside { border: 2px solid var(--muted); }
.r-article .ltx_title_theorem { font-family: inherit; font-size: 1em; color: var(--accent); }
.r-article .ltx_proof { margin: 1em 0 1.4em; }
.r-article .ltx_title_proof { font-family: inherit; font-size: 1em; color: var(--muted); }

/* Display math: wide content scrolls inside its own box */
.r-article table.ltx_eqn_table { display: block; max-width: 100%; overflow-x: auto; overflow-y: hidden; }
.r-article table.ltx_eqn_table > tbody { display: table; width: 100%; }
.r-article math[display="block"] { max-width: 100%; overflow-x: auto; overflow-y: hidden; }
/* LaTeXML's 50% pad cells leave the formula cell only its min-content width, and the browser counts
   break points inside Japanese \text (mtext) that MathML never uses: keep the cell on one line.
   The pads only centre the formula, so drop LaTeXML's 2em minimum, which pushed formulas that fit into scrolling. */
.r-article .ltx_eqn_cell { white-space: nowrap; }
.r-article .ltx_eqn_center_padleft, .r-article .ltx_eqn_center_padright { min-width: 0; }
/* LaTeXML gives the number cell max-width 0 and floats the number into the right pad. In a list item the
   paragraph is inline-block, so it shrinks to the formula and the pad has no room: the number overlapped
   the formula. There, give the number its own cell. */
.r-article .ltx_item .ltx_eqn_eqno { max-width: none; padding-left: 1em; }
.r-article .ltx_item .ltx_eqn_eqno .ltx_tag { float: none; }

/* Tables scroll inside their own box */
.r-article table.ltx_tabular {
  display: block; width: fit-content; max-width: 100%; overflow-x: auto;
  margin: 1em auto; font-size: 0.9em; line-height: 1.45;
}
.r-article .ltx_td, .r-article .ltx_th { padding: 6px 10px; }
.r-article [class*="ltx_border_"] { border-color: var(--border); }

/* Figures */
.r-article .ltx_figure, .r-article .ltx_table { margin: 1.8em 0; text-align: center; }
.r-article object.ltx_graphics { display: block; margin: 0 auto; height: auto; }
/* Several pictures in one figure stand side by side in the PDF (\hspace between them) and the captions
   say left/right: put them in a row, wrapping onto new lines when they do not fit. */
.r-article .ltx_block:has(> object.ltx_graphics + object.ltx_graphics) { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 1em 1.5em; }
.r-article .ltx_block:has(> object.ltx_graphics + object.ltx_graphics) > object.ltx_graphics { margin: 0; }
.r-article .ltx_caption {
  margin-top: 0.8em; font-family: var(--sans); font-size: 0.8em; line-height: 1.5;
  color: var(--muted); text-align: left;
}
/* Figures are SVG with hard-coded black ink: keep them on a light card in dark mode. */
:root[data-theme="dark"] .r-article object.ltx_graphics { background: #e6e4de; padding: 12px; border-radius: 12px; box-sizing: border-box; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) .r-article object.ltx_graphics { background: #e6e4de; padding: 12px; border-radius: 12px; box-sizing: border-box; }
}

/* References, notes, bibliography */
.r-article a.ltx_ref { text-decoration: none; }
.r-article a.ltx_ref:hover { text-decoration: underline; }
.r-article .ltx_note_content { background: var(--bg); color: var(--text); border-color: var(--border); box-shadow: none; }
.r-article .ltx_note_mark { color: var(--accent); }
.r-article .ltx_biblist { padding-left: 2.5em; }
.r-article .ltx_bibitem { margin: 0.8em 0; }
.r-article .ltx_bibblock { overflow-wrap: anywhere; }
.r-article .ltx_TOC { font-family: var(--sans); font-size: 0.9em; }

/* Reference preview (preview.js): the target's block in a popover; .r-article gives it the text styles. */
.r-preview {
  position: absolute; z-index: 35; box-sizing: border-box;
  width: min(36rem, calc(100vw - 16px)); padding: 4px 16px 10px;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 10px 30px rgb(0 0 0 / 0.15);
}
.r-preview[hidden] { display: none; }
.r-preview .r-preview-body {
  max-width: none; max-height: min(50vh, 24rem); overflow-y: auto;
  font-size: calc(0.85 * var(--r-size, 19px));
}
.r-preview .ltx_theorem, .r-preview .ltx_figure, .r-preview .ltx_table, .r-preview .ltx_bibitem { margin: 0.6em 0; }
.r-preview-go {
  display: inline-flex; align-items: center; min-height: 44px;
  font-family: var(--sans); font-size: 15px; font-weight: 600; text-decoration: none;
}
.r-preview-go[hidden] { display: none; }

/* ---------- Pager & footer ---------- */
.r-pager {
  max-width: 70ch; margin: 48px auto 0; display: flex; gap: 12px;
  font-family: var(--sans); font-size: 15px;
}
.r-pager a {
  flex: 1; display: flex; flex-direction: column; gap: 2px; min-height: 44px;
  padding: 12px 16px; border: 1px solid var(--border); border-radius: 12px;
  color: var(--text); text-decoration: none;
}
.r-pager a:hover { border-color: var(--accent); }
.r-pager span { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.r-next { text-align: right; margin-left: auto; }
.r-footer {
  padding: 20px; border-top: 1px solid var(--border);
  font-family: var(--sans); font-size: 13px; color: var(--muted); text-align: center;
}

/* Plain text of a heading's math for Pagefind, which skips <math> (site.py heading_ids) */
.mf-search-text { display: none; }

/* ---------- First reading: a folded ★ region keeps its title and a show button ---------- */
.mf-show { display: none; }
[data-first="on"] .mf-optional:not(.mf-open) > :not(.ltx_title):not(.mf-show) { display: none; }
[data-first="on"] .mf-optional:not(.mf-open) > .mf-show {
  display: inline-block; margin: 0.3em 0 0.6em; padding: 2px 10px; font: inherit; font-size: 0.85em;
  color: var(--accent); background: var(--soft); border: 1px solid var(--border); border-radius: 6px; cursor: pointer;
}

/* ---------- No JS ---------- */
.no-js .r-settings-toggle, .no-js .r-toc-toggle, .no-js .r-search-toggle { display: none; }

/* ---------- Phones: TOC drawer ---------- */
.r-scrim { display: none; }
@media (max-width: 760px) {
  .r-layout, :root[data-toc="closed"] .r-layout { grid-template-columns: minmax(0, 1fr); }
  .r-crumb { display: none; }
  .r-main { padding: 16px 16px 40px; }

  /* Without JS the TOC is an ordinary list above the content. */
  .no-js .r-toc { order: 0; position: static; max-height: none; border-left: 0; border-bottom: 1px solid var(--border); }

  .js .r-toc, :root.js[data-toc="closed"] .r-toc {
    display: block; position: fixed; top: 0; bottom: 0; right: 0; z-index: 50;
    width: min(320px, 85vw); max-height: none;
    background: var(--bg); box-shadow: 0 0 30px rgb(0 0 0 / 0.2);
    transform: translateX(100%); visibility: hidden;
  }
  :root[data-drawer="open"] .r-toc { transform: none; visibility: visible; }
  .js .r-scrim { position: fixed; inset: 0; z-index: 45; background: rgb(0 0 0 / 0.4); }
  :root[data-drawer="open"] .r-scrim { display: block; }
  :root[data-drawer="open"] body { overflow: hidden; }
}
@media (max-width: 760px) and (prefers-reduced-motion: no-preference) {
  /* Visibility flips at once on opening (so focus can move in) and after the slide on closing. */
  .js .r-toc { transition: transform 0.2s ease, visibility 0s linear 0.2s; }
  :root[data-drawer="open"] .r-toc { transition: transform 0.2s ease; }
}

/* ---------- "Try it" widgets (site/theme/widgets.js; hidden without JS) ---------- */
.no-js .mf-widget { display: none; }
.r-article .mf-widget {
  margin: -0.8em 0 1.8em; border: 1px solid var(--border); border-radius: 8px;
  background: var(--panel); font-family: var(--sans); font-size: 0.9em;
}
.r-article .ltx_para + .mf-widget { margin-top: 0.8em; } /* a paragraph has no bottom margin to overlap */
.mf-widget > summary { cursor: pointer; padding: 10px 14px; font-weight: 600; color: var(--accent); }
.mf-widget-body { padding: 0 14px 14px; }
.mf-widget-body svg { display: block; width: 100%; max-width: 420px; height: auto; margin: 0 auto; background: var(--bg); border-radius: 6px; }
.mf-widget-body label { display: flex; align-items: center; gap: 10px; margin: 10px 0 6px; }
.mf-widget-body input[type=range] { flex: 1; min-width: 0; accent-color: var(--accent); }
.mf-widget-body output { font-variant-numeric: tabular-nums; min-width: 3.2em; text-align: end; }
.mf-widget-body p { margin: 0.4em 0 0; }
