const { useState, useEffect, useCallback, useRef } = React;

/* ── Supabase storage ── */
const _db = supabase.createClient("https://befxwolodujpjvehvvdf.supabase.co", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImJlZnh3b2xvZHVqcGp2ZWh2dmRmIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzYxMzM4NDQsImV4cCI6MjA5MTcwOTg0NH0.AP46knVwqLbJlLURS7wX6IPT3Wh6kV03LSb_t82B5o0");

const dbGet = async (key, fallback) => {
  try {
    const { data } = await _db
      .from('app_storage')
      .select('value')
      .eq('key', key)
      .maybeSingle();
    if (!data || data.value == null) return fallback;
    return JSON.parse(data.value);
  } catch(e) { return fallback; }
};

const dbSet = async (key, value) => {
  try {
    await _db.from('app_storage').upsert(
      { key: key, value: JSON.stringify(value) },
      { onConflict: 'key' }
    );
  } catch(e) {}
};

/* aliases so existing code works unchanged */
const _ss = dbSet, _sl = dbGet, _s = dbSet, _l = dbGet;


const N="#2c3a43", AC="#D4693A", CR="#f4f1ec", W="#fff", BD="#e5dfd5", MU="#9a9189", RE="#c0392b", GR="#1e7e5e", BL="#4E7FA6";
const WA_CITIES=[
  {city:"Vancouver",    rate:8.9},
  {city:"Battle Ground",rate:8.9},
  {city:"Camas",        rate:8.8},
  {city:"La Center",    rate:8.8},
  {city:"Ridgefield",   rate:9.0},
  {city:"Washougal",    rate:8.8},
  {city:"Yacolt",       rate:8.7},
  {city:"Woodland",     rate:7.9},
];
const cityOpts=WA_CITIES.map(c=>({v:c.city,l:`${c.city} — ${c.rate}%`}));
const rateForCity=city=>(WA_CITIES.find(c=>c.city===city)?.rate)||8.9;
const DEF_SPLITS={marketing:10,overhead:5,pm_ivan:25,pm_cory:20,pm_cory_bonus:5,pm_yuriy:25,pm_mark:25};
const USERS=[
  {id:"u1",username:"admin", name:"Becky", role:"admin"},
  {id:"u2",username:"nazar", name:"Nazar", role:"pm"},
  {id:"u3",username:"ivan",  name:"Ivan",  role:"pm"},
  {id:"u4",username:"cory",  name:"Cory",  role:"pm"},
  {id:"u5",username:"yuriy", name:"Yuriy", role:"pm"},
  {id:"u6",username:"mark",  name:"Mark",  role:"pm"},
];
const LOGO="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCAYAAACAvzbMAABcqklEQVR42u29eZhdVZX/vdY691aGmisjhDAIgoA4MajtQAKCE84mDiBRW0LrI6+zYvu2SbpbW5+fdkuL9K/RFgUB34rSyNCCiClGhQaZw5yJkLmGJJVKVd171nr/2Hufs8+tSkiFpFIJ389jJEPde889Z++95rWIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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jBAAAAABJRU5ErkJggg==";
const fmt  = n => new Intl.NumberFormat("en-US",{style:"currency",currency:"USD"}).format(n||0);
const pctf = n => `${(+n||0).toFixed(1)}%`;
const uid  = () => Math.random().toString(36).slice(2,10);
const today= () => new Date().toISOString().split("T")[0];
const subTot  = s => (s.cost||0)+(s.inclTax?((s.cost||0)*(s.taxRate||0))/100:0);
const projCost= p => (p.subs||[]).reduce((t,s)=>t+(s.cost||0),0); /* pre-tax sub costs only */
const projBase= p => p.subTotal||p.value||0; /* pre-tax revenue */
const isDebit= s => (s.company||"").trim().toLowerCase()==="debit card transaction";
const getPmPct= (uname,est,sp) => {
  if(uname==="cory")  return (sp.pm_cory||20)+(est?(sp.pm_cory_bonus||5):0);
  if(uname==="ivan")  return sp.pm_ivan||25;
  if(uname==="yuriy") return sp.pm_yuriy||25;
  if(uname==="mark")  return sp.pm_mark||25;
  return 0;
};
async function seedOnce(){
  const done=await _l("pr_seeded9",false);
  if(done)return;
  await _s("pr_users9",USERS.map(u=>({...u,pass:"Paliy"})));
  await _s("pr_seeded9",true);
}

/* ── Primitives ── */
function Lbl({t}){ return t?<div style={{fontSize:11,fontWeight:700,color:MU,textTransform:"uppercase",letterSpacing:"0.6px",marginBottom:5}}>{t}</div>:null; }
function Inp({label,value,onChange,type="text",placeholder="",mb=14}){
  const[f,sf]=useState(false);
  return <div style={{marginBottom:mb}}>
    <Lbl t={label}/>
    <input type={type} value={value} onChange={e=>onChange(e.target.value)} placeholder={placeholder}
      onWheel={e=>e.target.blur()}
      onFocus={()=>sf(true)} onBlur={()=>sf(false)}
      style={{width:"100%",padding:"12px 14px",background:CR,border:`1.5px solid ${f?AC:BD}`,borderRadius:10,fontFamily:"inherit",fontSize:15,color:N,outline:"none",boxSizing:"border-box",transition:"border-color .15s"}}/>
  </div>;
}
function Sel({label,value,onChange,options,mb=14}){
  return <div style={{marginBottom:mb}}>
    <Lbl t={label}/>
    <select value={value} onChange={e=>onChange(e.target.value)}
      style={{width:"100%",padding:"12px 14px",background:CR,border:`1.5px solid ${BD}`,borderRadius:10,fontFamily:"inherit",fontSize:15,color:N,outline:"none",appearance:"none",cursor:"pointer"}}>
      {options.map(o=><option key={o.v} value={o.v}>{o.l}</option>)}
    </select>
  </div>;
}
function Btn({children,onClick,full,outline,color=N,disabled,sm}){
  return <button onClick={onClick} disabled={disabled}
    style={{display:"block",width:full?"100%":"auto",padding:sm?"9px 16px":"13px 20px",background:outline?W:color,border:`2px solid ${color}`,borderRadius:10,color:outline?color:W,fontFamily:"inherit",fontSize:sm?13:15,fontWeight:700,cursor:disabled?"default":"pointer",textAlign:"center",opacity:disabled?.5:1,transition:"opacity .15s",boxSizing:"border-box"}}
    onMouseEnter={e=>{if(!disabled)e.currentTarget.style.opacity=".78";}} onMouseLeave={e=>e.currentTarget.style.opacity=disabled?"0.5":"1"}>
    {children}
  </button>;
}
function Tog({on,onChange,label,color=AC}){
  return <div style={{display:"flex",alignItems:"center",gap:9,cursor:"pointer"}} onClick={()=>onChange(!on)}>
    <div style={{width:40,height:22,borderRadius:11,background:on?color:BD,position:"relative",transition:"background .2s",flexShrink:0}}>
      <div style={{position:"absolute",top:2,left:on?20:2,width:18,height:18,borderRadius:"50%",background:W,transition:"left .18s",boxShadow:"0 1px 4px rgba(0,0,0,0.25)"}}/>
    </div>
    {label&&<span style={{fontSize:13,color:N,userSelect:"none"}}>{label}</span>}
  </div>;
}
function StateTog({value,onChange}){
  return <div style={{marginBottom:14}}>
    <Lbl t="State"/>
    <div style={{display:"flex",background:CR,border:`1.5px solid ${BD}`,borderRadius:10,overflow:"hidden"}}>
      {[["WA","Washington"],["OR","Oregon"]].map(([v,l])=>(
        <button key={v} onClick={()=>onChange(v)}
          style={{flex:1,padding:"12px 0",border:"none",cursor:"pointer",fontFamily:"inherit",fontSize:14,fontWeight:600,background:value===v?N:"transparent",color:value===v?W:MU,transition:"all .15s"}}>{l}</button>
      ))}
    </div>
  </div>;
}
function Sheet({title,onClose,children}){
  return <div style={{position:"fixed",inset:0,background:"rgba(0,0,0,0.5)",display:"flex",flexDirection:"column",justifyContent:"flex-end",zIndex:999}} onClick={e=>e.target===e.currentTarget&&onClose()}>
    <div style={{background:W,borderRadius:"20px 20px 0 0",maxHeight:"92vh",overflowY:"auto"}}>
      <div style={{display:"flex",justifyContent:"space-between",alignItems:"center",padding:"16px 20px 14px",borderBottom:`1px solid ${BD}`,position:"sticky",top:0,background:W,zIndex:1}}>
        <div style={{fontSize:17,fontWeight:700,color:N}}>{title}</div>
        <button onClick={onClose} style={{background:"none",border:"none",cursor:"pointer",fontSize:26,color:MU,lineHeight:1}}>×</button>
      </div>
      <div style={{padding:"16px 20px 32px"}}>{children}</div>
    </div>
  </div>;
}
function Badge({label,color,bg}){ return <span style={{fontSize:11,fontWeight:700,padding:"3px 10px",borderRadius:20,background:bg,color,whiteSpace:"nowrap"}}>{label}</span>; }
function Card({children,mb=0}){ return <div style={{background:W,border:`1px solid ${BD}`,borderRadius:14,padding:16,marginBottom:mb}}>{children}</div>; }
function Divider(){ return <div style={{height:1,background:BD,margin:"12px 0"}}/>; }
function SplitRow({l,v,c=MU}){ return <div style={{display:"flex",justifyContent:"space-between",alignItems:"center"}}>
  <span style={{fontSize:12,color:MU}}>{l}</span>
  <span style={{fontFamily:"'Nunito',sans-serif",fontSize:14,fontWeight:800,color:c}}>{v}</span>
</div>; }
function Empty({text}){ return <div style={{color:MU,fontSize:14,padding:"12px 4px"}}>{text}</div>; }

/* ── Completion Slider ── */
function CompletionSlider({value=0,onChange,maxValue=100}){
  const[local,setLocal]=useState(value||0);
  const dragging=useRef(false);
  if(!dragging.current && local!==(value||0)) setLocal(value||0);

  const col=local===100?GR:local>=50?AC:BL;
  return <div style={{marginBottom:12}} onClick={e=>e.stopPropagation()}>
    <div style={{display:"flex",justifyContent:"space-between",marginBottom:5}}>
      <span style={{fontSize:10,fontWeight:700,color:MU,textTransform:"uppercase",letterSpacing:"0.5px"}}>Completion</span>
      <span style={{fontFamily:"'Nunito',sans-serif",fontSize:13,fontWeight:900,color:col}}>{local}%</span>
    </div>
    <div style={{position:"relative",height:8,background:BD,borderRadius:4}}>
      <div style={{position:"absolute",top:0,left:0,height:"100%",width:`${local}%`,background:col,borderRadius:4}}/>
      <input type="range" min="0" max={maxValue} step="10" value={Math.min(local,maxValue)}
        onChange={e=>{ dragging.current=true; setLocal(Math.min(+e.target.value,maxValue)); }}
        onPointerUp={e=>{ dragging.current=false; onChange(Math.min(+e.target.value,maxValue)); }}
        onMouseUp={e=>{  dragging.current=false; onChange(Math.min(+e.target.value,maxValue)); }}
        style={{position:"absolute",top:-8,left:0,width:"100%",height:24,opacity:0,cursor:"pointer",margin:0,padding:0}}/>
    </div>
    <div style={{display:"flex",justifyContent:"space-between",marginTop:3}}>
      {[0,25,50,75,100].map(v=><span key={v} style={{fontSize:9,color:MU,opacity:v>maxValue?0.3:1}}>{v}%</span>)}
    </div>
  </div>;
}

/* ── Auth ── */
function Auth({onLogin}){
  const[uname,setUname]=useState(""),  [pass,setPass]=useState(""), [err,setErr]=useState("");
  const submit=async()=>{
    setErr("");
    const users=await _l("pr_users9",[]);
    const u=users.find(x=>x.username.toLowerCase()===uname.toLowerCase().trim()&&x.pass===pass);
    if(!u){setErr("Incorrect username or password.");return;}
    const prof=USERS.find(x=>x.id===u.id);
    onLogin({...u,role:prof?.role||u.role});
  };
  return <div style={{minHeight:"100vh",background:N,display:"flex",flexDirection:"column",alignItems:"center",justifyContent:"center",padding:24,fontFamily:"'Plus Jakarta Sans',sans-serif"}}>
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&family=Nunito:wght@700;800;900&display=swap" rel="stylesheet"/>
    <style>{`input[type=number]::-webkit-inner-spin-button,input[type=number]::-webkit-outer-spin-button{-webkit-appearance:none;margin:0}input[type=number]{-moz-appearance:textfield}`}</style>
    <div style={{width:"100%",maxWidth:380}}>
      <div style={{textAlign:"center",marginBottom:36}}>
        <img src={LOGO} alt="Paliy" style={{height:220,objectFit:"contain",filter:"brightness(0) invert(1)",display:"block",margin:"0 auto"}}/>
      </div>
      <div style={{background:W,borderRadius:22,padding:30,boxShadow:"0 24px 64px rgba(0,0,0,0.45)"}}>
        <div style={{fontSize:20,fontWeight:800,color:N,marginBottom:22,textAlign:"center"}}>Team Sign In</div>
        <Inp label="Username" value={uname} onChange={setUname} placeholder="e.g. nazar"/>
        <Inp label="Password" value={pass}  onChange={setPass}  type="password" placeholder="••••••••"/>
        {err&&<div style={{color:RE,fontSize:13,marginBottom:14,textAlign:"center"}}>{err}</div>}
        <Btn onClick={submit} full>Sign In</Btn>
      </div>
    </div>
  </div>;
}

/* ── App ── */
function App(){
  const[user,setUser]=useState(null);
  const[tab,setTab]=useState("projects");
  const[projs,setProjs]=useState([]);
  const[subCos,setSubCos]=useState([]);
  const[splits,setSplits]=useState(DEF_SPLITS);
  const[selId,setSelId]=useState(null);
  const[ready,setReady]=useState(false);
  const lastSave=useRef(0);
  const scrollRef=useRef(null);

  const refresh=useCallback(async(force=false)=>{
    if(!force&&Date.now()-lastSave.current<30000)return;
    const[p,c,s]=await Promise.all([_sl("pr_p9",[]),_sl("pr_sc9",[]),_sl("pr_sp9",DEF_SPLITS)]);
    setProjs(p);setSubCos(c);setSplits(s);
  },[]);

  useEffect(()=>{(async()=>{ await seedOnce(); await refresh(true); setReady(true); })();},[]);
  useEffect(()=>{ if(!ready)return; const id=setInterval(()=>refresh(),30000); return()=>clearInterval(id); },[ready,refresh]);

  const sp=async v=>{lastSave.current=Date.now();setProjs(v);   await _ss("pr_p9",v);  lastSave.current=Date.now();};
  const sc=async v=>{lastSave.current=Date.now();setSubCos(v);  await _ss("pr_sc9",v); lastSave.current=Date.now();};
  const ss=async v=>{lastSave.current=Date.now();setSplits(v);  await _ss("pr_sp9",v); lastSave.current=Date.now();};

  if(!ready)return <div style={{height:"100vh",background:N}}/>;
  if(!user) return <Auth onLogin={setUser}/>;

  const isAdmin=user.role==="admin";
  const visible=isAdmin?projs:projs.filter(p=>p.pmId===user.id);
  const sel=visible.find(p=>p.id===selId);
  const navTabs=[
    {id:"projects", label:"Projects"},
    {id:"reports", label:"Reports"},
    {id:"settings", label:"Settings"},
  ];

  const goTab = id => {
    setTab(id); setSelId(null);
    if (scrollRef.current) scrollRef.current.scrollTop = 0;
  };

  return <div style={{minHeight:"100vh",background:CR,fontFamily:"'Plus Jakarta Sans',sans-serif",display:"flex",flexDirection:"column",maxWidth:720,margin:"0 auto",position:"relative"}}>
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&family=Nunito:wght@700;800;900&display=swap" rel="stylesheet"/>
    <style>{`input[type=number]::-webkit-inner-spin-button,input[type=number]::-webkit-outer-spin-button{-webkit-appearance:none;margin:0}input[type=number]{-moz-appearance:textfield}input[type=range]{accent-color:${AC}}`}</style>

    {/* Top bar */}
    <div style={{background:N,padding:"10px 18px",display:"flex",alignItems:"center",justifyContent:"space-between",position:"sticky",top:0,zIndex:10,flexShrink:0}}>
      <img src={LOGO} alt="Paliy" style={{height:52,objectFit:"contain",filter:"brightness(0) invert(1)",opacity:.95}}/>
      <div style={{textAlign:"right"}}>
        <div style={{color:"rgba(255,255,255,0.92)",fontSize:13,fontWeight:700}}>{user.name}</div>
        <div style={{color:"rgba(255,255,255,0.4)",fontSize:10,textTransform:"uppercase",letterSpacing:"0.5px"}}>{isAdmin?"Admin":"Project Manager"}</div>
      </div>
    </div>

    <div ref={scrollRef} style={{flex:1,overflowY:"auto",padding:"16px 16px 64px"}}>
      {selId&&sel
        ?<DetailPage project={sel} projects={projs} subCos={subCos} splits={splits} user={user} isAdmin={isAdmin} onSave={sp} onBack={()=>{ setSelId(null); if(scrollRef.current) scrollRef.current.scrollTop=0; }}/>
        :tab==="projects"?<ProjectsPage projects={visible} allProjects={projs} subCos={subCos} splits={splits} user={user} isAdmin={isAdmin} onSave={sp} onSelect={id=>{ setSelId(id); if(scrollRef.current) scrollRef.current.scrollTop=0; }}/>
        :tab==="reports"?<ReportsPage projects={isAdmin?projs:projs.filter(p=>p.pmId===user.id)} isAdmin={isAdmin} user={user}/>
        :<SettingsPage subCos={subCos} splits={splits} isAdmin={isAdmin} onSaveSubs={sc} onSaveSplits={ss}/>
      }
    </div>

    {!selId&&<div style={{position:"fixed",bottom:0,left:"50%",transform:"translateX(-50%)",width:"100%",maxWidth:520,background:W,borderTop:`2px solid ${N}`,display:"flex",zIndex:10,boxShadow:"0 -4px 12px rgba(0,0,0,0.08)"}}>
      {navTabs.map(t=><button key={t.id} onClick={()=>goTab(t.id)}
        style={{flex:1,padding:"12px 0",background:"none",border:"none",cursor:"pointer",fontFamily:"inherit",display:"flex",flexDirection:"column",alignItems:"center",gap:2}}>
        <span style={{fontSize:12,fontWeight:800,color:tab===t.id?AC:N,textTransform:"uppercase",letterSpacing:"0.6px"}}>{t.label}</span>
        {tab===t.id&&<div style={{width:16,height:2,background:AC,borderRadius:1}}/>}
      </button>)}
      <button onClick={()=>{setUser(null);setSelId(null);setTab("projects");}}
        style={{flex:1,padding:"12px 0",background:"none",border:"none",cursor:"pointer",fontFamily:"inherit",display:"flex",flexDirection:"column",alignItems:"center",gap:2}}>
        <span style={{fontSize:12,fontWeight:800,color:N,textTransform:"uppercase",letterSpacing:"0.6px"}}>Log Out</span>
      </button>
    </div>}
  </div>;
}

/* ── Project Row ── */
function ProjectRow({p,onSelect,isAdmin,onSave,allProjects}){
  const pmts=p.payments||[];
  const totalPd=p.paidInFull?(p.value||0):pmts.reduce((s,x)=>s+x.amount,0);
  const bal=p.paidInFull?0:(p.value||0)-totalPd;
  const pmUser=USERS.find(u=>u.id===p.pmId);
  const pct=p.completion||0;
  const col=pct===100?GR:pct>=60?AC:BL;
  const base=projBase(p), cost=projCost(p), gp=base-cost;
  const gpPct=base?(gp/base)*100:0;
  const pmCut=(gp*(p.pmPct||0))/100;
  const isFinished=p.status==="completed"&&p.paidInFull;
  return <div onClick={()=>onSelect(p.id)} style={{background:W,border:`1px solid ${BD}`,borderRadius:14,padding:"14px 16px",marginBottom:10,cursor:"pointer",transition:"box-shadow .15s"}}
    onMouseEnter={e=>e.currentTarget.style.boxShadow="0 4px 16px rgba(26,47,74,0.12)"}
    onMouseLeave={e=>e.currentTarget.style.boxShadow="none"}>
    {/* Slim read-only progress bar */}
    <div style={{marginBottom:10}}>
      <div style={{display:"flex",justifyContent:"space-between",marginBottom:4}}>
        <span style={{fontSize:10,fontWeight:700,color:MU,textTransform:"uppercase",letterSpacing:"0.5px"}}>Completion</span>
        <span style={{fontFamily:"'Nunito',sans-serif",fontSize:12,fontWeight:900,color:col}}>{pct}%</span>
      </div>
      <div style={{height:6,background:BD,borderRadius:3,overflow:"hidden"}}>
        <div style={{height:"100%",width:`${pct}%`,background:col,borderRadius:3}}/>
      </div>
    </div>
    <div style={{display:"flex",justifyContent:"space-between",alignItems:"flex-start"}}>
      <div style={{flex:1,minWidth:0,marginRight:12}}>
        <div style={{fontSize:16,fontWeight:700,color:N,marginBottom:5}}>{p.name}</div>
        <div style={{display:"flex",gap:6,flexWrap:"wrap",alignItems:"center"}}>
          <Badge label={p.status==="completed"?"Completed":"Active"} color={p.status==="completed"?BL:GR} bg={p.status==="completed"?`${BL}18`:`${GR}18`}/>
          <Badge label={p.state||"WA"} color={p.state==="OR"?GR:N} bg={p.state==="OR"?`${GR}15`:`${N}10`}/>
          {isAdmin&&pmUser&&<Badge label={pmUser.name} color={MU} bg={`${MU}12`}/>}
        </div>
      </div>
      <div style={{textAlign:"right",flexShrink:0}}>
        {isAdmin?(
          <>
            <div style={{fontFamily:"'Nunito',sans-serif",fontSize:18,fontWeight:900,color:N}}>{fmt(p.value||0)}</div>
            <div style={{fontSize:12,fontWeight:600,color:p.paidInFull?GR:bal>0?RE:MU,marginTop:2}}>
              {p.paidInFull?"✓ Paid in Full":bal>0?`${fmt(bal)} owed`:`${pmts.length} check${pmts.length!==1?"s":""}${pmts.length===0?" — unpaid":""}`}
            </div>
          </>
        ):(
          isFinished?(
            <>
              <div style={{fontSize:11,fontWeight:600,color:MU,marginBottom:2}}>commission · <span style={{color:gpPct>=30?GR:gpPct>=15?AC:RE}}>{pctf(gpPct)} GP</span></div>
              <div style={{fontFamily:"'Nunito',sans-serif",fontSize:18,fontWeight:900,color:AC}}>{fmt(pmCut)}</div>
            </>
          ):(
            <div style={{fontFamily:"'Nunito',sans-serif",fontSize:16,fontWeight:900,color:N}}>TBD</div>
          )
        )}
      </div>
    </div>
  </div>;
}

/* ── Projects Page ── */
function ProjectsPage({projects,allProjects,subCos,splits,user,isAdmin,onSave,onSelect}){
  const[showNew,setShowNew]=useState(false);
  const srt=arr=>[...arr].sort((a,b)=>(b.completedDate||b.createdAt||"").localeCompare(a.completedDate||a.createdAt||""));
  const running  =[...projects.filter(p=>!(p.status==="completed"&&p.paidInFull))].sort((a,b)=>(b.completion||0)-(a.completion||0));
  const needSplit=isAdmin?srt(projects.filter(p=>p.status==="completed"&&p.paidInFull&&!p.splitDone)):[];
  const done     =isAdmin?srt(projects.filter(p=>p.status==="completed"&&p.paidInFull&&p.splitDone)):srt(projects.filter(p=>p.status==="completed"&&p.paidInFull&&p.splitDone));
  const awaitingComm=!isAdmin?srt(projects.filter(p=>p.status==="completed"&&p.paidInFull&&!p.splitDone)):[];

  // Jobs with undeposited checks OR subs marked ready to pay but not yet paid
  const collectDeposit=isAdmin?srt(projects.filter(p=>
    !needSplit.find(n=>n.id===p.id) && (
      (p.payments||[]).some(pmt=>!pmt.deposited) ||
      (p.subs||[]).some(s=>s.readyToPay&&(!s.payStatus||s.payStatus==="unpaid"||s.payStatus==="partial")&&!isDebit(s))
    )
  )):[];

  const rp={onSelect,isAdmin,onSave:v=>onSave(v),allProjects};
  return <div>
    <div style={{display:"flex",justifyContent:"space-between",alignItems:"center",marginBottom:20}}>
      <div style={{fontSize:22,fontWeight:800,color:N}}>Projects</div>
      <Btn onClick={()=>setShowNew(true)} color={AC} sm>+ New</Btn>
    </div>
    {/* Admin action sections first */}
    {isAdmin&&collectDeposit.length>0&&<Section title="Deposit Payment or Pay Subcontractor" color={AC}>
      {collectDeposit.map(p=>{
        const unhandled=(p.payments||[]).filter(pmt=>!pmt.givenToAdmin&&!pmt.deposited);
        const pendingDeposit=(p.payments||[]).filter(pmt=>pmt.givenToAdmin&&!pmt.deposited);
        const readySubs=(p.subs||[]).filter(s=>s.readyToPay&&(!s.payStatus||s.payStatus==="unpaid"||s.payStatus==="partial")&&!isDebit(s));
        return <div key={p.id}>
          {unhandled.length>0&&<div style={{background:`${AC}12`,border:`1.5px solid ${AC}`,borderRadius:8,padding:"6px 12px",marginBottom:4,fontSize:12,fontWeight:700,color:AC}}>
            ⚠ {unhandled.length} check{unhandled.length!==1?"s":""} not yet handed to you
          </div>}
          {pendingDeposit.length>0&&<div style={{background:`${BL}10`,border:`1px solid ${BL}30`,borderRadius:8,padding:"6px 12px",marginBottom:4,fontSize:12,fontWeight:600,color:BL}}>
            📥 {pendingDeposit.length} check{pendingDeposit.length!==1?"s":""} received — deposit to bank
          </div>}
          {readySubs.length>0&&<div style={{background:`${AC}12`,border:`1.5px solid ${AC}`,borderRadius:8,padding:"6px 12px",marginBottom:4,fontSize:12,fontWeight:700,color:AC}}>
            ⚠ {readySubs.length} sub{readySubs.length!==1?"s":""} ready to be paid
          </div>}
          <ProjectRow p={p} {...rp}/>
        </div>;
      })}
    </Section>}

    {isAdmin&&needSplit.length>0&&<Section title="Need to Split" color={RE}>
      {needSplit.map(p=><ProjectRow key={p.id} p={p} {...rp}/>)}
    </Section>}

    <Section title="Currently Running" color={GR} count={running.length}>
      {running.length===0?<Empty text="No active jobs."/>:running.map(p=><ProjectRow key={p.id} p={p} {...rp}/>)}
    </Section>

    {!isAdmin&&awaitingComm.length>0&&<Section title="Completed — Awaiting Commission" color={AC}>
      {awaitingComm.map(p=><ProjectRow key={p.id} p={p} {...rp}/>)}
    </Section>}

    <Section title={isAdmin?"Completed":"Completed / Commission Paid"} color={BL}>
      {done.length===0?<Empty text="No completed jobs yet."/>:done.map(p=><ProjectRow key={p.id} p={p} {...rp}/>)}
    </Section>
    {showNew&&<Sheet title="New Project" onClose={()=>setShowNew(false)}>
      <NewProjForm splits={splits} user={user} isAdmin={isAdmin}
        onSubmit={p=>{onSave([...allProjects,{...p,id:uid(),subs:[],payments:[],createdAt:today(),status:"active",customerPaid:0,paidInFull:false,splitDone:false,completion:0,deposited:false}]);setShowNew(false);}}/>
    </Sheet>}
  </div>;
}

function Section({title,color,count,children}){
  const countStr=count!=null?` · ${count} ${count===1?"project":"projects"}`:""
  return <div style={{marginBottom:28}}>
    <div style={{display:"flex",alignItems:"center",gap:8,marginBottom:12}}>
      <div style={{width:8,height:8,borderRadius:"50%",background:color,boxShadow:`0 0 6px ${color}`}}/>
      <div style={{fontSize:12,fontWeight:700,color,textTransform:"uppercase",letterSpacing:"0.8px"}}>{title}{countStr}</div>
    </div>
    {children}
  </div>;
}

/* ── New Project Form ── */
function NewProjForm({splits,user,isAdmin,onSubmit}){
  const pms=USERS.filter(u=>u.role==="pm");
  const defPmId=isAdmin?"":user.id;
  const defPmPct=isAdmin?0:getPmPct(user.username,false,splits);
  const[f,setF]=useState({name:"",state:"WA",taxCity:"Vancouver",taxRate:8.9,subTotal:"",pmId:defPmId,pmPct:defPmPct,attendedEst:false,marketingPct:splits.marketing||10,overheadPct:splits.overhead||5});
  const set=(k,v)=>setF(p=>({...p,[k]:v}));
  const onPm=v=>{const u=USERS.find(x=>x.id===v);setF(p=>({...p,pmId:v,pmPct:getPmPct(u?.username||"",false,splits),attendedEst:false}));};
  const onEst=v=>{const u=USERS.find(x=>x.id===f.pmId);setF(p=>({...p,attendedEst:v,pmPct:getPmPct(u?.username||"",v,splits)}));};
  const onState=v=>setF(p=>({...p,state:v,taxCity:v==="OR"?"":p.taxCity||"Vancouver",taxRate:v==="OR"?0:rateForCity(p.taxCity||"Vancouver")}));
  const onCity=v=>setF(p=>({...p,taxCity:v,taxRate:rateForCity(v)}));
  const sub=+f.subTotal||0, tax=f.state==="WA"?sub*(+f.taxRate||0)/100:0, grand=sub+tax;
  const isCory=USERS.find(x=>x.id===f.pmId)?.username==="cory";
  return <div>
    <Inp label="Job Name" value={f.name} onChange={v=>set("name",v)} placeholder="Smith Family — Master Bath"/>
    <StateTog value={f.state} onChange={onState}/>
    {f.state==="WA"&&<Sel label="City / Tax Rate" value={f.taxCity||"Vancouver"} onChange={onCity} options={cityOpts}/>}
    <Inp label={f.state==="WA"?"Sub Total (before tax)":"Project Value ($)"} value={f.subTotal} onChange={v=>set("subTotal",v)} type="number" placeholder="0"/>
    {f.state==="WA"&&sub>0&&<div style={{background:`${AC}10`,border:`1.5px solid ${AC}`,borderRadius:10,padding:"12px 14px",marginBottom:14}}>
      <div style={{display:"flex",justifyContent:"space-between",marginBottom:5}}><span style={{fontSize:13,color:N}}>Sub Total</span><span style={{fontFamily:"'Nunito',sans-serif",fontSize:14,fontWeight:800,color:N}}>{fmt(sub)}</span></div>
      <div style={{display:"flex",justifyContent:"space-between",marginBottom:8}}><span style={{fontSize:13,color:N}}>Tax ({f.taxRate}%)</span><span style={{fontFamily:"'Nunito',sans-serif",fontSize:14,fontWeight:800,color:AC}}>{fmt(tax)}</span></div>
      <div style={{display:"flex",justifyContent:"space-between",borderTop:`1px solid ${AC}30`,paddingTop:8}}><span style={{fontSize:14,fontWeight:700,color:N}}>Grand Total</span><span style={{fontFamily:"'Nunito',sans-serif",fontSize:18,fontWeight:900,color:N}}>{fmt(grand)}</span></div>
    </div>}
    {isAdmin&&<><Sel label="Project Manager" value={f.pmId} onChange={onPm} options={[{v:"",l:"Select PM…"},...pms.map(u=>({v:u.id,l:u.name}))]}/>
    {isCory&&<div style={{marginBottom:14,padding:"12px 14px",background:`${AC}10`,border:`1px solid ${AC}30`,borderRadius:10}}>
      <Tog on={f.attendedEst} onChange={onEst} label={`Cory attended estimate (+${splits.pm_cory_bonus||5}% = ${getPmPct("cory",f.attendedEst,splits)}% total)`}/>
    </div>}</>}
    {/* Cory sees this on his own new projects */}
    {!isAdmin&&user.username==="cory"&&(
      <div style={{marginBottom:14,padding:"12px 14px",background:`${AC}10`,border:`1px solid ${AC}30`,borderRadius:10}}>
        <Tog on={f.attendedEst} onChange={v=>{setF(p=>({...p,attendedEst:v,pmPct:getPmPct("cory",v,splits)}));}} label={`I also estimated this project (+${splits.pm_cory_bonus||5}%)`} color={AC}/>
        {f.attendedEst&&<div style={{fontSize:11,color:MU,marginTop:5}}>Your commission: {getPmPct("cory",true,splits)}% · Admin will verify before splitting.</div>}
      </div>
    )}
    <Btn full onClick={()=>{if(!f.name.trim()||!sub)return;onSubmit({...f,subTotal:sub,value:grand,customerTax:tax,pmPct:+f.pmPct||0,marketingPct:+f.marketingPct||0,overheadPct:+f.overheadPct||0,taxRate:+f.taxRate||0});}}>Create Project</Btn>
  </div>;
}

/* ── Detail Page ── */
function DetailPage({project,projects,subCos,splits,user,isAdmin,onSave,onBack}){
  const[showSubSheet,setShowSubSheet]=useState(false);
  const[editSub,setEditSub]=useState(null);
  const[showEdit,setShowEdit]=useState(false);
  const[showOverride,setShowOverride]=useState(false);

  const upd=u=>onSave(projects.map(p=>p.id===u.id?u:p));

  // Weekly completion update gate — active jobs only
  const daysSinceUpdate = () => {
    if (!project.completionUpdatedAt) return 999;
    const then = new Date(project.completionUpdatedAt);
    const now  = new Date();
    return Math.floor((now - then) / (1000*60*60*24));
  };
  const isActive = project.status !== "completed";
  const needsWeeklyUpdate = isActive && daysSinceUpdate() >= 1;
  const[weeklyDone, setWeeklyDone] = useState(!needsWeeklyUpdate);

  const[balanceWarning, setBalanceWarning] = useState(false);

  const onCompletionChange = v => {
    // Non-admin can't reach 100% while balance is still owed or checks not handed to admin
    const uncheckedChecks = pmts.filter(p => !p.givenToAdmin);
    const blocked = !isAdmin && v === 100 && (bal > 0 || uncheckedChecks.length > 0);
    if (blocked) {
      setBalanceWarning(true);
      upd({...project, completion:99, completionUpdatedAt:today()});
      setWeeklyDone(true);
      return;
    }
    setBalanceWarning(false);
    const wasAutoCompleted = project.completion===100 && project.status==="completed";
    const extra = v===100
      ? {status:"completed", completedDate:project.completedDate||today()}
      : (wasAutoCompleted ? {status:"active", completedDate:null, paidInFull:false} : {});
    upd({...project, completion:v, completionUpdatedAt:today(), ...extra});
    setWeeklyDone(true);
  };

  // Wrap all save actions to require weekly update first
  const guarded = fn => (...args) => {
    if (!weeklyDone) return;
    fn(...args);
  };

  const onStatus=v=>upd({...project,status:v?"completed":"active",completedDate:v?today():null,...(v?{completion:100,completionUpdatedAt:today()}:{})});
  const addPmt=amt=>{
    const rate=project.state==="WA"?(+project.taxRate||0):0;
    const tax=rate>0?amt-amt/(1+rate/100):0;
    const newPmts=[...(project.payments||[]),{id:uid(),amount:amt,date:today(),tax}];
    const newTotal=newPmts.reduce((s,p)=>s+p.amount,0);
    const nowFull=Math.abs(newTotal-(project.value||0))<0.02;
    upd({...project,payments:newPmts,customerPaid:newTotal,customerTax:newPmts.reduce((s,p)=>s+(p.tax||0),0),
      paidInFull:nowFull});
  };
  const delPmt=id=>{
    const newPmts=(project.payments||[]).filter(p=>p.id!==id);
    const newTotal=newPmts.reduce((s,p)=>s+p.amount,0);
    const nowFull=newPmts.length>0&&Math.abs(newTotal-(project.value||0))<0.02;
    upd({...project,payments:newPmts,customerPaid:newTotal,customerTax:newPmts.reduce((s,p)=>s+(p.tax||0),0),paidInFull:nowFull});
  };
  const delSub=id=>upd({...project,subs:(project.subs||[]).filter(s=>s.id!==id)});
  const addSubFn = s => {
    const entry = isDebit(s)?{...s,payStatus:"paid",readyToPay:false}:s;
    const u=editSub?{...project,subs:(project.subs||[]).map(x=>x.id===entry.id?entry:x)}:{...project,subs:[...(project.subs||[]),{...entry,id:uid()}]};
    upd(u);setShowSubSheet(false);setEditSub(null);
  };

  const subs=project.subs||[], pmts=project.payments||[];
  const cost=projCost(project);
  const base=projBase(project);
  const gp=base-cost;
  const gpPct=base?(gp/base)*100:0;
  const pmCut=(gp*(project.pmPct||0))/100;
  const mktCut=(gp*(project.marketingPct||0))/100;
  const ovhCut=(gp*(project.overheadPct||0))/100;
  const netPaliy=gp-pmCut-mktCut-ovhCut;
  const pmUser=USERS.find(u=>u.id===project.pmId);
  const totalPaid=project.paidInFull?(project.value||0):pmts.reduce((s,p)=>s+p.amount,0);
  const bal=project.paidInFull?0:(project.value||0)-totalPaid;
  const taxPaidSubs=subs.reduce((s,x)=>s+(x.inclTax?((x.cost||0)*(x.taxRate||0))/100:0),0);

  return <div>
    {/* Header */}
    <div style={{display:"flex",alignItems:"center",gap:10,marginBottom:14}}>
      <button onClick={onBack} style={{background:N,border:"none",color:W,cursor:"pointer",borderRadius:10,width:36,height:36,display:"flex",alignItems:"center",justifyContent:"center",fontSize:18,flexShrink:0}}>←</button>
      <div style={{flex:1,minWidth:0}}>
        <div style={{fontSize:16,fontWeight:700,color:N,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"}}>{project.name}</div>
        <div style={{display:"flex",gap:6,marginTop:3,alignItems:"center"}}>
          <Badge label={project.status==="completed"?"Completed":"Active"} color={project.status==="completed"?BL:GR} bg={project.status==="completed"?`${BL}18`:`${GR}18`}/>
          <Badge label={project.state||"WA"} color={project.state==="OR"?GR:N} bg={project.state==="OR"?`${GR}15`:`${N}10`}/>
        </div>
      </div>
      <Btn onClick={()=>setShowEdit(true)} color={N} sm outline>Edit</Btn>
    </div>

    {/* Completion slider — always shown, gate unlock for active jobs */}
    <div style={{background:W,border:`2px solid ${needsWeeklyUpdate&&!weeklyDone?AC:BD}`,borderRadius:14,padding:16,marginBottom:10}}>
      {needsWeeklyUpdate&&!weeklyDone&&(
        <div style={{background:`${AC}12`,border:`1px solid ${AC}30`,borderRadius:10,padding:"10px 14px",marginBottom:12,display:"flex",alignItems:"center",gap:10}}>
          <span style={{fontSize:18}}>⏰</span>
          <div>
            <div style={{fontSize:13,fontWeight:700,color:AC}}>Daily update required</div>
            <div style={{fontSize:12,color:MU}}>Update completion % daily to unlock this job</div>
          </div>
        </div>
      )}
      <CompletionSlider value={project.completion||0} onChange={onCompletionChange}
        maxValue={(!isAdmin&&(bal>0||pmts.some(p=>!p.givenToAdmin)))?99:100}/>
      {project.completionUpdatedAt&&(
        <div style={{fontSize:11,color:MU,marginTop:2}}>Last updated: {project.completionUpdatedAt}</div>
      )}
      {balanceWarning&&<div style={{marginTop:10,padding:"10px 12px",background:`${AC}12`,border:`1px solid ${AC}40`,borderRadius:8,display:"flex",alignItems:"center",gap:8}}>
        <span style={{fontSize:16}}>⚠️</span>
        <div>
          <div style={{fontSize:12,fontWeight:700,color:AC}}>Can't mark 100% yet</div>
          {bal>0&&<div style={{fontSize:11,color:MU}}>Balance due: {fmt(bal)} — collect full payment first</div>}
          {pmts.some(p=>!p.givenToAdmin)&&<div style={{fontSize:11,color:MU}}>Check all payments as "given to admin" first</div>}
        </div>
      </div>}
      {needsWeeklyUpdate&&!weeklyDone&&(
        <div style={{marginTop:10,padding:"8px 12px",background:`${MU}10`,borderRadius:8,fontSize:12,color:MU,textAlign:"center"}}>
          Move the slider above to unlock all other sections
        </div>
      )}
    </div>

    {/* Everything below locked until weekly update done */}
    <div style={{opacity:weeklyDone?1:0.35,pointerEvents:weeklyDone?"auto":"none",transition:"opacity .3s"}}>

    {/* Financials */}
    <Card mb={10}>
      <div style={{display:"grid",gridTemplateColumns:"1fr 1fr",gap:0}}>
        <div style={{paddingRight:14,borderRight:`1px solid ${BD}`}}>
          <div style={{fontSize:10,fontWeight:700,color:MU,textTransform:"uppercase",marginBottom:3}}>Project Value</div>
          <div style={{fontFamily:"'Nunito',sans-serif",fontSize:22,fontWeight:900,color:N}}>{fmt(project.value)}</div>
        </div>
        <div style={{paddingLeft:14}}>
          <div style={{fontSize:10,fontWeight:700,color:MU,textTransform:"uppercase",marginBottom:3}}>Sub Costs</div>
          <div style={{fontFamily:"'Nunito',sans-serif",fontSize:22,fontWeight:900,color:RE}}>{fmt(cost)}</div>
        </div>
      </div>
      {!isAdmin&&gp!==0&&<div style={{marginTop:8,padding:"6px 10px",background:gp>=0?`${GR}10`:`${RE}10`,borderRadius:8,display:"flex",justifyContent:"space-between",alignItems:"center"}}>
        <span style={{fontSize:11,fontWeight:700,color:MU,textTransform:"uppercase",letterSpacing:"0.4px"}}>{project.status!=="completed"?"Current Gross Profit":"Final Gross Profit"}</span>
        <span style={{fontFamily:"'Nunito',sans-serif",fontSize:14,fontWeight:900,color:gp>=0?GR:RE}}>{fmt(gp)} <span style={{fontSize:11,fontWeight:600}}>({pctf(gpPct)})</span></span>
      </div>}

      {isAdmin&&<>
        <Divider/>
        <div style={{marginBottom:10}}>
          <div style={{fontSize:10,fontWeight:700,color:MU,textTransform:"uppercase",marginBottom:2}}>{project.status!=="completed"?"Current Gross Profit":"Final Gross Profit"}</div>
          <div style={{fontFamily:"'Nunito',sans-serif",fontSize:24,fontWeight:900,color:gp>=0?GR:RE}}>
            {fmt(gp)} <span style={{fontSize:13,color:MU,fontFamily:"'Plus Jakarta Sans',sans-serif",fontWeight:500}}>({pctf(gpPct)})</span>
          </div>
        </div>

        {/* Admin % overrides — hidden behind toggle */}
        <div style={{background:CR,borderRadius:10,padding:"10px 14px",marginBottom:10}}>
          <div onClick={()=>setShowOverride(v=>!v)} style={{display:"flex",justifyContent:"space-between",alignItems:"center",cursor:"pointer"}}>
            <div style={{fontSize:10,fontWeight:700,color:MU,textTransform:"uppercase"}}>Override Splits</div>
            <div style={{width:36,height:20,borderRadius:10,background:showOverride?AC:BD,position:"relative",transition:"background .2s",flexShrink:0}}>
              <div style={{position:"absolute",top:2,left:showOverride?18:2,width:16,height:16,borderRadius:"50%",background:W,transition:"left .2s"}}/>
            </div>
          </div>
          {showOverride&&<div style={{display:"grid",gridTemplateColumns:"1fr 1fr 1fr",gap:8,marginTop:10}}>
            {[["PM %","pmPct"],["Mkt %","marketingPct"],["OH %","overheadPct"]].map(([lbl,key])=>(
              <div key={key}>
                <div style={{fontSize:10,color:MU,marginBottom:3}}>{lbl}</div>
                <input type="number" min="0" max="100" step="0.5"
                  value={project[key]||0}
                  onChange={e=>upd({...project,[key]:+e.target.value})}
                  onWheel={e=>e.target.blur()}
                  style={{width:"100%",padding:"6px 8px",borderRadius:7,border:`1px solid ${BD}`,fontFamily:"inherit",fontSize:13,fontWeight:700,color:N,background:W}}/>
              </div>
            ))}
          </div>}
        </div>

        {(()=>{
          const taxCollected=project.paidInFull?(project.customerTax||0):pmts.reduce((s,p)=>s+(p.tax||0),0);
          const unpaidSubs=(project.subs||[]).filter(s=>!s.payStatus||s.payStatus==="unpaid"||s.payStatus==="partial");
          const subsAllPaid=unpaidSubs.length===0;
          const readyToSplit=project.paidInFull&&subsAllPaid;
          return <>
            {readyToSplit
              ? <>
                  <div style={{display:"grid",gap:8}}>
                    {pmUser&&<SplitRow l={`${pmUser.name} (${project.pmPct||0}%)`} v={fmt(pmCut)} c={AC}/>}
                    <SplitRow l={`Marketing (${project.marketingPct||0}%)`} v={fmt(mktCut)} c={BL}/>
                    <SplitRow l={`Overhead (${project.overheadPct||0}%)`}   v={fmt(ovhCut)} c={MU}/>
                  </div>
                  <div style={{background:`${GR}12`,border:`1px solid ${GR}28`,borderRadius:10,padding:"10px 14px",marginTop:10,display:"flex",justifyContent:"space-between",alignItems:"center"}}>
                    <span style={{fontSize:13,fontWeight:700,color:GR}}>Paliy Net</span>
                    <span style={{fontFamily:"'Nunito',sans-serif",fontSize:20,fontWeight:900,color:GR}}>{fmt(netPaliy)}</span>
                  </div>
                  {project.state==="WA"&&<><Divider/>
                    <div style={{fontSize:11,fontWeight:700,color:MU,textTransform:"uppercase",letterSpacing:"0.5px",marginBottom:8}}>Sales Tax</div>
                    <div style={{display:"grid",gap:7}}>
                      <SplitRow l="Collected from Customer" v={fmt(taxCollected)} c={AC}/>
                      <SplitRow l="Paid to Subs / Materials" v={`− ${fmt(taxPaidSubs)}`} c={RE}/>
                    </div>
                    <div style={{background:`${AC}12`,border:`1px solid ${AC}35`,borderRadius:10,padding:"10px 14px",marginTop:8,display:"flex",justifyContent:"space-between",alignItems:"center"}}>
                      <span style={{fontSize:13,fontWeight:700,color:AC}}>Transfer to Tax Account</span>
                      <span style={{fontFamily:"'Nunito',sans-serif",fontSize:18,fontWeight:900,color:AC}}>{fmt(taxCollected-taxPaidSubs)}</span>
                    </div>
                  </>}
                </>
              : <div style={{background:`${MU}08`,border:`1px solid ${BD}`,borderRadius:10,padding:"10px 14px",marginTop:4}}>
                  <div style={{fontSize:12,color:MU,textAlign:"center"}}>
                    Final split amounts will show once job is <strong>paid in full</strong> and all subs are <strong>marked paid</strong>
                  </div>
                  {project.state==="WA"&&(()=>{const tc=pmts.reduce((s,p)=>s+(p.tax||0),0);return tc>0?<div style={{marginTop:8,display:"flex",justifyContent:"space-between"}}>
                    <span style={{fontSize:12,color:MU}}>Tax collected so far</span>
                    <span style={{fontSize:12,fontWeight:700,color:AC}}>{fmt(tc)}</span>
                  </div>:null;})()}
                </div>
            }
          </>;
        })()}
      </>}

        {/* Split Done gate — admin only */}
        {isAdmin&&project.status==="completed"&&project.paidInFull&&(()=>{
          const unpaidSubs=(project.subs||[]).filter(s=>!s.payStatus||s.payStatus==="unpaid"||s.payStatus==="partial");
          const subsAllPaid=unpaidSubs.length===0;
          const allDeposited=pmts.length===0||pmts.every(p=>p.deposited);
          const readyToSplit=subsAllPaid&&allDeposited;
          return <><Divider/>
            {!subsAllPaid&&(
              <div style={{background:`${RE}08`,border:`1px solid ${RE}25`,borderRadius:10,padding:"10px 14px",marginBottom:10}}>
                <div style={{fontSize:12,fontWeight:700,color:RE,marginBottom:4}}>⚠ Cannot split yet</div>
                <div style={{fontSize:12,color:MU}}>{unpaidSubs.length} sub/material{unpaidSubs.length!==1?"s":""} still unpaid or partial:</div>
                {unpaidSubs.map(s=><div key={s.id} style={{fontSize:11,color:RE,marginTop:3}}>
                  · {s.nickname?`${s.company} (${s.nickname})`:s.company} — {s.payStatus==="partial"?"Partial":"Unpaid"}
                </div>)}
              </div>
            )}
            {!allDeposited&&pmts.length>0&&(
              <div style={{background:`${AC}08`,border:`1px solid ${AC}25`,borderRadius:10,padding:"10px 14px",marginBottom:10}}>
                <div style={{fontSize:12,fontWeight:700,color:AC,marginBottom:2}}>⚠ Mark all checks as deposited first</div>
                <div style={{fontSize:11,color:MU}}>{pmts.filter(p=>!p.deposited).length} check{pmts.filter(p=>!p.deposited).length!==1?"s":""} not yet deposited</div>
              </div>
            )}
            <div style={{opacity:readyToSplit?1:0.4,pointerEvents:readyToSplit?"auto":"none"}}>
              {pmUser?.username==="cory"&&project.attendedEst&&(
                <div style={{background:`${AC}12`,border:`1.5px solid ${AC}`,borderRadius:10,padding:"12px 14px",marginBottom:12}}>
                  <div style={{fontSize:13,fontWeight:700,color:AC,marginBottom:4}}>⚠ Verify estimate with manager</div>
                  <div style={{fontSize:12,color:N,lineHeight:1.5}}>Cory claimed he attended the estimate on this job (+{splits.pm_cory_bonus||5}% bonus = {project.pmPct||0}% total). Please confirm with the manager before splitting.</div>
                </div>
              )}
              <div style={{background:`${BL}08`,border:`1px solid ${BL}25`,borderRadius:10,padding:"10px 14px",display:"flex",justifyContent:"space-between",alignItems:"center"}}>
                <span style={{fontSize:13,fontWeight:700,color:BL}}>Job Split Completed</span>
                <Tog on={project.splitDone||false} onChange={v=>upd({...project,splitDone:v})} color={BL}/>
              </div>
            </div>
          </>;
        })()}

      {!isAdmin&&pmUser&&<>
        <Divider/>
        <SplitRow l={`${project.status!=="completed"?"Your Current Commission":"Your Final Commission"} (${project.pmPct||0}%)`} v={fmt(pmCut)} c={AC}/>
      </>}
    </Card>

    {/* Project Completed toggle — PMs only, admin doesn't need it */}
    {!isAdmin&&<Card mb={10}>
      {(()=>{
        const noSubs=subs.length===0;
        const pmBlocked=(bal>0||noSubs)&&project.status!=="completed";
        return <>
          <div style={{background:project.status==="completed"?`${BL}08`:pmBlocked?`${MU}08`:CR,border:`1.5px solid ${project.status==="completed"?BL:pmBlocked?BD:BD}`,borderRadius:10,padding:"11px 14px",cursor:pmBlocked?"default":"pointer",opacity:pmBlocked?0.5:1}}
            onClick={()=>!pmBlocked&&onStatus(project.status!=="completed")}>
            <div style={{fontSize:11,fontWeight:700,color:MU,textTransform:"uppercase",letterSpacing:"0.4px",marginBottom:6}}>Project Completed</div>
            <Tog on={project.status==="completed"} onChange={v=>!pmBlocked&&onStatus(v)} color={BL}/>
          </div>
          {pmBlocked&&<div style={{fontSize:11,color:MU,marginTop:6}}>
            {bal>0&&<div>Balance must be $0 before marking completed</div>}
            {noSubs&&<div>At least 1 sub or material must be added first</div>}
          </div>}
          {project.completedDate&&<div style={{fontSize:12,color:MU,marginTop:8}}>Completed: {project.completedDate}</div>}
        </>;
      })()}
    </Card>}

    {/* Customer Payments */}
    <Card mb={10}>
      <div style={{display:"flex",justifyContent:"space-between",alignItems:"center",marginBottom:12}}>
        <div style={{fontSize:13,fontWeight:700,color:N}}>Customer Payments</div>
        {project.paidInFull&&<span style={{fontSize:12,fontWeight:700,color:GR}}>✓ Paid in Full</span>}
      </div>

      {/* Checks list — always visible */}
      {pmts.length>0&&<div style={{marginBottom:10}}>
        {pmts.map((pmt,i)=>{
          const showTax=project.state==="WA"&&(pmt.tax||0)>0;
          const isDeposited=pmt.deposited||false;
          const isGiven=pmt.givenToAdmin||false;
          const toggleDeposit=()=>{const u=pmts.map(p=>p.id===pmt.id?{...p,deposited:!p.deposited}:p);upd({...project,payments:u});};
          const toggleGiven=()=>{const u=pmts.map(p=>p.id===pmt.id?{...p,givenToAdmin:!p.givenToAdmin}:p);upd({...project,payments:u});};
          return <div key={pmt.id} style={{background:(isAdmin?isDeposited:isGiven)?`${GR}08`:CR,borderRadius:10,padding:"10px 14px",marginBottom:8,border:`1px solid ${(isAdmin?isDeposited:isGiven)?GR:BD}`}}>
            <div style={{display:"flex",justifyContent:"space-between",alignItems:"center"}}>
              <div>
                <div style={{fontSize:12,fontWeight:700,color:MU,marginBottom:showTax?2:0}}>Check #{i+1} · {pmt.date}</div>
                {showTax&&<div style={{fontSize:11,color:MU}}>Pre-tax: {fmt(pmt.amount-(pmt.tax||0))} · Tax: {fmt(pmt.tax||0)}</div>}
                {isAdmin&&<div onClick={toggleDeposit} style={{display:"flex",alignItems:"center",gap:6,marginTop:5,cursor:"pointer"}}>
                  <div style={{width:16,height:16,borderRadius:4,border:`2px solid ${isDeposited?GR:BD}`,background:isDeposited?GR:W,flexShrink:0,display:"flex",alignItems:"center",justifyContent:"center"}}>
                    {isDeposited&&<span style={{color:W,fontSize:10,lineHeight:1}}>✓</span>}
                  </div>
                  <span style={{fontSize:11,fontWeight:600,color:isDeposited?GR:MU}}>{isDeposited?"Deposited":"Mark as deposited"}</span>
                </div>}
                {!isAdmin&&<div onClick={toggleGiven} style={{display:"flex",alignItems:"center",gap:6,marginTop:5,cursor:"pointer"}}>
                  <div style={{width:16,height:16,borderRadius:4,border:`2px solid ${isGiven?GR:BD}`,background:isGiven?GR:W,flexShrink:0,display:"flex",alignItems:"center",justifyContent:"center"}}>
                    {isGiven&&<span style={{color:W,fontSize:10,lineHeight:1}}>✓</span>}
                  </div>
                  <span style={{fontSize:11,fontWeight:600,color:isGiven?GR:MU}}>{isGiven?"Check given to admin":"Check given to admin?"}</span>
                </div>}
              </div>
              <div style={{display:"flex",alignItems:"center",gap:8}}>
                <span style={{fontFamily:"'Nunito',sans-serif",fontSize:16,fontWeight:900,color:GR}}>{fmt(pmt.amount)}</span>
                {(!project.paidInFull&&(isAdmin||!isDeposited))&&<button onClick={()=>delPmt(pmt.id)} style={{background:"none",border:"none",cursor:"pointer",fontSize:18,color:MU,lineHeight:1}}
                  onMouseEnter={e=>e.currentTarget.style.color=RE} onMouseLeave={e=>e.currentTarget.style.color=MU}>×</button>}
              </div>
            </div>
          </div>;
        })}
      </div>}

      {/* Add check — only when not fully paid */}
      {!project.paidInFull&&<AddCheckRow state={project.state} taxRate={project.taxRate} onAdd={addPmt}/>}

      {/* Running total */}
      <div style={{borderTop:`1px solid ${BD}`,marginTop:10,paddingTop:10,display:"grid",gap:6}}>
        <div style={{display:"flex",justifyContent:"space-between"}}><span style={{fontSize:13,color:MU}}>Total Received</span><span style={{fontFamily:"'Nunito',sans-serif",fontSize:14,fontWeight:800,color:GR}}>{fmt(totalPaid)}</span></div>
        {!project.paidInFull&&<div style={{display:"flex",justifyContent:"space-between"}}><span style={{fontSize:13,fontWeight:700,color:N}}>Balance Remaining</span><span style={{fontFamily:"'Nunito',sans-serif",fontSize:16,fontWeight:900,color:bal>0?RE:GR}}>{fmt(bal)}</span></div>}
      </div>
    </Card>

    {/* Subs */}
    <div style={{display:"flex",justifyContent:"space-between",alignItems:"center",marginBottom:12}}>
      <div style={{fontSize:16,fontWeight:700,color:N}}>Subcontractors / Materials</div>
      <Btn color={AC} sm onClick={()=>{setEditSub(null);setShowSubSheet(true);}}>+ Add</Btn>
    </div>
    {subs.length===0?<Card><Empty text="No items yet."/></Card>:
      <div style={{background:W,border:`1px solid ${BD}`,borderRadius:14,overflow:"hidden",marginBottom:8}}>
        {subs.map((s,i)=>{
          const stax=s.inclTax?(s.cost*(s.taxRate||0))/100:0;
          const stot=(s.cost||0)+stax;
          const dn=s.nickname?`${s.company} (${s.nickname})`:s.company;
          const toggleReady=()=>{
            const updated=subs.map(x=>x.id===s.id?{...x,readyToPay:!x.readyToPay}:x);
            upd({...project,subs:updated});
          };
          return <div key={s.id} style={{padding:"13px 16px",borderBottom:i<subs.length-1?`1px solid ${BD}`:"none"}}>
            <div style={{display:"flex",justifyContent:"space-between",alignItems:"flex-start"}}>
              <div style={{flex:1,minWidth:0,marginRight:10}}>
                <div style={{fontSize:14,fontWeight:700,color:N}}>{dn}</div>
                {s.desc&&<div style={{fontSize:12,color:MU,marginTop:2}}>{s.desc}</div>}
                <div style={{marginTop:5}}>
                  {isAdmin?(
                    <>
                      {s.payStatus==="paid"&&<span style={{fontSize:11,fontWeight:700,color:GR}}>✓ Paid in Full</span>}
                      {s.payStatus==="partial"&&<span style={{fontSize:11,fontWeight:700,color:AC}}>Partial — {fmt(s.paidAmt)}</span>}
                      {(!s.payStatus||s.payStatus==="unpaid")&&<span style={{fontSize:11,fontWeight:700,color:RE}}>Unpaid</span>}
                      {s.readyToPay&&s.payStatus!=="paid"&&<span style={{fontSize:11,color:BL,marginLeft:8}}>· PM marked ready</span>}
                    </>
                  ):(
                    isDebit(s)
                      ? <span style={{fontSize:11,fontWeight:600,color:MU}}>Card transaction</span>
                      : s.payStatus==="paid"
                      ? <span style={{fontSize:11,fontWeight:700,color:GR}}>✓ Paid in Full</span>
                      : <div onClick={toggleReady} style={{display:"inline-flex",alignItems:"center",gap:6,cursor:"pointer",padding:"4px 8px",background:s.readyToPay?`${GR}12`:CR,border:`1px solid ${s.readyToPay?GR:BD}`,borderRadius:8}}>
                          <div style={{width:14,height:14,borderRadius:3,border:`2px solid ${s.readyToPay?GR:BD}`,background:s.readyToPay?GR:W,display:"flex",alignItems:"center",justifyContent:"center",flexShrink:0}}>
                            {s.readyToPay&&<span style={{color:W,fontSize:9,lineHeight:1}}>✓</span>}
                          </div>
                          <span style={{fontSize:11,fontWeight:600,color:s.readyToPay?GR:MU}}>{s.readyToPay?"Sub Ready to be Paid in full!":"Sub Ready to be Paid in full?"}</span>
                        </div>
                  )}
                </div>
              </div>
              <div style={{display:"flex",alignItems:"center",gap:8,flexShrink:0}}>
                <div style={{textAlign:"right"}}>
                  <div style={{fontFamily:"'Nunito',sans-serif",fontSize:16,fontWeight:900,color:RE}}>{fmt(stot)}</div>
                  {stax>0&&<div style={{fontSize:11,color:MU}}>{fmt(s.cost)} + {fmt(stax)} ({s.taxRate}%) tax</div>}
                </div>
                <button onClick={()=>{setEditSub(s);setShowSubSheet(true);}} style={{padding:"6px 10px",background:CR,border:`1px solid ${BD}`,borderRadius:7,cursor:"pointer",fontSize:12,fontWeight:700,color:MU,...(!isAdmin&&s.payStatus==="paid"?{display:"none"}:{})}}>Edit</button>
                {!isDebit(s)&&(isAdmin||s.payStatus!=="paid")&&<button onClick={()=>delSub(s.id)} style={{background:"none",border:"none",cursor:"pointer",fontSize:20,color:MU,padding:"2px 4px"}}>×</button>}
              </div>
            </div>
          </div>;
        })}
        <div style={{padding:"11px 16px",background:CR,borderTop:`2px solid ${BD}`,display:"flex",justifyContent:"space-between"}}>
          <span style={{fontSize:13,fontWeight:700,color:N}}>Total</span>
          <span style={{fontFamily:"'Nunito',sans-serif",fontSize:16,fontWeight:900,color:RE}}>{fmt(cost)}</span>
        </div>
      </div>
    }

    </div>{/* end weekly gate wrapper */}

    {showSubSheet&&<Sheet title={editSub?"Edit Item":"Add Item"} onClose={()=>{setShowSubSheet(false);setEditSub(null);}}>
      <SubForm initial={editSub} subCos={subCos} projectState={project.state||"WA"} defaultTax={project.taxRate||8.7} isAdmin={isAdmin}
        onSubmit={s=>{
          const u=editSub?{...project,subs:subs.map(x=>x.id===s.id?s:x)}:{...project,subs:[...subs,{...s,id:uid()}]};
          upd(u);setShowSubSheet(false);setEditSub(null);
        }}/>
    </Sheet>}

    {showEdit&&<Sheet title="Edit Project" onClose={()=>setShowEdit(false)}>
      <EditProjForm project={project} splits={splits} isAdmin={isAdmin}
        onSubmit={p=>{upd({...project,...p});setShowEdit(false);}}
        onDelete={()=>{onSave(projects.filter(x=>x.id!==project.id));onBack();}}/>
    </Sheet>}
  </div>;
}

/* ── Add Check Row ── */
function AddCheckRow({state,taxRate,onAdd}){
  const[amt,setAmt]=useState("");
  const isWA=state==="WA"&&+taxRate>0;
  const val=+amt||0, tax=isWA?val-val/(1+(+taxRate||0)/100):0, pre=val-tax;
  const submit=()=>{if(!val)return;onAdd(val);setAmt("");};
  return <div style={{marginBottom:10}}>
    <div style={{fontSize:11,fontWeight:700,color:MU,textTransform:"uppercase",letterSpacing:"0.6px",marginBottom:6}}>
      {isWA?`Add Check (includes ${taxRate}% tax)`:"Add Check Amount ($)"}
    </div>
    <div style={{display:"flex",gap:8}}>
      <input type="number" value={amt} onChange={e=>setAmt(e.target.value)} onWheel={e=>e.target.blur()} placeholder="0"
        style={{flex:1,padding:"11px 14px",background:CR,border:`1.5px solid ${BD}`,borderRadius:10,fontFamily:"inherit",fontSize:15,color:N,outline:"none",boxSizing:"border-box"}}
        onFocus={e=>e.target.style.borderColor=AC} onBlur={e=>e.target.style.borderColor=BD}
        onKeyDown={e=>e.key==="Enter"&&submit()}/>
      <button onClick={submit} style={{padding:"0 18px",background:AC,border:"none",borderRadius:10,color:W,fontFamily:"inherit",fontSize:14,fontWeight:700,cursor:"pointer"}}
        onMouseEnter={e=>e.currentTarget.style.opacity=".78"} onMouseLeave={e=>e.currentTarget.style.opacity="1"}>+ Add</button>
    </div>
    {isWA&&val>0&&<div style={{marginTop:6,padding:"7px 12px",background:`${AC}10`,borderRadius:8,display:"flex",gap:16}}>
      <span style={{fontSize:12,color:N}}>Pre-tax: <b>{fmt(pre)}</b></span>
      <span style={{fontSize:12,color:AC}}>Tax: <b>{fmt(tax)}</b></span>
    </div>}
  </div>;
}

/* ── Sub Form ── */
function SubForm({initial,subCos,projectState,defaultTax,isAdmin,onSubmit}){
  const opts=subCos.map((s,i)=>{
    const co=typeof s==="string"?s:(s.company||"");
    const nk=typeof s==="object"?(s.nickname||""):"";
    return{key:i,company:co,nickname:nk,label:nk?`${co} — ${nk}`:co};
  }).filter(o=>o.company);
  const initIdx=initial?opts.findIndex(o=>o.company===initial.company):-1;
  const[selIdx,setSelIdx]=useState(initIdx>=0?String(initIdx):"");
  const[f,setF]=useState(initial||{company:"",nickname:"",desc:"",cost:"",inclTax:projectState==="WA",taxRate:defaultTax||8.7,payStatus:"unpaid",paidAmt:""});
  const set=(k,v)=>setF(p=>({...p,[k]:v}));
  const onSel=idx=>{setSelIdx(idx);if(idx===""){setF(p=>({...p,company:"",nickname:""}));return;}const o=opts[+idx];if(o)setF(p=>({...p,company:o.company,nickname:o.nickname}));};
  const tax=f.inclTax?((+f.cost||0)*(+f.taxRate||0))/100:0, tot=(+f.cost||0)+tax;
  return <div>
    <div style={{marginBottom:14}}>
      <Lbl t="Select Subcontractor / Material"/>
      {opts.length===0?<div style={{padding:"12px 14px",background:`${RE}10`,border:`1px solid ${RE}30`,borderRadius:10,fontSize:13,color:RE}}>No subcontractors saved yet. Add them in Settings first.</div>
      :<select value={selIdx} onChange={e=>onSel(e.target.value)}
        style={{width:"100%",padding:"12px 14px",background:CR,border:`1.5px solid ${selIdx!==""?AC:BD}`,borderRadius:10,fontFamily:"inherit",fontSize:15,color:selIdx!==""?N:MU,outline:"none",appearance:"none",cursor:"pointer"}}>
        <option value="">Select subcontractor…</option>
        {opts.map(o=><option key={o.key} value={o.key}>{o.label}</option>)}
      </select>}
    </div>
    <Inp label="Description" value={f.desc} onChange={v=>set("desc",v)} placeholder="Re-piping, tile, demo…"/>
    <Inp label="Invoice Amount ($)" value={f.cost} onChange={v=>set("cost",v)} type="number" placeholder="0"/>
    {projectState==="WA"&&<div style={{marginBottom:14}}>
      <Tog on={f.inclTax} onChange={v=>set("inclTax",v)} label="Include Sales Tax"/>
      {f.inclTax&&<div style={{marginTop:10}}><Sel label="" value={f.taxCity||"Vancouver"} onChange={v=>set("taxCity",v)&&set("taxRate",rateForCity(v))||setF(p=>({...p,taxCity:v,taxRate:rateForCity(v)}))} options={cityOpts} mb={0}/></div>}
    </div>}
    {!!f.cost&&<div style={{background:CR,borderRadius:10,padding:"10px 14px",marginBottom:14,display:"flex",justifyContent:"space-between",alignItems:"center"}}>
      <span style={{fontSize:13,fontWeight:700,color:N}}>Total</span>
      <span style={{fontFamily:"'Nunito',sans-serif",fontSize:18,fontWeight:900,color:RE}}>{fmt(tot)}</span>
    </div>}
    {isAdmin&&<div style={{marginBottom:14}}>
      <Lbl t="Payment Status"/>
      <div style={{display:"flex",gap:8}}>
        {[["unpaid","Unpaid",RE],["paid","Paid",GR],["partial","Partial",AC]].map(([s,l,c])=>(
          <button key={s} onClick={()=>set("payStatus",s)}
            style={{flex:1,padding:"11px 0",borderRadius:10,border:`2px solid ${f.payStatus===s?c:BD}`,cursor:"pointer",fontFamily:"inherit",fontSize:13,fontWeight:700,background:f.payStatus===s?`${c}18`:W,color:f.payStatus===s?c:MU,transition:"all .15s"}}>{l}</button>
        ))}
      </div>
    </div>}
    {isAdmin&&f.payStatus==="partial"&&<Inp label="Amount Paid ($)" value={f.paidAmt} onChange={v=>set("paidAmt",v)} type="number" placeholder="0"/>}
    <Btn full onClick={()=>{if(!f.company||!f.cost)return;onSubmit({...f,cost:+f.cost||0,taxRate:+f.taxRate||0,paidAmt:+f.paidAmt||0});}}>Save</Btn>
  </div>;
}

/* ── Edit Project Form ── */
function EditProjForm({project,splits,isAdmin,onSubmit,onDelete}){
  const pms=USERS.filter(u=>u.role==="pm");
  const[confirmDel,setConfirmDel]=useState(false);
  const stored=project.subTotal?project.subTotal:(project.state==="WA"&&project.taxRate?(project.value||0)/(1+(+project.taxRate||0)/100):(project.value||0));
  const defCity=project.taxCity||(WA_CITIES.find(c=>c.rate===(project.taxRate||8.9))?.city)||"Vancouver";
  const[f,setF]=useState({name:project.name||"",state:project.state||"WA",taxCity:defCity,taxRate:project.taxRate||8.9,subTotal:String(Math.round(stored*100)/100||""),pmId:project.pmId||"",pmPct:project.pmPct||0,attendedEst:project.attendedEst||false,marketingPct:project.marketingPct||splits.marketing,overheadPct:project.overheadPct||splits.overhead});
  const set=(k,v)=>setF(p=>({...p,[k]:v}));
  const onPm=v=>{const u=USERS.find(x=>x.id===v);setF(p=>({...p,pmId:v,pmPct:getPmPct(u?.username||"",false,splits),attendedEst:false}));};
  const onEst=v=>{const u=USERS.find(x=>x.id===f.pmId);setF(p=>({...p,attendedEst:v,pmPct:getPmPct(u?.username||"",v,splits)}));};
  const onState=v=>setF(p=>({...p,state:v,taxCity:v==="OR"?"":p.taxCity||"Vancouver",taxRate:v==="OR"?0:rateForCity(p.taxCity||"Vancouver")}));
  const onCity=v=>setF(p=>({...p,taxCity:v,taxRate:rateForCity(v)}));
  const isCory=USERS.find(x=>x.id===f.pmId)?.username==="cory";
  const sub=+f.subTotal||0, tax=f.state==="WA"?sub*(+f.taxRate||0)/100:0, grand=sub+tax;
  return <div>
    <Inp label="Job Name" value={f.name} onChange={v=>set("name",v)}/>
    <StateTog value={f.state} onChange={onState}/>
    {f.state==="WA"&&<Sel label="City / Tax Rate" value={f.taxCity||"Vancouver"} onChange={onCity} options={cityOpts}/>}
    <Inp label={f.state==="WA"?"Sub Total (before tax)":"Project Value ($)"} value={f.subTotal} onChange={v=>set("subTotal",v)} type="number"/>
    {f.state==="WA"&&sub>0&&<div style={{background:`${AC}10`,border:`1.5px solid ${AC}`,borderRadius:10,padding:"12px 14px",marginBottom:14}}>
      <div style={{display:"flex",justifyContent:"space-between",marginBottom:5}}><span style={{fontSize:13,color:N}}>Sub Total</span><span style={{fontFamily:"'Nunito',sans-serif",fontSize:14,fontWeight:800,color:N}}>{fmt(sub)}</span></div>
      <div style={{display:"flex",justifyContent:"space-between",marginBottom:8}}><span style={{fontSize:13,color:N}}>Tax ({f.taxRate}%)</span><span style={{fontFamily:"'Nunito',sans-serif",fontSize:14,fontWeight:800,color:AC}}>{fmt(tax)}</span></div>
      <div style={{display:"flex",justifyContent:"space-between",borderTop:`1px solid ${AC}30`,paddingTop:8}}><span style={{fontSize:14,fontWeight:700,color:N}}>Grand Total</span><span style={{fontFamily:"'Nunito',sans-serif",fontSize:18,fontWeight:900,color:N}}>{fmt(grand)}</span></div>
    </div>}
    {isAdmin&&<><Sel label="Project Manager" value={f.pmId} onChange={onPm} options={[{v:"",l:"Select PM…"},...pms.map(u=>({v:u.id,l:u.name}))]}/>
    {isCory&&<div style={{marginBottom:14,padding:"12px 14px",background:`${AC}10`,border:`1px solid ${AC}30`,borderRadius:10}}>
      <Tog on={f.attendedEst} onChange={onEst} label={`Cory attended estimate (+${splits.pm_cory_bonus||5}% = ${getPmPct("cory",f.attendedEst,splits)}% total)`}/>
    </div>}
    <div style={{background:CR,borderRadius:12,padding:14,marginBottom:14}}>
      <div style={{fontSize:11,fontWeight:700,color:MU,textTransform:"uppercase",marginBottom:10}}>Profit Split Override</div>
      <div style={{display:"grid",gridTemplateColumns:"1fr 1fr 1fr",gap:10}}>
        <Inp label="PM %" value={String(f.pmPct)} onChange={v=>set("pmPct",+v)} type="number" mb={0}/>
        <Inp label="Mktg %" value={String(f.marketingPct)} onChange={v=>set("marketingPct",+v)} type="number" mb={0}/>
        <Inp label="Ovhd %" value={String(f.overheadPct)} onChange={v=>set("overheadPct",+v)} type="number" mb={0}/>
      </div>
    </div></>}
    <Btn full onClick={()=>{if(!f.name.trim())return;onSubmit({...f,subTotal:sub,value:grand,customerTax:tax,pmPct:+f.pmPct||0,marketingPct:+f.marketingPct||0,overheadPct:+f.overheadPct||0,taxRate:+f.taxRate||0});}}>Save Changes</Btn>
    <div style={{marginTop:10}}>
      {(()=>{
        const hasLocked=!isAdmin&&(
          (project.payments||[]).some(p=>p.deposited)||
          (project.subs||[]).some(s=>s.payStatus==="paid")
        );
        if(hasLocked) return <div style={{padding:"10px 12px",background:`${MU}08`,borderRadius:10,fontSize:12,color:MU,textAlign:"center"}}>Job cannot be deleted — admin has confirmed payments or paid subs</div>;
        return (isAdmin||project.status!=="completed")&&(!confirmDel
          ?<Btn full outline color={RE} onClick={()=>setConfirmDel(true)}>Delete Project</Btn>
          :<div style={{padding:"14px",background:`${RE}08`,border:`1.5px solid ${RE}`,borderRadius:12,marginTop:4}}>
            <div style={{fontSize:14,fontWeight:700,color:RE,marginBottom:10,textAlign:"center"}}>Delete this project?</div>
            <div style={{display:"flex",gap:10}}>
              <Btn full outline color={MU} onClick={()=>setConfirmDel(false)}>Cancel</Btn>
              <Btn full color={RE} onClick={onDelete}>Yes, Delete</Btn>
            </div>
          </div>);
      })()}
    </div>
  </div>;
}

/* ── Reports ── */
const MONTHS=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
function ReportsPage({projects, isAdmin, user}){
  const allYears=[...new Set(projects.map(p=>(p.completedDate||p.createdAt||"").slice(0,4)).filter(Boolean))].sort().reverse();
  const[yr,setYr]=useState(allYears[0]||"all");
  const[mo,setMo]=useState("all");
  const[st,setSt]=useState("both");
  const[jobStatus,setJobStatus]=useState("done"); // admin only: done | split | ongoing
  const onYr=v=>{setYr(v);setMo("all");};

  const base_qual = isAdmin
    ? projects.filter(p=>{
        if(jobStatus==="done")    return p.status==="completed"&&p.paidInFull&&p.splitDone;
        if(jobStatus==="split")   return p.status==="completed"&&p.paidInFull&&!p.splitDone;
        if(jobStatus==="ongoing") return !(p.status==="completed"&&p.paidInFull);
        return p.status==="completed"&&p.paidInFull;
      })
    : projects.filter(p=>p.status==="completed"&&p.paidInFull);

  const fil=base_qual.filter(p=>{
    const d=p.completedDate||p.createdAt||"";
    if(yr!=="all"&&!d.startsWith(yr))return false;
    if(mo!=="all"&&d.slice(5,7)!==mo)return false;
    if(st==="WA"&&p.state==="OR")return false;
    if(st==="OR"&&(p.state||"WA")!=="OR")return false;
    return true;
  });

  const rev=fil.reduce((s,p)=>s+(p.value||0),0);
  const preRev=fil.reduce((s,p)=>s+projBase(p),0);
  const cost=fil.reduce((s,p)=>s+projCost(p),0);
  const gp=preRev-cost;
  const gpP=preRev?(gp/preRev)*100:0;
  const taxCol=fil.reduce((s,p)=>s+(p.customerTax||0),0);
  const taxSubs=fil.reduce((s,p)=>s+(p.subs||[]).reduce((t,x)=>t+(x.inclTax?((x.cost||0)*(x.taxRate||0))/100:0),0),0);
  const collected=fil.reduce((s,p)=>s+(p.paidInFull?(p.value||0):(p.customerPaid||0)),0);
  const outstanding=projects.filter(p=>!p.paidInFull).reduce((s,p)=>s+((p.value||0)-(p.customerPaid||0)),0);
  const waCount=fil.filter(p=>(p.state||"WA")==="WA").length;
  const orCount=fil.filter(p=>p.state==="OR").length;
  const waRev=fil.filter(p=>(p.state||"WA")==="WA").reduce((s,p)=>s+(p.value||0),0);
  const orRev=fil.filter(p=>p.state==="OR").reduce((s,p)=>s+(p.value||0),0);
  const mktTotal=fil.reduce((s,p)=>{ const g=projBase(p)-projCost(p); return s+(g*(p.marketingPct||0))/100; },0);
  const ovhTotal=fil.reduce((s,p)=>{ const g=projBase(p)-projCost(p); return s+(g*(p.overheadPct||0))/100; },0);
  const pmTotal =fil.reduce((s,p)=>{ const g=projBase(p)-projCost(p); return s+(g*(p.pmPct||0))/100; },0);
  const paliyNet=gp-pmTotal-mktTotal-ovhTotal;

  const activeMos=[...new Set(
    base_qual
      .filter(p=>yr==="all"||(p.completedDate||p.createdAt||"").startsWith(yr))
      .map(p=>(p.completedDate||p.createdAt||"").slice(5,7))
      .filter(Boolean)
  )].sort();
  const bySub={};
  fil.forEach(p=>(p.subs||[]).forEach(s=>{const k=s.nickname?`${s.company} (${s.nickname})`:(s.company||"Unknown");bySub[k]=(bySub[k]||0)+subTot(s);}));

  const Stat=({l,v,c=N})=><div style={{background:W,border:`1px solid ${BD}`,borderRadius:12,padding:"13px 14px",borderTop:`3px solid ${c}`}}>
    <div style={{fontSize:10,fontWeight:700,color:MU,textTransform:"uppercase",letterSpacing:"0.5px",marginBottom:4}}>{l}</div>
    <div style={{fontFamily:"'Nunito',sans-serif",fontSize:20,fontWeight:900,color:c}}>{v}</div>
  </div>;

  return <div>
    <div style={{fontSize:22,fontWeight:800,color:N,marginBottom:14}}>Reports</div>

    {/* Admin job status filter */}
    {isAdmin&&<div style={{marginBottom:10}}>
      <div style={{fontSize:11,fontWeight:700,color:MU,textTransform:"uppercase",letterSpacing:"0.5px",marginBottom:6}}>Job Status</div>
      <div style={{display:"flex",background:CR,border:`1.5px solid ${BD}`,borderRadius:10,overflow:"hidden"}}>
        {[["done","Completed"],["split","Need to Split"],["ongoing","Ongoing"]].map(([v,l])=>(
          <button key={v} onClick={()=>setJobStatus(v)}
            style={{flex:1,padding:"9px 0",border:"none",cursor:"pointer",fontFamily:"inherit",fontSize:12,fontWeight:600,background:jobStatus===v?N:"transparent",color:jobStatus===v?W:MU,transition:"all .15s"}}>{l}</button>
        ))}
      </div>
    </div>}

    {/* Year */}
    <div style={{marginBottom:10}}>
      <div style={{fontSize:11,fontWeight:700,color:MU,textTransform:"uppercase",letterSpacing:"0.5px",marginBottom:6}}>Year</div>
      <div style={{display:"flex",gap:8,flexWrap:"wrap"}}>
        {["all",...allYears].map(y=><button key={y} onClick={()=>onYr(y)}
          style={{padding:"7px 16px",borderRadius:20,border:`1.5px solid ${yr===y?N:BD}`,cursor:"pointer",fontFamily:"inherit",fontSize:13,fontWeight:600,background:yr===y?N:W,color:yr===y?W:MU,transition:"all .15s"}}>
          {y==="all"?"All Time":y}
        </button>)}
      </div>
    </div>

    <div style={{marginBottom:10}}>
      <div style={{fontSize:11,fontWeight:700,color:MU,textTransform:"uppercase",letterSpacing:"0.5px",marginBottom:6}}>Month</div>
      <div style={{display:"flex",gap:6,flexWrap:"wrap"}}>
        <button onClick={()=>setMo("all")} style={{padding:"6px 13px",borderRadius:20,border:`1.5px solid ${mo==="all"?N:BD}`,cursor:"pointer",fontFamily:"inherit",fontSize:12,fontWeight:600,background:mo==="all"?N:W,color:mo==="all"?W:MU}}>All</button>
        {MONTHS.map((m,i)=>{const k=String(i+1).padStart(2,"0"),has=activeMos.includes(k);return <button key={k} onClick={()=>has&&setMo(k)}
          style={{padding:"6px 13px",borderRadius:20,border:`1.5px solid ${mo===k?AC:has?BD:"transparent"}`,cursor:has?"pointer":"default",fontFamily:"inherit",fontSize:12,fontWeight:600,background:mo===k?AC:W,color:mo===k?W:has?N:BD,transition:"all .15s"}}>{m}</button>;})}
      </div>
    </div>

    <div style={{marginBottom:14}}>
      <div style={{fontSize:11,fontWeight:700,color:MU,textTransform:"uppercase",letterSpacing:"0.5px",marginBottom:6}}>State</div>
      <div style={{display:"flex",background:CR,border:`1.5px solid ${BD}`,borderRadius:10,overflow:"hidden"}}>
        {[["both","Both States"],["WA","Washington"],["OR","Oregon"]].map(([v,l])=><button key={v} onClick={()=>setSt(v)}
          style={{flex:1,padding:"10px 0",border:"none",cursor:"pointer",fontFamily:"inherit",fontSize:13,fontWeight:600,background:st===v?N:"transparent",color:st===v?W:MU,transition:"all .15s"}}>{l}</button>)}
      </div>
    </div>

    <div style={{marginBottom:14,padding:"8px 12px",background:`${N}08`,borderRadius:8,display:"flex",alignItems:"center",gap:6}}>
      <span style={{fontSize:11,fontWeight:700,color:MU,textTransform:"uppercase",letterSpacing:"0.5px"}}>Showing:</span>
      <span style={{fontSize:12,fontWeight:700,color:N}}>{[yr==="all"?"All Time":yr,mo!=="all"?MONTHS[+mo-1]:null,st!=="both"?st:null].filter(Boolean).join(" · ")}</span>
      <span style={{fontSize:12,color:MU}}>— {fil.length} job{fil.length!==1?"s":""}</span>
    </div>

    {/* PM simplified stats */}
    {!isAdmin&&<>
      <div style={{display:"grid",gridTemplateColumns:"1fr 1fr",gap:10,marginBottom:10}}>
        <Stat l="Your Commission" v={fmt(pmTotal)} c={AC}/>
        <Stat l="Avg GP %" v={pctf(gpP)} c={gpP>=30?GR:gpP>=15?AC:RE}/>
        <Stat l="WA Jobs" v={`${waCount} job${waCount!==1?"s":""}`} c={N}/>
        <Stat l="OR Jobs" v={`${orCount} job${orCount!==1?"s":""}`} c={GR}/>
      </div>
    </>}

    {/* Admin full stats */}
    {isAdmin&&<>
      <div style={{display:"grid",gridTemplateColumns:"1fr 1fr",gap:10,marginBottom:10}}>
        <Stat l="Revenue"      v={fmt(rev)}  c={N}/>
        <Stat l="Gross Profit" v={fmt(gp)}   c={GR}/>
        <Stat l="GP %"         v={pctf(gpP)} c={gpP>=30?GR:gpP>=15?AC:RE}/>
        <Stat l="Sub Costs"    v={fmt(cost)} c={RE}/>
      </div>
      <div style={{display:"grid",gridTemplateColumns:"1fr 1fr",gap:10,marginBottom:10}}>
        <Stat l="WA Jobs" v={fmt(waRev)} c={N}/>
        <Stat l="OR Jobs" v={fmt(orRev)} c={GR}/>
        <Stat l="Tax Collected"    v={fmt(taxCol)}  c={AC}/>
        <Stat l="Tax Paid to Subs" v={fmt(taxSubs)} c={RE}/>
      </div>
      <div style={{display:"grid",gridTemplateColumns:"1fr 1fr",gap:10,marginBottom:10}}>
        <Stat l="Collected"      v={fmt(collected)}   c={GR}/>
        <Stat l="Outstanding"    v={fmt(outstanding)} c={outstanding>0?RE:GR}/>
        <Stat l="Marketing Paid" v={fmt(mktTotal)}    c={BL}/>
        <Stat l="Overhead Paid"  v={fmt(ovhTotal)}    c={MU}/>
      </div>
      <div style={{background:GR,borderRadius:14,padding:"16px 20px",marginBottom:16,display:"flex",justifyContent:"space-between",alignItems:"center"}}>
        <div>
          <div style={{fontSize:11,fontWeight:700,color:"rgba(255,255,255,0.7)",textTransform:"uppercase",letterSpacing:"0.5px",marginBottom:4}}>Paliy Net</div>
          <div style={{fontSize:12,color:"rgba(255,255,255,0.65)"}}>After PM cuts, marketing & overhead</div>
        </div>
        <div style={{fontFamily:"'Nunito',sans-serif",fontSize:28,fontWeight:900,color:W}}>{fmt(paliyNet)}</div>
      </div>
    </>}

    <div style={{background:W,border:`1px solid ${BD}`,borderRadius:14,overflow:"hidden",marginBottom:16}}>
      <div style={{padding:"12px 16px",borderBottom:`1px solid ${BD}`,fontSize:15,fontWeight:700,color:N}}>Spending by Category</div>
      {Object.keys(bySub).length===0?<div style={{padding:"20px",color:MU,fontSize:14,textAlign:"center"}}>No data.</div>
      :Object.entries(bySub).sort((a,b)=>b[1]-a[1]).map(([co,amt])=><div key={co} style={{padding:"11px 16px",borderBottom:`1px solid ${BD}`,display:"flex",justifyContent:"space-between",alignItems:"center"}}>
        <div style={{fontSize:14,fontWeight:600,color:N}}>{co}</div>
        <div style={{fontFamily:"'Nunito',sans-serif",fontSize:15,fontWeight:800,color:RE}}>{fmt(amt)}</div>
      </div>)}
    </div>

    {/* Job Status list — 3 sections for admin, simple for PM */}
    {(()=>{
      const allProjects = projects; // full unfiltered list for status grouping
      // For admin: show 3 groups across ALL projects (not just filtered by year/month)
      // For PM: show their completed jobs
      if(isAdmin){
        const pending   = allProjects.filter(p=>p.status==="completed"&&!p.paidInFull);
        const needSplit = allProjects.filter(p=>p.status==="completed"&&p.paidInFull&&!p.splitDone);
        const completed = allProjects.filter(p=>p.status==="completed"&&p.paidInFull&&p.splitDone);
        const JobRow = ({p})=>{
          const pg=projBase(p)-projCost(p), pp=projBase(p)?(pg/projBase(p))*100:0, pm=USERS.find(u=>u.id===p.pmId);
          return <div style={{padding:"11px 16px",borderBottom:`1px solid ${BD}`,display:"flex",justifyContent:"space-between",alignItems:"center"}}>
            <div>
              <div style={{fontSize:14,fontWeight:700,color:N}}>{p.name}</div>
              <div style={{display:"flex",gap:6,marginTop:3,alignItems:"center"}}>
                {pm&&<span style={{fontSize:11,color:MU}}>{pm.name}</span>}
                <Badge label={p.state||"WA"} color={p.state==="OR"?GR:N} bg={p.state==="OR"?`${GR}15`:`${N}10`}/>
                {p.completedDate&&<span style={{fontSize:11,color:MU}}>{p.completedDate}</span>}
              </div>
            </div>
            <div style={{textAlign:"right"}}>
              <div style={{fontFamily:"'Nunito',sans-serif",fontSize:14,fontWeight:800,color:pg>=0?GR:RE}}>{fmt(pg)}</div>
              <div style={{fontSize:11,color:MU}}>{pctf(pp)}</div>
            </div>
          </div>;
        };
        const Section3 = ({title,color,jobs})=><div style={{background:W,border:`1px solid ${BD}`,borderRadius:14,overflow:"hidden",marginBottom:10}}>
          <div style={{padding:"12px 16px",borderBottom:`1px solid ${BD}`,display:"flex",justifyContent:"space-between",alignItems:"center"}}>
            <div style={{fontSize:15,fontWeight:700,color:N}}>{title}</div>
            <span style={{fontSize:12,fontWeight:600,color,background:`${color}15`,padding:"2px 10px",borderRadius:20}}>{jobs.length} job{jobs.length!==1?"s":""}</span>
          </div>
          {jobs.length===0?<div style={{padding:"16px",color:MU,fontSize:14,textAlign:"center"}}>None</div>
          :jobs.sort((a,b)=>(b.completedDate||"").localeCompare(a.completedDate||"")).map(p=><JobRow key={p.id} p={p}/>)}
        </div>;
        return <>
          <Section3 title="Pending" color={AC} jobs={pending}/>
          <Section3 title="Need to Split" color={RE} jobs={needSplit}/>
        </>;
      }
      // PM view — just their completed jobs
      return <div style={{background:W,border:`1px solid ${BD}`,borderRadius:14,overflow:"hidden"}}>
        <div style={{padding:"12px 16px",borderBottom:`1px solid ${BD}`,fontSize:15,fontWeight:700,color:N}}>Completed Jobs — {fil.length}</div>
        {fil.length===0?<div style={{padding:"20px",color:MU,fontSize:14,textAlign:"center"}}>No completed jobs.</div>
        :[...fil].sort((a,b)=>(b.completedDate||"").localeCompare(a.completedDate||"")).map(p=>{
          const pg=projBase(p)-projCost(p), pp=projBase(p)?(pg/projBase(p))*100:0;
          const pmCut=(pg*(p.pmPct||0))/100;
          return <div key={p.id} style={{padding:"11px 16px",borderBottom:`1px solid ${BD}`,display:"flex",justifyContent:"space-between",alignItems:"center"}}>
            <div>
              <div style={{fontSize:14,fontWeight:700,color:N}}>{p.name}</div>
              <div style={{display:"flex",gap:6,marginTop:3,alignItems:"center"}}>
                <Badge label={p.state||"WA"} color={p.state==="OR"?GR:N} bg={p.state==="OR"?`${GR}15`:`${N}10`}/>
                {p.completedDate&&<span style={{fontSize:11,color:MU}}>{p.completedDate}</span>}
              </div>
            </div>
            <div style={{textAlign:"right"}}>
              <div style={{fontFamily:"'Nunito',sans-serif",fontSize:14,fontWeight:800,color:AC}}>{fmt(pmCut)}</div>
              <div style={{fontSize:11,color:MU}}>{pctf(pp)} GP</div>
            </div>
          </div>;
        })}
      </div>;
    })()}

  </div>;
}

/* ── Settings ── */
function SettingsPage({subCos,splits,isAdmin,onSaveSubs,onSaveSplits}){
  const[tab,setTab]=useState("subs");
  const[coName,setCoName]=useState(""),  [coNick,setCoNick]=useState("");
  const[sp,setSp]=useState({...DEF_SPLITS,...splits});
  const addSub=()=>{if(!coName.trim())return;onSaveSubs([...subCos,{company:coName.trim(),nickname:coNick.trim()}]);setCoName("");setCoNick("");};
  const saveSp=()=>{const u={marketing:+sp.marketing||0,overhead:+sp.overhead||0,pm_ivan:+sp.pm_ivan||25,pm_cory:+sp.pm_cory||20,pm_cory_bonus:+sp.pm_cory_bonus||5,pm_yuriy:+sp.pm_yuriy||25,pm_mark:+sp.pm_mark||25};onSaveSplits(u);alert("Saved.");};
  const tabs=isAdmin?[["subs","Subcontractors"],["splits","Splits"]]:[["subs","Subcontractors"]];
  return <div>
    <div style={{fontSize:22,fontWeight:800,color:N,marginBottom:18}}>Settings</div>
    <div style={{display:"flex",background:W,border:`1px solid ${BD}`,borderRadius:12,padding:3,marginBottom:20}}>
      {tabs.map(([id,l])=><button key={id} onClick={()=>setTab(id)}
        style={{flex:1,padding:"10px 0",borderRadius:10,border:"none",cursor:"pointer",fontFamily:"inherit",fontSize:13,fontWeight:600,background:tab===id?N:"transparent",color:tab===id?W:MU,transition:"all .15s"}}>{l}</button>)}
    </div>

    {tab==="subs"&&<div>
      <Card mb={12}>
        <div style={{fontSize:13,fontWeight:700,color:N,marginBottom:14}}>Add Subcontractor / Material</div>
        <Inp label="Company Name" value={coName} onChange={setCoName} placeholder="ABC Plumbing LLC"/>
        <Inp label="Nickname" value={coNick} onChange={setCoNick} placeholder="ABC Plumbing"/>
        <Btn full onClick={addSub}>Add</Btn>
      </Card>
      {subCos.length===0?<Empty text="No subcontractors saved yet."/>
      :<div style={{background:W,border:`1px solid ${BD}`,borderRadius:14,overflow:"hidden"}}>
        {subCos.map((c,i)=>{
          const name=typeof c==="string"?c:(c.company||""), nick=typeof c==="object"?(c.nickname||""):"";
          return <div key={i} style={{padding:"13px 16px",borderBottom:i<subCos.length-1?`1px solid ${BD}`:"none",display:"flex",justifyContent:"space-between",alignItems:"center"}}>
            <div><div style={{fontSize:14,fontWeight:600,color:N}}>{name}</div>{nick&&<div style={{fontSize:12,color:MU}}>{nick}</div>}</div>
            <button onClick={()=>onSaveSubs(subCos.filter((_,j)=>j!==i))} style={{background:"none",border:"none",cursor:"pointer",fontSize:22,color:MU,lineHeight:1}}
              onMouseEnter={e=>e.currentTarget.style.color=RE} onMouseLeave={e=>e.currentTarget.style.color=MU}>×</button>
          </div>;
        })}
      </div>}
    </div>}

    {tab==="splits"&&<div>
      <Card mb={12}>
        <div style={{fontSize:13,fontWeight:700,color:N,marginBottom:14}}>Company Splits</div>
        <div style={{display:"grid",gridTemplateColumns:"1fr 1fr",gap:10}}>
          <Inp label="Marketing %" value={String(sp.marketing)} onChange={v=>setSp(s=>({...s,marketing:v}))} type="number" mb={0}/>
          <Inp label="Overhead %"  value={String(sp.overhead)}  onChange={v=>setSp(s=>({...s,overhead:v}))}  type="number" mb={0}/>
        </div>
      </Card>
      <Card mb={12}>
        <div style={{fontSize:13,fontWeight:700,color:N,marginBottom:14}}>PM Splits</div>
        <div style={{display:"grid",gap:10}}>
          {[{k:"pm_ivan",l:"Ivan %"},{k:"pm_yuriy",l:"Yuriy %"},{k:"pm_mark",l:"Mark %"},{k:"pm_cory",l:"Cory % (base)"},{k:"pm_cory_bonus",l:"Cory % (estimate bonus)"}].map(({k,l})=>(
            <Inp key={k} label={l} value={String(sp[k]!==undefined?sp[k]:(DEF_SPLITS[k]||0))} onChange={v=>setSp(s=>({...s,[k]:v}))} type="number" mb={0}/>
          ))}
        </div>
      </Card>
      <Btn full onClick={saveSp}>Save All Splits</Btn>
    </div>}
  </div>;
}

/* ── Mount the app ── */
const _root = ReactDOM.createRoot(document.getElementById('root'));
_root.render(React.createElement(App));
