<div class="space-y-6">
  <section class="grid gap-4 md:grid-cols-2 xl:grid-cols-5">
    <a href="/client/compliance" class="af-metric-card hover:border-brand-200"><div class="text-xs font-semibold uppercase text-slate-500">Active Services</div><div class="mt-2 text-3xl font-semibold text-slate-900">{{ total_engagements or 0 }}</div><div class="mt-1 text-xs text-slate-500">Services handled by firm</div></a>
    <button type="button" onclick="window.loadClientDashboardTab && window.loadClientDashboardTab('action-centre')" class="af-metric-card border-amber-200 bg-amber-50 text-left hover:border-amber-300"><div class="text-xs font-semibold uppercase text-amber-700">Pending From Me</div><div class="mt-2 text-3xl font-semibold text-amber-700">{{ pending_from_client or 0 }}</div><div class="mt-1 text-xs text-amber-700">Action required</div></button>
    <a href="/client/compliance" class="af-metric-card hover:border-brand-200"><div class="text-xs font-semibold uppercase text-slate-500">With Firm</div><div class="mt-2 text-3xl font-semibold text-brand-700">{{ with_firm or 0 }}</div><div class="mt-1 text-xs text-slate-500">Being processed</div></a>
    <button type="button" onclick="window.loadClientDashboardTab && window.loadClientDashboardTab('services-documents')" class="af-metric-card text-left hover:border-brand-200"><div class="text-xs font-semibold uppercase text-slate-500">Recent Documents</div><div class="mt-2 text-3xl font-semibold text-slate-900">{{ recent_documents|length if recent_documents else 0 }}</div><div class="mt-1 text-xs text-slate-500">Latest files</div></button>
    <button type="button" onclick="window.loadClientDashboardTab && window.loadClientDashboardTab('billing-messages')" class="af-metric-card border-amber-200 bg-amber-50 text-left hover:border-amber-300"><div class="text-xs font-semibold uppercase text-amber-700">Outstanding</div><div class="mt-2 text-2xl font-semibold text-amber-700">₹ {{ '%.2f'|format(billing_outstanding_amount or 0) }}</div><div class="mt-1 text-xs text-amber-700">{{ billing_open_count or 0 }} open bill(s)</div></button>
  </section>

  <section class="grid gap-6 xl:grid-cols-[minmax(0,1fr)_380px]">
    <div class="space-y-6">
      <div class="af-card">
        <div class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
          <div><h3 class="text-lg font-semibold text-slate-900">Needs Your Attention</h3><p class="mt-1 text-sm text-slate-500">Documents, replies or confirmations currently expected from you.</p></div>
          <button type="button" onclick="window.loadClientDashboardTab && window.loadClientDashboardTab('pending')" class="af-btn af-btn-primary">View Pending Items</button>
        </div>
        <div class="mt-5 space-y-3">
          {% set shown = namespace(count=0) %}
          {% for task in task_rows or [] %}
            {% if shown.count < 6 and (task.status or 'pending') in ['pending','blocked','client_pending','clarification_required','rework'] %}
            {% set shown.count = shown.count + 1 %}
            <a href="/client/engagements/{{ task.subscription_id }}" class="block rounded-2xl border border-amber-200 bg-amber-50 p-4 hover:bg-amber-100">
              <div class="flex flex-col gap-2 md:flex-row md:items-center md:justify-between">
                <div><div class="font-semibold text-slate-900">{{ task.task_name or (task.catalogue.service_name if task.catalogue else 'Task') }}</div><div class="text-xs text-amber-700">{{ (task.status or 'pending')|replace('_',' ')|title }}{% if task.financial_year %} • FY {{ task.financial_year }}{% endif %}</div></div>
                <div class="text-sm text-slate-600">Due: {{ task.internal_target_date.strftime('%d-%m-%Y') if task.internal_target_date else '-' }}</div>
              </div>
            </a>
            {% endif %}
          {% endfor %}
          {% if shown.count == 0 %}
          <div class="rounded-2xl border border-dashed border-slate-300 p-6 text-sm text-slate-500">No immediate action is pending from you.</div>
          {% endif %}
        </div>
      </div>

      <div class="af-card">
        <div class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
          <div><h3 class="text-lg font-semibold text-slate-900">Upcoming Services & Due Dates</h3><p class="mt-1 text-sm text-slate-500">Important service deadlines visible to your client login.</p></div>
          <a href="/client/compliance" class="af-btn af-btn-secondary">Full Compliance Page</a>
        </div>
        <div class="mt-5 space-y-3">
          {% for row in engagements[:6] if engagements is defined %}
          <a href="/client/engagements/{{ row.id }}" class="block rounded-2xl border border-slate-200 p-4 hover:bg-slate-50">
            <div class="flex flex-col gap-2 md:flex-row md:items-center md:justify-between">
              <div><div class="font-semibold text-slate-900">{{ row.catalogue.service_name if row.catalogue else 'Service' }}</div><div class="text-xs text-slate-500">FY {{ row.financial_year }}{% if row.assessment_year %} • AY {{ row.assessment_year }}{% endif %}</div></div>
              <div class="text-sm text-slate-600">Due: {{ row.current_due_date.strftime('%d-%m-%Y') if row.current_due_date else '-' }}</div>
            </div>
          </a>
          {% else %}
          <div class="rounded-2xl border border-dashed border-slate-300 p-6 text-sm text-slate-500">No upcoming service due dates found.</div>
          {% endfor %}
        </div>
      </div>
    </div>

    <aside class="space-y-6">
      <div class="af-card">
        <h3 class="text-base font-semibold text-slate-900">My Client Profile</h3>
        <div class="mt-4 space-y-3 text-sm">
          <div><div class="text-xs uppercase tracking-wide text-slate-500">Client</div><div class="font-semibold">{{ client_row.client_name }}</div></div>
          <div><div class="text-xs uppercase tracking-wide text-slate-500">PAN / GSTIN</div><div>{{ client_row.pan or '-' }}{% if client_row.gstin %} / {{ client_row.gstin }}{% endif %}</div></div>
          <div><div class="text-xs uppercase tracking-wide text-slate-500">Firm Contact</div><div>{{ client_row.partner_name or 'Firm team' }}</div></div>
          <div><div class="text-xs uppercase tracking-wide text-slate-500">Branch</div><div>{{ client_row.branch_name or '-' }}</div></div>
        </div>
        <a href="/client/profile" class="mt-5 inline-flex af-btn af-btn-secondary">Update Profile</a>
      </div>

      <div class="af-card">
        <h3 class="font-semibold text-slate-900">Quick Actions</h3>
        <div class="mt-4 grid gap-2 text-sm">
          <button type="button" onclick="window.loadClientDashboardTab && window.loadClientDashboardTab('action-centre')" class="rounded-xl border border-slate-200 px-4 py-3 text-left font-semibold text-slate-700 hover:bg-slate-50">Pending From Me</button>
          <a href="/client/documents" class="rounded-xl border border-slate-200 px-4 py-3 font-semibold text-slate-700 hover:bg-slate-50">Upload / View Documents</a>
          <a href="/client/messages" class="rounded-xl border border-slate-200 px-4 py-3 font-semibold text-slate-700 hover:bg-slate-50">Messages from Firm</a>
          <a href="/client/billing" class="rounded-xl border border-slate-200 px-4 py-3 font-semibold text-slate-700 hover:bg-slate-50">View Bills & Receipts</a>
          {% if billing_latest_due_invoice %}<a href="/client/billing/{{ billing_latest_due_invoice.id }}/pay-now" class="rounded-xl border border-amber-200 bg-amber-50 px-4 py-3 font-semibold text-amber-700 hover:bg-amber-100">Pay Latest Due</a>{% endif %}
        </div>
      </div>
    </aside>
  </section>
</div>
