/* The lit diagram: the fourth register of pictures (DESIGN.md, Pictures; D049 to D054, D056). One or more rows, each
   a setup on two sides of one band, with named parts joined by wires on rippled sand under a lamp that falls on the
   part that row is about.

   The figure types no colour: every colour is one of the page's tokens, and each strength that differs by theme is a
   custom property here, set again for the light page, so the figure follows the page's theme (D055). Its sizes, radii
   and times are ones the site already uses, and it has no breakpoint of its own: assets/js/writing/diagram.js sets
   data-frame from the figure's own width. Written into a page by tools/diagrams/build.py. */
.dia {
  /* every part but the lit one steps back, so the lit one is the only thing with light on it */
  --dia-part: color-mix(in srgb, var(--line) 4%, var(--ground));
  --dia-part-edge: color-mix(in srgb, var(--line) 18%, transparent);
  --dia-picked: color-mix(in srgb, var(--line) 5%, var(--ground));
  /* the lamp on the lit part: its brightest, and where it has fallen off (DESIGN.md, The diagram's light) */
  --dia-lamp-hi: color-mix(in srgb, var(--line) 28%, var(--ground));
  --dia-lamp-lo: color-mix(in srgb, var(--line) 6%, var(--ground));
  --dia-lit-edge: var(--line);
  /* the sand: how strong the line colour is where the lamp falls full on it */
  --dia-sand: 0.44;
  /* a line's role (DESIGN.md, The drawing's rules): its strength, one step stronger on the light page by --ink-gain */
  --dia-structure: calc(0.45 * var(--ink-gain));
  --dia-information: min(1, calc(0.85 * var(--ink-gain)));
  position: relative;
  margin: 52px 0 48px;
  font-size: 16px;
  line-height: 1.5;
  font-weight: 430;
  letter-spacing: 0;
}
/* On the light page the ground and what stands on it swap roles (D055): a part is a shade darker than the page, the
   lamp's light on the lit part rises to the page itself and the sand shows by its shade. */
:root[data-theme="light"] .dia {
  --dia-part: color-mix(in srgb, var(--line) 5%, var(--ground));
  --dia-part-edge: color-mix(in srgb, var(--line) 22%, transparent);
  --dia-picked: color-mix(in srgb, var(--line) 8%, var(--ground));
  --dia-lamp-hi: var(--ground);
  --dia-lamp-lo: color-mix(in srgb, var(--line) 9%, var(--ground));
  --dia-lit-edge: color-mix(in srgb, var(--line) 55%, transparent);
  --dia-sand: 0.3;
}

/* the frame. Narrow, one column with each row's calls running down it. Wide, each row is a grid of the figure's own
   columns, from its data, so the rows' parts and their band line up */
.dia-live { position: relative; display: grid; row-gap: 88px; }
.dia-row { position: relative; display: grid; grid-template-columns: minmax(0, 1fr); row-gap: 16px; }
.dia .it, .dia .zh, .dia .bnd, .dia .xg, .dia .rt { position: relative; z-index: 1; min-width: 0; }
.dia .it { display: grid; }
.dia-grd, .dia-wires { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; }
.dia-grd { z-index: 0; }
.dia-wires { z-index: 2; }

/* the script sets these in place; they never glide there, whatever the reader's motion setting makes of a transition */
.dia-live, .dia .it, .dia .zh, .dia .bnd, .dia .n { transition-property: none; }
/* the sand and the page-wide ground run past the figure; they never make the page scroll sideways */
main:has(.dia) { overflow-x: clip; }

/* a row's name: the setup, with its makers' marks, over a dimension line as wide as the row. The same line with end
   ticks stands under each column's name in an article's table (article.css), so a figure and its table read as one */
.dia .rt { display: flex; flex-wrap: wrap; align-items: center; column-gap: 10px; margin-bottom: 8px; font-size: 16px; line-height: 1.4; font-weight: 500; letter-spacing: -0.015em; color: var(--ink); }
.dia .rt b { font-weight: inherit; }
.dia .rt .mk { position: static; flex: none; color: var(--ink); }
.dia .rt::after {
  content: ""; flex-basis: 100%; height: 9px; margin-top: 10px; transform-origin: left center;
  background:
    linear-gradient(var(--ink-2), var(--ink-2)) left center / 1px 9px no-repeat,
    linear-gradient(var(--ink-2), var(--ink-2)) right center / 1px 9px no-repeat,
    linear-gradient(var(--ink-2), var(--ink-2)) center / 100% 1px no-repeat;
}

