/*!
 * Select2 v4 Bootstrap 5.3 Theme
 * Based on apalfrey/select2-bootstrap-5-theme
 * Refactored for Native CSS Variables & Dark Mode
 */

.select2-container--bootstrap-5 {
  display: block;
  /* Beat select2's inline width:NNNpx so the container tracks the column. */
  width: 100% !important;
  max-width: 100%;
}

select+.select2-container--bootstrap-5 {
  z-index: 1;
}

.select2-container--bootstrap-5 *:focus {
  outline: 0;
}

/* -------------------------------------------------------------------------
   1. Base Selection Styling
   ------------------------------------------------------------------------- */
.select2-container--bootstrap-5 .select2-selection {
  position: relative;
  width: 100%;
  min-height: calc(1.5em + 0.75rem + 2px);
  padding: 0.375rem 0.75rem;
  font-family: inherit;
  font-size: 1rem;
  font-weight: 400;
  line-height: 1.5;
  color: var(--bs-body-color);
  background-color: var(--bs-body-bg);
  border: var(--bs-border-width) solid var(--bs-border-color);
  border-radius: var(--bs-border-radius);
  transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
}

@media (prefers-reduced-motion: reduce) {
  .select2-container--bootstrap-5 .select2-selection {
    transition: none;
  }
}

/* Focus State */
.select2-container--bootstrap-5.select2-container--focus .select2-selection,
.select2-container--bootstrap-5.select2-container--open .select2-selection {
  border-color: var(--bs-primary-border-subtle);
  /* Fallback or specific var */
  box-shadow: 0 0 0 0.25rem rgba(var(--bs-primary-rgb), 0.25);
  border-color: #86b7fe;
  /* Fallback for older BS versions */
  border-color: var(--bs-primary);
  /* Modern override */
}

/* Open State Modifications */
.select2-container--bootstrap-5.select2-container--open.select2-container--below .select2-selection {
  border-bottom: 0 solid transparent;
  border-bottom-right-radius: 0;
  border-bottom-left-radius: 0;
}

.select2-container--bootstrap-5.select2-container--open.select2-container--above .select2-selection {
  border-top: 0 solid transparent;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}

/* -------------------------------------------------------------------------
   2. Search Field
   ------------------------------------------------------------------------- */
.select2-container--bootstrap-5 .select2-search {
  width: 100%;
}

.select2-container--bootstrap-5 .select2-search--inline .select2-search__field {
  vertical-align: top;
  color: var(--bs-body-color);
  background: transparent;
}

/* -------------------------------------------------------------------------
   3. Dropdown Menu
   ------------------------------------------------------------------------- */
.select2-container--bootstrap-5 .select2-dropdown {
  z-index: 1056;
  overflow: hidden;
  color: var(--bs-body-color);
  background-color: var(--bs-body-bg);
  border-color: var(--bs-border-color);
  /* Was hardcoded primary, standard is border */
  border: var(--bs-border-width) solid var(--bs-border-color);
  border-radius: var(--bs-border-radius);
}

.select2-container--bootstrap-5.select2-container--open .select2-dropdown {
  border-color: var(--bs-primary);
}

.select2-container--bootstrap-5 .select2-dropdown.select2-dropdown--below {
  border-top: 0 solid transparent;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}

.select2-container--bootstrap-5 .select2-dropdown.select2-dropdown--above {
  border-bottom: 0 solid transparent;
  border-bottom-right-radius: 0;
  border-bottom-left-radius: 0;
}

.select2-container--bootstrap-5 .select2-dropdown .select2-search {
  padding: 0.375rem 0.75rem;
}

.select2-container--bootstrap-5 .select2-dropdown .select2-search .select2-search__field {
  display: block;
  width: 100%;
  padding: 0.375rem 0.75rem;
  font-family: inherit;
  font-size: 1rem;
  font-weight: 400;
  line-height: 1.5;
  color: var(--bs-body-color);
  background-color: var(--bs-body-bg);
  background-clip: padding-box;
  border: 1px solid var(--bs-border-color);
  border-radius: 0.25rem;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
}

