/* ==========================================================================
   Beej's Guide to C — custom overrides
   - Code font: Maple Mono
   - Clean light/dark theme driven by <html data-theme="light|dark">
   - Fallback to system preference when no data-theme is set (JS-less)
   ========================================================================== */

/* ---- Fonts ---------------------------------------------------------------
   Maple Mono is installed system-wide (variable font). Referenced by name. */
:root {
  --font-code: "Maple Mono", Menlo, Monaco, Consolas, "Lucida Console", monospace;
  --font-body: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

code,
kbd,
pre,
pre code,
tt,
samp,
.sourceCode,
code.sourceCode span {
  font-family: var(--font-code) !important;
  font-variant-ligatures: contextual; /* Maple's coding ligatures */
}

/* Optional: comfortable reading font for body text.
   Comment this rule out to keep the original sans-serif. */
body {
  font-family: var(--font-body);
}

/* ==========================================================================
   THEME TOKENS
   ========================================================================== */

/* Light (default / explicit) */
:root,
html[data-theme="light"] {
  --bg:          #fdfdfd;
  --fg:          #1a1a1a;
  --muted:       #606060;
  --link:        #0645ad;
  --link-visit:  #6b1a8f;
  --rule:        #e6e6e6;
  --code-bg:     #f4f4f4;
  --code-fg:     #1a1a1a;
  --pre-bg:      #f4f4f4;
  --pre-fg:      #1a1a1a;
  --quote-fg:    #606060;
  --quote-bar:   #d0d0d0;
  --table-line:  #cccccc;

  /* Ayu Light — syntax palette (ayu, MIT) */
  --syn-tag: #55B4D4;
  --syn-func: #F2A300;
  --syn-entity: #399EE6;
  --syn-string: #86B300;
  --syn-regexp: #4CBF99;
  --syn-markup: #F07171;
  --syn-keyword: #FF7E33;
  --syn-special: #D9B077;
  --syn-comment: #787B80;
  --syn-constant: #A37ACC;
  --syn-operator: #ED9366;
}

/* Dark */
html[data-theme="dark"] {
  --bg:          #0b0c0d;
  --fg:          #d2d2d2;
  --muted:       #868c91;
  --link:        #7bb0ff;
  --link-visit:  #c99bff;
  --rule:        #232628;
  --code-bg:     #151718;
  --code-fg:     #e0e0e0;
  --pre-bg:      #121314;
  --pre-fg:      #dcdccc;
  --quote-fg:    #a6a6a6;
  --quote-bar:   #33383b;
  --table-line:  #2b2f32;

  /* Ayu Dark — syntax palette */
  --syn-tag: #39BAE6;
  --syn-func: #FFB454;
  --syn-entity: #59C2FF;
  --syn-string: #AAD94C;
  --syn-regexp: #95E6CB;
  --syn-markup: #F07178;
  --syn-keyword: #FF8F40;
  --syn-special: #E6C08A;
  --syn-comment: #99ADBF;
  --syn-constant: #D2A6FF;
  --syn-operator: #F29668;
}

/* JS-less fallback: follow the browser when no data-theme is present */
@media (prefers-color-scheme: dark) {
  html:not([data-theme]) {
    --bg:          #0b0c0d;
    --fg:          #d2d2d2;
    --muted:       #868c91;
    --link:        #7bb0ff;
    --link-visit:  #c99bff;
    --rule:        #232628;
    --code-bg:     #151718;
    --code-fg:     #e0e0e0;
    --pre-bg:      #121314;
    --pre-fg:      #dcdccc;
    --quote-fg:    #a6a6a6;
    --quote-bar:   #33383b;
    --table-line:  #2b2f32;

    --syn-tag: #39BAE6;
    --syn-func: #FFB454;
    --syn-entity: #59C2FF;
    --syn-string: #AAD94C;
    --syn-regexp: #95E6CB;
    --syn-markup: #F07178;
    --syn-keyword: #FF8F40;
    --syn-special: #E6C08A;
    --syn-comment: #99ADBF;
    --syn-constant: #D2A6FF;
    --syn-operator: #F29668;
  }
}

/* ==========================================================================
   APPLY TOKENS (win over pandoc's inline <style> via specificity + !important)
   ========================================================================== */
html {
  color: var(--fg) !important;
  background-color: var(--bg) !important;
}
body {
  color: var(--fg) !important;
  background-color: var(--bg) !important;
}

a,
a:link { color: var(--link) !important; }
a:visited { color: var(--link-visit) !important; }

h1, h2, h3, h4, h5, h6 { color: var(--fg) !important; }

/* Inline code */
code {
  color: var(--code-fg) !important;
  background-color: var(--code-bg) !important;
  border-radius: 3px;
  padding: 0.1em 0.3em;
}

/* Code blocks — no double background/padding for code inside pre */
pre {
  color: var(--pre-fg) !important;
  background-color: var(--pre-bg) !important;
  border: 1px solid var(--rule) !important;
  border-radius: 8px;
  padding: 0.8em 1em;
}

/* ==========================================================================
   CODE BLOCK CHROME (wrapper built by theme.js)
   A titlebar carrying the language, and a copy button that shows up on hover.
   ========================================================================== */
/* pandoc wraps every block in div.sourceCode and paints it a hardcoded
   grey. The frame belongs to .codeblock now, so the wrapper gets out of the
   way — otherwise its grey rings the block wherever .codeblock has margin. */
div.sourceCode {
  background: transparent !important;
  margin: 0 !important;
  padding: 0 !important;
  border: 0 !important;
}

.codeblock {
  margin: 1rem 0;
  border: 1px solid var(--rule);
  border-radius: 8px;
  overflow: hidden;
  background-color: var(--pre-bg);
}
/* The frame now belongs to the wrapper, so the <pre> gives its own up. */
.codeblock > pre {
  margin: 0;
  border: 0 !important;
  border-radius: 0 !important;
}
/* Line numbers, rebuilt.
   pandoc paints each number inside a link pointing at its own line, held in
   a 4em box shifted by -1em while the line itself is shifted by -4em, all
   inside a 3em margin. Those offsets fight each other and leave dead space.

   The gutter width has to be one measurement shared by the rule (drawn on
   the <pre>) and the number box (drawn on a line, where `code` is at 85% of
   the font size, so an em or a ch there is a different length). @property
   computes it where it is declared and passes it down as an absolute
   length, so both agree — and it still follows the reader's text size,
   since ch is resolved on the <pre>. */
@property --ln-gutter {
  syntax: "<length>";
  inherits: true;
  initial-value: 0px;
}
@property --ln-pad {
  syntax: "<length>";
  inherits: true;
  initial-value: 0px;
}

.codeblock > pre.numberSource {
  --ln-gutter: 3.4ch;
  --ln-pad: 0.6ch;
  margin-left: 0;
  padding-left: calc(var(--ln-gutter) + var(--ln-pad));
  border-left: 0 !important;
  position: relative;
}
/* the rule between gutter and code */
.codeblock > pre.numberSource::before {
  content: "";
  position: absolute;
  left: var(--ln-gutter);
  top: 0;
  bottom: 0;
  border-left: 1px solid var(--rule);
}
/* Each number is anchored to its own line, so it rides with it. Both
   lengths below come from @property, already resolved to px on the <pre>,
   so the line's smaller font cannot stretch them. */
pre.numberSource code > span {
  position: relative !important;
  left: 0 !important;
}
pre.numberSource code > span > a:first-child { display: none !important; }
pre.numberSource code > span::before {
  content: counter(source-line);
  position: absolute;
  /* top: 0 pins the number to its own line's box. Without it the browser
     places it where it would have landed in the flow, which depends on its
     line box — and a smaller font gives a shorter box, which is what was
     lifting every number a couple of pixels. Same font size and line height
     as the code is what makes the baselines agree; the numbers stay quiet
     through colour, not size. */
  top: 0;
  left: calc(-1 * (var(--ln-gutter) + var(--ln-pad)));
  width: var(--ln-gutter);
  text-align: center;
  color: var(--muted);
  opacity: 0.7;
  -webkit-user-select: none;
  user-select: none;
}

.codeblock-bar {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.35rem 0.75rem;
  border-bottom: 1px solid var(--rule);
  background-color: var(--code-bg);
  font-family: var(--font-body);
  line-height: 0;
  color: var(--muted);
}
/* Just the mark — the language name lives in the tooltip. */
.codeblock-lang {
  display: inline-flex;
  align-items: center;
}
.codeblock-lang svg {
  width: 15px;
  height: 15px;
  display: block;
  flex: 0 0 auto;
}
/* Centred on the bar itself, not on the space left between the logo and
   the button, so it lines up with the middle of the block. */
.codeblock-file {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  max-width: 60%;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-family: var(--font-code);
  font-size: 0.75rem;
  line-height: 1.4;
  color: var(--muted);
}
.codeblock-copy {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-left: auto;
  padding: 0.25rem;
  border: 1px solid var(--rule);
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.12s ease, color 0.12s ease, border-color 0.12s ease;
}
.codeblock-copy svg {
  width: 14px;
  height: 14px;
  display: block;
}
.codeblock:hover .codeblock-copy,
.codeblock-copy:focus-visible,
.codeblock-copy[data-done] { opacity: 1; }
.codeblock-copy:hover { color: var(--link); border-color: var(--link); }
.codeblock-copy[data-done] { color: var(--link); border-color: var(--link); }

/* Lines mdBook hides: boilerplate an example needs to compile but that is
   not the point being made. Out of sight, still in the DOM, so the copy
   button hands back code that actually builds. */
.boring,
code.sourceCode > span.boring { display: none; }

/* The Rust book wraps every listing in <figure class="listing">, and the
   pandoc stylesheet centres figures — which dragged the code with it.
   Captions stay centred, code goes back to the left margin. */
figure.listing { text-align: left; margin: 1.5em 0 2em; }
figure.listing figcaption {
  text-align: center;
  margin-top: 0.5em;   /* sit close to the block it describes */
}
/* ...which means the block above it must not push it away. */
figure.listing .codeblock,
figure.listing div.sourceCode,
figure.listing pre { margin-bottom: 0; }
/* The stylesheet numbers every figure itself ("Figure 3.1: "). The book's
   listings already carry their own number, so that prefix is dropped. */
figure.listing figcaption::before,
figure.illustration figcaption::before { content: none; }

figure.illustration { text-align: center; margin: 1.5em 0 2em; }
figure.illustration img { margin-bottom: 0; }
figure.illustration figcaption { margin-top: 0.5em; }

/* Diagrams should not be the only square corners on the page. */
img { border-radius: 8px; max-width: 100%; height: auto; }
.codeblock, .codeblock pre { text-align: left; }

/* Touch: no hover to reveal it, so leave it on. */
@media (hover: none) {
  .codeblock-copy { opacity: 1; }
}
pre code {
  background-color: transparent !important;
  padding: 0 !important;
  border-radius: 0;
}

/* Blockquotes */
blockquote {
  color: var(--quote-fg) !important;
  border-left: 3px solid var(--quote-bar) !important;
}

/* Tables */
table, th, td {
  border-color: var(--table-line) !important;
}
th, td {
  border-bottom: 1px solid var(--table-line) !important;
}

/* Horizontal rules */
hr { border-color: var(--rule) !important; background-color: var(--rule) !important; }

/* Muted / footnote-ish text */
.line-number, .lineNo { color: var(--muted) !important; }

/* ==========================================================================
   SYNTAX HIGHLIGHTING — dark palette (Zenburn-ish) when data-theme=dark
   ========================================================================== */
html[data-theme="dark"] code span.al { color: #ffcfaf; }
html[data-theme="dark"] code span.an { color: #7f9f7f; font-weight: bold; }
html[data-theme="dark"] code span.at { color: #7d9029; }
html[data-theme="dark"] code span.bn { color: #dca3a3; }
html[data-theme="dark"] code span.bu { color: #efef8f; }
html[data-theme="dark"] code span.cf { color: #f0dfaf; }
html[data-theme="dark"] code span.ch { color: #dca3a3; }
html[data-theme="dark"] code span.cn { color: #dca3a3; font-weight: bold; }
html[data-theme="dark"] code span.co { color: #7f9f7f; font-style: italic; }
html[data-theme="dark"] code span.cv { color: #7f9f7f; font-weight: bold; }
html[data-theme="dark"] code span.do { color: #7f9f7f; }
html[data-theme="dark"] code span.dt { color: #dfdfbf; }
html[data-theme="dark"] code span.dv { color: #dcdccc; }
html[data-theme="dark"] code span.er { color: #c3bf9f; font-weight: bold; }
html[data-theme="dark"] code span.fl { color: #c0bed1; }
html[data-theme="dark"] code span.fu { color: #efef8f; }
html[data-theme="dark"] code span.im { color: #efef8f; font-weight: bold; }
html[data-theme="dark"] code span.in { color: #7f9f7f; font-weight: bold; }
html[data-theme="dark"] code span.kw { color: #f0dfaf; font-weight: bold; }
html[data-theme="dark"] code span.op { color: #f0efd0; }
html[data-theme="dark"] code span.ot { color: #efef8f; }
html[data-theme="dark"] code span.pp { color: #ffcfaf; font-weight: bold; }
html[data-theme="dark"] code span.sc { color: #dca3a3; }
html[data-theme="dark"] code span.ss { color: #cc9393; }
html[data-theme="dark"] code span.st { color: #cc9393; }
html[data-theme="dark"] code span.va { color: #efdcbc; }
html[data-theme="dark"] code span.vs { color: #cc9393; }
html[data-theme="dark"] code span.wa { color: #7f9f7f; font-weight: bold; }

/* ==========================================================================
   TEXT SIZE — driven by --reader-scale, stamped on <html> by theme.js.
   It has to land on <body>, and win: the guides ship a hard-coded
   "body { font-size: 12pt }" that would otherwise cancel the whole thing.
   Everything inside is sized in em/%, so the page scales as a block —
   including body's max-width, which keeps the line length constant.
   ========================================================================== */
body {
  font-size: calc(1rem * var(--reader-scale, 1)) !important;
}

@media print {
  body { font-size: 12pt !important; }
}

/* ==========================================================================
   READER BAR (created by theme.js)
   Hidden by default: only a discreet grip shows in the corner, and the
   controls fade in when the pointer comes near.
   ========================================================================== */
#reader-dock {
  position: fixed;
  bottom: 0;
  right: 0;
  z-index: 9999;
  display: flex;
  justify-content: flex-end;
  padding: 1rem; /* enlarges the hover target around the bar */
}

/* The grip — the only thing visible at rest. */
#reader-dock::after {
  content: "";
  position: absolute;
  right: 1.1rem;
  bottom: 1.1rem;
  width: 2rem;
  height: 0.28rem;
  border-radius: 0.28rem;
  background-color: var(--fg);
  opacity: 0.28;
  transition: opacity 0.18s ease;
}
#reader-dock:hover::after { opacity: 0; }

#reader-bar {
  display: flex;
  gap: 0.1rem;
  padding: 0.25rem;
  border: 1px solid transparent;
  border-radius: 1.4rem;
  opacity: 0;
  pointer-events: none;
  transform: translateY(0.3rem);
  transition: opacity 0.18s ease, transform 0.18s ease,
              background-color 0.18s ease, border-color 0.18s ease,
              box-shadow 0.18s ease;
}
#reader-dock:hover #reader-bar,
#reader-bar:focus-within {
  opacity: 1;
  pointer-events: auto;
  transform: none;
  background-color: var(--code-bg);
  border-color: var(--rule);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.2);
}

#reader-bar .reader-btn {
  padding: 0.35rem 0.6rem;
  border: none;
  border-radius: 1rem;
  background-color: transparent;
  color: var(--muted) !important;
  text-decoration: none !important;
  font-family: var(--font-body);
  font-size: 0.78rem;
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
  transition: background-color 0.12s ease, color 0.12s ease;
  user-select: none;
}
#reader-bar .reader-btn:hover,
#reader-bar .reader-btn:focus-visible {
  background-color: var(--bg);
  color: var(--fg) !important;
}

/* No hover on touch: keep the controls visible instead. */
@media (hover: none) {
  #reader-dock::after { display: none; }
  #reader-bar {
    opacity: 1;
    pointer-events: auto;
    transform: none;
    background-color: var(--code-bg);
    border-color: var(--rule);
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.2);
  }
}

/* Transient text-size readout */
#reader-toast {
  position: fixed;
  bottom: 3.6rem;
  right: 1.2rem;
  z-index: 9999;
  padding: 0.3rem 0.7rem;
  border: 1px solid var(--rule);
  border-radius: 1rem;
  background-color: var(--code-bg);
  color: var(--fg);
  font-family: var(--font-body);
  font-size: 0.85rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}
#reader-toast.visible { opacity: 1; }

/* ==========================================================================
   ON-THIS-PAGE OUTLINE (created by theme.js)
   Lives in the left margin, and only where there is margin to spare — the
   text column stays exactly where it was, so nothing shifts when it appears.
   ========================================================================== */
#side-toc {
  display: none;
  position: fixed;
  top: 5.5rem;
  left: 1.5rem;
  width: 12rem;
  max-height: calc(100vh - 9rem);
  overflow-y: auto;
  font-family: var(--font-body);
  font-size: 0.76rem;
  line-height: 1.35;
  scrollbar-width: thin;
}
@media (min-width: 1200px) {
  #side-toc { display: block; }
}
#side-toc ul { list-style: none; margin: 0; padding: 0; }
#side-toc li { margin: 0 0 0.3rem; }
#side-toc li.lvl-h3 { padding-left: 0.7rem; }
#side-toc a,
#side-toc a:link,
#side-toc a:visited {
  display: block;
  padding: 0.1rem 0 0.1rem 0.55rem;
  border-left: 2px solid var(--rule);
  color: var(--muted) !important;
  text-decoration: none !important;
  transition: color 0.12s ease, border-color 0.12s ease;
}
#side-toc a:hover { color: var(--link) !important; }
#side-toc a.active {
  color: var(--fg) !important;
  border-left-color: var(--link);
}

@media print { #side-toc { display: none !important; } }

/* ==========================================================================
   BACK LINK (created by theme.js)
   ========================================================================== */
#back-link,
#back-link:link,
#back-link:visited {
  display: block;
  margin: 0 0 0.9rem;
  font-family: var(--font-body);
  font-size: 0.85rem;
  color: var(--muted) !important;
  text-decoration: none !important;
}
#back-link:hover { color: var(--link) !important; }

@media print { #back-link { display: none !important; } }

/* ==========================================================================
   CONTINUE READING (created by theme.js)
   ========================================================================== */
.resume-card,
.resume-card:link,
.resume-card:visited {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  border: 1px solid var(--rule);
  border-radius: 8px;
  background-color: var(--code-bg);
  text-decoration: none !important;
  color: var(--fg) !important;
  transition: border-color 0.12s ease;
}
.resume-card:hover { border-color: var(--link); }
.resume-text { flex: 1 1 auto; }
.resume-label { color: var(--muted); }
.resume-title { font-weight: 600; }
.resume-arrow {
  flex: 0 0 auto;
  margin-left: auto;
  color: var(--muted);
  transition: transform 0.12s ease;
}
.resume-card:hover .resume-arrow {
  color: var(--link);
  transform: translateX(2px);
}
.resume-banner { margin: 1.5rem 0; }

/* Same line, but sitting in the footer of a guide's box on the main menu. */
.resume-row,
.resume-row:link,
.resume-row:visited {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
  padding: 0.6rem 1.25rem;
  border-top: 1px solid var(--rule);
  font-size: 0.85rem;
  text-decoration: none !important;
  color: var(--fg) !important;
}
.resume-row:hover { color: var(--link) !important; }
.resume-row:hover .resume-arrow {
  color: var(--link);
  transform: translateX(2px);
}

@media print {
  #reader-bar, #reader-toast, .resume-card, .resume-row,
  .codeblock-bar { display: none !important; }
}


/* ==========================================================================
   SYNTAX COLOURS — Ayu Light / Ayu Dark
   pandoc names its tokens with two-letter classes; these map them onto the
   Ayu roles. Later than pandoc's own <style>, so these win.
   ========================================================================== */
code span.kw, code span.cf { color: var(--syn-keyword) !important; }
code span.dt                { color: var(--syn-entity) !important; }
code span.bu, code span.ex  { color: var(--syn-tag) !important; }
code span.fu                { color: var(--syn-func) !important; }
code span.at                { color: var(--syn-func) !important; }
code span.st, code span.vs, code span.ch, code span.im {
  color: var(--syn-string) !important;
}
code span.sc, code span.ss  { color: var(--syn-regexp) !important; }
code span.dv, code span.bn, code span.fl, code span.cn {
  color: var(--syn-constant) !important;
}
code span.op                { color: var(--syn-operator) !important; }
code span.pp                { color: var(--syn-special) !important; }
code span.co, code span.do, code span.an, code span.cv, code span.in {
  color: var(--syn-comment) !important;
}
code span.al, code span.er, code span.wa {
  color: var(--syn-markup) !important;
}
code span.va                { color: var(--syn-tag) !important; }

/* pandoc bolds keywords, types and functions. Ayu does not, and the weight
   fights the colour for attention — one signal per token is enough. */
pre code,
pre code span,
code.sourceCode span { font-weight: 400 !important; }

/* ==========================================================================
   HEADING RHYTHM — a heading belongs to what follows it
   ========================================================================== */
h2, h3, h4, h5, h6 {
  margin-top: 2.2em !important;
  margin-bottom: 0.6em !important;
}
h1 { margin-top: 1.4em !important; margin-bottom: 0.7em !important; }
body > h1:first-of-type,
header#title-block-header h1 { margin-top: 0.2em !important; }

/* A section page carries its chapter's title above it; the two belong
   together, so they sit close. */
h1.chapter-title { margin-bottom: 0.15em !important; }
h1.chapter-title + h2 { margin-top: 0.25em !important; }
