/* ============================================================
   Smart Inventory — Mobile Responsive Overhaul (Sprint 1)
   Single-file overrides to fix overflows on:
   - Réconciliation (Contrôle Logistique)
   - Workflow Intelligence
   - Marge Prévisionnelle
   - Scan workflow (Phase 3+D)
   - Dashboard cards (forced col-6 for demandes)
   - Stock — labeled separator band
   ============================================================ */

/* ----- GLOBAL TABLET (≤ 1024px) ----- */
@media (max-width: 1024px) {
    .bento-grid { gap: 12px; }
    .bento-cell.span-3 { grid-column: span 6; }
    .bento-cell.span-6 { grid-column: span 12; }
    .lc-table { font-size: 12.5px; }
    .lc-table th, .lc-table td { padding: 8px 10px; }
}

/* ----- MOBILE (≤ 768px) ----- */
@media (max-width: 768px) {

    /* Page hero stacked + smaller */
    .page-hero {
        flex-direction: column;
        align-items: flex-start;
        padding: 14px 16px;
        gap: 10px;
    }
    .page-hero-left { width: 100%; }
    .page-hero-title { font-size: 18px !important; line-height: 1.25; }
    .page-hero-subtitle { font-size: 12px !important; }
    .page-hero-icon { width: 38px !important; height: 38px !important; font-size: 18px !important; }
    .page-hero-actions {
        width: 100%;
        display: flex; flex-wrap: wrap; gap: 6px;
    }
    .page-hero-actions .btn { flex: 1 1 auto; font-size: 12px; padding: 7px 10px; }
    .page-hero-actions .btn i { margin-right: 4px; }

    /* Bento — force 1 column for most cells */
    .bento-grid { gap: 10px; }
    .bento-cell { grid-column: span 12 !important; }
    /* But: DASHBOARD DEMANDES cards stay 2 per row (col-6 as requested) */
    .dashboard-grid .bento-cell.dash-half,
    .dashboard-grid .bento-cell.is-compact,
    .bento-grid .bento-cell.span-3.is-compact { grid-column: span 6 !important; }
    /* Stat-cards on dashboard: 2 per row on phone */
    .dashboard-demandes-grid,
    .stats-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 8px !important;
    }
    .dashboard-demandes-grid .stat-card,
    .stats-grid .stat-card { padding: 10px 12px !important; }
    .dashboard-demandes-grid .stat-card .stat-value,
    .stats-grid .stat-card .stat-value { font-size: 20px !important; }
    .dashboard-demandes-grid .stat-card .stat-label,
    .stats-grid .stat-card .stat-label { font-size: 11px !important; line-height: 1.2; }

    /* Tables — horizontal scroll instead of overflow */
    .lc-table-wrap, .table-wrap, .table-responsive {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin: 0 -8px;
        padding: 0 8px;
    }
    .lc-table { font-size: 11.5px; min-width: 540px; }
    .lc-table th { font-size: 10.5px; letter-spacing: 0.02em; }
    .lc-table th, .lc-table td { padding: 7px 8px; white-space: nowrap; }
    .lc-table td:first-child { white-space: normal; }

    /* Buttons / btn-group containers wrap properly */
    .btn-group, .actions-row, [class*="-actions"] {
        flex-wrap: wrap !important;
        gap: 4px !important;
    }
    .btn { font-size: 12.5px; padding: 7px 11px; }
    .btn-sm { font-size: 11.5px; padding: 5px 8px; }
    .btn-xs { font-size: 11px; padding: 4px 7px; }

    /* ===== Réconciliation / Contrôle Logistique ===== */
    .recon-group {
        padding: 12px !important;
        margin-bottom: 12px;
    }
    .recon-group-head {
        flex-direction: column;
        align-items: stretch !important;
        gap: 8px !important;
    }
    .recon-group-head > div:first-child { font-size: 13.5px; line-height: 1.35; }
    .recon-group-head > div:last-child {
        display: flex; flex-wrap: wrap; gap: 4px;
        justify-content: flex-start;
    }
    .recon-group-head .btn { flex: 1 1 calc(50% - 4px); min-width: 0; }
    .recon-group-head .btn i { font-size: 12px; }
    /* Compact table headers in recon list */
    .recon-group .lc-table th { font-size: 10px; padding: 6px 4px; }
    .recon-group .lc-table td { padding: 6px 4px; }

    /* Recon hero */
    .recon-tabs {
        overflow-x: auto;
        white-space: nowrap;
        -webkit-overflow-scrolling: touch;
        gap: 0 !important;
        padding-bottom: 4px;
    }
    .recon-tabs .recon-tab {
        flex-shrink: 0;
        font-size: 12px;
        padding: 8px 12px;
    }

    /* ===== Phase 3 Scan — split becomes stack ===== */
    .scan-split { grid-template-columns: 1fr !important; gap: 12px !important; }
    .scan-method-tabs { gap: 4px; }
    .scan-method-tab { min-width: calc(50% - 4px) !important; font-size: 11.5px; padding: 9px 8px; }
    .scan-input { font-size: 16px; padding: 11px 13px; }
    .scan-progress-pct { font-size: 28px !important; }
    .scan-progress-stats { font-size: 11.5px; }
    .scan-progress-head { flex-direction: column; align-items: stretch; gap: 6px; }
    .scan-progress-stats { text-align: left; }
    .scan-cand { padding: 9px 10px; }
    .scan-cand-main { font-size: 12.5px; gap: 5px; }
    .scan-cand-lockbadge { margin-left: 0; }

    /* ===== Workflow Intelligence ===== */
    .wf-tabs {
        overflow-x: auto;
        white-space: nowrap;
        -webkit-overflow-scrolling: touch;
        flex-wrap: nowrap !important;
        padding-bottom: 4px;
    }
    .wf-tab { flex-shrink: 0; font-size: 11.5px; padding: 8px 12px; }
    .wf-bytype-row { grid-template-columns: 1fr !important; gap: 6px; }
    .wf-pattern-row { flex-direction: column; align-items: stretch; gap: 6px; }

    /* ===== Marge Prévisionnelle ===== */
    .lc-table-wrap table thead th:nth-child(8),
    .lc-table-wrap table tbody td:nth-child(8) {
        /* Hide "Ajustements" column on phone (low info density) */
        display: none;
    }

    /* ===== Dashboard cards spacing ===== */
    .stat-card, .dashboard-card {
        padding: 12px;
    }
    .stat-card-value, .dashboard-card-value { font-size: 22px !important; }
    .stat-card-label, .dashboard-card-label { font-size: 11px !important; }

    /* ===== Modal sizing on phone ===== */
    .modal {
        max-width: calc(100vw - 16px) !important;
        max-height: calc(100vh - 32px);
        margin: 8px auto;
        overflow-y: auto;
    }
    .modal-body { padding: 14px; }
    .modal-header h3 { font-size: 15px; }
    .modal-footer { flex-wrap: wrap; gap: 6px; }
    .modal-footer .btn { flex: 1 1 auto; }

    /* ===== Forms ===== */
    .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr !important; }

