/**
 * Unified Data Table Component
 * AgentTech Earn - Consistent table styling across the platform
 * Date: October 28, 2025
 */

/* ==========================================
   CONTAINER & WRAPPER
   ========================================== */

.unified-table-container {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 2px 8px var(--card-shadow);
    margin-bottom: 2rem;
}

/* ==========================================
   TABLE TOOLBAR
   ========================================== */

.table-toolbar {
    padding: 1rem 1.25rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    border-bottom: 1px solid var(--card-border);
    flex-wrap: wrap;
}

.toolbar-left {
    display: flex;
    align-items: center;
    gap: 1rem;
    flex: 1;
    min-width: 0;
}

.toolbar-right {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

/* Table Search */
.table-search {
    position: relative;
    flex: 1;
    max-width: 300px;
}

.table-search input {
    width: 100%;
    padding: 0.5rem 0.75rem 0.5rem 2.5rem;
    border: 1px solid var(--input-border);
    border-radius: 6px;
    background: var(--input-bg);
    color: var(--input-color);
    font-size: 0.875rem;
    transition: border-color 0.2s;
}

.table-search input:focus {
    outline: none;
    border-color: var(--input-focus-border);
}

.table-search i {
    position: absolute;
    left: 0.75rem;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
    font-size: 0.875rem;
}

/* Table Info */
.table-info {
    color: var(--text-muted);
    font-size: 0.875rem;
    white-space: nowrap;
}

/* Bulk Actions */
.bulk-actions {
    display: none;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem;
    background: rgba(var(--primary-rgb), 0.1);
    border-radius: 6px;
}

.bulk-actions.active {
    display: flex;
}

.bulk-actions-text {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--primary-bg);
}

/* ==========================================
   TABLE STYLES
   ========================================== */

.table-responsive {
    overflow-x: auto;
}

.unified-data-table {
    width: 100%;
    margin: 0;
    border-collapse: collapse;
    table-layout: auto;
}

/* Column width management for uniform appearance */
.unified-data-table th,
.unified-data-table td {
    min-width: 80px;
}

/* Specific column width classes */
.unified-data-table .col-agency {
    width: 20%;
    min-width: 150px;
}

.unified-data-table .col-numeric {
    width: 12%;
    min-width: 110px;
}

.unified-data-table .col-performance {
    width: 14%;
    min-width: 130px;
}

/* Table Header */
.unified-data-table thead {
    background: var(--table-header-bg);
    border-bottom: 2px solid var(--card-border);
}

.unified-data-table thead th {
    font-weight: 600;
    text-transform: uppercase;
    font-size: 0.7rem;
    letter-spacing: 0.8px;
    padding: 1rem;
    border-bottom: 2px solid var(--card-border);
    color: var(--text-secondary);
    white-space: nowrap;
    position: relative;
}

/* Table Body */
.unified-data-table tbody tr {
    border-bottom: 1px solid var(--card-border);
    transition: background-color 0.15s ease, box-shadow 0.15s ease;
}

.unified-data-table tbody tr:last-child {
    border-bottom: 1px solid var(--card-border);
}

