<!DOCTYPE html>
<html>
<head>
    <style>
        /* === EMAIL STYLES ===
           Email clients have limited CSS support, so we use inline-friendly styles.
           Worker name is the dominant element — no prominent Fox Fitt branding
           (Spark reads the vendor name from the document). */
        body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
        .container { max-width: 600px; margin: 0 auto; border: 1px solid #ddd; padding: 20px; }
        .header { text-align: center; border-bottom: 2px solid #333; padding-bottom: 10px; margin-bottom: 20px; }
        .beneficiary-name { font-size: 24px; font-weight: bold; text-transform: uppercase; color: #000; }
        .sub-header { font-size: 14px; color: #666; margin-bottom: 5px; }
        .title { font-size: 18px; font-weight: bold; color: #666; }
        .meta { margin-bottom: 20px; background-color: #f8f9fa; padding: 10px; border-radius: 4px; }
        .items-table { width: 100%; border-collapse: collapse; margin-bottom: 20px; }
        .items-table th, .items-table td { border-bottom: 1px solid #eee; padding: 8px; text-align: left; }
        .items-table th { background-color: #f8f9fa; }
        .totals { text-align: right; margin-top: 20px; border-top: 2px solid #333; padding-top: 10px; }
        .total-row { font-size: 20px; font-weight: bold; color: #000; }
        .footer { margin-top: 30px; font-size: 12px; color: #777; text-align: center; border-top: 1px solid #eee; padding-top: 10px; }
        .positive { color: green; }
        .negative { color: red; }
    </style>
</head>
<body>
    <div class="container">
        <!-- Header: worker name dominant -->
        <div class="header">
            <div class="sub-header">PAYMENT TO BENEFICIARY</div>
            <div class="beneficiary-name">{{ record.worker.name }}</div>
            <div class="title">{% if is_advance %}Advance Payslip #{{ record.id }}{% elif is_loan %}Loan Payslip #{{ record.id }}{% else %}Payslip #{{ record.id }}{% endif %}</div>
        </div>

        <!-- Beneficiary details -->
        <div class="meta">
            <strong>Beneficiary:</strong> {{ record.worker.name }}<br>
            <strong>ID Number:</strong> {{ record.worker.id_number }}<br>
            <strong>Date:</strong> {{ record.date }}
        </div>

        <!-- Line items table -->
        <table class="items-table">
            <thead>
                <tr>
                    <th>Description</th>
                    <th style="text-align: right;">Amount</th>
                </tr>
            </thead>
            <tbody>
                {% if is_advance %}
                <tr>
                    <td>Advance Payment: {{ advance_description }}</td>
                    <td style="text-align: right;">R {{ advance_amount|floatformat:2 }}</td>
                </tr>
                {% elif is_loan %}
                <tr>
                    <td>Loan Payment: {{ loan_description }}</td>
                    <td style="text-align: right;">R {{ loan_amount|floatformat:2 }}</td>
                </tr>
                {% else %}
                <!-- Base pay line -->
                <tr>
                    <td>Base Pay ({{ logs_count }} days worked)</td>
                    <td style="text-align: right;">R {{ logs_amount|floatformat:2 }}</td>
                </tr>

                <!-- All adjustments (bonuses add, deductions subtract) -->
                {% for adj in adjustments %}
                <tr>
                    <td>{{ adj.get_type_display }}: {{ adj.description }}</td>
                    <td style="text-align: right;" class="{% if adj.type in deductive_types %}negative{% else %}positive{% endif %}">
                        {% if adj.type in deductive_types %}- {% endif %}R {{ adj.amount|floatformat:2 }}
                    </td>
                </tr>
                {% endfor %}
                {% endif %}
            </tbody>
        </table>

        <!-- Net pay total -->
        <div class="totals">
            <p class="total-row">Net Pay: R {{ record.amount_paid|floatformat:2 }}</p>
        </div>

        <!-- Footer — minimal branding -->
        <div class="footer">
            <p>Payer: Fox Fitt | Generated for {{ record.worker.name }}</p>
            <p>Date Generated: {% now "Y-m-d H:i" %}</p>
        </div>
    </div>
</body>
</html>
