/* print.css — §10 print rules. Forces the light palette, hides chrome (TOC, banner,
   code copy buttons) and avoids awkward page breaks. Applies both to real printing
   (`@media print`) and to the in-app print preview the host drives via the `printMode`
   message (`:root[data-print]`), which main.js sets before Mermaid re-renders light and
   before invoking the OS print/PDF action. */

@media print {
  /* Both selectors, and the second is the one that does the work.
     `:root` alone is (0,1,0) and tokens.css sets the dark palette on `:root[data-theme="dark"]`, which is (0,2,0).
     A media query carries no specificity, so for years a browser print of a dark page kept every dark value on
     white paper: body text came out at 1.18:1 and a heading at 2.95:1, which is a blank sheet with a hint of
     colour on it. The in-app Print button never showed it, because main.js sets `data-print` first and the
     `:root[data-print]` block below matches that weight — but Ctrl+P does not go through main.js.
     `:root[data-theme]` matches the weight, and print.css is imported last, so it wins. */
  :root,
  :root[data-theme] {
    color-scheme: light;
    --mdr-bg: #FFFFFF;
    --mdr-fg: #1F2328;
    --mdr-fg-muted: #59636E;
    --mdr-border: #D1D9E0;
    --mdr-bg-subtle: #F6F8FA;
    --mdr-accent: #0969DA;
    --mdr-alert-note: #0969DA;
    --mdr-alert-tip: #1A7F37;
    --mdr-alert-important: #8250DF;
    --mdr-alert-warning: #9A6700;
    --mdr-alert-caution: #CF222E;

    /* The §14 structure colors are literal per-theme hex, so a page printed
       while the app is in dark mode would otherwise keep dark-mode hues (some
       of which do not clear 4.5:1 on white, e.g. the dark heading-blue is
       ~3:1). Force the already-verified, print-safe Light+ values. */
    --mdr-h1: #0451A5;
    --mdr-h2: #AF00DB;
    --mdr-h3: #267F99;
    --mdr-h4: #795E26;
    --mdr-h5: #A31515;
    --mdr-h6: #001080;
    --mdr-strong: #AF00DB;
    --mdr-em: #008000;
    --mdr-del: #CF222E;
    --mdr-code-fg: #A31515;
    --mdr-code-bg: #EFE6E8;
    --mdr-quote-fg: #008000;
    --mdr-quote-border: #008000;
    --mdr-list-marker: #0000FF;
    --mdr-link: #0969DA;
    --mdr-table-head-fg: #1B5E73;
    --mdr-hr: #ABB2E6;
  }

  /* The JSON view's own palette, on a selector that can actually win. The `:root` block above
     is outweighed by tokens.css's `:root[data-theme="dark"]` — a media query carries no
     specificity — so a dark-mode browser print keeps the dark values for everything it lists.
     The in-app preview escapes that through `[data-print]` further down, but Ctrl+P in the web
     version does not, and #D9C7FF keys on white paper are not a printout. An attribute selector
     matches that weight and comes later, so this is the rule that wins. These are tokens.css's
     light values, each measured at 4.5:1 or better on #FFFFFF. */
  :root[data-theme] {
    --mdr-json-depth-1: #63134F;
    --mdr-json-depth-2: #59187D;
    --mdr-json-depth-3: #4300B0;
    --mdr-json-depth-4: #0C00E6;
    --mdr-json-depth-5: #005487;
    --mdr-json-depth-6: #006B74;
    --mdr-json-depth-7: #298060;
    --mdr-json-depth-8: #538100;
    --mdr-json-string: #007562;
    --mdr-json-number: #2A67BD;
    --mdr-json-boolean: #A94E00;
    --mdr-json-ink: #1F2328;
    --mdr-json-quiet: #59636E;
    --mdr-json-card: #F6F8FA;
    --mdr-json-rule: #D1D9E0;
    --mdr-json-alarm: #CF222E;
    --mdr-json-note: #0969DA;
  }

  body {
    height: auto;
    overflow: visible;
    background: #FFFFFF;
  }

  .mdr-layout {
    display: block;
  }

  .mdr-banner,
  .mdr-toc,
  .mdr-toc-backdrop,
  .mdr-code-toolbar,
  .mdr-diagram-tools,
  .mdr-popover,
  /* The strip is a copy of a heading that is elsewhere on the page anyway, and on paper there
     is no scrolling for it to keep up with. */
  .mdr-sticky,
  .mdr-fold-toggle {
    display: none !important;
  }

  /* A long front-matter list is collapsed on screen, but a printout wants all of it. main.js
     opens these while the host's print mode is on, which covers the engines this selector — a
     recent Chromium addition — doesn't reach. */
  .markdown-frontmatter-more::details-content {
    content-visibility: visible !important;
  }

  /* Same again for a section the reader folded: printing is asking for the document, not for
     the shape they happened to have it in while they read it. */
  .mdr-fold::details-content {
    content-visibility: visible !important;
  }

  /* And once more for a JSON document, which is nothing but nested <details>: the renderer
     opens three levels and collapses the rest, so printing it as it stands would put a page of
     `{7 keys}` summaries on paper instead of the document. */
  .json-node::details-content {
    content-visibility: visible !important;
  }

  /* With every branch open the twisty says nothing — it is a control, and paper has none. */
  .markdown-body .json-doc .json-summary {
    list-style: none;
    break-inside: avoid;
    break-after: avoid;
  }

  /* `pre` becomes `pre-wrap` below, which is right for a long line of source and wrong for the
     excerpt: its second line is a caret under a character of its first, and wrapping moves one
     without the other. json.css already outweighs every `pre-wrap` rule here, so the block keeps
     `white-space: pre` on paper without help; what it needs from print is to shrink, because a
     page has no scrollbar and the excerpt can be 122 characters wide. A very long one is still
     cut at the page edge — a caret pointing at the wrong character would be worse. */
  .markdown-body .json-doc .json-error-context {
    font-size: 0.7em;
  }

  /* The control itself is chrome: by the time this matters the list is already open. */
  .markdown-frontmatter-more > summary {
    display: none;
  }

  .mdr-main {
    overflow: visible;
    height: auto;
  }

  .markdown-body {
    max-width: none;
    margin: 0;
    padding: 0;
  }

  /* Top-level task-list checkboxes are positioned with a `margin-left: -1.6em`
     pull (markdown.css) that borrows its "gutter" from .markdown-body's own
     left padding — zeroed above for print. Without this, the pull lands at a
     negative x and the checkbox falls off the page edge entirely (nested task
     lists are unaffected: they already carry their own `padding-left: 1.5em`,
     independent of the article's padding). Give the top-level list back just
     enough room to stay self-contained. */
  .markdown-body ul.contains-task-list {
    padding-left: 1.6em;
  }

  pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }

  pre,
  table,
  figure,
  .markdown-alert {
    break-inside: avoid;
  }
}

