/* Widget and module styles ship with !important colors. Keep the popup field
   palette in one cascade layer so it also applies to dynamically loaded forms. */
@layer popup-fields {
    @media screen {
        :is(.modal, .app-modal, .app-popover-body, .swal2-popup) {
            --popup-field-bg: #fff;
            --popup-field-ink: #334155;
            --popup-field-border: #8491a2;
            --popup-field-hint: #606e80;
            --popup-field-muted-bg: #f1f5f9;
            --popup-field-focus: var(--dc-primary-focus-ring, #29689e);
            --popup-field-error: #b42318;
            --popup-field-valid: #167447;
            --popup-create-accent: var(--dc-primary-bg, #2563eb);
            --popup-create-ink: color-mix(in srgb, var(--popup-create-accent) 75%, black);
        }

        body[data-color="1E202D" i] :is(.modal, .app-modal, .app-popover-body, .swal2-popup) {
            --popup-field-bg: #20242c;
            --popup-field-ink: #e5e7eb;
            --popup-field-border: #79879b;
            --popup-field-hint: #aab6c8;
            --popup-field-muted-bg: #343c4b;
            --popup-field-focus: #a7b8ff;
            --popup-field-error: #fda4a4;
            --popup-field-valid: #86d8af;
            --popup-create-accent: var(--popup-field-focus);
            --popup-create-ink: var(--popup-create-accent);
        }

        :is(.modal, .app-modal, .app-popover-body, .swal2-popup) {
            /* Single-line Select2 controls share the native field height.
               Multiple selections can still wrap onto additional lines. */
            & .select2-container-multi .select2-choices {
                min-block-size: 35px !important;
            }

            & :is(.quick-create-control-field, .expense-create-control-field) > .select2-container {
                display: grid !important;
            }

            & :is(.quick-create-control, .expense-create-control):has(.select2-container-multi) > :is(.quick-create-action, .expense-create-action) {
                align-self: stretch !important;
            }

            /* Keep inline validation below the control, outside the button's row. */
            & :is(.quick-create-control, .expense-create-control):has(> div > .help-block):not(:has(> div > a)) {
                display: grid !important;
                grid-template-columns: minmax(0, 1fr) auto;

                & > :is(.quick-create-control-field, .expense-create-control-field) {
                    grid-row: 1 / span 2;
                    display: grid;
                    grid-template-rows: subgrid;
                }
                & .help-block {
                    grid-column: 1;
                    grid-row: 2;
                }
                & > :is(.quick-create-action, .expense-create-action) {
                    grid-area: 1 / 2;
                }
            }

            & :is(.quick-create-action, .expense-create-action) {
                --popup-create-tint: 8%;
                background-color: color-mix(in srgb, var(--popup-create-accent) var(--popup-create-tint), var(--popup-field-bg)) !important;
                color: var(--popup-create-ink) !important;
                border-color: var(--popup-create-accent) !important;

                &:hover, &:focus-visible {
                    --popup-create-tint: 15%;
                }
                &:active {
                    --popup-create-tint: 22%;
                }
                &:focus-visible {
                    outline: 2px solid currentColor !important;
                    outline-offset: -2px !important;
                    box-shadow: none !important;
                }
                &:is(:disabled, .disabled, [aria-disabled="true"]) {
                    color: var(--popup-field-hint) !important;
                    border-color: var(--popup-field-border) !important;
                    background-color: var(--popup-field-muted-bg) !important;
                }
            }

            & :is(
                input:is(:not([type]), [type="text"], [type="email"], [type="tel"], [type="url"],
                    [type="search"], [type="number"], [type="password"], [type="date"], [type="time"],
                    [type="datetime-local"], [type="month"], [type="week"]):not(.select2-input, .select2-focusser),
                textarea, select, .swal2-input, .select2-choice, .select2-choices
            ) {
                border: 1px solid var(--popup-validation-edge, var(--popup-field-border)) !important;
                background-color: var(--popup-field-bg) !important;
                color: var(--popup-field-ink) !important;
                box-shadow: none !important;

                &:focus,
                .select2-container:focus-within > &,
                .select2-container-active > &,
                .select2-dropdown-open > & {
                    /* Paint over the resting border without resizing the field. */
                    border-color: var(--popup-validation-edge, var(--popup-field-focus)) !important;
                    outline: 2px solid var(--popup-validation-edge, var(--popup-field-focus)) !important;
                    outline-offset: -2px !important;
                }

                &:is(:disabled, [readonly]),
                .select2-container-disabled > & {
                    background-color: var(--popup-field-muted-bg) !important;
                }

                &.is-valid,
                .was-validated &:valid {
                    --popup-validation-edge: var(--popup-field-valid);
                }

                &:is(.error, .is-invalid, .wizard-field-error, [aria-invalid="true"], .swal2-inputerror),
                .has-error &,
                .wizard-field-error > &,
                .select2-container:has(+ :is(.is-invalid, [aria-invalid="true"])) > &,
                .was-validated &:invalid {
                    --popup-validation-edge: var(--popup-field-error);
                }
            }

            /* Select2 copies form-control onto its wrapper. Only the visible
               choice draws a border, including on multi-selects. */
            & .select2-container.form-control,
            & .select2-search-field input {
                border: 0 !important;
                background: transparent !important;
                box-shadow: none !important;
            }

            & .select2-container:focus-within .select2-search-field input {
                outline: none !important;
            }

            & :is(.select2-choice, .select2-choices) {
                background-image: none !important;
            }

            & .select2-choice .select2-chosen,
            & .select2-search-field input {
                color: var(--popup-field-ink) !important;
            }

            & :is(input, textarea)::placeholder,
            & .select2-default .select2-chosen,
            & .select2-search-field input.select2-default,
            & .select2-container:has(+ select:not([multiple]) option[value=""]:checked) .select2-chosen,
            & select:not([multiple]):has(option[value=""]:checked) {
                color: var(--popup-field-hint) !important;
                opacity: 1;
            }

            & select:not([multiple]):has(option[value=""]:checked) option:enabled:not(:checked) {
                color: var(--popup-field-ink) !important;
                background-color: var(--popup-field-bg) !important;
            }
        }
    }
}
