/* 中学数学からのディープラーニング — 共通スタイル
   Layout: 方眼ノートの紙面。左に「しおり」の目次、中央に本文（1行およそ40字）。
   青ペン＝要点・主役のグラフ、赤ペン＝注意・2本目のグラフ、蛍光ペン＝大事なことば。 */
:root {
  --paper: #f8fafc;
  --sheet: #ffffff;
  --panel: #eef3f8;
  --rule: #d6dfe9;
  --grid: #e6edf4;
  --ink: #1b2430;
  --ink-2: #4a5768;
  --ink-3: #6f7c8d;
  --blue: #1f5fcc;
  --blue-soft: #e4edfb;
  --red: #c9372c;
  --red-soft: #fbe9e6;
  --green: #187f55;
  --green-soft: #e1f2e9;
  --orange: #c7660f;
  --orange-soft: #fbeedd;
  --purple: #7046b8;
  --purple-soft: #efe8fa;
  --marker: #fff06b;
  --code-bg: #f1f4f8;
  --shadow: 0 1px 2px rgba(27, 36, 48, .06), 0 4px 16px rgba(27, 36, 48, .06);

  --c1: var(--blue);
  --c2: var(--red);
  --c3: var(--green);
  --c4: var(--orange);
  --c5: var(--purple);

  --font-display: "Zen Maru Gothic", "Hiragino Maru Gothic ProN", "BIZ UDPGothic", "Hiragino Sans", sans-serif;
  --font-body: "BIZ UDPGothic", "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic", "Meiryo", sans-serif;
  --font-mono: ui-monospace, "SFMono-Regular", Menlo, Consolas, "BIZ UDGothic", monospace;

  --measure: 44rem;
  --sidebar: 17rem;
  --topbar: 3.25rem;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #11161d;
    --sheet: #161c25;
    --panel: #1b2330;
    --rule: #2c3746;
    --grid: #212a36;
    --ink: #e5eaf0;
    --ink-2: #aab5c3;
    --ink-3: #8592a3;
    --blue: #74a6ff;
    --blue-soft: #1a2944;
    --red: #ff8278;
    --red-soft: #3a2222;
    --green: #55c993;
    --green-soft: #15301f;
    --orange: #f2a052;
    --orange-soft: #3a2a16;
    --purple: #b597ff;
    --purple-soft: #2a2140;
    --marker: #7a6a10;
    --code-bg: #1a212b;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #11161d;
  --sheet: #161c25;
  --panel: #1b2330;
  --rule: #2c3746;
  --grid: #212a36;
  --ink: #e5eaf0;
  --ink-2: #aab5c3;
  --ink-3: #8592a3;
  --blue: #74a6ff;
  --blue-soft: #1a2944;
  --red: #ff8278;
  --red-soft: #3a2222;
  --green: #55c993;
  --green-soft: #15301f;
  --orange: #f2a052;
  --orange-soft: #3a2a16;
  --purple: #b597ff;
  --purple-soft: #2a2140;
  --marker: #7a6a10;
  --code-bg: #1a212b;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
  color-scheme: dark;
}

/* ---------- 基本 ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 17px; -webkit-text-size-adjust: 100%; scroll-padding-top: calc(var(--topbar) + 1rem); }
@media (max-width: 560px) { html { font-size: 16px; } }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-body);
  line-height: 1.9;
  font-feature-settings: "palt" 0;
  overflow-wrap: anywhere;
}
a { color: var(--blue); text-underline-offset: .2em; }
a:hover { text-decoration-thickness: 2px; }
:focus-visible { outline: 3px solid var(--blue); outline-offset: 2px; border-radius: 4px; }
img, svg, canvas { max-width: 100%; }
strong { font-weight: 700; color: var(--ink); }
mark { background: linear-gradient(transparent 52%, var(--marker) 52%); color: inherit; padding: 0 .1em; }
small { font-size: .85em; color: var(--ink-2); }
ruby { ruby-position: over; }
ruby rt { font-size: .5em; font-weight: 400; color: var(--ink-2); letter-spacing: 0; line-height: 1; user-select: none; }
hr { border: 0; border-top: 1px dashed var(--rule); margin: 2.5rem 0; }
kbd { font-family: var(--font-mono); font-size: .85em; border: 1px solid var(--rule); border-bottom-width: 2px; border-radius: 4px; padding: 0 .35em; background: var(--sheet); }
.nowrap { white-space: nowrap; }
.muted { color: var(--ink-2); }
.center { text-align: center; }
.c1 { color: var(--c1); } .c2 { color: var(--c2); } .c3 { color: var(--c3); } .c4 { color: var(--c4); } .c5 { color: var(--c5); }

/* ---------- 上のバー ---------- */
.topbar {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 30;
  height: var(--topbar);
  display: flex; align-items: center; gap: .75rem;
  padding: 0 max(16px, env(safe-area-inset-right, 0px)) 0 max(16px, env(safe-area-inset-left, 0px));
  background: color-mix(in srgb, var(--paper) 88%, transparent);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--rule);
}
.topbar .brand {
  font-family: var(--font-display); font-weight: 700; font-size: 1rem; letter-spacing: .02em;
  color: var(--ink); text-decoration: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0;
}
.topbar .brand b { color: var(--blue); }
.topbar .spacer { flex: 1; }
.tb-btn {
  font: inherit; font-size: .82rem; color: var(--ink-2); background: var(--sheet);
  border: 1px solid var(--rule); border-radius: 999px; padding: .2rem .8rem; cursor: pointer; white-space: nowrap;
  line-height: 1.6;
}
.tb-btn:hover { color: var(--ink); border-color: var(--ink-3); }
.menu-btn { display: none; }

