/* Invoice Generator Layout - self-contained (no external stylesheet needed) */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap');
:root {
  --sidebar-width: 450px;
  --white:#ffffff;
  --gray-50:#F8FAFC; --gray-100:#F1F5F9; --gray-200:#E2E8F0; --gray-300:#CBD5E1;
  --gray-400:#94A3B8; --gray-500:#64748B; --gray-600:#475569; --gray-700:#334155; --gray-800:#1E293B;
  --primary:#111827;
  --brand-blue:#1A5DC8;
  --danger:#DC2626;
  --radius-sm:8px;
  --font-sans:'Inter', system-ui, -apple-system, sans-serif;
}
*{box-sizing:border-box}

body {
  margin: 0;
  padding: 0;
  overflow: hidden; /* Hide main scrollbar, panels scroll independently */
  background: var(--gray-100);
  font-family: var(--font-sans);
  color: var(--primary);
}

.generator-container {
  display: flex;
  height: 100vh;
  width: 100vw;
}

/* Editor Panel (Left) */
.editor-panel {
  width: var(--sidebar-width);
  background: var(--white);
  border-right: 1px solid var(--gray-200);
  display: flex;
  flex-direction: column;
  z-index: 10;
  box-shadow: 4px 0 15px rgba(0,0,0,0.03);
}

.editor-header {
  padding: 20px 24px;
  border-bottom: 1px solid var(--gray-200);
  background: var(--gray-50);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.editor-header h1 {
  font-size: 1.1rem;
  color: var(--primary);
  margin: 0;
}

/* Language toggle */
.lang-toggle {
  display: flex;
  border: 1px solid var(--gray-300);
  border-radius: var(--radius-sm);
  overflow: hidden;
}
.btn-lang {
  padding: 4px 10px;
  background: none;
  border: none;
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--gray-500);
  cursor: pointer;
  letter-spacing: 0.05em;
  transition: all 0.15s;
}
.btn-lang.active {
  background: var(--brand-blue, #1A5DC8);
  color: #fff;
}
.btn-lang:not(.active):hover {
  background: var(--gray-100);
  color: var(--primary);
}

.editor-content {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
}

.editor-section {
  margin-bottom: 32px;
}

.editor-section-title {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--gray-500);
  font-weight: 700;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--gray-200);
  padding-bottom: 8px;
}

/* Form Groups */
.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--gray-700);
  margin-bottom: 6px;
}

.form-control {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--gray-300);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-family: var(--font-sans);
  color: var(--primary);
  transition: border-color 0.2s;
}

.form-control:focus {
  outline: none;
  border-color: var(--brand-blue);
  box-shadow: 0 0 0 3px rgba(26, 93, 200, 0.1);
}

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

/* Dynamic Items */
.item-row {
  background: var(--gray-50);
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-sm);
  padding: 16px;
  margin-bottom: 12px;
  position: relative;
}

