/**
 * Responsive Tables - Mobile-Friendly Data Views
 * Version: 1.0
 * 
 * Converts standard HTML tables to mobile-optimized layouts:
 * - Card view for simple tables on small screens
 * - Horizontal scroll container for complex tables
 * - Sticky headers for better context
 * - Touch-friendly controls
 */

/* ========================================
   RESPONSIVE TABLE UTILITIES
   ======================================== */

/* Wrapper for tables that need horizontal scroll on mobile */
.table-responsive-mobile {
    position: relative;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
    border-radius: 8px;
    margin-bottom: 1rem;
}

/* Scroll indicator shadows */
.table-responsive-mobile::before,
.table-responsive-mobile::after {
    content: '';
    position: absolute;
    top: 0;
    bottom: 0;
    width: 30px;
    pointer-events: none;
    z-index: 2;
    transition: opacity 0.3s;
}

.table-responsive-mobile::before {
    left: 0;
    background: linear-gradient(to right, rgba(255,255,255,0.9), transparent);
}

.table-responsive-mobile::after {
    right: 0;
    background: linear-gradient(to left, rgba(255,255,255,0.9), transparent);
}

/* ========================================
   DESKTOP STYLES (Default)
   ======================================== */

.table-mobile-optimized {
    width: 100%;
    margin-bottom: 1rem;
    background-color: white;
    border-collapse: collapse;
}

