/*
 * Accepted core/table contract.
 *
 * Visual variants are four independent axes stored in the standard className:
 * data type (text/numeric), density (default/compact), borders
 * (default/minimal/none) and row coloring (none/stripes).
 */

.wp-block-table,
:where(.sggw-text-editor-table) {
	border-radius: var(--wp--custom--radius--small);
	overflow-x: auto;
}

.wp-block-table:focus-visible,
:where(.sggw-text-editor-table):focus-visible {
	outline: var(--wp--custom--line--focus-ring-width) solid var(--sggw-line-focus);
	outline-offset: var(--wp--custom--line--focus-ring-offset-inset);
}

.wp-block-table > table {
	width: max-content;
	min-width: 100%;
}

.wp-block-table > table,
:where(.sggw-text-editor-table > table) {
	border: var(--sggw-text-table-line-width, var(--wp--custom--line--width)) solid var(--sggw-line);
	border-collapse: separate;
	border-spacing: 0;
	border-radius: var(--wp--custom--radius--small);
	table-layout: auto;
}

.wp-block-table > table.has-fixed-layout {
	width: 100%;
	table-layout: fixed;
}

.wp-block-table > table.has-fixed-layout :is(th, td) {
	overflow-wrap: anywhere;
	word-break: normal;
}

.wp-block-table :is(thead, tbody, tfoot),
:where(.sggw-text-editor-table > table > :is(thead, tbody, tfoot)) {
	border: 0;
}

.wp-block-table :is(th, td),
:where(.sggw-text-editor-table > table > :is(thead, tbody, tfoot) > tr > :is(th, td)) {
	border: 0;
	padding-block: var(--sggw-text-table-cell-padding, var(--wp--preset--spacing--30));
	padding-inline: var(--sggw-text-table-cell-padding, var(--wp--preset--spacing--30));
}

.wp-block-table th:not([data-align]):not([class*="has-text-align-"]),
:where(.sggw-text-editor-table > table > :is(thead, tbody, tfoot) > tr > th:not([align]):not([data-align]):not([class*="has-text-align-"])) {
	text-align: left;
}

.wp-block-table.is-sggw-table-data-numeric :is(th, td) {
	font-family: var(--wp--preset--font-family--monospace);
}

.wp-block-table.is-sggw-table-data-numeric
	:is(th, td):not([data-align]):not([class*="has-text-align-"]) {
	text-align: right;
}

.wp-block-table.is-sggw-table-density-compact :is(th, td) {
	padding-block: var(--wp--preset--spacing--10);
}

.wp-block-table tr > :is(th, td) + :is(th, td),
:where(.sggw-text-editor-table > table > :is(thead, tbody, tfoot) > tr > :is(th, td) + :is(th, td)),
:where(.sggw-text-editor-table > table > :is(thead, tbody, tfoot) > tr > .sggw-table-cell-after-span) {
	border-inline-start: var(--sggw-text-table-line-width, var(--wp--custom--line--width)) solid var(--sggw-line);
}

.wp-block-table :is(thead, tbody, tfoot) > tr + tr > :is(th, td),
.wp-block-table > table > :is(tbody, tfoot):not(:first-child) > tr:first-child > :is(th, td),
:where(.sggw-text-editor-table > table > :is(thead, tbody, tfoot) > tr + tr > :is(th, td)),
:where(.sggw-text-editor-table > table > :is(tbody, tfoot):not(:first-child) > tr:first-child > :is(th, td)) {
	border-block-start: var(--sggw-text-table-line-width, var(--wp--custom--line--width)) solid var(--sggw-line);
}

.wp-block-table:is(
	.is-sggw-table-borders-minimal,
	.is-sggw-table-borders-none
) > table {
	border: 0;
	border-radius: 0;
}

.wp-block-table:is(
	.is-sggw-table-borders-minimal,
	.is-sggw-table-borders-none
) tr > :is(th, td) + :is(th, td) {
	border-inline-start: 0;
}

.wp-block-table.is-sggw-table-borders-none
	:is(thead, tbody, tfoot)
	> tr
	+ tr
	> :is(th, td),
.wp-block-table.is-sggw-table-borders-none
	> table
	> :is(tbody, tfoot):not(:first-child)
	> tr:first-child
	> :is(th, td) {
	border-block-start: 0;
}

.wp-block-table.is-style-stripes {
	border-block-end: 0;
}

.wp-block-table.is-style-stripes :is(th, td) {
	border-color: var(--sggw-line);
}

.wp-block-table.is-style-stripes tbody tr:nth-child(odd) {
	background-color: var(--sggw-surface-alternating);
}

/* Raw rich-text HTML shares the paint above, but not Core's width or variants.
 * Zero-specificity defaults leave authored classes and inline styles stronger.
 * Direct-child selectors deliberately leave nested tables alone. */
:where(.sggw-text-editor-table) {
	box-sizing: border-box;
	max-width: 100%;
	min-width: 0;
}
:where(.sggw-text-editor-table > table:not([width])) {
	width: 100%;
}
:where(.sggw-text-editor-table > table) {
	margin-block-end: var(--wp--preset--spacing--30);
}
:where(.sggw-text-editor-table > table[border]) {
	border-width: var(--sggw-text-table-line-width);
}
:where(.sggw-text-editor-table > table > :is(thead, tbody, tfoot) > tr > [data-align="left"]) { text-align: left; }
:where(.sggw-text-editor-table > table > :is(thead, tbody, tfoot) > tr > [data-align="center"]) { text-align: center; }
:where(.sggw-text-editor-table > table > :is(thead, tbody, tfoot) > tr > [data-align="right"]) { text-align: right; }

@media (forced-colors: active) {
	.wp-block-table > table,
	.wp-block-table :is(th, td),
	:where(.sggw-text-editor-table > table),
	:where(.sggw-text-editor-table > table > :is(thead, tbody, tfoot) > tr > :is(th, td)) {
		border-color: CanvasText;
	}
}
