// OTP Verification — phone input with country dropdown, email field, OTP modal
const { useState: useStateO, useRef: useRefO, useEffect: useEffectO, useMemo: useMemoO } = React;

// ===== Flag SVGs (rendered inside a circle) =====
const FlagSA = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="24" height="24" fill="#0d3f44" />
    <path d="M5 13.5h14M6 11.2c.7-.4 1.4-.4 2 0M16 11.2c.7-.4 1.4-.4 2 0" stroke="#fff" strokeWidth="0.6" fill="none" strokeLinecap="round" />
  </svg>;

const FlagUS = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="24" height="24" fill="#fff" />
    {[0, 2, 4, 6, 8, 10, 12].map((y) => <rect key={y} y={y + 0.8} width="24" height="1.6" fill="#b22234" />)}
    <rect width="11" height="9" fill="#3c3b6e" />
    {[...Array(12)].map((_, i) => <circle key={i} cx={1.5 + i % 4 * 2.6} cy={1.5 + Math.floor(i / 4) * 2.6} r="0.45" fill="#fff" />)}
  </svg>;

const FlagUZ = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="24" height="8" fill="#1eb53a" />
    <rect y="8" width="24" height="8" fill="#fff" />
    <rect y="16" width="24" height="8" fill="#0099b5" />
    <rect y="7.6" width="24" height="0.6" fill="#ce1126" />
    <rect y="15.8" width="24" height="0.6" fill="#ce1126" />
    <circle cx="6" cy="5" r="2.4" fill="#fff" />
    <circle cx="6.9" cy="5" r="2.0" fill="#0099b5" />
  </svg>;

const FlagAF = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="8" height="24" fill="#000" />
    <rect x="8" width="8" height="24" fill="#d32011" />
    <rect x="16" width="8" height="24" fill="#007a36" />
  </svg>;

const FlagAL = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="24" height="24" fill="#e41e20" />
    <text x="12" y="16" textAnchor="middle" fontSize="12" fill="#000" fontWeight="bold">𓅃</text>
  </svg>;

const FlagDZ = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="12" height="24" fill="#006633" />
    <rect x="12" width="12" height="24" fill="#fff" />
    <circle cx="12" cy="12" r="3.5" fill="#d21034" />
    <circle cx="12.8" cy="12" r="2.8" fill="#fff" />
    <path d="M14.5 12 l1.6 -.5 -1 1.4 0 -1.8 1 1.4z" fill="#d21034" />
  </svg>;

const FlagAD = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="8" height="24" fill="#10069f" />
    <rect x="8" width="8" height="24" fill="#fcd116" />
    <rect x="16" width="8" height="24" fill="#d52b1e" />
  </svg>;

const FlagAE = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="8" height="24" fill="#ce1126" />
    <rect x="8" width="16" height="8" fill="#00732f" />
    <rect x="8" y="8" width="16" height="8" fill="#fff" />
    <rect x="8" y="16" width="16" height="8" fill="#000" />
  </svg>;

const FlagEG = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="24" height="8" fill="#ce1126" />
    <rect y="8" width="24" height="8" fill="#fff" />
    <rect y="16" width="24" height="8" fill="#000" />
    <circle cx="12" cy="12" r="1.8" fill="#c09300" />
  </svg>;

const FlagJO = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="24" height="8" fill="#000" />
    <rect y="8" width="24" height="8" fill="#fff" />
    <rect y="16" width="24" height="8" fill="#007a3d" />
    <path d="M0 0 L9 12 L0 24 Z" fill="#ce1126" />
  </svg>;

const FlagKW = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="24" height="8" fill="#007a3d" />
    <rect y="8" width="24" height="8" fill="#fff" />
    <rect y="16" width="24" height="8" fill="#ce1126" />
    <path d="M0 0 L8 8 L8 16 L0 24 Z" fill="#000" />
  </svg>;

