/* Document viewer (docs/widgets/document.md), Soft design (spec §3.7): a white card with a thin border,
   rounded corners and the soft shadow; the toolbar on the card, round buttons and pill fields; the page
   on a pale grey with a soft shadow of its own. The other designs change the tokens below, never a
   property a Style control writes. The height is kept from the start, so nothing moves when the file
   arrives. Without a height setting, the frame is as tall as a portrait page at its width, up to 44rem
   or 85% of the screen. Another document, in an online viewer's frame (.hafe-embed), shares the card. */
.hafe-document,
.hafe-embed {
	/* The card and its toolbars, the line around and between them, and what's behind the page. */
	--hafe-document-paper: var(--hafe-surface);
	--hafe-document-frame: var(--hafe-line);
	--hafe-document-back: color-mix(in srgb, var(--hafe-ink) 5%, var(--hafe-surface));
	--hafe-document-bar: var(--hafe-document-paper);
	--hafe-document-bar-ink: var(--hafe-ink);
	/* Buttons and fields: their corners, a hovered or pressed button, and a field's fill and line. */
	--hafe-document-round: var(--hafe-pill);
	--hafe-document-hover: var(--hafe-tint);
	--hafe-document-press: var(--hafe-tint);
	--hafe-document-press-ink: var(--hafe-accent-ink);
	--hafe-document-field: var(--hafe-surface);
	--hafe-document-field-ink: var(--hafe-ink);
	--hafe-document-field-line: var(--hafe-line);
	--hafe-document-ring: var(--hafe-accent);
	--hafe-document-mark: var(--hafe-accent);
	/* A page: its shadow, and its line (none here). */
	--hafe-document-sheet: 0 1px 2px rgb(16 24 40 / 10%), 0 4px 14px -4px rgb(16 24 40 / 18%);
	--hafe-document-edge: 0 solid transparent;
	display: flex;
	flex-direction: column;
	box-sizing: border-box;
	height: var(--hafe-document-height, auto);
	max-height: var(--hafe-document-height, min(44rem, 85vh));
	overflow: hidden;
	border: 1px solid var(--hafe-document-border, var(--hafe-document-frame));
	border-radius: var(--hafe-document-radius, var(--hafe-radius));
	background: var(--hafe-document-area, var(--hafe-document-back));
	box-shadow: var(--hafe-shadow);
	color: var(--hafe-ink);
}

