// dashboard.jsx — the actual product, post-login

const { useState: useStateDb, useEffect: useEffectDb, useMemo: useMemoDb } = React;
const parseDateFn = window.parseDate;
const addDaysFn = window.addDays;

function Dashboard({ mode, setMode, onBack }) {
  const m = MODES[mode];
  const initial = useMemoDb(() => READINESS[mode].map(r => ({...r})), [mode]);
  const [rows, setRows] = useStateDb(initial);
  useEffectDb(() => { setRows(initial); }, [initial]);

  const baseDate = useMemoDb(() => parseDateFn(m.interception), [mode]);
  const totalNonGreen = initial.filter(r => r.state !== "green").length;
  const remaining = rows.filter(r => r.state !== "green").length;
  const cleared = totalNonGreen - remaining;
  const computedDate = useMemoDb(() => addDaysFn(baseDate, -cleared * 9), [baseDate, cleared]);
  const greenCount = rows.filter(r => r.state === "green").length;
  const pct = Math.round((greenCount / rows.length) * 100);

  function clearRow(i) {
    setRows(rs => rs.map((r, idx) => idx === i ? {...r, state: "green"} : r));
  }
  function resetAll() { setRows(initial); }

  return (
    <div data-screen-label="Dashboard" style={{ minHeight: "100vh", display: "grid", gridTemplateColumns: "232px 1fr", background: "var(--canvas)" }}>
      <DashSidebar mode={mode} setMode={setMode} onBack={onBack}/>
      <main style={{ minWidth: 0 }}>
        <DashTopBar mode={mode}/>

        <div style={{ padding: "32px 40px 80px", maxWidth: 1240, margin: "0 auto" }}>
          {/* Greeting + Goal */}
          <GoalHeader mode={mode} pct={pct} computedDate={computedDate} m={m}/>

          {/* Today's one thing + Live agent activity */}
          <div style={{ display: "grid", gridTemplateColumns: "1.4fr 1fr", gap: 16, marginBottom: 24 }}>
            <TodayCard mode={mode}/>
            <AgentActivity/>
          </div>

          {/* Readiness — the full matrix */}
          <ReadinessFull rows={rows} clearRow={clearRow} resetAll={resetAll} cleared={cleared}/>

          {/* Money + Properties side-by-side */}
          <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 16, marginTop: 24 }}>
            <MoneyPanel mode={mode}/>
            <MortgageStack mode={mode}/>
          </div>

          {/* Properties scored */}
          <PropertyShortlist mode={mode}/>

          {/* Agent chat */}
          <AgentChat mode={mode}/>
        </div>
      </main>
    </div>
  );
}

