// appwindow.jsx — full dot+ macOS app window. Relies on core.jsx + popover.jsx globals.

const PROJECTS = [
  { id:'aprocap', name:'APROCAP', count:12, color:'#276EF1', sub:'Client · annuity + web' },
  { id:'q3', name:'Q3 Launch', count:8, color:'#1F9E6E', sub:'Eng + product' },
  { id:'personal', name:'Personal', count:3, color:'#C2603A', sub:'Notes & ideas' },
  { id:'sales', name:'Sales', count:6, color:'#8A5CD6', sub:'Discovery calls' },
];
const CONTACTS = [
  { id:'vero', name:'Verónica Salas', role:'Xhinola · Design Lead', idx:0, recs:9 },
  { id:'rod', name:'Rodrigo Arvizu', role:'Xhinola · Engineering', idx:3, recs:14 },
  { id:'maya', name:'Maya Chen', role:'Product', idx:2, recs:5 },
  { id:'luis', name:'Luis Tavárez', role:'APROCAP · Director', idx:1, recs:4 },
];

function TrafficLights() {
  return (
    <div style={{ display:'flex', gap:8, alignItems:'center' }}>
      {['#FF5F57','#FEBC2E','#28C840'].map(c=>(
        <span key={c} style={{ width:12, height:12, borderRadius:'50%', background:c, boxShadow:'inset 0 0 0 0.5px rgba(0,0,0,0.12)' }}/>
      ))}
    </div>
  );
}

function SideItem({ icon, label, active, badge, onClick }) {
  return (
    <button onClick={onClick} style={{
      display:'flex', alignItems:'center', gap:11, width:'100%', padding:'9px 10px', borderRadius:8, cursor:'pointer',
      border:'none', textAlign:'left', transition:'background .12s',
      background: active ? 'var(--s2)' : 'transparent',
      color: active ? 'var(--c1)' : 'var(--c2)',
    }}
      onMouseEnter={(e)=>{ if(!active) e.currentTarget.style.background='var(--s1)'; }}
      onMouseLeave={(e)=>{ if(!active) e.currentTarget.style.background='transparent'; }}
    >
      <span style={{ display:'flex', color: active ? 'var(--accent)' : 'var(--c3)' }}>{icon}</span>
      <span style={{ flex:1, fontFamily:SANS, fontSize:13.5, fontWeight: active?600:500 }}>{label}</span>
      {badge && <span style={{ fontFamily:MONO, fontSize:10.5, color:'var(--c3)' }}>{badge}</span>}
    </button>
  );
}

