/* ==========================================================================
   BatiFacture Pro - Feuilles de style complètes (Écran et Impression A4)
   ========================================================================== */

:root {
  /* Thème par défaut : Bleu BTP / Chantier */
  --primary-color: #1e3a8a;
  --primary-light: #eff6ff;
  --primary-dark: #172554;
  --primary-border: #93c5fd;
  --accent-color: #2563eb;
  --text-main: #1f2937;
  --text-muted: #4b5563;
  --bg-app: #f1f5f9;
  --bg-card: #ffffff;
  --border-color: #e2e8f0;
  --success: #15803d;
  --warning: #b45309;
  --danger: #b91c1c;
}

/* Thèmes personnalisés */
[data-theme="blue"] {
  --primary-color: #1e3a8a;
  --primary-light: #eff6ff;
  --primary-dark: #172554;
  --primary-border: #93c5fd;
  --accent-color: #2563eb;
}

[data-theme="orange"] {
  --primary-color: #c2410c;
  --primary-light: #fff7ed;
  --primary-dark: #7c2d12;
  --primary-border: #fdba74;
  --accent-color: #ea580c;
}

[data-theme="green"] {
  --primary-color: #047857;
  --primary-light: #f0fdf4;
  --primary-dark: #064e3b;
  --primary-border: #86efac;
  --accent-color: #059669;
}

[data-theme="slate"] {
  --primary-color: #334155;
  --primary-light: #f8fafc;
  --primary-dark: #0f172a;
  --primary-border: #cbd5e1;
  --accent-color: #475569;
}

/* Base reset */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--text-main);
  background-color: var(--bg-app);
  min-height: 100vh;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* Header & Navigation */
