/* The Elements of Differentiable Programming — HTML edition */

/* Base colours (also the fallback before a style is applied) */
:root {
  --bg: #fdfdfb;
  --fg: #1d1f21;
  --muted: #5f6368;
  --line: #e3e3de;
  --side-bg: #f4f4f0;
  --side-fg: #2b2d30;
  --side-hover: #e8e8e2;
  --link: #1a5fb4;
  --accent: #1a5fb4;
  --code-bg: #f1f1ec;
  --fig-bg: #ffffff;
  --box-def: #2e8b57;   --box-def-bg: #f1f9f2;
  --box-thm: #3b6fb6;   --box-thm-bg: #f1f6fc;
  --box-exm: #c2185b;   --box-exm-bg: #fdf2f6;
  --box-rem: #b58b00;   --box-rem-bg: #fffbea;
  --proof: #5f6368;
  --sidebar-w: 19rem;
  color-scheme: light;
}

/* ---------- Styles (picked in the menu; see THEMES in build.py) ---------- */

/* Solarized (light) */
:root[data-theme="solarized"] {
  --bg: #fdf6e3;
  --fg: #3b4b52;
  --muted: #7b8a8b;
  --line: #e8dfc5;
  --side-bg: #eee8d5;
  --side-fg: #3b4b52;
  --side-hover: #e4dcc3;
  --link: #268bd2;
  --accent: #268bd2;
  --code-bg: #eee8d5;
  --fig-bg: #fffdf6;
  --box-def: #859900;
  --box-def-bg: #f3f0d6;
  --box-thm: #268bd2;
  --box-thm-bg: #eef0e4;
  --box-exm: #d33682;
  --box-exm-bg: #f8eadd;
  --box-rem: #b58900;
  --box-rem-bg: #f8edcc;
  --proof: #657b83;
}

/* Mint: soft green-grey, easy on the eyes */
:root[data-theme="mint"] {
  --bg: #f3f8f4;
  --fg: #1f2d26;
  --muted: #5c6f64;
  --line: #d9e6dd;
  --side-bg: #e8f1eb;
  --side-fg: #1f2d26;
  --side-hover: #dbe8df;
  --link: #0f6e5c;
  --accent: #0f6e5c;
  --code-bg: #e4efe7;
  --fig-bg: #ffffff;
  --box-def: #2e8b57;
  --box-def-bg: #e6f3ea;
  --box-thm: #3a6ea5;
  --box-thm-bg: #e7eff2;
  --box-exm: #b0436e;
  --box-exm-bg: #f3e9ec;
  --box-rem: #a8801a;
  --box-rem-bg: #f4f0dc;
  --proof: #56685e;
}

/* Sky: pale cool blue */
:root[data-theme="sky"] {
  --bg: #f4f7fb;
  --fg: #1b2430;
  --muted: #5d6b7d;
  --line: #dde5ef;
  --side-bg: #e9eff7;
  --side-fg: #1b2430;
  --side-hover: #dce5f1;
  --link: #1f5fbf;
  --accent: #1f5fbf;
  --code-bg: #e6edf6;
  --fig-bg: #ffffff;
  --box-def: #2b8a5e;
  --box-def-bg: #e9f4ef;
  --box-thm: #2f63b5;
  --box-thm-bg: #e6eefa;
  --box-exm: #b23a6c;
  --box-exm-bg: #f5eaf0;
  --box-rem: #a47c12;
  --box-rem-bg: #f6f1de;
  --proof: #566476;
}

