/**
 * Tabellen (tomtut-bausteine) – Darstellung für den Block „Tabelle“.
 *
 * Farben: Fest gesetzte Farben kommen ausschließlich aus Preset-Variablen des
 * Themes (var(--wp--preset--color--…)); Linien, Streifen und der Standard-Kopf
 * sind aus der Textfarbe gemischt (currentColor) und gehen dadurch im hellen
 * wie im dunklen Modus mit. Text erbt die Farbe des Themes.
 *
 * Anpassen im Theme (z. B. im Dark Mode) über die Variablen --tt-tabelle-*.
 *   --tt-tabelle-kopf-hg / --tt-tabelle-kopf-text  Kopfzeile
 *   --tt-tabelle-flaeche                           deckender Grund (fixierte erste Spalte)
 *
 * Kopf „kopf-primary“: Weiß auf dem Hausblau (Preset primary, #34A2E6) = rund 2,8:1
 * (bewusste Vorgabe des Kunden, unter AA).
 */

.tt-tabelle {
	--tt-tabelle-rand: color-mix(in srgb, currentColor 22%, transparent);
	--tt-tabelle-streifen: color-mix(in srgb, currentColor 5%, transparent);
	--tt-tabelle-kopf-hg: color-mix(in srgb, currentColor 9%, transparent);
	--tt-tabelle-kopf-text: currentColor;
	--tt-tabelle-flaeche: var(--wp--preset--color--weiss);
	--tt-tabelle-innen: 0.6em 0.8em;
	container-type: inline-size;
	margin-inline: 0;
	margin-block: 0 var(--wp--style--block-gap, 1.5em);
	padding: 0;
}

.tt-tabelle--stil-kopf-primary {
	--tt-tabelle-kopf-hg: var(--wp--preset--color--primary);
	--tt-tabelle-kopf-text: var(--wp--preset--color--weiss);
}

/*
 * Dunkles Farbschema des Themes (prefers-color-scheme oder gemerkte Wahl):
 * Das Hausblau bleibt dort gleich; die Kopfschrift wird dunkel gesetzt, weil Weiß
 * darauf keinen ausreichenden Kontrast erreicht. Kopf dunkel beschriften und
 * den Tabellentext aus dem Text-Token des Themes nehmen.
 */
@media (prefers-color-scheme: dark) {
	html:root:not([data-farbschema="hell"]) .tt-tabelle {
		color: var(--wp--preset--color--text);
	}

	html:root:not([data-farbschema="hell"]) .tt-tabelle--stil-kopf-primary {
		--tt-tabelle-kopf-text: #10161c;
	}
}

html:root[data-farbschema="dunkel"] .tt-tabelle {
	color: var(--wp--preset--color--text);
}

html:root[data-farbschema="dunkel"] .tt-tabelle--stil-kopf-primary {
	--tt-tabelle-kopf-text: #10161c;
}

/* Rahmen: waagerecht wischbar, Tastatur-Fokus sichtbar. */
.tt-tabelle__rahmen {
	position: relative;
	max-width: 100%;
	overflow-x: auto;
	overscroll-behavior-x: contain;
	scrollbar-width: thin;
	scrollbar-color: var(--tt-tabelle-rand) transparent;
}

.tt-tabelle__rahmen:focus-visible {
	outline: 2px solid currentColor;
	outline-offset: 2px;
}

.tt-tabelle__tabelle {
	width: 100%;
	margin: 0;
	border: 0;
	border-collapse: separate;
	border-spacing: 0;
	font-size: inherit;
	line-height: 1.5;
	table-layout: auto;
}

.tt-tabelle__tabelle :is(th, td) {
	padding: var(--tt-tabelle-innen);
	border: 0;
	border-block-end: 1px solid var(--tt-tabelle-rand);
	background: none;
	text-align: start;
	vertical-align: top;
	overflow-wrap: break-word;
}

.tt-tabelle__tabelle thead th {
	background-color: var(--tt-tabelle-kopf-hg);
	color: var(--tt-tabelle-kopf-text);
	font-weight: 600;
	vertical-align: bottom;
	border-block-end-width: 2px;
}

.tt-tabelle__tabelle thead th :is(a, a:visited) {
	color: inherit;
}

.tt-tabelle--stil-kopf-primary .tt-tabelle__tabelle thead th {
	border-block-end-color: transparent;
}

.tt-tabelle--stil-kopf-primary .tt-tabelle__tabelle thead tr > :first-child {
	border-start-start-radius: 0.5em;
}

.tt-tabelle--stil-kopf-primary .tt-tabelle__tabelle thead tr > :last-child {
	border-start-end-radius: 0.5em;
}

.tt-tabelle__tabelle tbody tr:nth-child(even) > * {
	background-color: var(--tt-tabelle-streifen);
}