/* the name of a side, and of the band between them */
.dia .zh { display: grid; row-gap: 2px; justify-items: start; }
.dia .zh b, .dia .bnd b { font-size: 16px; line-height: 1.4; font-weight: 500; letter-spacing: -0.015em; color: var(--ink); }
.dia .zh span { font-size: 13px; line-height: 1.5; color: var(--ink-2); }
.dia .bnd { display: grid; justify-items: start; padding-block: 10px; }
.dia .bnd b { font-family: var(--font-mono); font-size: 13px; line-height: 1.5; font-weight: 400; letter-spacing: 0; color: var(--ink-2); }

/* a part: what it is, its name and what it does. A button, so the keyboard reaches it */
.dia .n { position: relative; display: grid; grid-template-columns: minmax(0, 1fr); align-content: start; justify-items: start; margin: 0; padding: 15px; text-align: start; font: inherit; letter-spacing: inherit; color: inherit; cursor: default; background: var(--dia-part); border: 1px solid var(--dia-part-edge); border-radius: 6px; transition: border-color 120ms ease-out, background-color 120ms ease-out, opacity 120ms ease-out; }
.dia .n:hover, .dia .n.hot { border-color: var(--hair-2); }
.dia .gl { width: 24px; height: 24px; margin-bottom: 8px; fill: none; stroke: currentColor; stroke-width: 1.1; stroke-linecap: round; stroke-linejoin: round; }
.dia .gl .dot { fill: currentColor; stroke: none; }
.dia .rl { font-size: 13px; line-height: 1.5; color: var(--ink-2); }
.dia .pn { font-size: 16px; line-height: 1.4; font-weight: 500; letter-spacing: -0.015em; color: var(--ink); }
.dia .lns { display: grid; row-gap: 2px; margin-top: 12px; }
.dia .ln { display: flex; flex-wrap: wrap; align-items: baseline; column-gap: 6px; font-size: 13px; line-height: 1.5; color: var(--ink-2); text-wrap: pretty; }
.dia .ln.lead .lt { font-weight: 500; color: var(--ink); }
/* what the reader is not shown: a small square hatched on the garden's slant, after the words it marks */
.dia .hid { flex: none; width: 10px; height: 10px; fill: none; stroke: currentColor; stroke-width: 1; vector-effect: non-scaling-stroke; }
.dia .hk { display: flex; align-items: center; column-gap: 8px; margin-top: 16px; font-size: 13px; line-height: 1.5; color: var(--ink-2); }
/* where an agent would stand and none does */
.dia .na { justify-self: stretch; margin-bottom: 16px; padding: 4px 12px; border: 1px dashed color-mix(in srgb, var(--line) 45%, transparent); border-radius: 3px; font-size: 13px; line-height: 1.5; text-align: center; color: var(--ink-2); }

/* the maker's mark: whose product a part is, in one colour, in the part's far top corner. The drawing beside it says
   what the part is; the mark never takes its place. The second ink at rest, the first when picked and on the lit part */
.dia .mk { position: absolute; top: 15px; right: 15px; width: 20px; height: 20px; fill: currentColor; stroke: none; color: var(--ink-2); transition: color 120ms ease-out; }
.dia .n:hover .mk, .dia .n.hot .mk, .dia .n.lit .mk { color: var(--ink); }
.dia .n.lit .mk { top: 23px; right: 23px; }
.dia .n:has(.na) .mk { top: 59px; }

/* the two ends of a row's question, drawn on the open sand with no panel round them */
.dia .n.free { grid-template-columns: minmax(0, 1fr); row-gap: 4px; justify-items: center; justify-self: start; padding: 0; background: none; border: 0; border-radius: 3px; }
.dia .n.free .gl { width: 40px; height: 40px; margin: 0; }
.dia .n.free .gl * { vector-effect: non-scaling-stroke; }

