:root {
    --primary: #0f172a;
    --primary-light: #1e293b;
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --bg: #f8fafc;
    --sidebar-bg: #0f172a;
    --card-bg: #ffffff;
    --text-main: #1e293b;
    --text-muted: #64748b;
    --border: #e2e8f0;
    --success: #10b981;
    --danger: #ef4444;
    --warning: #f59e0b;
    --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --radius: 8px;
}

* { box-sizing: border-box; outline: none; margin: 0; padding: 0; }

html, body {
    overflow-x: hidden !important;
    width: 100% !important;
    position: relative;
    max-width: 100vw;
}

body {
    font-family: 'Inter', system-ui, -apple-system, sans-serif;
    background-color: var(--bg);
    color: var(--text-main);
    line-height: 1.5;
}

/* --- LAYOUT --- */
.app-layout {
    display: flex;
    height: 100vh;
    width: 100% !important;
    overflow: hidden !important;
    max-width: 100vw;
}

.sidebar {
    width: 260px;
    background-color: var(--sidebar-bg);
    color: white;
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    overflow-x: hidden;
}

.content-area {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden !important;
    padding: 2rem;
    width: 100%;
    max-width: 100%;
}

/* --- SIDEBAR COMPONENTS --- */
.sidebar-header {
    padding: 1.5rem;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
}

.user-info {
    padding: 1rem 1.5rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    background: rgba(0,0,0,0.2);
}

.avatar {
    width: 40px;
    height: 40px;
    background: var(--accent);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
}

.site-selector-container {
    padding: 1rem 1.5rem;
    border-bottom: 1px solid rgba(255,255,255,0.1);
}

.site-selector-container label {
    display: block;
    font-size: 0.75rem;
    color: #94a3b8;
    margin-bottom: 0.5rem;
}

.site-selector-container select {
    width: 100%;
    padding: 0.5rem;
    background: #1e293b;
    border: 1px solid #334155;
    color: white;
    border-radius: 4px;
}

.nav-links {
    flex: 1;
    padding: 1rem 0;
    overflow-y: auto;
}

.nav-links button {
    width: 100%;
    padding: 0.75rem 1.5rem;
    background: transparent;
    border: none;
    color: #94a3b8;
    text-align: left;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 0.95rem;
    transition: 0.2s;
}

.nav-links button:hover, .nav-links button.active {
    background: rgba(255,255,255,0.05);
    color: white;
    border-left: 4px solid var(--accent);
}

.sidebar-footer {
    padding: 1rem;
    border-top: 1px solid rgba(255,255,255,0.1);
}

/* --- DASHBOARD / KPI --- */
.kpi-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.kpi-card {
    background: var(--card-bg);
    padding: 1.5rem;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    border-left: 4px solid var(--accent);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.kpi-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1);
}

.kpi-card h3 {
    margin: 0;
    font-size: 0.875rem;
    color: var(--text-muted);
    text-transform: uppercase;
}

.kpi-card .value {
    font-size: 1.75rem;
    font-weight: 700;
    margin-top: 0.5rem;
}

.charts-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
    gap: 1.5rem;
}

.chart-container {
    background: var(--card-bg);
    padding: 1.5rem;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    height: 350px;
}

/* --- TABLES --- */
.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
    flex-wrap: wrap;
    gap: 1rem;
}

.action-group {
    display: flex;
    gap: 8px;
    align-items: center;
}

.table-responsive {
    background: var(--card-bg);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.95rem;
}

th {
    background: #f8fafc;
    padding: 1rem;
    text-align: left;
    font-weight: 600;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border);
}

td {
    padding: 1rem;
    border-bottom: 1px solid var(--border);
}

tr:hover {
    background: #f8fafc;
}

/* --- BUTTONS --- */
button {
    cursor: pointer;
    transition: 0.2s;
}

