/* Tabs (docs/widgets/tabs.md), in the design language (spec §3.7). This is Soft, the default: the
   tabs in a pale track, the chosen one a raised white pill, the content a white card with a thin
   border and a soft shadow. Fonts come from the theme. tabs-outline.css and tabs-solid.css change
   only the --hafe-tabs-d-* tokens below, so a Style control's own property always wins.

   Each panel is a <details>: the browser opens it from its tab's link, or from its own title where
   the widget is an accordion. --hafe-tabs-list and --hafe-tabs-toggle, written per device by the
   "Show as" setting, show the tabs or the titles. */
.hafe-tabs {
	--hafe-tabs-d-track: color-mix(in srgb, var(--hafe-ink) 5%, var(--hafe-surface));
	--hafe-tabs-d-track-pad: 0.25rem;
	--hafe-tabs-d-track-radius: var(--hafe-radius-sm);
	--hafe-tabs-d-gap: 0.25rem;
	--hafe-tabs-d-space: 0.75rem;
	--hafe-tabs-d-tab-bg: transparent;
	--hafe-tabs-d-tab-ink: var(--hafe-ink-soft);
	--hafe-tabs-d-tab-line: transparent;
	--hafe-tabs-d-tab-radius: 0.5rem;
	--hafe-tabs-d-hover-bg: transparent;
	--hafe-tabs-d-active-bg: var(--hafe-surface);
	--hafe-tabs-d-active-ink: var(--hafe-accent-ink);
	--hafe-tabs-d-active-line: transparent;
	--hafe-tabs-d-active-shadow: 0 1px 2px rgb(16 24 40 / 8%), 0 1px 4px rgb(16 24 40 / 6%);
	--hafe-tabs-d-panel-bg: var(--hafe-surface);
	--hafe-tabs-d-line: var(--hafe-line);
	--hafe-tabs-d-radius: var(--hafe-radius-sm);
	--hafe-tabs-d-shadow: var(--hafe-shadow);
	--hafe-tabs-d-toggle-hover: var(--hafe-tint);
	--hafe-tabs-d-sep: var(--hafe-tabs-line, var(--hafe-tabs-d-line));
	--hafe-tabs-d-join: 0;
	--hafe-tabs-d-facing: var(--hafe-tabs-d-active-line);
	--hafe-tabs-d-facing-radius: var(--hafe-tabs-d-tab-radius);
	--hafe-tabs-panel-ground: var(--hafe-tabs-panel-bg, var(--hafe-tabs-d-panel-bg));
	display: flex;
	flex-direction: column;
	gap: var(--hafe-tabs-space, var(--hafe-tabs-d-space));
	color: var(--hafe-ink);
	interpolate-size: allow-keywords;
}

.hafe-scheme-dark .hafe-tabs {
	--hafe-tabs-d-active-bg: color-mix(in srgb, var(--hafe-ink) 12%, var(--hafe-surface));
}

.hafe-tabs--start {
	flex-direction: row;
}

.hafe-tabs--end {
	flex-direction: row-reverse;
}

/* The tab list: a row above the content, wrapping when it doesn't fit, or a column beside it. */
.hafe-tabs__list {
	display: var(--hafe-tabs-list, flex);
	flex-wrap: wrap;
	align-self: flex-start;
	gap: var(--hafe-tabs-gap, var(--hafe-tabs-d-gap));
	box-sizing: border-box;
	max-inline-size: 100%;
	padding: var(--hafe-tabs-d-track-pad);
	border-radius: var(--hafe-tabs-d-track-radius);
	background: var(--hafe-tabs-d-track);
}

.hafe-tabs--align-center > .hafe-tabs__list {
	align-self: center;
}

.hafe-tabs--align-end > .hafe-tabs__list {
	align-self: flex-end;
}

.hafe-tabs--align-stretch > .hafe-tabs__list {
	align-self: stretch;
}

.hafe-tabs--start > .hafe-tabs__list,
.hafe-tabs--end > .hafe-tabs__list {
	flex: none;
	flex-direction: column;
	flex-wrap: nowrap;
	max-inline-size: 40%;
}

