/* # Host */

nldd-rich-text {
	--_rich-text-grid-size: calc(var(--primitives-font-size-100) * var(--primitives-line-height-snug));
	--_rich-text-spacing-snug: calc(var(--_rich-text-grid-size) / 2);
	--_rich-text-spacing: var(--_rich-text-spacing-snug);
	--_rich-text-main-max-width: var(--primitives-area-720);
	--_rich-text-wide-max-width: var(--primitives-area-800);
	--_rich-text-wide-bleed-width: calc(
		(var(--_rich-text-wide-max-width) - var(--_rich-text-main-max-width)) / 2
	);
	--_rich-text-main-shortfall: max(
		0px,
		calc(100% - var(--_rich-text-main-max-width))
	);
	--_rich-text-dt-width: var(--primitives-area-200);
	--_rich-text-dl-column-gap: var(--primitives-space-16);
	--_rich-text-dl-gap: calc(var(--_rich-text-spacing) / 2);

	container-type: inline-size;
	box-sizing: border-box;
	display: grid;
	width: 100%;
	grid-template-columns:
		[full-start]   0
		[wide-start]   0
		[main-start]   minmax(0, var(--_rich-text-main-max-width))
		[main-end]     min(calc(var(--_rich-text-wide-bleed-width) * 2), var(--_rich-text-main-shortfall))
		[wide-end]     1fr
		[full-end];
	row-gap: var(--_rich-text-spacing);
	color: var(--context-content-color);
	font: var(--primitives-font-body-md-regular-snug);
}

nldd-rich-text[centered] {
	--_rich-text-wide-max-width: var(--primitives-area-960);

	grid-template-columns:
		[full-start]   1fr
		[wide-start]   min(var(--_rich-text-wide-bleed-width), calc(var(--_rich-text-main-shortfall) / 2))
		[main-start]   minmax(0, var(--_rich-text-main-max-width))
		[main-end]     min(var(--_rich-text-wide-bleed-width), calc(var(--_rich-text-main-shortfall) / 2))
		[wide-end]     1fr
		[full-end];
}

nldd-rich-text[spacing="flat"]  { --_rich-text-spacing: var(--primitives-space-0); }
nldd-rich-text[spacing="tight"] { --_rich-text-spacing: calc(var(--_rich-text-grid-size) / 4); }
nldd-rich-text[spacing="loose"] { --_rich-text-spacing: var(--_rich-text-grid-size); }

nldd-rich-text[hidden] {
	display: none;
}

/* # Child placement
   A child belongs to the text unless it is listed: it reads at the main
   measure, so a new component lands there too, and so does code. Media,
   tables and a step bar get the wide accent, and what needs the room (a
   collection, a container) gets the full span. justify-self: start makes room available without
   forcing it: width-100% children fill their span, the rest shrinkwraps.
   In the left-aligned layout the full/wide start lines collapse onto
   main-start, so `full` and `wide` read as rightward bleed there and as
   symmetric spans in [centered]. */

nldd-rich-text > div:only-child:not([class], [style], [role], [data-rich-text-width]) {
	display: contents;
}

nldd-rich-text > *,
nldd-rich-text > div:only-child:not([class], [style], [role], [data-rich-text-width]) > * {
	grid-column: main;
	justify-self: start;
	max-width: 100%;
}

nldd-rich-text[centered] > *,
nldd-rich-text[centered] > div:only-child:not([class], [style], [role], [data-rich-text-width]) > * {
	justify-self: center;
}

nldd-rich-text > :is(h1, h2, h3, h4, h5, h6, p, ul, ol, dl, div, section, blockquote, details),
nldd-rich-text > div:only-child:not([class], [style], [role], [data-rich-text-width]) > :is(h1, h2, h3, h4, h5, h6, p, ul, ol, dl, div, section, blockquote, details) {
	grid-column: main;
	/* Fill the main column so short text blocks keep a shared left edge —
	   justify-self: center would otherwise float a shrinkwrapped heading
	   loose from the paragraphs below it. border-box keeps padded elements
	   (ul/ol list indent) inside the column. */
	box-sizing: border-box;
	width: 100%;
}

