/* ==========================================================================
   ATX Solutions - Mobile Responsive Tables
   Table patterns and components optimized for mobile devices
   ========================================================================== */

/* ==========================================================================
   Base Table Optimizations
   ========================================================================== */

@media (max-width: 767px) {
    /* Basic table improvements */
    .table {
        font-size: var(--mobile-font-sm);
        margin-bottom: var(--mobile-space-md);
    }
    
    .table th,
    .table td {
        padding: var(--mobile-space-sm);
        vertical-align: middle;
    }
    
    /* Table headings */
    .table thead th {
        font-size: var(--mobile-font-sm);
        font-weight: 600;
        white-space: nowrap;
    }
    
    /* Smaller tables */
    .table-sm th,
    .table-sm td {
        padding: 6px;
        font-size: var(--mobile-font-xs);
    }
}

/* ==========================================================================
   Pattern A: Horizontal Scroll Tables (Simple Data)
   Best for: Tables with few columns, read-only data
   ========================================================================== */

@media (max-width: 767px) {
    /* Responsive table wrapper with scroll */
    .table-responsive,
    .table-responsive-mobile {
        position: relative;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin-bottom: var(--mobile-space-md);
        border-radius: var(--mobile-radius-md);
    }
    
    /* Hide scrollbar for cleaner look */
    .table-responsive::-webkit-scrollbar,
    .table-responsive-mobile::-webkit-scrollbar {
        height: 4px;
    }
    
    .table-responsive::-webkit-scrollbar-track,
    .table-responsive-mobile::-webkit-scrollbar-track {
        background: #f1f1f1;
        border-radius: 2px;
    }
    
    .table-responsive::-webkit-scrollbar-thumb,
    .table-responsive-mobile::-webkit-scrollbar-thumb {
        background: #888;
        border-radius: 2px;
    }
    
    /* Table inside responsive wrapper */
    .table-responsive .table,
    .table-responsive-mobile .table {
        margin-bottom: 0;
        min-width: 600px; /* Force minimum width to enable scroll */
    }
    
    /* Sticky first column (optional) */
    .table-responsive-mobile.sticky-first-col th:first-child,
    .table-responsive-mobile.sticky-first-col td:first-child {
        position: sticky;
        left: 0;
        background: white;
        z-index: 1;
        box-shadow: 2px 0 4px rgba(0,0,0,0.1);
    }
    
    .table-responsive-mobile.sticky-first-col thead th:first-child {
        z-index: 2;
    }
    
    /* Scroll indicators (shadows) */
    .table-scroll-container {
        position: relative;
    }
    
    .table-scroll-container::before,
    .table-scroll-container::after {
        content: '';
        position: absolute;
        top: 0;
        bottom: 0;
        width: 20px;
        pointer-events: none;
        z-index: 2;
        opacity: 0;
        transition: opacity var(--mobile-transition-base) ease;
    }
    
    .table-scroll-container::before {
        left: 0;
        background: linear-gradient(to right, rgba(255,255,255,1), rgba(255,255,255,0));
    }
    
    .table-scroll-container::after {
        right: 0;
        background: linear-gradient(to left, rgba(255,255,255,1), rgba(255,255,255,0));
    }
    
    .table-scroll-container.has-scroll-left::before {
        opacity: 1;
    }
    
    .table-scroll-container.has-scroll-right::after {
        opacity: 1;
    }
}

/* ==========================================================================
   Pattern B: Card-Based Tables (Complex Data)
   Best for: Tables with many columns, interactive data
   ========================================================================== */

