/*
 * doc-mermaid-finish.css — the fill of a diagram's boxes, the stroke of its lines and the canvas it
 * stands on (CH-REQ-20260929-A §11), and words kept clear of the lines they cross (§12). Corners are
 * the runtime's: finish.js rounds each rectangle box.
 *
 * The runtime (src/modules/markdown-engine/elements/mermaid/finish.js) marks the drawing
 * `data-fill` / `data-stroke` and the figure `data-diagram-canvas`; everything visual is here, so the
 * workspace, an exported page and /help draw the same diagram.
 *
 * Part of the diagram's frame, like doc-mermaid.css, and dark wherever the diagram is drawn dark
 * (`data-mermaid-scheme` or `data-doc-scheme`, as doc-mermaid-spectrum.css says). Every rule beats
 * Mermaid's own through `!important`, since Mermaid's <style> reaches its shapes by the
 * diagram's #id. Loaded after doc-mermaid-spectrum.css: an outline replaces Spectrum's tinted boxes by
 * matching their specificity and coming later.
 */

.doc-mermaid-viewer {
  --mermaid-canvas-line: rgba(15, 23, 42, 0.07);
  --mermaid-canvas-point: rgba(15, 23, 42, 0.18);
}

:root:is([data-mermaid-scheme='dark'], [data-doc-scheme='dark']) .doc-mermaid-viewer {
  --mermaid-canvas-line: rgba(255, 255, 255, 0.055);
  --mermaid-canvas-point: rgba(255, 255, 255, 0.15);
}

/* A sequence diagram's words sit across the lifelines between the participants they join; an edge in
   the colour behind them, painted under each letter, keeps a lifeline from striking them through
   (CH-REQ-20260929-A §12). Mermaid gives them no stroke of their own. Behind them is the canvas, or a
   `rect` highlight or participant `box` over it — finish.js names that blend `--mermaid-word-ground`, so
   on a coloured block the edge is the block's colour and not a white outline (CH-REQ-20261118-B). */
.doc-mermaid svg :is(text.messageText, text.loopText, text.sectionTitle) {
  paint-order: stroke fill !important;
  stroke: var(--mermaid-word-ground, var(--mermaid-spectrum-ground, Canvas)) !important;
  stroke-width: 4px !important;
  stroke-linejoin: round !important;
}

/* A line runs behind a label's words where nothing of Mermaid's hides it: a Hand-drawn line, different at
   every drawing, behind an edge label's half-see-through backing or across a group's title, and any line once
   Label background is Off. There a halo in the canvas's colour keeps it off the letters (§14). Nowhere else:
   on the plate Mermaid 12 draws under every edge label — grey — it was a white glow round each word that
   mermaid.live does not draw (CH-REQ-20261118-B). Hand-drawn is Mermaid's own mark on its nodes and groups. */
.doc-mermaid svg:has([data-look='handDrawn']) :is(.labelBkg, .cluster-label :is(span, p)),
.doc-mermaid svg[data-label-background='off'] .labelBkg {
  text-shadow: 0 0 1px var(--mermaid-spectrum-ground, Canvas), 0 0 2px var(--mermaid-spectrum-ground, Canvas),
    0 0 3px var(--mermaid-spectrum-ground, Canvas) !important;
}

/* Label background: Mermaid draws a plate behind each line's label — a fill on the label's own boxes, and a
   `rect` where the label is drawn as SVG text. Off gives the words the canvas; the halo above still keeps a
   line off the letters themselves (CH-REQ-20261006-B). */
svg[data-label-background='off'] :is(.edgeLabel, .labelBkg, .edgeLabel :is(div, span, p)) {
  background: transparent !important;
  background-color: transparent !important;
}

svg[data-label-background='off'] .edgeLabel rect {
  fill: transparent !important;
  stroke: transparent !important;
}

/* Outline: a box is its outline on the canvas's own colour — a line behind it stays hidden — and a
   subgraph is left unfilled. */
