/**
 * Printable invoice document (SPEC-01). Loaded for all media: the same geometry is used by the
 * on-screen preview, the offscreen print root that is measured before printing, the browser print
 * and the html2pdf PDF. css/print.css must never change geometry inside .inv-doc, or the screen
 * measurement stops matching the printed page.
 *
 * The paper is always light: these document tokens are the same in every app theme and
 * html[data-theme] must not override them.
 */

.inv-doc {
    --inv-ink: #1a1e23;
    --inv-ink-strong: #2d3139;
    --inv-text-2: #475569;
    --inv-muted: #64748b;
    --inv-rule: #e2e8f0;
    --inv-rule-strong: #cbd5e1;
    --inv-surface: #f8fafc;
    --inv-surface-2: #fafafa;
    --inv-negative: #b91c1c;
    --inv-positive: #15803d;
    --inv-on-dark: #ffffff;
    --inv-paper: #ffffff;
    font-family: 'Outfit', 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    color: var(--inv-ink);
    background: var(--inv-paper);
    line-height: 1.4;
    box-sizing: border-box;
    width: 100%;
}

.inv-doc *,
.inv-doc *::before,
.inv-doc *::after {
    box-sizing: border-box;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
}

/* Offscreen print root: 718 px = A4 width minus 2 x 10 mm margins at 96 px/in. */
.inv-print-root {
    width: 718px;
    background: #ffffff;
}

@media screen {
    .inv-print-root {
        position: fixed;
        left: -10000px;
        top: 0;
        pointer-events: none;
    }
}

/* Preview: the continuous document. */
.inv-doc--flow {
    padding: 24px;
    max-width: 800px;
    margin: 0 auto;
}

/* Pages: flex columns with no gaps, so a page is exactly the sum of its parts. */
.inv-page {
    display: flex;
    flex-direction: column;
    padding: 4px 10px 0;
    background: var(--inv-paper);
}

/* Pins the footer to the page bottom. Never overflow: hidden (an oversized row must not be clipped). */
.inv-doc--multi .inv-page {
    min-height: 1046px;
}

.inv-doc--paged .inv-page {
    break-after: page;
    page-break-after: always;
}

.inv-doc--paged .inv-page:last-child {
    break-after: auto;
    page-break-after: auto;
}

.inv-part {
    flex: none;
}

.inv-foot-part {
    margin-top: auto;
}

/* First-page head: title strip */
.inv-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 16px;
    border-bottom: 2px solid var(--inv-ink-strong);
    padding-bottom: 6px;
    margin: 0 0 8px;
}

.inv-title {
    font-size: 1.6rem;
    line-height: 1.2;
    font-weight: 800;
    margin: 0;
    color: var(--inv-ink-strong);
    letter-spacing: -0.5px;
    white-space: nowrap;
}

.inv-meta {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    column-gap: 16px;
    row-gap: 0;
    margin: 0;
    font-size: 0.9rem;
    line-height: 1.4;
    min-width: 0;
}

.inv-meta-item {
    display: flex;
    gap: 6px;
    white-space: nowrap;
}

.inv-meta-item--key {
    flex-basis: 100%;
    justify-content: flex-end;
    font-size: 0.95rem;
}

.inv-meta dt {
    color: var(--inv-muted);
    font-weight: 500;
}

.inv-meta dt::after {
    content: ':';
}

.inv-meta dd {
    margin: 0;
    color: var(--inv-ink);
    font-weight: 600;
}

.inv-meta-item--key dd {
    font-weight: 700;
}

/* Seller and Billed To, side by side */
.inv-parties {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin: 0 0 8px;
    padding: 0;
}

.inv-party {
    padding: 6px 12px;
    min-width: 0;
}

.inv-party--seller {
    padding-left: 0;
}

.inv-party--buyer {
    background: var(--inv-surface);
    border-left: 4px solid var(--inv-text-2);
    border-radius: 0 8px 8px 0;
}

.inv-seller-name {
    font-size: 1.25rem;
    font-weight: 700;
    line-height: 1.25;
    margin: 0 0 2px;
    color: var(--inv-ink-strong);
}

