/*
 * choice.css — checkbox, radio and radio group.
 *
 * <label class="app-choice" data-type="checkbox | radio">
 *   <input class="app-choice-input" type="checkbox | radio" checked disabled aria-invalid>
 *   <span class="app-choice-mark" aria-hidden="true">[icon: check, minus | dot]</span>
 *   <span class="app-choice-label">Text</span>
 * </label>
 *
 * <label class="app-switch">
 *   <input class="app-switch-input" type="checkbox" role="switch" checked disabled>
 *   <span class="app-switch-track" aria-hidden="true"><span class="app-switch-thumb"></span></span>
 *   <span class="app-switch-label">Kunde per SMS erinnern</span>
 * </label>
 *
 * A switch is for something that takes effect at once (a setting); a checkbox is for a choice a
 * form saves later. Its track is one choice high, so it lines up with checkboxes.
 *
 * <div class="app-choice-group" role="radiogroup" aria-labelledby data-orientation>
 *   <span class="app-choice-group-label" id>Priorität</span>
 *   <div class="app-choice-group-options">[choices]</div>
 * </div>
 *
 * The native input covers the whole label, invisible, so the hit area is the row and the
 * browser keeps the keyboard and form behaviour. The mark is what the eye sees. A checkbox's
 * mark holds a check and a minus; the minus shows while the box is indeterminate (some of a
 * group chosen), a state only script can set, so the wrapper sets it. A choice without a
 * visible label (a row's checkbox in a table) has an empty label and an aria-label.
 */
