<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Lease Contract - <?= htmlspecialchars($lease['lease_number']) ?></title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: 'Helvetica Neue', Arial, sans-serif; font-size: 13px; color: #1e293b; background: #fff; line-height: 1.6; }
.container { max-width: 760px; margin: 0 auto; padding: 40px; }
.header { text-align: center; padding-bottom: 24px; border-bottom: 3px double #1a3c5e; margin-bottom: 28px; }
.header h1 { font-size: 26px; font-weight: 800; color: #1a3c5e; letter-spacing: 1px; }
.header p { color: #64748b; font-size: 12px; }
.section { margin-bottom: 24px; }
.section h3 { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: #1a3c5e; background: #f0f6ff; padding: 6px 12px; border-left: 4px solid #1a3c5e; margin-bottom: 12px; }
.row { display: flex; gap: 16px; margin-bottom: 8px; }
.col { flex: 1; }
.label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: #94a3b8; margin-bottom: 2px; }
.value { font-weight: 600; font-size: 13px; }
table { width: 100%; border-collapse: collapse; margin-bottom: 12px; }
table th { background: #1a3c5e; color: #fff; padding: 8px 12px; text-align: left; font-size: 11px; }
table td { padding: 8px 12px; border-bottom: 1px solid #e2e8f0; font-size: 12px; }
.terms { background: #f8fafc; border: 1px solid #e2e8f0; border-radius: 8px; padding: 16px; font-size: 12px; line-height: 1.7; }
.signatures { display: flex; justify-content: space-between; margin-top: 48px; gap: 40px; }
.sig-box { flex: 1; text-align: center; border-top: 2px solid #1e293b; padding-top: 8px; font-size: 12px; }
.watermark { position: fixed; top: 50%; left: 50%; transform: translate(-50%,-50%) rotate(-45deg); font-size: 80px; font-weight: 900; color: rgba(26,60,94,0.04); white-space: nowrap; pointer-events: none; z-index: -1; }
@media print { .no-print { display:none; } @page { size: A4; margin: 20mm; } }
</style>
</head>
<body>
<div class="watermark">ZABIBU RESIDENCE</div>
<div class="container">
  <div class="header">
    <h1>TENANCY AGREEMENT</h1>
    <p><?= htmlspecialchars($settings['company_name'] ?? APP_NAME) ?> | Ref: <?= htmlspecialchars($lease['lease_number']) ?></p>
    <p>Date: <?= date('d F Y') ?></p>
  </div>

  <div class="section">
    <h3>Parties to the Agreement</h3>
    <div class="row">
      <div class="col"><div class="label">Landlord / Lessor</div><div class="value"><?= htmlspecialchars($settings['company_name'] ?? APP_NAME) ?></div><div style="font-size:12px;color:#64748b"><?= htmlspecialchars($settings['company_address'] ?? '') ?></div></div>
      <div class="col"><div class="label">Tenant / Lessee</div><div class="value"><?= htmlspecialchars($lease['tenant_name']) ?></div><div style="font-size:12px;color:#64748b"><?= htmlspecialchars($lease['phone']) ?> | National ID: <?= htmlspecialchars($lease['national_id']??'—') ?></div></div>
    </div>
  </div>

  <div class="section">
    <h3>Property Details</h3>
    <div class="row">
      <div class="col"><div class="label">Property</div><div class="value"><?= htmlspecialchars($lease['property_name']) ?></div></div>
      <div class="col"><div class="label">Unit</div><div class="value"><?= htmlspecialchars($lease['unit_number']) ?> (<?= htmlspecialchars($lease['unit_type']) ?>)</div></div>
      <div class="col"><div class="label">Floor Area</div><div class="value"><?= $lease['floor_size'] ? htmlspecialchars($lease['floor_size']).' sqm' : '—' ?></div></div>
    </div>
    <div class="row">
      <div class="col"><div class="label">Bedrooms</div><div class="value"><?= $lease['bedrooms'] ?></div></div>
      <div class="col"><div class="label">Bathrooms</div><div class="value"><?= $lease['bathrooms'] ?></div></div>
      <div class="col"><div class="label">Address</div><div class="value"><?= htmlspecialchars($lease['property_address']) ?></div></div>
    </div>
  </div>

  <div class="section">
    <h3>Lease Period & Financial Terms</h3>
    <table>
      <tr><th>Term</th><th>Details</th><th>Term</th><th>Details</th></tr>
      <tr><td>Start Date</td><td><?= htmlspecialchars($lease['start_date']) ?></td><td>End Date</td><td><?= htmlspecialchars($lease['end_date']) ?></td></tr>
      <tr><td>Monthly Rent</td><td>TZS <?= number_format($lease['monthly_rent']) ?></td><td>Security Deposit</td><td>TZS <?= number_format($lease['security_deposit']??0) ?></td></tr>
      <tr><td>Payment Frequency</td><td><?= ucfirst(str_replace('_',' ',$lease['payment_frequency'])) ?></td><td>Due Day</td><td><?= $lease['payment_due_day'] ?>th of month</td></tr>
      <tr><td>Grace Period</td><td><?= $lease['grace_period_days'] ?> days</td><td>Late Fee</td><td><?= $lease['late_fee_percent'] ?>% of monthly rent</td></tr>
      <tr><td>VAT Applicable</td><td><?= $lease['vat_applicable'] ? 'Yes (18%)' : 'No' ?></td><td>Utilities Included</td><td><?= $lease['utilities_included'] ? 'Yes' : 'No (Tenant pays)' ?></td></tr>
    </table>
  </div>

  <div class="section">
    <h3>Terms & Conditions</h3>
    <div class="terms">
      <?php if ($lease['terms_conditions']): ?>
      <?= nl2br(htmlspecialchars($lease['terms_conditions'])) ?>
      <?php else: ?>
      <ol style="padding-left:20px;margin:0">
        <li style="margin-bottom:8px">The Tenant shall pay rent on or before the <?= $lease['payment_due_day'] ?>th day of each month.</li>
        <li style="margin-bottom:8px">The security deposit of TZS <?= number_format($lease['security_deposit']??0) ?> shall be refunded within 30 days of vacant possession, subject to deductions for damage beyond normal wear and tear.</li>
        <li style="margin-bottom:8px">The Tenant shall maintain the premises in good condition and notify the Landlord promptly of any damages or required repairs.</li>
        <li style="margin-bottom:8px">Subletting or assignment of this lease is strictly prohibited without prior written consent from the Landlord.</li>
        <li style="margin-bottom:8px">Any modifications to the premises require written approval from the Landlord.</li>
        <li style="margin-bottom:8px">Either party may terminate this agreement with 30 days written notice.</li>
        <li style="margin-bottom:8px">This agreement is governed by the laws of the United Republic of Tanzania.</li>
      </ol>
      <?php endif; ?>
    </div>
  </div>

  <div class="signatures">
    <div class="sig-box">
      <strong><?= htmlspecialchars($settings['company_name'] ?? APP_NAME) ?></strong><br>
      <span style="color:#64748b;font-size:11px">Landlord / Authorized Representative</span>
    </div>
    <div class="sig-box">
      <strong><?= htmlspecialchars($lease['tenant_name']) ?></strong><br>
      <span style="color:#64748b;font-size:11px">Tenant</span>
    </div>
    <div class="sig-box">
      <strong>Witness</strong><br>
      <span style="color:#64748b;font-size:11px">Name & Signature</span>
    </div>
  </div>

  <div style="margin-top:32px;border-top:1px solid #e2e8f0;padding-top:12px;font-size:10px;color:#94a3b8;text-align:center">
    This lease agreement was generated by <?= htmlspecialchars($settings['company_name'] ?? APP_NAME) ?> Management System on <?= date('d F Y H:i') ?>.
  </div>

  <div class="no-print" style="text-align:center;margin-top:24px">
    <button onclick="window.print()" style="padding:10px 32px;background:#1a3c5e;color:#fff;border:none;border-radius:8px;font-size:14px;cursor:pointer;margin-right:8px">Print / Save PDF</button>
    <button onclick="window.close()" style="padding:10px 24px;background:#64748b;color:#fff;border:none;border-radius:8px;font-size:14px;cursor:pointer">Close</button>
  </div>
</div>
</body>
</html>