.btn-primary {
    background: var(--accent);
    color: white;
    border: none;
    padding: 0.6rem 1.2rem;
    border-radius: 6px;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
}
.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary, .btn-export, .btn-template, .btn-import {
    background: #64748b;
    color: white;
    border: none;
    padding: 0.6rem 1.2rem;
    border-radius: 6px;
}
.btn-secondary:hover { background: #475569; }

.btn-danger {
    background: var(--danger);
    color: white;
    border: none;
    padding: 0.6rem 1.2rem;
    border-radius: 6px;
}

.btn-alert {
    background: var(--warning);
    color: white;
    border: none;
    padding: 0.6rem 1.2rem;
    border-radius: 6px;
}

.btn-plus {
    padding: 0.2rem 0.5rem;
    font-size: 0.9rem;
    background: var(--accent);
    color: white;
    border-radius: 4px;
    border: none;
}

/* --- MODALS --- */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.75);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    backdrop-filter: blur(4px);
}

.modal-content {
    background: white;
    padding: 2.5rem;
    border-radius: 12px;
    width: 90%;
    max-width: 700px;
    max-height: 90vh;
    overflow-y: auto;
    position: relative;
    box-shadow: 0 25px 50px -12px rgb(0 0 0 / 0.25);
}

.close {
    position: absolute;
    top: 1.5rem;
    right: 1.5rem;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--text-muted);
}

/* --- FORMS --- */
.form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.form-group { display: flex; flex-direction: column; gap: 0.5rem; }
.form-group label { font-size: 0.875rem; font-weight: 600; color: var(--text-main); }
.form-group input, .form-group select, .form-group textarea {
    padding: 0.75rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 0.95rem;
    width: 100%;
}
.form-group.full-width { grid-column: span 2; }

/* --- LOGIN SCREEN --- */
.login-container {
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--primary);
}

.login-box {
    background: white;
    padding: 3rem;
    border-radius: 16px;
    width: 100%;
    max-width: 400px;
    text-align: center;
    box-shadow: 0 20px 25px -5px rgb(0 0 0 / 0.1);
}

.login-box h1 { margin-bottom: 0.5rem; }
.login-box input { width: 100%; margin-bottom: 1rem; padding: 0.8rem; border: 1px solid var(--border); border-radius: 6px; }
.login-box button { width: 100%; padding: 0.8rem; border-radius: 6px; border: none; background: var(--accent); color: white; font-weight: 600; }

/* --- LOGO --- */
.app-logo {
    max-width: 100%;
    height: auto;
    max-height: 180px;
    margin-bottom: 30px;
    display: block;
    margin-left: auto;
    margin-right: auto;
}

.sidebar-header .app-logo {
    max-height: 50px;
    margin: 0 auto 10px auto;
}

/* --- BADGES --- */
.badge {
    display: inline-flex !important;
    justify-content: center;
    align-items: center;
    background-color: var(--danger) !important;
    color: white;
    font-size: 0.7rem;
    font-weight: bold;
    border-radius: 50%;
    min-width: 18px;
    height: 18px;
    padding: 0 4px;
    margin-left: 8px;
    vertical-align: super;
}
.badge-success { background-color: var(--success) !important; }

/* --- SEARCH BARS --- */
.search-bar-container {
    margin-bottom: 1.5rem;
    max-width: 500px;
}

.search-bar-container input {
    width: 100%;
    padding: 0.7rem 1rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: white;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    font-size: 0.95rem;
}

/* --- UTILS --- */
.hidden { display: none !important; }
.toast {
    position: fixed; bottom: 2rem; right: 2rem;
    background: var(--primary); color: white; padding: 1rem 2rem;
    border-radius: 8px; box-shadow: var(--shadow);
    transform: translateY(200%); transition: 0.3s; z-index: 3000;
}
.toast.show { transform: translateY(0); }
.toast.error { background: var(--danger); }

.loader-overlay {
    position: fixed; inset: 0; background: rgba(0,0,0,0.7);
    display: flex; flex-direction: column; align-items: center; justify-content: center; z-index: 9999;
}
.loader {
    width: 48px; height: 48px; border: 5px solid #FFF; border-bottom-color: var(--accent);
    border-radius: 50%; animation: rotation 1s linear infinite;
}
@keyframes rotation { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }

