// Yew — client portal / dashboard

const BROKER_DATA = [
  { name: "Spokeo",            cat: "people-search",  status: "removed",    last: "Today",     first: "Mar 14, 2026" },
  { name: "Whitepages",        cat: "people-search",  status: "removed",    last: "Yesterday", first: "Mar 14, 2026" },
  { name: "BeenVerified",      cat: "people-search",  status: "removed",    last: "Apr 02",    first: "Mar 14, 2026" },
  { name: "Intelius",          cat: "people-search",  status: "pending",    last: "Apr 11",    first: "Mar 14, 2026" },
  { name: "Radaris",           cat: "people-search",  status: "removed",    last: "Apr 18",    first: "Mar 18, 2026" },
  { name: "MyLife",            cat: "people-search",  status: "pending",    last: "Apr 22",    first: "Mar 18, 2026" },
  { name: "TruthFinder",       cat: "court-records",  status: "removed",    last: "May 02",    first: "Mar 22, 2026" },
  { name: "InstantCheckmate",  cat: "court-records",  status: "removed",    last: "May 04",    first: "Mar 22, 2026" },
  { name: "PeopleFinder",      cat: "people-search",  status: "removed",    last: "May 06",    first: "Mar 25, 2026" },
  { name: "FastPeopleSearch",  cat: "people-search",  status: "verifying",  last: "May 09",    first: "Mar 25, 2026" },
  { name: "USPhoneBook",       cat: "address",        status: "removed",    last: "May 10",    first: "Mar 27, 2026" },
  { name: "Acxiom",            cat: "marketing",      status: "removed",    last: "May 12",    first: "Mar 29, 2026" },
  { name: "LexisNexis",        cat: "marketing",      status: "in-progress",last: "May 14",    first: "Mar 30, 2026" },
  { name: "Epsilon",           cat: "marketing",      status: "removed",    last: "May 14",    first: "Apr 01, 2026" },
  { name: "Oracle Data Cloud", cat: "marketing",      status: "removed",    last: "May 15",    first: "Apr 02, 2026" },
  { name: "Experian Mkt.",     cat: "marketing",      status: "verifying",  last: "May 16",    first: "Apr 04, 2026" },
  { name: "PeekYou",           cat: "people-search",  status: "removed",    last: "May 16",    first: "Apr 05, 2026" },
  { name: "ZabaSearch",        cat: "address",        status: "removed",    last: "May 16",    first: "Apr 05, 2026" },
  { name: "AnyWho",            cat: "address",        status: "removed",    last: "May 17",    first: "Apr 06, 2026" },
  { name: "PublicRecords",     cat: "court-records",  status: "pending",    last: "May 17",    first: "Apr 08, 2026" },
];

const STATUS_META = {
  "removed":      { dot: "var(--brand)",   label: "Removed",      tone: "ok" },
  "pending":      { dot: "var(--accent)",  label: "Awaiting reply", tone: "warn" },
  "verifying":    { dot: "var(--accent)",  label: "Verifying",    tone: "warn" },
  "in-progress":  { dot: "var(--stone-soft)", label: "In progress", tone: "neutral" },
};

const ACTIVITY = [
  { t: "10:42",  type: "verify",  msg: "Verification clicked",            target: "PeekYou", note: "Removal confirmed via email" },
  { t: "10:28",  type: "submit",  msg: "Opt-out submitted",               target: "AnyWho",  note: "POST request · response 200" },
  { t: "09:55",  type: "mail",    msg: "Postal letter dispatched",        target: "Acxiom",  note: "Notarized · USPS tracking 9405…" },
  { t: "09:12",  type: "verify",  msg: "Verification clicked",            target: "ZabaSearch", note: "" },
  { t: "08:50",  type: "submit",  msg: "Opt-out submitted",               target: "USPhoneBook", note: "" },
  { t: "08:33",  type: "scan",    msg: "Re-scan complete",                target: "Quarterly cycle 02", note: "12 records re-indexed across 4 brokers" },
  { t: "Yest.",  type: "submit",  msg: "Opt-out submitted",               target: "Experian Marketing", note: "Phone call placed · 6m 14s" },
  { t: "Yest.",  type: "verify",  msg: "Verification clicked",            target: "Whitepages", note: "" },
  { t: "Yest.",  type: "flag",    msg: "Family member added",             target: "Eleanor R.", note: "Spouse · coverage begins May 18" },
];