function Sidebar({ tab, setTab, startRec }) {
  return (
    <div style={{ width:214, flexShrink:0, background:'var(--s0)', borderRight:'1px solid var(--border-sub)', display:'flex', flexDirection:'column', padding:'10px 10px 12px' }}>
      <div style={{ display:'flex', alignItems:'center', gap:8, padding:'4px 8px 12px' }}>
        <Monogram size={22}/>
        <span style={{ fontFamily:SANS, fontWeight:800, fontSize:18, letterSpacing:'-0.02em', color:'var(--c1)' }}>dot<span style={{ color:'var(--accent)' }}>+</span></span>
      </div>
      <Btn variant="record" size="default" shape="pill" full onClick={startRec} style={{ marginBottom:14 }}>
        <span style={{ width:9, height:9, borderRadius:'50%', background:'var(--neg)' }}/> New recording
      </Btn>
      <div style={{ display:'flex', flexDirection:'column', gap:2 }}>
        <SideItem icon={<I.doc size={18}/>} label="Recordings" badge="14" active={tab==='recordings'} onClick={()=>setTab('recordings')}/>
        <SideItem icon={<I.folder size={18}/>} label="Projects" badge="4" active={tab==='projects'} onClick={()=>setTab('projects')}/>
        <SideItem icon={<I.people size={18}/>} label="Contacts" badge="22" active={tab==='contacts'} onClick={()=>setTab('contacts')}/>
        <SideItem icon={<I.sparkles size={18}/>} label="Ask dot+" active={tab==='ai'} onClick={()=>setTab('ai')}/>
      </div>
      <div style={{ marginTop:18 }}>
        <div style={{ fontFamily:SANS, fontSize:10.5, fontWeight:600, letterSpacing:'.08em', textTransform:'uppercase', color:'var(--c3)', padding:'0 10px 6px' }}>Projects</div>
        <div style={{ display:'flex', flexDirection:'column', gap:2 }}>
          {PROJECTS.slice(0,3).map(p=>(
            <button key={p.id} onClick={()=>setTab('projects')} style={{ display:'flex', alignItems:'center', gap:10, width:'100%', padding:'7px 10px', borderRadius:8, border:'none', background:'transparent', cursor:'pointer', textAlign:'left' }}
              onMouseEnter={(e)=>e.currentTarget.style.background='var(--s1)'} onMouseLeave={(e)=>e.currentTarget.style.background='transparent'}>
              <span style={{ width:8, height:8, borderRadius:2, background:p.color, flexShrink:0 }}/>
              <span style={{ flex:1, fontFamily:SANS, fontSize:13, color:'var(--c2)', whiteSpace:'nowrap', overflow:'hidden', textOverflow:'ellipsis' }}>{p.name}</span>
              <span style={{ fontFamily:MONO, fontSize:10.5, color:'var(--c3)' }}>{p.count}</span>
            </button>
          ))}
        </div>
      </div>
      <span style={{ flex:1 }}/>
      <SideItem icon={<I.gear size={18}/>} label="Settings" active={tab==='settings'} onClick={()=>setTab('settings')}/>
      <div style={{ display:'flex', alignItems:'center', gap:10, padding:'10px 8px 2px', marginTop:6, borderTop:'1px solid var(--border-sub)' }}>
        <span style={{ width:30, height:30, borderRadius:'50%', background:'var(--s3)', border:'1.5px solid var(--border)', color:'var(--c1)', display:'flex', alignItems:'center', justifyContent:'center', fontFamily:SANS, fontWeight:600, fontSize:12, flexShrink:0 }}>RA</span>
        <span style={{ flex:1, minWidth:0 }}>
          <span style={{ display:'block', fontFamily:SANS, fontSize:12.5, fontWeight:600, color:'var(--c1)', whiteSpace:'nowrap', overflow:'hidden', textOverflow:'ellipsis' }}>Rodrigo Arvizu</span>
          <span style={{ display:'block', fontFamily:MONO, fontSize:10, color:'var(--accent)' }}>PRO</span>
        </span>
        <IconBtn icon={<I.ellipsis size={16}/>} size={26}/>
      </div>
    </div>
  );
}

// ── recordings master-detail ───────────────────────────────────────────────────
const FULL_RECENTS = [
  ...RECENTS,
  { id:'r5', title:'Onboarding call — Maya', when:'Mon · 9:00 AM', dur:'18:40', folder:'Q3 Launch', spk:[{name:'Maya',idx:2},{name:'You',idx:1}], status:'done' },
  { id:'r6', title:'Weekly notes', when:'Sun · 8:12 PM', dur:'06:02', folder:'Personal', spk:[{name:'You',idx:1}], status:'done' },
];

function MasterRow({ r, active, onClick }) {
  return (
    <button onClick={onClick} style={{
      display:'flex', alignItems:'center', gap:11, width:'100%', padding:'11px 12px', borderRadius:10, cursor:'pointer',
      border:'none', textAlign:'left', transition:'background .12s',
      background: active ? 'var(--s2)' : 'transparent',
    }}
      onMouseEnter={(e)=>{ if(!active) e.currentTarget.style.background='var(--s1)'; }}
      onMouseLeave={(e)=>{ if(!active) e.currentTarget.style.background='transparent'; }}
    >
      <span style={{ width:36, height:36, borderRadius:9, flexShrink:0, display:'flex', alignItems:'center', justifyContent:'center', background:'var(--s2)', color: r.status==='processing'?'var(--warn)':'var(--c2)' }}>
        {r.status==='processing' ? <I.waves size={18}/> : <I.doc size={18}/>}
      </span>
      <span style={{ flex:1, minWidth:0 }}>
        <span style={{ display:'block', fontFamily:SANS, fontSize:14, fontWeight:600, color:'var(--c1)', whiteSpace:'nowrap', overflow:'hidden', textOverflow:'ellipsis' }}>{r.title}</span>
        <span style={{ display:'flex', alignItems:'center', gap:6, marginTop:3, fontFamily:MONO, fontSize:11, color:'var(--c3)' }}>
          <span>{r.when}</span><span style={{ color:'var(--cdis)' }}>·</span><span>{r.dur}</span>
          {r.folder && <><span style={{ color:'var(--cdis)' }}>·</span><span style={{ color:'var(--c2)' }}>{r.folder}</span></>}
        </span>
      </span>
      {r.status==='processing'
        ? <Tag variant="warning" dot>Processing</Tag>
        : <span style={{ display:'flex' }}>{r.spk.slice(0,3).map((s,i)=><span key={i} style={{ marginLeft:i?-7:0 }}><Avatar name={s.name} idx={s.idx} size={22} ring/></span>)}</span>}
    </button>
  );
}