// ── Sidebar ─────────────────────────────────────────────────────────────
function DashSidebar({ mode, setMode, onBack }) {
  const m = MODES[mode];
  const nav = [
    { icon: "target", label: "Mission",     badge: null, active: true },
    { icon: "bldg",   label: "Properties",  badge: "23 new" },
    { icon: "euro",   label: "Money",       badge: null },
    { icon: "file",   label: "Paperwork",   badge: "3" },
    { icon: "chat",   label: "Ask the agent" },
    { icon: "calendar", label: "Viewings", badge: null },
    { icon: "bell", label: "Alerts", badge: "12" },
  ];
  return (
    <aside style={{
      background: "var(--surface-2)",
      borderRight: "0.5px solid var(--border)",
      padding: "20px 14px",
      position: "sticky", top: 0,
      height: "100vh",
      display: "flex", flexDirection: "column",
    }}>
      <div style={{ display: "flex", alignItems: "center", gap: 9, padding: "4px 8px 18px" }}>
        <svg width="20" height="20" viewBox="0 0 24 24"><path d="M4 11.5L12 4l8 7.5V20a1 1 0 01-1 1h-5v-6h-4v6H5a1 1 0 01-1-1z" fill="var(--ink)"/><circle cx="12" cy="13" r="2.2" fill="var(--canvas)"/></svg>
        <span style={{ fontSize: 15, fontWeight: 600, letterSpacing: "-0.01em" }}>HomeWoosh</span>
      </div>

      {/* Mode mini-switch */}
      <div style={{ padding: "10px 10px 14px", marginBottom: 8, background: "var(--surface)", borderRadius: 10, border: "0.5px solid var(--border)" }}>
        <div className="eyebrow" style={{ fontSize: 10, marginBottom: 8 }}>You are</div>
        <select
          value={mode}
          onChange={e => setMode(e.target.value)}
          style={{
            width: "100%", appearance: "none",
            background: "transparent", border: 0,
            fontSize: 13.5, color: "var(--ink)", fontWeight: 500,
            padding: 0, outline: "none",
          }}>
          {Object.values(MODES).map(mo => (
            <option key={mo.key} value={mo.key}>{mo.label}</option>
          ))}
        </select>
        <div style={{ fontSize: 11.5, color: "var(--ink-3)", marginTop: 4 }}>{m.goalShort}</div>
      </div>

      <nav style={{ display: "flex", flexDirection: "column", gap: 1, flex: 1 }}>
        {nav.map((n, i) => (
          <button key={i} style={{
            display: "flex", alignItems: "center", gap: 11,
            padding: "9px 10px",
            background: n.active ? "var(--ink)" : "transparent",
            color: n.active ? "var(--canvas)" : "var(--ink-2)",
            border: 0, borderRadius: 8,
            fontSize: 13.5, fontWeight: n.active ? 500 : 400,
            textAlign: "left",
          }}>
            <Icon name={n.icon} size={15}/>
            <span style={{ flex: 1 }}>{n.label}</span>
            {n.badge && (
              <span style={{
                fontSize: 10, padding: "1px 6px", borderRadius: 999,
                background: n.active ? "rgba(247,244,238,0.18)" : "var(--soft)",
                color: n.active ? "var(--canvas)" : "var(--ink-3)",
              }}>{n.badge}</span>
            )}
          </button>
        ))}
      </nav>

      <div style={{ marginTop: 16, padding: "12px 10px", background: "var(--surface)", borderRadius: 10, border: "0.5px solid var(--border)" }}>
        <div style={{ display: "flex", alignItems: "center", gap: 10, marginBottom: 8 }}>
          <div style={{ width: 28, height: 28, borderRadius: 999, background: "var(--green-soft)", color: "var(--green-2)", display:"inline-flex", alignItems:"center", justifyContent:"center", fontSize: 12, fontWeight: 600 }}>AN</div>
          <div style={{ minWidth: 0 }}>
            <div style={{ fontSize: 12.5, fontWeight: 500, whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }}>Aoife Brennan</div>
            <div style={{ fontSize: 10.5, color: "var(--ink-3)" }}>Buyer · day 47</div>
          </div>
        </div>
        <button onClick={onBack} style={{ width: "100%", padding: "6px", borderRadius: 6, background: "transparent", border: "0.5px solid var(--border)", fontSize: 11.5, color: "var(--ink-3)" }}>
          ← Back to homepage
        </button>
      </div>
    </aside>
  );
}

// ── Top bar ─────────────────────────────────────────────────────────────
function DashTopBar({ mode }) {
  return (
    <div style={{
      padding: "14px 40px",
      borderBottom: "0.5px solid var(--border)",
      background: "rgba(247,244,238,0.78)",
      backdropFilter: "blur(12px)",
      position: "sticky", top: 0, zIndex: 30,
      display: "flex", alignItems: "center", justifyContent: "space-between", gap: 24,
    }}>
      <div style={{
        flex: 1, maxWidth: 480,
        display: "flex", alignItems: "center", gap: 10,
        padding: "8px 12px",
        background: "var(--surface)", border: "0.5px solid var(--border)", borderRadius: 10,
      }}>
        <Icon name="sparkles" size={15} style={{ color: "var(--green-2)" }}/>
        <input
          placeholder="Ask the agent — 'when can I afford a 3-bed in Bray?'"
          style={{ flex: 1, background: "transparent", border: 0, outline: "none", fontSize: 13, color: "var(--ink)" }}
        />
        <span className="mono" style={{ fontSize: 10, padding: "2px 6px", background: "var(--soft)", borderRadius: 4, color: "var(--ink-3)" }}>⌘ K</span>
      </div>

      <div style={{ display: "flex", alignItems: "center", gap: 12 }}>
        <span className="tag" style={{ padding: "4px 9px" }}>
          <span style={{ width: 6, height: 6, borderRadius: 999, background: "var(--green)", animation: "pulse-soft 2s infinite" }}/>
          Agent active · scanning 47 sources
        </span>
        <button style={{ width: 34, height: 34, background: "var(--surface)", border: "0.5px solid var(--border)", borderRadius: 10, color: "var(--ink-2)" }}>
          <Icon name="bell" size={15}/>
        </button>
      </div>
    </div>
  );
}