const ACTIVITY_ICON = {
  verify:  "✓",
  submit:  "→",
  mail:    "✉",
  scan:    "◐",
  flag:    "·",
};

function Sidebar({ section, setSection, onSignOut }) {
  const items = [
    { id: "overview",   label: "Overview" },
    { id: "brokers",    label: "Brokers",    count: 95 },
    { id: "activity",   label: "Activity" },
    { id: "verify",     label: "Verifications", count: 3, accent: true },
    { id: "household",  label: "Household",  count: 4 },
    { id: "reports",    label: "Reports" },
    { id: "settings",   label: "Settings" },
  ];
  return (
    <aside
      style={{
        width: 240,
        borderRight: "1px solid var(--rule)",
        background: "var(--bg-elev)",
        display: "flex",
        flexDirection: "column",
        position: "sticky",
        top: 0,
        height: "100vh",
      }}
    >
      <div style={{ padding: "26px 22px 28px", borderBottom: "1px solid var(--rule)" }}>
        <YewWordmark size={34} glyphSize={28} />
      </div>

      <div style={{ padding: "20px 14px 10px" }}>
        <div className="eyebrow" style={{ padding: "0 8px 12px" }}>Client</div>
        <button
          style={{
            width: "100%", textAlign: "left",
            background: "var(--bg)", border: "1px solid var(--rule)",
            padding: "12px 12px",
            display: "flex", alignItems: "center", gap: 12,
            cursor: "pointer",
          }}
        >
          <div
            className="serif"
            style={{
              width: 32, height: 32, borderRadius: "50%",
              background: "var(--brand)", color: "var(--mist)",
              display: "grid", placeItems: "center",
              fontFamily: "var(--serif)",
              fontSize: 16, letterSpacing: "-0.02em",
            }}
          >
            R
          </div>
          <div style={{ flex: 1, minWidth: 0 }}>
            <div style={{ fontSize: 13, fontWeight: 500, color: "var(--fg)" }}>
              Robert R.
            </div>
            <div className="mono" style={{ fontSize: 10.5, color: "var(--muted)", letterSpacing: "0.06em" }}>
              HOUSEHOLD · 4 PROTECTED
            </div>
          </div>
        </button>
      </div>

      <nav style={{ padding: "16px 8px", flex: 1 }}>
        {items.map((it) => {
          const active = section === it.id;
          return (
            <button
              key={it.id}
              onClick={() => setSection(it.id)}
              style={{
                width: "100%", textAlign: "left",
                background: active ? "var(--bg)" : "transparent",
                border: 0,
                borderLeft: active ? "2px solid var(--brand)" : "2px solid transparent",
                padding: "10px 14px",
                fontSize: 14,
                fontWeight: active ? 500 : 400,
                color: active ? "var(--fg)" : "var(--muted)",
                cursor: "pointer",
                display: "flex", alignItems: "center", justifyContent: "space-between",
                gap: 8,
                fontFamily: "var(--sans)",
              }}
            >
              <span>{it.label}</span>
              {it.count !== undefined && (
                <span
                  className="num"
                  style={{
                    fontSize: 11, color: it.accent ? "var(--accent)" : "var(--stone-soft)",
                    fontVariantNumeric: "tabular-nums",
                  }}
                >
                  {it.count}
                </span>
              )}
            </button>
          );
        })}
      </nav>

      <div
        style={{
          padding: "16px 22px 22px",
          borderTop: "1px solid var(--rule)",
        }}
      >
        <div className="eyebrow" style={{ marginBottom: 8 }}>Your steward</div>
        <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
          <div
            style={{
              width: 28, height: 28, borderRadius: "50%",
              background: "var(--stone-soft)", border: "1px solid var(--rule)",
            }}
          />
          <div>
            <div style={{ fontSize: 13, color: "var(--fg)" }}>Theo Ainsworth</div>
            <div className="mono" style={{ fontSize: 10, color: "var(--muted)", letterSpacing: "0.06em" }}>
              ON CALL
            </div>
          </div>
        </div>
        <button
          className="btn-link"
          onClick={onSignOut}
          style={{ marginTop: 20, fontSize: 12, color: "var(--muted)" }}
        >
          ← Back to yew.ai
        </button>
      </div>
    </aside>
  );
}

