/**
 * Clean Bootstrap 5 Responsive Tables
 * 
 * Enhanced table styling that works with Bootstrap 5 display utilities for responsive behavior.
 * Bootstrap handles responsive column hiding automatically - this adds polish and usability.
 * 
 * Bootstrap 5 Breakpoints (handled automatically by Bootstrap):
 * - d-none d-sm-table-cell (≥576px)
 * - d-none d-md-table-cell (≥768px)  
 * - d-none d-lg-table-cell (≥992px)
 * - d-none d-xl-table-cell (≥1200px)
 * - d-none d-xxl-table-cell (≥1400px)
 */

/* ======================================
   GENERAL TABLE ENHANCEMENTS
   ====================================== */

/* Enhanced table wrapper with subtle styling */
.table-responsive-wrapper {
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
    background: var(--bs-body-bg, #fff);
    margin-bottom: 2rem;
}

/* Grid view container styling to match wrapper */
.table-responsive-wrapper .grid-view {
    border-radius: 8px;
    overflow: hidden;
    background: inherit;
    border: none;
}

/* Modern table styling - Target both responsive-table and Bootstrap table classes */
.table-responsive-wrapper .responsive-table,
.table-responsive-wrapper .table {
    margin-bottom: 0;
    border-radius: 0;
    border: none; /* Remove table border since wrapper provides it */
    --bs-table-striped-bg: rgba(0, 0, 0, 0.02); /* Lighter striping for better contrast */
    --bs-table-hover-bg: rgba(0, 0, 0, 0.04); /* Subtle hover effect */
}

/* Enhanced header styling with softer borders */
.table-responsive-wrapper .responsive-table thead th,
.table-responsive-wrapper .table thead th {
    background: var(--bs-light, #f8f9fa);
    border-bottom: 2px solid var(--bs-border-color-translucent, rgba(0,0,0,0.12));
    border-left: none; /* Remove left borders for cleaner look */
    border-right: none; /* Remove right borders for cleaner look */
    font-weight: 600;
    font-size: 0.875rem;
    text-transform: uppercase;
    letter-spacing: 0.025em;
    color: var(--bs-gray-700, #495057);
    padding: 12px 16px;
    vertical-align: middle;
    position: sticky;
    top: 0;
    z-index: 10;
    border-top: none; /* Remove top border since wrapper provides it */
}

/* First header cell gets top-left radius */
.table-responsive-wrapper .responsive-table thead tr:first-child th:first-child,
.table-responsive-wrapper .table thead tr:first-child th:first-child {
    border-top-left-radius: 7px;
}

/* Last header cell gets top-right radius */
.table-responsive-wrapper .responsive-table thead tr:first-child th:last-child,
.table-responsive-wrapper .table thead tr:first-child th:last-child {
    border-top-right-radius: 7px;
}

/* Enhanced cell styling with softer borders */
.table-responsive-wrapper .responsive-table tbody td,
.table-responsive-wrapper .table tbody td {
    padding: 12px 16px;
    vertical-align: middle;
    border-bottom: 1px solid var(--bs-border-color-translucent, rgba(0,0,0,0.08));
    border-left: none; /* Remove left borders for cleaner look */
    border-right: none; /* Remove right borders for cleaner look */
}

/* Last row cells get bottom border radius and remove border */
.table-responsive-wrapper .responsive-table tbody tr:last-child td:first-child,
.table-responsive-wrapper .table tbody tr:last-child td:first-child {
    border-bottom-left-radius: 7px;
}

.table-responsive-wrapper .responsive-table tbody tr:last-child td:last-child,
.table-responsive-wrapper .table tbody tr:last-child td:last-child {
    border-bottom-right-radius: 7px;
}

/* Remove bottom border from last row since wrapper provides it */
.table-responsive-wrapper .responsive-table tbody tr:last-child td,
.table-responsive-wrapper .table tbody tr:last-child td {
    border-bottom: none;
}

/* Override Bootstrap's default striped and hover styles for better integration */
.table-responsive-wrapper .table-striped > tbody > tr:nth-of-type(odd) > td,
.table-responsive-wrapper .table-striped > tbody > tr:nth-of-type(odd) > th {
    background-color: var(--bs-table-striped-bg, rgba(0,0,0,0.02));
}

.table-responsive-wrapper .table-hover > tbody > tr:hover > td,
.table-responsive-wrapper .table-hover > tbody > tr:hover > th {
    background-color: var(--bs-table-hover-bg, rgba(0,0,0,0.04));
}

/* Selected row styling */
.table-responsive-wrapper .table > tbody > tr.selected > td,
.table-responsive-wrapper .table > tbody > tr.selected > th,
.table-responsive-wrapper .responsive-table tbody tr.selected td {
    background-color: rgba(var(--bs-primary-rgb, 13, 110, 253), 0.1) !important;
    border-color: rgba(var(--bs-primary-rgb, 13, 110, 253), 0.2);
}

/* Selected row hover state - darker when hovering over selected row */
.table-responsive-wrapper .table > tbody > tr.selected:hover > td,
.table-responsive-wrapper .table > tbody > tr.selected:hover > th,
.table-responsive-wrapper .responsive-table tbody tr.selected:hover td {
    background-color: rgba(var(--bs-primary-rgb, 13, 110, 253), 0.15) !important;
}

/* Remove Bootstrap's default table borders for cleaner look */
.table-responsive-wrapper .table-bordered th,
.table-responsive-wrapper .table-bordered td {
    border-left: none;
    border-right: none;
}

.table-responsive-wrapper .table-bordered {
    border: none; /* Remove outer border since wrapper provides it */
}

/* Bootstrap's .table-bordered adds `border-width: 1px 0` to each <tr> via
   `.table-bordered > :not(caption) > *`, which (combined with reboot's
   `border-style: solid`) draws horizontal lines bracketing the table. Row
   separation already comes from per-cell bottom borders, so zero out the
   tr-level border-width here. */
.table-responsive-wrapper .table-bordered > :not(caption) > * {
    border-width: 0;
}

/* Ensure proper background inheritance for Bootstrap classes */
.table-responsive-wrapper .table {
    background-color: transparent; /* Let wrapper handle background */
}

/* Fix Bootstrap's table-striped to work better with our design */
.table-responsive-wrapper .table > :not(caption) > * > * {
    padding: 12px 16px; /* Consistent padding */
    border-bottom-width: 1px;
    box-shadow: none; /* Remove Bootstrap's default box-shadow */
}

/* Filter row styling with softer borders */
.table-responsive-wrapper .responsive-table tr.filters td,
.table-responsive-wrapper .table tr.filters td {
    background: var(--bs-gray-50, #f8f9fa);
    border-bottom: 1px solid var(--bs-border-color-translucent, rgba(0,0,0,0.08));
    border-left: none;
    border-right: none;
    padding: 8px 16px;
}

.table-responsive-wrapper .responsive-table tr.filters input,
.table-responsive-wrapper .responsive-table tr.filters select,
.table-responsive-wrapper .table tr.filters input,
.table-responsive-wrapper .table tr.filters select {
    font-size: 0.875rem;
    padding: 4px 8px;
    border-radius: 4px;
    border: 1px solid var(--bs-border-color, #dee2e6);
    background: var(--bs-body-bg, #fff);
}

/* Action buttons in tables */
.table-responsive-wrapper .responsive-table .btn-group .btn,
.table-responsive-wrapper .table .btn-group .btn {
    min-height: 30px; /* Ensure consistent button height */
    font-size: 0.875rem; /* Slightly larger for better readability - override too-small 0.8125rem */
    border-radius: 4px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

/* Dropdown toggle buttons need specific styling */
.table-responsive-wrapper .responsive-table .btn-group .dropdown-toggle,
.table-responsive-wrapper .table .btn-group .dropdown-toggle {
    min-width: 30px; /* Ensure square-ish appearance for icon-only buttons */
}

/* Loading state styling */
.table-responsive-wrapper.loading {
    opacity: 0.7;
    pointer-events: none;
}

.table-responsive-wrapper.loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 20px;
    height: 20px;
    margin: -10px 0 0 -10px;
    border: 2px solid var(--bs-primary, #0d6efd);
    border-top-color: transparent;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

/* Dark mode enhancements */
[data-bs-theme="dark"] .table-responsive-wrapper {
    background: var(--bs-dark, #212529);
    --bs-table-striped-bg: rgba(255, 255, 255, 0.03);
    --bs-table-hover-bg: rgba(255, 255, 255, 0.08);
}

[data-bs-theme="dark"] .table-responsive-wrapper .responsive-table,
[data-bs-theme="dark"] .table-responsive-wrapper .table {
    --bs-table-striped-bg: rgba(255, 255, 255, 0.03);
    --bs-table-hover-bg: rgba(255, 255, 255, 0.08);
}

[data-bs-theme="dark"] .table-responsive-wrapper .responsive-table thead th,
[data-bs-theme="dark"] .table-responsive-wrapper .table thead th {
    background: var(--bs-gray-800, #343a40);
    color: var(--bs-gray-100, #f8f9fa);
    border-color: var(--bs-border-color, #495057);
}

/* Dark mode striped rows */
[data-bs-theme="dark"] .table-responsive-wrapper .table-striped > tbody > tr:nth-of-type(odd) > td,
[data-bs-theme="dark"] .table-responsive-wrapper .table-striped > tbody > tr:nth-of-type(odd) > th,
[data-bs-theme="dark"] .table-responsive-wrapper .responsive-table tbody tr:nth-child(odd) td {
    background-color: rgba(255, 255, 255, 0.03) !important;
}

/* Dark mode hover effect */
[data-bs-theme="dark"] .table-responsive-wrapper .table-hover > tbody > tr:hover > td,
[data-bs-theme="dark"] .table-responsive-wrapper .table-hover > tbody > tr:hover > th,
[data-bs-theme="dark"] .table-responsive-wrapper .responsive-table tbody tr:hover td,
[data-bs-theme="dark"] .table-responsive-wrapper .table tbody tr:hover td {
    background-color: rgba(255, 255, 255, 0.08) !important;
}

/* Dark mode selected row styling */
[data-bs-theme="dark"] .table-responsive-wrapper .table > tbody > tr.selected > td,
[data-bs-theme="dark"] .table-responsive-wrapper .table > tbody > tr.selected > th,
[data-bs-theme="dark"] .table-responsive-wrapper .responsive-table tbody tr.selected td {
    background-color: rgba(var(--bs-primary-rgb, 13, 110, 253), 0.2) !important;
    border-color: rgba(var(--bs-primary-rgb, 13, 110, 253), 0.3);
}

/* Dark mode selected row hover state */
[data-bs-theme="dark"] .table-responsive-wrapper .table > tbody > tr.selected:hover > td,
[data-bs-theme="dark"] .table-responsive-wrapper .table > tbody > tr.selected:hover > th,
[data-bs-theme="dark"] .table-responsive-wrapper .responsive-table tbody tr.selected:hover td {
    background-color: rgba(var(--bs-primary-rgb, 13, 110, 253), 0.25) !important;
}

[data-bs-theme="dark"] .table-responsive-wrapper .responsive-table tr.filters td,
[data-bs-theme="dark"] .table-responsive-wrapper .table tr.filters td {
    background: var(--bs-gray-800, #343a40);
}

/* ======================================
   RESPONSIVE BEHAVIOR
   ====================================== */

/* Fix Bootstrap's table-responsive overflow issue that hides action buttons */
.table-responsive {
    overflow-x: visible !important;
    -webkit-overflow-scrolling: touch;
}

/* User-hidden columns (manual toggles override Bootstrap) */
.table-responsive .user-hidden,
.table-responsive-wrapper .user-hidden,
.responsive-table .user-hidden {
    display: none !important;
}

/* ======================================
   TABLE CONTROLS STYLING
   ====================================== */

/* Column visibility controls */
.responsive-table-controls {
    position: relative;
}

.responsive-table-controls .dropdown-menu {
    min-width: 280px;
    max-height: 400px;
    overflow-y: auto;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    border: 1px solid var(--bs-border-color, #dee2e6);
}

.responsive-table-controls .dropdown-header {
    background: var(--bs-primary, #0d6efd);
    color: white;
    margin: -8px -12px 8px -12px;
    padding: 8px 12px;
    font-weight: 600;
    font-size: 0.875rem;
    border-radius: 7px 7px 0 0;
}

.responsive-table-controls .column-checkboxes {
    max-height: 250px;
    overflow-y: auto;
}

.responsive-table-controls .form-check {
    padding: 4px 0;
    margin: 0;
    border-bottom: 1px solid var(--bs-border-color-translucent, rgba(0,0,0,0.05));
}

.responsive-table-controls .form-check:last-child {
    border-bottom: none;
}

.responsive-table-controls .form-check-label {
    cursor: pointer;
    font-size: 0.875rem;
    color: var(--bs-body-color, #212529);
    padding-left: 4px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
}

.responsive-table-controls .form-check-label small {
    font-size: 0.75rem;
    opacity: 0.7;
    font-style: italic;
}

.responsive-table-controls .form-check-input {
    margin-top: 0;
}

.responsive-table-controls .form-check-input:checked {
    background-color: var(--bs-primary, #0d6efd);
    border-color: var(--bs-primary, #0d6efd);
}

/* Reset button styling */
.responsive-table-controls .btn-outline-secondary {
    font-size: 0.8125rem;
    padding: 6px 12px;
    border-radius: 6px;
    transition: all 0.15s ease-in-out;
}

.responsive-table-controls .btn-outline-secondary:hover {
    transform: translateY(-1px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* Dark mode for controls */
[data-bs-theme="dark"] .responsive-table-controls .dropdown-menu {
    background: var(--bs-dark, #212529);
    border-color: var(--bs-border-color, #495057);
}

[data-bs-theme="dark"] .responsive-table-controls .form-check {
    border-color: var(--bs-border-color-translucent, rgba(255,255,255,0.1));
}

[data-bs-theme="dark"] .responsive-table-controls .form-check-label {
    color: var(--bs-body-color, #f8f9fa);
}

/* ======================================
   USER PREFERENCE OVERRIDES
   ====================================== */

/* Mobile Card Layout - Transform table into cards on very small screens */
@media screen and (max-width: 767px) {
    .table-responsive-wrapper .responsive-table,
    .table-responsive-wrapper .responsive-table thead,
    .table-responsive-wrapper .responsive-table tbody,
    .table-responsive-wrapper .responsive-table th,
    .table-responsive-wrapper .responsive-table td,
    .table-responsive-wrapper .responsive-table tr {
        display: block !important;
    }
    
    /* Hide table headers.

       Off the TOP only. `left: -9999px !important` sat here too, and it is
       what made a grid in an RTL locale scroll roughly ten thousand pixels
       sideways. The block axis does not flip with direction, so content above
       the origin is unreachable either way; the INLINE axis does, so content
       parked at inline-start becomes real scrollable overflow the moment that
       axis points the other way. Measured on /people at 606px: 0px of
       horizontal overflow under LTR, 9978px under RTL, same markup.

       Dropping the declaration rather than making it logical
       (`inset-inline-start`) is deliberate — a logical offset would mirror
       correctly and still put 9999px of content on the scrollable side. The
       row does not need to move horizontally at all.

       Still positioned off-screen rather than display:none, because the intent
       is to keep the header row in the accessibility tree while the cells
       carry their own labels in the card layout. */
    .table-responsive-wrapper .responsive-table thead tr {
        position: absolute !important;
        top: -9999px !important;
    }
    
    /* Style each row as a card */
    .table-responsive-wrapper .responsive-table tr {
        border: 1px solid var(--bs-border-color, #dee2e6) !important;
        border-radius: 8px !important;
        margin-bottom: 15px !important;
        padding: 15px !important;
        background: var(--bs-body-bg, white) !important;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1) !important;
    }
    
    /* Style each cell */
    .table-responsive-wrapper .responsive-table td {
        border: none !important;
        position: relative !important;
        padding-left: 50% !important;
        padding-top: 8px !important;
        padding-bottom: 8px !important;
        padding-right: 10px !important;
        white-space: normal !important;
        text-align: left !important;
    }
    
    /* Add labels using data-title */
    .table-responsive-wrapper .responsive-table td:before {
        content: attr(data-title) !important;
        position: absolute !important;
        left: 6px !important;
        width: 45% !important;
        padding-right: 10px !important;
        white-space: nowrap !important;
        font-weight: 600 !important;
        color: var(--bs-secondary, #6c757d) !important;
        font-size: 0.875rem !important;
    }
    
    /* Special handling for action buttons */
    .table-responsive-wrapper .responsive-table td.actions-column,
    .table-responsive-wrapper .responsive-table td.button-column {
        padding-left: 10px !important;
        text-align: center !important;
    }
    
    .table-responsive-wrapper .responsive-table td.actions-column:before,
    .table-responsive-wrapper .responsive-table td.button-column:before {
        content: "" !important;
    }
}

/* Dark mode for mobile cards */
@media screen and (max-width: 767px) {
    [data-bs-theme="dark"] .table-responsive-wrapper .responsive-table tr {
        background: var(--bs-dark, #212529) !important;
        border-color: var(--bs-border-color, #495057) !important;
        color: var(--bs-body-color, #fff) !important;
    }
    
    [data-bs-theme="dark"] .table-responsive-wrapper .responsive-table td:before {
        color: var(--bs-secondary, #adb5bd) !important;
    }
}