function AppDetail({ startRec }) {
  const [tab, setTab] = React.useState('Summary');
  const tabs = ['Summary','Transcript','Key Points','Next Steps'];
  return (
    <div style={{ flex:1, minWidth:0, display:'flex', flexDirection:'column', background:'var(--s0)', overflow:'hidden' }}>
      <div style={{ padding:'22px 28px 0', overflowY:'auto', flex:1 }}>
        <div style={{ display:'flex', alignItems:'flex-start', gap:14 }}>
          <h1 style={{ flex:1, margin:0, fontFamily:SANS, fontSize:26, fontWeight:700, lineHeight:'32px', letterSpacing:'-0.02em', color:'var(--c1)' }}>Anualidad + Web APROCAP</h1>
          <div style={{ display:'flex', gap:6 }}>
            <Btn variant="ghost" size="small"><I.copy size={16}/> Copy</Btn>
            <Btn variant="ghost" size="small"><I.share size={16}/> Share</Btn>
            <IconBtn icon={<I.ellipsis size={18}/>} size={34} active/>
          </div>
        </div>
        <div style={{ display:'flex', alignItems:'center', gap:10, marginTop:10, fontFamily:MONO, fontSize:12, color:'var(--c2)', flexWrap:'wrap' }}>
          <span style={{ display:'inline-flex', alignItems:'center', gap:5 }}><I.cal size={14} color="var(--c3)"/> Jun 26, 2026 · 5:57 PM</span>
          <span style={{ color:'var(--cdis)' }}>·</span>
          <span style={{ display:'inline-flex', alignItems:'center', gap:5 }}><I.clock size={14} color="var(--c3)"/> 24:08</span>
          <span style={{ color:'var(--cdis)' }}>·</span>
          <span style={{ display:'inline-flex', alignItems:'center', gap:5 }}><I.pin size={14} color="var(--c3)"/> Higüey, La Altagracia</span>
          <span style={{ color:'var(--cdis)' }}>·</span>
          <Tag dot><span style={{ display:'inline-flex', alignItems:'center', gap:5 }}><I.folder size={12}/> APROCAP</span></Tag>
        </div>

        <div style={{ marginTop:18 }}><MiniPlayer/></div>

        {/* speakers */}
        <div style={{ display:'flex', alignItems:'center', gap:10, marginTop:16 }}>
          <span style={{ display:'flex' }}>{[{name:'Verónica',idx:0},{name:'You',idx:1}].map((s,i)=><span key={i} style={{ marginLeft:i?-8:0 }}><Avatar name={s.name} idx={s.idx} size={28} ring/></span>)}</span>
          <span style={{ fontFamily:SANS, fontSize:13, color:'var(--c2)' }}>Verónica, You</span>
          <span style={{ flex:1 }}/>
          <Btn variant="ghost" size="small" shape="pill"><I.people size={15}/> Manage speakers</Btn>
        </div>

        <div style={{ display:'flex', gap:22, marginTop:20, borderBottom:'1px solid var(--border-sub)', position:'sticky', top:0 }}>
          {tabs.map(tb=>{ const on=tab===tb; return <button key={tb} onClick={()=>setTab(tb)} style={{ background:'none', border:'none', cursor:'pointer', padding:'0 0 12px', position:'relative', fontFamily:SANS, fontSize:14, fontWeight:on?600:500, color:on?'var(--c1)':'var(--c3)' }}>{tb}{on && <span style={{ position:'absolute', left:0, right:0, bottom:-1, height:2, borderRadius:2, background:'var(--c1)' }}/>}</button>; })}
        </div>

        <div style={{ padding:'18px 0 28px' }}>
          {tab==='Summary' && (
            <div style={{ borderRadius:14, background:'var(--s1)', padding:18, maxWidth:680 }}>
              <div style={{ display:'flex', alignItems:'center', gap:8, marginBottom:12 }}><I.sparkles size={17} color="var(--accent)"/><span style={{ fontFamily:SANS, fontSize:13, fontWeight:600, color:'var(--c2)' }}>AI Summary</span></div>
              <p style={{ margin:0, fontFamily:SANS, fontSize:15.5, lineHeight:'25px', color:'var(--c1)' }}>Verónica and the team aligned the APROCAP annuity page around a renewal-first hero. They agreed to pull last quarter's APROCAP figures directly into the page, confirmed the launch date can hold for the 14th, and assigned Verónica to share the supporting figures deck right after the call.</p>
            </div>
          )}
          {tab==='Transcript' && (
            <div style={{ display:'flex', flexDirection:'column', gap:16, maxWidth:680 }}>
              {LIVE_LINES.map((l,i)=>(
                <div key={i} style={{ display:'flex', gap:12 }}>
                  <Avatar name={l.spk} idx={l.idx} size={30}/>
                  <span style={{ flex:1 }}>
                    <span style={{ display:'flex', alignItems:'center', gap:8, marginBottom:3 }}>
                      <span style={{ fontFamily:SANS, fontSize:13, fontWeight:600, color:SPK[l.idx%4] }}>{l.spk}</span>
                      <span style={{ fontFamily:MONO, fontSize:11, color:'var(--c3)' }}>{mm(l.at*60)}</span>
                    </span>
                    <span style={{ display:'block', fontFamily:SANS, fontSize:15, lineHeight:'23px', color:'var(--c1)' }}>{l.text}</span>
                  </span>
                </div>
              ))}
            </div>
          )}
          {tab==='Key Points' && (
            <div style={{ display:'flex', flexDirection:'column', gap:9, maxWidth:680 }}>
              {['Renewal CTA should lead the annuity hero.','Pull last quarter APROCAP figures into the page.','Launch date is held for the 14th.','Verónica owns the supporting figures deck.'].map((p,i)=>(
                <div key={i} style={{ display:'flex', gap:12, alignItems:'flex-start', background:'var(--s1)', borderRadius:12, padding:'14px 16px' }}>
                  <I.star size={15} color="var(--warn)" style={{ marginTop:3 }}/>
                  <span style={{ fontFamily:SANS, fontSize:15, lineHeight:'22px', color:'var(--c1)' }}>{p}</span>
                </div>
              ))}
            </div>
          )}
          {tab==='Next Steps' && (
            <div style={{ display:'flex', flexDirection:'column', gap:9, maxWidth:680 }}>
              {[['Share Q3 figures deck','Verónica',false],['Hold launch date — the 14th','You',true],['Draft renewal hero copy','You',false]].map(([txt,who,done],i)=>(
                <div key={i} style={{ display:'flex', gap:12, alignItems:'center', background:'var(--s1)', borderRadius:12, padding:'14px 16px' }}>
                  <span style={{ display:'flex', color:done?'var(--pos)':'var(--c3)' }}>{done?<I.checkfill size={22}/>:<I.circle size={22}/>}</span>
                  <span style={{ flex:1 }}>
                    <span style={{ display:'block', fontFamily:SANS, fontSize:15, fontWeight:600, color:done?'var(--c3)':'var(--c1)', textDecoration:done?'line-through':'none' }}>{txt}</span>
                    <span style={{ display:'block', fontFamily:MONO, fontSize:11, color:'var(--c3)', marginTop:2 }}>@{who}</span>
                  </span>
                </div>
              ))}
            </div>
          )}
        </div>
      </div>
    </div>
  );
}

