/* Website brand tokens: web-base.css defines the --mg-* palette and fonts;
   these map the theme's variables onto them. Text in the accent reads
   web-base's --mg-ochre-text, AA on every surface in both themes; the brand
   --mg-ochre is for fills, borders and rules only (Magma-Maths/web-base#80). */

:root,
html[data-theme="light"] {
  /* Fonts */
  --pst-font-family-base: var(--mg-font-sans);
  --pst-font-family-heading: var(--mg-font-sans);
  --pst-font-family-monospace: var(--mg-font-mono);

  /* Sampled from the Magma logo's triangle/chevrons (MagmaLogoOfficial.png).
     --mh- is the Handbook's own prefix; --mg- means web-base's (#2893). */
  --mh-logo-red: #A93E28;

  /* The secondary accents (--mg-purple, --mg-teal, --mg-amber and their
     -text variants) are web-base's, for its citation cards and here. */

  --pst-color-primary-bg: rgba(230, 71, 37, 0.12);
  --pst-color-secondary-bg: rgba(200, 63, 30, 0.12);

  /* Match the main Magma website's compact header. */
  --pst-header-height: 60px;
}

html[data-theme="dark"] {
  /* web-base keeps the brand --mg-ochre as the fixed brand coral in both
     themes; only --mg-ochre-text follows the dark palette. */
  --mh-logo-red: var(--mg-ochre-text);

  --pst-color-primary-bg: color-mix(in srgb, var(--mg-ochre-text) 18%, transparent);
  --pst-color-secondary-bg: color-mix(in srgb, var(--mg-ochre-text) 18%, transparent);
}

:root,
html[data-theme="light"],
html[data-theme="dark"] {
  /* Backgrounds */
  --pst-color-background: var(--mg-bg);
  --pst-color-on-background: var(--mg-white);
  --pst-color-surface: var(--mg-sandstone);

  /* Text */
  --pst-color-text-base: var(--mg-text);
  --pst-color-text-muted: var(--mg-text-muted);
  --pst-color-heading: var(--mg-black);
  /* The theme's h1-h4 read this first, and fall back to the text colour
     rather than to --pst-color-heading when it is unset. */
  --pst-heading-color: var(--pst-color-heading);
  /* sphinx-book-theme colors h1-h5/captions via this variable instead of
     --pst-color-heading (see _typography.scss / _sidebar-secondary.scss) */
  --pst-color-muted: var(--mg-black);

  /* Borders */
  --pst-color-border: var(--mg-border);
  --pst-color-border-muted: var(--mg-border);

  /* Accent / links. The theme paints all of these as text (headerlinks,
     current nav items and tabs, hovered nav links). Hover keeps the colour:
     web-base's --mg-ochre-hover is short of AA as text, and the theme
     thickens a hovered link's underline, which marks it without colour. */
  --pst-color-primary: var(--mg-ochre-text);
  --pst-color-secondary: var(--mg-ochre-text);
  --pst-color-link: var(--mg-ochre-text);
  --pst-color-link-hover: var(--mg-ochre-text);
  /* Muted (not ochre) so plain inline code doesn't read as a link; theme
     default here is a leftover purple (#912583) unrelated to the brand. */
  --pst-color-inline-code: var(--pst-color-text-muted);
  /* The colour of inline code inside a link. */
  --pst-color-link-higher-contrast: var(--mg-ochre-text);

  /* Keyboard focus rings (search box, nav links, buttons, cards, copy
     button, toctree toggles): theme default here is a leftover magenta
     (#c132af) unrelated to the brand. */
  --pst-color-accent: var(--mg-ochre-text);
  --pst-color-accent-bg: var(--pst-color-primary-bg);
}

/* The header is web-base's own (handbook-header.html, placed by layout.html
   without the theme's header class), so web-base's rules draw it, its
   background, edge and stacking included. */

/* --pst-header-height (60px) is the header row alone, so the theme's sticky
   sidebars sat under the header (#383): offset them by --mg-header-height,
   which web-base builds from the row, its padding and its border. Below
   960px the theme's drawer sets its own top. magma-parameter.css offsets the
   secondary sidebar itself, as its !important rules would beat this one. */
@media (min-width: 960px) {
  .bd-sidebar-primary {
    top: var(--mg-header-height);
    max-height: calc(100vh - var(--mg-header-height));
  }
}

/* On a short viewport web-base's header scrolls away (its 0.7.12), so the
   theme's sticky sidebar, which sat under the header, takes the height. */
@media (min-width: 960px) and (max-height: 30em) {
  .bd-sidebar-primary {
    top: 0;
    max-height: 100vh;
  }
}

/* The loaded nav's math is typeset after the article's; hide its raw TeX
   ("\(KG\)-Modules") until then (#2949). global-sidebar-nav.js marks it
   pending until MathJax is done with it, or has not come at all. */
.magma-global-sidebar-nav[data-math-pending] .math {
  visibility: hidden;
}

/* The "On this page" toggle, the handbook's control in web-base's search
   slot: its icon takes web-base's 15px, not the text size, and the theme's
   rule hiding it from 1200px, where the panel is a column, is written for
   the theme's own header. */
.mg-site-header button.secondary-toggle {
  font-size: 15px;
}

@media (min-width: 1200px) {
  .mg-site-header button.secondary-toggle {
    display: none;
  }
}

/* A link around code is the link colour, as a link around text is. The theme
   gives it its own teal, so the name in a written call, FreeGroup(n), whose
   link sits inside the code, would otherwise differ from a bare reference.
   A literal joined to a formula has a run between it and the link. */
