/* Tab 3: Financial Details */
function TabFinancial({ c, mode }) {
  return (
    <div className="l-split" style={{display:"grid", gridTemplateColumns:"minmax(0,1.4fr) minmax(0,1fr)", gap:12}}>
      <div style={{display:"flex", flexDirection:"column", gap:12}}>
        <Card title="Income & Capacity" tight>
          <div className="l-stats" style={{display:"grid", gridTemplateColumns:"repeat(4, 1fr)", gap:14}}>
            <Stat label="Annual income" value={fmtINR(c.annual_income,{compact:true})} sub={c.income_band+" band"}/>
            <Stat label="Monthly income" value={fmtINR(c.monthly_income,{compact:true})}/>
            <Stat label="Current EMI" value={fmtINR(c.total_emi||0,{compact:true})} sub={mode==="ETB"?"All active loans":"No current EMI"}/>
            <Stat label="Disposable (est.)" value={fmtINR((c.monthly_income||0)-(c.total_emi||0),{compact:true})} sub={fmtPct(1-(c.foir||c.foir_estimated||0),0)+" of income"}/>
          </div>
          <div className="hairline" style={{margin:"12px 0"}}/>
          <div className="l-pair" style={{display:"grid", gridTemplateColumns:"repeat(2, minmax(0,1fr))", gap:14}}>
            <RingChart value={c.foir || c.foir_estimated || 0} max={1} color="var(--ring-1)" label={mode==="ETB"?"FOIR (current)":"FOIR (estimated post-disbursal)"} sublabel={(c.foir_band||"—")+" band"}
              tone={(c.foir||c.foir_estimated) < 0.3 ? {severity:"POSITIVE", label:"Headroom"} : (c.foir||c.foir_estimated) < 0.5 ? {severity:"INFO", label:"OK"} : {severity:"WARNING", label:"Stretched"}} />
            <RingChart value={c.utilization_ratio || 0} max={1} color="var(--ring-2)" label="Credit Utilisation" sublabel={fmtPct(c.utilization_ratio||0,0)+" of limits"}
              tone={(c.utilization_ratio||0) > 0.7 ? {severity:"INFO", label:"Consolidation fit"} : null}/>
          </div>
        </Card>

        <Card title="Bureau Breakdown">
          <div className="l-stats" style={{display:"grid", gridTemplateColumns:"1fr 1fr 1fr", gap:14}}>
            {Object.entries(c.bureau_scores||{}).map(([k,v])=>(
              <div key={k} style={{background:"var(--fill-4)", borderRadius:"var(--r-tile)", padding:"12px 14px"}}>
                <div className="eyebrow">{k}</div>
                <div className="num" style={{font:"600 22px/26px var(--font-display)", margin:"4px 0 6px"}}>{v}</div>
                <Bar value={v-300} max={600} color={v>=750?"var(--pos)":v>=680?"var(--info)":v>=580?"var(--warn)":"var(--neg)"}/>
                <div style={{fontSize:10, color:"var(--ink-3)", marginTop:6}}>
                  {v>=750?"Prime":v>=680?"Near-prime":v>=580?"Sub-prime":"High risk"}
                </div>
              </div>
            ))}
          </div>
          <div className="hairline" style={{margin:"12px 0"}}/>
          <div className="l-stats" style={{display:"grid", gridTemplateColumns:"repeat(4, 1fr)", gap:14}}>
            <Stat label="Best / Selected" value={c.selected_score}/>
            <Stat label="Enquiries (12m)" value={c.total_enquiries_12m} sub={c.credit_hungry_flag?"Credit-hungry":"Normal"}/>
            <Stat label="Trade lines (total)" value={c.total_trade_lines}/>
            <Stat label="Max DPD" value={c.max_dpd_bureau || 0} sub={c.has_default?"Historical default present":"No defaults"}/>
          </div>
        </Card>

        {mode==="ETB" && (
          <Card title="Book Details">
            <div className="l-stats" style={{display:"grid", gridTemplateColumns:"repeat(4, 1fr)", gap:14}}>
              <Stat label="Total sanctioned" value={fmtINR(c.total_sanctioned,{compact:true})}/>
              <Stat label="Outstanding" value={fmtINR(c.total_outstanding,{compact:true})}/>
              <Stat label="Lifetime paid" value={fmtINR(c.total_paid,{compact:true})}/>
              <Stat label="Currently due" value={fmtINR(c.total_due,{compact:true})}/>
              <Stat label="Collateral value" value={fmtINR(c.total_collateral_value||0,{compact:true})}/>
              <Stat label="Avg LTV" value={fmtPct(c.avg_ltv||0,0)}/>
              <Stat label="Secured loans" value={c.secured_loan_count||0}/>
              <Stat label="Coverage ratio" value={fmtPct(c.collateral_coverage_ratio||0,0)}/>
            </div>
          </Card>
        )}
      </div>

      <div style={{display:"flex", flexDirection:"column", gap:12}}>
        <Card title="Flags & Risk Signals">
          <div style={{display:"flex", flexDirection:"column", gap:8}}>
            {[
              {label:"Credit-hungry", on:c.credit_hungry_flag===1, sev:"ACTION", reason:`${c.total_enquiries_12m} enquiries in last 12 months`},
              {label:"Inactive payer", on:c.inactive_flag===1, sev:"WARNING", reason:`Last payment ${c.recent_payment_days} days ago`},
              {label:"High utilisation", on:(c.utilization_ratio||0)>0.7, sev:"INFO", reason:fmtPct(c.utilization_ratio||0,0)+" of credit limits used"},
              {label:"Subprime", on:c.subprime_flag===1, sev:"NEGATIVE", reason:"Bureau below prime threshold"},
              {label:"Delinquency", on:c.delinquency_flag===1, sev:"NEGATIVE", reason:"Prior DPD 30+"},
              {label:"Historical default", on:c.has_default===1, sev:"WARNING", reason:"One or more closed defaults on bureau"},
              {label:"Low FOIR / headroom", on:(c.foir||c.foir_estimated||1)<0.15, sev:"POSITIVE", reason:"Significant EMI headroom"},
              {label:"Affluent", on:c.affluence_indicator===1, sev:"POSITIVE", reason:"High-income / HNI indicators"}
            ].filter(f=>f.on).map((f,i)=>(
              <div key={i} style={{display:"flex", gap:10, alignItems:"flex-start", padding:"8px 0", borderBottom:"1px dashed var(--divider)"}}>
                <Pill severity={f.sev}>{f.sev}</Pill>
                <div style={{flex:1}}>
                  <div style={{fontWeight:600, fontSize:12}}>{f.label}</div>
                  <div style={{fontSize:11, color:"var(--ink-2)", marginTop:2}}>{f.reason}</div>
                </div>
              </div>
            ))}
          </div>
        </Card>

        <Card title="Score Volatility" subtitle="Across bureaus + history">
          {(c.avg_score != null) ? (
            <div style={{display:"grid", gridTemplateColumns:"1fr 1fr", gap:10}}>
              <Stat label="Min" value={c.min_score || Math.min(...Object.values(c.bureau_scores||{}))}/>
              <Stat label="Max" value={c.max_score || Math.max(...Object.values(c.bureau_scores||{}))}/>
              <Stat label="Average" value={Math.round(c.avg_score||0)}/>
              <Stat label="Std deviation" value={fmtNum(c.score_std_dev||0,1)}/>
            </div>
          ) : (
            <div style={{display:"grid", gridTemplateColumns:"1fr 1fr", gap:10}}>
              <Stat label="Min" value={Math.min(...Object.values(c.bureau_scores||{0:0}))}/>
              <Stat label="Max" value={Math.max(...Object.values(c.bureau_scores||{0:0}))}/>
            </div>
          )}
        </Card>

        <Card title="Policy Fit">
          <div style={{fontSize:11.5, color:"var(--ink-2)", lineHeight:1.5}}>
            Based on current signals, customer qualifies for <b>preferential pricing</b> and a <b>ticket size up to ₹ {fmtNum(((c.monthly_income||0)*0.35*36)/1000,0)}K</b> under standard FOIR caps (35% of income, 36m tenure).
          </div>
          <div className="hairline" style={{margin:"10px 0"}}/>
          <div style={{display:"flex", flexDirection:"column", gap:6, fontSize:11.5}}>
            <div style={{display:"flex", justifyContent:"space-between"}}><span style={{color:"var(--ink-2)"}}>FOIR policy cap</span><span className="num">35%</span></div>
            <div style={{display:"flex", justifyContent:"space-between"}}><span style={{color:"var(--ink-2)"}}>Current FOIR</span><span className="num" style={{fontWeight:600, color:(c.foir||c.foir_estimated||0)<0.35?"var(--pos)":"var(--warn)"}}>{fmtPct(c.foir||c.foir_estimated||0,1)}</span></div>
            <div style={{display:"flex", justifyContent:"space-between"}}><span style={{color:"var(--ink-2)"}}>Available headroom</span><span className="num" style={{fontWeight:600}}>{fmtPct(Math.max(0, 0.35 - (c.foir||c.foir_estimated||0)),1)}</span></div>
          </div>
        </Card>
      </div>
    </div>
  );
}
window.TabFinancial = TabFinancial;