/* ---------- Top bar of dashboard ---------- */
function DashTopBar({ section }) {
  const titles = {
    overview: "Overview",
    brokers: "Brokers",
    activity: "Activity",
    verify: "Verifications",
    household: "Household",
    reports: "Reports",
    settings: "Settings",
  };
  return (
    <div
      style={{
        display: "flex", alignItems: "center", justifyContent: "space-between",
        padding: "22px 40px",
        borderBottom: "1px solid var(--rule)",
        background: "var(--bg)",
      }}
    >
      <div style={{ display: "flex", alignItems: "baseline", gap: 16 }}>
        <h1
          className="serif"
          style={{
            fontFamily: "var(--serif)",
            fontSize: 28, fontWeight: 500, margin: 0,
            letterSpacing: "-0.015em",
          }}
        >
          {titles[section] || "Overview"}
        </h1>
        <span
          className="mono"
          style={{
            fontSize: 11, color: "var(--muted)", letterSpacing: "0.1em",
          }}
        >
          MONDAY · MAY 17 · 2026
        </span>
      </div>
      <div style={{ display: "flex", alignItems: "center", gap: 14 }}>
        <div
          style={{
            display: "flex", alignItems: "center", gap: 8,
            fontSize: 12, color: "var(--muted)",
            fontFamily: "var(--mono)",
            letterSpacing: "0.06em",
            textTransform: "uppercase",
          }}
        >
          <span
            style={{
              width: 7, height: 7, borderRadius: "50%",
              background: "var(--brand)",
              boxShadow: "0 0 0 4px color-mix(in oklab, var(--brand) 18%, transparent)",
            }}
          />
          Agent · running
        </div>
        <button className="btn btn-ghost" style={{ padding: "8px 14px", fontSize: 13 }}>
          Notify steward
        </button>
      </div>
    </div>
  );
}

/* ---------- Overview content ---------- */
function ExposureScoreCard() {
  // Quarter timeline
  const points = [
    { d: "Mar 14", v: 412 },
    { d: "Mar 28", v: 386 },
    { d: "Apr 11", v: 248 },
    { d: "Apr 25", v: 162 },
    { d: "May 09", v: 81 },
    { d: "Today",  v: 23 },
  ];
  const max = points[0].v;
  const w = 480, h = 120, pad = 6;
  const xs = (i) => pad + (i / (points.length - 1)) * (w - pad * 2);
  const ys = (v) => pad + (1 - v / max) * (h - pad * 2);
  const path = points.map((p, i) => `${i === 0 ? "M" : "L"}${xs(i)},${ys(p.v)}`).join(" ");
  const area = `${path} L ${xs(points.length - 1)},${h - pad} L ${xs(0)},${h - pad} Z`;

  return (
    <div
      style={{
        padding: "28px 32px",
        borderRight: "1px solid var(--rule)",
        borderBottom: "1px solid var(--rule)",
        gridColumn: "span 2",
        background: "var(--bg)",
      }}
    >
      <div className="exp-card-inner">
        <div>
          <Eyebrow accent>Exposure index</Eyebrow>
          <div style={{ display: "flex", alignItems: "baseline", gap: 16, marginTop: 14 }}>
            <span
              className="serif num"
              style={{
                fontFamily: "var(--serif)",
                fontSize: 84,
                lineHeight: 1,
                letterSpacing: "-0.025em",
                color: "var(--brand)",
              }}
            >
              23
            </span>
            <span
              className="mono"
              style={{
                fontSize: 12, color: "var(--accent)",
                letterSpacing: "0.06em",
              }}
            >
              ▼ 94.4% · QTR
            </span>
          </div>
          <div style={{ fontSize: 13.5, color: "var(--muted)", marginTop: 12, maxWidth: 360, lineHeight: 1.5 }}>
            Records currently findable on monitored brokers.
            Lower is better. <span style={{ color: "var(--fg)" }}>Yew target: under 30.</span>
          </div>
        </div>

        <svg className="exp-chart" viewBox={`0 0 ${w} ${h}`} preserveAspectRatio="xMaxYMid meet">
          <defs>
            <linearGradient id="exp" x1="0" y1="0" x2="0" y2="1">
              <stop offset="0%"  stopColor="var(--brand)" stopOpacity="0.18" />
              <stop offset="100%" stopColor="var(--brand)" stopOpacity="0" />
            </linearGradient>
          </defs>
          <path d={area} fill="url(#exp)" />
          <path d={path} stroke="var(--brand)" strokeWidth="1.5" fill="none" />
          {points.map((p, i) => (
            <g key={i}>
              <circle cx={xs(i)} cy={ys(p.v)} r={i === points.length - 1 ? 4 : 2.2}
                      fill={i === points.length - 1 ? "var(--accent)" : "var(--brand)"} />
              <text x={xs(i)} y={h - 1} textAnchor="middle"
                    style={{ fontSize: 10, fill: "var(--muted)", fontFamily: "var(--mono)", letterSpacing: "0.05em" }}>
                {p.d}
              </text>
            </g>
          ))}
        </svg>
      </div>
    </div>
  );
}

