/* ============================================================
   APP SHELL — CENTRE DE COMMANDE
   Layout unifié : sidebar + topbar + content-area
   Source de vérité layout. Utilise les variables de charte.css.
   Usage: <link rel="stylesheet" href="charte.css">
          <link rel="stylesheet" href="app-shell.css">
   ============================================================ */

/* === SKIP NAV (WCAG) === */
.skip-nav {
    position: absolute; top: -100%; left: 16px;
    background: var(--accent-green); color: var(--on-accent);
    padding: 8px 16px; border-radius: var(--radius-sm);
    font-weight: 600; z-index: 9999;
    transition: top 0.2s;
}
.skip-nav:focus { top: 12px; }

/* === APP LAYOUT === */
.app-layout {
    display: flex;
    min-height: 100vh;
}

/* === SIDEBAR === */
.app-sidebar {
    width: 260px;
    min-width: 260px;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 150;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.app-sidebar.collapsed {
    transform: translateX(-260px);
}

/* Sidebar Header (Logo + Brand) */
.sidebar-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 20px 20px 16px;
    border-bottom: 1px solid var(--border-color);
}
.sidebar-logo {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: var(--accent-green);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 800;
    font-size: 0.9rem;
    color: var(--on-accent);
    flex-shrink: 0;
}
.sidebar-brand {
    display: flex;
    flex-direction: column;
}
.sidebar-brand-name {
    font-size: 0.95rem;
    font-weight: 700;
    color: var(--text-primary);
    line-height: 1.2;
}
.sidebar-brand-sub {
    font-size: 0.7rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Sidebar Navigation */
.sidebar-nav {
    flex: 1;
    overflow-y: auto;
    padding: 12px 0;
}
.sidebar-section {
    padding: 0 12px;
    margin-bottom: 8px;
}
.sidebar-section-title {
    font-size: 0.65rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 1px;
    padding: 12px 12px 6px;
}
.sidebar-link {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    font-size: 0.88rem;
    font-weight: 500;
    cursor: pointer;
    transition: var(--transition);
    text-decoration: none;
    position: relative;
}
.sidebar-link:hover {
    color: var(--text-primary);
    background: var(--accent-green-faint);
}
.sidebar-link.active {
    color: var(--accent-green);
    background: var(--accent-green-soft);
    font-weight: 600;
}
.sidebar-link i {
    width: 20px;
    text-align: center;
    font-size: 0.9rem;
    flex-shrink: 0;
}
.sidebar-link .badge-count {
    margin-left: auto;
    background: var(--accent-green);
    color: var(--on-accent);
    font-size: 0.65rem;
    font-weight: 700;
    padding: 2px 7px;
    border-radius: 10px;
    min-width: 20px;
    text-align: center;
}

/* Sidebar sub-items */
.sidebar-sub {
    display: none;
    padding-left: 32px;
}
.sidebar-sub.open {
    display: block;
}
.sidebar-sub .sidebar-link {
    font-size: 0.82rem;
    padding: 7px 12px;
}

/* Sidebar Footer (User Info) */
.sidebar-footer {
    padding: 16px 16px;
    border-top: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 10px;
}
.sidebar-avatar {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: var(--accent-green-soft);
    color: var(--accent-green);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 0.8rem;
    flex-shrink: 0;
}
.sidebar-user-info {
    flex: 1;
    overflow: hidden;
}
.sidebar-user-name {
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.sidebar-user-role {
    font-size: 0.7rem;
    color: var(--text-muted);
    text-transform: capitalize;
}
.sidebar-logout {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 6px;
    border-radius: var(--radius-sm);
    transition: var(--transition);
    font-size: 0.9rem;
}
.sidebar-logout:hover {
    color: var(--accent-red);
    background: rgba(248, 113, 113, 0.1);
}

/* === MAIN AREA === */
.app-main {
    flex: 1;
    margin-left: 260px;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    transition: margin-left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.app-sidebar.collapsed ~ .app-main {
    margin-left: 0;
}

/* === TOPBAR === */
.app-topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 24px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    position: sticky;
    top: 0;
    z-index: 100;
    min-height: 56px;
}
.app-topbar-left {
    display: flex;
    align-items: center;
    gap: 12px;
}
.app-topbar-title {
    font-size: 1.1rem;
    font-weight: 700;
}
.app-topbar-title span {
    color: var(--accent-green);
}
.app-topbar-breadcrumb {
    font-size: 0.78rem;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    gap: 6px;
}
.app-topbar-breadcrumb a {
    color: var(--text-secondary);
    transition: var(--transition);
}
.app-topbar-breadcrumb a:hover {
    color: var(--accent-green);
}
.app-topbar-right {
    display: flex;
    align-items: center;
    gap: 10px;
}

/* Hamburger (mobile) */
.hamburger {
    display: none;
    background: none;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 6px 10px;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 1.1rem;
    transition: var(--transition);
}
.hamburger:hover {
    color: var(--accent-green);
    border-color: var(--accent-green);
}

/* Search bar in topbar */
.app-search {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 6px 12px;
    max-width: 280px;
}
.app-search input {
    background: transparent;
    border: none;
    outline: none;
    color: var(--text-primary);
    font-size: 0.85rem;
    font-family: var(--font-sans);
    width: 100%;
}
.app-search i {
    color: var(--text-muted);
    font-size: 0.85rem;
}

/* Notification bell */
.app-notif {
    position: relative;
    background: none;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 6px 10px;
    color: var(--text-secondary);
    cursor: pointer;
    transition: var(--transition);
}
.app-notif:hover {
    color: var(--accent-green);
    border-color: var(--accent-green);
}
.app-notif-dot {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--accent-red);
}

/* === CONTENT AREA === */
.app-content {
    flex: 1;
    padding: 24px;
    max-width: 1600px;
    width: 100%;
    margin: 0 auto;
    overflow-x: hidden;
    min-width: 0;
}

/* === PAGE BANNER — Hero full-width en tête de module (2026-04-24) ===
   Pattern réutilisable : image + overlay gradient + titre+pitch blanc.
   Injecté dynamiquement par app-shell.js::renderBanner() pour les modules
   non-tabbed, ou inline dans mod-soc.html pour la Vue globale. */
.page-banner {
    position: relative;
    height: 180px;
    margin: -24px -24px 24px -24px;
    overflow: hidden;
    background: var(--bg-tertiary);
}
.page-banner img {
    position: absolute; inset: 0;
    width: 100%; height: 100%;
    object-fit: cover;
    object-position: center;
    display: block;
}
.page-banner-overlay {
    position: absolute; inset: 0;
    background: linear-gradient(135deg, rgba(79,70,229,0.35) 0%, rgba(79,70,229,0) 42%, rgba(15,23,42,0.7) 100%);
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    padding: 20px 32px;
}
.page-banner-overlay h2 {
    color: #ffffff; font-size: 1.5rem; font-weight: 700;
    margin: 0 0 6px 0;
    display: flex; align-items: center; gap: 12px;
    text-shadow: 0 2px 8px rgba(0,0,0,0.55);
}
.page-banner-overlay h2 i {
    color: #c4b5fd;
    font-size: 1.3rem;
}
.page-banner-overlay p {
    color: rgba(255,255,255,0.92);
    font-size: 0.9rem;
    max-width: 680px;
    margin: 0;
    line-height: 1.45;
    text-shadow: 0 1px 4px rgba(0,0,0,0.55);
}
@media(max-width:640px){
    .page-banner { height: 130px; margin: -12px -12px 16px -12px; }
    .page-banner-overlay { padding: 14px 18px; }
    .page-banner-overlay h2 { font-size: 1.15rem; }
    .page-banner-overlay h2 i { font-size: 1rem; }
    .page-banner-overlay p { font-size: 0.8rem; }
}

/* Page header within content */
.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 24px;
    flex-wrap: wrap;
    gap: 12px;
}
.page-header h2 {
    font-size: 1.4rem;
    font-weight: 700;
}
.page-header h2 i {
    color: var(--accent-green);
    margin-right: 8px;
}
.page-actions {
    display: flex;
    gap: 8px;
    align-items: center;
}

