/* Explanation diagrams, drawn as pencil sketches: no boxes, no fills, no colour.
   Labels are in the small margin hand; only literal code stays in monospace.
   A tone is a mark, never a colour: good is ticked, bad is crossed, note is ringed or underlined, muted recedes. */
.dg { margin: var(--step-3) 0 0; max-width: 100%; }
.dg-caption { margin-top: var(--step-2); color: var(--pencil); font: 300 15px/1.6 var(--margin-hand); }
.dg-label { font: 300 17px/1.45 var(--margin-hand); overflow-wrap: anywhere; }
.dg-detail { color: var(--pencil); font: 300 15px/1.5 var(--margin-hand); overflow-wrap: anywhere; }
.dg code, .dg pre { font-size: 14px; line-height: 1.6; }
/* Literal code inside a handwritten label is set in monospace, a little smaller than the hand around it. */
.dg code:not([class]) { font-size: .84em; line-height: 1; }

.dg [data-tone="good"], .dg [data-tone="note"] { color: var(--marked); }
.dg [data-tone="muted"], .dg [data-tone="muted"] .dg-label { color: var(--receded); }
.dg [data-tone="muted"] .dg-detail { color: var(--faint); }

:is(.dg-node, .dg-step, .dg-tree__node, .dg-ref__value)[data-tone="good"] .dg-label::after,
:is(.dg-node, .dg-step, .dg-tree__node, .dg-ref__value)[data-tone="bad"] .dg-label::after,
.dg-panel:is([data-tone="good"], [data-tone="bad"]) .dg-panel__title::after,
.dg-layer:is([data-tone="good"], [data-tone="bad"]) .dg-layer__label::after,
.dg-line:is([data-tone="good"], [data-tone="bad"]) .dg-line__code::after,
.dg-token:is([data-tone="good"], [data-tone="bad"]) .dg-token__text::after,
:is(.dg-event, .dg-ref__name, .dg-table td, .dg-table th):is([data-tone="good"], [data-tone="bad"])::after {
  content: "";
  display: inline-block;
  width: 15px;
  height: 13px;
  margin-left: 8px;
  vertical-align: -1px;
  background: var(--check) center / contain no-repeat;
}
:is(.dg-node, .dg-step, .dg-tree__node, .dg-ref__value)[data-tone="bad"] .dg-label::after,
.dg-panel[data-tone="bad"] .dg-panel__title::after,
.dg-layer[data-tone="bad"] .dg-layer__label::after,
.dg-line[data-tone="bad"] .dg-line__code::after,
.dg-token[data-tone="bad"] .dg-token__text::after,
:is(.dg-event, .dg-ref__name, .dg-table td, .dg-table th)[data-tone="bad"]::after {
  background-image: var(--cross);
}
:is(.dg-node, .dg-step, .dg-tree__node, .dg-ref__value)[data-tone="note"] .dg-label,
.dg-panel[data-tone="note"] .dg-panel__title,
.dg-event[data-tone="note"] {
  padding-bottom: 4px;
  background: var(--sketch-rule) left bottom / 100% 6px no-repeat;
  -webkit-box-decoration-break: clone;
  box-decoration-break: clone;
}

/* annotate: lines of code with notes in the margin, each pointing back at its line. */
.dg-annotate { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 2px 20px; align-items: baseline; }
.dg-line { display: contents; }
.dg-line__code { grid-column: 1; white-space: pre; color: var(--secondary-ink); }
.dg-line[data-noted] .dg-line__code { color: var(--marked); }
.dg-line__note { grid-column: 2; padding-left: 34px; color: var(--pencil); font: 300 15px/1.5 var(--margin-hand); background: var(--note-arrow) left 6px / 24px 14px no-repeat; }
.dg-line[data-tone="muted"] .dg-line__code { color: var(--receded); }

/* compare: accounts side by side, parted by a pencil stroke. */
.dg-compare { display: flex; flex-wrap: wrap; gap: 20px 0; }
.dg-panel { position: relative; flex: 1 1 200px; min-width: 0; padding-right: 28px; }
.dg-panel + .dg-panel { padding-left: 28px; }
.dg-panel + .dg-panel::before { content: ""; position: absolute; inset: 2px auto 2px 0; width: 10px; background: var(--pencil-edge) center / 100% 100% no-repeat; }
.dg-panel__title { display: inline; font: 21px/1.5 var(--hand); }
.dg-panel__lines { margin: 8px 0 0; overflow-x: auto; color: var(--secondary-ink); white-space: pre-wrap; overflow-wrap: anywhere; }
.dg-panel .dg-detail { margin-top: 8px; }

/* flow: words joined by small arrows. */
.dg-flow { display: flex; flex-wrap: wrap; align-items: center; gap: 14px 18px; }
.dg-node { max-width: 15em; }
.dg-arrow { display: flex; flex-direction: column; align-items: center; gap: 2px; color: var(--pencil); font: 300 14px/1.3 var(--margin-hand); }
.dg-arrow svg { width: 30px; height: 17px; }

/* layers: strata, one ruled off from the next. */
.dg-layers { max-width: 34em; padding-bottom: 8px; background: var(--sketch-rule) left bottom / 100% 8px no-repeat; }
.dg-layer { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 0 24px; padding: 13px 4px 5px; background: var(--sketch-rule) left top / 100% 8px no-repeat; }
.dg-layer__label { color: var(--ink); overflow-wrap: anywhere; }
.dg-layer[data-tone="good"] .dg-layer__label, .dg-layer[data-tone="note"] .dg-layer__label { color: var(--marked); }
.dg-layer[data-tone="muted"] .dg-layer__label { color: var(--receded); }
.dg-layer__detail { color: var(--pencil); font: 300 15px/1.5 var(--margin-hand); }

