/*
 * Printing. Linked with media="print", so it costs nothing on screen.
 *
 * printReport() in ui.js puts the report into #printRoot as ONE table (.pframe): a header row
 * (logo, title, date range), a footer row, and a body row holding the summary box and the report.
 * A browser repeats a table's header and footer on every page it spreads over, which is what puts
 * the logo and the title at the top of every page. Everything outside #printRoot is hidden.
 */
@page {
  size: A4;
  margin: 10mm 10mm 14mm;
  @bottom-right { content: "Page " counter(page) " of " counter(pages); font: 8pt sans-serif; color: #445; }
}

/* A ledger is wide: the journal prints on a landscape page, where most rows fit on one line. */
@page journal {
  size: A4 landscape;
  margin: 10mm 10mm 14mm;
  @bottom-right { content: "Page " counter(page) " of " counter(pages); font: 8pt sans-serif; color: #445; }
}

@media print {
  #printRoot.land { page: journal; }
  html, body { height: auto !important; overflow: visible !important; background: #fff !important; }
  body > *:not(#printRoot) { display: none !important; }

  #printRoot {
    display: block !important;
    color: #14202b;
    font: 9pt/1.35 var(--sans, "Inter", system-ui, sans-serif);
    -webkit-print-color-adjust: exact; print-color-adjust: exact;
  }

  /* ---- the frame: repeating header and footer ---- */
  #printRoot table.pframe { width: 100%; border-collapse: collapse; table-layout: fixed; }
  #printRoot .pframe > thead { display: table-header-group; }
  #printRoot .pframe > tfoot { display: table-footer-group; }
  #printRoot .pframe > thead > tr > td,
  #printRoot .pframe > tbody > tr > td,
  #printRoot .pframe > tfoot > tr > td { padding: 0; border: 0; white-space: normal; }

  #printRoot .phead { display: flex; align-items: center; gap: 12px; padding-bottom: 5px; margin-bottom: 7px;
                      border-bottom: 1.5px solid #0b5cab; }
  #printRoot .phead img { width: 92px; height: auto; display: block; flex: 0 0 auto; }
  #printRoot .phead .pt { flex: 1; min-width: 0; }
  #printRoot .phead .po { font-size: 6.5pt; letter-spacing: .5px; text-transform: uppercase; color: #667; }
  #printRoot .phead .pn { font-size: 13pt; font-weight: 650; line-height: 1.15; }
  #printRoot .phead .ps { font-size: 8pt; color: #445; }
  #printRoot .phead .pd { font-size: 7pt; color: #667; text-align: right; white-space: nowrap; line-height: 1.3; }
  #printRoot .pfoot { display: flex; justify-content: space-between; gap: 12px; margin-top: 5px; padding-top: 3px;
                      border-top: 1px solid #cfd6de; font-size: 7pt; color: #667; }

  /* ---- "About this report": what it is and what is in it, first page only ---- */
  #printRoot .pabout { margin: 0 0 9px; padding: 6px 10px; border-left: 3px solid #0b5cab; background: #f3f7fc !important; break-inside: avoid; }
  #printRoot .pabout h4 { margin: 0 0 2px; font-size: 7pt; letter-spacing: .5px; text-transform: uppercase; color: #0b5cab; }
  #printRoot .pabout p { margin: 0 0 3px; font-size: 8.5pt; line-height: 1.4; }
  #printRoot .pabout ul { margin: 0; padding-left: 14px; font-size: 8pt; line-height: 1.4; }
  #printRoot .pabout li { margin: 0 0 1px; }
  #printRoot.land .pabout ul { column-count: 2; column-gap: 24px; }
  #printRoot.land .pabout li { break-inside: avoid; }

  /* ---- the summary box, first page only (it is in the body, not the header) ---- */
  #printRoot.land .psum { grid-template-columns: repeat(8, 1fr); }
  #printRoot.land .psum > div:nth-child(4n) { border-right: 1px solid #e3e8ee; }
  #printRoot.land .psum > div:nth-child(8n) { border-right: 0; }
  #printRoot.land .psum > div:nth-last-child(-n+8) { border-bottom: 0; }
  #printRoot .psum { display: grid; grid-template-columns: repeat(4, 1fr); margin: 0 0 9px;
                     border: 1px solid #cfd6de; border-radius: 3px; break-inside: avoid; background: #f8fafc !important; }
  #printRoot .psum > div { padding: 4px 8px; border-right: 1px solid #e3e8ee; border-bottom: 1px solid #e3e8ee; min-width: 0; }
  #printRoot .psum > div:nth-child(4n) { border-right: 0; }
  #printRoot .psum > div:nth-last-child(-n+4) { border-bottom: 0; }
  #printRoot .psum span { display: block; font-size: 6.5pt; letter-spacing: .4px; text-transform: uppercase; color: #667; }
  #printRoot .psum b { display: block; font-size: 9pt; font-weight: 650; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }

  /* ---- things that only make sense on a screen ---- */
  #printRoot button, #printRoot .exportmenu, #printRoot .pager, #printRoot .toolbar,
  #printRoot input, #printRoot select, #printRoot textarea, #printRoot .spinner,
  #printRoot [data-no-print], #printRoot [hidden] { display: none !important; }

  /* ---- cards and tables copied from a screen ---- */
  #printRoot .pbody .card { box-shadow: none !important; border: 1px solid #cfd6de; border-radius: 3px; margin: 0 0 10px; }
  #printRoot .pbody .card > header { padding: 6px 9px; border-bottom: 1px solid #e3e8ee; background: #f6f8fb !important; }
  #printRoot .pbody .card > header h3 { font-size: 10.5pt; }
  #printRoot .pbody .card .body { padding: 8px 9px; }
  #printRoot .tw { overflow: visible !important; }
  #printRoot .pbody table { width: 100%; border-collapse: collapse; font-size: 8.5pt; table-layout: auto; }
  #printRoot .pbody thead { display: table-header-group; }          /* column headings repeat on each page */
  #printRoot .pbody tfoot { display: table-row-group; }             /* totals once, at the end */
  #printRoot .pbody tr { break-inside: avoid; }
  #printRoot .pbody th { background: #eef2f7 !important; text-align: left; font-weight: 650; padding: 4px 6px; border-bottom: 1px solid #aeb8c4; }
  #printRoot .pbody td { padding: 3px 6px; border-bottom: 1px solid #e6ebf1; vertical-align: top; }
  /* On screen the app keeps every cell on one line and scrolls sideways; paper cannot scroll, so text wraps. */
  #printRoot .pbody th, #printRoot .pbody td { white-space: normal !important; overflow-wrap: anywhere; }
  #printRoot .pbody th.num, #printRoot .pbody td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap !important; }
  #printRoot .pbody tfoot td { font-weight: 650; border-top: 1.5px solid #14202b; border-bottom: 0; }
  #printRoot .pbody .mono { font-family: ui-monospace, Consolas, monospace; font-size: 8pt; white-space: nowrap !important; }
  #printRoot .muted { color: #667; }

  /* ---- the dashboard's cards and charts ---- */
  #printRoot .kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-bottom: 10px; }
  #printRoot .kpi { border: 1px solid #cfd6de; border-radius: 3px; padding: 6px 8px; break-inside: avoid; box-shadow: none !important; }
  #printRoot .grid2, #printRoot .grid3 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  #printRoot .grid2 > *, #printRoot .grid3 > * { min-width: 0; }
  #printRoot .pbody .card, #printRoot .kpi { break-inside: avoid; }
  #printRoot svg.chart { width: 100%; height: auto; max-height: 62mm; }
  #printRoot .bar-row { display: grid; grid-template-columns: 40% 1fr 70px; gap: 6px; align-items: center; margin: 2px 0; break-inside: avoid; }
  #printRoot .bar-row .track { background: #e8edf3 !important; height: 9px; border-radius: 2px; overflow: hidden; }
  #printRoot .bar-row .fill { background: #0b5cab !important; height: 100%; }
  #printRoot .bar-row .fill.neg { background: #b4492c !important; }
  #printRoot .ok, #printRoot .err, #printRoot .note { border: 1px solid #cfd6de; border-radius: 3px; padding: 5px 8px; margin: 0 0 8px; break-inside: avoid; }
  #printRoot .pill { border: 1px solid #aeb8c4; border-radius: 8px; padding: 0 5px; font-size: 7.5pt; }

  /* ---- the journal ----
   * Fixed column widths (the browser need not measure thousands of cells to decide them), and a plain
   * rule for breaking: a row never splits, an entry's heading row stays with its first line, and a SHORT
   * entry stays on one page (a long one, such as a 336-line transfer, is free to run across pages).
   */
  #printRoot table.pj { width: 100%; border-collapse: collapse; table-layout: fixed; font-size: 7.5pt; }
  #printRoot table.pj th { background: #eef2f7 !important; text-align: left; font-weight: 650; padding: 3px 4px; border-bottom: 1px solid #aeb8c4;
                           letter-spacing: 0; font-size: 7pt; }
  #printRoot table.pj td { padding: 1.5px 4px; line-height: 1.22; border-bottom: 1px solid #edf0f4; vertical-align: top; white-space: normal; overflow-wrap: anywhere; }
  #printRoot table.pj th.num, #printRoot table.pj td.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums;
                                                          font-family: inherit; }   /* the screen's wide number font would not fit */
  #printRoot table.pj .rt { color: #667; font-size: 6.5pt; }
  #printRoot table.pj thead { display: table-header-group; }
  #printRoot table.pj tfoot { display: table-row-group; }
  #printRoot table.pj tr { break-inside: avoid; }
  #printRoot table.pj tbody.keep { break-inside: avoid; }
  #printRoot table.pj tr.pj-h { break-after: avoid; }
  #printRoot table.pj tr.pj-h td { background: #eef2f7 !important; font-weight: 600; border-top: 1px solid #aeb8c4; }
  #printRoot table.pj tfoot td { font-weight: 650; border-top: 1.5px solid #14202b; border-bottom: 0; padding-top: 4px; }
}