@layer components {
    .app-choice {
        position: relative;
        display: inline-flex;
        align-items: flex-start;
        gap: var(--app-space-choice);
        color: var(--app-color-text);
        font-size: var(--app-text-body-size);
        /* One line of text is exactly as tall as the mark, so the two align at any length. */
        line-height: var(--app-choice-size);
        cursor: pointer;
    }

    .app-choice-input {
        position: absolute;
        inset: 0;
        inline-size: 100%;
        block-size: 100%;
        margin: 0;
        opacity: 0;
        cursor: inherit;
        appearance: none;
    }

    .app-choice-mark {
        display: inline-flex;
        flex: none;
        align-items: center;
        justify-content: center;
        inline-size: var(--app-choice-size);
        block-size: var(--app-choice-size);
        border: var(--app-border-width) solid var(--app-color-line-control);
        border-radius: var(--app-radius-check);
        background: var(--app-color-surface);
        color: transparent;
        transition:
            background-color var(--app-duration-hover) var(--app-easing),
            border-color var(--app-duration-hover) var(--app-easing);
    }

    .app-choice[data-type="radio"] .app-choice-mark {
        border-radius: var(--app-radius-round);
    }

    .app-choice-label:empty {
        display: none;
    }

    .app-choice-mark > .app-icon + .app-icon,
    .app-choice-input:indeterminate + .app-choice-mark > .app-icon:first-child {
        display: none;
    }

    /* The tick is hidden, not painted transparent: a contrast theme repaints every colour. */
    .app-choice-mark > .app-icon {
        visibility: hidden;
    }

    .app-choice-input:is(:checked, :indeterminate) + .app-choice-mark > .app-icon {
        visibility: visible;
    }

    .app-choice-input:indeterminate + .app-choice-mark > .app-icon + .app-icon {
        display: inline-block;
    }

    /* Checked */
    .app-choice-input:is(:checked, :indeterminate) + .app-choice-mark {
        border-color: var(--app-color-accent);
        background: var(--app-color-accent);
        color: var(--app-color-text-on-accent);
    }

    .app-choice[data-type="radio"] .app-choice-input:checked + .app-choice-mark {
        background: var(--app-color-surface);
        color: var(--app-color-accent);
    }

    @media (hover: hover) {
        .app-choice:hover .app-choice-input:not(:disabled, :checked, :indeterminate) + .app-choice-mark {
            border-color: var(--app-color-line-hover);
        }
    }

    .app-choice-input:focus-visible + .app-choice-mark {
        outline: var(--app-focus-ring-width) solid var(--app-color-focus);
        outline-offset: var(--app-focus-ring-offset);
    }

    .app-choice-input[aria-invalid="true"] + .app-choice-mark {
        border-color: var(--app-color-danger-line);
    }

    .app-choice-input[aria-invalid="true"]:focus-visible + .app-choice-mark {
        outline-color: var(--app-color-danger-line);
    }

    /* Disabled */
    .app-choice:has(.app-choice-input:disabled) {
        color: var(--app-color-text-disabled);
        cursor: not-allowed;
    }

    .app-choice-input:disabled + .app-choice-mark {
        border-color: var(--app-color-line);
        background: var(--app-color-surface-disabled);
    }

    .app-choice-input:disabled:checked + .app-choice-mark {
        color: var(--app-color-text-disabled);
    }

    /* Touch: the row is at least the touch target. */
    @media (pointer: coarse) {
        .app-choice {
            align-items: center;
            min-block-size: var(--app-touch-target);
        }
    }

    /* Group */
    .app-choice-group {
        display: flex;
        flex-direction: column;
        gap: var(--app-space-label);
    }

    .app-choice-group-label {
        color: var(--app-color-text-muted);
        font-size: var(--app-text-label-size);
        font-weight: var(--app-text-label-weight);
        line-height: var(--app-text-label-leading);
    }

    .app-choice-group-options {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        gap: var(--app-space-label);
    }

    .app-choice-group[data-orientation="horizontal"] .app-choice-group-options {
        flex-flow: row wrap;
        column-gap: var(--app-space-section);
    }

    /* Grid: the options in equal columns, as many as fit, so groups stacked one under another
       line up column by column instead of flowing like text. */
    .app-choice-group[data-orientation="grid"] .app-choice-group-options {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(var(--app-choice-column), 1fr));
        column-gap: var(--app-space-section);
    }

    /* Switch */
    .app-switch {
        position: relative;
        display: inline-flex;
        align-items: flex-start;
        gap: var(--app-space-choice);
        color: var(--app-color-text);
        font-size: var(--app-text-body-size);
        line-height: var(--app-choice-size);
        cursor: pointer;
    }

    .app-switch-input {
        position: absolute;
        inset: 0;
        inline-size: 100%;
        block-size: 100%;
        margin: 0;
        opacity: 0;
        cursor: inherit;
        appearance: none;
    }

    .app-switch-track {
        display: inline-flex;
        flex: none;
        align-items: center;
        inline-size: var(--app-switch-width);
        block-size: var(--app-choice-size);
        padding: 0 var(--app-space-switch-inset);
        border-radius: var(--app-radius-round);
        background: var(--app-color-line-control);
        transition: background-color var(--app-duration-hover) var(--app-easing);
    }

    .app-switch-thumb {
        inline-size: var(--app-switch-thumb);
        block-size: var(--app-switch-thumb);
        border-radius: var(--app-radius-round);
        background: var(--app-color-switch-thumb);
        box-shadow: var(--app-shadow-card);
        transition: translate var(--app-duration-hover) var(--app-easing);
    }

    .app-switch-input:checked + .app-switch-track {
        background: var(--app-color-accent);
    }

    .app-switch-input:checked + .app-switch-track .app-switch-thumb {
        translate: calc(var(--app-switch-width) - var(--app-choice-size)) 0;
    }

    @media (hover: hover) {
        .app-switch:hover .app-switch-input:not(:disabled, :checked) + .app-switch-track {
            background: var(--app-color-line-hover);
        }

        .app-switch:hover .app-switch-input:checked:not(:disabled) + .app-switch-track {
            background: var(--app-color-accent-hover);
        }
    }

    .app-switch-input:focus-visible + .app-switch-track {
        outline: var(--app-focus-ring-width) solid var(--app-color-focus);
        outline-offset: var(--app-focus-ring-offset);
    }

    .app-switch:has(.app-switch-input:disabled) {
        color: var(--app-color-text-disabled);
        cursor: not-allowed;
    }

    .app-switch-input:disabled + .app-switch-track {
        background: var(--app-color-surface-disabled);
    }

    /* Disabled: the thumb stays visible, so its side still tells on from off. */
    .app-switch-input:disabled + .app-switch-track .app-switch-thumb {
        background: var(--app-color-line-control);
        box-shadow: none;
    }

    @media (pointer: coarse) {
        .app-switch {
            align-items: center;
            min-block-size: var(--app-touch-target);
        }
    }

    /* Forced colours (a Windows contrast theme): shadows and backgrounds are gone, so frames
       are outlines and a chosen state is the system highlight. */
    @media (forced-colors: active) {
        .app-switch-track {
            outline: var(--app-border-width) solid var(--app-color-forced-line);
            outline-offset: calc(-1 * var(--app-border-width));
        }
        .app-switch-thumb {
            forced-color-adjust: none;
            background: var(--app-color-forced-line);
        }

        .app-switch-input:checked + .app-switch-track {
            forced-color-adjust: none;
            background: var(--app-color-forced-current);
            outline-color: var(--app-color-forced-current);
        }

        .app-switch-input:checked + .app-switch-track .app-switch-thumb {
            background: var(--app-color-forced-current-text);
        }

        .app-switch-input:disabled + .app-switch-track {
            outline-color: var(--app-color-forced-disabled);
        }

        .app-switch-input:disabled + .app-switch-track .app-switch-thumb {
            background: var(--app-color-forced-disabled);
        }
    }
}
