/* =====================================================================
   responsive.css — Gestion Coopérative
   Menu burger + adaptation à tous les écrans. À charger APRÈS style.css.

   Points de rupture :
     > 992px  : ordinateur — barre latérale fixe, repliable (icônes seules)
     ≤ 992px  : tablette — la barre latérale devient un tiroir coulissant
     ≤ 768px  : mobile — formulaires, filtres et statistiques compacts
     ≤ 640px  : téléphone — les tableaux s'affichent en cartes empilées
     ≤ 400px  : petit téléphone
   ===================================================================== */

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
img, video, canvas, iframe { max-width: 100%; }
img { height: auto; }
.app-wrapper { overflow-x: clip; }
.badge { white-space: nowrap; }

/* =====================================================================
   BOUTON BURGER (visible sur tous les écrans)
   ===================================================================== */
.burger-btn {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 5px;
    width: 42px;
    height: 42px;
    padding: 0;
    margin: 0;
    flex-shrink: 0;
    background: transparent;
    border: 1px solid var(--border, #e2e8e4);
    border-radius: 10px;
    color: var(--primary-dark, #125c38);
    cursor: pointer;
    font-size: 0;
    line-height: 0;
    -webkit-tap-highlight-color: transparent;
    transition: background .2s, border-color .2s;
}
.burger-btn:hover { background: #eef4f0; border-color: #c9d8cf; }
.burger-btn:focus-visible { outline: 2px solid var(--primary, #1a7a4c); outline-offset: 2px; }
.burger-btn span {
    display: block;
    width: 20px;
    height: 2px;
    border-radius: 2px;
    background: currentColor;
    transition: transform .28s ease, opacity .2s ease;
}
body.menu-open .burger-btn span:nth-child(1) { transform: translateY(7px) rotate(45deg); }
body.menu-open .burger-btn span:nth-child(2) { opacity: 0; }
body.menu-open .burger-btn span:nth-child(3) { transform: translateY(-7px) rotate(-45deg); }

/* Barre du haut : burger + titre (tronqué si trop long) + utilisateur */
.topbar { gap: 12px; }
.topbar h1 { flex: 1 1 auto; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-chip { flex-shrink: 0; }
.btn-logout .logout-ico { display: none; }

.sidebar-overlay { display: none; }
.sidebar-close { display: none; }
.nav-link .nav-icon { flex-shrink: 0; }

/* Tableaux enveloppés automatiquement par responsive.js */
.table-scroll { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* =====================================================================
   ORDINATEUR (> 992px) : barre latérale repliable
   ===================================================================== */
@media (min-width: 993px) {
    .sidebar, .main { transition: width .25s ease, margin-left .25s ease; }
    .sidebar { overflow-x: hidden; }

    body.sidebar-collapsed { --sidebar-w: 72px; }
    body.sidebar-collapsed .sidebar { width: 72px; }
    body.sidebar-collapsed .main { margin-left: 72px; }

    body.sidebar-collapsed .sidebar-brand { justify-content: center; padding: 18px 0; }
    body.sidebar-collapsed .sidebar-brand .brand-text,
    body.sidebar-collapsed .nav-link .nav-txt { display: none; }
    body.sidebar-collapsed .sidebar-brand .brand-ico { font-size: 22px; }
    body.sidebar-collapsed .nav-group-title { font-size: 0; padding: 8px 0 4px; margin: 6px 14px 0; border-top: 1px solid rgba(255,255,255,.1); }
    body.sidebar-collapsed .nav-link { justify-content: center; padding: 11px 0; gap: 0; }
    body.sidebar-collapsed .nav-link .nav-icon { width: auto; font-size: 18px; }
}

/* =====================================================================
   TABLETTE ET MOBILE (≤ 992px) : barre latérale en tiroir
   ===================================================================== */
@media (max-width: 992px) {
    .sidebar {
        position: fixed !important;
        top: 0; left: 0; bottom: 0;
        height: 100%;
        height: 100dvh;
        width: 280px !important;
        max-width: 85vw;
        transform: translateX(-105%);
        transition: transform .28s ease, box-shadow .28s ease;
        z-index: 1001 !important;
        overflow-y: auto;
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
        padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    }
    body.menu-open .sidebar { transform: translateX(0); box-shadow: 6px 0 28px rgba(0, 0, 0, .35); }

    .sidebar-overlay {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(8, 30, 18, .5);
        z-index: 1000;
        opacity: 0;
        visibility: hidden;
        transition: opacity .28s ease, visibility .28s ease;
        -webkit-tap-highlight-color: transparent;
    }
    body.menu-open .sidebar-overlay { opacity: 1; visibility: visible; }
    body.menu-open { overflow: hidden; }

    .sidebar-close {
        display: flex;
        align-items: center;
        justify-content: center;
        position: absolute;
        top: 16px;
        right: 12px;
        width: 34px;
        height: 34px;
        border: none;
        border-radius: 50%;
        background: rgba(255, 255, 255, .14);
        color: #fff;
        font-size: 24px;
        line-height: 1;
        cursor: pointer;
        z-index: 2;
    }
    .sidebar-close:hover { background: rgba(255, 255, 255, .25); }
    .sidebar-brand { padding-right: 56px !important; }
    .nav-link { min-height: 46px; font-size: 14.5px; }

    .main { margin-left: 0 !important; width: 100%; }
    .topbar { padding: 12px 18px; }
    .content { padding: 18px; }
}

/* =====================================================================
   MOBILE (≤ 768px)
   ===================================================================== */
@media (max-width: 768px) {
    .topbar { padding: 10px 12px; gap: 10px; }
    .topbar h1 { font-size: 16px; }
    .user-chip { gap: 8px; }
    .user-chip .user-meta { display: none; }
    .btn-logout { padding: 7px 9px !important; }
    .btn-logout .logout-txt { display: none; }
    .btn-logout .logout-ico { display: inline; font-size: 15px; line-height: 1; }

    .content { padding: 14px 12px; }

    /* Statistiques : 2 par ligne */
    .stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; gap: 10px; margin-bottom: 14px; }
    .stat-card { padding: 12px; gap: 10px; min-width: 0; }
    .stat-icon { width: 36px; height: 36px; font-size: 17px; flex-shrink: 0; }
    .stat-card .stat-value { font-size: 16px; word-break: break-word; }
    .stat-card .stat-label { font-size: 11.5px; }
    .stat-card > div { min-width: 0; }

    .card { padding: 14px 12px; margin-bottom: 14px; }
    .card-header { flex-wrap: wrap; gap: 10px; }
    .card-header h2 { font-size: 15px; }
    .card-header > form, .card-header .filters-bar { flex: 1 1 100%; }

    /* Formulaires : une colonne */
    .form-grid { grid-template-columns: minmax(0, 1fr) !important; gap: 12px; }
    .form-grid [style*="grid-column"] { grid-column: auto !important; }
    .form-actions { flex-wrap: wrap; }
    input, select, textarea { font-size: 16px !important; max-width: 100%; min-width: 0; } /* pas de zoom auto sur iPhone */

    /* Barre de filtres : champs sur toute la largeur */
    .filters-bar { gap: 8px; align-items: stretch; }
    .filters-bar > * { flex: 1 1 calc(50% - 8px); min-width: 0 !important; }
    .filters-bar > input[type=search], .filters-bar > input[type=text], .filters-bar > .form-group:first-child { flex-basis: 100%; }
    .filters-bar input, .filters-bar select { width: 100%; }

    .pagination { flex-wrap: wrap; }

    /* Styles écrits directement dans les pages */
    [style*="grid-template-columns"]:not([style*="auto-fit"]):not([style*="auto-fill"]) { grid-template-columns: minmax(0, 1fr) !important; }
    [style*="display:flex"], [style*="display: flex"] { flex-wrap: wrap; }
    [style*="min-width"] { min-width: 0 !important; }

    /* Tableaux qui défilent : cellules sur une ligne, textes longs repliés */
    table.data-table { font-size: 12.5px; }
    table.data-table th, table.data-table td { white-space: nowrap; padding: 9px 10px; }
    table.data-table td.td-long { white-space: normal; min-width: 200px; max-width: 320px; }

    /* Connexion / installation */
    .login-page { padding: 16px; align-items: flex-start; padding-top: max(24px, 6vh); }
    .login-box { width: 100% !important; max-width: 420px; padding: 28px 22px !important; }

    .modal-box { width: 94% !important; max-height: 90vh; overflow-y: auto; }
    .flash { word-break: break-word; }
}

/* =====================================================================
   TÉLÉPHONE (≤ 640px) : tableaux en cartes empilées
   (responsive.js ajoute .table-cards et les attributs data-label)
   ===================================================================== */
@media (max-width: 640px) {
    table.table-cards,
    table.table-cards tbody,
    table.table-cards thead,
    table.table-cards tfoot,
    table.table-cards tr,
    table.table-cards td { display: block; width: 100%; }
    table.table-cards tr.row-header { display: none; }
    table.table-cards tr.row-caption { background: transparent; border: none; box-shadow: none; padding: 0; margin: 0 0 8px; }
    table.table-cards tr.row-caption th { display: block; background: transparent; padding: 4px 2px; font-size: 13px; }

    table.table-cards tr {
        background: #fff;
        border: 1px solid var(--border, #e2e8e4);
        border-radius: 10px;
        margin-bottom: 10px;
        padding: 4px 12px;
        box-shadow: 0 1px 2px rgba(0,0,0,.04);
    }
    table.table-cards tr:hover td { background: transparent; }
    table.table-cards td {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        padding: 8px 0 !important;
        border-bottom: 1px dashed var(--border, #e2e8e4);
        white-space: normal !important;
        text-align: right !important;
        min-width: 0 !important;
        max-width: none !important;
        word-break: break-word;
    }
    table.table-cards td:last-child { border-bottom: none; }
    table.table-cards td::before {
        content: attr(data-label);
        flex: 0 0 38%;
        text-align: left;
        font-size: 11px;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: .03em;
        color: var(--text-muted, #6b7a72);
    }
    table.table-cards td.cell-title { font-weight: 700; font-size: 14px; }
    table.table-cards td.cell-nolabel { justify-content: flex-end; }
    table.table-cards td.cell-nolabel::before { display: none; }
    table.table-cards td[colspan] { justify-content: center; text-align: center !important; }
    table.table-cards td[colspan]::before { display: none; }
    table.table-cards td:empty { display: none; }
    /* Ligne de total (tfoot ou ligne en gras) */
    table.table-cards tfoot tr, table.table-cards tr.total-row { background: #f0f4f2; }

    /* Cellules d'actions : boutons alignés à droite, passage à la ligne sans écrasement */
    table.table-cards td.cell-actions { flex-wrap: wrap; justify-content: flex-end; gap: 6px; word-break: normal; }
    table.table-cards td.cell-actions::before { margin-right: auto; }
    table.table-cards td.cell-actions > * { flex: 0 0 auto; margin: 0 !important; }
    table.table-cards td.cell-actions .btn { white-space: nowrap; }
    table.table-cards td.cell-actions > form { display: inline-flex !important; gap: 6px; flex-wrap: wrap; align-items: center; }
    table.table-cards td details { display: inline-block; text-align: left; }
    table.table-cards td details[open] { flex: 1 1 100% !important; order: 10; background: #f4f7f5; border-radius: 8px; padding: 8px; }
    table.table-cards td details form { display: block !important; }
}

/* =====================================================================
   PETIT TÉLÉPHONE
   ===================================================================== */
@media (max-width: 480px) {
    .burger-btn { width: 40px; height: 40px; }
    .topbar h1 { font-size: 15px; }
    .avatar { width: 32px; height: 32px; font-size: 12px; }
    .btn { padding: 9px 13px; }
    .form-actions > .btn { flex: 1 1 100%; justify-content: center; }
    .filters-bar > * { flex-basis: 100%; }
}
@media (max-width: 400px) {
    table.table-cards td::before { flex-basis: 45%; }
}
@media (max-width: 340px) {
    .stat-grid { grid-template-columns: minmax(0, 1fr) !important; }
}

/* =====================================================================
   GRANDS ÉCRANS
   ===================================================================== */
@media (min-width: 1600px) {
    .content { padding: 28px 40px; }
    .topbar { padding: 14px 40px; }
}

/* Bandeau d'installation : sous le tiroir du menu */
#pwa-install-banner { z-index: 900 !important; }

/* ---------- Réduction des animations ---------- */
@media (prefers-reduced-motion: reduce) {
    .sidebar, .main, .sidebar-overlay, .burger-btn span { transition: none !important; }
}

/* ---------- Impression : jamais de menu ni de burger ---------- */
@media print {
    .burger-btn, .sidebar, .sidebar-overlay, .sidebar-close, #pwa-install-banner { display: none !important; }
    .main { margin-left: 0 !important; }
    .topbar { position: static; }
    .table-scroll { overflow: visible !important; }
}
