/* select2 in the IPT: the look of a Bootstrap 5 form-select, on top of the bootstrap4 theme.
   Load it right after select2-bootstrap4.min.css. Sizes come from the variables below; a page or a wrapper sets them
   (smaller-inputs.css, manage-home.css, ...) instead of overriding the select2 rules. The dropdown is appended to
   <body>, so it only gets variables set on .select2-container--bootstrap4 itself, not those of a wrapper. */

.select2-container--bootstrap4 {
    --s2-font-size: 1rem;
    --s2-height: calc(1.5em + .75rem + 2px);
    --s2-padding-x: .75rem;
    /* where the chevron sits: .75rem from the right edge at every size, as on a Bootstrap 5 form-select (form-select-sm
       makes only the text padding smaller), so select2 and plain selects line up */
    --s2-chevron-x: .75rem;
    --s2-radius: var(--bs-border-radius);
    --s2-color: #4e565f;
    --s2-muted: #6c757d;
    --s2-border: var(--bs-border-color);
    --s2-border-hover: rgba(33, 37, 41, .5);
    --s2-focus: rgb(var(--color-gbif-primary));
    --s2-chevron: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath fill='none' stroke='%23343a40' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='m2 5 6 6 6-6'/%3e%3c/svg%3e");
    --s2-close: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3e%3cpath d='M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z'/%3e%3c/svg%3e");
    --s2-check: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3e%3cpath d='M9 16.17 4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z'/%3e%3c/svg%3e");
}

/* the box */
.select2-container--bootstrap4 .select2-selection {
    font-size: var(--s2-font-size);
    color: var(--s2-color);
    background-color: #fff;
    border: 1px solid var(--s2-border);
    border-radius: var(--s2-radius);
    box-shadow: none;
}

.select2-container--bootstrap4 .select2-selection--single {
    height: var(--s2-height) !important;
}

.select2-container--bootstrap4:not(.select2-container--disabled, .select2-container--focus, .select2-container--open) .select2-selection:hover {
    border-color: var(--s2-border-hover);
}

.select2-container--bootstrap4.select2-container--focus .select2-selection,
.select2-container--bootstrap4.select2-container--open .select2-selection,
.select2-container--bootstrap4.select2-container--focus.select2-container--open .select2-selection {
    border-color: var(--s2-focus);
    border-bottom: 1px solid var(--s2-focus);
    border-radius: var(--s2-radius);
    box-shadow: none;
}

/* invalid (e.g. translation.ftl marks an unchosen value): red, also on hover and focus, as a Bootstrap 5 form-select.
   ~ and not +, because the invalid-feedback div can sit between the select and its container */
select.is-invalid ~ .select2-container--bootstrap4 .select2-selection,
select.is-invalid ~ .select2-container--bootstrap4 .select2-selection:hover,
select.is-invalid ~ .select2-container--bootstrap4.select2-container--focus .select2-selection,
select.is-invalid ~ .select2-container--bootstrap4.select2-container--open .select2-selection {
    border-color: var(--bs-form-invalid-border-color);
}

.select2-container--bootstrap4.select2-container--disabled .select2-selection {
    background-color: var(--bs-secondary-bg);
    border-color: var(--s2-border);
}

/* the value, room on the right for the chevron (and the clear button when there is one) */
.select2-container--bootstrap4 .select2-selection--single .select2-selection__rendered {
    line-height: calc(var(--s2-height) - 2px);
    padding-left: var(--s2-padding-x);
    padding-right: calc(var(--s2-chevron-x) + 16px + .5rem);
    color: var(--s2-color);
}

.select2-container--bootstrap4 .select2-selection--single .select2-selection__rendered:has(.select2-selection__clear) {
    padding-right: calc(var(--s2-chevron-x) + 16px + .5rem + 1.5rem);
}

.select2-container--bootstrap4 .select2-selection--single .select2-selection__placeholder {
    line-height: inherit;
    color: var(--s2-muted);
}