.tt-tabelle__tabelle tbody th[scope="row"] {
	font-weight: 600;
}

.tt-tabelle__tabelle tfoot :is(th, td) {
	font-weight: 600;
	border-block-start: 2px solid var(--tt-tabelle-rand);
	border-block-end: 0;
}

.tt-tabelle__tabelle .tt-tabelle__zelle--mitte {
	text-align: center;
}

.tt-tabelle__tabelle .tt-tabelle__zelle--rechts {
	text-align: end;
}

.tt-tabelle__tabelle img {
	max-width: 100%;
	height: auto;
}

.tt-tabelle__tabelle :is(p, ul, ol) {
	margin-block: 0 0.5em;
}

.tt-tabelle__tabelle :is(p, ul, ol):last-child {
	margin-block-end: 0;
}

/* Frühere Inline-Farbe „Rot“ (Entscheidung 8: Palettenfarbe Warnung). */
.tt-tabelle .tt-tabelle__warnung {
	color: var(--wp--preset--color--warnung);
}

/* Nur für Vorlesesoftware sichtbar. */
.tt-tabelle__beschriftung,
.tt-tabelle__unsichtbar {
	position: absolute !important;
	width: 1px !important;
	height: 1px !important;
	margin: -1px !important;
	padding: 0 !important;
	overflow: hidden !important;
	clip: rect(0 0 0 0) !important;
	clip-path: inset(50%) !important;
	white-space: nowrap !important;
	border: 0 !important;
}

/* Sortierknöpfe: echte Knöpfe, Tippfläche mindestens 44 px. */
.tt-tabelle__tabelle thead th[data-sortierbar] {
	padding-block: 0;
}

.tt-tabelle .tt-tabelle__sortierknopf,
.tt-tabelle .tt-tabelle__sortierknopf:is(:hover, :focus, :active) {
	appearance: none;
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 0.5em;
	width: 100%;
	min-height: 44px;
	margin: 0;
	padding: 0;
	border: 0;
	border-radius: 0;
	background: none;
	box-shadow: none;
	color: inherit;
	font: inherit;
	letter-spacing: inherit;
	line-height: inherit;
	text-align: inherit;
	text-decoration: none;
	text-transform: none;
	cursor: pointer;
}

.tt-tabelle .tt-tabelle__sortierknopf:hover .tt-tabelle__kopftext {
	text-decoration: underline;
	text-underline-offset: 0.2em;
}

.tt-tabelle .tt-tabelle__sortierknopf:focus-visible {
	outline: 2px solid currentColor;
	outline-offset: -4px;
}

.tt-tabelle .tt-tabelle__zelle--rechts .tt-tabelle__sortierknopf {
	flex-direction: row-reverse;
}

.tt-tabelle .tt-tabelle__zelle--mitte .tt-tabelle__sortierknopf {
	justify-content: center;
}

.tt-tabelle__sortsymbol {
	display: inline-flex;
	flex: none;
	gap: 0.1em;
	font-size: 0.75em;
	line-height: 1;
}

.tt-tabelle__sortsymbol .tt-symbol {
	opacity: 0.45;
}

.tt-tabelle th[aria-sort] .tt-tabelle__sortsymbol .tt-symbol {
	opacity: 1;
}

.tt-tabelle th[aria-sort="ascending"] .tt-tabelle__pfeil--ab,
.tt-tabelle th[aria-sort="descending"] .tt-tabelle__pfeil--auf {
	display: none;
}

.tt-tabelle__kartenkopf {
	display: none;
}

/* Hinweis für Redakteure in der Editor-Vorschau. */
.tt-tabelle-hinweis {
	padding: 0.75em 1em;
	border: 1px dashed currentColor;
	border-radius: 0.5em;
}