/* ============================================================
   Sprint 2.4 — Fix débordement cards stat (dashboard + intelligence)
   ============================================================ */

/* Dashboard demandes grid : 5 cards mobile → 2 cols et la dernière en span complet */
@media (max-width: 640px) {
    .dashboard-demandes-grid {
        grid-template-columns: 1fr 1fr !important;
    }
    .dashboard-demandes-grid > .stat-card:nth-child(5) {
        grid-column: 1 / -1;
    }
}

/* Intelligence opérationnelle (page Coût Logistique PO) : score chips
   ne doivent pas s'étirer verticalement quand la cellule voisine est haute */
.rec-scores-row {
    align-content: start;
    align-items: start;
}
.rec-score-chip {
    align-self: start;
    min-height: auto !important;
}

/* Bento cells : si une cellule a peu de contenu, elle ne doit pas s'étirer
   pour matcher la voisine — laisser respirer */
.bento-cell { align-self: start; }

/* Compact dashboard cards (dashboard cards) : limiter la hauteur naturelle */
.dashboard-demandes-grid > .stat-card {
    min-height: auto;
    align-self: start;
}

    .form-input { font-size: 14px; }

    /* ===== Sidebar / topbar (already responsive on mobile via existing CSS) ===== */
    .app-topbar h1, .app-topbar .topbar-title { font-size: 15px; }

    /* ===== Empty states ===== */
    .empty-state { padding: 18px 14px; }
    .empty-state i { font-size: 28px; }
    .empty-state h3 { font-size: 14px; }

    /* ===== Hide floating help on data-heavy pages ===== */
    body[data-page="reconciliation"] #floating-help-btn,
    body[data-page="reconciliation"] .help-tooltip-panel,
    body[data-page="reconciliation"] .onboarding-tooltip,
    body.recon-session #floating-help-btn,
    body.recon-session .help-tooltip-panel,
    body.recon-session .onboarding-tooltip,
    body[data-page="scan"] #floating-help-btn,
    body[data-page="scan"] .help-tooltip-panel,
    body[data-page="scan"] .onboarding-tooltip {
        display: none !important;
    }
}

