/* ===== Home dashboard ===== */
function ScreenHome({ go, openTxn }) {
  const nw = NETWORTH;
  const fiCorpus = nw.invest + nw.retirement;       // investible toward FI
  const fr = fireProject({ corpus:7900000, monthly:85000, returnPct:11, inflationPct:6, swrPct:3.2, annualExpense:TRACKED_ANNUAL });
  const fiTarget = fr.fiNumber;                       // Regular-FIRE number from tracked spend
  const fiPct = Math.min(1, fiCorpus / fiTarget);
  const recent = TXNS.slice(0, 4);
  const pstats = portfolioStats();
  const pscore = Math.max(40, Math.round(100 - pstats.regular.length*4 - pstats.laggards.length*5 - (Math.abs(pstats.drift.Equity)>5?8:0) - 6));
  const recover = pstats.annualLeak + pstats.taxSaved;

  return (
    <div className="scroll" style={{ padding:'4px 16px 96px' }}>
      {/* header */}
      <div className="row" style={{ justifyContent:'space-between', padding:'10px 2px 16px' }}>
        <div>
          <div style={{ fontSize:13, color:'var(--dim)' }}>Good evening,</div>
          <div style={{ fontSize:20, fontWeight:700, letterSpacing:'-.02em', display:'flex', alignItems:'baseline', gap:7, whiteSpace:'nowrap' }}>
            {USER.name}
            <span style={{ fontFamily:'var(--font-serif)', fontStyle:'italic', color:'var(--gold)', fontWeight:500, fontSize:16 }}>· MaxFi</span>
          </div>
        </div>
        <div className="row" style={{ gap:10 }}>
          <button style={iconBtn}><Icon name="bell" size={20} color="var(--dim)"/><i style={notifDot}/></button>
          <div style={{ width:38, height:38, borderRadius:'50%', background:'linear-gradient(150deg,var(--gold),var(--gold-deep))',
            display:'flex', alignItems:'center', justifyContent:'center', fontWeight:700, fontSize:14, color:'#1a1407' }}>{USER.initials}</div>
        </div>
      </div>

      {/* net worth hero */}
      <div className="card" style={{ padding:'18px 18px 16px', position:'relative', overflow:'hidden',
        background:'linear-gradient(160deg,#1a1d20,#121417 70%)' }}>
        <div style={{ position:'absolute', right:-40, top:-50, width:160, height:160, borderRadius:'50%', background:'var(--gold-glow)', filter:'blur(8px)' }}/>
        <div className="row" style={{ justifyContent:'space-between', alignItems:'flex-start' }}>
          <div>
            <div className="eyebrow">Net worth</div>
            <div className="serif tnum" style={{ fontSize:38, lineHeight:1.05, marginTop:8 }}>{fmtINR(nw.total,2)}</div>
            <div className="row" style={{ gap:6, marginTop:9 }}>
              <span className="tag" style={{ background:'var(--green-dim)', color:'var(--green)' }}>
                <Icon name="arrowUp" size={11} color="var(--green)"/> {fmtINR(nw.deltaMonth,2)} · {nw.deltaPct}%
              </span>
              <span style={{ fontSize:11.5, color:'var(--faint)' }}>this month</span>
            </div>
          </div>
          <Sparkline data={nw.trail} w={84} h={40} color="var(--gold)"/>
        </div>
        <div style={{ marginTop:18 }}>
          <SegmentBar cats={[
            { name:'Investments', val:nw.invest, color:'var(--gold)' },
            { name:'Retirement',  val:nw.retirement, color:'#7fa8d9' },
            { name:'Cash',        val:nw.cash, color:'#4f5a52' },
          ]} total={nw.total} />
          <div className="row" style={{ gap:16, marginTop:11 }}>
            <Legend c="var(--gold)" l="Investments" v={fmtINR(nw.invest,2)}/>
            <Legend c="#7fa8d9" l="Retirement" v={fmtINR(nw.retirement,2)}/>
            <Legend c="#4f5a52" l="Cash" v={fmtINR(nw.cash,1)}/>
          </div>
        </div>
      </div>

      {/* sync status */}
      <div className="row" style={{ gap:8, padding:'13px 4px 16px', justifyContent:'center' }}>
        <i className="pulse-dot"/>
        <span style={{ fontSize:11.5, color:'var(--dim)' }}>Auto-tracking SMS · 4 banks · synced 2 min ago</span>
      </div>

      {/* journey: Spend → Save → FI */}
      <SectionTitle>Your path to FI</SectionTitle>
      <div className="card" style={{ padding:'4px 2px', marginBottom:10 }}>
        <JourneyStep i={0} icon="receipt" accent="var(--gold)"
          label="Spend" metric={fmtINR(SPEND.spent,2)} metricSub="this month"
          hook="₹612 left on table" onClick={()=>go('spend')}/>
        <JourneyStep i={1} icon="pie" accent="#7fa8d9"
          label="Save" metric={fmtINR(pstats.current,2)} metricSub="invested"
          hook={`Health ${pscore} · ₹${Math.round(recover/1000)}k/yr to recover`} onClick={()=>go('save')}/>
        <JourneyStep i={2} last icon="target" accent="var(--gold-bright)"
          label="Financial independence" metric={Math.round(fiPct*100)+'%'} metricSub={`${fr.years} yrs to go`}
          hook={`Regular FIRE · age ${USER.age+fr.years}`} onClick={()=>go('fi')}/>
      </div>

      {/* recent activity */}
      <SectionTitle action="All" onAction={()=>go('spend')}>Recent activity</SectionTitle>
      <div className="card" style={{ overflow:'hidden' }}>
        {recent.map((t,i)=>(
          <TxnRow key={t.id} t={t} divider={i<recent.length-1} onClick={()=>openTxn(t)}/>
        ))}
      </div>
    </div>
  );
}