/* the chevron of a Bootstrap 5 form-select, turned over while the list is open */
.select2-container--bootstrap4 .select2-selection--single .select2-selection__arrow {
    top: 0;
    right: var(--s2-chevron-x);
    width: 16px;
    height: 100%;
    background: var(--s2-chevron) no-repeat center / 16px 12px;
    transition: transform .15s ease-in-out;
}

.select2-container--bootstrap4 .select2-selection--single .select2-selection__arrow b {
    display: none;
}

.select2-container--bootstrap4.select2-container--open .select2-selection--single .select2-selection__arrow {
    transform: rotate(180deg);
}

.select2-container--bootstrap4.select2-container--disabled .select2-selection__arrow {
    opacity: .5;
}

@media (prefers-reduced-motion: reduce) {
    .select2-container--bootstrap4 .select2-selection--single .select2-selection__arrow {
        transition: none;
    }
}

/* the clear button: a plain close icon left of the chevron, round grey background on hover. It shows while the
   pointer is over the box or the box has focus, and always on touch screens */
/* .select2-selection in the selector to beat select2's own position: relative */
.select2-container--bootstrap4 .select2-selection .select2-selection__clear {
    display: block;
    position: absolute;
    top: 50%;
    right: calc(var(--s2-chevron-x) + 16px + .25rem);
    float: none;
    width: 1.5rem;
    height: 1.5rem;
    margin: -.75rem 0 0;
    padding: 0;
    font-size: 0;
    line-height: 0;
    color: transparent;
    background-color: transparent;
    border-radius: 50%;
    cursor: pointer;
    opacity: 0;
    transition: opacity .1s ease-in-out, background-color .1s ease-in-out;
}

.select2-container--bootstrap4 .select2-selection__clear::before {
    content: "";
    position: absolute;
    inset: .25rem;
    background-color: var(--s2-muted);
    -webkit-mask: var(--s2-close) no-repeat center / contain;
    mask: var(--s2-close) no-repeat center / contain;
}

.select2-container--bootstrap4:hover .select2-selection__clear,
.select2-container--bootstrap4.select2-container--focus .select2-selection__clear,
.select2-container--bootstrap4.select2-container--open .select2-selection__clear {
    opacity: 1;
}

@media (hover: none) {
    .select2-container--bootstrap4 .select2-selection .select2-selection__clear {
        opacity: 1;
    }
}

.select2-container--bootstrap4 .select2-selection__clear:hover {
    background-color: rgba(0, 0, 0, .08);
}

.select2-container--bootstrap4 .select2-selection__clear:hover::before {
    background-color: #212529;
}

/* several values: chips with the same close icon */
.select2-container--bootstrap4 .select2-selection--multiple {
    min-height: var(--s2-height) !important;
}

/* the chips and the search field in a wrapping row; select2's own inline-block list adds a gap under it */
.select2-container--bootstrap4 .select2-selection--multiple .select2-selection__rendered {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: .25rem;
    padding: calc((var(--s2-height) - 2px - 1.5em) / 2) calc(var(--s2-padding-x) - .25rem);
}

.select2-container--bootstrap4 .select2-selection--multiple .select2-selection__rendered:has(.select2-selection__clear) {
    padding-right: 1.75rem;
}

.select2-container--bootstrap4 .select2-selection--multiple .select2-selection__clear {
    position: absolute !important;
    /* 50% and not --s2-height / 2: the button has font-size 0, so the em in --s2-height would be 0 here */
    top: 50%;
    right: .25rem;
}

.select2-container--bootstrap4 .select2-selection--multiple .select2-selection__choice {
    display: inline-flex;
    align-items: center;
    float: none;
    margin: 0;
    padding: 0 .5rem 0 .25rem;
    font-size: inherit;
    line-height: 1.5;
    color: var(--s2-color);
    background-color: var(--bs-secondary-bg);
    border: 0;
    border-radius: .2rem;
}

.select2-container--bootstrap4 .select2-selection--multiple .select2-search--inline {
    /* small basis: the cursor goes after the last chip rather than onto a line of its own */
    flex: 1 1 1.5rem;
    min-width: 1.5rem;
    float: none;
}