.inv-party-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 0 0 2px;
}

.inv-party-label {
    font-size: 0.85rem;
    font-weight: 700;
    text-transform: uppercase;
    color: var(--inv-text-2);
    letter-spacing: 0.5px;
    margin: 0;
}

.inv-badge {
    font-size: 0.7rem;
    font-weight: 700;
    color: var(--inv-on-dark);
    background: var(--inv-text-2);
    padding: 1px 6px;
    border-radius: 4px;
    text-transform: uppercase;
}

.inv-buyer-name {
    font-size: 1.05rem;
    font-weight: 700;
    line-height: 1.3;
    margin: 0 0 2px;
    color: var(--inv-ink);
}

.inv-address {
    margin: 0;
    font-size: 0.85rem;
    line-height: 1.35;
    color: var(--inv-text-2);
    white-space: pre-line;
    overflow-wrap: anywhere;
}

.inv-line {
    margin: 2px 0 0;
    font-size: 0.85rem;
    line-height: 1.35;
    color: var(--inv-text-2);
    overflow-wrap: anywhere;
}

.inv-line strong {
    font-weight: 600;
    color: var(--inv-ink);
}

.inv-sep {
    color: var(--inv-muted);
    padding: 0 2px;
}

.inv-nowrap {
    white-space: nowrap;
}

/* Continuation head and page footer */
.inv-cont-head {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 12px;
    border-bottom: 2px solid var(--inv-ink-strong);
    padding-bottom: 4px;
    margin: 0 0 8px;
}

.inv-cont-title {
    font-size: 1.1rem;
    font-weight: 800;
    color: var(--inv-ink-strong);
    white-space: nowrap;
}

.inv-cont-flag {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--inv-muted);
}

.inv-cont-meta {
    font-size: 0.85rem;
    color: var(--inv-text-2);
    text-align: right;
}

.inv-cont-meta strong {
    color: var(--inv-ink);
}

.inv-page-foot {
    display: flex;
    justify-content: space-between;
    border-top: 1px solid var(--inv-rule);
    margin-top: 6px;
    padding-top: 3px;
    font-size: 0.75rem;
    color: var(--inv-muted);
}

/* Items table */
.inv-items-wrap {
    padding-bottom: 8px;
}

.inv-items {
    width: 100%;
    border-collapse: collapse;
    table-layout: auto;
}

.inv-items--frozen {
    table-layout: fixed;
}

.inv-items th {
    background: var(--inv-ink-strong);
    color: var(--inv-on-dark);
    padding: 4px 8px;
    font-size: 0.8rem;
    line-height: 1.35;
    font-weight: 600;
    text-transform: uppercase;
}

.inv-items th:first-child {
    border-radius: 4px 0 0 4px;
}

.inv-items th:last-child {
    border-radius: 0 4px 4px 0;
}

.inv-items td {
    padding: 4px 8px;
    border-bottom: 1px solid var(--inv-rule);
    font-size: 0.8rem;
    line-height: 1.35;
    vertical-align: top;
    white-space: nowrap;
}

/* Only the name column wraps. Never overflow-wrap: anywhere on number columns (it split "10"). */
.inv-items td.inv-item-name {
    white-space: normal;
    overflow-wrap: break-word;
    font-weight: 500;
}

.inv-c {
    text-align: center;
}

.inv-l {
    text-align: left;
}

.inv-r {
    text-align: right;
}

.inv-amt {
    font-weight: 500;
}

.inv-item-desc {
    display: block;
    color: var(--inv-muted);
    font-size: 0.8rem;
    font-weight: 400;
}

.inv-item-row {
    break-inside: avoid;
    page-break-inside: avoid;
}

/* Closing block: bank + QR, notes | totals, sign-off. Atomic. */
.inv-closing {
    display: grid;
    grid-template-columns: 1.2fr 1fr;
    gap: 16px;
    align-items: start;
    break-inside: avoid;
    page-break-inside: avoid;
}

.inv-closing-left,
.inv-closing-right {
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-width: 0;
}