.unified-data-table tbody tr:hover {
    background: var(--table-hover);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.unified-data-table tbody tr.selected {
    background: rgba(var(--primary-rgb), 0.1);
}

.unified-data-table tbody td {
    padding: 1rem;
    color: var(--body-color);
    font-size: 0.875rem;
    vertical-align: middle;
}

.unified-data-table tbody td:first-child {
    font-weight: 600;
}

/* Table Footer (Totals Row) */
.unified-data-table tfoot {
    background: linear-gradient(135deg, rgba(52, 73, 94, 0.95) 0%, rgba(44, 62, 80, 0.95) 100%);
    border-top: 3px solid var(--primary-bg);
}

.unified-data-table tfoot tr {
    border: none;
}

.unified-data-table tfoot td,
.unified-data-table tfoot th {
    padding: 1.25rem 1rem;
    font-weight: 700;
    font-size: 0.95rem;
    color: #ffffff;
    border: none;
    vertical-align: middle;
}

.unified-data-table tfoot strong {
    font-size: 1.05rem;
    letter-spacing: 0.3px;
}

/* ==========================================
   SORTABLE COLUMNS
   ========================================== */

.sortable {
    cursor: pointer;
    user-select: none;
    position: relative;
    padding-right: 2rem !important;
}

.sortable:hover {
    background: var(--table-hover);
}

.sortable .sort-icon {
    position: absolute;
    right: 0.75rem;
    top: 50%;
    transform: translateY(-50%);
    opacity: 0.3;
    transition: opacity 0.2s;
    font-size: 0.7rem;
}

.sortable:hover .sort-icon {
    opacity: 0.6;
}

.sortable.sorted-asc .sort-icon,
.sortable.sorted-desc .sort-icon {
    opacity: 1;
    color: var(--primary-bg);
}

.sortable.sorted-asc .sort-icon::before {
    content: "\f0de"; /* fa-sort-up */
}

.sortable.sorted-desc .sort-icon::before {
    content: "\f0dd"; /* fa-sort-down */
}

/* ==========================================
   SPECIAL COLUMNS
   ========================================== */

/* Checkbox Column */
.checkbox-column {
    width: 48px;
    text-align: center;
}

.checkbox-column input[type="checkbox"] {
    cursor: pointer;
    width: 16px;
    height: 16px;
}

/* Actions Column */
.actions-column {
    width: auto;
    text-align: right;
    white-space: nowrap;
}

.actions-column .btn {
    padding: 0.375rem 0.75rem;
    font-size: 0.75rem;
    margin-left: 0.25rem;
}

.actions-column .btn i {
    font-size: 0.75rem;
}

/* Status Badges */
.status-badge {
    display: inline-block;
    padding: 0.25rem 0.75rem;
    border-radius: 12px;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.status-badge.success {
    background: var(--alert-success-bg);
    color: var(--alert-success-color);
}

.status-badge.warning {
    background: var(--alert-warning-bg);
    color: var(--alert-warning-color);
}

.status-badge.danger {
    background: var(--alert-danger-bg);
    color: var(--alert-danger-color);
}

.status-badge.info {
    background: var(--alert-info-bg);
    color: var(--alert-info-color);
}

/* ==========================================
   PAGINATION
   ========================================== */

.table-pagination {
    padding: 1rem 1.25rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    flex-wrap: wrap;
}

.pagination-info {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.per-page-select {
    width: auto;
    min-width: 140px;
}

.pagination {
    margin: 0;
}

.pagination .page-link {
    color: var(--body-color);
    background: var(--card-bg);
    border-color: var(--card-border);
    padding: 0.375rem 0.75rem;
}

.pagination .page-link:hover {
    background: var(--table-hover);
    border-color: var(--card-border);
}

.pagination .page-item.active .page-link {
    background: var(--primary-bg);
    border-color: var(--primary-bg);
    color: white;
}

.pagination .page-item.disabled .page-link {
    opacity: 0.5;
    pointer-events: none;
}

/* ==========================================
   LOADING & EMPTY STATES
   ========================================== */

.table-loading {
    padding: 3rem;
    text-align: center;
}

.table-loading .spinner {
    display: inline-block;
    width: 2rem;
    height: 2rem;
    border: 3px solid var(--card-border);
    border-top-color: var(--primary-bg);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.table-empty {
    padding: 3rem;
    text-align: center;
}

.table-empty i {
    font-size: 3rem;
    color: var(--text-muted);
    margin-bottom: 1rem;
    display: block;
}

.table-empty h5 {
    color: var(--body-color);
    margin-bottom: 0.5rem;
}

.table-empty p {
    color: var(--text-muted);
    margin-bottom: 0;
}

/* ==========================================
   RESPONSIVE DESIGN
   ========================================== */

@media (max-width: 768px) {
    .table-toolbar {
        flex-direction: column;
        align-items: stretch;
    }

    .toolbar-left,
    .toolbar-right {
        width: 100%;
        justify-content: space-between;
    }

    .table-search {
        max-width: none;
    }

    .table-pagination {
        flex-direction: column;
        align-items: stretch;
    }

    .pagination-info {
        justify-content: space-between;
    }

    .pagination {
        justify-content: center;
    }

    /* Stack table on mobile if needed */
    .unified-data-table.stack-mobile thead {
        display: none;
    }

    .unified-data-table.stack-mobile tbody tr {
        display: block;
        margin-bottom: 1rem;
        border: 1px solid var(--card-border);
        border-radius: 8px;
    }

    .unified-data-table.stack-mobile tbody td {
        display: block;
        text-align: right;
        padding: 0.75rem 1rem;
        border-bottom: 1px solid var(--card-border);
    }

    .unified-data-table.stack-mobile tbody td:last-child {
        border-bottom: none;
    }

    .unified-data-table.stack-mobile tbody td::before {
        content: attr(data-label);
        float: left;
        font-weight: 600;
        text-transform: uppercase;
        font-size: 0.75rem;
        color: var(--text-secondary);
    }

    .unified-data-table.stack-mobile .checkbox-column,
    .unified-data-table.stack-mobile .actions-column {
        text-align: right;
    }
}

/* ==========================================
   EXPORT DROPDOWN
   ========================================== */

.export-dropdown {
    position: relative;
}

.export-menu {
    position: absolute;
    top: 100%;
    right: 0;
    margin-top: 0.5rem;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 8px;
    box-shadow: 0 4px 12px var(--card-shadow);
    min-width: 160px;
    z-index: 1000;
    display: none;
}

.export-menu.show {
    display: block;
}

.export-menu button {
    display: block;
    width: 100%;
    padding: 0.75rem 1rem;
    border: none;
    background: none;
    color: var(--body-color);
    text-align: left;
    cursor: pointer;
    transition: background 0.2s;
}

.export-menu button:hover {
    background: var(--table-hover);
}

.export-menu button i {
    width: 1.5rem;
    margin-right: 0.5rem;
}

/* ==========================================
   COLUMN TOGGLE
   ========================================== */

.column-toggle-menu {
    position: absolute;
    top: 100%;
    right: 0;
    margin-top: 0.5rem;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 8px;
    box-shadow: 0 4px 12px var(--card-shadow);
    min-width: 200px;
    padding: 0.5rem;
    z-index: 1000;
    display: none;
}

.column-toggle-menu.show {
    display: block;
}

.column-toggle-item {
    display: flex;
    align-items: center;
    padding: 0.5rem;
    cursor: pointer;
    border-radius: 4px;
    transition: background 0.2s;
}

.column-toggle-item:hover {
    background: var(--table-hover);
}

.column-toggle-item input {
    margin-right: 0.75rem;
}

.column-toggle-item label {
    margin: 0;
    cursor: pointer;
    flex: 1;
}