/* refs: names on the left, what they point at on the right, joined by drawn arrows. */
.dg-refs { position: relative; display: grid; grid-template-columns: max-content minmax(0, max-content); align-items: center; gap: 12px clamp(56px, 14vw, 120px); }
.dg-ref__names, .dg-ref__values { display: grid; gap: 14px; }
.dg-ref__name { justify-self: end; font: 14px/1.6 var(--mono); }
.dg-ref__name[data-dangling] { color: var(--receded); background: var(--strike) left 50% / 100% 8px no-repeat; }
.dg-ref__wires { position: absolute; inset: 0; width: 100%; height: 100%; }

/* steps: a numbered trace hanging from one stroke. */
.dg-steps { display: grid; gap: 12px; }
.dg-step { position: relative; display: flex; align-items: baseline; gap: 16px; }
.dg-step:not(:last-child)::before { content: ""; position: absolute; inset: 30px auto -10px 4px; width: 10px; background: var(--pencil-edge) center / 100% 100% no-repeat; }
.dg-step__n { flex: none; width: 18px; color: var(--pencil); font: 300 17px/1.45 var(--margin-hand); text-align: center; }

/* table: ruled only under its heading; a cell that matters is ringed. */
.dg-scroll { overflow-x: auto; padding: 4px 0; }
.dg-table { border-collapse: collapse; font: 300 16px/1.5 var(--margin-hand); }
.dg-table th, .dg-table td { padding: 7px 26px 7px 10px; text-align: left; font-weight: 300; white-space: nowrap; }
.dg-table thead th { padding-bottom: 12px; color: var(--pencil); font: 11px/1.8 var(--mono); background: var(--sketch-rule) left bottom / 100% 7px no-repeat; }
.dg-table tbody th { color: var(--secondary-ink); }
.dg-table tbody [data-tone="note"], .dg-table tbody [data-tone="good"] { background: var(--ring) center / 100% 100% no-repeat; }

/* timeline: one lifeline per lane, time running down the page. */
.dg-timeline { display: grid; column-gap: 20px; }
.dg-lane__name { padding-bottom: 6px; font: 20px/1.5 var(--hand); overflow-wrap: anywhere; }
.dg-lane__cell { min-height: 38px; padding: 7px 0 7px 26px; background: var(--pencil-edge) left top / 10px 100% no-repeat; }
.dg-event { font: 300 16px/1.45 var(--margin-hand); overflow-wrap: anywhere; }
.dg-lane__cell:has(.dg-event) { background: var(--dash) 7px 14px / 14px 7px no-repeat, var(--pencil-edge) left top / 10px 100% no-repeat; }

/* tokens: each piece sits on its own bracket. */
.dg-tokens { display: grid; gap: 18px; }
.dg-tokens__label { margin-bottom: 4px; color: var(--pencil); font: 300 15px/1.5 var(--margin-hand); }
.dg-tokens__chips { display: flex; flex-wrap: wrap; gap: 12px 8px; }
.dg-token { display: inline-flex; flex-direction: column; align-items: center; min-width: 26px; }
.dg-token__text { padding: 0 6px 9px; background: var(--bracket) left bottom / 100% 7px no-repeat; font: 15px/1.6 var(--mono); white-space: pre; }
.dg-token__space { color: var(--faint); }
.dg-token__sub { color: var(--pencil); font: 300 13px/1.5 var(--margin-hand); }
.dg-token[data-tone="note"] .dg-token__text { padding: 3px 10px 6px; margin-bottom: 0; background: var(--ring) center / 100% 100% no-repeat; }

/* tree: children hang from their parent on a drawn elbow. */
.dg-tree__children { padding-left: 14px; }
.dg-tree__children > .dg-tree__item { padding-left: 26px; background: var(--elbow) left 0 / 20px 28px no-repeat; }
.dg-tree__node { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0 14px; padding: 5px 0; }

@media (max-width: 700px) {
  .dg-annotate { grid-template-columns: minmax(0, 1fr); gap: 0; }
  .dg-line__code { white-space: pre-wrap; overflow-wrap: anywhere; }
  .dg-line__code, .dg-line__note { grid-column: 1; }
  .dg-line__note { margin: 0 0 8px 12px; }
  .dg-panel, .dg-panel + .dg-panel { flex-basis: 100%; padding: 0 0 0 22px; }
  .dg-panel:first-child { padding-left: 0; }
  .dg-panel + .dg-panel { padding-left: 0; padding-top: 6px; }
  .dg-panel + .dg-panel::before { display: none; }
  .dg-flow { flex-direction: column; align-items: flex-start; gap: 8px; }
  .dg-arrow { flex-direction: row; gap: 10px; padding-left: 6px; }
  .dg-arrow svg { transform: rotate(90deg); width: 24px; height: 14px; }
  .dg-timeline { column-gap: 18px; }
  .dg-lane__name { font-size: 17px; }
  .dg-lane__cell { padding-left: 20px; }
  .dg-event { font-size: 14px; }
  /* A table never hides a column off the side of a phone: its cells wrap instead, and only a single long word scrolls. */
  .dg-table { font-size: 15px; }
  .dg-table th, .dg-table td { min-width: 4.5em; padding: 6px 14px 6px 6px; white-space: normal; overflow-wrap: break-word; }
  .dg-table th:first-child, .dg-table td:first-child { padding-left: 0; }
  .dg-table tbody :is([data-tone="note"], [data-tone="good"]) { padding-inline: 12px; }
}