.select2-container--bootstrap-5 .select2-dropdown .select2-search .select2-search__field:focus {
  border-color: var(--bs-primary);
  box-shadow: 0 0 0 0.25rem rgba(var(--bs-primary-rgb), 0.25);
  outline: 0;
}

/* Results */
.select2-container--bootstrap-5 .select2-dropdown .select2-results__options:not(.select2-results__options--nested) {
  max-height: 15rem;
  overflow-y: auto;
}

.select2-container--bootstrap-5 .select2-dropdown .select2-results__options .select2-results__option {
  padding: 0.375rem 0.75rem;
  font-size: 1rem;
  font-weight: 400;
  line-height: 1.5;
  color: var(--bs-body-color);
}

.select2-container--bootstrap-5 .select2-dropdown .select2-results__options .select2-results__option.select2-results__message {
  color: var(--bs-secondary-color);
}

/* Highlighted (Hover) */
.select2-container--bootstrap-5 .select2-dropdown .select2-results__options .select2-results__option.select2-results__option--highlighted {
  color: #fff;
  /* Always white on highlight */
  background-color: var(--bs-primary);
}

/* Selected */
.select2-container--bootstrap-5 .select2-dropdown .select2-results__options .select2-results__option.select2-results__option--selected,
.select2-container--bootstrap-5 .select2-dropdown .select2-results__options .select2-results__option[aria-selected=true]:not(.select2-results__option--highlighted) {
  color: var(--bs-white);
  background-color: var(--bs-primary);
  /* Or var(--bs-secondary-bg) if you prefer subtle */
}

.select2-container--bootstrap-5 .select2-dropdown .select2-results__options .select2-results__option.select2-results__option--disabled,
.select2-container--bootstrap-5 .select2-dropdown .select2-results__options .select2-results__option[aria-disabled=true] {
  color: var(--bs-secondary-color);
}

/* Groups */
.select2-container--bootstrap-5 .select2-dropdown .select2-results__options .select2-results__option[role=group] {
  padding: 0;
}

.select2-container--bootstrap-5 .select2-dropdown .select2-results__options .select2-results__option[role=group] .select2-results__group {
  padding: 0.375rem 0.375rem;
  font-weight: 500;
  line-height: 1.5;
  color: var(--bs-secondary-color);
}

.select2-container--bootstrap-5 .select2-dropdown .select2-results__options .select2-results__option[role=group] .select2-results__options--nested .select2-results__option {
  padding: 0.375rem 0.75rem;
}

/* -------------------------------------------------------------------------
   4. Single Select Specifics
   ------------------------------------------------------------------------- */
.select2-container--bootstrap-5 .select2-selection--single {
  /* Force relative positioning so the X stays inside */
  position: relative !important;

  padding: 0.375rem 2.25rem 0.375rem 0.75rem;
  background-image: 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 5l6 6 6-6'/%3e%3c/svg%3e");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
  background-size: 16px 12px;
}

.select2-container--bootstrap-5 .select2-selection--single .select2-selection__rendered {
  padding: 0;
  font-weight: 400;
  line-height: 1.5;
  color: var(--bs-body-color);

  /* CRITICAL: Force the browser to respect these single-line rules */
  display: block !important;
  width: 100% !important;
  overflow: hidden !important;
  text-overflow: ellipsis !important;
  white-space: nowrap !important;
}

.select2-container--bootstrap-5 .select2-selection--single .select2-selection__rendered .select2-selection__placeholder {
  color: var(--bs-secondary-color);
}

.select2-container--bootstrap-5 .select2-selection--single .select2-selection__rendered .select2-selection__arrow {
  display: none;
}