nldd-rich-text > :is(img, figure, video, iframe, table, nldd-table, nldd-image, nldd-step-bar),
nldd-rich-text > div:only-child:not([class], [style], [role], [data-rich-text-width]) > :is(img, figure, video, iframe, table, nldd-table, nldd-image, nldd-step-bar) {
	grid-column: wide;
}

nldd-rich-text > :is(hr, nldd-collection, nldd-container),
nldd-rich-text > div:only-child:not([class], [style], [role], [data-rich-text-width]) > :is(hr, nldd-collection, nldd-container) {
	grid-column: full;
}

nldd-rich-text > :is(pre, nldd-code-viewer),
nldd-rich-text > div:only-child:not([class], [style], [role], [data-rich-text-width]) > :is(pre, nldd-code-viewer) {
	width: 100%;
}

/* Per-item override for editors; wins on specificity from every default
   above. */
nldd-rich-text > [data-rich-text-width="main"],
nldd-rich-text > div:only-child:not([class], [style], [role], [data-rich-text-width]) > [data-rich-text-width="main"] {
	grid-column: main;
}

nldd-rich-text > [data-rich-text-width="wide"],
nldd-rich-text > div:only-child:not([class], [style], [role], [data-rich-text-width]) > [data-rich-text-width="wide"] {
	grid-column: wide;
}

nldd-rich-text > [data-rich-text-width="full"],
nldd-rich-text > div:only-child:not([class], [style], [role], [data-rich-text-width]) > [data-rich-text-width="full"] {
	grid-column: full;
}

nldd-rich-text > :first-child,
nldd-rich-text > div:only-child:not([class], [style], [role], [data-rich-text-width]) > :first-child {
	margin-top: 0;
}

nldd-rich-text > :last-child,
nldd-rich-text > div:only-child:not([class], [style], [role], [data-rich-text-width]) > :last-child {
	margin-bottom: 0;
}

nldd-rich-text *:focus-visible {
	outline: var(--semantics-focus-ring-outline);
	outline-offset: var(--semantics-focus-ring-outline-offset);
	box-shadow: var(--semantics-focus-ring-box-shadow);
}

nldd-rich-text *:focus:not(:focus-visible) {
	outline: none;
}


/* # Headings */

nldd-rich-text :is(h1, h2, h3, h4, h5, h6) {
	margin-top: var(--_rich-text-spacing);
	margin-bottom: 0;
	overflow-wrap: break-word;
	text-wrap: balance;
	-webkit-hyphens: auto;
	hyphens: auto;
	-webkit-hyphenate-limit-before: 5;
	-webkit-hyphenate-limit-after: 5;
	hyphenate-limit-chars: 12 5 5;
}


/* # Heading 1 */

nldd-rich-text h1 {
	font: var(--primitives-font-display-1-sm);
}


/* # Heading 2 */

nldd-rich-text h2 {
	font: var(--primitives-font-display-2-sm);
}


/* # Heading 3 */

nldd-rich-text h3 {
	font: var(--primitives-font-display-3-sm);
}


/* # Heading 4 */

nldd-rich-text h4 {
	font: var(--primitives-font-display-4-sm);
}


/* # Heading 5 */

nldd-rich-text h5 {
	font: var(--primitives-font-display-5-sm);
}


/* # Heading 6 */

nldd-rich-text h6 {
	font: var(--primitives-font-display-6-sm);
}


/* # Paragraph */

nldd-rich-text p {
	margin: 0;
	/* Safety net, independent of [hyphens]: lets long URLs and Dutch compounds
	   break as soon as they are wider than the column, instead of overflowing
	   at narrow (mobile) widths. break-word (not anywhere) leaves the grid
	   min-content size alone, so the main column stays intact. */
	overflow-wrap: break-word;
	text-wrap: pretty;
}


