@layer components {
  /* 
     * Form Layout & Spacing Defaults
     * Only applied when specifically requested to avoid breaking semantic inline forms.
     */
  .form-base {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    margin-top: var(--space-2);
  }

  /* Standardize spacing between label and input */
  .form-group {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
  }

  .form-group-large {
    gap: var(--space-3);
  }

  .form-label {
    color: var(--color-ink);
    font-weight: var(--font-weight-semibold);
    font-size: var(--text-small);
  }

  .form-label-large {
    font-size: var(--text-normal);
    margin-block-end: var(--space-1);
  }

  .form-actions {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: var(--space-3);
    margin-top: var(--space-3);
  }

  /* 
     * Form Error Wrapper (for Rails error explanation blocks)
     */
  .form-errors {
    background-color: var(--color-negative-soft);
    border: var(--border-width-1) solid var(--color-negative);
    border-radius: var(--radius-md);
    padding: var(--space-3);
    color: var(--color-negative);
  }

  .form-errors p {
    font-weight: var(--font-weight-bold);
    margin-block-start: 0;
    margin-block-end: var(--space-2);
  }

  .form-errors ul {
    margin: 0;
    padding-inline-start: var(--space-4);
  }

  /* Rails wraps every field with a validation error, and its label, in .field_with_errors */
  .field_with_errors .input {
    background-color: var(--color-negative-soft);
    border-color: var(--color-negative);
  }

  .field_with_errors .form-label {
    color: var(--color-negative);
  }

  /* 
   * Base Input (Text, Email, Password, Number, Search, etc.)
   */
  .input {
    appearance: none;
    /* Removes iOS inner shadows */
    inline-size: 100%;

    padding: var(--space-2) var(--space-3);
    font-family: inherit;
    font-size: var(--text-normal);
    line-height: inherit;
    color: var(--color-ink);
    background-color: var(--color-surface-raised);

    border: var(--border);
    border-radius: var(--radius-md);
    transition:
      border-color 0.2s ease,
      box-shadow 0.2s ease;
  }

  .input::placeholder {
    color: var(--color-muted);
    opacity: 1;
    /* Firefox lowers opacity by default */
  }

  /* When the user focuses an input, we usually want to highlight its border or shadow */
  .input:where(:focus, :focus-within) {
    border-color: var(--color-link);
    /* For high contrast mode, we use a solid outline instead of a box-shadow to ensure visibility against all backgrounds */
    outline: var(--focus-ring-size) solid transparent;
    /* Replaced by standard box-shadow or your base focus ring */
    box-shadow: 0 0 0 var(--focus-ring-size)
      color-mix(in srgb, var(--color-link) 30%, transparent);
  }

  /* 
   * Fix for Webkit's ugly yellow/blue autofill background 
   */
  .input:-webkit-autofill,
  .input:-webkit-autofill:hover,
  .input:-webkit-autofill:focus {
    -webkit-text-fill-color: var(--color-ink);
    -webkit-box-shadow: 0 0 0px 1000px var(--color-surface) inset;
    transition: background-color 5000s ease-in-out 0s;
  }

  /* 
   * Textarea specific overrides
   */
  textarea.input {
    resize: vertical;
    /* Only allow vertical resizing to preserve layouts */
    min-block-size: 5rem;
    /* ~3 lines of text */
    padding-block: var(--space-2);
    /* As tall as its text, so it is read without scrolling in the field (e.g. a passage marked in the PDF), up to half
       the window. Chrome and Edge; elsewhere its rows, scrolling as before. */
    field-sizing: content;
    max-block-size: 50vh;
  }

  /* 
   * Select specific overrides
   */
  select.input {
    cursor: pointer;
    padding-inline-end: var(--space-4);

    /* Adds a universal, color-agnostic generic arrow */
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='currentColor'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M19 9l-7 7-7-7'%3E%3C/path%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.75rem center; /* the arrow in the room padding-inline-end leaves */
    background-size: 1em;
  }

  /* 
   * Basic Checkboxes and Radios
   */
  .input-checkbox,
  .input-radio {
    accent-color: var(--color-link);
    inline-size: 1.25em; /* in em: as large as its label's text */
    block-size: 1.25em;
    cursor: pointer;
    vertical-align: middle;
    margin: 0;
  }

  /* 
   * Reusable CSS-Only Toggle Switch
   * Usage: <label class="switch"><input type="checkbox" class="sr-only"><span></span></label>
   */
  .switch {
    position: relative;
    display: inline-flex;
    align-items: center;
    cursor: pointer;
  }

  .switch input {
    /* Uses your utility class sr-only concept without needing the class */
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
  }

  .switch span {
    display: inline-block;
    inline-size: 2.75em; /* in em, as its knob: the switch grows with the text */
    block-size: 1.5em;
    background-color: var(--border-color-strong);
    border-radius: var(--radius-pill);
    position: relative;
    transition: background-color 0.2s ease;
  }

  .switch span::before {
    content: "";
    position: absolute;
    block-size: 1.1em;
    inline-size: 1.1em;
    border-radius: 50%;
    background-color: var(--color-surface-raised);
    inset-block-start: 0.2em;
    inset-inline-start: 0.2em;
    transition: transform 0.2s ease;
    box-shadow: var(--shadow-sm);
  }

  .switch input:checked + span {
    background-color: var(--color-positive);
  }

  .switch input:checked + span::before {
    transform: translateX(1.25em);
  }

  .switch input:focus-visible + span {
    outline: var(--focus-ring-size) solid var(--focus-ring-color);
    outline-offset: var(--focus-ring-offset);
  }

  .switch input:disabled + span {
    opacity: 0.5;
    cursor: not-allowed;
  }

  /* 
   * Modern File Dropzone
   */
  /* The PDF's row in the source form (sources/_form) */
  .dropzone {
    border: var(--border-width-2) dashed var(--color-border);
    transition:
      background-color 0.2s ease,
      border-color 0.2s ease;
  }

  /* Hover & Focus */
  .dropzone:where(:hover, :focus-within) {
    background-color: var(--color-surface-soft);
    border-color: var(--color-link);
  }

  /* Put input above */
  .dropzone-input {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    cursor: pointer;
    z-index: var(--z-1);
  }

  /*
   * A password's field with the button that shows it (shared/_password_field, password controller)
   */
  .password-field {
    position: relative;
  }

  /* Room for the button, so the text doesn't run under it: its icon plus its padding on both sides */
  .password-field .input {
    padding-inline-end: calc(1em + 3 * var(--space-2));
  }

  /* Edge has a button of its own for this */
  .password-field .input::-ms-reveal {
    display: none;
  }

  .password-field__toggle {
    position: absolute;
    inset-block: 0;
    inset-inline-end: var(--space-1);
    margin-block: auto;
    block-size: fit-content;
  }

  .password-field__toggle[aria-pressed="true"] {
    color: var(--color-link);
  }

  /* "At least 12 characters" below a new password: green with a check once it is long enough, the check so it doesn't
     rest on the color alone */
  .password-hint {
    display: flex;
    align-items: center;
    gap: var(--space-1);
    margin-block-start: var(--space-1);
    color: var(--color-muted);
  }

  .password-hint__check {
    display: none;
  }

  .password-hint.is-met {
    color: var(--color-positive);
  }

  .password-hint.is-met .password-hint__check {
    display: inline-block;
  }
}
