/* 
=============================================================================
FIXED Searchable Select CSS - Grid/Flex Compatible Version
=============================================================================
المشاكل المحددة والحلول:

1. المشكلة الرئيسية: flex: 1 1 auto في wrapper يكسر Grid
2. عدم تحديد عرض مناسب للـ wrapper في Grid containers
3. الـ wrapper بياخد أكتر من مساحته المطلوبة
4. عدم وجود قيود على min-width و max-width
*/

.searchable-select-wrapper {
    position: relative;
    box-sizing: border-box;
    
    /* FIXED: إزالة flex properties التي تكسر Grid */
    /* flex: 1 1 auto; - REMOVED */
    
    /* FIXED: تحديد أبعاد مناسبة للـ Grid */
    width: 100%;
    min-width: 200px; /* منع الانكماش أكتر من اللازم */
    max-width: 100%; /* منع التمدد أكتر من Container */
    
    /* FIXED: تحديد display behavior حسب السياق */
    display: block; /* Default للـ grid items */
}

/* CRITICAL FIX: قواعد محددة للـ Grid Containers */
.grid .searchable-select-wrapper,
.md\\:grid .searchable-select-wrapper,
.lg\\:grid .searchable-select-wrapper,
.xl\\:grid .searchable-select-wrapper,
[class*="grid-cols"] .searchable-select-wrapper {
    /* إجبار Block display في Grid */
    display: block !important;
    
    /* منع أي flex properties */
    flex: none !important;
    
    /* تحديد عرض مناسب */
    width: 100%;
    min-width: 0; /* السماح بـ shrinking في Grid ضيق */
    max-width: 100%;
    
    /* منع overflow */
    overflow: visible;
    
    /* تحديد grid behavior */
    grid-column: span 1;
    align-self: stretch;
    justify-self: stretch;
}

/* CRITICAL FIX: قواعد محددة للـ Flex Containers */
.flex .searchable-select-wrapper,
[class*="flex-"] .searchable-select-wrapper {
    /* تفعيل flex behavior في Flex containers */
    display: block;
    flex: 1 1 auto;
    min-width: 0;
    max-width: none;
    align-self: stretch;
}

/* FIXED: منع التداخل مع Bootstrap/Tailwind Grid */
.row .searchable-select-wrapper,
.container .searchable-select-wrapper {
    width: 100%;
    flex: none;
    max-width: 100%;
}

