/* Structure for build-time rendered diagrams. Every colour comes from the
   --diagram-* tokens in tokens.css; nothing here re-colours the SVG after the
   fact, and the mermaid theme variables in bin/mermaid.js point at the same set. */

/* A diagram is a card: 16px radius and 32px of breathing room, per the system. */
.diagram {
  margin: var(--space-6) 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--diagram-canvas);
  padding: var(--space-6);
}

/* Wide diagrams scroll inside their own box rather than being scaled down until the
   labels stop being readable. The page itself never scrolls sideways. */
.diagram__canvas {
  overflow-x: auto;
  overscroll-behavior-x: contain;
}

/* The build writes an explicit width from the viewBox, so a narrow diagram sits at
   its natural size and centres, and a wide one scrolls inside the canvas above at
   full size rather than shrinking. */
.diagram__canvas svg {
  display: block;
  margin: 0 auto;
  height: auto;
}

.diagram__caption {
  margin-top: var(--space-5);
  padding-top: var(--space-4);
  border-top: 1px solid var(--border);
  font-size: var(--text-sm);
  line-height: var(--leading-snug);
  color: var(--ink-muted);
  text-align: center;
}

/* Mermaid writes some colours as presentation attributes rather than into its
   stylesheet, and those cannot take a custom property. These few rules put the
   tokens back in charge. */
.diagram svg .edgePath path,
.diagram svg .flowchart-link,
.diagram svg .relationshipLine {
  stroke: var(--diagram-line);
}

.diagram svg marker path,
.diagram svg .marker {
  fill: var(--diagram-line);
  stroke: var(--diagram-line);
}

.diagram svg .edgeLabel,
.diagram svg .edgeLabel rect {
  fill: var(--diagram-label-bg);
  background-color: var(--diagram-label-bg);
  color: var(--ink-muted);
}

.diagram svg .edgeLabel foreignObject,
.diagram svg .edgeLabel span,
.diagram svg .edgeLabel text {
  color: var(--ink-muted);
  fill: var(--ink-muted);
  font-size: var(--text-xs);
}

.diagram svg .nodeLabel,
.diagram svg .label text,
.diagram svg text {
  fill: var(--diagram-ink);
  color: var(--diagram-ink);
}

.diagram svg .cluster rect {
  fill: var(--diagram-cluster);
  stroke: var(--diagram-stroke-muted);
}

/* Subgraph titles are coloured but never resized or letter-spaced: mermaid measured
   them at their natural size during the build, and changing the metrics afterwards
   clips the label. */
.diagram svg .cluster-label text,
.diagram svg .cluster-label span {
  fill: var(--ink-muted);
  color: var(--ink-muted);
}

.diagram svg .cluster rect {
  rx: 12;
  ry: 12;
}

/* --- expand ---------------------------------------------------------------- */

.diagram {
  position: relative;
}

.diagram__expand {
  position: absolute;
  top: var(--space-3);
  right: var(--space-3);
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-pill);
  background: var(--surface);
  color: var(--ink-muted);
  font-size: var(--text-2xs);
  font-weight: 600;
  letter-spacing: var(--tracking-wide);
  text-transform: uppercase;
  opacity: 0;
  transition: opacity var(--transition), color var(--transition), border-color var(--transition);
}

.diagram__expand svg {
  width: 13px;
  height: 13px;
}

.diagram:hover .diagram__expand,
.diagram__expand:focus-visible {
  opacity: 1;
}

/* On touch there is no hover, so the control is always visible. */
@media (hover: none) {
  .diagram__expand {
    opacity: 1;
  }
}

.diagram__expand:hover {
  color: var(--accent);
  border-color: var(--accent-border);
}

/* --- the full screen viewer ------------------------------------------------ */

.viewer {
  width: min(96vw, 96rem);
  height: min(92vh, 60rem);
  max-width: none;
  max-height: none;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--diagram-canvas);
  color: var(--ink);
  overflow: hidden;
}

.viewer::backdrop {
  background: rgb(0 0 0 / 0.72);
  backdrop-filter: blur(3px);
}

.viewer__bar {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.viewer__caption {
  font-size: var(--text-sm);
  color: var(--ink-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.viewer__controls {
  margin-left: auto;
  display: flex;
  gap: var(--space-2);
  flex: none;
}

.viewer__button {
  min-width: 34px;
  height: 34px;
  padding: 0 var(--space-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-raised);
  color: var(--ink-muted);
  font-size: var(--text-base);
  line-height: 1;
  transition: color var(--transition), border-color var(--transition);
}

.viewer__button--wide {
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: var(--tracking-wide);
  text-transform: uppercase;
}

.viewer__button:hover {
  color: var(--accent);
  border-color: var(--accent-border);
}

.viewer__close {
  font-size: var(--text-lg);
}

.viewer__stage {
  position: relative;
  height: calc(100% - 60px);
  overflow: hidden;
  display: grid;
  place-items: center;
  cursor: grab;
  touch-action: none;
}

.viewer__stage.is-grabbing {
  cursor: grabbing;
}

.viewer__canvas {
  transform-origin: center center;
  will-change: transform;
}

.viewer__canvas svg {
  display: block;
  max-width: none;
  width: auto;
  height: auto;
}

.viewer__hint {
  position: absolute;
  bottom: var(--space-3);
  left: 50%;
  transform: translateX(-50%);
  font-size: var(--text-2xs);
  letter-spacing: var(--tracking-wide);
  text-transform: uppercase;
  color: var(--ink-faint);
  pointer-events: none;
}

@media (max-width: 40rem) {
  .viewer__hint,
  .viewer__caption {
    display: none;
  }
}
