/**
 * Data Tabs & Tables - front end
 *
 * Design rule for this file: it may control layout, overflow and position.
 * It may not decide what anything looks like. No font families, no font sizes
 * in absolute units, no colours that are not derived from the theme. Anything
 * visual comes from theme.json custom properties with a neutral fallback, so a
 * table looks like it shipped with the theme rather than with a plugin.
 */

.dtt {
	/* Neutral fallbacks, replaced below where color-mix is available. */
	--dtt-line: rgba(128, 128, 128, 0.32);
	--dtt-bar-fill: rgba(128, 128, 128, 0.22);
	--dtt-accent: var(--wp--preset--color--primary, currentColor);
	--dtt-surface: var(--wp--preset--color--base, Canvas);

	display: block;
	margin-block: 1.5em;
}

@supports (color: color-mix(in srgb, red, blue)) {
	.dtt {
		--dtt-line: color-mix(in srgb, currentColor 18%, transparent);
		--dtt-bar-fill: color-mix(in srgb, var(--dtt-accent) 20%, transparent);
	}
}

/* -------------------------------------------------- Tab strip */

.dtt__tablist {
	display: flex;
	gap: 0.25em;
	margin: 0 0 0.75em;
	overflow-x: auto;
	overscroll-behavior-x: contain;
	scrollbar-width: none;
	-webkit-overflow-scrolling: touch;
	border-block-end: 1px solid var(--dtt-line);
}

.dtt__tablist::-webkit-scrollbar {
	display: none;
}

.dtt__tab {
	flex: 0 0 auto;
	margin: 0;
	padding: 0.5em 0.85em;
	border: 0;
	border-block-end: 2px solid transparent;
	border-radius: 0;
	background: none;
	color: inherit;
	font: inherit;
	line-height: inherit;
	white-space: nowrap;
	cursor: pointer;
	opacity: 0.65;
	transition: opacity 120ms ease, border-color 120ms ease;
}

.dtt__tab:hover {
	opacity: 1;
}

.dtt__tab.is-active {
	opacity: 1;
	font-weight: 600;
	border-block-end-color: var(--dtt-accent);
}

.dtt__tab:focus-visible {
	outline: 2px solid var(--dtt-accent);
	outline-offset: -2px;
}

/* -------------------------------------------------- Panels */

/*
 * Panels are visible by default so a visitor without JavaScript, and any
 * crawler that skips it, gets every table stacked one after another. The
 * dtt-js flag is set in the document head, before first paint, so there is no
 * flash of all panels at once for everybody else.
 */
.dtt-js .dtt__panel:not(.is-active) {
	display: none;
}

.dtt__panel:focus {
	outline: none;
}

.dtt__scrollwrap {
	position: relative;
}

.dtt__scroller {
	overflow: auto;
	overscroll-behavior-x: contain;
	-webkit-overflow-scrolling: touch;
}

.dtt__scroller:focus-visible {
	outline: 2px solid var(--dtt-accent);
	outline-offset: 2px;
}

.dtt--capped .dtt__scroller {
	max-height: var(--dtt-max-height, none);
}

/* Fade the cut-off edge into the page so the overflow is discoverable. */
.dtt__scrollwrap.is-overflowing::after {
	content: "";
	position: absolute;
	inset-block: 0;
	inset-inline-end: 0;
	width: 2.5rem;
	pointer-events: none;
	background: linear-gradient(to left, var(--dtt-surface), transparent);
}

.dtt__scrollwrap.is-scrolled-end::after {
	display: none;
}

/* -------------------------------------------------- Table */

.dtt table {
	width: 100%;
	margin: 0;
	border-collapse: collapse;
}

.dtt caption {
	text-align: start;
	padding-block-end: 0.5em;
}

.dtt .dtt-num {
	font-variant-numeric: tabular-nums;
	font-feature-settings: "tnum";
}

.dtt .dtt-high {
	font-weight: 700;
}

.dtt--scroll .dtt-num {
	text-align: end;
}

/* Pinned first column and header, for wide tables. */
.dtt--scroll thead th:first-child,
.dtt--scroll tbody th[scope="row"] {
	position: sticky;
	inset-inline-start: 0;
	z-index: 2;
	background-color: var(--dtt-surface);
}

.dtt--capped thead th {
	position: sticky;
	inset-block-start: 0;
	z-index: 3;
	background-color: var(--dtt-surface);
}

/* -------------------------------------------------- Value bars */

.dtt__bar {
	position: relative;
	isolation: isolate;
}

.dtt__bar::before {
	content: "";
	position: absolute;
	inset-block: 2px;
	inset-inline-start: 0;
	width: var(--dtt-bar, 0%);
	background: var(--dtt-bar-fill);
	z-index: -1;
}

/* -------------------------------------------------- Filter and tools */

.dtt__filter,
.dtt__copy {
	display: none;
}

.dtt-js .dtt__filter {
	display: flex;
	align-items: center;
	gap: 0.5em;
	flex-wrap: wrap;
	margin-block-end: 0.75em;
}

.dtt-js .dtt__copy {
	display: inline-flex;
}

/* Visually hidden, still announced. Themes cannot be relied on to define
   .screen-reader-text, so the plugin ships its own. */