.bd-article a > code,
.bd-article a > .handbook-math-run > code {
  color: var(--pst-color-link);
}
.bd-article a:hover > code,
.bd-article a:hover > .handbook-math-run > code {
  color: var(--pst-color-link-hover);
}

/* Keep the link's native underline continuous through MathJax glyphs. */
.bd-article a.reference:has(.math),
.bd-article a.reference:has(.math) .math {
  text-decoration-skip-ink: none;
}

/* "Hide Search Matches" toggle (issue #389). pydata-sphinx-theme renders
   it into #searchbox -- which sits *before* the <article>, above the H1 --
   and styles it as a full-size coral button with a Font Awesome icon. It
   only appears when a page is opened with a ?highlight= query (the rebuilt
   handbook search no longer emits those, but bookmarked/external links
   still can). Demote it to a quiet inline text link so it never upstages
   the page title. Selectors match the theme's own
   `div#searchbox p.highlight-link a` specificity. */
div#searchbox p.highlight-link {
  box-shadow: none !important;
  width: auto;
  margin: 0 0 1rem !important;
}
div#searchbox p.highlight-link a {
  background: none;
  color: var(--pst-color-primary);
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0;
  border-radius: 0;
  box-shadow: none !important;
  text-decoration: underline;
}
div#searchbox p.highlight-link a:hover {
  box-shadow: none !important;
  text-decoration: none;
}
div#searchbox p.highlight-link a:before {
  content: "";
  margin: 0;
}

/* Matches the page tree's drawer (below): under the bar, with its scrim and
   2px edge, and no sideways pan. The padding keeps the active bar and focus
   ring the theme draws 1rem left of a link's text inside the clip, and puts
   the text 1.25rem in, level with the left drawer's header links. */
@media (max-width: 1199.98px) {
  #pst-secondary-sidebar-modal {
    border-left: 2px solid var(--mg-text-muted);
    bottom: 0;
    height: auto;
    margin-block: 0;
    max-height: none;
    overflow-x: hidden;
    overscroll-behavior: contain;
    padding-left: 0.75rem;
    top: var(--mg-header-height);
  }

  /* The ::backdrop inherits --mg-header-height from the dialog, so the scrim
     starts under the bar. A click on the bar lands on it and closes the drawer. */
  #pst-secondary-sidebar-modal::backdrop {
    background: linear-gradient(
      to bottom,
      transparent var(--mg-header-height),
      rgba(0, 0, 0, 0.45) var(--mg-header-height)
    );
  }
}

/* Forced colours drop a gradient, and with it the scrim. Keep it, in the
   colour they give the page tree's scrim: Canvas at the same 0.45. */
@media (max-width: 1199.98px) and (forced-colors: active) {
  #pst-secondary-sidebar-modal::backdrop {
    background: linear-gradient(
      to bottom,
      transparent var(--mg-header-height),
      color-mix(in srgb, Canvas 45%, transparent) var(--mg-header-height)
    );
    forced-color-adjust: none;
  }
}

/* The page tree's drawer is 20rem (85vw at most). Match it below 56.25em,
   past the theme's 350px cap, which a large text size reaches. Above, keep
   the theme's 350px, unless a large text size makes 20rem wider, so the
   drawer does not shrink as the page widens past 56.25em. */
@media (max-width: 1199.98px) {
  #pst-secondary-sidebar-modal {
    max-width: max(350px, 20rem);
  }
}

@media (width < 56.25em) {
  #pst-secondary-sidebar-modal {
    width: min(20rem, 85vw);
  }
}

/* The close button sidebar-dialog-close.js adds for the keyboard and screen
   readers, web-base's icon button shown only while it has focus, in the
   panel's top padding. The button and its focus ring do not scale with the
   text, so the theme's 2rem padding grows where it is too short for them. */
#pst-secondary-sidebar-modal {
  padding-top: max(2rem, calc(0.5rem + 32px + 6px));
}

#pst-secondary-sidebar-modal > .handbook-dialog-close {
  position: absolute;
  right: 0.5rem;
  top: 0.5rem;
}

/* The bar's toggle turns into a cross while the drawer is open, as the
   hamburger does for the page tree's. Font Awesome's script has replaced the
   icon's span with an SVG, so hide that and draw the cross. */
.mg-site-header button.secondary-toggle[aria-expanded="true"] > :is(svg, .fa-outdent) {
  display: none;
}

.mg-site-header button.secondary-toggle[aria-expanded="true"]::after {
  background: currentColor;
  content: "";
  height: 1em;
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round'%3E%3Cpath d='M18 6 6 18M6 6l12 12'/%3E%3C/svg%3E") center / contain no-repeat;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round'%3E%3Cpath d='M18 6 6 18M6 6l12 12'/%3E%3C/svg%3E") center / contain no-repeat;
  width: 1em;
}

/* Forced colours replace the background the mask shows with the page's. */
@media (forced-colors: active) {
  .mg-site-header button.secondary-toggle[aria-expanded="true"]::after {
    background: ButtonText;
    forced-color-adjust: none;
  }
}

/* -- The page tree in the header's drawer -----------------------------------
   Web-base's drawer serves below 960px, or 60em at a large text size, where
   the tree becomes a column (handbook-header.html sets the reach;
   handbook-drawer.js moves the tree into the drawer slot, after the links).
   Without JavaScript the tree is out of reach there, its column hidden, as
   with the theme's drawer. */
@media (width < 960px), (width < 60em) {
  body:not(.mg-no-js) .handbook-drawer-tree {
    border-top: 2px solid var(--mg-ochre);
    margin-top: 0.5rem;
    padding: 0.75rem 1.25rem 0;
  }
}

