/*
 * Surveys and Feedback: front-end forms.
 *
 * Fonts, colours, inputs and buttons come from the theme. Purely cosmetic rules are
 * wrapped in :where() so that any theme rule wins over them; only layout and the
 * rating controls use real specificity.
 *
 * To adjust from a theme or the Customizer's Additional CSS:
 *   .saf-form-wrap { --saf-accent: #0a7c66; --saf-star-size: 1.75em; }
 */

.saf-form-wrap {
	--saf-accent: var(--wp--preset--color--primary, currentColor);
	--saf-error: #d63638;
	--saf-gap: 1.5em;
	--saf-star-size: 2em;
}

.saf-form-wrap [hidden] {
	display: none !important;
}

/* Layout */

.saf-form {
	display: flex;
	flex-direction: column;
	gap: var(--saf-gap);
	margin: 0;
}

:where(.saf-form__title) {
	margin: 0;
}

:where(.saf-form__intro) > :first-child {
	margin-top: 0;
}

:where(.saf-form__intro) > :last-child {
	margin-bottom: 0;
}

/* Captions: the required note, help text, hints, counters and rating captions.
 * Their size and spacing are layout, so they use real specificity to stop a
 * theme's paragraph margins pushing them apart; colour and font family still come
 * from the theme. */

.saf-form .saf-form__required-note,
.saf-form .saf-help,
.saf-form .saf-counter,
.saf-form .saf-rating__ends {
	font-size: 0.875em;
	opacity: 0.8;
}

.saf-form .saf-form__required-note {
	margin: 0;
}

.saf-form .saf-help {
	margin: -0.15em 0 0.5em;
}

.saf-form .saf-counter {
	margin: 0.35em 0 0;
	font-variant-numeric: tabular-nums;
}

/* Fields. Every question's heading is a <label>, so the theme styles them all
 * the same way. */

.saf-field {
	min-width: 0;
	margin: 0;
	padding: 0;
	border: 0;
}

.saf-label {
	display: block;
	margin: 0 0 0.4em;
	padding: 0;
	font-weight: 600;
}

/* A rating's heading names the group; it is not clickable like a text label. */
.saf-field--rating > .saf-label {
	cursor: default;
}

.saf-input {
	display: block;
	box-sizing: border-box;
	width: 100%;
	max-width: 100%;
}

:where(.saf-input) {
	font: inherit;
}

textarea.saf-input {
	min-height: 7em;
	resize: vertical;
}

.saf-form .saf-counter.is-near-limit {
	font-weight: 600;
	opacity: 1;
}

/* Errors and notices */

.saf-form .saf-error {
	margin: 0.4em 0 0;
	padding-left: 0.6em;
	border-left: 3px solid var(--saf-error);
	font-weight: 600;
}

.saf-field.is-invalid .saf-input {
	border-color: var(--saf-error);
	box-shadow: 0 0 0 1px var(--saf-error);
}

.saf-notice,
.saf-success {
	padding: 0.85em 1.1em;
	border-left: 4px solid var(--saf-error);
	background: color-mix(in srgb, var(--saf-error) 8%, transparent);
}

.saf-success {
	border-left-color: var(--saf-accent);
	background: color-mix(in srgb, var(--saf-accent) 8%, transparent);
}

/* Focus is moved to these messages so screen readers announce them; they are not
 * controls, so they need no focus ring. */
.saf-notice:focus,
.saf-success:focus {
	outline: none;
}

.saf-success > :first-child {
	margin-top: 0;
}

.saf-success > :last-child {
	margin-bottom: 0;
}

/* Ratings */

/* Sized to the rating so the end captions sit under the first and last options. */
.saf-scale {
	display: flex;
	flex-direction: column;
	width: fit-content;
	max-width: 100%;
}

.saf-rating {
	position: relative;
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: 0.35em;
}

/* The radio buttons stay in the page for keyboard and screen-reader use but are
 * hidden visually. Many themes restyle every radio button, so this must win. */
.saf-rating .saf-rating__input {
	position: absolute !important;
	width: 1px !important;
	height: 1px !important;
	min-width: 0 !important;
	margin: 0 !important;
	padding: 0 !important;
	border: 0 !important;
	opacity: 0 !important;
	-webkit-appearance: none;
	appearance: none;
	pointer-events: none;
}

/* Two classes, to beat theme rules such as `input[type="radio"] + label`. */
.saf-rating .saf-rating__option {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	margin: 0;
	padding: 0;
	border-radius: 4px;
	font-size: inherit;
	line-height: 1;
	vertical-align: middle;
	cursor: pointer;
}