function RecordingsScreen({ startRec }) {
  const [sel, setSel] = React.useState('r1');
  return (
    <div style={{ flex:1, minWidth:0, display:'flex' }}>
      <div style={{ width:368, flexShrink:0, display:'flex', flexDirection:'column', borderRight:'1px solid var(--border-sub)', background:'var(--s0)' }}>
        <div style={{ padding:'18px 16px 12px' }}>
          <div style={{ display:'flex', alignItems:'center', gap:10, marginBottom:14 }}>
            <h1 style={{ flex:1, margin:0, fontFamily:SANS, fontSize:22, fontWeight:700, letterSpacing:'-0.01em', color:'var(--c1)' }}>Recordings</h1>
            <IconBtn icon={<I.list size={18}/>} size={32} active/>
            <IconBtn icon={<I.sliders size={18}/>} size={32}/>
          </div>
          <div style={{ display:'flex', alignItems:'center', gap:8, height:36, padding:'0 10px', borderRadius:8, background:'var(--s2)' }}>
            <I.search size={16} color="var(--c3)"/>
            <span style={{ flex:1, fontFamily:SANS, fontSize:13, color:'var(--c3)' }}>Search recordings & transcripts</span>
          </div>
          <div style={{ display:'flex', gap:6, marginTop:12, overflowX:'auto' }}>
            {['All','Recent','APROCAP','Q3 Launch','Sales'].map((c,i)=>(
              <span key={c} style={{ flexShrink:0, padding:'5px 12px', borderRadius:999, fontFamily:SANS, fontSize:12.5, fontWeight:600, cursor:'pointer', background:i===0?'var(--ink-s)':'var(--s2)', color:i===0?'var(--ink-c)':'var(--c2)' }}>{c}</span>
            ))}
          </div>
        </div>
        <div style={{ flex:1, overflowY:'auto', padding:'0 8px 12px' }}>
          {FULL_RECENTS.map(r=><MasterRow key={r.id} r={r} active={sel===r.id} onClick={()=>setSel(r.id)}/>)}
        </div>
      </div>
      <AppDetail startRec={startRec}/>
    </div>
  );
}