/* # Hyphenation */

/* Opt-in through [hyphens]; only works with a correct `lang` on the page. */
nldd-rich-text[hyphens] :is(p, li, dd) {
	-webkit-hyphens: auto;
	hyphens: auto;
	-webkit-hyphenate-limit-before: 3;
	-webkit-hyphenate-limit-after: 3;
	hyphenate-limit-chars: 6 3 3;
}


/* # Lists */

nldd-rich-text ul {
	margin: 0;
	padding-left: var(--_rich-text-grid-size);
	list-style-type: disc;
}

nldd-rich-text ol {
	margin: 0;
	padding-left: var(--_rich-text-grid-size);
	list-style-type: decimal;
}

nldd-rich-text > :is(ul, ol),
nldd-rich-text > div:only-child:not([class], [style], [role], [data-rich-text-width]) > :is(ul, ol) {
	margin-block: calc(var(--_rich-text-spacing) / -2);
}

nldd-rich-text ol > li > ol {
	list-style-type: lower-alpha;
}


/* # List Item */

nldd-rich-text li {
	margin: calc(var(--_rich-text-spacing) / 2) 0;
	padding: 0;
	/* Same safety net as p: long strings break instead of overflowing. */
	overflow-wrap: break-word;
	text-wrap: pretty;
}

nldd-rich-text li > * + * {
	margin-top: var(--_rich-text-spacing);
}


/* # Definition list */

nldd-rich-text dl {
	margin: 0;
	display: grid;
	column-gap: var(--_rich-text-dl-column-gap);

	@container (max-width: 40rem) {
		grid-template-columns: 1fr;
	}

	@container (min-width: 40.0625rem) {
		grid-template-columns: var(--_rich-text-dt-width) 1fr;
	}
}

nldd-rich-text dt {
	font-weight: var(--primitives-font-weight-body-bold);

	@container (min-width: 40.0625rem) {
		grid-column: 1;
	}
}

nldd-rich-text dd {
	margin-inline: 0;
	text-wrap: pretty;

	@container (min-width: 40.0625rem) {
		grid-column: 2;
	}
}

/* Gap before each new definition group: a dt that starts a group, i.e. the
   first dt after the previous group's dd. (The old dt:not(:first-child) also
   gapped a second, synonym dt within the same group.) */
nldd-rich-text dd + dt {
	margin-top: var(--_rich-text-dl-gap);
}

/* Wide-viewport companion: in the two-column grid the new group's dd shares
   its row with that group-starting dt, so it needs the same top margin to
   stay aligned. */
nldd-rich-text dd + dt + dd {
	@container (min-width: 40.0625rem) {
		margin-top: var(--_rich-text-dl-gap);
	}
}


/* # Link */

nldd-rich-text a {
	border-radius: var(--primitives-corner-radius-xxs);
	--context-content-color: var(--context-link-color);
	color: var(--context-link-color);
	text-decoration: underline;
	text-underline-offset: var(--primitives-space-2);
	cursor: pointer;

	&:hover {
		--context-content-color: var(--context-link-is-hovered-color);
		color: var(--context-link-is-hovered-color);
	}

	&:active {
		--context-content-color: var(--context-link-is-active-color);
		color: var(--context-link-is-active-color);
	}
}


/* # Mention */