svg[data-fill='outline'] :is(g.node, g.rough-node) :is(.label-container, .label-container > path, .outer-path > path, .row-rect-odd > path, .row-rect-even > path),
svg[data-fill='outline'] :is(g.node, g.cluster)[data-spectrum-node] :is(.label-container, .outer-path > path),
svg[data-fill='outline'] g.node[data-spectrum-node] .row-rect-odd > path,
svg[data-fill='outline'] rect.actor,
svg[data-fill='outline'] [data-spectrum-node] rect.actor {
  fill: var(--mermaid-spectrum-ground, Canvas) !important;
}

/* Hand-drawn draws a body as hatching, first, before its outline (measured on every Hand-drawn shape):
   an outlined box keeps only the outline (CH-REQ-20260929-A §14). */
svg[data-fill='outline'] [data-look='handDrawn'] :is(.label-container, .outer-path) > path:first-child,
svg[data-fill='outline'] g.cluster[data-look='handDrawn'] > g > path:first-child {
  stroke: transparent !important;
}

svg[data-fill='outline'] g.cluster > rect,
svg[data-fill='outline'] g.cluster[data-spectrum-node] > rect {
  fill: transparent !important;
}

/* Stroke: every line dashed or dotted — an author's own dotted, dashed or thick edge keeps its own. */
svg[data-stroke='dashed'] :is(.edgePaths > path, path.flowchart-link, .messageLine0, .architecture-edges path.edge):not(.edge-pattern-dotted, .edge-pattern-dashed, .edge-thickness-thick, .dashed-line, .dotted-line, .doc-mermaid-halo) {
  stroke-dasharray: 7 5 !important;
}

svg[data-stroke='dotted'] :is(.edgePaths > path, path.flowchart-link, .messageLine0, .architecture-edges path.edge):not(.edge-pattern-dotted, .edge-pattern-dashed, .edge-thickness-thick, .dashed-line, .dotted-line, .doc-mermaid-halo) {
  stroke-dasharray: 0.5 5.5 !important;
  stroke-linecap: round !important;
}

/* Canvas: a faint lattice behind the diagram, lines or points. It is the figure's, not the canvas's: the toolbar's row sits
   above the canvas (doc-mermaid.css, §9.2), so a lattice on the canvas alone stopped under it and left a plain strip behind
   the buttons — where fullscreen, whose canvas is the whole screen, had none. The canvas lets it through, and the figure's own
   colour is the ground (finish.js `groundOf` and the share dialog's `diagramGround` both fall back to it). Kept inside the
   figure's edge, as the canvas was. Named with `data-doc-mermaid-viewer` to outrank the frame's `background` shorthand, the
   dark scheme's included, which would otherwise take the lattice away. */
:root .doc-mermaid-viewer[data-doc-mermaid-viewer][data-diagram-canvas='grid'] {
  background-image:
    linear-gradient(var(--mermaid-canvas-line) 1px, transparent 1px),
    linear-gradient(90deg, var(--mermaid-canvas-line) 1px, transparent 1px) !important;
  background-size: 24px 24px !important;
  background-position: center center !important;
  background-origin: padding-box !important;
  background-clip: padding-box !important;
}

:root .doc-mermaid-viewer[data-doc-mermaid-viewer][data-diagram-canvas='dots'] {
  background-image: radial-gradient(circle, var(--mermaid-canvas-point) 1.2px, transparent 1.7px) !important;
  background-size: 18px 18px !important;
  background-position: center center !important;
  background-origin: padding-box !important;
  background-clip: padding-box !important;
}

:root .doc-mermaid-viewer[data-doc-mermaid-viewer][data-diagram-canvas='grid'] .doc-mermaid-canvas,
:root .doc-mermaid-viewer[data-doc-mermaid-viewer][data-diagram-canvas='dots'] .doc-mermaid-canvas {
  background: transparent !important;
}

/* Paper keeps the boxes and lines, and leaves the lattice off. */
@media print {
  :root .doc-mermaid-viewer[data-doc-mermaid-viewer][data-diagram-canvas] {
    background-image: none !important;
  }
}