/* ---------- 全体の配置 ---------- */
.layout { display: grid; grid-template-columns: var(--sidebar) minmax(0, 1fr); min-height: calc(100vh - var(--topbar)); }
.sidenav {
  position: sticky; top: calc(var(--topbar) + env(safe-area-inset-top, 0px)); align-self: start;
  height: calc(100vh - var(--topbar) - env(safe-area-inset-top, 0px)); overflow-y: auto;
  border-right: 1px solid var(--rule); padding: 1rem .75rem 3rem 1rem; font-size: .84rem; line-height: 1.55;
}
.sidenav .part { font-family: var(--font-display); font-weight: 700; font-size: .72rem; letter-spacing: .12em; color: var(--ink-3); margin: 1.1rem 0 .3rem .25rem; }
.sidenav .part:first-child { margin-top: 0; }
.sidenav ul { list-style: none; margin: 0; padding: 0; }
.sidenav .ch > a { display: flex; gap: .5rem; padding: .3rem .4rem; border-radius: 6px; color: var(--ink); text-decoration: none; }
.sidenav .ch > a:hover { background: var(--panel); }
.sidenav .ch > a .n { flex: 0 0 1.6rem; font-variant-numeric: tabular-nums; color: var(--ink-3); font-weight: 700; }
.sidenav .ch.current > a { background: var(--blue-soft); color: var(--ink); font-weight: 700; }
.sidenav .ch.current > a .n { color: var(--blue); }
.sidenav .pages { margin: .2rem 0 .5rem 1.15rem; border-left: 1px solid var(--rule); }
.sidenav .pages a { display: flex; gap: .45rem; padding: .22rem .5rem .22rem .7rem; color: var(--ink-2); text-decoration: none; border-radius: 0 6px 6px 0; }
.sidenav .pages a:hover { color: var(--ink); background: var(--panel); }
.sidenav .pages a.here { color: var(--blue); font-weight: 700; box-shadow: inset 2px 0 0 var(--blue); }
.sidenav .pages a .pn { flex: 0 0 auto; font-variant-numeric: tabular-nums; color: var(--ink-3); }
.sidenav .pages a.read .pn::after { content: " ✓"; color: var(--green); }
.sidenav .home { display: block; margin-bottom: .5rem; padding: .3rem .4rem; color: var(--ink); text-decoration: none; font-weight: 700; border-radius: 6px; }
.sidenav .home:hover { background: var(--panel); }

@media (max-width: 980px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .menu-btn { display: inline-block; }
  .sidenav {
    position: fixed; z-index: 40; top: calc(var(--topbar) + env(safe-area-inset-top, 0px)); left: 0; bottom: 0; width: min(20rem, 88vw); height: auto;
    background: var(--sheet); box-shadow: var(--shadow); transform: translateX(-105%); transition: transform .2s ease;
  }
  body.nav-open .sidenav { transform: none; }
  .scrim { position: fixed; inset: 0; z-index: 35; background: rgba(0, 0, 0, .3); display: none; }
  body.nav-open .scrim { display: block; }
}

/* ---------- 本文 ---------- */
main#content {
  width: 100%; max-width: calc(var(--measure) + 32px); margin: 0 auto;
  padding: 2rem 16px 5rem;
  min-width: 0;
}
.eyebrow { font-size: .8rem; color: var(--ink-3); letter-spacing: .06em; margin: 0 0 .4rem; }
.eyebrow a { color: inherit; text-decoration: none; }
.eyebrow a:hover { color: var(--blue); }
.eyebrow .num { display: inline-block; font-family: var(--font-display); font-weight: 700; color: var(--blue); margin-right: .4em; }
h1, h2, h3, h4 { font-family: var(--font-display); font-weight: 700; line-height: 1.45; text-wrap: balance; color: var(--ink); }
h1 { font-size: 2rem; margin: 0 0 1rem; letter-spacing: .01em; }
h2 { font-size: 1.42rem; margin: 3rem 0 1rem; padding-bottom: .35rem; border-bottom: 2px dashed var(--rule); }
h3 { font-size: 1.14rem; margin: 2rem 0 .6rem; }
h4 { font-size: 1rem; margin: 1.5rem 0 .4rem; }
p { margin: 0 0 1rem; }
ul, ol { margin: 0 0 1rem; padding-left: 1.5rem; }
li { margin: .2rem 0; }
li::marker { color: var(--ink-3); }
.lead { font-size: 1.08rem; color: var(--ink-2); margin-bottom: 1.5rem; }

.goals { background: var(--sheet); border: 1px solid var(--rule); border-radius: 12px; padding: .9rem 1.2rem .6rem; margin: 0 0 2rem; }
.goals > p:first-child { font-family: var(--font-display); font-weight: 700; font-size: .9rem; color: var(--blue); margin: 0 0 .3rem; }
.goals ul { list-style: none; padding-left: 0; }
.goals li { padding-left: 1.6rem; position: relative; }
.goals li::before { content: ""; position: absolute; left: .1rem; top: .5em; width: .9rem; height: .9rem; border: 2px solid var(--blue); border-radius: 3px; }

/* ---------- 会話 ---------- */
.talk { display: grid; gap: .7rem; margin: 1.4rem 0 1.6rem; }
.talk p { display: grid; grid-template-columns: 2.6rem minmax(0, 1fr); gap: .7rem; margin: 0; align-items: start; }
.talk p::before {
  content: attr(data-who); width: 2.6rem; height: 2.6rem; border-radius: 50%;
  display: grid; place-items: center; font-family: var(--font-display); font-weight: 700; font-size: .78rem; line-height: 1.1; text-align: center;
}
.talk p > span { background: var(--sheet); border: 1px solid var(--rule); border-radius: 4px 14px 14px 14px; padding: .55rem .9rem; }
.talk p.k::before { content: "カネ"; background: var(--blue-soft); color: var(--blue); border: 2px solid var(--blue); }
.talk p.s::before { content: "ソラ"; background: var(--orange-soft); color: var(--orange); border: 2px solid var(--orange); }
.talk p.m::before { content: "ミオ"; background: var(--purple-soft); color: var(--purple); border: 2px solid var(--purple); }

