/* Global form controls: mock-up A, soft filled. All colours follow existing light/dark tokens.
   Field tokens: fill = tint, resting border = hairline (contrast), hover = darker, focus = accent, error = coral.
   For the outlined variant set --fc-field-fill: transparent (the border is already visible).
   Fields retain a 1px border; focus uses a 3px teal ring at 2px offset, errors use border + error fill.
   Check tokens: --fc-check-border = hairline; --fc-check-on = accent (teal).
   Switch: --fc-switch-off-fill = hairline. Drop: --fc-drop-border = hairline, --fc-drop-border-hover = accent.
   Label/layout tokens set colour, gaps, height, padding and font size; radius aliases the control radius.
   Error/disabled tokens alias semantic fill and ink; --fc-focus-ring aliases accent.
   Button tokens set border width, radius, height and each role's fill/ink/border.
   Primary is the teal-tint role; CTA is solid mustard and is meant once per screen.
   Secondary is outlined ink, quiet is underlined ink, danger is outlined coral.
   .fc-switch markup should use role="switch". .fc-search wraps a search input for its decorative icon.
   .fc-dropzone is an opt-in label wrapping a native file input; picked-file-name + remove control
   needs a tiny Stimulus controller, deliberately not built here. */
:root {
  --fc-field-fill: var(--tint);
  --fc-field-border: var(--hairline);
  --fc-field-border-hover: color-mix(in srgb, var(--hairline) 55%, var(--ink));
  --fc-field-border-focus: var(--accent);
  --fc-field-radius: var(--radius-control);
  --fc-field-height: 2.75rem;
  --fc-field-pad-x: 0.75rem;
  --fc-field-pad-y: 0.6rem;
  /* Keep at least 16px to avoid mobile focus zoom. */
  --fc-field-font-size: 1rem;
  --fc-label-color: var(--ink);
  --fc-label-gap: 0.35rem;
  --fc-row-gap: 0.75rem;
  --fc-focus-ring: var(--accent);
  --fc-check-border: var(--hairline);
  --fc-check-on: var(--accent);
  --fc-switch-off-fill: var(--hairline);
  --fc-drop-border: var(--hairline);
  --fc-drop-border-hover: var(--accent);
  --fc-error: var(--danger);
  --fc-error-fill: var(--danger-tint);
  --fc-disabled-fill: var(--disabled-surface);
  --fc-disabled-ink: var(--disabled-ink);
  --fc-btn-border-width: 1px;
  --fc-btn-radius: var(--radius-control);
  --fc-btn-height: 2.75rem;
  /* Primary keeps the app's established teal-tint look; only CTA is solid mustard (one per screen). */
  --fc-btn-primary-fill: var(--selected-fill);
  --fc-btn-primary-ink: var(--selected-ink);
  --fc-btn-primary-border: var(--selected-line);
  /* CTA: the one solid mustard action per screen. */
  --fc-btn-cta-fill: var(--primary);
  --fc-btn-cta-ink: var(--on-primary);
  --fc-btn-cta-border: var(--primary);
  --fc-btn-secondary-fill: var(--surface);
  --fc-btn-secondary-ink: var(--ink);
  --fc-btn-secondary-border: var(--ink);
  --fc-btn-quiet-fill: color-mix(in srgb, var(--surface) 0%, transparent);
  --fc-btn-quiet-ink: var(--ink);
  --fc-btn-quiet-border: color-mix(in srgb, var(--accent) 0%, transparent);
  --fc-btn-danger-fill: transparent;
  --fc-btn-danger-ink: var(--danger);
  --fc-btn-danger-border: var(--danger);
}

/* Text fields: zero specificity lets existing page components keep their overrides. */
:where(input:not([type]), input[type="text"], input[type="email"], input[type="password"],
  input[type="search"], input[type="number"], input[type="url"], input[type="tel"], input[type="date"],
  input[type="time"], input[type="datetime-local"], textarea, select, input[type="file"]) {
  box-sizing: border-box;
  min-width: 0;
  min-height: var(--fc-field-height);
  padding: var(--fc-field-pad-y) var(--fc-field-pad-x);
  border: 1px solid var(--fc-field-border);
  border-radius: var(--fc-field-radius);
  color: var(--ink);
  background-color: var(--fc-field-fill);
  font-family: var(--font-ui);
  font-size: max(16px, var(--fc-field-font-size));
  line-height: 1.4;
}

