/* "Programming Macaw OS", the book (tools/docs_book.py writes the pages). Four themes,
   chosen with the brush in the top bar and kept in the browser: light, paper, night, navy. */
:root, [data-theme="light"] {
  --bg: #ffffff; --fg: #1f1d24; --dim: #6b6676; --line: #e6e3ea;
  --side-bg: #f6f4f1; --side-fg: #3a3640; --side-here: #c62f2c; --side-line: #e4ded3;
  --link: #2c5fa8; --code-bg: #f4f1ea; --code-fg: #33303a; --inline-bg: #f1eee8;
  --c: #7a7d6e; --s: #2f7d3a; --n: #b05a00; --k: #a8296a; --f: #2c5fa8; --g: #7a4fb0;
  --bar-fg: #6b6676; --pop-bg: #ffffff; --mark: #ffe58a; --accent: #c62f2c;
}
[data-theme="paper"] {
  --bg: #fbf7ee; --fg: #2a2520; --dim: #786e60; --line: #e7dfcd;
  --side-bg: #f3ecdc; --side-fg: #43392d; --side-here: #c62f2c; --side-line: #e2d7bf;
  --link: #2c5fa8; --code-bg: #f2ead8; --code-fg: #3a3128; --inline-bg: #efe6d2;
  --c: #8a7f69; --s: #3d7a2c; --n: #b45f00; --k: #a8296a; --f: #2c5fa8; --g: #7a4fb0;
  --bar-fg: #786e60; --pop-bg: #fbf7ee; --mark: #f9da7a; --accent: #c62f2c;
}
[data-theme="night"] {
  --bg: #17151b; --fg: #dedae4; --dim: #9a94a6; --line: #2d2934;
  --side-bg: #1f1c25; --side-fg: #c8c3d2; --side-here: #f2c033; --side-line: #2d2934;
  --link: #8db4f0; --code-bg: #211e27; --code-fg: #dedae4; --inline-bg: #26222d;
  --c: #7f798c; --s: #9fd18b; --n: #f2a65a; --k: #f08bb7; --f: #8db4f0; --g: #c3a6f0;
  --bar-fg: #9a94a6; --pop-bg: #24202b; --mark: #6b5a1a; --accent: #f2c033;
}
[data-theme="navy"] {
  --bg: #161f30; --fg: #c9d3e6; --dim: #8796b3; --line: #26324a;
  --side-bg: #2b3a56; --side-fg: #c3cde0; --side-here: #f2c033; --side-line: #3a4a69;
  --link: #79aef0; --code-bg: #1d2840; --code-fg: #d5ddeb; --inline-bg: #1f2a42;
  --c: #7c8aa6; --s: #a6d98f; --n: #f2a65a; --k: #f094c0; --f: #79aef0; --g: #c3a6f0;
  --bar-fg: #8796b3; --pop-bg: #222e48; --mark: #5c5121; --accent: #f2c033;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-padding-top: 64px; }
body {
  margin: 0; background: var(--bg); color: var(--fg);
  font: 17px/1.7 "Open Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
:focus-visible { outline: 2px solid var(--link); outline-offset: 2px; border-radius: 3px; }
.skip { position: absolute; left: -999px; top: 8px; background: var(--bg); padding: 6px 10px; z-index: 50; }
.skip:focus { left: 8px; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---- the chapters, down the left ---- */
:root { --side-w: 300px; }
#sidebar {
  position: fixed; top: 0; bottom: 0; left: 0; width: var(--side-w); overflow-y: auto;
  background: var(--side-bg); color: var(--side-fg); padding: 18px 14px 30px 18px;
  font-size: 0.95rem; line-height: 1.45; border-right: 1px solid var(--side-line);
  transition: transform 0.25s ease; z-index: 20; overscroll-behavior: contain;
}
.sidebar-closed #sidebar { transform: translateX(-100%); }
.book-home { display: flex; align-items: center; gap: 10px; color: var(--side-fg); font-weight: 700; font-size: 1.05rem; margin: 0 0 16px; }
.book-home img { width: 32px; height: 32px; image-rendering: pixelated; }
.book-home:hover { text-decoration: none; }
#sidebar ol { list-style: none; margin: 0; padding: 0; }
.chapters > li > a { display: block; padding: 6px 6px; color: var(--side-fg); border-radius: 6px; }
.chapters > li > a b { font-weight: 700; margin-right: 2px; }
.chapters > li > a:hover { color: var(--side-here); text-decoration: none; }
.chapters > li > a.here { color: var(--side-here); font-weight: 600; }
ol.sections { margin: 2px 0 8px 18px !important; border-left: 2px solid var(--side-line); }
ol.sections a { display: block; padding: 3px 10px; color: var(--side-fg); opacity: 0.8; margin-left: -2px; border-left: 3px solid transparent; }
ol.sections a:hover { opacity: 1; text-decoration: none; color: var(--side-here); }
ol.sections a.sub { padding-left: 24px; font-size: 0.9em; }
ol.sections a.on { opacity: 1; color: var(--link); border-left-color: var(--link); }
.back { margin: 22px 6px 0; font-size: 0.85rem; }
.back a { color: var(--side-fg); opacity: 0.75; }

/* ---- the page ---- */
#page { margin-left: var(--side-w); transition: margin-left 0.25s ease; min-height: 100vh; }
.sidebar-closed #page { margin-left: 0; }
#bar {
  position: sticky; top: 0; z-index: 10; height: 56px; display: flex; align-items: center; gap: 8px;
  padding: 0 14px; background: var(--bg); color: var(--bar-fg); border-bottom: 1px solid transparent;
  transition: border-color 0.2s;
}
#bar.scrolled { border-bottom-color: var(--line); }
.tools { display: flex; gap: 4px; align-items: center; flex: 1; }
.tools.right { justify-content: flex-end; }
.tools button, .tools a { display: grid; place-items: center; width: 40px; height: 40px; border-radius: 8px; color: var(--bar-fg); }
.tools button:hover, .tools a:hover { color: var(--fg); background: var(--line); text-decoration: none; }
.bar-title { color: var(--fg); font-size: 1.35rem; font-weight: 300; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-title:hover { text-decoration: none; }
.pop-wrap { position: relative; }
.pop {
  position: absolute; top: 44px; left: 0; min-width: 140px; list-style: none; margin: 0; padding: 6px;
  background: var(--pop-bg); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18); z-index: 30;
}
.pop button { display: block; width: 100%; text-align: left; padding: 6px 12px; border-radius: 6px; color: var(--fg); }
.pop button:hover, .pop button[aria-checked="true"] { background: var(--line); }

main {
  max-width: 820px; margin: 0 auto; padding: 18px 40px 60px;
}
main h1 { font-size: 2.5rem; line-height: 1.2; margin: 0.4em 0 0.6em; letter-spacing: -0.01em; }
main h2 { font-size: 1.75rem; line-height: 1.25; margin: 1.9em 0 0.5em; }
main h3 { font-size: 1.3rem; margin: 1.6em 0 0.4em; }
main h4 { font-size: 1.05rem; margin: 1.4em 0 0.3em; }
main h2 a.anchor, main h3 a.anchor, main h4 a.anchor { color: inherit; }
main h2 a.anchor:hover::after, main h3 a.anchor:hover::after { content: " #"; color: var(--dim); }
main h2 a.anchor:hover, main h3 a.anchor:hover { text-decoration: none; }
main p, main ul, main ol { margin: 0 0 1em; }
main li { margin: 0.25em 0; }
main hr { border: 0; border-top: 1px solid var(--line); margin: 2em 0; }
blockquote { margin: 0 0 1em; padding: 4px 18px; border-left: 4px solid var(--accent); background: var(--inline-bg); border-radius: 0 6px 6px 0; }
code { font-family: "Source Code Pro", ui-monospace, Menlo, Consolas, monospace; font-size: 0.875em; }
:not(pre) > code { background: var(--inline-bg); padding: 0.1em 0.35em; border-radius: 4px; white-space: nowrap; }
td :not(pre) > code { white-space: normal; }
pre.code {
  position: relative; background: var(--code-bg); color: var(--code-fg); padding: 14px 18px; border-radius: 8px;
  overflow-x: auto; line-height: 1.5; margin: 0 0 1.2em; font-size: 0.95rem;
}
pre.code .c { color: var(--c); font-style: italic; }
pre.code .s { color: var(--s); }
pre.code .n { color: var(--n); }
pre.code .k { color: var(--k); font-weight: 600; }
pre.code .f { color: var(--f); }
pre.code .g { color: var(--g); }
.copy {
  position: absolute; top: 8px; right: 8px; font-size: 0.75rem; padding: 3px 9px; border-radius: 6px;
  border: 1px solid var(--line); background: var(--bg); color: var(--dim); opacity: 0; transition: opacity 0.15s;
  font-family: system-ui, sans-serif;
}
pre.code:hover .copy, .copy:focus { opacity: 1; }
.table { overflow-x: auto; margin: 0 0 1.4em; }
table { border-collapse: collapse; width: 100%; font-size: 0.93rem; line-height: 1.5; }
th, td { text-align: left; vertical-align: top; padding: 8px 12px; border-bottom: 1px solid var(--line); }
th { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--dim); border-bottom-width: 2px; }
tbody tr:hover { background: var(--inline-bg); }
td:first-child { width: 42%; }