function MetricCard({ label, value, suffix, sub, accent, lastBorder }) {
  return (
    <div
      style={{
        padding: "28px 32px",
        borderBottom: "1px solid var(--rule)",
        borderRight: lastBorder ? "none" : "1px solid var(--rule)",
        background: "var(--bg)",
      }}
    >
      <Eyebrow>{label}</Eyebrow>
      <div style={{ display: "flex", alignItems: "baseline", gap: 6, marginTop: 14 }}>
        <span
          className="serif num"
          style={{
            fontFamily: "var(--serif)",
            fontSize: 44,
            lineHeight: 1,
            letterSpacing: "-0.02em",
            color: accent ? "var(--accent)" : "var(--brand)",
          }}
        >
          {value}
        </span>
        {suffix && (
          <span style={{ fontSize: 14, color: "var(--muted)" }}>{suffix}</span>
        )}
      </div>
      <div style={{ fontSize: 12.5, color: "var(--muted)", marginTop: 10, lineHeight: 1.5 }}>
        {sub}
      </div>
    </div>
  );
}

function BrokerRow({ b, last }) {
  const s = STATUS_META[b.status];
  return (
    <div
      style={{
        display: "grid",
        gridTemplateColumns: "1.6fr 1fr 1fr 1fr auto",
        gap: 16,
        padding: "14px 0",
        borderBottom: last ? "none" : "1px solid var(--rule)",
        alignItems: "center",
        fontSize: 13.5,
      }}
    >
      <div style={{ display: "flex", alignItems: "center", gap: 12 }}>
        <span
          style={{
            width: 6, height: 6, borderRadius: "50%",
            background: s.dot, flex: "none",
            boxShadow: b.status === "verifying"
              ? "0 0 0 3px color-mix(in oklab, var(--accent) 22%, transparent)"
              : "none",
          }}
        />
        <span style={{ color: "var(--fg)", fontWeight: 500 }}>{b.name}</span>
      </div>
      <div className="mono" style={{ fontSize: 11, color: "var(--muted)", letterSpacing: "0.05em", textTransform: "uppercase" }}>
        {b.cat}
      </div>
      <div style={{ color: "var(--fg)" }}>{s.label}</div>
      <div className="num" style={{ color: "var(--muted)", fontVariantNumeric: "tabular-nums" }}>
        {b.last}
      </div>
      <button className="btn-link" style={{ fontSize: 12, color: "var(--muted)" }}>
        Receipt →
      </button>
    </div>
  );
}

function ActivityItem({ a, last }) {
  return (
    <div
      style={{
        display: "grid",
        gridTemplateColumns: "60px 28px 1fr auto",
        gap: 14,
        padding: "16px 0",
        borderBottom: last ? "none" : "1px solid var(--rule)",
        alignItems: "flex-start",
      }}
    >
      <div
        className="mono num"
        style={{
          fontFamily: "var(--mono)", fontSize: 11,
          color: "var(--muted)", letterSpacing: "0.04em",
          paddingTop: 2,
          fontVariantNumeric: "tabular-nums",
        }}
      >
        {a.t}
      </div>
      <div
        style={{
          width: 22, height: 22, borderRadius: "50%",
          border: "1px solid var(--rule)",
          display: "grid", placeItems: "center",
          fontSize: 11, color: a.type === "verify" ? "var(--brand)" : "var(--muted)",
          fontFamily: "var(--mono)",
          background: "var(--bg)",
        }}
      >
        {ACTIVITY_ICON[a.type]}
      </div>
      <div style={{ fontSize: 13.5, lineHeight: 1.4 }}>
        <div style={{ color: "var(--fg)" }}>
          {a.msg}
          <span style={{ color: "var(--muted)" }}> · </span>
          <span className="serif" style={{ fontFamily: "var(--serif)", fontStyle: "italic" }}>
            {a.target}
          </span>
        </div>
        {a.note && (
          <div className="mono" style={{ fontSize: 11, color: "var(--muted)", marginTop: 4, letterSpacing: "0.02em" }}>
            {a.note}
          </div>
        )}
      </div>
    </div>
  );
}