nldd-rich-text [data-mention] {
	--_rich-text-mention-icon-size: 0.95em;
	--_rich-text-mention-inline-padding: 0.3em;

	--context-content-color: var(--semantics-mentions-content-color);
	color: var(--semantics-mentions-content-color);
	font-weight: var(--primitives-font-weight-body-bold);
	white-space: nowrap;

	/* The DS 'at' icon as a mask, so it takes the text color without an element. */
	&::before {
		content: '';
		display: inline-block;
		margin-inline-end: 0.1em;
		background-color: currentColor;
		width: var(--_rich-text-mention-icon-size);
		height: var(--_rich-text-mention-icon-size);
		vertical-align: -0.16em;
		mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M16 12c0 2.73 1.3 3.5 2 3.5s2-.77 2-3.5a8 8 0 1 0-5.93 7.73l.52 1.93A10 10 0 1 1 22 12c0 3.27-1.7 5.5-4 5.5-1.36 0-2.5-.78-3.22-2.08A4.3 4.3 0 0 1 11.5 17C9 17 7 14.76 7 12s2-5 4.5-5c.92 0 1.78.31 2.5.84V7h2zm-4.5-3C10.3 9 9 10.14 9 12s1.31 3 2.5 3 2.5-1.14 2.5-3-1.31-3-2.5-3'/%3E%3C/svg%3E") center / contain no-repeat;

		@media (forced-colors: active) {
			forced-color-adjust: none;
			background-color: CanvasText;
		}
	}
}

nldd-rich-text a[data-mention] {
	padding-block: max(0em, (1lh - 1.2em) / 2);
	padding-inline: max(0em, (max(1.2em, 1lh) - var(--_rich-text-mention-icon-size)) / 2) var(--_rich-text-mention-inline-padding);
	border-radius: var(--primitives-corner-radius-sm);
	box-shadow: inset 0 0 0 var(--semantics-surfaces-highlight-border-width) var(--semantics-mentions-highlight-border-color);
	background-color: var(--semantics-mentions-background-color);
	--context-content-color: var(--semantics-mentions-content-color);
	color: var(--semantics-mentions-content-color);
	text-decoration: none;
	cursor: default;
	-webkit-box-decoration-break: clone;
	box-decoration-break: clone;

	&:hover {
		background-color: var(--semantics-mentions-is-hovered-background-color);
		--context-content-color: var(--semantics-mentions-content-color);
		color: var(--semantics-mentions-content-color);
	}

	&:active {
		background-color: var(--semantics-mentions-is-active-background-color);
		--context-content-color: var(--semantics-mentions-content-color);
		color: var(--semantics-mentions-content-color);
	}

	@media (forced-colors: active) {
		&::before {
			background-color: LinkText;
		}
	}
}


/* # Strong and Bold */

nldd-rich-text strong,
nldd-rich-text b {
	color: inherit;
	font-weight: var(--primitives-font-weight-body-bold);
}


/* # Emphasis and Italic */

nldd-rich-text em,
nldd-rich-text i {
	color: inherit;
	font-style: italic;
}


/* # Code */

nldd-rich-text code {
	padding: var(--primitives-space-2) var(--primitives-space-4);
	border-radius: var(--semantics-controls-xs-corner-radius);
	box-shadow: inset 0 0 0 var(--semantics-surfaces-highlight-border-width) var(--semantics-surfaces-tinted-highlight-border-color);
	background-color: var(--semantics-surfaces-tinted-background-color);
	font-family: var(--primitives-font-family-monospace);
	font-size: var(--semantics-code-inline-font-size);
}

/* Inside table cells, let long code tokens (e.g. type_spec.precision) break so
   they don't force a column wide; in running text code stays intact. */
nldd-rich-text :is(th, td) code {
	overflow-wrap: anywhere;
}

/* A markdown renderer emits a fenced block as `<pre><code>`, so the frame goes
   on the pre: it is the block-level element, and `code` stays inline. Without
   overflow-x a long line escapes the element and stretches every ancestor,
   which shows up as a card that ignores its own width. */
