/**
 * Reason design system for Gravity Forms.
 *
 * Applies only inside .rcom-ds-form, the class the module's Theme Layer adds
 * to a form's wrapper when that form's "Use the Reason design system" toggle
 * (Form Settings → Form Layout) is on.
 * Plain CSS: no build step, no !important.
 *
 * Spec: docs/plans/gravity-forms-design-system/design.md
 *
 * Sections: 1. Tokens  2. Orbital map  3. Rules
 */

/* 1. Tokens ----------------------------------------------------------------
 * Design-system values, named after the Figma variables (reason-tokens slug
 * rule) with an rcom-ds- prefix. Each colour reads the theme token of the
 * same Figma name, --wp--preset--color--<slug> (common/_design-tokens.scss,
 * docs/plans/design-tokens.md), at runtime, so the theme's dark mode reaches
 * the form; the fallback is the Figma value, for a page without the theme's
 * tokens.
 * Fluid sizes use the reason-tokens formula: Mobile value at 440px, Desktop
 * value at 1440px, extrapolated to the ceiling (2000px type, 2500px space).
 * They're px, not rem: common/_global.scss:1-11 sets
 * `html, body { font-size: $base-font-size * $scale-ratio }` (20px) above
 * the md breakpoint and 16px below it, so a rem-based clamp here would read
 * this module's own root font-size, not the browser default, and come out
 * ~25% oversized on desktop above that breakpoint. px keeps the curve this
 * module was given regardless of that rule.
 */