/* === APP FOOTER === */
.app-footer {
    padding: 16px 24px;
    border-top: 1px solid var(--border-color);
    background: var(--bg-secondary);
    text-align: center;
    font-size: 0.75rem;
    color: var(--text-muted);
}
.app-footer a {
    color: var(--accent-green);
}

/* === OVERLAY (mobile sidebar) === */
.app-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    z-index: 140;
}
.app-overlay.active {
    display: block;
}

/* === ROLE-BASED VISIBILITY === */
[data-role-hide] {
    display: none !important;
}
[data-role-show] {
    display: none !important;
}

/* === LOADING SCREEN === */
.app-loader {
    position: fixed;
    inset: 0;
    background: var(--bg-primary);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    gap: 16px;
    transition: opacity 0.3s;
}
.app-loader.fade-out {
    opacity: 0;
    pointer-events: none;
}
.app-loader-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--border-color);
    border-top-color: var(--accent-green);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
.app-loader-text {
    font-size: 0.85rem;
    color: var(--text-muted);
}

/* === RESPONSIVE === */
@media (max-width: 1024px) {
    .app-sidebar {
        transform: translateX(-260px);
    }
    .app-sidebar.open {
        transform: translateX(0);
    }
    .app-main {
        margin-left: 0;
    }
    .hamburger {
        display: flex;
    }
    .app-search {
        max-width: 180px;
    }
}

