/* --- Style Minimaliste iOS & Structure Impression --- */
:root {
    --ios-blue: #007aff;
    --ios-green: #34c759;
    --ios-orange: #ff9500;
    --ios-red: #ff3b30;
    --ios-grey: #8e8e93;
    --ios-background: #f2f2f7;
    --radius-lg: 12px;
    --radius-md: 8px;
    --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);
    --border-color: #d1d1d6;
    --text-muted: #8e8e93;
    --text-primary: #000;
    --accent: #ff9500;
    --indigo: #4338ca;
    --indigo-bg: #eef2ff;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--ios-background);
    color: #000;
    margin: 0;
    padding-bottom: 80px;
}

.header {
    background: #fff;
    padding: 15px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: sticky;
    top: 0;
    z-index: 100;
}

.app-logo {
    height: 44px;
    width: auto;
    object-fit: contain;
    max-width: 180px;
    display: block;
    cursor: pointer;
}

.container {
    max-width: 600px;
    margin: 0 auto;
    padding: 15px;
}

.card {
    background: white;
    border-radius: var(--radius-lg);
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: var(--shadow);
}

.card-header {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 15px;
    padding-bottom: 10px;
    border-bottom: 1px solid #f2f2f7;
}
.card-header h2 { font-size: 0.9rem; font-weight: 700; text-transform: uppercase; margin: 0; color: var(--ios-grey); }
.card-header i { color: var(--ios-blue); }