.hafe-tabs .hafe-tabs__tab {
	position: relative;
	display: flex;
	align-items: center;
	justify-content: center;
	gap: 0.5em;
	box-sizing: border-box;
	min-block-size: 2.75rem;
	padding: var(--hafe-tabs-tab-padding, 0.625em 1.125em);
	border: 1px solid var(--hafe-tabs-d-tab-line);
	border-radius: var(--hafe-tabs-d-tab-radius);
	background: var(--hafe-tabs-bg, var(--hafe-tabs-d-tab-bg));
	color: var(--hafe-tabs-ink, var(--hafe-tabs-d-tab-ink));
	font-weight: 600;
	line-height: 1.4;
	text-align: center;
	text-decoration: none;
	/* Whole words: tabs that don't fit wrap onto another row rather than splitting a title. */
	overflow-wrap: break-word;
	transition: color 0.2s, background-color 0.2s, box-shadow 0.2s;
}

.hafe-tabs--align-stretch .hafe-tabs__tab {
	flex: 1 1 0;
}

.hafe-tabs--start .hafe-tabs__tab,
.hafe-tabs--end .hafe-tabs__tab {
	justify-content: flex-start;
	text-align: start;
}

.hafe-tabs--icon-above .hafe-tabs__tab {
	flex-direction: column;
	gap: 0.25em;
}

.hafe-tabs .hafe-tabs__tab:hover {
	background: var(--hafe-tabs-d-hover-bg);
	color: var(--hafe-tabs-ink, var(--hafe-ink));
}

/* The chosen tab: the one whose panel is open, so it's right with no script too (the first twelve;
   tabs.js marks any tab with aria-selected). */
.hafe-tabs .hafe-tabs__tab[aria-selected="true"],
.hafe-tabs:has(> .hafe-tabs__panels > :nth-child(1)[open]) > .hafe-tabs__list > :nth-child(1),
.hafe-tabs:has(> .hafe-tabs__panels > :nth-child(2)[open]) > .hafe-tabs__list > :nth-child(2),
.hafe-tabs:has(> .hafe-tabs__panels > :nth-child(3)[open]) > .hafe-tabs__list > :nth-child(3),
.hafe-tabs:has(> .hafe-tabs__panels > :nth-child(4)[open]) > .hafe-tabs__list > :nth-child(4),
.hafe-tabs:has(> .hafe-tabs__panels > :nth-child(5)[open]) > .hafe-tabs__list > :nth-child(5),
.hafe-tabs:has(> .hafe-tabs__panels > :nth-child(6)[open]) > .hafe-tabs__list > :nth-child(6),
.hafe-tabs:has(> .hafe-tabs__panels > :nth-child(7)[open]) > .hafe-tabs__list > :nth-child(7),
.hafe-tabs:has(> .hafe-tabs__panels > :nth-child(8)[open]) > .hafe-tabs__list > :nth-child(8),
.hafe-tabs:has(> .hafe-tabs__panels > :nth-child(9)[open]) > .hafe-tabs__list > :nth-child(9),
.hafe-tabs:has(> .hafe-tabs__panels > :nth-child(10)[open]) > .hafe-tabs__list > :nth-child(10),
.hafe-tabs:has(> .hafe-tabs__panels > :nth-child(11)[open]) > .hafe-tabs__list > :nth-child(11),
.hafe-tabs:has(> .hafe-tabs__panels > :nth-child(12)[open]) > .hafe-tabs__list > :nth-child(12) {
	z-index: 1;
	border-color: var(--hafe-tabs-d-active-line);
	border-block-end-color: var(--hafe-tabs-facing-be, var(--hafe-tabs-d-active-line));
	border-inline-start-color: var(--hafe-tabs-facing-is, var(--hafe-tabs-d-active-line));
	border-inline-end-color: var(--hafe-tabs-facing-ie, var(--hafe-tabs-d-active-line));
	background: var(--hafe-tabs-active-bg, var(--hafe-tabs-d-active-bg));
	box-shadow: var(--hafe-tabs-d-active-shadow);
	color: var(--hafe-tabs-active-ink, var(--hafe-tabs-d-active-ink));
}

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