/* --- SYNC INDICATOR --- */
.sync-indicator {
    background: #f59e0b;
    color: white;
    width: 24px; height: 24px;
    display: flex; align-items: center; justify-content: center;
    border-radius: 50%; font-size: 0.8rem;
    animation: pulse-sync 2s infinite; cursor: help; margin-left: auto;
}
@keyframes pulse-sync {
    0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(245, 158, 11, 0.7); }
    70% { transform: scale(1.1); box-shadow: 0 0 0 8px rgba(245, 158, 11, 0); }
    100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(245, 158, 11, 0); }
}

#sig-canvas { touch-action: none; }
.role-restricted { display: none !important; }
.mobile-only { display: none; }

/* --- MOBILE SPECIFICS (ISOLATED) --- */
.mobile-nav { display: none; }
.menu-toggle { display: none; }

@media (max-width: 768px) {
    .mobile-only { display: block; }
    
    /* NEW TOP BAR MOBILE */
    .mobile-top-bar {
        position: fixed; top: 0; left: 0; right: 0; height: 60px;
        background: var(--primary); color: white;
        display: flex; align-items: center; justify-content: space-between;
        padding: 0 15px; z-index: 1100;
        box-shadow: 0 2px 10px rgba(0,0,0,0.2);
    }
    .menu-toggle-alt { background: none; border: none; color: white; font-size: 1.5rem; cursor: pointer; }
    .mobile-site-selector-container { flex: 1; margin: 0 15px; }
    .mobile-site-select { 
        width: 100%; background: rgba(255,255,255,0.15); border: 1px solid rgba(255,255,255,0.2);
        color: white; padding: 6px 10px; border-radius: 8px; font-size: 0.9rem;
    }
    .mobile-logo-mini { font-size: 1.2rem; font-weight: bold; color: var(--accent); }

    /* PORTAL MOBILE */
    .mobile-action-portal {
        margin-top: 15px;
        margin-bottom: 2rem;
        display: flex;
        flex-direction: column;
        gap: 12px;
    }

    .big-action-btn {
        width: 100%;
        padding: 25px;
        background: var(--accent);
        color: white;
        border: none;
        border-radius: 16px;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 15px;
        box-shadow: 0 10px 15px -3px rgba(59, 130, 246, 0.3);
    }
    .big-action-btn .icon { font-size: 2.5rem; }
    .big-action-btn .label { font-weight: 800; font-size: 1.1rem; letter-spacing: 1px; }

    .small-actions-grid {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 12px;
    }

    .action-card {
        background: white;
        padding: 15px;
        border-radius: 16px;
        border: 1px solid var(--border);
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 8px;
        box-shadow: var(--shadow);
    }
    .action-card .icon { font-size: 1.5rem; }
    .action-card .label { font-size: 0.75rem; font-weight: 600; color: var(--text-main); }

    .menu-toggle { 
        display: block; position: fixed; top: 1rem; left: 1rem; z-index: 1100;
        background: var(--primary); color: white; padding: 0.5rem; border-radius: 4px;
    }

    .sidebar {
        position: fixed; left: 0; top: 0; bottom: 0; z-index: 1050;
        transform: translateX(-100%); transition: transform 0.3s ease; width: 280px;
        display: flex !important;
    }
    .sidebar.open { transform: translateX(0); }

    /* Correction alignement contenu sous la barre fixe (60px) + Marge de sécurité */
    .content-area { padding: 90px 0.75rem 80px 0.75rem !important; }

    .sidebar-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 1040; display: none; }
    .sidebar-overlay.active { display: block; }

    .mobile-nav {
        display: grid; grid-template-columns: repeat(6, 1fr);
        position: fixed; bottom: 0; left: 0; width: 100%; height: 65px;
        background: white; border-top: 1px solid var(--border); z-index: 1000;
        box-shadow: 0 -2px 15px rgba(0,0,0,0.1);
    }
    .mobile-nav-item { display: flex; flex-direction: column; align-items: center; justify-content: center; background: none; border: none; color: var(--text-muted); padding: 0; }
    .mobile-nav-item.active { color: var(--accent); }
    .mobile-nav-item span { font-size: 1.2rem; }
    .mobile-nav-item small { font-size: 0.6rem; font-weight: 500; }

    .scan-btn { background: var(--accent); color: white !important; border-radius: 50%; width: 45px; height: 45px; margin-top: -20px; display: flex; align-items: center; justify-content: center; }

    /* ACTION BAR - FLEXIBLE & READABLE */
    .section-header { flex-direction: column; align-items: flex-start; gap: 1rem; margin-bottom: 1.5rem; }
    .section-header h1 { font-size: 1.5rem; }
    .action-group { 
        width: 100%; display: flex; flex-wrap: wrap; 
        gap: 8px; justify-content: flex-start;
    }
    .action-group button { 
        flex: 0 1 auto; 
        white-space: nowrap; 
        padding: 0.6rem 1rem; 
        font-size: 0.85rem;
    }

    /* TABLE TO CARDS (Proposition 1) */
    table, thead, tbody, th, td, tr { display: block; width: 100%; }
    thead tr { position: absolute; top: -9999px; left: -9999px; }
    
    tr {
        background: white; border: 1px solid var(--border); border-radius: 16px;
        margin-bottom: 1.2rem; box-shadow: 0 4px 12px rgba(0,0,0,0.05); overflow: hidden;
        cursor: pointer; position: relative;
    }
    tr:active { background: #f8fafc; transform: scale(0.98); transition: 0.1s; }

    /* Titre de la carte (Première colonne) */
    td:first-child {
        background: #f8fafc; font-weight: 700; color: var(--accent);
        text-align: left !important; padding: 1.2rem 1rem !important;
        border-bottom: 1px solid var(--border); font-size: 1.1rem;
        display: flex; align-items: center; justify-content: space-between;
    }
    td:first-child::after { content: "➔"; font-size: 0.9rem; opacity: 0.4; }
    td:first-child::before { display: none !important; }

    /* Autres colonnes */
    td {
        border: none; border-bottom: 1px solid #f8fafc; position: relative;
        padding: 0.8rem 1rem 0.8rem 45% !important;
        text-align: left !important; min-height: 45px;
        display: flex !important; align-items: center;
        white-space: normal; word-break: break-word;
    }

    td::before {
        content: attr(data-label); position: absolute; left: 1rem; width: 40%;
        text-align: left; font-weight: 600; color: var(--text-muted);
        font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.05em;
        display: inline-block;
    }

    /* Masquer la dernière colonne (Actions) sur la liste mobile car on clique sur la carte */
    td:last-child {
        display: none !important;
    }

    /* FORMS IN FULL SCREEN (Proposition 2) */
    .modal { z-index: 2000; overflow-x: hidden; }
    .modal-content { 
        width: 100% !important; height: 100% !important; max-height: 100% !important; 
        border-radius: 0; padding: 4.5rem 1rem 1.5rem 1rem !important; top: 0; left: 0; transform: none;
        overflow-x: hidden;
    }
    .modal-content h2 { font-size: 1.4rem; margin-bottom: 1.5rem; padding-right: 30px; text-align: left; }
    .form-grid { grid-template-columns: 1fr !important; gap: 1rem !important; width: 100%; }
    .form-group { width: 100%; margin: 0; padding: 0; }
    .form-group label { font-size: 0.8rem; margin-bottom: 4px; text-align: left; padding-left: 2px; }
    
    input, select, textarea { 
        width: 100% !important;
        padding: 0.75rem !important; 
        font-size: 16px !important; 
        box-sizing: border-box !important;
    }

    /* Spécifique : Alignement à gauche pour les Relevés / Mesures */
    #int-mesures { 
        align-items: flex-start !important; 
        padding-left: 0 !important; 
        margin-left: 0 !important;
    }
    #int-mesures > div { 
        width: 100% !important; 
        text-align: left !important; 
        justify-content: flex-start !important;
        padding-left: 0 !important;
    }

    /* Force responsive signature and elements */
    #sig-canvas { 
        width: 100% !important; 
        height: auto !important; 
        max-width: 100%; 
        box-sizing: border-box; 
    }
    canvas { max-width: 100%; }
}

@media print {
    body * { visibility: hidden; }
    #help, #help * { visibility: visible; }
    #help {
        position: absolute;
        left: 0; top: 0;
        width: 100%;
        background: white !important;
    }
    .action-group, .btn-primary, .menu-toggle, #app-sidebar, #sidebar-overlay, .mobile-nav {
        display: none !important;
    }
}
