:root {
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --paper: #ffffff;
  --wash: #f8fafc;
  --accent: #1d4ed8;
  --sans: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: var(--sans); color: var(--ink); background: var(--wash); display: flex; flex-direction: column; height: 100vh; }

.tw-top {
  display: flex; align-items: baseline; gap: 16px;
  padding: 14px 22px; background: var(--paper); border-bottom: 1px solid var(--line);
}
.tw-top h1 { font-family: var(--serif); font-size: 22px; margin: 0; letter-spacing: .01em; }
.tw-sub { color: var(--muted); font-size: 13px; margin: 0; }
.tw-tools { margin-left: auto; display: flex; gap: 8px; }
.tw-tools button {
  font: inherit; font-size: 13px; padding: 6px 12px; border: 1px solid var(--line);
  background: var(--paper); border-radius: 8px; cursor: pointer; color: var(--ink);
}
.tw-tools button:hover { background: var(--wash); border-color: #cbd5e1; }

.tw-main { flex: 1; display: flex; min-height: 0; }
.tw-side {
  width: 272px; flex-shrink: 0; background: var(--paper); border-right: 1px solid var(--line);
  overflow-y: auto; padding: 8px 0;
}
.tw-side section { padding: 12px 18px; border-bottom: 1px solid var(--line); }
.tw-side h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 0 0 10px; }
.tw-hint, .tw-muted { color: var(--muted); font-size: 12px; line-height: 1.5; margin: 4px 0; }