:where(textarea) {
  min-height: 6rem;
  resize: vertical;
}

:where(input, textarea)::placeholder {
  color: var(--muted);
  opacity: 1;
}

/* Single selects draw a token-coloured chevron; list boxes keep their native list UI. */
select:not([multiple]):not([size]) {
  appearance: none;
  padding-right: 2.5rem !important;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%) !important;
  background-position: right 1.25rem center, right 0.75rem center !important;
  background-size: 0.5rem 0.5rem !important;
  background-repeat: no-repeat !important;
}

:where(select[multiple], select[size]) {
  background-image: none;
}

/* Native filename and browser-provided button label preserve keyboard and accessible behaviour. */
input[type="file"],
.fc-dropzone {
  box-sizing: border-box;
  display: block;
  width: 100%;
  min-height: 7rem;
  padding: 1.25rem;
  border: 2px dashed var(--fc-drop-border);
  border-radius: var(--fc-field-radius);
  color: var(--muted);
  background-color: var(--fc-field-fill);
  text-align: center;
}

input[type="file"]::file-selector-button {
  display: block;
  min-height: var(--fc-btn-height);
  margin: 0 auto 0.75rem;
  padding: 0.5rem var(--fc-field-pad-x);
  border: var(--fc-btn-border-width) solid var(--fc-btn-secondary-border);
  border-radius: var(--fc-btn-radius);
  color: var(--fc-btn-secondary-ink);
  background: var(--fc-btn-secondary-fill);
  font: inherit;
  cursor: pointer;
}

input[type="file"]:enabled:is(:hover, :focus-visible)::file-selector-button {
  border-color: var(--accent);
  outline: 3px solid var(--fc-focus-ring);
  outline-offset: 2px;
  box-shadow: none;
}

input[type="file"]:disabled::file-selector-button {
  color: var(--fc-disabled-ink);
  background: var(--fc-disabled-fill);
  cursor: not-allowed;
}

/* The wrapper owns the dashed edge; its native input stays visible and focusable. */
.fc-dropzone input[type="file"] {
  min-height: var(--fc-field-height);
  padding: 0;
  border-color: transparent;
  background-color: transparent;
}

input[type="file"]:enabled:is(:hover, :focus-visible),
.fc-dropzone:has(input:enabled:is(:hover, :focus-visible)) {
  border-color: var(--fc-drop-border-hover);
}

input[type="file"]:is([aria-invalid="true"], .is-invalid, .is-invalid *, .field_with_errors *),
.fc-dropzone:has(input:is([aria-invalid="true"], .is-invalid, .is-invalid *, .field_with_errors *)) {
  border-color: var(--fc-error);
  background-color: var(--fc-error-fill);
}

input[type="file"]:disabled,
.fc-dropzone:has(input:disabled) {
  opacity: 0.55;
  cursor: not-allowed;
}

/* Checks and radios leave the existing bespoke form and appearance controls alone. */
:where(input:is([type="checkbox"], [type="radio"]):not(
  .settings-form *, .group-form *, .library-filters *,
  .member-nav__appearance *, .fc-switch)) {
  appearance: none;
  box-sizing: border-box;
  display: inline-grid;
  place-content: center;
  flex: none;
  width: 1.25rem;
  height: 1.25rem;
  min-height: 1.25rem;
  /* Together with the box, margins reserve a 44px row and horizontal target space. */
  margin: 0.75rem;
  padding: 0;
  vertical-align: middle;
  border: 1px solid var(--fc-check-border);
  border-radius: 0.25rem;
  color: var(--bg);
  background: var(--fc-field-fill);
  cursor: pointer;
}

:where(input:is([type="checkbox"], [type="radio"]):not(
  .settings-form *, .group-form *, .library-filters *,
  .member-nav__appearance *, .fc-switch)):where([type="radio"]) {
  border-radius: 50%;
}