/* The rule over the tree: Magma's ochre in light mode, Gruvbox's orange in
   dark (web-base keeps the brand ochre fixed there, and its --mg-ochre-text
   is the Gruvbox orange). */
html[data-theme="dark"] .handbook-drawer-tree {
  border-top-color: var(--mg-ochre-text);
}

/* The page tree in the drawer. The theme styles a tree's branches and
   toggles under .bd-sidebar-primary, which the drawer is not (the scripts
   that take the first one on the page must not find it here): restated
   from the theme's rules for it. */
.handbook-drawer-tree {
  font-size: var(--pst-sidebar-font-size-mobile);
}

.handbook-drawer-tree .list-caption {
  list-style: none;
  padding-left: 0;
}

.handbook-drawer-tree li.has-children {
  position: relative;
}

.handbook-drawer-tree li.has-children .caption,
.handbook-drawer-tree li.has-children > .reference {
  margin-right: calc(30px + 0.1875rem);
  padding-top: 0.25rem;
}

.handbook-drawer-tree li.has-children > details > summary {
  list-style: none;
}

.handbook-drawer-tree li.has-children > details > summary::-webkit-details-marker {
  display: none;
}

.handbook-drawer-tree li.has-children > details > summary:focus-visible {
  outline: none;
}

.handbook-drawer-tree li.has-children > details > summary:focus-visible > .toctree-toggle {
  outline: var(--pst-color-accent) solid 0.1875rem;
  outline-offset: -0.1875rem;
}

.handbook-drawer-tree li.has-children > details > summary .toctree-toggle {
  align-items: center;
  cursor: pointer;
  display: inline-flex;
  height: 30px;
  justify-content: center;
  position: absolute;
  right: 0;
  top: 0;
  width: 30px;
}

.handbook-drawer-tree li.has-children > details > summary .toctree-toggle .fa-chevron-down {
  font-size: 0.75rem;
}

.handbook-drawer-tree li.has-children > details[open] > summary .fa-chevron-down {
  transform: rotate(180deg);
}

/* Anchor the handbook navigation to the browser's left edge on wide screens. */
@media (min-width: 960px) {
  .bd-container {
    justify-content: flex-start;
    /* Containing block for the two .magma-sidebar-rail elements
       (left-sidebar-collapse.js / collapsible-right-toc.js) -- the edge
       tabs are absolutely positioned against it so they span the full
       article height and stay clear of any sidebar's own overflow clip. */
    position: relative;
  }

  .bd-sidebar-primary {
    padding-top: 0.75rem;
  }

  .bd-sidebar-primary .sidebar-primary-items__start .sidebar-primary-item:first-of-type {
    padding-top: 0;
  }
}

/* Below 960px, or 60em at a large text size, the page tree is in the header's
   drawer (handbook-drawer.js), and the theme's static sidebar column is empty
   (issue #120). 60em is 960px at the usual 16px; any sooner, a large text
   size would leave the article a strip (30 characters at 960px at 32px). */
@media (width < 960px), (width < 60em) {
  #pst-primary-sidebar {
    display: none;
  }
}

/* ── Sidebar hide/reveal (issue #409) ────────────────────────────────────
   The desktop hide/reveal for both sidebars: the grid tracks in
   magma-parameter.css read --magma-*-sidebar-w, and the two scripts tween
   that length between its open value and 0. Transitioning the registered
   custom property (rather than grid-template-columns directly) is what
   makes the columns animate smoothly -- the tracks just re-resolve the
   var() each frame. .magma-sidebar-anim gates it on so setting the initial
   width at load doesn't itself animate. */
html.magma-sidebar-anim {
  transition:
    --magma-primary-sidebar-w 0.18s ease,
    --magma-secondary-sidebar-w 0.18s ease;
}

/* A grid track at 0 still can't squeeze out the sidebar's own 16px edge
   padding (or the right panel's 1px border), so the collapsed column keeps
   a ~33px stub with a sliver of clipped nav text showing. Drop the padding
   and edge border with the same 0.18s timing as the width so the column
   truly closes to nothing. */
html.magma-sidebar-anim #pst-primary-sidebar,
html.magma-sidebar-anim #pst-secondary-sidebar {
  transition: padding 0.18s ease, border-width 0.18s ease;
}

html.left-sidebar-collapsed #pst-primary-sidebar,
html.right-toc-collapsed #pst-secondary-sidebar {
  border-left-width: 0 !important;
  border-right-width: 0 !important;
  padding-left: 0 !important;
  padding-right: 0 !important;
}

/* Fade the nav contents slightly ahead of the track closing, so the eye
   reads a panel sliding away rather than a list re-wrapping as it's
   squeezed. */
html.magma-sidebar-anim #pst-primary-sidebar > *,
html.magma-sidebar-anim #pst-secondary-sidebar > * {
  transition: opacity 0.12s ease;
}

html.left-sidebar-collapsed #pst-primary-sidebar > *,
html.right-toc-collapsed #pst-secondary-sidebar > * {
  opacity: 0;
}

@media (prefers-reduced-motion: reduce) {
  html.magma-sidebar-anim #pst-primary-sidebar,
  html.magma-sidebar-anim #pst-secondary-sidebar,
  html.magma-sidebar-anim #pst-primary-sidebar > *,
  html.magma-sidebar-anim #pst-secondary-sidebar > * {
    transition: none;
  }
}

