<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
/* ═══════════════════════════════════════════════
   WORK ORDER — Professional Field Service Document
   ═══════════════════════════════════════════════ */

/* ── Print-ready page setup ── */
@page {
    size: {{ style.page_format | default('Letter') }};
    margin: {{ style.margin_top | default('14mm') }} {{ style.margin_right | default('14mm') }} {{ style.margin_bottom | default('14mm') }} {{ style.margin_left | default('14mm') }};
    @bottom-center {
        content: "Page " counter(page) " of " counter(pages);
        font-size: 9px;
        color: #94a3b8;
        font-family: {{ style.font_family | default('Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif') }};
    }
}

:root {
    --primary: {{ style.primary_color | default('#1e3a5f') }};
    --primary-dark: {{ style.primary_dark | default('#152a44') }};
    --primary-light: {{ style.primary_light | default('#dbeafe') }};
    --primary-faint: {{ style.primary_faint | default('#eff6ff') }};
    --accent: {{ style.accent_color | default('#dc2626') }};
    --font: {{ style.font_family | default('Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif') }};
    --text-heading: {{ style.text_primary | default('#0f172a') }};
    --text-body: {{ style.text_secondary | default('#475569') }};
    --text-muted: {{ style.text_tertiary | default('#94a3b8') }};
    --border: {{ style.border_color | default('#cbd5e1') }};
    --border-light: {{ style.border_light | default('#e2e8f0') }};
    --bg-subtle: {{ style.bg_subtle | default('#f1f5f9') }};
    --bg-card: #ffffff;
    --success: #059669;
    --success-faint: #ecfdf5;
}

/* ── Print color preservation ── */
*, *::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: 11px;
    color: var(--text-body);
    line-height: 1.55;
    background: #e2e8f0;
    padding: 16px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ═══════════════════════════════════════════════
   DOCUMENT SHELL
   ═══════════════════════════════════════════════ */
.document {
    max-width: 740px;
    margin: 0 auto;
    background: var(--bg-card);
    box-shadow: 0 1px 3px rgba(0,0,0,0.10);
}

/* ═══════════════════════════════════════════════
   HEADER
   ═══════════════════════════════════════════════ */
.header {
    padding: 28px 36px 0;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
}

.header-left {
    flex: 1;
}

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

.header-company {
    font-size: 16px;
    font-weight: 800;
    color: var(--text-heading);
    letter-spacing: 0.3px;
    text-transform: uppercase;
    margin-bottom: 4px;
}

.header-company-detail {
    font-size: 10px;
    color: var(--text-muted);
    line-height: 1.5;
}

.header-right {
    text-align: right;
    min-width: 220px;
}

.doc-title {
    font-size: 26px;
    font-weight: 900;
    color: var(--primary);
    letter-spacing: 4px;
    text-transform: uppercase;
    margin-bottom: 8px;
    line-height: 1.1;
}

.doc-number {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-heading);
    margin-bottom: 2px;
}

.doc-date {
    font-size: 10px;
    color: var(--text-muted);
}

.header-divider {
    margin: 18px 36px 0;
    border: none;
    height: 3px;
    background: var(--primary);
}

/* ═══════════════════════════════════════════════
   CONTENT AREA
   ═══════════════════════════════════════════════ */
.content {
    padding: 20px 36px 28px;
}

/* ── Section Blocks ── */
.section {
    margin-bottom: 20px;
    page-break-inside: avoid;
}

.section:last-child {
    margin-bottom: 0;
}

.section-header {
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 1.5px;
    color: var(--primary);
    padding: 0 0 6px;
    margin-bottom: 10px;
    border-bottom: 2px solid var(--primary);
    display: flex;
    align-items: center;
    gap: 8px;
}

.section-icon {
    width: 18px;
    height: 18px;
    background: var(--primary);
    color: #fff;
    border-radius: 3px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    font-weight: 700;
    line-height: 1;
}

/* ═══════════════════════════════════════════════
   JOB DETAILS — Two-column layout
   ═══════════════════════════════════════════════ */
.job-details {
    display: flex;
    gap: 20px;
}