@media (max-width: 768px) {
    .app-topbar {
        padding: 10px 16px;
    }
    .app-content {
        padding: 16px;
    }
    .app-search {
        display: none;
    }
    .page-header {
        flex-direction: column;
        align-items: flex-start;
    }
    .page-header h2 {
        font-size: 1.2rem;
    }
}

@media (max-width: 480px) {
    .app-content {
        padding: 12px;
    }
    .app-topbar-breadcrumb {
        display: none;
    }
}

/* === PROTECTION OVERFLOW (mobile) — H.2 Phase ============= */
/* Garantit qu'aucun élément ne déborde du viewport, sans casser
   le layout natif des tables (le scroll horizontal doit se faire
   sur le WRAPPER .table-wrap, pas sur le <table> lui-même). */
html, body {
    max-width: 100%;
    overflow-x: hidden;
}
.app-content, .app-main {
    max-width: 100%;
    overflow-x: hidden;
}
.app-content img,
.app-content video,
.app-content iframe,
.app-content canvas,
.app-content svg {
    max-width: 100%;
    height: auto;
}
/* Les tables conservent leur structure native (sinon les widths %
   et thead sticky cassent). Le scroll horizontal doit toujours se
   faire via le wrapper .table-wrap qui a overflow-x:auto. */
.app-content .table-wrap,
.app-content [class*="table-wrap"] {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
}
.app-content table {
    max-width: 100%;
    border-collapse: collapse;
}

/* === LANDSCAPE (paysage) === */
/* Quand le téléphone est en mode paysage, hauteur réduite → compacter
   verticalement + ÉLARGIR horizontalement. Layout KPIs/cartes en plus
   de colonnes pour afficher plus d'éléments d'un coup.
   Déclencheur : orientation paysage ET hauteur écran ≤ 600px (exclut
   les tablettes larges qui ont déjà assez de place). */
