/* CRUX user manual - page styles.
   Colours come from assets/themes.css, which is generated from the app's own
   palettes. Nothing here hard-codes a colour except the diagram plate, which has
   to stay light because the diagrams are drawn for a light ground.

   NO transitions and NO animations anywhere, deliberately: this page is shown
   inside CRUX, which has none. Anchor jumps land instantly. */

:root {
  --sidebar-w: 17rem;
  --page-pad: 2rem;
  --font-prose: -apple-system, BlinkMacSystemFont, system-ui, "Segoe UI", sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
}

* { box-sizing: border-box; }

html { scroll-behavior: auto; }

body {
  margin: 0;
  background: var(--bg-base);
  color: var(--content-primary);
  font: 400 16px/1.65 var(--font-prose);
  -webkit-text-size-adjust: 100%;
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
}

/* ── scrollbars ──────────────────────────────────────────────────────────── */
/* The browser default is a grey overlay bar that reads as somebody else's UI.
   These are the page's own: a thin track in the surface colour, a thumb on the
   border tones. WebKit covers Safari, Chrome and the WKWebView the app will use;
   the two `scrollbar-*` properties cover Firefox. */

* {
  scrollbar-width: thin;
  scrollbar-color: var(--border-default) var(--bg-surface);
}

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: var(--bg-surface); }
::-webkit-scrollbar-thumb {
  background: var(--border-default);
  border: 2px solid var(--bg-surface);   /* inset, so the thumb reads as narrower */
  border-radius: 6px;
}
::-webkit-scrollbar-thumb:hover { background: var(--border-accent); }
::-webkit-scrollbar-corner { background: var(--bg-surface); }

/* The document's own bar sits against the page ground, not the sidebar's. */
html::-webkit-scrollbar-track, body::-webkit-scrollbar-track { background: var(--bg-base); }
html::-webkit-scrollbar-thumb, body::-webkit-scrollbar-thumb { border-color: var(--bg-base); }

/* ── sidebar ─────────────────────────────────────────────────────────────── */

.toc {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  padding: 1rem 0 3rem;
  background: var(--bg-surface);
  border-right: 1px solid var(--border-subtle);
  font-family: var(--font-mono);
  font-size: 12px;
  overscroll-behavior: contain;
}

.toc-brand {
  padding: 0.4rem 1rem 1rem;
  font-weight: 700;
  font-size: 18px;
  letter-spacing: 0.06em;
  color: var(--content-primary);
}
.toc-brand span {
  display: block;
  font-weight: 400;
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--content-tertiary);
}

.toc-part {
  padding: 1rem 1rem 0.35rem;
  font-size: 10px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--content-tertiary);
  border-top: 1px solid var(--border-subtle);
  margin-top: 0.6rem;
}
.toc-part:first-of-type { border-top: 0; margin-top: 0; }

.toc ul { list-style: none; margin: 0; padding: 0; }

.toc a {
  display: flex;
  gap: 0.5rem;
  padding: 0.25rem 1rem;
  color: var(--content-secondary);
  text-decoration: none;
}
.toc a .n { color: var(--content-tertiary); min-width: 1.6rem; }
/* No underline on the contents rows: the background change is the whole hover
   signal. The generic `a:hover` rule below would otherwise add one. */
.toc a:hover {
  color: var(--content-primary);
  background: var(--bg-raised);
  text-decoration: none;
}
.toc a.current {
  color: var(--content-primary);
  background: var(--bg-raised);
  box-shadow: inset 3px 0 0 var(--accent);
}

/* ── main column ─────────────────────────────────────────────────────────── */

main {
  padding: var(--page-pad) var(--page-pad) 6rem;
  min-width: 0;
}

.chapter {
  max-width: 52rem;
  margin: 0;          /* left aligned, not centred (user, 2026-09-04) */
  padding-bottom: 3rem;
  border-bottom: 1px solid var(--border-subtle);
  scroll-margin-top: 1rem;
}
.chapter:last-child { border-bottom: 0; }