@media (max-width: 767px) {
    /* Hide desktop table on mobile */
    .responsive-table-wrapper .table.d-none.d-md-table {
        display: none !important;
    }
    
    /* Show mobile card list */
    .mobile-card-list {
        display: block;
    }
    
    @media (min-width: 768px) {
        .mobile-card-list {
            display: none;
        }
    }
    
    /* Mobile card item */
    .mobile-card {
        background: white;
        border: 1px solid #dee2e6;
        border-radius: var(--mobile-radius-md);
        margin-bottom: var(--mobile-space-md);
        overflow: hidden;
        box-shadow: var(--mobile-shadow-sm);
    }
    
    /* Card header */
    .mobile-card-header {
        background: #f8f9fa;
        padding: var(--mobile-space-md);
        border-bottom: 1px solid #dee2e6;
        font-weight: 600;
        font-size: var(--mobile-font-base);
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    
    /* Card body */
    .mobile-card-body {
        padding: var(--mobile-space-md);
    }
    
    /* Card row (label-value pair) */
    .mobile-card-row {
        display: flex;
        padding: var(--mobile-space-sm) 0;
        border-bottom: 1px solid #f1f1f1;
    }
    
    .mobile-card-row:last-child {
        border-bottom: none;
    }
    
    .mobile-card-label {
        flex: 0 0 40%;
        font-weight: 600;
        font-size: var(--mobile-font-sm);
        color: #6c757d;
        padding-right: var(--mobile-space-sm);
    }
    
    .mobile-card-value {
        flex: 1;
        font-size: var(--mobile-font-sm);
        word-wrap: break-word;
        overflow-wrap: break-word;
    }
    
    /* Card actions */
    .mobile-card-actions {
        padding: var(--mobile-space-md);
        border-top: 1px solid #dee2e6;
        background: #f8f9fa;
        display: flex;
        gap: var(--mobile-space-sm);
        flex-wrap: wrap;
    }
    
    .mobile-card-actions .btn {
        flex: 1;
        min-width: 100px;
        min-height: var(--mobile-touch-target-min);
    }
    
    /* Compact card (less padding) */
    .mobile-card.compact .mobile-card-body,
    .mobile-card.compact .mobile-card-header,
    .mobile-card.compact .mobile-card-actions {
        padding: var(--mobile-space-sm);
    }
}

/* ==========================================================================
   Table Action Buttons (Mobile)
   ========================================================================== */

@media (max-width: 767px) {
    /* Action buttons in table cells */
    .table td .btn,
    .table td .btn-group {
        min-height: var(--mobile-touch-target-min);
        min-width: var(--mobile-touch-target-min);
        padding: var(--mobile-space-sm);
    }
    
    .table td .btn-sm {
        min-height: 38px;
        min-width: 38px;
        padding: 6px 10px;
    }
    
    /* Button group in table */
    .table td .btn-group .btn {
        min-height: var(--mobile-touch-target-min);
    }
    
    /* Dropdown in table */
    .table td .dropdown-toggle {
        min-height: var(--mobile-touch-target-min);
        min-width: var(--mobile-touch-target-min);
    }
    
    /* Icon-only buttons */
    .table td .btn i,
    .table td .btn svg {
        font-size: 16px;
    }
}

/* ==========================================================================
   Table Pagination (Mobile)
   ========================================================================== */

@media (max-width: 767px) {
    /* Pagination container */
    .pagination {
        flex-wrap: wrap;
        justify-content: center;
        gap: var(--mobile-space-xs);
    }
    
    .page-item {
        margin: 0;
    }
    
    .page-link {
        min-width: var(--mobile-touch-target-min);
        min-height: var(--mobile-touch-target-min);
        padding: var(--mobile-space-sm);
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: var(--mobile-font-sm);
    }
    
    /* Hide middle page numbers on very small screens */
    @media (max-width: 375px) {
        .pagination .page-item:not(.active):not(:first-child):not(:last-child):not(.page-item:nth-child(2)):not(.page-item:nth-last-child(2)) {
            display: none;
        }
    }
    
    /* Pagination info text */
    .pagination-info {
        width: 100%;
        text-align: center;
        font-size: var(--mobile-font-sm);
        color: #6c757d;
        margin-bottom: var(--mobile-space-sm);
    }
}

/* ==========================================================================
   Table Search and Filters (Mobile)
   ========================================================================== */

@media (max-width: 767px) {
    /* Search bar above table */
    .table-search {
        margin-bottom: var(--mobile-space-md);
    }
    
    .table-search .form-control {
        min-height: var(--mobile-touch-target-comfortable);
        font-size: var(--mobile-font-base);
    }
    
    .table-search .btn {
        min-height: var(--mobile-touch-target-comfortable);
        min-width: var(--mobile-touch-target-comfortable);
    }
    
    /* Filter row */
    .table-filters {
        display: flex;
        flex-direction: column;
        gap: var(--mobile-space-sm);
        margin-bottom: var(--mobile-space-md);
    }
    
    .table-filters .form-select,
    .table-filters .form-control {
        min-height: var(--mobile-touch-target-comfortable);
    }
    
    /* Mobile filter toggle */
    .mobile-filter-toggle {
        min-height: var(--mobile-touch-target-min);
        width: 100%;
        margin-bottom: var(--mobile-space-sm);
    }
}

/* ==========================================================================
   Table Sorting (Mobile)
   ========================================================================== */

@media (max-width: 767px) {
    /* Sortable column headers */
    .table thead th.sortable {
        cursor: pointer;
        user-select: none;
        position: relative;
        padding-right: 24px;
    }
    
    .table thead th.sortable::after {
        content: '?';
        position: absolute;
        right: 6px;
        opacity: 0.3;
        font-size: 12px;
    }
    
    .table thead th.sortable.asc::after {
        content: '?';
        opacity: 1;
    }
    
    .table thead th.sortable.desc::after {
        content: '?';
        opacity: 1;
    }
    
    .table thead th.sortable:active {
        background-color: rgba(0,0,0,0.05);
    }
}

/* ==========================================================================
   Table Empty State (Mobile)
   ========================================================================== */

@media (max-width: 767px) {
    .table-empty-state {
        padding: var(--mobile-space-xl);
        text-align: center;
        background: #f8f9fa;
        border-radius: var(--mobile-radius-md);
        margin: var(--mobile-space-md) 0;
    }
    
    .table-empty-state-icon {
        font-size: 48px;
        color: #dee2e6;
        margin-bottom: var(--mobile-space-md);
    }
    
    .table-empty-state-title {
        font-size: var(--mobile-font-lg);
        font-weight: 600;
        margin-bottom: var(--mobile-space-sm);
    }
    
    .table-empty-state-text {
        font-size: var(--mobile-font-sm);
        color: #6c757d;
        margin-bottom: var(--mobile-space-md);
    }
    
    .table-empty-state .btn {
        min-height: var(--mobile-touch-target-comfortable);
    }
}

/* ==========================================================================
   Table Loading State (Mobile)
   ========================================================================== */

@media (max-width: 767px) {
    .table-loading {
        position: relative;
        min-height: 200px;
    }
    
    .table-loading-overlay {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(255, 255, 255, 0.9);
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        gap: var(--mobile-space-md);
        z-index: 10;
        border-radius: var(--mobile-radius-md);
    }
    
    .table-loading-spinner {
        width: 40px;
        height: 40px;
    }
    
    .table-loading-text {
        font-size: var(--mobile-font-sm);
        color: #6c757d;
    }
}

/* ==========================================================================
   Table Selection (Mobile)
   ========================================================================== */

@media (max-width: 767px) {
    /* Checkbox column */
    .table td .form-check-input,
    .table th .form-check-input {
        min-width: 24px;
        min-height: 24px;
        margin: 0;
    }
    
    /* Selectable row */
    .table tbody tr.selectable {
        cursor: pointer;
        transition: background-color var(--mobile-transition-fast) ease;
    }
    
    .table tbody tr.selectable:active {
        background-color: rgba(var(--bs-primary-rgb), 0.1);
    }
    
    .table tbody tr.selected {
        background-color: rgba(var(--bs-primary-rgb), 0.15);
    }
    
    /* Bulk actions bar */
    .table-bulk-actions {
        position: sticky;
        bottom: 0;
        background: white;
        padding: var(--mobile-space-md);
        border-top: 2px solid var(--bs-primary);
        box-shadow: 0 -4px 10px rgba(0,0,0,0.1);
        display: flex;
        gap: var(--mobile-space-sm);
        align-items: center;
        z-index: 100;
    }
    
    .table-bulk-actions-count {
        flex: 1;
        font-size: var(--mobile-font-sm);
        font-weight: 600;
    }
    
    .table-bulk-actions .btn {
        min-height: var(--mobile-touch-target-min);
    }
}

/* ==========================================================================
   Table Header Actions (Create, Export, etc.)
   ========================================================================== */

@media (max-width: 767px) {
    .table-header-actions {
        display: flex;
        flex-direction: column;
        gap: var(--mobile-space-sm);
        margin-bottom: var(--mobile-space-md);
    }
    
    .table-header-actions .btn {
        width: 100%;
        min-height: var(--mobile-touch-target-comfortable);
        justify-content: center;
    }
    
    /* Primary action (e.g., Create) */
    .table-header-actions .btn-primary {
        order: -1; /* Show first */
    }
    
    /* Action button group */
    .table-header-actions .btn-group {
        width: 100%;
        display: flex;
    }
    
    .table-header-actions .btn-group .btn {
        flex: 1;
    }
}

/* ==========================================================================
   Card Table Hybrid (Inside Cards)
   ========================================================================== */

@media (max-width: 767px) {
    /* Table inside card */
    .card .table {
        margin-bottom: 0;
    }
    
    /* Remove card body padding for tables */
    .card-body.table-container {
        padding: 0;
    }
    
    /* Table in card with actions */
    .card .table-responsive {
        border-radius: 0;
    }
    
    .card .card-footer .btn {
        min-height: var(--mobile-touch-target-min);
        width: 100%;
    }
}

/* ==========================================================================
   Striped and Bordered Tables (Mobile)
   ========================================================================== */

@media (max-width: 767px) {
    /* Striped table enhancement */
    .table-striped tbody tr:nth-of-type(odd) {
        background-color: rgba(0, 0, 0, 0.02);
    }
    
    /* Bordered table */
    .table-bordered {
        border: 1px solid #dee2e6;
    }
    
    .table-bordered th,
    .table-bordered td {
        border: 1px solid #dee2e6;
    }
    
    /* Hover effect for mobile */
    .table-hover tbody tr:active {
        background-color: rgba(0, 0, 0, 0.05);
    }
}

/* ==========================================================================
   Responsive Table Utilities
   ========================================================================== */

@media (max-width: 767px) {
    /* Force table cell content to wrap */
    .table-wrap-content th,
    .table-wrap-content td {
        white-space: normal !important;
        word-wrap: break-word;
        overflow-wrap: break-word;
    }
    
    /* Truncate long text */
    .table-truncate {
        max-width: 150px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    
    /* Center align */
    .table .text-center-mobile {
        text-align: center;
    }
    
    /* Hide column on mobile */
    .table .d-none-mobile {
        display: none !important;
    }
}

/* ==========================================================================
   Table Animations
   ========================================================================== */

@media (max-width: 767px) {
    /* Fade in animation for cards */
    .mobile-card {
        animation: fadeInUp var(--mobile-transition-base) ease-out;
    }
    
    @keyframes fadeInUp {
        from {
            opacity: 0;
            transform: translateY(10px);
        }
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }
    
    /* Slide in animation for rows */
    .table tbody tr {
        animation: fadeIn var(--mobile-transition-fast) ease-out;
    }
    
    @keyframes fadeIn {
        from {
            opacity: 0;
        }
        to {
            opacity: 1;
        }
    }
}