/* Stone: neutral light grey */
:root[data-theme="stone"] {
  --bg: #f2f2f0;
  --fg: #222222;
  --muted: #666664;
  --line: #dcdcd8;
  --side-bg: #e8e8e5;
  --side-fg: #262626;
  --side-hover: #dcdcd8;
  --link: #3456a0;
  --accent: #3456a0;
  --code-bg: #e6e6e2;
  --fig-bg: #ffffff;
  --box-def: #3d7a55;
  --box-def-bg: #e7eee8;
  --box-thm: #3e5f99;
  --box-thm-bg: #e6e9ef;
  --box-exm: #a3446a;
  --box-exm-bg: #efe6e9;
  --box-rem: #92731f;
  --box-rem-bg: #efebdc;
  --proof: #5a5a58;
}

:root[data-theme="dark"] {
  --bg: #17181a;
  --fg: #e3e3e0;
  --muted: #a0a3a8;
  --line: #2e3034;
  --side-bg: #1e1f22;
  --side-fg: #d6d6d2;
  --side-hover: #2a2c30;
  --link: #8ab4f8;
  --accent: #8ab4f8;
  --code-bg: #24262a;
  --fig-bg: #f4f4f2;
  --box-def: #6fcf97;   --box-def-bg: #1b2620;
  --box-thm: #8ab4f8;   --box-thm-bg: #1b2230;
  --box-exm: #f48fb1;   --box-exm-bg: #2a1d24;
  --box-rem: #e6c35c;   --box-rem-bg: #29261a;
  --proof: #a0a3a8;
}

/* Dark styles share a few rules: */
:root[data-tone="dark"] { color-scheme: dark; }

* { box-sizing: border-box; }

html { scroll-padding-top: 1rem; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua",
               Georgia, "Times New Roman", serif;
  font-size: 1.075rem;
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
}

a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- Sidebar ---------- */

#sidebar {
  position: fixed;
  top: 0; left: 0; bottom: 0;
  width: var(--sidebar-w);
  overflow-y: auto;
  background: var(--side-bg);
  color: var(--side-fg);
  border-right: 1px solid var(--line);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 0.86rem;
  line-height: 1.35;
  padding: 0;
  z-index: 20;
  display: flex; flex-direction: column;
}
.toc { flex: 1 0 auto; padding-bottom: 1rem; }
.sidebar-foot {
  position: sticky; bottom: 0;
  background: var(--side-bg);
  border-top: 1px solid var(--line);
  padding: 0.6rem 1rem;
}

.sidebar-head {
  position: sticky; top: 0;
  background: var(--side-bg);
  padding: 1rem 1rem 0.75rem;
  border-bottom: 1px solid var(--line);
  z-index: 1;
}
.book-title {
  display: block;
  text-align: center;
  font-weight: 650;
  font-size: 0.98rem;
  color: var(--side-fg);
}
.theme-picker {
  display: inline-flex; align-items: center; gap: 0.45rem;
  font-size: 0.8rem; color: var(--muted);
}
.theme-picker select {
  font: inherit; font-size: 0.82rem;
  color: var(--side-fg);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.2rem 1.6rem 0.2rem 0.55rem;
  cursor: pointer;
  appearance: none; -webkit-appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
                    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 0.85rem) 55%, calc(100% - 0.55rem) 55%;
  background-size: 0.3rem 0.3rem;
  background-repeat: no-repeat;
}
.theme-picker select:hover { border-color: var(--muted); }
.theme-picker select:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.toc ul { list-style: none; margin: 0; padding: 0; }
.toc-chapters { padding: 0.5rem 0.5rem 0; }
.toc a {
  display: block;
  color: var(--side-fg);
  padding: 0.22rem 0.5rem;
  border-radius: 5px;
}
.toc a:hover { background: var(--side-hover); text-decoration: none; }
.toc a.active { font-weight: 700; color: var(--fg); }
.toc .num { color: var(--muted); margin-right: 0.4em; font-variant-numeric: tabular-nums; }
.toc-part {
  margin: 1rem 0.5rem 0.25rem;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.toc-part span { display: block; text-transform: none; letter-spacing: 0; font-size: 0.82rem; font-weight: 600; color: var(--side-fg); margin-top: 0.1rem; }
.toc-chapter > a, .toc-chapter summary > a { font-weight: 500; }
.toc-chapter.current > details > summary > a { font-weight: 700; }
.toc details > summary { list-style: none; cursor: pointer; position: relative; }
.toc details > summary::-webkit-details-marker { display: none; }
.toc details > summary > a { padding-right: 1.6rem; }
.toc details > summary::after {
  content: "";
  position: absolute; right: 0.6rem; top: 0.62rem;
  width: 0.35rem; height: 0.35rem;
  border-right: 1.5px solid var(--muted); border-bottom: 1.5px solid var(--muted);
  transform: rotate(-45deg);
  transition: transform 0.15s;
  pointer-events: none;
}
.toc details[open] > summary::after { transform: rotate(45deg); }
.toc details > ul { margin: 0.1rem 0 0.4rem; }
.toc-l1 > a { padding-left: 1.1rem; }
.toc-l2 > a { padding-left: 2.1rem; font-size: 0.82rem; }
.toc .katex { font-size: 1em; }

/* Phone top bar (hidden on wide screens) */
#topbar { display: none; }
#menu-toggle {
  font-size: 1.15rem; line-height: 1;
  padding: 0.4rem 0.55rem;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: transparent; color: var(--side-fg);
  cursor: pointer;
}
.topbar-title {
  color: var(--side-fg); font-weight: 600; font-size: 0.9rem;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

/* ---------- Main column ---------- */

#content {
  margin-left: var(--sidebar-w);
  padding: 2.5rem 2.5rem 4rem;
}
article { max-width: 46rem; margin: 0 auto; }

