/* ==========================================
   FACTURA / DASHBOARD
   Archivo: css/factura.css
   Propósito: estilos exclusivos de dashboard.html
   ========================================== */

/* --- Variables de tema --- */
:root{
  --primary: #1A73E8; /* se puede actualizar desde JS */
  --muted: #666666;   /* añadida para footer y textos suaves */
  --ink: #111111;     /* añadida para textos fuertes */
}

/* --- Layout general --- */
.invoice-body {
  font-family: 'Poppins', 'Roboto', 'Montserrat', system-ui, -apple-system, sans-serif;
  background: #f5f7fb;
  color: #111;
  margin: 0;
}

.dashboard-container {
  display: grid;
  grid-template-columns: 280px 1fr 280px; /* izquierda | factura | derecha */
  gap: 24px;
  padding: 16px;
  min-height: 100dvh;
  align-items: start;
}

/* --- Sidebar izquierda --- */
.sidebar {
  background: #fff;
  border: 1px solid #e6e8ee;
  border-radius: 12px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  grid-column: 1;
  position: sticky;
  top: 16px;
}

.sidebar h2 {
  margin: 0 0 8px 0;
  font-size: 1.1rem;
  font-weight: 600;
}

/* --- Sidebar derecha --- */
.sidebar-right {
  background: #fff;
  border: 1px solid #e6e8ee;
  border-radius: 12px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  grid-column: 3;        /* ubica en la 3ra columna */
  position: sticky;
  top: 16px;
}

/* Utilidades de margen */
.mt-10 { margin-top: 10px; }
.mt-20 { margin-top: 20px; }

/* Botón "Agregar fila" */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 9px 12px;
  border-radius: 8px;
  border: 1px solid #d8deea;
  background: #ffffff;
  color: #111827;
  font: inherit;
  font-weight: 600;
  line-height: 1.2;
  cursor: pointer;
  transition: background 0.2s ease, border-color 0.2s ease, color 0.2s ease, box-shadow 0.2s ease;
}

.btn-block {
  width: 100%;
}

.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #ffffff;
}

.btn-neutral {
  background: #f6f8fb;
  color: #111827;
}

.btn-outline {
  background: #ffffff;
  border-color: var(--primary);
  color: var(--primary);
}

.btn:hover {
  box-shadow: 0 6px 14px rgba(17, 24, 39, 0.12);
}

.voluntary-pay-panel {
  display: grid;
  gap: 7px;
  margin-top: 12px;
  padding: 11px;
  border: 1px solid rgba(0, 160, 192, .18);
  border-radius: 8px;
  background: linear-gradient(180deg, #ffffff, #f3fbfc);
}

.voluntary-pay-panel h2 {
  margin: 0;
  color: #111827;
  font-size: .86rem;
  line-height: 1.25;
}

.voluntary-pay-panel p {
  margin: 0;
  color: #4b5563;
  font-size: .78rem;
  line-height: 1.4;
}

.voluntary-pay-panel .btn {
  min-height: 34px;
  padding: 7px 10px;
  font-size: .8rem;
}

.voluntary-pay-panel small {
  color: #6b7280;
  font-size: .68rem;
  line-height: 1.35;
}

.btn-add-row {
  padding: 8px 12px;
  background-color: var(--primary);
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
.btn-add-row:hover { opacity: .95; }

.mobile-tools-grid,
.tool-field {
  display: grid;
  gap: 6px;
}

.document-type-group {
  display: grid;
  gap: 8px;
}

.document-type-toggle {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.document-type-btn {
  min-height: 42px;
}

.document-type-btn.is-active {
  background: var(--primary);
  border-color: var(--primary);
  color: #ffffff;
}

/* --- Área de factura (columna central) --- */
.invoice-container {
  background: transparent;
  grid-column: 2;
  min-width: 0; /* evita overflow dentro del grid */
}

.invoice {
  background: #fff;
  border: 1px solid #e6e8ee;
  border-radius: 12px;
  padding: 24px;
  transform-origin: top center; /* zoom más natural con tu JS */
  color: inherit;
  font-family: inherit;
  max-width: 900px;
  margin: 0 auto; /* centra dentro de la columna */
}

/* Inputs heredan color y fuente del contenedor */
.invoice input:not([type]),
.invoice input[type="text"],
.invoice input[type="date"],
.invoice input[type="number"] {
  width: 100%;
  box-sizing: border-box;
  padding: 8px 10px;
  border: 2px solid var(--primary);
  border-radius: 8px;
  outline: none;
  color: inherit;       /* hereda color */
  font-family: inherit; /* hereda fuente */
  background-color: #f9f9f9;
  transition: border-color 0.3s ease, box-shadow 0.3s ease, background-color 0.3s ease;
}
.invoice input:focus {
  border-color: var(--primary);
  background-color: #fff;
  box-shadow: 0 0 0 4px rgba(26, 115, 232, 0.2), 0 2px 4px rgba(0,0,0,0.1);
}

/* Encabezado */
.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
}

/* Logo */
.logo-container { display: flex; justify-content: flex-start; align-items: flex-start; }
.logo-img { display: none; max-height: 120px; object-fit: contain; }

/* Títulos / secciones */
h1 { margin: 16px 0; letter-spacing: 1px; }

.section-title {
  font-weight: 600;
  font-size: 0.85rem;
  color: inherit;
  margin-bottom: 6px;
  letter-spacing: .3px;
}

/* BILL TO y meta */
.bill-info {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.bill-inputs {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.bill-inputs input {
  flex: 1;
}
.meta-info {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px 16px;
}

/* Tabla de factura */
#invoiceTable {
  width: 100%;
  border-collapse: collapse;
  margin-top: 16px;
  border: 1px solid #e6e8ee;
  border-radius: 8px;
  overflow: hidden;
  table-layout: fixed;
}

#invoiceTable th:first-child,
#invoiceTable td:first-child {
  width: 46%;
}

#invoiceTable th:nth-child(2),
#invoiceTable td:nth-child(2) {
  width: 12%;
}

#invoiceTable th:nth-child(3),
#invoiceTable td:nth-child(3),
#invoiceTable th:nth-child(4),
#invoiceTable td:nth-child(4) {
  width: 18%;
}

