function FullDataRecovery({actor,business}){
 const [data,setData]=React.useState(null),[error,setError]=React.useState(''),[notice,setNotice]=React.useState(''),[open,setOpen]=React.useState(false),[notes,setNotes]=React.useState(''),[confirmation,setConfirmation]=React.useState(''),[busy,setBusy]=React.useState(false),[revision,setRevision]=React.useState(0);
 const live=React.useRef(false),scope=React.useRef(null),running=React.useRef(false),nonce=React.useRef(null),tag=actor+':'+business;
 const current=()=>live.current&&scope.current===tag&&window.BOOKUNO_WORKSPACE?.businessId?.()===business;
 const call=(action,payload={})=>window.BOOKUNO_API.fullDataRecovery(action,{...payload,business_id:business},actor);
 React.useEffect(()=>{let stopped=false;live.current=true;scope.current=tag;setData(null);setError('');setNotice('');setOpen(false);setNotes('');setConfirmation('');nonce.current=null;
  call('read').then(r=>{if(!stopped&&current())setData(r);}).catch(e=>{if(!stopped&&current())setError(e.message||'Recovery status could not be checked.');});
  const invalidate=()=>{scope.current=null;setData(null);setOpen(false);setNotes('');setConfirmation('');setRevision(x=>x+1);};
  const storage=e=>{if(e.key===null||['bookuno.business_id','bookuno.business_actor'].includes(e.key))invalidate();};
  window.addEventListener('bookuno-auth-changed',invalidate);window.addEventListener('storage',storage);
  return()=>{stopped=true;live.current=false;scope.current=null;window.removeEventListener('bookuno-auth-changed',invalidate);window.removeEventListener('storage',storage);};
 },[tag,revision]);
 async function act(action){if(running.current||!current()||!data)return;running.current=true;setBusy(true);setError('');setNotice('');
  try{if(action==='request')nonce.current=nonce.current||crypto.randomUUID();const r=await call(action,action==='request'?{run_id:data.runs[0].id,request_id:nonce.current,notes,confirmation}:{request_id:data.request.id});
   if(current()){setData(r);setOpen(false);setConfirmation('');setNotes('');nonce.current=null;setNotice(action==='cancel'?'Recovery review cancelled. Your current records are unchanged.':'Recovery review saved. Your current records are unchanged. No email was sent.');}
  }catch(e){if(current())setError(e.message||'The request could not be confirmed. Refresh before trying again.');}finally{running.current=false;if(live.current)setBusy(false);}
 }
 const run=data?.runs[0],labels={customers:'Customers',products:'Products',stock:'Stock records',orders:'Shop orders',appointments:'Appointments',group_bookings:'Group bookings',stays:'Overnight bookings',services:'Services',sessions:'Sessions',pitches:'Overnight spaces'},date=v=>new Date(v).toLocaleString(undefined,{dateStyle:'medium',timeStyle:'short'});
 return <section className="frame-card full-data-recovery" aria-label={uiText("Full data recovery")} data-frame-control="true"><p className="frame-eyebrow">{uiText("More than a label change")}</p><h2>{uiText("Full data recovery")}</h2><p>{uiText("Recover customers, orders, stock and booking records with a careful review. A separate restored copy is checked before any change to your working business.")}</p>
  {!data&&!error&&<p role="status">{uiText("Checking recovery evidence…")}</p>}
  {data&&(run?<><div className="full-recovery-verified"><span className="badge pri">{uiText("Restored copy checked")}</span><p>{uiText("Captured ")}{date(run.captured_at)}<br/>{uiText("Checked ")}{date(run.verified_at)}{uiText(" · Private recovery copy")}</p></div><p className="orders-note">{uiText("This check covers the saved records at that time. Later work is not included. It does not mean your current business has been rolled back or that automatic backups are scheduled.")}</p>
   <details className="full-recovery-scope"><summary>{uiText("See what was checked")}</summary><dl>{Object.entries(run.counts).filter(([key])=>labels[key]).map(([key,count])=><div key={key}><dt>{uiText(labels[key])}</dt><dd>{count.toLocaleString()}</dd></div>)}<div><dt>{uiText("Business files")}</dt><dd>{run.files.toLocaleString()}</dd></div></dl><p className="orders-note">{uiText("Database relationships, access rules and backed-up file bytes were checked in isolation. Account passwords and private backup files are never shown here.")}</p></details>
   {data.request?<div className="account-care-impact" role="status"><h3>{uiText("Recovery review requested")}</h3><p>{uiText("Saved ")}{date(data.request.created_at)}{uiText(". Bookuno needs to agree the recovery scope, later changes and any payment or availability conflicts with you before applying a restore.")}</p><p className="full-recovery-notes">{data.request.notes}</p><button type="button" className="btn outline" disabled={busy} onClick={()=>act('cancel')}>{uiText("Cancel recovery review")}</button></div>:open?<form onSubmit={e=>{e.preventDefault();act('request');}}><h3>{uiText("Request a recovery review")}</h3><p>{uiText("Describe what is missing or wrong and when it happened. Leave out passwords, payment details and customer lists. This saves a request; it does not overwrite your business or send a message.")}</p><label>{uiText("What needs recovering?")}<textarea required minLength="20" maxLength="1000" value={notes} disabled={busy} onChange={e=>setNotes(e.target.value)}/></label><label>{uiText("Type REQUEST RECOVERY")}<input value={confirmation} autoComplete="off" disabled={busy} onChange={e=>setConfirmation(e.target.value)}/></label><div className="account-care-actions"><button type="button" className="btn outline" disabled={busy} onClick={()=>{setOpen(false);setConfirmation('');setNotes('');}}>{uiText("Keep my current records")}</button><button type="submit" className="btn primary" disabled={busy||notes.trim().length<20||confirmation!=='REQUEST RECOVERY'}>{busy?uiText("Saving review…"):uiText("Save recovery review")}</button></div></form>:<button type="button" className="btn outline" disabled={busy} onClick={()=>{setOpen(true);setNotice('');}}>{uiText("Review full recovery")}</button>}
  </>:<div className="account-care-impact"><h3>{uiText("No full recovery check recorded yet")}</h3><p>{uiText("Label recovery points and exported files are useful, but they are not a verified full backup. Ask support to check the available recovery source before making a recovery plan.")}</p><a className="business-help-link" href="/app.html#/help">{uiText("Get help with recovery →")}</a></div>)}
  <p className="orders-note">{uiText("A recovery review does not replay payments, issue refunds, send queued messages or change account access. Before a working-business restore, Bookuno must preserve current account closures and contact preferences, then reconcile provider balances, later bookings and stock movements.")}</p>
  {error&&<p role="alert" className="orders-error">{uiText(error)}</p>}{notice&&<p role="status" className="account-care-notice">{uiText(notice)}</p>}<button type="button" className="btn outline" disabled={busy} onClick={()=>setRevision(x=>x+1)}>{uiText("Refresh full recovery")}</button>
 </section>;
}
function FullRecoveryReviews(){
 const [data,setData]=React.useState(null),[error,setError]=React.useState(''),[offset,setOffset]=React.useState(0),[revision,setRevision]=React.useState(0);
 React.useEffect(()=>{let stopped=false;setData(null);setError('');window.BOOKUNO_API.getCurrentUser().then(user=>{if(!user)throw Error('Sign in with your support operator account.');return window.BOOKUNO_API.fullRecoveryReviews(offset,user.id);}).then(r=>{if(!stopped)setData(r);}).catch(e=>{if(!stopped)setError(e.message);});const invalidate=()=>{stopped=true;setData(null);setError("Your account changed. Reopen the review workspace.");};window.addEventListener('bookuno-auth-changed',invalidate);return()=>{stopped=true;window.removeEventListener('bookuno-auth-changed',invalidate);};},[offset,revision]);
 return <section aria-label={uiText("Full recovery reviews")} className="frame-card full-data-recovery"><p className="frame-eyebrow">{uiText("Recovery support")}</p><h2>{uiText("Full recovery reviews")}</h2><p>{uiText("Agree the scope with the owner. Preserve current closures and contact preferences, reconcile later work and provider balances, and check a separate recovery copy before proposing a working restore. This inbox does not apply restores or send messages.")}</p><button className="btn outline" type="button" onClick={()=>setRevision(x=>x+1)}>{uiText("Refresh recovery reviews")}</button>{error&&<p role="alert" className="orders-error">{uiText(error)}</p>}{!data&&!error&&<p role="status">{uiText("Checking recovery reviews…")}</p>}{data?.requests.length===0&&<p>{uiText("No active recovery reviews.")}</p>}{data?.requests.map(r=><article className="account-care-impact" key={r.id}><h3>{r.business_name}</h3><p>{r.requester_name||uiText("Business owner")} · {new Date(r.created_at).toLocaleString()}</p><p className="full-recovery-notes">{r.notes}</p><p className="orders-note">{uiText("Recovery copy captured ")}{new Date(r.captured_at).toLocaleString()}{uiText("; checked ")}{new Date(r.verified_at).toLocaleString()}{uiText(". The working business is unchanged.")}</p></article>)}{data&&<div className="account-care-actions"><button type="button" className="btn outline" disabled={offset===0} onClick={()=>setOffset(x=>Math.max(0,x-50))}>{uiText("Previous reviews")}</button><button type="button" className="btn outline" disabled={!data.has_more} onClick={()=>setOffset(x=>x+50)}>{uiText("More reviews")}</button></div>}</section>;
}
Object.assign(window,{FullDataRecovery,FullRecoveryReviews});