.hafe-scheme-dark :is(.hafe-document, .hafe-embed) {
	--hafe-document-back: color-mix(in srgb, #000 30%, var(--hafe-surface));
	--hafe-document-mark: var(--hafe-accent-ink);
	color-scheme: dark;
}

.hafe-document--bar-bottom {
	flex-direction: column-reverse;
}

/* The toolbar, not the frame, is the container: a contained frame is measured before its width is known,
   and its page-shaped height would come out wrong. */
.hafe-document__bar,
.hafe-document__find {
	display: flex;
	flex: none;
	align-items: center;
	gap: 0.25em;
	padding: 0.375em 0.5em;
	border-block-end: 1px solid var(--hafe-document-border, var(--hafe-document-frame));
	background: var(--hafe-document-bar-background, var(--hafe-document-bar));
	color: var(--hafe-document-bar-color, var(--hafe-document-bar-ink));
	font-size: max(0.8em, 14px);
	line-height: 1.2;
}

.hafe-document__bar {
	container-type: inline-size;
	flex-wrap: wrap;
	justify-content: space-between;
	gap: 0.25em 0.75em;
}

/* Below the page, the line is above. */
.hafe-document--bar-bottom .hafe-document__bar,
.hafe-document--bar-bottom .hafe-document__find,
.hafe-document .hafe-document__bar--pages {
	border-block: 1px solid var(--hafe-document-border, var(--hafe-document-frame));
	border-block-end: 0;
}

.hafe-document__group {
	display: flex;
	align-items: center;
	gap: 0.25em;
}

/* Specific enough to beat a theme's or kit's button styles, hover included. */
.hafe-document .hafe-document__button,
.hafe-document .hafe-document__button:hover,
.hafe-document .hafe-document__button:focus {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	box-sizing: border-box;
	width: 2.5em;
	min-width: 36px;
	height: 2.5em;
	min-height: 36px;
	margin: 0;
	padding: 0;
	border: 1px solid var(--hafe-document-button-line, transparent);
	border-radius: var(--hafe-document-round);
	background: none;
	box-shadow: none;
	color: inherit;
	font: inherit;
	text-decoration: none;
	cursor: pointer;
}

.hafe-document .hafe-document__button:hover:not(:disabled) {
	border-color: var(--hafe-document-button-hover-line, var(--hafe-document-button-line, transparent));
	background: var(--hafe-document-hover);
}

/* A panel or mode that's open: the side panel, search, full screen. */
.hafe-document .hafe-document__button:is([aria-expanded="true"], [aria-pressed="true"]) {
	border-color: var(--hafe-document-button-hover-line, transparent);
	background: var(--hafe-document-press);
	color: var(--hafe-document-press-ink);
}

.hafe-document .hafe-document__button:disabled,
.hafe-document .hafe-document__zoom:disabled,
.hafe-document .hafe-document__number:disabled {
	opacity: 0.45;
	cursor: default;
}

.hafe-document .hafe-document__button[hidden] {
	display: none;
}

.hafe-document__button svg {
	fill: none;
	stroke: currentcolor;
	stroke-width: 2;
	stroke-linecap: round;
	stroke-linejoin: round;
}

/* Arrows point the way pages turn. */
.hafe-document__turn:dir(rtl) svg {
	transform: scaleX(-1);
}

/* Stated here too: some packs take the outline off every focused button, site-wide. */
.hafe-document button:focus-visible {
	outline: 2px solid var(--hafe-accent);
	outline-offset: 2px;
}

.hafe-document .hafe-document__bar :focus-visible,
.hafe-document .hafe-document__find :focus-visible {
	outline-color: var(--hafe-document-ring);
}

/* Themes colour labels: this one takes the bar's colour, like the buttons beside it. */
.hafe-document .hafe-document__pages {
	display: inline-flex;
	align-items: center;
	gap: 0.3em;
	margin: 0;
	color: inherit;
	white-space: nowrap;
}

/* :not(#hafe-never): spec §4. */
.hafe-document .hafe-document__number:not(#hafe-never),
.hafe-document .hafe-document__zoom,
.hafe-document .hafe-document__query:not(#hafe-never) {
	box-sizing: border-box;
	width: auto;
	min-height: 36px;
	margin: 0;
	padding: 0.2em 0.75em;
	border: 1px solid var(--hafe-document-field-line);
	border-radius: var(--hafe-document-round);
	background: var(--hafe-document-field);
	box-shadow: none;
	color: var(--hafe-document-field-ink);
	font: inherit;
	line-height: inherit;
}

/* Browsers show focus on fields however they were reached, so it's quiet: the line turns to the accent. */
.hafe-document .hafe-document__number:focus:not(#hafe-never),
.hafe-document .hafe-document__zoom:focus,
.hafe-document .hafe-document__query:focus:not(#hafe-never) {
	border-color: var(--hafe-accent);
	outline: none;
}

.hafe-document .hafe-document__number:not(#hafe-never) {
	width: 3.5em;
	padding-inline: 0.4em;
	text-align: center;
}

/* The zoom menu draws its own arrow, inset from the edge (spec §3.7): the browser's sits against it. */
.hafe-document__menu {
	display: inline-flex;
	position: relative;
	align-items: center;
}

.hafe-document .hafe-document__zoom {
	padding-inline-end: 2em;
	appearance: none;
	cursor: pointer;
}

.hafe-document__chevron {
	position: absolute;
	inset-inline-end: 0.8em;
	width: 0.7em;
	height: auto;
	color: var(--hafe-document-field-ink);
	pointer-events: none;
}

.hafe-document__zoom:disabled + .hafe-document__chevron {
	opacity: 0.45;
}

/* The list opens on our surface, whatever colour the toolbar is. */
.hafe-document__zoom option {
	background: var(--hafe-surface);
	color: var(--hafe-ink);
}

/* The side panel and the pages. */
.hafe-document__body {
	display: flex;
	flex: 1 1 auto;
	min-height: 0;
	aspect-ratio: 210 / 297;
}

/* The page sits in the middle; a page wider than the frame scrolls both ways from its start. */
.hafe-document__area {
	display: flex;
	position: relative;
	flex: 1 1 0;
	min-width: 0;
	overflow: auto;
	overscroll-behavior: contain;
	touch-action: pan-x pan-y;
}

.hafe-document .hafe-document__area:focus-visible {
	outline-offset: -2px;
}

/* A page that fits sideways leaves sideways swipes to the script. */
.hafe-document__area.is-fitted {
	touch-action: pan-y;
}

.hafe-document__sheet {
	flex: none;
	box-sizing: content-box;
	margin: auto;
	padding: 8px;
	transform-origin: 0 0;
}

/* One under another, pages keep to the top and stack. */
.hafe-document--scroll .hafe-document__sheet {
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 8px;
	margin: 0 auto;
}

/* A PDF page is white whatever the scheme, as on paper. Its line is an outline, which leaves its size
   alone: the script sizes the page to fit the frame exactly. */
.hafe-document__page,
.hafe-document__shot {
	position: relative;
	flex: none;
	outline: var(--hafe-document-edge);
	background: #fff;
	box-shadow: var(--hafe-document-sheet);
}

.hafe-document__page canvas,
.hafe-document__shot canvas {
	display: block;
	width: 100%;
	height: 100%;
}

/* PDF.js's text layer: invisible text over the drawing, there to be selected, searched and read
   out. PDF.js sets each piece's place, height, width and turn; this turns them into size. */
.hafe-document__text,
.hafe-document__links {
	position: absolute;
	inset: 0;
	overflow: clip;
}

.hafe-document__text {
	color: transparent;
	font-style: normal;
	font-weight: 400;
	line-height: 1;
	letter-spacing: normal;
	word-spacing: normal;
	text-align: initial;
	text-transform: none;
	text-size-adjust: none;
	overflow-wrap: normal;
	forced-color-adjust: none;
}

.hafe-document__text span,
.hafe-document__text br {
	position: absolute;
	white-space: pre;
	cursor: text;
	transform-origin: 0 0;
}

.hafe-document__text span {
	font-size: calc(var(--total-scale-factor) * var(--min-font-size, 1) * var(--font-height));
	transform: rotate(var(--rotate, 0deg)) scaleX(var(--scale-x, 1)) scale(calc(1 / var(--min-font-size, 1)));
}

.hafe-document__text .markedContent {
	display: contents;
}

.hafe-document__text ::selection {
	background: rgb(0 100 255 / 0.3);
}

.hafe-document__text br::selection {
	background: transparent;
}

/* Matches are marked as a highlighter would, the current one deeper: they sit on the PDF's own colours. */
.hafe-document__text mark {
	padding: 0;
	border-radius: 2px;
	background: rgb(255 200 0 / 0.5);
	color: transparent;
}

.hafe-document__text mark.is-current {
	background: rgb(255 110 0 / 0.6);
}

/* The PDF's own links, over its text. */
.hafe-document__links {
	pointer-events: none;
}

.hafe-document .hafe-document__links a {
	position: absolute;
	margin: 0;
	padding: 0;
	border: 0;
	background: none;
	box-shadow: none;
	pointer-events: auto;
}

.hafe-document .hafe-document__links a:hover {
	background: rgb(255 220 0 / 0.25);
}

/* The search row, under the toolbar. */
.hafe-document__find[hidden],
.hafe-document__side[hidden],
.hafe-document__side [hidden] {
	display: none;
}

.hafe-document .hafe-document__query {
	flex: 1 1 8em;
	min-width: 0;
	appearance: none;
}

.hafe-document__found {
	padding-inline: 0.25em;
	white-space: nowrap;
}

/* The side panel: thumbnails, and the PDF's contents. */
.hafe-document__side {
	box-sizing: border-box;
	flex: none;
	width: calc(96px + 4em);
	max-width: 45%;
	padding: 8px;
	overflow: hidden auto;
	overscroll-behavior: contain;
	border-inline-end: 1px solid var(--hafe-document-border, var(--hafe-document-frame));
	background: var(--hafe-document-paper);
	font-size: max(0.8em, 14px);
	line-height: 1.3;
}

.hafe-document__tabs {
	display: flex;
	flex-wrap: wrap;
	gap: 4px;
	margin-bottom: 8px;
}

.hafe-document__thumbs,
.hafe-document__outline ul {
	margin: 0;
	padding: 0;
	list-style: none;
}

.hafe-document__thumbs {
	display: grid;
	gap: 4px;
	justify-items: center;
}

.hafe-document__outline ul ul {
	padding-inline-start: 0.75em;
}

.hafe-document .hafe-document__thumb,
.hafe-document .hafe-document__tab,
.hafe-document .hafe-document__outline a {
	display: flex;
	box-sizing: border-box;
	width: auto;
	height: auto;
	min-height: 36px;
	margin: 0;
	padding: 4px 8px;
	border: 2px solid transparent;
	border-radius: var(--hafe-radius-sm);
	background: none;
	box-shadow: none;
	color: inherit;
	font: inherit;
	text-align: start;
	text-decoration: none;
	cursor: pointer;
}

.hafe-document .hafe-document__thumb {
	flex-direction: column;
	align-items: center;
	gap: 4px;
	padding: 6px;
}

.hafe-document .hafe-document__tab {
	flex: 1 1 auto;
	justify-content: center;
	border-radius: var(--hafe-document-round);
}

.hafe-document .hafe-document__outline a {
	align-items: center;
	overflow-wrap: anywhere;
}

.hafe-document .hafe-document__thumb:hover,
.hafe-document .hafe-document__tab:hover,
.hafe-document .hafe-document__outline a:hover {
	background: var(--hafe-document-hover);
}

/* The page shown, and the tab open. */
.hafe-document .hafe-document__thumb[aria-current] {
	border-color: var(--hafe-document-mark);
}

.hafe-document .hafe-document__tab[aria-pressed="true"] {
	background: var(--hafe-document-press);
	color: var(--hafe-document-press-ink);
}

.hafe-document__shot {
	display: block;
	width: 96px;
}

/* Another document: the online viewer fills a page-shaped space, and the link sits in a bar below it. */
.hafe-embed__body {
	display: flex;
	flex: 1 1 auto;
	min-height: 0;
	aspect-ratio: 210 / 297;
}

.hafe-embed .hafe-embed__frame {
	flex: 1 1 auto;
	width: 100%;
	height: auto;
	min-height: 0;
	margin: 0;
	border: 0;
	background: #fff;
}

.hafe-embed__bar {
	flex: none;
	margin: 0;
	padding: 0.6em 1em;
	border-block-start: 1px solid var(--hafe-document-border, var(--hafe-document-frame));
	background: var(--hafe-document-bar-background, var(--hafe-document-bar));
	color: var(--hafe-document-bar-color, var(--hafe-document-bar-ink));
	font-size: max(0.8em, 14px);
	line-height: 1.4;
}

.hafe-embed .hafe-embed__bar a {
	color: inherit;
	text-decoration: underline;
}

.hafe-embed__bar :focus-visible {
	outline-color: var(--hafe-document-ring);
}

.hafe-document__sr,
.hafe-document__fallback {
	position: absolute;
	width: 1px;
	height: 1px;
	margin: -1px;
	padding: 0;
	border: 0;
	overflow: hidden;
	clip-path: inset(50%);
	white-space: nowrap;
}

.hafe-document__error {
	margin: 0;
	padding: 0.75em;
}

.hafe-document:fullscreen {
	height: 100%;
	max-height: none;
	border: 0;
	border-radius: 0;
}

/* The viewer can't be shown: the link is what's left, on the card, and the frame shrinks to it. */
.hafe-document.is-failed {
	height: auto;
	background: var(--hafe-document-paper);
}

.hafe-document.is-failed .hafe-document__bar,
.hafe-document.is-failed .hafe-document__find,
.hafe-document.is-failed .hafe-document__body {
	display: none;
}

.hafe-document.is-failed .hafe-document__fallback {
	position: static;
	width: auto;
	height: auto;
	margin: 0;
	padding: 1em 1.25em;
	clip-path: none;
	white-space: normal;
}

.hafe-document .hafe-document__fallback a {
	color: var(--hafe-accent-ink);
}

/* A narrow viewer keeps its toolbar on one row. First the zoom buttons step aside (the menu has every
   level); then, on phones, the zoom menu, the side panel and print: two fingers zoom there, the page
   fits the width, and printing from a phone opens the file, which its own viewer prints. */
@container (max-width: 37em) {

	.hafe-document__group--zoom .hafe-document__button {
		display: none;
	}
}

@container (max-width: 420px) {

	.hafe-document__group {
		font-size: min(1em, 14px);
	}

	.hafe-document__group--zoom,
	.hafe-document__group--side,
	.hafe-document .hafe-document__button[data-act="print"] {
		display: none;
	}
}

@media (scripting: none) {

	.hafe-document {
		height: auto;
		background: var(--hafe-document-paper);
	}

	.hafe-document__bar,
	.hafe-document__find,
	.hafe-document__body {
		display: none;
	}

	.hafe-document__fallback {
		position: static;
		width: auto;
		height: auto;
		margin: 0;
		padding: 1em 1.25em;
		clip-path: none;
		white-space: normal;
	}
}

@media print {

	.hafe-document {
		height: auto;
		max-height: none;
		box-shadow: none;
	}

	.hafe-document__bar,
	.hafe-document__find,
	.hafe-document__side {
		display: none;
	}
}