/* ---------- 囲み ---------- */
.box { border-radius: 12px; padding: .9rem 1.15rem .3rem; margin: 1.5rem 0; border: 1px solid var(--rule); background: var(--sheet); }
.box-title { font-family: var(--font-display); font-weight: 700; margin: 0 0 .5rem; display: flex; flex-wrap: wrap; align-items: baseline; gap: .1rem .6rem; line-height: 1.5; }
.box-title .tag { font-size: .72rem; letter-spacing: .08em; padding: .05rem .55rem; border-radius: 999px; color: var(--sheet); background: var(--ink-2); white-space: nowrap; }
.box.point { background: var(--blue-soft); border-color: color-mix(in srgb, var(--blue) 30%, transparent); }
.box.point .tag { background: var(--blue); }
.box.warn { background: var(--red-soft); border-color: color-mix(in srgb, var(--red) 30%, transparent); }
.box.warn .tag { background: var(--red); }
.box.paper { background: var(--sheet); border: 1.5px dashed var(--ink-3); }
.box.paper .tag { background: var(--ink); }
.box.py { background: var(--green-soft); border-color: color-mix(in srgb, var(--green) 30%, transparent); }
.box.py .tag { background: var(--green); }
.box.def { background: var(--sheet); }
.box.def .tag { background: var(--purple); }
.box.formula { background: var(--sheet); border: 2px solid var(--blue); }
.box.formula .tag { background: var(--blue); }
.box.think { background: var(--orange-soft); border-color: color-mix(in srgb, var(--orange) 30%, transparent); }
.box.think .tag { background: var(--orange); }
.box.summary { background: var(--panel); }
.box.summary .tag { background: var(--ink); }
.box > :last-child { margin-bottom: .8rem; }

