/**
 * MANANORD — Factures & impressions responsive
 * Logo + société à GAUCHE | Facture + client à DROITE — tous écrans
 */

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  overflow-x: auto;
  word-break: normal;
  overflow-wrap: normal;
}

img {
  max-width: 100%;
  height: auto;
}

/* ─── Conteneurs ─── */
.invoice-container,
.facture-container,
#print-sales,
#print-recu,
#print-achat,
#print-depense,
#print-commande,
.print-container {
  max-width: 100%;
  width: 100%;
  box-sizing: border-box;
  overflow-x: auto;
}

/* ═══ EN-TÊTE : grille 2 colonnes (gauche / droite) ═══ */
.header-section {
  display: grid !important;
  grid-template-columns: minmax(0, 1fr) minmax(145px, 38%) !important;
  grid-template-rows: auto !important;
  gap: 0.65rem 0.75rem !important;
  align-items: start !important;
  width: 100% !important;
  box-sizing: border-box !important;
  overflow-x: auto !important;
  -webkit-overflow-scrolling: touch;
  /* annule flex hérité */
  flex-direction: unset !important;
  flex-wrap: unset !important;
}

/* ── Structure 2 blocs (print_sales_credit) ── */
.logo-company {
  grid-column: 1 !important;
  grid-row: 1 !important;
  display: flex !important;
  flex-direction: row !important;
  align-items: flex-start !important;
  min-width: 0 !important;
  max-width: none !important;
  width: auto !important;
  text-align: left !important;
}