#invoiceTable th:nth-child(4),
#invoiceTable td:nth-child(4),
#invoiceTable .amount,
#subtotal,
#tax,
#total {
  text-align: center;
}

#invoiceTable th:nth-child(5),
#invoiceTable td:nth-child(5) {
  width: 44px;
  min-width: 44px;
}

/* ✅ ÚNICA definición del thead: usa var(--primary) */
#invoiceTable thead th {
  background: var(--primary);
  color: #fff;
  text-align: left;
  padding: 10px;
  font-weight: 600;
  border-bottom: 1px solid #e6e8ee;
}

#invoiceTable tbody td {
  padding: 8px 10px;
  border-bottom: 1px solid #f0f2f6;
}
#invoiceTable tbody tr:last-child td { border-bottom: none; }

#invoiceTable tfoot td { padding: 8px 10px; }

/* Utilidad de alineación */
.text-right { text-align: right; }

.signature-lines {
  display: none;
  grid-template-columns: 1fr 1fr;
  gap: 34px;
  margin-top: 36px;
  padding-top: 8px;
}

.document-estimate .estimate-only.signature-lines {
  display: grid;
}

.document-invoice .estimate-only.signature-lines {
  display: none !important;
}

.signature-line {
  position: relative;
  min-height: 46px;
  padding-top: 14px;
  color: #6b7280;
  font-size: .74rem;
  font-weight: 500;
  letter-spacing: .04em;
  text-align: center;
  text-transform: uppercase;
}

.signature-line::before {
  content: "";
  position: absolute;
  top: 0;
  left: 8%;
  right: 8%;
  height: 1px;
  background: currentColor;
  opacity: .55;
}

/* ✅ ÚNICA definición del TOTAL: usa var(--primary) */
.row-total td { background: var(--primary); color: #fff; font-weight: 600; }
.cell-total   { background: var(--primary); color: #fff; font-weight: 600; }

/* Montos */
.amount { white-space: nowrap; }

.row-action {
  text-align: center;
  vertical-align: middle;
  width: 44px;
  min-width: 44px;
  padding-left: 4px !important;
  padding-right: 4px !important;
}

.row-action.no-print {
  display: table-cell;
}

.delete-row-btn {
  display: inline-grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border: 1px solid rgba(220, 38, 38, .24);
  border-radius: 999px;
  background: #fff5f5;
  color: #b91c1c;
  font-size: 1.1rem;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  opacity: 1;
  visibility: visible;
}

.delete-row-btn:hover {
  background: #fee2e2;
  border-color: rgba(220, 38, 38, .42);
}

/* === Posición dinámica del logo (usa el ID real: #logoContainer) === */
#logoContainer {
  display: flex;
  align-items: flex-start;
}

/* Izquierda (por defecto) */
#logoContainer.logo-left {
  order: 0;
  margin-right: auto;
  justify-content: flex-start;
}