/* ---------- 図 ---------- */
figure.fig { margin: 1.8rem 0; }
figure.fig > figcaption { font-size: .84rem; color: var(--ink-2); text-align: center; margin-top: .5rem; line-height: 1.6; }
figure.fig > figcaption .fn { font-weight: 700; color: var(--ink); margin-right: .6em; font-family: var(--font-display); }
.scroll-x { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.diagram svg { display: block; margin: 0 auto; min-width: 540px; height: auto; }
.diagram.small svg { min-width: 360px; }
.diagram svg text { font-family: var(--font-body); }

/* グラフ部品（book.js が描く SVG） */
.widget { background: var(--sheet); border: 1px solid var(--rule); border-radius: 12px; padding: .6rem .6rem .7rem; }
.widget .plot { position: relative; width: 100%; }
.widget .plot svg { display: block; width: 100%; height: auto; touch-action: pan-y; }
.widget .controls { display: grid; grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr)); gap: .35rem 1.2rem; padding: .5rem .5rem 0; }
.widget .controls label { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; gap: .6rem; font-size: .88rem; }
.widget .controls label .pname { font-family: var(--font-mono); font-weight: 700; color: var(--ink); white-space: nowrap; }
.widget .controls .choice { grid-column: 1 / -1; display: flex; flex-wrap: wrap; align-items: center; gap: .35rem .6rem; font-size: .88rem; }
.widget .controls .seg { display: flex; flex-wrap: wrap; gap: .3rem; }
.widget .controls .seg button { font: inherit; font-size: .85rem; padding: .22rem .65rem; border-radius: 999px; border: 1.5px solid var(--rule); background: var(--sheet); color: var(--ink); cursor: pointer; }
.widget .controls .seg button:hover { border-color: var(--blue); background: var(--blue-soft); }
.widget .controls .seg button[aria-pressed="true"] { background: var(--blue); border-color: var(--blue); color: var(--sheet); font-weight: 700; }
.widget .controls output { font-family: var(--font-mono); font-variant-numeric: tabular-nums; min-width: 3.4em; text-align: right; color: var(--blue); font-weight: 700; }
@media (max-width: 560px) { .widget .controls label .pname { white-space: normal; max-width: 8.5em; line-height: 1.3; } }
.widget input[type="range"] { width: 100%; accent-color: var(--blue); min-width: 0; }
.widget .buttons { display: flex; flex-wrap: wrap; gap: .5rem; padding: .6rem .5rem 0; }
.widget .buttons button, .btn {
  font: inherit; font-size: .86rem; padding: .3rem .9rem; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--blue); background: var(--sheet); color: var(--blue); font-weight: 700; line-height: 1.6;
}
.widget .buttons button:hover, .btn:hover { background: var(--blue-soft); }
.widget .buttons button.on { background: var(--blue); color: var(--sheet); }
.widget .readout { padding: .5rem .6rem 0; font-size: .9rem; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.widget .readout:empty { display: none; }
.widget .readout b { color: var(--ink); }
.widget .readout .big { font-family: var(--font-mono); font-size: 1.05rem; color: var(--blue); font-weight: 700; }
.g-axis { stroke: var(--ink-2); stroke-width: 1.4; fill: none; }
.g-grid { stroke: var(--grid); stroke-width: 1; fill: none; }
.g-grid.major { stroke: var(--rule); }
.g-tick { fill: var(--ink-3); font-size: 11.5px; font-family: var(--font-mono); }
.g-label { fill: var(--ink-2); font-size: 13px; font-style: italic; font-family: "Times New Roman", serif; }
.g-text { font-size: 13px; paint-order: stroke; stroke: var(--sheet); stroke-width: 4px; stroke-linejoin: round; }
.g-frame { fill: var(--sheet); stroke: none; }

/* ---------- 流れ図（HTML） ---------- */
.flow { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: .4rem; margin: 1.5rem 0; }
.flow .node { border: 1.5px solid var(--ink-2); background: var(--sheet); border-radius: 10px; padding: .45rem .9rem; text-align: center; line-height: 1.5; font-size: .92rem; min-width: 0; }
.flow .node small { display: block; font-size: .78rem; }
.flow .node.data { background: var(--blue-soft); border-color: var(--blue); }
.flow .node.model { border-style: dashed; border-width: 2px; }
.flow .node.accent { background: var(--blue); color: var(--sheet); border-color: var(--blue); font-weight: 700; }
.flow .node.accent small { color: inherit; opacity: .85; }
.flow .node.warn { background: var(--red-soft); border-color: var(--red); }
.flow .node.ok { background: var(--green-soft); border-color: var(--green); }
.flow .arr { color: var(--ink-3); font-size: 1.1rem; line-height: 1; padding: 0 .1rem; }
.flow .arr[data-label]::after { content: attr(data-label); display: block; font-size: .7rem; color: var(--ink-3); text-align: center; }
.flow.v { flex-direction: column; flex-wrap: nowrap; }
.flow.v .arr { transform: none; }
@media (max-width: 560px) {
  .flow:not(.keep) { flex-direction: column; flex-wrap: nowrap; }
  .flow:not(.keep) .arr { transform: rotate(90deg); }
}

/* ---------- 表 ---------- */
.tablewrap { overflow-x: auto; margin: 1.2rem 0 1.5rem; }
table.data { border-collapse: collapse; margin: 0 auto; font-size: .92rem; font-variant-numeric: tabular-nums; background: var(--sheet); }
table.data caption { caption-side: top; font-size: .84rem; color: var(--ink-2); padding-bottom: .4rem; white-space: nowrap; }
table.data th, table.data td { border: 1px solid var(--rule); padding: .35rem .8rem; text-align: center; }
table.data th { background: var(--panel); font-weight: 700; }
table.data td.l, table.data th.l { text-align: left; }
table.data td.r { text-align: right; }
table.data tr.hl td { background: var(--blue-soft); }

/* ---------- 数式 ---------- */
mjx-container[display="true"] { position: relative; overflow-x: auto; overflow-y: hidden; max-width: 100%; padding: .2rem 0; margin: .9rem 0 !important; }
/* 読み上げ用のかくれた数式が、せまい画面で横にはみ出さないように */
mjx-assistive-mml { max-width: 100%; }
mjx-container { font-size: 108% !important; }
.eq { margin: 1rem 0; }
.eqnote { display: block; text-align: center; font-size: .82rem; color: var(--ink-3); margin-top: -.6rem; margin-bottom: 1rem; }
.steps { counter-reset: step; list-style: none; padding-left: 0; }
.steps > li { counter-increment: step; position: relative; padding-left: 2.4rem; margin: .7rem 0; }
.steps > li::before { content: counter(step); position: absolute; left: 0; top: .15em; width: 1.7rem; height: 1.7rem; border-radius: 50%; background: var(--ink); color: var(--paper); display: grid; place-items: center; font-weight: 700; font-size: .85rem; line-height: 1; }

/* ---------- コード ---------- */
.code { margin: 1.3rem 0; border: 1px solid var(--rule); border-radius: 10px; overflow: hidden; background: var(--code-bg); }
.code-head { display: flex; align-items: center; gap: .6rem; padding: .3rem .4rem .3rem .9rem; font-size: .75rem; color: var(--ink-3); border-bottom: 1px solid var(--rule); font-family: var(--font-mono); }
.code-head .fname { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.code-head button { font: inherit; font-size: .72rem; border: 1px solid var(--rule); background: var(--sheet); color: var(--ink-2); border-radius: 6px; padding: .05rem .6rem; cursor: pointer; }
.code-head button:hover { color: var(--ink); }
pre { margin: 0; overflow-x: auto; }
pre code, .code pre { font-family: var(--font-mono); font-size: .84rem; line-height: 1.7; }
.code pre { padding: .8rem 1rem; tab-size: 4; }
.code pre code { font-size: inherit; }
pre.out { background: var(--sheet); border: 1px dashed var(--rule); border-radius: 10px; padding: .6rem 1rem; margin: -.6rem 0 1.4rem; font-family: var(--font-mono); font-size: .82rem; line-height: 1.65; color: var(--ink-2); }
pre.out::before { content: "実行結果"; display: block; font-size: .7rem; color: var(--ink-3); letter-spacing: .1em; margin-bottom: .2rem; font-family: var(--font-body); }
code { font-family: var(--font-mono); font-size: .88em; }
:not(pre) > code { background: var(--code-bg); border: 1px solid var(--rule); border-radius: 4px; padding: .02em .35em; }
.tk-c { color: var(--ink-3); font-style: italic; }
.tk-s { color: var(--green); }
.tk-n { color: var(--orange); }
.tk-k { color: var(--purple); font-weight: 700; }
.tk-b { color: var(--blue); }
.tk-f { color: var(--blue); font-weight: 700; }

/* ---------- 確認問題 ---------- */
.quiz { border: 1px solid var(--rule); border-radius: 12px; padding: .9rem 1.15rem .5rem; margin: 1.2rem 0; background: var(--sheet); }
.quiz .qnum { font-family: var(--font-display); font-weight: 700; font-size: .85rem; color: var(--red); margin: 0 0 .3rem; }
.quiz details { margin: .2rem 0 .5rem; }
.quiz summary { cursor: pointer; color: var(--blue); font-weight: 700; font-size: .9rem; width: fit-content; }
.quiz details[open] summary { margin-bottom: .5rem; }
.quiz details > div { border-top: 1px dashed var(--rule); padding-top: .6rem; }

/* ---------- ページ送り ---------- */
.page-end { margin-top: 4rem; display: grid; gap: 1rem; }
.readmark { justify-self: center; }
.readmark.done { border-color: var(--green); color: var(--green); background: var(--green-soft); }
.pager { display: grid; grid-template-columns: 1fr 1fr; gap: .8rem; }
.pager a { display: block; border: 1px solid var(--rule); border-radius: 12px; padding: .7rem 1rem; text-decoration: none; color: var(--ink); background: var(--sheet); line-height: 1.5; min-width: 0; }
.pager a:hover { border-color: var(--blue); }
.pager a small { display: block; color: var(--ink-3); font-size: .75rem; }
.pager a.next { text-align: right; grid-column: 2; }
@media (max-width: 560px) { .pager { grid-template-columns: 1fr; } .pager a.next { grid-column: auto; } }

/* ---------- 章の表紙 ---------- */
.chapter-hero {
  margin: 0 0 2rem; padding: 1.6rem 1.4rem 1.3rem; border-radius: 14px; border: 1px solid var(--rule);
  background-color: var(--sheet);
  background-image: linear-gradient(var(--grid) 1px, transparent 1px), linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 22px 22px;
}
.chapter-hero .chnum { font-family: var(--font-display); font-weight: 700; color: var(--blue); font-size: .95rem; letter-spacing: .1em; margin: 0; }
.chapter-hero h1 { margin: .2rem 0 .6rem; font-size: 2.1rem; }
.chapter-hero p.sum { margin: 0; color: var(--ink-2); }
.page-list { list-style: none; padding: 0; display: grid; gap: .6rem; margin: 1rem 0 2rem; }
.page-list a { display: grid; grid-template-columns: 3.4rem minmax(0, 1fr); gap: .2rem .8rem; align-items: baseline; padding: .75rem 1rem; border: 1px solid var(--rule); border-radius: 12px; background: var(--sheet); text-decoration: none; color: var(--ink); }
.page-list a:hover { border-color: var(--blue); }
.page-list .pn { font-family: var(--font-display); font-weight: 700; color: var(--blue); font-variant-numeric: tabular-nums; grid-row: span 2; }
.page-list .pt { font-weight: 700; }
.page-list .ps { font-size: .85rem; color: var(--ink-2); }
.page-list a.read .pt::after { content: "  ✓ 読んだ"; color: var(--green); font-size: .78rem; font-weight: 400; }

/* ---------- トップページ ---------- */
.cover { padding: 1.5rem 0 1rem; }
.cover .kicker { font-family: var(--font-display); font-weight: 700; color: var(--blue); letter-spacing: .12em; font-size: .9rem; margin: 0 0 .3rem; }
.cover h1 { font-size: clamp(2rem, 6vw, 3rem); line-height: 1.3; margin: 0 0 .8rem; }
.cover h1 .accent { color: var(--blue); }
.cover .sub { font-size: 1.1rem; color: var(--ink-2); margin: 0 0 1.4rem; }
.goal-pair { display: grid; grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr)); gap: .9rem; margin: 1.4rem 0 2rem; }
.goal-pair > div { border: 1px solid var(--rule); border-radius: 14px; padding: 1rem 1.2rem .4rem; background: var(--sheet); }
.goal-pair h3 { margin: 0 0 .3rem; font-size: 1.05rem; }
.goal-pair .gi { font-family: var(--font-mono); font-size: .8rem; color: var(--ink-3); margin: 0; }
.toc-part { margin: 2.4rem 0 1rem; display: flex; align-items: baseline; gap: .8rem; flex-wrap: wrap; border-bottom: 2px solid var(--ink); padding-bottom: .3rem; }
.toc-part h2 { margin: 0; border: 0; padding: 0; font-size: 1.3rem; }
.toc-part span { color: var(--ink-3); font-size: .85rem; }
.toc-ch { display: grid; grid-template-columns: 3.2rem minmax(0, 1fr); gap: .1rem .9rem; padding: 1rem 0; border-bottom: 1px dashed var(--rule); }
.toc-ch .cn { font-family: var(--font-display); font-weight: 700; font-size: 1.7rem; line-height: 1.2; color: var(--blue); font-variant-numeric: tabular-nums; grid-row: span 3; }
.toc-ch .ct { font-family: var(--font-display); font-weight: 700; font-size: 1.15rem; color: var(--ink); text-decoration: none; }
.toc-ch .ct:hover { color: var(--blue); }
.toc-ch .cs { font-size: .86rem; color: var(--ink-2); margin: .1rem 0 .4rem; }
.toc-ch ol { list-style: none; padding: 0; margin: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(15rem, 1fr)); gap: .1rem 1.2rem; font-size: .88rem; }
.toc-ch ol a { color: var(--ink-2); text-decoration: none; display: flex; gap: .5rem; }
.toc-ch ol a:hover { color: var(--blue); }
.toc-ch ol a .pn { color: var(--ink-3); font-variant-numeric: tabular-nums; flex: 0 0 auto; }
.toc-ch ol a.read .pn::after { content: " ✓"; color: var(--green); }

