/* Target DataTables 3.1.3 Flexbox column headers */
.dt-container table.dataTable thead>tr>th div.dt-column-header,
.dt-container table.dataTable thead>tr>td div.dt-column-header {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: baseline;
    gap: 12px;
    height: auto;
    min-height: min-content;
}

/* Force layout text to align cleanly on the left wall */
.dt-container table.dataTable thead>tr>th div.dt-column-title,
.dt-container table.dataTable thead>tr>td div.dt-column-title {
    text-align: left;
    flex-grow: 1;
    white-space: normal;
    overflow: visible;
}

/* Pin the standalone icon carat container to the right edge */
.dt-container table.dataTable thead>tr>th .dt-column-order,
.dt-container table.dataTable thead>tr>td .dt-column-order {
    position: relative;
    margin-left: auto;
    order: 2;
    align-self: center;
}

/* ==========================================================================
   STICKY DESKTOP TABLE HEADERS
   ========================================================================== */
@media screen and (min-width: 769px) {
    /* Ensure no ancestor clips sticky positioning */
    .dt-container,
    .dt-layout-table,
    .dt-layout-full,
    .table-responsive-wrapper {
        overflow: visible !important;
    }

    .table-responsive-wrapper table.dataTable thead.sticky-stack-item {
        position: sticky;
        top: 0; /* Dynamically updated by updateStickyStack() in main.js */
        z-index: 90;
        background-color: var(--color-bg-card, #ffffff);
        box-shadow: 0 2px 2px -1px rgba(0, 0, 0, 0.1);
    }

    .table-responsive-wrapper table.dataTable thead.sticky-stack-item th {
        background-color: var(--color-bg-card, #ffffff);
    }
}

/* ==========================================================================
   UNIVERSAL DATA TABLES PORTRAIT MOBILE OVERRIDES (STACKED CARDS ENGINE)
   ========================================================================== */
@media screen and (max-width: 768px) {

    /* 1. Break the table grid and wrapper bounds */
    .table-responsive-wrapper table.dataTable {
        display: block !important;
        width: 100% !important;
        border: none !important;
        table-layout: auto !important;
    }

    /* 2. Hide traditional horizontal desktop headers entirely from view */
    .table-responsive-wrapper table.dataTable thead {
        display: none !important;
    }

    /* 3. Convert rows and row bodies into clean fluid stacks */
    .table-responsive-wrapper table.dataTable tbody,
    .table-responsive-wrapper table.dataTable tbody tr {
        display: block !important;
        width: 100% !important;
    }

    /* 4. Shape each table row into an independent card block */
    .table-responsive-wrapper table.dataTable tbody tr {
        background-color: var(--color-bg-card, #ffffff);
        border: 1px solid var(--color-border, #dee2e6) !important;
        border-radius: 8px;
        margin-bottom: 16px;
        padding: 16px;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.02);
        box-sizing: border-box;
    }

    /* 5. Convert standard cells into full-width vertical block layouts */
    .table-responsive-wrapper table.dataTable tbody td {
        display: flex !important;
        flex-direction: row !important;
        align-items: flex-start !important;
        justify-content: flex-start !important;
        width: 100% !important;
        padding: 8px 0 !important;
        border: none !important;
        box-sizing: border-box;
        text-align: left !important;
        white-space: normal;
    }

    /* 6. Dynamic Label Injection from data-label attribute */
    .table-responsive-wrapper table.dataTable tbody td[data-label]::before {
        content: attr(data-label);
        font-weight: 700;
        color: var(--color-text-muted, #6c757d);
        display: inline-block;
        width: 110px;
        flex-shrink: 0;
        font-size: 0.85rem;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }

    /* 7. Specific font sizing adjustment for role names inside mobile view cards */
    .table-responsive-wrapper table.dataTable tbody td.role-name-cell {
        font-size: 0.9rem !important;
    }

    /* 8. Fix background styling striping while preserving hover/tap feedback */
    .table-responsive-wrapper table.dataTable.stripe tbody tr:nth-child(odd):not(:hover) {
        background-color: var(--color-bg-card, #ffffff) !important;
    }
}

/* Force numeric/date columns to keep left-aligned headers and right-side sort arrows */
table.dataTable thead th.dt-type-numeric .dt-column-header,
table.dataTable thead th.dt-type-date .dt-column-header {
    flex-direction: row;
    text-align: center;
}

table.dataTable tbody td.dt-type-numeric,
table.dataTable tbody td.dt-type-date {
    text-align: center;
}

@media (max-width: 576px) {
    /* --- DataTables 2.x --- */
    .dt-container .dt-layout-row {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 0.75rem;
    }

    .dt-container .dt-layout-cell {
        width: 100% !important;
        justify-content: flex-start !important;
    }

    .dt-container .dt-search {
        display: flex;
        flex-direction: column;
        width: 100%;
    }

    .dt-container .dt-search input {
        width: 100% !important;
        margin-left: 0 !important;
        margin-top: 0.25rem;
    }

    /* --- DataTables 1.x (Legacy / Bootstrap 5) --- */
    .dataTables_wrapper .dataTables_length,
    .dataTables_wrapper .dataTables_filter {
        float: none !important;
        text-align: left !important;
        width: 100%;
        margin-bottom: 0.75rem;
    }

    .dataTables_wrapper .dataTables_filter input {
        width: 100% !important;
        margin-left: 0 !important;
        display: block;
        margin-top: 0.25rem;
    }
}
