/* A4 Base Styles - semantic and print-friendly */

/* Defaults para variáveis CSS (podem ser sobrescritas) */
:root {
  --a4-bottom-gap: 24px;         /* gap mínimo do último elemento ao rodapé (ajustado) */
  --a4-bottom-safety: 24px;      /* margem de segurança para detecção de overflow (ajustado) */
  --a4-page-margin-top: 3cm;
  --a4-page-margin-right: 3cm;
  --a4-page-margin-bottom: 3cm;
  --a4-page-margin-left: 3cm;
  --a4-pad-top: 3cm;
  --a4-pad-right: 3cm;
  --a4-pad-bottom: 3cm;         /* bottom extra para área do número da página */
  --a4-pad-left: 3cm;
  --a4-page-number-bottom: 14mm;
  --a4-page-number-right: 20mm;
  --a4-page-number-size: 9pt;
  --a4-font-family: "Times New Roman", Georgia, Garamond, serif;
  --a4-font-size: 11.5pt;
  --a4-line-height: 1.42;
  --a4-paragraph-indent: 1.25em;
}

/* Page size for print */
@page {
  size: A4;
  /* alinhado com bkp: 18mm topo, 16mm laterais, 20mm inferior */
  margin: var(--a4-page-margin-top) var(--a4-page-margin-right)
          var(--a4-page-margin-bottom) var(--a4-page-margin-left);
}

@media print {
  html, body { background: #fff !important; }
  .a4-page { box-shadow: none !important; }
}

/* A4 editor container */
#a4-pages-container {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.a4-page {
  background: #ffffff;
  color: #111827; /* default text color; allow presets to override */
  max-width: 794px;
  width: 794px;      /* 210mm at 96dpi ≈ 794px */
  height: 1123px;    /* 297mm at 96dpi ≈ 1123px */
  border-radius: 2px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  position: relative;
}

.a4-content {
  /* default inner paddings com variáveis (fallbacks mantêm comportamento) */
  /* top right bottom left */
  padding: var(--a4-pad-top, 25mm) var(--a4-pad-right, 20mm)
           var(--a4-pad-bottom, 30mm) var(--a4-pad-left, 20mm);
  height: 100%;
  overflow: hidden;
  outline: none;
}

.a4-fixed-header,
.a4-fixed-footer {
  position: absolute;
  left: var(--a4-pad-left, 20mm);
  right: var(--a4-pad-right, 20mm);
  font-size: var(--a4-font-size, 11.5pt);
  z-index: 2;
  pointer-events: none;
}

.a4-fixed-header {
  top: 10mm;
}

.a4-fixed-footer {
  bottom: 10mm;
}

.a4-page-number {
  position: absolute;
  bottom: var(--a4-page-number-bottom, 14mm);
  right: var(--a4-page-number-right, 20mm);
  font-size: var(--a4-page-number-size, 9pt);
  color: #6b7280;
}

/* Typography defaults - can be overridden by presets */
.a4-content {
  font-family: var(--a4-font-family, "Times New Roman", Georgia, Garamond, serif);
  font-size: var(--a4-font-size, 11.5pt);
  line-height: var(--a4-line-height, 1.42);
  hyphens: auto;
}

.a4-content h1 { font-size: 22px; font-weight: 700; margin: 18px 0 10px; }
.a4-content h2 { font-size: 20px; font-weight: 700; margin: 16px 0 8px; }
.a4-content h3 { font-size: 18px; font-weight: 700; margin: 14px 0 6px; }

.a4-content p { margin: 10px 0; }

/* Parágrafos divididos pela paginação */
.a4-content p.a4-split-start {
  margin-bottom: 0 !important;  /* Remove margem inferior da parte que fica na página anterior */
}
.a4-content p.a4-split-continue {
  margin-top: 0 !important;      /* Remove margem superior da continuação */
  text-indent: 0 !important;     /* Sem recuo na continuação (não é início de parágrafo) */
}

/* Semantic helpers used by the HTML generator */
.measure {
  max-width: 160mm; /* readable line length */
  margin-left: auto;
  margin-right: auto;
}

.juizo {
  text-align: justify;
  text-align-last: left; /* avoid stretched last line */
}

.right { text-align: right; }