@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; scroll-behavior: auto !important; } }

/* 囲みの中の「答えを見る」 */
.box details { margin: .2rem 0 .8rem; }
.box summary { cursor: pointer; color: var(--blue); font-weight: 700; font-size: .9rem; width: fit-content; }
.box details[open] summary { margin-bottom: .4rem; }

/* Python で描いた図（Matplotlib の出力は白地のまま見せる） */
:root { --plot-bg: #ffffff; }
.pyfig img { display: block; margin: 0 auto; height: auto; background: var(--plot-bg); border: 1px solid var(--rule); border-radius: 10px; padding: 6px; }

/* 行列の積を確かめる部品 */
.matmul { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: .6rem 1rem; padding: .8rem .5rem .4rem; }
.matmul .mlabel { font-family: var(--font-display); font-weight: 700; text-align: center; font-size: .85rem; color: var(--ink-2); margin-top: .2rem; }
.matmul table { border-collapse: separate; border-spacing: 3px; border-left: 2px solid var(--ink-2); border-right: 2px solid var(--ink-2); border-radius: 6px; padding: 2px 4px; font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
.matmul td { min-width: 2.4rem; height: 2.1rem; text-align: center; border-radius: 6px; background: var(--panel); transition: background .15s; }
.matmul td.hl-row { background: var(--blue-soft); outline: 2px solid var(--blue); }
.matmul td.hl-col { background: var(--orange-soft); outline: 2px solid var(--orange); }
.matmul .res td { cursor: pointer; }
.matmul .res td:hover { outline: 2px dashed var(--green); }
.matmul .res td.on { background: var(--green-soft); outline: 2px solid var(--green); font-weight: 700; }
.matmul .op { font-size: 1.4rem; color: var(--ink-2); }

/* 2つの図を横に並べる（せまい画面では縦に並ぶ） */
.duo { display: grid; grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr)); gap: .8rem; }
.duo > * { min-width: 0; }

