/* ── web-base logos in the dark theme ───────────────────────────────────── */
/* pydata-sphinx-theme gives every content image a white card in the dark
   theme; web-base's logos (the 404 card's) are transparent PNGs drawn for
   that background. */
html[data-theme=dark] img.mg-logo-light,
html[data-theme=dark] img.mg-logo-dark {
  background-color: transparent !important;
  border-radius: 0 !important;
}

/* It also dims every image without only-dark or dark-light, the footer's
   logos included; the dark logos are drawn for the dark ground (#2912).
   The "img" matches the theme rule's specificity, and this file loads
   after it. */
html[data-theme=dark] img:is(.mg-logo-light, .mg-logo-dark) {
  filter: none;
}

/* ── Handbook sidebar and content proportions ───────────────────────────── */
/*
 * This block intentionally targets sphinx-book-theme/pydata-sphinx-theme
 * layout internals (#pst-primary-sidebar, #main-content.bd-main,
 * .bd-content, .bd-article-container, .bd-sidebar-secondary) and uses
 * !important to override their defaults. These selectors are not a public
 * theme API, so pyproject.toml pins sphinx-book-theme to the 1.2 minor
 * line; revisit this block before bumping the theme.
 *
 * Layout is two nested CSS grids rather than the theme's nested flexboxes
 * (issue #409): the sidebar tracks are driven by the two custom properties
 * below so left-sidebar-collapse.js / collapsible-right-toc.js can animate
 * a hide/reveal by tweening a track width from its open size to 0 (the
 * article column, minmax(0, 1fr), reflows with it every frame). The
 * properties are registered as <length> so the transition interpolates a
 * real length; an unregistered custom property (or a raw min()/% track) is
 * treated as discrete by the transition engine and the layout snaps.
 *
 * The scripts set each property to a resolved pixel width (20% of the page
 * capped at 17.6rem for the primary; 24% of the main area for the
 * secondary) on load and on resize, and to 0 while collapsed. The :root
 * fallbacks here are fixed lengths (a <length> property rejects a
 * percentage value) so a no-JS visitor still gets both sidebars at a
 * sensible static width from 1200px; below that both are drawers, which
 * need JavaScript.
 *
 * Both sidebars are in-flow columns only from 1200px, the theme's own
 * breakpoint for the right one; under that each is its toggle's drawer
 * (#2664), so the left track is pinned to 0 and the right grid is off.
 *
 * Primary sidebar:  20% of page, capped at 17.6rem  (--magma-primary-sidebar-w)
 * Content:          the remaining space (minmax(0, 1fr))
 * Secondary sidebar: 24% of the main area           (--magma-secondary-sidebar-w)
 */

@property --magma-primary-sidebar-w {
  syntax: "<length>";
  inherits: true;
  initial-value: 0px;
}

@property --magma-secondary-sidebar-w {
  syntax: "<length>";
  inherits: true;
  initial-value: 0px;
}

:root {
  --magma-primary-sidebar-w: 17.6rem;
  --magma-secondary-sidebar-w: 17rem;
}

/* Below 1200px the left nav is not an in-flow column (it is the hamburger
   dialog), so zero its track or the grid keeps an empty gutter where the
   column used to sit. left-sidebar-collapse.js applies the same rule (it
   resolves a pixel width only at >= 1200px). The right TOC's column starts
   at the same width -- see below. */
@media (max-width: 1199.98px) {
  :root {
    --magma-primary-sidebar-w: 0px;
  }
}

@media (min-width: 960px) {
  /* The theme caps the content shell at 88rem, which otherwise leaves a
     growing empty gutter outside the right sidebar on wide displays. */
  .bd-container__inner.bd-page-width {
    max-width: none !important;
    display: grid !important;
    grid-template-columns: var(--magma-primary-sidebar-w) minmax(0, 1fr) !important;
    align-items: start !important;
  }

  #pst-primary-sidebar {
    grid-column: 1 !important;
    width: auto !important;
    max-width: none !important;
    min-width: 0 !important;
    /* Clip the nav list horizontally as the track tweens to 0 (otherwise it
       spills over the article mid-animation); keep the vertical auto-scroll
       the theme relies on for a taller-than-viewport nav list. */
    overflow: hidden auto !important;
  }

  #main-content.bd-main {
    grid-column: 2 !important;
    display: block !important;
    min-width: 0 !important;
    max-width: none !important;
  }

}