const FlagBH = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="24" height="24" fill="#ce1126" />
    <path d="M0 0 H7 L9 3 L7 6 L9 9 L7 12 L9 15 L7 18 L9 21 L7 24 H0 Z" fill="#fff" />
  </svg>;

const FlagQA = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="24" height="24" fill="#8d1b3d" />
    <path d="M0 0 H8 L10 2 L8 4 L10 6 L8 8 L10 10 L8 12 L10 14 L8 16 L10 18 L8 20 L10 22 L8 24 H0 Z" fill="#fff" />
  </svg>;

const FlagOM = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="24" height="24" fill="#fff" />
    <rect width="24" height="8" fill="#fff" />
    <rect y="8" width="24" height="8" fill="#fff" />
    <rect y="16" width="24" height="8" fill="#009b3a" />
    <rect y="0" width="24" height="8" fill="#db161b" />
    <rect y="8" width="24" height="8" fill="#fff" />
    <rect width="9" height="24" fill="#db161b" />
  </svg>;

const FlagYE = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="24" height="8" fill="#ce1126" />
    <rect y="8" width="24" height="8" fill="#fff" />
    <rect y="16" width="24" height="8" fill="#000" />
  </svg>;

const FlagIQ = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="24" height="8" fill="#ce1126" />
    <rect y="8" width="24" height="8" fill="#fff" />
    <rect y="16" width="24" height="8" fill="#000" />
    <text x="12" y="13" textAnchor="middle" fontSize="3.5" fill="#007a3d" fontWeight="bold">الله أكبر</text>
  </svg>;

const FlagSY = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="24" height="8" fill="#ce1126" />
    <rect y="8" width="24" height="8" fill="#fff" />
    <rect y="16" width="24" height="8" fill="#000" />
  </svg>;

const FlagLB = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="24" height="6" fill="#ed1c24" />
    <rect y="6" width="24" height="12" fill="#fff" />
    <rect y="18" width="24" height="6" fill="#ed1c24" />
    <path d="M12 9 L13.5 13 L17 13 L14 15 L15 18 L12 16 L9 18 L10 15 L7 13 L10.5 13 Z" fill="#00a651" />
  </svg>;

const FlagPS = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="24" height="8" fill="#000" />
    <rect y="8" width="24" height="8" fill="#fff" />
    <rect y="16" width="24" height="8" fill="#007a3d" />
    <path d="M0 0 L9 12 L0 24 Z" fill="#ce1126" />
  </svg>;

const FlagGB = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="24" height="24" fill="#012169" />
    <path d="M0 0 L24 24 M24 0 L0 24" stroke="#fff" strokeWidth="3" />
    <path d="M0 0 L24 24 M24 0 L0 24" stroke="#c8102e" strokeWidth="1.5" />
    <path d="M12 0 V24 M0 12 H24" stroke="#fff" strokeWidth="4" />
    <path d="M12 0 V24 M0 12 H24" stroke="#c8102e" strokeWidth="2.4" />
  </svg>;

const FlagFR = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="8" height="24" fill="#0055a4" />
    <rect x="8" width="8" height="24" fill="#fff" />
    <rect x="16" width="8" height="24" fill="#ef4135" />
  </svg>;

const FlagDE = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="24" height="8" fill="#000" />
    <rect y="8" width="24" height="8" fill="#dd0000" />
    <rect y="16" width="24" height="8" fill="#ffce00" />
  </svg>;

const FlagIN = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="24" height="8" fill="#ff9933" />
    <rect y="8" width="24" height="8" fill="#fff" />
    <rect y="16" width="24" height="8" fill="#138808" />
    <circle cx="12" cy="12" r="2" fill="none" stroke="#000080" strokeWidth="0.4" />
  </svg>;

const FlagPK = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="24" height="24" fill="#01411c" />
    <rect width="6" height="24" fill="#fff" />
    <circle cx="14" cy="12" r="3.5" fill="#01411c" />
    <circle cx="15.2" cy="11" r="3.2" fill="#fff" />
  </svg>;

