<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
@page {
    size: A4;
    margin: 0;
    @bottom-center {
        content: "Page " counter(page) " of " counter(pages);
        font-family: 'Inter', sans-serif;
        font-size: 8px;
        color: #9CA3AF;
    }
}

:root {
    --primary: {{ style.primary_color | default('#FF7A59') }};
    --primary-dark: {{ style.primary_dark | default('#E66A4A') }};
    --text-primary: #18191C;
    --text-secondary: #474A54;
    --text-tertiary: #84858D;
    --border: #C9CAD0;
    --bg-subtle: #F4F5F7;
    --bg-white: #FFFFFF;
    --radius: 4px;
    --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-print-color-adjust: exact !important;
    -print-color-adjust: exact !important;
    color-adjust: exact !important;
}

body {
    font-family: var(--font);
    font-size: 11px;
    color: var(--text-primary);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    counter-reset: page;
}

/* โ”€โ”€ Page break prevention โ”€โ”€ */
.totals, .totals-section { page-break-inside: avoid; }

/* โ”€โ”€ Hero Header โ”€โ”€ */
.hero-header {
    background: var(--primary);
    padding: 32px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: white;
}

.hero-brand {
    display: flex;
    align-items: center;
    gap: 16px;
}

.hero-logo {
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 16px;
    color: white;
}

.hero-company-name {
    font-size: 18px;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.hero-company-detail {
    font-size: 11px;
    opacity: 0.85;
    margin-top: 2px;
}

.hero-meta {
    text-align: right;
}

.hero-title {
    font-size: 32px;
    font-weight: 800;
    letter-spacing: 2px;
    text-transform: uppercase;
    margin: 0;
}

.hero-document-number {
    font-size: 13px;
    opacity: 0.85;
    margin-top: 4px;
}

/* โ”€โ”€ Content โ”€โ”€ */
.content {
    padding: 32px 40px;
    max-width: 740px;
    margin: 0 auto;
}

/* โ”€โ”€ Info Grid โ”€โ”€ */
.info-grid {
    display: flex;
    gap: 24px;
    margin-bottom: 32px;
}

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

.info-label {
    font-size: 9px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-tertiary);
    margin-bottom: 8px;
}

.info-name {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-primary);
    letter-spacing: -0.01em;
    margin-bottom: 4px;
}

.info-detail {
    font-size: 11px;
    color: var(--text-secondary);
    line-height: 1.5;
}

/* โ”€โ”€ Status Badge โ”€โ”€ */
.status-badge {
    display: inline-block;
    padding: 4px 10px;
    border-radius: 2px;
    font-size: 9px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-top: 8px;
}

.status-badge.paid {
    background: #26BA37;
    color: white;
}

.status-badge.pending {
    background: #FF7A59;
    color: white;
}

.status-badge.overdue {
    background: #D73953;
    color: white;
}

.status-badge.draft {
    background: var(--bg-subtle);
    color: var(--text-secondary);
}

/* โ”€โ”€ Line Items โ”€โ”€ */
.line-items-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    margin-bottom: 24px;
    border-radius: var(--radius);
    overflow: hidden;
    border: 1px solid var(--border);
}

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

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

.line-items-table tbody td {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    font-size: 11px;
}

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

.line-items-table tbody tr:nth-child(odd) {
    background: var(--bg-subtle);
}

.line-items-table tbody td.right {
    text-align: right;
    font-variant-numeric: tabular-nums;
}

.line-items-table tbody td.description {
    font-weight: 500;
    color: var(--text-primary);
}

.line-items-table tbody td.quantity {
    color: var(--text-secondary);
    font-variant-numeric: tabular-nums;
}

.line-items-table tbody td.price {
    color: var(--text-secondary);
    font-variant-numeric: tabular-nums;
}

.line-items-table tbody td.amount {
    font-weight: 500;
    font-variant-numeric: tabular-nums;
}

/* โ”€โ”€ Totals โ”€โ”€ */
.totals-section {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 32px;
}

.totals-container {
    width: 260px;
    background: var(--bg-subtle);
    border-radius: var(--radius);
    padding: 16px 20px;
}

.totals-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 6px 0;
    font-size: 11px;
}

.totals-row .label {
    color: var(--text-secondary);
}

.totals-row .value {
    font-weight: 500;
    font-variant-numeric: tabular-nums;
}

.totals-row.grand-total {
    padding-top: 12px;
    margin-top: 8px;
    border-top: 2px solid var(--primary);
}

.totals-row.grand-total .label {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-primary);
}

.totals-row.grand-total .value {
    font-size: 20px;
    font-weight: 700;
    color: var(--primary);
    font-variant-numeric: tabular-nums;
    letter-spacing: -0.02em;
}

/* โ”€โ”€ Payment Info โ”€โ”€ */
.payment-info {
    display: flex;
    gap: 24px;
    margin-bottom: 24px;
}