nldd-rich-text pre {
	box-sizing: border-box;
	margin: 0;
	padding: var(--primitives-space-16);
	border-radius: var(--semantics-surfaces-corner-radius);
	/* Inner box-shadow paints the ring inside the radius without taking layout
	   space, same as nldd-box, nldd-banner and nldd-code-viewer. Without it the
	   block is invisible on a tinted surface: identical background, no edge. */
	box-shadow: inset 0 0 0 var(--semantics-surfaces-highlight-border-width) var(--semantics-surfaces-tinted-highlight-border-color);
	background-color: var(--semantics-surfaces-tinted-background-color);
	font: var(--primitives-font-monospace-sm-regular-snug);
	overflow-x: auto;
	-webkit-overflow-scrolling: touch;
	scrollbar-width: thin;

	/* forced-colors strips box-shadow, so restore the frame with a real border.
	   Same fallback as nldd-box, nldd-banner and nldd-list. */
	@media (forced-colors: active) {
		border: var(--semantics-surfaces-highlight-border-width) solid CanvasText;
	}
}

/* The tinted chip is for inline code. Inside a pre it paints once per rendered
   line, so the block hands it back and lets the pre carry the frame. */
nldd-rich-text pre > code {
	padding: 0;
	border-radius: 0;
	box-shadow: none;
	background-color: transparent;
	font: inherit;
}


/* # Mark */

nldd-rich-text mark {
	padding: var(--primitives-space-1) var(--primitives-space-2);
	border-radius: var(--semantics-controls-xs-corner-radius);
	background-color: var(--semantics-content-mark-background-color);
	--context-content-color: var(--semantics-content-mark-color);
	color: var(--semantics-content-mark-color);
}


/* # Figure */

nldd-rich-text figure {
	display: flex;
	flex-direction: column;
	gap: var(--primitives-space-8);
	margin: 0;
}


/* # Figure Caption */

nldd-rich-text figcaption {
	--context-content-color: var(--context-content-secondary-color);
	color: var(--context-content-secondary-color);
	font: var(--primitives-font-body-xs-regular-snug);
	text-align: left;
	text-wrap: pretty;
}


/* # Blockquote */

nldd-rich-text blockquote {
	display: flex;
	flex-direction: column;
	gap: calc(var(--semantics-blockquotes-md-spacing) / 2);
	margin-block: max(
		0px,
		calc(var(--semantics-blockquotes-md-spacing) - var(--_rich-text-spacing))
	);
	margin-inline: 0;
	padding-inline: var(--semantics-blockquotes-md-spacing);
	border-left: var(--semantics-blockquotes-border);
	font: var(--semantics-blockquotes-md-quote-font);
	text-wrap: pretty;

	@container (max-width: 40rem) {
		gap: calc(var(--semantics-blockquotes-sm-spacing) / 2);
		margin-block: max(
			0px,
			calc(var(--semantics-blockquotes-sm-spacing) - var(--_rich-text-spacing))
		);
		padding-inline: var(--semantics-blockquotes-sm-spacing);
		font: var(--semantics-blockquotes-sm-quote-font);
	}
}

nldd-rich-text blockquote > p {
	margin: 0;
}


/* # Details */