:where(input:is([type="checkbox"], [type="radio"]):not(
  .settings-form *, .group-form *, .library-filters *,
  .member-nav__appearance *, .fc-switch)):where(:checked, :indeterminate) {
  border-color: var(--fc-check-on);
  background: var(--fc-check-on);
}

:where(input:is([type="checkbox"], [type="radio"]):not(
  .settings-form *, .group-form *, .library-filters *,
  .member-nav__appearance *, .fc-switch))::before {
  content: "";
  display: none;
}

:where(input:is([type="checkbox"], [type="radio"]):not(
  .settings-form *, .group-form *, .library-filters *,
  .member-nav__appearance *, .fc-switch)):where([type="checkbox"]:checked)::before {
  display: block;
  width: 0.3rem;
  height: 0.6rem;
  border: solid currentColor;
  border-width: 0 0.15rem 0.15rem 0;
  transform: translateY(-0.08rem) rotate(45deg);
}

:where(input:is([type="checkbox"], [type="radio"]):not(
  .settings-form *, .group-form *, .library-filters *,
  .member-nav__appearance *, .fc-switch)):where([type="checkbox"]:indeterminate)::before {
  display: block;
  width: 0.65rem;
  height: 0;
  border: 0;
  border-top: 0.15rem solid currentColor;
  transform: none;
}

:where(input:is([type="checkbox"], [type="radio"]):not(
  .settings-form *, .group-form *, .library-filters *,
  .member-nav__appearance *, .fc-switch)):where([type="radio"]:checked)::before {
  display: block;
  width: 0.5rem;
  height: 0.5rem;
  border-radius: 50%;
  background: currentColor;
}

@supports not (appearance: none) {
  :where(input[type="checkbox"], input[type="radio"]) {
    accent-color: var(--accent);
  }
}

/* Shared field states: keep an outline for keyboard focus, including invalid controls. */
:where(:is(input:not([type]), input[type="text"], input[type="email"], input[type="password"],
  input[type="search"], input[type="number"], input[type="url"], input[type="tel"], input[type="date"],
  input[type="time"], input[type="datetime-local"], textarea, select, input[type="file"],
  input[type="checkbox"], input[type="radio"]):hover:enabled) {
  border-color: var(--fc-field-border-hover);
}

:where(:is(input:not([type]), input[type="text"], input[type="email"], input[type="password"],
  input[type="search"], input[type="number"], input[type="url"], input[type="tel"], input[type="date"],
  input[type="time"], input[type="datetime-local"], textarea, select, input[type="file"],
  input[type="checkbox"], input[type="radio"]):focus-visible) {
  border-color: var(--fc-field-border-focus);
  outline: 3px solid var(--fc-focus-ring);
  outline-offset: 2px;
  box-shadow: none;
}

:where(:is(input:not([type]), input[type="text"], input[type="email"], input[type="password"],
  input[type="search"], input[type="number"], input[type="url"], input[type="tel"], input[type="date"],
  input[type="time"], input[type="datetime-local"], textarea, select, input[type="file"],
  input[type="checkbox"], input[type="radio"]):is(
  [aria-invalid="true"], .is-invalid, .is-invalid *, .field_with_errors *)) {
  border-color: var(--fc-error);
  background-color: var(--fc-error-fill);
}

:where(:is(input:not([type]), input[type="text"], input[type="email"], input[type="password"],
  input[type="search"], input[type="number"], input[type="url"], input[type="tel"], input[type="date"],
  input[type="time"], input[type="datetime-local"], textarea, select, input[type="file"],
  input[type="checkbox"], input[type="radio"])):disabled {
  color: var(--fc-disabled-ink);
  background-color: var(--fc-disabled-fill);
  border-color: var(--fc-field-border);
  opacity: 0.55;
  cursor: not-allowed;
}

:where(input:is([type="checkbox"], [type="radio"]):not(
  .settings-form *, .group-form *, .library-filters *, .member-nav__appearance *, .fc-switch)):disabled {
  border-color: var(--fc-check-border);
}