/* 13-7 数字をかくマス */
.pad-wrap { display: grid; grid-template-columns: minmax(0, 15rem) minmax(0, 1fr); gap: 1rem; align-items: start; }
@media (max-width: 640px) { .pad-wrap { grid-template-columns: minmax(0, 1fr); } }
.pad-grid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 3px; background: var(--rule); padding: 3px; border-radius: 8px; touch-action: none; max-width: 15rem; width: 100%; margin: 0 auto; }
.pad-cell { aspect-ratio: 1; max-width: 100%; border: 0; border-radius: 3px; background: var(--ink); cursor: pointer; padding: 0; }
.pad-cell.on { background: var(--paper); box-shadow: inset 0 0 0 2px var(--blue); }
.pad-side .buttons { padding-left: 0; }
.pad-side .widget { border: 0; padding: 0; background: none; }

/* ---------- 例題・つまずき度・解答（第0章から使う） ---------- */
.ex { position: relative; margin: 1.8rem 0 1.2rem; padding: .9rem 1.15rem .4rem; border-radius: 12px; background: var(--panel); border: 1px solid var(--rule); }
.ex-head { display: flex; flex-wrap: wrap; align-items: center; gap: .4rem .8rem; margin: 0 0 .5rem; line-height: 1.5; }
.ex-tag { font-family: var(--font-display); font-weight: 700; color: var(--sheet); background: var(--ink); padding: .1rem .9rem .1rem .7rem; border-radius: 4px 0 0 4px; clip-path: polygon(0 0, 100% 0, calc(100% - 8px) 50%, 100% 100%, 0 100%); padding-right: 1.2rem; }
.ex-title { font-family: var(--font-display); font-weight: 700; }
.ex > p:not(.ex-head) { font-size: 1.05rem; }
.ex > :last-child { margin-bottom: .6rem; }
.level { display: inline-flex; align-items: center; gap: 2px; font-size: .75rem; font-family: var(--font-display); font-weight: 700; color: var(--ink-2); background: var(--sheet); border: 1.5px solid var(--ink-3); border-radius: 999px; padding: 0 .55rem 0 .7rem; line-height: 1.7; white-space: nowrap; vertical-align: middle; }
.level .lv-t { margin-right: .25rem; }
.level i { font-style: normal; display: inline-grid; place-items: center; width: 1.05rem; height: 1.05rem; border-radius: 50%; font-size: .7rem; line-height: 1; background: var(--rule); color: var(--sheet); }
.level i.on { background: var(--red); }
.quiz .qnum { display: flex; flex-wrap: wrap; align-items: center; gap: .3rem .7rem; }
.quiz .qnum .level { font-size: .7rem; }
.ans { margin: .8rem 0 1.2rem; font-size: 1.05rem; line-height: 2.1; }
.ans::before { content: "解答"; display: inline-block; margin-right: .6rem; vertical-align: .08em; font-family: var(--font-display); font-weight: 700; font-size: .78rem; line-height: 1.6; color: var(--ink); background: var(--orange-soft); border: 1px solid color-mix(in srgb, var(--orange) 45%, transparent); padding: 0 .5rem; border-radius: 4px; }
.ans b, .ans strong { border-bottom: 3px double var(--ink); padding: 0 .1em; }
.ans .ex-ref { display: inline-block; margin-left: .7rem; font-size: .75rem; color: var(--ink-3); line-height: 1.5; vertical-align: .1em; }
.rule { display: block; width: fit-content; max-width: 100%; margin: .8rem 0 1.1rem; padding: .35rem .9rem; border-radius: 6px; background: var(--red-soft); border-left: 6px solid var(--red); font-weight: 700; color: var(--ink); line-height: 1.6; }

