.select2-container {
    width: 100% !important;
}

.select2-selection {
    background-color: var(--form-field-bg) !important;
    color: var(--form-field-fg) !important;
    border: 2px solid rgba(128, 128, 128, 0.2) !important;
    border-radius: 12px !important;
    /* Explicit transition prevents border-radius from animating on open (the "zoom" effect) */
    transition: border-color 0.3s ease !important;
}

/* Fix multiple selection box height — match .form-control: 2×12px padding + 24px line-height + 4px border = 52px */
.select2-selection--multiple {
    min-height: 52px !important;
    padding: 0 !important;
    display: flex !important;
    align-items: center !important;
    position: relative !important;
}

/* Dropdown arrow indicator */
.select2-selection--multiple:before {
    content: "";
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    border-top: 5px solid var(--form-field-fg);
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    pointer-events: none;
    z-index: 10;
}

/* Clear all button - position on the far left */
.select2-selection__clear {
    position: absolute !important;
    left: 8px !important;
    top: 50% !important;
    transform: translateY(-50%) !important;
    width: 20px !important;
    height: 20px !important;
    line-height: 20px !important;
    text-align: center !important;
    font-size: 1.2em !important;
    font-weight: bold !important;
    color: #999 !important;
    cursor: pointer !important;
    padding: 0 !important;
    margin: 0 !important;
    z-index: 11 !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
}

.select2-selection__clear:hover {
    color: #d9534f !important;
}

/* Add padding to selection rendered area to make room for clear button on the left */
.select2-selection--multiple .select2-selection__rendered {
    padding-left: 36px !important;
}

.select2-dropdown {
    background-color: var(--form-field-bg) !important;
    color: var(--form-field-fg) !important;
    border-left: 2px solid rgba(128, 128, 128, 0.2) !important;
    border-right: 2px solid rgba(128, 128, 128, 0.2) !important;
    border-bottom: 2px solid rgba(128, 128, 128, 0.2) !important;

    border-bottom-right-radius: 12px !important;
    border-bottom-left-radius: 12px !important;

    /* Fix positioning gap - align dropdown directly below select box */
    margin-top: -1px !important;
    padding-top: 0 !important;
    transform: translateY(0) !important;
}

/* Search field inside the dropdown menu */
.select2-dropdown .select2-search__field {
    margin-top: 7px !important;
    margin-left: 12px !important;
    padding-top: 0px !important;
    height: 24px !important;
    background-color: var(--form-field-bg) !important;
    color: var(--form-field-fg) !important;
    border-color: rgba(128, 128, 128, 0.3) !important;
}

/* Single-select: match .form-select padding and height */
.select2-selection--single {
    height: auto !important;
    min-height: unset !important;
}

.select2-selection--single .select2-selection__rendered {
    color: var(--form-field-fg) !important;
    padding: 0.75rem 2.5rem 0.75rem 1rem !important;
    line-height: 1.5 !important;
}

.select2-selection--single .select2-selection__arrow {
    height: 100% !important;
    top: 0 !important;
    right: 0.75rem !important;
}

.select2-selection--single .select2-selection__placeholder {
    color: var(--form-field-fg) !important;
    opacity: 0.5;
}

/* Inline search container - should not add extra height */
.select2-search--inline {
    display: inline-block !important;
    vertical-align: middle !important;
    margin: 0 !important;
    padding: 0 !important;
}

/* Inline search field inside the selection box - fix positioning */
.select2-search--inline .select2-search__field {
    margin: 0 !important;
    padding: 0 !important;
    height: 1.5em !important;
    max-height: 1.5em !important;
    min-height: 1.5em !important;
    line-height: 1.5em !important;
    vertical-align: middle !important;
    border: none !important;
    box-sizing: border-box !important;
    resize: none !important;
    overflow: hidden !important;
}

/* Selection rendered area should display items inline */
.select2-selection__rendered {
    display: flex !important;
    flex-wrap: wrap !important;
    align-items: center !important;
    padding: 12px 2.5rem 12px 1rem !important;
    margin: 0 !important;
    gap: 4px !important;
    width: 100% !important;
    box-sizing: border-box !important;
}

.select2-container--open .select2-selection {
    border-color: var(--button-color) !important;
}

.select2-container--focus .select2-selection {
    border-color: var(--button-color) !important;
    outline: none !important;
}

.select2-results__option {
    background-color: var(--form-field-bg) !important;
    color: var(--form-field-fg) !important;
}

.select2-results__option--highlighted[aria-selected] {
    background-color: var(--accent-color-1) !important;
    color: var(--button-text-color) !important;
}

/* Selected items (badges) styling */
.select2-selection__choice {
    margin: 0 !important;
    padding: 2px 8px !important;
    display: inline-flex !important;
    align-items: center !important;
    max-height: 1.5em !important;
}

.select2-selection__choice__display {
    padding-left: 20px !important;
    line-height: 1.5em !important;
}

.select2-selection__choice__remove {
    margin-right: 4px !important;
}

/* Fix dropdown positioning - remove all gaps */
.select2-container--default.select2-container--open .select2-selection--single,
.select2-container--default.select2-container--open .select2-selection--multiple {
    border-bottom-left-radius: 0 !important;
    border-bottom-right-radius: 0 !important;
    border-bottom-color: transparent !important;
}

/* Fix for dropdown positioning when attached to body (below) */
.select2-container--default .select2-dropdown--below {
    border-top: none !important;
    border-top-left-radius: 0 !important;
    border-top-right-radius: 0 !important;
    margin-top: -1px !important;
    transform: translateY(0) !important;
}

/* Fix for dropdown positioning when attached to body (above) */
.select2-container--default .select2-dropdown--above {
    border-bottom: none !important;
    border-bottom-left-radius: 0 !important;
    border-bottom-right-radius: 0 !important;
    margin-bottom: -1px !important;
    transform: translateY(0) !important;
}

/* Additional fix for body-attached dropdowns */
body > .select2-container--default.select2-container--open .select2-dropdown {
    margin-top: -1px !important;
    transform: translateY(0) !important;
}

/* Specifically target the dropdown when it appears below */
.select2-container--open .select2-dropdown--below {
    top: 100% !important;
}

/* DARK MODE SUPPORT */
[data-theme="dark"] .select2-selection {
    background-color: var(--form-field-bg) !important;
    border: 2px solid rgba(255, 255, 255, 0.2) !important;
    color: var(--form-field-fg) !important;
}

[data-theme="dark"] .select2-dropdown {
    background-color: var(--form-field-bg) !important;
    border-color: rgba(255, 255, 255, 0.2) !important;
    color: var(--form-field-fg) !important;
}

[data-theme="dark"] .select2-search__field {
    background-color: var(--container-bg-color) !important;
    color: var(--text-color) !important;
    border-color: rgba(255, 255, 255, 0.2) !important;
}

[data-theme="dark"] .select2-results__option {
    background-color: var(--container-bg-color) !important;
    color: var(--text-color) !important;
}

[data-theme="dark"] .select2-results__option--highlighted[aria-selected] {
    background-color: var(--accent-color-1) !important;
    color: var(--button-text-color) !important;
}

[data-theme="dark"] .select2-selection__choice {
    background-color: rgba(255, 255, 255, 0.1) !important;
    color: var(--text-color) !important;
    border-color: rgba(255, 255, 255, 0.2) !important;
}

[data-theme="dark"] .select2-selection__choice__remove {
    color: var(--text-color) !important;
}

[data-theme="dark"] .select2-selection__choice__remove:hover {
    color: var(--error-color) !important;
}