@layer tokens, base, layout, components, pages, utilities;

/*
 * Printed pages (payslip and loan receipts, shift closing report, attendance
 * reports), printed straight from the browser.
 *   - The paper comes from ?size=; layouts/print writes its @page rule in
 *     <head> (PrintHelper) and the body class paper-a4 | paper-a5 |
 *     paper-80 | paper-58 | paper-a4-landscape.
 *   - On screen, .print-sheet is the sheet itself (same width and margins),
 *     so what is seen is what prints.
 *   - Sizes are print units (pt, mm) from tokens.css, per paper; the type
 *     and spacing scale with --receipt-scale, which paper_controller.js
 *     lowers to keep an A5/A4 receipt on one page.
 *   - A5/A4 receipts fill the page: the signatures and the footer go to the
 *     bottom (.paper-fit). Thermal paper is one narrow column, as long as
 *     the receipt.
 */
@layer pages {
  .print-page {
    background: var(--color-bg);
  }

  .print-toolbar-area {
    padding: var(--space-3);
    text-align: center;
  }

  .print-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
  }

  .print-hint {
    margin: var(--space-2) 0 0;
    color: var(--color-text-muted);
    font-size: var(--text-sm);
  }

  /* A sheet wider than a phone scrolls inside the stage, not the page. */
  .print-stage {
    overflow-x: auto;
    padding-inline: var(--space-4);
  }

  /* The sheet: paper width, page margins as padding. */
  .print-sheet {
    box-sizing: border-box;
    inline-size: var(--paper-width);
    margin: 0 auto var(--space-6);
    padding: var(--paper-margin);
    background: var(--color-surface);
    box-shadow: var(--print-sheet-shadow);
  }

  .receipt {
    --unit: calc(var(--receipt-gap) * var(--receipt-scale));
    box-sizing: border-box;
    inline-size: var(--paper-content-width);
    margin-inline: auto;
    color: var(--color-text);
    font-size: calc(var(--receipt-font) * var(--receipt-scale));
    line-height: 1.45;
  }

  /* A5/A4: the receipt fills the printable height (measured without it). */
  .paper-fit .receipt {
    display: flex;
    flex-direction: column;
    min-block-size: calc(var(--paper-content-height) - 1mm);
  }

  .paper-fit .is-measuring .receipt {
    min-block-size: 0;
  }

  .paper-fit .receipt-signatures {
    margin-block-start: auto;
    padding-block-start: calc(var(--unit) * 2);
  }

  .receipt-head {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: calc(var(--unit) / 3);
    padding-block-end: var(--unit);
    border-block-end: var(--receipt-frame) solid var(--color-text);
    text-align: center;
  }

  .receipt-logo {
    inline-size: calc(var(--receipt-logo) * var(--receipt-scale));
    block-size: auto;
  }

  .receipt-title {
    margin: 0;
    font-size: calc(var(--receipt-title) * var(--receipt-scale));
    font-weight: var(--weight-bold);
  }

  .receipt-subtitle {
    margin: 0;
    font-weight: var(--weight-semibold);
  }

  .receipt-number {
    font-family: ui-monospace, Consolas, monospace;
    direction: ltr;
    unicode-bidi: isolate;
    white-space: nowrap;
  }

  .receipt-meta {
    display: grid;
    grid-template-columns: max-content 1fr;
    gap: calc(var(--unit) / 3) var(--unit);
    margin: var(--unit) 0;
  }

  /* A5/A4: two pairs per row, spread over the width. */
  :is(.paper-a4, .paper-a5) .receipt-meta {
    grid-template-columns: max-content 1fr max-content 1fr;
  }

  .receipt-meta dt { color: var(--color-text-muted); }
  .receipt-meta dd { margin: 0; font-weight: var(--weight-semibold); }

  .receipt-lines {
    inline-size: 100%;
    border-collapse: collapse;
    line-height: 1.3;
  }

  .receipt-lines th,
  .receipt-lines td {
    padding: calc(var(--unit) / 4) 0;
    border-block-end: var(--receipt-rule) dashed var(--color-border-strong);
    text-align: start;
    vertical-align: top;
  }

  .receipt-lines .amount {
    padding-inline-start: var(--unit);
    text-align: end;
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
  }

  .receipt-lines .section th {
    padding-block-start: calc(var(--unit) * 0.7);
    color: var(--color-text-muted);
    font-size: 0.9em;
  }

  .receipt-lines .details {
    display: block;
    color: var(--color-text-muted);
    font-size: 0.85em;
  }

  /* A5/A4: the details beside the label, one line per row where it fits. */
  :is(.paper-a4, .paper-a5) .receipt-lines .details {
    display: inline;
    margin-inline-start: 0.8em;
  }

  .receipt-lines .total td {
    border-block-end: 0;
    font-weight: var(--weight-bold);
  }

  .receipt-net {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    margin-block-start: var(--unit);
    padding: calc(var(--unit) * 0.6) var(--unit);
    border: var(--receipt-frame) solid var(--color-text);
    border-radius: var(--receipt-radius);
  }

  .receipt-net strong {
    font-size: calc(var(--receipt-net) * var(--receipt-scale));
    font-variant-numeric: tabular-nums;
  }

  .receipt-note {
    margin: calc(var(--unit) * 0.6) 0 0;
    font-weight: var(--weight-semibold);
  }

  /* Notes after the net: a light frame; on thermal paper a rule instead. */
  .receipt-notes {
    margin-block-start: var(--unit);
    padding: calc(var(--unit) * 0.6) var(--unit);
    border: var(--receipt-rule) solid var(--color-border-strong);
    border-radius: var(--receipt-radius);
    font-weight: var(--weight-regular);
    line-height: 1.6;
  }

  .receipt-notes h2 {
    margin: 0 0 calc(var(--unit) / 3);
    font-size: 1em;
    font-weight: var(--weight-bold);
  }

  .receipt-notes p {
    margin: 0;
    white-space: pre-line;
    overflow-wrap: anywhere;
  }

  .receipt-notes p + p {
    margin-block-start: calc(var(--unit) / 3);
  }

  :is(.paper-80, .paper-58) .receipt-notes {
    padding: calc(var(--unit) * 0.6) 0 0;
    border: 0;
    border-block-start: var(--receipt-rule) dashed var(--color-text);
    border-radius: 0;
  }

  .receipt-signatures {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: calc(var(--unit) * 1.5);
    margin-block-start: calc(var(--receipt-sign-gap) * var(--receipt-scale));
    text-align: center;
  }

  .receipt-signatures span {
    display: block;
    padding-block-start: calc(var(--unit) / 3);
    border-block-start: var(--receipt-rule) solid var(--color-text);
  }

  .paper-58 .receipt-signatures { grid-template-columns: 1fr; gap: var(--receipt-sign-gap); }

  .receipt-foot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--unit);
    margin-block-start: var(--unit);
    padding-block-start: calc(var(--unit) * 0.6);
    border-block-start: var(--receipt-rule) dashed var(--color-border-strong);
    color: var(--color-text-muted);
    font-size: 0.85em;
  }

  /* Thermal: one column, the code centred under the text. */
  :is(.paper-80, .paper-58) .receipt-foot {
    flex-direction: column;
    text-align: center;
  }

  .receipt-qr svg {
    display: block;
    inline-size: calc(var(--receipt-qr) * var(--receipt-scale));
    block-size: calc(var(--receipt-qr) * var(--receipt-scale));
  }

  /* Thermal paper prints black only. */
  :is(.paper-80, .paper-58) .receipt-logo {
    filter: grayscale(1) contrast(1.4);
  }

  /* ---------- Printed reports (attendance) ---------- */
  .report-period {
    margin: var(--unit) 0;
    text-align: center;
    font-weight: var(--weight-semibold);
  }

  .report-stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(28mm, 1fr));
    gap: calc(var(--unit) * 0.6);
    margin-block-end: var(--unit);
  }

  .report-stats div {
    padding: calc(var(--unit) / 3) calc(var(--unit) * 0.6);
    border: var(--receipt-rule) solid var(--color-border-strong);
    border-radius: var(--receipt-radius);
  }

  .report-stats span { display: block; color: var(--color-text-muted); font-size: 0.85em; }
  .report-stats strong { font-size: 1.25em; font-variant-numeric: tabular-nums; }

  .report-table {
    inline-size: 100%;
    border-collapse: collapse;
    font-size: 0.9em;
  }

  .report-table th,
  .report-table td {
    padding: calc(var(--unit) / 3) calc(var(--unit) * 0.6);
    border: var(--receipt-rule) solid var(--color-border-strong);
    text-align: start;
    vertical-align: middle;
  }

  .report-table th {
    background: var(--color-surface-muted);
    font-weight: var(--weight-semibold);
  }

  .report-table .num {
    text-align: center;
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
  }

  .report-table tfoot td {
    background: var(--color-surface-muted);
    font-weight: var(--weight-bold);
  }

  .report-table tr.is-holiday td { background: var(--color-info-soft); }
  .report-table tr.is-assumed td { color: var(--color-text-muted); }

  /* Keep the colored badges and shaded rows on paper. */
  .report-print {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  @media print {
    /* Only the receipt: no toolbar, no hint, no buttons, no sheet frame. */
    .print-page {
      background: none;
    }

    .print-toolbar-area,
    .print-page .btn,
    .print-page button {
      display: none;
    }

    .print-stage {
      overflow: visible;
      padding: 0;
    }

    .print-sheet {
      inline-size: auto;
      margin: 0;
      padding: 0;
      box-shadow: none;
    }

    .receipt-head,
    .receipt-meta,
    .receipt-net,
    .receipt-notes,
    .receipt-signatures,
    .receipt-foot,
    .report-stats,
    .receipt-lines tr,
    .report-table tr {
      break-inside: avoid;
      page-break-inside: avoid;
    }

    .report-table thead {
      display: table-header-group;
    }
  }
}