/* The right column, from the theme's own 1200px drawer breakpoint (#2664). */
@media (min-width: 1200px) {
  #main-content.bd-main > .bd-content {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) var(--magma-secondary-sidebar-w) !important;
    align-items: start !important;
  }

  #main-content.bd-main > .bd-content > .bd-article-container {
    grid-column: 1 !important;
    max-width: none !important;
    width: auto !important;
    min-width: 0 !important;
  }

  #main-content.bd-main > .bd-content > .bd-sidebar-secondary {
    grid-column: 2 !important;
    height: auto !important;
    margin-right: 0 !important;
    /* --mg-header-height, not --pst-header-height (60px), which covers only
       the header row, not the .bd-header with its padding and border: see
       custom.css's .bd-sidebar-primary comment (issue #383). Needed here (not
       just in custom.css) because this !important reassignment would
       otherwise beat a plain override there regardless of source order. */
    max-height: calc(100vh - var(--mg-header-height)) !important;
    max-width: none !important;
    min-width: 0 !important;
    overflow: hidden auto !important;
    position: sticky !important;
    right: auto !important;
    top: var(--mg-header-height) !important;
    visibility: visible !important;
    width: auto !important;
    z-index: auto !important;
  }

  /* Pages without a right TOC: collapse the second track so content fills
     the full main area. */
  #main-content.bd-main > .bd-content:not(:has(> .bd-sidebar-secondary)) {
    grid-template-columns: minmax(0, 1fr) 0px !important;
    container-type: inline-size;
  }

  /* Only the previous/next links keep the right column's width, so they end
     where they do on a page with a TOC (#3116). collapsible-right-toc.js sizes
     that column at 24% of .bd-content but sizes none on a page with no TOC,
     so the same 24% is taken here; without JavaScript, the column's fallback. */
  #main-content.bd-main > .bd-content:not(:has(> .bd-sidebar-secondary)) .prev-next-area {
    padding-right: 24cqw;
  }

  body.mg-no-js
    #main-content.bd-main > .bd-content:not(:has(> .bd-sidebar-secondary))
    .prev-next-area {
    padding-right: var(--magma-secondary-sidebar-w);
  }

  html.right-toc-collapsed
    #main-content.bd-main > .bd-content:not(:has(> .bd-sidebar-secondary))
    .prev-next-area {
    padding-right: 0;
  }
}

/* A short viewport unsticks the header (custom.css), so the sidebar's
   offset below it would leave a gap once the header scrolls away. */
@media (min-width: 1200px) and (max-height: 30em) {
  #main-content.bd-main > .bd-content > .bd-sidebar-secondary {
    max-height: 100vh !important;
    top: 0 !important;
  }
}

/* ── Link colours ─────────────────────────────────────────────────────────
   Content links use --pst-color-link / --pst-color-link-hover, which
   custom.css points at web-base's text ochre (--mg-ochre-text). Navigation
   chrome (prev/next, sidebar TOC) is deliberately kept text-coloured below,
   matching the website's navbar links which also stay plain until hover. */

/* Content body links: add underline so they're still identifiable without colour.
   The longhand keeps the theme's underline thickness, thicker on hover,
   which marks a hovered link whose colour stays (Magma-Maths/web-base#80).
   web-base's arrow link (a part page's downloads) sets its own underline,
   and web-base's buttons (the 404 page's) are not underlined. Nor are the
   notes pages' own links (back, previous and next, card and attachment
   links), and the notes' text links keep web-base's underline colour
   (Magma-Maths/web-base#92). */
.bd-article-container a:not(.headerlink):not(.sd-stretched-link):not(.nav-link):not([class*="sd-"]):not(.mg-link-arrow):not(.mg-button-primary):not(.mg-button-secondary):not([class*="mg-notes-"]):not(.mg-notes-content a) {
  text-decoration-line: underline;
  text-underline-offset: 0.15em;
}

