/**
 * Campaign Link Builder form: the front end and the block editor's preview.
 *
 * The form's own look (colours, borders, spacing, the shadow) applies to
 * forms in "the form's own look", the starting look of every form since
 * version 1. A block set to "the theme's look" (class
 * wp-cltb-container--theme-look) gets none of it: only the layout below and
 * what is set in the block's Styles tab, so the theme's text, fields and
 * buttons show through.
 *
 * The own-look selectors are wrapped in :where(), so they are exactly as
 * specific as before 2.0.0 and a theme's rules win over them as they did.
 *
 * Custom properties come in two layers, the block's own winning:
 *   --wp-cltb-own-*   set by one block in its Styles tab (printed by PHP
 *                     on the form's id)
 *   --wp-cltb-site-*  a theme may set these once, for every form on the site
 */

/*
 * The own-look rules come last; some of their selectors are less specific
 * than selectors above them.
 */

/* Layout: every form. */

/*
 * The container's width includes its padding and border, so the form is as
 * wide as the page's other blocks (the theme's content width), in the editor
 * and on the page alike. In the editor the block's own wrapper is what the
 * page's layout sizes, and the form fills it; before 25 November 2026 the
 * page added the form's padding and border to that width.
 */
.wp-cltb-container {
	box-sizing: border-box;
	margin: 0 auto;
}

.wp-cltb-field-group {
	margin-bottom: var(--wp-cltb-own-field-gap, var(--wp-cltb-site-field-gap, 15px));
}

.wp-cltb-form label {
	display: block;
}

.wp-cltb-form input[type="text"],
.wp-cltb-form input[type="url"] {
	width: 100%;
}

.wp-cltb-actions {
	display: flex;
	flex-wrap: wrap;
	gap: 10px;
}

.wp-cltb-url-container {
	display: flex;
	gap: 10px;
}

.wp-cltb-url-container input {
	flex: 1;
	min-width: 0;
}

.wp-cltb-copy {
	white-space: nowrap;
}

/*
 * The message under a field that does not pass (added by frontend.js): an
 * icon and words, never colour alone. Every look shows it. Its colours come
 * from "Messages and validation" (--wp-cltb-own-error-text-color and
 * --wp-cltb-own-error-icon-color); the form's own look prints its error
 * colour for it on the form's id, which wins over this. In the theme's look
 * with nothing set, the theme's red preset or WordPress's error red is used.
 * The icon follows the words' colour unless its own is set.
 */