h1, h2, h3 {
  font-family: var(--font-mono);
  color: var(--content-primary);
  line-height: 1.25;
}
h1 { font-size: 30px; margin: 2.5rem 0 1rem; letter-spacing: 0.01em; }
h2 { font-size: 20px; margin: 2rem 0 0.75rem; }
h3 { font-size: 15px; margin: 1.5rem 0 0.5rem; letter-spacing: 0.04em; text-transform: uppercase; color: var(--content-secondary); }
.chapter > h1:first-child { margin-top: 0; }

p, li { color: var(--content-primary); }
strong { color: var(--content-primary); }
em { color: var(--content-secondary); }

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

h1 a.header-anchor, h2 a.header-anchor, h3 a.header-anchor {
  color: var(--content-tertiary);
  font-weight: 400;
  padding-left: 0.4rem;
}

code {
  font-family: var(--font-mono);
  font-size: 0.86em;
  padding: 0.1em 0.35em;
  border: 1px solid var(--border-subtle);
  border-radius: 3px;
  background: var(--bg-surface);
  color: var(--content-primary);
  white-space: nowrap;
}
pre {
  font-family: var(--font-mono);
  font-size: 13px;
  padding: 0.9rem 1rem;
  overflow-x: auto;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: 4px;
}
pre code { border: 0; padding: 0; background: none; white-space: pre; }

blockquote {
  margin: 1.2rem 0;
  padding: 0.2rem 0 0.2rem 1rem;
  border-left: 3px solid var(--border-accent);
  color: var(--content-secondary);
}

/* ── tables ──────────────────────────────────────────────────────────────── */

.table-scroll { overflow-x: auto; margin: 1.2rem 0; }
table {
  border-collapse: collapse;
  font-family: var(--font-mono);
  font-size: 12.5px;
  width: 100%;
}
th, td {
  border: 1px solid var(--border-subtle);
  padding: 0.35rem 0.6rem;
  text-align: left;
  vertical-align: top;
}
th {
  background: var(--bg-raised);
  color: var(--content-secondary);
  font-weight: 700;
  text-transform: uppercase;
  font-size: 10.5px;
  letter-spacing: 0.06em;
  white-space: nowrap;
}
tbody tr:nth-child(even) td { background: var(--bg-surface); }
td code { border: 0; background: none; padding: 0; }

/* ── images ──────────────────────────────────────────────────────────────── */

img {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 1.2rem 0;
  border: 1px solid var(--border-default);
  border-radius: 4px;
}
/* The diagrams are authored for a light ground on purpose, so they keep one. */
img.diagram {
  background: #ffffff;
  padding: 0.75rem;
  border-color: var(--border-subtle);
}

/* ── title page ──────────────────────────────────────────────────────────── */

.title-page { padding: 3rem 0 3.5rem; }
.title-page h1 { font-size: 56px; letter-spacing: 0.1em; margin: 0; }
.title-page .subtitle { color: var(--content-secondary); margin: 0.35rem 0 2rem; }
.title-page .edition, .title-page .copyright {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--content-tertiary);
  margin: 0.2rem 0;
}

/* ── theme picker (website only) ─────────────────────────────────────────── */

/* Pinned to the top of the reading column and always reachable, so the theme can
   be changed from anywhere in a manual this long (user, 2026-09-04). The negative
   margins let the bar span the column's padding while its content stays aligned
   with the text below it. */
.theme-pick {
  position: sticky;
  top: 0;
  z-index: 10;
  margin: calc(var(--page-pad) * -1) calc(var(--page-pad) * -1) 1.25rem;
  padding: 0.6rem var(--page-pad);
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 0.5rem;
  background: var(--bg-base);
  border-bottom: 1px solid var(--border-subtle);
  font-family: var(--font-mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--content-tertiary);
}
/* Same box as the Contents button: same font, padding, radius and border, so the
   two read as one row of controls (user, 2026-09-04). */
.theme-pick select {
  font: 700 11px var(--font-mono);
  text-transform: none;
  letter-spacing: 0.08em;
  color: var(--content-primary);
  background: var(--bg-raised);
  border: 1px solid var(--border-default);
  border-radius: 4px;
  padding: 0.45rem 0.7rem;
}
/* Inside the app the theme is CRUX's own choice, so the picker has no business
   being there. `embedded` is set by the boot script from ?embedded=1. The bar
   itself stays and carries the link to the public manual instead, at the right,
   level with the Contents button (user, 2026-10-04). */
