<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-building-user me-2 text-primary"></i>Client Portal · <?= e($tenant['name']) ?></h5>
    <div class="d-flex gap-2">
        <a class="btn btn-sm btn-outline-primary" href="<?= url('/ClientPortal/incidents') ?>"><i class="fas fa-triangle-exclamation me-1"></i>All Incidents</a>
        <a class="btn btn-sm btn-outline-primary" href="<?= url('/ClientPortal/workOrders') ?>"><i class="fas fa-clipboard-check me-1"></i>All Work Orders</a>
    </div>
</div>

<div class="kpi-grid">
    <div class="rs-card rs-stat-card"><div class="stat-icon blue"><i class="fas fa-cogs"></i></div><div><div class="stat-value"><?= (int) $assets['total'] ?></div><div class="stat-label">Assets</div><div class="small text-muted"><?= (int) $assets['active'] ?> active</div></div></div>
    <div class="rs-card rs-stat-card"><div class="stat-icon red"><i class="fas fa-triangle-exclamation"></i></div><div><div class="stat-value"><?= (int) $incidents['open_count'] ?></div><div class="stat-label">Open Incidents</div><div class="small text-muted"><?= (int) $incidents['critical'] ?> critical</div></div></div>
    <div class="rs-card rs-stat-card"><div class="stat-icon amber"><i class="fas fa-clipboard-check"></i></div><div><div class="stat-value"><?= (int) $workOrders['open_count'] ?></div><div class="stat-label">Open Work Orders</div><div class="small text-muted"><?= (int) $workOrders['overdue'] ?> overdue</div></div></div>
    <div class="rs-card rs-stat-card"><div class="stat-icon green"><i class="fas fa-check-circle"></i></div><div><div class="stat-value"><?= (int) $workOrders['completed'] ?></div><div class="stat-label">Completed</div><div class="small text-muted">this period</div></div></div>
</div>

<div class="row g-3">
    <div class="col-lg-6"><div class="rs-card p-3">
        <h6 class="fw-bold mb-3"><i class="fas fa-triangle-exclamation me-1 text-danger"></i>Open Incidents</h6>
        <?php if (empty($openIncidents)): ?><p class="text-muted small mb-0">Nothing open.</p><?php else: ?>
            <table class="table table-sm table-hover mb-0">
                <thead><tr><th>Title</th><th>Severity</th><th>Started</th></tr></thead>
                <tbody><?php foreach ($openIncidents as $i): ?><tr>
                    <td class="small"><?= e($i['title']) ?></td>
                    <td><span class="badge bg-secondary text-uppercase"><?= e($i['severity']) ?></span></td>
                    <td class="small text-muted"><?= e($i['start_at'] ?? '—') ?></td>
                </tr><?php endforeach; ?></tbody>
            </table>
        <?php endif; ?>
    </div></div>
    <div class="col-lg-6"><div class="rs-card p-3">
        <h6 class="fw-bold mb-3"><i class="fas fa-clipboard-check me-1 text-primary"></i>Work Orders In Progress</h6>
        <?php if (empty($inProgressWos)): ?><p class="text-muted small mb-0">Nothing in progress.</p><?php else: ?>
            <table class="table table-sm table-hover mb-0">
                <thead><tr><th>Title</th><th>Priority</th><th>Due</th></tr></thead>
                <tbody><?php foreach ($inProgressWos as $w): ?><tr>
                    <td class="small"><?= e($w['title']) ?></td>
                    <td><span class="badge bg-secondary text-uppercase"><?= e($w['priority']) ?></span></td>
                    <td class="small text-muted"><?= e($w['due_at'] ?? '—') ?></td>
                </tr><?php endforeach; ?></tbody>
            </table>
        <?php endif; ?>
    </div></div>
</div>

<p class="mt-3 small text-muted"><i class="fas fa-lock me-1"></i>You have read-only access to your organisation's operations data.</p>
