/* ==========================================================
   LAYOUT PROTECTION CSS - Add this to your style.css file
   This ensures AI chat doesn't interfere with main layout
   ========================================================== */

/* Protect main layout from AI chat interference */
body {
    position: relative !important;
    overflow-x: hidden !important; /* Prevent horizontal scroll from chat */
}

/* Multi-select filter dropdown - ensure it's always on top */
.multi-select-container {
    position: relative !important;
    z-index: 1050 !important;
}

.multi-select-dropdown {
    position: fixed !important;
    z-index: 99999 !important;
    background: white !important;
    border: 2px solid #007bff !important;
    box-shadow: 0 0.5rem 1.5rem rgba(0,0,0,0.25) !important;
    border-radius: 0.375rem !important;
    visibility: visible !important;
    opacity: 1 !important;
    display: block !important;
}

.multi-select-button {
    position: relative !important;
    z-index: 1050 !important;
}

/* Ensure dropdown options are visible */
.multi-select-dropdown div {
    background: white !important;
    color: #212529 !important;
}

.multi-select-dropdown input[type="checkbox"] {
    cursor: pointer !important;
    z-index: 1 !important;
}

.multi-select-dropdown label {
    cursor: pointer !important;
    color: #212529 !important;
}

/* 🆕 Completely hide original select elements that have been replaced by multi-select */
select.filter-select[style*="display: none"],
select.filter-select[style*="display:none"] {
    display: none !important;
    visibility: hidden !important;
    position: absolute !important;
    opacity: 0 !important;
    width: 0 !important;
    height: 0 !important;
    pointer-events: none !important;
    z-index: -1 !important;
}

/* 🆕 Ensure only the active dropdown is visible */
.multi-select-dropdown:not([style*="display: block"]):not([style*="display:block"]) {
    display: none !important;
    visibility: hidden !important;
}

/* Ensure main container has proper spacing for floating button */
main.container-fluid {
    padding-bottom: 100px !important; /* Space for floating AI button */
    min-height: calc(100vh - 200px) !important;
}

/* Protect Bootstrap grid system */
.container,
.container-fluid,
.row,
.col,
[class*="col-"] {
    position: relative !important;
    z-index: 1 !important; /* Keep main content below AI chat */
}

/* Protect card layouts */
.card {
    position: relative !important;
    z-index: 1 !important;
}

/* Protect navigation */
.navbar {
    position: relative !important;
    z-index: 100 !important; /* Below AI chat but above main content */
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
}

/* Navbar links styling */
.navbar .nav-link {
    color: #ffffff !important;
    font-weight: 500 !important;
    transition: all 0.3s ease !important;
}

.navbar .nav-link:hover {
    color: #e3f2fd !important;
    transform: translateY(-1px) !important;
}

/* Logout button styling */
.navbar .btn-outline-light {
    color: #ffffff !important;
    border-color: #ffffff !important;
    background: transparent !important;
}

.navbar .btn-outline-light:hover {
    background: rgba(255, 255, 255, 0.2) !important;
    color: #ffffff !important;
    border-color: #ffffff !important;
}

/* Protect filter sections */
.filter-select,
.filter-status,
.filter-actions {
    position: relative !important;
    z-index: 1 !important;
}

/* Protect analysis tabs */
.nav-tabs,
.tab-content,
.tab-pane {
    position: relative !important;
    z-index: 1 !important;
}

/* Protect charts and tables */
.chart-container,
.table-responsive,
.card-body {
    position: relative !important;
    z-index: 1 !important;
}

/* Ensure buttons and modals work properly */
.btn,
.modal,
.dropdown-menu {
    position: relative !important;
    z-index: 50 !important; /* Above main content but below AI chat */
}

/* Protect vendor cards on index page */
.vendor-card {
    position: relative !important;
    z-index: 1 !important;
}

/* Ensure no margin/padding conflicts */
.ai-chat-toggle,
.ai-chat-panel {
    margin: 0 !important;
    padding: 0 !important;
}

/* Fix any potential Bootstrap conflicts */
.ai-chat-toggle:not(.btn),
.ai-chat-panel:not(.card) {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
}

/* Prevent AI chat from affecting document flow */
.ai-chat-toggle,
.ai-chat-panel {
    position: fixed !important;
    float: none !important;
    clear: none !important;
    display: block !important; /* Override any display changes */
}

.ai-chat-panel[style*="display: flex"] {
    display: flex !important;
}

.ai-chat-panel[style*="display: none"] {
    display: none !important;
}

/* Mobile layout protection */
@media (max-width: 768px) {
    main.container-fluid {
        padding-bottom: 80px !important; /* Less space on mobile */
        padding-left: 15px !important;
        padding-right: 15px !important;
    }
    
    /* Ensure no horizontal overflow */
    .ai-chat-panel {
        max-width: calc(100vw - 40px) !important;
        left: 20px !important;
        right: 20px !important;
        width: auto !important;
    }
}

/* Tablet layout protection */
@media (max-width: 992px) {
    .ai-chat-panel {
        width: 350px !important; /* Slightly smaller on tablets */
    }
}