.detail-block {
    flex: 1;
    background: var(--bg-subtle);
    border: 1px solid var(--border-light);
    border-radius: 6px;
    padding: 14px 16px;
    page-break-inside: avoid;
}

.detail-block .label {
    font-size: 8.5px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-muted);
    margin-bottom: 5px;
}

.detail-block .value {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-heading);
    line-height: 1.5;
}

.detail-block .sub {
    font-size: 10px;
    color: var(--text-body);
    margin-top: 3px;
    line-height: 1.5;
}

/* ═══════════════════════════════════════════════
   TECHNICIAN ASSIGNMENT
   ═══════════════════════════════════════════════ */
.tech-row {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
}

.tech-item {
    flex: 1;
    min-width: 180px;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    background: var(--primary-faint);
    border: 1px solid var(--primary-light);
    border-radius: 6px;
}

.tech-badge {
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    background: var(--primary);
    color: #fff;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 700;
}

.tech-label {
    font-size: 8.5px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: var(--text-muted);
}

.tech-value {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-heading);
    margin-top: 1px;
}

/* ═══════════════════════════════════════════════
   TASK CHECKLIST
   ═══════════════════════════════════════════════ */
.checklist-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    border: 1px solid var(--border-light);
    border-radius: 6px;
    overflow: hidden;
}

.checklist-table thead th {
    background: var(--primary);
    color: #fff;
    padding: 9px 12px;
    font-size: 8.5px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    text-align: left;
}

.checklist-table thead th.center { text-align: center; }

.checklist-table tbody td {
    padding: 9px 12px;
    border-bottom: 1px solid var(--border-light);
    font-size: 11px;
    vertical-align: top;
}

.checklist-table tbody td.center { text-align: center; }
.checklist-table tbody tr:last-child td { border-bottom: none; }
.checklist-table tbody tr:nth-child(even) { background: var(--bg-subtle); }

.check-box {
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 2px solid var(--border);
    border-radius: 3px;
    vertical-align: middle;
    position: relative;
}

.check-box.checked {
    background: var(--success);
    border-color: var(--success);
}

.check-box.checked::after {
    content: "✓";
    position: absolute;
    top: -2px;
    left: 1px;
    font-size: 12px;
    color: #fff;
    font-weight: 700;
}

.status-badge {
    display: inline-block;
    font-size: 8px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: 2px 8px;
    border-radius: 3px;
}

.status-badge.done {
    background: var(--success-faint);
    color: var(--success);
}

.status-badge.pending {
    background: #fffbeb;
    color: #d97706;
}

.task-notes {
    font-size: 10px;
    color: var(--text-muted);
    font-style: italic;
}

/* ═══════════════════════════════════════════════
   MATERIALS TABLE
   ═══════════════════════════════════════════════ */
.materials-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    border: 1px solid var(--border-light);
    border-radius: 6px;
    overflow: hidden;
}

.materials-table thead th {
    background: var(--bg-subtle);
    padding: 9px 12px;
    font-size: 8.5px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: var(--text-muted);
    text-align: left;
    border-bottom: 1px solid var(--border-light);
}

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

.materials-table tbody td {
    padding: 9px 12px;
    border-bottom: 1px solid var(--border-light);
    font-size: 11px;
}

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

.materials-table tbody tr:last-child td { border-bottom: none; }
.materials-table tbody tr:nth-child(even) { background: var(--bg-subtle); }

/* ── Materials Totals ── */
.materials-totals {
    display: flex;
    justify-content: flex-end;
    margin-top: 8px;
    page-break-inside: avoid;
}

.materials-totals .totals-box {
    min-width: 220px;
    background: var(--bg-subtle);
    border: 1px solid var(--border-light);
    border-radius: 6px;
    padding: 10px 16px;
}

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

.totals-row .amount {
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

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

.totals-row.grand .amount {
    font-weight: 800;
}

/* ═══════════════════════════════════════════════
   TIME TRACKING
   ═══════════════════════════════════════════════ */
.time-tracker {
    display: flex;
    gap: 12px;
    page-break-inside: avoid;
}

.time-card {
    flex: 1;
    text-align: center;
    padding: 12px;
    background: var(--bg-subtle);
    border: 1px solid var(--border-light);
    border-radius: 6px;
}

.time-card .label {
    font-size: 8px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-muted);
    margin-bottom: 4px;
}