// ── Goal header ─────────────────────────────────────────────────────────
function GoalHeader({ mode, pct, computedDate, m }) {
  const months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
  const today = new Date(2026, 4, 24);
  const daysToGo = Math.round((computedDate - today) / (1000*60*60*24));
  const monthsToGo = (daysToGo / 30.4).toFixed(1);
  return (
    <div style={{
      background: "var(--ink)",
      color: "var(--canvas)",
      borderRadius: 18,
      padding: 32,
      marginBottom: 24,
      position: "relative",
      overflow: "hidden",
    }}>
      <div style={{ display: "grid", gridTemplateColumns: "1.5fr 1fr 1fr", gap: 32, alignItems: "center", position: "relative", zIndex: 2 }}>
        <div>
          <div className="eyebrow" style={{ color: "rgba(247,244,238,0.55)" }}>Good morning, Aoife</div>
          <div style={{ fontSize: 22, fontWeight: 500, marginTop: 8, letterSpacing: "-0.01em" }}>
            {m.goalShort}
          </div>
          <div style={{ fontSize: 13.5, color: "rgba(247,244,238,0.65)", marginTop: 4 }}>
            Target: {m.goalDate} · day 47 of your mission
          </div>
          {/* Progress bar */}
          <div style={{ marginTop: 18, display: "flex", alignItems: "center", gap: 12 }}>
            <div style={{ flex: 1, height: 5, background: "rgba(247,244,238,0.14)", borderRadius: 999, overflow: "hidden" }}>
              <div style={{ height: "100%", width: `${pct}%`, background: "var(--clay-soft)", transition: "width .5s cubic-bezier(.4,.0,.2,1)" }}/>
            </div>
            <span className="mono tnum" style={{ fontSize: 12 }}>{pct}%</span>
          </div>
        </div>

        <div>
          <div className="eyebrow" style={{ color: "rgba(247,244,238,0.55)" }}>{m.metricLabel}</div>
          <div className="display tnum" style={{ fontSize: 44, marginTop: 8, color: "var(--clay-soft)", letterSpacing: "-0.02em" }}>
            {months[computedDate.getMonth()]} {String(computedDate.getDate()).padStart(2,"0")}
          </div>
          <div style={{ fontSize: 13, color: "rgba(247,244,238,0.65)", marginTop: 2 }}>
            {computedDate.getFullYear()} · {monthsToGo} months to go
          </div>
        </div>

        <div>
          <div className="eyebrow" style={{ color: "rgba(247,244,238,0.55)" }}>What's working</div>
          <div style={{ marginTop: 10, display: "flex", flexDirection: "column", gap: 7, fontSize: 13 }}>
            <div style={{ display: "flex", gap: 8, alignItems: "center" }}>
              <Icon name="arrowUR" size={13} style={{color:"var(--clay-soft)"}}/>
              <span>Savings rate up <b className="tnum">€140/mo</b></span>
            </div>
            <div style={{ display: "flex", gap: 8, alignItems: "center" }}>
              <Icon name="arrowUR" size={13} style={{color:"var(--clay-soft)"}}/>
              <span>3 viewings booked this week</span>
            </div>
            <div style={{ display: "flex", gap: 8, alignItems: "center" }}>
              <Icon name="arrowUR" size={13} style={{color:"var(--clay-soft)"}}/>
              <span>Agent on a 14-day check-in streak</span>
            </div>
          </div>
        </div>
      </div>
      <div style={{
        position: "absolute", right: -100, top: -100, width: 360, height: 360,
        background: "radial-gradient(circle, rgba(199,107,60,0.18), transparent 65%)",
        pointerEvents: "none",
      }}/>
    </div>
  );
}

