
The user said "Continue" — meaning proceed with the plan. Based on the prior conversation, the most sensible interpretation is the **"Approve plan & start Phase 1"** option I previously offered. Let me present a tight, actionable Phase 1 plan they can approve to kick off implementation.

Stack confirmed: React + Vite + TS + Tailwind + Lovable Cloud (Postgres + Auth + Edge Functions). Workspaces from day 1. Roles: clinic_user + bpo_user. PDF: pdf-lib. Design: slate + teal. HIPAA-ready structure only.

Keep this concise — focus on what ships in Phase 1.

---

# Phase 1 — Foundations: Auth, Workspaces, Patients, Insurance, Claims (Draft)

## What ships in Phase 1
A logged-in user can create a workspace, add patients & insurance payers, draft a claim, and see a dashboard with metrics. PDF generation, payments, reports, and templates come in later phases.

## Pages & routes
```text
/auth              → Login / Signup (tabs)
/                  → Dashboard (metrics + recent claims)
/patients          → List + search + "New patient" modal
/insurance         → List + "New payer" modal
/claims            → List with status filters
/claims/new        → Single-page fast claim form
/claims/:id        → Claim details + notes
/settings          → Profile, workspace, role
*                  → NotFound
```

Layout: persistent **sidebar** (collapsible icon mode) + **topbar** with workspace switcher + user menu. Modals/drawers for all create/edit forms.

## Database (Lovable Cloud / Postgres + RLS)

```text
profiles              id (=auth.uid), full_name, default_workspace_id
user_roles            user_id, workspace_id, role(app_role enum)
workspaces            id, name, owner_id, type(clinic|bpo)
workspace_members     workspace_id, user_id, role
patients              id, workspace_id, first_name, last_name, dob,
                      sex, address, phone, mrn
insurance_payers      id, workspace_id, name, payer_id, address, phone
patient_insurance     id, patient_id, payer_id, member_id, group_no,
                      relationship, priority(primary|secondary)
claims                id, workspace_id, patient_id, payer_id,
                      provider_snapshot(jsonb), dx_codes(text[]),
                      status(draft|pending|paid|denied),
                      total_charge, total_paid, balance, claim_number,
                      service_date_from, service_date_to, created_by
claim_items           id, claim_id, line_no, dos, cpt, modifiers,
                      dx_pointers, units, charge
claim_notes           id, claim_id, author_id, body, created_at
audit_logs            id, workspace_id, actor_id, action, entity,
                      entity_id, diff(jsonb), created_at
```

**Enums:** `app_role` (`owner`, `clinic_user`, `bpo_user`), `claim_status`, `workspace_type`.

**RLS strategy (critical):**
- Every tenant table filtered by `workspace_id` membership via a `SECURITY DEFINER` function `is_workspace_member(_uid, _ws)`.
- Roles stored in `user_roles` (separate table — never on profiles).
- `has_role(_uid, _ws, _role)` SECURITY DEFINER helper for admin gates.
- Trigger to auto-create `profiles` row + a default workspace on signup.

## Auth
- Email + password via Lovable Cloud Auth.
- `onAuthStateChange` listener mounted before `getSession()`.
- Protected route wrapper redirects to `/auth`.
- Workspace context provider chooses active workspace; persists in localStorage.

## Claims fast-form (the heart of Phase 1)
Single page, no reloads:
1. Patient picker (combobox, create-new inline)
2. Insurance picker (auto-fills from patient_insurance)
3. Provider snapshot (free text now; templates in Phase 2)
4. ICD-10 chips input (up to 12)
5. CPT line-item table (add/remove rows, autocompute totals)
6. Save as Draft / Submit (sets status `pending`)

## Dashboard
4 metric cards (Total / Pending / Paid / Denied) + recent claims table (last 10) — all scoped to active workspace.

## Design system
- Update `index.css` tokens to slate base + **teal accent** (HSL only).
- Status palette: `paid=green`, `pending=amber`, `denied=red`, `draft=slate`.
- Components: shadcn (already present) — `Card`, `Table`, `Dialog`, `Sheet`, `Sidebar`, `Badge`, `Form`.
- Inter font, tight spacing, dense tables, keyboard-friendly inputs.

## Deferred to later phases
- Phase 2: Provider/Insurance/CPT templates, CMS-1500 PDF (pdf-lib), payments
- Phase 3: Reports + CSV export, BPO workspace switcher UI, audit log viewer
- Phase 4: HIBP password check, advanced search, bulk actions

## Build order (single implementation pass)
1. Enable Lovable Cloud + apply DB migration (tables, enums, RLS, triggers)
2. Auth pages + protected routes + workspace context
3. App shell (sidebar + topbar + workspace switcher)
4. Patients module (list + modal CRUD)
5. Insurance module (list + modal CRUD)
6. Claims list + fast-form + details page
7. Dashboard metrics
8. Settings page (profile + workspace)

## Open items I'll assume unless you say otherwise
- Email confirmation **disabled** in dev for fast iteration (you can enable later in Cloud auth settings).
- Single default workspace auto-created on signup; BPO users can create more from Settings.
- ICD/CPT inputs are free-text in Phase 1 (no code-set lookup yet).