// ── projects ─────────────────────────────────────────────────────────────────
function ProjectsScreen() {
  return (
    <div style={{ flex:1, minWidth:0, overflowY:'auto', background:'var(--s0)', padding:'22px 28px' }}>
      <div style={{ display:'flex', alignItems:'center', gap:10, marginBottom:20 }}>
        <h1 style={{ flex:1, margin:0, fontFamily:SANS, fontSize:24, fontWeight:700, letterSpacing:'-0.02em', color:'var(--c1)' }}>Projects</h1>
        <Btn variant="secondary" size="small" shape="pill"><I.plus size={16}/> New project</Btn>
      </div>
      <div style={{ display:'grid', gridTemplateColumns:'repeat(auto-fill, minmax(240px, 1fr))', gap:14 }}>
        {PROJECTS.map(p=>(
          <div key={p.id} style={{ borderRadius:14, background:'var(--s1)', border:'1px solid var(--border-sub)', padding:16, cursor:'pointer' }}>
            <div style={{ display:'flex', alignItems:'center', justifyContent:'space-between' }}>
              <span style={{ width:40, height:40, borderRadius:10, display:'flex', alignItems:'center', justifyContent:'center', background:`color-mix(in srgb, ${p.color} 18%, transparent)`, color:p.color }}><I.folder size={20}/></span>
              <I.ellipsis size={18} color="var(--c3)"/>
            </div>
            <div style={{ fontFamily:SANS, fontSize:16, fontWeight:700, color:'var(--c1)', marginTop:12 }}>{p.name}</div>
            <div style={{ fontFamily:SANS, fontSize:12.5, color:'var(--c3)', marginTop:2 }}>{p.sub}</div>
            <div style={{ fontFamily:MONO, fontSize:11, color:'var(--c2)', marginTop:14 }}>{p.count} recordings</div>
          </div>
        ))}
      </div>
    </div>
  );
}