/* Derecha */
#logoContainer.logo-right {
  order: 2;
  margin-left: auto;
  justify-content: flex-end;
}

/* --- Footer (unificado) --- */
.footer {
  margin-top: 20px;
  font-size: .95rem;
  color: var(--muted);
  border-top: 1px dashed color-mix(in srgb, var(--ink) 15%, transparent);
  padding-top: 12px;
  text-align: center;
  line-height: 1.6;
  font-style: italic;
}
.footer strong {
  color: var(--ink);
  font-weight: 700;
  font-style: normal;
}

/* Responsivo */
@media (max-width: 960px) {
  .dashboard-container {
    grid-template-columns: 1fr;
    gap: 14px;
    padding: 10px;
  }

  .sidebar, .invoice-container, .sidebar-right {
    grid-column: 1;
    position: static;
    max-height: none;
  }

  .invoice { padding: 16px; }
  .bill-info { grid-template-columns: 1fr; }
  .signature-lines {
    grid-template-columns: 1fr;
    gap: 30px;
  }

  .sidebar {
    gap: 12px;
    padding: 14px;
  }

  .mobile-tools-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }

  .tool-field label,
  .sidebar > label,
  .control-group label {
    font-size: .82rem;
    line-height: 1.25;
  }

  .mobile-tools-grid .control {
    width: 100%;
    height: 42px;
    padding: 4px;
  }

  #fontPicker {
    min-height: 42px;
  }

  .add-row-group .btn {
    min-height: 46px;
    font-size: .95rem;
    box-shadow: 0 10px 22px rgba(26, 115, 232, .22);
  }
}

/* ============== Rendimiento UI ============== */
/* Sidebars con scroll propio para no forzar repintados grandes */
.sidebar,
.sidebar-right {
  max-height: calc(100dvh - 32px);
  overflow: auto;
}

/* Suaviza repintados del zoom en la factura */
.invoice {
  will-change: transform;
  transform: translateZ(0);
}

/* Optimiza el grid principal */
.dashboard-container {
  contain: layout style;
}

/* ============== Exportación PDF (clon sin efectos) ============== */
/* Contenedor oculto para exportación */
#mi-export-wrap {
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  z-index: -1;
}

/* Clon exportable: sin transforms/animaciones/sombras costosas */
.exporting {
  transform: none !important;
  animation: none !important;
  transition: none !important;
  box-shadow: none !important;
  background: #ffffff !important;
  max-width: 794px;   /* ~ A4 ancho a 96dpi (aprox) */
  width: 100%;
}

/* Asegurar fondo blanco en todo el clon exportado */
.exporting, .exporting * {
  background-color: #ffffff !important;
}

/* Saltos de página opcionales para PDF (usa class="page-break" en el HTML) */
@media print {
  .page-break { page-break-before: always; }
}

/* Colores de impresión correctos */
html, body {
  -webkit-print-color-adjust: exact;
  print-color-adjust: exact;
}

/* ==========================================================
   🎯 IMPRESIÓN PERFECTA (usa directamente la factura)
   ========================================================== */
