/* ==========================================================================
   Forms — fields, text inputs, selects, checkboxes, radios, switches, range,
   icons inside inputs, and validation.

   <div class="field">
     <label class="form-label" for="email">Email</label>
     <input class="form-control" id="email" type="email" required>
     <div class="form-text">We'll never share it.</div>
     <div class="form-error">Enter a valid email address.</div>
   </div>

   Errors appear only after someone has interacted with the field (:user-invalid),
   or when you add .is-invalid yourself (e.g. after a server check).
   ========================================================================== */

:root {
  --mo-control-bg: var(--mo-bg-surface);
  --mo-control-fill: color-mix(in oklab, var(--mo-fg-body) 6%, transparent);   /* .form-filled — see-through, reads on cards and on the page */
  --mo-control-padding-x: .75rem;
  --mo-control-font-size: .875rem;
  --mo-control-placeholder: var(--mo-fg-3);
  --mo-check-size: 1rem;
}

/* Field: label + control + help/error, evenly spaced ----------------------------- */
.field {
  display: grid;
  grid-template-columns: minmax(0, 1fr);       /* a wide child can't push past the screen */
  gap: .375rem;
  min-width: 0;
  align-content: start;
}

.form-label {
  display: inline-flex;
  align-items: center;
  gap: .375rem;
  font-size: .8125rem;
  font-weight: var(--mo-font-weight-medium);
  color: var(--mo-fg-body);
}
/* Required fields get a marker automatically */
.field:has(:required) > .form-label::after,
.form-label.required::after {
  content: "*";
  color: var(--mo-red-fg);
}
/* Optional hint after a label: <label class="form-label">Phone <span class="form-label-hint">Optional</span></label> */
.form-label-hint { font-weight: var(--mo-font-weight-normal); color: var(--mo-fg-3); }

.form-text { font-size: .8125rem; color: var(--mo-fg-3); }
.form-text > :last-child { margin-bottom: 0; }

/* Text inputs, textarea, select ---------------------------------------------------- */
.form-control,
.form-select {
  display: block;
  width: 100%;
  min-height: var(--mo-control-height);
  padding: .4375rem var(--mo-control-padding-x);
  font-family: inherit;
  font-size: var(--mo-control-font-size);
  line-height: 1.5;
  color: var(--mo-fg-body);
  background-color: var(--mo-control-bg);
  background-clip: padding-box;
  border: var(--mo-border-width) solid var(--mo-control-border-color);
  border-radius: var(--mo-radius-control);
  appearance: none;
}
@media (prefers-reduced-motion: no-preference) {
  .form-control, .form-select { transition: border-color var(--mo-duration), box-shadow var(--mo-duration), background-color var(--mo-duration); }
}

.form-control::placeholder { color: var(--mo-control-placeholder); opacity: 1; }
.form-control:hover:not(:disabled, [readonly]),
.form-select:hover:not(:disabled) { border-color: color-mix(in oklab, var(--mo-control-border-color), var(--mo-fg-body) 15%); }

.form-control:focus,
.form-select:focus {
  border-color: var(--mo-theme-bg);
  outline: 0;
  box-shadow: 0 0 0 var(--mo-focus-ring-width) var(--mo-theme-focus-ring);
}

.form-control:disabled,
.form-select:disabled {
  color: var(--mo-fg-3);
  cursor: not-allowed;
  background-color: var(--mo-bg-1);
  border-color: var(--mo-border-color);
}
.form-control[readonly] { background-color: var(--mo-bg-1); }

textarea.form-control { min-height: calc(var(--mo-control-height) * 2.25); resize: vertical; }

/* Select: chevron drawn in a neutral gray that works in both color modes */
.form-select {
  padding-inline-end: calc(var(--mo-control-padding-x) + 1.5rem);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M4 6l4 4 4-4' fill='none' stroke='%237d8494' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right .625rem center;
  background-size: 1rem;
}
.form-select:dir(rtl) { background-position: left .625rem center; }
.form-select[multiple],
.form-select[size]:not([size="1"]) {
  padding-inline-end: var(--mo-control-padding-x);
  background-image: none;
}