.table-mobile-optimized thead th {
    background-color: var(--color-primary-500, #0066FF);
    color: white;
    font-weight: 600;
    padding: 12px 16px;
    text-align: left;
    border-bottom: 2px solid var(--color-primary-600, #1E88E5);
    position: sticky;
    top: 0;
    z-index: 10;
}

.table-mobile-optimized tbody tr {
    border-bottom: 1px solid #e0e0e0;
    transition: background-color 0.2s;
}

.table-mobile-optimized tbody tr:hover {
    background-color: #f5f5f5;
}

.table-mobile-optimized tbody td {
    padding: 12px 16px;
    vertical-align: middle;
}

/* Alternating row colors for better readability */
.table-mobile-optimized tbody tr:nth-child(even) {
    background-color: #fafafa;
}

.table-mobile-optimized tbody tr:nth-child(even):hover {
    background-color: #f0f0f0;
}

/* ========================================
   MOBILE STYLES (<768px)
   ======================================== */

@media (max-width: 767px) {
    
    /* Card-based layout for simple tables */
    .table-card-mobile {
        display: block;
        width: 100%;
    }
    
    .table-card-mobile thead {
        display: none; /* Hide headers on mobile */
    }
    
    .table-card-mobile tbody {
        display: block;
    }
    
    .table-card-mobile tbody tr {
        display: block;
        margin-bottom: 16px;
        background: white;
        border-radius: 12px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        border: 1px solid #e0e0e0;
        padding: 16px;
        border-bottom: none;
    }
    
    .table-card-mobile tbody tr:hover {
        box-shadow: 0 4px 16px rgba(0,0,0,0.15);
        transform: translateY(-2px);
        transition: all 0.2s;
    }
    
    .table-card-mobile tbody td {
        display: block;
        text-align: right;
        padding: 8px 0;
        border: none;
        position: relative;
        padding-left: 50%;
    }
    
    /* Data labels (from headers) */
    .table-card-mobile tbody td::before {
        content: attr(data-label);
        position: absolute;
        left: 0;
        width: 45%;
        padding-right: 10px;
        text-align: left;
        font-weight: 600;
        color: var(--color-primary-700, #1976D2);
    }
    
    .table-card-mobile tbody td:first-child {
        padding-top: 0;
        font-weight: 600;
        font-size: 1.1em;
        color: var(--color-primary-600, #1E88E5);
    }
    
    .table-card-mobile tbody td:last-child {
        padding-bottom: 0;
        border-bottom: none;
    }
    
    /* Horizontal scroll for complex tables */
    .table-scroll-mobile {
        display: block;
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        white-space: nowrap;
    }
    
    .table-scroll-mobile table {
        min-width: 600px; /* Ensures table doesn't collapse */
    }
    
    .table-scroll-mobile thead th {
        position: sticky;
        top: 0;
        z-index: 10;
        background-color: var(--color-primary-500, #0066FF);
    }
    
    /* Reduce padding on mobile for space efficiency */
    .table-scroll-mobile tbody td,
    .table-scroll-mobile thead th {
        padding: 8px 12px;
        font-size: 0.9rem;
    }
    
    /* First column sticky for context */
    .table-scroll-mobile tbody td:first-child,
    .table-scroll-mobile thead th:first-child {
        position: sticky;
        left: 0;
        background-color: white;
        z-index: 5;
        box-shadow: 2px 0 4px rgba(0,0,0,0.1);
    }
    
    .table-scroll-mobile thead th:first-child {
        background-color: var(--color-primary-500, #0066FF);
        z-index: 11;
    }
}

/* ========================================
   TABLET STYLES (768px - 1024px)
   ======================================== */

@media (min-width: 768px) and (max-width: 1024px) {
    .table-mobile-optimized tbody td,
    .table-mobile-optimized thead th {
        padding: 10px 14px;
        font-size: 0.95rem;
    }
}

/* ========================================
   TABLE ACTION BUTTONS (Mobile-Optimized)
   ======================================== */

@media (max-width: 767px) {
    .table-actions {
        display: flex;
        flex-direction: column;
        gap: 8px;
        margin-top: 8px;
    }
    
    .table-actions .btn {
        width: 100%;
        justify-content: center;
    }
}

@media (min-width: 768px) {
    .table-actions {
        display: flex;
        gap: 8px;
        flex-wrap: wrap;
    }
}

/* ========================================
   LOADING STATE FOR TABLES
   ======================================== */

.table-loading {
    position: relative;
    min-height: 200px;
}

.table-loading::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255,255,255,0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}

/* ========================================
   SORTABLE TABLE HEADERS
   ======================================== */

.sortable-header {
    cursor: pointer;
    user-select: none;
    position: relative;
    padding-right: 24px !important;
}

.sortable-header:hover {
    background-color: var(--color-primary-600, #1E88E5);
}

.sortable-header::after {
    content: '⇅';
    position: absolute;
    right: 8px;
    opacity: 0.5;
    font-size: 0.8em;
}

.sortable-header.sorted-asc::after {
    content: '↑';
    opacity: 1;
}

.sortable-header.sorted-desc::after {
    content: '↓';
    opacity: 1;
}

/* Touch-friendly tap targets on mobile */
@media (max-width: 767px) {
    .sortable-header {
        min-height: 48px;
        display: flex;
        align-items: center;
    }
}

/* ========================================
   TABLE SEARCH/FILTER (Mobile-Optimized)
   ======================================== */

.table-search-container {
    margin-bottom: 16px;
}

@media (max-width: 767px) {
    .table-search-container {
        position: sticky;
        top: 0;
        z-index: 20;
        background: white;
        padding: 12px 0;
        border-bottom: 2px solid #e0e0e0;
    }
    
    .table-search-container input {
        width: 100%;
        padding: 12px 16px;
        font-size: 16px; /* Prevents iOS zoom on focus */
        border-radius: 8px;
        border: 2px solid #e0e0e0;
    }
    
    .table-search-container input:focus {
        border-color: var(--color-primary-500, #0066FF);
        outline: none;
    }
}

/* ========================================
   EMPTY STATE
   ======================================== */

.table-empty-state {
    text-align: center;
    padding: 48px 24px;
    color: #757575;
}

.table-empty-state i {
    font-size: 48px;
    margin-bottom: 16px;
    opacity: 0.5;
}

.table-empty-state h3 {
    font-size: 1.2rem;
    font-weight: 600;
    margin-bottom: 8px;
}

.table-empty-state p {
    font-size: 0.95rem;
    margin-bottom: 0;
}

/* ========================================
   PAGINATION (Mobile-Optimized)
   ======================================== */

@media (max-width: 767px) {
    .pagination {
        font-size: 0.9rem;
    }
    
    .pagination .page-link {
        padding: 8px 12px;
        min-width: 44px; /* Touch-friendly */
        min-height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    /* Hide page numbers, keep only prev/next */
    .pagination .page-item:not(.previous):not(.next):not(.active) {
        display: none;
    }
}

/* ========================================
   ACCESSIBILITY IMPROVEMENTS
   ======================================== */

/* Screen reader only text for context */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0,0,0,0);
    white-space: nowrap;
    border: 0;
}

/* Focus indicators for keyboard navigation */
.table-mobile-optimized tbody tr:focus-within {
    outline: 2px solid var(--color-primary-500, #0066FF);
    outline-offset: -2px;
}

.table-mobile-optimized a:focus,
.table-mobile-optimized button:focus {
    outline: 2px solid var(--color-accent-500, #FF6F00);
    outline-offset: 2px;
}