/* Clear Button (Single) */
.select2-container--bootstrap-5 .select2-selection--single .select2-selection__clear {
  /* CRITICAL: Force the X to position itself relative to the parent box */
  position: absolute !important;
  top: 50%;
  right: 2.25rem;
  width: 0.75rem;
  height: 0.75rem;
  padding: 0.25em 0.25em;
  overflow: hidden;
  text-indent: 100%;
  white-space: nowrap;
  background: transparent url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%23676a6d'%3e%3cpath d='M.293.293a1 1 0 011.414 0L8 6.586 14.293.293a1 1 0 111.414 1.414L9.414 8l6.293 6.293a1 1 0 01-1.414 1.414L8 9.414l-6.293 6.293a1 1 0 01-1.414-1.414L6.586 8 .293 1.707a1 1 0 010-1.414z'/%3e%3c/svg%3e") center/0.75rem auto no-repeat;
  transform: translateY(-50%);
  z-index: 2;
}

.select2-container--bootstrap-5 .select2-selection--single .select2-selection__clear:hover {
  background: transparent url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%23000'%3e%3cpath d='M.293.293a1 1 0 011.414 0L8 6.586 14.293.293a1 1 0 111.414 1.414L9.414 8l6.293 6.293a1 1 0 01-1.414 1.414L8 9.414l-6.293 6.293a1 1 0 01-1.414-1.414L6.586 8 .293 1.707a1 1 0 010-1.414z'/%3e%3c/svg%3e") center/0.75rem auto no-repeat;
}

/* -------------------------------------------------------------------------
   5. Multiple Select Specifics
   -------------------------------------------------------------------------
   Design note: this mirrors base select2's default-theme mechanics
   intentionally. The inner ``<ul>`` is ``display: inline`` (no box of its
   own), so the pill ``<li>``s + the inline-search ``<li>`` share the outer
   container's inline flow. ``vertical-align: bottom`` keeps every element
   sitting on the same baseline as it wraps, so the cursor stays beside the
   last pill instead of falling onto its own row.

   We don't override the <input>'s width here either: select2's SearchInline
   adapter writes a pixel ``style="width: …px"`` on the field every
   keystroke; in inline flow that's exactly the right behaviour — the input
   is as wide as the typed text and wraps with the rest of the content.
   ------------------------------------------------------------------------- */
.select2-container--bootstrap-5 .select2-selection--multiple {
  cursor: text;
  padding: 0.25rem 0.5rem;
}

.select2-container--bootstrap-5 .select2-selection--multiple .select2-selection__rendered {
  display: inline;
  padding: 0;
  margin: 0;
  list-style: none;
  /* Override --default's nowrap/hidden so pills can wrap. */
  white-space: normal !important;
  overflow: visible !important;
  text-overflow: clip !important;
}

.select2-container .select2-selection--multiple .select2-selection__rendered {
  white-space: normal !important;
  overflow: visible !important;
}

.select2-container--bootstrap-5 .select2-selection--multiple .select2-selection__rendered .select2-selection__choice {
  display: inline-block;
  vertical-align: bottom;
  padding: 0 0.5em;
  margin: 0.15rem 0.25rem 0.15rem 0;
  font-size: 0.875rem;
  line-height: 1.4;
  color: var(--bs-body-color);
  cursor: default;
  border: 1px solid var(--bs-border-color);
  background-color: var(--bs-tertiary-bg);
  border-radius: 0.25rem;
}

.select2-container--bootstrap-5 .select2-selection--multiple .select2-selection__rendered .select2-selection__choice .select2-selection__choice__remove {
  display: inline-block;
  width: 0.65rem;
  height: 0.65rem;
  padding: 0;
  margin-right: 0.35rem;
  overflow: hidden;
  text-indent: 100%;
  white-space: nowrap;
  vertical-align: middle;
  background: transparent url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%23676a6d'%3e%3cpath d='M.293.293a1 1 0 011.414 0L8 6.586 14.293.293a1 1 0 111.414 1.414L9.414 8l6.293 6.293a1 1 0 01-1.414 1.414L8 9.414l-6.293 6.293a1 1 0 01-1.414-1.414L6.586 8 .293 1.707a1 1 0 010-1.414z'/%3e%3c/svg%3e") center/0.75rem auto no-repeat;
  border: 0;
}

