/**
 * UNC — Responsive mobile & tablette (320px → 1920px+)
 * Chargé en dernier pour surcharger les modules existants.
 */

/* ── Variables tactiles ─────────────────────────────────────── */
:root {
    --touch-min: 44px;
    --sidebar-drawer-width: min(300px, 88vw);
    --mobile-header-h: 56px;
}

/* ── Viewport / safe-area (PWA iPhone) ──────────────────────── */
@supports (padding: env(safe-area-inset-top)) {
    .header-premium {
        padding-top: env(safe-area-inset-top);
        min-height: calc(var(--header-height) + env(safe-area-inset-top));
    }
    .sidebar-premium {
        padding-top: env(safe-area-inset-top);
        padding-bottom: env(safe-area-inset-bottom);
    }
    .app-content {
        padding-bottom: calc(2rem + env(safe-area-inset-bottom));
    }
}

/* ── Sidebar drawer mobile ──────────────────────────────────── */
.sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 190;
    background: rgba(21, 42, 78, 0.45);
    opacity: 0;
    transition: opacity 0.28s ease;
    -webkit-tap-highlight-color: transparent;
}

.sidebar-backdrop.is-visible {
    display: block;
    opacity: 1;
}

body.sidebar-scroll-lock {
    overflow: hidden;
    touch-action: none;
}

@media (max-width: 768px) {
    .sidebar-premium {
        width: var(--sidebar-drawer-width) !important;
        transform: translateX(-105%);
        transition: transform 0.32s cubic-bezier(0.4, 0, 0.2, 1);
        box-shadow: none;
        z-index: 210;
    }

    .app-shell.sidebar-open .sidebar-premium {
        transform: translateX(0);
        box-shadow: 4px 0 24px rgba(21, 42, 78, 0.18);
    }

    .app-shell.sidebar-open .sidebar-backdrop {
        display: block;
        opacity: 1;
    }

    /* Masquer le bouton collapse desktop sur mobile */
    .sidebar-toggle {
        display: none !important;
    }

    .nav-item-premium {
        min-height: var(--touch-min);
        padding: 0.75rem 1rem;
    }

    .sidebar-logout {
        min-height: var(--touch-min);
    }
}

/* ── Header mobile ──────────────────────────────────────────── */
.header-mobile-menu {
    display: none;
    align-items: center;
    justify-content: center;
    min-width: var(--touch-min);
    min-height: var(--touch-min);
    border: 1px solid var(--unc-border);
    border-radius: var(--radius-sm);
    background: var(--unc-white);
    color: var(--unc-navy);
    cursor: pointer;
    flex-shrink: 0;
    -webkit-tap-highlight-color: transparent;
}

.header-mobile-menu[aria-expanded="true"] {
    background: var(--unc-blue-soft);
    border-color: var(--unc-blue);
    color: var(--unc-blue);
}

.header-search-toggle {
    display: none;
    align-items: center;
    justify-content: center;
    min-width: var(--touch-min);
    min-height: var(--touch-min);
    border: 1px solid var(--unc-border);
    border-radius: var(--radius-sm);
    background: var(--unc-white);
    color: var(--unc-text-secondary);
    cursor: pointer;
    flex-shrink: 0;
}

