/* ========================================
   SGND - Mobile Tables Optimization v2.0
   Unified Card Design for All Tables
   ======================================== */

@media (max-width: 768px) {

    /* 1. Force tables to not look like tables */
    table,
    thead,
    tbody,
    th,
    td,
    tr {
        display: block !important;
    }

    /* 2. Hide headers (but keep accesibility if possible, here we just hide) */
    thead tr {
        position: absolute;
        top: -9999px;
        left: -9999px;
    }

    /* 3. Card Container (The Row) - Flex Column for Reordering */
    tr {
        background: var(--bg-card);
        border: 1px solid rgba(255, 255, 255, 0.08);
        border-radius: var(--radius-lg);
        margin-bottom: var(--space-4);
        box-shadow: var(--shadow-sm);
        padding: var(--space-4);
        position: relative;
        display: flex !important;
        flex-direction: column;
    }

    /* Zebra striping preservation */
    tbody tr:nth-of-type(odd) {
        background: var(--bg-card);
    }

    tbody tr:nth-of-type(even) {
        background: var(--bg-card);
    }

    /* 4. Cell Styling (The Items) */
    td {
        border: none;
        border-bottom: 1px solid rgba(255, 255, 255, 0.04);
        position: relative;
        padding: 8px 0 8px 45% !important;
        /* Right side content */
        text-align: right !important;
        min-height: 24px;
        display: flex !important;
        align-items: center;
        justify-content: flex-end;
        flex-wrap: wrap;
        width: 100%;
        /* Fill row width */
        box-sizing: border-box;
        order: 10;
        /* Default order - put everything else at bottom */
    }

    td:last-child {
        border-bottom: 0;
    }

    /* 5. The Label */
    td:before {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        left: 0;
        width: 40%;
        text-align: left;
        font-weight: 600;
        text-transform: uppercase;
        font-size: 0.7rem;
        color: var(--text-muted);
        content: attr(data-label);
        line-height: 1.2;
    }

    /* =========================================
       LOGICAL REORDERING (The Magic)
       ========================================= */

    /* HEADER: Expediente (Order 1) */
    td[data-label="Expediente"],
    td[data-label="Usuario"],
    .col-exp {
        order: 1;
        background: rgba(255, 255, 255, 0.03);
        margin: -16px -16px 15px -16px !important;
        /* Extend to edges */
        width: calc(100% + 32px) !important;
        padding: 12px 16px !important;
        border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        border-radius: var(--radius-lg) var(--radius-lg) 0 0;
        justify-content: space-between;
        color: var(--primary-400);
        font-size: 1.1rem;
        font-weight: 700;
    }

    td[data-label="Expediente"]:before,
    .col-exp:before {
        content: "EXPEDIENTE";
        /* Explicit label in header */
        position: static;
        transform: none;
        width: auto;
        margin-right: 10px;
    }

    /* SUB-HEADER: Destinatario (Order 2) */
    td[data-label="Destinatario"],
    .col-dest {
        order: 2;
        padding-left: 0 !important;
        text-align: left !important;
        justify-content: flex-start;
        margin-bottom: 8px;
    }

    td[data-label="Destinatario"]:before,
    .col-dest:before {
        position: static;
        transform: none;
        display: block;
        margin-bottom: 2px;
        width: 100%;
    }

    td[data-label="Destinatario"],
    .col-dest {
        font-size: 1.1rem;
        color: var(--text-primary);
        display: block !important;
        /* Allow stacking */
    }

    /* DOMICILIO (Order 3) */
    td[data-label="Domicilio"],
    .col-dom {
        order: 3;
        padding-left: 0 !important;
        text-align: left !important;
        justify-content: flex-start;
        display: block !important;
        margin-bottom: 12px;
        border-bottom: 1px solid rgba(255, 255, 255, 0.08);
        padding-bottom: 12px !important;
    }

    td[data-label="Domicilio"]:before,
    .col-dom:before {
        position: static;
        transform: none;
        display: block;
        width: 100%;
        margin-bottom: 2px;
    }

    /* KEY INFO GRID (Order 4-9) */

    /* Estado (Order 4) */
    td[data-label="Estado"],
    .col-status {
        order: 4;
        justify-content: space-between;
        padding-left: 0 !important;
    }

    td[data-label="Estado"]:before,
    .col-status:before {
        position: static;
        transform: none;
        width: auto;
    }

    /* Fecha (Order 5) */
    td[data-label="Fecha"],
    .col-date {
        order: 5;
    }

    /* Ujier (Order 6) */
    td[data-label="Ujier"],
    .col-ujier {
        order: 6;
    }

    /* Zona (Order 7) */
    td[data-label="Zona"],
    .col-zona {
        order: 7;
    }

    /* HIDE LESS IMPORTANT FIELDS IF EMPTY OR OPTIONAL */
    td[data-label="Letrado"],
    .col-letrado,
    td[data-label="Cargador"],
    .col-cargador,
    td[data-label="Troquel"],
    .col-troquel {
        order: 20;
        /* Push to bottom */
        display: none !important;
        /* Hide to clean up UI as requested 'missing info' usually means 'clutter' or 'blank space' */
        /* Note: If user wants them, we can remove display:none */
    }

    /* Only show Caratula if it fits logic, maybe Order 2.5? For now hide to simplify as text is long */
    td[data-label="Carátula"],
    .col-caratula {
        display: flex !important;
        order: 8;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    /* ACTIONS (Order 100) */
    td.actions-cell {
        order: 100;
        padding-left: 0 !important;
        justify-content: space-between;
        gap: 10px;
        margin-top: 15px;
        border-top: 1px solid rgba(255, 255, 255, 0.1);
        padding-top: 15px !important;
    }

    td.actions-cell .btn {
        flex: 1;
        justify-content: center;
    }

    /* CHECKBOX FIX for Asignaciones */
    td:has(input[type="checkbox"]) {
        order: 0;
        /* Absolute top */
        position: absolute;
        top: 10px;
        left: 10px;
        width: auto !important;
        padding: 0 !important;
        z-index: 50;
    }

    /* Adjust header pushing */
    td:has(input[type="checkbox"])~td[data-label="Expediente"] {
        padding-left: 50px !important;
    }

    /* --- SPECIFIC TABLE TWEAKS --- */

    /* Notifications Table Specifics */
    #tabla-notificaciones tr.date-group-row {
        background: transparent !important;
        box-shadow: none;
        border: none;
        padding: 0;
        margin-bottom: 0;
    }

    #tabla-notificaciones tr.date-group-row td {
        text-align: center !important;
        padding: 10px !important;
        color: var(--primary);
        font-weight: bold;
        justify-content: center;
    }

    #tabla-notificaciones tr.date-group-row td:before {
        display: none;
    }

    /* Ujier Dashboard Fixes */
    .ujier-list {
        display: flex;
        flex-direction: column;
    }

    /* Hide less critical info if needed to save space */
    td[data-label="Cargador"],
    td[data-label="Letrado"] {
        /* display: none !important;  Uncomment to compact view */
    }
}