// ── Today's one thing ──────────────────────────────────────────────────
function TodayCard({ mode }) {
  const m = MODES[mode];
  return (
    <div style={{ background: "var(--surface)", border: "0.5px solid var(--border)", borderRadius: 14, padding: 22 }}>
      <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", marginBottom: 14 }}>
        <span className="eyebrow" style={{ color: "var(--green)" }}><Icon name="bolt" size={11} style={{verticalAlign:-1, marginRight:5}}/>Today's one thing</span>
        <span className="mono" style={{ fontSize: 10.5, color: "var(--ink-3)" }}>updated 06:14</span>
      </div>
      <div style={{ fontSize: 18, lineHeight: 1.45, color: "var(--ink)", marginBottom: 16, letterSpacing: "-0.005em" }}>
        {m.todayThing}
      </div>
      <div style={{ display: "flex", gap: 8 }}>
        <button style={{ background: "var(--ink)", color: "var(--canvas)", border: 0, padding: "9px 14px", borderRadius: 8, fontSize: 13, fontWeight: 500 }}>Do it now</button>
        <button style={{ background: "transparent", color: "var(--ink-2)", border: "0.5px solid var(--border)", padding: "8px 14px", borderRadius: 8, fontSize: 13 }}>Remind me at 6pm</button>
        <button style={{ background: "transparent", color: "var(--ink-3)", border: 0, padding: "8px 10px", fontSize: 13 }}>Snooze</button>
      </div>
      <div style={{ marginTop: 18, padding: "10px 12px", background: "var(--surface-2)", borderRadius: 8, fontSize: 12, color: "var(--ink-3)", fontFamily: "var(--font-mono)", lineHeight: 1.5 }}>
        → Why? You're 1 cleared red away from a 2-week date pull-forward. The agent ranks this above 18 other possible actions today.
      </div>
    </div>
  );
}

// ── Agent activity feed ─────────────────────────────────────────────────
function AgentActivity() {
  const items = [
    { time: "06:14", text: "Scanned 47 sources — 2 new listings match", tone: "info" },
    { time: "05:51", text: "AIB cut variable rates 0.15% — saved €38/mo", tone: "green" },
    { time: "Tue", text: "Drafted reference reminder to Murphy & Co.", tone: "info" },
    { time: "Tue", text: "Spotted illegal RPZ uplift at 4 Glandore Park", tone: "rose" },
    { time: "Mon", text: "Filed your HTB application with Revenue", tone: "green" },
  ];
  const colorFor = t => t === "green" ? "var(--green)" : t === "rose" ? "var(--rose)" : "var(--ink-3)";
  return (
    <div style={{ background: "var(--surface)", border: "0.5px solid var(--border)", borderRadius: 14, padding: 22 }}>
      <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", marginBottom: 14 }}>
        <span className="eyebrow"><Icon name="sparkles" size={11} style={{verticalAlign:-1, marginRight:5}}/>Agent activity</span>
        <a style={{ fontSize: 11.5, color: "var(--ink-3)" }}>See all →</a>
      </div>
      <div style={{ display: "flex", flexDirection: "column", gap: 11 }}>
        {items.map((it, i) => (
          <div key={i} style={{ display: "flex", alignItems: "flex-start", gap: 11, fontSize: 13, lineHeight: 1.4 }}>
            <span className="mono tnum" style={{ fontSize: 10.5, color: "var(--ink-3)", minWidth: 32, marginTop: 2 }}>{it.time}</span>
            <span style={{ width: 6, height: 6, borderRadius: 999, background: colorFor(it.tone), marginTop: 7, flexShrink: 0 }}/>
            <span style={{ color: "var(--ink-2)" }}>{it.text}</span>
          </div>
        ))}
      </div>
    </div>
  );
}

