function PageAccountRecovery(){
 const [email,setEmail]=React.useState(''),[code,setCode]=React.useState(''),[sent,setSent]=React.useState(false),[busy,setBusy]=React.useState(false),[notice,setNotice]=React.useState(''),[retryAt,setRetryAt]=React.useState(0),[tick,setTick]=React.useState(0);
 const running=React.useRef(false),request=React.useRef(null),live=React.useRef(false);
 React.useEffect(()=>{live.current=true;window.BOOKUNO_API.getCurrentUser().then(user=>{if(live.current&&user)setEmail(user.email||'');}).catch(()=>{});return()=>{live.current=false;};},[]);
 React.useEffect(()=>{if(!retryAt)return;const t=setInterval(()=>setTick(x=>x+1),1000);return()=>clearInterval(t);},[retryAt]);
 async function send(e){e.preventDefault();if(running.current||Date.now()<retryAt)return;running.current=true;setBusy(true);setNotice('');request.current=request.current||crypto.randomUUID();
  try{await window.BOOKUNO_API.requestAccountRecovery({email:email.trim().toLowerCase(),request_id:request.current});if(live.current){setSent(true);setCode('');setRetryAt(Date.now()+60000);request.current=null;}}catch(e){if(live.current)setNotice(e.message||'The request could not be checked. Try again.');}finally{running.current=false;if(live.current)setBusy(false);}}
 async function verify(e){e.preventDefault();if(running.current)return;running.current=true;setBusy(true);setNotice('');try{await window.BOOKUNO_API.verifyAccountRecovery(email.trim().toLowerCase(),code.trim());if(live.current)window.location.hash='#/reset-password';}catch(e){if(live.current)setNotice(e.message||'This code is invalid or expired. Use the newest email or request another code.');}finally{running.current=false;if(live.current)setBusy(false);}}
 const wait=Math.max(0,Math.ceil((retryAt-Date.now())/1000));
 return <div className="source-auth" data-frame-control="true"><div className="source-auth-shell"><BULogo variant="default" size={32}/><div className="source-auth-card"><span className="badge pri">{uiText("Account recovery")}</span><h1>{uiText("A safe way back.")}</h1><p>{sent?uiText("If this account exists, its latest recovery code is on its way. Enter it below."):uiText("Enter the email you use for Bookuno. We’ll send a code so you can choose a new password.")}</p>{!sent?<form className="source-auth-form" onSubmit={send}><label>{uiText("Account email")}<input type="email" required autoComplete="email" value={email} disabled={busy} onChange={e=>{setEmail(e.target.value);request.current=null;}}/></label><button type="submit" className="btn primary block" disabled={busy}>{busy?uiText("Checking…"):uiText("Send recovery code")}</button></form>:<><p className="account-care-email">{email}</p><form className="source-auth-form" onSubmit={verify}><label>{uiText("Recovery code")}<input required inputMode="numeric" autoComplete="one-time-code" pattern="[0-9]{6,10}" maxLength="10" value={code} disabled={busy} onChange={e=>setCode(e.target.value.replace(/\D/g,''))}/></label><button type="submit" className="btn primary block" disabled={busy||code.length<6}>{busy?uiText("Checking code…"):uiText("Verify recovery code")}</button></form><form onSubmit={send}><button className="btn outline block" disabled={busy||wait>0}>{wait>0?uiText("Send another code in {0}s",{0:(wait)}):uiText("Send another code")}</button></form><button className="btn outline block" disabled={busy} onClick={()=>{setSent(false);setCode('');setNotice('');}}>{uiText("Use a different email")}</button></>}<p className="orders-note">{uiText("Use your newest code. Never share it or send anyone your password. A code does not create another business or recover a closed account automatically.")}</p>{notice&&<p className="source-auth-note" role="alert">{uiText(notice)}</p>}<a className="business-help-link" href="/app.html#/auth">{uiText("Back to sign in")}</a></div></div></div>;
}
function PageAccountClosed(){let receipt;try{receipt=JSON.parse(sessionStorage.getItem('bookuno.closure.receipt')||'null');}catch{}const confirmed=receipt?.closed===true&&Date.now()-receipt.at>=0&&Date.now()-receipt.at<86400000;
 React.useEffect(()=>{if(confirmed)window.BOOKUNO_API.finishClosedSession(receipt.actor_id).catch(()=>{});},[confirmed,receipt?.actor_id]);
 return <div className="source-auth" data-frame-control="true"><div className="source-auth-shell"><BULogo variant="default" size={32}/><div className="source-auth-card"><span className="badge pri">{uiText("Account care")}</span><h1>{confirmed?uiText("Your account is closed."):uiText("Check your account status.")}</h1><p>{confirmed?uiText("Sign-in and account access are blocked. Sole-owner businesses are offline for new orders and bookings."):uiText("Contact Bookuno support if closure was interrupted or you need to check its status.")}</p><p>{uiText("Your business and transaction history has been retained. Closure does not erase records or cancel provider subscriptions. A support review is needed before reopening access; businesses stay offline until separately reviewed.")}</p><a className="btn primary block" href="mailto:hello@bookuno.co.uk?subject=Bookuno%20account%20closure">{uiText("Contact Bookuno support")}</a><a className="btn outline block" href="/app.html#/public">{uiText("Return to Bookuno")}</a></div></div></div>;
}
function PageClosureReviews(){
 const [data,setData]=React.useState(null),[error,setError]=React.useState(''),[busy,setBusy]=React.useState(false),[notes,setNotes]=React.useState({}),[checks,setChecks]=React.useState({});const actor=React.useRef(null),live=React.useRef(false),running=React.useRef(false);
 async function load(){setError('');try{const user=await window.BOOKUNO_API.getCurrentUser();if(!user)throw Error('Sign in with your support operator account.');actor.current=user.id;const r=await window.BOOKUNO_API.closureReview('list',{},user.id);if(live.current)setData(r);}catch(e){if(live.current)setError(e.message);}}
 React.useEffect(()=>{live.current=true;load();const invalidate=()=>{actor.current=null;setData(null);setError("Your account changed. Reopen the review inbox.");};window.addEventListener('bookuno-auth-changed',invalidate);return()=>{live.current=false;actor.current=null;window.removeEventListener('bookuno-auth-changed',invalidate);};},[]);
 async function act(action,r){if(running.current||!actor.current)return;running.current=true;setBusy(true);setError('');try{if(action==='approve')await window.BOOKUNO_API.closureReview('approve',{request_id:r.id,note:notes[r.id]||'',confirmation:'REVIEWED'},actor.current);else await window.BOOKUNO_API.reopenAccount({request_id:r.id,note:notes[r.id]||'',confirmation:action==='resume'?'FINISH CLOSURE':'REOPEN ACCESS',action},actor.current);if(live.current)await load();}catch(e){if(live.current)setError(e.message);}finally{running.current=false;if(live.current)setBusy(false);}}
 return <div className="clarity-site cs-help-page" data-frame-control="true"><ClaritySiteHeader/><main className="cs-wrap business-data account-care"><div className="frame-heading"><div><p className="frame-eyebrow">{uiText("Support workspace")}</p><h1>{uiText("Account closure reviews")}</h1><p>{uiText("Check ownership, commitments and retention before approving. The requester makes the final choice.")}</p></div><button className="btn outline" disabled={busy} onClick={load}>{uiText("Refresh reviews")}</button></div>{error&&<p role="alert" className="orders-error">{uiText(error)}</p>}{data?.requests.length===0&&<p>{uiText("No active closure requests.")}</p>}{data?.requests.map(r=><article className="frame-card" key={r.id}><h2>{r.name||uiText("Account closure request")}</h2><p>{r.email} · {r.phase} · {new Date(r.created_at).toLocaleString()}</p><ul>{r.impact.workspaces.map(w=><li key={w.id}>{w.name} · {w.sole_owner?uiText("Sole owner; goes offline"):uiText("Other owners remain")}</li>)}</ul>{r.impact.blockers.length>0?<div className="account-care-impact"><strong>{uiText("Resolve before approval")}</strong><ul>{r.impact.blockers.map(b=><li key={uiText(b)}>{uiText(b)}</li>)}</ul></div>:<p>{uiText("No outstanding commitments were found in Bookuno. Check external obligations separately.")}</p>}<label>{uiText("Review notes")}<span className="orders-note">{uiText("Record ownership, billing and retention checks. Leave out passwords and private customer lists.")}</span><textarea maxLength="1000" minLength="20" value={notes[r.id]||''} disabled={busy} onChange={e=>setNotes({...notes,[r.id]:e.target.value})}/></label><label className="closure-review-check"><input type="checkbox" checked={!!checks[r.id]} disabled={busy} onChange={e=>setChecks({...checks,[r.id]:e.target.checked})}/>{uiText("I have checked ownership, billing, outstanding work and retention.")}</label>{['requested','approved'].includes(r.phase)?<button className="btn primary" disabled={busy||!r.impact.eligible||!checks[r.id]||(notes[r.id]||'').trim().length<20} onClick={()=>act('approve',r)}>{uiText("Approve closure review")}</button>:<div className="account-care-actions">{r.phase==='closing'&&<button className="btn primary" disabled={busy||!checks[r.id]||(notes[r.id]||'').trim().length<20} onClick={()=>act('resume',r)}>{uiText("Finish interrupted closure")}</button>}<button className="btn outline" disabled={busy||!checks[r.id]||(notes[r.id]||'').trim().length<20} onClick={()=>act('reopen',r)}>{uiText("Restore account access")}</button></div>}<p className="orders-note">{uiText("No automatic erasure or provider cancellation. Reopened accounts keep their businesses offline for a separate review.")}</p></article>)}{data&&<FullRecoveryReviews/>}</main><ClaritySiteFooter/></div>;
}
Object.assign(window,{PageAccountRecovery,PageAccountClosed,PageClosureReviews});