.select2-container--bootstrap-5 .select2-selection--multiple .select2-selection__rendered .select2-selection__choice .select2-selection__choice__remove:hover {
  background: transparent url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%23000'%3e%3cpath d='M.293.293a1 1 0 011.414 0L8 6.586 14.293.293a1 1 0 111.414 1.414L9.414 8l6.293 6.293a1 1 0 01-1.414 1.414L8 9.414l-6.293 6.293a1 1 0 01-1.414-1.414L6.586 8 .293 1.707a1 1 0 010-1.414z'/%3e%3c/svg%3e") center/0.75rem auto no-repeat;
}

.select2-container--bootstrap-5 .select2-selection--multiple .select2-search--inline {
  display: inline;
  width: auto;
  margin: 0;
}

.select2-container--bootstrap-5 .select2-selection--multiple .select2-search--inline .select2-search__field {
  margin: 0;
  padding: 0;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.5;
  color: var(--bs-body-color);
  background-color: transparent;
  border: 0;
  vertical-align: bottom;
  max-width: 100%;
}

.select2-container--bootstrap-5 .select2-selection--multiple .select2-selection__clear {
  position: absolute;
  top: 50%;
  right: 0.75rem;
  width: 0.75rem;
  height: 0.75rem;
  transform: translateY(-50%);
  padding: 0.25em;
  background: transparent url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%23676a6d'%3e%3cpath d='M.293.293a1 1 0 011.414 0L8 6.586 14.293.293a1 1 0 111.414 1.414L9.414 8l6.293 6.293a1 1 0 01-1.414 1.414L8 9.414l-6.293 6.293a1 1 0 01-1.414-1.414L6.586 8 .293 1.707a1 1 0 010-1.414z'/%3e%3c/svg%3e") center/0.75rem auto no-repeat;
  font-size: 0;
}

.select2-container--bootstrap-5 .select2-selection--multiple .select2-selection__clear:hover {
  background: transparent url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%23000'%3e%3cpath d='M.293.293a1 1 0 011.414 0L8 6.586 14.293.293a1 1 0 111.414 1.414L9.414 8l6.293 6.293a1 1 0 01-1.414 1.414L8 9.414l-6.293 6.293a1 1 0 01-1.414-1.414L6.586 8 .293 1.707a1 1 0 010-1.414z'/%3e%3c/svg%3e") center/0.75rem auto no-repeat;
}

/* -------------------------------------------------------------------------
   6. Disabled State
   ------------------------------------------------------------------------- */
.select2-container--bootstrap-5.select2-container--disabled .select2-selection,
.select2-container--bootstrap-5.select2-container--disabled.select2-container--focus .select2-selection {
  color: var(--bs-secondary-color);
  cursor: not-allowed;
  background-color: var(--bs-secondary-bg);
  border-color: var(--bs-border-color);
  box-shadow: none;
}

.select2-container--bootstrap-5.select2-container--disabled .select2-selection--multiple .select2-selection__choice,
.select2-container--bootstrap-5.select2-container--disabled.select2-container--focus .select2-selection--multiple .select2-selection__choice {
  cursor: not-allowed;
  background-color: var(--bs-tertiary-bg);
}

.select2-container--bootstrap-5.select2-container--disabled .select2-selection--multiple .select2-selection__choice .select2-selection__choice__remove {
  display: none;
}

/* -------------------------------------------------------------------------
   7. Validation States
   ------------------------------------------------------------------------- */
/* Valid */
.is-valid+.select2-container--bootstrap-5 .select2-selection,
.was-validated select:valid+.select2-container--bootstrap-5 .select2-selection {
  border-color: var(--bs-success);
}

.is-valid+.select2-container--bootstrap-5.select2-container--focus .select2-selection,
.was-validated select:valid+.select2-container--bootstrap-5.select2-container--focus .select2-selection {
  box-shadow: 0 0 0 0.25rem rgba(var(--bs-success-rgb), 0.25);
}

/* Invalid */
.is-invalid+.select2-container--bootstrap-5 .select2-selection,
.was-validated select:invalid+.select2-container--bootstrap-5 .select2-selection {
  border-color: var(--bs-danger);
}