/* File input: the button looks like a Moey button */
.form-control[type="file"] { padding-block: 0; padding-inline-start: 0; line-height: calc(var(--mo-control-height) - 2px); cursor: pointer; }
.form-control::file-selector-button {
  height: calc(var(--mo-control-height) - 2px);
  padding: 0 .875rem;
  margin-inline-end: .75rem;
  font: inherit;
  font-weight: var(--mo-font-weight-medium);
  color: var(--mo-fg-body);
  cursor: pointer;
  background: var(--mo-bg-2);
  border: 0;
  border-inline-end: var(--mo-border-width) solid var(--mo-control-border-color);
}
.form-control:hover:not(:disabled)::file-selector-button { background: var(--mo-bg-3); }

/* Color swatch */
.form-control-color {
  width: 3rem;
  padding: .3125rem;
  cursor: pointer;
}
.form-control-color::-webkit-color-swatch-wrapper { padding: 0; }
.form-control-color::-webkit-color-swatch { border: 0; border-radius: var(--mo-radius-1); }
.form-control-color::-moz-color-swatch { border: 0; border-radius: var(--mo-radius-1); }

/* Read-only value shown as plain text, aligned with real controls */
.form-control-plaintext {
  display: block;
  width: 100%;
  min-height: var(--mo-control-height);
  padding: .4375rem 0;
  font-size: var(--mo-control-font-size);
  line-height: 1.5;
  color: var(--mo-fg-body);
  background: transparent;
  border: solid transparent;
  border-width: var(--mo-border-width) 0;
}

/* Sizes */
.form-control-sm, .form-select-sm {
  --mo-control-height: var(--mo-control-height-sm);
  --mo-control-padding-x: .625rem;
  --mo-control-font-size: .8125rem;
  padding-block: .25rem;
  border-radius: calc(var(--mo-radius-control) * .8);
}
/* Touch screens: 16px text in every field, every size — iPhone Safari zooms the page into
   any field with smaller text (moey.js also stops that zoom on iOS, see "Keyboard") */
@media (pointer: coarse) {
  :is(.form-control, .form-select, .form-control-sm, .form-select-sm) { --mo-control-font-size: 1rem; }
}
.form-control-lg, .form-select-lg {
  --mo-control-height: var(--mo-control-height-lg);
  --mo-control-padding-x: 1rem;
  --mo-control-font-size: 1rem;
  padding-block: .5625rem;
  border-radius: calc(var(--mo-radius-control) * 1.2);
}

/* Filled (borderless) style — on a .form, .field or any container -------------------- */
.form-filled :is(.form-control, .form-select),
:is(.form-control, .form-select).form-filled {
  --mo-control-bg: var(--mo-control-fill);
  border-color: transparent;
}
.form-filled :is(.form-control, .form-select):hover:not(:disabled, [readonly]),
:is(.form-control, .form-select).form-filled:hover:not(:disabled, [readonly]) {
  border-color: transparent;
  background-color: color-mix(in oklab, var(--mo-fg-body) 9%, transparent);
}
.form-filled :is(.form-control, .form-select):focus,
:is(.form-control, .form-select).form-filled:focus {
  background-color: var(--mo-bg-surface);
  border-color: var(--mo-theme-bg);
}

/* Checkbox & radio ------------------------------------------------------------------
   <label class="form-check">
     <input type="checkbox"> Remember me
   </label>
   With a description:
   <label class="form-check">
     <input type="radio" name="plan"> <span>Pro <span class="form-text">For growing teams</span></span>
   </label>                                                                        */
/* (a label.form-switch on its own lays out the same way — see Switch below) */
:is(.form-check, label.form-switch) {
  display: flex;
  align-items: flex-start;
  gap: .625rem;
  font-size: .875rem;
  line-height: 1.5;
  cursor: pointer;
}
.form-check-inline { display: inline-flex; margin-inline-end: 1.25rem; }
:is(.form-check, label.form-switch) > span { display: grid; gap: .125rem; }
.form-check .form-text { font-size: .8125rem; }

.form-check > input,
.form-check-input {
  flex-shrink: 0;
  width: var(--mo-check-size);
  height: var(--mo-check-size);
  margin: calc((1.5em - var(--mo-check-size)) / 2) 0 0;
  margin-top: round(down, calc((1.5em - var(--mo-check-size)) / 2), 1px);
  cursor: pointer;
  background-color: var(--mo-control-bg);
  background-repeat: no-repeat;
  background-position: center;
  background-size: 100%;
  border: var(--mo-border-width) solid var(--mo-control-border-color);
  border-radius: min(calc(var(--mo-radius-control) * .5), .375rem);   /* never a circle */
  appearance: none;
}
@media (prefers-reduced-motion: no-preference) {
  .form-check > input, .form-check-input { transition: background-color var(--mo-duration), border-color var(--mo-duration), background-position var(--mo-duration); }
}
.form-check > input[type="radio"],
.form-check-input[type="radio"] { border-radius: 50%; }

