:root {
    --primary: #3f4f5f;
    --accent: #3498db;
    --bg: #e9ecef;
    --paper: #ffffff;
    --danger: #e74c3c;
    --success: #2ecc71;
    --input-bg: #f8f9fa;
    --border-color: #dee2e6;
}

body { 
    font-family: 'Segoe UI', Arial, sans-serif; 
    background: var(--bg); margin: 0; padding: 20px; 
    color: #000; font-size: 12px;
}

.paper {
    background: var(--paper);
    width: 210mm; min-height: 297mm;
    margin: 0 auto; padding: 15mm; 
    box-sizing: border-box; 
    box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}

/* --- EN-TÊTE COMPACT --- */
header { display: flex; justify-content: space-between; gap: 20px; margin-bottom: 25px; align-items: flex-start; }
.header-col { display: flex; flex-direction: column; }
.left-col { width: 50%; }
.right-col { width: 45%; align-items: flex-end; text-align: right; }

.logo-wrapper { margin-bottom: 10px; }
.logo-placeholder { 
    width: 100px; height: 80px; 
    background: var(--input-bg); border: 1px dashed #ccc; 
    display: flex; flex-direction: column; align-items: center; justify-content: center; 
    cursor: pointer; font-size: 10px; color: #888; border-radius: 4px;
}
.logo-placeholder img { width: 100%; height: 100%; object-fit: contain; }

