const UGD=window.UG_GUIDE_DATA;
const ugStyles={
h2:{fontFamily:'var(--font-display)',fontWeight:800,fontSize:28,color:'var(--text-heading)',margin:'28px 0 8px'},
lede:{fontFamily:'var(--font-body)',fontSize:17,color:'var(--text-body)',lineHeight:1.6,margin:'0 0 14px',maxWidth:840},
panel:{background:'var(--white)',border:'1px solid var(--border-default)',borderRadius:'var(--radius-md)',padding:'18px 20px',margin:'14px 0',boxShadow:'var(--shadow-sm)'},
label:{fontFamily:'var(--font-body)',fontSize:13,fontWeight:700,color:'var(--text-muted)',letterSpacing:'.05em',textTransform:'uppercase'},
input:{background:'var(--white)',color:'var(--text-body)',border:'1px solid var(--border-default)',borderRadius:'var(--radius-sm)',padding:'11px 13px',fontSize:16,fontFamily:'var(--font-body)',width:150},
select:{background:'var(--white)',color:'var(--text-body)',border:'1px solid var(--border-default)',borderRadius:'var(--radius-sm)',padding:'11px 13px',fontSize:16,fontFamily:'var(--font-body)',minWidth:160},
hint:{fontFamily:'var(--font-body)',fontSize:15,color:'var(--text-muted)',marginTop:8,lineHeight:1.5},
callout:{background:'var(--white)',border:'1px solid var(--border-default)',borderLeft:'3px solid var(--brand-accent)',borderRadius:'var(--radius-sm)',padding:'16px 19px',fontFamily:'var(--font-body)',fontSize:16,color:'var(--text-body)',lineHeight:1.6,margin:'12px 0'},
th:{background:'var(--gray-50)',textAlign:'left',padding:'11px 14px',fontFamily:'var(--font-body)',fontSize:13.5,letterSpacing:'.03em',textTransform:'uppercase',color:'var(--text-muted)',whiteSpace:'nowrap'},
td:{padding:'10px 14px',borderTop:'1px solid var(--border-default)',fontFamily:'var(--font-body)',fontSize:16.5,color:'var(--text-body)'},
kv:{fontFamily:'var(--font-body)',fontSize:14,color:'var(--text-body)',marginTop:5,lineHeight:1.5},
};
function UgField({label,children}){return <div style={{display:'flex',flexDirection:'column',gap:4}}><label style={ugStyles.label}>{label}</label>{children}</div>;}
function UgTable({head,rows,rightFrom=1,wrapCols=[]}){
return <div style={{border:'1px solid var(--border-default)',borderRadius:'var(--radius-md)',overflow:'auto',margin:'10px 0',background:'var(--white)'}}>
<table style={{borderCollapse:'collapse',width:'100%'}}>
<thead><tr>{head.map((h,i)=><th key={i} style={{...ugStyles.th,textAlign:i>=rightFrom&&!wrapCols.includes(i)?'right':'left'}}>{h}</th>)}</tr></thead>
<tbody>{rows.map((r,ri)=><tr key={ri}>{r.map((v,i)=><td key={i} style={{...ugStyles.td,textAlign:i>=rightFrom&&!wrapCols.includes(i)?'right':'left',fontWeight:i===0?700:400,fontVariantNumeric:'tabular-nums',...(wrapCols.includes(i)?{fontSize:14.5,color:'var(--text-muted)',minWidth:180}:{})}}>{v==null?'—':v}</td>)}</tr>)}</tbody>
</table></div>;}
function ugChanceFor(prog,cat,s){
const floor=prog.cut[cat]; if(floor==null||isNaN(s)||prog.scale) return null;
const ratio=s/floor;
if(ratio>=1.15) return ['Strong','success','well above the shortlist cutoff — the interview/profile stage will decide'];
if(ratio>=1.0)  return ['Fair','success','above the cutoff but near it — expect the call; final admit depends on interview and academics'];
if(ratio>=0.92) return ['Slim','warning','just below the latest cutoff — possible in a lenient year or a later list'];
return ['Unlikely','danger','clearly below the latest cutoff'];
}
function ugPctOf(ex,s){const L=UGD.lookup[ex];let p;
if(s<=L[0][0])p=L[0][1]*s/L[0][0];else if(s>=L[L.length-1][0])p=100;
else for(let i=0;i<L.length-1;i++){const[a,pa]=L[i],[b,pb]=L[i+1];if(s>=a&&s<=b){p=pa+(pb-pa)*(s-a)/(b-a);break;}}
return p;}