.form-check > input:hover:not(:disabled, :checked),
.form-check-input:hover:not(:disabled, :checked) { border-color: color-mix(in oklab, var(--mo-control-border-color), var(--mo-fg-body) 20%); }

.form-check > input:checked,
.form-check-input:checked,
.form-check > input:indeterminate,
.form-check-input:indeterminate {
  background-color: var(--mo-accent, var(--mo-theme-accent));   /* the soft solid shade, like solid buttons */
  border-color: var(--mo-accent, var(--mo-theme-accent));
}
.form-check > input[type="checkbox"]:checked,
.form-check-input[type="checkbox"]:checked { background-image: var(--mo-check-mark, var(--mo-theme-accent-mark)); }
.form-check > input[type="checkbox"]:indeterminate,
.form-check-input[type="checkbox"]:indeterminate { background-image: var(--mo-check-dash, var(--mo-theme-accent-dash)); }
.form-check > input[type="radio"]:checked,
.form-check-input[type="radio"]:checked {
  background-image: radial-gradient(circle, var(--mo-accent-contrast, var(--mo-theme-accent-text)) 0 27%, transparent 32%);
}
/* Checks drawn on the page's own colors — on-text: on something text-colored (dark in light mode),
   on-body: on something page-colored. Each theme's accent picks its own (--mo-theme-accent-mark). */
:root, [data-mo-theme="light"] { --mo-check-on-text: var(--mo-icon-check); --mo-dash-on-text: var(--mo-icon-dash); --mo-check-on-body: var(--mo-icon-check-dark); --mo-dash-on-body: var(--mo-icon-dash-dark); }
[data-mo-theme="dark"] { --mo-check-on-text: var(--mo-icon-check-dark); --mo-dash-on-text: var(--mo-icon-dash-dark); --mo-check-on-body: var(--mo-icon-check); --mo-dash-on-body: var(--mo-icon-dash); }
@media (prefers-color-scheme: dark) { :root:not([data-mo-theme="light"]) { --mo-check-on-text: var(--mo-icon-check-dark); --mo-dash-on-text: var(--mo-icon-dash-dark); --mo-check-on-body: var(--mo-icon-check); --mo-dash-on-body: var(--mo-icon-dash); } }

.form-check > input:focus-visible,
.form-check-input:focus-visible {
  outline: var(--mo-focus-ring-width) solid var(--mo-theme-focus-ring);
  outline-offset: 1px;
}
.form-check:has(> input:disabled) { cursor: not-allowed; opacity: .5; }
.form-check > input:disabled { cursor: not-allowed; }

/* Switch: <label class="form-switch"><input type="checkbox" role="switch"> Notifications</label> */
.form-switch {
  --mo-switch-width: 2.25rem;
  --mo-switch-height: 1.25rem;
}
.form-switch > input,
.form-switch .form-check-input {
  position: relative;
  width: var(--mo-switch-width);
  height: var(--mo-switch-height);
  margin-top: calc((1.5em - var(--mo-switch-height)) / 2);
  margin-top: round(down, calc((1.5em - var(--mo-switch-height)) / 2), 1px);   /* whole pixels: a half pixel blurs the knob unevenly */
  background-color: var(--mo-track, var(--mo-bg-3));
  background-image: none;
  border: 0;                                 /* no border: a fractional border left a seam at 150% scaling */
  border-radius: var(--mo-radius-pill);
  will-change: transform;                    /* drawn on its own pixel-aligned layer: the same crisp gap on every side at any screen scaling */
}
/* The knob: a real circle laid out with the track (not a background image), so the gap around it is the
   same 2px on every side at any zoom or screen scaling. It slides when switched. */