/* Réglages Admin : sections repliables */
.settings-section { margin-bottom: 1.2rem; }
.settings-section-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 0;
    font-size: 0.9rem;
    font-weight: 700;
    text-transform: uppercase;
    color: var(--ios-grey);
    cursor: pointer;
    list-style: none;
    border-bottom: 1px solid #f2f2f7;
}
.settings-section-header::-webkit-details-marker { display: none; }
.settings-section-header::after {
    content: '▸';
    margin-left: auto;
    font-size: 0.8rem;
    transition: transform 0.15s ease;
}
.settings-section[open] > .settings-section-header::after {
    transform: rotate(90deg);
}
.settings-section-header i { color: var(--ios-blue); }
.settings-section-body { padding-top: 12px; }
.settings-section.danger > .settings-section-header { color: #b91c1c; }
.settings-section.danger > .settings-section-header i { color: #b91c1c; }

.badge {
    padding: 6px 12px;
    border-radius: 12px;
    font-size: 0.85rem;
    font-weight: 600;
    display: inline-block;
}

.badge-blue    { background: #e1f0ff; color: var(--ios-blue); }
.badge-orange  { background: #fff4e5; color: var(--ios-orange); }
.badge-green   { background: #e2f9e9; color: var(--ios-green); }
/* Badges statut colis */
.badge-attente { background: #fff4e5; color: #c2410c; border: 1px solid #fdba74; }
.badge-transit { background: #dbeafe; color: #1d4ed8; border: 1px solid #93c5fd; }
.badge-hub     { background: #f3e8ff; color: #7c3aed; border: 1px solid #c4b5fd; }
.badge-scanne  { background: #f1f5f9; color: #475569; border: 1px solid #cbd5e1; }
.badge-livre   { background: #dcfce7; color: #15803d; border: 1px solid #86efac; }
.badge-erreur  { background: #fee2e2; color: #b91c1c; border: 1px solid #fca5a5; }
.badge-relais  { background: #eef2ff; color: #4338ca; border: 1px solid #a5b4fc; }
.badge-force   { background: #ffedd5; color: #c2410c; border: 1px solid #fdba74; }
.badge-annule  { background: #f1f5f9; color: #64748b; border: 1px solid #cbd5e1; }

.btn {
    border: none;
    border-radius: var(--radius-md);
    padding: 12px 20px;
    font-weight: 600;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: opacity 0.2s;
    font-size: 0.95rem;
}

.btn:active    { opacity: 0.7; }
.driver-scan-btn:active { opacity: 0.8; }
.btn-primary   { background: var(--ios-blue); color: white; }
.btn-secondary { background: #e5e5ea; color: #000; }
.btn-accent    { background: var(--ios-orange); color: white; }
.btn-danger    { background: #fee2e2; color: var(--ios-red); }

.tabs {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(249, 249, 249, 0.94);
    backdrop-filter: blur(24px) saturate(180%);
    -webkit-backdrop-filter: blur(24px) saturate(180%);
    border-top: 0.5px solid rgba(0, 0, 0, 0.14);
    display: flex;
    align-items: center;
    padding: 8px 10px calc(env(safe-area-inset-bottom, 0px) + 8px);
    gap: 6px;
    z-index: 1000;
}

.tab-btn {
    flex: 1;
    background: transparent;
    border: none;
    border-radius: 14px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--ios-grey);
    font-size: 0.67rem;
    font-weight: 500;
    gap: 3px;
    cursor: pointer;
    padding: 8px 6px 7px;
    transition: background 0.18s ease, color 0.18s ease, transform 0.1s ease;
    -webkit-tap-highlight-color: transparent;
}

.tab-btn:active { transform: scale(0.94); }

.tab-btn.active {
    background: var(--ios-blue);
    color: #fff;
}

.tab-btn:not(.active):hover {
    background: rgba(0, 0, 0, 0.05);
}

.form-group { margin-bottom: 15px; }
label { display: block; font-size: 0.85rem; font-weight: 600; margin-bottom: 5px; color: #48484a; }
input, select, textarea {
    width: 100%;
    padding: 12px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    font-size: 1rem;
    box-sizing: border-box;
    background: #fff;
}

.table-responsive { overflow-x: auto; margin: 0 -20px; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 0.75rem; color: var(--ios-grey); padding: 10px 15px; border-bottom: 1px solid var(--border-color); background: #f8f8f8; }
td { padding: 10px 15px; border-bottom: 1px solid #f2f2f7; font-size: 0.85rem; }

.fab {
    position: fixed;
    right: 20px;
    bottom: 100px;
    width: 64px;
    height: 64px;
    border-radius: 32px;
    background: var(--ios-orange);
    color: white;
    border: none;
    box-shadow: 0 4px 12px rgba(255, 149, 0, 0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 999;
    cursor: pointer;
}

.modal-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.5);
    display: none;
    align-items: center;
    justify-content: center;
    padding: 20px;
    z-index: 2000;
}

.modal-content {
    background: white;
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 450px;
    padding: 20px;
    max-height: 90vh;
    overflow-y: auto;
}

/* Planning hebdomadaire des navettes */
#header-planning-btn { display: none; }
.pl-grid-wrap { overflow-x: auto; }
.pl-week-grid {
    display: grid;
    grid-template-columns: 100px repeat(5, minmax(200px, 1fr));
    gap: 6px;
    min-width: 1120px;
}
.pl-cell { border-radius: var(--radius-md); padding: 8px; min-height: 36px; }
.pl-row-label { display: flex; flex-direction: column; justify-content: center; font-size: 12px; font-weight: 700; gap: 2px; padding: 8px 4px; }
.pl-row-label.nord { color: var(--ios-blue); }
.pl-row-label.sud  { color: var(--ios-orange); }
.pl-row-label .pl-hub-tag { font-size: 9px; font-weight: 600; color: var(--text-muted); text-transform: none; letter-spacing: 0; }
.pl-day-header {
    text-align: center; font-size: 12px; font-weight: 700; color: var(--text-muted);
    text-transform: uppercase; letter-spacing: .03em; padding: 8px 4px; border-radius: var(--radius-md);
}
.pl-day-header.today { color: var(--ios-orange); background: #fff4e0; }
.pl-today-tag { display: block; font-size: 9px; font-weight: 700; color: var(--ios-orange); margin-top: 2px; }
.pl-cell.nord { background: #eaf3ff; }
.pl-cell.sud  { background: #fff4e8; }
.pl-cell.today { outline: 2px solid var(--ios-orange); outline-offset: -2px; }
.pl-cell .pl-chips { display: flex; flex-direction: column; gap: 3px; }
.pl-chip {
    background: #fff; border: 1px solid var(--border-color); border-radius: 6px;
    font-size: 11px; font-weight: 600; padding: 4px 8px; white-space: normal; position: relative;
    display: block;
}
.pl-chip .code { font-weight: 700; }
.pl-chip .nom { font-weight: 400; color: var(--text-muted); margin-left: 3px; }
.pl-chip.me { background: #eafaf0; border-color: var(--ios-green); color: #137a36; font-weight: 700; }
.pl-chip.me .nom { color: #1f9c4d; }
.pl-chip.relais { border-color: var(--indigo); background: var(--indigo-bg); color: var(--indigo); }
.pl-chip.relais::after {
    content: "R"; display: inline-block; margin-left: 4px; background: var(--indigo); color: #fff;
    font-size: 8px; font-weight: 800; width: 13px; height: 13px; line-height: 13px;
    text-align: center; border-radius: 50%; vertical-align: middle;
}
.pl-chip[data-tipidx] { cursor: default; }
.pl-chip.hub { border-color: #0a7ea4; background: #e0f2fe; color: #075985; font-weight: 800; letter-spacing: .03em; }
.pl-chip.hub .nom { color: #0369a1; }
/* Tooltip repositionné en JS (position:fixed) — non rogné par le scroll du modal */
#pl-tooltip {
    position: fixed; z-index: 3000; display: none;
    background: #1c1c1e; color: #fff; font-size: 12px; line-height: 1.45;
    padding: 9px 12px; border-radius: 8px; max-width: 320px;
    white-space: pre-line; box-shadow: 0 6px 20px rgba(0,0,0,.35); pointer-events: none;
}
.pl-legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 11px; color: var(--text-muted); margin-bottom: 14px; }
.pl-legend span { display: flex; align-items: center; gap: 4px; }
.pl-dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }

.app-page { display: none; }
.app-page.active { display: block; }

.suggestions {
    background: white;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    position: absolute;
    width: 100%;
    z-index: 10;
    box-shadow: var(--shadow);
    max-height: 200px;
    overflow-y: auto;
}
.suggestion-item { padding: 12px; cursor: pointer; border-bottom: 1px solid #f2f2f7; }
.suggestion-item:hover { background: #f2f2f7; }
.suggestion-empty { padding: 12px; text-align: center; font-size: 0.82rem; color: var(--ios-grey); }

.loader {
    width: 16px;
    height: 16px;
    border: 2px solid #FFF;
    border-bottom-color: transparent;
    border-radius: 50%;
    display: inline-block;
    animation: rotation 1s linear infinite;
}
@keyframes rotation { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }

/* Checkbox agences */
.agency-checkbox { width: 18px; height: 18px; cursor: pointer; accent-color: var(--ios-blue); }

/* Tiers inférieur — affichage écran */
.pz-bottom-third {
    height: 99mm;
    border-top: 1px dashed #000;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    background-color: #ffffff;
    box-sizing: border-box;
    padding: 5mm 14mm;
}

/* --- ZONE IMPRESSION — masquée à l'écran --- */
#print-zone { display: none; }

/* --- PROTECTION IMPRESSION A4 & ÉCONOMIE D'ENCRE OBLIGATOIRE --- */
@media print {
    @page { size: A4 portrait; margin: 0; }

    /* Forcer fond blanc pur sur tous les éléments */
    html, body, div, section, article, header, footer,
    table, thead, tbody, tr, th, td,
    h1, h2, h3, h4, h5, h6, p, span, strong, em, li {
        background: #ffffff !important;
        background-color: #ffffff !important;
        background-image: none !important;
        color: #000000 !important;
        box-shadow: none !important;
        text-shadow: none !important;
    }

    /* Masquer tout l'UI applicatif */
    .no-print,
    .tabs,
    .fab,
    .header,
    #active-role,
    .nav-tabs,
    .container,
    .modal-overlay,
    .suggestions {
        display: none !important;
        visibility: hidden !important;
    }

    body * { visibility: hidden; }

    /* Afficher uniquement la zone d'impression */
    #print-zone,
    #print-zone * {
        display: block !important;
        visibility: visible !important;
    }

    #print-zone {
        position: fixed;
        top: 0;
        left: 0;
        width: 210mm;
        min-height: 297mm;
        margin: 0;
        padding: 12mm 14mm;
        box-sizing: border-box;
        font-family: Arial, Helvetica, sans-serif;
        font-size: 11pt;
        color: #000 !important;
        background: #fff !important;
    }

    .page-break { page-break-before: always; }

    /* Styles spécifiques au contenu imprimé */
    .pz-section-box {
        border: 1.5pt solid #000 !important;
        background: #fff !important;
    }
    .pz-table th,
    .pz-table td {
        border: 1pt solid #000 !important;
        background: #fff !important;
        color: #000 !important;
    }
    .pz-sig-box {
        border: 1.5pt solid #000 !important;
        background: #fff !important;
    }
    .pz-qr-img {
        display: block !important;
    }

    /* Restaurer l'affichage des tables (écrasé par display:block!important) */
    #print-zone table    { display: table      !important; }
    #print-zone thead,
    #print-zone tbody    { display: table-row-group !important; }
    #print-zone tr       { display: table-row  !important; }
    #print-zone td,
    #print-zone th       { display: table-cell !important; vertical-align: top !important; }

    /* Restaurer les éléments inline (écrasés par display:block!important) */
    #print-zone span,
    #print-zone strong,
    #print-zone em,
    #print-zone b { display: inline !important; }

    /* Restaurer les flex/grid spécifiques (sélecteur plus spécifique que #print-zone *) */
    #print-zone .pz-tier1        { display: flex !important; flex-direction: column !important; justify-content: flex-start !important; }
    #print-zone .pz-logo-row     { display: flex !important; align-items: center !important; position: relative !important; }
    #print-zone .pz-flex-row     { display: flex !important; justify-content: space-between !important; }
    #print-zone .pz-flex-cols    { display: flex !important; align-items: flex-start !important; }
    #print-zone .pz-arrow-center { display: flex !important; flex-direction: column !important; align-items: center !important; justify-content: center !important; }
    #print-zone .pz-sig-grid     { display: grid !important; grid-template-columns: 1fr 1fr !important; gap: 6mm !important; }

    /* Éléments vraiment cachés à l'impression */
    #print-zone .pz-hidden { position: absolute !important; width: 0 !important; height: 0 !important; overflow: hidden !important; font-size: 0 !important; line-height: 0 !important; opacity: 0 !important; }

    /* Tiers inférieur — positionnement absolu à 198mm du haut de page */
    .pz-bottom-third {
        position: absolute !important;
        top: 198mm !important;
        left: 0 !important;
        right: 0 !important;
        height: 99mm !important;
        border-top: 1px dashed #000 !important;
        display: flex !important;
        flex-direction: column !important;
        justify-content: center !important;
        align-items: center !important;
        text-align: center !important;
        background-color: #ffffff !important;
        box-sizing: border-box !important;
        padding: 5mm 14mm !important;
    }
}

/* =====================================================
   MODE CHAUFFEUR (NAVETTE)
   ===================================================== */
#driver-view {
    display: none;
    flex-direction: column;
    height: calc(100vh - 75px);
    height: calc(100dvh - 75px);
    overflow: hidden;
}

.driver-tabs {
    display: flex;
    background: #fff;
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

.driver-tab-btn {
    flex: 1;
    padding: 12px 10px 10px;
    border: none;
    background: transparent;
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--ios-grey);
    cursor: pointer;
    border-bottom: 3px solid transparent;
    transition: color 0.15s, border-color 0.15s;
    -webkit-tap-highlight-color: transparent;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
}

.driver-tab-btn.active {
    color: var(--ios-blue);
    border-bottom-color: var(--ios-blue);
}

#driver-scan-page {
    flex: 1;
    overflow: hidden;
}

#driver-suivi-page {
    flex: 1;
    overflow-y: auto;
    padding: 15px;
    background: var(--ios-background);
}

.driver-toast {
    position: fixed;
    bottom: 40px;
    left: 50%;
    transform: translateX(-50%);
    padding: 13px 22px;
    border-radius: 50px;
    font-weight: 600;
    font-size: 0.9rem;
    z-index: 9999;
    display: none;
    white-space: nowrap;
    box-shadow: 0 4px 20px rgba(0,0,0,0.35);
    max-width: calc(100vw - 40px);
    text-align: center;
}

.driver-toast-success { background: var(--ios-green); color: #fff; }
.driver-toast-error   { background: var(--ios-red);   color: #fff; }
.driver-toast-warning { background: var(--ios-orange); color: #fff; }

.badge-en-transit { background: #dbeafe; color: #1d4ed8; border: 1px solid #93c5fd; }
.badge-depose     { background: #f3e8ff; color: #7c3aed; border: 1px solid #c4b5fd; }
.badge-livre-nav  { background: #dcfce7; color: #15803d; border: 1px solid #86efac; }

/* =====================================================
   LAYOUT DESKTOP (body.desktop)
   ===================================================== */
body.desktop .container {
    max-width: 980px;
    padding: 20px 30px;
}
body.desktop .card {
    padding: 24px;
}
body.desktop table th,
body.desktop table td {
    padding: 12px 18px;
}
body.desktop .modal-content {
    max-width: 560px;
}
body.desktop #page-create.active {
    display: flex;
    flex-direction: column;
    max-width: 760px;
    margin: 0 auto;
    width: 100%;
}
body.desktop .table-responsive {
    overflow-x: visible;
}
/* Scanner caméra inutile sur poste fixe */
body.desktop .fab {
    display: none !important;
}
/* Admin card et stats visibles sur desktop seulement */
body.mobile #admin-card,
body.mobile #admin-stats-card {
    display: none !important;
}
/* QR header : masqué sur mobile */
body.mobile #header-qr-wrap {
    display: none !important;
}
/* Tableaux de suivi → cartes empilées "Label : Valeur" sur mobile */
body.mobile .table-responsive:not(.no-stack) table,
body.mobile .table-responsive:not(.no-stack) tbody,
body.mobile .table-responsive:not(.no-stack) tr {
    display: block;
    width: 100%;
}
body.mobile .table-responsive:not(.no-stack) thead { display: none; }
body.mobile .table-responsive:not(.no-stack) tr {
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 8px 12px;
    margin-bottom: 10px;
}
body.mobile .table-responsive:not(.no-stack) td {
    display: grid;
    grid-template-columns: 110px 1fr;
    gap: 8px;
    align-items: start;
    border-bottom: none;
    padding: 5px 0;
}
body.mobile .table-responsive:not(.no-stack) td::before {
    content: attr(data-label);
    font-weight: 600;
    font-size: 0.72rem;
    color: var(--ios-grey);
    text-transform: uppercase;
}
/* Lignes "Aucun envoi" / "Chargement..." (colspan) : pas de label, centré */
body.mobile .table-responsive:not(.no-stack) td[colspan] {
    grid-template-columns: 1fr;
    text-align: center;
}
body.mobile .table-responsive:not(.no-stack) td[colspan]::before { content: none; }
/* Overlay QR mobile (no-role) */
#mobile-qr-overlay {
    display: none;
    position: fixed; inset: 0; z-index: 2000;
    background: var(--ios-background);
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 30px;
    text-align: center;
}
#mobile-qr-overlay.visible { display: flex; }
#mobile-qr-overlay .mqr-icon {
    width: 90px; height: 90px;
    background: var(--ios-blue);
    border-radius: 22px;
    display: flex; align-items: center; justify-content: center;
    margin-bottom: 28px;
}
#mobile-qr-overlay h2 { font-size: 1.3rem; margin: 0 0 12px; color: var(--text-primary); }
#mobile-qr-overlay p  { font-size: 0.9rem; color: var(--text-muted); line-height: 1.5; max-width: 280px; margin: 0; }
/* Modal login desktop */
#desktop-login-modal .login-logo {
    display: block; height: 48px; margin: 0 auto 6px;
}
#desktop-login-modal h2 {
    text-align: center; font-size: 1.2rem; margin: 0 0 22px; color: var(--text-primary);
}
#login-error {
    display: none; color: #dc2626; font-size: 0.85rem;
    text-align: center; margin-bottom: 12px;
    background: #fef2f2; padding: 8px; border-radius: 6px;
}
/* Section admins dans Réglages */
.admins-table { width: 100%; font-size: 0.8rem; border-collapse: collapse; }
.admins-table th, .admins-table td { padding: 8px; border-bottom: 1px solid var(--border-color); text-align: left; }
.admins-table th { background: #f9fafb; font-weight: 600; }
