@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-1);
    }

    .form-label {
        font-weight: var(--font-weight-medium);
    }

    /* 
     * 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);
    }

    /* 
   * 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(--white);

        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: 2px 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);
    }

    /* 
   * 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;
        background-size: 1em;
    }

    /* 
   * Basic Checkboxes and Radios
   */
    .input-checkbox,
    .input-radio {
        accent-color: var(--color-link);
        inline-size: 1.25em;
        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;
        block-size: 1.5em;
        background-color: var(--border-color-strong, var(--color-muted));
        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(--white);
        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, var(--color-link));
    }

    .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;
    }
}