.embedded .theme-pick label,
.embedded .theme-pick select { display: none; }
.embedded .theme-pick { justify-content: flex-end; }
.manual-site { display: none; }
.embedded .manual-site {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.15rem;
  text-transform: none;
}
.manual-site-title {
  font: 700 10px var(--font-mono);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--content-tertiary);
}
.manual-site-url {
  font: 700 11px var(--font-mono);
  color: var(--accent);
}

/* ── phone ───────────────────────────────────────────────────────────────── */

.toc-toggle {
  display: none;
  position: fixed;
  top: 0.6rem;
  left: 0.6rem;
  z-index: 20;
  font: 700 11px var(--font-mono);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--content-primary);
  background: var(--bg-raised);
  border: 1px solid var(--border-default);
  border-radius: 4px;
  padding: 0.45rem 0.7rem;
}

@media (max-width: 860px) {
  body { grid-template-columns: minmax(0, 1fr); }
  .toc-toggle { display: block; }
  .toc {
    position: fixed;
    inset: 0;
    height: 100vh;
    width: 100%;
    z-index: 15;
    display: none;
    padding-top: 3.5rem;
  }
  .toc.open { display: block; }
  main { padding: 3.2rem 1rem 4rem; }
  /* The Contents button is fixed at the left, so the picker goes to the RIGHT
     (user, 2026-09-04) and the two share the bar without colliding. */
  .theme-pick {
    margin: -3.2rem -1rem 1.25rem;
    padding: 0.6rem 1rem;
    justify-content: flex-end;
  }
}

/* ── print / PDF ─────────────────────────────────────────────────────────── */

/* Printing a themed page straight from a browser gives light text on white and
   clipped tables, so print gets its own pass. Both the modern `break-*` and the
   legacy `page-break-*` properties are written: Safari still honours the old
   names in places. */