function JourneyStep({ i, last, icon, accent, label, metric, metricSub, hook, onClick }) {
  const c = 21; // node-centre offset from row top
  return (
    <button onClick={onClick} style={{ ...plainBtn, width:'100%' }}>
      <div style={{ display:'flex', gap:13, padding:'2px 14px 2px 12px', textAlign:'left' }}>
        {/* continuous spine + node */}
        <div style={{ position:'relative', width:36, flexShrink:0 }}>
          <div style={{ position:'absolute', left:'50%', transform:'translateX(-50%)', width:2,
            top: i===0 ? c : 0, bottom: last ? `calc(100% - ${c}px)` : 0,
            background:'linear-gradient(var(--line-2),var(--line-2))' }}/>
          <div style={{ position:'relative', zIndex:1, marginTop:c-18, width:36, height:36, borderRadius:'50%',
            background:accent+'1c', border:`1.5px solid ${accent}66`,
            display:'flex', alignItems:'center', justifyContent:'center' }}>
            <Icon name={icon} size={17} color={accent}/>
          </div>
        </div>
        {/* content */}
        <div style={{ flex:1, minWidth:0, padding:'12px 0 16px' }}>
          <div className="row" style={{ justifyContent:'space-between', alignItems:'center', gap:8 }}>
            <span className="eyebrow">{label}</span>
            <Icon name="chevR" size={14} color="var(--faint)"/>
          </div>
          <div className="row" style={{ alignItems:'baseline', gap:7, marginTop:6, flexWrap:'nowrap' }}>
            <span className="serif tnum" style={{ fontSize:23, whiteSpace:'nowrap' }}>{metric}</span>
            <span style={{ fontSize:11, color:'var(--dim)', whiteSpace:'nowrap' }}>{metricSub}</span>
          </div>
          <div style={{ display:'inline-flex', alignItems:'center', gap:6, marginTop:10, padding:'5px 10px', borderRadius:8,
            background:'rgba(212,175,92,.12)', border:'1px solid rgba(212,175,92,.22)' }}>
            <Icon name="bolt" size={12} color="var(--gold)"/>
            <span style={{ fontSize:11, color:'var(--gold-bright)', fontWeight:600, whiteSpace:'nowrap' }}>{hook}</span>
          </div>
        </div>
      </div>
    </button>
  );
}

function Legend({ c, l, v }) {
  return (
    <div className="row" style={{ gap:6 }}>
      <i style={{ width:8, height:8, borderRadius:2.5, background:c, flexShrink:0 }}/>
      <div style={{ lineHeight:1.15 }}>
        <div style={{ fontSize:10, color:'var(--faint)', whiteSpace:'nowrap' }}>{l}</div>
        <div className="tnum" style={{ fontSize:12, fontWeight:700, whiteSpace:'nowrap' }}>{v}</div>
      </div>
    </div>
  );
}

function TxnRow({ t, divider, onClick }) {
  return (
    <button onClick={onClick} style={{ ...plainBtn, width:'100%' }}>
      <div className="row" style={{ gap:12, padding:'12px 14px', textAlign:'left',
        borderBottom:divider?'1px solid var(--line)':'none' }}>
        <MerchantBadge name={t.merchant}/>
        <div style={{ flex:1, minWidth:0 }}>
          <div className="row" style={{ gap:6 }}>
            <span style={{ fontSize:14, fontWeight:600, whiteSpace:'nowrap', overflow:'hidden', textOverflow:'ellipsis' }}>{t.merchant}</span>
            {t.missed && <span style={{ width:6, height:6, borderRadius:'50%', background:'var(--gold)', flexShrink:0 }}/>}
          </div>
          <div className="row" style={{ gap:6, marginTop:2 }}>
            <span style={{ fontSize:11, color:'var(--faint)' }}>{t.cat}</span>
            <span style={{ fontSize:11, color:'var(--faint)' }}>·</span>
            <span className="tag" style={{ background:'rgba(127,168,217,.12)', color:'#7fa8d9', padding:'1px 5px' }}><Icon name="sms" size={9} color="#7fa8d9"/>SMS</span>
          </div>
        </div>
        <div style={{ textAlign:'right' }}>
          <div className="tnum" style={{ fontSize:14.5, fontWeight:700 }}>−{fmtINR(t.amt,0).replace('₹','₹')}</div>
          <div style={{ fontSize:10.5, color:'var(--faint)', marginTop:2 }}>{t.time.split(' · ')[1]||t.time}</div>
        </div>
      </div>
    </button>
  );
}

const iconBtn = { position:'relative', width:38, height:38, borderRadius:'50%', background:'var(--surface)', border:'1px solid var(--line)', display:'flex', alignItems:'center', justifyContent:'center', cursor:'pointer' };
const notifDot = { position:'absolute', top:9, right:10, width:7, height:7, borderRadius:'50%', background:'var(--gold)', border:'2px solid var(--surface)' };
const plainBtn = { background:'none', border:0, padding:0, cursor:'pointer', display:'block', fontFamily:'var(--font-sans)', color:'inherit' };

Object.assign(window, { ScreenHome, JourneyStep, TxnRow, Legend, plainBtn });
