<?php
/*
 * Letterhead top partial — outputs <html> through the header band.
 *
 * Set before requiring:
 *   string $docType      e.g. 'INVOICE', 'QUOTATION', 'DELIVERY NOTE'
 *   string $docNumber    document reference number
 *   array  $co           Branding::documentDetailsForBranch() array
 *   string $docNewLink   (optional) "New …" button href
 *   string $docNewLabel  (optional) "New …" button label
 */
$_lhTitle = htmlspecialchars(Branding::pageTitle($docType . ' #' . $docNumber));
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $_lhTitle; ?></title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
/* ══════════════════════════════════════════════════
   LETTERHEAD — BR Computers (simple plain)
   Navy #1a2878 · Sky #4db8de · Purple #8b30c8
   Toolbar: #212529 (dashboard bg-dark)
══════════════════════════════════════════════════ */
:root {
    --lh-navy:        #1a2878;
    --lh-navy-deep:   #111d5e;
    --lh-navy-mid:    #1e3290;
    --lh-blue:        #4db8de;
    --lh-blue-light:  #a8dcf0;
    --lh-blue-faint:  #e8f6fb;
    --lh-purple:      #8b30c8;
    --lh-text:        #0f1a3d;
    --lh-muted:       #5a6a8a;
    --lh-border:      #d4dff0;
    --lh-surface:     #f5f7fc;
}
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
    font-family: 'Inter', 'Segoe UI', Helvetica, Arial, sans-serif;
    font-size: 13px;
    color: var(--lh-text);
    background: #f0f2f5;
    line-height: 1.55;
}