nldd-rich-text details {
	--_rich-text-details-highlight-border-width: var(--semantics-surfaces-highlight-border-width);
	--_rich-text-details-background-color: var(--semantics-surfaces-tinted-background-color);
	--_rich-text-details-content-color: var(--semantics-content-color);
	--_rich-text-details-content-background-color: var(--semantics-surfaces-base-background-color);
	--_rich-text-details-highlight-border-color: var(--semantics-surfaces-tinted-highlight-border-color);
	--_rich-text-details-summary-highlight-border: inset 0 0 0 transparent;
	--_rich-text-details-corner-radius: var(--primitives-corner-radius-lg);
	--_rich-text-details-start-corner-radius: var(--_rich-text-details-corner-radius);
	--_rich-text-details-end-corner-radius: var(--_rich-text-details-corner-radius);
	--_rich-text-details-inset: var(--semantics-disclosures-inset);
	--_rich-text-details-summary-min-size: var(--semantics-disclosures-title-bar-min-height);
	--_rich-text-details-summary-padding-block: var(--semantics-disclosures-title-bar-padding-block);
	--_rich-text-details-summary-gap: var(--semantics-disclosures-title-bar-gap);
	--_rich-text-details-summary-font: var(--semantics-disclosures-title-bar-font);
	--_rich-text-details-disclosure-icon-size: var(--semantics-disclosures-disclosure-icon-size);
	--_rich-text-details-disclosure-icon-open-rotation: var(--semantics-disclosures-disclosure-icon-open-rotation);
	--_rich-text-details-summary-hover-background-color: var(--semantics-surfaces-tinted-is-hovered-background-color);

	--context-background-color: var(--_rich-text-details-background-color);
	--context-content-color: var(--_rich-text-details-content-color);
	--context-content-secondary-color: var(--semantics-content-secondary-color);
	--context-content-accent-color: var(--semantics-content-accent-color);
	--context-content-success-color: var(--semantics-content-success-color);
	--context-content-warning-color: var(--semantics-content-warning-color);
	--context-content-critical-color: var(--semantics-content-critical-color);
	--context-link-color: var(--semantics-links-color);
	--context-link-is-hovered-color: var(--semantics-links-is-hovered-color);
	--context-link-is-active-color: var(--semantics-links-is-active-color);
	box-sizing: border-box;
	border: var(--_rich-text-details-highlight-border-width) solid var(--_rich-text-details-highlight-border-color);
	border-radius: var(--_rich-text-details-start-corner-radius) var(--_rich-text-details-start-corner-radius) var(--_rich-text-details-end-corner-radius) var(--_rich-text-details-end-corner-radius);
	background-color: var(--_rich-text-details-background-color);
	color: var(--_rich-text-details-content-color);
}

/* A block, not a row: the summary holds text and inline elements of the page,
   and a flex or grid row would make each of them a column. */
nldd-rich-text summary {
	display: block;
	align-content: center;
	box-sizing: border-box;
	border-radius: max(0px, var(--_rich-text-details-start-corner-radius) - var(--_rich-text-details-highlight-border-width)) max(0px, var(--_rich-text-details-start-corner-radius) - var(--_rich-text-details-highlight-border-width)) max(0px, var(--_rich-text-details-end-corner-radius) - var(--_rich-text-details-highlight-border-width)) max(0px, var(--_rich-text-details-end-corner-radius) - var(--_rich-text-details-highlight-border-width));
	min-height: var(--_rich-text-details-summary-min-size);
	padding-block: var(--_rich-text-details-summary-padding-block);
	box-shadow: var(--_rich-text-details-summary-highlight-border);
	padding-inline-start: var(--_rich-text-details-inset);
	padding-inline-end: calc((var(--_rich-text-details-summary-min-size) - var(--_rich-text-details-disclosure-icon-size)) / 2 + var(--_rich-text-details-disclosure-icon-size) + var(--_rich-text-details-summary-gap));
	font: var(--_rich-text-details-summary-font);
	cursor: default;
	list-style: none;
	overflow-wrap: break-word;
	-webkit-tap-highlight-color: transparent;

	&::-webkit-details-marker {
		display: none;
	}

	/* Floated into the padding on the first line, so it moves with the centered
	   text and a second line does not run under it. */
	&::before {
		content: '';
		float: inline-end;
		margin-block-start: calc((1lh - var(--_rich-text-details-disclosure-icon-size)) / 2);
		margin-inline-end: calc(-1 * (var(--_rich-text-details-disclosure-icon-size) + var(--_rich-text-details-summary-gap)));
		background-color: currentColor;
		width: var(--_rich-text-details-disclosure-icon-size);
		height: var(--_rich-text-details-disclosure-icon-size);
		mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 96 96'%3E%3Cpath d='M48 60.7 13.6 26.4 8 32l40 40 40-40-5.6-5.6z'/%3E%3C/svg%3E") center / contain no-repeat;
		transition: rotate var(--primitives-transition-duration-fast) ease-out;

		@media (prefers-reduced-motion: reduce) {
			transition: none;
		}

		@media (forced-colors: active) {
			forced-color-adjust: none;
			background-color: CanvasText;
		}
	}

	@media (hover: hover) {
		&:hover {
			--context-background-color: var(--_rich-text-details-summary-hover-background-color);
			background-color: var(--_rich-text-details-summary-hover-background-color);
		}
	}

	&:active {
		--context-background-color: var(--_rich-text-details-summary-hover-background-color);
		background-color: var(--_rich-text-details-summary-hover-background-color);
	}

	/* Above a joined block below, which would cover the ring. */
	&:focus-visible {
		position: relative;
		z-index: 1;
	}
}