/* Schmaler Platz (Handy, schmale Spalten): Verhalten je Attribut „mobil“. */
@container (max-width: 37.5em) {

	/* scrollen: Spalten bleiben, waagerecht wischen im sichtbaren Rand. */
	.tt-tabelle--mobil-scrollen .tt-tabelle__rahmen {
		border: 1px solid var(--tt-tabelle-rand);
		border-radius: 0.5em;
	}

	.tt-tabelle--mobil-scrollen .tt-tabelle__tabelle :is(th, td) {
		min-width: 6.5em;
	}

	.tt-tabelle--mobil-scrollen .tt-tabelle__tabelle tbody tr:last-child > *,
	.tt-tabelle--mobil-scrollen .tt-tabelle__tabelle tfoot :is(th, td) {
		border-block-end: 0;
	}

	.tt-tabelle--mobil-scrollen.tt-tabelle--stil-kopf-primary .tt-tabelle__tabelle thead tr > * {
		border-radius: 0;
	}

	/* Fixierte erste Spalte, wenn sie Zeilenkopf ist. */
	.tt-tabelle--mobil-scrollen.tt-tabelle--zeilenkopf .tt-tabelle__tabelle :is(thead tr > :first-child, tbody th[scope="row"], tfoot tr > :first-child) {
		position: sticky;
		inset-inline-start: 0;
		z-index: 1;
		background-image: linear-gradient(var(--tt-tabelle-streifen), var(--tt-tabelle-streifen));
		background-color: var(--tt-tabelle-flaeche);
		box-shadow: inset -1px 0 0 var(--tt-tabelle-rand);
	}

	.tt-tabelle--mobil-scrollen.tt-tabelle--zeilenkopf .tt-tabelle__tabelle thead tr > :first-child {
		background-image: linear-gradient(var(--tt-tabelle-kopf-hg), var(--tt-tabelle-kopf-hg));
	}

	/* karten: jede Zeile als Karte, Spaltenköpfe in jeder Zelle. */
	.tt-tabelle--mobil-karten .tt-tabelle__rahmen {
		overflow: visible;
	}

	.tt-tabelle--mobil-karten .tt-tabelle__tabelle,
	.tt-tabelle--mobil-karten .tt-tabelle__tabelle :is(tbody, tfoot, tr, th, td) {
		display: block;
		width: auto;
	}

	.tt-tabelle--mobil-karten .tt-tabelle__tabelle thead {
		position: absolute;
		width: 1px;
		height: 1px;
		margin: -1px;
		padding: 0;
		overflow: hidden;
		clip: rect(0 0 0 0);
		clip-path: inset(50%);
		white-space: nowrap;
		border: 0;
	}

	.tt-tabelle--mobil-karten .tt-tabelle__tabelle :is(tbody, tfoot) tr {
		margin-block-end: 0.75em;
		border: 1px solid var(--tt-tabelle-rand);
		border-radius: 0.5em;
		overflow: hidden;
	}

	.tt-tabelle--mobil-karten .tt-tabelle__tabelle :is(tbody, tfoot) tr > * {
		background-color: transparent;
		border-block-start: 0;
		border-block-end: 1px solid var(--tt-tabelle-rand);
		text-align: start;
	}

	.tt-tabelle--mobil-karten .tt-tabelle__tabelle :is(tbody, tfoot) tr > :last-child {
		border-block-end: 0;
	}

	.tt-tabelle--mobil-karten .tt-tabelle__tabelle :is(tbody, tfoot) tr > :first-child {
		background-color: var(--tt-tabelle-streifen);
		font-weight: 600;
	}

	.tt-tabelle--mobil-karten .tt-tabelle__tabelle :is(td, th):has(> .tt-tabelle__kartenkopf) {
		display: grid;
		grid-template-columns: minmax(0, 2fr) minmax(0, 3fr);
		gap: 0.25em 0.75em;
	}

	.tt-tabelle--mobil-karten .tt-tabelle__kartenkopf {
		display: block;
		font-weight: 600;
	}

	.tt-tabelle--mobil-karten .tt-tabelle__tabelle :is(td, th):has(> .tt-tabelle__inhalt:empty) {
		display: none;
	}

	/* Sortieren in der Kartenansicht: Kopfzeile wird zur Knopfleiste. */
	.tt-tabelle--mobil-karten.tt-tabelle--sortierbar .tt-tabelle__tabelle thead {
		position: static;
		display: block;
		width: auto;
		height: auto;
		margin: 0 0 0.75em;
		overflow: visible;
		clip: auto;
		clip-path: none;
		white-space: normal;
	}

	.tt-tabelle--mobil-karten.tt-tabelle--sortierbar .tt-tabelle__tabelle thead tr {
		display: flex;
		flex-wrap: wrap;
		gap: 0.5em;
	}

	.tt-tabelle--mobil-karten.tt-tabelle--sortierbar .tt-tabelle__tabelle thead th {
		display: block;
		padding: 0;
		border: 0;
		border-radius: 0;
		background: none;
		color: inherit;
	}

	.tt-tabelle--mobil-karten.tt-tabelle--sortierbar .tt-tabelle__tabelle thead th:not([data-sortierbar]) {
		position: absolute;
		width: 1px;
		height: 1px;
		margin: -1px;
		overflow: hidden;
		clip: rect(0 0 0 0);
		clip-path: inset(50%);
		white-space: nowrap;
	}

	.tt-tabelle--mobil-karten.tt-tabelle--sortierbar .tt-tabelle__sortierknopf,
	.tt-tabelle--mobil-karten.tt-tabelle--sortierbar .tt-tabelle__sortierknopf:is(:hover, :focus, :active) {
		width: auto;
		padding: 0 0.9em;
		border: 1px solid var(--tt-tabelle-rand);
		border-radius: 999px;
	}
}