.select2-container--bootstrap4 .select2-selection--multiple .select2-search--inline .select2-search__field {
    width: 100% !important;
    height: 1.5em;
    margin: 0;
    font-size: inherit;
    line-height: 1.5;
}

.select2-container--bootstrap4 .select2-selection--multiple .select2-selection__choice__remove {
    position: relative;
    width: 1em;
    height: 1em;
    margin: 0 .25rem 0 0;
    padding: 0;
    font-size: inherit;
    color: transparent;
    border-radius: 50%;
}

.select2-container--bootstrap4 .select2-selection--multiple .select2-selection__choice__remove::before {
    content: "";
    position: absolute;
    inset: 0;
    background-color: var(--s2-muted);
    -webkit-mask: var(--s2-close) no-repeat center / contain;
    mask: var(--s2-close) no-repeat center / contain;
}

.select2-container--bootstrap4 .select2-selection--multiple .select2-selection__choice__remove:hover::before {
    background-color: #212529;
}

.select2-container--bootstrap4 .select2-selection--multiple .select2-search__field {
    color: var(--s2-color);
}

/* the list: a separate panel just under (or over) the box */
.select2-container--bootstrap4 .select2-dropdown {
    border: 1px solid var(--s2-border);
    border-radius: var(--bs-border-radius);
    box-shadow: 0 .25rem .75rem rgba(0, 0, 0, .1);
    overflow: hidden;
}

.select2-container--bootstrap4 .select2-dropdown.select2-dropdown--below {
    margin-top: 2px;
    border-top: 1px solid var(--s2-border);
    border-radius: var(--bs-border-radius);
}

.select2-container--bootstrap4 .select2-dropdown.select2-dropdown--above {
    margin-top: -2px;
    border-radius: var(--bs-border-radius);
}

.select2-container--bootstrap4 .select2-search--dropdown {
    padding: .375rem;
}

.select2-container--bootstrap4 .select2-search--dropdown .select2-search__field {
    padding: .25rem .5rem;
    font-size: .875rem;
    color: var(--s2-color);
    border: 1px solid var(--s2-border);
    border-radius: .2rem;
    outline: 0;
}

.select2-container--bootstrap4 .select2-search--dropdown .select2-search__field:focus {
    border-color: var(--s2-focus);
}

.select2-container--bootstrap4 .select2-results > .select2-results__options {
    max-height: 18em;
}

.select2-container--bootstrap4 .select2-results__option {
    position: relative;
    padding: .375rem 2rem .375rem .75rem;
    font-size: .875rem;
    color: var(--s2-color);
}

.select2-container--bootstrap4 .select2-results__message {
    color: var(--s2-muted);
}

.select2-container--bootstrap4 .select2-results__group {
    padding: .5rem .75rem .25rem;
    font-size: .75rem;
    font-weight: 500;
    color: var(--s2-muted);
    text-transform: uppercase;
}

.select2-container--bootstrap4 .select2-results__option[role="group"] {
    padding: 0;
}

/* the option under the pointer (or keyboard): grey; the chosen option: primary colour and a check mark */
.select2-container--bootstrap4 .select2-dropdown .select2-results__option[aria-selected="true"] {
    color: var(--s2-focus);
    font-weight: 500;
    background-color: transparent;
}

.select2-container--bootstrap4 .select2-results__option[aria-selected="true"]::after {
    content: "";
    position: absolute;
    top: 50%;
    right: .625rem;
    width: 1rem;
    height: 1rem;
    margin-top: -.5rem;
    background-color: var(--s2-focus);
    -webkit-mask: var(--s2-check) no-repeat center / contain;
    mask: var(--s2-check) no-repeat center / contain;
}

.select2-container--bootstrap4 .select2-results__option--highlighted,
.select2-container--bootstrap4 .select2-dropdown .select2-results__option--highlighted[aria-selected] {
    background-color: rgba(0, 0, 0, .06);
}

.select2-container--bootstrap4 .select2-results__option--highlighted[aria-selected="false"] {
    color: #212529;
}

.select2-container--bootstrap4 .select2-results__option[aria-disabled="true"] {
    color: var(--s2-muted);
    opacity: .65;
}
