/**
 * Manage the improvements of the standard advanced search.
 *
 * Chosen containers are display:inline-block by default. Fix:
 * display:block + min-width:0 for proper flex behavior.
 *
 * The admin CSS sets .inputs select { width: 100% }. Chosen
 * also sets width:100% inline. In flex-nowrap, all elements
 * with width:100% share space equally via flex-shrink — this
 * is the correct core behavior. The module only needs to make
 * chosen containers display:block and shrinkable.
 */
@media all {
    /* Override theme .value { flex-wrap: wrap } to keep
       multi-element rows on one line. Property-queries,
       item-sets and datetime need nowrap. */
    #advanced-search {
        & #property-queries .value,
        & #item-sets .value,
        & #datetime-queries .value {
            flex-wrap: nowrap;
        }

        /* Omeka 4.2 sets the flex layout on .inputs, but not Omeka 4.1, where the
           fields of the module would not be justified. */
        & .inputs {
            &,
            & .value {
                display: flex;
                flex-wrap: wrap;
            }
            & > .value {
                flex: 1 1 auto;
                min-width: 0;
            }
            & > input[type=text],
            & > input[type=number] {
                width: 100%;
            }
        }
        & #sort_field .inputs,
        & .inputs > .field-geo {
            display: flex;
            gap: .25rem;
        }
        & .inputs > .field-geo {
            flex: 1 1 auto;
            min-width: 0;
        }
        & #sort_field .inputs > select,
        & .inputs [name^="geo["] {
            flex: 1 1 auto;
            min-width: 0;
        }
    }

    /* The close of a choice is a button since 4.2. */
    .chosen-container .search-choice-close {
        min-width: 0;
        min-height: 0;
        padding: 0;
        border-radius: 0;
        box-shadow: none;
    }

    /* The radios and the checkboxes are inline by default. */
    #advanced-search {
        & .inputs label {
            width: auto;
        }

        & .chosen-container-multi {
            & .chosen-choices {
                min-height: 36px;
                padding: 0 5px;
                & .search-field input[type=text] {
                    height: 34px;
                    min-height: 0;
                    margin: 0;
                    padding: 0;
                }
            }
        }
        & .chosen-container-single .chosen-single {
            height: 36px;
            line-height: 34px;
        }
        /* Make chosen behave like native selects in flex. */
        & .chosen-container {
            display: block;
            min-width: 0;
        }

        /* === Filter-queries specific === */

        & #filter-queries .inputs > .value {
            flex-wrap: wrap;
        }
        & .inputs > .value .value-fields {
            display: flex;
            flex-wrap: nowrap;
            align-items: center;
            gap: .25rem;
            flex: 1 1 auto;
            min-width: 0;
        }
        & #filter-queries {
            & .chosen-container,
            & .query-text {
                flex: 1 1 0;
            }
            & .query-type {
                width: auto;
                flex: 0 0 auto;
            }
            & .joiner {
                width: auto;
                flex: 0 0 auto;
                min-width: 4em;
            }

            /* Hide disabled/empty filter elements. */
            & .query-text:disabled,
            & .chosen-disabled,
            &.multi-value:not(.has-joiner-not) .value:first-child .joiner,
            &.multi-value:not(.has-joiner-not) .value:first-child .chosen-container:first-of-type {
                display: none;
            }
            /* Show first joiner when "not" joiner is enabled. */
            &.multi-value.has-joiner-not .value:first-child .joiner {
                display: initial;
            }

            & .chosen-container-multi .chosen-choices .search-choice {
                max-width: 24em;
            }
        }
        & .chosen-choices li.search-choice span {
            padding: 4px;
        }

        & #filter-queries {
            & .query-form-element {
                display: none;
            }
            & .query-text.hide {
                display: none;
            }
        }

        /* Sub-query wraps to next line in the parent .value. */
        & .inputs {
            & > .value {
                & .sub-query {
                    width: 100%;
                }
                & .sub-query .query-display {
                    min-height: 35px;
                }
                & .sub-query .query-form-element {
                    flex: 1;
                    margin-top: 0.25rem;
                    margin-bottom: 0.5rem;
                }
            }
        }

        /* === Item-sets === */
        & #item-sets {
            & .value {
                gap: .25rem;
            }
            /* "Dans/Pas dans" fixed width for column alignment. */
            & .item-set-select-type + .chosen-container {
                flex: 0 0 7em;
                width: 7em !important;
            }
        }

        /* === Datetime: native selects, no chosen === */
        & #datetime-queries {
            & .value {
                gap: .25rem;
                align-items: center;
                & select {
                    width: auto;
                    flex: 0 0 auto;
                }
                & .query-text {
                    width: auto;
                    flex: 1 1 auto;
                    min-width: 12em;
                }
            }
        }
    }
}