h1, h2, h3, h4 { line-height: 1.25; font-weight: 650; }
h1 { font-size: 2rem; margin: 0.5rem 0 1.5rem; }
h1 .chapnum { display: block; font-size: 0.95rem; font-weight: 500; color: var(--muted); letter-spacing: 0.04em; text-transform: uppercase; margin-bottom: 0.4rem; }
h1.book { font-size: 2.3rem; margin-top: 1rem; text-align: center; }
.authors { color: var(--muted); margin-top: -0.6rem; text-align: center; }
.authors span { font-size: 0.92em; }
h2 { font-size: 1.45rem; margin: 2.4rem 0 0.8rem; }
h3 { font-size: 1.18rem; margin: 2rem 0 0.6rem; }
h4 { font-size: 1.04rem; margin: 1.6rem 0 0.5rem; font-style: italic; font-weight: 600; }
.secnum { color: var(--muted); margin-right: 0.35em; font-variant-numeric: tabular-nums; }
.part-head {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--muted);
  font-size: 0.95rem;
  border-bottom: 1px solid var(--line);
  padding-bottom: 0.4rem; margin-bottom: 1.2rem;
}
.part-head span { font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; font-size: 0.8rem; margin-right: 0.4rem; }

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

code { background: var(--code-bg); padding: 0.1em 0.3em; border-radius: 3px; font-size: 0.9em; }

/* ---------- Math ---------- */

.katex { font-size: 1.07em; }
.katex-display { overflow-x: auto; overflow-y: hidden; padding: 0.15rem 0; margin: 0.9rem 0; }
.katex-display > .katex { white-space: nowrap; }
/* Equations wider than the screen scroll sideways; app.js adds these
   classes so the cut-off edge fades out, hinting there is more. */
