<!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') }};
}

*, *::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.5;
    margin: 0;
    padding: 20px 40px;
    -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: 700px;
    margin: 0 auto;
}

/* ── Header ── */
.header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 40px;
    padding-bottom: 20px;
    border-bottom: 3px solid var(--primary);
}

.logo-section {
    flex: 1;
}

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

.company-name {
    font-size: 16px;
    font-weight: 700;
    color: var(--primary);
    margin-bottom: 4px;
}

.company-detail {
    font-size: 12px;
    color: #666;
    line-height: 1.6;
}

.document-title {
    text-align: right;
}

.document-title h1 {
    font-size: 28px;
    font-weight: 700;
    color: var(--primary);
    margin: 0;
    letter-spacing: 2px;
}

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

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

.meta-group {
    width: 45%;
}

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

.meta-name {
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 2px;
}

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

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

.line-items thead th {
    background: var(--primary);
    color: white;
    padding: 10px 12px;
    text-align: left;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.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: 10px 12px;
    border-bottom: 1px solid #eee;
}

.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: 30px;
}

.totals-table {
    width: 250px;
}

.totals-row {
    display: flex;
    justify-content: space-between;
    padding: 6px 0;
}

.totals-row.subtotal {
    border-bottom: 1px solid #eee;
}

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

/* ── Footer ── */
.footer {
    margin-top: 40px;
    padding-top: 20px;
    border-top: 1px solid #eee;
    font-size: 11px;
    color: #999;
}

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

.notes h4 {
    margin: 0 0 4px 0;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #999;
}

.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;
}

.payment-link {
    text-align: center;
    margin: 30px 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: 4px;
    letter-spacing: 0.5px;
}

{% 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">
        <div class="logo-section">
            {% 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="document-title">
            <h1>{{ title_map.get(document_type, 'INVOICE') }}</h1>
            {% if data.document_number %}<div class="document-number">#{{ data.document_number }}</div>{% endif %}
            {% if data.issue_date %}<div class="document-number">Date: {{ data.issue_date | date_format }}</div>{% endif %}
        </div>
    </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 subtotal">
                <span>Subtotal</span>
                <span>{{ data.subtotal | currency }}</span>
            </div>
            {% if data.tax_rate > 0 %}
            <div class="totals-row subtotal">
                <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">
        <h4>Notes</h4>
        <p>{{ data.notes | nl2br }}</p>
    </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 class="footer">
        Thank you for your business.
    </div>
</div>
</body>
</html>