/*
 * Answer block: ```diagram - a figure of a physical setup, drawn from the
 * small SVG the model writes with the class vocabulary below (taught in
 * answer_blocks/diagram.py, allowlisted in blocks/diagram.js).
 *
 * Every colour is a blocks.css token, so both themes and any palette apply:
 *   lines    ab-line, ab-thick, ab-thin (dashed guide), ab-path (dashed
 *            motion path), ab-dim (dimension line, measure red)
 *   shapes   ab-solid, ab-surface (floor), ab-hatch (wall, ceiling), ab-wood,
 *            ab-metal, ab-water, ab-ball, ab-ghost (an earlier position),
 *            ab-dot, ab-soft (a pale fill that takes its role's colour)
 *   text     ab-sym (italic symbol), ab-label, ab-note (small mono note)
 *   roles    ab-measure, ab-calc, ab-muted, ab-good, ab-warn: the stroke of a
 *            line or shape, the fill of a label; the same key as the text
 * Materials are the only colours minted here (--ab-dg-*). They are declared
 * on the figure, so a palette restates them on the figure too
 * ([data-ab-palette="mono"] .ab-dg in style.css).
 *
 * Sizes are in viewBox units scaled by --ab-dg-u (diagram.js sets it from the
 * viewBox), so a label is the same size on the page whatever units the
 * model drew in. Inline styles the model set (font-size="18") win.
 */
.ab-dg,
.ab-dg-overlay {
  --ab-dg-wood: #7d6343;
  --ab-dg-wood-edge: #a9855a;
  --ab-dg-metal: #8c9690;
  --ab-dg-metal-edge: #a7b0ab;
  --ab-dg-water: rgba(96, 150, 214, 0.22);
  --ab-dg-water-edge: #6f9fd6;
}
/* A printed page (page.js .ab-page-print) is Aluma's light look whatever the
   app shows, so its figures take these over the dark ones and over a
   palette's (one ID of weight, as the chat's table rules carry). */
body[data-theme="light"] .ab-dg,
body[data-theme="light"] .ab-dg-overlay,
body:not(#_) .ab-page-print .ab-dg {
  --ab-dg-wood: #ddbb88;
  --ab-dg-wood-edge: #ae8750;
  --ab-dg-metal: #6b7570;
  --ab-dg-metal-edge: #59625d;
  --ab-dg-water: #dbe8f6;
  --ab-dg-water-edge: #5b8cc4;
}

/* ── The figure: a quiet card, a caption under it like a book figure ─── */
.ab-dg {
  margin: 18px 0 20px;
  padding: 0;
  min-width: 0;
  max-width: 100%;
}
.ab-dg-plate {
  background: var(--ab-card);
  border: 1px solid var(--ab-hair);
  border-radius: var(--ab-radius);
  overflow: hidden;
}
.ab-dg-frame {
  overflow-x: auto;
  overflow-y: hidden;
  overscroll-behavior-x: contain;
  -webkit-overflow-scrolling: touch;
  padding: 16px 18px 12px;
  cursor: zoom-in;
  border-radius: inherit;
  scrollbar-width: thin;
}
/* On a phone the drawing is wider than the column: the side with more of it
   fades, so there is visibly more to scroll to (diagram.js sets the classes
   from the scroll position). */
