// Shared 1c components: page frame, header, cards, form controls and the switch.
// Used by every 1c entry (admin-ui, forms, mapping); everything is scoped under .cf7hs-ui.
// Rules: @use only; logical properties instead of an RTL build; no :has().
@use "tokens" as *;

.cf7hs-ui {
	color: $gray-900;

	// The header bar sits flush under the admin bar, like the @wordpress/components admin pages.
	&.wrap {
		margin-block-start: 0;
	}

	&__header {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		justify-content: space-between;
		gap: $grid-unit-20;
		min-height: 60px;
		margin-inline: -20px;
		padding: $grid-unit-10 $grid-unit-30;
		background: $white;
		border-block-end: 1px solid #dcdcde;

		h1 {
			margin: 2px 0 0;
			padding: 0;
			font-size: 20px;
			font-weight: 400;
			line-height: 1.2;
			color: $gray-900;
		}
	}

	&__crumb {
		margin: 0;
		font-size: 12px;
		color: $gray-700;
	}

	> .notice {
		max-width: 760px;
		margin-block: $grid-unit-20 0;
		margin-inline: auto;
		box-sizing: border-box;
	}

	&__body {
		display: flex;
		flex-direction: column;
		gap: $grid-unit-20;
		max-width: 760px;
		margin-inline: auto;
		padding-block: $grid-unit-30 $grid-unit-80;
	}

	&__card {
		background: $white;
		border: 1px solid rgba(0, 0, 0, 0.1);
		border-radius: $radius-medium;
	}

	&__card-header {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		justify-content: space-between;
		gap: $grid-unit-15;
		padding: $grid-unit-15 $grid-unit-30;
		border-block-end: 1px solid $divider;

		h2 {
			margin: 0;
			font-size: 15px;
			font-weight: 600;
			line-height: 1.3;
			color: $gray-900;
		}
	}

	&__card-body {
		padding: $grid-unit-20 $grid-unit-30;
	}

	&__list {
		padding-block: $grid-unit-05;
	}

	&__grid {
		display: grid;
		grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
		gap: $grid-unit-20;
	}

	&__label {
		display: block;
		margin-block: $grid-unit-15 $grid-unit-10;
		// WordPress admin label: 13px semibold, sentence case (no tiny uppercase).
		font-size: 13px;
		font-weight: 600;
		line-height: 1.4;
		color: $gray-900;

		&:first-child {
			margin-block-start: 0;
		}
	}

	&__help {
		margin: $grid-unit-10 0 0;
		font-size: 13px;
		color: $gray-700;
		text-wrap: pretty;
	}

	&__row {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		gap: $grid-unit-10;
	}

	&__grow {
		flex: 1 1 240px;
		min-width: 0;
	}

	&__wide {
		width: 100%;
	}

	&__toggle-row {
		display: flex;
		align-items: flex-start;
		justify-content: space-between;
		gap: $grid-unit-30;
		padding-block: $grid-unit-20;

		& + & {
			border-block-start: 1px solid $hairline;
		}

		.notice.inline {
			margin-block: $grid-unit-10 0;
		}
	}

	&__toggle-text {
		flex: 1 1 auto;
		min-width: 0;
	}

	&__toggle-title {
		font-size: 13px;
		font-weight: 500;
		color: $gray-900;
		cursor: pointer;
	}

	code {
		font-size: 11.5px;
		padding: 2px 5px;
		border-radius: $radius-small;
		background: $gray-100;
	}

	// Controls at the 40px size of @wordpress/components.
	input[type="text"],
	input[type="email"],
	input[type="number"] {
		min-height: $button-size-next-default-40px;
		max-width: 100%;
		padding: 0 $grid-unit-15;
		border: 1px solid $control-border;
		border-radius: $radius-small;
		font-size: 13px;
		color: $gray-900;
		box-shadow: none;

		&:focus {
			border-color: $accent;
			box-shadow: 0 0 0 1px $accent;
			outline: 2px solid transparent;
		}
	}

	input.small-text {
		width: 96px;
	}

	.button {
		display: inline-flex;
		align-items: center;
		min-height: $button-size-next-default-40px;
		padding: 0 $grid-unit-15;
		border-radius: $radius-small;
		line-height: 1;
	}

	// Switch: the real checkbox, restyled (32×16 track, 12px thumb). The label is also a target.
	input[type="checkbox"][role="switch"] {
		appearance: none;
		-webkit-appearance: none;
		position: relative;
		flex: none;
		width: 32px;
		min-width: 32px;
		height: 16px;
		margin: 4px 0;
		padding: 0;
		border: 1px solid #8d8d8d;
		border-radius: 9px;
		background: $white;
		box-shadow: none;
		cursor: pointer;
		transition: background-color 0.1s ease, border-color 0.1s ease;

		&::before {
			content: "";
			position: absolute;
			inset-block-start: 1px;
			inset-inline-start: 1px;
			width: 12px;
			height: 12px;
			margin: 0;
			border-radius: 50%;
			background: $gray-900;
			transition: transform 0.1s ease;
		}

		&:checked {
			background: $accent;
			border-color: $accent;

			&::before {
				content: "";
				background: $white;
				transform: translateX(16px);
			}
		}

		&:focus {
			box-shadow: none;
			outline: none;
		}

		&:focus-visible {
			outline: $focus-width solid $accent;
			outline-offset: 2px;
		}

		@media (prefers-reduced-motion: reduce) {
			transition: none;

			&::before {
				transition: none;
			}
		}

		@media (forced-colors: active) {
			border-color: CanvasText;
			background: Canvas;

			&::before {
				background: CanvasText;
			}

			&:checked {
				border-color: Highlight;
				background: Highlight;

				&::before {
					background: HighlightText;
				}
			}
		}
	}

	@media (max-width: 782px) {
		&__header {
			margin-inline: -10px;
			padding: $grid-unit-10 $grid-unit-15;
		}

		&__body {
			padding-block: $grid-unit-15 $grid-unit-50;
		}

		&__card-header,
		&__card-body {
			padding-inline: $grid-unit-15;
		}

		&__toggle-row {
			gap: $grid-unit-20;
		}

		input[type="text"],
		input[type="email"],
		input[type="number"] {
			font-size: 16px;
		}
	}
}