/* ----- HIDE THE HELP TOOLTIP ON SCAN + RECON ON ALL SCREENS ----- */
body.recon-session .onboarding-tooltip,
body.recon-session #onboarding-tooltip,
body.scan-page-active .onboarding-tooltip,
body.scan-page-active #onboarding-tooltip {
    display: none !important;
}

/* ===== STOCK PAGE — separator with right-aligned background label ===== */
.stock-separator {
    display: flex;
    align-items: center;
    gap: 14px;
    margin: 18px 0 12px;
}
.stock-separator-line {
    flex: 1;
    height: 1px;
    background: linear-gradient(90deg, transparent, var(--border-color, rgba(0,0,0,.12)));
}
.stock-separator-label {
    padding: 6px 14px;
    background: linear-gradient(135deg, rgba(59,130,246,.12), rgba(124,58,237,.10));
    border: 1px solid rgba(59,130,246,.25);
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
    letter-spacing: 0.05em;
    color: var(--text-primary);
    white-space: nowrap;
}
[data-theme="dark"] .stock-separator-label {
    background: linear-gradient(135deg, rgba(59,130,246,.18), rgba(124,58,237,.15));
}

/* ===== Small mobile (≤ 480px) ===== */
@media (max-width: 480px) {
    .page-hero-title { font-size: 16px !important; }
    .btn { padding: 6px 9px; font-size: 12px; }
    .btn-xs { padding: 4px 6px; }
    .bento-head { gap: 8px; }
    .bento-icon { width: 32px !important; height: 32px !important; font-size: 14px !important; }
    .bento-title { font-size: 13px; }
    .bento-sub { font-size: 11px; }
    /* Recon group head 4 actions → 2x2 grid */
    .recon-group-head .btn { flex: 1 1 calc(50% - 4px); }
}

/* ============================================================
   PHASE UX MOBILE CORE — Refonte responsive globale (téléphone)
   Corrige : débordement horizontal du body (cards « déplacées »),
   filtres non adaptés, header encombré, tables, boutons flottants.
   ============================================================ */

