/* ==========================================================================
   Interactive chapter diagrams (Mermaid)

   Progressive enhancement: the <pre class="mermaid"> source renders as a plain
   code block if JS or the CDN is unavailable, so nothing is ever blank.

   Visual language mirrors the rest of the site: Syne for labels, DM Sans for
   supporting text, purple/teal accents, and a staged entrance that draws the
   diagram in the order a reader would follow it.
   ========================================================================== */

.diagram {
  --dg-accent: #a855f7;
  --dg-accent-2: #2dd4bf;
  --dg-surface: #101019;
  position: relative;
  margin: 2.4rem 0 1.1rem;
  border: 1px solid rgba(255, 255, 255, 0.09);
  border-radius: 18px;
  background:
    radial-gradient(120% 90% at 12% -10%, rgba(124, 58, 237, 0.14), transparent 60%),
    radial-gradient(110% 90% at 100% 110%, rgba(45, 212, 191, 0.10), transparent 62%),
    var(--dg-surface);
  overflow: hidden;
  box-shadow: 0 18px 46px rgba(0, 0, 0, 0.34);
}

/* hairline of brand colour along the top edge */
/* Diagrams read better a little wider than the prose column. Bleed outward,
   but never past the viewport, and never on small screens. */
@media (min-width: 900px) {
  .diagram {
    width: calc(100% + 8rem);
    max-width: calc(100vw - 3rem);
    margin-left: -4rem;
    margin-right: -4rem;
  }
  .diagram.is-expanded { width: auto; max-width: none; margin: 0; }
}

/* The member dashboard content is narrower and offset by the sidebar, so the
   viewport-based bleed above would push past the right edge. Keep diagrams to
   the container there. */
.dashboard-content .diagram {
  width: 100%;
  max-width: 100%;
  margin-left: 0;
  margin-right: 0;
}

.diagram::before {
  content: "";
  position: absolute;
  inset: 0 0 auto;
  height: 2px;
  background: linear-gradient(90deg, var(--dg-accent), var(--dg-accent-2));
  opacity: 0.75;
  z-index: 2;
}

/* --- pre-render state: source stays readable but quiet ------------------ */
.diagram pre.mermaid {
  margin: 0;
  padding: 1.2rem 1.35rem;
  font-family: ui-monospace, Consolas, "JetBrains Mono", monospace;
  font-size: 0.74rem;
  line-height: 1.55;
  color: #6e6e8a;
  white-space: pre;
  overflow-x: auto;
}

/* --- rendered state: the <pre> becomes the stage ------------------------ */
.diagram.is-ready pre.mermaid {
  padding: 1.9rem 1.25rem;
  overflow: hidden;
  cursor: grab;
  touch-action: none;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 380px;
}

.diagram.is-ready pre.mermaid:active { cursor: grabbing; }
.diagram.is-ready pre.mermaid:focus-visible { outline: 2px solid var(--dg-accent-2); outline-offset: -2px; }

.diagram.is-ready pre.mermaid > svg {
  max-width: none;
  height: auto;
  display: block;
  transform-origin: center center;
  will-change: transform;
  user-select: none;
  overflow: visible;
}

/* ==========================================================================
   Diagram internals
   ========================================================================== */

/* --- typography --------------------------------------------------------- */
/* Type only. Colour is deliberately left to Mermaid so that per-node
   `style X fill:...,color:...` directives keep their contrast — a light fill
   with forced light text would be unreadable. */
.diagram svg .nodeLabel,
.diagram svg .nodeLabel p {
  font-family: 'Syne', var(--font-display, system-ui), sans-serif !important;
  font-weight: 700 !important;
  font-size: 14.5px !important;
  letter-spacing: -0.005em;
  line-height: 1.34 !important;
}

.diagram svg .edgeLabel,
.diagram svg .edgeLabel p {
  font-family: 'DM Sans', system-ui, sans-serif !important;
  font-weight: 500 !important;
  font-size: 12.5px !important;
  color: #b9b9d2 !important;
  fill: #b9b9d2 !important;
}