.katex-display.more-right { -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 2.5rem), transparent); mask-image: linear-gradient(to right, #000 calc(100% - 2.5rem), transparent); }
.katex-display.more-left { -webkit-mask-image: linear-gradient(to left, #000 calc(100% - 2.5rem), transparent); mask-image: linear-gradient(to left, #000 calc(100% - 2.5rem), transparent); }
.katex-display.more-left.more-right { -webkit-mask-image: linear-gradient(to right, transparent, #000 2.5rem, #000 calc(100% - 2.5rem), transparent); mask-image: linear-gradient(to right, transparent, #000 2.5rem, #000 calc(100% - 2.5rem), transparent); }
.anchor { position: relative; top: -1rem; }
.math-fallback { vertical-align: middle; max-width: 100%; }
.fallback-display { display: flex !important; align-items: center; justify-content: center; gap: 1rem; }
.fallback-display .math-fallback { height: auto; }
.eqno { margin-left: auto; }
:root[data-tone="dark"] .math-fallback { filter: invert(0.9) hue-rotate(180deg); }
.math-error { color: #c00; white-space: pre-wrap; }

/* ---------- Theorem-like boxes ---------- */

.box {
  margin: 1.3rem 0;
  padding: 0.55rem 1rem 0.6rem;
  border-left: 3px solid var(--box-thm);
  background: var(--box-thm-bg);
  border-radius: 0 6px 6px 0;
}
.box.definition { border-color: var(--box-def); background: var(--box-def-bg); }
.box.example { border-color: var(--box-exm); background: var(--box-exm-bg); }
.box.remark { border-color: var(--box-rem); background: var(--box-rem-bg); }
.box > p:first-child { margin-top: 0.3rem; }
.box-head { font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; font-size: 0.95em; }
.box-head strong { font-weight: 700; }

details.proof { color: var(--proof); margin: 0.6rem 0 0.9rem; }
details.proof > summary {
  display: inline-flex; align-items: center; gap: 0.4rem;
  cursor: pointer; list-style: none;
  font-style: italic; color: var(--muted);
  padding: 0.1rem 0.6rem 0.1rem 0.45rem;
  border: 1px solid var(--line); border-radius: 999px;
  font-size: 0.95em; user-select: none;
}
details.proof > summary::-webkit-details-marker { display: none; }
details.proof > summary::before {
  content: ""; width: 0.35rem; height: 0.35rem;
  border-right: 1.5px solid currentColor; border-bottom: 1.5px solid currentColor;
  transform: rotate(-45deg); transition: transform 0.15s;
}
details.proof[open] > summary::before { transform: rotate(45deg); }
details.proof > summary:hover { color: var(--fg); border-color: var(--muted); }
details.proof > summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.proof-body { border-left: 2px solid var(--line); padding-left: 0.9rem; margin: 0.3rem 0 0 0.3rem; }
.proof-body > p:first-child { margin-top: 0.4rem; }

/* ---------- Floats: figures, tables, algorithms ---------- */

.float {
  margin: 1.8rem 0;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: flex-end;
  gap: 1rem 2%;
}
.float > * { flex-basis: 100%; }
.float > .subfigure, .float > .minipage { flex-grow: 0; flex-shrink: 1; }
.float > .subfigure .caption, .float > .minipage .caption { text-align: left; }
.float p { text-align: center; margin: 0.4rem 0; }
.float img { max-width: 100%; height: auto; background: var(--fig-bg); border-radius: 4px; }
/* Light styles: the figures' white background takes the page colour (one
   set of PNGs; on dark styles they keep their white card). */
:root[data-tone="light"] .float img { mix-blend-mode: multiply; background: transparent; border-radius: 0; }
:root[data-tone="dark"] .float.figure img { padding: 4px; }
.caption {
  font-size: 0.9rem;
  line-height: 1.45;
  color: var(--fg);
}
.caption p { text-align: left; }
.float > .caption { order: 10; }
.float.table > .caption, .float.algorithm > .caption { order: -1; }
.subfigure .caption p { text-align: center; }

table {
  border-collapse: collapse;
  margin: 0.6rem auto;
  font-size: 0.92rem;
  max-width: 100%;
}
.float.table { overflow-x: auto; }
th, td { padding: 0.3rem 0.7rem; vertical-align: top; text-align: left; }
thead th { border-top: 1.5px solid var(--fg); border-bottom: 1px solid var(--fg); }
tbody tr:last-child td { border-bottom: 1.5px solid var(--fg); }

.float.algorithm {
  display: block;
  border-top: 1.5px solid var(--fg);
  border-bottom: 1.5px solid var(--fg);
  padding: 0.2rem 0 0.4rem;
}
.float.algorithm > .caption { border-bottom: 1px solid var(--fg); padding: 0.2rem 0; margin-bottom: 0.3rem; font-size: 0.95rem; }
.float.algorithm > .caption p { margin: 0.2rem 0; }
.algorithmic { counter-reset: none; font-size: 0.95rem; }
.alg-line { position: relative; margin-left: 2rem; }
.alg-line > p { margin: 0.12rem 0; text-align: left; }
.alg-line[data-line]::before {
  content: attr(data-line);
  position: absolute; left: -2rem; top: 0.1rem;
  width: 1.4rem; text-align: right;
  font-size: 0.8em; color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.alg-comment { float: right; color: var(--muted); margin-left: 1rem; }

/* ---------- Bibliography ---------- */

#refs .csl-entry { margin: 0 0 0.7rem; padding-left: 1.6rem; text-indent: -1.6rem; font-size: 0.95rem; }
#refs .csl-entry:target { background: var(--code-bg); }

/* ---------- Index page ---------- */

.abstract-title { font-size: 1.1rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.index-toc { list-style: none; padding-left: 0; }
.index-toc li.part { margin-top: 1rem; font-weight: 650; }

.cover-block { margin: 1.5rem 0 0.5rem; text-align: center; }
.cover-block .cover { display: inline-block; }
.cover-block img {
  width: 15rem; max-width: 100%; height: auto; display: block; margin: 0 auto;
  background: #fff; border: 1px solid var(--line); border-radius: 3px;
  box-shadow: 0 4px 18px rgba(0,0,0,0.12);
  transition: transform 0.15s, box-shadow 0.15s;
}
.cover-block .cover:hover img { transform: translateY(-2px); box-shadow: 0 8px 24px rgba(0,0,0,0.18); }
.cover-block p { margin: 0.6rem 0 0; font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; font-size: 0.9rem; }
pre.bibtex {
  background: var(--code-bg); border: 1px solid var(--line); border-radius: 6px;
  padding: 0.8rem 1rem; overflow-x: auto; font-size: 0.85rem; line-height: 1.45;
}
pre.bibtex code { background: none; padding: 0; font-size: inherit; }

/* ---------- Pager ---------- */

.pager {
  max-width: 46rem; margin: 3.5rem auto 0;
  display: flex; justify-content: space-between; gap: 1rem;
  border-top: 1px solid var(--line); padding-top: 1rem;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; font-size: 0.9rem;
}
.pager .next { text-align: right; margin-left: auto; }

/* ---------- Small screens ---------- */

@media (max-width: 900px) {
  html { scroll-padding-top: 3.6rem; }
  #topbar {
    display: flex; align-items: center; gap: 0.7rem;
    position: fixed; top: 0; left: 0; right: 0; height: 3rem;
    padding: 0 0.75rem;
    background: var(--side-bg); border-bottom: 1px solid var(--line);
    z-index: 30;
  }
  #sidebar { top: 3rem; transform: translateX(-100%); transition: transform 0.2s; box-shadow: none; width: min(85vw, var(--sidebar-w)); }
  body.menu-open #sidebar { transform: none; box-shadow: 0 0 30px rgba(0,0,0,0.25); }
  #content { margin-left: 0; padding: 4.2rem 1rem 3rem; }
  .katex { font-size: 1em; }
  .katex-display > .katex { font-size: 0.97em; }
  .float > .subfigure, .float > .minipage { flex-basis: 100% !important; }
}

@media print {
  details.proof > summary { display: none; }
  #sidebar, #topbar, .pager { display: none; }
  #content { margin: 0; padding: 0; }
}
