/*
 * Tableaux (DataTables) - ameliorations communes a toutes les listes
 */

/* Chiffres de largeur egale : les montants s'alignent verticalement */
table.dataTable {
    font-variant-numeric: tabular-nums;
}

/* Montants alignes a droite */
table.dataTable tbody td:has(> .display_currency) {
    text-align: right;
    white-space: nowrap;
}

/* Colonne Action (1re colonne) figee lors du defilement horizontal */
.dataTables_scrollHead table.dataTable thead th:first-child,
.dataTables_scrollBody table.dataTable tbody td:first-child {
    position: sticky;
    left: 0;
    z-index: 2;
    background-color: #fff;
    box-shadow: 2px 0 4px -2px rgba(0, 0, 0, 0.15);
}

.dataTables_scrollBody table.table-striped tbody tr:nth-of-type(odd) td:first-child {
    background-color: #f9f9f9;
}

/* Menu "Actions" ouvert : passe au-dessus des lignes voisines */
.dataTables_scrollBody table.dataTable tbody td:first-child:focus-within {
    z-index: 5;
}

/* Filtres rapides : bouton actif */
.quick-filters .quick-filter.active {
    background-color: #4f46e5;
    border-color: #4f46e5;
    color: #fff;
}

/* Lignes cliquables (ouvrent le detail) */
table.dataTable tbody tr.dt-row-clickable > td:not(:first-child) {
    cursor: pointer;
}

table.dataTable tbody tr.dt-row-clickable:hover > td {
    background-color: #eef2ff;
}

/* Tableau vide */
table.dataTable td.dataTables_empty {
    padding: 32px 16px;
    color: #6b7280;
}

/* Indicateur cliquable (applique un filtre rapide) */
.list-kpi[data-quick-filter],
.list-kpi[data-href] {
    cursor: pointer;
    transition: border-color 0.15s, box-shadow 0.15s;
}

.list-kpi[data-quick-filter]:hover,
.list-kpi[data-href]:hover {
    border-color: #4f46e5;
    box-shadow: 0 2px 8px rgba(79, 70, 229, 0.15);
}

/* ==========================================================================
   Affichage mobile (ecrans < 768px) : chaque ligne devient une carte
   Libelles fournis par data-label (voir __dt_set_cell_labels dans common.js).
   Ajouter la classe "dt-no-cards" a un tableau pour garder l'affichage classique.
   ========================================================================== */