.searchable-select-trigger {
    width: 100%;
    max-width: 100%;
    padding: 0.75rem 1rem;
    border: 1px solid var(--color-border, #d1d5db);
    border-radius: 0.75rem;
    background: var(--color-bg-primary, #ffffff);
    color: var(--color-text-primary, #374151);
    font-size: 0.875rem;
    cursor: pointer;
    transition: all 0.2s ease-in-out;
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 2.75rem;
    box-sizing: border-box;
    outline: none;
    
    /* FIXED: منع flex shrinking */
    flex-shrink: 0;
}

.searchable-select-trigger:hover:not(:disabled) {
    border-color: var(--color-primary, #3b82f6);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.searchable-select-trigger:focus {
    outline: none;
    border-color: var(--color-primary, #3b82f6);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
}

.searchable-select-trigger.error {
    border-color: var(--color-red, #ef4444);
}

.searchable-select-trigger:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    background: var(--color-bg-disabled, #f3f4f6);
}

.searchable-select-trigger:disabled:hover {
    transform: none;
    box-shadow: none;
    border-color: var(--color-border, #d1d5db);
}

.searchable-select-content {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex: 1;
    min-width: 0;
    text-align: inherit;
}

[dir="rtl"] .searchable-select-content {
    text-align: right;
}

[dir="ltr"] .searchable-select-content {
    text-align: left;
}

.searchable-select-placeholder {
    color: var(--color-text-tertiary, #9ca3af);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    min-width: 0;
}

.searchable-select-value {
    color: var(--color-text-primary, #374151);
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    min-width: 0;
}

.searchable-select-indicators {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-shrink: 0;
    margin-left: 0.75rem;
}

[dir="rtl"] .searchable-select-indicators {
    margin-right: 0.75rem;
    margin-left: 0;
}

.searchable-select-clear {
    padding: 0.25rem;
    border-radius: 50%;
    background: transparent;
    border: none;
    cursor: pointer;
    color: var(--color-text-tertiary, #6b7280);
    transition: all 0.15s ease;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
}

.searchable-select-clear:hover {
    background: var(--color-bg-secondary, #f3f4f6);
    color: var(--color-text-primary, #374151);
}

.searchable-select-arrow {
    color: var(--color-text-tertiary, #9ca3af);
    transition: transform 0.2s ease;
    flex-shrink: 0;
    width: 16px;
    height: 16px;
}

.searchable-select-arrow.open,
.rotate-180 {
    transform: rotate(180deg);
}

.searchable-select-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    z-index: 50;
    margin-top: 0.5rem;
    background: var(--color-bg-primary, #ffffff);
    border: 1px solid var(--color-border, #d1d5db);
    border-radius: 0.75rem;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
    
    /* FIXED: منع dropdown من كسر التخطيط */
    min-width: max-content;
    max-height: 400px;
}

.searchable-select-search {
    padding: 0.75rem;
    border-bottom: 1px solid var(--color-border, #e5e7eb);
}

.searchable-select-search-input {
    width: 100%;
    padding: 0.5rem 1rem;
    border: 1px solid var(--color-border, #d1d5db);
    border-radius: 0.5rem;
    background: var(--color-bg-secondary, #f9fafb);
    color: var(--color-text-primary, #374151);
    font-size: 0.875rem;
    outline: none;
    box-sizing: border-box;
}

[dir="rtl"] .searchable-select-search-input {
    text-align: right;
    padding: 0.5rem 2.5rem 0.5rem 1rem;
}

[dir="ltr"] .searchable-select-search-input {
    text-align: left;
    padding: 0.5rem 1rem 0.5rem 2.5rem;
}

.searchable-select-search-input:focus {
    outline: none;
    border-color: var(--color-primary, #3b82f6);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
}

.searchable-select-search-icon {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    color: var(--color-text-tertiary, #9ca3af);
    pointer-events: none;
    width: 16px;
    height: 16px;
}

[dir="rtl"] .searchable-select-search-icon {
    right: 0.75rem;
    left: auto;
}

[dir="ltr"] .searchable-select-search-icon {
    left: 0.75rem;
    right: auto;
}

.searchable-select-options {
    max-height: 12rem;
    overflow-y: auto;
    box-sizing: border-box;
}

.searchable-select-option {
    width: 100%;
    padding: 0.75rem 1rem;
    border: none;
    background: transparent;
    color: var(--color-text-primary, #374151);
    cursor: pointer;
    transition: all 0.15s ease;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--color-border, #f3f4f6);
    box-sizing: border-box;
}

[dir="rtl"] .searchable-select-option {
    text-align: right;
}

[dir="ltr"] .searchable-select-option {
    text-align: left;
}

.searchable-select-option:last-child {
    border-bottom: none;
}

.searchable-select-option:hover {
    background: var(--color-bg-secondary, #f9fafb);
}

.searchable-select-option.selected {
    background: var(--color-primary-light, #eff6ff);
    color: var(--color-primary, #2563eb);
    font-weight: 500;
}

.searchable-select-option:focus {
    outline: 2px solid var(--color-primary, #3b82f6);
    outline-offset: -2px;
    background: var(--color-bg-secondary, #f9fafb);
}

.searchable-select-option-content {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    min-width: 0;
    flex: 1;
}

[dir="rtl"] .searchable-select-option-content {
    flex-direction: row;
}

[dir="ltr"] .searchable-select-option-content {
    flex-direction: row-reverse;
}

.searchable-select-option-icon {
    font-size: 1.125rem;
    flex-shrink: 0;
    width: 18px;
    text-align: center;
}

.searchable-select-option-text {
    font-size: 0.875rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    min-width: 0;
}

.searchable-select-option-indicator {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-shrink: 0;
}

.searchable-select-check {
    color: var(--color-primary, #2563eb);
    width: 16px;
    height: 16px;
}

.searchable-select-checkbox {
    width: 1rem;
    height: 1rem;
    background: var(--color-primary, #2563eb);
    border-radius: 0.125rem;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.searchable-select-checkbox-icon {
    color: white;
    width: 12px;
    height: 12px;
}

.searchable-select-empty {
    padding: 1.5rem 1rem;
    text-align: center;
    color: var(--color-text-tertiary, #9ca3af);
}

.searchable-select-empty-icon {
    width: 2rem;
    height: 2rem;
    margin: 0 auto 0.5rem;
    opacity: 0.5;
}

.hidden-inputs-container {
    display: none !important;
    visibility: hidden;
    position: absolute;
    top: -9999px;
    left: -9999px;
}

/* Animation classes */
.animate-spin {
    animation: spin 1s linear infinite;
}

@keyframes spin {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

/* Transition animations */
.searchable-select-enter {
    opacity: 0;
    transform: translateY(-4px) scale(0.95);
}

.searchable-select-enter-active {
    opacity: 1;
    transform: translateY(0) scale(1);
    transition: all 0.2s ease-out;
}

.searchable-select-leave {
    opacity: 1;
    transform: translateY(0) scale(1);
}

.searchable-select-leave-active {
    opacity: 0;
    transform: translateY(-4px) scale(0.95);
    transition: all 0.15s ease-in;
}

/* Custom scrollbar */
.searchable-select-options::-webkit-scrollbar {
    width: 6px;
}

.searchable-select-options::-webkit-scrollbar-track {
    background: var(--color-bg-secondary, #f3f4f6);
    border-radius: 3px;
}

.searchable-select-options::-webkit-scrollbar-thumb {
    background: var(--color-border, #d1d5db);
    border-radius: 3px;
}

.searchable-select-options::-webkit-scrollbar-thumb:hover {
    background: var(--color-primary, #3b82f6);
}

/* Dark mode support */
.dark .searchable-select-trigger {
    background: var(--color-bg-primary-dark, #1f2937);
    border-color: var(--color-border-dark, #4b5563);
    color: var(--color-text-primary-dark, #f9fafb);
}

.dark .searchable-select-trigger:hover:not(:disabled) {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.dark .searchable-select-dropdown {
    background: var(--color-bg-primary-dark, #1f2937);
    border-color: var(--color-border-dark, #4b5563);
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3);
}

.dark .searchable-select-search-input {
    background: var(--color-bg-secondary-dark, #374151);
    border-color: var(--color-border-dark, #4b5563);
    color: var(--color-text-primary-dark, #f9fafb);
}

.dark .searchable-select-option {
    color: var(--color-text-primary-dark, #f9fafb);
    border-color: var(--color-border-dark, #4b5563);
}

.dark .searchable-select-option:hover {
    background: var(--color-bg-secondary-dark, #374151);
}

/* CRITICAL: معالجة Responsive Breakpoints */
@media (min-width: 768px) {
    /* MD breakpoint */
    .md\\:grid .searchable-select-wrapper,
    .md\\:grid-cols-1 .searchable-select-wrapper,
    .md\\:grid-cols-2 .searchable-select-wrapper,
    .md\\:grid-cols-3 .searchable-select-wrapper,
    .md\\:grid-cols-4 .searchable-select-wrapper,
    .md\\:grid-cols-5 .searchable-select-wrapper,
    .md\\:grid-cols-6 .searchable-select-wrapper {
        display: block !important;
        width: 100% !important;
        flex: none !important;
        max-width: 100%;
        grid-column: span 1;
    }
}

@media (min-width: 1024px) {
    /* LG breakpoint */
    .lg\\:grid .searchable-select-wrapper,
    .lg\\:grid-cols-1 .searchable-select-wrapper,
    .lg\\:grid-cols-2 .searchable-select-wrapper,
    .lg\\:grid-cols-3 .searchable-select-wrapper,
    .lg\\:grid-cols-4 .searchable-select-wrapper,
    .lg\\:grid-cols-5 .searchable-select-wrapper,
    .lg\\:grid-cols-6 .searchable-select-wrapper {
        display: block !important;
        width: 100% !important;
        flex: none !important;
        max-width: 100%;
        grid-column: span 1;
    }
}

@media (min-width: 1280px) {
    /* XL breakpoint */
    .xl\\:grid .searchable-select-wrapper,
    .xl\\:grid-cols-1 .searchable-select-wrapper,
    .xl\\:grid-cols-2 .searchable-select-wrapper,
    .xl\\:grid-cols-3 .searchable-select-wrapper,
    .xl\\:grid-cols-4 .searchable-select-wrapper,
    .xl\\:grid-cols-5 .searchable-select-wrapper,
    .xl\\:grid-cols-6 .searchable-select-wrapper {
        display: block !important;
        width: 100% !important;
        flex: none !important;
        max-width: 100%;
        grid-column: span 1;
    }
}

/* FIXED: ضمان box-sizing صحيح لكل العناصر */
.searchable-select-wrapper *,
.searchable-select-wrapper *::before,
.searchable-select-wrapper *::after {
    box-sizing: border-box;
}

/* CRITICAL: منع conflicts مع CSS frameworks أخرى */
.searchable-select-wrapper {
    /* Reset any external styling */
    margin: 0;
    padding: 0;
    border: none;
    background: none;
    
    /* Ensure proper stacking */
    position: relative;
    z-index: auto;
    
    /* Force recalculation of layout */
    contain: layout;
}

/* FIXED: معالجة خاصة للـ Small screens */
@media (max-width: 640px) {
    .searchable-select-wrapper {
        width: 100%;
        min-width: auto;
        max-width: 100%;
    }
    
    .searchable-select-dropdown {
        max-height: 60vh;
        margin-top: 0.25rem;
        left: -0.5rem;
        right: -0.5rem;
        width: calc(100% + 1rem);
    }
    
    .searchable-select-options {
        max-height: 40vh;
    }
    
    .searchable-select-option-text {
        font-size: 0.8rem;
    }
    
    .searchable-select-trigger {
        padding: 0.625rem 0.875rem;
        min-height: 2.5rem;
    }
    
    /* Special grid handling on mobile */
    .grid .searchable-select-wrapper {
        width: 100%;
        min-width: 0;
        grid-column: 1 / -1; /* Take full width on mobile */
    }
}

/* Print styles */
@media print {
    .searchable-select-dropdown {
        display: none !important;
    }
    
    .searchable-select-wrapper {
        page-break-inside: avoid;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }
}

/* FINAL FIX: Force override للتأكد من عدم التداخل */
.searchable-select-wrapper[class] {
    /* Force specificity */
    width: 100% !important;
}

.grid .searchable-select-wrapper[class],
[class*="grid"] .searchable-select-wrapper[class] {
    display: block !important;
    flex: none !important;
    width: 100% !important;
    max-width: 100% !important;
}

.flex .searchable-select-wrapper[class],
[class*="flex"] .searchable-select-wrapper[class] {
    flex: 1 1 auto !important;
    width: auto !important;
    min-width: 0 !important;
}