/* ── Zafa — Vendor funnel: Verify → Create Account → Business Details → Choose plan ── */

/* numbered step list (shown on Become page) */
const VFLOW = [
['phone', 'Verification', 'التحقق'],
['user', 'Create Account', 'إنشاء حساب'],
['store', 'Business Details', 'تفاصيل النشاط'],
['card', 'Choose Plan', 'اختر الباقة']];


/* progress dots for the funnel (4 steps) */
function VSteps({ i, n = 4 }) {
  return (
    <div style={{ display: 'flex', gap: 6, alignItems: 'center' }}>
      {Array.from({ length: n }).map((_, k) =>
      <span key={k} style={{ height: 5, width: k === i ? 22 : 5, borderRadius: 99, background: k <= i ? 'var(--rose)' : 'var(--line)', transition: 'all .25s' }} />
      )}
    </div>);

}

/* two-part tab stepper used inside Business Details */
function VTabSteps({ active, labels }) {
  const { lang } = useZafa();
  return (
    <div style={{ display: 'flex', gap: 10, alignItems: 'center' }}>
      {labels.map(([en, ar], i) => {
        const on = i === active;const done = i < active;
        return (
          <React.Fragment key={i}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 7, opacity: on || done ? 1 : 0.5 }}>
              <span style={{ width: 22, height: 22, borderRadius: '50%', flexShrink: 0, display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 11, fontWeight: 700, background: on || done ? '#F6B0C6' : 'var(--line)', color: on || done ? 'var(--ink)' : 'var(--ink-2)' }}>{done ? <Icon name="check" size={12} stroke={3} /> : i + 1}</span>
              <span style={{ fontSize: 12.5, fontWeight: on ? 700 : 600, color: on ? 'var(--ink)' : 'var(--ink-2)' }}>{L(lang, en, ar)}</span>
            </div>
            {i < labels.length - 1 && <span style={{ flex: 1, height: 1.5, background: 'var(--line)', minWidth: 12 }} />}
          </React.Fragment>);

      })}
    </div>);

}

/* 0 ── Become a vendor (intro + 4-step summary) */
function VBecome({ onNext, onExit }) {
  const { lang } = useZafa();
  return (
    <div style={{ height: '100%', display: 'flex', flexDirection: 'column', background: 'var(--bg)', position: 'relative' }}>
      <div style={{ padding: '6px 20px 6px', position: 'relative', zIndex: 1 }}><ScreenHeader title={L(lang, 'Become a vendor', 'كن مزوّداً')} onBack={onExit} /></div>
      <div className="z-scroll no-bar" style={{ flex: 1, padding: '8px 20px 20px', position: 'relative', zIndex: 1 }}>
        <span className="eyebrow">{L(lang, 'Grow your business', 'نمِّ نشاطك')}</span>
        <h1 className="h1" style={{ margin: '10px 0 10px' }}>{L(lang, 'List your services & reach thousands of couples', 'اعرض خدماتك وتواصل مع آلاف المقبلين على الزواج')}</h1>
        <p className="sec" style={{ margin: 0, fontSize: 14.5, lineHeight: 1.55, textWrap: 'pretty' }}>
          {L(lang, 'A paid marketplace for wedding professionals. Just four quick steps to get published.', 'منصّة مدفوعة لمحترفي الأعراس. أربع خطوات سريعة لنشر ملفك.')}
        </p>

        {/* 4-step summary */}
        <p className="lbl" style={{ margin: '24px 0 12px' }}>{L(lang, 'How it works', 'كيف تعمل')}</p>
        <div style={{ display: 'flex', flexDirection: 'column', gap: 0 }}>
          {VFLOW.map(([ic, en, ar], i) =>
          <div key={i} style={{ display: 'flex', gap: 14, alignItems: 'flex-start' }}>
              <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', alignSelf: 'stretch' }}>
                <span style={{ width: 38, height: 38, borderRadius: '50%', flexShrink: 0, display: 'flex', alignItems: 'center', justifyContent: 'center', fontWeight: 800, fontSize: 15, color: '#F6B0C6', background: '#FCE7EE', border: '1.5px solid #EEACC2' }}>{i + 1}</span>
                {i < VFLOW.length - 1 && <span style={{ flex: 1, width: 2, background: 'var(--line)', margin: '4px 0' }} />}
              </div>
              <div style={{ flex: 1, paddingBottom: i < VFLOW.length - 1 ? 18 : 0, paddingTop: 6 }}>
                <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
                  <Icon name={ic} size={17} style={{ color: '#F6B0C6' }} />
                  <p style={{ margin: 0, fontWeight: 700, fontSize: 15 }}>{L(lang, en, ar)}</p>
                </div>
                <p className="sec" style={{ margin: '4px 0 0', fontSize: 12.5, lineHeight: 1.4 }}>{L(lang,
                ['Verify by phone or email — or continue with Google / Apple.', 'Set your name, email & password.', 'Add company info, then upload your documents.', 'Pick Standard or Upper, pay, and go live.'][i],
                ['تحقّق عبر الهاتف أو البريد — أو تابع بجوجل / آبل.', 'حدّد اسمك وبريدك وكلمة المرور.', 'أضف معلومات الشركة ثم ارفع مستنداتك.', 'اختر الأساسية أو المتقدّمة، ادفع، وانطلق.'][i])}</p>
              </div>
            </div>
          )}
        </div>
      </div>
      <div style={{ padding: '12px 20px 26px', borderTop: '1px solid var(--line)', background: '#fff' }}>
        <Btn variant="rose" iconR="arrow" onClick={onNext}>{L(lang, 'Get started', 'لنبدأ')}</Btn>
      </div>
    </div>);

}