/* the arrows to the chapters either side */
.side-arrow {
  position: fixed; top: 50%; transform: translateY(-50%); display: grid; place-items: center;
  width: 56px; height: 120px; color: var(--dim); border-radius: 10px; z-index: 5;
}
.side-arrow svg { width: 34px; height: 34px; stroke-width: 1.6; }
.side-arrow:hover { color: var(--fg); background: var(--inline-bg); text-decoration: none; }
.side-arrow.prev { left: calc(var(--side-w) + 10px); transition: left 0.25s ease; }
.sidebar-closed .side-arrow.prev { left: 10px; }
.side-arrow.next { right: 10px; }
@media (max-width: 1240px) { .side-arrow { display: none; } }
.page-nav { display: flex; justify-content: space-between; gap: 16px; margin-top: 3.5em; padding-top: 1.2em; border-top: 1px solid var(--line); }
.page-nav a { display: flex; flex-direction: column; padding: 10px 14px; border: 1px solid var(--line); border-radius: 10px; max-width: 48%; }
.page-nav a.next { text-align: right; margin-left: auto; }
.page-nav small { color: var(--dim); font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.05em; }
.page-nav a:hover { border-color: var(--link); text-decoration: none; }

/* search */
#search { max-width: 820px; margin: 0 auto; padding: 12px 40px 0; }
#search-box {
  width: 100%; font: inherit; color: var(--fg); background: var(--code-bg); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 14px;
}
#search-count { color: var(--dim); font-size: 0.9rem; margin: 8px 2px; }
#search-results { list-style: none; margin: 0 0 12px; padding: 0; border-bottom: 1px solid var(--line); }
#search-results li { padding: 10px 2px; border-top: 1px solid var(--line); }
#search-results a { font-weight: 600; }
#search-results .where { color: var(--dim); font-size: 0.85rem; }
#search-results p { margin: 2px 0 0; font-size: 0.92rem; color: var(--dim); }
#search-results mark, main mark { background: var(--mark); color: inherit; border-radius: 2px; }
.muted { color: var(--dim); }

/* narrow screens: the chapters slide over the page */
@media (max-width: 900px) {
  #page, .sidebar-closed #page { margin-left: 0; }
  #sidebar { box-shadow: 0 0 30px rgba(0, 0, 0, 0.3); }
  .sidebar-closed #sidebar { box-shadow: none; visibility: hidden; transition: transform 0.25s ease, visibility 0s 0.25s; }
  main, #search { padding-left: 20px; padding-right: 20px; }
  main h1 { font-size: 2rem; }
  td:first-child { width: auto; }
  .bar-title { font-size: 1.1rem; }
}

/* the whole book on paper */
.print-all .chapter + .chapter { margin-top: 4em; }
@media print {
  #sidebar, #bar, #search, .side-arrow, .page-nav, .copy, .skip { display: none !important; }
  #page { margin: 0 !important; }
  body { background: #fff; color: #000; font-size: 11pt; }
  main { max-width: none; padding: 0; }
  pre.code { white-space: pre-wrap; border: 1px solid #ccc; break-inside: avoid; }
  .print-all .chapter { break-before: page; }
  a { color: #000; }
  h2, h3 { break-after: avoid; }
}
