<section class="bg-gray-50 py-16">
    <div class="max-w-7xl mx-auto px-4">
        <div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4">
            <div>
                <h1 class="text-4xl font-bold text-premium-charcoal">Welcome, <?= htmlspecialchars($user['customer_name'] ?? $user['email']) ?></h1>
                <p class="mt-2 text-architectural-gray text-lg">Review your pending quotations, invoices, and active projects.</p>
            </div>
            <a href="<?= htmlspecialchars($baseUrl) ?>/portal/logout" class="rounded-lg border-2 border-soft-beige px-6 py-3 text-sm text-premium-charcoal hover:bg-soft-beige transition">Logout</a>
        </div>
        <div class="grid gap-6 mt-12 md:grid-cols-3">
            <div class="rounded-3xl bg-elegant-white p-8 shadow-sm border border-slate-200 hover:shadow-md transition">
                <h2 class="text-xs uppercase tracking-widest font-bold text-balorp-magenta">Quotations</h2>
                <p class="mt-4 text-5xl font-bold text-premium-charcoal"><?= count($quotations) ?></p>
                <p class="mt-2 text-architectural-gray">Refine scope, review pricing and approve online.</p>
            </div>
            <div class="rounded-3xl bg-elegant-white p-8 shadow-sm border border-slate-200 hover:shadow-md transition">
                <h2 class="text-xs uppercase tracking-widest font-bold text-warm-gold-peach">Invoices</h2>
                <p class="mt-4 text-5xl font-bold text-premium-charcoal"><?= count($invoices) ?></p>
                <p class="mt-2 text-architectural-gray">Track due amounts and payment status.</p>
            </div>
            <div class="rounded-3xl bg-elegant-white p-8 shadow-sm border border-slate-200 hover:shadow-md transition">
                <h2 class="text-xs uppercase tracking-widest font-bold text-rich-crimson">Projects</h2>
                <p class="mt-4 text-5xl font-bold text-premium-charcoal"><?= count($projects) ?></p>
                <p class="mt-2 text-architectural-gray">Monitor project progress and milestones.</p>
            </div>
        </div>

        <div class="mt-12 grid gap-6 xl:grid-cols-3">
            <div class="rounded-3xl bg-elegant-white p-8 shadow-sm border border-slate-200">
                <h2 class="text-2xl font-bold text-premium-charcoal">Latest Quotations</h2>
                <div class="mt-4 space-y-4">
                    <?php if (empty($quotations)): ?>
                        <p class="text-slate-500">No quotations available yet.</p>
                    <?php endif; ?>
                    <?php foreach ($quotations as $quote): ?>
                        <div class="rounded-2xl bg-slate-50 p-4">
                            <p class="text-sm font-semibold text-slate-950"><?= htmlspecialchars($quote['quotation_number']) ?></p>
                            <p class="text-slate-600">Status: <?= htmlspecialchars($quote['status']) ?></p>
                            <p class="text-slate-600">Total: GHS <?= number_format($quote['total_amount'], 2) ?></p>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
            <div class="rounded-3xl bg-white p-6 shadow-lg border border-slate-200">
                <h2 class="text-xl font-semibold text-slate-950">Active Projects</h2>
                <div class="mt-4 space-y-4">
                    <?php if (empty($projects)): ?>
                        <p class="text-slate-500">No active projects yet.</p>
                    <?php endif; ?>
                    <?php foreach ($projects as $project): ?>
                        <div class="rounded-2xl bg-slate-50 p-4">
                            <p class="text-sm font-semibold text-slate-950"><?= htmlspecialchars($project['name']) ?></p>
                            <p class="text-slate-600">Status: <?= htmlspecialchars($project['status']) ?> · <?= htmlspecialchars($project['progress']) ?>%</p>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
            <div class="rounded-3xl bg-white p-6 shadow-lg border border-slate-200">
                <h2 class="text-xl font-semibold text-slate-950">Recent Invoices</h2>
                <div class="mt-4 space-y-4">
                    <?php if (empty($invoices)): ?>
                        <p class="text-slate-500">No invoices available yet.</p>
                    <?php endif; ?>
                    <?php foreach ($invoices as $invoice): ?>
                        <div class="rounded-2xl bg-slate-50 p-4">
                            <p class="text-sm font-semibold text-slate-950"><?= htmlspecialchars($invoice['invoice_number']) ?></p>
                            <p class="text-slate-600">Due: <?= htmlspecialchars($invoice['due_date']) ?></p>
                            <p class="text-slate-600">Status: <?= htmlspecialchars($invoice['status']) ?></p>
                            <a href="<?= htmlspecialchars($baseUrl) ?>/portal/invoice?id=<?= htmlspecialchars($invoice['id']) ?>" class="mt-3 inline-flex link-primary">View details</a>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>

        <div class="mt-10 grid gap-6 xl:grid-cols-2">
            <div class="rounded-3xl bg-white p-6 shadow-lg border border-slate-200">
                <h2 class="text-xl font-semibold text-slate-950">Documents</h2>
                <div class="mt-4 space-y-4">
                    <?php if (empty($documents)): ?>
                        <p class="text-slate-500">No downloadable documents available.</p>
                    <?php endif; ?>
                    <?php foreach ($documents as $document): ?>
                        <div class="rounded-2xl bg-slate-50 p-4 flex items-center justify-between gap-4">
                            <div>
                                <p class="text-sm font-semibold text-slate-950"><?= htmlspecialchars($document['title']) ?></p>
                                <p class="text-slate-600">Type: <?= htmlspecialchars($document['document_type']) ?></p>
                            </div>
                            <a href="<?= htmlspecialchars($baseUrl) ?>/portal/document/download?id=<?= htmlspecialchars($document['id']) ?>" class="inline-flex rounded-full btn-primary px-4 py-2 text-sm font-semibold">Download</a>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
            <div class="rounded-3xl bg-white p-6 shadow-lg border border-slate-200">
                <div class="flex items-center justify-between gap-4">
                    <h2 class="text-xl font-semibold text-slate-950">Project Gallery</h2>
                    <a href="<?= htmlspecialchars($baseUrl) ?>/portal/gallery" class="link-primary text-sm">View all</a>
                </div>
                <div class="mt-4 grid gap-3 sm:grid-cols-2">
                    <?php if (empty($projectPhotos)): ?>
                        <p class="text-slate-500">No project images available yet.</p>
                    <?php endif; ?>
                    <?php foreach (array_slice($projectPhotos, 0, 4) as $photo): ?>
                        <div class="overflow-hidden rounded-3xl border border-slate-200 bg-slate-50">
                            <img src="<?= htmlspecialchars(str_starts_with($photo['file_path'], '/') ? $photo['file_path'] : '/' . ltrim($photo['file_path'], '/')) ?>" alt="<?= htmlspecialchars($photo['caption'] ?? $photo['project_name']) ?>" class="h-44 w-full object-cover" />
                            <div class="p-4">
                                <p class="text-sm font-semibold text-slate-950"><?= htmlspecialchars($photo['project_name']) ?></p>
                                <p class="text-slate-500 text-xs mt-1"><?= htmlspecialchars($photo['caption'] ?? 'Project photo') ?></p>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
    </div>
</section>