.is-invalid+.select2-container--bootstrap-5.select2-container--focus .select2-selection,
.was-validated select:invalid+.select2-container--bootstrap-5.select2-container--focus .select2-selection {
  box-shadow: 0 0 0 0.25rem rgba(var(--bs-danger-rgb), 0.25);
}

/* -------------------------------------------------------------------------
   8. Input Groups
   ------------------------------------------------------------------------- */
.input-group>.select2-container--bootstrap-5 {
  flex: 1 1 auto;
  width: 1% !important;
}

.input-group>.select2-container--bootstrap-5 .select2-selection {
  height: 100%;
  line-height: inherit;
}

.input-group:not(.has-validation)>.select2-container--bootstrap-5:not(:last-child) .select2-selection {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}

.input-group>.select2-container--bootstrap-5:not(:first-child) .select2-selection {
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
}

/* -------------------------------------------------------------------------
   9. Dark Mode Override (The Missing Piece)
   ------------------------------------------------------------------------- */
[data-bs-theme="dark"] .select2-container--bootstrap-5 .select2-selection--single {
  /* White Chevron for Dark Mode */
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath fill='none' stroke='%23dee2e6' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M2 5l6 6 6-6'/%3e%3c/svg%3e");
}

[data-bs-theme="dark"] .select2-container--bootstrap-5 .select2-selection__clear {
  /* White 'X' for Dark Mode */
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%23dee2e6'%3e%3cpath d='M.293.293a1 1 0 011.414 0L8 6.586 14.293.293a1 1 0 111.414 1.414L9.414 8l6.293 6.293a1 1 0 01-1.414 1.414L8 9.414l-6.293 6.293a1 1 0 01-1.414-1.414L6.586 8 .293 1.707a1 1 0 010-1.414z'/%3e%3c/svg%3e") !important;
}

[data-bs-theme="dark"] .select2-container--bootstrap-5 .select2-selection__clear:hover {
  /* White 'X' Hover (Bright White) */
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%23fff'%3e%3cpath d='M.293.293a1 1 0 011.414 0L8 6.586 14.293.293a1 1 0 111.414 1.414L9.414 8l6.293 6.293a1 1 0 01-1.414 1.414L8 9.414l-6.293 6.293a1 1 0 01-1.414-1.414L6.586 8 .293 1.707a1 1 0 010-1.414z'/%3e%3c/svg%3e") !important;
}

[data-bs-theme="dark"] .select2-container--bootstrap-5 .select2-selection--multiple .select2-selection__choice__remove {
  /* White 'X' for tags in Dark Mode */
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%23dee2e6'%3e%3cpath d='M.293.293a1 1 0 011.414 0L8 6.586 14.293.293a1 1 0 111.414 1.414L9.414 8l6.293 6.293a1 1 0 01-1.414 1.414L8 9.414l-6.293 6.293a1 1 0 01-1.414-1.414L6.586 8 .293 1.707a1 1 0 010-1.414z'/%3e%3c/svg%3e") !important;
}

[data-bs-theme="dark"] .select2-container--bootstrap-5 .select2-selection--multiple .select2-selection__choice__remove:hover {
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%23fff'%3e%3cpath d='M.293.293a1 1 0 011.414 0L8 6.586 14.293.293a1 1 0 111.414 1.414L9.414 8l6.293 6.293a1 1 0 01-1.414 1.414L8 9.414l-6.293 6.293a1 1 0 01-1.414-1.414L6.586 8 .293 1.707a1 1 0 010-1.414z'/%3e%3c/svg%3e") !important;
}

/* -------------------------------------------------------------------------
   10. Floating Label Support (New Feature)
   ------------------------------------------------------------------------- */
.form-floating .select2-container--bootstrap-5 .select2-selection {
  height: calc(3.5rem + 2px);
  padding-top: 1.625rem;
  padding-bottom: 0.625rem;
}

.form-floating .select2-container--bootstrap-5 .select2-selection__rendered {
  margin-top: -4px;
}

.form-floating .select2-container--bootstrap-5~label {
  transform: scale(0.85) translateY(-0.5rem) translateX(0.15rem);
  opacity: 0.65;
  z-index: 2;
}