/* edge labels sit on the line — give them a solid plate so they stay legible */
.diagram svg .edgeLabel rect,
.diagram svg .edgeLabel .labelBkg {
  fill: var(--dg-surface) !important;
  opacity: 1 !important;
}

.diagram svg .cluster-label,
.diagram svg .cluster-label p {
  font-family: 'Syne', var(--font-display, system-ui), sans-serif !important;
  font-weight: 800 !important;
  font-size: 11.5px !important;
  letter-spacing: 0.11em !important;
  text-transform: uppercase;
  color: #c6c6dd;
  fill: #c6c6dd;
}

/* --- node shapes -------------------------------------------------------- */
.diagram svg .node rect,
.diagram svg .node polygon,
.diagram svg .node circle,
.diagram svg .node path {
  rx: 11px;
  ry: 11px;
  stroke-width: 1.4px !important;
  filter: drop-shadow(0 6px 14px rgba(0, 0, 0, 0.42));
  transition: filter 0.22s ease, stroke 0.22s ease;
}

.diagram svg .node:hover rect,
.diagram svg .node:hover polygon,
.diagram svg .node:hover circle {
  stroke: var(--dg-accent-2) !important;
  filter: drop-shadow(0 0 12px rgba(45, 212, 191, 0.35)) drop-shadow(0 6px 14px rgba(0, 0, 0, 0.42));
}

/* --- clusters (subgraph containers) ------------------------------------- */
.diagram svg .cluster rect {
  rx: 15px;
  ry: 15px;
  stroke-dasharray: 5 4;
  stroke-width: 1.1px !important;
  opacity: 0.96;
}

/* the cluster title sits on the container's top edge — give it an opaque
   plate so the border never strikes through the words */
.diagram svg .cluster-label foreignObject > div,
.diagram svg .cluster-label span.nodeLabel {
  background: var(--dg-plate, var(--dg-surface));
  padding: 2px 9px;
  border-radius: 6px;
}

/* --- edges -------------------------------------------------------------- */
.diagram svg .flowchart-link,
.diagram svg .edgePath .path,
.diagram svg .relationshipLine {
  stroke-width: 1.7px !important;
  stroke-linecap: round;
}

.diagram svg marker path,
.diagram svg .arrowheadPath {
  fill: #8d8dae !important;
  stroke: none !important;
}

/* ==========================================================================
   Entrance animation — staged, triggered once the figure scrolls into view
   ========================================================================== */

.diagram.is-ready svg .node,
.diagram.is-ready svg .cluster,
.diagram.is-ready svg .edgeLabel {
  opacity: 0;
}

.diagram.is-ready svg .flowchart-link,
.diagram.is-ready svg .edgePath .path {
  stroke-dashoffset: var(--dg-len, 0);
  stroke-dasharray: var(--dg-len, 0);
}

.diagram.is-animated svg .cluster {
  animation: dg-rise 0.5s cubic-bezier(0.22, 1, 0.36, 1) both;
  animation-delay: calc(var(--dg-i, 0) * 60ms);
}

.diagram.is-animated svg .node {
  animation: dg-pop 0.52s cubic-bezier(0.22, 1, 0.36, 1) both;
  animation-delay: calc(140ms + var(--dg-i, 0) * 85ms);
}

.diagram.is-animated svg .flowchart-link,
.diagram.is-animated svg .edgePath .path {
  animation: dg-draw 0.6s ease-out both;
  animation-delay: calc(240ms + var(--dg-i, 0) * 85ms);
}

.diagram.is-animated svg .edgeLabel {
  animation: dg-fade 0.4s ease-out both;
  animation-delay: calc(520ms + var(--dg-i, 0) * 60ms);
}

/* NOTE: animate the individual `translate` / `scale` properties, never
   `transform`. Mermaid positions every node with a transform *attribute*, and
   a CSS `transform` would replace it and stack the whole graph at the origin.
   The individual properties compose with it instead. */
@keyframes dg-pop {
  from { opacity: 0; translate: 0 10px; scale: 0.965; }
  to   { opacity: 1; translate: 0 0;    scale: 1; }
}

@keyframes dg-rise {
  from { opacity: 0; translate: 0 14px; }
  to   { opacity: 1; translate: 0 0; }
}