/* Joined tabs (Outline): the list overlaps the content's border, and the chosen tab's side facing
   the content takes the content's colour, so the two read as one shape. The corners facing the
   content are square, and so is the content's corner beside the first tab, where tabs show
   (--hafe-tabs-joined, 0 where the widget is an accordion). */
.hafe-tabs--top > .hafe-tabs__list {
	margin-block-end: calc(-1 * var(--hafe-tabs-line-width, 1px) * var(--hafe-tabs-d-join));
}

.hafe-tabs--start > .hafe-tabs__list {
	margin-inline-end: calc(-1 * var(--hafe-tabs-line-width, 1px) * var(--hafe-tabs-d-join));
}

.hafe-tabs--end > .hafe-tabs__list {
	margin-inline-start: calc(-1 * var(--hafe-tabs-line-width, 1px) * var(--hafe-tabs-d-join));
}

.hafe-tabs--top .hafe-tabs__tab {
	border-end-start-radius: var(--hafe-tabs-d-facing-radius);
	border-end-end-radius: var(--hafe-tabs-d-facing-radius);
}

.hafe-tabs--start .hafe-tabs__tab {
	border-start-end-radius: var(--hafe-tabs-d-facing-radius);
	border-end-end-radius: var(--hafe-tabs-d-facing-radius);
}

.hafe-tabs--end .hafe-tabs__tab {
	border-start-start-radius: var(--hafe-tabs-d-facing-radius);
	border-end-start-radius: var(--hafe-tabs-d-facing-radius);
}

.hafe-tabs--top {
	--hafe-tabs-facing-be: var(--hafe-tabs-d-facing);
}

.hafe-tabs--start {
	--hafe-tabs-facing-ie: var(--hafe-tabs-d-facing);
}

.hafe-tabs--end {
	--hafe-tabs-facing-is: var(--hafe-tabs-d-facing);
}

.hafe-tabs__panels {
	--hafe-tabs-corner: calc(var(--hafe-tabs-radius, var(--hafe-tabs-d-radius)) * (1 - var(--hafe-tabs-d-join) * var(--hafe-tabs-joined, 1)));
}

.hafe-tabs--top.hafe-tabs--align-start > .hafe-tabs__panels,
.hafe-tabs--top.hafe-tabs--align-stretch > .hafe-tabs__panels,
.hafe-tabs--start > .hafe-tabs__panels {
	border-start-start-radius: var(--hafe-tabs-corner);
}

.hafe-tabs--top.hafe-tabs--align-end > .hafe-tabs__panels,
.hafe-tabs--top.hafe-tabs--align-stretch > .hafe-tabs__panels,
.hafe-tabs--end > .hafe-tabs__panels {
	border-start-end-radius: var(--hafe-tabs-corner);
}

.hafe-tabs__icon {
	display: inline-flex;
	flex: none;
	font-size: var(--hafe-tabs-icon-size, 1.25em);
	line-height: 1;
}

.hafe-tabs__icon svg {
	inline-size: 1em;
	block-size: 1em;
	fill: currentColor;
}

/* The content: one card holding the panels. Where the widget is an accordion, each title shows
   above its panel, the titles parted by a line. */
.hafe-tabs__panels {
	box-sizing: border-box;
	min-inline-size: 0;
	overflow: clip;
	border: var(--hafe-tabs-line-width, 1px) solid var(--hafe-tabs-line, var(--hafe-tabs-d-line));
	border-radius: var(--hafe-tabs-radius, var(--hafe-tabs-d-radius));
	background: var(--hafe-tabs-panel-ground);
	box-shadow: var(--hafe-tabs-d-shadow);
}

.hafe-tabs--start > .hafe-tabs__panels,
.hafe-tabs--end > .hafe-tabs__panels {
	flex: 1 1 0;
}

.hafe-tabs__item {
	margin: 0;
	padding: 0;
}