/* ---------- 1手ずつ進める部品（筆算・計算の順番） ---------- */
.stepper .plot { display: flex; justify-content: center; padding: .4rem 0 .2rem; overflow-x: auto; }
.stepper .buttons { justify-content: center; }
.stepper .buttons button.main { background: var(--blue); color: var(--sheet); }
.stepper .buttons button.main:hover { background: color-mix(in srgb, var(--blue) 85%, var(--ink)); }
.stepper .buttons button:disabled { opacity: .4; cursor: default; }
.stepper .readout { min-height: 3.4em; line-height: 1.75; }
.stepper .readout .stepno { display: inline-block; margin-right: .5rem; padding: 0 .55rem; border-radius: 999px; font-size: .75rem; font-weight: 700; background: var(--ink); color: var(--sheet); vertical-align: .1em; }
.stepper .readout .ansline { display: inline-block; margin-top: .25rem; color: var(--ink); font-weight: 700; }
.stepper .readout .ansline b { color: var(--red); font-size: 1.08rem; }
.readout .err { color: var(--red); font-weight: 700; }
.ask { display: flex; flex-wrap: wrap; align-items: end; gap: .4rem .8rem; padding: .3rem .5rem .6rem; margin-bottom: .3rem; border-bottom: 1px dashed var(--rule); font-size: .86rem; }
.ask .ask-title { flex-basis: 100%; font-family: var(--font-display); font-weight: 700; color: var(--ink-2); font-size: .8rem; }
.ask label { display: grid; gap: .1rem; }
.ask label span { font-size: .75rem; color: var(--ink-3); }
.ask input, .ask select { font: inherit; font-family: var(--font-mono); font-size: .95rem; padding: .2rem .45rem; border: 1px solid var(--ink-3); border-radius: 6px; background: var(--sheet); color: var(--ink); max-width: 100%; }
.ask select { font-family: var(--font-body); font-size: .82rem; }
.ask .btn { font-size: .82rem; }
svg.hissan { display: block; max-width: 100%; height: auto; }
.hs-d { fill: var(--ink); font-family: var(--font-display); font-size: 26px; font-weight: 700; }
.hs-d.op { fill: var(--ink-2); }
.hs-d.pad { fill: var(--ink-3); opacity: .6; }
.hs-s { fill: var(--ink-2); font-family: var(--font-display); font-size: 13px; font-weight: 700; }
.hs-s.used { fill: var(--ink-3); }
.hs-p { fill: var(--ink); }
.hs-p.pad { fill: var(--ink-3); opacity: .6; }
.hs-p.faint { fill: var(--red); opacity: .7; }
.hs-x { stroke: var(--ink-2); stroke-width: 2; stroke-linecap: round; }
.hs-h { stroke: var(--ink); stroke-width: 2.2; fill: none; stroke-linecap: round; }
.hs-a { stroke: var(--red); stroke-width: 1.6; fill: none; stroke-dasharray: 4 3; }
.hs-ah { fill: var(--red); }
.hs-focus { fill: var(--red-soft); stroke: var(--red); stroke-width: 1.4; stroke-dasharray: 5 4; }
.hs-d.new, .hs-s.new, .hs-p.new { fill: var(--red); opacity: 1; }
.hs-x.new, .hs-h.new { stroke: var(--red); }
.calcbox { font-family: var(--font-display); font-size: 1.3rem; line-height: 1.5; padding: .3rem .4rem; font-variant-numeric: tabular-nums; }
.calcbox .cline { display: grid; grid-template-columns: 1.4em auto; color: var(--ink-3); margin: .15rem 0 .45rem; }
.calcbox .cline.now { color: var(--ink); }
.calcbox .eqs { text-align: center; }
.calcbox .chl { color: var(--red); text-decoration: underline wavy var(--red) 1.5px; text-underline-offset: .32em; }
.calcbox .cdone { text-decoration: underline var(--ink-3) 1px; text-underline-offset: .32em; }
.calcbox .cnew { color: var(--red); }
.calcbox sup { font-size: .62em; }
.frac { display: inline-flex; flex-direction: column; vertical-align: middle; text-align: center; line-height: 1.05; font-size: .85em; margin: 0 .1em; }
.frac > span:first-child { border-bottom: 1.5px solid currentColor; padding: 0 .15em; }
.ladderbox .plot { display: flex; justify-content: center; padding: .6rem 0 .2rem; }
table.ladder { border-collapse: collapse; font-family: var(--font-display); font-size: 1.3rem; font-variant-numeric: tabular-nums; }
table.ladder td { padding: .05rem .7rem; text-align: right; }
table.ladder td.lp { color: var(--blue); border-right: 2px solid var(--ink); text-align: center; padding-right: .5rem; }
table.ladder tr:not(.last) td.lv { border-bottom: 2px solid var(--ink); }
table.ladder tr:not(.last) td.lv:last-child { border-right: 0; }
table.ladder td.lb { color: var(--orange); }
table.ladder tr.last td.lp { border-right: 0; }
.ladderbox .readout .lg { color: var(--blue); font-weight: 700; }
.ladderbox .readout .ll { color: var(--orange); font-weight: 700; }
/* 汎用の「1手ずつ」：√ を簡単にする・n進法・循環小数（arith.js の Book.steps） */
.stepbox { display: flex; flex-direction: column; align-items: center; gap: .35rem; font-family: var(--font-display); font-size: 1.25rem; line-height: 1.6; padding: .3rem .4rem; font-variant-numeric: tabular-nums; text-align: center; }
.stepbox .sb-line { margin: .1rem 0; }
.stepbox .sb-line.big { font-size: 1.45rem; }
.stepbox .sb-new { color: var(--red); font-weight: 700; }
.stepbox .sb-faint { color: var(--ink-3); }
.stepbox .red, .stepper .readout .red { color: var(--red); }
.stepbox sub, .stepper .readout sub { font-size: .6em; }
.stepbox .pair { font-weight: 700; }
.stepbox .pair.c1 { color: var(--blue); } .stepbox .pair.c2 { color: var(--orange); } .stepbox .pair.c3 { color: var(--green); } .stepbox .pair.c4 { color: var(--purple); }
.rad { position: relative; white-space: nowrap; display: inline-flex; align-items: stretch; vertical-align: bottom; }
span.rad > svg.rs { display: inline-block; width: .62em; height: auto; flex: none; align-self: stretch; overflow: visible; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linejoin: round; stroke-linecap: round; }
.rad .rs path { vector-effect: non-scaling-stroke; }
.rad .rin { border-top: 1.6px solid currentColor; padding: .06em .12em 0 .06em; line-height: 1.25; }
.rad .sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.stepbox table.ladder tr.new td { color: var(--red); }
table.ladder td.lr { color: var(--red); text-align: left; padding-left: .2rem; border: 0 !important; }
.radix-wrap { display: flex; align-items: flex-end; gap: .3rem; }
.radix-wrap .up { color: var(--red); font-size: .85rem; font-weight: 700; writing-mode: vertical-rl; text-orientation: upright; padding-bottom: 2.2em; letter-spacing: .05em; }
.stepbox .rp { background: var(--red-soft); }
.rdot { position: relative; display: inline-block; }
.rdot::before { content: ''; position: absolute; left: 50%; top: -.02em; width: .2em; height: .2em; margin-left: -.1em; border-radius: 50%; background: currentColor; }
.chain { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: .15rem .3rem; font-size: 1.05rem; max-width: 34rem; }
.chain .ch-l { font-size: .8rem; font-weight: 700; color: var(--ink-3); margin-right: .2rem; }
.chain span { min-width: 1.6em; padding: 0 .25em; border-radius: .4em; }
.chain span.now { color: var(--blue); font-weight: 700; }
.chain span.hit { background: var(--red); color: var(--sheet); font-weight: 700; }
.chain i { font-style: normal; color: var(--ink-3); font-size: .8em; }