const FlagTR = () =>
<svg viewBox="0 0 24 24" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
    <rect width="24" height="24" fill="#e30a17" />
    <circle cx="9" cy="12" r="3.5" fill="#fff" />
    <circle cx="10" cy="12" r="2.8" fill="#e30a17" />
    <path d="M13 12 l2-.6 -1.2 1.6 0 -2 1.2 1.6z" fill="#fff" />
  </svg>;


const COUNTRIES = [
{ code: 'US', name: 'United States', dial: '+1', flag: FlagUS },
{ code: 'UZ', name: 'Uzbekistan', dial: '+998', flag: FlagUZ },
{ code: 'AF', name: 'Afganistan', dial: '+93', flag: FlagAF },
{ code: 'AL', name: 'Albania', dial: '+355', flag: FlagAL },
{ code: 'DZ', name: 'Algeria', dial: '+213', flag: FlagDZ },
{ code: 'AD', name: 'Andorra', dial: '+376', flag: FlagAD },
{ code: 'SA', name: 'Saudi Arabia', dial: '+966', flag: FlagSA },
{ code: 'AE', name: 'United Arab Emirates', dial: '+971', flag: FlagAE },
{ code: 'EG', name: 'Egypt', dial: '+20', flag: FlagEG },
{ code: 'JO', name: 'Jordan', dial: '+962', flag: FlagJO },
{ code: 'KW', name: 'Kuwait', dial: '+965', flag: FlagKW },
{ code: 'BH', name: 'Bahrain', dial: '+973', flag: FlagBH },
{ code: 'QA', name: 'Qatar', dial: '+974', flag: FlagQA },
{ code: 'OM', name: 'Oman', dial: '+968', flag: FlagOM },
{ code: 'YE', name: 'Yemen', dial: '+967', flag: FlagYE },
{ code: 'IQ', name: 'Iraq', dial: '+964', flag: FlagIQ },
{ code: 'SY', name: 'Syria', dial: '+963', flag: FlagSY },
{ code: 'LB', name: 'Lebanon', dial: '+961', flag: FlagLB },
{ code: 'PS', name: 'Palestine', dial: '+970', flag: FlagPS },
{ code: 'GB', name: 'United Kingdom', dial: '+44', flag: FlagGB },
{ code: 'FR', name: 'France', dial: '+33', flag: FlagFR },
{ code: 'DE', name: 'Germany', dial: '+49', flag: FlagDE },
{ code: 'IN', name: 'India', dial: '+91', flag: FlagIN },
{ code: 'PK', name: 'Pakistan', dial: '+92', flag: FlagPK },
{ code: 'TR', name: 'Türkiye', dial: '+90', flag: FlagTR }];


// Verify icon (refresh-style)
const IcVerify = ({ size = 18 }) =>
<svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
    <path d="M21 12a9 9 0 0 1-15.5 6.3L3 16" />
    <path d="M3 12a9 9 0 0 1 15.5-6.3L21 8" />
    <path d="M21 3v5h-5" />
    <path d="M3 21v-5h5" />
  </svg>;

const IcInfo = ({ size = 16 }) =>
<svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
    <circle cx="12" cy="12" r="10" />
    <line x1="12" y1="16" x2="12" y2="12" />
    <line x1="12" y1="8" x2="12.01" y2="8" />
  </svg>;

const IcChevronUpDown = () =>
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
    <polyline points="7 15 12 20 17 15" />
    <polyline points="7 9 12 4 17 9" />
  </svg>;

const IcSearch = ({ size = 18 }) =>
<svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
    <circle cx="11" cy="11" r="7" />
    <line x1="21" y1="21" x2="16.65" y2="16.65" />
  </svg>;

const IcCheckCircle = ({ size = 18 }) =>
<svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
    <circle cx="12" cy="12" r="10" />
    <polyline points="8 12.5 11 15.5 16.5 9.5" />
  </svg>;