.time-card .value {
    font-size: 16px;
    font-weight: 700;
    color: var(--text-heading);
    font-variant-numeric: tabular-nums;
    line-height: 1.2;
}

.time-card.highlight {
    background: var(--primary);
    border-color: var(--primary);
}

.time-card.highlight .label {
    color: rgba(255,255,255,0.65);
}

.time-card.highlight .value {
    color: #fff;
}

/* ═══════════════════════════════════════════════
   SIGNATURE BLOCK
   ═══════════════════════════════════════════════ */
.signature-section {
    margin-top: 28px;
    page-break-inside: avoid;
}

.signature-block {
    border: 1px solid var(--border-light);
    border-radius: 6px;
    padding: 20px 24px;
    background: var(--bg-subtle);
}

.sig-grid {
    display: flex;
    gap: 40px;
}

.sig-col {
    flex: 1;
}

.sig-line {
    border-top: 1.5px solid var(--border);
    margin-top: 40px;
    margin-bottom: 6px;
    padding-top: 6px;
}

.sig-field {
    font-size: 9px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: var(--text-muted);
}

.sig-underlined {
    border-bottom: 1px solid var(--border);
    min-width: 180px;
    display: inline-block;
    padding: 2px 0;
    font-size: 10px;
    color: var(--text-muted);
    margin-top: 2px;
}

/* ═══════════════════════════════════════════════
   FOOTER
   ═══════════════════════════════════════════════ */
.footer {
    padding: 16px 36px;
    background: var(--bg-subtle);
    border-top: 2px solid var(--primary);
    text-align: center;
}

.footer-brand {
    font-size: 11px;
    font-weight: 700;
    color: var(--text-heading);
    margin-bottom: 3px;
}

.footer-detail {
    font-size: 9px;
    color: var(--text-muted);
    line-height: 1.5;
}

.footer-message {
    font-size: 10px;
    font-weight: 600;
    color: var(--primary);
    margin-top: 8px;
    letter-spacing: 0.5px;
}

/* ═══════════════════════════════════════════════
   PAGE BREAKS
   ═══════════════════════════════════════════════ */
.materials-totals, .signature-section, .time-tracker, .detail-block, .checklist-table tbody tr {
    page-break-inside: avoid;
}

/* ═══════════════════════════════════════════════
   WATERMARK
   ═══════════════════════════════════════════════ */
.watermark {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) rotate(-35deg);
    font-size: 64px;
    font-weight: 900;
    color: var(--primary);
    opacity: 0.06;
    pointer-events: none;
    text-transform: uppercase;
    letter-spacing: 8px;
}