/* 入れ子の箱（数のなかま分けなど） */
.nest { margin: .4rem 0; font-size: .95rem; }
.nest-box { border: 2px solid var(--rule); border-radius: 12px; padding: .45rem .6rem .55rem; background: var(--sheet); display: flex; flex-direction: column; gap: .3rem; min-width: 0; }
.nest-box > b { font-family: var(--font-display); }
.nest-box > span, .nest-ex span { color: var(--ink-2); font-variant-numeric: tabular-nums; }
.nest-row { display: flex; flex-wrap: wrap; gap: .5rem; align-items: stretch; }
.nest-row > * { flex: 1 1 9rem; }
.nest-ex { display: flex; flex-direction: column; justify-content: center; gap: .25rem; padding: .2rem .4rem; }
.nest-box.n-real { background: var(--panel); }
.nest-box.n-rat { border-color: var(--blue); } .nest-box.n-rat > b { color: var(--blue); }
.nest-box.n-int { border-color: var(--green); background: var(--green-soft); } .nest-box.n-int > b { color: var(--green); }
.nest-box.n-nat { border-color: var(--orange); background: var(--orange-soft); } .nest-box.n-nat > b { color: var(--orange); }
.nest-box.n-irr { border-color: var(--red); background: var(--red-soft); } .nest-box.n-irr > b { color: var(--red); }
/* 表の中で、条件にあうマスに色をつける（9-11 のさいころの表など） */
table.data td.hl5 { background: var(--blue-soft); color: var(--blue); font-weight: 700; }
table.data td.hl10 { background: var(--orange-soft); color: var(--orange); font-weight: 700; }

/* 自作 LLM 編（第18〜23章）：トークンの並び・文字の入力・生成した文章 */
.toks { display: flex; flex-wrap: wrap; gap: .35rem .22rem; align-items: flex-start; margin: .35rem 0; line-height: 1.35; }
.tok { display: inline-flex; flex-direction: column; align-items: center; min-width: 0; }
.tok > b { font-weight: 400; padding: .12rem .32rem; border-radius: 6px; border: 1px solid var(--rule); background: var(--sheet); white-space: pre; min-width: 1.15em; text-align: center; color: var(--ink); }
.tok > i { font-style: normal; font-family: var(--font-mono); font-size: .68rem; color: var(--ink-3); margin-top: .1rem; white-space: nowrap; }
.tok.c1 > b { background: var(--blue-soft); border-color: color-mix(in srgb, var(--blue) 45%, transparent); }
.tok.c2 > b { background: var(--orange-soft); border-color: color-mix(in srgb, var(--orange) 45%, transparent); }
.tok.c3 > b { background: var(--green-soft); border-color: color-mix(in srgb, var(--green) 45%, transparent); }
.tok.c4 > b { background: var(--purple-soft); border-color: color-mix(in srgb, var(--purple) 45%, transparent); }
.tok.c5 > b { background: var(--red-soft); border-color: color-mix(in srgb, var(--red) 45%, transparent); }
.tok.on > b { outline: 2px solid var(--blue); outline-offset: 1px; font-weight: 700; }
.tok.dim > b { opacity: .45; }
.tok.special > b { background: var(--ink-2); border-color: var(--ink-2); color: var(--sheet); font-family: var(--font-mono); font-size: .8rem; }
.tok .sp { color: var(--ink-3); }
.llm-in { width: 100%; box-sizing: border-box; font: inherit; font-size: 1rem; padding: .45rem .65rem; border: 1.5px solid var(--rule); border-radius: 8px; background: var(--sheet); color: var(--ink); }
.llm-in:focus { outline: 2px solid var(--blue); outline-offset: 1px; }
.llm-row { display: flex; flex-wrap: wrap; gap: .45rem .6rem; align-items: center; margin: .45rem 0; }
.llm-row > label { font-size: .88rem; font-weight: 700; color: var(--ink-2); }
.llm-out { font-size: 1rem; line-height: 1.85; padding: .55rem .75rem; border-radius: 8px; background: var(--panel); min-height: 2.4em; white-space: pre-wrap; overflow-wrap: anywhere; }
.llm-out .gen { color: var(--blue); font-weight: 700; }
.llm-out .bad { color: var(--red); font-weight: 700; }
.llm-out .note { color: var(--ink-3); font-size: .85rem; }
.llm-panel { padding: .5rem .5rem .2rem; }
.llm-panel h4 { margin: .2rem 0 .3rem; font-size: .9rem; color: var(--ink-2); }
.bits { font-family: var(--font-mono); letter-spacing: .02em; white-space: nowrap; }
.bits .h { color: var(--red); font-weight: 700; }
.bits .x { color: var(--blue); font-weight: 700; }
table.data td.mono, table.data th.mono { font-family: var(--font-mono); }