@media (max-width: 768px) {
    .header-premium {
        flex-wrap: wrap;
        height: auto;
        min-height: var(--mobile-header-h);
        padding: 0.5rem 0.75rem;
        gap: 0.5rem;
    }

    .header-app-title {
        order: 2;
        flex: 1;
        min-width: 0;
        font-size: 0.85rem;
    }

    .header-app-title span {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .header-mobile-menu { display: flex; order: 1; }
    .header-search-toggle { display: flex; order: 3; }

    .header-search {
        order: 10;
        flex: 1 1 100%;
        max-width: none;
        display: none;
    }

    .header-search.is-open {
        display: block;
        animation: slideDownSearch 0.22s ease;
    }

    .header-search-input {
        min-height: var(--touch-min);
        font-size: 16px; /* évite zoom iOS */
    }

    .header-actions {
        order: 4;
        margin-left: auto;
        gap: 0.35rem;
    }

    .header-icon-btn,
    .header-profile {
        min-width: var(--touch-min);
        min-height: var(--touch-min);
    }

    .header-profile {
        padding: 0.25rem;
    }
}

@keyframes slideDownSearch {
    from { opacity: 0; transform: translateY(-6px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Barre recherche fixe contextuelle (listes) */
.mobile-search-bar {
    position: sticky;
    top: calc(var(--mobile-header-h) + env(safe-area-inset-top, 0px));
    z-index: 90;
    background: var(--unc-bg-elevated, #fff);
    border-bottom: 1px solid var(--unc-border);
    padding: 0.65rem 0.75rem;
    margin: -0.5rem -0.75rem 1rem;
    display: none;
}

@media (max-width: 768px) {
    .mobile-search-bar.is-active {
        display: block;
    }

    .mobile-search-bar .form-control {
        min-height: var(--touch-min);
        font-size: 16px;
    }
}

/* ── Cibles tactiles globales ───────────────────────────────── */
@media (max-width: 1024px) {
    .btn,
    .btn-sm,
    .btn-lg,
    .form-control,
    select.form-control,
    .filter-chip,
    .quick-access-btn,
    .level-btn,
    .program-btn,
    .visit-mode-tab,
    .pagination a,
    .pagination .btn {
        min-height: var(--touch-min);
    }

    .btn-sm {
        padding: 0.5rem 0.85rem;
        font-size: 0.875rem;
    }

    .header-icon-btn {
        width: var(--touch-min);
        height: var(--touch-min);
    }

    .prospect-actions {
        display: flex;
        flex-wrap: wrap;
        gap: 0.35rem;
    }

    .prospect-actions .btn {
        flex: 1 1 auto;
        min-width: 5rem;
    }
}

/* ── Formulaires mobile ─────────────────────────────────────── */
@media (max-width: 768px) {
    .form-group {
        gap: 0.5rem;
    }

    .form-control,
    .form-control-lg,
    textarea.form-control,
    select.form-control {
        min-height: var(--touch-min);
        padding: 0.65rem 0.85rem;
        font-size: 16px;
        border-radius: var(--radius, 8px);
    }

    .form-row,
    .form-row-agent,
    .filter-row-wrap,
    .admin-filters-form .filter-row-wrap,
    .report-filters .filter-row-wrap {
        grid-template-columns: 1fr !important;
        gap: 0.65rem;
    }

    .login-form .btn,
    .auth-form-panel .btn {
        min-height: 48px;
        width: 100%;
        font-size: 1rem;
    }

    .auth-form-panel {
        padding: 1.5rem 1.25rem;
    }

    .auth-premium-form-side {
        padding: 1rem;
    }
}

/* ── Tableaux de bord — grilles KPI ─────────────────────────── */
@media (min-width: 1025px) {
    .kpi-grid-premium,
    .admin-kpis,
    .agent-stats,
    .kpi-grid {
        grid-template-columns: repeat(4, 1fr);
    }
}

@media (min-width: 1440px) {
    .kpi-grid-premium,
    .admin-kpis,
    .agent-stats {
        grid-template-columns: repeat(6, 1fr);
    }
}

@media (min-width: 769px) and (max-width: 1024px) {
    .kpi-grid-premium,
    .admin-kpis,
    .agent-stats,
    .kpi-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (max-width: 768px) {
    .kpi-grid-premium,
    .admin-kpis,
    .agent-stats,
    .kpi-grid,
    .report-kpi-grid {
        grid-template-columns: 1fr !important;
    }
}

/* ── Page headers ───────────────────────────────────────────── */
@media (max-width: 768px) {
    .page-header,
    .page-header-premium {
        flex-direction: column;
        align-items: stretch !important;
        gap: 0.75rem;
    }

    .page-header h1,
    .page-header-premium h1 {
        font-size: 1.25rem !important;
    }

    .page-header .btn,
    .page-header-premium .btn,
    .report-actions,
    .report-form-actions {
        display: flex;
        flex-direction: column;
        width: 100%;
        gap: 0.5rem;
    }

    .page-header .btn,
    .report-actions .btn,
    .report-actions a.btn {
        width: 100%;
        justify-content: center;
        text-align: center;
    }
}

/* ── Tableaux — scroll horizontal ───────────────────────────── */
.table-responsive {
    -webkit-overflow-scrolling: touch;
    overflow-x: auto;
    margin-bottom: 0.5rem;
}

.table-responsive::-webkit-scrollbar {
    height: 6px;
}

.table-responsive::-webkit-scrollbar-thumb {
    background: var(--unc-border, #cbd5e1);
    border-radius: 3px;
}

@media (max-width: 768px) {
    .table-responsive:not(.table-cards-mode) .table {
        min-width: 640px;
    }

    .table-responsive:not(.table-cards-mode) .table th {
        position: sticky;
        top: 0;
        z-index: 2;
        white-space: nowrap;
    }
}

/* ── Tableaux — mode Carte (mobile) ─────────────────────────── */
@media (max-width: 768px) {
    .table-responsive.table-cards-mode {
        overflow: visible;
    }

    .table-responsive.table-cards-mode .table {
        min-width: 0;
        border: none;
    }

    .table-responsive.table-cards-mode .table thead {
        display: none;
    }

    .table-responsive.table-cards-mode .table tbody {
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
    }

    .table-responsive.table-cards-mode .table tbody tr {
        display: block;
        background: var(--unc-bg-elevated, #fff);
        border: 1px solid var(--unc-border);
        border-radius: var(--radius-lg, 12px);
        padding: 0.85rem 1rem;
        box-shadow: var(--shadow-xs);
    }

    .table-responsive.table-cards-mode .table tbody tr:empty,
    .table-responsive.table-cards-mode .table tbody tr td[colspan] {
        display: block;
        text-align: center;
    }

    .table-responsive.table-cards-mode .table td {
        display: flex;
        align-items: flex-start;
        justify-content: space-between;
        gap: 0.75rem;
        padding: 0.4rem 0;
        border: none;
        font-size: 0.875rem;
        text-align: right;
    }

    .table-responsive.table-cards-mode .table td::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--unc-text-muted);
        font-size: 0.75rem;
        text-transform: uppercase;
        letter-spacing: 0.03em;
        text-align: left;
        flex-shrink: 0;
        max-width: 45%;
    }

    .table-responsive.table-cards-mode .table td.prospect-actions,
    .table-responsive.table-cards-mode .table td.actions-cell {
        flex-direction: column;
        align-items: stretch;
        padding-top: 0.65rem;
        margin-top: 0.35rem;
        border-top: 1px solid var(--unc-border);
    }

    .table-responsive.table-cards-mode .table td.prospect-actions::before,
    .table-responsive.table-cards-mode .table td.actions-cell::before {
        margin-bottom: 0.25rem;
    }

    .table-responsive.table-cards-mode .table td[colspan]::before {
        display: none;
    }

    .table-responsive.table-cards-mode .table td[colspan] {
        justify-content: center;
        text-align: center;
    }
}

/* Toggle vue tableau / cartes */
.table-view-toggle {
    display: none;
    gap: 0.35rem;
    margin-bottom: 0.75rem;
}

@media (max-width: 768px) {
    .table-view-toggle {
        display: flex;
    }

    .table-view-toggle .btn {
        flex: 1;
    }
}

/* ── Rapports mobile ────────────────────────────────────────── */
@media (max-width: 768px) {
    .report-module-form {
        padding: 1rem;
    }

    .report-type-grid {
        grid-template-columns: 1fr;
    }

    .report-type-card {
        min-height: var(--touch-min);
    }

    .report-document {
        padding: 1rem;
        border-radius: var(--radius-md);
    }

    .report-doc-brand {
        flex-direction: column;
        text-align: center;
    }

    .report-doc-meta-grid {
        grid-template-columns: 1fr 1fr;
    }

    .report-doc-title-block h1 {
        font-size: 1.15rem;
    }
}

@media (max-width: 480px) {
    .report-doc-meta-grid {
        grid-template-columns: 1fr;
    }
}

/* ── Orientation / agent dashboard ──────────────────────────── */
@media (max-width: 768px) {
    .orientation-grid,
    .programs-grid,
    .level-selector,
    .quick-access-grid {
        grid-template-columns: 1fr !important;
    }

    .program-info-panel {
        padding: 1rem;
    }

    .agent-table-wrap.table-cards-mode .agent-table {
        min-width: 0;
    }

    .modal-content,
    .agent-modal-content {
        width: calc(100vw - 2rem) !important;
        max-width: none !important;
        margin: 1rem;
        max-height: calc(100vh - 2rem);
        overflow-y: auto;
    }
}

/* ── Admin module ───────────────────────────────────────────── */
@media (max-width: 768px) {
    .admin-visits-table {
        white-space: normal;
    }

    .grid-2,
    .grid-3 {
        grid-template-columns: 1fr !important;
    }

    .notif-panel {
        width: calc(100vw - 1.5rem) !important;
        right: 0.75rem !important;
        left: 0.75rem !important;
    }

    .pagination {
        flex-wrap: wrap;
        justify-content: center;
    }
}

/* ── Auth connexion ─────────────────────────────────────────── */
@media (max-width: 480px) {
    .auth-visual-logo {
        max-width: 100px;
    }

    .login-footer {
        font-size: 0.75rem !important;
        line-height: 1.5;
    }
}

/* ── Images lazy ────────────────────────────────────────────── */
img[loading="lazy"] {
    background: var(--unc-bg, #f5f7fa);
}

/* ── Accessibilité mobile ───────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
    .sidebar-premium,
    .sidebar-backdrop,
    .header-search.is-open {
        transition: none !important;
        animation: none !important;
    }
}

/* Focus visible renforcé */
@media (max-width: 768px) {
    :focus-visible {
        outline: 3px solid var(--unc-blue);
        outline-offset: 2px;
    }
}

/* ── Breakpoints intermédiaires ─────────────────────────────── */
@media (max-width: 390px) {
    .app-content {
        padding: 1rem 0.75rem 1.5rem;
    }

    .header-app-title span {
        max-width: 140px;
    }
}

@media (min-width: 769px) and (max-width: 820px) {
    .app-content {
        padding: 1.5rem 1.25rem;
    }
}

@media (min-width: 821px) and (max-width: 1024px) {
    .header-search {
        max-width: 320px;
    }
}