// ── contacts (speaker CRM) ─────────────────────────────────────────────────────
function ContactsScreen() {
  return (
    <div style={{ flex:1, minWidth:0, overflowY:'auto', background:'var(--s0)', padding:'22px 28px' }}>
      <div style={{ display:'flex', alignItems:'center', gap:10, marginBottom:20 }}>
        <h1 style={{ flex:1, margin:0, fontFamily:SANS, fontSize:24, fontWeight:700, letterSpacing:'-0.02em', color:'var(--c1)' }}>Contacts</h1>
        <Btn variant="secondary" size="small" shape="pill"><I.plus size={16}/> Add contact</Btn>
      </div>
      <div style={{ borderRadius:14, background:'var(--s1)', border:'1px solid var(--border-sub)', overflow:'hidden' }}>
        {CONTACTS.map((c,i)=>(
          <div key={c.id} style={{ display:'flex', alignItems:'center', gap:14, padding:'14px 16px', borderTop: i?'1px solid var(--border-sub)':'none', cursor:'pointer' }}>
            <Avatar name={c.name} idx={c.idx} size={40}/>
            <span style={{ flex:1, minWidth:0 }}>
              <span style={{ display:'block', fontFamily:SANS, fontSize:14.5, fontWeight:600, color:'var(--c1)' }}>{c.name}</span>
              <span style={{ display:'block', fontFamily:SANS, fontSize:12.5, color:'var(--c3)', marginTop:1 }}>{c.role}</span>
            </span>
            <span style={{ fontFamily:MONO, fontSize:11, color:'var(--c2)' }}>{c.recs} recordings</span>
            <I.chev size={17} color="var(--c3)"/>
          </div>
        ))}
      </div>
    </div>
  );
}

// ── AI ─────────────────────────────────────────────────────────────────────────
function AiScreen() {
  return (
    <div style={{ flex:1, minWidth:0, display:'flex', flexDirection:'column', background:'var(--s0)' }}>
      <div style={{ flex:1, overflowY:'auto', padding:'28px 0' }}>
        <div style={{ maxWidth:680, margin:'0 auto', padding:'0 28px', display:'flex', flexDirection:'column', gap:18 }}>
          <div style={{ display:'flex', alignItems:'center', gap:10 }}>
            <span style={{ width:36, height:36, borderRadius:10, background:`color-mix(in srgb, var(--accent) 16%, transparent)`, color:'var(--accent)', display:'flex', alignItems:'center', justifyContent:'center' }}><I.sparkles size={20}/></span>
            <h1 style={{ margin:0, fontFamily:SANS, fontSize:22, fontWeight:700, letterSpacing:'-0.01em', color:'var(--c1)' }}>Ask dot+</h1>
            <span style={{ flex:1 }}/>
            <span style={{ fontFamily:MONO, fontSize:11, color:'var(--c3)' }}>across 14 recordings</span>
          </div>
          <div style={{ alignSelf:'flex-end', maxWidth:'80%', background:'var(--s2)', borderRadius:'14px 14px 4px 14px', padding:'11px 15px', fontFamily:SANS, fontSize:14.5, lineHeight:'21px', color:'var(--c1)' }}>What did we decide about the APROCAP launch date, and who owns the follow-up?</div>
          <div style={{ alignSelf:'flex-start', maxWidth:'92%' }}>
            <div style={{ display:'flex', alignItems:'center', gap:7, marginBottom:8, color:'var(--accent)' }}><I.sparkles size={16}/><span style={{ fontFamily:SANS, fontSize:12, fontWeight:600 }}>dot+</span></div>
            <div style={{ fontFamily:SANS, fontSize:15, lineHeight:'23px', color:'var(--c1)' }}>The launch is held for <b>the 14th</b> — Verónica confirmed it works and is holding the date. The follow-up is split: <b>Verónica</b> shares the Q3 figures deck, and <b>you</b> draft the renewal hero copy.</div>
            <div style={{ display:'flex', gap:8, marginTop:12, flexWrap:'wrap' }}>
              {['Anualidad + Web APROCAP','Sales discovery — Altagracia'].map(s=>(
                <span key={s} style={{ display:'inline-flex', alignItems:'center', gap:7, background:'var(--s1)', border:'1px solid var(--border-sub)', borderRadius:9, padding:'8px 11px', cursor:'pointer' }}>
                  <I.doc size={15} color="var(--c3)"/><span style={{ fontFamily:SANS, fontSize:12.5, fontWeight:600, color:'var(--c1)' }}>{s}</span>
                </span>
              ))}
            </div>
          </div>
        </div>
      </div>
      <div style={{ borderTop:'1px solid var(--border-sub)', padding:'14px 0' }}>
        <div style={{ maxWidth:680, margin:'0 auto', padding:'0 28px' }}>
          <div style={{ display:'flex', alignItems:'center', gap:10, height:46, padding:'0 8px 0 16px', borderRadius:999, background:'var(--s1)', border:'1px solid var(--border)' }}>
            <I.sparkles size={17} color="var(--c3)"/>
            <span style={{ flex:1, fontFamily:SANS, fontSize:14, color:'var(--c3)' }}>Ask anything across your recordings…</span>
            <button style={{ width:34, height:34, borderRadius:'50%', border:'none', background:'var(--accent)', color:'#fff', display:'flex', alignItems:'center', justifyContent:'center', cursor:'pointer' }}><I.send size={17} style={{ marginLeft:-1 }}/></button>
          </div>
        </div>
      </div>
    </div>
  );
}