:root[data-print] {
  color-scheme: light;
  --mdr-bg: #FFFFFF;
  --mdr-fg: #1F2328;
  --mdr-fg-muted: #59636E;
  --mdr-border: #D1D9E0;
  --mdr-bg-subtle: #F6F8FA;
  --mdr-accent: #0969DA;
  --mdr-alert-note: #0969DA;
  --mdr-alert-tip: #1A7F37;
  --mdr-alert-important: #8250DF;
  --mdr-alert-warning: #9A6700;
  --mdr-alert-caution: #CF222E;

  /* Same fix as the @media print rule above, for the in-app print preview (and
     the real print path, which sets [data-print] before invoking OS print).
     This rule has matching specificity to tokens.css's `:root[data-theme="dark"]`
     block and comes later, so it is the one that actually wins the cascade for
     a dark-mode print. */
  --mdr-h1: #0451A5;
  --mdr-h2: #AF00DB;
  --mdr-h3: #267F99;
  --mdr-h4: #795E26;
  --mdr-h5: #A31515;
  --mdr-h6: #001080;
  --mdr-strong: #AF00DB;
  --mdr-em: #008000;
  --mdr-del: #CF222E;
  --mdr-code-fg: #A31515;
  --mdr-code-bg: #EFE6E8;
  --mdr-quote-fg: #008000;
  --mdr-quote-border: #008000;
  --mdr-list-marker: #0000FF;
  --mdr-link: #0969DA;
  --mdr-table-head-fg: #1B5E73;
  --mdr-hr: #ABB2E6;

  /* See the @media print rule above: the JSON view's whole palette, forced to the values that
     were measured on white. */
  --mdr-json-depth-1: #63134F;
  --mdr-json-depth-2: #59187D;
  --mdr-json-depth-3: #4300B0;
  --mdr-json-depth-4: #0C00E6;
  --mdr-json-depth-5: #005487;
  --mdr-json-depth-6: #006B74;
  --mdr-json-depth-7: #298060;
  --mdr-json-depth-8: #538100;
  --mdr-json-string: #007562;
  --mdr-json-number: #2A67BD;
  --mdr-json-boolean: #A94E00;
  --mdr-json-ink: #1F2328;
  --mdr-json-quiet: #59636E;
  --mdr-json-card: #F6F8FA;
  --mdr-json-rule: #D1D9E0;
  --mdr-json-alarm: #CF222E;
  --mdr-json-note: #0969DA;
}

:root[data-print] body {
  height: auto;
  overflow: visible;
  background: #FFFFFF;
}

:root[data-print] .mdr-layout {
  display: block;
}

:root[data-print] .mdr-banner,
:root[data-print] .mdr-toc,
:root[data-print] .mdr-toc-backdrop,
:root[data-print] .mdr-code-toolbar,
:root[data-print] .mdr-diagram-tools,
:root[data-print] .mdr-popover,
:root[data-print] .mdr-sticky,
:root[data-print] .mdr-fold-toggle {
  display: none !important;
}

:root[data-print] .markdown-frontmatter-more > summary {
  display: none;
}

/* See the @media print rule above: a folded section is still part of the document. */
:root[data-print] .mdr-fold::details-content {
  content-visibility: visible !important;
}

/* And a collapsed JSON branch is still part of the document it belongs to. */
:root[data-print] .json-node::details-content {
  content-visibility: visible !important;
}

:root[data-print] .markdown-body .json-doc .json-summary {
  list-style: none;
  break-inside: avoid;
  break-after: avoid;
}

/* See the @media print rule above: the excerpt shrinks so that more of a long line reaches the
   page, and keeps `white-space: pre` from json.css so the caret keeps pointing at its own
   character. */
:root[data-print] .markdown-body .json-doc .json-error-context {
  font-size: 0.7em;
}

:root[data-print] .mdr-main {
  overflow: visible;
  height: auto;
}

:root[data-print] .markdown-body {
  max-width: none;
  margin: 0;
  padding: 0;
}

/* See the @media print rule above: same fix, for the in-app print preview. */
:root[data-print] .markdown-body ul.contains-task-list {
  padding-left: 1.6em;
}

:root[data-print] pre {
  white-space: pre-wrap;
  word-wrap: break-word;
}

:root[data-print] pre,
:root[data-print] table,
:root[data-print] figure,
:root[data-print] .markdown-alert {
  break-inside: avoid;
}
