// WhatsApp primitives + hand-drawn SVG illustrations for "How it works".
const { useEffect, useRef, useState } = React;

const Icon = {
  Phone: (p) => <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M22 16.92v3a2 2 0 0 1-2.18 2A19.79 19.79 0 0 1 2.08 4.18 2 2 0 0 1 4.07 2h3a2 2 0 0 1 2 1.72c.13.96.37 1.9.72 2.81a2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45c.91.35 1.85.59 2.81.72A2 2 0 0 1 22 16.92z"/></svg>,
  Video: (p) => <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><polygon points="23 7 16 12 23 17 23 7"/><rect x="1" y="5" width="15" height="14" rx="2" ry="2"/></svg>,
  More: (p) => <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><circle cx="12" cy="5" r="1"/><circle cx="12" cy="12" r="1"/><circle cx="12" cy="19" r="1"/></svg>,
  Back: (p) => <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><polyline points="15 18 9 12 15 6"/></svg>,
  Emoji: (p) => <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" {...p}><circle cx="12" cy="12" r="9"/><path d="M8 14s1.5 2 4 2 4-2 4-2" strokeLinecap="round"/><line x1="9" y1="9" x2="9.01" y2="9" strokeLinecap="round"/><line x1="15" y1="9" x2="15.01" y2="9" strokeLinecap="round"/></svg>,
  Paperclip: (p) => <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M21.44 11.05l-9.19 9.19a6 6 0 0 1-8.49-8.49l9.19-9.19a4 4 0 0 1 5.66 5.66l-9.2 9.19a2 2 0 0 1-2.83-2.83l8.49-8.48"/></svg>,
  Camera: (p) => <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M23 19a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4l2-3h6l2 3h4a2 2 0 0 1 2 2z"/><circle cx="12" cy="13" r="4"/></svg>,
  Mic: (p) => <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><rect x="9" y="3" width="6" height="11" rx="3"/><path d="M5 11a7 7 0 0 0 14 0"/><line x1="12" y1="18" x2="12" y2="22"/></svg>,
  Play: (p) => <svg width="10" height="10" viewBox="0 0 10 10" fill="currentColor" {...p}><path d="M1 1 L9 5 L1 9 Z"/></svg>,
  Pause: (p) => <svg width="10" height="10" viewBox="0 0 10 10" fill="currentColor" {...p}><rect x="1" y="1" width="3" height="8"/><rect x="6" y="1" width="3" height="8"/></svg>,
  Arrow: (p) => <svg width="14" height="14" viewBox="0 0 14 14" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" {...p}><path d="M3 7 H11 M7 3 L11 7 L7 11"/></svg>,
  Check: (p) => <svg width="14" height="14" viewBox="0 0 14 14" fill="none" {...p}><path d="M2 7 L6 11 L12 3" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  Reply: (p) => <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><polyline points="9 17 4 12 9 7"/><path d="M20 18v-2a4 4 0 0 0-4-4H4"/></svg>,
  Target: (p) => <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><circle cx="12" cy="12" r="9"/><circle cx="12" cy="12" r="5"/><circle cx="12" cy="12" r="1" fill="currentColor"/></svg>,
  Calendar: (p) => <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><rect x="3" y="5" width="18" height="16" rx="2"/><line x1="3" y1="10" x2="21" y2="10"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="16" y1="2" x2="16" y2="6"/></svg>,
  Paper: (p) => <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/></svg>,
  Loop: (p) => <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><polyline points="17 1 21 5 17 9"/><path d="M3 11V9a4 4 0 0 1 4-4h14"/><polyline points="7 23 3 19 7 15"/><path d="M21 13v2a4 4 0 0 1-4 4H3"/></svg>,
  Waveform: (p) => <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" {...p}><line x1="3" y1="12" x2="3" y2="12"/><line x1="7" y1="8" x2="7" y2="16"/><line x1="11" y1="4" x2="11" y2="20"/><line x1="15" y1="7" x2="15" y2="17"/><line x1="19" y1="10" x2="19" y2="14"/></svg>,
  WAGlyph: (p) => <svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor" {...p}><path d="M20.5 3.5A11.2 11.2 0 0 0 12.5 0C6 0 0.7 5.3 0.7 11.8a11.7 11.7 0 0 0 1.6 5.9L0 24l6.5-1.7a11.8 11.8 0 0 0 5.9 1.5h.1c6.5 0 11.8-5.3 11.8-11.8a11.7 11.7 0 0 0-3.4-8.5Zm-8 18.1a9.6 9.6 0 0 1-5-1.4l-.4-.2-3.8 1 1-3.7-.2-.4a9.8 9.8 0 1 1 18.1-5.1c0 5.4-4.4 9.8-9.7 9.8Z"/></svg>,
};