/* Every native control/button gets the same keyboard ring, independently of its error border. */
:is(input, select, textarea, button):focus-visible {
  outline: 3px solid var(--fc-focus-ring);
  outline-offset: 2px;
  box-shadow: none;
}

/* Explicit switch opt-in also works inside the otherwise excluded bespoke checkbox groups. */
input[type="checkbox"].fc-switch {
  appearance: none;
  box-sizing: border-box;
  display: inline-block;
  flex: none;
  width: 2.5rem;
  height: 1.5rem;
  min-height: 1.5rem;
  margin: 0.625rem;
  padding: 0;
  border: 1px solid var(--fc-check-border);
  border-radius: 999px;
  color: var(--bg);
  background: var(--fc-switch-off-fill);
  vertical-align: middle;
  cursor: pointer;
}

input[type="checkbox"].fc-switch::before {
  content: "";
  display: block;
  width: 1.125rem;
  height: 1.125rem;
  margin: calc(0.1875rem - 1px);
  border-radius: 50%;
  background: currentColor;
  transform: translateX(0);
  transition: transform 160ms ease;
}

input[type="checkbox"].fc-switch:checked {
  border-color: var(--fc-check-on);
  background: var(--fc-check-on);
}

input[type="checkbox"].fc-switch:checked::before {
  transform: translateX(1rem);
}

input[type="checkbox"].fc-switch:is([aria-invalid="true"], .is-invalid, .is-invalid *, .field_with_errors *) {
  border-color: var(--fc-error);
  background-color: var(--fc-error-fill);
}

input[type="checkbox"].fc-switch:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

@media (prefers-reduced-motion: reduce) {
  input[type="checkbox"].fc-switch::before {
    transition: none;
  }
}

/* Alpha-only SVG mask: its paint comes from --muted; the icon never receives pointer events. */
.fc-search {
  position: relative;
  display: block;
  min-width: 0;
}

.fc-search::before {
  content: "";
  position: absolute;
  top: calc(var(--fc-field-height) / 2);
  left: var(--fc-field-pad-x);
  width: 1rem;
  height: 1rem;
  transform: translateY(-50%);
  /* Decorative icon, drawn from the mask; not text, so no text/background contrast pairing. */
  color: currentColor;
  background-color: var(--muted);
  mask-image: url('data:image/svg+xml,\
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">\
<path fill="none" stroke="currentColor" stroke-width="2" d="M21 21l-6-6M17 10a7 7 0 1 1-14 0a7 7 0 0 1 14 0"/>\
</svg>');
  mask-size: contain;
  mask-repeat: no-repeat;
  pointer-events: none;
}

.fc-search input[type="search"] {
  width: 100%;
  padding-left: calc(var(--fc-field-pad-x) + 1.75rem);
}

/* Opt-in layout helpers; bare labels keep their page styling. */
.fc-field {
  display: grid;
  gap: var(--fc-label-gap);
  min-width: 0;
}

.fc-field label {
  color: var(--fc-label-color);
}

.fc-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--fc-row-gap);
  min-height: var(--fc-field-height);
}

.fc-row > label {
  display: inline-flex;
  min-height: var(--fc-field-height);
  align-items: center;
}

.fc-row > :where(input, select, textarea, .fc-field, .fc-search) {
  min-height: var(--fc-field-height);
  flex: 1 1 12rem;
  min-width: 0;
  max-width: 100%;
}

.fc-help {
  margin: 0;
  color: var(--muted);
  font-size: 0.875rem;
}

.fc-error-text {
  color: var(--fc-error);
}

.fc-fieldset {
  min-width: 0;
  margin: 0;
  padding: 1rem;
  border: 1px solid var(--fc-field-border);
  border-radius: var(--fc-field-radius);
}

.fc-fieldset > legend {
  padding-inline: var(--fc-label-gap);
  color: var(--fc-label-color);
  font-weight: 700;
}