.hafe-tabs .hafe-tabs__toggle {
	display: var(--hafe-tabs-toggle, none);
	align-items: center;
	gap: 0.75em;
	min-block-size: 2.75rem;
	box-sizing: border-box;
	margin: 0;
	padding: 1em 1.5em;
	/* The tab colours from the Style tab, paired as on the tabs; the design's own stay on the tabs. */
	background: var(--hafe-tabs-bg, transparent);
	color: var(--hafe-tabs-ink, var(--hafe-ink));
	font-weight: 600;
	line-height: 1.4;
	list-style: none;
	cursor: pointer;
	transition: background-color 0.2s;
}

.hafe-tabs__toggle::-webkit-details-marker {
	display: none;
}

.hafe-tabs__item + .hafe-tabs__item > .hafe-tabs__toggle {
	border-block-start: var(--hafe-tabs-line-width, 1px) solid var(--hafe-tabs-d-sep);
}

/* As with the accordion: a closed title tints all over; an open one would be a band on its text. */
.hafe-tabs .hafe-tabs__item:not([open]) > .hafe-tabs__toggle:hover {
	background: var(--hafe-tabs-d-toggle-hover);
}

.hafe-tabs .hafe-tabs__item[open] > .hafe-tabs__toggle {
	background: var(--hafe-tabs-active-bg, transparent);
	color: var(--hafe-tabs-active-ink, var(--hafe-accent-ink));
}

.hafe-tabs .hafe-tabs__item > .hafe-tabs__toggle:focus-visible {
	outline: 2px solid var(--hafe-accent);
	outline-offset: -4px;
}

.hafe-tabs__toggle .hafe-tabs__label {
	flex: 1;
	min-inline-size: 0;
	overflow-wrap: break-word;
}

/* A drawn arrow, which turns as the panel opens. */
.hafe-tabs__chevron {
	flex: none;
	inline-size: 0.5em;
	block-size: 0.5em;
	margin-inline: 0.25em;
	border-block-end: 2px solid;
	border-inline-end: 2px solid;
	transform: translateY(-25%) rotate(45deg);
	transition: transform 0.25s;
}

[open] > .hafe-tabs__toggle .hafe-tabs__chevron {
	transform: translateY(25%) rotate(225deg);
}

.hafe-tabs .hafe-tabs__panel {
	padding: var(--hafe-tabs-panel-padding, var(--hafe-tabs-panel-top, 1.5em) 1.5em 1.5em);
	color: var(--hafe-tabs-panel-ink, var(--hafe-ink));
}

.hafe-tabs__panel > :first-child {
	margin-block-start: 0;
}

.hafe-tabs__panel > :last-child {
	margin-block-end: 0;
}

/* A newly chosen tab's panel fades in (tabs.js marks it). As an accordion, the panel slides open
   where the browser can animate to a height of auto. */
.hafe-tabs__item.is-entering > .hafe-tabs__panel {
	animation: hafe-tabs-in 0.2s ease-out;
}

@keyframes hafe-tabs-in {
	from {
		opacity: 0;
	}
}

.hafe-tabs.is-accordion .hafe-tabs__item::details-content {
	block-size: 0;
	overflow-y: clip;
	transition: block-size 0.25s ease, content-visibility 0.25s allow-discrete;
}

.hafe-tabs.is-accordion .hafe-tabs__item[open]::details-content {
	block-size: auto;
}

@media (prefers-reduced-motion: reduce) {
	.hafe-tabs .hafe-tabs__tab,
	.hafe-tabs .hafe-tabs__toggle,
	.hafe-tabs__chevron,
	.hafe-tabs.is-accordion .hafe-tabs__item::details-content {
		transition: none;
	}

	.hafe-tabs__item.is-entering > .hafe-tabs__panel {
		animation: none;
	}
}

/* Printed, every panel shows under its title (tabs.js opens them all). */
@media print {
	.hafe-tabs .hafe-tabs__list,
	.hafe-tabs__chevron {
		display: none;
	}

	.hafe-tabs .hafe-tabs__toggle {
		display: flex;
	}
}