.saf-rating .saf-rating__option::before,
.saf-rating .saf-rating__option::after {
	content: none;
}

.saf-rating__input:focus-visible + .saf-rating__option {
	outline: 2px solid var(--saf-accent);
	outline-offset: 2px;
}

.saf-field.is-invalid .saf-rating__option {
	outline: 1px dashed var(--saf-error);
	outline-offset: 1px;
}

:where(.saf-rating__ends) {
	display: flex;
	justify-content: space-between;
	gap: 1em;
	margin-top: 0.4em;
}

/* Stars: outlined until chosen; stars up to the chosen (or hovered) one are filled. */

.saf-rating--stars {
	gap: 0.1em;
}

.saf-rating.saf-rating--stars .saf-rating__option {
	padding: 0.1em;
	color: var(--saf-accent);
}

.saf-star {
	display: block;
	width: var(--saf-star-size);
	height: var(--saf-star-size);
	fill: transparent;
	stroke: currentColor;
	stroke-width: 1.5;
	stroke-linejoin: round;
	transition: fill 0.1s ease, transform 0.1s ease;
}

:where(.saf-rating--stars:has(.saf-rating__input:checked)) .saf-star {
	fill: currentColor;
}

:where(.saf-rating--stars .saf-rating__input:checked ~ .saf-rating__input + .saf-rating__option) .saf-star {
	fill: transparent;
}

@media (hover: hover) {
	.saf-rating--stars:has(.saf-rating__option:hover) .saf-star {
		fill: currentColor;
	}

	.saf-rating--stars .saf-rating__option:hover ~ .saf-rating__option .saf-star {
		fill: transparent;
	}

	.saf-rating--stars .saf-rating__option:hover .saf-star {
		transform: scale(1.1);
	}
}

/* Numbers */

.saf-rating.saf-rating--numbers .saf-rating__option {
	box-sizing: border-box;
	min-width: 2.75em;
	height: 2.75em;
	padding: 0 0.5em;
	border: 1px solid color-mix(in srgb, currentColor 40%, transparent);
	font-weight: 600;
	font-variant-numeric: tabular-nums;
}

.saf-rating--numbers .saf-rating__option:hover {
	border-color: var(--saf-accent);
}

.saf-rating--numbers .saf-rating__input:checked + .saf-rating__option {
	border-color: var(--saf-accent);
	background: color-mix(in srgb, var(--saf-accent) 16%, transparent);
	box-shadow: inset 0 0 0 1px var(--saf-accent);
}

/* Multiple choice. Only layout: the radio buttons and checkboxes keep the
 * theme's own styling, like any other form on the site. */

.saf-choices {
	display: flex;
	flex-direction: column;
	gap: 0.5em;
}

.saf-choice {
	display: flex;
	align-items: center;
	gap: 0.6em;
}

.saf-choice .saf-choice__input {
	flex: none;
	margin: 0;
}

.saf-choice .saf-choice__label {
	margin: 0;
	cursor: pointer;
}

.saf-choice__input:disabled + .saf-choice__label {
	opacity: 0.55;
	cursor: not-allowed;
}

/* Security check */

.saf-captcha__challenge {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: 0.5em 1em;
	margin: 0 0 0.6em;
}

.saf-captcha__image {
	display: block;
	max-width: 100%;
	height: auto;
	border-radius: 4px;
}

:where(.saf-captcha__question) {
	font-size: 1.1em;
	font-weight: 600;
}

.saf-captcha__refresh {
	padding: 0;
	border: 0;
	background: none;
	color: inherit;
	font: inherit;
	font-size: 0.875em;
	text-decoration: underline;
	cursor: pointer;
}

.saf-captcha__answer {
	max-width: 12em;
	letter-spacing: 0.1em;
}

.saf-captcha__widget {
	min-height: 65px;
}

/* Submit */

:where(.saf-form__actions) {
	margin: 0;
}

.saf-form[aria-busy="true"] .saf-submit {
	opacity: 0.7;
	cursor: progress;
}

/* Hidden helpers */

.saf-hp {
	position: absolute !important;
	left: -10000px !important;
	width: 1px;
	height: 1px;
	overflow: hidden;
}

.saf-sr-only {
	position: absolute !important;
	width: 1px !important;
	height: 1px !important;
	margin: -1px !important;
	padding: 0 !important;
	overflow: hidden !important;
	clip-path: inset(50%) !important;
	white-space: nowrap !important;
	border: 0 !important;
}

.saf-admin-notice {
	padding: 0.75em 1em;
	border: 1px dashed currentColor;
	font-size: 0.875em;
}

@media (prefers-reduced-motion: reduce) {
	.saf-star {
		transition: none;
	}
}