/* ══════════════════════════════════════════════
   TOOLBAR — #212529 (matches dashboard)
══════════════════════════════════════════════ */
.lh-toolbar {
    display: flex; align-items: center; gap: 8px;
    padding: 10px 24px;
    background: #212529;
    position: sticky; top: 0; z-index: 200;
    border-bottom: 3px solid var(--lh-blue);
    box-shadow: 0 2px 10px rgba(0,0,0,.4);
}
.lh-toolbar .lt-ref { flex: 1; color: #8d97a5; font-size: 12px; }
.lh-toolbar .lt-ref strong { color: #f8f9fa; font-size: 13px; margin-right: 6px; font-weight: 700; }
.lh-btn {
    display: inline-flex; align-items: center; gap: 6px;
    padding: 7px 15px; border: none; border-radius: 4px;
    font-size: 11.5px; font-weight: 600; cursor: pointer;
    text-decoration: none; letter-spacing: .2px;
    transition: background .15s, color .15s, border-color .15s;
    font-family: inherit;
}
.lh-btn-print { background: var(--lh-blue); color: #0d1a2e; }
.lh-btn-print:hover { background: var(--lh-blue-light); }
.lh-btn-ghost {
    background: rgba(255,255,255,.07); color: #adb5bd;
    border: 1px solid rgba(255,255,255,.14);
}
.lh-btn-ghost:hover { background: rgba(255,255,255,.14); color: #f8f9fa; border-color: rgba(255,255,255,.28); }

/* ══════════════════════════════════════════════
   PAGE WRAPPER & CARD
══════════════════════════════════════════════ */
.lh-wrap { max-width: 880px; margin: 28px auto 56px; padding: 0 14px; }
.lh-doc {
    background: #fff;
    box-shadow: 0 6px 40px rgba(10,20,80,.13), 0 1px 5px rgba(10,20,80,.05);
    overflow: hidden;
}

/* ══════════════════════════════════════════════
   LETTERHEAD HEADER — simple plain
══════════════════════════════════════════════ */
.lh-head {
    background: #fff;
    display: flex;
    align-items: center;
    padding: 26px 40px 26px 40px;
    min-height: 140px;
}

/* Logo — left */
.lh-logo-zone {
    flex-shrink: 0;
    margin-right: 32px;
}
.lh-logo-zone img {
    display: block;
    max-height: 110px;
    max-width: 280px;
    object-fit: contain;
}

/* Thin vertical divider */
.lh-vdiv {
    width: 1px;
    align-self: stretch;
    background: #d0daf0;
    margin: 6px 32px 6px 0;
    flex-shrink: 0;
}

/* Company info — plain text, right-aligned */
.lh-co {
    flex: 1;
    text-align: right;
    min-width: 0;
}
.lh-co-name {
    font-size: 19px;
    font-weight: 700;
    color: var(--lh-navy);
    margin-bottom: 10px;
    line-height: 1.1;
}
.lh-co-contact { display: flex; flex-direction: column; align-items: flex-end; gap: 5px; }
.lh-co-contact-item {
    display: inline-flex; align-items: center; gap: 7px;
    font-size: 12px; color: var(--lh-muted);
}
.lh-co-contact-item i { color: var(--lh-blue); font-size: 10px; width: 12px; text-align: center; }

/* ══════════════════════════════════════════════
   DOCUMENT TITLE BLOCK — type / number / date
══════════════════════════════════════════════ */
.lh-doc-title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 36px 20px 40px;
    border-bottom: 2px solid var(--lh-border);
    background: #fff;
    position: relative;
}
/* Sky-blue left accent strip */
.lh-doc-title::before {
    content: '';
    position: absolute;
    top: 0; left: 0; bottom: 0;
    width: 4px;
    background: var(--lh-blue);
}
.lh-doc-type {
    font-size: 32px;
    font-weight: 800;
    color: var(--lh-navy);
    letter-spacing: 3px;
    text-transform: uppercase;
    line-height: 1;
    padding-left: 8px;
}
.lh-doc-refs {
    text-align: right;
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.lh-doc-ref-row {
    display: flex;
    align-items: baseline;
    gap: 12px;
    justify-content: flex-end;
}
.lh-doc-ref-lbl {
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--lh-muted);
    min-width: 78px;
}
.lh-doc-ref-val {
    font-size: 14px;
    font-weight: 700;
    color: var(--lh-navy);
    min-width: 130px;
    text-align: right;
}

/* ══════════════════════════════════════════════
   PARTIES — Bill To / Quote For / Deliver To
══════════════════════════════════════════════ */
.lh-parties {
    display: grid; grid-template-columns: 1fr;
    border-bottom: 1px solid var(--lh-border);
}
.lh-party { padding: 18px 36px 18px 40px; }
.lh-party.lh-pto { background: var(--lh-surface); position: relative; }
.lh-party.lh-pto::before {
    content: '';
    position: absolute; top: 14px; bottom: 14px; left: 26px;
    width: 3px; background: var(--lh-purple); border-radius: 2px; opacity: .65;
}
.lh-plabel {
    font-size: 8.5px; font-weight: 700; letter-spacing: 2px; text-transform: uppercase;
    color: var(--lh-muted); margin-bottom: 8px;
}
.lh-pname { font-size: 15px; font-weight: 700; color: var(--lh-text); margin-bottom: 5px; }
.lh-pline {
    display: flex; align-items: flex-start; gap: 8px;
    font-size: 12px; color: var(--lh-muted); margin-bottom: 3px; line-height: 1.4;
}
.lh-pline i { margin-top: 2px; flex-shrink: 0; font-size: 10px; color: var(--lh-blue); }

/* ══════════════════════════════════════════════
   ITEMS TABLE
══════════════════════════════════════════════ */
.lh-table-wrap { padding: 20px 36px 0 40px; }
.lh-table { width: 100%; border-collapse: collapse; }
.lh-table thead tr { background: var(--lh-navy); }
.lh-table thead th {
    padding: 10px 10px; font-size: 9px; font-weight: 700;
    letter-spacing: 1.2px; text-transform: uppercase; color: var(--lh-blue-light); text-align: left;
}
.lh-table thead th:first-child { padding-left: 12px; }
.lh-table thead th.r { text-align: right; }
.lh-table thead th.c { text-align: center; }
.lh-table tbody tr { border-bottom: 1px solid var(--lh-border); }
.lh-table tbody tr:last-child { border-bottom: 2px solid var(--lh-border); }
.lh-table tbody tr:nth-child(even) td { background: var(--lh-surface); }
.lh-table tbody td { padding: 10px 10px; font-size: 13px; color: var(--lh-text); vertical-align: middle; }
.lh-table tbody td:first-child { padding-left: 12px; }
.lh-table tbody td.r   { text-align: right; }
.lh-table tbody td.c   { text-align: center; }
.lh-table tbody td.desc { font-weight: 500; }
.lh-table tbody td.amt  { font-weight: 700; color: var(--lh-navy); }
.lh-rn {
    display: inline-flex; align-items: center; justify-content: center;
    width: 20px; height: 20px; background: var(--lh-blue-faint);
    color: #1a5c80; border-radius: 50%; font-size: 10px; font-weight: 700;
}

/* ══════════════════════════════════════════════
   TOTALS
══════════════════════════════════════════════ */
.lh-totals-wrap { padding: 8px 36px 24px 40px; display: flex; justify-content: flex-end; }
.lh-totals { width: 290px; border: 1px solid var(--lh-border); border-top: none; }
.lh-tr {
    display: flex; justify-content: space-between; align-items: center;
    padding: 8px 14px; font-size: 13px; color: #3a4a6a;
    border-bottom: 1px solid var(--lh-border);
}
.lh-tr:last-child { border-bottom: none; }
.lh-tr.grand {
    background: var(--lh-purple); color: #fff; font-weight: 700; font-size: 15px; padding: 12px 14px;
}
.lh-tr.grand .g-sub {
    display: block; font-size: 8.5px; font-weight: 400;
    color: rgba(255,255,255,.65); text-transform: uppercase; letter-spacing: .7px; margin-bottom: 2px;
}

/* ══════════════════════════════════════════════
   NOTES + VALIDITY
══════════════════════════════════════════════ */
.lh-bottom {
    display: grid; grid-template-columns: 1fr 200px;
    border-top: 1px solid var(--lh-border); background: var(--lh-surface);
}
.lh-notes { padding: 18px 28px 18px 40px; border-right: 1px solid var(--lh-border); }
.lh-notes-lbl { font-size: 8.5px; font-weight: 700; letter-spacing: 1.5px; text-transform: uppercase; color: var(--lh-blue); margin-bottom: 7px; }
.lh-notes-txt { font-size: 12px; color: #4a5a7a; line-height: 1.65; }
.lh-validity { padding: 18px 22px; display: flex; flex-direction: column; justify-content: center; }
.lh-validity-lbl  { font-size: 8.5px; font-weight: 700; letter-spacing: 1.2px; text-transform: uppercase; color: var(--lh-muted); margin-bottom: 5px; }
.lh-validity-date { font-size: 15px; font-weight: 700; color: var(--lh-navy); }
.lh-validity-note { font-size: 11px; color: #8090b0; margin-top: 4px; font-style: italic; }

/* ══════════════════════════════════════════════
   SIGNATURES
══════════════════════════════════════════════ */
.lh-sig { display: flex; gap: 48px; padding: 16px 36px 24px 40px; }
.lh-sig-slot { flex: 1; max-width: 220px; }
.lh-sig-line { height: 38px; border-bottom: 1px solid #c4cfe0; margin-bottom: 7px; }
.lh-sig-lbl  { font-size: 8.5px; color: #8090b0; text-transform: uppercase; letter-spacing: 1px; }

/* ══════════════════════════════════════════════
   FOOTER BAND
══════════════════════════════════════════════ */
.lh-rule-sm {
    height: 3px;
    background: linear-gradient(90deg, var(--lh-navy) 0%, var(--lh-blue) 30%, var(--lh-blue-light) 50%, var(--lh-blue) 70%, var(--lh-navy) 100%);
}
.lh-foot {
    background: var(--lh-navy);
    padding: 13px 40px;
    text-align: center;
    position: relative;
}
.lh-foot::after {
    content: '';
    position: absolute; bottom: 0; left: 0; right: 0; height: 3px;
    background: linear-gradient(90deg, var(--lh-navy) 0%, var(--lh-blue) 30%, var(--lh-blue-light) 50%, var(--lh-blue) 70%, var(--lh-navy) 100%);
}
.lh-foot-thanks { font-size: 12px; color: var(--lh-blue); font-weight: 600; letter-spacing: .3px; }

/* ══════════════════════════════════════════════
   PRINT
══════════════════════════════════════════════ */
@media print {
    @page { size: A4; margin: 0.5cm 0.4cm; }
    * { -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; }
    body { background: #fff !important; }
    .lh-toolbar { display: none !important; }
    .lh-wrap { margin: 0; padding: 0; max-width: 100%; }
    .lh-doc { box-shadow: none !important; }
}

/* ── Mobile ─────────────────────────────────── */
@media (max-width: 640px) {
    .lh-head { flex-wrap: wrap; padding: 20px; gap: 16px; }
    .lh-vdiv { display: none; }
    .lh-co { text-align: left; }
    .lh-co-contact { align-items: flex-start; }
.lh-bottom { grid-template-columns: 1fr; }
    .lh-validity { border-top: 1px solid var(--lh-border); }
    .lh-table-wrap, .lh-totals-wrap, .lh-notes, .lh-sig, .lh-foot { padding-left: 20px; padding-right: 20px; }
    .lh-party { padding-left: 20px; }
}
    </style>
</head>
<body>

<!-- ══ SCREEN TOOLBAR ══ -->
<div class="lh-toolbar">
    <div class="lt-ref">
        <strong><?php echo htmlspecialchars($docType); ?>&nbsp;#<?php echo htmlspecialchars($docNumber); ?></strong>
        &mdash; <?php echo htmlspecialchars($co['name']); ?>
    </div>
    <a href="../dashboard/admin.php" class="lh-btn lh-btn-ghost">
        <i class="fas fa-tachometer-alt"></i>&nbsp;Dashboard
    </a>
    <a href="#" onclick="history.back();return false;" class="lh-btn lh-btn-ghost">
        <i class="fas fa-arrow-left"></i>&nbsp;Back
    </a>
    <button class="lh-btn lh-btn-print" onclick="window.print()">
        <i class="fas fa-print"></i>&nbsp;Print / Save PDF
    </button>
    <?php if (!empty($docNewLink)): ?>
        <a href="<?php echo htmlspecialchars($docNewLink); ?>" class="lh-btn lh-btn-ghost">
            <i class="fas fa-plus"></i>&nbsp;<?php echo htmlspecialchars($docNewLabel ?? 'New Document'); ?>
        </a>
    <?php endif; ?>
</div>

<div class="lh-wrap">
<div class="lh-doc">

    <!-- ══ LETTERHEAD HEADER ══ -->
    <div class="lh-head">

        <!-- Logo -->
        <div class="lh-logo-zone">
            <?php if (!empty($co['logo'])): ?>
                <img src="<?php echo htmlspecialchars($co['logo']); ?>"
                     alt="<?php echo htmlspecialchars($co['name']); ?>">
            <?php endif; ?>
        </div>

        <!-- Vertical divider -->
        <div class="lh-vdiv"></div>

        <!-- Company name + contacts (plain text) -->
        <div class="lh-co">
            <div class="lh-co-name"><?php echo htmlspecialchars($co['name']); ?></div>
            <div class="lh-co-contact">
                <?php if (!empty($co['address'])): ?>
                <span class="lh-co-contact-item">
                    <i class="fas fa-map-marker-alt"></i>
                    <?php echo htmlspecialchars($co['address']); ?>
                </span>
                <?php endif; ?>
                <?php if (!empty($co['phone'])): ?>
                <span class="lh-co-contact-item">
                    <i class="fas fa-phone-alt"></i>
                    <?php echo htmlspecialchars($co['phone']); ?>
                </span>
                <?php endif; ?>
                <?php if (!empty($co['email'])): ?>
                <span class="lh-co-contact-item">
                    <i class="fas fa-envelope"></i>
                    <?php echo htmlspecialchars($co['email']); ?>
                </span>
                <?php endif; ?>
                <?php if (!empty($co['website'])): ?>
                <span class="lh-co-contact-item">
                    <i class="fas fa-globe"></i>
                    <?php echo htmlspecialchars($co['website']); ?>
                </span>
                <?php endif; ?>
            </div>
        </div>

    </div><!-- /.lh-head -->
