/* HelpBnk mobile — shared primitives across variants
   Aligned with web design system (styles.css, home.css, shared.jsx) */
const { useState, useEffect, useRef, useMemo, useCallback } = React;

/* ---------- Palette mirroring web styles.css ---------- */
const HB = {
  bg:      '#FFFFFF',
  bgSubtle:'#F4F6F9',
  bgTint:  '#E8F0FB',
  ink:     '#0B1220',
  ink2:    '#1B2434',
  ink3:    '#4A5566',
  ink4:    '#7B8595',
  ink5:    '#B6BECB',
  line:    '#E8ECF2',
  lineStr: '#D6DCE6',
  accent:  '#2D7FF9',
  accentInk:'#1255C3',
  accentTint:'#E8F0FB',
  /* secondary teal/cyan from the logo gradient, only used on the Journey card */
  cyan1:   '#3FB8E6',
  cyan2:   '#7BD9E6',
};

/* Web tile palette (from styles.css) */
const TILE = [
  ['#E8F0FB','#1F4FA0'],
  ['#E6F4EC','#1F6E45'],
  ['#FBF1E2','#8C5A1F'],
  ['#F5ECFB','#6A2EA8'],
  ['#FCEAEA','#A82E2E'],
  ['#E5F3F5','#1F6F7A'],
  ['#F1F2F5','#34425A'],
  ['#FBF7E0','#7A6A1F'],
];

/* Web avatar gradients (from home.css) */
const AV_GRAD = [
  'linear-gradient(135deg,#FF8A65,#D84315)',
  'linear-gradient(135deg,#5C6BC0,#283593)',
  'linear-gradient(135deg,#26A69A,#00695C)',
  'linear-gradient(135deg,#AB47BC,#6A1B9A)',
  'linear-gradient(135deg,#42A5F5,#1565C0)',
  'linear-gradient(135deg,#66BB6A,#2E7D32)',
  'linear-gradient(135deg,#EC407A,#AD1457)',
  'linear-gradient(135deg,#FFA726,#E65100)',
];

/* Intent prompt tints (from home.css) */
const PROMPT_TONES = {
  ask:    { bg:'rgba(45,127,249,0.12)',  fg:'#2D7FF9' },
  belong: { bg:'rgba(216,67,21,0.12)',   fg:'#D84315' },
  find:   { bg:'rgba(0,105,92,0.12)',    fg:'#00695C' },
  learn:  { bg:'rgba(106,27,154,0.12)',  fg:'#6A1B9A' },
  tools:  { bg:'rgba(230,81,0,0.12)',    fg:'#E65100' },
};

/* ---------- HelpBnk wordmark using the real asset ---------- */
const HelpBnkWordmark = ({ size = 22 }) => (
  <img src="assets/helpbnk-logo.png" alt="HelpBnk"
    style={{ height: size, width:'auto', display:'block' }}/>
);
const HelpBnkMark = ({ size = 28 }) => (
  <img src="assets/helpbnk-mark.png" alt="HelpBnk"
    style={{ height: size, width:'auto', display:'block' }}/>
);