.form-switch > input::before,
.form-switch .form-check-input::before {
  position: absolute;
  top: 2px;
  inset-inline-start: 2px;
  width: calc(var(--mo-switch-height) - 4px);
  height: calc(var(--mo-switch-height) - 4px);
  content: "";
  background: var(--mo-white);
  border-radius: 50%;
}
@media (prefers-reduced-motion: no-preference) {
  .form-switch > input::before, .form-switch .form-check-input::before { transition: translate var(--mo-duration) var(--mo-ease), background-color var(--mo-duration); }
}
.form-switch > input:hover:not(:disabled, :checked),
.form-switch .form-check-input:hover:not(:disabled, :checked) { border-color: transparent; background-color: color-mix(in oklab, var(--mo-track, var(--mo-bg-3)), var(--mo-fg-body) 10%); }
.form-switch > input[type="checkbox"]:checked,
.form-switch .form-check-input[type="checkbox"]:checked {
  background-color: var(--mo-accent, var(--mo-theme-accent));
  background-image: none;
}
.form-switch > input:checked::before,
.form-switch .form-check-input:checked::before {
  background: var(--mo-accent-contrast, var(--mo-theme-accent-text));
  translate: calc(var(--mo-switch-width) - var(--mo-switch-height)) 0;
}
.form-switch > input:checked:dir(rtl)::before,
.form-switch .form-check-input:checked:dir(rtl)::before { translate: calc(var(--mo-switch-height) - var(--mo-switch-width)) 0; }
.form-switch-lg { --mo-switch-width: 2.75rem; --mo-switch-height: 1.5rem; }

/* Text inside the switch: <input type="checkbox" role="switch" data-on="ON" data-off="OFF">
   The track fits its word: the knob, a gap, the longer of the two words, the same gap, end. moey.js
   measures the word once (--mo-switch-word, in units of --mo-switch-font), so every switch is as compact as
   its word allows; without JS a short word (ON / OFF) fits. The width is a whole pixel so the knob stays crisp.
   Centering: the word is sized by its capital letters — 8px tall in the 20px track, 10px in the 24px one
   (font-size-adjust: cap-height, any font) — and its box is trimmed to them (text-box). Track and capitals are
   both an even number of pixels, so the baseline lands on a whole pixel and the text renderer has nothing to
   round: the capitals sit exactly on the knob's middle line.
   It is decoration: the state is read out from the switch itself, so the word is hidden from screen readers. */
.form-switch-text > input,
.form-switch-text .form-check-input {
  --mo-switch-word: 2.7;
  --mo-switch-font: calc(var(--mo-switch-height) * .5);
  --mo-switch-pad: calc(var(--mo-switch-height) * .3);
  --mo-switch-width: calc(var(--mo-switch-height) - 2px + 2 * var(--mo-switch-pad) + var(--mo-switch-word) * var(--mo-switch-font));
}
@supports (width: round(up, 1.5px, 1px)) {
  .form-switch-text > input,
  .form-switch-text .form-check-input {
    --mo-switch-pad: round(calc(var(--mo-switch-height) * .3), 1px);   /* 6px; 7px in the large switch */
    --mo-switch-width: round(up, calc(var(--mo-switch-height) - 2px + 2 * var(--mo-switch-pad) + var(--mo-switch-word) * var(--mo-switch-font)), 1px);
  }
}
@supports (font-size-adjust: cap-height 1) and (width: round(1.5px, 1px)) {
  .form-switch-text > input,
  .form-switch-text .form-check-input { --mo-switch-font: calc(round(calc(var(--mo-switch-height) * .2), 1px) * 2); }   /* capitals 8px; 10px large */
  .form-switch-text > input::after,
  .form-switch-text .form-check-input::after { font-size-adjust: cap-height 1; }
}
.form-switch-text > input::after,
.form-switch-text .form-check-input::after {
  position: absolute;
  inset-block: 0;
  inset-inline: calc(var(--mo-switch-height) - 2px + var(--mo-switch-pad)) var(--mo-switch-pad);   /* off: after the knob */
  display: block;
  height: fit-content;
  margin-block: auto;                                  /* centered top to bottom */
  overflow-x: clip;                                    /* only sideways: the trimmed box is just the capitals, the ink above and below must show */
  overflow-y: visible;
  font-size: var(--mo-switch-font);
  font-weight: var(--mo-font-weight-semibold);
  line-height: 1;
  color: var(--mo-fg-2);
  text-align: center;
  white-space: nowrap;
  text-box: trim-both cap alphabetic;                  /* the box is exactly the capitals' height */
  pointer-events: none;
  content: attr(data-off);
  content: attr(data-off) / "";
}
.form-switch-text > input:checked::after,
.form-switch-text .form-check-input:checked::after {
  inset-inline: var(--mo-switch-pad) calc(var(--mo-switch-height) - 2px + var(--mo-switch-pad));   /* on: before the knob */
  color: var(--mo-accent-contrast, var(--mo-theme-accent-text));
  content: attr(data-on);
  content: attr(data-on) / "";
}