/* The reopen/close control: a slim tab permanently docked to the sidebar's
   own inner (content-facing) edge, protruding out past it into the
   article/gutter space. As --magma-*-sidebar-w tweens, the rail's
   left/right position reads that same live (already-animating, see above)
   value each frame, exactly like the grid track does -- so the tab rides
   along with the sidebar's edge as it slides, rather than sitting fixed
   while the sidebar moves under it. Collapsed (width 0) that edge is the
   window edge, so the tab ends up flush there; open, it sits right where
   the sidebar's own edge stops. Shared by left-sidebar-collapse.js and
   collapsible-right-toc.js; the rail is an absolutely-positioned,
   full-height strip so the tab clears the sidebar's overflow clip and a
   sticky offset keeps it near the middle of the viewport without drifting
   past the footer. */
.magma-sidebar-rail {
  position: absolute;
  top: 0;
  bottom: 0;
  /* WCAG 2.5.8's 24px minimum target width. */
  width: 24px;
  z-index: 30;
  pointer-events: none;
}

.magma-sidebar-rail--left {
  left: var(--magma-primary-sidebar-w);
}

.magma-sidebar-rail--right {
  right: var(--magma-secondary-sidebar-w);
}

/* The left tab, 24px wide, can show from 960px, but the theme pads the
   article 2rem on the left only from 1200px, so in between the text would sit
   under the tab. The theme's rule, from 960px. */
@media (min-width: 960px) {
  .bd-main .bd-content .bd-article-container .bd-article {
    padding-left: 2rem;
  }
}

.magma-sidebar-tab {
  align-items: center;
  background: var(--pst-color-on-background);
  border: 1px solid var(--pst-color-border);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
  color: var(--pst-color-text-muted);
  cursor: pointer;
  display: flex;
  height: 54px;
  justify-content: center;
  margin: 0;
  padding: 0;
  pointer-events: auto;
  position: sticky;
  /* Tracks the viewport middle while scrolling through the article, then
     rides up with the rail near the footer (the rail ends where the
     article does) -- not position: fixed, which would keep it pinned over
     the footer after the sidebars themselves have scrolled away. */
  top: calc(50vh - 27px);
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  width: 100%;
}

/* No border/shadow on the side touching the sidebar's own edge, so the tab
   reads as growing directly out of it rather than floating just beside it. */
.magma-sidebar-rail--left .magma-sidebar-tab {
  border-left: 0;
  border-radius: 0 7px 7px 0;
}

.magma-sidebar-rail--right .magma-sidebar-tab {
  border-right: 0;
  border-radius: 7px 0 0 7px;
}

.magma-sidebar-tab:hover {
  background: var(--pst-color-surface);
  border-color: var(--mg-ochre);
  color: var(--mg-ochre-text);
}

.magma-sidebar-tab span {
  font-size: 11px;
  line-height: 1;
  pointer-events: none;
}

/* The tab sits at the middle of the viewport, but the right panel's edge
   is only as tall as its own list, so on a page with a heading or two the
   edge stopped above the tab and the tab floated (#2710). Keep the panel
   at least tall enough for its edge to pass under the tab's lower corner:
   the tab's bottom is 50vh + 27px down the viewport, and the panel starts
   at the header offset magma-parameter.css gives it (top: 0 on a short
   viewport). The max-height there still caps a long list. */
@media (min-width: 1200px) {
  #pst-secondary-sidebar {
    min-height: calc(50vh + 27px - var(--mg-header-height));
  }
}

@media (min-width: 1200px) and (max-height: 30em) {
  #pst-secondary-sidebar {
    min-height: calc(50vh + 27px);
  }
}

/* A sidebar that is a drawer, not an in-flow column, has nothing for its tab
   to collapse: the page tree's below 960px or 60em, the on-this-page panel's
   below 1200px. */
@media (width < 960px), (width < 60em) {
  .magma-sidebar-rail--left {
    display: none;
  }
}