.dtt__sr {
	position: absolute !important;
	width: 1px;
	height: 1px;
	margin: -1px;
	padding: 0;
	border: 0;
	overflow: hidden;
	white-space: nowrap;
	clip-path: inset(50%);
}

.dtt__icon {
	display: inline-flex;
	flex: 0 0 auto;
}

.dtt__icon svg {
	display: block;
	width: 1.05em;
	height: 1.05em;
}

.dtt__meta .dtt__icon,
.dtt__filter-icon {
	vertical-align: -0.15em;
}

.dtt__meta .dtt__source,
.dtt__meta .dtt__updated {
	display: inline-flex;
	align-items: center;
	gap: 0.35em;
}

.dtt__filter-icon {
	display: inline-flex;
	opacity: 0.7;
}

/* Copy confirmation is a class toggle, so it works for icons and words alike. */
.dtt__state {
	display: inline-flex;
	align-items: center;
	gap: 0.35em;
}

.dtt__state--done {
	display: none;
}

.dtt__copy.is-copied .dtt__state--idle {
	display: none;
}

.dtt__copy.is-copied .dtt__state--done {
	display: inline-flex;
}

.dtt__filter-input {
	flex: 1 1 12em;
	min-width: 0;
}

.dtt__filter-count,
.dtt__meta,
.dtt__legend {
	font-size: 0.875em;
	opacity: 0.75;
}

.dtt__tools {
	display: flex;
	flex-wrap: wrap;
	gap: 1em;
	margin-block: 0.75em 0;
	font-size: 0.875em;
}

.dtt__copy,
.dtt__download {
	align-items: center;
	gap: 0.35em;
}

.dtt__download {
	display: inline-flex;
}

.dtt__copy {
	padding: 0;
	border: 0;
	background: none;
	color: inherit;
	font: inherit;
	text-decoration: underline;
	cursor: pointer;
}

.dtt__meta {
	margin-block-start: 0.75em;
}

.dtt__method {
	margin-block-start: 0.75em;
	font-size: 0.9375em;
}

.dtt__method summary {
	cursor: pointer;
}

.dtt__method-body > :first-child {
	margin-block-start: 0.5em;
}

/* Sortable headers. The arrow follows aria-sort, so state stays in one place. */
.dtt table[data-sortable] th[aria-sort] {
	cursor: pointer;
	user-select: none;
}

.dtt table[data-sortable] th[aria-sort="ascending"]::after {
	content: " \2191";
}

.dtt table[data-sortable] th[aria-sort="descending"]::after {
	content: " \2193";
}

/* -------------------------------------------------- Stacked cards */

@media (max-width: 600px) {

	.dtt--stack table,
	.dtt--stack caption,
	.dtt--stack thead,
	.dtt--stack tbody,
	.dtt--stack tr,
	.dtt--stack th,
	.dtt--stack td {
		display: block;
		width: auto;
	}

	/* Without this the caption inherits display:table-caption inside a block
	   parent, gets wrapped in an anonymous table box, and shrink-wraps to its
	   longest word. */
	.dtt--stack caption {
		text-align: start;
		max-width: none;
	}

	/* Headings move into each cell via data-label, but stay in the document. */
	.dtt--stack thead {
		position: absolute;
		width: 1px;
		height: 1px;
		overflow: hidden;
		white-space: nowrap;
		clip-path: inset(50%);
	}

	.dtt--stack tbody tr {
		padding: 0.6em 0.85em;
		margin-block-end: 0.75em;
		border: 1px solid var(--dtt-line);
	}

	.dtt--stack tbody tr:last-child {
		margin-block-end: 0;
	}

	.dtt--stack tbody th[scope="row"] {
		padding: 0 0 0.35em;
		border: 0;
		font-weight: 600;
		text-align: start;
	}

	.dtt--stack tbody td {
		display: flex;
		align-items: baseline;
		justify-content: space-between;
		gap: 1em;
		padding: 0.35em 0;
		border: 0;
		border-block-start: 1px solid var(--dtt-line);
		text-align: start;
	}

	.dtt--stack tbody td::before {
		content: attr(data-label);
		flex: 0 1 auto;
		opacity: 0.7;
		font-size: 0.9em;
		font-weight: 400;
	}

	.dtt--stack tbody td:not([data-label])::before {
		content: none;
	}

	.dtt--stack .dtt__tab {
		padding-inline: 0.65em;
	}
}

@media (min-width: 601px) {

	.dtt--stack .dtt-num {
		text-align: end;
	}
}

/* -------------------------------------------------- Print */

@media print {

	.dtt-js .dtt__panel:not(.is-active),
	.dtt__panel {
		display: block;
	}

	.dtt__panel {
		break-inside: avoid;
		margin-block-end: 1.5em;
	}

	/* Give each table its tab label back, since the strip is gone. */
	.dtt__panel::before {
		content: attr(data-label);
		display: block;
		font-weight: 700;
		margin-block-end: 0.5em;
	}

	.dtt__tablist,
	.dtt__filter,
	.dtt__tools {
		display: none;
	}

	.dtt__scroller {
		overflow: visible;
		max-height: none;
	}

	.dtt__method {
		display: block;
	}
}

@media (prefers-reduced-motion: reduce) {

	.dtt__tab {
		transition: none;
	}
}