@media print {

  /* Ink on paper. Redefining the fourteen theme tokens re-colours the WHOLE page
     from one place, because every rule above already draws from them. The
     `[data-theme]` selector matches the attribute the boot script stamps on
     <html>, so this wins over themes.css whichever theme the reader had on, and
     `!important` keeps it winning if the two files are ever reordered. */
  :root, :root[data-theme] {
    --bg-base:         #ffffff !important;
    --bg-surface:      #f5f5f5 !important;
    --bg-raised:       #ebebeb !important;
    --bg-overlay:      #ffffff !important;
    --bg-header:       #ffffff !important;
    --border-subtle:   #c8c8c8 !important;
    --border-default:  #ababab !important;
    --border-strong:   #8e8e8e !important;
    --border-accent:   #8e8e8e !important;
    --border-separator:#c8c8c8 !important;
    --content-primary: #000000 !important;
    --content-secondary:#2e2e2e !important;
    --content-tertiary:#555555 !important;
    --accent:          #000000 !important;
  }

  /* Belt and braces under the token override. A browser holding a CACHED copy of
     an older stylesheet, or any rule that ever hard-codes a colour, would print
     the dark palette's near-white text onto white paper and be unreadable. Paper
     is always white, so print forces ink and drops the grey hierarchy with it. */
  body, main, .chapter, .toc, .toc-part, .toc-brand, .toc-brand span,
  h1, h2, h3, h4, p, li, td, th, dt, dd,
  strong, em, span, div, a, code, pre, blockquote {
    color: #000000 !important;
  }
  body, main, .chapter, .toc, p, li, blockquote, table, td {
    background-color: #ffffff !important;
  }
  code, pre { background-color: #f5f5f5 !important; }

  @page { margin: 16mm 14mm 18mm; }

  html, body { background: #ffffff; }
  /* Front matter in book order: title page, contents, then the chapters. The
     document has the sidebar BEFORE <main>, which is right for the screen and
     backwards on paper, so print turns the body into a flex column and reorders.
     `display: contents` on <main> lifts the sections up to be flex items too,
     which is what lets the title page overtake the contents. Same-order items
     keep document order, so the chapters stay in sequence. */
  body {
    display: flex;
    flex-direction: column;
    font-size: 10.5pt;
    line-height: 1.5;
  }
  main { display: contents; }
  .chapter { order: 3; }
  .toc { order: 2; }
  .chapter.title-page { order: 1; }   /* beats `.chapter` above on specificity */

  /* Screen chrome with no meaning on paper. */
  .toc-toggle,
  .theme-pick,
  .header-anchor { display: none !important; }

  /* The sidebar becomes the contents page. */
  .toc {
    position: static;
    display: block;
    width: auto;
    height: auto;
    overflow: visible;
    padding: 0;
    background: none;
    border: 0;
    font-size: 9pt;
    columns: 2;
    column-gap: 12mm;
    break-before: page;
    page-break-before: always;
    break-after: page;
    page-break-after: always;
  }
  .toc-brand {
    padding: 0 0 0.8rem;
    font-size: 16pt;
    column-span: all;
    -webkit-column-span: all;
  }
  .toc-part {
    padding: 0.7rem 0 0.25rem;
    margin-top: 0.5rem;
    break-after: avoid;
    page-break-after: avoid;
  }
  .toc ul { margin-bottom: 0.2rem; }
  .toc a {
    display: block;
    padding: 0.08rem 0;
    color: #000000;
    break-inside: avoid;
    page-break-inside: avoid;
  }
  .toc a .n { display: inline-block; min-width: 1.9em; }
  .toc a.current { background: none; box-shadow: none; }

  /* One chapter per page, and never a heading stranded at the foot of one. */
  .chapter {
    max-width: none;
    padding-bottom: 0;
    border-bottom: 0;
    break-before: page;
    page-break-before: always;
  }
  .chapter:first-of-type {          /* the title page */
    break-before: avoid;
    page-break-before: avoid;
    padding: 0 0 2rem;
    break-after: page;
    page-break-after: always;
  }
  .title-page h1 { font-size: 40pt; }

  h1, h2, h3 {
    break-after: avoid;
    page-break-after: avoid;
    break-inside: avoid;
  }
  h1 { font-size: 19pt; margin: 0 0 0.8rem; }
  h2 { font-size: 13.5pt; margin: 1.4rem 0 0.5rem; }
  h3 { font-size: 11pt; margin: 1rem 0 0.35rem; }
  p, li { orphans: 3; widows: 3; }

  /* Nothing may be clipped on paper: the screen scroll boxes have to open up. */
  .table-scroll { overflow: visible; }
  pre {
    overflow: visible;
    white-space: pre-wrap;
    word-wrap: break-word;
    font-size: 8.5pt;
  }
  code { white-space: normal; overflow-wrap: break-word; }

  /* A table must fit the paper. `width: 100%` cannot shrink a table below its
     MIN-CONTENT width, so the two `nowrap` rules the screen uses have to go or a
     wide table runs off the right edge. */
  /* `calc(100% - 2pt)`: with `border-collapse: collapse` the outer border sits
     half outside the box, and at exactly 100% that half falls on the page edge
     and is clipped, so the right-hand rule goes missing. 2pt is invisible. */
  table { font-size: 8.5pt; width: calc(100% - 2pt); table-layout: fixed; }
  th { white-space: normal; }
  /* `anywhere`, not `break-word`: only `anywhere` also shrinks the cell's
     MIN-CONTENT width, which is what lets `width: 100%` actually hold. */
  td, th { overflow-wrap: anywhere; }
  td code, th code { white-space: normal; }
  thead { display: table-header-group; }   /* repeat the header on every page */
  tr, img, pre, blockquote {
    break-inside: avoid;
    page-break-inside: avoid;
  }
  /* The zebra stripe costs toner and earns nothing once the row borders print. */
  tbody tr:nth-child(even) td { background: none; }
  th {
    background-color: #ebebeb !important;   /* beats the blanket white above */
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  /* A screenshot should illustrate a page, not consume one. Width and height stay
     auto so the max constraints scale it without distorting the aspect ratio. */
  img { max-height: 105mm; max-width: 100%; width: auto; height: auto; }
  img.diagram {
    background: #ffffff;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  /* An internal anchor is dead on paper, so it prints as plain text. An external
     one is still usable, so its address is printed after it. */
  a { color: #000000; text-decoration: none; }
  a[href^="http"]::after {
    content: " (" attr(href) ")";
    font-family: var(--font-mono);
    font-size: 0.85em;
    overflow-wrap: break-word;
  }
}