/* ── Step 1: Phone OR Email verification (+ Google / Apple) ── */
function VVerify({ onNext, onBack }) {
  const { lang } = useZafa();
  const [method, setMethod] = React.useState('phone'); // phone | email
  const [value, setValue] = React.useState('');
  const [sent, setSent] = React.useState(false);
  const [code, setCode] = React.useState('');
  const [status, setStatus] = React.useState('idle');
  const N = 4;
  React.useEffect(() => {
    if (code.length === N) {
      setStatus('verifying');
      const id = setTimeout(() => {if (code === '1234') onNext();else {setStatus('error');setTimeout(() => {setCode('');setStatus('idle');}, 1100);}}, 850);
      return () => clearTimeout(id);
    }
  }, [code]);
  const valid = method === 'phone' ? value.replace(/\D/g, '').length >= 9 : /\S+@\S+\.\S+/.test(value);

  return (
    <div style={{ height: '100%', display: 'flex', flexDirection: 'column', background: 'var(--bg)', position: 'relative' }}>
      {!sent && <img src={window.ASSET('bgVerify')} alt="" aria-hidden="true" style={{ position: 'absolute', top: 96, insetInlineEnd: -36, width: 168, height: 'auto', opacity: 0.5, pointerEvents: 'none', zIndex: 0 }} className="flip-x" />}
      <div style={{ padding: '6px 20px 4px', position: 'relative', zIndex: 1 }}><ScreenHeader title={L(lang, 'Verification', 'التحقق')} onBack={onBack} /></div>
      <div style={{ padding: '6px 20px 0', position: 'relative', zIndex: 1 }}><VSteps i={0} /></div>
      <div className="z-scroll no-bar" style={{ flex: 1, padding: '16px 20px 20px', position: 'relative', zIndex: 1, background: 'transparent' }}>
        {!sent ?
        <>
            <h1 className="h1" style={{ margin: '0 0 8px' }}>{L(lang, 'Let\u2019s verify you', 'لنتحقّق منك')}</h1>
            <p className="sec" style={{ margin: 0, fontSize: 14.5, lineHeight: 1.5 }}>{L(lang, 'Verify with your phone or email to continue.', 'تحقّق عبر هاتفك أو بريدك للمتابعة.')}</p>
            {/* method toggle */}
            <div className="z-seg z-seg--dark" style={{ marginTop: 20 }}>
              <button className={method === 'phone' ? 'on' : ''} onClick={() => {setMethod('phone');setValue('');}}>{L(lang, 'Phone', 'الهاتف')}</button>
              <button className={method === 'email' ? 'on' : ''} onClick={() => {setMethod('email');setValue('');}}>{L(lang, 'Email', 'البريد')}</button>
            </div>
            <div style={{ marginTop: 16 }}>
              {method === 'phone' ?
            <div style={{ display: 'flex', gap: 10 }}>
                  <div style={{ display: 'flex', alignItems: 'center', gap: 8, height: 56, padding: '0 14px', borderRadius: 14, border: '1.5px solid var(--line)', background: '#fff', flexShrink: 0 }}><span style={{ fontSize: 20 }}>🇦🇪</span><span style={{ fontWeight: 600, fontSize: 15 }}>+971</span></div>
                  <input className="z-input" inputMode="tel" value={value} onChange={(e) => setValue(e.target.value.replace(/\D/g, '').slice(0, 9))} placeholder="50 123 4567" style={{ direction: 'ltr', textAlign: lang === 'ar' ? 'right' : 'left' }} />
                </div> :

            <input className="z-input" type="email" value={value} onChange={(e) => setValue(e.target.value)} placeholder="name@business.com" style={{ direction: 'ltr', textAlign: lang === 'ar' ? 'right' : 'left' }} />
            }
            </div>

            {/* social login */}
            <div style={{ display: 'flex', alignItems: 'center', gap: 12, margin: '24px 0 16px' }}>
              <div className="z-hr" style={{ flex: 1 }} /><span className="muted" style={{ fontSize: 12.5 }}>{L(lang, 'or continue with', 'أو تابع عبر')}</span><div className="z-hr" style={{ flex: 1 }} />
            </div>
            <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
              <button onClick={onNext} className="z-btn z-btn--outline" style={{ gap: 10 }}>
                <svg width="19" height="19" viewBox="0 0 24 24" aria-hidden="true"><path fill="#EA4335" d="M12 10.2v3.9h5.5c-.24 1.4-1.7 4.1-5.5 4.1-3.3 0-6-2.7-6-6.1S8.7 6 12 6c1.9 0 3.1.8 3.8 1.5l2.6-2.5C16.7 3.5 14.6 2.6 12 2.6 6.9 2.6 2.8 6.7 2.8 12S6.9 21.4 12 21.4c5.9 0 9.8-4.1 9.8-9.9 0-.7-.1-1.2-.2-1.7H12z" /></svg>
                {L(lang, 'Continue with Google', 'تابع عبر جوجل')}
              </button>
              <button onClick={onNext} className="z-btn z-btn--ink" style={{ gap: 10 }}>
                <svg width="18" height="18" viewBox="0 0 24 24" fill="#fff" aria-hidden="true"><path d="M16.4 12.7c0-2.2 1.8-3.3 1.9-3.3-1-1.5-2.6-1.7-3.2-1.7-1.4-.1-2.6.8-3.3.8-.7 0-1.7-.8-2.8-.8-1.5 0-2.8.8-3.5 2.1-1.5 2.6-.4 6.4 1.1 8.5.7 1 1.5 2.2 2.6 2.1 1-.04 1.4-.7 2.7-.7s1.6.7 2.7.6c1.1 0 1.8-1 2.5-2 .8-1.2 1.1-2.3 1.1-2.4-.02-.01-2.1-.8-2.1-3.2zM14.3 6.3c.6-.7 1-1.7.9-2.7-.9.04-1.9.6-2.5 1.3-.5.6-1 1.6-.9 2.6 1 .07 1.9-.5 2.5-1.2z" /></svg>
                {L(lang, 'Continue with Apple', 'تابع عبر آبل')}
              </button>
            </div>
          </> :

        <>
            <h1 className="h1" style={{ margin: '0 0 8px' }}>{L(lang, 'Enter the code', 'أدخل الرمز')}</h1>
            <p className="sec" style={{ margin: 0, fontSize: 14.5, lineHeight: 1.5 }}>{L(lang, 'We sent a 4-digit code to', 'أرسلنا رمزاً من ٤ أرقام إلى')} <b style={{ color: 'var(--ink)', direction: 'ltr', display: 'inline-block' }}>{method === 'phone' ? '+971 ' + value : value}</b></p>
            <div style={{ display: 'flex', gap: 10, marginTop: 24, direction: 'ltr' }}>
              {Array.from({ length: N }).map((_, i) => {
              const ch = code[i];const active = i === code.length && status !== 'verifying';const err = status === 'error';
              return <div key={i} style={{ flex: 1, height: 60, borderRadius: 14, display: 'flex', alignItems: 'center', justifyContent: 'center', background: '#fff', fontSize: 24, fontWeight: 700, color: err ? '#C0392B' : 'var(--ink)', border: `1.5px solid ${err ? '#E8A99F' : active ? 'var(--rose)' : ch ? 'var(--rose-200)' : 'var(--line)'}`, boxShadow: active ? '0 0 0 4px var(--rose-100)' : 'none' }}>{ch || ''}</div>;
            })}
            </div>
            <div style={{ marginTop: 14, minHeight: 22, display: 'flex', alignItems: 'center', gap: 8 }}>
              {status === 'verifying' && <><span className="z-spin z-spin--ink" /><span className="sec" style={{ fontSize: 13.5 }}>{L(lang, 'Verifying…', 'جارٍ التحقق…')}</span></>}
              {status === 'error' && <span style={{ color: '#C0392B', fontSize: 13.5, fontWeight: 600 }}>{L(lang, 'Wrong code. Try again.', 'رمز خاطئ. حاول مجدداً.')}</span>}
              {status === 'idle' && <button onClick={() => {setSent(false);setCode('');}} style={{ background: 'none', border: 'none', color: 'var(--rose-deep)', fontWeight: 600, fontSize: 13.5, cursor: 'pointer', fontFamily: 'var(--sans)' }}>{L(lang, 'Change', 'تغيير')} {method === 'phone' ? L(lang, 'number', 'الرقم') : L(lang, 'email', 'البريد')}</button>}
            </div>
            <div style={{ marginTop: 8, padding: '10px 14px', borderRadius: 12, background: 'var(--rose-100)', display: 'inline-flex', alignSelf: 'flex-start', gap: 7, alignItems: 'center' }}>
              <Icon name="spark" size={15} style={{ color: 'var(--rose-deep)' }} /><span style={{ fontSize: 12.5, color: 'var(--rose-deep)', fontWeight: 600 }}>{L(lang, 'Demo code — use 1234', 'رمز التجربة — ١٢٣٤')}</span>
            </div>
            <div style={{ marginTop: 18 }}>
              <Keypad onKey={(k) => status !== 'verifying' && setCode((c) => (c + k).slice(0, N))} onBack={() => setCode((c) => c.slice(0, -1))} />
            </div>
          </>
        }
      </div>
      {!sent && <div style={{ padding: '12px 20px 26px', borderTop: '1px solid var(--line)', background: '#fff' }}>
        <Btn variant="rose" disabled={!valid} onClick={() => setSent(true)}>{L(lang, 'Send code', 'إرسال الرمز')}</Btn>
      </div>}
    </div>);

}