/* Prev/next page navigation links */
.prev-next-area a,
.prev-next-area a:visited,
.prev-next-subtitle,
.prev-next-title {
  color: var(--pst-color-text-base) !important;
}

/* Sidebar TOC nav links, in the header's drawer too */
.bd-sidebar-primary a,
.bd-sidebar-primary a:visited,
.handbook-drawer-tree a,
.handbook-drawer-tree a:visited,
.bd-sidebar-secondary a,
.bd-sidebar-secondary a:visited {
  color: var(--pst-color-text-base) !important;
}

/* Navigation chrome highlights on hover by colour alone, as the header's
   links do; the theme and the content-link rule would underline it, the
   prev/next links even at rest. */
.prev-next-area a,
.prev-next-area a p.prev-next-title {
  text-decoration: none !important;
}

.bd-sidebar-primary a:hover,
.handbook-drawer-tree a:hover,
.bd-sidebar-secondary a:hover,
.prev-next-area a:hover,
.prev-next-area a:hover .prev-next-subtitle,
.prev-next-area a:hover .prev-next-title {
  color: var(--mg-ochre-text) !important;
  text-decoration: none !important;
}

/* In-page toctree entries (issue #98): a handful of pages show their
   subsections as an expandable in-page toctree (collapsible-toctree.js
   adds the +/- controls and indentation) rather than hiding it in favour
   of the sidebar alone. That expand/collapse affordance already marks
   these as navigation, so treat the link text the same as the sidebar
   TOC/prev-next above instead of a content-body link. */
.bd-article-container .toctree-wrapper a,
.bd-article-container .toctree-wrapper a:visited {
  color: var(--pst-color-text-base) !important;
  text-decoration: none !important;
}

/* Bold the page-level entries (the ones collapsible-toctree.js gives a
   +/- toggle to) so they read as the "row" a reader scans, distinct from
   the section links nested underneath once a page is expanded. */
.bd-article-container .toctree-wrapper > ul > li.toctree-l1 > a,
.bd-article-container .toctree-wrapper > ul > li.toctree-l1 > .toc-page-row > a {
  font-weight: 600;
}