@media (max-width: 1199.98px) {
  .magma-sidebar-rail--right {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  html.magma-sidebar-anim {
    transition: none;
  }

  .magma-sidebar-tab {
    transition: none;
  }
}

/* Headings: match the website's weight + tracking on h1-h3. The article's
   own, not those inside an element with an mg- or pf- class (web-base's
   parts and its search package's: the notes' h3, the results page's h2),
   which set their own. A class on body is the page's state, not a part. */
.bd-article-container :is(h1, h2, h3):not(body :is([class^="mg-"], [class*=" mg-"], [class^="pf-"], [class*=" pf-"]) *) {
  font-family: var(--pst-font-family-heading);
  font-weight: 600;
  letter-spacing: -0.01em;
}

/* Reduced text spacing (issue #38): theme defaults are loose
   (line-height: 1.65, p margin-bottom: 1.15rem), leaving large gaps within
   and between paragraphs/text blocks. Tighten to a more compact reading
   density, scoped to the article body so sidebar/nav chrome is untouched. */
.bd-article-container {
  line-height: 1.35;
}

/* Paragraph gap (issue #53): 0.6rem still read as a much bigger jump than
   the tightened in-paragraph line gap above. Bring it down further so
   consecutive paragraphs feel like one reading rhythm. Not inside an mg- or
   pf- element, as for the headings: web-base's cards and pages set their own. */
.bd-article-container p:not(body :is([class^="mg-"], [class*=" mg-"], [class^="pf-"], [class*=" pf-"]) *) {
  margin-bottom: 0.5rem;
}

/* The theme's 0.1rem vertical padding makes inline code 23.2px tall in a
   21.6px line, so literals on consecutive lines overlap (#2949). Dropping it
   fits the box to the line without changing the line spacing; a wrapped
   literal keeps its border and padding on both pieces. */
.bd-article-container code.literal {
  padding-block: 0;
  -webkit-box-decoration-break: clone;
  box-decoration-break: clone;
}

/* MathJax scales inline math to about 1.2 times the text, so the container's
   inherited 1.35 line-height is a 25.8px box in a 21.6px line and pushes the
   lines apart (#2949). Without it only the formula's own height counts, so a
   tall one (a fraction, a sum) still opens its line. */
.bd-article-container mjx-container[jax="CHTML"]:not([display="true"]) {
  line-height: 0;
}

/* The theme clips inline math so a wide formula cannot widen the column,
   but clipping down as well cut off what a glyph draws above or below the
   formula's box, such as the foot of a subset sign (#3373). */
span.math {
  overflow-x: clip;
  overflow-y: visible;
}

/* A line may break on either side of the theme's inline-flex math, which
   left a full stop at the start of a line (#3381). math_word.py wraps a
   formula with the text it touches in one flex box, which wraps only when
   they do not fit the column together: the text then takes the next line. */
.handbook-math-word {
  display: inline-flex;
  flex-wrap: wrap;
  max-width: 100%;
  white-space: nowrap;
}

/* Never shrunk, as the theme's overflow: hidden would cut the formula's end
   unseen. Lines are filled at its narrowest, so one MathJax can break keeps
   its text beside its last line, and it then widens to fill its line. */
.handbook-math-word > .math {
  flex: 1 0 min-content;
}

/* MathJax breaks a formula too wide for its line onto lines of its own,
   and text after it reads on from the last. Chromium takes a flex box's
   last baseline only from items aligned by it. Text only before a formula
   leads its first line. */
.handbook-math-word,
.handbook-math-word > .math {
  align-items: baseline;
  align-items: last baseline;
}
.handbook-math-lead,
.handbook-math-lead > .math {
  align-items: baseline;
}

/* An element joined to a formula stays inline, as in a flex box it would be
   a block, and a copied selection would gain a line break at each end. One
   inline block breaks only when wider than the column; nowrap did not hold
   where a phone lets a literal wrap, and scrolled the article sideways. */
.handbook-math-run {
  display: inline-block;
  max-width: 100%;
}

/* Until MathJax typesets it, or without JavaScript, the TeX wraps as it did. */
.handbook-math-word .math {
  white-space: normal;
}

/* A text decoration stops at an inline-flex or inline-block box: carry a
   link's down to the words and runs in it, whatever state the link is in. */
a :is(.handbook-math-word, .handbook-math-run, :has(.handbook-math-word, .handbook-math-run)) {
  text-decoration: inherit;
}

/* Cap prose line length on large screens (issue #121): magma-parameter.css
   deliberately gives .bd-article-container itself no max-width (removing
   the theme's old 88rem page cap avoided a growing empty gutter outside
   the sidebars on wide displays), so its width is purely proportional
   (76% of the page) with nothing stopping it growing arbitrarily wide on
   large monitors, and lines got uncomfortably long to read.
   Capping the paragraph/list text itself, not the container, keeps that
   fix: tables and signature blocks still use the full column width
   (display math and code blocks get the same cap in their own rules
   below), and any leftover space next to short-measure text is just
   ordinary trailing whitespace, not a layout gutter.
   web-base's notes pages set their own measure (web-base#92); :where()
   keeps this rule's specificity below the admonition rule's. */
.bd-article-container p:not(:where(.notes-page p)),
.bd-article-container li:not(:where(.notes-page li)) {
  max-width: 112ch; /* 1.5x a plain-prose 75ch measure */
}

/* An admonition's border already bounds its text (issue #152).
   Leave the text uncapped so it fills the box. */
.bd-article-container .admonition p,
.bd-article-container .admonition li {
  max-width: none;
}

/* Display math (issue #230): div.math -- MathJax's display-equation wrapper
   -- sits alongside p/li in the article body rather than being one itself,
   so the 112ch cap two rules up never applied to it. MathJax
   centers it within whatever width it's given; with no cap that's the
   full uncapped container, not the capped text column the surrounding
   prose actually reads at, so the equation visibly sits to the right of
   the text (and shifts further whenever a sidebar's open/closed state
   changes the container's own width) instead of tracking the column the
   prose around it is capped to. Same 112ch value as the prose cap, kept
   in sync by hand -- centering (margin: auto) is MathJax's own default
   behavior on the block once it fits within a max-width, not something
   this rule needs to set itself. */
.bd-article-container div.math {
  max-width: 112ch;
}

/* A code block (issue #3041) is not a p or li, so the prose cap misses it.
   Hold it to the same 112ch text column, kept in sync by hand; a longer
   line scrolls or wraps inside the block. */
.bd-article-container div.highlight {
  max-width: 112ch;
}

/* Card-like surfaces: bump toward the website's rounder card radius */
div.admonition {
  border-radius: 10px;
}

/* Flatten sphinx-design's cards site-wide (issue #90): drop the drop-shadow
   it applies by default (.sd-shadow-sm, on every grid-item-card) and use a
   plain border plus a subtle ochre hover accent instead of a shadow/lift.
   Scoped to .sd-card generally, so the homepage's compact topic cards
   (index-cards.css) and the single-column category lists below both pick
   up the same flat look automatically.
   The theme's own stylesheet sets both of these with selectors specific
   enough (and, for the shadow, !important) to otherwise win regardless of
   load order -- html[data-theme=...] .sd-shadow-sm for the shadow,
   .bd-content .sd-card.sd-card-hover:hover for the hover border -- so these
   overrides match that specificity (rather than just adding !important,
   which the shadow rule would still tie on) to reliably beat them. */
.sd-card {
  border-radius: 4px;
  transition: border-color 0.15s ease;
}

html .sd-card.sd-shadow-sm {
  box-shadow: none !important;
}

.sd-card:hover,
.bd-content .sd-card.sd-card-hover:hover {
  border-color: var(--mg-ochre);
}

/* sphinx-design's own .sd-card-hover:hover rule also sets
   `transform: scale(1.01)` -- a whole-card (and thus title) grow-on-hover
   effect (issue #189) separate from the border/shadow this block already
   flattens above. That rule shares this same selector, so it isn't a
   specificity gap: cascade doesn't borrow one rule's specificity for a
   property the overriding rule never declares, so `transform` still fell
   through to the theme's declaration until named explicitly here. Cancel
   just the scale, keep the border-color hover accent above. */
.sd-card-hover:hover {
  transform: none;
}

@media (prefers-reduced-motion: reduce) {
  .sd-card {
    transition: none;
  }
}

/* Rows with a single grid-item-card (intro text, Preface): the grid still
   reserves a topic-card-width column, so center that lone card instead of
   leaving it pinned to the left. */
.index-solo-row .sd-row {
  justify-content: center;
}

/* ── Depth-2 category pages: single-column list of cards (issue #90) ──────
   One level below the homepage, a category's subsections render as a
   single-column list (the .rst grid directive is forced to 1 column)
   instead of the homepage's compact multi-column grid. Title-only, same as
   the homepage -- the description stays in a hover/focus tooltip rather
   than a permanent second line (index-cards.js), just left-aligned and
   full width instead of centred. Still carries index-topic-grid too (see
   the .rst sources), so it keeps the same flat .sd-card look above and
   llms_txt.py's card scraper keeps working unchanged; these rules just
   override index-cards.css's centred layout for this container. */
.index-list {
  max-width: 760px;
  margin-left: 0;
  margin-right: 0;
}

.index-list .sd-card-body {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-start;
  text-align: left;
  padding: 0.85rem 1.2rem;
  min-height: 0;
}

.index-list .sd-card-title {
  text-align: left;
  font-size: 0.95rem;
  margin: 0;
}

/* Buttons / inputs: match the website's 8px control radius */
.btn,
.bd-search-button__click-hint,
input[type="search"],
.search-button-field {
  border-radius: 8px;
}

/* ── Cleaner nav/sidebar scrollbars (issue #102) ───────────────────────────
   The left nav and right on-this-page panes scroll their own overflow
   (theme default) and are tall enough that a scrollbar is visible on
   most pages, permanently, in the browser/OS's default thick, high-
   contrast style -- clashing with the site's flat, muted chrome. Thin it
   down, colour it from the same border token the rest of the chrome uses
   (already theme-reactive light/dark), and keep the thumb transparent
   until the pane is hovered so it doesn't read as "always on". Firefox
   uses the standard scrollbar-color/-width properties; other browsers
   use the older -webkit-scrollbar-* pseudo-elements -- both are declared
   since neither engine reads the other's syntax. The header's drawer, which
   holds the left nav where it is not a column, keeps web-base's scrollbar,
   as on magma-maths.org. */
.bd-sidebar-primary,
.bd-sidebar-secondary {
  scrollbar-color: transparent transparent;
  scrollbar-width: thin;
}

/* Move the right sidebar's own scrollbar to its inner-left edge (issue
   #109): .bd-sidebar-secondary sits flush against the browser's right
   viewport edge, so its scrollbar lands under the browser's own edge
   scrollbar/overlay and can't be reliably grabbed. Flipping direction puts
   the scrollbar on the left of the sidebar instead; the immediate content
   wrapper flips back to ltr so text, links and layout inside read normally
   -- only the scrollbar's side is affected.
   The sidebar shares the page's own background with no border, so a
   scrollbar sitting at its bare left edge reads as floating in the gap
   before the sidebar rather than inside it -- add a hairline border (the
   same muted token already used for the scrollbar thumb) so there's a
   visible edge for the scrollbar to sit against. */
.bd-sidebar-secondary {
  border-left: 1px solid var(--pst-color-border);
  direction: rtl;
  /* The theme's own unconditional 1rem left/right padding (same on both
     sides, meant for a plain undecorated edge) now sits between the
     flipped scrollbar/border and the sidebar's real content -- shrink
     just this side so the now-decorated (bordered) edge reads as closer
     to what it's decorating, matching how little space the theme leaves
     around a plain content edge elsewhere. */
  padding-left: 0.375rem;
}

.bd-sidebar-secondary > .sidebar-secondary-items {
  direction: ltr;
}

/* issue #188: at >=1200px the theme's own .sidebar-secondary-item (the
   "On this page" wrapper, one level inside .sidebar-secondary-items) gets
   its own separate border-left + padding-left: 1rem -- meant, in the
   theme's original left-to-right layout, to read as a matched pair with
   .bd-sidebar-secondary's own right-side border as a single visual
   separator between article and sidebar. The RTL flip above only moved
   .bd-sidebar-secondary's own border/scrollbar to the left; it left this
   inner element's identical border+1rem padding stacked on the SAME side,
   so the flipped scrollbar (backed by .bd-sidebar-secondary's border,
   right at the sidebar's true edge) ends up ~40px+ before any real
   content -- reading as "floating" well to the left of the sidebar
   instead of part of it. That inner border is now redundant (the outer
   one already gives the scrollbar its edge); drop it and its matching
   padding back to the theme's own narrower-breakpoint value. */
.bd-sidebar-secondary .sidebar-secondary-item {
  border-left: none;
  padding-left: 0.5rem;
}

.bd-sidebar-primary:hover,
.bd-sidebar-secondary:hover {
  scrollbar-color: var(--pst-color-border) transparent;
}

.bd-sidebar-primary::-webkit-scrollbar,
.bd-sidebar-secondary::-webkit-scrollbar {
  width: 8px;
}

.bd-sidebar-primary::-webkit-scrollbar-track,
.bd-sidebar-secondary::-webkit-scrollbar-track {
  background: transparent;
}

.bd-sidebar-primary::-webkit-scrollbar-thumb,
.bd-sidebar-secondary::-webkit-scrollbar-thumb {
  background-color: transparent;
  border-radius: 8px;
}

.bd-sidebar-primary:hover::-webkit-scrollbar-thumb,
.bd-sidebar-secondary:hover::-webkit-scrollbar-thumb {
  background-color: var(--pst-color-border);
}

/* "Disguised list" description lists (issue #273): the converter falls
   back to a description list for bulleted/lettered/numbered runs whose
   marker has no macro signal to structure into a real list -- CommonMark
   simply has no native syntax for lettered/roman/fully-parenthesised
   markers. The theme's default <dl>/<dt>/<dd> renders term and definition
   as separate block siblings, so the definition starts on its own line
   below the marker instead of hanging beside it -- correct for a genuine
   description list, wrong for a list in disguise. marker_list_style.py
   tags only the latter (every term marker-shaped) with this class; a
   description list with any real term text is untouched.
   Grid (not float) so a marker-shaped term still lines up correctly even
   when a definition wraps onto several lines. */
dl.marker-list {
  display: grid;
  /* minmax(0, ...): a bare 1fr track is never narrower than its widest
     content, so a wide display formula would widen the list past the
     article and pan it on a phone rather than scroll in its own box (#2949). */
  grid-template-columns: max-content minmax(0, 1fr);
  column-gap: 0.5em;
  row-gap: 0.25rem;
  /* Align by the first line's text baseline, not the grid row's box edge --
     the marker and definition can differ in font metrics (e.g. a definition
     starting with MathJax-rendered math, whose line box isn't plain text
     height), and top/stretch alignment then leaves the marker looking
     slightly higher than the text it's paired with. */
  align-items: baseline;
}

dl.marker-list > dt {
  grid-column: 1;
  font-weight: 400;
  /* Right-aligned, so that markers of different widths (-3, -1, r) end in
     one column against their definitions. Both properties: a dt nested in
     an entry's dd is a flex container, where text-align does nothing, and
     a top-level one is a block, where justify-content does nothing. */
  text-align: right;
  justify-content: flex-end;
}

dl.marker-list > dd {
  grid-column: 2;
  margin: 0;
}

dl.marker-list.marker-list-wide {
  column-gap: 1.25em;
}

/* An empty TeX label (\item{}, \itemitem{}) has no marker: keep the
   indent, hide the converter's placeholder (visibility, so a screen reader
   does not announce a bullet either). */
dl.marker-list-empty > dt {
  visibility: hidden;
  width: 1.5em;
}

/* ── Permanent example labels (issue #237) ─────────────────────────────── */

/* A permanent example identifier stays readable without competing with its title. */
.magma-example-tag {
  color: var(--pst-color-text-muted);
  font-family: var(--pst-font-family-monospace, monospace);
  font-size: 0.8em;
}

/* ── Accessibility corrections (#2546) ─────────────────────────────────── */

/* A wide display formula scrolls in its div.math, not in itself: focus on
   a formula goes to MathJax's explorer, which takes the arrow keys
   (math-overflow-focus.js). Across only, as the theme's overflow: hidden
   already has it down; tall glyphs' boxes overflow into the margin. */
div.math {
  overflow-x: auto;
}

div.math mjx-container {
  overflow: visible;
}

/* The theme's row-reverse would open a wide formula scrolled to its right
   end; row opens it at its start, and an equation number stays right. */
div.math {
  flex-direction: row;
}

div.math > .eqno {
  order: 1;
}

/* A div.math that scrolls is a Tab stop (math-overflow-focus.js); it gets
   the site's ring, inside because the box clips its overflow. */
div.math:focus-visible {
  outline: 3px solid var(--pst-color-accent);
  outline-offset: -3px;
}

/* The theme's breadcrumb separator is an icon-font character, which screen
   readers announce; the empty alternative text keeps it visual only. A
   browser without that syntax would drop the separator, so it keeps the
   theme's. */
@supports (content: "x" / "") {
  ul.bd-breadcrumbs li.breadcrumb-item:not(.breadcrumb-home)::before {
    content: var(--pst-breadcrumb-divider) / "";
  }
}

/* The theme keeps each breadcrumb on one line and cuts a long one off with
   an ellipsis, which at 320px hid most of a part or chapter title (#2719).
   A long one wraps instead. */
ul.bd-breadcrumbs li.breadcrumb-item {
  white-space: normal;
}

/* sphinx-copybutton shows its button only while the pointer is over the
   code, or while the button has focus, but link hinting (Vimium) still
   hinted it while it was invisible (#2719). It stays shown, in the theme's
   muted colour. */
div.highlight button.copybtn {
  opacity: 1;
}

/* Reflow at 320px: Sphinx keeps each span.pre on one line, so a long
   intrinsic name widened the whole article past the viewport. A signature
   breaks inside a word only when it cannot fit on its own line. */
.bd-article-container dt.sig span.pre {
  white-space: normal;
}

/* Wrapping text may break after a hyphen, which split a signature's "->".
   Punctuation tokens are short, so they stay whole. */
.bd-article-container dt.sig .p span.pre {
  white-space: nowrap;
}

.bd-article-container dt.sig {
  overflow-wrap: anywhere;
}

/* A browser sets an em italic and no stylesheet overrode it. A
   constructor's "< F | R >" is plain text, so its variables are upright;
   every other signature's variables are set to match. */
dt.sig em.sig-param,
code.literal em.sig-param {
  font-style: normal;
}

/* A literal in prose is one span.pre per word, not tokenised, so letting
   it wrap split "x -> y" or "--check-map" after the hyphen wherever a
   line happened to end there. It keeps Sphinx's nowrap and wraps only on
   a phone, where a long name would otherwise scroll the article. */
@media (max-width: 40em) {
  .bd-article-container code.literal span.pre {
    white-space: normal;
  }

  .bd-article-container code.literal {
    overflow-wrap: anywhere;
  }

  /* A heading's last word and its permalink "#" are one unbreakable run: it
     scrolled the article sideways, or broke to leave the "#", hidden until
     hover, on a blank line of its own (#2960). A phone has no hover to show
     it, so it takes no space; a word longer than the column still breaks. */
  .bd-article-container :is(h1, h2, h3, h4, h5, h6) {
    overflow-wrap: break-word;
  }

  .bd-article-container :is(h1, h2, h3, h4, h5, h6) > a.headerlink {
    display: none;
  }

  /* A word in prose wider than the column, such as an intrinsic's name in
     plain text, scrolled the article sideways (#2993). As in a heading, only
     a word that cannot fit on a line breaks. The breadcrumbs are list items
     here too, and already wrap (#2719). */
  .bd-article-container :is(p, li) {
    overflow-wrap: break-word;
  }
}

/* A long previous/next title kept its min-content width, so the theme's
   overflow clip cut off the title and the chevron beside it. */
.prev-next-area a .prev-next-info {
  min-width: 0;
  overflow-wrap: anywhere;
}

.prev-next-area a > :is(i, svg) {
  flex-shrink: 0;
}

/* Side by side at 45% each, a narrow screen left a title some 80px, and
   ordinary words broke mid-word: stack the two links instead. */
@media (max-width: 40em) {
  .prev-next-area a {
    float: none;
    max-width: none;
  }

  .prev-next-area a.right-next {
    justify-content: flex-end;
  }
}

/* The general index sets its entries out in a three-column table, 756px
   wide at a 320px viewport: one column on a narrow screen. !important
   beats the cell's inline width. */
@media (max-width: 40em) {
  table.genindextable,
  table.genindextable tbody,
  table.genindextable tr,
  table.genindextable td {
    display: block;
    width: auto !important;
  }
}

/* Index links were 22px tall rows of adjacent targets and the [1], [2]
   aliases narrower still; WCAG 2.5.8 asks for 24px. */
table.indextable li {
  overflow-wrap: break-word;
}

table.indextable li > a {
  display: inline-block;
  line-height: 24px;
  max-width: 100%;
  min-height: 24px;
}

table.indextable li > a ~ a {
  min-width: 24px;
  text-align: center;
}

/* Reduced motion: the theme's and the handbook's transitions, some with no
   rule of their own (0.01ms, so transitionend handlers still run). Not
   web-base's parts (mg- and pf- elements and what is in them, as for the
   headings), which have their own; the handbook's content in a slot is in. */
@media (prefers-reduced-motion: reduce) {
  :not(body :is([class^="mg-"], [class*=" mg-"], [class^="pf-"], [class*=" pf-"]), body :is([class^="mg-"], [class*=" mg-"], [class^="pf-"], [class*=" pf-"]) *),
  :not(body :is([class^="mg-"], [class*=" mg-"], [class^="pf-"], [class*=" pf-"]), body :is([class^="mg-"], [class*=" mg-"], [class^="pf-"], [class*=" pf-"]) *)::before,
  :not(body :is([class^="mg-"], [class*=" mg-"], [class^="pf-"], [class*=" pf-"]), body :is([class^="mg-"], [class*=" mg-"], [class^="pf-"], [class*=" pf-"]) *)::after,
  :is(.mg-notes-content, .handbook-cited-in),
  :is(.mg-notes-content, .handbook-cited-in)::before,
  :is(.mg-notes-content, .handbook-cited-in)::after,
  :is(.mg-notes-content, .handbook-cited-in) *,
  :is(.mg-notes-content, .handbook-cited-in) *::before,
  :is(.mg-notes-content, .handbook-cited-in) *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
    transition-delay: 0s !important;
    transition-duration: 0.01ms !important;
  }
}

/* A no-test example's row has no calculator link, so without script it
   would stand empty. */
body.mg-no-js .magma-play-copy-only {
  display: none;
}

/* The two composition-factor tables each sit centred in one half of the
   text width. The columns are sized in rem, so a larger font stacks the
   tables instead of making them overlap. */
.mh-table-pair {
  container-type: inline-size;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 20rem), 1fr));
  align-items: start;
  gap: 2rem;
  margin-bottom: 3rem;
  max-width: 112ch; /* same measure as the prose cap above */
}
.mh-table-pair > .pst-scrollable-table-container {
  justify-self: center;
  max-width: 100%; /* a table too wide for its column scrolls inside it */
}
.mh-table-pair table {
  margin-bottom: 0; /* the pair's own margin spaces it like an entry */
}
.mh-table-pair table.mh-table-wide {
  min-width: min(16rem, 100cqi);
}
.mh-table-pair td {
  vertical-align: baseline; /* a number sits level with a taller math name */
}
/* "Table:" label ahead of an unnumbered caption (HTML only; the PDF
   numbers captions itself). Its alt text is empty, as a screen reader
   already announces a table; the first line is for browsers without it. */
table.mh-table-label > caption .caption-text::before {
  content: "Table: ";
  content: "Table: " / "";
}