// ── settings / account ─────────────────────────────────────────────────────────
function SettingsScreen({ t, setTweak }) {
  const [v, setV] = React.useState({ login:true, timer:true, sys:true, both:true, auto:true, transcript:true });
  const tog = (k)=>setV(s=>({ ...s, [k]:!s[k] }));
  const Row = ({ label, sub, k, right, last }) => (
    <div style={{ display:'flex', alignItems:'center', gap:14, padding:'14px 16px', borderTop: last===false?'1px solid var(--border-sub)':undefined }}>
      <span style={{ flex:1 }}>
        <span style={{ display:'block', fontFamily:SANS, fontSize:14, fontWeight:500, color:'var(--c1)' }}>{label}</span>
        {sub && <span style={{ display:'block', fontFamily:SANS, fontSize:12, color:'var(--c3)', marginTop:2 }}>{sub}</span>}
      </span>
      {right || <Toggle on={v[k]} onClick={()=>tog(k)}/>}
    </div>
  );
  const Group = ({ title, children }) => (
    <div style={{ marginBottom:24 }}>
      <div style={{ fontFamily:SANS, fontSize:11, fontWeight:600, letterSpacing:'.08em', textTransform:'uppercase', color:'var(--c3)', marginBottom:8 }}>{title}</div>
      <div style={{ borderRadius:14, background:'var(--s1)', border:'1px solid var(--border-sub)', overflow:'hidden' }}>{children}</div>
    </div>
  );
  return (
    <div style={{ flex:1, minWidth:0, overflowY:'auto', background:'var(--s0)', padding:'22px 28px' }}>
      <h1 style={{ margin:'0 0 20px', fontFamily:SANS, fontSize:24, fontWeight:700, letterSpacing:'-0.02em', color:'var(--c1)' }}>Settings</h1>
      <div style={{ maxWidth:620 }}>
        {/* account */}
        <div style={{ display:'flex', alignItems:'center', gap:16, padding:18, borderRadius:14, background:'var(--s1)', border:'1px solid var(--border-sub)', marginBottom:24 }}>
          <span style={{ width:54, height:54, borderRadius:'50%', background:'var(--s3)', border:'1.5px solid var(--border)', color:'var(--c1)', display:'flex', alignItems:'center', justifyContent:'center', fontFamily:SANS, fontWeight:600, fontSize:18, flexShrink:0 }}>RA</span>
          <span style={{ flex:1, minWidth:0 }}>
            <span style={{ display:'flex', alignItems:'center', gap:8 }}><span style={{ fontFamily:SANS, fontSize:17, fontWeight:700, color:'var(--c1)' }}>Rodrigo Arvizu</span><Tag variant="info" dot>PRO</Tag></span>
            <span style={{ display:'block', fontFamily:SANS, fontSize:13, color:'var(--c3)', marginTop:2 }}>rodrigo@xhinola.io</span>
          </span>
          <Btn variant="ghost" size="small" shape="pill">Manage account</Btn>
        </div>

        {/* plan / usage */}
        <Group title="Plan & usage">
          <div style={{ padding:16 }}>
            <div style={{ display:'flex', alignItems:'center', gap:10, marginBottom:14 }}>
              <span style={{ fontFamily:SANS, fontSize:15, fontWeight:700, color:'var(--c1)' }}>dot+ Pro</span>
              <span style={{ flex:1, fontFamily:SANS, fontSize:12.5, color:'var(--c3)' }}>Renews Jul 14, 2026</span>
              <Btn variant="secondary" size="small" shape="pill">Manage billing</Btn>
            </div>
            {[['Recordings this month','142 / Unlimited',1],['Transcription hours','6.2 / Unlimited',1],['AI queries','86 / 500',0.17]].map(([l,r,p],i)=>(
              <div key={i} style={{ marginTop: i?12:0 }}>
                <div style={{ display:'flex', justifyContent:'space-between', marginBottom:6, fontFamily:SANS, fontSize:12.5, color:'var(--c2)' }}><span>{l}</span><span style={{ fontFamily:MONO, fontSize:11, color:'var(--c3)' }}>{r}</span></div>
                <div style={{ height:5, borderRadius:3, background:'var(--s3)', overflow:'hidden' }}><div style={{ width:`${p*100}%`, height:'100%', background: p>0.9?'var(--warn)':'var(--accent)', borderRadius:3 }}/></div>
              </div>
            ))}
          </div>
        </Group>

        <Group title="General">
          <Row label="Launch dot+ at login" k="login"/>
          <Row label="Show recording timer in menu bar" k="timer" last={false}/>
          <Row label="Global record hotkey" right={<span style={{ display:'flex', gap:4 }}><Key>⌃</Key><Key>⌥</Key><Key>R</Key></span>} last={false}/>
          <Row label="Appearance" right={<AppearanceSeg t={t} setTweak={setTweak}/>} last={false}/>
        </Group>

        <Group title="Capture">
          <Row label="Capture system audio" sub="From Zoom, Google Meet, Teams and any app" k="sys"/>
          <Row label="Record microphone + system together" k="both" last={false}/>
          <Row label="Auto-record calendar meetings" sub="dot+ asks for confirmation before each meeting" k="auto" last={false}/>
          <Row label="Live transcript while recording" k="transcript" last={false}/>
        </Group>
      </div>
    </div>
  );
}