function Tick({ read }) {
  return (
    <span className={`wa-tick ${read ? "read" : ""}`}>
      <svg viewBox="0 0 20 14" fill="none"><path d="M1 8 L5 12 L12 3" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"/><path d="M7 8 L11 12 L19 3" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"/></svg>
    </span>
  );
}

function TextBubble({ dir, children, time, read, delay = 0 }) {
  return (
    <div className={`wa-bubble ${dir}`} style={{ animationDelay: `${delay}s` }}>
      <span className="txt">{children}</span>
      <span className="meta">{time}{dir === "out" && <Tick read={read} />}</span>
    </div>
  );
}

function VoiceBubble({ dir, dur = "0:12", time, read, playing, delay = 0 }) {
  const bars = 26;
  return (
    <div className={`wa-bubble ${dir} wa-voice ${playing ? "playing" : ""}`} style={{ animationDelay: `${delay}s` }}>
      <div className="va"><Icon.Mic width="14" height="14" /></div>
      <div className="vplay"><Icon.Play /></div>
      <div className="wa-bars">
        {Array.from({ length: bars }).map((_, i) => {
          const h = 4 + Math.abs(Math.sin(i * 0.85)) * 16;
          return <span key={i} className={i < bars * 0.35 ? "played" : ""} style={{ height: `${h}px` }}/>;
        })}
      </div>
      <span className="vdur">{dur}</span>
      <span className="meta-v">{time}{dir === "out" && <Tick read={read} />}</span>
    </div>
  );
}

function PhotoBubble({ dir, label, caption, time, read, delay = 0 }) {
  return (
    <div className={`wa-bubble ${dir} wa-photo`} style={{ animationDelay: `${delay}s` }}>
      <div className="ph"><div style={{ position: "relative", zIndex: 1 }}>{label}</div></div>
      {caption && <div className="caption">{caption}</div>}
      <span className="meta" style={{ position: "relative", right: 0, bottom: 0, marginTop: 2, justifyContent: "flex-end", display: "flex", paddingRight: 4 }}>
        {time}{dir === "out" && <Tick read={read} />}
      </span>
    </div>
  );
}

function PhotoCardBubble({ dir, src, alt, title, sub, time, read, delay = 0 }) {
  const [err, setErr] = useState(false);
  const fallback = `https://picsum.photos/seed/${encodeURIComponent(alt || title || "card")}/600/400`;
  return (
    <div className={`wa-bubble ${dir} wa-photocard`} style={{ animationDelay: `${delay}s` }}>
      <img className="pc-img" src={err ? fallback : src} alt={alt || title} onError={() => setErr(true)} loading="lazy"/>
      <div className="pc-body">
        <div className="pc-title">{title}</div>
        {sub && <div className="pc-sub">{sub}</div>}
      </div>
      <span className="meta">{time}{dir === "out" && <Tick read={read} />}</span>
    </div>
  );
}

function CardBubble({ dir, thumb, title, sub, time, read, delay = 0 }) {
  return (
    <div className={`wa-bubble ${dir}`} style={{ animationDelay: `${delay}s`, minWidth: 220 }}>
      <div className="wa-card">
        <div className="thumb" style={thumb ? { background: thumb } : null}/>
        <div>
          <div className="title">{title}</div>
          <div className="sub">{sub}</div>
        </div>
      </div>
      <span className="meta">{time}{dir === "out" && <Tick read={read} />}</span>
    </div>
  );
}

function TypingBubble() { return <div className="wa-typing"><i/><i/><i/></div>; }

function Phone({ name, initials, avatar = "a", presence = "online", children, size = "default", showInput = true }) {
  const cls = size === "lg" ? "phone lg" : size === "compact" ? "phone compact" : "phone";
  return (
    <div className={cls}>
      <div className="phone-notch" />
      <div className="phone-screen">
        <div className="wa-header">
          <div className="wa-back"><Icon.Back /></div>
          <div className={`wa-avatar ${avatar}`}>{initials}</div>
          <div>
            <div className="wa-name">{name}</div>
            <div className="wa-presence">{presence}</div>
          </div>
          <div className="wa-head-actions">
            <Icon.Video />
            <Icon.Phone />
            <Icon.More />
          </div>
        </div>
        <div className="wa-body">
          <div className="wa-date">TODAY</div>
          {children}
        </div>
        {showInput && (
          <div className="wa-input">
            <button className="icon-btn"><Icon.Emoji /></button>
            <div className="field">Message</div>
            <button className="icon-btn"><Icon.Paperclip /></button>
            <button className="icon-btn"><Icon.Camera /></button>
            <button className="send"><Icon.Mic /></button>
          </div>
        )}
      </div>
    </div>
  );
}