.btn-remove-item {
  position: absolute;
  top: 10px;
  right: 10px;
  background: none;
  border: none;
  color: var(--danger);
  cursor: pointer;
  font-size: 1.2rem;
  padding: 4px;
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.grid-3 {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 12px;
}

.btn-action {
  width: 100%;
  padding: 10px;
  background: var(--white);
  border: 1px dashed var(--gray-400);
  color: var(--gray-600);
  border-radius: var(--radius-sm);
  font-weight: 600;
  cursor: pointer;
  margin-bottom: 16px;
  transition: all 0.2s;
}

.btn-action:hover {
  border-color: var(--brand-blue);
  color: var(--brand-blue);
  background: var(--gray-50);
}

.btn-primary {
  width: 100%;
  padding: 14px;
  background: #1A5DC8;
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}
.btn-primary:hover {
  background: #154bad;
}

/* Preview Panel (Right) */
.preview-panel {
  flex: 1;
  background: var(--gray-200);
  overflow-y: auto;
  padding: 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* The actual invoice sheet inside preview */
.invoice-sheet {
  width: 100%;
  max-width: 850px;
  background: var(--white);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  margin-bottom: 40px;
  position: relative;
  min-height: 1100px; /* A4 aspect approximation */
  flex-shrink: 0; /* prevent flex container from collapsing card height */
}

/* Visual page-break rules every A4 page height so multi-page content
   looks paginated in the screen preview */
.invoice-sheet::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  pointer-events: none;
  background-image: repeating-linear-gradient(
    to bottom,
    transparent 0px,
    transparent calc(1100px - 1px),
    var(--gray-200) calc(1100px - 1px),
    var(--gray-200) 1100px
  );
  background-size: 100% 1100px;
}

/* Print Specific Rules */
@media print {
  @page {
    margin: 15mm;
    size: A4;
  }

  html, body {
    overflow: visible !important;
    height: auto !important;
    background: white;
  }

  .generator-container {
    display: block !important;
    height: auto !important;
    width: 100% !important;
    overflow: visible !important;
  }

  .editor-panel {
    display: none !important;
  }

  .preview-panel {
    display: block !important;
    padding: 0 !important;
    background: white !important;
    overflow: visible !important;
    height: auto !important;
    width: 100% !important;
  }

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

  /* Hide the screen-only page-break overlay */
  .invoice-sheet::after {
    display: none !important;
  }

  /* Keep table rows together across page breaks */
  tr {
    page-break-inside: avoid;
  }

  .page-break {
    page-break-before: always;
  }

  .no-print {
    display: none !important;
  }
}

/* Inherited Invoice Aesthetics */
.invoice-top-bar { height: 8px; background: linear-gradient(90deg, #1A5DC8 0%, #5A9DF5 100%); }
.invoice-content { padding: 50px; }
.invoice-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 50px; }
.brand-name { font-size: 24px; font-weight: 800; color: var(--primary); letter-spacing: -0.02em; }
.brand-name span { font-weight: 400; color: var(--gray-400); }
.company-details { font-size: 13px; color: var(--gray-500); line-height: 1.6; margin-top: 10px; white-space: pre-line; }
.brand-tagline { font-size: 11px; color: var(--gray-500); margin-top: 2px; }
.invoice-title { font-size: 42px; font-weight: 800; color: var(--gray-400); text-transform: uppercase; letter-spacing: 2px; margin-bottom: 20px; text-align: right; }
.meta-grid { display: grid; grid-template-columns: auto auto; gap: 6px 20px; text-align: right; font-size: 13px; }
.meta-label { color: var(--gray-500); font-weight: 500; }
.meta-value { color: var(--primary); font-weight: 600; }
.billing-section { display: flex; justify-content: space-between; margin-bottom: 40px; background: var(--gray-50); padding: 24px; border-radius: 8px; border: 1px solid var(--gray-100); }
.billing-block { flex: 1; }
.billing-heading { font-size: 11px; text-transform: uppercase; letter-spacing: 1.2px; color: var(--gray-400); font-weight: 700; margin-bottom: 10px; }
.client-name { font-size: 16px; font-weight: 700; color: var(--primary); margin-bottom: 6px; white-space: pre-wrap; }
.client-details { font-size: 13px; color: var(--gray-600); line-height: 1.6; white-space: pre-wrap; }
.table-container { margin-bottom: 40px; }
table { width: 100%; border-collapse: collapse; text-align: left; }
th { font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: var(--gray-500); font-weight: 600; padding: 12px; border-bottom: 2px solid var(--gray-200); }
td { padding: 16px 12px; font-size: 14px; color: var(--primary); border-bottom: 1px solid var(--gray-100); vertical-align: top; }
th.col-qty, th.col-price, th.col-total { text-align: right; }
td.col-qty, td.col-price, td.col-total { text-align: right; }
.col-total { font-weight: 600; }
.item-title { font-weight: 600; margin-bottom: 4px; color: var(--primary); }
.item-desc { font-size: 12px; color: var(--gray-500); line-height: 1.5; white-space: pre-wrap; }
.badge-type { display: inline-block; padding: 3px 8px; border-radius: 20px; font-size: 10px; font-weight: 600; text-transform: uppercase; }
.badge-recurring { background: #EAF1FE; color: #1A5DC8; }
.badge-onetime { background: #FEF3C7; color: #92400E; }
.totals-wrapper { display: flex; justify-content: flex-end; margin-bottom: 40px; }
.totals-table { width: 300px; }
.totals-table tr td { padding: 10px; font-size: 14px; border: none; }
.totals-table tr td:first-child { color: var(--gray-500); font-weight: 500; }
.totals-table tr.total-row { border-top: 2px solid var(--gray-200); }
.totals-table tr.total-row td { padding-top: 16px; }
.totals-table tr.total-row td:first-child { font-size: 16px; color: var(--primary); font-weight: 700; }
.totals-table tr.total-row td:last-child { font-size: 20px; color: #1A5DC8; font-weight: 800; }
.invoice-footer { border-top: 1px solid var(--gray-200); padding-top: 30px; display: flex; justify-content: space-between; gap: 30px; font-size: 12px; color: var(--gray-500); }
.payment-info h4, .terms-info h4 { font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: var(--gray-800); font-weight: 700; margin-bottom: 10px; }
.payment-info pre, .terms-info pre { font-family: var(--font-sans); white-space: pre-wrap; margin: 0; color: var(--gray-600); }

/* Terms of Service Print Area */
.terms-sheet { padding: 50px; }
.terms-header { text-align: center; border-bottom: 1px solid var(--gray-200); padding-bottom: 20px; margin-bottom: 30px; }
.terms-header h2 { font-size: 24px; color: var(--primary); }
.terms-content { font-size: 12px; color: var(--gray-700); line-height: 1.6; }
.terms-content h2 { font-size: 16px; color: var(--primary); margin: 24px 0 10px; }
.terms-content h3 { font-size: 14px; color: var(--gray-800); margin: 16px 0 8px; }
.terms-content p, .terms-content ul { margin-bottom: 12px; }
.terms-content ul { padding-left: 20px; }

/* ── Multi-system, systems-ordered cards, line-item grouping, balance ── */
.systems-block { margin-bottom: 40px; }
.systems-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 16px; margin-top: 12px; }
.system-card { background: var(--gray-50); border: 1px solid var(--gray-200); border-radius: 8px; padding: 16px; }
.system-name { font-weight: 700; font-size: 14px; color: var(--primary); margin-bottom: 6px; }
.system-config { font-size: 12px; color: var(--gray-600); line-height: 1.6; white-space: pre-wrap; }
.group-row td { background: var(--gray-50); font-weight: 700; font-size: 11px; text-transform: uppercase; letter-spacing: .6px; color: var(--brand-blue); padding: 10px 12px; border-bottom: 1px solid var(--gray-200); }
.group-subtotal td { text-align: right; font-size: 12px; font-weight: 600; color: var(--gray-600); padding: 8px 12px; border-bottom: 2px solid var(--gray-200); }
.group-subtotal td.col-total { color: var(--primary); font-weight: 700; }
.totals-table tr.balance-row { border-top: 2px solid var(--gray-200); }
.totals-table tr.balance-row td { padding: 14px 10px 10px; }
.totals-table tr.balance-row td:first-child { font-size: 16px; color: var(--primary); font-weight: 700; }
.totals-table tr.balance-row td:last-child { font-size: 20px; color: #1A5DC8; font-weight: 800; }

/* ── PDF export: let TOS flow across pages cleanly + keep backgrounds ── */
@media print {
  * { -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; }
  .tos-sheet { page-break-before: always; }
  .terms-sheet { padding: 30px 40px; }
  .terms-content h2, .terms-content h3 { page-break-after: avoid; break-after: avoid; }
  .terms-content p, .terms-content li { page-break-inside: avoid; break-inside: avoid; orphans: 3; widows: 3; }
  .group-row, .group-subtotal, tr.total-row, tr.balance-row { page-break-inside: avoid; }
}