@keyframes dg-fade {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@keyframes dg-draw {
  from { stroke-dashoffset: var(--dg-len, 0); }
  to   { stroke-dashoffset: 0; }
}

/* scale about each shape's own centre, not the canvas origin */
.diagram svg .node,
.diagram svg .cluster {
  transform-box: fill-box;
  transform-origin: 50% 50%;
}

/* ==========================================================================
   Toolbar
   ========================================================================== */

.diagram__bar {
  display: none;
  align-items: center;
  gap: 0.35rem;
  padding: 0.62rem 0.7rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.07);
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.045), rgba(255, 255, 255, 0.012));
  backdrop-filter: blur(6px);
  position: relative;
  z-index: 3;
}

.diagram.is-ready .diagram__bar { display: flex; }

.diagram__label {
  font-family: 'Syne', var(--font-display, system-ui), sans-serif;
  font-weight: 800;
  font-size: 0.68rem;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  color: #8f8fae;
  margin-right: auto;
  padding-left: 0.4rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.diagram__btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  padding: 0;
  border: 1px solid rgba(255, 255, 255, 0.11);
  border-radius: 9px;
  background: rgba(255, 255, 255, 0.03);
  color: #cfcfe2;
  cursor: pointer;
  transition: background 0.18s, color 0.18s, border-color 0.18s, transform 0.18s;
}

.diagram__btn:hover {
  background: rgba(124, 58, 237, 0.22);
  border-color: rgba(168, 85, 247, 0.5);
  color: #fff;
  transform: translateY(-1px);
}

.diagram__btn:active { transform: translateY(0); }

.diagram__btn:focus-visible {
  outline: 2px solid var(--dg-accent-2);
  outline-offset: 2px;
}

.diagram__btn svg { width: 15px; height: 15px; }

.diagram__zoom {
  font-family: 'JetBrains Mono', ui-monospace, monospace;
  font-size: 0.68rem;
  color: #8f8fae;
  min-width: 3.2rem;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.diagram__hint {
  padding: 0.5rem 1rem 0.65rem;
  font-size: 0.71rem;
  color: #64647f;
  border-top: 1px solid rgba(255, 255, 255, 0.055);
  display: none;
  margin: 0;
}

.diagram.is-ready .diagram__hint { display: block; }

/* ==========================================================================
   Expanded / fullscreen
   ========================================================================== */

.diagram:fullscreen,
.diagram.is-expanded {
  border-radius: 0;
  border: none;
  box-shadow: none;
}

.diagram:fullscreen pre.mermaid,
.diagram.is-expanded pre.mermaid {
  height: calc(100vh - 96px);
  min-height: 0;
}

.diagram.is-expanded {
  position: fixed;
  inset: 0;
  z-index: 9000;
  margin: 0;
}

/* ==========================================================================
   Accessibility & print
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
  .diagram.is-ready svg .node,
  .diagram.is-ready svg .cluster,
  .diagram.is-ready svg .edgeLabel { opacity: 1; }
  .diagram.is-ready svg .flowchart-link,
  .diagram.is-ready svg .edgePath .path { stroke-dasharray: none; stroke-dashoffset: 0; }
  .diagram.is-animated svg * { animation: none !important; }
  .diagram__btn { transition: none; }
}

@media print {
  .diagram { box-shadow: none; background: none; }
  .diagram__bar,
  .diagram__hint { display: none !important; }
  .diagram.is-ready pre.mermaid { height: auto; }
  .diagram.is-ready pre.mermaid > svg { transform: none !important; }
  .diagram.is-ready svg .node,
  .diagram.is-ready svg .cluster,
  .diagram.is-ready svg .edgeLabel { opacity: 1; }
  .diagram.is-ready svg .flowchart-link { stroke-dasharray: none; stroke-dashoffset: 0; }
}

@media (max-width: 640px) {
  .diagram { margin: 1.8rem 0 0.9rem; border-radius: 14px; }
  .diagram.is-ready pre.mermaid { min-height: 230px; padding: 1rem 0.5rem; }
  .diagram__label { display: none; }
  .diagram svg .nodeLabel, .diagram svg .nodeLabel p { font-size: 13px !important; }
}
