// claims.jsx - claims tracking + FNOL filing wizard + rich detail (exported to window)
const { useState: useStateCl, useEffect: useEffectCl } = React;

const INCIDENT_TYPES = [
  { id: "accident", label: "Accident", icon: "car" },
  { id: "theft", label: "Theft / burglary", icon: "vault" },
  { id: "fire", label: "Fire", icon: "alert" },
  { id: "water", label: "Water / flood", icon: "ship" },
  { id: "damage", label: "Accidental damage", icon: "cog" },
  { id: "other", label: "Something else", icon: "doc" },
];

function Claims({ claims, assets, onFileClaim, onChat, onInsure, openId }) {
  const D = window.VC_DATA;
  const { t } = window.useT();
  const [openCl, setOpenCl] = useStateCl(claims[0] || null);
  const [fnol, setFnol] = useStateCl(false);

  useEffectCl(() => {
    if (openId) { const c = claims.find((x) => x.id === openId); if (c) setOpenCl(c); }
  }, [openId, claims]);
  useEffectCl(() => { if (openCl && !claims.find((c) => c.id === openCl.id)) setOpenCl(claims[0] || null); }, [claims]);

  return (
    <div className="vc-page" style={{ padding: "28px 34px 60px", maxWidth: 1180, margin: "0 auto" }}>
      <div style={{ display: "flex", alignItems: "flex-end", justifyContent: "space-between", flexWrap: "wrap", gap: 16 }}>
        <div>
          <h1 className="display" style={{ fontSize: 32, display: "flex", alignItems: "center", gap: 12 }}>
            <span style={{ display: "grid", placeItems: "center", width: 40, height: 40, borderRadius: 13, background: "var(--accent-soft)", color: "var(--accent)" }}><window.Icon name="claim" size={22} /></span>
            Claims
          </h1>
          <p style={{ color: "var(--text-muted)", fontSize: 15, marginTop: 8 }}>{t("cl.subtitle")}</p>
        </div>
        <window.Btn icon="plus" onClick={() => setFnol(true)}>{t("cl.fileClaim")}</window.Btn>
      </div>

      {/* stat strip */}
      <div className="vc-grid-2" style={{ display: "grid", gridTemplateColumns: "repeat(4,1fr)", gap: 12, marginTop: 22 }}>
        <ClaimStat label={t("cl.openClaims")} value={claims.filter((c) => c.status !== "Settled").length} icon="clock" />
        <ClaimStat label={t("cl.settled")} value={claims.filter((c) => c.status === "Settled").length} icon="check" tone="success" />
        <ClaimStat label={t("cl.totalClaimed")} value={D.fmtNaira(claims.reduce((s, c) => s + c.loss, 0))} icon="trend" />
        <ClaimStat label={t("cl.avgSettlement")} value={t("cl.days", { n: 8 })} icon="clock" tone="accent" />
      </div>

      <div className="vc-grid" style={{ display: "grid", gridTemplateColumns: "0.9fr 1.1fr", gap: 18, marginTop: 18, alignItems: "start" }}>
        <div style={{ display: "grid", gap: 12 }}>
          {claims.map((c) => {
            const active = openCl?.id === c.id;
            return (
              <div key={c.id} onClick={() => setOpenCl(c)} style={{
                padding: 16, borderRadius: 18, cursor: "pointer", background: "var(--surface)",
                border: "1.5px solid " + (active ? "var(--accent)" : "var(--border)"), boxShadow: active ? "var(--shadow-md)" : "var(--shadow-sm)", transition: "all .2s",
                animation: c.isNew ? "vc-drop-in .7s var(--ease) both" : "none",
              }}>
                <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
                  <span className="mono" style={{ fontSize: 12.5, color: "var(--text-faint)", fontWeight: 600 }}>{c.id}</span>
                  <StatusPill status={c.status} />
                </div>
                <div style={{ fontWeight: 700, fontSize: 15.5, marginTop: 8 }}>{c.asset}</div>
                <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", marginTop: 6 }}>
                  <span style={{ fontSize: 13, color: "var(--text-muted)" }}>{c.type} · {c.date}</span>
                  <span className="mono" style={{ fontSize: 14, fontWeight: 700 }}>{D.fmtNaira(c.loss)}</span>
                </div>
                <div style={{ marginTop: 12 }}><MiniTrack stage={c.stage} /></div>
              </div>
            );
          })}
          {claims.length === 0 && <div style={{ padding: 40, textAlign: "center", color: "var(--text-faint)", border: "1px dashed var(--border-strong)", borderRadius: 16 }}>No claims yet - that's good news.</div>}
        </div>

        {openCl && <ClaimDetail claim={openCl} onChat={onChat} />}
      </div>

      <FnolWizard open={fnol} assets={assets} onClose={() => setFnol(false)} onInsure={(a) => { setFnol(false); onInsure && onInsure(a); }} onSubmit={(c) => { setFnol(false); onFileClaim(c); setTimeout(() => setOpenCl(c), 100); }} />
    </div>
  );
}

function ClaimStat({ label, value, icon, tone }) {
  const c = tone === "success" ? "var(--success)" : tone === "accent" ? "var(--accent)" : "var(--text)";
  return (
    <div style={{ padding: 16, borderRadius: 16, background: "var(--surface)", border: "1px solid var(--border)" }}>
      <span style={{ display: "grid", placeItems: "center", width: 32, height: 32, borderRadius: 9, background: tone === "success" ? "var(--success-soft)" : "var(--accent-soft)", color: tone === "success" ? "var(--success)" : "var(--accent)" }}><window.Icon name={icon} size={16} /></span>
      <div className="display mono" style={{ fontSize: 22, marginTop: 10, color: c }}>{value}</div>
      <div style={{ fontSize: 12, color: "var(--text-faint)", fontWeight: 600, marginTop: 2 }}>{label}</div>
    </div>
  );
}
function StatusPill({ status }) {
  const settled = status === "Settled";
  return <span style={{ fontSize: 11.5, fontWeight: 700, padding: "4px 10px", borderRadius: 999, background: settled ? "var(--success-soft)" : "var(--accent-soft)", color: settled ? "var(--success)" : "var(--accent-ink)" }}>{status}</span>;
}
function MiniTrack({ stage }) {
  const total = window.VC_DATA.CLAIM_STAGES.length;
  return (
    <div style={{ display: "flex", gap: 4 }}>
      {Array.from({ length: total }).map((_, i) => (
        <div key={i} style={{ flex: 1, height: 5, borderRadius: 999, background: i <= stage ? "var(--accent)" : "var(--surface-3)", transition: "background .3s" }} />
      ))}
    </div>
  );
}

/* ---------------- rich detail ---------------- */
function ClaimDetail({ claim, onChat }) {
  const D = window.VC_DATA;
  const stages = D.CLAIM_STAGES;
  const excess = Math.round(claim.loss * 0.1);
  const approved = claim.loss - excess;
  const settled = claim.status === "Settled";
  const docs = claim.docs || ["Incident report", "Photos of damage", "Police report"];
  const activity = buildActivity(claim);

  return (
    <div style={{ position: "sticky", top: 20, borderRadius: 22, background: "var(--surface)", border: "1px solid var(--border)", boxShadow: "var(--shadow-md)", overflow: "hidden", animation: "vc-fade-in .3s var(--ease) both" }}>
      <div style={{ padding: 22, background: "linear-gradient(150deg,var(--surface),var(--surface-2))", borderBottom: "1px solid var(--border)" }}>
        <div style={{ display: "flex", justifyContent: "space-between", alignItems: "flex-start" }}>
          <div>
            <span className="mono" style={{ fontSize: 13, color: "var(--text-faint)" }}>{claim.id}</span>
            <h2 className="display" style={{ fontSize: 23, marginTop: 4 }}>{claim.asset}</h2>
            <p style={{ fontSize: 13.5, color: "var(--text-muted)", marginTop: 4 }}>{claim.type} · Reported {claim.date}</p>
          </div>
          <StatusPill status={claim.status} />
        </div>
      </div>

      <div style={{ padding: 22 }}>
        {/* payout breakdown */}
        <div style={{ padding: 16, borderRadius: 16, background: "var(--surface-2)", border: "1px solid var(--border)" }}>
          <div style={{ fontSize: 12.5, fontWeight: 700, color: "var(--text-faint)", textTransform: "uppercase", letterSpacing: ".03em", marginBottom: 12 }}>Payout breakdown</div>
          <PayRow k="Estimated loss" v={D.fmtFull(claim.loss)} />
          <PayRow k="Policy excess (10%)" v={"- " + D.fmtFull(excess)} c="var(--text-muted)" />
          <div style={{ height: 1, background: "var(--border)", margin: "10px 0" }} />
          <PayRow k={settled ? "Amount settled" : "Expected payout"} v={D.fmtFull(approved)} c={settled ? "var(--success)" : "var(--accent)"} big />
          <div style={{ display: "flex", alignItems: "center", gap: 7, marginTop: 8, fontSize: 12, color: "var(--text-muted)" }}>
            <window.Icon name="shield" size={13} />Adjuster: <b style={{ color: "var(--text)" }}>Kunle Adeyemi</b>
          </div>
        </div>

        {/* timeline */}
        <div style={{ marginTop: 20 }}>
          <div style={{ fontSize: 12.5, fontWeight: 700, color: "var(--text-faint)", textTransform: "uppercase", letterSpacing: ".03em", marginBottom: 14 }}>Progress</div>
          {stages.map((s, i) => {
            const done = i < claim.stage, current = i === claim.stage;
            return (
              <div key={s} style={{ display: "flex", gap: 14, alignItems: "flex-start" }}>
                <div style={{ display: "flex", flexDirection: "column", alignItems: "center", alignSelf: "stretch" }}>
                  <span style={{ display: "grid", placeItems: "center", width: 28, height: 28, borderRadius: 999, flexShrink: 0, background: done ? "var(--accent)" : current ? "var(--surface)" : "var(--surface-3)", color: done ? "#fff" : current ? "var(--accent)" : "var(--text-faint)", border: current ? "2px solid var(--accent)" : "none", boxShadow: current ? "0 0 0 4px var(--accent-soft)" : "none" }}>
                    {done ? <window.Icon name="check" size={15} stroke={3} /> : <span style={{ fontSize: 11.5, fontWeight: 700 }}>{i + 1}</span>}
                  </span>
                  {i < stages.length - 1 && <span style={{ width: 2, flex: 1, minHeight: 22, background: done ? "var(--accent)" : "var(--surface-3)" }} />}
                </div>
                <div style={{ paddingBottom: 16, flex: 1 }}>
                  <div style={{ fontWeight: 700, fontSize: 14, color: current ? "var(--accent)" : done ? "var(--text)" : "var(--text-faint)" }}>{s}</div>
                  <div style={{ fontSize: 12.5, color: "var(--text-muted)", marginTop: 2 }}>{done ? "Completed" : current ? "In progress - Cova is on it" : "Pending"}</div>
                </div>
              </div>
            );
          })}
        </div>

        {/* documents */}
        <div style={{ marginTop: 8 }}>
          <div style={{ fontSize: 12.5, fontWeight: 700, color: "var(--text-faint)", textTransform: "uppercase", letterSpacing: ".03em", marginBottom: 10 }}>Documents</div>
          <div style={{ display: "grid", gap: 8 }}>
            {docs.map((d, i) => (
              <div key={d} style={{ display: "flex", alignItems: "center", gap: 10, padding: "10px 12px", borderRadius: 11, background: "var(--surface-2)", border: "1px solid var(--border)" }}>
                <window.Icon name="doc" size={16} style={{ color: "var(--accent)" }} />
                <span style={{ flex: 1, fontSize: 13 }}>{d}</span>
                <window.Icon name="check" size={14} stroke={3} style={{ color: "var(--success)" }} />
              </div>
            ))}
          </div>
        </div>

        {/* activity */}
        <div style={{ marginTop: 20 }}>
          <div style={{ fontSize: 12.5, fontWeight: 700, color: "var(--text-faint)", textTransform: "uppercase", letterSpacing: ".03em", marginBottom: 12 }}>Activity</div>
          <div style={{ display: "grid", gap: 12 }}>
            {activity.map((a, i) => (
              <div key={i} style={{ display: "flex", gap: 10 }}>
                <span style={{ display: "grid", placeItems: "center", width: 28, height: 28, borderRadius: 999, flexShrink: 0, background: a.bot ? "linear-gradient(135deg,var(--accent-2),var(--accent))" : "var(--surface-3)", color: a.bot ? "#fff" : "var(--text-muted)" }}><window.Icon name={a.bot ? "sparkle" : "user"} size={14} /></span>
                <div style={{ flex: 1 }}>
                  <div style={{ fontSize: 13, lineHeight: 1.45 }}><b>{a.who}</b> {a.text}</div>
                  <div style={{ fontSize: 11, color: "var(--text-faint)", marginTop: 2 }}>{a.time}</div>
                </div>
              </div>
            ))}
          </div>
        </div>

        <window.Btn variant="soft" icon="sparkle" style={{ width: "100%", justifyContent: "center", marginTop: 20 }} onClick={() => onChat(`What's the status of claim ${claim.id}?`)}>Ask Cova about this claim</window.Btn>
      </div>
    </div>
  );
}

function PayRow({ k, v, c, big }) {
  return (
    <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", padding: "3px 0" }}>
      <span style={{ fontSize: 13.5, color: "var(--text-muted)" }}>{k}</span>
      <span className="mono" style={{ fontSize: big ? 19 : 14, fontWeight: 700, color: c || "var(--text)" }}>{v}</span>
    </div>
  );
}

function buildActivity(claim) {
  const base = [{ who: "Cova", bot: true, text: "logged your claim and notified " + (claim.insurer || "your insurer") + ".", time: claim.date }];
  if (claim.stage >= 1) base.push({ who: "You", bot: false, text: "uploaded supporting documents.", time: "1 day later" });
  if (claim.stage >= 2) base.push({ who: "Cova", bot: true, text: "scheduled a further assessment.", time: "2 days later" });
  if (claim.stage >= 3) base.push({ who: "Cova", bot: true, text: "submitted the assessed report to the insurer.", time: "4 days later" });
  if (claim.stage >= 4) base.push({ who: "Insurer", bot: false, text: "approved the settlement amount.", time: "6 days later" });
  return base.reverse();
}

/* ---------------- FNOL wizard ---------------- */
function FnolWizard({ open, assets, onClose, onSubmit, onInsure }) {
  const D = window.VC_DATA;
  const [stp, setStp] = useStateCl(0);
  const [asset, setAsset] = useStateCl(null);
  const [type, setType] = useStateCl(null);
  const [date, setDate] = useStateCl("2026-06-05");
  const [loss, setLoss] = useStateCl("");
  const [desc, setDesc] = useStateCl("");
  const [submitting, setSubmitting] = useStateCl(false);
  const [evidence, setEvidence] = useStateCl([]); // uploaded docs {id, kind, name}
  const [uploadingKind, setUploadingKind] = useStateCl("");

  useEffectCl(() => { if (open) { setStp(0); setAsset(null); setType(null); setLoss(""); setDesc(""); setSubmitting(false); setEvidence([]); setUploadingKind(""); } }, [open]);

  // Claim evidence is real: files upload to the asset's document store
  // (/api/documents), where Cova also reads images to support the claim.
  const uploadEvidence = (kind, e) => {
    const file = e.target.files && e.target.files[0];
    e.target.value = "";
    if (!file || !asset) return;
    if (file.size > 6 * 1024 * 1024) return;
    const reader = new FileReader();
    reader.onload = () => {
      setUploadingKind(kind);
      fetch("/api/documents", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ assetId: asset.id, kind, name: file.name, dataUrl: reader.result }) })
        .then((r) => r.json())
        .then((doc) => { if (doc && doc.id) setEvidence((xs) => [...xs, { id: doc.id, kind, name: file.name }]); })
        .catch(() => {})
        .finally(() => setUploadingKind(""));
    };
    reader.readAsDataURL(file);
  };

  // A claim can only proceed on an asset that actually has cover. Uninsured
  // assets never start the claim flow — we route the customer to insure instead.
  const insured = !!(asset && asset.insurer);
  const canNext = (stp === 0 && insured) || (stp === 1 && type) || stp === 2;
  const submit = () => {
    setSubmitting(true);
    const lossN = window.parseAmount(loss) || Math.round((asset?.replacement || 5000000) * 0.15);
    const claim = {
      id: "CL-2026-00" + (40 + Math.floor(Math.random() * 50)),
      assetId: asset.id, // the real vault asset — lets the backend persist + triage it
      asset: asset.name, type: INCIDENT_TYPES.find((x) => x.id === type)?.label || "Incident",
      loss: lossN, status: "In progress", date: new Date().toISOString().slice(0, 10), stage: 0,
      insurer: asset.insurer, isNew: true, note: desc,
      docs: ["Incident report", ...(desc ? ["Your description"] : []), ...evidence.map((ev) => ev.name)],
    };
    setTimeout(() => onSubmit(claim), 1500);
  };

  const titles = ["Which asset is affected?", "What happened?", "Tell us more", "Review & submit"];

  return (
    <window.Overlay open={open} onClose={submitting ? undefined : onClose} width={560}>
      {!submitting ? (
        <div>
          <div style={{ padding: "20px 24px", borderBottom: "1px solid var(--border)" }}>
            <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between" }}>
              <h2 className="display" style={{ fontSize: 20 }}>File a claim</h2>
              <button onClick={onClose} style={{ display: "grid", placeItems: "center", width: 34, height: 34, borderRadius: 999, background: "var(--surface-3)" }}><window.Icon name="close" size={17} /></button>
            </div>
            <div style={{ display: "flex", gap: 6, marginTop: 14 }}>
              {titles.map((_, i) => <div key={i} style={{ flex: 1, height: 5, borderRadius: 999, background: i <= stp ? "var(--accent)" : "var(--surface-3)", transition: "background .3s" }} />)}
            </div>
            <p style={{ fontSize: 14.5, fontWeight: 600, color: "var(--text-muted)", marginTop: 12 }}>{titles[stp]}</p>
          </div>

          <div style={{ padding: 22, maxHeight: 420, overflowY: "auto" }}>
            {stp === 0 && (
              <div style={{ display: "grid", gap: 10 }}>
                {assets.map((a) => {
                  const on = asset?.id === a.id;
                  return (
                    <button key={a.id} onClick={() => setAsset(a)} style={{ display: "flex", alignItems: "center", gap: 12, padding: 13, borderRadius: 13, textAlign: "left", background: on ? "var(--accent-soft)" : "var(--surface)", border: "1.5px solid " + (on ? "var(--accent)" : "var(--border)") }}>
                      <window.AssetThumb category={a.category} size={42} />
                      <div style={{ flex: 1 }}><div style={{ fontWeight: 700, fontSize: 14 }}>{a.name}</div><div style={{ fontSize: 12, color: "var(--text-faint)" }}>{a.insurer || "Uninsured"}</div></div>
                      {on && <window.Icon name="check" size={18} stroke={3} style={{ color: "var(--accent)" }} />}
                    </button>
                  );
                })}
                {asset && !insured && (
                  <div style={{ padding: 14, borderRadius: 13, background: "var(--surface-2)", border: "1.5px solid var(--border-strong)", display: "flex", gap: 11, alignItems: "flex-start", marginTop: 2 }}>
                    <span style={{ display: "grid", placeItems: "center", width: 34, height: 34, borderRadius: 10, flexShrink: 0, background: "var(--accent-soft)", color: "var(--accent)" }}><window.Icon name="shield" size={18} /></span>
                    <div>
                      <div style={{ fontWeight: 700, fontSize: 14 }}>{asset.name} isn't insured</div>
                      <div style={{ fontSize: 13, color: "var(--text-muted)", marginTop: 3, lineHeight: 1.5 }}>There's no cover to claim against yet. Let's get it protected first. I can set up cover for it in a minute.</div>
                    </div>
                  </div>
                )}
              </div>
            )}

            {stp === 1 && (
              <div style={{ display: "grid", gap: 16 }}>
                <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 10 }}>
                  {INCIDENT_TYPES.map((it) => {
                    const on = type === it.id;
                    return (
                      <button key={it.id} onClick={() => setType(it.id)} style={{ display: "flex", alignItems: "center", gap: 10, padding: 13, borderRadius: 13, background: on ? "var(--accent-soft)" : "var(--surface)", border: "1.5px solid " + (on ? "var(--accent)" : "var(--border)") }}>
                        <span style={{ display: "grid", placeItems: "center", width: 34, height: 34, borderRadius: 10, background: on ? "var(--accent)" : "var(--surface-3)", color: on ? "#fff" : "var(--text-muted)" }}><window.Icon name={it.icon} size={17} /></span>
                        <span style={{ fontWeight: 700, fontSize: 13.5 }}>{it.label}</span>
                      </button>
                    );
                  })}
                </div>
                <FnolField label="When did it happen?" value={date} onChange={setDate} type="date" />
                <FnolField label="Estimated loss (optional)" value={loss} onChange={setLoss} placeholder="e.g. ₦6.5M" />
              </div>
            )}

            {stp === 2 && (
              <div style={{ display: "grid", gap: 16 }}>
                <label style={{ display: "block" }}>
                  <span style={{ fontSize: 12.5, fontWeight: 600, color: "var(--text-muted)" }}>Describe what happened</span>
                  <textarea value={desc} onChange={(e) => setDesc(e.target.value)} rows={4} placeholder="A short account of the incident helps Cova process your claim faster…" style={{ width: "100%", marginTop: 6, padding: 13, borderRadius: 12, border: "1.5px solid var(--border)", background: "var(--surface-2)", fontSize: 14.5, color: "var(--text)", resize: "vertical", outline: "none", fontFamily: "var(--font-body)" }} />
                </label>
                <div>
                  <span style={{ fontSize: 12.5, fontWeight: 600, color: "var(--text-muted)" }}>Upload evidence</span>
                  <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 10, marginTop: 6 }}>
                    {[["photo", "Photos", "image/*"], ["doc", "Documents", "image/*,application/pdf"]].map(([kind, label, accept]) => (
                      <label key={kind} style={{ display: "grid", placeItems: "center", gap: 6, padding: "22px 12px", borderRadius: 13, cursor: "pointer", border: "1.5px dashed " + (evidence.some((e) => e.kind === kind) ? "var(--success)" : "var(--border-strong)"), color: evidence.some((e) => e.kind === kind) ? "var(--success)" : "var(--text-faint)" }}>
                        <window.Icon name={evidence.some((e) => e.kind === kind) ? "check" : "download"} size={22} style={evidence.some((e) => e.kind === kind) ? {} : { transform: "rotate(180deg)" }} />
                        <span className="mono" style={{ fontSize: 11.5 }}>{uploadingKind === kind ? "uploading…" : evidence.filter((e) => e.kind === kind).length ? evidence.filter((e) => e.kind === kind).length + " uploaded ✓" : "add " + label.toLowerCase()}</span>
                        <input type="file" accept={accept} style={{ display: "none" }} disabled={!!uploadingKind} onChange={(e) => uploadEvidence(kind, e)} />
                      </label>
                    ))}
                  </div>
                  {evidence.length > 0 && (
                    <div style={{ display: "flex", flexWrap: "wrap", gap: 6, marginTop: 8 }}>
                      {evidence.map((ev) => <span key={ev.id} style={{ fontSize: 11.5, fontWeight: 600, padding: "3px 10px", borderRadius: 999, background: "var(--success-soft)", color: "var(--success)" }}>{ev.name}</span>)}
                    </div>
                  )}
                </div>
              </div>
            )}

            {stp === 3 && (
              <div style={{ display: "grid", gap: 12 }}>
                <ReviewRow k="Asset" v={asset?.name} />
                <ReviewRow k="Insurer" v={asset?.insurer || "-"} />
                <ReviewRow k="Incident" v={INCIDENT_TYPES.find((x) => x.id === type)?.label} />
                <ReviewRow k="Date" v={date} />
                <ReviewRow k="Estimated loss" v={window.parseAmount(loss) ? D.fmtFull(window.parseAmount(loss)) : "Cova will assess"} />
                <div style={{ padding: 14, borderRadius: 13, background: "var(--accent-soft)", border: "1px solid var(--accent)", display: "flex", gap: 11, alignItems: "center", marginTop: 4 }}>
                  <window.Icon name="sparkle" size={20} style={{ color: "var(--accent)" }} />
                  <span style={{ fontSize: 13, color: "var(--accent-ink)", fontWeight: 600 }}>Cova will notify {asset?.insurer || "your insurer"} instantly and guide you through every step.</span>
                </div>
              </div>
            )}
          </div>

          <div style={{ padding: "16px 22px", borderTop: "1px solid var(--border)", display: "flex", justifyContent: "space-between" }}>
            <window.Btn variant="ghost" onClick={stp === 0 ? onClose : () => setStp(stp - 1)}>{stp === 0 ? "Cancel" : "Back"}</window.Btn>
            {stp === 0 && asset && !insured
              ? <window.Btn icon="shield" onClick={() => onInsure && onInsure(asset)}>Insure this asset</window.Btn>
              : stp < 3 ? <window.Btn iconR="arrowR" disabled={!canNext} onClick={() => setStp(stp + 1)}>Continue</window.Btn>
              : <window.Btn icon="check" onClick={submit}>Submit claim</window.Btn>}
          </div>
        </div>
      ) : (
        <div style={{ padding: 48, textAlign: "center" }}>
          <span style={{ display: "inline-block", width: 52, height: 52, border: "4px solid var(--surface-3)", borderTopColor: "var(--accent)", borderRadius: 999, animation: "vc-spin .8s linear infinite" }} />
          <h2 className="display" style={{ fontSize: 21, marginTop: 22 }}>Filing your claim…</h2>
          <p style={{ fontSize: 14, color: "var(--text-muted)", marginTop: 6 }}>Cova is notifying your insurer.</p>
        </div>
      )}
    </window.Overlay>
  );
}

function FnolField({ label, value, onChange, type = "text", placeholder }) {
  return (
    <label style={{ display: "block" }}>
      <span style={{ fontSize: 12.5, fontWeight: 600, color: "var(--text-muted)" }}>{label}</span>
      <input type={type} value={value} placeholder={placeholder} onChange={(e) => onChange(e.target.value)} style={{ width: "100%", marginTop: 6, padding: "12px 13px", borderRadius: 12, border: "1.5px solid var(--border)", background: "var(--surface-2)", fontSize: 14.5, color: "var(--text)", outline: "none", fontFamily: "var(--font-body)" }} />
    </label>
  );
}
function ReviewRow({ k, v }) {
  return (
    <div style={{ display: "flex", justifyContent: "space-between", padding: "9px 0", borderBottom: "1px solid var(--border)" }}>
      <span style={{ fontSize: 13.5, color: "var(--text-muted)" }}>{k}</span>
      <span style={{ fontSize: 13.5, fontWeight: 700 }}>{v}</span>
    </div>
  );
}

Object.assign(window, { Claims });