@media print {
  #__printRoot {
    display: none !important;
  }

  body.print-ready .dashboard-container {
    display: none !important;
  }

  body.print-ready #__printRoot {
    display: block !important;
  }

  @page {
    size: letter;
    margin: 12mm;
  }

  /* Ocultamos sidebars, panel derecho y cualquier cosa extra */
  .sidebar,
  .sidebar-right,
  .no-print,
  .dashboard-container > *:not(.invoice-container) {
    display: none !important;
  }

  /* Mostramos SOLO la factura */
  .dashboard-container,
  .invoice-container {
    display: block !important;
    width: 100% !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: visible !important;
  }

  /* Quita zoom/transform para que no recorte */
  #invoiceArea, .invoice {
    transform: none !important;
  }

  /* La factura ocupa un ancho seguro para Letter sin recortar */
  .invoice {
    margin: 0 auto !important;
    max-width: 720px !important; /* ✅ si quieres un pelín más: 740 */
    padding: 24px !important;
    border: none !important;
    box-shadow: none !important;
    overflow: visible !important;
  }

  /* Inputs impresos igual que en pantalla */
  .invoice input:not([type]),
  .invoice input[type="text"],
  .invoice input[type="date"],
  .invoice input[type="number"] {
    width: 100% !important;
    box-sizing: border-box !important;
    border: 1px solid var(--primary) !important;
    padding: 6px 8px !important;
    font-size: 14px !important;
    line-height: 18px !important;
    height: 32px !important;
    min-height: 32px !important;
    margin: 0 !important;
    appearance: none !important;
    -webkit-appearance: none !important;
    background: #ffffff !important;
  }

  .bill-info {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) !important;
    gap: 16px !important;
    align-items: start !important;
    page-break-inside: avoid !important;
    break-inside: avoid !important;
  }

  .bill-inputs {
    display: flex !important;
    flex-direction: column !important;
    gap: 8px !important;
  }

  .meta-info {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 12px 16px !important;
  }

  .bill-info > div,
  .meta-info > div {
    min-width: 0 !important;
    page-break-inside: avoid !important;
    break-inside: avoid !important;
  }

  /* Tabla igual que en pantalla */
  #invoiceTable {
    width: 100% !important;
    border-collapse: collapse !important;
    table-layout: fixed !important; /* ✅ evita que se salga */
  }

  #invoiceTable thead th {
    background: var(--primary) !important;
    color: #fff !important;
  }

  .row-total td,
  .cell-total {
    background: var(--primary) !important;
    color: #fff !important;
  }

  .footer {
    margin-top: 24px !important;
    text-align: center !important;
    font-size: 13px !important;
    color: var(--muted) !important;
    font-style: italic !important;
  }
}

/* Evitar que el contenteditable del address se deforme */
.company-info[contenteditable="true"] {
  display: block;
  width: auto !important;
  max-width: 170px;
  white-space: normal;
  line-height: 1.4;
}

.company-info[contenteditable="true"] br {
  display: block;
  line-height: 1.4;
}

/* ==========================================================
   ✅ FIX EXTRA: evita recortes laterales “por poquito”
   ========================================================== */
#invoiceArea, .invoice {
  max-width: 100% !important;
  box-sizing: border-box !important;
  overflow: visible !important;
}

#invoiceTable {
  width: 100% !important;
  table-layout: fixed !important;
}

/* ✅ Aquí estaba tu CSS cortado (ov...) — ya corregido */
#invoiceTable th, #invoiceTable td {
  overflow-wrap: anywhere !important;
  word-wrap: break-word !important;
  word-break: break-word !important;
}

/* ============================
   MISS INVOICE ASISTENTE
============================ */
.miss-invoice-panel {
  text-align: center;
  padding-top: 10px;
}

.miss-invoice-img {
  max-width: 150px;   /* tamaño controlado */
  height: auto;
  object-fit: contain;
  opacity: 0.9;
}

/* Ocultar en móviles para no estorbar */
@media (max-width: 960px) {
  .miss-invoice-panel {
    display: none;
  }
}

/* Scroll móvil para ver la factura completa sin cortar números */
@media (max-width: 960px) {
  .invoice-container {
    width: 100%;
    overflow-x: auto;
    overflow-y: visible;
    padding-bottom: 0;
    -webkit-overflow-scrolling: touch;
  }

  .invoice {
    width: 100%;
    min-width: 0;
    max-width: 100%;
    padding: 12px;
    border-radius: 10px;
    font-size: clamp(10px, 2.75vw, 13px);
  }

  .header {
    gap: 10px;
  }

  .company-info[contenteditable="true"] {
    max-width: 145px;
    font-size: .82em;
    line-height: 1.35;
  }

  .invoice-title {
    margin: 12px 0;
    font-size: clamp(1.35rem, 7vw, 2rem);
  }

  .bill-info {
    gap: 12px;
  }

  .meta-info {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  .section-title {
    font-size: .72rem;
    margin-bottom: 4px;
  }

  .invoice input:not([type]),
  .invoice input[type="text"],
  .invoice input[type="date"],
  .invoice input[type="number"] {
    min-height: 30px;
    padding: 6px 7px;
    border-width: 1px;
    border-radius: 6px;
    font-size: 1em;
  }

  #invoiceTable {
    margin-top: 12px;
    font-size: clamp(9px, 2.45vw, 12px);
  }

  #invoiceTable th:first-child,
  #invoiceTable td:first-child {
    width: 38%;
  }

  #invoiceTable th:nth-child(2),
  #invoiceTable td:nth-child(2) {
    width: 13%;
  }

  #invoiceTable th:nth-child(3),
  #invoiceTable td:nth-child(3),
  #invoiceTable th:nth-child(4),
  #invoiceTable td:nth-child(4) {
    width: 20%;
  }

  #invoiceTable th:nth-child(5),
  #invoiceTable td:nth-child(5),
  .row-action {
    width: 34px;
    min-width: 34px;
  }

  .delete-row-btn {
    width: 26px;
    height: 26px;
    font-size: 1rem;
  }

  #invoiceTable thead th,
  #invoiceTable tbody td,
  #invoiceTable tfoot td {
    padding: 6px 5px;
  }

  #invoiceTable input {
    min-width: 0;
    padding-left: 4px !important;
    padding-right: 4px !important;
  }

  .footer {
    margin-top: 14px;
    font-size: .82em;
    line-height: 1.45;
  }
}

