/**
 * CoreDataTable - Custom AJAX Data Table System
 * Built from scratch for CoreHive3 with Tailwind CSS integration
 * 
 * Features OCR-style professional spinner system:
 * - Overlay spinner: EXACT replication of OCR Tailwind spinner (h-8 w-8 border-b-2 border-red-600)
 * - Button spinner: FontAwesome spinner with OCR spacing (fas fa-spinner fa-spin mr-1)
 * - Consistent professional UX across all CoreHive3 tables
 * 
 * Container Expansion Feature (v2.4.0):
 * - Smart container breakout for wide monitors
 * - Preserves sticky columns during expansion
 * - Smooth transitions with localStorage persistence
 */

/* Admin-style table layout using direct CSS for immediate effect */
.core-datatable {
    background-color: white;
    overflow: visible; /* Allow tooltips to show outside bounds */
}

/* Top controls container */
.core-datatable-top-controls {
    margin-bottom: 1rem;
}

/* System control buttons (icon only) */
.core-datatable-top-controls button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
}

.core-datatable-top-controls button:hover {
    transform: translateY(-1px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}


/* Base widget card - no animation for clean expansion */
.widget-card {
    position: relative;
    left: 0;
    transform: translateX(0);
}

/* Table card expansion - expand and center properly */
.widget-card.table-card-expanded {
    max-width: none !important;
    width: calc(100vw - 16rem - 8rem) !important; /* Available space minus sidebar and padding */
    position: relative;
    left: 50%;
    transform: translateX(-50%);
    margin-left: 0 !important;
    margin-right: 0 !important;
}

/* Responsive adjustments */
@media (max-width: 1024px) {
    .widget-card.table-card-expanded {
        width: calc(100vw - 4rem) !important; /* No sidebar on mobile - just account for padding */
    }
}

/* Table-specific expanded state */
.core-datatable.table-expanded {
    animation: expandAnimation 0.3s ease-in-out;
}

/* Make table fill available width when expanded */
.core-datatable.table-expanded .core-datatable-scroll table {
    width: 100% !important;
    min-width: 100% !important;
}

/* Checkbox column should be minimal width - more specific selectors */
.core-datatable .checkbox-column,
.core-datatable th.checkbox-column,
.core-datatable td.checkbox-column,
.core-datatable-scroll th.checkbox-column,
.core-datatable-scroll td.checkbox-column {
    width: 40px !important;
    min-width: 40px !important;
    max-width: 40px !important;
    padding: 0.5rem !important;
    white-space: nowrap;
    text-align: center;
}

/* Actions column should fit content - more specific selectors */
.core-datatable .actions-column,
.core-datatable th.actions-column,
.core-datatable td.actions-column,
.core-datatable-scroll th.actions-column,
.core-datatable-scroll td.actions-column {
    width: 1% !important;  /* This forces minimum width needed for content */
    min-width: fit-content !important;
    max-width: fit-content !important;
    white-space: nowrap;
    padding: 0.5rem 1rem !important;
    text-align: center !important;
}

/* Center the expanded table content */
.container-expanded .widget-card {
    margin-left: auto;
    margin-right: auto;
}

/* Smooth expansion animation */
@keyframes expandAnimation {
    from {
        opacity: 0.95;
        transform: scaleX(0.98);
    }
    to {
        opacity: 1;
        transform: scaleX(1);
    }
}

/* Ensure expanded tables still respect viewport boundaries */
@media (max-width: 768px) {
    .container-expanded {
        padding-left: 0.5rem !important;
        padding-right: 0.5rem !important;
    }
}

/* Horizontal scroll container for tables with many columns */
.core-datatable-scroll {
    overflow-x: auto;
    overflow-y: visible; /* Allow tooltips to show above/below */
    /* Smooth scrolling on mobile devices */
    -webkit-overflow-scrolling: touch;
    /* Custom scrollbar styling */
    scrollbar-width: thin;
    scrollbar-color: #d1d5db #f3f4f6;
    /* Ensure tooltips are not clipped */
    position: relative;
    z-index: 1;
    /* Add left and right borders for all tables */
    border-left: 1px solid #e5e7eb;
    border-right: 1px solid #e5e7eb;
}

/* Webkit scrollbar styling for better UX */
.core-datatable-scroll::-webkit-scrollbar {
    height: 8px;
}

.core-datatable-scroll::-webkit-scrollbar-track {
    background: #f3f4f6;
    border-radius: 4px;
}

.core-datatable-scroll::-webkit-scrollbar-thumb {
    background: #d1d5db;
    border-radius: 4px;
    transition: background-color 0.2s;
}

.core-datatable-scroll::-webkit-scrollbar-thumb:hover {
    background: #9ca3af;
}

.core-datatable table {
    min-width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    /* Ensure table maintains minimum width for horizontal scroll */
    width: max-content;
    /* Allow tooltips to overflow table bounds */
    overflow: visible;
}

/* For tables with many columns, enforce a minimum width */
.core-datatable.multi-sticky-columns table {
    min-width: 1400px; /* Force horizontal scroll for wide tables */
}

.core-datatable table {
    border-top: 1px solid #e5e7eb;
}

.core-datatable thead {
    background-color: #f9fafb;
}

.core-datatable th {
    padding: 0.75rem 1.5rem;
    text-align: left;
    font-size: 0.75rem;
    font-weight: 500;
    color: #6b7280;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    border-bottom: 1px solid #e5e7eb;
}

.core-datatable tbody {
    background-color: white;
}

.core-datatable tbody tr {
    border-bottom: 1px solid #e5e7eb;
}

.core-datatable tbody tr:hover {
    background-color: #f9fafb;
}

.core-datatable tbody tr.selected {
    background-color: #eff6ff;
}

.core-datatable td {
    padding: 1rem 1.5rem;
    white-space: nowrap;
    /* Improve text readability during horizontal scroll */
    text-overflow: ellipsis;
    overflow: visible; /* Allow tooltips to show */
    position: relative; /* Positioning context for tooltips */
}

/* Ensure sticky columns have proper padding to prevent overlap */
.core-datatable.multi-sticky-columns .sticky-left-1,
.core-datatable.multi-sticky-columns .sticky-left-2,
.core-datatable.multi-sticky-columns .sticky-left-3 {
    padding-right: 2rem; /* Extra padding on right to prevent overlap */
}

.core-datatable.multi-sticky-columns .sticky-right-1 {
    padding-left: 2rem; /* Extra padding on left to prevent overlap */
}

/* Allow text wrapping for specific columns when needed */
.core-datatable td.wrap-text {
    white-space: normal;
    min-width: 150px;
}

/* Sticky first column option for better UX with many columns */
.core-datatable.sticky-first-column th:first-child,
.core-datatable.sticky-first-column td:first-child {
    position: sticky;
    left: 0;
    z-index: 5;
    background-color: inherit;
}

.core-datatable.sticky-first-column thead th:first-child {
    z-index: 6;
}

/* Advanced sticky columns system for wide tables */
.core-datatable.multi-sticky-columns {
    position: relative;
}

/* Sticky column shadows for visual separation */
.core-datatable.multi-sticky-columns .sticky-column::after {
    content: "";
    position: absolute;
    top: 0;
    bottom: 0;
    width: 1px;
    background: linear-gradient(to bottom, transparent 0%, rgba(0,0,0,0.1) 50%, transparent 100%);
    pointer-events: none;
}

/* Left sticky columns with proper background and shadows */
.core-datatable.multi-sticky-columns .sticky-left-1 {
    position: sticky;
    left: 0;
    z-index: 5;
    background-color: inherit;
    box-shadow: 2px 0 4px -2px rgba(0, 0, 0, 0.1);
}

.core-datatable.multi-sticky-columns .sticky-left-2 {
    position: sticky;
    left: var(--sticky-left-2-offset, 50px);
    z-index: 4;
    background-color: inherit;
    box-shadow: 2px 0 4px -2px rgba(0, 0, 0, 0.1);
}

.core-datatable.multi-sticky-columns .sticky-left-3 {
    position: sticky;
    left: var(--sticky-left-3-offset, 200px);
    z-index: 3;
    background-color: inherit;
    box-shadow: 2px 0 4px -2px rgba(0, 0, 0, 0.1);
}

/* Right sticky columns */
.core-datatable.multi-sticky-columns .sticky-right-1 {
    position: sticky;
    right: 0;
    z-index: 5;
    background-color: inherit;
    box-shadow: -2px 0 4px -2px rgba(0, 0, 0, 0.1);
}

/* Header sticky columns need higher z-index and stronger background */
.core-datatable.multi-sticky-columns thead .sticky-left-1 {
    z-index: 8;
    background-color: #f9fafb !important;
}

.core-datatable.multi-sticky-columns thead .sticky-left-2 {
    z-index: 7;
    background-color: #f9fafb !important;
}

.core-datatable.multi-sticky-columns thead .sticky-left-3 {
    z-index: 6;
    background-color: #f9fafb !important;
}

.core-datatable.multi-sticky-columns thead .sticky-right-1 {
    z-index: 8;
    background-color: #f9fafb !important;
}

/* Body sticky columns need white background */
.core-datatable.multi-sticky-columns tbody .sticky-left-1 {
    background-color: white !important;
}

.core-datatable.multi-sticky-columns tbody .sticky-left-2 {
    background-color: white !important;
}

.core-datatable.multi-sticky-columns tbody .sticky-left-3 {
    background-color: white !important;
}

.core-datatable.multi-sticky-columns tbody .sticky-right-1 {
    background-color: white !important;
}

/* Hover state for rows - ensure sticky columns maintain proper background */
.core-datatable.multi-sticky-columns tbody tr:hover .sticky-left-1,
.core-datatable.multi-sticky-columns tbody tr:hover .sticky-left-2,
.core-datatable.multi-sticky-columns tbody tr:hover .sticky-left-3,
.core-datatable.multi-sticky-columns tbody tr:hover .sticky-right-1 {
    background-color: #f9fafb !important;
}

/* Selected state for rows */
.core-datatable.multi-sticky-columns tbody tr.selected .sticky-left-1,
.core-datatable.multi-sticky-columns tbody tr.selected .sticky-left-2,
.core-datatable.multi-sticky-columns tbody tr.selected .sticky-left-3,
.core-datatable.multi-sticky-columns tbody tr.selected .sticky-right-1 {
    background-color: #eff6ff !important;
}

/* Loading overlay - EXACT OCR overlay spinner replication */
.core-datatable-loading {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(255, 255, 255, 0.75);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10;
    border-radius: 0.5rem;
}

.core-datatable-spinner {
    /* EXACT replication of OCR Tailwind spinner: animate-spin rounded-full h-8 w-8 border-b-2 border-red-600 */
    height: 2rem;
    width: 2rem;
    border-radius: 50%;
    border-width: 0;
    border-bottom-width: 2px;
    border-color: #dc2626;
    animation: spin 1s linear infinite;
    margin: 0 auto 0.5rem auto;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.core-datatable-loading-text {
    color: #4b5563;
    font-size: 0.875rem;
    font-weight: 500;
    text-align: center;
}

/* Pagination - proper three-column layout */
.core-datatable .core-datatable-pagination {
    background-color: #f9fafb;
    border-top: 1px solid #e5e7eb;
    padding: 1rem 1.5rem;
    min-height: 60px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
}

.core-datatable .core-datatable-pagination .pagination-counter {
    font-size: 0.875rem;
    color: #6b7280;
    flex-shrink: 0;
}

.core-datatable .core-datatable-pagination .pagination-controls {
    display: flex;
    justify-content: center;
    flex: 1;
}

.core-datatable .core-datatable-pagination .pagination-size-selector {
    flex-shrink: 0;
}

/* Page size selector styling */
.core-datatable .core-datatable-pagination .page-size-selector {
    background-color: white;
    transition: border-color 0.2s ease;
}

.core-datatable .core-datatable-pagination .page-size-selector:focus {
    outline: none;
    border-color: #3b82f6;
    box-shadow: 0 0 0 1px #3b82f6;
}

/* Responsive pagination - stack on smaller screens */
@media (max-width: 768px) {
    .core-datatable .core-datatable-pagination {
        flex-direction: column;
        gap: 1rem;
        text-align: center;
        padding: 1rem;
    }
    
    .core-datatable .core-datatable-pagination > div {
        width: 100%;
        justify-content: center;
    }
}

.core-datatable .core-datatable-pagination .page-numbers {
    display: flex;
    align-items: center;
    gap: 0;
}

/* Empty state */
.core-datatable-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 3rem 1.5rem;
    text-align: center;
}

/* Legacy truncated text with tooltip */
.core-datatable .truncated-text {
    position: relative;
    text-overflow: ellipsis;
    overflow: visible;
    white-space: nowrap;
    cursor: help;
    display: inline-block;
}

/* Smart truncated text - only shows ellipsis when text actually overflows */
.core-datatable .smart-truncated-text {
    display: block;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: help;
    max-width: 300px; /* Constrain to original column width */
}

/* When table is expanded, allow more space for description */
.core-datatable.table-expanded .smart-truncated-text {
    max-width: 500px; /* More space when expanded */
}

/* Smart truncated text uses native browser tooltips for better reliability */
.core-datatable .smart-truncated-text:hover {
    /* Keep overflow: hidden to prevent text spillover */
}