/*
 * The mermaid addon's styles: the figure, the capped frame with its click-to-engage
 * states, and the fullscreen overlay. Colors ride the site's theme tokens where they
 * exist (hsl(var(--token)) holds HSL channels), with plain fallbacks for a site that
 * ships none, so the addon runs on stock PhlatPage.
 */

/* the figure: one diagram in the body's flow, centered like a block */
.mermaid-figure {
	position: relative;
	width: 100%;
	max-width: 100%;
	margin: 1.5rem auto;
}

/* the frame: the diagram's window. Capped, and the controls own it once engaged */
.mermaid-frame {
	position: relative;
	overflow: hidden;
	max-height: 26rem;
	display: flex;
	justify-content: center;
	border: 1px solid hsl(var(--ink, 220 13% 18%) / 0.2);
	border-radius: var(--radius, 0.5rem);
	background: hsl(var(--canvas, 0 0% 98%));
	cursor: zoom-in;
}

.mermaid-frame svg {
	max-width: 100%;
	max-height: 26rem;
	height: auto;
	display: block;
}

.mermaid-frame:focus-visible {
	outline: 2px solid hsl(var(--ring, var(--primary, 25 95% 53%)));
	outline-offset: 2px;
}

/* engaged: grab, a highlighted border; mid-drag: grabbing */
.mermaid-frame.diagram-active {
	cursor: grab;
	border-color: hsl(var(--primary, 25 95% 53%));
}

.mermaid-frame.diagram-active:active {
	cursor: grabbing;
}

/* the code before the library renders it, and after it fails: a readable code block */
.mermaid-src {
	margin: 0;
	padding: 1rem;
	overflow: auto;
	max-height: 26rem;
	width: 100%;
	font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);
	font-size: 0.75rem;
	line-height: 1.6;
	white-space: pre;
}

.mermaid-src.mermaid-broken {
	border-color: hsl(var(--destructive, 0 72% 51%));
}

/* a prerendered figure carries its code only as the loader's source */
.mermaid-figure[data-mermaid-state='ready'] .mermaid-src {
	display: none;
}

/* the placeholder: while the loader has not settled the figure, a script-enabled
 * browser sees the same frame, border and width as the finished diagram, with a
 * centered label, instead of the code, so nothing flashes. The frame keeps its
 * minimum height once drawn, so a diagram no taller than that never moves the page.
 * The code stays in the document for assistive tech; with scripting off, or once the figure is "failed", it shows as
 * the readable fallback. */
@media (scripting: enabled) {
	.mermaid-figure:not([data-mermaid-state="failed"]) .mermaid-frame {
		min-height: 20rem;
	}

	/* centered when there is room, from the top when the diagram is taller than the cap */
	.mermaid-figure[data-mermaid-state='ready'] .mermaid-frame > svg {
		margin-block: auto;
	}

	.mermaid-figure:not([data-mermaid-state]) .mermaid-frame {
		align-items: center;
		cursor: progress;
	}

	.mermaid-figure:not([data-mermaid-state]) .mermaid-frame::before {
		content: 'Rendering diagram';
		color: hsl(var(--muted, 220 13% 46%));
		font-size: 0.875rem;
	}

	.mermaid-figure:not([data-mermaid-state]) .mermaid-src {
		position: absolute;
		width: 1px;
		height: 1px;
		padding: 0;
		overflow: hidden;
		clip-path: inset(50%);
		white-space: nowrap;
	}
}

/* the diagram fades in over the placeholder; the loader eases the frame's height */
@media (prefers-reduced-motion: no-preference) {
	.mermaid-figure[data-mermaid-state='ready'] .mermaid-frame > svg {
		animation: mermaid-in 250ms ease-out;
	}
}

@keyframes mermaid-in {
	from {
		opacity: 0;
	}
}

/* the accessible name under the diagram */
.mermaid-caption {
	margin-top: 0.5rem;
	text-align: center;
	font-size: 0.875rem;
	color: hsl(var(--muted, 220 13% 46%));
}

/* the expand button: over the frame's top corner, a sibling so it never nests
 * inside the interactive frame */
.mermaid-expand,
.mermaid-overlay-close {
	display: grid;
	place-items: center;
	width: 2rem;
	height: 2rem;
	border: 1px solid hsl(var(--ink, 220 13% 18%) / 0.2);
	border-radius: var(--radius, 0.5rem);
	background: hsl(var(--canvas, 0 0% 98%));
	color: hsl(var(--ink, 220 13% 18%));
	cursor: pointer;
}

.mermaid-expand {
	position: absolute;
	top: 0.5rem;
	right: 0.5rem;
	z-index: 1;
}

.mermaid-expand:hover,
.mermaid-overlay-close:hover {
	background: hsl(var(--ink, 220 13% 18%) / 0.05);
}

.mermaid-expand:disabled {
	border-color: hsl(var(--ink, 220 13% 18%) / 0.1);
	color: hsl(var(--muted, 220 13% 46%));
	cursor: progress;
}

/* no script, or a failed render: nothing will ever enable the button */
.mermaid-figure[data-mermaid-state="failed"] .mermaid-expand {
	display: none;
}

@media (scripting: none) {
	.mermaid-expand {
		display: none;
	}
}

.mermaid-expand:focus-visible,
.mermaid-overlay-close:focus-visible {
	outline: 2px solid hsl(var(--ring, var(--primary, 25 95% 53%)));
	outline-offset: 2px;
}

.mermaid-expand svg,
.mermaid-overlay-close svg {
	width: 1rem;
	height: 1rem;
}

/* the fullscreen overlay: the svg cloned, uncapped, with the same controls */
.mermaid-overlay {
	position: fixed;
	inset: 0;
	z-index: 50;
	display: flex;
	flex-direction: column;
	background: hsl(var(--canvas, 0 0% 98%) / 0.97);
}

.mermaid-overlay .mermaid-frame {
	flex: 1;
	min-height: 0;
	max-height: none;
	margin: 0;
	padding: 2rem 3rem;
	overflow: auto;
	border: none;
	background: none;
}

.mermaid-overlay .mermaid-frame svg {
	max-width: none;
	max-height: none;
}

.mermaid-overlay-close {
	position: absolute;
	top: 1rem;
	right: 1rem;
	z-index: 1;
}