@media (max-width: 767px) {
    /* En-tetes inutiles : le nom de colonne est repris dans chaque carte */
    .dataTables_wrapper table.dataTable:not(.dt-no-cards) > thead,
    .dataTables_wrapper .dataTables_scrollHead:has(table.dataTable:not(.dt-no-cards)) {
        display: none !important;
    }

    /* Pas de defilement horizontal ni de hauteur fixe : les cartes s'empilent */
    .dataTables_wrapper .dataTables_scrollBody:has(table.dataTable:not(.dt-no-cards)) {
        height: auto !important;
        max-height: none !important;
        overflow: visible !important;
    }

    .dataTables_wrapper table.dataTable:not(.dt-no-cards) {
        width: 100% !important;
        border: none !important;
    }

    /* Une carte par ligne */
    .dataTables_wrapper table.dataTable:not(.dt-no-cards) > tbody > tr {
        display: flex;
        flex-direction: column;
        margin-bottom: 12px;
        padding: 8px 14px;
        background-color: #fff !important;
        border: 1px solid #e5e7eb;
        border-radius: 12px;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
    }

    /* Une ligne "Libelle ........ valeur" par information */
    .dataTables_wrapper table.dataTable:not(.dt-no-cards) > tbody > tr > td {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        gap: 12px;
        width: auto !important;
        padding: 6px 0 !important;
        border: none !important;
        border-bottom: 1px solid #f3f4f6 !important;
        background-color: transparent !important;
        text-align: right !important;
        white-space: normal !important;
        box-shadow: none !important;
        position: static !important;
    }

    .dataTables_wrapper table.dataTable:not(.dt-no-cards) > tbody > tr > td::before {
        content: attr(data-label);
        flex: 0 0 42%;
        text-align: left;
        font-weight: 600;
        color: #6b7280;
    }

    .dataTables_wrapper table.dataTable:not(.dt-no-cards) > tbody > tr > td[data-label=""]::before {
        display: none;
    }

    /* Colonne Action : en bas de la carte */
    .dataTables_wrapper table.dataTable:not(.dt-no-cards) > tbody > tr > td:first-child {
        order: 99;
        border-bottom: none !important;
        padding-top: 10px !important;
    }

    /* Tableau vide : message centre */
    .dataTables_wrapper table.dataTable:not(.dt-no-cards) > tbody > tr > td.dataTables_empty {
        display: block;
        text-align: center !important;
    }

    .dataTables_wrapper table.dataTable:not(.dt-no-cards) > tbody > tr > td.dataTables_empty::before {
        display: none;
    }

    /* Copies techniques d'en-tete/pied que DataTables place dans le corps defilant */
    .dataTables_wrapper .dataTables_scrollBody table.dataTable:not(.dt-no-cards) > tfoot {
        display: none !important;
    }

    /* Ligne des totaux : carte grisee, cellules vides masquees */
    .dataTables_wrapper .dataTables_scrollFoot:has(table.dataTable:not(.dt-no-cards)) {
        overflow: visible !important;
    }

    .dataTables_wrapper table.dataTable:not(.dt-no-cards) > tfoot > tr,
    .dataTables_wrapper .dataTables_scrollFoot table > tfoot > tr {
        display: flex;
        flex-direction: column;
        padding: 8px 14px;
        border-radius: 12px;
    }

    .dataTables_wrapper table.dataTable:not(.dt-no-cards) > tfoot > tr > td,
    .dataTables_wrapper .dataTables_scrollFoot table > tfoot > tr > td {
        display: flex;
        justify-content: space-between;
        gap: 12px;
        width: auto !important;
        padding: 4px 0 !important;
        border: none !important;
        text-align: right !important;
    }

    .dataTables_wrapper table.dataTable:not(.dt-no-cards) > tfoot > tr > td:empty,
    .dataTables_wrapper .dataTables_scrollFoot table > tfoot > tr > td:empty {
        display: none;
    }

    .dataTables_wrapper table.dataTable:not(.dt-no-cards) > tfoot > tr > td::before,
    .dataTables_wrapper .dataTables_scrollFoot table > tfoot > tr > td::before {
        content: attr(data-label);
        text-align: left;
        font-weight: 600;
        color: #6b7280;
    }

    /* Barre recherche / export / nombre de lignes : empilee */
    .dataTables_wrapper .dataTables_filter,
    .dataTables_wrapper .dataTables_length {
        text-align: left !important;
        margin-bottom: 8px;
    }

    .dataTables_wrapper .dataTables_filter input {
        width: 100% !important;
        margin-left: 0 !important;
    }
}

/* Factures / achats en retard de paiement */
table.dataTable tbody tr.dt-row-overdue > td:first-child {
    box-shadow: inset 4px 0 0 #dc2626, 2px 0 4px -2px rgba(0, 0, 0, 0.15);
}

.overdue-days {
    margin-top: 3px;
    font-size: 11px;
    font-weight: 600;
    color: #dc2626;
    white-space: nowrap;
}

/* Filtres actifs (etiquettes) */
.active-filters .active-filter {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 3px 6px 3px 12px;
    font-size: 12px;
    font-weight: 600;
    color: #3730a3;
    background-color: #eef2ff;
    border: 1px solid #c7d2fe;
    border-radius: 999px;
}

.active-filters .active-filter-remove {
    width: 20px;
    height: 20px;
    line-height: 18px;
    padding: 0;
    font-size: 15px;
    color: #4f46e5;
    background: transparent;
    border: none;
    border-radius: 50%;
    cursor: pointer;
}

.active-filters .active-filter-remove:hover {
    background-color: #c7d2fe;
}

.active-filters .active-filters-clear {
    padding: 0;
    font-size: 12px;
    font-weight: 600;
    color: #6b7280;
    text-decoration: underline;
    background: transparent;
    border: none;
    cursor: pointer;
}

/* Tendance des indicateurs */
.list-kpi-trend {
    font-size: 11px;
    font-weight: 600;
}

.list-kpi-trend.up {
    color: #16a34a;
}

.list-kpi-trend.down {
    color: #dc2626;
}

/* ==========================================================================
   Panneau lateral de detail (ouvert depuis une ligne de tableau)
   ========================================================================== */
.modal.dt-drawer {
    padding-right: 0 !important;
}

.modal.dt-drawer .modal-dialog {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(960px, 92vw) !important;
    max-width: none;
    margin: 0;
    transform: translateX(100%) !important;
    transition: transform 0.25s ease-out !important;
}

.modal.dt-drawer.in .modal-dialog {
    transform: translateX(0) !important;
}