/* the lit part: the lamp falls on it from the upper left and falls off across it. The largest thing in its row */
.dia .n.lit { padding: 23px; border-color: var(--dia-lit-edge); background: radial-gradient(130% 105% at 18% 0%, var(--dia-lamp-hi), var(--dia-lamp-lo) 74%); color: var(--ink); }
.dia .n.lit .gl { margin-bottom: 12px; }
.dia .n.lit .pn { font-size: 28px; line-height: 1.1; letter-spacing: -0.02em; }
.dia .n.lit .rl, .dia .n.lit .ln { color: var(--ink); }
.dia .ln.is-hid { color: var(--ink-2); }
/* on the lit part every word is Ink, which the lamp's brightest still carries; the hatch says what is not shown */
.dia .n.lit .ln.is-hid, .dia .n.lit .hk { color: var(--ink); }
.dia .n.lit .ln.lead .lt { font-size: 16px; }

/* the calls between two parts and their replies: words beside the wires. Down (narrow, and wide where a pair stands
   one above the other): calls on the left of two wires, replies on the right; the wires run down the middle, so every
   pair in a figure shares one axis. A numbered run's numbers stand at the outer edge, in a column of their own */
.dia .xg { display: grid; grid-template-columns: minmax(0, 1fr) 40px minmax(0, 1fr); row-gap: 4px; padding-block: 12px; font-size: 13px; line-height: 1.5; }
.dia .xg .x { display: contents; }
.dia .w { display: flex; align-items: baseline; column-gap: 6px; min-width: 0; color: var(--ink-2); }
.dia .w.go { grid-column: 1; justify-self: end; color: var(--ink); }
.dia .w.go:has(.sq) { justify-self: stretch; justify-content: start; }
.dia .w.bk { grid-column: 3; }
.dia .w .hid { align-self: center; }
.dia .sq { min-width: 16px; font-variant-numeric: tabular-nums; color: var(--ink-2); }
/* across (wide): a call's words stand above its wire and the reply's below its own, the two wires close together,
   so each pair reads as words, two wires, words */
.dia[data-frame="wide"] .xg[data-dir="across"] { grid-template-columns: minmax(0, 1fr); row-gap: 0; align-self: start; padding: 40px 0 0 16px; }
.dia[data-frame="wide"] .xg[data-dir="across"] .w { grid-column: 1; justify-self: start; justify-content: start; }
.dia[data-frame="wide"] .xg[data-dir="across"] .w.go { padding-bottom: 4px; }
.dia[data-frame="wide"] .xg[data-dir="across"] .w.bk { margin-top: 16px; padding-top: 4px; }
.dia[data-frame="wide"] .xg[data-dir="across"] .x + .x .w.go { margin-top: 16px; }

/* the ground and the lines on it. The sand is lit by a filter in diagram.js; here are its colour and its strength.
   Lines take the drawing roles: a wire is structure, a wire that touches the lit part is information, the band is
   structure, dashed */
.dia .grain { flood-color: var(--line); }
.dia .sand { opacity: var(--dia-sand); }
.dia .cast { fill: var(--line); opacity: 0.08; }
.dia .mcp { stroke: var(--line); stroke-opacity: var(--dia-structure); stroke-width: 0.9; stroke-dasharray: 4 4; }
.dia .wire { fill: none; stroke: var(--line); stroke-opacity: var(--dia-structure); stroke-width: 0.9; }
.dia .wire.on { stroke-opacity: 1; }
.dia .wire.acc { stroke: var(--accent); stroke-opacity: var(--dia-information); stroke-width: 1.1; }
.dia .port { fill: var(--ground); stroke: var(--line); stroke-opacity: var(--dia-structure); stroke-width: 0.9; }
.dia .port.to { fill: var(--ink-2); stroke: var(--ink-2); stroke-opacity: 1; }
.dia .port.on { stroke-opacity: 1; }
.dia .port.on.to { fill: var(--line); stroke: var(--line); }
.dia .port.acc { stroke: var(--accent); stroke-opacity: var(--dia-information); }
.dia .port.acc.to { fill: var(--accent); }

/* a pick: the wires of the picked part light, and what it does not touch steps back */
.dia.picked .wire:not(.on), .dia.picked .port:not(.on) { opacity: 0.35; }
.dia.picked .n:not(.hot) { opacity: 0.62; }
.dia-tip { position: fixed; z-index: 30; inset: 0 auto auto 0; max-width: 320px; pointer-events: none; padding: 8px 12px; background: var(--ground); border: 1px solid var(--hair-2); border-radius: 6px; font-family: var(--font-text); font-size: 13px; line-height: 1.5; color: var(--ink-2); }
.dia-tip p { display: flex; column-gap: 8px; align-items: baseline; margin: 0; }
.dia-tip strong { flex: none; font-weight: 500; color: var(--ink); }
.dia-tip span { overflow-wrap: anywhere; }