// ========== HAND-DRAWN HOW-IT-WORKS ILLUSTRATIONS ==========
function HowIllu1() {
  return (
    <svg viewBox="0 0 200 180" width="200" fill="none">
      <g stroke="rgba(134,150,160,0.4)" strokeWidth="1" fill="#0e151a">
        <rect x="62" y="24" width="76" height="130" rx="12"/>
        <rect x="70" y="34" width="60" height="105" rx="4" fill="#0b141a" stroke="rgba(255,255,255,0.04)"/>
      </g>
      <g>
        <path d="M 18 62 Q 18 52, 30 52 L 108 52 Q 120 52, 120 62 L 120 82 Q 120 92, 108 92 L 44 92 L 34 102 L 38 92 Q 18 92, 18 82 Z"
          fill="#0f1a14" stroke="#25d366" strokeWidth="1.2"/>
        <g stroke="#4aed85" strokeWidth="1.5" strokeLinecap="round">
          {[0,1,2,3,4,5,6,7,8,9].map(i => (
            <line key={i} x1={32 + i*8} y1="72" x2={32 + i*8} y2="72">
              <animate attributeName="y1" values={`${66 + (i%3)*2};${58 - (i%4)*3};${66 + (i%3)*2}`} dur="2.4s" begin={`${i*0.08}s`} repeatCount="indefinite"/>
              <animate attributeName="y2" values={`${78 - (i%3)*2};${86 + (i%4)*3};${78 - (i%3)*2}`} dur="2.4s" begin={`${i*0.08}s`} repeatCount="indefinite"/>
            </line>
          ))}
        </g>
      </g>
    </svg>
  );
}

function HowIllu2() {
  const nodes = [
    {x:30, y:40}, {x:30, y:90}, {x:30, y:140},
    {x:100, y:30}, {x:100, y:90}, {x:100, y:150},
    {x:170, y:50}, {x:170, y:110}, {x:170, y:150},
  ];
  const edges = [[0,3],[0,4],[1,3],[1,4],[1,5],[2,4],[2,5],[3,6],[4,6],[4,7],[4,8],[5,7],[5,8]];
  return (
    <svg viewBox="0 0 200 180" width="200" fill="none">
      <g stroke="rgba(37,211,102,0.2)" strokeWidth="1">
        {edges.map(([a,b], i) => (
          <line key={i} x1={nodes[a].x} y1={nodes[a].y} x2={nodes[b].x} y2={nodes[b].y}>
            <animate attributeName="stroke" values="rgba(37,211,102,0.15);rgba(37,211,102,0.7);rgba(37,211,102,0.15)" dur="3s" begin={`${i*0.22}s`} repeatCount="indefinite"/>
          </line>
        ))}
      </g>
      {nodes.map((n, i) => (
        <g key={i}>
          <circle cx={n.x} cy={n.y} r="10" fill="rgba(37,211,102,0.06)" stroke="rgba(37,211,102,0.3)">
            <animate attributeName="r" values="8;13;8" dur="2.5s" begin={`${i*0.25}s`} repeatCount="indefinite"/>
            <animate attributeName="stroke" values="rgba(37,211,102,0.3);rgba(74,237,133,0.9);rgba(37,211,102,0.3)" dur="2.5s" begin={`${i*0.25}s`} repeatCount="indefinite"/>
          </circle>
          <circle cx={n.x} cy={n.y} r="3" fill="#4aed85">
            <animate attributeName="opacity" values="0.4;1;0.4" dur="2.5s" begin={`${i*0.25}s`} repeatCount="indefinite"/>
          </circle>
        </g>
      ))}
    </svg>
  );
}