.no-indent { text-indent: 0 !important; }

/* Default first-line indent for body paragraphs; presets can override */
.a4-content p { text-indent: var(--a4-paragraph-indent, 1.25em); }
.a4-content p.no-indent { text-indent: 0; }

/* Quotes */
blockquote, .quote {
  border-left: 3px solid #d1d5db;
  padding-left: 12px;
  color: #374151;
  margin: 12px 0;
}

/* Action heading */
.acao {
  text-align: center;
  font-weight: 700;
}

/* Signature block */
.signature {
  text-align: center;
  margin-top: 24px;
}

/* Tables - ensure clean split behavior; pagination script handles row splitting */
.a4-content table { width: 100%; border-collapse: collapse; margin: 12px 0; }
.a4-content th, .a4-content td { border: 1px solid #e5e7eb; padding: 6px 8px; }
.a4-content thead th { background: #f3f4f6; }

/* Tabelas que foram divididas pela paginação: reduzir margens e padding para caber mais linhas */
.a4-content table.a4-table-split-part {
  margin-top: 6px;
  margin-bottom: 8px;
}
.a4-content table.a4-table-split-part th,
.a4-content table.a4-table-split-part td {
  padding: 4px 6px;
}

/* ✅ CONTROLE DE QUEBRA DE PÁGINA PARA TABELAS */
/* Tabelas pequenas (≤10 linhas): evitar quebra de página */
.a4-table-wrapper.keep-together {
  page-break-inside: avoid;
  break-inside: avoid;
}

.a4-table-wrapper.keep-together table {
  page-break-inside: avoid;
  break-inside: avoid;
}

/* Tabelas grandes (>10 linhas): permitir quebra mas evitar órfãos */
.a4-table-wrapper.allow-split table {
  page-break-inside: auto;
  break-inside: auto;
}

/* Sempre evitar quebra no cabeçalho */
.a4-content thead {
  page-break-inside: avoid;
  break-inside: avoid;
  page-break-after: avoid;
  break-after: avoid;
}

/* Evitar linhas órfãs (última linha de tabela sozinha em nova página) */
.a4-content tbody tr {
  page-break-inside: avoid;
  break-inside: avoid;
}

/* Espaçador inferior invisível para garantir gap mínimo do rodapé */
.a4-bottom-spacer {
  display: block;
  pointer-events: none;
  user-select: none;
  min-height: var(--a4-bottom-gap, 15px);
  height: var(--a4-bottom-gap, 15px);
  opacity: 0;
  flex-shrink: 0;
}

/* ✅ SUPORTE PARA GRÁFICOS E DIAGRAMAS SVG */
.a4-content svg {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 16px auto;
}

.a4-content figure {
  margin: 20px 0;
  text-align: center;
  page-break-inside: avoid;
  break-inside: avoid;
}

.a4-content figure svg {
  max-width: 100%;
  max-height: 600px; /* Limitar altura para caber em uma página */
  height: auto;
  margin: 0 auto;
}

.a4-content figcaption {
  margin-top: 8px;
  font-size: 10pt;
  color: #6b7280;
  font-style: italic;
}

/* ✅ BLOCOS DE JURISPRUDÊNCIA - garantir estilo consistente mesmo após divisão de página */
/* Aplica APENAS a jp-jurisprudencia (citações), não a jp-email (template de documento) */
.a4-content .jp-block.jp-jurisprudencia,
.a4-content section.jp-jurisprudencia {
  display: block;
  margin: 1.3em 2.5em;
  padding: 0.4em 1.6em;
  border-left: 3px solid #c4c4c4;
  background: #f8f8f8;
  border-radius: 3px;
  font-size: 0.9em !important;
  line-height: 1.55;
}

.a4-content .jp-block.jp-jurisprudencia p,
.a4-content section.jp-jurisprudencia p {
  margin: 0 0 0.8em 0;
  text-align: justify;
  text-indent: 0;
}

.a4-content .jp-block.jp-jurisprudencia p:last-child,
.a4-content section.jp-jurisprudencia p:last-child {
  margin-bottom: 0;
}

.a4-content .jp-block.jp-jurisprudencia *,
.a4-content section.jp-jurisprudencia * {
  font-size: inherit;
  line-height: inherit;
}