/* Where the height animates, the bar keeps its square corners and its line
   until the closing content is gone. */
@supports (interpolate-size: allow-keywords) {
	nldd-rich-text details:not([open]) > summary {
		transition: border-radius 0s var(--primitives-transition-duration-fast), box-shadow 0s var(--primitives-transition-duration-fast);

		@media (prefers-reduced-motion: reduce) {
			transition: none;
		}
	}
}

nldd-rich-text details[open] > summary {
	border-end-start-radius: 0;
	border-end-end-radius: 0;
	--_rich-text-details-summary-highlight-border: inset 0 calc(-1 * var(--_rich-text-details-highlight-border-width)) 0 var(--_rich-text-details-highlight-border-color);
}

nldd-rich-text summary:focus-visible {
	box-shadow: var(--semantics-focus-ring-box-shadow), var(--_rich-text-details-summary-highlight-border);
}

nldd-rich-text details[open] > summary::before {
	rotate: var(--_rich-text-details-disclosure-icon-open-rotation);
}

nldd-rich-text details > :not(summary) {
	margin: var(--_rich-text-spacing) var(--_rich-text-details-inset) 0;
}

nldd-rich-text details > summary + * {
	margin-block-start: var(--_rich-text-details-inset);
}

nldd-rich-text details[open] {
	padding-block-end: var(--_rich-text-details-inset);
}

@supports selector(::details-content) {
	nldd-rich-text details[open] {
		padding-block-end: 0;
	}

	nldd-rich-text details::details-content {
		--context-background-color: var(--_rich-text-details-content-background-color);
		border-end-start-radius: max(0px, var(--_rich-text-details-end-corner-radius) - var(--_rich-text-details-highlight-border-width));
		border-end-end-radius: max(0px, var(--_rich-text-details-end-corner-radius) - var(--_rich-text-details-highlight-border-width));
		background-color: var(--_rich-text-details-content-background-color);
	}

	nldd-rich-text details[open]::details-content {
		padding-block: var(--_rich-text-details-inset);
	}

	nldd-rich-text details {
		interpolate-size: allow-keywords;
	}

	nldd-rich-text details::details-content {
		block-size: 0;
		overflow-y: clip;
		transition:
			block-size var(--primitives-transition-duration-fast) ease-out,
			padding-block var(--primitives-transition-duration-fast) ease-out,
			content-visibility var(--primitives-transition-duration-fast) allow-discrete;

		@media (prefers-reduced-motion: reduce) {
			transition: none;
		}
	}

	nldd-rich-text details[open]::details-content {
		block-size: auto;
	}

	nldd-rich-text details > summary + * {
		margin-block-start: 0;
	}
}

nldd-rich-text details:has(+ details) {
	--_rich-text-details-end-corner-radius: 0px;
}

/* Pulled up over the gap and the border, so the two borders become one line. */
nldd-rich-text details + details {
	margin-block-start: calc(-1 * (var(--_rich-text-spacing) + var(--_rich-text-details-highlight-border-width)));
	--_rich-text-details-start-corner-radius: 0px;
}