.app-header {
  background: #ffffff;
  border-bottom: 1px solid var(--border-color);
  padding: 0.75rem 1.5rem;
  position: sticky;
  top: 0;
  z-index: 40;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.header-container {
  max-width: 1700px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 1rem;
}

.logo-area {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.logo-badge {
  background: linear-gradient(135deg, var(--primary-color), var(--accent-color));
  color: white;
  width: 42px;
  height: 42px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 1.25rem;
  box-shadow: 0 4px 6px -1px rgba(37, 99, 235, 0.2);
}

.logo-text h1 {
  font-size: 1.25rem;
  font-weight: 700;
  color: #0f172a;
  line-height: 1.2;
}

.logo-text p {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

/* Boutons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.5rem 0.9rem;
  font-size: 0.875rem;
  font-weight: 600;
  border-radius: 0.5rem;
  border: 1px solid transparent;
  cursor: pointer;
  transition: all 0.15s ease-in-out;
  text-decoration: none;
  white-space: nowrap;
}

.btn-primary {
  background-color: var(--accent-color);
  color: white;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.btn-primary:hover {
  filter: brightness(0.9);
  transform: translateY(-1px);
}

.btn-success {
  background-color: #10b981;
  color: white;
}

.btn-success:hover {
  background-color: #059669;
  transform: translateY(-1px);
}

.btn-secondary {
  background-color: #ffffff;
  border-color: #cbd5e1;
  color: #334155;
}

.btn-secondary:hover {
  background-color: #f8fafc;
  border-color: #94a3b8;
}

.btn-dark {
  background-color: #0f172a;
  color: white;
}

.btn-dark:hover {
  background-color: #1e293b;
}

.btn-danger {
  background-color: #fee2e2;
  color: #b91c1c;
  border-color: #fca5a5;
}

.btn-danger:hover {
  background-color: #fecaca;
}

.btn-sm {
  padding: 0.3rem 0.6rem;
  font-size: 0.775rem;
  border-radius: 0.375rem;
}

/* Layout général */
.main-wrapper {
  max-width: 1700px;
  margin: 1.5rem auto;
  padding: 0 1.5rem;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2rem;
  align-items: flex-start;
}

@media (max-width: 1200px) {
  .main-wrapper {
    grid-template-columns: 1fr;
  }
}

/* Onglets mobile/tablette */
.view-tabs {
  display: none;
  background: #e2e8f0;
  padding: 0.25rem;
  border-radius: 0.5rem;
  margin-bottom: 1rem;
}

@media (max-width: 1200px) {
  .view-tabs {
    display: flex;
    gap: 0.25rem;
  }
  .view-tab {
    flex: 1;
    padding: 0.5rem;
    text-align: center;
    border-radius: 0.375rem;
    border: none;
    font-weight: 600;
    cursor: pointer;
    background: transparent;
  }
  .view-tab.active {
    background: white;
    color: var(--accent-color);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  }
}

/* Cartes et sections du formulaire */
.form-panel {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.card {
  background: #ffffff;
  border: 1px solid var(--border-color);
  border-radius: 0.75rem;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.card-header {
  padding: 0.85rem 1.25rem;
  background-color: #f8fafc;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.card-title {
  font-size: 0.975rem;
  font-weight: 700;
  color: #0f172a;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.card-body {
  padding: 1.25rem;
}

/* Grille de formulaires */
.form-grid {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  gap: 0.9rem;
}

.col-12 { grid-column: span 12; }
.col-8  { grid-column: span 8; }
.col-6  { grid-column: span 6; }
.col-4  { grid-column: span 4; }
.col-3  { grid-column: span 3; }
.col-2  { grid-column: span 2; }

@media (max-width: 640px) {
  .col-8, .col-6, .col-4, .col-3, .col-2 {
    grid-column: span 12;
  }
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.form-label {
  font-size: 0.775rem;
  font-weight: 600;
  color: #334155;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.form-label .sub {
  font-size: 0.7rem;
  font-weight: normal;
  color: #64748b;
}

.form-control {
  width: 100%;
  padding: 0.45rem 0.65rem;
  border: 1px solid #cbd5e1;
  border-radius: 0.375rem;
  font-size: 0.85rem;
  color: #0f172a;
  background-color: #ffffff;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.form-control:focus {
  outline: none;
  border-color: var(--accent-color);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

textarea.form-control {
  min-height: 60px;
  resize: vertical;
}

/* Lots et Lignes de travaux */
.lot-card {
  border: 1px solid #cbd5e1;
  border-radius: 0.5rem;
  margin-bottom: 1rem;
  background: #ffffff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}

.lot-header {
  background: #f1f5f9;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid #cbd5e1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-top-left-radius: 0.5rem;
  border-top-right-radius: 0.5rem;
}

.lot-title-input {
  font-weight: 700;
  font-size: 0.875rem;
  border: 1px solid transparent;
  background: transparent;
  padding: 0.2rem 0.4rem;
  border-radius: 0.25rem;
  color: var(--primary-color);
  width: 60%;
}

.lot-title-input:hover, .lot-title-input:focus {
  border-color: #cbd5e1;
  background: white;
}

.lot-items-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.825rem;
}

.lot-items-table th {
  background: #f8fafc;
  padding: 0.4rem 0.5rem;
  text-align: left;
  font-weight: 600;
  color: #475569;
  border-bottom: 1px solid #e2e8f0;
}

.lot-items-table td {
  padding: 0.4rem 0.5rem;
  border-bottom: 1px solid #f1f5f9;
  vertical-align: top;
}

.line-row:hover {
  background-color: #f8fafc;
}

.col-btn {
  width: 32px;
  text-align: center;
}

.quick-presets-btn {
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  color: #166534;
  font-size: 0.75rem;
  padding: 0.25rem 0.5rem;
  border-radius: 0.375rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  font-weight: 600;
}

.quick-presets-btn:hover {
  background: #dcfce7;
}

/* ==========================================================================
   PANNEAU DE PRÉVISUALISATION (FACTURE A4)
   ========================================================================== */

.preview-panel {
  position: sticky;
  top: 5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.preview-toolbar {
  background: #ffffff;
  padding: 0.6rem 1rem;
  border-radius: 0.5rem;
  border: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.theme-selector {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.8rem;
  font-weight: 600;
  color: #475569;
}

.color-dot {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 2px solid white;
  box-shadow: 0 0 0 1px #cbd5e1;
  cursor: pointer;
  transition: transform 0.15s;
}

.color-dot:hover {
  transform: scale(1.15);
}

.color-dot.active {
  box-shadow: 0 0 0 2px #0f172a;
}

.color-dot[data-theme="blue"]   { background-color: #1e3a8a; }
.color-dot[data-theme="orange"] { background-color: #c2410c; }
.color-dot[data-theme="green"]  { background-color: #047857; }
.color-dot[data-theme="slate"]  { background-color: #334155; }

/* Feuille Facture A4 */
.invoice-sheet-container {
  overflow-x: auto;
  padding: 0.5rem 0;
  background: #94a3b8;
  border-radius: 0.5rem;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: center;
}

.invoice-sheet {
  background: #ffffff;
  width: 210mm;
  min-height: 297mm;
  padding: 15mm 15mm 12mm 15mm;
  margin: 0 auto;
  box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.25);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  position: relative;
  box-sizing: border-box;
  font-size: 11pt;
  color: #1f2937;
  line-height: 1.4;
}

/* En-tête Facture */
.sheet-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 6mm;
  gap: 15px;
}

.company-brand {
  max-width: 55%;
}

.company-logo-img {
  max-height: 70px;
  max-width: 180px;
  object-fit: contain;
  margin-bottom: 6px;
  display: block;
}

.company-name {
  font-size: 16pt;
  font-weight: 800;
  color: var(--primary-color);
  line-height: 1.2;
}

.company-details {
  font-size: 8.5pt;
  color: #4b5563;
  margin-top: 3px;
  line-height: 1.35;
}

.company-decennale-badge {
  display: inline-block;
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  color: #166534;
  font-size: 7.5pt;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 4px;
  margin-top: 5px;
}

/* Bloc Client et Chantier */
.client-box-container {
  width: 42%;
  display: flex;
  flex-direction: column;
  gap: 4mm;
}

.client-card {
  border: 1.5px solid var(--primary-border);
  background: #f8fafc;
  border-radius: 6px;
  padding: 3mm 4mm;
}

.client-card-title {
  font-size: 7.5pt;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 700;
  color: var(--primary-color);
  margin-bottom: 2px;
}

.client-card-name {
  font-size: 11pt;
  font-weight: 700;
  color: #0f172a;
}

.client-card-text {
  font-size: 8.5pt;
  color: #334155;
  line-height: 1.35;
}

.site-card {
  border: 1.5px dashed #cbd5e1;
  background: #fffbeb;
  border-radius: 6px;
  padding: 2.5mm 3.5mm;
}

.site-card-title {
  font-size: 7.5pt;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 700;
  color: #92400e;
  margin-bottom: 2px;
}

.site-card-text {
  font-size: 8.5pt;
  color: #78350f;
  line-height: 1.35;
}

/* Bandeau Informations Document */
.doc-info-bar {
  background: var(--primary-light);
  border-left: 4px solid var(--primary-color);
  border-top: 1px solid var(--primary-border);
  border-right: 1px solid var(--primary-border);
  border-bottom: 1px solid var(--primary-border);
  border-radius: 4px;
  padding: 2.5mm 4mm;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 5mm;
}

.doc-badge-title {
  font-size: 13pt;
  font-weight: 800;
  color: var(--primary-color);
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.doc-info-item {
  font-size: 8.5pt;
  color: #1e293b;
}

.doc-info-item strong {
  color: #0f172a;
  font-weight: 700;
}

/* Tableau des prestations BTP */
.sheet-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 5mm;
  font-size: 9pt;
}

.sheet-table thead th {
  background-color: var(--primary-color);
  color: #ffffff;
  padding: 3mm 2.5mm;
  text-align: left;
  font-weight: 600;
  font-size: 8.5pt;
  letter-spacing: 0.02em;
}

.sheet-table thead th.text-right {
  text-align: right;
}

.sheet-table thead th.text-center {
  text-align: center;
}

.sheet-lot-header {
  background-color: #f1f5f9;
  border-top: 1.5px solid #cbd5e1;
  border-bottom: 1.5px solid #cbd5e1;
}

.sheet-lot-header td {
  padding: 2mm 2.5mm;
  font-weight: 700;
  color: var(--primary-color);
  font-size: 9pt;
}

.sheet-item-row td {
  padding: 2.5mm 2.5mm;
  border-bottom: 1px solid #e2e8f0;
  vertical-align: top;
}

.sheet-item-desc {
  font-weight: 500;
  color: #0f172a;
  white-space: pre-line;
}

/* Totaux et Règlements */
.sheet-totals-section {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 5mm;
  margin-top: auto;
  margin-bottom: 4mm;
}

.sheet-left-block {
  width: 54%;
  display: flex;
  flex-direction: column;
  gap: 3mm;
}

/* Tableau TVA */
.tva-breakdown-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 8pt;
  border: 1px solid #cbd5e1;
}

.tva-breakdown-table th {
  background: #f8fafc;
  padding: 1.5mm 2mm;
  text-align: right;
  border-bottom: 1px solid #cbd5e1;
  font-weight: 600;
  color: #475569;
}

.tva-breakdown-table th:first-child, .tva-breakdown-table td:first-child {
  text-align: left;
}

.tva-breakdown-table td {
  padding: 1.5mm 2mm;
  border-bottom: 1px solid #f1f5f9;
  text-align: right;
}

/* Bloc Bancaire & Règlement */
.payment-info-box {
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 4px;
  padding: 2.5mm 3mm;
  font-size: 8pt;
  line-height: 1.35;
}

.payment-info-box strong {
  color: #0f172a;
}

/* Bloc Totaux à droite */
.sheet-totals-box {
  width: 44%;
  border: 1.5px solid var(--primary-border);
  border-radius: 6px;
  overflow: hidden;
  background: #ffffff;
}

.total-row {
  display: flex;
  justify-content: space-between;
  padding: 1.8mm 3mm;
  font-size: 8.5pt;
  border-bottom: 1px solid #e2e8f0;
}

.total-row.discount, .total-row.deposit {
  color: #b91c1c;
}

.total-row.net-to-pay {
  background: var(--primary-color);
  color: #ffffff;
  padding: 2.5mm 3.5mm;
  font-size: 11pt;
  font-weight: 800;
  border-bottom: none;
}

/* Tampon Acquittée */
.stamp-paid {
  position: absolute;
  top: 45%;
  left: 30%;
  transform: rotate(-25deg);
  border: 4px dashed #16a34a;
  color: #16a34a;
  padding: 8px 24px;
  font-size: 26pt;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 0.15em;
  opacity: 0.75;
  pointer-events: none;
  border-radius: 8px;
}

/* Mentions Légales & Pied de page */
.sheet-footer {
  border-top: 1px solid #cbd5e1;
  padding-top: 3mm;
  font-size: 7pt;
  color: #64748b;
  line-height: 1.3;
  text-align: center;
}

.footer-assurance {
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 4px;
  padding: 1.5mm 2.5mm;
  margin-bottom: 2mm;
  font-weight: 500;
  color: #334155;
  text-align: left;
}

/* Modals */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.6);
  backdrop-filter: blur(2px);
  z-index: 100;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal-overlay.active {
  display: flex;
}

.modal-content {
  background: white;
  border-radius: 0.75rem;
  max-width: 650px;
  width: 100%;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.2);
}

.modal-header {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid #e2e8f0;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-body {
  padding: 1.25rem;
  overflow-y: auto;
}

.modal-footer {
  padding: 0.75rem 1.25rem;
  border-top: 1px solid #e2e8f0;
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  background: #f8fafc;
  border-bottom-left-radius: 0.75rem;
  border-bottom-right-radius: 0.75rem;
}

/* Liste des factures enregistrées */
.invoices-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.invoice-item-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem;
  border: 1px solid #e2e8f0;
  border-radius: 0.5rem;
  transition: all 0.15s;
}

.invoice-item-row:hover {
  border-color: var(--accent-color);
  background: #f8fafc;
}

/* ==========================================================================
   STYLES D'IMPRESSION STRICTE A4 (@media print)
   ========================================================================== */
@media print {
  @page {
    size: A4 portrait;
    margin: 10mm 12mm 10mm 12mm;
  }

  html, body {
    background: #ffffff !important;
    color: #000000 !important;
    margin: 0 !important;
    padding: 0 !important;
    width: 100% !important;
    height: auto !important;
    font-size: 10pt;
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }

  /* Masquer tous les éléments UI */
  .app-header,
  .form-panel,
  .preview-toolbar,
  .view-tabs,
  .modal-overlay,
  .no-print {
    display: none !important;
  }

  .main-wrapper {
    display: block !important;
    max-width: 100% !important;
    margin: 0 !important;
    padding: 0 !important;
  }

  .preview-panel {
    position: static !important;
    display: block !important;
  }

  .invoice-sheet-container {
    background: transparent !important;
    padding: 0 !important;
    box-shadow: none !important;
    overflow: visible !important;
    display: block !important;
  }

  .invoice-sheet {
    width: 100% !important;
    min-height: auto !important;
    padding: 0 !important;
    margin: 0 !important;
    box-shadow: none !important;
    border: none !important;
  }

  .sheet-lot-header,
  .sheet-item-row {
    page-break-inside: avoid !important;
    break-inside: avoid !important;
  }

  .sheet-totals-section {
    page-break-inside: avoid !important;
    break-inside: avoid !important;
  }

  .sheet-footer {
    page-break-inside: avoid !important;
    break-inside: avoid !important;
  }
}
