@use '../00-abstract/index' as *;
@use '../tokens/variables';

/* Widgets - Table */
////
/// @group Widgets-Table
/// Widgets - Table

///
.table {
	// ─── Component CSS API ─────────────────────────────────────────────
	--osui-table-border-color: var(--color-border);
	--osui-table-border-radius: var(--border-radius-lg, #{variables.$token-shape-soft-lg});
	--osui-table-header-background: #{variables.$token-bg-neutral-subtle-default};
	--osui-table-header-color: var(--color-text-subtlest);
	--osui-table-cell-background: var(--color-background-surface);
	--osui-table-row-hover-background: var(--color-background-input-disabled);
	--osui-table-row-stripe-background: #{variables.$token-bg-neutral-subtle-default};
	--osui-table-sorted-color: var(--color-text);
	--osui-table-row-selected-background: var(--color-primary-selected);
	// ───────────────────────────────────────────────────────────────────

	border: variables.$token-border-size-025 solid var(--osui-table-border-color);
	border-radius: var(--osui-table-border-radius);
	border-spacing: 0;
	empty-cells: show;
	white-space: nowrap;
	width: 100%;
	word-break: keep-all;

	&-header {
		// Service Studio Preview
		& {
			-servicestudio-border-bottom: none !important;
		}

		th {
			background-color: var(--osui-table-header-background);
			border-block-end: variables.$token-border-size-025 solid var(--osui-table-border-color);
			color: var(--osui-table-header-color);
			font-size: variables.$token-font-size-350;
			font-weight: variables.$token-font-weight-regular;
			height: variables.$token-scale-1200;
			padding-block: variables.$token-scale-0;
			padding-inline: variables.$token-scale-600;
			text-align: start;

			&:first-child {
				border-start-start-radius: var(--osui-table-border-radius);
			}

			&:last-child {
				border-start-end-radius: var(--osui-table-border-radius);
			}

			&.sortable {
				cursor: pointer;
			}

			&.sorted {
				color: var(--osui-table-sorted-color);
				font-weight: variables.$token-font-weight-semi-bold;
			}
		}
	}

	&-row {
		&:hover td {
			background: var(--osui-table-row-hover-background);
		}

		&.table-row-selected td {
			background: color-mix(
				in srgb,
				var(--osui-table-row-selected-background) 10%,
				#{variables.$token-bg-surface-default}
			);
		}

		td {
			background: var(--osui-table-cell-background);
			border-block-end: variables.$token-border-size-025 solid var(--osui-table-border-color);
			color: var(--color-text);
			font-size: variables.$token-font-size-350;
			height: variables.$token-scale-1600;
			padding-block: variables.$token-scale-200;
			padding-inline: variables.$token-scale-600;
			transition: background-color variables.$token-transition-time-100 ease; // no token match for ease
			vertical-align: inherit;
		}

		&-small {
			td {
				height: variables.$token-scale-1000;
			}
		}

		&-medium {
			td {
				height: variables.$token-scale-1200;
			}
		}

		&-stripping:nth-child(even) {
			td {
				background: var(--osui-table-row-stripe-background);
			}
			&:hover td {
				background: var(--osui-table-row-hover-background);
			}
		}
	}

	.table-row:last-child {
		& > td {
			border-block-end: none;
		}

		& > td:first-child {
			border-end-start-radius: var(--osui-table-border-radius);
		}

		& > td:last-child {
			border-end-end-radius: var(--osui-table-border-radius);
		}
	}
}

///
.table-cell-secondary {
	color: var(--color-text-subtle);
}

// Responsive --------------------------------------------------------------------
///
.desktop {
	.table-header th.sortable:hover {
		color: var(--osui-table-sorted-color);
	}
}

///
.phone,
.tablet {
	.table {
		th {
			width: 100%;
		}

		overflow-x: auto;

		// Service Studio Preview
		& {
			-servicestudio-word-break: normal;
		}
	}

	.layout-native {
		.table {
			thead {
				display: table-header-group;
			}

			tr {
				display: table-row;
			}

			td {
				background: var(--osui-table-cell-background);
				border-block-end: variables.$token-border-size-025 solid var(--osui-table-border-color);
				display: table-cell;
				padding-block: variables.$token-scale-200;
				padding-inline: variables.$token-scale-600;
				vertical-align: inherit;
				width: auto !important;
			}

			td:before {
				display: none;
			}
		}
	}
}

///
.phone {
	.table {
		display: block;
		overflow: auto;
		position: relative;

		// Service Studio Preview
		& {
			-servicestudio-display: table;
		}
	}

	.layout-native {
		.table {
			td:before {
				display: none;
			}
		}
	}
}

// Accessibility ------------------------------------------------------------------
///
.has-accessible-features {
	th.sortable:focus-visible {
		@include a11y-focus-shadow;

		position: relative;
		z-index: var(--layer-global-screen);
	}
}