@media (max-width: 640px) {
  .dashboard-container {
    padding: 8px;
    gap: 10px;
  }

  .sidebar,
  .sidebar-right {
    padding: 12px;
    border-radius: 10px;
  }

  .mobile-tools-grid {
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .btn,
  .control {
    min-height: 38px;
  }

  .invoice {
    min-width: 620px;
  }

  .invoice-container::after {
    content: "Desliza para ver la factura completa";
    display: block;
    margin: 8px 2px 0;
    color: #6b7280;
    font-size: .74rem;
    text-align: center;
  }
}

@media (max-width: 390px) {
  .invoice {
    min-width: 590px;
  }
}

/* Modo movil ajustado: la factura encaja en pantalla sin desbordarse a los lados. */
@media (max-width: 640px) {
  html,
  .invoice-body {
    max-width: 100%;
    overflow-x: hidden;
  }

  .dashboard-container,
  .invoice-container {
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
  }

  .invoice {
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    padding: 10px;
    overflow: hidden !important;
    transform: none !important;
  }

  .invoice-container::after {
    content: "";
    display: none;
  }

  .header {
    display: grid;
    grid-template-columns: minmax(0, auto) minmax(0, 1fr);
    align-items: start;
  }

  .logo-img {
    max-width: 90px;
    max-height: 80px;
  }

  .company-info[contenteditable="true"] {
    max-width: 100%;
    min-width: 0;
    font-size: .74em;
  }

  .invoice-title {
    font-size: clamp(1.15rem, 6vw, 1.65rem);
  }

  .meta-info {
    gap: 6px;
  }

  #invoiceTable {
    width: 100% !important;
    min-width: 0 !important;
    table-layout: fixed !important;
    font-size: clamp(7px, 2.05vw, 9px);
  }

  #invoiceTable th:first-child,
  #invoiceTable td:first-child {
    width: 34%;
  }

  #invoiceTable th:nth-child(2),
  #invoiceTable td:nth-child(2) {
    width: 12%;
  }

  #invoiceTable th:nth-child(3),
  #invoiceTable td:nth-child(3),
  #invoiceTable th:nth-child(4),
  #invoiceTable td:nth-child(4) {
    width: 20%;
  }

  #invoiceTable th:nth-child(5),
  #invoiceTable td:nth-child(5),
  .row-action {
    width: 26px;
    min-width: 26px;
  }

  #invoiceTable thead th,
  #invoiceTable tbody td,
  #invoiceTable tfoot td {
    padding: 5px 3px;
  }

  #invoiceTable input {
    min-height: 26px !important;
    padding: 4px 2px !important;
    font-size: 1em !important;
  }

  .delete-row-btn {
    width: 22px;
    height: 22px;
    font-size: .88rem;
  }

  .amount,
  #subtotal,
  #tax,
  #total {
    white-space: normal;
    overflow-wrap: anywhere;
  }
}

@media (max-width: 360px) {
  .invoice {
    padding: 8px;
  }

  #invoiceTable {
    font-size: clamp(6.5px, 1.9vw, 8px);
  }

  #invoiceTable thead th,
  #invoiceTable tbody td,
  #invoiceTable tfoot td {
    padding: 4px 2px;
  }

  #invoiceTable th:nth-child(5),
  #invoiceTable td:nth-child(5),
  .row-action {
    width: 22px;
    min-width: 22px;
  }

  .delete-row-btn {
    width: 19px;
    height: 19px;
    font-size: .78rem;
  }
}
