/* Keep the native checkbox's form behaviour while presenting it as a switch. */
:root {
    --form-switch-on: var(--clr-primary, #4f46e5);
    --form-switch-off: #64748b;
    --form-switch-off-border: #475569;
    --form-switch-focus: #4f46e5;
}

html[data-theme="dark"] {
    --form-switch-off: #475569;
    --form-switch-off-border: #94a3b8;
    --form-switch-focus: #a5b4fc;
}

footer.bg-footer-bg {
    --form-switch-on: #818cf8;
    --form-switch-off-border: #cbd5e1;
    --form-switch-focus: #c7d2fe;
}

label:has(> input[type="checkbox"]):not(.switch-row) {
    display: inline-flex;
    align-items: center;
    gap: .4rem;
    cursor: pointer;
}

/* In multi-column forms the neighbouring control has a field label above it.
   Reserve that same space above a switch so their control centres line up, even
   when the neighbouring field also has help or validation text below it. */
@media (min-width: 640px) {
    .grid[class*="sm:grid-cols-"] > label:has(> input[type="checkbox"]):not(.switch-row) {
        align-self: start;
        margin-top: 1.5rem;
    }
}

@media (min-width: 768px) {
    .grid[class*="md:grid-cols-"] > label:has(> input[type="checkbox"]):not(.switch-row) {
        align-self: start;
        margin-top: 1.5rem;
    }
}

@media (min-width: 1024px) {
    .grid[class*="lg:grid-cols-"] > label:has(> input[type="checkbox"]):not(.switch-row) {
        align-self: start;
        margin-top: 1.5rem;
    }
}

@media (min-width: 1280px) {
    .grid[class*="xl:grid-cols-"] > label:has(> input[type="checkbox"]):not(.switch-row) {
        align-self: start;
        margin-top: 1.5rem;
    }
}

input[type="checkbox"]:not(.switch input) {
    appearance: none !important;
    -webkit-appearance: none !important;
    position: relative;
    display: inline-block;
    flex: 0 0 2.5rem !important;
    width: 2.5rem !important;
    min-width: 2.5rem !important;
    height: 1.4rem !important;
    margin: 0 !important;
    padding: 0 !important;
    vertical-align: middle;
    border: 1px solid var(--form-switch-off-border) !important;
    border-radius: 999px !important;
    background: var(--form-switch-off) !important;
    box-shadow: none !important;
    cursor: pointer;
    transition: background-color .15s ease, border-color .15s ease;
}

input[type="checkbox"]:not(.switch input)::before {
    content: "";
    position: absolute;
    top: 50%;
    right: .25rem;
    width: 1rem;
    height: 1rem;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .2);
    transform: translateY(-50%);
    transition: transform .15s ease;
}

input[type="checkbox"]:not(.switch input):checked {
    border-color: var(--form-switch-on) !important;
    background: var(--form-switch-on) !important;
}

input[type="checkbox"]:not(.switch input):checked::before {
    transform: translate(-1rem, -50%);
}

input[type="checkbox"]:not(.switch input):dir(ltr)::before {
    right: auto;
    left: .25rem;
}

input[type="checkbox"]:not(.switch input):dir(ltr):checked::before {
    transform: translate(1rem, -50%);
}

input[type="checkbox"]:not(.switch input):focus-visible {
    outline: 2px solid var(--form-switch-focus) !important;
    outline-offset: 2px;
}

input[type="checkbox"]:not(.switch input):is(.error, .input-validation-error, [aria-invalid="true"]) {
    border-color: var(--validation-error-border, #d92d20) !important;
}

input[type="checkbox"]:not(.switch input):disabled {
    opacity: .55;
    cursor: not-allowed;
}

@media (prefers-reduced-motion: reduce) {
    input[type="checkbox"]:not(.switch input),
    input[type="checkbox"]:not(.switch input)::before { transition: none; }
}