.wp-cltb-error-text {
	display: flex;
	gap: 0.4em;
	align-items: flex-start;
	margin: 0.4em 0 0;
	font-size: 0.875rem;
	line-height: 1.4;
	color: var(--wp-cltb-own-error-text-color, var(--wp-cltb-site-error-text-color, var(--wp--preset--color--vivid-red, #b32d2e)));
}

.wp-cltb-error-text[hidden] {
	display: none;
}

.wp-cltb-error-text svg {
	flex: none;
	width: 1.15em;
	height: 1.15em;
	margin-top: 0.08em;
	color: var(--wp-cltb-own-error-icon-color, var(--wp-cltb-site-error-icon-color, currentcolor));
}

/* Read by screen readers, not shown: the "Error: " before a message. */
.wp-cltb-visually-hidden {
	position: absolute;
	width: 1px;
	height: 1px;
	margin: -1px;
	padding: 0;
	overflow: hidden;
	clip: rect(0, 0, 0, 0);
	white-space: nowrap;
	border: 0;
}

/* Button alignment, from the block's Styles tab. */

.wp-cltb-container--buttons-center .wp-cltb-actions {
	justify-content: center;
}

.wp-cltb-container--buttons-end .wp-cltb-actions {
	justify-content: flex-end;
}

.wp-cltb-container--buttons-stretch .wp-cltb-actions > .wp-cltb-button {
	flex: 1 1 0;
}

/*
 * Two columns of fields, from the block's Styles tab. The form measures its
 * own width (a container query), so in a narrow column it stays at one.
 */

.wp-cltb-container--two-columns {
	container: wp-cltb-form / inline-size;
}

@container wp-cltb-form (min-width: 36rem) {

	.wp-cltb-container--two-columns .wp-cltb-form {
		display: grid;
		grid-template-columns: repeat(2, minmax(0, 1fr));
		column-gap: var(--wp-cltb-own-field-gap, var(--wp-cltb-site-field-gap, 15px));
		align-items: start;
	}

	.wp-cltb-container--two-columns .wp-cltb-actions {
		grid-column: 1 / -1;
	}

	/*
	 * The fields beside each other line up: labels with labels, inputs with
	 * inputs, whatever the length of their help texts or labels. Each field
	 * spans four rows of the form's grid (label, help text, input, the
	 * message under a field that does not pass), shared with the field
	 * beside it through subgrid, so each row is as tall as the taller of the
	 * two. A field without help text leaves its help text row empty.
	 */
	@supports (grid-template-rows: subgrid) {

		.wp-cltb-container--two-columns .wp-cltb-form > :is(.wp-cltb-field-group, .wp-cltb-term-field, .wp-cltb-content-field) {
			display: grid;
			grid-template-rows: subgrid;
			grid-row: span 4;
		}

		.wp-cltb-container--two-columns .wp-cltb-form > :is(.wp-cltb-field-group, .wp-cltb-term-field, .wp-cltb-content-field) > label {
			grid-row: 1;
		}

		.wp-cltb-container--two-columns .wp-cltb-form > :is(.wp-cltb-field-group, .wp-cltb-term-field, .wp-cltb-content-field) > .wp-cltb-help-text {
			grid-row: 2;
		}

		.wp-cltb-container--two-columns .wp-cltb-form > :is(.wp-cltb-field-group, .wp-cltb-term-field, .wp-cltb-content-field) > input {
			grid-row: 3;
		}

		.wp-cltb-container--two-columns .wp-cltb-form > :is(.wp-cltb-field-group, .wp-cltb-term-field, .wp-cltb-content-field) > .wp-cltb-error-text {
			grid-row: 4;
		}
	}

	/*
	 * Without subgrid (older browsers; with container queries, only Chrome and
	 * Edge 105 to 116, which have :has()): each field is a column as tall as
	 * its row, label at the top, the input at the bottom, so the inputs of a
	 * row still line up. A message under a field that does not pass is shown
	 * above its input there, taking the free space above itself, so it does
	 * not push its input out of line.
	 */
	@supports not (grid-template-rows: subgrid) {

		.wp-cltb-container--two-columns .wp-cltb-form > :is(.wp-cltb-field-group, .wp-cltb-term-field, .wp-cltb-content-field) {
			display: flex;
			flex-direction: column;
			align-self: stretch;
		}

		.wp-cltb-container--two-columns .wp-cltb-form > :is(.wp-cltb-field-group, .wp-cltb-term-field, .wp-cltb-content-field) > label {
			order: 1;
		}

		.wp-cltb-container--two-columns .wp-cltb-form > :is(.wp-cltb-field-group, .wp-cltb-term-field, .wp-cltb-content-field) > .wp-cltb-help-text {
			order: 2;
		}

		.wp-cltb-container--two-columns .wp-cltb-form > :is(.wp-cltb-field-group, .wp-cltb-term-field, .wp-cltb-content-field) > .wp-cltb-error-text {
			order: 3;
			margin-top: auto;
		}

		.wp-cltb-container--two-columns .wp-cltb-form > :is(.wp-cltb-field-group, .wp-cltb-term-field, .wp-cltb-content-field) > input {
			order: 4;
			margin-top: auto;
		}

		.wp-cltb-container--two-columns .wp-cltb-form > :is(.wp-cltb-field-group, .wp-cltb-term-field, .wp-cltb-content-field):has(> .wp-cltb-error-text) > input {
			margin-top: 0;
		}
	}
}

/*
 * The theme's look: the theme decides the width, as for any other block. The
 * rule has no weight (:where()), so the page's layout (the theme's content
 * or wide width) wins over it, as it does for the block's editor wrapper;
 * the block's own "Largest width" is printed on the form's id.
 */

:where(.wp-cltb-container.wp-cltb-container--theme-look) {
	max-width: var(--wp-cltb-site-max-width, none);
}

.wp-cltb-container.wp-cltb-container--theme-look {
	padding: 0;
}

/* The form's own look (as before 2.0.0). */

/*
 * The form's own look keeps to 800px unless the block is set to wide or full
 * width (then the page's layout decides, as in the editor).
 */
.wp-cltb-container:where(:not(.wp-cltb-container--theme-look):not(.alignwide):not(.alignfull)) {
	max-width: var(--wp-cltb-own-max-width, var(--wp-cltb-site-max-width, 800px));
}

.wp-cltb-container:where(:not(.wp-cltb-container--theme-look)) {
	padding: 20px;
	background-color: #f9f9f9;
	border-radius: 5px;
	box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

:where(.wp-cltb-container:not(.wp-cltb-container--theme-look)) .wp-cltb-form {
	margin-bottom: 20px;
}

:where(.wp-cltb-container:not(.wp-cltb-container--theme-look)) .wp-cltb-field-group label {
	margin-bottom: 5px;
	font-weight: 600;
}

:where(.wp-cltb-container:not(.wp-cltb-container--theme-look)) .wp-cltb-field-group input {
	width: 100%;
	padding: 8px 12px;
	border: 1px solid #ddd;
	border-radius: 4px;
	font-size: 14px;
}

:where(.wp-cltb-container:not(.wp-cltb-container--theme-look)) .wp-cltb-field-group input.wp-cltb-error {
	border-color: #dc3232;
	background-color: #fef7f7;
}

:where(.wp-cltb-container:not(.wp-cltb-container--theme-look)) .wp-cltb-help-text {
	margin-top: 5px;
	font-size: 12px;
	color: #666;
}

:where(.wp-cltb-container:not(.wp-cltb-container--theme-look)) .wp-cltb-actions {
	margin-top: 20px;
}

:where(.wp-cltb-container:not(.wp-cltb-container--theme-look)) .wp-cltb-button {
	padding: 8px 16px;
	border: 1px solid #ddd;
	border-radius: 4px;
	background-color: #f7f7f7;
	cursor: pointer;
	font-size: 14px;
	transition: all 0.2s ease;
}

:where(.wp-cltb-container:not(.wp-cltb-container--theme-look)) .wp-cltb-button:hover {
	background-color: #f0f0f0;
}

:where(.wp-cltb-container:not(.wp-cltb-container--theme-look)) .wp-cltb-primary {
	background-color: #2271b1;
	border-color: #2271b1;
	color: #fff;
}

:where(.wp-cltb-container:not(.wp-cltb-container--theme-look)) .wp-cltb-primary:hover {
	background-color: #135e96;
	border-color: #135e96;
}

:where(.wp-cltb-container:not(.wp-cltb-container--theme-look)) .wp-cltb-result {
	margin-top: 30px;
	padding: 15px;
	background-color: #fff;
	border: 1px solid #ddd;
	border-radius: 4px;
}

:where(.wp-cltb-container:not(.wp-cltb-container--theme-look)) .wp-cltb-url-container input {
	padding: 8px 12px;
	border: 1px solid #ddd;
	border-radius: 4px;
	font-size: 14px;
	background-color: #f9f9f9;
}

:where(.wp-cltb-container:not(.wp-cltb-container--theme-look)) .wp-cltb-copy-message {
	margin-top: 10px;
	padding: 5px 10px;
	background-color: #edfaef;
	border: 1px solid #c3e6cb;
	border-radius: 4px;
	color: #155724;
	font-size: 14px;
	text-align: center;
}

.required {
	color: #dc3232;
}

/*
 * Fields and the finished link are full width including their padding and
 * border, so they never run past their column (or the form's edge). Some
 * classic themes (Twenty Twenty-One) give inputs a side margin, which would
 * push a full-width field past its column, so the sides are zero.
 */
.wp-cltb-form input[type="text"],
.wp-cltb-form input[type="url"],
.wp-cltb-result input {
	box-sizing: border-box;
	max-width: 100%;
	margin-left: 0;
	margin-right: 0;
}