/* ---------- Icon set mirroring web shared.jsx Icon ---------- */
const MI = {
  Home:     (p) => <svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M3 11.5 12 4l9 7.5"/><path d="M5 10v10h14V10"/></svg>,
  HomeFill: (p) => <svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill="currentColor"><path d="M12 3.2 2.4 11.3a1 1 0 0 0 .65 1.76H4V20a1 1 0 0 0 1 1h4v-6h6v6h4a1 1 0 0 0 1-1v-6.94h.95a1 1 0 0 0 .65-1.76L12 3.2z"/></svg>,
  Users:    (p) => <svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M22 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></svg>,
  Grad:     (p) => <svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M22 10v6M2 10l10-5 10 5-10 5z"/><path d="M6 12v5a6 3 0 0 0 12 0v-5"/></svg>,
  Wrench:   (p) => <svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M14.7 6.3a4 4 0 1 1-5.4 5.4L4 17l3 3 5.3-5.3a4 4 0 0 0 5.4-5.4l-2 2-2.6-2.6 2-2Z"/></svg>,
  Coins:    (p) => <svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><circle cx="8" cy="8" r="6"/><path d="M18.09 10.37A6 6 0 1 1 10.34 18"/><path d="M7 6h1v4M16.71 13.88l.71.71-2.83 2.82"/></svg>,
  Search:   (p) => <svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/></svg>,
  Bell:     (p) => <svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M6 9a6 6 0 1 1 12 0c0 5 2 6 2 6H4s2-1 2-6"/><path d="M10 19a2 2 0 0 0 4 0"/></svg>,
  Compass:  (p) => <svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="10"/><path d="m16.24 7.76-2.12 6.36-6.36 2.12 2.12-6.36 6.36-2.12z"/></svg>,
  Globe:    (p) => <svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="10"/><path d="M2 12h20"/><path d="M12 2a15 15 0 0 1 0 20M12 2a15 15 0 0 0 0 20"/></svg>,
  Book:     (p) => <svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M4 19.5A2.5 2.5 0 0 1 6.5 17H20"/><path d="M6.5 2H20v20H6.5A2.5 2.5 0 0 1 4 19.5v-15A2.5 2.5 0 0 1 6.5 2z"/></svg>,
  Sparkles: (p) => <svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="m12 3-1.9 5.8a2 2 0 0 1-1.3 1.3L3 12l5.8 1.9a2 2 0 0 1 1.3 1.3L12 21l1.9-5.8a2 2 0 0 1 1.3-1.3L21 12l-5.8-1.9a2 2 0 0 1-1.3-1.3Z"/></svg>,
  Mic:      (p) => <svg viewBox="0 0 24 24" width={p.size||18} height={p.size||18} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><rect x="9" y="2" width="6" height="12" rx="3"/><path d="M5 10v2a7 7 0 0 0 14 0v-2"/><path d="M12 19v3"/></svg>,
  ArrowUp:  (p) => <svg viewBox="0 0 24 24" width={p.size||16} height={p.size||16} fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M12 19V5m0 0-6 6m6-6 6 6"/></svg>,
  ArrowR:   (p) => <svg viewBox="0 0 24 24" width={p.size||14} height={p.size||14} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M5 12h14M13 5l7 7-7 7"/></svg>,
  ChevR:    (p) => <svg viewBox="0 0 24 24" width={p.size||14} height={p.size||14} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="m9 18 6-6-6-6"/></svg>,
  Plus:     (p) => <svg viewBox="0 0 24 24" width={p.size||18} height={p.size||18} fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round"><path d="M12 5v14M5 12h14"/></svg>,
  Sort:     (p) => <svg viewBox="0 0 24 24" width={p.size||16} height={p.size||16} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M8 4v16m0 0-3-3m3 3 3-3M16 20V4m0 0-3 3m3-3 3 3"/></svg>,
  Heart:    (p) => <svg viewBox="0 0 24 24" width={p.size||18} height={p.size||18} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M20.8 4.6a5.5 5.5 0 0 0-7.8 0L12 5.7l-1.1-1.1a5.5 5.5 0 0 0-7.8 7.8l1.1 1L12 21.2l7.7-7.7 1.1-1a5.5 5.5 0 0 0 0-7.9z"/></svg>,
  Comment:  (p) => <svg viewBox="0 0 24 24" width={p.size||18} height={p.size||18} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M21 12a8 8 0 1 1-3.6-6.6L21 4l-1.4 3.6A7.9 7.9 0 0 1 21 12z"/></svg>,
  Share:    (p) => <svg viewBox="0 0 24 24" width={p.size||18} height={p.size||18} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M12 3v13"/><path d="m7 8 5-5 5 5"/><path d="M5 14v5a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2v-5"/></svg>,
  Dots:     (p) => <svg viewBox="0 0 24 24" width={p.size||18} height={p.size||18} fill="currentColor"><circle cx="5" cy="12" r="1.7"/><circle cx="12" cy="12" r="1.7"/><circle cx="19" cy="12" r="1.7"/></svg>,
  Calendar: (p) => <svg viewBox="0 0 24 24" width={p.size||16} height={p.size||16} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><rect x="3" y="4" width="18" height="18" rx="2"/><path d="M16 2v4M8 2v4M3 10h18"/></svg>,
  Hash:     (p) => <svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M4 9h16M4 15h16M10 3 8 21M16 3l-2 18"/></svg>,
};