const IcResend = ({ size = 16 }) =>
<svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round">
    <path d="M3 12a9 9 0 1 0 3-6.7L3 8" />
    <polyline points="3 3 3 8 8 8" />
  </svg>;


// ===== Phone Input with Country Dropdown =====
const PhoneInput = ({ data, onChange, onVerify, verified, showVerify }) => {
  const [open, setOpen] = useStateO(false);
  const [query, setQuery] = useStateO('');
  const wrapRef = useRefO(null);
  const country = COUNTRIES.find((c) => c.code === (data.phoneCountry || 'SA')) || COUNTRIES[6];

  useEffectO(() => {
    if (!open) return;
    const onDoc = (e) => {if (wrapRef.current && !wrapRef.current.contains(e.target)) setOpen(false);};
    document.addEventListener('mousedown', onDoc);
    return () => document.removeEventListener('mousedown', onDoc);
  }, [open]);

  const filtered = useMemoO(() => {
    const q = query.trim().toLowerCase();
    if (!q) return COUNTRIES;
    return COUNTRIES.filter((c) => c.name.toLowerCase().includes(q) || c.dial.includes(q));
  }, [query]);

  const select = (c) => {onChange('phoneCountry', c.code);setOpen(false);setQuery('');};
  const Flag = country.flag;

  return (
    <div className="otp-phone-wrap" ref={wrapRef}>
      <div className={`otp-phone-input ${verified ? 'is-verified' : ''}`}>
        <button type="button" className="otp-cc-btn" onClick={() => setOpen((o) => !o)}>
          <span className="otp-flag"><Flag /></span>
          <span className="otp-cc-chev"><IcChevronUpDown /></span>
        </button>
        <span className="otp-cc-divider" />
        <span className="otp-cc-dial">{country.dial}</span>
        <input
          className="otp-phone-num"
          type="tel"
          value={data.phone || ''}
          onChange={(e) => onChange('phone', e.target.value)}
          placeholder="5XX XXX XXXX" />

        {showVerify && (verified ?
        <span className="otp-verified-tag">
            <IcCheckCircle size={16} /> Verified
          </span> :

        <button type="button" className="otp-verify-btn" onClick={onVerify}>
            <IcVerify size={16} /> Verify
          </button>
        )}
      </div>

      {showVerify && !verified &&
      <div className="otp-field-error">
          <IcInfo size={15} />
          <span>Verification required before saving</span>
        </div>
      }

      {open &&
      <div className="otp-cc-menu">
          <div className="otp-cc-search">
            <IcSearch />
            <input
            type="text"
            autoFocus
            value={query}
            onChange={(e) => setQuery(e.target.value)}
            placeholder="Search for countries" />
          
          </div>
          <div className="otp-cc-list">
            {filtered.slice(0, 2).map((c) => {
            const F = c.flag;
            return (
              <button key={c.code} type="button" className={`otp-cc-item ${c.code === country.code ? 'is-active' : ''}`} onClick={() => select(c)}>
                  <span className="otp-flag otp-flag-md"><F /></span>
                  <span className="otp-cc-name">{c.name}</span>
                  <span className="otp-cc-dialpill">{c.dial}</span>
                </button>);

          })}
            {filtered.length > 2 && <div className="otp-cc-sep" />}
            {filtered.slice(2).map((c) => {
            const F = c.flag;
            return (
              <button key={c.code} type="button" className={`otp-cc-item ${c.code === country.code ? 'is-active' : ''}`} onClick={() => select(c)}>
                  <span className="otp-flag otp-flag-md"><F /></span>
                  <span className="otp-cc-name">{c.name}</span>
                  <span className="otp-cc-dialpill">{c.dial}</span>
                </button>);

          })}
            {filtered.length === 0 && <div className="otp-cc-empty">No countries match</div>}
          </div>
        </div>
      }
    </div>);

};
// Expose for reuse by other modules (e.g. Voice Service phone-number entry).
window.PhoneInput = PhoneInput;
window.COUNTRIES = COUNTRIES;