/* Range — flat track (filled up to the thumb) and a white thumb with a colored ring.
   moey.js keeps --mo-range-fill in sync while dragging; Firefox fills natively. */
.form-range {
  --mo-range-track: .375rem;
  --mo-range-thumb: 1.125rem;
  --mo-range-fill: 0%;
  display: block;
  width: 100%;
  height: var(--mo-range-thumb);
  margin: 0;
  padding: 0;
  cursor: pointer;
  background: transparent;
  appearance: none;
}
.form-range:focus { outline: 0; }
.form-range::-webkit-slider-runnable-track {
  height: var(--mo-range-track);
  border-radius: var(--mo-radius-pill);
  background: linear-gradient(to right, var(--mo-accent, var(--mo-theme-accent)) var(--mo-range-fill), var(--mo-track, var(--mo-bg-3)) var(--mo-range-fill));
}
.form-range:dir(rtl)::-webkit-slider-runnable-track {
  background: linear-gradient(to left, var(--mo-accent, var(--mo-theme-accent)) var(--mo-range-fill), var(--mo-track, var(--mo-bg-3)) var(--mo-range-fill));
}
.form-range::-moz-range-track { height: var(--mo-range-track); border-radius: var(--mo-radius-pill); background: var(--mo-track, var(--mo-bg-3)); }
.form-range::-moz-range-progress { height: var(--mo-range-track); border-radius: var(--mo-radius-pill); background: var(--mo-accent, var(--mo-theme-accent)); }
.form-range::-webkit-slider-thumb {
  width: var(--mo-range-thumb);
  height: var(--mo-range-thumb);
  margin-top: calc((var(--mo-range-track) - var(--mo-range-thumb)) / 2);
  background: var(--mo-accent-contrast, var(--mo-white));
  border: 2px solid var(--mo-accent, var(--mo-theme-accent));
  border-radius: 50%;
  appearance: none;
  transition: scale var(--mo-duration), box-shadow var(--mo-duration);
}
.form-range::-moz-range-thumb {
  width: var(--mo-range-thumb);
  height: var(--mo-range-thumb);
  box-sizing: border-box;
  background: var(--mo-accent-contrast, var(--mo-white));
  border: 2px solid var(--mo-accent, var(--mo-theme-accent));
  border-radius: 50%;
}
.form-range:hover::-webkit-slider-thumb { scale: 1.1; }
.form-range:active::-webkit-slider-thumb { scale: 1.15; }
.form-range:focus-visible::-webkit-slider-thumb { box-shadow: 0 0 0 var(--mo-focus-ring-width) var(--mo-theme-focus-ring); }
.form-range:focus-visible::-moz-range-thumb { box-shadow: 0 0 0 var(--mo-focus-ring-width) var(--mo-theme-focus-ring); }
.form-range:disabled { cursor: not-allowed; opacity: .5; }

/* Fieldset & legend -------------------------------------------------------------------- */
.form-fieldset { display: grid; gap: .75rem; padding: 0; margin: 0; border: 0; min-width: 0; }
.form-fieldset > legend { float: none; margin-bottom: .25rem; font-size: .875rem; font-weight: var(--mo-font-weight-semibold); }

/* Validation ---------------------------------------------------------------------------
   Automatic after interaction (:user-invalid / :user-valid), or set yourself:
   .is-invalid / .is-valid on the control.                                           */
.form-error,
.form-success {
  display: none;
  align-items: center;
  gap: .375rem;
  font-size: .8125rem;
}
.form-error { color: var(--mo-red-fg); }
.form-success { color: var(--mo-green-fg); }

:is(.form-control, .form-select):is(.is-invalid, :user-invalid) {
  --mo-theme-bg: var(--mo-red-bg);
  --mo-theme-focus-ring: var(--mo-red-focus-ring);
  border-color: var(--mo-red-bg);
}
:is(.form-control, .form-select).is-valid {
  --mo-theme-bg: var(--mo-green-bg);
  --mo-theme-focus-ring: var(--mo-green-focus-ring);
  border-color: var(--mo-green-bg);
}
.form-check > input:is(.is-invalid, :user-invalid) { border-color: var(--mo-red-bg); }

.field:has(:is(.is-invalid, :user-invalid)) > .form-error,
:is(.form-control, .form-select, .form-check, .form-adorn):is(.is-invalid, :user-invalid) ~ .form-error,
.field:has(.is-valid) > .form-success { display: flex; }
.field:has(:is(.is-invalid, :user-invalid)) > .form-text:not(.form-error) { display: none; }