@media print {
	nldd-rich-text details::details-content {
		content-visibility: visible;
		block-size: auto;
	}

	nldd-rich-text summary::before {
		display: none;
	}
}


/* # Inline Quote */

nldd-rich-text q {
	font-style: italic;

	&::before {
		content: open-quote;
	}

	&::after {
		content: close-quote;
	}
}


/* # Image */

nldd-rich-text img {
	display: block;
	border-radius: var(--semantics-controls-md-corner-radius);
	max-width: 100%;
	height: auto;
}


/* # Horizontal Rule */

nldd-rich-text hr {
	margin: 0;
	border: none;
	border-top: var(--semantics-dividers-thickness) solid var(--semantics-dividers-color);
	/* An hr has no content, so the justify-self: start default would
	   shrinkwrap it to zero width. */
	width: 100%;
}


/* # Table */

nldd-rich-text table {
	display: block;
	/* Mobile scroll: the block shrinks to its content (so the border hugs
	   narrow tables) but caps at the container, scrolling when the columns
	   need more room. width: auto would instead fill 100% as a block. */
	width: max-content;
	max-width: 100%;
	margin-block: max(
		0px,
		calc(var(--_rich-text-spacing-snug) - var(--_rich-text-spacing))
	);
	border-collapse: separate;
	border-spacing: 0;
	border: var(--semantics-tables-border-width) solid var(--semantics-tables-border-color);
	border-radius: var(--semantics-tables-corner-radius);
	background-color: var(--semantics-surfaces-base-background-color);
	font: var(--primitives-font-body-md-regular-tight);
	overflow-x: auto;
	-webkit-overflow-scrolling: touch;
	scrollbar-width: thin;

	@container (min-width: 40.0625rem) {
		display: table;
		width: 100%;
		overflow-x: unset;
		scrollbar-width: unset;
	}
}

/* ## Table Row */

nldd-rich-text tr {
	/* Row min-height: `min-height` does not apply to a table-cell per spec, so
	   set it on the row, where `height` acts as a minimum. Mirrors nldd-table. */
	height: var(--semantics-tables-row-min-height);
}

/* ## Table Header Cell */

nldd-rich-text th {
	min-width: var(--primitives-area-160);
	padding-block: var(--semantics-tables-row-padding-block);
	padding-inline: calc(var(--semantics-tables-column-gap) / 2);
	border-bottom: var(--semantics-tables-border-width) solid var(--semantics-tables-border-color);
	font: var(--primitives-font-body-md-bold-tight);
	text-align: start;

	@container (min-width: 40.0625rem) {
		min-width: unset;
	}
}

/* ## Table Data Cell */

nldd-rich-text td {
	min-width: var(--primitives-area-160);
	padding-block: var(--semantics-tables-row-padding-block);
	padding-inline: calc(var(--semantics-tables-column-gap) / 2);
	border-bottom: var(--semantics-tables-border-width) solid var(--semantics-tables-border-color);
	font: var(--primitives-font-body-md-regular-tight);
	text-align: left;

	@container (min-width: 40.0625rem) {
		min-width: unset;
	}
}

/* ## Table Outer Cells */

nldd-rich-text tr > :is(th, td):first-child {
	padding-inline-start: var(--semantics-tables-row-padding-inline);
}

nldd-rich-text tr > :is(th, td):last-child {
	padding-inline-end: var(--semantics-tables-row-padding-inline);
}

/* ## Table Last Row */

/* Only the final BODY row drops its bottom border (the table's own border
   closes it off). Without the `tbody` scope an unscoped `tr:last-child` also
   matches the header row — it is the last (only) child of `thead` — which
   wrongly stripped the header underline on tables that render a `<thead>`. */
nldd-rich-text tbody tr:last-child > :is(th, td) {
	border-bottom: none;
}
