<!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: #999;
        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') }};
    --bg: #f8f9fa;
}

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

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: var(--font);
    font-size: 13px;
    color: #1a1a1a;
    line-height: 1.6;
    background: var(--bg);
    padding: 30px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

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

.invoice {
    max-width: 680px;
    margin: 0 auto;
    background: white;
    border-radius: 12px;
    overflow: hidden;
    border: 1px solid #eee;
}

/* ── Hero banner ── */
.banner {
    background: var(--primary);
    color: white;
    padding: 30px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

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

.banner-left .company-name {
    font-size: 18px;
    font-weight: 700;
    margin-bottom: 2px;
}

.banner-left .company-detail {
    font-size: 12px;
    opacity: 0.85;
    line-height: 1.5;
}

.banner-right h1 {
    font-size: 32px;
    font-weight: 800;
    letter-spacing: 3px;
    text-transform: uppercase;
    margin: 0;
}

.banner-right .doc-number {
    font-size: 13px;
    opacity: 0.85;
    margin-top: 2px;
    text-align: right;
}

/* ── Content ── */
.content {
    padding: 30px 40px;
}

/* ── Info row ── */
.info-row {
    display: flex;
    gap: 30px;
    margin-bottom: 30px;
}

.info-card {
    flex: 1;
    background: var(--bg);
    border-radius: 8px;
    padding: 16px 20px;
}

.info-card .label {
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #999;
    margin-bottom: 6px;
}

.info-card .name {
    font-size: 15px;
    font-weight: 600;
    margin-bottom: 2px;
}

.info-card .detail {
    font-size: 12px;
    color: #666;
}

/* ── Line items ── */
.line-items {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    margin-bottom: 20px;
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid #eee;
}

.line-items thead th {
    background: #f8f9fa;
    padding: 12px 16px;
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: #999;
    text-align: left;
    border-bottom: 1px solid #eee;
}

.line-items thead th.right {
    text-align: right;
}

.line-items tbody td {
    padding: 12px 16px;
    border-bottom: 1px solid #f0f0f0;
}

.line-items tbody td.right {
    text-align: right;
}

.line-items tbody tr:last-child td {
    border-bottom: none;
}

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

.totals-box {
    background: var(--bg);
    border-radius: 8px;
    padding: 16px 24px;
    min-width: 240px;
}

.totals-row {
    display: flex;
    justify-content: space-between;
    padding: 4px 0;
    font-size: 13px;
}

.totals-row.grand-total {
    font-size: 18px;
    font-weight: 700;
    color: var(--primary);
    padding-top: 10px;
    margin-top: 6px;
    border-top: 2px solid var(--primary);
}

/* ── Footer ── */
.footer {
    padding: 20px 40px;
    background: var(--bg);
    font-size: 11px;
    color: #999;
    text-align: center;
    border-top: 1px solid #eee;
}

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

.notes .label {
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #999;
    margin-bottom: 4px;
}

.payment-link {
    text-align: center;
    margin: 20px 0;
}

.pay-btn {
    display: inline-block;
    padding: 12px 32px;
    background: var(--primary);
    color: white;
    text-decoration: none;
    font-size: 13px;
    font-weight: 600;
    border-radius: 8px;
    letter-spacing: 0.5px;
}

.watermark {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) rotate(-45deg);
    font-size: 80px;
    font-weight: 900;
    color: var(--primary);
    opacity: 0.15;
    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="banner">
        <div class="banner-left">
            {% 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>
        <div class="banner-right">
            <h1>{{ title_map.get(document_type, 'INVOICE') }}</h1>
            {% if data.document_number %}<div class="doc-number">#{{ data.document_number }}</div>{% endif %}
            {% if data.issue_date %}<div class="doc-number">{{ data.issue_date | date_format }}</div>{% endif %}
        </div>
    </div>

    <div class="content">
        <div class="info-row">
            <div class="info-card">
                <div class="label">Bill To</div>
                <div class="name">{{ data.to.name }}</div>
                {% if data.to.address %}<div class="detail">{{ data.to.address | nl2br }}</div>{% endif %}
                {% if data.to.email %}<div class="detail">{{ data.to.email }}</div>{% endif %}
                {% if data.to.phone %}<div class="detail">{{ data.to.phone }}</div>{% endif %}
            </div>
            <div class="info-card">
                {% if data.due_date %}
                <div class="label">Due Date</div>
                <div class="name">{{ data.due_date | date_format }}</div>
                {% else %}
                <div class="label">Issue Date</div>
                <div class="name">{{ data.issue_date | date_format }}</div>
                {% endif %}
                {% if data.payment_terms %}
                <div class="detail" style="margin-top:4px;">{{ data.payment_terms }}</div>
                {% endif %}
            </div>
        </div>

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

        <div class="totals">
            <div class="totals-box">
                <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.notes %}
        <div class="notes">
            <div class="label">Notes</div>
            <div>{{ data.notes | nl2br }}</div>
        </div>
        {% endif %}

        {% 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 %}
    </div>

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