/* ==========================================================================
   Print styles (loaded only when printing or in print preview)
   ========================================================================== */

/* Page box: size, margins, and first/left/right variants (modern paged media) */
@page {
	size: a4;
	margin: 2cm;
}

@page :first {
	margin-top: 2cm;
}

@page :left {
	margin-left: 2.5cm;
	margin-right: 1.5cm;
}

@page :right {
	margin-left: 1.5cm;
	margin-right: 2.5cm;
}

@media print {
	/* Force readable colors regardless of theme */
	html,
	html[data-theme='light'],
	html[data-theme='dark'] {
		--ns-body-bg: #fff;
		--ns-body-color: #1a1a1a;
		--code-bg: #f5f5f5;
		--code-fg: #1a1a1a;
		--nav-border: #1a1a1a;
	}

	body {
		background: #fff;
		color: #1a1a1a;

		/* Prevent single lines stranded at top/bottom of a page (widows & orphans) */
		orphans: 2;
		widows: 2;
	}

	/* Hide non-essential UI */
	#skip-link,
	header,
	footer,
	.back-to-top,
	.theme-container,
	details#detailsElement,
	.webmentions,
	.links-nextprev,
	.ha-placeholder,
	.e-content .code-fullscreen-btn,
	.e-content .code-copy-btn,
	.e-content .code-gist-btn {
		display: none !important;
	}

	/* Focus on main content */
	main {
		max-width: none;
		padding-inline: 0;
	}

	/* Show link URLs for external links when printing */
	main a[href^="http"]::after {
		content: " (" attr(href) ")";
		font-size: 0.85em;
		word-break: break-all;
	}

	/* Keep code blocks readable with light background; no borders (avoids per-line borders when printing) */
	code[class*='language-'],
	pre[class*='language-'],
	:not(pre) > code[class*='language-'] {
		background: #f5f5f5 !important;
		color: #1a1a1a !important;
		text-shadow: none;
		border: none !important;

		/* Smaller font and wrap long lines so code fits on the page */
		font-size: 0.7rem;
		white-space: pre-wrap;
		overflow-wrap: break-word;
		max-width: 100%;

		/* Ensure background prints (browsers may suppress it by default) */
		print-color-adjust: exact;
		-webkit-print-color-adjust: exact;
	}

	pre[class*='language-'] {
		overflow: visible;
	}

	/* Remove any border/outline/box-shadow from elements inside code blocks (e.g. line wrappers) */
	pre[class*='language-'] *,
	pre[class*='language-'] *::before,
	pre[class*='language-'] *::after {
		border: none !important;
		box-shadow: none !important;
		outline: none !important;
	}

	/* Syntax token colors for print (dark text for contrast) */
	.token.cdata,
	.token.comment,
	.token.doctype,
	.token.prolog {
		color: #5a5a5a;
	}

	.token.punctuation,
	.token.namespace,
	.token.constant,
	.token.deleted,
	.token.property,
	.token.symbol,
	.token.tag,
	.token.boolean,
	.token.number,
	.token.attr-name,
	.token.builtin,
	.token.char,
	.token.inserted,
	.token.selector,
	.token.string,
	.token.entity,
	.token.operator,
	.token.url,
	.token.variable,
	.token.atrule,
	.token.attr-value,
	.token.class-name,
	.token.function,
	.token.keyword,
	.token.important,
	.token.regex {
		color: #1a1a1a;
	}

	/* Avoid breaking content awkwardly (modern break-* with page-break-* fallback) */
	article {
		break-inside: avoid;
		page-break-inside: avoid;
	}

	main h1,
	main h2,
	main h3,
	main h4 {
		break-after: avoid;
		page-break-after: avoid;
	}

	pre,
	blockquote,
	table {
		break-inside: avoid;
		page-break-inside: avoid;
	}

	/* Images: fit page, avoid breaks, preserve colors */
	img,
	picture img {
		max-width: 100% !important;
		height: auto !important;
		object-fit: contain;
		break-inside: avoid;
		page-break-inside: avoid;

		/* Preserve appearance (disable browser's "save ink" suppression of images) */
		print-color-adjust: exact;
		-webkit-print-color-adjust: exact;
	}

	figure,
	.figure {
		break-inside: avoid;
		page-break-inside: avoid;
		max-width: 100%;
	}

	figure img,
	.figure img {
		max-width: 100% !important;
		height: auto !important;
	}

	/* Ensure images and links don't break mid-element */
	a {
		text-decoration: underline;

		/* Replicate decoration when link wraps across lines */
		box-decoration-break: clone;
	}
}