.payment-block {
    flex: 1;
    background: var(--bg-subtle);
    border-radius: var(--radius);
    padding: 16px 20px;
}

.payment-label {
    font-size: 9px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-tertiary);
    margin-bottom: 8px;
}

.payment-value {
    font-size: 11px;
    color: var(--text-secondary);
    line-height: 1.5;
}

/* โ”€โ”€ Notes โ”€โ”€ */
.notes-section {
    margin-bottom: 24px;
}

.notes-label {
    font-size: 9px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-tertiary);
    margin-bottom: 8px;
}

.notes-text {
    font-size: 11px;
    color: var(--text-secondary);
    line-height: 1.5;
}

/* โ”€โ”€ Payment Link โ”€โ”€ */
.payment-link {
    text-align: center;
    padding: 24px 0;
}

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

/* โ”€โ”€ Footer โ”€โ”€ */
.footer {
    text-align: center;
    padding: 32px 0;
    border-top: 1px solid var(--border);
}

.footer-text {
    font-size: 11px;
    color: var(--text-tertiary);
    margin-bottom: 4px;
}

.footer-company {
    font-size: 10px;
    color: var(--text-tertiary);
}

/* โ”€โ”€ Watermark โ”€โ”€ */
{% if style.show_watermark | default(false) | to_bool %}
.watermark {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) rotate(-45deg);
    font-size: 120px;
    font-weight: 900;
    color: var(--primary);
    opacity: 0.06;
    pointer-events: none;
    text-transform: uppercase;
    letter-spacing: 20px;
    z-index: -1;
}
{% endif %}
</style>
</head>
<body>
{% if style.show_watermark | default(false) | to_bool %}
<div class="watermark">{{ style.watermark_text | default('PAID') }}</div>
{% endif %}

<div class="hero-header">
    <div class="hero-brand">
        {% if style.logo_url %}
        <img src="{{ style.logo_url }}" alt="Logo" class="hero-logo" style="background: rgba(255,255,255,0.2); border-radius: 8px; object-fit: contain;">
        {% else %}
        <div class="hero-logo">{{ data.from.name | default('') | truncate(1, True, '') }}</div>
        {% endif %}
        <div>
            <div class="hero-company-name">{{ data.from.name }}</div>
            {% if data.from.email or data.from.phone %}
            <div class="hero-company-detail">
                {% if data.from.email %}{{ data.from.email }}{% endif %}
                {% if data.from.email and data.from.phone %} ยท {% endif %}
                {% if data.from.phone %}{{ data.from.phone }}{% endif %}
            </div>
            {% endif %}
        </div>
    </div>
    <div class="hero-meta">
        <div class="hero-title">{{ title_map.get(document_type, 'INVOICE') }}</div>
        {% if data.document_number %}
        <div class="hero-document-number">#{{ data.document_number }}</div>
        {% endif %}
        {% if data.issue_date %}
        <div class="hero-document-number">{{ data.issue_date | date_format }}</div>
        {% endif %}
    </div>
</div>

<div class="content">
    <div class="info-grid">
        <div class="info-card">
            <div class="info-label">Bill To</div>
            <div class="info-name">{{ data.to.name }}</div>
            {% if data.to.email %}
            <div class="info-detail">{{ data.to.email }}</div>
            {% endif %}
            {% if data.to.phone %}
            <div class="info-detail">{{ data.to.phone }}</div>
            {% endif %}
            {% if data.to.address %}
            <div class="info-detail">{{ data.to.address | nl2br }}</div>
            {% endif %}
        </div>
        <div class="info-card">
            <div class="info-label">Payment Details</div>
            {% if data.due_date %}
            <div class="info-name">Due {{ data.due_date | date_format }}</div>
            {% endif %}
            {% if data.payment_terms %}
            <div class="info-detail">{{ data.payment_terms }}</div>
            {% endif %}
            {% if data.status %}
            <div class="status-badge {{ data.status | lower | replace(' ', '-') }}">
                {{ data.status | upper }}
            </div>
            {% endif %}
        </div>
    </div>

    <table class="line-items-table">
        <thead>
            <tr>
                <th style="flex: 2;">Description</th>
                <th class="right" style="width: 60px;">Qty</th>
                <th class="right" style="width: 100px;">Unit</th>
                <th class="right" style="width: 100px;">Amount</th>
            </tr>
        </thead>
        <tbody>
            {% for item in data.line_items %}
            <tr>
                <td class="description">{{ item.description }}</td>
                <td class="quantity">{{ item.quantity }}</td>
                <td class="price">{{ item.unit_price | currency }}</td>
                <td class="amount">{{ (item.amount or item.unit_price * item.qty) | currency }}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>

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

    {% if data.notes %}
    <div class="notes-section">
        <div class="notes-label">Notes</div>
        <div class="notes-text">{{ 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 class="footer">
        <div class="footer-text">Thank you for your business.</div>
        <div class="footer-company">{{ data.from.name }}</div>
    </div>
</div>
</body>
</html>