.ab-dg-frame.is-more-right,
.ab-dg-ov-frame.is-more-right {
  -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 40px), transparent);
          mask-image: linear-gradient(to right, #000 calc(100% - 40px), transparent);
}
.ab-dg-frame.is-more-left,
.ab-dg-ov-frame.is-more-left {
  -webkit-mask-image: linear-gradient(to left, #000 calc(100% - 40px), transparent);
          mask-image: linear-gradient(to left, #000 calc(100% - 40px), transparent);
}
.ab-dg-frame.is-more-left.is-more-right,
.ab-dg-ov-frame.is-more-left.is-more-right {
  -webkit-mask-image: linear-gradient(to right, transparent, #000 40px, #000 calc(100% - 40px), transparent);
          mask-image: linear-gradient(to right, transparent, #000 40px, #000 calc(100% - 40px), transparent);
}
/* Drawn inside the edge: a newest reply has paint containment, which cuts
   a ring drawn outside it. */
.ab-dg-frame:focus-visible {
  outline: 2px solid var(--ab-accent);
  outline-offset: -3px;
}
.ab-dg-svg {
  display: block;
  width: 100%;
  height: auto;
  margin: 0 auto;
  overflow: hidden;
  color: var(--ab-ink);
  fill: var(--ab-ink);
}

.ab-dg-cap {
  display: flex;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 6px 14px;
  margin: 8px 2px 0;
}
.ab-dg-words {
  flex: 1 1 220px;
  min-width: 0;
  padding-top: 5px;
  font-family: var(--cc-serif, 'Source Serif 4', Georgia, 'Times New Roman', serif);
  font-size: 14.5px;
  line-height: 1.5;
  color: var(--ab-muted);
  overflow-wrap: anywhere;
}
.ab-dg-title { color: var(--ab-ink); font-weight: 600; }
.ab-dg-tools {
  display: inline-flex;
  flex: 0 0 auto;
  gap: 2px;
  margin-left: auto;
}
body .ab-dg-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 30px;
  margin: 0;
  padding: 0 11px 0 9px;
  border: 1px solid transparent;
  border-radius: 999px;
  background: transparent;
  box-shadow: none;
  color: var(--ab-muted);
  font: 500 12.5px/1 var(--ab-font-ui);
  letter-spacing: 0;
  text-transform: none;
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}
body .ab-dg-btn:hover {
  color: var(--ab-ink);
  background: color-mix(in srgb, var(--ab-ink) 7%, transparent);
}
body .ab-dg-btn:focus-visible {
  outline: 2px solid var(--ab-accent);
  outline-offset: 1px;
}
body .ab-dg-btn.is-done { color: var(--ab-good); }
.ab-dg-ico {
  width: 16px;
  height: 16px;
  flex: 0 0 auto;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ── Inside the drawing ─────────────────────────────────────────────────
   Bare elements first, so a shape the model left unclassed still reads in
   both themes; then the vocabulary; then the roles, which win over both. */
.ab-dg-svg text {
  font-family: var(--ab-font-ui);
  font-size: calc(15px * var(--ab-dg-u, 1));
  font-weight: 450;
  fill: var(--ab-ink);
  /* A halo in the card's colour keeps a label legible where it crosses a
     line or a dashed path. */
  paint-order: stroke fill;
  stroke: var(--ab-card);
  stroke-width: calc(3px * var(--ab-dg-u, 1));
  stroke-linejoin: round;
}
.ab-dg-svg tspan { paint-order: stroke fill; }
.ab-dg-svg line,
.ab-dg-svg polyline {
  fill: none;
  stroke: var(--ab-ink);
  stroke-width: calc(2px * var(--ab-dg-u, 1));
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Lines */
.ab-dg-svg .ab-line,
.ab-dg-svg .ab-thick {
  fill: none;
  stroke: var(--ab-ink);
  stroke-width: calc(2px * var(--ab-dg-u, 1));
  stroke-linecap: round;
  stroke-linejoin: round;
}
.ab-dg-svg .ab-thick { stroke-width: calc(3.5px * var(--ab-dg-u, 1)); }
.ab-dg-svg .ab-thin {
  fill: none;
  stroke: var(--ab-muted);
  stroke-width: calc(1.2px * var(--ab-dg-u, 1));
  stroke-dasharray: var(--ab-dg-dash-thin, 3px 4.5px);
  stroke-linecap: round;
}
.ab-dg-svg .ab-path {
  fill: none;
  stroke: var(--ab-muted);
  stroke-width: calc(2px * var(--ab-dg-u, 1));
  stroke-dasharray: var(--ab-dg-dash-path, 6px 7px);
  stroke-linecap: round;
}
.ab-dg-svg .ab-dim {
  fill: none;
  stroke: var(--ab-measure);
  stroke-width: calc(1.8px * var(--ab-dg-u, 1));
  stroke-linecap: round;
}

/* Shapes */
.ab-dg-svg .ab-solid {
  fill: var(--ab-paper);
  stroke: var(--ab-ink);
  stroke-width: calc(1.5px * var(--ab-dg-u, 1));
  stroke-linejoin: round;
}
.ab-dg-svg .ab-surface { fill: var(--ab-rule); stroke: none; }
.ab-dg-svg .ab-hatch { stroke: none; }
.ab-dg-svg .ab-dg-hatch-bg { fill: var(--ab-card); stroke: none; }
.ab-dg-svg .ab-dg-hatch-line {
  stroke: var(--ab-muted);
  stroke-width: calc(1.2px * var(--ab-dg-u, 1));
  opacity: 0.75;
}
.ab-dg-svg .ab-wood {
  fill: var(--ab-dg-wood);
  stroke: var(--ab-dg-wood-edge);
  stroke-width: calc(1.5px * var(--ab-dg-u, 1));
  stroke-linejoin: round;
}
.ab-dg-svg .ab-metal {
  fill: var(--ab-dg-metal);
  stroke: var(--ab-dg-metal-edge);
  stroke-width: calc(1.5px * var(--ab-dg-u, 1));
  stroke-linejoin: round;
}
.ab-dg-svg .ab-water {
  fill: var(--ab-dg-water);
  stroke: var(--ab-dg-water-edge);
  stroke-width: calc(1.5px * var(--ab-dg-u, 1));
  stroke-linejoin: round;
}
/* A line in a material is a rod seen from the side: a barrel, a stick, a pipe. */
.ab-dg-svg :is(line, polyline).ab-wood,
.ab-dg-svg :is(line, polyline).ab-metal {
  fill: none;
  stroke-width: calc(12px * var(--ab-dg-u, 1));
  stroke-linecap: round;
}
.ab-dg-svg :is(line, polyline).ab-wood { stroke: var(--ab-dg-wood); }
.ab-dg-svg :is(line, polyline).ab-metal { stroke: var(--ab-dg-metal); }
.ab-dg-svg .ab-ball {
  fill: var(--ab-card);
  stroke: var(--ab-ink);
  stroke-width: calc(1.6px * var(--ab-dg-u, 1));
}
.ab-dg-svg .ab-ghost {
  fill: var(--ab-card);
  stroke: var(--ab-muted);
  stroke-width: calc(1.2px * var(--ab-dg-u, 1));
  opacity: 0.6;
}
.ab-dg-svg .ab-dot { fill: var(--ab-ink); stroke: none; }
.ab-dg-svg .ab-soft {
  fill: var(--ab-hair);
  stroke: var(--ab-rule);
  stroke-width: calc(1.2px * var(--ab-dg-u, 1));
  stroke-linejoin: round;
}

/* Text */
.ab-dg-svg .ab-sym {
  font-family: var(--ab-font-math);
  font-style: italic;
  font-weight: 400;
  font-size: calc(24px * var(--ab-dg-u, 1));
}
.ab-dg-svg .ab-label {
  font-family: var(--ab-font-ui);
  font-size: calc(15px * var(--ab-dg-u, 1));
  font-weight: 550;
}
.ab-dg-svg .ab-note {
  font-family: var(--ab-font-mono);
  font-size: calc(12.5px * var(--ab-dg-u, 1));
  font-weight: 400;
  fill: var(--ab-muted);
}

/* Roles: the same key as the reply's text, its tables and its charts. A
   line or a shape takes the colour as its stroke, a label as its fill. */
.ab-dg-svg .ab-measure:not(text):not(tspan) { stroke: var(--ab-measure); }
.ab-dg-svg .ab-calc:not(text):not(tspan) { stroke: var(--ab-calc); }
.ab-dg-svg .ab-muted:not(text):not(tspan) { stroke: var(--ab-muted); }
.ab-dg-svg .ab-good:not(text):not(tspan) { stroke: var(--ab-good); }
.ab-dg-svg .ab-warn:not(text):not(tspan) { stroke: var(--ab-warn); }
.ab-dg-svg :is(text, tspan).ab-measure { fill: var(--ab-measure); }
.ab-dg-svg :is(text, tspan).ab-calc { fill: var(--ab-calc); }
.ab-dg-svg :is(text, tspan).ab-muted { fill: var(--ab-muted); }
.ab-dg-svg :is(text, tspan).ab-good { fill: var(--ab-good); }
.ab-dg-svg :is(text, tspan).ab-warn { fill: var(--ab-warn); }
.ab-dg-svg .ab-dot.ab-measure { fill: var(--ab-measure); }
.ab-dg-svg .ab-dot.ab-calc { fill: var(--ab-calc); }
.ab-dg-svg .ab-dot.ab-muted { fill: var(--ab-muted); }
.ab-dg-svg .ab-dot.ab-good { fill: var(--ab-good); }
.ab-dg-svg .ab-dot.ab-warn { fill: var(--ab-warn); }
.ab-dg-svg .ab-soft.ab-measure { fill: var(--ab-measure-soft); stroke: var(--ab-measure); }
.ab-dg-svg .ab-soft.ab-calc { fill: var(--ab-calc-soft); stroke: var(--ab-calc); }
.ab-dg-svg .ab-soft.ab-good { fill: var(--ab-good-soft); stroke: var(--ab-good); }
.ab-dg-svg .ab-soft.ab-warn { fill: var(--ab-warn-soft); stroke: var(--ab-warn); }
.ab-dg-svg .ab-soft.ab-muted { fill: var(--ab-hair); stroke: var(--ab-muted); }

/* Arrowheads (diagram.js draws one marker per colour in use). The thin
   round-joined edge takes the point off the tip. */
.ab-dg-svg .ab-dg-head { stroke-width: 1.3; stroke-linejoin: round; }
.ab-dg-svg .ab-dg-head-ink { fill: var(--ab-ink); stroke: var(--ab-ink); }
.ab-dg-svg .ab-dg-head-measure { fill: var(--ab-measure); stroke: var(--ab-measure); }
.ab-dg-svg .ab-dg-head-calc { fill: var(--ab-calc); stroke: var(--ab-calc); }
.ab-dg-svg .ab-dg-head-muted { fill: var(--ab-muted); stroke: var(--ab-muted); }
.ab-dg-svg .ab-dg-head-good { fill: var(--ab-good); stroke: var(--ab-good); }
.ab-dg-svg .ab-dg-head-warn { fill: var(--ab-warn); stroke: var(--ab-warn); }

/* ── Expand: the figure larger, over the page ───────────────────────── */
body.ab-dg-open { overflow: hidden; }
.ab-dg-overlay {
  position: fixed;
  inset: 0;
  z-index: 2147483000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(16px, env(safe-area-inset-top, 0px)) max(16px, env(safe-area-inset-right, 0px))
    max(16px, env(safe-area-inset-bottom, 0px)) max(16px, env(safe-area-inset-left, 0px));
  background: color-mix(in srgb, #000 52%, transparent);
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
}
body[data-theme="light"] .ab-dg-overlay { background: color-mix(in srgb, #0d1612 38%, transparent); }
.ab-dg-ov-box {
  display: flex;
  flex-direction: column;
  width: min(1180px, 100%);
  max-height: 100%;
  background: var(--ab-card);
  border: 1px solid var(--ab-hair);
  border-radius: calc(var(--ab-radius) + 6px);
  box-shadow: 0 24px 60px rgb(0 0 0 / 0.35), 0 4px 14px rgb(0 0 0 / 0.18);
  overflow: hidden;
}
.ab-dg-ov-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 10px 10px 20px;
  border-bottom: 1px solid var(--ab-hair);
}
.ab-dg-ov-title {
  flex: 1 1 auto;
  min-width: 0;
  margin: 0;
  font-family: var(--cc-serif, 'Source Serif 4', Georgia, 'Times New Roman', serif);
  font-size: 16px;
  font-weight: 600;
  line-height: 1.35;
  color: var(--ab-ink);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
/* Class names here avoid the words rounded.css rounds by substring ("tag",
   "card", "panel", "chip", "pill"...): a "stage" there became a 999px pill
   whose corners cut the drawing off. The radius is still pinned. */
.ab-dg-ov-frame {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  padding: clamp(14px, 3vw, 32px);
  border-radius: 0;
}
.ab-dg-ov-frame .ab-dg-svg {
  max-width: none !important;
  max-height: calc(100dvh - 150px);
}
.ab-dg-ov-frame:focus-visible { outline: 2px solid var(--ab-accent); outline-offset: -3px; }

@media (prefers-reduced-motion: no-preference) {
  .ab-dg-overlay { animation: ab-dg-fade 0.16s ease-out; }
  .ab-dg-ov-box { animation: ab-dg-rise 0.2s cubic-bezier(0.2, 0.8, 0.2, 1); }
}
@keyframes ab-dg-fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes ab-dg-rise { from { opacity: 0; transform: translateY(8px) scale(0.985); } to { opacity: 1; transform: none; } }

/* Phones: the drawing keeps its legible width and scrolls inside its card;
   the caption buttons drop their words. */
@media (max-width: 480px) {
  .ab-dg-frame { padding: 12px 12px 8px; }
  .ab-dg-words { font-size: 14px; }
  body .ab-dg-btn { padding: 0 9px; }
  .ab-dg-cap .ab-dg-btn span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
  .ab-dg-cap .ab-dg-btn { width: 34px; height: 34px; padding: 0; justify-content: center; }
  .ab-dg-ov-head { padding-left: 16px; }
  .ab-dg-ov-head .ab-dg-save span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
}
/* On a phone the larger view really is larger: the drawing at one and a
   half times its inline width, panned inside the frame. */
@media (max-width: 600px) {
  .ab-dg-ov-frame .ab-dg-svg {
    min-width: calc(var(--ab-dg-minw, 547px) * 1.5) !important;
    max-height: none;
  }
  .ab-dg-ov-title {
    white-space: normal;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    font-size: 15px;
  }
}
@media (forced-colors: active) {
  .ab-dg-svg text { stroke: none; }
}