.modal.dt-drawer .modal-content {
    height: 100%;
    overflow-y: auto;
    border: none;
    border-radius: 0;
    box-shadow: -8px 0 24px rgba(0, 0, 0, 0.18);
}

.modal.dt-drawer .modal-header {
    position: sticky;
    top: 0;
    z-index: 2;
    background-color: #fff;
}

.modal-backdrop.dt-drawer-backdrop.in {
    opacity: 0.25;
}

.dt-drawer-nav {
    float: right;
    display: inline-flex;
    gap: 4px;
    margin-left: 12px;
}

.dt-drawer-nav button {
    width: 30px;
    height: 30px;
    font-size: 20px;
    line-height: 26px;
    color: #4f46e5;
    background-color: #eef2ff;
    border: 1px solid #c7d2fe;
    border-radius: 8px;
    cursor: pointer;
}

.dt-drawer-nav button:disabled {
    opacity: 0.35;
    cursor: default;
}

@media (max-width: 767px) {
    .modal.dt-drawer .modal-dialog {
        width: 100vw !important;
    }
}

/* ==========================================================================
   Recherche globale (Ctrl + K)
   ========================================================================== */
.global-search-open {
    cursor: pointer;
}

.global-search-kbd {
    padding: 1px 5px;
    font-size: 10px;
    color: #64748b;
    background-color: #f1f5f9;
    border: 1px solid #cbd5e1;
    border-radius: 4px;
    box-shadow: none;
}

.gs-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 2000;
    padding: 10vh 16px 16px;
    background-color: rgba(15, 23, 42, 0.45);
}

.gs-box {
    max-width: 640px;
    margin: 0 auto;
    overflow: hidden;
    background-color: #fff;
    border-radius: 14px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25);
}

.gs-input-wrap {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 14px 16px;
    border-bottom: 1px solid #e5e7eb;
}

.gs-input-wrap svg {
    width: 20px;
    height: 20px;
    color: #6b7280;
    flex: none;
}

.gs-input {
    flex: 1;
    font-size: 16px;
    border: none;
    outline: none;
}

.gs-input-wrap kbd {
    padding: 1px 6px;
    font-size: 11px;
    color: #64748b;
    background-color: #f1f5f9;
    border: 1px solid #cbd5e1;
    border-radius: 4px;
    box-shadow: none;
}

.gs-results {
    max-height: 55vh;
    overflow-y: auto;
    padding: 6px 0;
}

.gs-results:empty {
    display: none;
}

.gs-section {
    padding: 8px 16px 4px;
    font-size: 11px;
    font-weight: 700;
    color: #6b7280;
    text-transform: uppercase;
}

.gs-item {
    padding: 8px 16px;
    cursor: pointer;
}

.gs-item.active {
    background-color: #eef2ff;
}

.gs-item-title {
    font-weight: 600;
    color: #111827;
}

.gs-item-subtitle {
    font-size: 12px;
    color: #6b7280;
}

.gs-empty {
    padding: 16px;
    text-align: center;
    color: #6b7280;
}

.gs-hint {
    padding: 8px 16px;
    font-size: 11px;
    color: #9ca3af;
    border-top: 1px solid #f3f4f6;
}

@media (max-width: 767px) {
    .global-search-label,
    .global-search-kbd {
        display: none;
    }
}

/* ==========================================================================
   Mobile : menu "Actions" des cartes, ouvert vers la gauche et contenu dans l'ecran
   ========================================================================== */
@media (max-width: 767px) {
    .dataTables_wrapper table.dataTable:not(.dt-no-cards) > tbody > tr > td .btn-group {
        position: relative;
    }

    .dataTables_wrapper table.dataTable:not(.dt-no-cards) > tbody > tr > td .dropdown-menu {
        left: auto !important;
        right: 0 !important;
        min-width: 220px;
        max-width: calc(100vw - 32px);
        max-height: 60vh;
        overflow-y: auto;
        text-align: left;
        white-space: normal;
    }

    .dataTables_wrapper table.dataTable:not(.dt-no-cards) > tbody > tr > td .dropdown-menu > li > a {
        white-space: normal;
    }

    /* Les listes ne doivent jamais elargir la page */
    .dataTables_wrapper {
        max-width: 100%;
        overflow-x: hidden;
    }
}

/* ==========================================================================
   Lisibilite commune a tous les tableaux et rapports
   ========================================================================== */
/* Montants et totaux sur une seule ligne */
table.dataTable td.dt-amount,
table.dataTable th.dt-amount,
table.dataTable td .display_currency {
    white-space: nowrap;
}

