/* ==========================================================================
   Nizam print layer
   --------------------------------------------------------------------------
   Improves the GENERAL print path: screens sent to the printer directly or
   through printThis() — ledgers, reports, payment vouchers, purchase and sale
   detail views, trial balance, register details.

   WHAT THIS FILE DOES NOT TOUCH
   The receipt templates in resources/views/sale_pos/receipts/ (slim, slim2,
   classic, detailed, elegant, english-arabic, packing_slip, delivery_note,
   zatca_*) carry their own tuned @media print rules and millimetre sizing for
   thermal and A4 stationery. Every rule below is either scoped away from them
   with :not(), or written at a specificity their own rules beat. The same
   applies to label/barcode sheets, whose geometry must not move by a pixel.

   Load order: last, after nizam-compat.css.
   ========================================================================== */

@media print {

  /* ── Page setup ───────────────────────────────────────────────────────── */
  @page { margin: 12mm 10mm; }
  @page :first { margin-top: 10mm; }

  html, body {
    background: #fff !important;
    color: #000 !important;
    font-family: var(--nz-font);
    font-size: 11pt;
    line-height: 1.5;
  }

  /* Toner discipline: no filled grounds, no shadows, no rounded boxes. */
  *, *::before, *::after {
    box-shadow: none !important;
    text-shadow: none !important;
  }

  /* ── Hide interface chrome ────────────────────────────────────────────── */
  .no-print,
  #side-bar, .main-sidebar, .sidebar-toggle, .main-header, .navbar,
  .content-header .breadcrumb,
  .main-footer, footer,
  #toast-container,
  .dataTables_filter, .dataTables_length, .dataTables_paginate,
  .dataTables_info, .dt-buttons,
  .modal-header .close, .modal-footer,
  .box-tools, .btn-box-tool,
  .filters, .filter-box,
  .select2-container--open,
  .tooltip, .popover,
  /* The guided tour lays three full-document overlay divs over the page. They
     are the last thing left overflowing a printed sheet, and a tour left open
     would grey out every page that comes off the printer. */
  .tour-backdrop, .tour-step-background,
  .pf-card {
    display: none !important;
  }

  /* Buttons and inputs are meaningless on paper — but leave anything inside a
     receipt or label alone, those templates decide for themselves. */
  :not(.ticket):not(.label-wrapper) > .btn,
  :not(.ticket):not(.label-wrapper) > .tw-dw-btn,
  .btn-group, .input-group-btn {
    display: none !important;
  }

  /* ── Layout: reclaim the full sheet ───────────────────────────────────── */
  /* Only the app's own chrome wrappers are reset. Bootstrap grid classes are
     deliberately NOT touched: `.row` carries -15px side margins that the
     column padding compensates for, so zeroing the columns alone pushes
     content past both page edges — which clips the start edge in RTL and
     silently truncates Arabic labels and totals on printed invoices. */
  .content-wrapper, .content, .container, .container-fluid {
    margin: 0 !important;
    padding: 0 !important;
    width: 100% !important;
    float: none !important;
  }
  /* The grid IS reset here, but only as a matched pair. The row's -15px side
     margins and the column's 15px side padding cancel out on screen, yet they
     leave the row's box 30px wider than the sheet, so the printed document
     scrolls sideways and the engine may rescale the page. Zeroing both keeps
     content exactly where it was - it is zeroing the columns alone that
     pushes text past the page edges and truncates Arabic labels. Receipt and
     label templates size themselves in millimetres and are left alone. */
  body:not(.ticket) .row:not(.ticket-row) {
    margin-inline: 0 !important;
  }
  body:not(.ticket) .row:not(.ticket-row) > [class*="col-"] {
    padding-inline: 0 !important;
  }
  .ticket .row, .label-wrapper .row, .zatca-invoice .row,
  .ticket [class*="col-"], .label-wrapper [class*="col-"], .zatca-invoice [class*="col-"] {
    margin-inline: revert !important;
    padding-inline: revert !important;
  }
  /* ── Wide tables ───────────────────────────────────────────────────────
     Reports with many columns use DataTables scrollX, which puts the table in
     a fixed-width scroller and clones the header into a second table above
     it. On paper that prints only whichever horizontal slice the user
     happened to be scrolled to - the stock report is 1650px of table in a
     1100px page, so a third of its columns silently never appear.

     Both scrollers are opened up and both tables forced to page width, so the
     clone header still lines up with the body beneath it. Columns get narrow
     rather than lost; the type steps down and long values wrap.             */
  .dataTables_scroll,
  .dataTables_scrollHead,
  .dataTables_scrollBody,
  .dataTables_scrollFoot,
  .table-responsive {
    overflow: visible !important;
    width: auto !important;
    max-width: none !important;
  }
  /* The *Inner wrappers must be 100%, not auto: DataTables gives them an
     inline pixel width matching the un-shrunk table, and an auto-width
     shrink-to-fit box makes the table's own width:100% resolve right back to
     that same over-wide value. */
  .dataTables_scrollHeadInner,
  .dataTables_scrollFootInner {
    overflow: visible !important;
    width: 100% !important;
    max-width: none !important;
  }
  .dataTables_scrollHeadInner > table,
  .dataTables_scrollBody > table,
  .dataTables_scrollFootInner > table {
    width: 100% !important;
    table-layout: fixed !important;
  }
  table.dataTable {
    font-size: 9pt !important;
  }
  table.dataTable th, table.dataTable td {
    overflow-wrap: anywhere;
    word-break: break-word;
    padding: 3px 4px !important;
    /* DataTables writes a measured pixel width onto every header cell. Under
       table-layout: fixed those add back up to the original over-wide table,
       so they have to go for the page-width table to hold. */
    width: auto !important;
    min-width: 0 !important;
    max-width: none !important;
  }
  /* Header labels must wrap once the columns are narrow. .nz-col-num keeps
     figures on one line, which is right on screen but makes a long header
     like "Current stock value (by purchase price)" run over its neighbours
     on paper. The rule is relaxed for header cells only. */
  table.dataTable th,
  table.dataTable th.nz-col-num {
    white-space: normal !important;
    vertical-align: bottom;
    /* Wrap between words, never inside one: overflow-wrap:anywhere splits
       Arabic headers mid-word ("الإختلافا / ت"), which is unreadable. Long
       unbroken values in body cells still need the harder break. */
    overflow-wrap: break-word;
    word-break: normal;
    hyphens: none;
  }
  table.dataTable td.nz-col-num { white-space: nowrap !important; }
  /* Sort indicators are a screen control. */
  table.dataTable thead th::before,
  table.dataTable thead th::after,
  table.dataTable thead .sorting::after,
  table.dataTable thead .sorting_asc::after,
  table.dataTable thead .sorting_desc::after,
  table.dataTable thead th > .DataTables_sort_wrapper > span {
    display: none !important;
  }
  /* The body table keeps a sizing-only copy of the header. It is hidden on
     screen; it must stay hidden on paper or every report prints its headers
     twice. */
  .dataTables_scrollBody > table > thead {
    display: none !important;
  }

  .modal, .modal-dialog, .modal-content, .modal-body {
    position: static !important;
    width: 100% !important;
    max-width: none !important;
    max-height: none !important;
    overflow: visible !important;
    margin: 0 !important;
    padding: 0 !important;
    border: 0 !important;
    background: #fff !important;
  }
  .modal-backdrop { display: none !important; }

  /* Panels flatten to plain sections. */
  .box, .panel {
    border: 0 !important;
    margin: 0 0 6mm !important;
    break-inside: avoid;
  }
  .box-header, .panel-heading {
    background: #fff !important;
    border-bottom: 1px solid #000 !important;
    padding: 0 0 2mm !important;
  }
  .box-body, .panel-body { padding: 0 !important; }

  /* ── Tables: the thing most often printed here ────────────────────────── */
  .table, table {
    width: 100% !important;
    border-collapse: collapse !important;
    font-size: 9.5pt;
  }
  /* Header repeats on every page of a long report. */
  thead { display: table-header-group; }
  tfoot { display: table-footer-group; }
  tr, td, th { break-inside: avoid; page-break-inside: avoid; }

  .table > thead > tr > th {
    background: #fff !important;
    color: #000 !important;
    border-bottom: 1.2pt solid #000 !important;
    font-weight: 700;
    padding: 2mm 1.5mm !important;
  }
  .table > tbody > tr > td {
    border-top: 0.4pt solid #999 !important;
    padding: 1.6mm 1.5mm !important;
    color: #000 !important;
  }
  .table-striped > tbody > tr:nth-of-type(odd),
  .table > tbody > tr:hover { background: #fff !important; }
  .table-bordered, .table-bordered th, .table-bordered td {
    border-color: #999 !important;
  }

  /* Digits stay aligned on paper too. */
  td, th, .ws-nowrap { font-variant-numeric: tabular-nums; }

  /* ── Status must survive greyscale ────────────────────────────────────── */
  /* Colour is unreliable on a mono laser, so state is carried by the border
     and the text, both of which print. Backgrounds are dropped. */
  .label, .badge,
  .bg-light-green, .bg-aqua, .bg-yellow, .bg-red, .bg-gray, .bg-green,
  .label-success, .label-info, .label-warning, .label-danger, .label-default {
    background: #fff !important;
    color: #000 !important;
    border: 0.8pt solid #000 !important;
    padding: 0.4mm 1.2mm !important;
    font-weight: 700;
  }

  /* ── Typography ───────────────────────────────────────────────────────── */
  h1 { font-size: 16pt; }
  h2 { font-size: 14pt; }
  h3 { font-size: 12pt; }
  h1, h2, h3, h4 { break-after: avoid; page-break-after: avoid; color: #000 !important; }
  a, a:visited { color: #000 !important; text-decoration: none; }
  a[href]::after { content: none !important; }   /* keep the app's existing behaviour */
  .text-muted, .text-muted-imp { color: #333 !important; }

  /* ── RTL on paper ─────────────────────────────────────────────────────── */
  /* Direction is inherited from <html dir>, so it already prints correctly.
     These two keep mixed Arabic/English from reordering in the printed copy. */
  [dir="rtl"] .ws-nowrap,
  [dir="rtl"] .display_currency,
  [dir="rtl"] td [data-orig-value] { unicode-bidi: isolate; }

  /* ── Explicitly out of scope ──────────────────────────────────────────── */
  /* Receipt and label templates own their own print geometry. Reset anything
     this file might otherwise have inherited onto them. */
  .ticket, .ticket *,
  .label-wrapper, .label-wrapper *,
  .zatca-invoice, .zatca-invoice * {
    font-size: revert;
    line-height: revert;
  }
  .ticket table, .label-wrapper table, .zatca-invoice table {
    width: revert;
    font-size: revert;
  }
}
