/* Front page content managed from Admin → Front page & notices (GET /api/home):
   <WhatsNew/>      notices marked "Show on front page" (can be switched off in admin)
   <Testimonials/>  student testimonials (can be switched off in admin)
   <PromoPopup/>    the current pop-up offer: shown after a short delay, as often as the admin chose;
                    a centred card on desktop, a bottom sheet on phones; closes with ✕, Esc, the
                    backdrop or "Maybe later". */
(function(){
const h=React.createElement;
function homeData(){
if(!window.__sbHome)window.__sbHome=fetch('/api/home',{credentials:'same-origin'}).then(r=>r.ok?r.json():null).catch(()=>null);
return window.__sbHome;
}
function useHome(){
const [d,setD]=React.useState(null);
React.useEffect(()=>{let on=true;homeData().then(x=>{if(on)setD(x||{});});return ()=>{on=false;};},[]);
return d;
}
const CSS=`
.hx-follow{max-width:1180px;margin:0 auto;padding:8px 48px 56px}
.hx-fbox{background:linear-gradient(135deg,var(--navy-900,#131a5c),#24309a);border-radius:var(--radius-lg,18px);padding:28px 32px;display:flex;align-items:center;justify-content:space-between;gap:20px 28px;flex-wrap:wrap;box-shadow:var(--shadow-md)}
.hx-fbox h2{font-family:var(--font-display);font-weight:800;font-size:24px;color:#fff;margin:0}
.hx-fbox p{font-family:var(--font-body);font-size:14.5px;color:rgba(255,255,255,.78);margin:6px 0 0;max-width:420px;line-height:1.5}
.hx-soc{display:flex;flex-wrap:wrap;gap:10px}
.hx-soc a{display:inline-flex;align-items:center;gap:8px;background:var(--white);color:var(--text-heading,#131a5c);border-radius:999px;padding:9px 16px 9px 10px;font-family:var(--font-body);font-weight:700;font-size:14px;text-decoration:none;transition:transform .15s,box-shadow .15s}
.hx-soc a:hover{transform:translateY(-2px);box-shadow:0 6px 16px rgba(0,0,0,.25)}
.hx-soc a:focus-visible{outline:3px solid #ffd166;outline-offset:2px}
.hx-soc i{width:30px;height:30px;border-radius:50%;display:inline-flex;align-items:center;justify-content:center;color:#fff;flex:none}
@media (max-width:640px){.hx-follow{padding:4px 16px 40px}.hx-fbox{padding:22px 18px}.hx-fbox h2{font-size:20px}.hx-soc{gap:8px}.hx-soc a{padding:7px 13px 7px 8px;font-size:13px}}
.hx-sec{padding:64px 48px;max-width:1180px;margin:0 auto}
.hx-head{display:flex;justify-content:space-between;align-items:flex-end;gap:16px;flex-wrap:wrap;margin-bottom:22px}
.hx-head h2{font-family:var(--font-display);font-weight:800;font-size:28px;color:var(--text-heading);margin:0}
.hx-head p{font-family:var(--font-body);font-size:15px;color:var(--text-muted);margin:6px 0 0}
.hx-news{display:grid;grid-template-columns:repeat(auto-fill,minmax(260px,1fr));gap:16px}
.hx-card{background:var(--white);border:1px solid var(--border-default);border-radius:var(--radius-lg);box-shadow:var(--shadow-sm);padding:18px 20px;display:flex;flex-direction:column;gap:8px}
.hx-card.pin{border:2px solid var(--brand-accent)}
.hx-meta{display:flex;align-items:center;gap:8px;flex-wrap:wrap;font-family:var(--font-body);font-size:12px;color:var(--text-muted)}
.hx-tag{font-weight:800;font-size:11px;letter-spacing:.05em;text-transform:uppercase;border-radius:999px;padding:3px 9px;background:var(--navy-50,#eef0fb);color:var(--brand-primary)}
.hx-tag.t-offer,.hx-tag.t-important,.hx-tag.t-live-test{background:var(--brand-accent);color:#fff}
.hx-tag.t-result,.hx-tag.t-new-test{background:var(--teal-50,#e6f5f3);color:var(--teal-700,#0b6b62)}
.hx-card h3{font-family:var(--font-display);font-weight:700;font-size:16.5px;color:var(--text-heading);margin:0;line-height:1.35}
.hx-card p{font-family:var(--font-body);font-size:13.5px;color:var(--text-body);line-height:1.55;margin:0;white-space:pre-line;display:-webkit-box;-webkit-line-clamp:4;-webkit-box-orient:vertical;overflow:hidden}
.hx-card a.go{margin-top:auto;font-family:var(--font-body);font-weight:700;font-size:13.5px;color:var(--brand-accent);text-decoration:none}
.hx-quotes{display:grid;grid-template-columns:repeat(auto-fill,minmax(280px,1fr));gap:18px}
.hx-q{background:var(--white);border:1px solid var(--border-default);border-radius:var(--radius-lg);box-shadow:var(--shadow-sm);padding:22px;display:flex;flex-direction:column;gap:14px}
.hx-stars{color:#f5a623;font-size:16px;letter-spacing:2px}
.hx-q blockquote{margin:0;font-family:var(--font-body);font-size:14.5px;line-height:1.65;color:var(--text-body);flex:1}
.hx-q blockquote::before{content:"“";font-family:var(--font-display);font-size:40px;line-height:0;color:var(--brand-accent);vertical-align:-14px;margin-right:4px}
.hx-who{display:flex;align-items:center;gap:16px}
.hx-av{width:76px;height:76px;border-radius:50%;object-fit:cover;flex:none;background:var(--navy-900,#131a5c);color:#fff;display:flex;align-items:center;justify-content:center;font-family:var(--font-display);font-weight:800;font-size:24px;box-shadow:0 0 0 3px #fff,0 0 0 5px var(--brand-accent)}
.hx-who b{display:block;font-family:var(--font-display);font-size:15px;color:var(--text-heading)}
.hx-who span{font-family:var(--font-body);font-size:12.5px;color:var(--text-muted)}
.hx-more{display:block;margin:20px auto 0;border:1.5px solid var(--border-default);background:var(--white);border-radius:999px;padding:10px 22px;font-family:var(--font-body);font-weight:700;font-size:14px;color:var(--text-heading);cursor:pointer}
/* pop-up */
.hx-ov{position:fixed;inset:0;z-index:6000;background:rgba(11,16,51,.62);display:flex;align-items:center;justify-content:center;padding:20px;animation:hxFade .2s ease}
.hx-pop{position:relative;width:100%;max-width:460px;max-height:calc(100vh - 40px);overflow:auto;background:var(--white);border-radius:18px;box-shadow:0 24px 64px rgba(11,16,51,.4);animation:hxUp .25s ease}
.hx-x{position:absolute;top:10px;right:10px;z-index:2;width:40px;height:40px;border-radius:50%;border:none;background:rgba(255,255,255,.92);color:#0b1033;font-size:18px;font-weight:700;cursor:pointer;box-shadow:0 2px 8px rgba(0,0,0,.18)}
.hx-x:focus-visible{outline:3px solid #fff;outline-offset:2px}
.hx-img{display:block;width:100%;max-height:220px;object-fit:cover}
.hx-band{padding:26px 24px 20px;color:#fff}
.hx-band.navy{background:linear-gradient(135deg,#131a5c,#1d2fa0)}.hx-band.red{background:linear-gradient(135deg,#b3261e,#e02b2b)}
.hx-band.teal{background:linear-gradient(135deg,#0b6b62,#12917f)}.hx-band.gold{background:linear-gradient(135deg,#8a5a00,#d99a1e)}
.hx-band h3{font-family:var(--font-display);font-weight:800;font-size:23px;line-height:1.25;margin:0 36px 0 0;color:#fff}
.hx-body{padding:18px 24px 22px}
.hx-body p{font-family:var(--font-body);font-size:15px;line-height:1.6;color:var(--text-body);margin:0 0 16px;white-space:pre-line}
.hx-code{display:flex;align-items:center;justify-content:space-between;gap:10px;border:2px dashed var(--brand-accent);border-radius:12px;padding:10px 12px 10px 16px;margin-bottom:16px}
.hx-code span{font-family:var(--font-body);font-size:12px;color:var(--text-muted);display:block}
.hx-code b{font-family:var(--font-display);font-size:20px;letter-spacing:.08em;color:var(--text-heading)}
.hx-code button{border:none;background:var(--navy-900,#131a5c);color:#fff;border-radius:8px;padding:9px 14px;font-family:var(--font-body);font-weight:700;font-size:13px;cursor:pointer;flex:none}
.hx-cta{display:block;width:100%;text-align:center;background:var(--brand-accent);color:#fff;text-decoration:none;border-radius:999px;padding:14px;font-family:var(--font-body);font-weight:800;font-size:15.5px;box-shadow:0 8px 20px rgba(224,43,43,.25)}
.hx-cta:hover{color:#fff;filter:brightness(.96)}
.hx-later{display:block;margin:12px auto 0;background:none;border:none;font-family:var(--font-body);font-size:13.5px;color:var(--text-muted);text-decoration:underline;cursor:pointer;padding:6px}
@keyframes hxFade{from{opacity:0}to{opacity:1}}
@keyframes hxUp{from{transform:translateY(24px);opacity:0}to{transform:none;opacity:1}}
@media(max-width:640px){
.hx-sec{padding:44px 16px}.hx-head h2{font-size:23px}
.hx-news{display:flex;overflow-x:auto;scroll-snap-type:x mandatory;gap:12px;padding-bottom:6px;margin:0 -16px;padding-left:16px;padding-right:16px;scrollbar-width:none}
.hx-news::-webkit-scrollbar{display:none}
.hx-news .hx-card{flex:0 0 82%;scroll-snap-align:start}
.hx-quotes{grid-template-columns:1fr}.hx-av{width:64px;height:64px;font-size:21px}
/* phones: the pop-up is a bottom sheet */
.hx-ov{align-items:flex-end;padding:0}
.hx-pop{max-width:none;max-height:88vh;border-radius:20px 20px 0 0;padding-bottom:env(safe-area-inset-bottom)}
.hx-band h3{font-size:21px}
}
@media (prefers-reduced-motion:reduce){.hx-ov,.hx-pop{animation:none}}
`;
if(!document.getElementById('hx-css')){const s=document.createElement('style');s.id='hx-css';s.textContent=CSS;document.head.appendChild(s);}
const fmtDay=(t)=>t?new Date(t).toLocaleDateString('en-IN',{day:'numeric',month:'short'}):'';
const slug=(t)=>'t-'+String(t||'').toLowerCase().replace(/[^a-z]+/g,'-');
const internal=(u)=>!/^https?:/i.test(u||'');

function WhatsNew(){
const d=useHome();
if(!d||!d.updates||!d.updates.length)return null;
return h('section',{className:'hx-sec','aria-labelledby':'hx-news-h'},
h('div',{className:'hx-head'},h('div',null,h('h2',{id:'hx-news-h'},'What’s new'),h('p',null,'Latest tests, results and announcements from Scorebuddy.'))),
h('div',{className:'hx-news'},d.updates.map(u=>h('article',{key:u.id,className:'hx-card'+(u.pinned?' pin':'')},
h('div',{className:'hx-meta'},h('span',{className:'hx-tag '+slug(u.tag)},u.tag),h('span',null,fmtDay(u.date)),u.pinned?h('span',null,'📌'):null),
h('h3',null,u.title),u.body?h('p',null,u.body):null,
u.linkUrl?h('a',{className:'go',href:u.linkUrl,target:internal(u.linkUrl)?undefined:'_blank',rel:internal(u.linkUrl)?undefined:'noopener'},(u.linkLabel||'Read more')+' →'):null))));
}

function Testimonials(){
const d=useHome();
const [all,setAll]=React.useState(false);
if(!d||!d.testimonials||!d.testimonials.length)return null;
const list=all?d.testimonials:d.testimonials.slice(0,6);
const initials=(n)=>String(n||'?').split(/\s+/).map(x=>x[0]).join('').slice(0,2).toUpperCase();
return h('section',{className:'hx-sec','aria-labelledby':'hx-t-h'},
h('div',{className:'hx-head'},h('div',null,h('h2',{id:'hx-t-h'},'What students say'),h('p',null,'Real students, real results.'))),
h('div',{className:'hx-quotes'},list.map(t=>h('figure',{key:t.id,className:'hx-q',style:{margin:0}},
t.rating?h('div',{className:'hx-stars','aria-label':t.rating+' out of 5 stars'},'★'.repeat(t.rating)+'☆'.repeat(5-t.rating)):null,
h('blockquote',null,t.quote),
h('figcaption',{className:'hx-who'},t.photo?h('img',{className:'hx-av',src:t.photo,alt:'',loading:'lazy'}):h('span',{className:'hx-av','aria-hidden':true},initials(t.name)),
h('div',null,h('b',null,t.name),t.detail?h('span',null,t.detail):null))))),
d.testimonials.length>6&&!all?h('button',{className:'hx-more',type:'button',onClick:()=>setAll(true)},'Show all '+d.testimonials.length+' testimonials'):null);
}

/* How often: every visit / once per browser session / once a day / only once (per offer version). */
function promoDue(p){
const key='sb_promo_'+p.id+'_'+p.version;
try{
if(p.frequency==='every')return true;
if(p.frequency==='session')return !sessionStorage.getItem(key);
const last=+localStorage.getItem(key)||0;
if(p.frequency==='once')return !last;
return Date.now()-last>86400000;
}catch(e){return true;}
}
function promoSeen(p){const key='sb_promo_'+p.id+'_'+p.version;try{if(p.frequency==='session')sessionStorage.setItem(key,'1');else localStorage.setItem(key,String(Date.now()));}catch(e){}}

function PromoPopup(){
const d=useHome();
const [open,setOpen]=React.useState(false);
const [copied,setCopied]=React.useState(false);
const closeRef=React.useRef(null);
const p=window.SB_APP?null:d&&d.promo; /* no offers inside the Android app (components/sb-app.js) */
React.useEffect(()=>{if(!p||!promoDue(p))return;const t=setTimeout(()=>{setOpen(true);promoSeen(p);},(p.delaySec||0)*1000);return ()=>clearTimeout(t);},[p&&p.id]);
React.useEffect(()=>{
if(!open)return;
const prev=document.documentElement.style.overflow;document.documentElement.style.overflow='hidden';
const k=(e)=>{if(e.key==='Escape')setOpen(false);};document.addEventListener('keydown',k);
setTimeout(()=>closeRef.current&&closeRef.current.focus(),50);
return ()=>{document.documentElement.style.overflow=prev;document.removeEventListener('keydown',k);};
},[open]);
if(!p||!open)return null;
const copy=()=>{(navigator.clipboard?navigator.clipboard.writeText(p.couponCode):Promise.reject()).then(()=>setCopied(true),()=>setCopied(false));try{localStorage.setItem('sb_ref',p.couponCode);}catch(e){}};
return h('div',{className:'hx-ov',onClick:(e)=>{if(e.target===e.currentTarget)setOpen(false);}},
h('div',{className:'hx-pop',role:'dialog','aria-modal':'true','aria-labelledby':'hx-pop-h'},
h('button',{ref:closeRef,className:'hx-x',type:'button','aria-label':'Close',onClick:()=>setOpen(false)},'✕'),
p.image?h('img',{className:'hx-img',src:p.image,alt:''}):null,
h('div',{className:'hx-band '+(p.theme||'navy')},h('h3',{id:'hx-pop-h'},p.title)),
h('div',{className:'hx-body'},
p.body?h('p',null,p.body):null,
p.couponCode?h('div',{className:'hx-code'},h('div',null,h('span',null,'Use code at checkout'),h('b',null,p.couponCode)),h('button',{type:'button',onClick:copy},copied?'Copied ✓':'Copy')):null,
p.ctaUrl?h('a',{className:'hx-cta',href:p.ctaUrl,onClick:()=>{if(p.couponCode){try{localStorage.setItem('sb_ref',p.couponCode);}catch(e){}}}},p.ctaLabel||'Learn more'):null,
h('button',{className:'hx-later',type:'button',onClick:()=>setOpen(false)},'Maybe later'))));
}

/* "Follow us" band: the social links set in Admin → Front page & notices → Social media. */
function FollowUs(){
const S=window.SBSocial;
const d=S?S.useLinks():null;
if(!d||d.band===false||!d.links.length)return null;
return h('section',{className:'hx-follow','aria-labelledby':'hx-f-h'},h('div',{className:'hx-fbox'},
h('div',null,h('h2',{id:'hx-f-h'},'Follow Scorebuddy'),h('p',null,'Daily prep tips, new tests, live-test alerts and results. Join us on social media.')),
h('div',{className:'hx-soc'},d.links.map(l=>h('a',{key:l.id,href:l.url,target:'_blank',rel:'noopener','aria-label':'Scorebuddy on '+l.name},
h('i',{style:{background:S.color(l.id)},'aria-hidden':true},S.icon(l.id,17)),l.name.replace(/ \(.*\)$/,''))))));
}

window.FollowUs=FollowUs;window.WhatsNew=WhatsNew;window.Testimonials=Testimonials;window.PromoPopup=PromoPopup;
})();