function Verifications() {
  const items = [
    { broker: "FastPeopleSearch", from: "noreply@fastpeoplesearch.com", time: "12 min ago", action: "Click confirmation link" },
    { broker: "Experian Marketing", from: "donotreply@experian.com", time: "1 hr ago", action: "Confirm postal address on file" },
    { broker: "LexisNexis", from: "consumer@lexisnexis.com", time: "3 hr ago", action: "Notarized form requires signature" },
  ];
  return (
    <div style={{ marginTop: 0 }}>
      {items.map((it, i) => (
        <div
          key={i}
          className="verify-row"
          style={{
            padding: "16px 0",
            borderBottom: i < items.length - 1 ? "1px solid var(--rule)" : "none",
            display: "grid",
            gridTemplateColumns: "1fr auto",
            gap: 16,
            alignItems: "center",
          }}
        >
          <div>
            <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
              <span
                style={{
                  width: 6, height: 6, borderRadius: "50%",
                  background: "var(--accent)",
                  boxShadow: "0 0 0 3px color-mix(in oklab, var(--accent) 22%, transparent)",
                }}
              />
              <span style={{ fontSize: 14, fontWeight: 500, color: "var(--fg)" }}>{it.broker}</span>
              <span className="mono" style={{ fontSize: 10.5, color: "var(--muted)", letterSpacing: "0.06em" }}>
                {it.time.toUpperCase()}
              </span>
            </div>
            <div style={{ fontSize: 13, color: "var(--muted)", marginTop: 6 }}>
              <span style={{ fontFamily: "var(--mono)", fontSize: 11 }}>{it.from}</span>
              <span style={{ margin: "0 8px" }}>·</span>
              {it.action}
            </div>
          </div>
          <div style={{ display: "flex", gap: 8 }}>
            <button
              className="btn btn-ghost"
              style={{ padding: "8px 14px", fontSize: 12 }}
            >
              Defer to steward
            </button>
            <button
              className="btn btn-primary"
              style={{ padding: "8px 14px", fontSize: 12 }}
            >
              Yew handled it ✓
            </button>
          </div>
        </div>
      ))}
    </div>
  );
}

function Household() {
  const members = [
    { name: "Robert R.",    role: "Primary",   since: "Mar 14, 2026", exposure: 23,  status: "Protected" },
    { name: "Eleanor R.",   role: "Spouse",    since: "May 17, 2026", exposure: 198, status: "Onboarding" },
    { name: "Charlotte R.", role: "Daughter",  since: "Mar 18, 2026", exposure: 14,  status: "Protected" },
    { name: "James R.",     role: "Son",       since: "Mar 18, 2026", exposure: 9,   status: "Protected" },
  ];
  return (
    <div style={{ padding: "0" }}>
      {members.map((m, i) => (
        <div
          key={m.name}
          style={{
            display: "grid",
            gridTemplateColumns: "auto 1fr auto auto auto",
            gap: 20,
            padding: "20px 0",
            alignItems: "center",
            borderBottom: i < members.length - 1 ? "1px solid var(--rule)" : "none",
          }}
        >
          <div
            className="serif"
            style={{
              width: 40, height: 40, borderRadius: "50%",
              background: m.status === "Onboarding" ? "var(--accent)" : "var(--brand)",
              color: "var(--mist)",
              display: "grid", placeItems: "center",
              fontSize: 17, fontFamily: "var(--serif)",
            }}
          >
            {m.name[0]}
          </div>
          <div>
            <div style={{ fontSize: 14.5, fontWeight: 500 }}>{m.name}</div>
            <div className="eyebrow" style={{ marginTop: 4 }}>{m.role} · since {m.since}</div>
          </div>
          <div>
            <div className="eyebrow">Exposure</div>
            <div className="serif num" style={{ fontFamily: "var(--serif)", fontSize: 22, color: m.exposure > 100 ? "var(--accent)" : "var(--brand)" }}>
              {m.exposure}
            </div>
          </div>
          <div>
            <div className="eyebrow">Status</div>
            <div style={{ fontSize: 13.5, color: "var(--fg)", marginTop: 4 }}>{m.status}</div>
          </div>
          <button className="btn-link" style={{ fontSize: 12 }}>Open file →</button>
        </div>
      ))}
    </div>
  );
}