.invoice-section {
  grid-column: 2 !important;
  grid-row: 1 !important;
  min-width: 145px !important;
  max-width: none !important;
  width: auto !important;
  text-align: right !important;
  justify-self: stretch !important;
  align-self: start !important;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* ── Structure 3 blocs (cash, achat, etc.) → gauche fusionné ── */
.header-section > .logo-section {
  grid-column: 1 !important;
  grid-row: 1 !important;
  align-self: start !important;
  flex: 0 0 auto !important;
  width: auto !important;
  max-width: none !important;
  z-index: 1;
}

.header-section > .company-title {
  grid-column: 1 !important;
  grid-row: 1 !important;
  margin-left: 72px !important;
  padding-left: 0.35rem !important;
  padding-right: 0.5rem !important;
  text-align: left !important;
  min-width: 0 !important;
  max-width: none !important;
  z-index: 0;
}

.header-section > .invoice-title,
.header-section > .receipt-title {
  grid-column: 2 !important;
  grid-row: 1 !important;
  min-width: 145px !important;
  max-width: none !important;
  width: auto !important;
  text-align: right !important;
  justify-self: stretch !important;
  align-self: start !important;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* Logo + société côte à côte (sans chevauchement) */
.header-left {
  display: flex !important;
  flex-direction: row !important;
  align-items: flex-start !important;
  gap: 6px !important;
  min-width: 0 !important;
  grid-column: 1 !important;
  grid-row: 1 !important;
}

.header-left .logo-section {
  flex: 0 0 auto !important;
  grid-column: unset !important;
  grid-row: unset !important;
  width: auto !important;
  max-width: none !important;
  z-index: auto !important;
  margin: 0 !important;
}

.header-left .company-title {
  flex: 1 1 0 !important;
  min-width: 0 !important;
  margin-left: 0 !important;
  padding: 0 !important;
  grid-column: unset !important;
  grid-row: unset !important;
}

.logo-section img,
.logo {
  flex-shrink: 0;
  max-width: 90px;
  height: auto;
}

.logo-placeholder {
  flex-shrink: 0;
}

.company-details,
.company-title,
.company-name {
  text-align: left !important;
  min-width: 0;
}

.company-title h1,
.company-title h2,
.company-name,
.company-details,
.company-details div,
.invoice-details,
.invoice-details div {
  word-break: normal !important;
  overflow-wrap: normal !important;
  word-wrap: normal !important;
  hyphens: none !important;
  white-space: normal !important;
}

.invoice-title h2,
.document-title,
.receipt-title h2 {
  word-break: normal !important;
  overflow-wrap: normal !important;
  white-space: nowrap !important;
  hyphens: none !important;
}

.invoice-title > div,
.invoice-details div {
  text-align: right !important;
}

/* Pas de casse caractère par caractère dans l'en-tête */
.header-section,
.header-section * {
  word-break: normal !important;
  overflow-wrap: normal !important;
}

/* ─── Tableaux produits ─── */
.table-responsive,
.invoice-table-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
  margin-bottom: 0.75rem;
}

.products-table,
.invoice-table,
table.table {
  width: 100%;
  max-width: 100%;
}

.products-table th,
.products-table td {
  word-break: normal;
  overflow-wrap: normal;
  white-space: normal;
}

.products-table td:first-child,
.products-table td:nth-child(4),
.products-table td:nth-child(5),
.products-table td:last-child,
.totals-table td:last-child {
  white-space: nowrap;
}

.products-table td:nth-child(3) {
  white-space: normal;
}

.totals-section {
  max-width: 100%;
  box-sizing: border-box;
}

.totals-table td {
  word-break: normal;
  overflow-wrap: normal;
}

.signatures {
  flex-wrap: wrap;
  gap: 1rem;
}

.signature-box {
  min-width: 0;
  flex: 1 1 140px;
  max-width: 100%;
}

.conversion-text,
.notes-section {
  word-break: normal;
  overflow-wrap: normal;
  white-space: normal;
}

.no-print,
.print-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.no-print .btn,
.no-print button {
  min-height: 44px;
}

.header {
  word-break: normal;
}

body > table,
.print-table-wrap {
  display: block;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
}

/* ─── Mobile ─── */
@media (max-width: 768px) {
  body {
    padding: 8px !important;
    margin: 0 !important;
  }

  .invoice-container,
  .facture-container {
    padding: 8px;
    border-width: 1px;
  }

  .header-section {
    grid-template-columns: minmax(0, 1fr) minmax(120px, 42%) !important;
    gap: 0.4rem 0.5rem !important;
  }

  .header-section > .company-title {
    margin-left: 58px !important;
  }

  .logo,
  .logo-placeholder {
    width: 52px !important;
    height: 52px !important;
    margin-right: 6px !important;
  }

  .logo-section img {
    max-width: 52px;
    max-height: 52px;
  }

  .company-details,
  .invoice-details {
    font-size: 9px !important;
    line-height: 1.35 !important;
  }

  .company-name {
    font-size: 11px !important;
  }

  .document-title,
  .invoice-title h2 {
    font-size: 13px !important;
  }

  .products-table {
    font-size: 8px;
    min-width: 480px;
  }

  .products-table th,
  .products-table td {
    padding: 3px;
  }

  .totals-section {
    width: 100% !important;
    margin-left: 0 !important;
  }

  .no-print .btn,
  .no-print button {
    flex: 1 1 100%;
    width: 100%;
  }
}

@media (max-width: 576px) {
  .header-section {
    grid-template-columns: minmax(0, 1fr) minmax(110px, 44%) !important;
  }

  .company-title h1 {
    font-size: 13px !important;
  }

  .company-title h2 {
    font-size: 9px !important;
  }

  .products-table {
    min-width: 420px;
    font-size: 7px;
  }
}

@media print {
  @page {
    size: A4 portrait;
    margin: 6mm 8mm;
  }

  html, body, .print-invoice-body {
    overflow: visible !important;
    padding: 0 !important;
    margin: 0 !important;
    height: auto !important;
    min-height: 0 !important;
    background: #fff !important;
  }

  .invoice-container,
  .facture-container,
  #print-sales {
    max-width: 100% !important;
    width: 100% !important;
    border: 1px solid #333 !important;
    overflow: visible !important;
    padding: 4mm 5mm !important;
    margin: 0 !important;
    font-size: 8.5pt !important;
    line-height: 1.25 !important;
    page-break-inside: avoid;
  }

  .header-section {
    overflow: visible !important;
    margin-bottom: 6px !important;
    padding-bottom: 6px !important;
    gap: 4px 6px !important;
  }

  .header-section > .company-title {
    margin-left: 48px !important;
  }

  .logo-section img,
  .logo {
    max-width: 48px !important;
    max-height: 42px !important;
  }

  .company-title h1,
  .company-name {
    font-size: 11pt !important;
    margin-bottom: 2px !important;
  }

  .company-title h2,
  .company-details {
    font-size: 7pt !important;
  }

  .invoice-title h2,
  .document-title {
    font-size: 10pt !important;
  }

  .invoice-title > div,
  .invoice-details,
  .invoice-details div {
    font-size: 7pt !important;
    line-height: 1.3 !important;
  }

  .table-responsive,
  .invoice-table-scroll {
    overflow: visible !important;
    margin-bottom: 4px !important;
  }

  .products-table,
  .totals-table {
    font-size: 7.5pt !important;
  }

  .products-table th,
  .products-table td,
  .totals-table td {
    padding: 2px 3px !important;
  }

  .products-table tfoot td,
  .products-table tfoot th {
    font-size: 7.5pt !important;
    padding: 2px 3px !important;
  }

  .totals-section {
    width: 55% !important;
    margin-left: auto !important;
  }

  .conversion-text {
    font-size: 7pt !important;
    margin: 4px 0 !important;
    padding: 0 !important;
  }

  .signatures {
    margin-top: 8px !important;
    gap: 0.5rem !important;
  }

  .signature-box {
    font-size: 7pt !important;
  }

  .signature-line {
    height: 22px !important;
    margin-bottom: 2px !important;
  }

  .petit-texte {
    font-size: 7pt !important;
  }

  .no-print {
    display: none !important;
  }

  /* ─── A4 (défaut) ─── */
  body.paper-a4 .invoice-container,
  body.paper-a4 #print-sales,
  body.paper-a4 .facture-container {
    font-size: 8.5pt !important;
  }

  /* ─── A5 ─── */
  body.paper-a5 .invoice-container,
  body.paper-a5 #print-sales,
  body.paper-a5 .facture-container {
    font-size: 7.5pt !important;
    padding: 3mm 4mm !important;
    line-height: 1.2 !important;
  }

  body.paper-a5 .header-section > .company-title {
    margin-left: 0 !important;
  }

  body.paper-a5 .header-left {
    gap: 4px !important;
  }

  body.paper-a5 .logo-section img,
  body.paper-a5 .logo {
    max-width: 36px !important;
    max-height: 32px !important;
  }

  body.paper-a5 .company-title h1,
  body.paper-a5 .company-name {
    font-size: 9.5pt !important;
  }

  body.paper-a5 .company-title h2,
  body.paper-a5 .company-details,
  body.paper-a5 .invoice-title > div,
  body.paper-a5 .invoice-details div {
    font-size: 6pt !important;
  }

  body.paper-a5 .invoice-title h2,
  body.paper-a5 .document-title {
    font-size: 8.5pt !important;
  }

  body.paper-a5 .products-table,
  body.paper-a5 .totals-table {
    font-size: 6.5pt !important;
  }

  body.paper-a5 .products-table th,
  body.paper-a5 .products-table td,
  body.paper-a5 .totals-table td,
  body.paper-a5 .products-table tfoot td,
  body.paper-a5 .products-table tfoot th {
    padding: 1px 2px !important;
    font-size: 6.5pt !important;
  }

  body.paper-a5 .conversion-text,
  body.paper-a5 .petit-texte,
  body.paper-a5 .signature-box {
    font-size: 6pt !important;
  }

  body.paper-a5 .signature-line {
    height: 16px !important;
  }

  body.paper-a5 .totals-section {
    width: 62% !important;
  }

  /* ─── A6 (ticket compact) ─── */
  body.paper-a6 .invoice-container,
  body.paper-a6 #print-sales,
  body.paper-a6 .facture-container {
    font-size: 6pt !important;
    padding: 2mm 2.5mm !important;
    line-height: 1.15 !important;
    border-width: 1px !important;
  }

  body.paper-a6 .header-section {
    grid-template-columns: 1fr !important;
    grid-template-rows: auto auto !important;
    gap: 3px !important;
    margin-bottom: 3px !important;
    padding-bottom: 3px !important;
  }

  body.paper-a6 .header-left {
    grid-column: 1 !important;
    grid-row: 1 !important;
    gap: 3px !important;
  }

  body.paper-a6 .invoice-title {
    grid-column: 1 !important;
    grid-row: 2 !important;
    text-align: left !important;
    min-width: 0 !important;
    width: 100% !important;
  }

  body.paper-a6 .invoice-title > div {
    text-align: left !important;
  }

  body.paper-a6 .header-section > .company-title {
    margin-left: 0 !important;
  }

  body.paper-a6 .logo-section img,
  body.paper-a6 .logo {
    max-width: 24px !important;
    max-height: 22px !important;
  }

  body.paper-a6 .company-title h1,
  body.paper-a6 .company-name {
    font-size: 7pt !important;
  }

  body.paper-a6 .company-title h2,
  body.paper-a6 .company-details,
  body.paper-a6 .invoice-title > div,
  body.paper-a6 .invoice-details div {
    font-size: 5pt !important;
  }

  body.paper-a6 .invoice-title h2,
  body.paper-a6 .document-title {
    font-size: 7pt !important;
  }

  body.paper-a6 .products-table,
  body.paper-a6 .totals-table {
    font-size: 5.5pt !important;
  }

  body.paper-a6 .products-table th,
  body.paper-a6 .products-table td,
  body.paper-a6 .totals-table td,
  body.paper-a6 .products-table tfoot td,
  body.paper-a6 .products-table tfoot th {
    padding: 1px !important;
    font-size: 5.5pt !important;
  }

  body.paper-a6 .conversion-text,
  body.paper-a6 .petit-texte,
  body.paper-a6 .signature-box {
    font-size: 5pt !important;
  }

  body.paper-a6 .signature-line {
    height: 12px !important;
  }

  body.paper-a6 .signatures {
    margin-top: 4px !important;
  }

  body.paper-a6 .totals-section {
    width: 72% !important;
  }
}