/* Titres de colonnes : largeur minimale (plus d'empilement mot par mot) */
table.dataTable thead th:not(:first-child) {
    min-width: 96px;
}

/* Colonne Action : tous les boutons au style du bouton "Actions" des ventes
   (petit, arrondi, contour bleu, une seule ligne ; texte long tronque, complet en infobulle) */
table.dataTable tbody td.dt-action-cell {
    white-space: nowrap;
}

table.dataTable tbody td.dt-action-cell > a:not(.dropdown-toggle),
table.dataTable tbody td.dt-action-cell > button:not(.dropdown-toggle) {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    max-width: 190px;
    height: 24px;
    min-height: 24px;
    margin: 1px 2px;
    padding: 0 8px;
    overflow: hidden;
    font-size: 12px;
    font-weight: 600;
    line-height: 22px;
    white-space: nowrap;
    text-overflow: ellipsis;
    text-align: left;
    vertical-align: middle;
    color: oklch(0.7206 0.191 231.6);
    background: transparent;
    border: 1px solid oklch(0.7206 0.191 231.6);
    border-radius: 8px;
    box-shadow: none;
}

table.dataTable tbody td.dt-action-cell > a:not(.dropdown-toggle):hover,
table.dataTable tbody td.dt-action-cell > button:not(.dropdown-toggle):hover {
    color: #fff;
    background: oklch(0.7206 0.191 231.6);
    text-decoration: none;
}

/* Pas de retour a la ligne ni de pleine largeur dans les boutons d'action */
table.dataTable tbody td.dt-action-cell > a:not(.dropdown-toggle) br,
table.dataTable tbody td.dt-action-cell > button:not(.dropdown-toggle) br {
    display: none;
}

table.dataTable tbody td.dt-action-cell > a:not(.dropdown-toggle),
table.dataTable tbody td.dt-action-cell > button:not(.dropdown-toggle) {
    width: auto !important;
    flex-direction: row;
    flex-wrap: nowrap !important;
}

table.dataTable tbody td.dt-action-cell > a:not(.dropdown-toggle) > i,
table.dataTable tbody td.dt-action-cell > button:not(.dropdown-toggle) > i {
    display: inline-block !important;
}

/* Boutons de suppression : rouges */
table.dataTable tbody td.dt-action-cell > a[class*="error"],
table.dataTable tbody td.dt-action-cell > a[class*="danger"],
table.dataTable tbody td.dt-action-cell > a[class*="delete"],
table.dataTable tbody td.dt-action-cell > button[class*="error"],
table.dataTable tbody td.dt-action-cell > button[class*="danger"],
table.dataTable tbody td.dt-action-cell > button[class*="delete"] {
    color: #dc2626;
    border-color: #dc2626;
}

table.dataTable tbody td.dt-action-cell > a[class*="error"]:hover,
table.dataTable tbody td.dt-action-cell > a[class*="danger"]:hover,
table.dataTable tbody td.dt-action-cell > a[class*="delete"]:hover,
table.dataTable tbody td.dt-action-cell > button[class*="error"]:hover,
table.dataTable tbody td.dt-action-cell > button[class*="danger"]:hover,
table.dataTable tbody td.dt-action-cell > button[class*="delete"]:hover {
    color: #fff;
    background: #dc2626;
}

table.dataTable tbody td.dt-action-cell > a:not(.dropdown-toggle) i,
table.dataTable tbody td.dt-action-cell > a:not(.dropdown-toggle) svg,
table.dataTable tbody td.dt-action-cell > button:not(.dropdown-toggle) i,
table.dataTable tbody td.dt-action-cell > button:not(.dropdown-toggle) svg {
    flex: none;
    width: 14px;
    height: 14px;
    margin: 0;
}

@media (max-width: 767px) {
    table.dataTable thead th:not(:first-child) {
        min-width: 0;
    }
}

/* Stock sous le seuil d'alerte : trait orange a gauche + badge (au lieu d'une ligne rose) */
table.dataTable tbody tr.dt-row-low-stock > td:first-child {
    box-shadow: inset 4px 0 0 #f59e0b, 2px 0 4px -2px rgba(0, 0, 0, 0.15);
}

.dt-low-stock {
    display: inline-block;
    margin-left: 6px;
    padding: 1px 7px;
    font-size: 10.5px;
    font-weight: 700;
    color: #92400e;
    background-color: #fef3c7;
    border-radius: 999px;
    white-space: nowrap;
}