function OverviewSection() {
  const removed = BROKER_DATA.filter(b => b.status === "removed").length;
  const total = 95;
  const pct = Math.round((removed / total) * 100);

  return (
    <div className="page-fade">
      {/* Top metrics row */}
      <div className="dash-grid-top">
        <ExposureScoreCard />
        <MetricCard
          label="Brokers cleared"
          value={removed}
          suffix={`/ ${total}`}
          sub={`${pct}% of monitored set. Next sweep begins June 14.`}
        />
        <MetricCard
          label="Awaiting your attention"
          value="3"
          accent
          sub="Verifications Yew cannot click on your behalf. Steward will follow up by 6pm."
          lastBorder
        />
      </div>

      {/* Two-column work area */}
      <div className="dash-grid-work">
        {/* Activity */}
        <div style={{ padding: "32px 40px" }}>
          <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", marginBottom: 12 }}>
            <Eyebrow>Agent activity · today</Eyebrow>
            <button className="btn-link" style={{ fontSize: 12 }}>View full log →</button>
          </div>
          <div style={{ marginTop: 12 }}>
            {ACTIVITY.map((a, i) => (
              <ActivityItem key={i} a={a} last={i === ACTIVITY.length - 1} />
            ))}
          </div>
        </div>

        {/* Right column */}
        <div>
          <div style={{ padding: "32px 40px", borderBottom: "1px solid var(--rule)" }}>
            <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", marginBottom: 16 }}>
              <Eyebrow accent>Verifications · 3 open</Eyebrow>
              <button className="btn-link" style={{ fontSize: 12 }}>Open inbox →</button>
            </div>
            <Verifications />
          </div>

          <div style={{ padding: "32px 40px" }}>
            <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", marginBottom: 16 }}>
              <Eyebrow>Next quarterly run</Eyebrow>
              <span className="mono" style={{ fontSize: 11, color: "var(--muted)", letterSpacing: "0.06em" }}>
                JUN 14 · 28 DAYS
              </span>
            </div>
            <p className="serif" style={{ fontFamily: "var(--serif)", fontSize: 22, lineHeight: 1.3, letterSpacing: "-0.01em", margin: 0, color: "var(--fg)", textWrap: "balance" }}>
              Cycle 03 will re-submit removals across all <span style={{ color: "var(--brand)", fontWeight: 600 }}>{total}</span> brokers and audit for new aggregators added since March.
            </p>
            <div
              style={{
                marginTop: 24,
                display: "flex", alignItems: "center", gap: 18,
              }}
            >
              <div
                style={{
                  flex: 1, height: 4, background: "var(--rule)",
                  position: "relative", overflow: "hidden",
                }}
              >
                <div
                  style={{
                    position: "absolute", inset: 0,
                    width: `${pct}%`, background: "var(--brand)",
                  }}
                />
              </div>
              <span className="num" style={{ fontSize: 12, color: "var(--muted)" }}>
                Cycle 02 · {pct}% complete
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}

/* ---------- Brokers section ---------- */
function BrokersSection() {
  const [filter, setFilter] = useState("all");
  const filtered = BROKER_DATA.filter(b => filter === "all" || b.status === filter);
  const counts = {
    all: BROKER_DATA.length,
    removed: BROKER_DATA.filter(b => b.status === "removed").length,
    pending: BROKER_DATA.filter(b => b.status === "pending").length,
    verifying: BROKER_DATA.filter(b => b.status === "verifying").length,
  };
  const tabs = [
    { id: "all", label: "All visible" },
    { id: "removed", label: "Removed" },
    { id: "pending", label: "Awaiting" },
    { id: "verifying", label: "Verifying" },
  ];
  return (
    <div className="page-fade" style={{ padding: "32px 40px" }}>
      <div style={{ display: "flex", alignItems: "center", gap: 32, borderBottom: "1px solid var(--rule)", marginBottom: 18 }}>
        {tabs.map((t) => {
          const active = filter === t.id;
          return (
            <button
              key={t.id}
              onClick={() => setFilter(t.id)}
              style={{
                background: "transparent", border: 0, padding: "12px 0",
                cursor: "pointer",
                color: active ? "var(--fg)" : "var(--muted)",
                fontSize: 14, fontWeight: active ? 500 : 400,
                borderBottom: active ? "2px solid var(--brand)" : "2px solid transparent",
                marginBottom: -1,
                display: "inline-flex", alignItems: "center", gap: 8,
                fontFamily: "var(--sans)",
              }}
            >
              {t.label}
              <span
                className="num"
                style={{
                  fontSize: 11, color: active ? "var(--accent)" : "var(--stone-soft)",
                  fontVariantNumeric: "tabular-nums",
                }}
              >
                {counts[t.id]}
              </span>
            </button>
          );
        })}
      </div>

      <div
        style={{
          display: "grid",
          gridTemplateColumns: "1.6fr 1fr 1fr 1fr auto",
          gap: 16,
          padding: "10px 0",
        }}
      >
        {["Broker", "Category", "Status", "Last action", ""].map((h) => (
          <div key={h} className="eyebrow">{h}</div>
        ))}
      </div>
      <div style={{ borderTop: "1px solid var(--rule)" }}>
        {filtered.map((b, i) => (
          <BrokerRow key={b.name} b={b} last={i === filtered.length - 1} />
        ))}
      </div>

      <div
        style={{
          marginTop: 28,
          padding: "20px 24px",
          borderTop: "1px solid var(--rule)",
          background: "var(--bg-elev)",
          display: "flex", alignItems: "center", justifyContent: "space-between",
        }}
      >
        <div style={{ fontSize: 13, color: "var(--muted)" }}>
          Showing {filtered.length} of {BROKER_DATA.length} active brokers ·
          <span style={{ fontStyle: "italic", fontFamily: "var(--serif)" }}> {95 - BROKER_DATA.length} additional</span> below the visibility threshold.
        </div>
        <button className="btn-link" style={{ fontSize: 13 }}>Download CSV →</button>
      </div>
    </div>
  );
}

function HouseholdSection() {
  return (
    <div className="page-fade" style={{ padding: "32px 40px" }}>
      <div style={{ display: "grid", gridTemplateColumns: "1fr auto", alignItems: "end", marginBottom: 24 }}>
        <div>
          <Eyebrow>Household coverage</Eyebrow>
          <p style={{ marginTop: 10, fontSize: 14.5, color: "var(--muted)", maxWidth: 520, lineHeight: 1.55 }}>
            Four protected identities under one engagement. Add up to one more under the current tier; beyond five, you'll move to the Office plan.
          </p>
        </div>
        <button className="btn btn-ghost" style={{ padding: "10px 16px", fontSize: 13 }}>
          + Add member
        </button>
      </div>
      <Household />
    </div>
  );
}

function ActivitySection() {
  return (
    <div className="page-fade" style={{ padding: "32px 40px" }}>
      <Eyebrow>Full agent log</Eyebrow>
      <p style={{ marginTop: 10, fontSize: 14, color: "var(--muted)", maxWidth: 580 }}>
        Every action the agent takes is recorded here, plain. Each row links to a signed receipt.
      </p>
      <div style={{ marginTop: 28, borderTop: "1px solid var(--rule)" }}>
        {[...ACTIVITY, ...ACTIVITY].map((a, i) => (
          <ActivityItem key={i} a={a} last={i === ACTIVITY.length * 2 - 1} />
        ))}
      </div>
    </div>
  );
}

function VerificationsSection() {
  return (
    <div className="page-fade" style={{ padding: "32px 40px", maxWidth: 920 }}>
      <Eyebrow accent>Verifications · 3 open</Eyebrow>
      <p style={{ marginTop: 10, fontSize: 14, color: "var(--muted)", maxWidth: 580 }}>
        These items require a human signal. Yew flags only the actions an automated agent should not take.
      </p>
      <div style={{ marginTop: 28 }}>
        <Verifications />
      </div>
    </div>
  );
}

function ReportsSection() {
  const reports = [
    { q: "Cycle 02", date: "May 17, 2026", state: "In progress", removed: 14, found: 23 },
    { q: "Cycle 01", date: "Mar 14, 2026", state: "Complete",    removed: 71, found: 412 },
  ];
  return (
    <div className="page-fade" style={{ padding: "32px 40px" }}>
      <Eyebrow>Quarterly reports</Eyebrow>
      <p style={{ marginTop: 10, fontSize: 14, color: "var(--muted)", maxWidth: 580 }}>
        A single written briefing each quarter. Signed by your steward.
      </p>
      <div style={{ marginTop: 32, borderTop: "1px solid var(--rule)" }}>
        {reports.map((r, i) => (
          <div
            key={r.q}
            style={{
              padding: "28px 0",
              borderBottom: "1px solid var(--rule)",
              display: "grid", gridTemplateColumns: "1fr 1fr 1fr 1fr auto",
              gap: 24, alignItems: "center",
            }}
          >
            <div>
              <div className="serif" style={{ fontFamily: "var(--serif)", fontSize: 24, fontWeight: 500 }}>{r.q}</div>
              <div className="eyebrow" style={{ marginTop: 6 }}>{r.date}</div>
            </div>
            <div>
              <div className="eyebrow">State</div>
              <div style={{ fontSize: 14, marginTop: 6, color: r.state === "Complete" ? "var(--fg)" : "var(--accent)" }}>{r.state}</div>
            </div>
            <div>
              <div className="eyebrow">Records found</div>
              <div className="serif num" style={{ fontFamily: "var(--serif)", fontSize: 22, color: "var(--fg)" }}>{r.found}</div>
            </div>
            <div>
              <div className="eyebrow">Records removed</div>
              <div className="serif num" style={{ fontFamily: "var(--serif)", fontSize: 22, color: "var(--brand)" }}>{r.removed}</div>
            </div>
            <button className="btn-ghost btn" style={{ padding: "8px 14px", fontSize: 12 }}>
              Open PDF
            </button>
          </div>
        ))}
      </div>
    </div>
  );
}

function SettingsSection() {
  return (
    <div className="page-fade" style={{ padding: "32px 40px", maxWidth: 720 }}>
      <Eyebrow>Engagement</Eyebrow>
      <div style={{ marginTop: 28, borderTop: "1px solid var(--rule)" }}>
        {[
          ["Plan", "Household · $2,400 / year"],
          ["Renewal", "March 14, 2027"],
          ["Steward", "Theo Ainsworth — theo@yew.ai"],
          ["Notification cadence", "Quarterly written briefing"],
          ["Notarization on file", "Yes · valid through Mar 2028"],
          ["Postal address for letters", "Yew Suite 14 · PO Box 1862 · Hanover, NH"],
        ].map(([k, v]) => (
          <div
            key={k}
            style={{
              display: "grid", gridTemplateColumns: "200px 1fr auto", gap: 16,
              padding: "18px 0", borderBottom: "1px solid var(--rule)",
              alignItems: "center",
            }}
          >
            <div className="eyebrow">{k}</div>
            <div style={{ fontSize: 14, color: "var(--fg)" }}>{v}</div>
            <button className="btn-link" style={{ fontSize: 12, color: "var(--muted)" }}>Edit</button>
          </div>
        ))}
      </div>
    </div>
  );
}

/* ---------- Dashboard root ---------- */
function Dashboard({ onSignOut }) {
  const [section, setSection] = useState("overview");
  const content = {
    overview: <OverviewSection />,
    brokers: <BrokersSection />,
    activity: <ActivitySection />,
    verify: <VerificationsSection />,
    household: <HouseholdSection />,
    reports: <ReportsSection />,
    settings: <SettingsSection />,
  }[section];
  return (
    <div
      data-screen-label="02 Dashboard"
      data-om-validate
      style={{ display: "grid", gridTemplateColumns: "240px 1fr", minHeight: "100vh", background: "var(--bg)" }}
    >
      <Sidebar section={section} setSection={setSection} onSignOut={onSignOut} />
      <main style={{ minWidth: 0 }}>
        <DashTopBar section={section} />
        {content}
      </main>
    </div>
  );
}

Object.assign(window, { Dashboard });