@media (max-width: 768px) {

    /* ----- 1. STOP au débordement horizontal (cause des cards décalées) ----- */
    html, body { overflow-x: hidden !important; max-width: 100vw; }
    .app-container, .main-content, .page-content { max-width: 100vw; overflow-x: hidden; }
    .main-content { margin-left: 0 !important; width: 100%; }
    img, video, canvas { max-width: 100%; height: auto; }

    /* ----- 2. Header / topbar compact ----- */
    .header { height: 54px; padding: 0 10px; gap: 6px; }
    .header-left { gap: 8px; min-width: 0; flex: 1; }
    .header-title { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .header-right, .header-actions { display: flex; align-items: center; gap: 4px; flex-wrap: nowrap; flex-shrink: 0; }
    .header .btn, .header-right .btn { padding: 6px 8px; font-size: 12px; }
    .menu-toggle { display: block; font-size: 20px; padding: 6px 8px; background: none; border: none; color: var(--text-primary); }

    /* ----- 3. Contenu de page ----- */
    .page-content { padding: 12px 10px 90px; } /* marge basse pour les boutons flottants */

    /* ----- 4. Filtres : pleine largeur, empilés ----- */
    .filters-bar { flex-direction: column; align-items: stretch !important; gap: 8px; width: 100%; margin-bottom: 14px; }
    .filters-bar .form-input, .filters-bar .form-select,
    .filters-bar .search-input { max-width: 100% !important; width: 100% !important; min-width: 0 !important; }
    /* Le conteneur flex qui porte filtres + boutons d'action s'empile */
    .flex.justify-between.items-center.mb-4 { flex-direction: column; align-items: stretch; gap: 10px; }
    .flex.justify-between.items-center.mb-4 > .flex { justify-content: flex-start; }

    /* ----- 5. Cards & grilles génériques ----- */
    .card { border-radius: 12px; margin-left: 0; margin-right: 0; }
    .card-body { padding: 14px 12px; }
    .card-header { padding: 12px; flex-wrap: wrap; gap: 8px; }
    .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr !important; }

    /* ----- 6. Tables : scroll horizontal contenu DANS la card ----- */
    .table-container { overflow-x: auto; -webkit-overflow-scrolling: touch; max-width: 100%; }
    .table-container table { min-width: 560px; }
    table th, table td { padding: 8px 10px; font-size: 12.5px; }
    table th { font-size: 11px; }

    /* ----- 7. Boutons flottants : plus petits, ne masquent pas le contenu ----- */
    #floating-help-btn { width: 44px !important; height: 44px !important; font-size: 17px !important;
        bottom: 14px !important; right: 12px !important; }
    #floating-search-btn { width: 44px !important; height: 44px !important; font-size: 17px !important;
        bottom: 66px !important; right: 12px !important; }

    /* ----- 8. Sidebar mobile : au-dessus du contenu + fond assombri ----- */
    .sidebar { z-index: 1000; box-shadow: 4px 0 24px rgba(0,0,0,0.25); }
    .sidebar.mobile-open::after { content: ''; }
    body.sidebar-open-backdrop::before {
        content: ''; position: fixed; inset: 0; background: rgba(0,0,0,0.45); z-index: 999;
    }

    /* ----- 9. Modales pleine largeur ----- */
    .modal-overlay { padding: 8px; align-items: flex-start; }
    .modal { width: 100% !important; max-width: calc(100vw - 16px) !important; }

    /* ----- 10. Bento & stat cards ----- */
    .bento-grid { grid-template-columns: repeat(12, 1fr); }
    .stats-grid { gap: 10px !important; }
    .stat-card { min-width: 0; }
    .stat-value, .stat-card-value { word-break: break-word; }

    /* ----- 11. Page hero / titres ----- */
    h2, h3 { word-break: break-word; }
    .page-hero { margin: 0 0 12px; }
}

@media (max-width: 480px) {
    .header { padding: 0 8px; }
    .page-content { padding: 10px 8px 90px; }
    .table-container table { min-width: 520px; }
    .btn { min-height: 36px; } /* cible tactile confortable */
    .form-input, .form-select { font-size: 16px; } /* évite le zoom iOS au focus */
}