// RTL: the thumb starts on the inline-start side, so it travels the other way.
.rtl .cf7hs-ui input[type="checkbox"][role="switch"]:checked::before {
	transform: translateX(-16px);
}

.cf7hs-ui [hidden],
.cf7hs-ui .hidden {
	display: none !important;
}

.cf7hs-ui__actions-slot {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: 16px;
}

// List screens (Forms, Logs): the white panel around a WP_List_Table, its title and sub-line, and
// the header and row rules both lists share. Screen-specific columns live in forms.scss / logs.scss.
.cf7hs-ui {
	&__body--wide {
		max-width: 1100px;
	}

	&__panel {
		background: $white;
		border: 1px solid rgba(0, 0, 0, 0.1);
		border-radius: $radius-medium;
	}

	&__row-title {
		font-weight: 500;
		color: $gray-900;
		text-decoration: none;
	}

	&__row-title:hover,
	&__row-title:focus {
		color: $accent;
	}

	&__row-sub {
		display: block;
		margin-block-start: 2px;
		font-size: 12px;
		color: $text-muted;
	}

	.wp-list-table {
		border: 0;
		box-shadow: none;

		tfoot {
			display: none; // Core always prints a footer header row; one is enough.
		}

		thead th,
		thead td {
			padding: 10px $grid-unit-15;
			font-size: 12px;
			font-weight: 500;
			line-height: 1.4;
			color: $text-muted;
			border-block-end: 1px solid $gray-200;
		}

		tbody > tr {
			background: transparent; // No core stripes.
		}

		tbody > tr > * {
			padding: $grid-unit-15;
			border-block-end: 1px solid $gray-100;
			background: transparent;
		}

		tbody > tr:last-child > * {
			border-block-end: 0;
		}
	}
}

