/*
  Shared structure for all our code blocks: the line-number gutter, line
  wrapping, and the code-annotation overlay.

  This file exists because these three things kept getting fixed for one
  language and silently missing for the other. Nothing here names a
  language. syntax/pseudo.css and syntax/mojo.css carry only palette, rail, tint, label,
  and tokens, and adjust the geometry by overriding the variables below on
  their own wrapper.

  Load order does not matter; custom properties resolve when used.
*/

:root {
  /* Width of the line-number column, and the breathing room on each side
       of the divider between it and the code. */
  --code-gutter: 2.4rem;
  --code-pad: 0.95rem;

  /* Top padding a labeled block needs to clear its gutter label. Used
       both for the padding and for correcting the annotation overlay, which
       is measured without it. */
  --code-label-space: 1.35rem;

  --code-divider: rgba(141, 153, 174, 0.4);
  --code-gutter-fg: var(--bs-secondary-color, #8d99ae);

  /* Size of the code in pseudocode and Mojo blocks, relative to the body text. Monospace
       type looks larger than proportional type at the same size, so this sits well below 1.
       One value for both languages (they used to differ: 0.92em and Quarto's 0.875em). */
  --code-font-size: 0.8em;
}

/* ------------------------------------------------------- number gutter
   Pandoc puts the digits in an ::before on each line's anchor, 4em wide and
   right-aligned, and shifts the whole line span 4em left so the code lands
   back where it started; the pre carries margin-left: 3em to make room.

   That shift breaks any wrapped line, because it applies to every fragment
   of the span rather than only the first, putting continuations 4em left of
   the code column. So the shift comes off and the anchor is hung out of the
   line by absolute positioning instead — it then occupies no inline space,
   and positions against the span, whose origin is the start of its own
   line.

   Requires code-line-numbers: true, or .numberLines on the fence. */

pre.sourceCode.numberSource {
  margin-left: var(--code-gutter);
  padding-left: var(--code-pad);
  border-left: 1px solid var(--code-divider);
}

pre.sourceCode.numberSource code>span {
  left: 0;
}

/* The 1px accounts for the pre's left border, so the digit box spans
   exactly [0, gutter] in the wrapper's coordinates. */
pre.sourceCode.numberSource code>span>a:first-child {
  position: absolute;
  left: calc(-1 * (var(--code-gutter) + var(--code-pad)) - 1px);
  width: var(--code-gutter);
  padding-right: 0.4rem;
  text-align: right;
}

pre.sourceCode.numberSource code>span>a:first-child::before {
  position: static;
  display: block;
  left: auto;
  width: auto;
  padding: 0;
  color: var(--code-gutter-fg);
  opacity: 0.6;
}

/* A labeled block's label sits at the code column. */
div.sourceCode:has(> pre.sourceCode.numberSource)::before {
  left: calc(var(--code-gutter) + var(--code-pad));
}

/* ------------------------------------------------------- line wrapping
   A long line wraps its continuation two characters past the line start.

   This needs the line spans to be block-level, which needs the newlines
   between them to stop producing breaks of their own: white-space normal on
   the code collapses those away, and pre-wrap on each span keeps the
   indentation inside it. min-height gives a blank line something to stand
   on, since its span holds only the line-number anchor and that anchor is
   taken out of flow above.

   NOT on annotated blocks, and this exclusion is the whole point of the
   :not(). Quarto's annotation marker is an anchor that precedes its line
   span as a sibling, floated right:

     <a class="code-annotation-anchor" data-target-annotation="1">1</a>
     <span id="annotated-cell-1-2"> … the line … </span>

   That float only lands correctly in the inline-span layout Pandoc ships:

     pre > code.sourceCode > span { display: inline-block; ... }

   Blocking the spans breaks it. Each marker then sits alone in a
   zero-height anonymous block, and since a float is not contained by its
   parent's height, consecutive markers stack leftward down the gutter.
   Trying to lift them out of the float model — a zero-height block with
   text-align: right — moved them inside the code instead. Leaving annotated
   blocks in Quarto's own layout is what actually works.

   The cost is that a wrapped line in an annotated block continues flush at
   the code column rather than indented two characters. It still lands
   inside the block, because the gutter section above already removed
   Pandoc's -4em line shift. */

pre.sourceCode:not(.code-annotated)>code.sourceCode:has(> span[id]) {
  white-space: normal;
}

pre.sourceCode:not(.code-annotated)>code.sourceCode:has(> span[id])>span {
  display: flow-root;
  white-space: pre-wrap;
  padding-left: 2ch;
  text-indent: -2ch;
  min-height: 1.4em;
  min-height: 1lh;
}

/* Quarto's annotation gutter (the numbered bubbles) is absolutely positioned over the right edge of
   the block, about 28px wide, and the pre has no right padding of its own. Without room kept clear,
   the last characters of a line that fills the block sit under the gutter and are cut off. */
pre.sourceCode.code-annotation-code {
  padding-right: 2.4rem;
}

/* --------------------------------------------- code annotation overlay
   Quarto draws the highlight as two divs created by JS on selection, not as
   a class on the line spans:

     #code-annotation-line-highlight         over the code lines
     #code-annotation-line-highlight-gutter  over the annotation gutter

   Colors come from $code-annotation-higlight-color and
   $code-annotation-higlight-bg (Quarto's spelling). Size comes from the
   target lines' bounding boxes plus a hard-coded reach:

     margin-left: -4em; width: calc(100% + 4em);

   The 4em assumes Pandoc's default gutter. Unnumbered blocks have no gutter
   at all, so the whole 4em hangs into the page margin; ours is not 4em
   either. Both cases are quarto-dev/quarto-cli#13448.

   These selectors assume the overlay is appended inside div.sourceCode. If
   the inspector shows it elsewhere, they need to start at its real parent. */

div.sourceCode:has(> pre.sourceCode:not(.numberSource)) #code-annotation-line-highlight {
  margin-left: 0;
  width: 100%;
}

div.sourceCode:has(> pre.sourceCode.numberSource) #code-annotation-line-highlight {
  margin-left: calc(-1 * (var(--code-gutter) + var(--code-pad)));
  width: calc(100% + var(--code-gutter) + var(--code-pad));
}

/* The overlay's top is measured without the pre's own top padding, so the
   space a labeled block reserves for its label shifts the band up by that
   amount. The gutter overlay is measured differently and lands correctly,
   so only the code band moves. Add a selector here for each labeled
   language. */
div.sourceCode:has(> pre.sourceCode.pseudo) #code-annotation-line-highlight,
div.sourceCode:has(> pre.sourceCode.mojo) #code-annotation-line-highlight {
  margin-top: var(--code-label-space);
}