/**
 * 🎨 UNIFIED AI TABLE STYLING - Consistent across ALL models
 * Ensures all AI-generated tables have identical appearance and behavior
 */

/* === BASE AI TABLE STYLES === */
.ai-generated-table {
    width: 100%;
    border-collapse: collapse;
    margin: 8px 0;
    font-size: 14px;
    background: #ffffff;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    border: 1px solid #e9ecef;
}

/* === UNIFIED HEADER STYLING === */
.ai-table-header {
    background: #f8f9fa !important;
    padding: 12px 8px !important;
    text-align: left !important;
    font-weight: 600 !important;
    font-size: 0.8rem !important;
    border-bottom: 2px solid #dee2e6 !important;
    color: #495057 !important;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* === UNIFIED CELL STYLING === */
.ai-table-cell {
    padding: 10px 8px !important;
    border-bottom: 1px solid #dee2e6 !important;
    vertical-align: middle !important;
    word-break: break-word;
    color: #212529;
    font-size: 14px;
}

/* === BODY ROW STYLING === */
.ai-table-body tr {
    transition: all 0.2s ease;
    border-bottom: 1px solid #f1f3f4;
}

.ai-table-body tr:hover {
    background-color: #f8f9fa;
    transform: translateY(-1px);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

/* === RESPONSIVE WRAPPER STYLING - CONSOLIDATED === */
.ai-table-responsive-wrapper {
    /* Core layout */
    overflow-x: auto !important; /* Force horizontal scrolling */
    overflow-y: visible !important; /* Prevent vertical scroll on wrapper */
    margin: 8px 0;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    background: #ffffff;
    border: 1px solid #e9ecef;
    /* Ensure minimum width triggers horizontal scroll */
    min-width: 100%;
    max-width: 100%;
    
    /* CRITICAL FIX: Override universal scrollbar hiding */
    scrollbar-width: thin !important;
    -ms-overflow-style: scrollbar !important;
}

.ai-table-responsive-wrapper::-webkit-scrollbar {
    display: block !important; /* Override universal display: none */
    height: 8px;
    background: transparent;
}

.ai-table-responsive-wrapper::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 4px;
}

.ai-table-responsive-wrapper::-webkit-scrollbar-thumb {
    background: #3b82f6;
    border-radius: 4px;
}

.ai-table-responsive-wrapper::-webkit-scrollbar-thumb:hover {
    background: #2563eb;
}

/* === MOBILE RESPONSIVE RULES (CONSISTENT ACROSS ALL MODELS) === */
@media (max-width: 575.98px) {
    .ai-generated-table {
        font-size: 12px;
        min-width: 700px !important; /* Force horizontal scroll on mobile */
        width: 700px !important; /* Explicit width to trigger scroll */
    }
    
    .ai-table-header {
        font-size: 0.7rem !important;
        padding: 8px 6px !important;
        min-width: 100px; /* Prevent column collapse */
    }
    
    .ai-table-cell {
        padding: 8px 6px !important;
        font-size: 12px;
        min-width: 100px; /* Prevent column collapse */
        white-space: nowrap; /* Prevent text wrapping */
    }
    
    .ai-table-responsive-wrapper {
        -webkit-overflow-scrolling: touch !important; /* Smooth scrolling on iOS */
        overflow-x: auto !important; /* Force horizontal scroll */
        width: 100% !important;
        max-width: 100% !important;
    }
}

@media (min-width: 576px) and (max-width: 767.98px) {
    .ai-generated-table {
        font-size: 13px;
    }
    
    .ai-table-header {
        font-size: 0.75rem !important;
        padding: 10px 7px !important;
    }
    
    .ai-table-cell {
        padding: 9px 7px !important;
        font-size: 13px;
    }
}

@media (min-width: 768px) and (max-width: 991.98px) {
    .ai-generated-table {
        font-size: 14px;
    }
    
    .ai-table-header {
        font-size: 0.8rem !important;
        padding: 11px 8px !important;
    }
    
    .ai-table-cell {
        padding: 10px 8px !important;
        font-size: 14px;
    }
}

@media (min-width: 992px) {
    .ai-generated-table {
        font-size: 14px;
    }
    
    .ai-table-header {
        font-size: 0.8rem !important;
        padding: 12px 8px !important;
    }
    
    .ai-table-cell {
        padding: 10px 8px !important;
        font-size: 14px;
    }
}

/* === ENHANCED ACCESSIBILITY === */
.ai-generated-table:focus-within {
    outline: 2px solid #3b82f6;
    outline-offset: 2px;
}

/* === PRINT STYLES === */
@media print {
    .ai-generated-table {
        box-shadow: none;
        border: 1px solid #000;
    }
    
    .ai-table-responsive-wrapper {
        overflow: visible;
        box-shadow: none;
    }
    
    .ai-table-header {
        background: #f0f0f0 !important;
        color: #000 !important;
    }
}

/* === DARK THEME SUPPORT === */
[data-bs-theme="dark"] .ai-generated-table,
.theme-dark .ai-generated-table {
    background: #343a40;
    border-color: #495057;
    color: #f8f9fa;
}

[data-bs-theme="dark"] .ai-table-header,
.theme-dark .ai-table-header {
    background: #495057 !important;
    border-color: #6c757d !important;
    color: #f8f9fa !important;
}

[data-bs-theme="dark"] .ai-table-cell,
.theme-dark .ai-table-cell {
    border-color: #495057 !important;
    color: #f8f9fa;
}

[data-bs-theme="dark"] .ai-table-body tr:hover,
.theme-dark .ai-table-body tr:hover {
    background-color: #495057;
}

[data-bs-theme="dark"] .ai-table-responsive-wrapper,
.theme-dark .ai-table-responsive-wrapper {
    background: #343a40;
    border-color: #495057;
}

/* === CURRENCY ALIGNMENT === */
.ai-table-cell[data-type="currency"],
.ai-table-cell:has([class*="currency"]),
.ai-table-cell:has([style*="£"]) {
    text-align: right;
    font-weight: 500;
}

/* === PERCENTAGE ALIGNMENT === */
.ai-table-cell[data-type="percentage"],
.ai-table-cell:has([class*="percent"]) {
    text-align: center;
    font-weight: 500;
}

/* === STATUS BADGES IN TABLES === */
.ai-table-cell .badge {
    font-size: 0.7rem;
    padding: 0.25rem 0.5rem;
    border-radius: 12px;
    font-weight: 500;
}

/* === TABLE LOADING STATE === */
.ai-generated-table.loading {
    opacity: 0.6;
    pointer-events: none;
}

.ai-generated-table.loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 20px;
    height: 20px;
    margin: -10px 0 0 -10px;
    border: 2px solid #3b82f6;
    border-radius: 50%;
    border-top-color: transparent;
    animation: spin 1s linear infinite;
}

/* spin keyframes in core-animations.css */

/* === OVERRIDE BOOTSTRAP TABLE-RESPONSIVE === */
.table-responsive {
    overflow-x: auto !important;
}

/* === ENSURE CONSISTENT BEHAVIOR === */
.ai-generated-table * {
    box-sizing: border-box;
}

/* === TABLET RESPONSIVE RULES === */
@media (min-width: 576px) and (max-width: 991.98px) {
    .ai-generated-table {
        min-width: 600px !important; /* Ensure horizontal scroll on tablets */
    }
    
    .ai-table-responsive-wrapper {
        overflow-x: auto !important;
    }
}

/* === DESKTOP RESPONSIVE RULES === */
@media (min-width: 992px) {
    .ai-generated-table {
        min-width: 100%; /* Full width on desktop */
        width: 100%;
    }
    
    /* Only show horizontal scroll if content actually overflows */
    .ai-table-responsive-wrapper {
        overflow-x: auto;
    }
}