/* ── Sidebar search bar: remove pink/primary colour ───────────────────────── */
.search-button-field {
  background: transparent !important;
  border-color: var(--pst-color-border, #dee2e6) !important;
  color: var(--pst-color-text-base) !important;
  box-shadow: none !important;
}
.search-button-field:hover,
.search-button-field:focus {
  background: var(--pst-color-surface, #f8f9fa) !important;
  border-color: var(--pst-color-text-muted) !important;
  color: var(--pst-color-text-base) !important;
  box-shadow: none !important;
}

/* ── Hide %%a> test assertion lines in Magma code blocks ─────────────────── */
.highlight-magma .cs {
    display: none;
}

/* ── "Run in calculator" / "Copy input" action rows at the bottom of a
   magma:example, and at the top of a long one ───────────────────────────── */
p.magma-play-wrap {
  text-align: right;
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

/* Scoped to the example so they outrank custom.css's article paragraph gap,
   which a bare p.magma-play-wrap does not. */
dl.magma.example > dd > p.magma-play-wrap {
  margin: 0.75rem 0 0.35rem;
}

dl.magma.example > dd > p.magma-play-wrap:first-child {
  margin: 0 0 0.75rem;
}

/* web-base's primary button colours: the label clears AA on the fill and
   its hover in both themes, which the brand ochre does not. */
a.magma-play-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.25rem 0.65rem;
  border-radius: 999px;
  background: var(--mg-button-bg);
  color: var(--mg-button-text) !important;
  font-size: 0.8rem;
  font-weight: 600;
  text-decoration: none !important;
  line-height: 1.4;
}

a.magma-play-btn:hover {
  background: var(--mg-button-hover);
}

a.magma-play-btn svg {
  flex: none;
}

/* ── Function/procedure/intrinsic signature names: logo red ──────────────── */
dl.magma > dt .sig-name {
  color: var(--mh-logo-red);
}

/* An example's title is prose (#2704): the body font, not the theme's
   monospace for the signature line it sits in. */
dl.magma.example > dt .magma-example-title {
  color: var(--mh-logo-red);
  font-family: var(--pst-font-family-base);
  font-weight: 700;
}

/* A magma object block (dl.magma.function/.procedure/.signature/...) that
   directly follows prose gets far less top spacing than one that follows
   another such block: the gap between two consecutive blocks is entirely
   the previous block's own margin-bottom (48px, theme default), while a
   preceding prose element's own margin-bottom is much smaller (~8px) and
   the block itself carries no margin-top of its own. Give it one so a
   block opening a new topic reads as clearly separated from the prose
   above it, same as it already is from a preceding block; matches (rather
   than adds to) the block-to-block gap via ordinary margin collapsing. */
p + dl.magma,
ul + dl.magma,
ol + dl.magma,
blockquote + dl.magma {
  margin-top: 3rem;
}

/* Grouped overloads are one visual unit: stack signature rows directly.
   A function/procedure overload carries its own :input_types:/:output_types:/
   :label: metadata under the name, which supplies enough visual bulk to read
   as a distinct row even with zero gap from its neighbour. A magma:literal
   overload (func<...>/for/while/case/... syntax entries) usually has none of
   that -- just the bare signature text -- so two such rows with zero gap
   between them read as one run-on line instead of two distinct signatures.
   Give every row after the first a little breathing room; it costs nothing
   for the metadata-bearing case and fixes the bare-literal one. */
dl.magma.signature > dt {
  margin-top: 0 !important;
  margin-bottom: 0 !important;
}

dl.magma.signature > dt + dt {
  border-top: 0;
  margin-top: 0.5em !important;
}

/* Whitespace alone reads like a paragraph break in a description, so a rule
   opens each entry after another, halfway down the 48px gap so pages get no
   longer (#2949); an example's box opens it instead (#3209). It ends where
   the text below does, 112ch past the dd's 2em, as an example does (#2960). */
.bd-article-container dl.magma + dl.magma:not(.example) {
  position: relative;
  padding-top: 1.5rem;
  margin-top: -1.5rem;
}

.bd-article-container dl.magma + dl.magma:not(.example)::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  max-width: calc(112ch + 2em);
  border-top: 1px solid var(--pst-color-border);
}

/* A wrapped signature's later lines hang past its description's 2em, so
   none starts where the next overload does (#2949). */
.bd-article-container dl.magma:not(.example) > dt.sig:not(:has(br)) {
  padding-left: 4em;
  text-indent: -4em;
}

/* A statement's syntax (for ... do / end for) has its own lines and indents,
   so only the lines the browser wrapped hang. A rule of its own, so a browser
   without each-line drops it and still hangs the signatures above. */
.bd-article-container dl.magma:not(.example) > dt.sig:has(br) {
  text-indent: 4em hanging each-line;
}

/* ── Collapsible examples ─────────────────────────────────────────────────── */

/* Hide content when collapsed */
dl.magma.example.collapsed > dd {
  display: none !important;
}

/* Clickable heading */
dl.magma.example > dt {
  cursor: pointer;
  user-select: none;
}

/* The permanent id is for copying, so the heading's no-select stops short of
   it (#3116); a click on it does not toggle either (magma-code.js). */
dl.magma.example > dt .magma-example-tag {
  cursor: text;
  user-select: text;
}

/* The real toggle control magma-code.js wraps the title in: reset to look
   like the plain title text it replaces. */
dl.magma.example > dt .magma-example-toggle {
  padding: 0;
  margin: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: inherit;
  cursor: pointer;
}

/* Small inline arrow -- no position/padding changes so layout is untouched.
   The empty alternative text keeps the glyph out of the accessible name;
   the first declaration is the fallback for browsers without that syntax. */
dl.magma.example > dt .magma-example-toggle::before {
  content: "\25B6";
  content: "\25B6" / "";
  display: inline-block;
  font-size: 0.6em;
  margin-right: 0.5em;
  vertical-align: middle;
  transition: transform 0.15s ease;
  color: var(--pst-color-text-muted, #6c757d);
}

dl.magma.example:not(.collapsed) > dt .magma-example-toggle::before {
  transform: rotate(90deg);
}

@media (prefers-reduced-motion: reduce) {
  dl.magma.example > dt .magma-example-toggle::before {
    transition: none;
  }
}

/* Cap example blocks to match the surrounding prose (issue #232), plus the
   2em a description's text is indented, so an example ends where that text
   does (#2960). The 112ch is kept in sync by hand with the entry rule above
   and custom.css's prose, display math and code block caps. */
dl.magma.example {
  max-width: calc(112ch + 2em);
  /* One tinted box round the whole example (#3209), so a reader sees where
     it ends and that the text after its code belongs to it. */
  background: var(--pst-color-surface);
  border: 1px solid var(--pst-color-border);
  border-radius: 10px;
  padding: 0.75rem 1rem;
}

/* A permalink targets the title: offset by the box's padding and border, so
   the box's top edge, not the title, clears the sticky header as targets do. */
dl.magma.example > dt {
  scroll-margin-top: calc(0.75rem + 1px);
}

/* The box has inline code's usual tint, so inline code inside takes the
   colour code blocks have. They keep it in the box: web-base tunes its token
   colours to --syntax-bg-color, and on the page's they fall short of AA. */
dl.magma.example code.literal {
  background-color: var(--syntax-bg-color);
}

/* The copy button sits on the code, so takes its colour; :where() leaves the
   theme's more specific hover rule in force. */
:where(dl.magma.example) div.highlight button.copybtn {
  background-color: var(--syntax-bg-color);
}

/* On a phone the box's padding and the 2em indent left the action row too
   narrow for its two buttons, which wrapped their labels at 320px. */
@media (max-width: 40em) {
  dl.magma.example {
    padding-inline: 0.75rem;
  }

  dl.magma.example > dd {
    margin-left: 0;
  }
}

/* magma-param blocks: reducing the margins (issue #163). Their width cap is
   custom.css's, which holds every code block to the prose column (#3041). */
.magma-param {
    margin: 0.1em 0;
}

/* Inner code block: keeps its normal tinted background (from the theme's
   default pre { background-color: var(--pst-color-surface) } rule), sized
   tight to its own text. */
.magma-param pre {
    padding: 2px 6px;
    margin: 0;
    font-size: 0.9em;
    /* Drop the theme's default pre border: with many stacked var-arg lines
       now merged into one block, its bottom edge read as an unwanted
       lighter "shadow" underside. */
    border: none;
}

/* sphinx-copybutton gives every .highlight wrapper a JS-applied min-height
   (to fit the copy button), taller than a single-line pre's own content
   (32px vs. 22px). pygments.css also gives that wrapper its own hardcoded
   background (html[data-theme=...] .highlight { background: #fefefe/
   #2b2b2b }), independent of and different from the pre's tint, so that
   leftover 10px shows through below the pre as a "shadow" strip on
   single-line blocks (multi-line blocks are naturally taller than the
   floor and never show it). Make the wrapper transparent, rather than
   matching its background to the pre's tint, so the leftover strip shows
   the page background instead of extending the tinted box downward - the
   visible box then hugs exactly the pre's own text height.
   !important: pygments.css's html[data-theme=...] .highlight rule is more
   specific than a plain .magma-param .highlight selector. */
.magma-param .highlight {
    background: transparent !important;
}

/* A parameter's row wraps rather than scrolls, so on a phone its default
   (one span, from magma_domain.py's _wrap_varargs_defaults) moves to the
   next line whole (#2949). text-wrap-mode, not white-space:
   strip_indentation.py keeps white space only where an element does. */
.magma-param pre {
    text-wrap-mode: wrap;
    /* Indents only a line the browser wrapped, so a default moved to a line
       of its own hangs in from the names that start every row. */
    text-indent: 4ch hanging each-line;
}

/* On a phone 4ch pushed a long default past the column; 2ch still sets it
   off from the next parameter's name (#2960). */
@media (max-width: 40em) {
  .magma-param pre {
    text-indent: 2ch hanging each-line;
  }
}

.magma-param .magma-param-default {
    display: inline-block;
    text-wrap-mode: nowrap;
}