/* narrow: each part on the axis, the band across it with the vendor's side named under it */
.dia:not([data-frame="wide"]) .n:not(.free) { justify-self: stretch; }
.dia:not([data-frame="wide"]) .zh[data-z="vendor"] { margin-top: -4px; }

/* wide: each row a grid of the figure's own columns: its name across the top, then the names of the two sides at the
   band, one each side of it, then its parts. A part's row in the data counts from the sides' names */
.dia[data-frame="wide"] .dia-row { grid-template-columns: var(--dia-cols); justify-content: center; column-gap: 0; row-gap: 0; align-items: start; }
.dia[data-frame="wide"] .dia-row > .it, .dia[data-frame="wide"] .dia-row > .xg { grid-column: var(--col); grid-row: calc(var(--row) + 1); }
.dia[data-frame="wide"] .rt { grid-column: 1 / -1; grid-row: 1; margin-bottom: 28px; }
.dia[data-frame="wide"] .zh, .dia[data-frame="wide"] .bnd { grid-row: 2; align-self: start; margin-bottom: 20px; }
.dia[data-frame="wide"] .zh[data-z="mine"] { grid-column: 1 / var(--dia-band); justify-self: end; justify-items: end; text-align: end; padding-right: 16px; }
.dia[data-frame="wide"] .zh[data-z="vendor"] { grid-column: calc(var(--dia-band) + 1) / -1; padding-left: 40px; }
.dia[data-frame="wide"] .bnd { grid-column: var(--dia-band); justify-items: center; padding-block: 0; }
.dia[data-frame="wide"] .xg[data-dir="down"] { padding-block: 16px; }

/* the still: the whole figure as an image, with its sand and shades, one a frame and a theme. It stands in the
   figure's place when scripts are off (check 6) and where the lighting filter cannot run */
.dia-still { display: none; }
.dia-still img { display: block; width: 100%; height: auto; }
.dia-still img.wide { display: none; }
@media (min-width: 1240px) { .dia-still img.wide { display: block; } .dia-still img.column { display: none; } }
:is(.figs-still, .no-js) .dia-still, .dia.is-still .dia-still { display: block; }
:is(.figs-still, .no-js) .dia-live, .dia.is-still .dia-live { display: none; }

/* in an article: the caption keeps the reading column, and from 1240 px the figure leaves the column and spans the page
   between its margins, on its own ground */
.dia .fig-cap { position: relative; z-index: 1; margin-top: 40px; }
.dia[data-frame="wide"] .fig-cap { margin-top: 64px; }
@media (min-width: 1240px) {
  .prose .dia { z-index: 1; width: min(1200px, calc(100vw - 2 * var(--margin))); margin-left: calc((100% - min(1200px, 100vw - 2 * var(--margin))) / 2); }
  /* room above for the sand that runs past the lit part, so it stays on the figure's own ground and never under the text
     before it or the contents list */
  .prose .dia { margin-top: 12px; padding-top: 88px; }
  /* its ground runs the page's whole width, so the contents list in the gutter passes behind it and is never half covered */
  .prose .dia::before { content: ""; position: absolute; z-index: -1; inset: 0 -100vw; background: var(--ground); }
  .prose .dia .fig-cap { max-width: var(--dia-col, 100%); margin-inline: auto; }
}

/* arrival: the wires draw once, towards the parts they call, when the figure first comes into view. Nothing moves after */
@media (prefers-reduced-motion: no-preference) {
  .js .dia:not(.is-in) .wire { stroke-dasharray: 1; stroke-dashoffset: 1; }
  .js .dia:not(.is-in) .port { opacity: 0; }
  .js .dia:not(.is-in) .rt::after { transform: scaleX(0); }
  .js .dia.is-in .rt::after { transition: transform 700ms var(--ease); }
  .js .dia.is-in .wire { stroke-dasharray: 1; stroke-dashoffset: 0; transition: stroke-dashoffset 500ms var(--ease) calc(var(--i, 0) * 40ms + 120ms); }
  .js .dia.is-in .port { transition: opacity 260ms linear 800ms; }
}
