/**
 * Responsive Tables
 * Table to card transformation for mobile devices
 */

/* ============================================
   Desktop Table Styles (Default)
   ============================================ */

.data-table {
    width: 100%;
    border-collapse: collapse;
    background: var(--white, #ffffff);
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.data-table thead {
    background: var(--gray-50, #f9fafb);
}

.data-table th {
    padding: 0.75rem 1rem;
    text-align: left;
    font-weight: 600;
    color: var(--gray-700, #374151);
    font-size: 0.875rem;
    border-bottom: 2px solid var(--gray-200, #e5e7eb);
}

.data-table td {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--gray-200, #e5e7eb);
    color: var(--gray-900, #111827);
    font-size: 0.875rem;
}

.data-table tbody tr:hover {
    background: var(--gray-50, #f9fafb);
}

/* ============================================
   Mobile Card Layout (< 768px)
   ============================================ */

@media (max-width: 767px) {
    /* Hide table header */
    .data-table thead {
        display: none;
    }
    
    /* Make table, tbody, tr, td block elements */
    .data-table,
    .data-table tbody,
    .data-table tr,
    .data-table td {
        display: block;
        width: 100%;
    }
    
    /* Style each row as a card */
    .data-table tr {
        background: var(--white, #ffffff);
        border: 1px solid var(--gray-200, #e5e7eb);
        border-radius: 8px;
        padding: 1rem;
        margin-bottom: 1rem;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
        transition: box-shadow 0.2s ease;
    }
    
    .data-table tr:hover {
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    }
    
    .data-table tr:last-child {
        margin-bottom: 0;
    }
    
    /* Style cells with labels */
    .data-table td {
        padding: 0.5rem 0;
        border: none;
        text-align: left;
        position: relative;
        padding-left: 50%;
        min-height: 44px; /* Touch-friendly */
        display: flex;
        align-items: center;
        font-size: 14px; /* Minimum 14px for readability - Requirement 5.5 */
    }
    
    /* Add data labels before each cell */
    .data-table td::before {
        content: attr(data-label);
        position: absolute;
        left: 0;
        width: 45%;
        padding-right: 10px;
        font-weight: 600;
        color: var(--gray-600, #4b5563);
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        font-size: 13px; /* Slightly smaller for labels */
    }
    
    /* First cell in row - make it prominent like a card title */
    .data-table td:first-child {
        font-weight: 600;
        color: var(--gray-900, #111827);
        padding-top: 0;
        margin-bottom: 0.5rem;
        border-bottom: 1px solid var(--gray-100, #f3f4f6);
        padding-bottom: 0.75rem;
    }
    
    .data-table td:first-child::before {
        font-weight: 500;
    }
    
    /* Actions column styling */
    .data-table td.actions {
        padding-left: 0;
        display: flex;
        gap: 0.5rem; /* 8px minimum spacing - Requirement 9.2 */
        margin-top: 0.5rem;
        flex-wrap: wrap;
    }
    
    .data-table td.actions::before {
        display: none;
    }
    
    /* Ensure action buttons are touch-friendly - Requirement 5.4 */
    .data-table td.actions button,
    .data-table td.actions a {
        min-width: 44px;
        min-height: 44px;
        padding: 0.5rem 1rem;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 0.25rem;
        transition: transform 0.2s ease;
    }
    
    /* Touch feedback */
    .data-table td.actions button:active,
    .data-table td.actions a:active {
        transform: scale(0.98);
    }
    
    /* Status badges */
    .data-table td .badge,
    .data-table td .status-badge {
        display: inline-block;
        padding: 0.25rem 0.75rem;
        border-radius: 12px;
        font-size: 0.75rem;
        font-weight: 600;
        white-space: nowrap;
    }
    
    /* Ensure proper spacing for cells with badges */
    .data-table td:has(.badge),
    .data-table td:has(.status-badge) {
        align-items: flex-start;
        padding-top: 0.75rem;
    }
}

/* ============================================
   Tablet Adjustments (768px - 1023px)
   ============================================ */

@media (min-width: 768px) and (max-width: 1023px) {
    /* Slightly reduce padding on tablet */
    .data-table th,
    .data-table td {
        padding: 0.625rem 0.875rem;
        font-size: 0.8125rem;
    }
}

/* ============================================
   Horizontal Scroll Alternative
   ============================================ */

/* For tables that must remain tables on mobile */
.table-scroll-wrapper {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin-bottom: 1rem;
}

@media (max-width: 767px) {
    .table-scroll-wrapper {
        /* Add scroll indicator - Requirement 5.3 */
        position: relative;
        border-radius: 8px;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    }
    
    /* Right edge gradient indicator */
    .table-scroll-wrapper::after {
        content: '';
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        width: 30px;
        background: linear-gradient(to left, rgba(255, 255, 255, 0.9), transparent);
        pointer-events: none;
        transition: opacity 0.3s ease;
    }
    
    /* Hide indicator when scrolled to end */
    .table-scroll-wrapper.scrolled-end::after {
        opacity: 0;
    }
    
    /* Left edge gradient indicator when scrolled */
    .table-scroll-wrapper.scrolled::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        width: 30px;
        background: linear-gradient(to right, rgba(255, 255, 255, 0.9), transparent);
        pointer-events: none;
        z-index: 1;
    }
}

/* Keep table structure for scrollable tables */
.table-scroll-wrapper .data-table {
    display: table;
    min-width: 600px; /* Minimum table width */
}

.table-scroll-wrapper .data-table thead {
    display: table-header-group;
}

.table-scroll-wrapper .data-table tbody {
    display: table-row-group;
}

.table-scroll-wrapper .data-table tr {
    display: table-row;
}

.table-scroll-wrapper .data-table td,
.table-scroll-wrapper .data-table th {
    display: table-cell;
}

/* ============================================
   Empty State
   ============================================ */

.table-empty-state {
    padding: 3rem 1rem;
    text-align: center;
    color: var(--gray-500, #6b7280);
}

@media (max-width: 767px) {
    .table-empty-state {
        padding: 2rem 1rem;
    }
}

/* ============================================
   Loading State
   ============================================ */

.table-loading {
    padding: 2rem;
    text-align: center;
}

.table-loading-spinner {
    display: inline-block;
    width: 40px;
    height: 40px;
    border: 4px solid var(--gray-200, #e5e7eb);
    border-top-color: var(--primary-color, #6366f1);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* ============================================
   Sortable Table Headers
   ============================================ */

.data-table th.sortable {
    cursor: pointer;
    user-select: none;
    position: relative;
    padding-right: 2rem;
}

.data-table th.sortable::after {
    content: '\f0dc'; /* Font Awesome sort icon */
    font-family: 'Font Awesome 6 Free';
    font-weight: 900;
    position: absolute;
    right: 0.5rem;
    opacity: 0.3;
}

.data-table th.sortable.asc::after {
    content: '\f0de'; /* Sort up */
    opacity: 1;
}

.data-table th.sortable.desc::after {
    content: '\f0dd'; /* Sort down */
    opacity: 1;
}

/* Mobile: Hide sort indicators in card view */
@media (max-width: 767px) {
    .data-table th.sortable::after {
        display: none;
    }
}

/* ============================================
   Pagination
   ============================================ */

.table-pagination {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    flex-wrap: wrap;
    gap: 1rem;
}

.pagination-info {
    color: var(--gray-600, #4b5563);
    font-size: 0.875rem;
}

.pagination-controls {
    display: flex;
    gap: 0.5rem;
}

.pagination-controls button {
    min-width: 44px;
    min-height: 44px;
    padding: 0.5rem 1rem;
}

@media (max-width: 767px) {
    .table-pagination {
        flex-direction: column;
        align-items: stretch;
    }
    
    .pagination-controls {
        justify-content: center;
    }
}