/* ---------- Status bar (390-wide tuned) ---------- */
const StatusBar = ({ dark = false }) => {
  const c = dark ? '#fff' : '#000';
  return (
    <div style={{ display:'flex', justifyContent:'space-between', alignItems:'center',
      padding:'14px 28px 6px',
      fontFamily:'-apple-system, "SF Pro", system-ui',
      fontSize:15, fontWeight:600, color: c, position:'relative', zIndex:5 }}>
      <span style={{ paddingTop:1 }}>9:41</span>
      <div style={{ display:'flex', gap:6, alignItems:'center', paddingRight:2 }}>
        <svg width="17" height="11" viewBox="0 0 17 11"><rect x="0" y="6" width="3" height="5" rx=".6" fill={c}/><rect x="4.5" y="4" width="3" height="7" rx=".6" fill={c}/><rect x="9" y="2" width="3" height="9" rx=".6" fill={c}/><rect x="13.5" y="0" width="3" height="11" rx=".6" fill={c}/></svg>
        <svg width="16" height="11" viewBox="0 0 17 12"><path d="M8.5 3.2C10.8 3.2 12.9 4.1 14.4 5.6L15.5 4.5C13.7 2.7 11.2 1.5 8.5 1.5C5.8 1.5 3.3 2.7 1.5 4.5L2.6 5.6C4.1 4.1 6.2 3.2 8.5 3.2Z" fill={c}/><path d="M8.5 6.8C9.9 6.8 11.1 7.3 12 8.2L13.1 7.1C11.8 5.9 10.2 5.1 8.5 5.1C6.8 5.1 5.2 5.9 3.9 7.1L5 8.2C5.9 7.3 7.1 6.8 8.5 6.8Z" fill={c}/><circle cx="8.5" cy="10.5" r="1.5" fill={c}/></svg>
        <svg width="24" height="11" viewBox="0 0 27 13"><rect x="0.5" y="0.5" width="23" height="12" rx="3" stroke={c} strokeOpacity=".4" fill="none"/><rect x="2" y="2" width="20" height="9" rx="2" fill={c}/><path d="M25 4.5V8.5C25.8 8.2 26.5 7.2 26.5 6.5C26.5 5.8 25.8 4.8 25 4.5Z" fill={c} fillOpacity=".4"/></svg>
      </div>
    </div>
  );
};

/* ---------- Phone frame wrapper ---------- */
const Phone = ({ children, dark = false, bg = '#fff' }) => (
  <div style={{
    width: 390, height: 844, borderRadius: 54, overflow:'hidden',
    position:'relative', background: dark ? '#0B1220' : bg,
    boxShadow: '0 30px 70px rgba(0,0,0,0.16), 0 0 0 1px rgba(0,0,0,0.12), inset 0 0 0 8px #0B0F18',
    fontFamily:"'Plus Jakarta Sans', -apple-system, system-ui, sans-serif",
    letterSpacing:'-0.005em',
  }}>
    <div style={{ position:'absolute', top:11, left:'50%', transform:'translateX(-50%)',
      width:122, height:34, borderRadius:24, background:'#000', zIndex:50 }}/>
    <div style={{ width:'100%', height:'100%', display:'flex', flexDirection:'column' }}>
      <StatusBar dark={dark}/>
      <div style={{ flex:1, overflow:'hidden', position:'relative' }}>{children}</div>
    </div>
    <div style={{ position:'absolute', bottom:9, left:'50%', transform:'translateX(-50%)',
      width:134, height:5, borderRadius:100, background: dark ? 'rgba(255,255,255,.55)' : 'rgba(0,0,0,.28)',
      zIndex:60, pointerEvents:'none' }}/>
    {/* Bezel drawn on top so the dark phone edge stays visible all the way around, even over the bottom nav */}
    <div style={{ position:'absolute', inset:0, borderRadius:54, boxShadow:'inset 0 0 0 8px #0B0F18', pointerEvents:'none', zIndex:100 }}/>
  </div>
);

/* ---------- Avatar — web gradient palette ---------- */
const Avatar = ({ name='A B', idx=0, size=36, ring=false }) => {
  const initials = name.split(' ').map(n=>n[0]).slice(0,2).join('').toUpperCase();
  return (
    <div style={{
      width:size, height:size, borderRadius:'50%',
      background: AV_GRAD[idx % AV_GRAD.length], color:'#fff',
      display:'inline-flex', alignItems:'center', justifyContent:'center',
      fontWeight:700, fontSize: size*0.36, flexShrink:0,
      boxShadow: ring ? '0 0 0 2px #fff, 0 0 0 4px rgba(45,127,249,.4)' : 'none',
    }}>{initials}</div>
  );
};

/* ---------- Striped placeholder ---------- */
const Stripes = ({ label, h, r=12, tone='light', style }) => (
  <div style={{
    height: h, borderRadius: r, width:'100%',
    backgroundColor: tone === 'dark' ? 'rgba(255,255,255,.07)' : 'rgba(11,18,32,.04)',
    backgroundImage: `repeating-linear-gradient(-45deg, transparent 0 10px, ${tone==='dark'?'rgba(255,255,255,.06)':'rgba(11,18,32,.04)'} 10px 20px)`,
    display:'flex', alignItems:'center', justifyContent:'center',
    fontFamily:'JetBrains Mono, ui-monospace, monospace',
    fontSize:10, letterSpacing:'.04em',
    color: tone==='dark' ? 'rgba(255,255,255,.55)' : 'rgba(11,18,32,.4)',
    textTransform:'lowercase', overflow:'hidden', ...style,
  }}>{label}</div>
);