.inv-pay {
    border: 1px dashed var(--inv-rule-strong);
    border-radius: 8px;
    padding: 8px 10px;
    background: var(--inv-surface-2);
}

.inv-pay--qr {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 10px;
    align-items: start;
}

.inv-block-label {
    font-size: 0.8rem;
    font-weight: 700;
    text-transform: uppercase;
    color: var(--inv-text-2);
    margin: 0 0 4px;
    letter-spacing: 0.5px;
}

.inv-bank-table {
    border-collapse: collapse;
    font-size: 0.8rem;
}

.inv-bank-table td {
    padding: 1px 8px 1px 0;
    vertical-align: top;
}

.inv-bank-table td:first-child {
    color: var(--inv-muted);
    white-space: nowrap;
}

.inv-bank-table td:last-child {
    font-weight: 600;
    color: var(--inv-ink);
    overflow-wrap: anywhere;
}

.inv-mono {
    font-family: monospace;
    font-size: 0.85rem;
}

/* BUG-29: the column is at least 112 px and grows only to fit the one-line label; a long UPI ID
   still wraps (min-content ignores its max-content width). */
.inv-pay-qr {
    width: min-content;
    min-width: 112px;
    text-align: center;
}

.inv-pay-qr-label {
    white-space: nowrap;
    margin: 0 0 3px;
    font-size: 0.7rem;
    line-height: 1.2;
    font-weight: 700;
    color: var(--inv-text-2);
    text-transform: uppercase;
}

/* Fixed 80 px box. Do not style the qrcodejs canvas/img children (forcing display shows both). */
.inv-upi-qr,
.inv-qr-img {
    width: 80px;
    height: 80px;
    margin: 0 auto;
    padding: 4px;
    border: 1px solid var(--inv-rule-strong);
    background: #ffffff;
    overflow: hidden;
}

.inv-qr-img {
    object-fit: contain;
    display: block;
}

.inv-upi-id {
    margin: 2px 0 0;
    font-size: 0.7rem;
    line-height: 1.2;
    font-weight: 600;
    font-family: monospace;
    color: var(--inv-ink);
    overflow-wrap: anywhere;
}

.inv-notes p {
    margin: 0;
    font-size: 0.8rem;
    line-height: 1.4;
    color: var(--inv-muted);
    white-space: pre-line;
}

.inv-notes-flow {
    padding-bottom: 2px;
}

.inv-thanks {
    margin: 0;
    font-size: 0.85rem;
    color: var(--inv-muted);
    font-style: italic;
}

/* Line-mode totals: every row as compact as the tax rows, same 0.9rem text (QA BUG-21).
   !important beats the unchanged inline padding of the totals markup. */
.invoice-totals-line td {
    padding-top: 2px !important;
    padding-bottom: 2px !important;
    line-height: 1.3 !important;
}

.invoice-totals-line .invoice-grand-row td {
    padding-top: 4px !important;
    padding-bottom: 4px !important;
}

.inv-signoff {
    align-self: flex-end;
    width: 180px;
    text-align: center;
}

/* A fixed box, so the layout never depends on the signature image having decoded. */
.inv-sign-slot {
    height: 44px;
    display: flex;
    align-items: flex-end;
    justify-content: center;
}

.inv-sign-img {
    display: block;
    position: static;
    max-height: 40px;
    max-width: 150px;
    object-fit: contain;
}

.inv-sign-label {
    margin: 0;
    border-top: 1px solid var(--inv-rule-strong);
    padding-top: 3px;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--inv-muted);
    text-transform: uppercase;
}

/* Preview on narrow paper (desktop preview at 1440 px, tablets, 390 px phones): stack. */
@container invoice-paper (max-width: 599px) {
    .inv-doc--flow {
        padding: 16px;
    }

    .inv-head {
        flex-direction: column;
        gap: 4px;
    }

    .inv-meta,
    .inv-meta-item--key {
        justify-content: flex-start;
    }

    .inv-parties,
    .inv-closing {
        grid-template-columns: 1fr;
    }

    .inv-doc--flow .inv-items-wrap {
        overflow-x: auto;
    }
}