textarea, input, select { 
    font-family: inherit; font-size: inherit; 
    border: 1px solid transparent; background: transparent; 
    padding: 4px; border-radius: 3px; width: 100%; transition: 0.2s;
}
textarea:hover, input:hover, select:hover { background: var(--input-bg); }
textarea:focus, input:focus { outline: none; background: #fff; border-color: var(--accent); }

.company-section textarea { font-weight: bold; font-size: 11px; line-height: 1.4; height: auto; resize: none; }
.tiny-label { font-size: 9px; color: #7f8c8d; text-transform: uppercase; letter-spacing: 0.5px; display: block; margin-bottom: 2px; }

/* --- GESTION DROITE : DOC TYPE & PROJET --- */
.doc-meta { text-align: right; width: 100%; }
.title-select { 
    font-size: 24px; font-weight: 800; color: var(--primary); 
    text-align: right; margin-bottom: 0; text-transform: uppercase; padding: 0; cursor: pointer;
}
.project-name {
    font-size: 13px; font-style: italic; color: #555; font-weight: bold;
    text-align: right; margin-bottom: 10px; border-bottom: 1px dashed transparent;
}
.project-name:hover { border-bottom: 1px dashed #ccc; }

.meta-row { display: flex; justify-content: flex-end; align-items: center; gap: 5px; font-size: 11px; margin-bottom: 2px; }
.meta-row input { width: 110px; text-align: right; font-weight: bold; }

.client-box { 
    border: 1px solid var(--border-color); background: #fdfdfd; 
    padding: 10px; border-radius: 4px; width: 100%; margin-top: 15px; text-align: left;
}
.client-box textarea { text-align: left; font-size: 12px; }

/* --- TABLEAU --- */
table { width: 100%; border-collapse: collapse; margin-bottom: 15px; border-top: 2px solid var(--primary); }
th { background: #f0f0f0; color: #000; padding: 8px 4px; text-align: left; font-size: 11px; text-transform: uppercase; border-bottom: 1px solid #aaa; }
td { border-bottom: 1px solid #eee; padding: 4px; vertical-align: middle; font-size: 12px; }
td input { padding: 4px; margin: 0; }
.col-money { text-align: right; font-family: 'Courier New', monospace; font-weight: bold; white-space: nowrap; }

.qty-wrapper { display: flex; border: 1px solid #ddd; width: 85px; height: 26px; margin: 0 auto; border-radius: 3px; overflow: hidden;}
.qty-btn { width: 25px; background: #eee; border: none; font-size: 16px; cursor: pointer; padding: 0; display: flex; align-items: center; justify-content: center; }
.qty-wrapper input.qty { text-align: center; border: none !important; font-weight: bold; font-size: 12px; height: 100%; padding: 0; width: 35px; }

/* --- BAS DE PAGE --- */
.bottom-section { display: flex; justify-content: space-between; align-items: flex-start; margin-top: 20px; }

.signatures-section { display: flex; gap: 15px; width: 50%; }
.sig-box { flex: 1; border: 1px solid #ccc; height: 80px; padding: 8px; font-size: 10px; display: flex; flex-direction: column; background: #fafafa; border-radius: 3px;}
.sig-title { font-weight: bold; margin: 0 0 2px 0; text-decoration: underline; font-size: 10px; }
.sig-subtitle { margin: 0; color: #666; font-style: italic; font-size: 8px; opacity: 50px; text-align: center; }

.footer-totals { width: 45%; font-size: 12px; }
.total-row { display: flex; justify-content: space-between; padding: 4px 0; border-bottom: 1px dashed #eee; align-items: center; }
.highlight-row { font-weight: bold; color: var(--primary); border-top: 1px solid #ccc; margin-top: 5px; padding-top: 5px; }
.final-total { background: var(--primary); color: white; padding: 10px; font-weight: bold; font-size: 14px; margin-top: 10px; border-radius: 3px; display: flex; justify-content: space-between; }
.input-money { text-align: right; width: 100px; font-weight: bold; font-size: 12px; }
.select-tva { width: auto; font-size: 11px; padding: 0; text-align: right; border: none; cursor: pointer; }

.legal-footer { margin-top: 30px; border-top: 1px solid #ccc; padding-top: 10px; }
.legal-footer textarea { text-align: center; font-size: 10px; color: #7f8c8d; font-style: italic; }

/* --- OUTILS --- */
.toolbar { 
    position: sticky; top: 10px; background: rgba(255,255,255,0.95); 
    padding: 8px 15px; margin-bottom: 20px; border: 1px solid #ccc; 
    display: flex; gap: 10px; align-items: center; border-radius: 4px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); z-index: 999;
}
.btn { padding: 6px 12px; border: none; border-radius: 4px; cursor: pointer; color: white; font-weight: bold; font-size: 12px; }
.btn-primary { background: var(--primary); }
.btn-secondary { background: var(--success); font-size: 12px; }
.btn-danger { background: var(--danger); }
.btn-del { color: #e74c3c; background: none; border: none; cursor: pointer; font-size: 14px; }
#saveStatus { font-size: 12px; color: var(--success); margin-left: auto; display: none; }

/* --- MOBILE --- */
@media screen and (max-width: 768px) {
    .paper { width: 100%; padding: 10px; min-height: auto; }
    header { flex-direction: column; }
    .left-col, .right-col { width: 100%; text-align: center; align-items: center; }
    .doc-meta { text-align: center; }
    .title-select, .project-name, .meta-row { justify-content: center; text-align: center; }
    .meta-row input { text-align: center; }
    .client-box { text-align: center; } 
    .client-box textarea { text-align: center; }
    .bottom-section { flex-direction: column-reverse; gap: 20px; }
    .signatures-section, .footer-totals { width: 100%; }
    .toolbar { position: fixed; bottom: 0; top: auto; width: 100%; margin: 0; left: 0; justify-content: center; border-radius: 0; }
}

/* --- IMPRESSION (Zéro marge parasite) --- */
@media print {
    @page { margin: 0; size: A4; } /* Enlève la date et URL du navigateur */
    body { margin: 1.5cm !important; padding: 0 !important; background: white; } /* Recrée la marge propre */
    
    .paper { width: 100%; max-width: 100%; padding: 0; border: none; box-shadow: none; margin: 0; }
    .no-print, .toolbar, .add-btn-container, .qty-btn { display: none !important; }
    
    input, textarea, select { border: none !important; resize: none; appearance: none; padding: 0; margin: 0; background: transparent !important; }
    .client-box, .sig-box { border: 1px solid #000; background: none !important; }
    .qty-wrapper { border: none; width: auto; justify-content: flex-start; }
    
    th { background-color: #eee !important; color: #000 !important; -webkit-print-color-adjust: exact; }
    .final-total { background-color: #333 !important; color: white !important; -webkit-print-color-adjust: exact; }
    
    tr, .bottom-section { page-break-inside: avoid; }
}