function UgFitSection(){
const {Badge}=window.ScorebuddyDesignSystem_ee75f2;
const [cat,setCat]=React.useState('General');
const [grp,setGrp]=React.useState('');
const [v,setV]=React.useState({ipmi:'',ipmr:'',jip:'',cuet:'',npat:'',set:'',iimb:'',jeem:'',sa:'',mcq:'',va:'',t10:'',t12:'',age:'',maths:''});
const set=k=>e=>setV(o=>({...o,[k]:e.target.value}));
const num=k=>parseFloat(v[k]);
const sa=num('sa'),mcq=num('mcq'),va=num('va');
const ipmiVal=(!isNaN(sa)&&!isNaN(mcq)&&!isNaN(va))?sa+mcq+va:num('ipmi');
const scores={ipmi:ipmiVal,ipmr:num('ipmr'),jip:num('jip'),cuet:num('cuet'),npat:num('npat'),set:num('set'),iimb:num('iimb'),jeem:num('jeem')};
const t10=num('t10'),t12=num('t12'),age=num('age'),maths=v.maths;
const anyScore=Object.values(scores).some(x=>!isNaN(x));
const groups=[...new Set(UGD.P.map(p=>p.group))].filter(g=>!grp||g===grp);
let clears=0,total=0;
const inp=(k,ph,w)=><input type="number" value={v[k]} onChange={set(k)} placeholder={ph} style={{...ugStyles.input,...(w?{width:w}:{})}}></input>;
const groupEls=groups.map(g=>{
const cards=UGD.P.filter(p=>p.group===g).map(p=>{
const [exLabel,exMax]=UGD.exams[p.exam]; const s=scores[p.exam]; const floor=p.cut[cat];
let badge=null;
if(p.sectional){
const f=p.sect[cat];
if(!f) badge=<Badge tone="neutral">ℹ no separate {cat} floor reported</Badge>;
else if(isNaN(sa)||isNaN(mcq)||isNaN(va)) badge=<Badge tone="neutral">↑ enter your three Indore sectional marks to check</Badge>;
else{ total++;
const chk=[['QA short-ans',sa,f[0]],['QA-MCQ',mcq,f[1]],['Verbal',va,f[2]]]; const fails=chk.filter(x=>x[1]<x[2]);
const minRatio=Math.min(...chk.map(x=>x[1]/x[2]));
if(!fails.length){clears++; badge=<Badge tone="success">✓ clears all three {cat} floors · chance: {minRatio>=1.25?'Strong':'Fair'}</Badge>;}
else badge=<Badge tone="danger">✗ below the {cat} floor in {fails.map(x=>x[0]+' ('+x[1]+' vs '+x[2]+')').join(', ')} · chance: {minRatio>=0.9?'Slim':'Unlikely'}</Badge>;
}}
else if(floor==null) badge=<Badge tone="neutral">ℹ no published cutoff — see note</Badge>;
else if(p.scale) badge=<Badge tone="neutral">ℹ cutoff is on the {p.scale} — not comparable with raw marks</Badge>;
else{ total++; if(!isNaN(s)){ const c=ugChanceFor(p,cat,s);
if(s>=floor){clears++;badge=<Badge tone="success">✓ clears cutoff · chance: {c[0]}</Badge>;}
else badge=<Badge tone={c[1]==='warning'?'neutral':'danger'}>✗ {floor-s} marks short · chance: {c[0]}</Badge>;}}
let warn=[];
if(!isNaN(age)&&/age 20/i.test(p.elig)&&age>20&&!(cat==='SC'||cat==='ST'||cat==='PwD')) warn.push('age limit 20');
if(!isNaN(age)&&/age 21/i.test(p.elig)&&age>21) warn.push('age limit 21');
if(maths==='n'&&/(Maths|PCM)/.test(p.elig)&&/(compulsory|mandatory|required|Maths in|PCM|with Maths)/.test(p.elig)) warn.push('needs Maths in Class 12');
if(!isNaN(t12)&&/60% in Class 1(0 and 1)?2/.test(p.elig)&&t12<((cat==='SC'||cat==='ST'||cat==='PwD')?55:60)) warn.push('below the Class 12 % floor');
if(!isNaN(t10)&&/60% in Class 10/.test(p.elig)&&t10<((cat==='SC'||cat==='ST'||cat==='PwD')?55:60)) warn.push('below the Class 10 % floor');
let comp=null;
if(p.w&&!isNaN(s)&&exMax&&(!isNaN(t10)||!isNaN(t12))){
const parts=[[p.w[0],s/exMax*100],[p.w[1],t10],[p.w[2],t12]]; let n=0,d=0;
parts.forEach(([w,val])=>{if(w>0&&!isNaN(val)){n+=w*val;d+=w;}});
if(d){const sc=n/d;
const lab=[['Exam',p.w[0]],['X',p.w[1]],['XII',p.w[2]]].filter(x=>x[1]>0).map(x=>x[0]+' '+x[1]+'%').join(' · ')+(p.w[3]?' · then interview '+p.w[3]+'%':'')+(p.w[4]?'':' · assumed');
comp=<div><div style={{display:'flex',alignItems:'center',gap:9,margin:'7px 0 2px'}}>
<div style={{flex:1,height:8,background:'var(--gray-100)',borderRadius:'var(--radius-pill)',overflow:'hidden'}}><div style={{height:'100%',width:sc.toFixed(1)+'%',background:'var(--brand-primary)',borderRadius:'var(--radius-pill)'}}></div></div>
<div style={{fontFamily:'var(--font-body)',fontSize:18,fontWeight:700,fontVariantNumeric:'tabular-nums',color:'var(--text-heading)'}}>{sc.toFixed(1)}<span style={{fontSize:12,color:'var(--text-muted)',fontWeight:600}}>/100</span></div></div>
<div style={{fontFamily:'var(--font-body)',fontSize:13,color:'var(--text-muted)',letterSpacing:'.02em'}}>{lab}</div></div>;}}
const floorTxt=p.sectional?
(p.sect[cat]?<span><b style={{fontSize:22,color:'var(--brand-accent)'}}>{(p.est?'≈ ':'')+p.sect[cat].join(' / ')}</b><span style={{color:'var(--text-muted)'}}> sectional floors: QA-SA /60 · QA-MCQ /120 · Verbal /180 · {cat} — no overall cutoff</span></span>:<span style={{color:'var(--text-muted)'}}>sectional floors for {cat} not reported</span>):
floor==null?<span style={{color:'var(--text-muted)'}}>cutoff not published</span>:
<span><b style={{fontSize:28,color:'var(--brand-accent)'}}>{(p.est?'≈ ':'')+floor}</b><span style={{color:'var(--text-muted)'}}> {p.scale?'('+p.scale+')':(exMax?'/ '+exMax:'')} · {exLabel} · {cat}</span></span>;
return <div key={p.name} style={{background:'var(--white)',border:'1px solid var(--border-default)',borderRadius:'var(--radius-md)',padding:'14px 16px',boxShadow:'var(--shadow-sm)'}}>
<div style={{fontFamily:'var(--font-body)',fontSize:13,color:'var(--text-muted)',fontWeight:600,letterSpacing:'.04em',textTransform:'uppercase',marginBottom:2}}>{exLabel} · {p.years}-yr</div>
<h3 style={{margin:0,fontFamily:'var(--font-display)',fontWeight:700,fontSize:19,color:'var(--text-heading)'}}>{p.name}</h3>
<div style={{fontFamily:'var(--font-body)',fontSize:14.5,color:'var(--text-body)',marginTop:2}}>{p.programme}</div>
<div style={{fontFamily:'var(--font-body)',fontSize:16,color:'var(--text-body)',margin:'8px 0 3px'}}>{floorTxt}</div>
{comp}
<div style={ugStyles.kv}><b>Seats</b> {p.seats??'—'} &nbsp;·&nbsp; <b>Fees</b> {p.fees}</div>
<div style={ugStyles.kv}><b>Eligibility</b> {p.elig}</div>
<div style={{display:'flex',gap:8,flexWrap:'wrap',marginTop:8}}>{badge}{warn.length?<Badge tone="danger">⚠ {warn.join(' · ')}</Badge>:null}</div>
<div style={{fontFamily:'var(--font-body)',fontSize:15,color:'var(--text-muted)',marginTop:8,lineHeight:1.55}}>{p.cutNote} {p.note}</div>
</div>;});
return <div key={g}>
<div style={{fontFamily:'var(--font-display)',fontWeight:700,fontSize:17,letterSpacing:'.02em',color:'var(--brand-accent)',textTransform:'uppercase',margin:'30px 0 12px'}}>{g}</div>
<div style={{display:'grid',gridTemplateColumns:'repeat(auto-fill,minmax(320px,1fr))',gap:12}}>{cards}</div></div>;});
return <div>
<h2 style={ugStyles.h2}>Check <span style={{color:'var(--brand-accent)'}}>your fit</span></h2>
<p style={ugStyles.lede}>Enter whichever exam scores you have (or are targeting), your category and your board marks. Each programme's card compares you with its latest published cutoff and — where the institute publishes selection weights — scores your overall profile.</p>
<div style={ugStyles.panel}>
<div style={{display:'flex',gap:10,flexWrap:'wrap',alignItems:'end'}}>
<UgField label="Category"><select value={cat} onChange={e=>setCat(e.target.value)} style={ugStyles.select}>{UGD.cats.map(c=><option key={c}>{c}</option>)}</select></UgField>
<UgField label="IPMAT Indore /360">{inp('ipmi','e.g. 210')}</UgField>
<UgField label="IPMAT Rohtak /480">{inp('ipmr','e.g. 340')}</UgField>
<UgField label="JIPMAT /400">{inp('jip','e.g. 300')}</UgField>
<UgField label="CUET (DU norm. /1000)">{inp('cuet','e.g. 760')}</UgField>
<UgField label="NPAT /120">{inp('npat','e.g. 98')}</UgField>
<UgField label="SET /60">{inp('set','e.g. 46')}</UgField>
<UgField label="IIMB UG test /180">{inp('iimb','e.g. 120')}</UgField>
<UgField label="JEE Main %ile">{inp('jeem','e.g. 92')}</UgField>
</div>
<div style={{display:'flex',gap:10,flexWrap:'wrap',alignItems:'center',marginTop:12}}>
<UgField label="Indore QA short-ans /60">{inp('sa','e.g. 30')}</UgField>
<UgField label="Indore QA-MCQ /120">{inp('mcq','e.g. 60')}</UgField>
<UgField label="Indore Verbal /180">{inp('va','e.g. 130')}</UgField>
<div style={{...ugStyles.hint,margin:0,maxWidth:520}}>← IIM Indore, Amritsar and Shillong shortlist on <b>sectional</b> floors, not a total — enter these three and each is checked; the total fills itself for Ranchi / IIFT / Nirma / Sambalpur, which use the overall score</div>
</div>
<div style={{display:'flex',gap:10,flexWrap:'wrap',alignItems:'end',marginTop:12}}>
<UgField label="Class 10 %">{inp('t10','e.g. 92')}</UgField>
<UgField label="Class 12 % (or expected)">{inp('t12','e.g. 88')}</UgField>
<UgField label="Age on 1 Aug 2027">{inp('age','e.g. 18')}</UgField>
<UgField label="Maths in Class 12?"><select value={v.maths} onChange={set('maths')} style={ugStyles.select}><option value="">—</option><option value="y">Yes</option><option value="n">No</option></select></UgField>
<UgField label="Show"><select value={grp} onChange={e=>setGrp(e.target.value)} style={ugStyles.select}><option value="">All programmes</option>{[...new Set(UGD.P.map(p=>p.group))].map(g=><option key={g}>{g}</option>)}</select></UgField>
</div>
<div style={ugStyles.hint}>{anyScore?('You clear the published shortlist cutoff at '+(()=>{let c=0,t=0;groups.forEach(g=>UGD.P.filter(p=>p.group===g).forEach(p=>{
if(p.sectional){const f=p.sect[cat];if(f&&!isNaN(sa)&&!isNaN(mcq)&&!isNaN(va)){t++;if(sa>=f[0]&&mcq>=f[1]&&va>=f[2])c++;}}
else if(p.cut[cat]!=null&&!p.scale){t++;const s=scores[p.exam];if(!isNaN(s)&&s>=p.cut[cat])c++;}
}));return c+' of the '+t;})()+' programmes that publish one (for your category and the exams you entered).'):'Enter a score to light the cards up. Age and maths flags appear automatically once you fill those in.'}</div>
</div>
{groupEls}
<div style={ugStyles.callout}><b>How to read the cards.</b> The big number is the latest published shortlist cutoff for your category on that programme's exam (≈ marks an estimate or a prior-year figure). Green / red compares your score with it. The <b>profile score</b> blends your exam percentage-of-maximum with your Class 10 and 12 marks on the institute's published weights (interview weight excluded, since that happens later) — it's a comparison tool across programmes, not an admission probability.</div>
</div>;}
Object.assign(window,{UgFitSection,UgField,UgTable,ugStyles,ugChanceFor,ugPctOf});