function AppearanceSeg({ t, setTweak }) {
  const opts = ['Dark','Light'];
  return (
    <div style={{ display:'flex', gap:2, padding:2, borderRadius:8, background:'var(--s2)' }}>
      {opts.map(o=>{
        const on = t.appearance===o;
        return <button key={o} onClick={()=>setTweak('appearance', o)} style={{ padding:'5px 12px', borderRadius:6, border:'none', cursor:'pointer', fontFamily:SANS, fontSize:12.5, fontWeight:600, background:on?'var(--s0)':'transparent', color:on?'var(--c1)':'var(--c3)', boxShadow:on?'0 1px 2px rgba(0,0,0,.15)':'none' }}>{o}</button>;
      })}
    </div>
  );
}

// ── window shell ─────────────────────────────────────────────────────────────
function AppWindow({ t, setTweak, tab, setTab, startRec, onClose, width, height }) {
  return (
    <div style={{ width, height, borderRadius:13, overflow:'hidden', display:'flex', flexDirection:'column', background:'var(--s0)', border:'1px solid var(--win-border)', boxShadow:'var(--pop-shadow)' }}>
      {/* titlebar */}
      <div style={{ height:42, flexShrink:0, display:'flex', alignItems:'center', gap:14, padding:'0 16px', background:'var(--s0)', borderBottom:'1px solid var(--border-sub)' }}>
        <div onClick={onClose} style={{ cursor:'pointer' }} title="Close"><TrafficLights/></div>
        <span style={{ flex:1 }}/>
        <span style={{ fontFamily:SANS, fontSize:13, fontWeight:600, color:'var(--c3)' }}>dot+</span>
        <span style={{ flex:1 }}/>
        <div style={{ width:54 }}/>
      </div>
      <div style={{ flex:1, display:'flex', minHeight:0 }}>
        <Sidebar tab={tab} setTab={setTab} startRec={startRec}/>
        {tab==='recordings' && <RecordingsScreen startRec={startRec}/>}
        {tab==='projects' && <ProjectsScreen/>}
        {tab==='contacts' && <ContactsScreen/>}
        {tab==='ai' && <AiScreen/>}
        {tab==='settings' && <SettingsScreen t={t} setTweak={setTweak}/>}
      </div>
    </div>
  );
}

Object.assign(window, { AppWindow });