.cf7hs-ui {
	// List toolbar (Forms, Logs): tabs on the left, search on the right. The search box is first in
	// the markup (Enter submits it, not a bulk button or a filter), so `order` puts it back right.
	// Screens place their own controls with order 2 (the Forms bulk bar, the Logs form filter).
	.tablenav.top {
		display: flex;
		flex-wrap: wrap;
		align-items: stretch;
		gap: 0 $grid-unit-20;
		height: auto;
		min-height: 56px;
		margin: 0;
		padding-block: 0;
		padding-inline: $grid-unit-10 $grid-unit-20;
		// The line is a shadow, not a border, so the current tab's underline sits exactly on it.
		box-shadow: inset 0 -1px 0 $gray-200;

		.tablenav-pages { order: 4; align-self: center; margin: 0; float: none; }
	}
	.tablenav.bottom {
		height: auto;
		margin: 0;
		padding: $grid-unit-15 $grid-unit-20;
		border-block-start: 1px solid $gray-200;
	}
	.tablenav-pages.one-page { display: none; }

	&__views-nav { order: 1; display: flex; }
	&__views {
		display: flex;
		flex-wrap: wrap;
		gap: $grid-unit-05;
		margin: 0;
		padding: 0;
		list-style: none;

		li { display: flex; margin: 0; }
		a {
			display: flex;
			align-items: center;
			gap: 6px;
			padding-inline: $grid-unit-15;
			font-size: 13px;
			color: $gray-900;
			text-decoration: none;
			white-space: nowrap;
		}
		a:hover { color: $accent; }
		.count { font-size: 12px; color: $text-muted; }
		// Tab underline across the whole tab, on the toolbar's bottom line (WordPress components tabs).
		a.current { font-weight: 500; box-shadow: inset 0 -2px 0 $accent; }
		a:focus { box-shadow: none; outline: $focus-width solid $accent; outline-offset: calc(-1 * #{$focus-width}); }
		a.current:focus { box-shadow: inset 0 -2px 0 $accent; }
	}

	&__search {
		order: 3;
		position: relative;
		display: flex;
		align-items: center;
		align-self: center;
		margin-inline-start: auto;

		// Magnifier: a ring and a handle, both borders so they survive forced colours.
		&::before {
			content: "";
			position: absolute;
			inset-inline-start: 11px;
			width: 8px;
			height: 8px;
			border: 1.5px solid $text-muted;
			border-radius: 50%;
			pointer-events: none;
		}
		&::after {
			content: "";
			position: absolute;
			inset-inline-start: 21px;
			inset-block-start: calc(50% + 4px);
			width: 5px;
			border-block-start: 1.5px solid $text-muted;
			transform: rotate(45deg);
			pointer-events: none;
		}
	}
	// Beats core's `.wp-core-ui .tablenav input[type="search"]`.
	.tablenav .cf7hs-ui__search input[type="search"] {
		width: 240px;
		min-height: $button-size-compact;
		margin: 0;
		padding-inline: 30px 28px;
		border: 1px solid transparent;
		border-radius: $radius-small;
		background: $gray-100;
		font-size: 13px;
		color: $gray-900;
		box-shadow: none;
	}
	.tablenav .cf7hs-ui__search input[type="search"]:focus {
		border-color: $accent;
		background: $white;
		box-shadow: 0 0 0 1px $accent;
		outline: 2px solid transparent;
	}

	&__search-clear {
		position: absolute;
		inset-inline-end: $grid-unit-05;
		display: flex;
		align-items: center;
		justify-content: center;
		width: $button-size-small;
		height: $button-size-small;
		font-size: 16px;
		line-height: 1;
		color: $text-muted;
		text-decoration: none;
	}

	// An empty list screen (no CF7 forms, nothing logged): one card instead of the table.
	&__empty {
		display: flex;
		flex-direction: column;
		align-items: center;
		gap: $grid-unit-10;
		padding: 56px $grid-unit-30 60px;
		text-align: center;

		h2 { margin: 0; font-size: 15px; font-weight: 600; color: $gray-900; }
		p { max-width: 420px; margin: 0; font-size: 13px; color: $text-muted; text-wrap: pretty; }
		.button { margin-block-start: $grid-unit-10; min-height: $button-size-next-default-40px; }
	}

	@media (max-width: 782px) {
		.tablenav.top {
			flex-direction: column;
			align-items: stretch;
			padding: 0;
		}
		&__search {
			order: 0;
			align-self: stretch;
			margin-block: $grid-unit-15 $grid-unit-05;
			margin-inline: $grid-unit-15;
		}
		.tablenav .cf7hs-ui__search input[type="search"] { width: 100%; min-height: $button-size-next-default-40px; padding-inline-end: $button-size-next-default-40px; font-size: 16px; }
		&__search-clear { width: $button-size-next-default-40px; height: $button-size-next-default-40px; inset-inline-end: 0; }
		&__views a { min-height: 44px; }
	}

	@media (forced-colors: active) {
		// The tab underline is a box-shadow, which forced colours drop.
		&__views a.current { border-block-end: 2px solid Highlight; }
	}
}

// Status and module badges; each colour pair passes 4.5:1 at 12px.
.cf7hs-ui__badge {
	display: inline-flex;
	align-items: center;
	min-height: 22px;
	padding: 0 8px;
	border-radius: 2px;
	background: #f0f0f0;
	color: #1e1e1e;
	font-size: 12px;
	white-space: nowrap;

	&.is-active {
		background: #eff9f1;
		color: #005c12;
	}

	&.is-paused,
	&.is-retried {
		background: #f0f0f0;
		color: #50575e;
	}

	&.is-unset,
	&.is-blocked,
	&.is-warning {
		background: #fef8ee;
		color: #614200;
	}

	// A failed log entry, in the error colours.
	&.is-failed {
		background: #fcf0f1;
		color: #8a2424;
	}
}