/* ── Step 2: Create Account ── */
function VCreateAccount({ onNext, onBack }) {
  const { lang } = useZafa();
  const [f, setF] = React.useState({ name: '', email: '', phone: '', pw: '', cpw: '' });
  const [waToggle, setWaToggle] = React.useState(true);
  const [agree, setAgree] = React.useState(false);
  const [done, setDone] = React.useState(false);
  const set = (k) => (e) => setF((s) => ({ ...s, [k]: e.target.value }));
  const valid = f.name.trim() && /\S+@\S+\.\S+/.test(f.email) && f.phone.trim() && f.pw.length >= 6 && f.pw === f.cpw && agree;

  if (done) {
    return (
      <div style={{ height: '100%', display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', padding: '30px 28px 40px', background: 'var(--bg)', textAlign: 'center' }}>
        <div style={{ width: 104, height: 104, borderRadius: '50%', display: 'flex', alignItems: 'center', justifyContent: 'center', background: 'var(--rose-100)', color: '#2E9E6B', animation: 'pop .5s' }}><Icon name="check" size={52} stroke={2.6} /></div>
        <h1 className="h1" style={{ margin: '24px 0 10px' }}>{L(lang, 'Account created!', 'تم إنشاء الحساب!')}</h1>
        <p className="sec" style={{ margin: 0, fontSize: 15, lineHeight: 1.55, maxWidth: 290 }}>{L(lang, 'Welcome aboard. Next, tell us about your business.', 'أهلاً بك. التالي، أخبرنا عن نشاطك.')}</p>
        <div style={{ marginTop: 28, width: '100%', maxWidth: 300 }}><Btn variant="rose" iconR="arrow" onClick={onNext}>{L(lang, 'Add business details', 'أضف تفاصيل النشاط')}</Btn></div>
      </div>);

  }
  return (
    <div style={{ height: '100%', display: 'flex', flexDirection: 'column', background: 'var(--bg)' }}>
      <div style={{ padding: '6px 20px 4px' }}><ScreenHeader title={L(lang, 'Create account', 'إنشاء حساب')} onBack={onBack} /></div>
      <div style={{ padding: '6px 20px 0' }}><VSteps i={1} /></div>
      <div className="z-scroll no-bar" style={{ flex: 1, padding: '16px 20px 20px' }}>
        <div style={{ display: 'flex', flexDirection: 'column', gap: 14 }}>
          <AdField label={L(lang, 'Name', 'الاسم')}><input className="z-input" value={f.name} onChange={set('name')} placeholder={L(lang, 'Full name', 'الاسم الكامل')} /></AdField>
          <AdField label={L(lang, 'Email address', 'البريد الإلكتروني')}><input className="z-input" type="email" value={f.email} onChange={set('email')} placeholder="name@business.com" style={{ direction: 'ltr', textAlign: lang === 'ar' ? 'right' : 'left' }} /></AdField>
          <AdField label={L(lang, 'Phone number', 'رقم الهاتف')}><input className="z-input" inputMode="tel" value={f.phone} onChange={set('phone')} placeholder="+971 50 123 4567" style={{ direction: 'ltr', textAlign: lang === 'ar' ? 'right' : 'left' }} /></AdField>
          <button onClick={() => setWaToggle((v) => !v)} style={{ display: 'flex', alignItems: 'center', gap: 11, background: 'none', border: 'none', cursor: 'pointer', textAlign: 'start', padding: '0', marginTop: -4 }}>
            <span style={{ width: 30, height: 30, borderRadius: 9, background: '#25D366', color: '#fff', display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0 }}><Icon name="whatsapp" size={17} /></span>
            <span style={{ flex: 1, fontSize: 13.5, fontWeight: 500, color: 'var(--ink-2)', lineHeight: 1.35 }}>{L(lang, 'Use this number as my WhatsApp', 'استخدم هذا الرقم كواتساب')}</span>
            <span style={{ width: 46, height: 27, borderRadius: 99, background: waToggle ? 'var(--rose)' : 'var(--line)', position: 'relative', transition: 'background .15s', flexShrink: 0 }}>
              <span style={{ position: 'absolute', top: 3, insetInlineStart: waToggle ? 22 : 3, width: 21, height: 21, borderRadius: '50%', background: '#fff', transition: 'inset-inline-start .15s', boxShadow: '0 1px 3px rgba(0,0,0,.2)' }} />
            </span>
          </button>
          <AdField label={L(lang, 'Password', 'كلمة المرور')}><input className="z-input" type="password" value={f.pw} onChange={set('pw')} placeholder="••••••••" style={{ direction: 'ltr', textAlign: lang === 'ar' ? 'right' : 'left' }} /></AdField>
          <AdField label={L(lang, 'Confirm password', 'تأكيد كلمة المرور')}>
            <input className="z-input" type="password" value={f.cpw} onChange={set('cpw')} placeholder="••••••••" style={{ direction: 'ltr', textAlign: lang === 'ar' ? 'right' : 'left' }} />
            {f.cpw && f.pw !== f.cpw && <span style={{ display: 'block', fontSize: 11.5, marginTop: 5, color: '#C0392B', fontWeight: 600 }}>{L(lang, 'Passwords don\u2019t match', 'كلمتا المرور غير متطابقتين')}</span>}
          </AdField>

          {/* terms checkbox */}
          <button onClick={() => setAgree((a) => !a)} style={{ display: 'flex', gap: 11, alignItems: 'flex-start', background: 'none', border: 'none', cursor: 'pointer', textAlign: 'start', padding: '4px 0' }}>
            <span style={{ width: 22, height: 22, borderRadius: 7, flexShrink: 0, marginTop: 1, border: agree ? 'none' : '2px solid var(--line)', background: agree ? 'var(--ink)' : 'transparent', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>{agree && <Icon name="check" size={14} stroke={3} style={{ color: '#fff' }} />}</span>
            <span style={{ flex: 1, fontSize: 13, lineHeight: 1.5, color: 'var(--ink-2)' }}>
              {L(lang, 'By creating an account, I agree to our ', 'بإنشاء حساب، أوافق على ')}
              <span style={{ color: 'var(--rose-deep)', fontWeight: 600 }}>{L(lang, 'Terms of Use', 'شروط الاستخدام')}</span>
              {L(lang, ' and ', ' و')}
              <span style={{ color: 'var(--rose-deep)', fontWeight: 600 }}>{L(lang, 'Privacy Policy', 'سياسة الخصوصية')}</span>.
            </span>
          </button>
        </div>
      </div>
      <div style={{ padding: '12px 20px 26px', borderTop: '1px solid var(--line)', background: '#fff' }}>
        <Btn variant="rose" disabled={!valid} onClick={() => setDone(true)}>{L(lang, 'Sign Up', 'إنشاء الحساب')}</Btn>
      </div>
    </div>);

}

/* upload row for documents */
function VUpload({ id, label, optional }) {
  const { lang } = useZafa();
  return (
    <div>
      <p className="lbl" style={{ marginBottom: 8 }}>{label} {optional && <span style={{ color: 'var(--muted)', fontWeight: 600 }}>· {L(lang, 'optional', 'اختياري')}</span>}</p>
      <div style={{ position: 'relative', height: 84, borderRadius: 14, overflow: 'hidden', border: '1.5px dashed var(--rose-200)', background: 'var(--rose-soft)' }}>
        <Img id={id} radius={14} ph="" />
        <div style={{ position: 'absolute', inset: 0, display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 9, color: 'var(--rose-deep)', pointerEvents: 'none' }}>
          <Icon name="share" size={18} className="flip-x" style={{ transform: 'rotate(-90deg)' }} /><span style={{ fontSize: 13, fontWeight: 600 }}>{L(lang, 'Tap or drag to upload', 'اضغط أو اسحب للرفع')}</span>
        </div>
      </div>
    </div>);

}

/* ── Step 3: Business Details (2 parts) ── */
function VBusiness({ onNext, onBack, profile, setProfile }) {
  const { lang } = useZafa();
  const [part, setPart] = React.useState(0); // 0 = details, 1 = documents
  const labels = [['Business Details', 'تفاصيل النشاط'], ['Verification & Documents', 'التحقق والمستندات']];
  const [b, setB] = React.useState(profile.biz || { company: '', cats: ['flowers'], license: '', desc: '', web: '', years: '', building: '', city: 'Dubai', post: '', region: '' });
  const [pick, setPick] = React.useState('photography');
  const set = (k) => (e) => setB((s) => ({ ...s, [k]: e.target.value }));
  const addCat = () => setB((s) => s.cats.includes(pick) ? s : { ...s, cats: [...s.cats, pick] });
  const removeCat = (id) => setB((s) => ({ ...s, cats: s.cats.filter((x) => x !== id) }));
  const p1valid = b.company.trim() && b.license.trim() && b.city.trim() && b.cats.length > 0;

  return (
    <div style={{ height: '100%', display: 'flex', flexDirection: 'column', background: 'var(--bg)' }}>
      <div style={{ padding: '6px 20px 4px' }}><ScreenHeader title={L(lang, 'Business details', 'تفاصيل النشاط')} onBack={part === 0 ? onBack : () => setPart(0)} /></div>
      <div style={{ padding: '6px 20px 2px' }}><VSteps i={2} /></div>
      <div style={{ padding: '12px 20px 4px' }}><VTabSteps active={part} labels={labels} /></div>

      <div className="z-scroll no-bar" style={{ flex: 1, padding: '14px 20px 20px' }}>
        {part === 0 ?
        <div style={{ display: 'flex', flexDirection: 'column', gap: 14 }}>
            <AdField label={L(lang, 'Company name', 'اسم الشركة')}><input className="z-input" value={b.company} onChange={set('company')} placeholder={L(lang, 'e.g. Atelier Lumière', 'مثال: أتيليه لوميير')} /></AdField>
            <div>
              <p className="lbl" style={{ marginBottom: 8 }}>{L(lang, 'Company logo', 'شعار الشركة')}</p>
              <div style={{ display: 'flex', gap: 12, alignItems: 'center' }}>
                <div style={{ width: 72, height: 72, borderRadius: 16, overflow: 'hidden', border: '1.5px dashed var(--rose-200)', background: 'var(--rose-soft)', flexShrink: 0, position: 'relative' }}>
                  <Img id="vbiz-logo" radius={15} ph="" />
                  <div style={{ position: 'absolute', inset: 0, display: 'flex', alignItems: 'center', justifyContent: 'center', color: 'var(--rose-deep)', pointerEvents: 'none' }}><Icon name="plus" size={22} stroke={2.4} /></div>
                </div>
                <div style={{ flex: 1 }}>
                  <p style={{ margin: 0, fontSize: 13.5, fontWeight: 600 }}>{L(lang, 'Upload your logo', 'ارفع شعارك')}</p>
                  <p className="muted" style={{ margin: '3px 0 0', fontSize: 12, lineHeight: 1.4 }}>{L(lang, 'PNG or JPG · square works best', 'PNG أو JPG · يُفضّل مربّع')}</p>
                </div>
              </div>
            </div>
            <AdField label={L(lang, 'Business category', 'فئة النشاط')} hint={L(lang, 'Add one or more categories', 'أضف فئة أو أكثر')}>
              {/* chosen categories */}
              {b.cats.length > 0 &&
            <div style={{ display: 'flex', flexWrap: 'wrap', gap: 8, marginBottom: 10 }}>
                  {b.cats.map((id) => {const c = catOf(id) || CATEGORIES[0];return (
                  <span key={id} style={{ display: 'inline-flex', alignItems: 'center', gap: 6, padding: '7px 10px 7px 12px', borderRadius: 999, background: 'var(--rose-100)', color: 'var(--rose-deep)', fontSize: 13, fontWeight: 600 }}>
                      <Icon name={c.icon} size={14} />{L(lang, c.en, c.ar)}
                      <button onClick={() => removeCat(id)} style={{ background: 'none', border: 'none', cursor: 'pointer', color: 'var(--rose-deep)', display: 'flex', padding: 0 }}><Icon name="x" size={14} stroke={2.4} /></button>
                    </span>);
              })}
                </div>
            }
              <div style={{ display: 'flex', gap: 10 }}>
                <div style={{ position: 'relative', flex: 1 }}>
                  <select className="z-input" value={pick} onChange={(e) => setPick(e.target.value)} style={{ appearance: 'none', WebkitAppearance: 'none', cursor: 'pointer', paddingInlineEnd: 40 }}>
                    {CATEGORIES.map((c) => <option key={c.id} value={c.id}>{L(lang, c.en, c.ar)}</option>)}
                  </select>
                  <Icon name="down" size={18} style={{ position: 'absolute', insetInlineEnd: 14, top: 19, color: 'var(--rose-deep)', pointerEvents: 'none' }} />
                </div>
                <button onClick={addCat} title="add category" style={{ flexShrink: 0, width: 56, height: 56, borderRadius: 14, border: 'none', cursor: 'pointer', background: 'var(--rose)', color: 'var(--ink)', display: 'flex', alignItems: 'center', justifyContent: 'center', boxShadow: '0 4px 12px rgba(246,176,198,.5)' }}><Icon name="plus" size={22} stroke={2.6} /></button>
              </div>
            </AdField>
            <AdField label={L(lang, 'Trade license number', 'رقم الرخصة التجارية')}><input className="z-input" value={b.license} onChange={set('license')} placeholder="CN-1234567" style={{ direction: 'ltr', textAlign: lang === 'ar' ? 'right' : 'left' }} /></AdField>
            <AdField label={L(lang, 'Company description', 'وصف الشركة')}><textarea value={b.desc} onChange={set('desc')} rows={3} className="z-input" style={{ height: 'auto', padding: '13px 16px', resize: 'none', lineHeight: 1.5 }} placeholder={L(lang, 'What does your business do?', 'ماذا يقدّم نشاطك؟')} /></AdField>
            <AdField label={L(lang, 'Website / social media link', 'الموقع / رابط التواصل')} hint={L(lang, 'Optional', 'اختياري')}><input className="z-input" value={b.web} onChange={set('web')} placeholder="https://" style={{ direction: 'ltr', textAlign: lang === 'ar' ? 'right' : 'left' }} /></AdField>
            <AdField label={L(lang, 'Years in operation', 'سنوات العمل')}><input className="z-input" inputMode="numeric" value={b.years} onChange={(e) => setB((s) => ({ ...s, years: e.target.value.replace(/\D/g, '').slice(0, 2) }))} placeholder="6" style={{ direction: 'ltr', textAlign: lang === 'ar' ? 'right' : 'left' }} /></AdField>
            <AdField label={L(lang, 'Building name', 'اسم المبنى')}><input className="z-input" value={b.building} onChange={set('building')} placeholder={L(lang, 'e.g. Marina Plaza', 'مثال: مارينا بلازا')} /></AdField>
            <div style={{ display: 'flex', gap: 12 }}>
              <AdField label={L(lang, 'City', 'المدينة')}>
                <div style={{ position: 'relative' }}>
                  <select className="z-input" value={b.city} onChange={set('city')} style={{ appearance: 'none', WebkitAppearance: 'none', cursor: 'pointer', paddingInlineEnd: 40 }}>{CITIES.map((c) => <option key={c} value={c}>{c}</option>)}</select>
                  <Icon name="down" size={18} style={{ position: 'absolute', insetInlineEnd: 14, top: 19, color: 'var(--rose-deep)', pointerEvents: 'none' }} />
                </div>
              </AdField>
              <AdField label={L(lang, 'Post code', 'الرمز البريدي')}><input className="z-input" value={b.post} onChange={set('post')} placeholder="00000" style={{ direction: 'ltr', textAlign: lang === 'ar' ? 'right' : 'left' }} /></AdField>
            </div>
            <AdField label={L(lang, 'Region / State', 'المنطقة / الإمارة')}><input className="z-input" value={b.region} onChange={set('region')} placeholder={L(lang, 'e.g. Dubai', 'مثال: دبي')} /></AdField>
          </div> :

        <div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
            <p className="sec" style={{ margin: 0, fontSize: 13.5, lineHeight: 1.5 }}>{L(lang, 'Upload clear copies. These are reviewed before your profile goes live.', 'ارفع نسخاً واضحة. تتم مراجعتها قبل نشر ملفك.')}</p>
            <VUpload id="vdoc-license" label={L(lang, 'Upload trade license', 'ارفع الرخصة التجارية')} />
            <VUpload id="vdoc-logo" label={L(lang, 'Upload company logo', 'ارفع شعار الشركة')} />
            <VUpload id="vdoc-vat" label={L(lang, 'Upload VAT certificate', 'ارفع شهادة الضريبة')} />
            <VUpload id="vdoc-id" label={L(lang, 'ID proof of contact person', 'إثبات هوية مسؤول التواصل')} optional />
          </div>
        }
      </div>

      <div style={{ padding: '12px 20px 26px', borderTop: '1px solid var(--line)', background: '#fff', display: 'flex', gap: 12 }}>
        <button onClick={part === 0 ? onBack : () => setPart(0)} className="z-btn z-btn--outline" style={{ flex: '0 0 auto', width: 'auto', padding: '0 22px' }}>{L(lang, 'Back', 'رجوع')}</button>
        {part === 0 ?
        <Btn variant="rose" disabled={!p1valid} onClick={() => {setProfile((pr) => ({ ...pr, biz: b, name: b.company, cats: b.cats }));setPart(1);}}>{L(lang, 'Continue', 'متابعة')}</Btn> :
        <Btn variant="rose" onClick={onNext}>{L(lang, 'Continue', 'متابعة')}</Btn>}
      </div>
    </div>);

}

/* ── Step 4: Choose plan ── */
function VTiers({ tier, setTier, onNext, onBack }) {
  const { lang } = useZafa();
  return (
    <div style={{ height: '100%', display: 'flex', flexDirection: 'column', background: 'var(--bg)' }}>
      <div style={{ padding: '6px 20px 4px' }}><ScreenHeader title={L(lang, 'Choose your plan', 'اختر باقتك')} onBack={onBack} /></div>
      <div style={{ padding: '6px 20px 0' }}><VSteps i={3} /></div>
      <div className="z-scroll no-bar" style={{ flex: 1, padding: '16px 20px 20px' }}>
        <div style={{ display: 'flex', flexDirection: 'column', gap: 14 }}>
          {Object.entries(TIERS).map(([id, tr]) => {
            const on = tier === id;const gold = tr.tone === 'gold';
            return (
              <button key={id} onClick={() => setTier(id)} style={{ textAlign: 'start', cursor: 'pointer', width: '100%', borderRadius: 22, padding: 18, background: '#fff', border: on ? `2px solid ${gold ? '#C8A24A' : 'var(--ink)'}` : '1.5px solid var(--line)', position: 'relative', overflow: 'hidden' }}>
                {gold && <span className="z-badge z-badge--gold" style={{ position: 'absolute', top: 16, insetInlineEnd: 16 }}>{L(lang, 'Best value', 'الأفضل قيمة')}</span>}
                <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
                  <span style={{ width: 24, height: 24, borderRadius: '50%', flexShrink: 0, border: on ? `7px solid ${gold ? '#C8A24A' : 'var(--ink)'}` : '2px solid var(--line)', background: '#fff', transition: 'border-color .15s, border-width .15s' }} />
                  <h2 className="serif" style={{ margin: 0, fontSize: 22, fontWeight: 600 }}>{L(lang, tr.en, tr.ar)}</h2>
                </div>
                <p className="sec" style={{ margin: '8px 0 0', fontSize: 13.5 }}>{L(lang, tr.tagEn, tr.tagAr)}</p>
                <div style={{ display: 'flex', alignItems: 'baseline', gap: 6, margin: '12px 0 14px' }}>
                  <span className="serif" style={{ fontSize: 30, fontWeight: 700 }}>AED {tr.price}</span><span className="muted" style={{ fontSize: 13.5 }}>/ {L(lang, 'month', 'شهر')}</span>
                </div>
                <div style={{ display: 'flex', flexDirection: 'column', gap: 9 }}>
                  {tr.perks.map(([pe, pa], i) =>
                  <div key={i} style={{ display: 'flex', gap: 9, alignItems: 'flex-start' }}>
                      <Icon name="check" size={16} stroke={2.4} style={{ color: gold ? '#C8A24A' : 'var(--rose-deep)', flexShrink: 0, marginTop: 2 }} />
                      <span style={{ fontSize: 13.5, fontWeight: 500 }}>{L(lang, pe, pa)}</span>
                    </div>
                  )}
                </div>
              </button>);

          })}
        </div>
        <p className="muted" style={{ fontSize: 12, textAlign: 'center', marginTop: 16 }}>{L(lang, 'Placeholder pricing · billed monthly · cancel anytime', 'أسعار توضيحية · فوترة شهرية · يمكن الإلغاء')}</p>
      </div>
      <div style={{ padding: '12px 20px 26px', borderTop: '1px solid var(--line)', background: '#fff' }}>
        <Btn variant="rose" iconR="arrow" onClick={onNext}>{L(lang, 'Continue with', 'المتابعة بـ')} {L(lang, TIERS[tier].en, TIERS[tier].ar)}</Btn>
      </div>
    </div>);

}

/* ── Subscription checkout (+ success → add your service) ── */
function VSubPay({ tier, onActivated, onBack, changeMode }) {
  const { lang, t } = useZafa();
  const tr = TIERS[tier];
  const [card, setCard] = React.useState('');const [exp, setExp] = React.useState('');const [cvv, setCvv] = React.useState('');const [nm, setNm] = React.useState('');
  const [outcome, setOutcome] = React.useState('success');
  const [state, setState] = React.useState('form');
  const fmtCard = (s) => s.replace(/\D/g, '').slice(0, 16).replace(/(.{4})/g, '$1 ').trim();
  const fmtExp = (s) => {const d = s.replace(/\D/g, '').slice(0, 4);return d.length > 2 ? d.slice(0, 2) + '/' + d.slice(2) : d;};
  const valid = card.replace(/\s/g, '').length >= 16 && exp.length === 5 && cvv.length >= 3 && nm.length > 1;
  const pay = () => {setState('processing');setTimeout(() => setState(outcome), 1700);};

  if (state === 'success' || state === 'failed') {
    const ok = state === 'success';
    return (
      <div style={{ height: '100%', display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', padding: '30px 28px 40px', background: 'var(--bg)', textAlign: 'center' }}>
        <div style={{ width: 104, height: 104, borderRadius: '50%', display: 'flex', alignItems: 'center', justifyContent: 'center', background: ok ? 'var(--rose-100)' : '#FBEAE7', color: ok ? '#2E9E6B' : '#C0392B', animation: 'pop .5s' }}>
          <Icon name={ok ? 'check' : 'x'} size={52} stroke={2.6} />
        </div>
        <h1 className="h1" style={{ margin: '24px 0 10px' }}>{ok ? changeMode ? L(lang, 'Plan updated!', 'تم تحديث الباقة!') : L(lang, 'You\u2019re a vendor!', 'أصبحت مزوّداً!') : t('payFailed')}</h1>
        <p className="sec" style={{ margin: 0, fontSize: 15, lineHeight: 1.55, maxWidth: 300 }}>{ok ? changeMode ? L(lang, `Your ${L(lang, tr.en, tr.ar)} plan is now active.`, `باقة ${L(lang, tr.en, tr.ar)} مفعّلة الآن.`) : L(lang, `Your ${L(lang, tr.en, tr.ar)} plan is active. Let\u2019s set up your profile.`, `باقة ${L(lang, tr.en, tr.ar)} مفعّلة. لنُعِدّ ملفك.`) : t('payFailedSub')}</p>
        <div style={{ marginTop: 28, width: '100%', maxWidth: 300 }}>
          {ok ? <Btn variant="rose" iconR={changeMode ? null : 'arrow'} onClick={onActivated}>{changeMode ? L(lang, 'Done', 'تم') : L(lang, 'Add your service', 'أضف خدمتك')}</Btn> :
          <Btn variant="rose" onClick={() => setState('form')}>{t('tryAgain')}</Btn>}
        </div>
      </div>);

  }

  return (
    <div style={{ height: '100%', display: 'flex', flexDirection: 'column', background: 'var(--bg)' }}>
      <div style={{ padding: '6px 20px 4px' }}><ScreenHeader title={L(lang, 'Checkout', 'الدفع')} onBack={onBack} /></div>
      <div style={{ padding: '6px 20px 0' }}><VSteps i={3} /></div>
      <div className="z-scroll no-bar" style={{ flex: 1, padding: '16px 20px 20px' }}>
        <div className="z-card" style={{ padding: 16, marginBottom: 20, display: 'flex', alignItems: 'center', gap: 12 }}>
          <span className={`z-badge z-badge--${tr.tone === 'gold' ? 'gold' : 'rose'}`} style={{ fontSize: 12, padding: '6px 11px' }}>{L(lang, tr.en, tr.ar)}</span>
          <div style={{ flex: 1 }}>
            <p style={{ margin: 0, fontWeight: 600, fontSize: 14.5 }}>{L(lang, 'Monthly subscription', 'اشتراك شهري')}</p>
            <p className="muted" style={{ margin: '1px 0 0', fontSize: 12 }}>{L(lang, 'Renews automatically', 'يتجدّد تلقائياً')}</p>
          </div>
          <span className="serif" style={{ fontSize: 20, fontWeight: 700 }}>AED {tr.price}</span>
        </div>
        <div style={{ display: 'flex', flexDirection: 'column', gap: 13 }}>
          <Field label={t('cardNumber')}><input className="z-input" inputMode="numeric" value={card} onChange={(e) => setCard(fmtCard(e.target.value))} placeholder="1234 5678 9012 3456" style={{ direction: 'ltr' }} /></Field>
          <div style={{ display: 'flex', gap: 12 }}>
            <Field label={t('expiry')} flex><input className="z-input" inputMode="numeric" value={exp} onChange={(e) => setExp(fmtExp(e.target.value))} placeholder="MM/YY" style={{ direction: 'ltr' }} /></Field>
            <Field label={t('cvv')} flex><input className="z-input" inputMode="numeric" value={cvv} onChange={(e) => setCvv(e.target.value.replace(/\D/g, '').slice(0, 4))} placeholder="•••" style={{ direction: 'ltr' }} /></Field>
          </div>
          <Field label={t('nameOnCard')}><input className="z-input" value={nm} onChange={(e) => setNm(e.target.value)} placeholder={L(lang, 'Full name', 'الاسم الكامل')} /></Field>
        </div>
        <div style={{ display: 'flex', alignItems: 'center', gap: 12, margin: '20px 0 12px' }}>
          <div className="z-hr" style={{ flex: 1 }} /><span className="muted" style={{ fontSize: 12.5 }}>{t('orPayWith')}</span><div className="z-hr" style={{ flex: 1 }} />
        </div>
        <div style={{ display: 'flex', gap: 10 }}>{[' Pay', 'mada', 'Tabby'].map((g) => <button key={g} style={{ flex: 1, height: 50, borderRadius: 12, border: '1.5px solid var(--line)', background: '#fff', cursor: 'pointer', fontWeight: 700, fontSize: 14, color: 'var(--ink)' }}>{g}</button>)}</div>
        <div style={{ marginTop: 18, padding: 12, borderRadius: 14, background: 'var(--rose-soft)', border: '1px dashed var(--rose-200)' }}>
          <p style={{ margin: '0 0 8px', fontSize: 12, fontWeight: 700, color: 'var(--rose-deep)', letterSpacing: '.04em' }}>{L(lang, 'DEMO · simulate outcome', 'تجربة · محاكاة النتيجة')}</p>
          <div style={{ display: 'flex', gap: 8 }}><Chip on={outcome === 'success'} onClick={() => setOutcome('success')}>{L(lang, 'Success', 'نجاح')}</Chip><Chip on={outcome === 'failed'} onClick={() => setOutcome('failed')}>{L(lang, 'Decline', 'رفض')}</Chip></div>
        </div>
      </div>
      <div style={{ padding: '12px 20px 26px', borderTop: '1px solid var(--line)', background: '#fff' }}>
        <Btn variant="rose" disabled={!valid || state === 'processing'} onClick={pay}>{state === 'processing' ? <span className="z-spin" /> : <><Icon name="shield" size={18} />{t('payNow')} AED {tr.price}/{L(lang, 'mo', 'شهر')}</>}</Btn>
      </div>
    </div>);

}

Object.assign(window, { VSteps, VTabSteps, VBecome, VVerify, VCreateAccount, VBusiness, VUpload, VTiers, VSubPay });