function HowIllu3() {
  // "Every message answered" — three incoming text bubbles, three outgoing replies
  // with green double-ticks animating in. Same hand-drawn WA-green aesthetic.
  return (
    <svg viewBox="0 0 220 180" width="200" fill="none">
      {/* Incoming bubbles (left, dim) */}
      <g>
        <rect x="14" y="14" width="92" height="22" rx="6" fill="#1a2229" stroke="#2a3942" strokeWidth="1"/>
        <line x1="22" y1="22" x2="78" y2="22" stroke="#54656f" strokeWidth="1.4" strokeLinecap="round"/>
        <line x1="22" y1="28" x2="64" y2="28" stroke="#54656f" strokeWidth="1.4" strokeLinecap="round"/>

        <rect x="14" y="62" width="80" height="22" rx="6" fill="#1a2229" stroke="#2a3942" strokeWidth="1"/>
        <line x1="22" y1="70" x2="68" y2="70" stroke="#54656f" strokeWidth="1.4" strokeLinecap="round"/>
        <line x1="22" y1="76" x2="56" y2="76" stroke="#54656f" strokeWidth="1.4" strokeLinecap="round"/>

        <rect x="14" y="110" width="100" height="22" rx="6" fill="#1a2229" stroke="#2a3942" strokeWidth="1"/>
        <line x1="22" y1="118" x2="86" y2="118" stroke="#54656f" strokeWidth="1.4" strokeLinecap="round"/>
        <line x1="22" y1="124" x2="74" y2="124" stroke="#54656f" strokeWidth="1.4" strokeLinecap="round"/>
      </g>

      {/* Outgoing reply bubbles (right, emerald) — fade-in staggered */}
      <g>
        <g opacity="0">
          <rect x="120" y="38" width="86" height="22" rx="6" fill="rgba(37,211,102,0.10)" stroke="#25d366" strokeWidth="1.1"/>
          <line x1="128" y1="46" x2="180" y2="46" stroke="#4aed85" strokeWidth="1.4" strokeLinecap="round"/>
          <line x1="128" y1="52" x2="170" y2="52" stroke="#4aed85" strokeWidth="1.4" strokeLinecap="round"/>
          {/* double tick */}
          <path d="M188 56 L191 59 L196 53 M192 56 L195 59 L200 53" stroke="#4aed85" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round" fill="none"/>
          <animate attributeName="opacity" values="0;0;1" dur="2.4s" begin="0.2s" fill="freeze"/>
        </g>

        <g opacity="0">
          <rect x="120" y="86" width="76" height="22" rx="6" fill="rgba(37,211,102,0.10)" stroke="#25d366" strokeWidth="1.1"/>
          <line x1="128" y1="94" x2="170" y2="94" stroke="#4aed85" strokeWidth="1.4" strokeLinecap="round"/>
          <line x1="128" y1="100" x2="160" y2="100" stroke="#4aed85" strokeWidth="1.4" strokeLinecap="round"/>
          <path d="M178 104 L181 107 L186 101 M182 104 L185 107 L190 101" stroke="#4aed85" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round" fill="none"/>
          <animate attributeName="opacity" values="0;0;1" dur="2.4s" begin="0.9s" fill="freeze"/>
        </g>

        <g opacity="0">
          <rect x="120" y="134" width="86" height="22" rx="6" fill="rgba(37,211,102,0.10)" stroke="#25d366" strokeWidth="1.1"/>
          <line x1="128" y1="142" x2="180" y2="142" stroke="#4aed85" strokeWidth="1.4" strokeLinecap="round"/>
          <line x1="128" y1="148" x2="168" y2="148" stroke="#4aed85" strokeWidth="1.4" strokeLinecap="round"/>
          <path d="M188 152 L191 155 L196 149 M192 152 L195 155 L200 149" stroke="#4aed85" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round" fill="none"/>
          <animate attributeName="opacity" values="0;0;1" dur="2.4s" begin="1.6s" fill="freeze"/>
        </g>
      </g>

      {/* tiny connecting wisps from each incoming → its reply */}
      <g stroke="rgba(74,237,133,0.4)" strokeWidth="0.8" strokeDasharray="2 3" fill="none">
        <path d="M108 25 Q 116 30, 120 44"/>
        <path d="M96 73 Q 110 82, 120 92"/>
        <path d="M116 121 Q 120 130, 122 140"/>
      </g>
    </svg>
  );
}

function WobblyArrow() {
  return (
    <svg viewBox="0 0 100 40" width="80" height="40" fill="none">
      <path d="M 8 20 Q 25 14, 40 22 T 72 18 L 88 20" stroke="#25d366" strokeWidth="1.3" strokeLinecap="round" fill="none" strokeDasharray="140" strokeDashoffset="140">
        <animate attributeName="stroke-dashoffset" values="140;0" dur="1.8s" fill="freeze"/>
      </path>
      <path d="M 82 14 L 90 20 L 82 26" stroke="#25d366" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round" fill="none" opacity="0">
        <animate attributeName="opacity" values="0;1" dur="0.3s" begin="1.6s" fill="freeze"/>
      </path>
    </svg>
  );
}

window.WA = { Icon, Tick, TextBubble, VoiceBubble, PhotoBubble, PhotoCardBubble, CardBubble, TypingBubble, Phone, HowIllu1, HowIllu2, HowIllu3, WobblyArrow };