// ── Full readiness matrix ───────────────────────────────────────────────
function ReadinessFull({ rows, clearRow, resetAll, cleared }) {
  return (
    <div style={{ background: "var(--surface)", border: "0.5px solid var(--border)", borderRadius: 14, padding: 4 }}>
      <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", padding: "16px 20px 14px", borderBottom: "0.5px solid var(--border)" }}>
        <div style={{ display: "flex", alignItems: "center", gap: 14 }}>
          <span className="eyebrow">Readiness matrix</span>
          <span style={{ fontSize: 12, color: "var(--ink-3)" }}>
            <span style={{ color: "var(--green-2)" }}>{rows.filter(r=>r.state==="green").length} done</span> ·{" "}
            <span style={{ color: "var(--clay)" }}>{rows.filter(r=>r.state==="yellow").length} to fix</span> ·{" "}
            <span style={{ color: "var(--rose)" }}>{rows.filter(r=>r.state==="red").length} blocking</span>
          </span>
        </div>
        <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
          {cleared > 0 && <span style={{ fontSize: 11.5, color: "var(--green-2)" }}><Icon name="arrowUR" size={12} style={{verticalAlign:-2}}/> {cleared * 9} days earlier</span>}
          <button onClick={resetAll} style={{ background: "transparent", border: 0, color: "var(--ink-3)", fontSize: 11.5, display: "flex", alignItems: "center", gap: 4 }}>
            <Icon name="refresh" size={12}/> Reset
          </button>
        </div>
      </div>

      <div style={{ padding: 6 }}>
        {rows.map((r, i) => (
          <div key={r.id}
            className={`vital ${r.state} ${r.state !== "green" ? "clickable" : ""}`}
            onClick={() => r.state !== "green" && clearRow(i)}
          >
            <span className="vdot"/>
            <span style={{ fontSize: 14, fontWeight: 500, color: "var(--ink)" }}>{r.label}</span>
            <span style={{ fontSize: 12.5, color: "var(--ink-2)", textWrap: "pretty" }}>{r.detail}</span>
            <span style={{
              fontSize: 10, fontWeight: 600, letterSpacing: ".08em", textTransform: "uppercase", textAlign: "right",
              color: r.state === "green" ? "var(--green-2)" : r.state === "yellow" ? "#7A3A14" : "#6E2118",
            }}>
              {r.state === "green" ? "✓ Done" : r.state === "yellow" ? "Fix" : "Block"}
            </span>
            <span style={{ fontSize: 12, color: "var(--ink-3)", display: "flex", alignItems: "center", gap: 6, justifyContent: "flex-end" }}>
              {r.state !== "green" && r.action ? <><span style={{ color: "var(--ink-2)", fontWeight: 500 }}>{r.action}</span><Icon name="chevR" size={12}/></> : <Icon name="chevR" size={12}/>}
            </span>
          </div>
        ))}
      </div>
    </div>
  );
}

// ── Money / financial mission control ───────────────────────────────────
function MoneyPanel({ mode }) {
  // mini bar chart of saving rate over 6 months
  const months = ["Dec", "Jan", "Feb", "Mar", "Apr", "May"];
  const savings = [420, 380, 510, 640, 720, 860];
  const max = 1000;
  return (
    <div style={{ background: "var(--surface)", border: "0.5px solid var(--border)", borderRadius: 14, padding: 22, minHeight: 280 }}>
      <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", marginBottom: 18 }}>
        <span className="eyebrow"><Icon name="euro" size={11} style={{verticalAlign:-1, marginRight:5}}/>Financial mission control</span>
        <span className="tag" style={{ background: "var(--green-soft)", color: "var(--green-2)", border: 0 }}>
          <span style={{ width: 6, height: 6, borderRadius: 999, background: "var(--green-2)" }}/>
          AIB · BOI · Revolut connected
        </span>
      </div>

      <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 18, marginBottom: 22 }}>
        <div>
          <div style={{ fontSize: 11, color: "var(--ink-3)", marginBottom: 4 }}>Net monthly</div>
          <div className="display tnum" style={{ fontSize: 28 }}>€4,820</div>
          <div style={{ fontSize: 11.5, color: "var(--green-2)" }}>+€140 vs last month</div>
        </div>
        <div>
          <div style={{ fontSize: 11, color: "var(--ink-3)", marginBottom: 4 }}>Saving rate</div>
          <div className="display tnum" style={{ fontSize: 28 }}>17.8%</div>
          <div style={{ fontSize: 11.5, color: "var(--green-2)" }}>on track for Aug 2027</div>
        </div>
      </div>

      <div style={{ display: "flex", alignItems: "flex-end", gap: 8, height: 100, marginBottom: 6 }}>
        {savings.map((v, i) => (
          <div key={i} style={{ flex: 1, display: "flex", flexDirection: "column", alignItems: "center", gap: 6, height: "100%", justifyContent: "flex-end" }}>
            <div className="tnum mono" style={{ fontSize: 9.5, color: "var(--ink-3)" }}>€{v}</div>
            <div style={{
              width: "100%",
              height: `${(v / max) * 100}%`,
              background: i === savings.length - 1 ? "var(--green)" : "var(--clay-soft)",
              border: i === savings.length - 1 ? "none" : "0.5px solid var(--clay)",
              borderRadius: 4,
              minHeight: 4,
              transition: "all .3s",
            }}/>
          </div>
        ))}
      </div>
      <div style={{ display: "flex", gap: 8 }}>
        {months.map((m, i) => (
          <div key={i} style={{ flex: 1, textAlign: "center", fontSize: 10.5, color: "var(--ink-3)" }}>{m}</div>
        ))}
      </div>

      <div style={{ marginTop: 18, padding: 12, background: "var(--surface-2)", borderRadius: 8, fontSize: 12.5, color: "var(--ink-2)", lineHeight: 1.5 }}>
        <b style={{ color: "var(--ink)" }}>Boost suggestion:</b> €340/mo on takeaways. Trim to €100 — interception date pulls forward 7 weeks.
      </div>
    </div>
  );
}