{% if style.show_watermark | default(false) | to_bool %}
<div class="watermark">{{ style.watermark_text | default('WORK ORDER') }}</div>
{% endif %}
</style>
</head>
<body>
<div class="document">

    <!-- ════════════════════════════════════════
         HEADER
         ════════════════════════════════════════ -->
    <div class="header">
        <div class="header-left">
            {% if style.logo_url %}
            <img src="{{ style.logo_url }}" alt="Logo" class="header-logo">
            {% endif %}
            <div class="header-company">{{ data.from.name | default('Company Name') }}</div>
            {% if data.from.address %}
            <div class="header-company-detail">{{ data.from.address | nl2br }}</div>
            {% endif %}
            {% if data.from.phone %}
            <div class="header-company-detail">Tel: {{ data.from.phone }}</div>
            {% endif %}
            {% if data.from.email %}
            <div class="header-company-detail">{{ data.from.email }}</div>
            {% endif %}
        </div>
        <div class="header-right">
            <div class="doc-title">WORK ORDER</div>
            {% if data.document_number or data.work_order_number %}
            <div class="doc-number">#{{ data.work_order_number | default(data.document_number) }}</div>
            {% endif %}
            {% if data.issue_date %}
            <div class="doc-date">Issued: {{ data.issue_date | date_format }}</div>
            {% endif %}
        </div>
    </div>
    <hr class="header-divider">

    <div class="content">

        <!-- ════════════════════════════════════
             JOB DETAILS
             ════════════════════════════════════ -->
        <div class="section">
            <div class="section-header">
                <span class="section-icon">&#9670;</span>
                Job Details
            </div>
            <div class="job-details">
                <div class="detail-block">
                    <div class="label">Job Address</div>
                    {% if data.job_address %}
                    <div class="value">{{ data.job_address | nl2br }}</div>
                    {% else %}
                    <div class="value" style="color:var(--text-muted);">—</div>
                    {% endif %}
                </div>
                <div class="detail-block">
                    <div class="label">Customer Contact</div>
                    {% if data.to.name %}
                    <div class="value">{{ data.to.name }}</div>
                    {% endif %}
                    {% if data.to.phone %}
                    <div class="sub">Phone: {{ data.to.phone }}</div>
                    {% endif %}
                    {% if data.to.email %}
                    <div class="sub">{{ data.to.email }}</div>
                    {% endif %}
                </div>
                <div class="detail-block">
                    <div class="label">Scheduled</div>
                    {% if data.scheduled_date %}
                    <div class="value">{{ data.scheduled_date | date_format }}</div>
                    {% endif %}
                    {% if data.scheduled_time %}
                    <div class="sub">Time: {{ data.scheduled_time }}</div>
                    {% endif %}
                </div>
            </div>
        </div>

        <!-- ════════════════════════════════════
             TECHNICIAN ASSIGNMENT
             ════════════════════════════════════ -->
        <div class="section">
            <div class="section-header">
                <span class="section-icon">&#9986;</span>
                Technician Assignment
            </div>
            <div class="tech-row">
                {% if data.technician_name %}
                <div class="tech-item">
                    <div class="tech-badge">&#9986;</div>
                    <div>
                        <div class="tech-label">Technician</div>
                        <div class="tech-value">{{ data.technician_name }}</div>
                    </div>
                </div>
                {% endif %}
                {% if data.vehicle %}
                <div class="tech-item">
                    <div class="tech-badge">&#128663;</div>
                    <div>
                        <div class="tech-label">Vehicle / Equipment</div>
                        <div class="tech-value">{{ data.vehicle }}</div>
                    </div>
                </div>
                {% endif %}
            </div>
        </div>

        <!-- ════════════════════════════════════
             TASK CHECKLIST
             ════════════════════════════════════ -->
        {% if data.tasks %}
        <div class="section">
            <div class="section-header">
                <span class="section-icon">&#10003;</span>
                Task Checklist
            </div>
            <table class="checklist-table">
                <thead>
                    <tr>
                        <th style="width:3%;">#</th>
                        <th style="width:16%;">Status</th>
                        <th style="width:41%;">Description</th>
                        <th style="width:40%;">Notes</th>
                    </tr>
                </thead>
                <tbody>
                    {% for task in data.tasks %}
                    <tr>
                        <td class="center">{{ loop.index }}</td>
                        <td class="center">
                            {% if task.completed | default(false) | to_bool %}
                                <span class="check-box checked"></span>
                                <span class="status-badge done">Done</span>
                            {% else %}
                                <span class="check-box"></span>
                                <span class="status-badge pending">Pending</span>
                            {% endif %}
                        </td>
                        <td>{{ task.description | default('') }}</td>
                        <td class="task-notes">{{ task.notes | default('') }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        {% endif %}

        <!-- ════════════════════════════════════
             MATERIALS USED
             ════════════════════════════════════ -->
        {% if data.line_items %}
        <div class="section">
            <div class="section-header">
                <span class="section-icon">&#9935;</span>
                Materials Used
            </div>
            <table class="materials-table">
                <thead>
                    <tr>
                        <th>Description</th>
                        <th class="right" style="width:60px;">Qty</th>
                        <th class="right" style="width:90px;">Unit Cost</th>
                        <th class="right" style="width:90px;">Total</th>
                    </tr>
                </thead>
                <tbody>
                    {% for item in data.line_items %}
                    <tr>
                        <td>{{ item.description | default('') }}</td>
                        <td class="right">{{ item.quantity | default(1) }}</td>
                        <td class="right">{{ item.unit_cost | default(item.unit_price | default(0)) | currency }}</td>
                        <td class="right">{{ item.amount | default((item.quantity | default(1)) * (item.unit_cost | default(item.unit_price | default(0)))) | currency }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
            {% if data.subtotal or data.total %}
            <div class="materials-totals">
                <div class="totals-box">
                    {% if data.subtotal and data.subtotal > 0 %}
                    <div class="totals-row">
                        <span>Subtotal</span>
                        <span class="amount">{{ data.subtotal | currency }}</span>
                    </div>
                    {% endif %}
                    {% if data.tax and data.tax > 0 %}
                    <div class="totals-row">
                        <span>Tax</span>
                        <span class="amount">{{ data.tax | currency }}</span>
                    </div>
                    {% endif %}
                    <div class="totals-row grand">
                        <span>Total Materials</span>
                        <span class="amount">{{ data.total | currency }}</span>
                    </div>
                </div>
            </div>
            {% endif %}
        </div>
        {% endif %}

        <!-- ════════════════════════════════════
             TIME TRACKING
             ════════════════════════════════════ -->
        <div class="section">
            <div class="section-header">
                <span class="section-icon">&#9200;</span>
                Time Tracking
            </div>
            <div class="time-tracker">
                <div class="time-card">
                    <div class="label">Time In</div>
                    {% if data.time_in %}
                    <div class="value">{{ data.time_in }}</div>
                    {% else %}
                    <div class="value" style="color:var(--text-muted);">--:--</div>
                    {% endif %}
                </div>
                <div class="time-card">
                    <div class="label">Time Out</div>
                    {% if data.time_out %}
                    <div class="value">{{ data.time_out }}</div>
                    {% else %}
                    <div class="value" style="color:var(--text-muted);">--:--</div>
                    {% endif %}
                </div>
                <div class="time-card highlight">
                    <div class="label">Total Hours</div>
                    {% if data.total_hours %}
                    <div class="value">{{ data.total_hours }}{{ ' hrs' }}</div>
                    {% else %}
                    <div class="value">0.0 hrs</div>
                    {% endif %}
                </div>
            </div>
        </div>

        <!-- ════════════════════════════════════
             ADDITIONAL NOTES
             ════════════════════════════════════ -->
        {% if data.notes %}
        <div class="section">
            <div class="section-header">
                <span class="section-icon">&#128221;</span>
                Additional Notes
            </div>
            <div style="font-size:11px; color:var(--text-body); line-height:1.65; padding:10px 14px; background:var(--bg-subtle); border-radius:6px; border:1px solid var(--border-light);">
                {{ data.notes | nl2br }}
            </div>
        </div>
        {% endif %}

        <!-- ════════════════════════════════════
             CUSTOMER SIGNATURE
             ════════════════════════════════════ -->
        <div class="signature-section">
            <div class="section-header">
                <span class="section-icon">&#128065;</span>
                Customer Sign-Off
            </div>
            <div class="signature-block">
                <div class="sig-grid">
                    <div class="sig-col">
                        <div class="sig-line"></div>
                        <div class="sig-field">Customer Signature</div>
                    </div>
                    <div class="sig-col">
                        <div class="sig-line"></div>
                        <div class="sig-field">Printed Name</div>
                        {% if data.to.name %}
                        <div class="sig-underlined">{{ data.to.name }}</div>
                        {% else %}
                        <div class="sig-underlined">&nbsp;</div>
                        {% endif %}
                        <br>
                        <div class="sig-field" style="margin-top:6px;">Date</div>
                        <div class="sig-underlined">&nbsp;</div>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <!-- ════════════════════════════════════════
         FOOTER
         ════════════════════════════════════════ -->
    <div class="footer">
        <div class="footer-brand">{{ data.from.name | default('Company Name') }}</div>
        {% if data.from.phone or data.from.email %}
        <div class="footer-detail">
            {% if data.from.phone %}{{ data.from.phone }}{% if data.from.email %} &middot; {% endif %}{% endif %}
            {% if data.from.email %}{{ data.from.email }}{% endif %}
        </div>
        {% endif %}
        <div class="footer-message">Thank you for your business</div>
    </div>

</div>
</body>
</html>