/* Large screen optimization */
@media (min-width: 1400px) {
    .ai-chat-panel {
        right: 40px !important; /* More space from edge on large screens */
    }
    
    .ai-chat-toggle {
        right: 40px !important;
    }
}

/* Fix for any potential conflicts with existing animations */
.ai-chat-toggle {
    animation: aiPulse 2s infinite !important;
}

@keyframes aiPulse {
    0%, 100% { 
        box-shadow: 0 4px 20px rgba(102, 126, 234, 0.4) !important; 
        transform: scale(1) !important;
    }
    50% { 
        box-shadow: 0 4px 30px rgba(102, 126, 234, 0.8) !important; 
        transform: scale(1.02) !important;
    }
}

/* Ensure plotly charts are not affected */
.plotly,
.plotly-graph-div {
    position: relative !important;
    z-index: 1 !important;
}

/* Ensure loading indicators work properly */
.spinner-border,
.loading-indicator {
    position: relative !important;
    z-index: 10 !important;
}

/* Error message protection */
.alert,
.error-message {
    position: relative !important;
    z-index: 10 !important;
}

/* Ensure forms work properly */
form,
.form-control,
.form-select {
    position: relative !important;
    z-index: 1 !important;
}

/* Debug helper - remove in production */
/*
.debug-layout {
    border: 2px solid red !important;
    background: rgba(255, 0, 0, 0.1) !important;
}
*/

/* Reset any potential CSS grid or flexbox conflicts */
.ai-chat-toggle,
.ai-chat-panel {
    grid-area: unset !important;
    order: unset !important;
    flex: none !important;
}

/* Ensure text selection still works in main content */
main.container-fluid * {
    user-select: auto !important;
}

/* AI chat specific text selection */
.ai-chat-panel * {
    user-select: text !important;
}

.ai-chat-toggle {
    user-select: none !important;
}

/* Performance optimization */
.ai-chat-toggle,
.ai-chat-panel {
    will-change: transform, opacity !important;
    backface-visibility: hidden !important;
    -webkit-font-smoothing: antialiased !important;
}

/* Accessibility improvements */
.ai-chat-toggle:focus,
.ai-chat-panel input:focus {
    outline: 2px solid #667eea !important;
    outline-offset: 2px !important;
}

/* Print media - hide AI chat when printing */
@media print {
    .ai-chat-toggle,
    .ai-chat-panel {
        display: none !important;
    }
    
    main.container-fluid {
        padding-bottom: 0 !important;
    }
}

/* ======================================================
   RESPONSIVE LAYOUT — Laptop / Tablet / Mobile
   ====================================================== */

/* Prevent any horizontal scrollbar on the page body */
html, body {
    overflow-x: hidden !important;
    max-width: 100vw !important;
}

/* Main container should fill width without overflowing */
main.container-fluid {
    max-width: 100% !important;
    box-sizing: border-box !important;
}

/* Cards should never exceed viewport width */
.card {
    max-width: 100% !important;
    overflow: hidden !important;
}

/* All tab pane content must scroll horizontally if needed */
.tab-pane, .tab-content {
    overflow-x: auto !important;
    max-width: 100% !important;
}

/* Row must not exceed parent width */
.row {
    max-width: 100% !important;
    margin-left: 0 !important;
    margin-right: 0 !important;
}

/* Tables always get horizontal scrolling */
.table-responsive {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
}

/* Nav tabs horizontal scrolling on all screens (no wrapping/clipping) */
.nav-tabs.card-header-tabs {
    flex-wrap: nowrap !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
    padding-bottom: 2px !important;
}

/* Mid-size laptop: 1024px – 1280px */
@media (max-width: 1280px) {
    main.container-fluid {
        padding-left: 12px !important;
        padding-right: 12px !important;
    }
    .card-body {
        padding: 0.75rem !important;
    }
}

/* Small laptop / large tablet: 768px – 1024px */
@media (max-width: 1024px) {
    main.container-fluid {
        padding-left: 10px !important;
        padding-right: 10px !important;
    }
    .card {
        margin-bottom: 0.75rem !important;
    }
    .card-body {
        padding: 0.6rem !important;
    }
    h5 { font-size: 1rem !important; }
    h6 { font-size: 0.875rem !important; }
    .badge { font-size: 0.7rem !important; }
}

/* Tablet: ≤ 768px */
@media (max-width: 768px) {
    main.container-fluid {
        padding-left: 8px !important;
        padding-right: 8px !important;
        padding-bottom: 80px !important;
    }
    .card-body {
        padding: 0.5rem !important;
    }
    .nav-tabs .nav-link {
        font-size: 0.78rem !important;
        padding: 0.3rem 0.5rem !important;
    }
    .btn-sm, .btn {
        font-size: 0.78rem !important;
    }
}

/* Mobile: ≤ 576px */
@media (max-width: 576px) {
    main.container-fluid {
        padding-left: 6px !important;
        padding-right: 6px !important;
    }
}