.rcom-ds-form {
	--rcom-ds-text-primary: var(--wp--preset--color--text-primary, #1a1a1a);
	--rcom-ds-text-secondary: var(--wp--preset--color--text-secondary, #666666);
	--rcom-ds-text-tertiary: var(--wp--preset--color--text-tertiary, #757575);
	--rcom-ds-text-inverse: var(--wp--preset--color--text-inverse, #ffffff);
	--rcom-ds-brand-primary: var(--wp--preset--color--brand-primary, #f36b34);
	--rcom-ds-brand-secondary: var(--wp--preset--color--brand-secondary, #e64c0e);
	--rcom-ds-brand-action: var(--wp--preset--color--brand-action, #f36b34);
	--rcom-ds-surface-card: var(--wp--preset--color--surface-card, #fefefe);
	--rcom-ds-border-strong: var(--wp--preset--color--border-strong, #a1a1a1);
	--rcom-ds-border-subtle: var(--wp--preset--color--border-subtle, #e0e0e0);
	--rcom-ds-state-error: var(--wp--preset--color--state-error, #dc3e24);
	--rcom-ds-state-success: var(--wp--preset--color--state-success, #1a8419);

	--rcom-ds-radius-sm: var(--wp--custom--radius--sm, 4px);
	--rcom-ds-radius-checkbox: 2px;
	--rcom-ds-border-width-default: var(--wp--custom--border--width-default, 2px);
	--rcom-ds-border-width-heavy: var(--wp--custom--border--width-heavy, 3px);
	--rcom-ds-space-2xs: var(--wp--preset--spacing--2xs, 8px);
	--rcom-ds-space-xs: var(--wp--preset--spacing--xs, 12px);
	--rcom-ds-space-s: var(--wp--preset--spacing--s, 16px);
	--rcom-ds-space-m: var(--wp--preset--spacing--m, clamp(20px, 18.24px + 0.4vw, 28.24px)); /* 20 → 24 */

	/* Franklin Gothic until the site licenses Helvetica Neue and loads Newsreader. */
	--rcom-ds-font-family-ui: var(--wp--preset--font-family--heading, "franklin-gothic-urw", Arial, Gadget);
	--rcom-ds-font-family-body: var(--wp--preset--font-family--body, "franklin-gothic-urw", Arial, Gadget);
	/* Figma families. To switch, replace the two lines above with these (and load the fonts):
	--rcom-ds-font-family-ui: "Helvetica Neue", Helvetica, Arial, sans-serif;
	--rcom-ds-font-family-body: "Newsreader", Georgia, serif;
	*/
	--rcom-ds-weight-regular: var(--wp--custom--font-weight--regular, 400);
	--rcom-ds-weight-medium: var(--wp--custom--font-weight--medium, 500);
	--rcom-ds-size-meta: var(--wp--preset--font-size--meta, clamp(11px, 10.12px + 0.2vw, 14.12px)); /* 11 → 13 */
	--rcom-ds-size-body-sm: var(--wp--preset--font-size--body-sm, clamp(12px, 11.12px + 0.2vw, 15.12px)); /* 12 → 14 */
	--rcom-ds-size-body: var(--wp--preset--font-size--body, clamp(14px, 13.12px + 0.2vw, 17.12px)); /* 14 → 16 */
	--rcom-ds-size-ui: var(--wp--preset--font-size--ui, clamp(14px, 13.12px + 0.2vw, 17.12px)); /* 14 → 16 */
	--rcom-ds-size-h5: var(--wp--preset--font-size--h5, clamp(17px, 16.56px + 0.1vw, 18.56px)); /* 17 → 18 */
	--rcom-ds-size-h4: var(--wp--preset--font-size--h4, clamp(18px, 17.12px + 0.2vw, 21.12px)); /* 18 → 20 */
}

/* 2. Orbital map -------------------------------------------------------------
 * Points Orbital's --gf-* properties at the tokens. Each name here is a Figma
 * variable in the "Gravity Forms components" file (gf/ctrl/border-color →
 * --gf-ctrl-border-color), except the -rgb triples, which are derived.
 *
 * :not(#_) matches every element but counts as an ID, so this beats Gravity
 * Forms' per-form inline block (#gform_wrapper_N[data-form-index].gform-theme).
 *
 * -rgb triples are literal: Orbital needs "r, g, b", which CSS cannot derive
 * from var() at runtime. They do not follow the theme's dark mode.
 */
.gform_wrapper.gform-theme.rcom-ds-form:not(#_) {
	--gf-color-primary: var(--rcom-ds-brand-primary);
	--gf-color-primary-rgb: 243, 107, 52; /* brand/primary #f36b34 */
	--gf-color-primary-contrast: var(--rcom-ds-text-inverse);
	--gf-color-primary-contrast-rgb: 255, 255, 255; /* text/inverse */
	--gf-color-primary-darker: var(--rcom-ds-brand-secondary);
	--gf-color-primary-lighter: var(--rcom-ds-brand-primary);
	--gf-color-danger: var(--rcom-ds-state-error);
	--gf-color-success: var(--rcom-ds-state-success);
	--gf-color-in-ctrl: var(--rcom-ds-surface-card);
	--gf-color-in-ctrl-rgb: 254, 254, 254; /* surface/card */
	--gf-color-in-ctrl-contrast: var(--rcom-ds-text-primary);
	--gf-color-in-ctrl-contrast-rgb: 26, 26, 26; /* text/primary */
	--gf-color-in-ctrl-primary: var(--rcom-ds-brand-primary);
	--gf-color-in-ctrl-primary-rgb: 243, 107, 52; /* brand/primary */
	--gf-color-in-ctrl-primary-contrast: var(--rcom-ds-text-inverse);
	--gf-color-in-ctrl-primary-darker: var(--rcom-ds-brand-secondary); /* else Gravity Forms' blue from the per-form inline block: "Select files" hover/focus */
	--gf-color-in-ctrl-primary-lighter: var(--rcom-ds-brand-primary);
	--gf-color-out-ctrl-dark: var(--rcom-ds-text-secondary);
	--gf-color-out-ctrl-dark-rgb: 102, 102, 102; /* text/secondary */
	--gf-color-out-ctrl-dark-darker: var(--rcom-ds-text-primary);
	--gf-color-out-ctrl-light-darker: var(--rcom-ds-border-subtle);
	--gf-radius: var(--rcom-ds-radius-sm);
	/* Orbital's base family is `initial` (no value: text inherits the page's
	 * font). Primary/secondary/tertiary, progress bar, password strength, file
	 * upload and the validation summary all fall back to it. */
	--gf-font-family-base: var(--rcom-ds-font-family-ui);
	--gf-form-gap-y: var(--rcom-ds-space-m);
	--gf-form-validation-heading-font-size: var(--rcom-ds-size-body-sm); /* error summary heading: Body/Small */

	/* Controls — Figma Text Input / Select */
	--gf-ctrl-bg-color: var(--rcom-ds-surface-card);
	--gf-ctrl-bg-color-disabled: var(--rcom-ds-surface-card);
	--gf-ctrl-border-color: var(--rcom-ds-border-strong);
	--gf-ctrl-border-color-hover: var(--rcom-ds-border-strong);
	--gf-ctrl-border-color-focus: var(--rcom-ds-brand-primary);
	--gf-ctrl-border-color-error: var(--rcom-ds-state-error);
	--gf-ctrl-border-color-disabled: var(--rcom-ds-border-subtle);
	--gf-ctrl-border-width: var(--rcom-ds-border-width-default);
	--gf-ctrl-radius: var(--rcom-ds-radius-sm);
	--gf-ctrl-shadow: none;
	--gf-ctrl-outline-width-focus: 0; /* Figma focus is a thicker border, not a ring: see Rules */
	--gf-ctrl-color: var(--rcom-ds-text-primary);
	--gf-ctrl-color-disabled: var(--rcom-ds-text-tertiary);
	--gf-ctrl-placeholder-color: var(--rcom-ds-text-tertiary);
	--gf-ctrl-size: 50px;
	--gf-ctrl-padding-x: var(--rcom-ds-space-s);
	--gf-ctrl-font-family: var(--rcom-ds-font-family-body);
	--gf-ctrl-font-size: var(--rcom-ds-size-body);
	--gf-ctrl-font-weight: var(--rcom-ds-weight-regular);

	/* Labels — primary: field label (Meta); secondary: choice label (UI/Nav); tertiary: sub-label (Body/Small) */
	--gf-ctrl-label-color-primary: var(--rcom-ds-text-secondary);
	--gf-ctrl-label-font-family-primary: var(--rcom-ds-font-family-ui);
	--gf-ctrl-label-font-size-primary: var(--rcom-ds-size-meta);
	--gf-ctrl-label-font-weight-primary: var(--rcom-ds-weight-regular);
	--gf-ctrl-label-line-height-primary: 1.4;
	--gf-ctrl-label-color-secondary: var(--rcom-ds-text-primary);
	--gf-ctrl-label-font-family-secondary: var(--rcom-ds-font-family-ui);
	--gf-ctrl-label-font-size-secondary: var(--rcom-ds-size-ui);
	--gf-ctrl-label-font-weight-secondary: var(--rcom-ds-weight-regular);
	--gf-ctrl-label-line-height-secondary: 1.4;
	--gf-ctrl-label-color-tertiary: var(--rcom-ds-text-secondary);
	--gf-ctrl-label-font-family-tertiary: var(--rcom-ds-font-family-body);
	--gf-ctrl-label-font-size-tertiary: var(--rcom-ds-size-body-sm);
	--gf-ctrl-label-color-req: var(--rcom-ds-state-error);
	--gf-ctrl-label-font-size-req: var(--rcom-ds-size-meta);
	--gf-ctrl-label-font-weight-req: var(--rcom-ds-weight-regular);

	/* Descriptions (Body/Small) and field error messages (Meta, state/error) */
	--gf-ctrl-desc-color: var(--rcom-ds-text-secondary);
	--gf-ctrl-desc-font-family: var(--rcom-ds-font-family-ui);
	--gf-ctrl-desc-font-size: var(--rcom-ds-size-body-sm);
	--gf-ctrl-desc-line-height: 1.5;
	--gf-ctrl-desc-color-error: var(--rcom-ds-state-error);
	--gf-ctrl-desc-font-size-error: var(--rcom-ds-size-meta);
	--gf-ctrl-desc-line-height-error: 1.4;

	/* Choices — Figma Checkbox */
	--gf-ctrl-choice-size: 20px;
	--gf-ctrl-choice-check-color: var(--rcom-ds-text-inverse);

	/* Buttons — Figma Button. Primary: Submit / Next. Secondary: Previous. */
	--gf-ctrl-btn-radius: var(--rcom-ds-radius-sm);
	--gf-ctrl-btn-shadow: none;
	--gf-ctrl-btn-size: 47px;
	--gf-ctrl-btn-padding-x: 24px;
	--gf-ctrl-btn-font-family: var(--rcom-ds-font-family-ui);
	--gf-ctrl-btn-font-size: var(--rcom-ds-size-h5);
	--gf-ctrl-btn-font-weight: var(--rcom-ds-weight-medium);
	--gf-ctrl-btn-line-height: 1.3;
	--gf-ctrl-btn-bg-color-primary: var(--rcom-ds-brand-action);
	--gf-ctrl-btn-bg-color-hover-primary: var(--rcom-ds-brand-secondary);
	--gf-ctrl-btn-color-primary: var(--rcom-ds-text-inverse);
	--gf-ctrl-btn-border-color-focus-primary: var(--rcom-ds-text-primary);
	--gf-ctrl-btn-bg-color-secondary: transparent;
	--gf-ctrl-btn-bg-color-hover-secondary: transparent;
	--gf-ctrl-btn-border-color-secondary: var(--rcom-ds-text-primary);
	--gf-ctrl-btn-border-width-secondary: var(--rcom-ds-border-width-default);
	--gf-ctrl-btn-color-secondary: var(--rcom-ds-text-primary);

	/* Section and product fields */
	--gf-field-section-border-color: var(--rcom-ds-border-subtle);
	--gf-field-prod-price-color: var(--rcom-ds-text-primary);
}

/* 3. Rules -------------------------------------------------------------------
 * What the custom properties cannot express.
 */

/* Focus: Figma draws focus as a 3px brand/primary border (not a ring). Orbital
 * has no variable for the focus border width. padding-inline-start drops by
 * the extra 1px so the text's start edge does not move. The end/right side is
 * left alone here: for a single select, Orbital gives it extra room
 * (--gf-ctrl-select-padding-x) for the dropdown chevron, and shrinking it by
 * the same flat amount as the start side would eat into that room and run
 * the text under the chevron — so the select rule below compensates for the
 * border growth on that side instead, using the select's own padding math. */
.gform-theme--framework.rcom-ds-form :is(input:not([type="checkbox"], [type="radio"], [type="submit"], [type="button"], [type="hidden"]), select, textarea):focus {
	border-width: var(--rcom-ds-border-width-heavy);
	padding-inline-start: calc(var(--rcom-ds-space-s) - 1px);
}

/* Single select's right padding reserves room for the chevron
 * (--gf-icon-font-size, 20px) beyond the base padding; keep that room while
 * still dropping 1px for the heavier focus border. */
.gform-theme--framework.rcom-ds-form select:not([multiple]):focus {
	padding-inline-end: calc(var(--gf-icon-font-size) + var(--rcom-ds-space-s) - 1px);
}

/* Keyboard focus on buttons: a visible ring, since the fill already carries color. */
.gform-theme--framework.rcom-ds-form :is(button, [type="submit"], [type="button"]):focus-visible {
	outline: var(--rcom-ds-border-width-heavy) solid var(--rcom-ds-brand-primary);
	outline-offset: 2px;
}

/* Label 8px above the field; error message 8px below (Figma Text Input).
 * The label margin reads --gf-label-space-primary and the message's top margin
 * --gf-desc-space (both 8px, = space-2xs), the variables the Figma components
 * bind, so those levers are real. Below a fieldset's legend the two margins do
 * not collapse, which is why fieldsets show 16px there. */
.gform-theme--framework.rcom-ds-form .gfield_label {
	margin-block-end: var(--gf-label-space-primary);
}

.gform-theme--framework.rcom-ds-form .gfield_validation_message {
	margin-block-start: var(--gf-desc-space);
}

/* Checkbox: 2px radius; checked = brand/primary fill and border (the check mark
 * itself is --gf-ctrl-choice-check-color, text/inverse). Checked colours read
 * --gf-color-in-ctrl-primary (mapped to brand/primary above), the variable the
 * Figma components bind. */
.gform-theme--framework.rcom-ds-form input[type="checkbox"] {
	border-radius: var(--rcom-ds-radius-checkbox);
}

.gform-theme--framework.rcom-ds-form input[type="checkbox"]:checked {
	background-color: var(--gf-color-in-ctrl-primary);
	border-color: var(--gf-color-in-ctrl-primary);
}

/* Radio: no Figma component yet — Checkbox geometry, round, brand/primary dot.
 * The dot is 8px, not Orbital's 7px: the inner box is 16px (20px − two 2px
 * borders), and 16 − 7 leaves an odd 9px gap the browser can't split evenly,
 * which puts the dot half a pixel off centre. 16 − 8 = 4px on every side. */
.gform-theme--framework.rcom-ds-form input[type="radio"] {
	--gf-ctrl-radio-check-size: 8px;
	border-radius: 50%;
}

.gform-theme--framework.rcom-ds-form input[type="radio"]:checked {
	border-color: var(--gf-color-in-ctrl-primary);
}

.gform-theme--framework.rcom-ds-form input[type="radio"]:checked::before {
	background-color: var(--gf-color-in-ctrl-primary);
}

/* Choice style: Pills. A radio field opts in either way: the field's
 * Appearance → "Choice style" set to Pills (rcomChoiceStyle), or the legacy
 * radio-pills CSS class. Either way the field gets rcom-ds-choice--pills
 * (gform_field_css_class), on Radio Buttons fields and on Product / Option
 * fields set to Radio Buttons. Every rule here is scoped under .rcom-ds-form,
 * so pills follow the form's design-system toggle: with it off, the class is
 * inert and radio-pills fields keep the theme's own pill look
 * (components/_gravity-forms.scss).
 *
 * Beats Orbital's choice rules by class count, no IDs: Orbital's list and
 * .gchoice rules are 0,3,0 (.gform-theme--framework .gfield--type-choice …),
 * its label rule 0,2,0 (.gform-theme--framework .gform-field-label), and its
 * input rule 0,2,1; each rule below carries .gform-theme--framework,
 * .rcom-ds-form and .rcom-ds-choice--pills plus its own target. */
.gform-theme--framework.rcom-ds-form .rcom-ds-choice--pills .gfield_radio {
	flex-direction: row;
	flex-wrap: wrap;
	gap: var(--rcom-ds-space-2xs);
}

.gform-theme--framework.rcom-ds-form .rcom-ds-choice--pills .gchoice {
	display: block; /* Orbital's inline-grid sizes its first column to
	max-content and takes the hidden input out of flow, which stops a long
	pill label from wrapping; block layout lets the label size itself. */
	gap: 0;
	margin: 0;
	position: relative; /* Anchors the visually hidden input. */
}

/* Visually hidden but still focusable and checkable: never display:none, or
 * keyboard and screen-reader users lose the control. */
.gform-theme--framework.rcom-ds-form .rcom-ds-choice--pills .gchoice > input[type="radio"] {
	position: absolute;
	inline-size: 1px;
	block-size: 1px;
	opacity: 0;
	margin: 0;
	clip-path: inset(50%);
	overflow: hidden;
}

.gform-theme--framework.rcom-ds-form .rcom-ds-choice--pills .gchoice > label {
	display: inline-flex;
	align-items: center;
	padding: 8px var(--gf-ctrl-padding-x);
	border: var(--rcom-ds-border-width-default) solid var(--gf-color-primary);
	border-radius: var(--gf-radius);
	background: var(--gf-ctrl-bg-color);
	color: var(--gf-ctrl-color);
	font-family: var(--rcom-ds-font-family-ui);
	font-size: var(--rcom-ds-size-ui);
	font-weight: var(--rcom-ds-weight-medium);
	line-height: 1.4;
	cursor: pointer;
	margin: 0;
	max-inline-size: 100%;
	white-space: normal;
}

.gform-theme--framework.rcom-ds-form .rcom-ds-choice--pills .gchoice > input:checked + label {
	background: var(--gf-color-primary);
	color: var(--gf-color-primary-contrast);
}

.gform-theme--framework.rcom-ds-form .rcom-ds-choice--pills .gchoice > input:focus-visible + label {
	outline: var(--rcom-ds-border-width-heavy) solid var(--gf-color-primary);
	outline-offset: 2px;
}

/* Section heading: Heading/H4 with a divider (border via --gf-field-section-*).
 * The colour reads --gf-color-out-ctrl-dark-darker (mapped to text/primary
 * above), the variable the Figma gsection component binds, so that lever is
 * real. */
.gform-theme--framework.rcom-ds-form .gsection_title {
	color: var(--gf-color-out-ctrl-dark-darker);
	font-family: var(--rcom-ds-font-family-ui);
	font-size: var(--rcom-ds-size-h4);
	font-weight: var(--rcom-ds-weight-medium);
	line-height: 1.3;
}

/* Product price: Body/Default; the colour reads --gf-field-prod-price-color,
 * the variable the Figma product component binds, so that lever is real.
 * The total follows --gf-ctrl-readonly-* (Orbital's reset-input rule). */
.gform-theme--framework.rcom-ds-form .ginput_product_price {
	color: var(--gf-field-prod-price-color);
	font-family: var(--rcom-ds-font-family-body);
	font-size: var(--rcom-ds-size-body);
}

/* Error summary at the top of the form. */
.gform-theme--framework.rcom-ds-form .gform_validation_errors {
	border-color: var(--rcom-ds-state-error);
	border-radius: var(--rcom-ds-radius-sm);
}

/* The heading colour and size read --gf-form-validation-heading-color
 * (state/error via --gf-color-danger in the map) and
 * --gf-form-validation-heading-font-size (Body/Small in the map), the variables
 * the Figma summary binds, so those levers are real. */
.gform-theme--framework.rcom-ds-form .gform_submission_error {
	color: var(--gf-form-validation-heading-color);
	font-family: var(--rcom-ds-font-family-ui);
	font-size: var(--gf-form-validation-heading-font-size);
}

/* PayPal's hosted card fields (number/expiration/CVC) render inside an
 * iframe, each wrapped in a .ginput_card_field span. Orbital's own focus
 * styling for that span (.gform-theme-field-control:focus, same mechanism as
 * a plain input) never fires: focus lands inside the iframe, not on the span
 * itself. :focus-within sees a focused descendant even across the iframe
 * boundary, so it picks up what :focus misses. */
.gform-theme--framework.rcom-ds-form .ginput_card_field:focus-within {
	border-color: var(--gf-ctrl-border-color-focus); /* brand/primary via the map; the Figma card-field focus binds it */
	border-width: var(--rcom-ds-border-width-heavy);
}