// ===== Email Input =====
const EmailInput = ({ value, onChange, onVerify, verified, error, showVerify }) => {
  return (
    <div className="otp-email-wrap">
      <div className={`otp-email-input ${error ? 'is-error' : ''} ${verified ? 'is-verified' : ''}`}>
        <input
          type="email"
          value={value || ''}
          onChange={(e) => onChange(e.target.value)}
          placeholder="name@company.sa" />

        {showVerify && (verified ?
        <span className="otp-verified-tag">
            <IcCheckCircle size={16} /> Verified
          </span> :

        <button type="button" className="otp-verify-btn" onClick={onVerify}>
            <IcVerify size={16} /> Verify
          </button>
        )}
      </div>
      {showVerify && !verified &&
      <div className="otp-field-error">
          <IcInfo size={15} />
          <span>Verification required before saving</span>
        </div>
      }
    </div>);

};

// ===== OTP Modal =====
const OTP_LEN = 6;
const TIMER_SECONDS = 60;

const OtpModal = ({ target, channel, onClose, onSuccess }) => {
  const [code, setCode] = useStateO(Array(OTP_LEN).fill(''));
  const [seconds, setSeconds] = useStateO(TIMER_SECONDS);
  const [status, setStatus] = useStateO('input'); // 'input' | 'invalid' | 'expired' | 'success'
  const refs = useRefO([]);

  useEffectO(() => {
    if (status === 'success') return;
    if (seconds <= 0) {
      if (status !== 'expired') setStatus('expired');
      return;
    }
    const t = setTimeout(() => setSeconds((s) => s - 1), 1000);
    return () => clearTimeout(t);
  }, [seconds, status]);

  useEffectO(() => {
    refs.current[0]?.focus();
  }, []);

  const handleChange = (i, v) => {
    if (status === 'expired') return;
    const ch = v.replace(/[^0-9]/g, '').slice(-1);
    const next = [...code];
    next[i] = ch;
    setCode(next);
    if (status === 'invalid') setStatus('input');
    if (ch && i < OTP_LEN - 1) refs.current[i + 1]?.focus();

    if (next.every((c) => c !== '')) {
      // Pretend any code that ends with "0" is invalid; otherwise success after a beat
      const joined = next.join('');
      setTimeout(() => {
        if (joined === '150999') {
          setStatus('invalid');
        } else {
          setStatus('success');
          setTimeout(() => onSuccess(), 1300);
        }
      }, 200);
    }
  };

  const handleKeyDown = (i, e) => {
    if (e.key === 'Backspace' && !code[i] && i > 0) {
      refs.current[i - 1]?.focus();
    } else if (e.key === 'ArrowLeft' && i > 0) {
      refs.current[i - 1]?.focus();
    } else if (e.key === 'ArrowRight' && i < OTP_LEN - 1) {
      refs.current[i + 1]?.focus();
    }
  };

  const handlePaste = (e) => {
    const t = (e.clipboardData.getData('text') || '').replace(/[^0-9]/g, '').slice(0, OTP_LEN);
    if (!t) return;
    e.preventDefault();
    const next = Array(OTP_LEN).fill('');
    for (let i = 0; i < t.length; i++) next[i] = t[i];
    setCode(next);
    refs.current[Math.min(t.length, OTP_LEN - 1)]?.focus();
    if (next.every((c) => c !== '')) {
      setTimeout(() => {
        if (next.join('') === '150999') setStatus('invalid');else
        {setStatus('success');setTimeout(() => onSuccess(), 1300);}
      }, 200);
    }
  };

  const resend = () => {
    setSeconds(TIMER_SECONDS);
    setCode(Array(OTP_LEN).fill(''));
    setStatus('input');
    setTimeout(() => refs.current[0]?.focus(), 50);
  };

  // Circular progress
  const R = 36;
  const C = 2 * Math.PI * R;
  const progress = Math.max(0, Math.min(1, seconds / TIMER_SECONDS));

  return (
    <div className="ac-modal-overlay" onClick={onClose}>
      <div className="ac-modal otp-modal" onClick={(e) => e.stopPropagation()}>
        <button className="ac-modal-close" type="button" onClick={onClose} aria-label="Close">
          <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round"><path d="M6 6L18 18M18 6L6 18" /></svg>
        </button>

        <h3 className="otp-title">OTP Verification</h3>

        {status === 'success' ?
        <div className="otp-success">
            <div className="otp-success-icon">
              <svg width="34" height="34" viewBox="0 0 24 24" fill="none" stroke="#0d3f44" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round">
                <polyline points="6 12.5 10 16.5 18 8" />
              </svg>
            </div>
            <div className="otp-success-title">Successfully</div>
            <div className="otp-success-sub">OTP verified successfully</div>
          </div> :

        <>
            {status === 'invalid' &&
          <div className="otp-invalid-banner">
                <IcInfo size={16} />
                <span>Invalid OTP</span>
              </div>
          }

            <div className="otp-sent-line">
              The verification code has been sent to your {channel === 'phone' ? 'Phone number' : 'Email address'}
            </div>
            <div className="otp-sent-target">{target}</div>

            <div className={`otp-boxes ${status === 'invalid' ? 'is-invalid' : ''}`}>
              {code.slice(0, 3).map((c, i) =>
            <input
              key={i}
              ref={(el) => refs.current[i] = el}
              className={`otp-box ${c ? 'has-value' : ''} ${status === 'invalid' ? 'is-invalid' : ''}`}
              inputMode="numeric"
              maxLength={1}
              value={c}
              onChange={(e) => handleChange(i, e.target.value)}
              onKeyDown={(e) => handleKeyDown(i, e)}
              onPaste={handlePaste}
              disabled={status === 'expired'} />

            )}
              <span className="otp-sep" />
              {code.slice(3).map((c, idx) => {
              const i = idx + 3;
              return (
                <input
                  key={i}
                  ref={(el) => refs.current[i] = el}
                  className={`otp-box ${c ? 'has-value' : ''} ${status === 'invalid' ? 'is-invalid' : ''}`}
                  inputMode="numeric"
                  maxLength={1}
                  value={c}
                  onChange={(e) => handleChange(i, e.target.value)}
                  onKeyDown={(e) => handleKeyDown(i, e)}
                  onPaste={handlePaste}
                  disabled={status === 'expired'} />);


            })}
            </div>

            <div className="otp-timer">
              <svg width="96" height="96" viewBox="0 0 96 96">
                <circle cx="48" cy="48" r={R} fill="none" stroke="#e5e7eb" strokeWidth="4" />
                <circle
                cx="48" cy="48" r={R}
                fill="none"
                stroke="#0d3f44"
                strokeWidth="4"
                strokeLinecap="round"
                strokeDasharray={C}
                strokeDashoffset={C * (1 - progress)}
                transform="rotate(-90 48 48)"
                style={{ transition: 'stroke-dashoffset 0.95s linear' }} />

                <text x="48" y="55" textAnchor="middle" fontSize="22" fontWeight="700" fill="#0a1f24">{seconds}</text>
              </svg>
              {status === 'expired' && <div className="otp-expired-msg">Code expired — please resend</div>}
            </div>

            <button
            type="button"
            className={`otp-resend ${seconds > 0 && status !== 'expired' ? 'is-disabled' : ''}`}
            onClick={resend}
            disabled={seconds > 0 && status !== 'expired'}>
            
              <IcResend /> Resend
            </button>
          </>
        }
      </div>
    </div>);

};

Object.assign(window, {
  PhoneInput, EmailInput, OtpModal, COUNTRIES
});