// ── Mortgage stack ─────────────────────────────────────────────────────
function MortgageStack({ mode }) {
  const lenders = [
    { name: "AIB", offer: 510, rate: "3.85%", status: "Active AIP", state: "green" },
    { name: "Bank of Ireland", offer: 495, rate: "3.95%", status: "Draft", state: "yellow" },
    { name: "Avant Money", offer: 525, rate: "3.65%", status: "Pre-match", state: "yellow" },
    { name: "Haven", offer: 502, rate: "3.80%", status: "Pre-match", state: "yellow" },
  ];
  return (
    <div style={{ background: "var(--surface)", border: "0.5px solid var(--border)", borderRadius: 14, padding: 22, minHeight: 280 }}>
      <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", marginBottom: 18 }}>
        <span className="eyebrow"><Icon name="bldg" size={11} style={{verticalAlign:-1, marginRight:5}}/>Mortgage stack</span>
        <a style={{ fontSize: 11.5, color: "var(--ink-3)" }}>Compare side-by-side →</a>
      </div>

      <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 14, marginBottom: 18 }}>
        <div style={{ padding: 14, background: "var(--green-soft)", borderRadius: 10 }}>
          <div style={{ fontSize: 10.5, letterSpacing: ".06em", textTransform: "uppercase", color: "var(--green-2)", marginBottom: 5 }}>Best offer</div>
          <div className="display tnum" style={{ fontSize: 28, color: "var(--green-2)" }}>€525k</div>
          <div style={{ fontSize: 11.5, color: "var(--green-2)" }}>Avant · 3.65% · 30y fixed</div>
        </div>
        <div style={{ padding: 14, background: "var(--surface-2)", borderRadius: 10 }}>
          <div style={{ fontSize: 10.5, letterSpacing: ".06em", textTransform: "uppercase", color: "var(--ink-3)", marginBottom: 5 }}>Monthly</div>
          <div className="display tnum" style={{ fontSize: 28 }}>€2,398</div>
          <div style={{ fontSize: 11.5, color: "var(--ink-3)" }}>at €495k borrowed</div>
        </div>
      </div>

      <div style={{ display: "flex", flexDirection: "column", gap: 6 }}>
        {lenders.map((l, i) => (
          <div key={i} style={{ display: "flex", alignItems: "center", justifyContent: "space-between", padding: "8px 12px", background: "var(--surface-2)", borderRadius: 8, fontSize: 12.5 }}>
            <span style={{ fontWeight: 500, minWidth: 100 }}>{l.name}</span>
            <span className="tnum mono" style={{ color: "var(--ink-2)" }}>€{l.offer}k</span>
            <span className="mono" style={{ color: "var(--ink-3)" }}>{l.rate}</span>
            <span style={{
              fontSize: 10.5, padding: "2px 7px", borderRadius: 999,
              background: l.state === "green" ? "var(--green-soft)" : "var(--clay-soft)",
              color: l.state === "green" ? "var(--green-2)" : "var(--clay)",
            }}>{l.status}</span>
          </div>
        ))}
      </div>
    </div>
  );
}