.tw-list { display: flex; flex-direction: column; gap: 6px; }
.tw-prof, .tw-ex, .tw-rule {
  font: inherit; font-size: 13px; text-align: left; padding: 8px 11px;
  border: 1px solid var(--line); background: var(--paper); border-radius: 8px; cursor: pointer;
  color: var(--ink); transition: background .12s, border-color .12s;
}
.tw-prof:hover, .tw-ex:hover, .tw-rule:hover { background: var(--wash); border-color: #cbd5e1; }
.tw-prof.tw-on, .tw-ex.tw-on { background: #eff6ff; border-color: #bfdbfe; color: var(--accent); font-weight: 600; }

#rules { display: flex; flex-direction: column; gap: 6px; }
.tw-rule { display: flex; justify-content: space-between; align-items: center; font-variant-numeric: tabular-nums; }
.tw-rule-here { border-color: #86efac; background: #f0fdf4; }
.tw-count { color: var(--muted); font-size: 11px; background: var(--wash); border-radius: 999px; padding: 1px 7px; }

.tw-canvas {
  flex: 1; min-width: 0; overflow: auto; display: flex; flex-direction: column; align-items: safe center; justify-content: safe center;
  gap: 14px;
  padding: 32px;
  background-color: var(--wash);
  background-image: radial-gradient(circle, #e2e8f0 1px, transparent 1px);
  background-size: 22px 22px;
}
.tw-proofbar { display: flex; flex-direction: column; align-items: center; gap: 10px; max-width: 660px; }
.tw-proofbar .tw-cap { max-width: 640px; text-align: center; color: var(--ink); font-size: 14px; line-height: 1.5; font-family: var(--serif); }
.tw-proofbar .tw-licence { font-size: 13px; color: #334155; }
.tw-einops { max-width: 640px; margin: 0; padding: 8px 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--paper); font-size: 11.5px; line-height: 1.55; color: #334155; text-align: left; overflow-x: auto; }
.tw-hintline { font-size: 12.5px; color: #047857; background: #ecfdf5; border: 1px solid #a7f3d0; border-radius: 999px; padding: 3px 12px; }
.tw-canvas svg { max-width: 100%; height: auto; background: var(--paper); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 1px 3px rgba(15,23,42,.06), 0 8px 24px rgba(15,23,42,.05); padding: 10px; }

.tw-node { transition: opacity 160ms cubic-bezier(.2,.7,.2,1), filter 120ms ease; }
.tw-node.tw-hover { filter: drop-shadow(0 0 2px rgba(15,23,42,.22)); }
.tw-node.tw-selected,
#canvas svg .tw-node:focus-visible { filter: drop-shadow(0 0 2.2px rgba(190,24,93,.42)); }
.tw-node.tw-suggest { filter: drop-shadow(0 0 3px rgba(0,158,115,.55)); }
#canvas svg path[data-wire] { transition: stroke 120ms ease; }
#canvas svg path[data-wire].tw-selected,
#canvas svg path[data-wire]:focus-visible { stroke: rgba(190,24,93,.22); stroke-width: 7; stroke-linecap: round; }

.tw-figure { display: flex; align-items: center; justify-content: flex-start; gap: 8px; flex-wrap: nowrap; background: var(--paper); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 1px 3px rgba(15,23,42,.06), 0 8px 24px rgba(15,23,42,.05); padding: 18px 26px; max-width: 100%; overflow-x: auto; }
.tw-figure.tw-multi { flex-wrap: wrap; justify-content: center; row-gap: 12px; width: min(100%, var(--tw-fig-w, 100%)); overflow-x: visible; }
/* panels shrink together (flex-basis = 0.8 * natural width, flex-grow = natural width)
   so they stay on one row down to 0.8, then wrap into the fewest rows 0.8 allows and
   each row grows uniformly back towards full size. Each sign stays paired with its panel. */
.tw-figure .tw-panel { flex: 0 1 auto; min-width: 0; }
.tw-figure .tw-pair { display: flex; align-items: center; gap: var(--tw-rel-gap, 8px); flex-wrap: nowrap; min-width: 0; }
.tw-figure .tw-pair .tw-rel { flex: 0 0 auto; }
.tw-figure .tw-pair .tw-panel { flex: 1 1 0; min-width: 0; }
.tw-figure .tw-panel svg { display: block; border: none; box-shadow: none; background: none; padding: 0; max-width: 100%; height: auto; }
.tw-figure.tw-multi .tw-panel svg { width: 100%; }
.tw-rel { font-family: var(--serif); font-size: var(--tw-rel-size, 26px); color: var(--ink); padding: 0 4px; }

.tw-status { font-size: 12px; color: var(--muted); align-self: center; margin-left: 4px; }
.tw-chyp { border-color: #ddd6fe; background: #f5f3ff; }
.tw-chyp:hover { background: #ede9fe; }
.tw-walk { display: flex; flex-direction: column; align-items: center; gap: 14px; max-width: 100%; }
.tw-walk .tw-frame { background: var(--paper); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 1px 3px rgba(15,23,42,.06), 0 8px 24px rgba(15,23,42,.05); padding: 14px; max-width: 100%; overflow-x: auto; }
.tw-walk .tw-frame svg { border: none; box-shadow: none; background: none; padding: 0; max-width: 100%; height: auto; max-height: 46vh; }
.tw-cap { max-width: 620px; text-align: center; color: var(--ink); font-size: 14px; line-height: 1.5; font-family: var(--serif); }
.tw-walk-ctl { display: flex; align-items: center; gap: 12px; font-variant-numeric: tabular-nums; color: var(--muted); }
.tw-walk-ctl button { font: inherit; font-size: 13px; padding: 6px 12px; border: 1px solid var(--line); background: var(--paper); border-radius: 8px; cursor: pointer; color: var(--ink); }
.tw-walk-ctl button:disabled { opacity: .4; cursor: default; }

.tw-goal { font-size: 13.5px; color: #065f46; background: #ecfdf5; border: 1px solid #6ee7b7; border-radius: 10px; padding: 8px 16px; max-width: 620px; text-align: center; font-family: var(--serif); }

/* Embed mode (?embed=1): iframed into a DLdiagrams page. Strip the standalone
   chrome — big title, theory switcher, and the chyp DPO oracle — leaving the
   demo (examples + rewrites + canvas) plus a slim undo/reset/export strip. The
   surrounding page supplies the title and fixes which theory is in view. */
.tw-embed .tw-top { padding: 8px 14px; }
.tw-embed .tw-top h1,
.tw-embed .tw-sub,
.tw-embed #chyp,
.tw-embed #chyp-status,
.tw-embed #proofs-sec { display: none; }
.tw-embed .tw-side section:has(#profiles) { display: none; }
.tw-embed .tw-side { width: 230px; }

/* The algebraic notation of a registry diagram, set beneath the picture. */
.tw-notation { display: none; }

/* Bare mode (?bare=1): the diagram IS the illustration. No chrome, no card,
   no backdrop: the embedding page's own background shows through, the SVG
   sits directly in the text like a printed figure that happens to move. */
body.tw-bare { height: auto; background: transparent; }
.tw-bare .tw-top, .tw-bare .tw-side { display: none; }
.tw-bare .tw-main { display: block; }
.tw-bare .tw-canvas {
  padding: 6px 4px 0; overflow: visible; background: none; background-image: none;
  display: flex; flex-direction: column; align-items: center; gap: 10px;
}
.tw-bare .tw-canvas svg { background: none; border: none; box-shadow: none; border-radius: 0; padding: 0; }
.tw-bare .tw-figure { background: none; border: none; box-shadow: none; padding: 0; margin: 0; justify-content: center; gap: var(--tw-rel-gap, 8px); }
.tw-bare .tw-figure.tw-multi { width: 100%; row-gap: 10px; }
.tw-bare .tw-notation {
  display: block; text-align: center; font-family: var(--serif); font-style: italic;
  font-size: 14.5px; color: #334155; padding: 8px 10px 10px; line-height: 1.5;
}
.tw-bare .tw-notation:empty { display: none; }
.tw-not-gloss { display: block; }   /* the words sit on their own line under the mathematics */
.tw-bare #wk-exit { display: none; }   /* a bare proof has no example to exit to */
#bare-reset {
  position: fixed; top: 4px; right: 6px; z-index: 5;
  font: inherit; font-size: 15px; line-height: 1; padding: 4px 7px;
  border: none; border-radius: 999px; background: rgba(148,163,184,0.14);
  color: #64748b; cursor: pointer; opacity: 0.55; transition: opacity .15s;
}
#bare-reset:hover { opacity: 1; background: rgba(148,163,184,0.28); }

/* Gallery proofsheet (?gallery=1): every registry diagram, stacked. */
.tw-gallery { background: #fff; padding: 20px 28px; }
.tw-gal { border-bottom: 1px solid var(--line); padding: 14px 0 18px; }
.tw-gal h3 { font-family: var(--serif); font-size: 15px; margin: 0 0 6px; }
.tw-gal h3 small { color: var(--muted); font-weight: 400; }
.tw-gal-row { display: flex; align-items: center; gap: 10px; flex-wrap: nowrap; }
.tw-gal-row .tw-panel { flex: 0 1 auto; min-width: 0; }
.tw-gal-row svg { max-width: 100%; height: auto; }
.tw-gal-not { font-family: var(--serif); font-style: italic; font-size: 13.5px; color: #334155; margin: 6px 0 0; }

/* learner boxes that open into their internals (training pages, ?open=) */
.tw-openable { cursor: pointer; }
.tw-openable:hover rect, .tw-openable:focus-visible rect { stroke: #E69F00; stroke-width: 2.5; }
.tw-openable:focus { outline: none; }

#tw-futures:empty { display: none; }
#tw-futures { display: flex; justify-content: safe center; flex-wrap: wrap; gap: .4rem; padding: .35rem; }
#tw-futures button { display: grid; grid-template-rows: 46px auto; align-items: center; justify-items: center; max-width: 15rem; padding: .3rem .6rem; background: var(--paper); color: var(--ink); border: 1px solid var(--line); border-radius: 5px; cursor: pointer; }
#tw-futures button svg { width: 100%; height: 44px; pointer-events: none; }
#tw-futures button.tw-aimed, #tw-futures button:focus-visible { border-color: rgba(190,24,93,.55); box-shadow: 0 0 0 2px rgba(190,24,93,.12); outline: none; }
.tw-exercise { text-align: center; max-width: 40rem; }
.tw-exercise p { margin: .35rem 0; }
.tw-exercise details svg { max-width: 100%; max-height: 110px; }
.tw-exercise ol { text-align: left; }
.tw-exercise button, .tw-exercise summary { cursor: pointer; }
.tw-exercise { margin-inline: auto; padding: .65rem; line-height: 1.45; }
.tw-exercise strong { font-family: var(--serif); font-size: 1.15rem; }
.tw-exercise button { font: inherit; font-size: .85rem; color: var(--ink); background: var(--paper); border: 1px solid var(--line); border-radius: .35rem; padding: .28rem .55rem; }
.tw-exercise button:disabled { color: var(--muted); cursor: default; }
.tw-exercise details, .tw-exercise label { font-size: .85rem; }
.tw-exercise details code { overflow-wrap: anywhere; }
/* An exercise on a page is a picture (Vincent, 1 Dec 2026: "way too many useless bits of text and buttons"):
   the diagram in hand, an equals sign, and the goal drawn faint until it is reached. The page carries the sentence.
   The standalone page keeps every control. The status line stays for screen readers. */
.tw-bare .tw-canvas:has(.tw-exercise) { flex-direction: row; flex-wrap: wrap; justify-content: center; gap: 4px 18px; }
.tw-bare .tw-canvas:has(.tw-exercise) #tw-futures { order: 2; flex-basis: 100%; }
.tw-bare .tw-exercise { display: flex; align-items: center; gap: 18px; margin: 0; padding: 0; max-width: none; }
.tw-bare .tw-exercise::before { content: "="; font: 1.3rem var(--serif); color: var(--ink); }
.tw-bare .tw-exercise > :not(.tw-ex-goal):not([role=status]) { display: none; }
.tw-bare .tw-exercise > [role=status] { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
.tw-bare .tw-ex-goal > summary { display: none; }
.tw-bare .tw-ex-goal svg { display: block; max-height: none; opacity: .32; pointer-events: none; transition: opacity .3s; }
.tw-bare .tw-ex-goal.reached svg { opacity: 1; }
.tw-drop-site { fill: none; stroke: var(--accent); stroke-width: 1.5; stroke-dasharray: 3 3; pointer-events: none; }
/* a drag must not paint a text selection over the figure's labels */
.tw-canvas svg { user-select: none; -webkit-user-select: none; }

/* Equation buttons: a few quiet letters beside the pointer. */
.tw-eq { position: fixed; z-index: 5; display: flex; flex-direction: column; flex-wrap: wrap; gap: 3px; align-content: flex-start; animation: tw-eq-in 160ms cubic-bezier(.2,.7,.2,1); }
.tw-eq button { font: italic .85rem/1 var(--serif); min-width: 1.9rem; padding: .22rem .45rem; color: var(--ink); background: rgba(255,255,255,.92); border: 1px solid var(--line); border-radius: 1rem; cursor: pointer; transition: border-color 120ms, background 120ms; }
.tw-eq button:hover, .tw-eq button:focus-visible { border-color: rgba(190,24,93,.55); background: var(--paper); outline: none; }
.tw-eq button:focus-visible { box-shadow: 0 0 0 2px rgba(190,24,93,.14); }
@keyframes tw-eq-in { from { opacity: 0; transform: translateX(-3px); } }
.tw-ghost { filter: grayscale(1); }
.tw-node.tw-going { opacity: .3; }
@media (prefers-reduced-motion: reduce) { .tw-eq { animation: none; } }
#canvas svg [tabindex]:focus,
#canvas svg [tabindex]:focus-visible { outline: none; }
.tw-wiring-port { cursor: grab; }
.tw-wiring-port:active { cursor: grabbing; }
.tw-wiring-halo { fill: rgba(190,24,93,.14); stroke: rgba(190,24,93,.55); stroke-width: 1.5; opacity: 0; transition: opacity 120ms ease; pointer-events: none; }
.tw-wiring-port:hover .tw-wiring-halo,
.tw-wiring-port:focus-visible .tw-wiring-halo,
.tw-wiring-port.tw-wiring-active .tw-wiring-halo { opacity: 1; }
.tw-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