/* Buttons require .button; native submit controls receive no button styling. */
.button {
  display: inline-flex;
  box-sizing: border-box;
  min-height: var(--fc-btn-height);
  align-items: center;
  justify-content: center;
  padding: 0.65rem 1rem;
  border: var(--fc-btn-border-width) solid var(--fc-btn-secondary-border);
  border-radius: var(--fc-btn-radius);
  box-shadow: none;
  font: inherit;
  font-weight: 700;
  text-decoration: none;
  cursor: pointer;
}

.button:where(:hover) {
  background: var(--tint);
}

.button.button--primary {
  border-color: var(--fc-btn-primary-border);
  color: var(--fc-btn-primary-ink);
  background: var(--fc-btn-primary-fill);
}

.button.button--primary:where(:hover:not([disabled]):not([aria-disabled="true"])) {
  background: var(--fc-btn-primary-fill);
}

.button.button--primary:where(:active:not([disabled]):not([aria-disabled="true"])) {
  background: color-mix(in srgb, var(--fc-btn-primary-fill) 88%, var(--fc-btn-primary-ink));
}

.button.button--cta {
  border-color: var(--fc-btn-cta-border);
  color: var(--fc-btn-cta-ink);
  background: var(--fc-btn-cta-fill);
}

.button.button--cta:where(:hover:not([disabled]):not([aria-disabled="true"])) {
  background: color-mix(in srgb, var(--fc-btn-cta-fill) 90%, var(--fc-btn-cta-ink));
}

.button.button--cta:where(:active:not([disabled]):not([aria-disabled="true"])) {
  background: color-mix(in srgb, var(--fc-btn-cta-fill) 88%, var(--fc-btn-cta-ink));
}

.button.button--secondary {
  border-color: var(--fc-btn-secondary-border);
  color: var(--fc-btn-secondary-ink);
  background: var(--fc-btn-secondary-fill);
}

.button.button--secondary:where(:hover:not([disabled]):not([aria-disabled="true"])) {
  background: var(--tint);
  outline: 3px solid var(--fc-focus-ring);
  outline-offset: 2px;
  box-shadow: none;
}

.button.button--secondary:where(:active:not([disabled]):not([aria-disabled="true"])) {
  background: color-mix(in srgb, var(--fc-btn-secondary-fill) 88%, var(--fc-btn-secondary-ink));
}

.button.button--quiet {
  border-color: var(--fc-btn-quiet-border);
  text-decoration: underline;
  color: var(--fc-btn-quiet-ink);
  background: var(--fc-btn-quiet-fill);
}

.button.button--quiet:where(:hover:not([disabled]):not([aria-disabled="true"])) {
  background: var(--fc-btn-quiet-fill);
  color: var(--accent);
}

.button.button--quiet:where(:active:not([disabled]):not([aria-disabled="true"])) {
  background: color-mix(in srgb, var(--fc-btn-quiet-fill) 88%, var(--fc-btn-quiet-ink));
}

.button.button--danger {
  border-color: var(--fc-btn-danger-border);
  color: var(--fc-btn-danger-ink);
  background: var(--fc-btn-danger-fill);
}

.button.button--danger:where(:hover:not([disabled]):not([aria-disabled="true"])) {
  background: var(--danger-tint);
}

.button.button--danger:where(:active:not([disabled]):not([aria-disabled="true"])) {
  background: color-mix(in srgb, var(--fc-btn-danger-fill) 88%, var(--fc-btn-danger-ink));
}

/* Double-class specificity matches role rules while leaving public-header/stage overrides intact. */
.button:where(:focus-visible),
.button:is(.button--primary, .button--cta, .button--secondary, .button--quiet, .button--danger):where(:focus-visible) {
  outline: 3px solid var(--fc-focus-ring);
  outline-offset: 2px;
  box-shadow: none;
}

.button:where([disabled], [aria-disabled="true"]),
.button:is(.button--primary, .button--cta, .button--secondary, .button--quiet, .button--danger):where(
  [disabled], [aria-disabled="true"]) {
  border-color: var(--fc-field-border);
  color: var(--fc-disabled-ink);
  background: var(--fc-disabled-fill);
  opacity: 0.55;
  cursor: not-allowed;
}