// ── Property shortlist ─────────────────────────────────────────────────
function PropertyShortlist({ mode }) {
  return (
    <div style={{ marginTop: 24 }}>
      <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", marginBottom: 14 }}>
        <span className="eyebrow">Shortlist · scored against you</span>
        <div style={{ display: "flex", gap: 10, fontSize: 12, color: "var(--ink-3)" }}>
          <span>3 active</span>
          <span>·</span>
          <span><span style={{ color: "var(--clay)" }}>1 sale-agreed</span> — watching for fall-through</span>
        </div>
      </div>
      <div style={{ display: "grid", gridTemplateColumns: "repeat(3, 1fr)", gap: 14 }}>
        {PROPERTIES.map(p => <PropertyCard key={p.id} p={p}/>)}
      </div>
    </div>
  );
}

// ── Agent chat (mini) ──────────────────────────────────────────────────
function AgentChat({ mode }) {
  const msgs = [
    { from: "agent", text: "Morning Aoife. Two new listings dropped overnight — 12 Beechwood Park in Bray scored 92. Want me to draft a viewing request?" },
    { from: "user", text: "What's the catch with Beechwood Park?" },
    { from: "agent", text: "Asking is 3% above sold comps in A98. Time on market: 3 days, so they may still get asking. If you want it, I'd suggest €485k as an opening bid — that's bang on the median for the postcode in the last 90 days." },
    { from: "user", text: "OK draft the viewing request" },
  ];
  return (
    <div style={{ marginTop: 32, background: "var(--ink)", color: "var(--canvas)", borderRadius: 16, padding: 28, position: "relative", overflow: "hidden" }}>
      <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", marginBottom: 22 }}>
        <div style={{ display: "flex", alignItems: "center", gap: 12 }}>
          <span style={{ width: 32, height: 32, borderRadius: 999, background: "var(--clay)", display: "inline-flex", alignItems: "center", justifyContent: "center" }}>
            <Icon name="sparkles" size={16} style={{ color: "var(--canvas)" }}/>
          </span>
          <div>
            <div style={{ fontSize: 14, fontWeight: 500 }}>Ask the agent</div>
            <div style={{ fontSize: 11.5, color: "rgba(247,244,238,0.55)" }}>Always on · trained on your goal &amp; finances</div>
          </div>
        </div>
        <a style={{ fontSize: 12, color: "rgba(247,244,238,0.6)" }}>Open full chat →</a>
      </div>

      <div style={{ display: "flex", flexDirection: "column", gap: 12, marginBottom: 18 }}>
        {msgs.map((mm, i) => (
          <div key={i} style={{ display: "flex", justifyContent: mm.from === "user" ? "flex-end" : "flex-start" }}>
            <div style={{
              maxWidth: "70%", fontSize: 13.5, lineHeight: 1.5,
              padding: "10px 14px", borderRadius: 12,
              background: mm.from === "user" ? "var(--clay)" : "rgba(247,244,238,0.08)",
              color: mm.from === "user" ? "var(--canvas)" : "var(--canvas)",
            }}>
              {mm.text}
            </div>
          </div>
        ))}
      </div>

      <div style={{
        background: "rgba(247,244,238,0.05)", border: "0.5px solid rgba(247,244,238,0.12)", borderRadius: 10,
        padding: "10px 14px", display: "flex", alignItems: "center", gap: 10,
      }}>
        <Icon name="chat" size={15} style={{ color: "rgba(247,244,238,0.5)" }}/>
        <input
          placeholder="Reply to the agent…"
          style={{ flex: 1, background: "transparent", border: 0, outline: "none", color: "var(--canvas)", fontSize: 13.5 }}
        />
        <span className="mono" style={{ fontSize: 10.5, color: "rgba(247,244,238,0.4)" }}>Enter</span>
      </div>
    </div>
  );
}

window.Dashboard = Dashboard;