@media (orientation: landscape) and (max-height: 600px) {
    /* Topbar plus compacte pour libérer de l'espace vertical */
    .app-topbar {
        padding: 6px 14px;
        height: 48px;
    }
    .app-content {
        padding: 12px 16px;
    }
    /* Bannières module réduites (moins d'espace perdu en hauteur) */
    .page-banner {
        min-height: 120px !important;
        padding: 14px 18px !important;
    }
    .page-banner h1,
    .page-banner h2 {
        font-size: 1.15rem !important;
    }
    /* KPI row : 4 colonnes même sur mobile landscape (plus large que portrait) */
    .kpi-row {
        grid-template-columns: repeat(4, 1fr) !important;
        gap: 8px;
    }
    .kpi-card {
        padding: 10px 12px;
    }
    .kpi-value {
        font-size: 1.25rem !important;
    }
    .kpi-label {
        font-size: 0.7rem !important;
    }
    /* Cartes/sections denses */
    .page-header h2 {
        font-size: 1.05rem;
    }
    .page-header {
        margin-bottom: 12px;
    }
    /* Sidebar overlay plus étroite pour ne pas masquer tout le content */
    .app-sidebar {
        width: 220px;
    }
}

/* === PORTRAIT (mobile) === */
/* H.2 : "la même chose visuel sur mobile que sur PC" → on PRÉSERVE
   le visuel desktop autant que possible (auto-fit responsive plutôt
   que forcer 2 colonnes rigides). */
@media (orientation: portrait) and (max-width: 768px) {
    .kpi-row {
        grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)) !important;
        gap: 8px;
    }
    /* Cartes et formulaires en pleine largeur */
    .card, .panel, .chart-card {
        max-width: 100%;
        padding: 14px;
    }
    /* Tableaux : scroll horizontal sur le WRAPPER, pas sur le <table> */
    .table-wrap {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;  /* scroll smooth iOS */
    }
    /* Actions (boutons) prennent toute la largeur si plusieurs empilés */
    .page-actions .btn {
        min-width: 44px;  /* touch target Apple HIG */
        min-height: 44px;
    }
    /* Bannières adaptées portrait */
    .page-banner {
        min-height: 140px;
    }
    .page-banner h1 {
        font-size: 1.1rem;
    }
}

/* === MOBILE — modals plein-écran pour expérience native ====== */
/* Sur petit écran, les modals doivent occuper tout l'écran (pas
   coincés en petit centre avec marges) — UX native iOS/Android. */
@media (max-width: 640px) {
    /* Modals génériques (CRM, Tickets, Audit, etc.) */
    .crm-modal-overlay, .audit-modal-overlay, .modal-overlay {
        padding: 0 !important;
        align-items: stretch !important;
        justify-content: stretch !important;
    }
    .crm-modal, .audit-modal, .modal-content {
        max-width: 100% !important;
        max-height: 100% !important;
        width: 100% !important;
        height: 100% !important;
        border-radius: 0 !important;
        margin: 0 !important;
        display: flex;
        flex-direction: column;
    }
    .crm-modal-body, .modal-body, .audit-modal-body {
        flex: 1;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    /* Touch targets confortables */
    button, .btn, a.btn {
        min-height: 40px;
    }
    /* Inputs assez larges pour le doigt */
    input[type="text"], input[type="email"], input[type="tel"],
    input[type="number"], select, textarea {
        font-size: 16px !important; /* évite le zoom iOS auto */
    }
}

/* === MOBILE — sidebar drawer plein-écran ===================== */
@media (max-width: 640px) {
    .app-sidebar {
        width: 80vw;
        max-width: 320px;
    }
}

/* === SAFE AREA iOS (notch / barre nav virtuelle) === */
/* Respecte les safe zones des téléphones avec encoche ou barre nav tactile */
@supports (padding: env(safe-area-inset-top)) {
    .app-topbar {
        padding-top: calc(10px + env(safe-area-inset-top));
    }
    .app-content {
        padding-bottom: calc(20px + env(safe-area-inset-bottom));
    }
}
