<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
/* ── Print-ready page setup ── */
@page {
    size: {{ style.page_format | default('A4') }};
    margin: {{ style.margin_top | default('18mm') }} {{ style.margin_right | default('18mm') }} {{ style.margin_bottom | default('18mm') }} {{ style.margin_left | default('18mm') }};
    @bottom-center {
        content: "Page " counter(page) " of " counter(pages);
        font-size: 9px;
        color: #ccc;
        font-family: Inter, sans-serif;
    }
}

:root {
    --primary: {{ style.primary_color | default('#2563eb') }};
    --font: {{ style.font_family | default('Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif') }};
}

*, *::before, *::after {
    -webkit-print-color-adjust: exact !important;
    -print-color-adjust: exact !important;
    color-adjust: exact !important;
}

body {
    font-family: var(--font);
    font-size: 12px;
    color: #1a1a1a;
    line-height: 1.6;
    margin: 0;
    padding: 40px 60px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ── Page break prevention ── */
.totals, .payment-link {
    page-break-inside: avoid;
}
.line-items tbody tr {
    page-break-inside: avoid;
}

.invoice {
    max-width: 650px;
    margin: 0 auto;
}

/* ── Header ── */
.header {
    margin-bottom: 60px;
}

.logo {
    max-width: 160px;
    max-height: 48px;
    margin-bottom: 10px;
    display: block;
}

.company-name {
    font-size: 18px;
    font-weight: 600;
    letter-spacing: 0.5px;
    margin-bottom: 4px;
}

.company-detail {
    font-size: 12px;
    color: #999;
    margin-bottom: 2px;
}

.document-title {
    font-size: 11px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 2px;
    color: var(--primary);
    margin-top: 12px;
}

.document-number {
    font-size: 11px;
    color: #ccc;
    margin-top: 4px;
}

/* ── Meta ── */
.meta {
    display: flex;
    justify-content: space-between;
    margin-bottom: 50px;
}

.meta-group {
    width: 45%;
}

.meta-label {
    font-size: 10px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #ccc;
    margin-bottom: 8px;
}

.meta-name {
    font-size: 13px;
    font-weight: 600;
    margin-bottom: 4px;
}

.meta-detail {
    font-size: 12px;
    color: #999;
}

/* ── Table ── */
.line-items {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 30px;
}

.line-items thead th {
    font-size: 10px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #ccc;
    padding: 12px 0;
    text-align: left;
    border-bottom: 1px solid #f0f0f0;
}

.line-items thead th:last-child,
.line-items thead th:nth-child(3),
.line-items thead th:nth-child(4) {
    text-align: right;
}

.line-items tbody td {
    padding: 14px 0;
    border-bottom: 1px solid #f5f5f5;
}

.line-items tbody td:last-child,
.line-items tbody td:nth-child(3),
.line-items tbody td:nth-child(4) {
    text-align: right;
}

/* ── Totals ── */
.totals {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 40px;
}

.totals-table {
    width: 200px;
}

.totals-row {
    display: flex;
    justify-content: space-between;
    padding: 8px 0;
    font-size: 12px;
    color: #999;
}

.totals-row.grand-total {
    font-size: 18px;
    font-weight: 600;
    color: var(--primary);
    padding-top: 16px;
    margin-top: 8px;
    border-top: 1px solid #f0f0f0;
}

/* ── Payment ── */
.payment-link {
    text-align: center;
    margin: 40px 0;
}

.pay-btn {
    display: inline-block;
    padding: 12px 32px;
    border: 1px solid #e0e0e0;
    border-radius: 4px;
    color: #1a1a1a;
    text-decoration: none;
    font-size: 12px;
    font-weight: 500;
    letter-spacing: 0.5px;
}

/* ── Footer ── */
.footer {
    margin-top: 60px;
    font-size: 11px;
    color: #ccc;
}

.notes {
    margin-bottom: 20px;
    font-size: 12px;
    color: #999;
}

.notes h4 {
    margin: 0 0 8px 0;
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #ccc;
}

/* ── Watermark ── */
.watermark {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) rotate(-45deg);
    font-size: 80px;
    font-weight: 900;
    color: var(--primary);
    opacity: 0.08;
    pointer-events: none;
    text-transform: uppercase;
}

{% if style.show_watermark | default(false) | to_bool %}
<div class="watermark">{{ style.watermark_text | default('PAID') }}</div>
{% endif %}
</style>
</head>
<body>
<div class="invoice">
    <div class="header">
        {% if style.logo_url %}
        <img src="{{ style.logo_url }}" alt="Logo" class="logo">
        {% endif %}
        <div class="company-name">{{ data.from.name }}</div>
        {% if data.from.address %}<div class="company-detail">{{ data.from.address | nl2br }}</div>{% endif %}
        {% if data.from.email %}<div class="company-detail">{{ data.from.email }}</div>{% endif %}
        {% if data.from.phone %}<div class="company-detail">{{ data.from.phone }}</div>{% endif %}
        <div class="document-title">{{ title_map.get(document_type, 'INVOICE') }}</div>
        {% if data.document_number %}<div class="document-number">#{{ data.document_number }}</div>{% endif %}
        {% if data.issue_date %}<div class="document-number">{{ data.issue_date | date_format }}</div>{% endif %}
    </div>

    <div class="meta">
        <div class="meta-group">
            <div class="meta-label">Bill To</div>
            <div class="meta-name">{{ data.to.name }}</div>
            {% if data.to.address %}<div class="meta-detail">{{ data.to.address | nl2br }}</div>{% endif %}
            {% if data.to.email %}<div class="meta-detail">{{ data.to.email }}</div>{% endif %}
            {% if data.to.phone %}<div class="meta-detail">{{ data.to.phone }}</div>{% endif %}
        </div>
        <div class="meta-group">
            {% if data.due_date %}
            <div class="meta-label">Due Date</div>
            <div class="meta-name">{{ data.due_date | date_format }}</div>
            {% endif %}
            {% if data.payment_terms %}
            <div class="meta-label">Terms</div>
            <div class="meta-detail">{{ data.payment_terms }}</div>
            {% endif %}
        </div>
    </div>

    <table class="line-items">
        <thead>
            <tr>
                <th>Description</th>
                <th>Qty</th>
                <th>Unit Price</th>
                <th>Amount</th>
            </tr>
        </thead>
        <tbody>
            {% for item in data.line_items %}
            <tr>
                <td>{{ item.description }}</td>
                <td>{{ item.quantity }}</td>
                <td>{{ item.unit_price | currency }}</td>
                <td>{{ (item.amount or item.unit_price * item.qty) | currency }}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>

    <div class="totals">
        <div class="totals-table">
            <div class="totals-row">
                <span>Subtotal</span>
                <span>{{ data.subtotal | currency }}</span>
            </div>
            {% if data.tax_rate > 0 %}
            <div class="totals-row">
                <span>Tax ({{ data.tax_rate | percent }})</span>
                <span>{{ (data.tax or data.tax_amount or 0) | currency }}</span>
            </div>
            {% endif %}
            <div class="totals-row grand-total">
                <span>Total</span>
                <span>{{ data.total | currency }}</span>
            </div>
        </div>
    </div>

    {% if data.payment_link %}
    <div class="payment-link">
        <a href="{{ data.payment_link }}" class="pay-btn">{{ data.payment_link_text | default('Pay Now') }}</a>
    </div>
    {% endif %}

    {% if data.notes %}
    <div class="notes">
        <h4>Notes</h4>
        <p>{{ data.notes | nl2br }}</p>
    </div>
    {% endif %}

    <div class="footer">
        Thank you for your business.
    </div>
</div>
</body>
</html>