/* ---------- Intent prompts — same set as web home ---------- */
const INTENT_PROMPTS = [
  { tone:'ask',    icon:'Users',  label:'Ask another founder a question' },
  { tone:'belong', icon:'Globe',  label:'Find a community of founders like me' },
  { tone:'learn',  icon:'Book',   label:'Find a course to learn about business' },
  { tone:'tools',  icon:'Wrench', label:'Find the right tools for my business' },
];

/* ---------- Feed posts ---------- */
const FEED_POSTS = [
  { id:1, name:'Lina Alturki',   handle:'EU DTC Founders', when:'12m',
    body:`I joined HelpBnk last month, and the community has been so supportive. I've already made valuable connections and gained insights into my career and finances.`,
    likes:16, replies:21, av:0, follow:true },
  { id:2, name:'Marcus Wei',     handle:'EU DTC Founders', when:'38m',
    body:`How is everyone handling the new IOSS thresholds for sub-€150 shipments into France? My 3PL just bumped fees.`,
    likes:18, replies:22, av:1 },
  { id:3, name:'Esther Kim',     handle:'AI Builders', when:'1h',
    body:`Shared our last 3 winning ad creatives. Top one ran 0.87 CPC for 9 days straight. AMA.`,
    likes:91, replies:31, av:2 },
];

/* ---------- Bottom nav matching web pages ---------- */
const NAV_ITEMS = [
  { id:'Home',      icon:'HomeFill', altIcon:'Home',  label:'Home',      href:'index.html' },
  { id:'Community', icon:'Users',                    label:'Community',  href:'community.html' },
  { id:'AI',        icon:'Sparkles',                 label:'AI',         href:'chat.html' },
  { id:'Learn',     icon:'Grad',                     label:'Learn',      href:'learn.html' },
  { id:'Tools',     icon:'Wrench',                   label:'Tools',      href:'tools.html' },
];

const BottomNav = ({ active='Home', dark=false, emphasizeAI=false }) => {
  const fg  = dark ? 'rgba(255,255,255,.55)' : HB.ink4;
  const fgA = dark ? '#fff' : HB.ink;
  return (
    <div style={{
      paddingBottom: 26, paddingTop: 8,
      borderTop: dark ? '1px solid rgba(255,255,255,.08)' : `1px solid ${HB.line}`,
      background: dark ? 'rgba(11,18,32,.7)' : 'rgba(255,255,255,.92)',
      backdropFilter:'saturate(180%) blur(14px)', WebkitBackdropFilter:'saturate(180%) blur(14px)',
    }}>
      <div style={{ display:'flex', justifyContent:'space-around', alignItems:'flex-end', padding:'0 6px' }}>
        {NAV_ITEMS.map(it => {
          const isA = it.id === active;
          const isAI = it.id === 'AI';
          /* Center AI tab gets a gradient pill treatment when emphasized */
          if (isAI && emphasizeAI) {
            return (
              <button key={it.id} style={{
                flex:1, display:'flex', flexDirection:'column', alignItems:'center', gap:3,
                padding:'0', background:'transparent', border:'none', cursor:'pointer',
              }}>
                <div style={{
                  width:46, height:46, borderRadius:'50%',
                  background:`linear-gradient(135deg, ${HB.cyan1}, ${HB.cyan2})`,
                  color:'#fff', display:'inline-flex', alignItems:'center', justifyContent:'center',
                  boxShadow:'0 8px 20px -6px rgba(63,184,230,.7), 0 0 0 4px rgba(123,217,230,.18)',
                  marginTop:-12,
                }}><MI.Sparkles size={22}/></div>
                <span style={{ fontSize:10.5, fontWeight:700, color: fgA, letterSpacing:'-.01em', marginTop:1 }}>{it.label}</span>
              </button>
            );
          }
          const Cmp = MI[isA && it.altIcon ? it.icon : (it.altIcon || it.icon)] || MI.Home;
          return (
            <button key={it.id} style={{
              flex:1, display:'flex', flexDirection:'column', alignItems:'center', gap:3,
              padding:'4px 0',
              color: isA ? fgA : fg,
              background:'transparent', border:'none', cursor:'pointer',
            }}>
              <Cmp size={22}/>
              <span style={{ fontSize:10.5, fontWeight: isA ? 700 : 600, letterSpacing:'-.01em' }}>{it.label}</span>
            </button>
          );
        })}
      </div>
    </div>
  );
};

Object.assign(window, {
  HB, TILE, AV_GRAD, PROMPT_TONES,
  HelpBnkWordmark, HelpBnkMark, MI,
  StatusBar, Phone, Avatar, Stripes,
  INTENT_PROMPTS, FEED_POSTS, NAV_ITEMS, BottomNav,
});
