/* global React */
// ─────────────────────────────────────────────────────────────
// RopeCounter V3 — icons, silhouette, camera feed, scrim, waveform
// Synchronized with iOS commit 4bc8d57b (_claudecode_20261104) // 2026.11.04 Naron
// ─────────────────────────────────────────────────────────────
const { useRef, useEffect, useState } = React;

// graduationcap.fill / school
const IcGradCap = ({ size = 14, color = '#fff' }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill={color} aria-hidden="true">
    <path d="M12 3 1 8l11 5 9-4.09V15h2V8L12 3Z"/>
    <path d="M5 11.18v3.5L12 18l7-3.32v-3.5L12 14.5 5 11.18Z" opacity="0.72"/>
  </svg>
);
const IcPause = ({ size = 20, color }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill={color} aria-hidden="true">
    <rect x="6" y="4.5" width="4" height="15" rx="1.6"/><rect x="14" y="4.5" width="4" height="15" rx="1.6"/>
  </svg>
);
const IcChevDown = ({ size = 12, color }) => (
  <svg width={size} height={size} viewBox="0 0 12 12" fill="none" stroke={color} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M2 4l4 4 4-4"/></svg>
);
const IcChevRight = ({ size = 14, color }) => (
  <svg width={size} height={size} viewBox="0 0 14 14" fill="none" stroke={color} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M5 2l5 5-5 5"/></svg>
);
const IcX = ({ size = 16, color }) => (
  <svg width={size} height={size} viewBox="0 0 16 16" fill="none" stroke={color} strokeWidth="2" strokeLinecap="round" aria-hidden="true"><path d="M3 3l10 10M13 3L3 13"/></svg>
);
const IcCheck = ({ size = 14, color }) => (
  <svg width={size} height={size} viewBox="0 0 14 14" fill="none" stroke={color} strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M2.5 7.5l3 3 6-6.5"/></svg>
);
const IcArrowUp = ({ size = 14, color }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill={color} aria-hidden="true"><path d="M12 4l8 8h-5v8h-6v-8H4l8-8z"/></svg>
);
const IcShare = ({ size = 18, color }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M12 3v12M7.5 7.5 12 3l4.5 4.5"/><path d="M5 13v6a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2v-6"/></svg>
);
const IcTrash = ({ size = 18, color }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M3 6h18M8 6V4h8v2M6 6l1 14h10l1-14"/></svg>
);
const IcHeart = ({ size = 16, color }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.9" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M20.8 4.6a5.5 5.5 0 0 0-7.8 0L12 5.7l-1-1.1a5.5 5.5 0 0 0-7.8 7.8l1 1L12 21.2l7.8-7.8 1-1a5.5 5.5 0 0 0 0-7.8z"/></svg>
);
const IcSpeaker = ({ size = 18, color, on = true }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
    <path d="M4 9v6h4l5 4V5L8 9H4z" fill={color} stroke="none"/>
    {on ? <><path d="M16 8.5a4 4 0 0 1 0 7"/><path d="M18.5 6a7 7 0 0 1 0 12"/></> : <path d="M16 9l5 6M21 9l-5 6"/>}
  </svg>
);
const IcMapPin = ({ size = 18, color }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill={color} aria-hidden="true"><path d="M12 2a7 7 0 0 0-7 7c0 5 7 13 7 13s7-8 7-13a7 7 0 0 0-7-7zm0 9.5A2.5 2.5 0 1 1 12 6.5a2.5 2.5 0 0 1 0 5z"/></svg>
);

// gift.fill — upsell banner icon (2026.10.14) // 2026.11.04 Naron
const IcGift = ({ size = 24, color = '#fff' }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.9" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
    <rect x="3" y="8" width="18" height="4" rx="1.2" fill={color} fillOpacity="0.22"/>
    <path d="M5 12v7a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2v-7"/>
    <path d="M12 8v13"/>
    <path d="M12 8H7.5a2.5 2.5 0 1 1 0-5C10 3 12 8 12 8z"/>
    <path d="M12 8h4.5a2.5 2.5 0 1 0 0-5C14 3 12 8 12 8z"/>
  </svg>
);

// person.crop.circle — header WeChat login capsule icon (2026.11.04) // 2026.11.04 Naron
const IcUserCircle = ({ size = 14, color = '#07C160' }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
    <circle cx="12" cy="12" r="9.5"/>
    <circle cx="12" cy="9.5" r="3.2"/>
    <path d="M6.8 18.2c1.4-2.3 3.1-3.4 5.2-3.4s3.8 1.1 5.2 3.4"/>
  </svg>
);

// message.fill — WeChat auth icon // 2026.11.04 Naron
const IcWechat = ({ size = 18, color = '#fff' }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill={color} aria-hidden="true">
    <path d="M9.5 4C5.36 4 2 6.91 2 10.5c0 1.98 1.03 3.75 2.66 4.95L4 18l2.77-1.39c.86.25 1.78.39 2.73.39.34 0 .68-.02 1.01-.06A5.95 5.95 0 0 1 10 14.5c0-3.31 3.13-6 7-6 .33 0 .65.02.97.06C17.22 5.93 13.72 4 9.5 4zm-2.25 4.25a1 1 0 1 1 0 2 1 1 0 0 1 0-2zm4.5 0a1 1 0 1 1 0 2 1 1 0 0 1 0-2z"/>
    <path d="M17 9.5c-3.31 0-6 2.24-6 5s2.69 5 6 5c.74 0 1.45-.11 2.1-.32L21.5 20.5l-.55-2.18C22.22 17.34 23 15.99 23 14.5c0-2.76-2.69-5-6-5zm-2 3.25a.85.85 0 1 1 0 1.7.85.85 0 0 1 0-1.7zm4 0a.85.85 0 1 1 0 1.7.85.85 0 0 1 0-1.7z"/>
  </svg>
);

// qrcode — WeChat QR login icon // 2026.11.04 Naron
const IcQrCode = ({ size = 16, color = '#07C160' }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
    <rect x="3" y="3" width="7" height="7" rx="1.5"/>
    <rect x="14" y="3" width="7" height="7" rx="1.5"/>
    <rect x="3" y="14" width="7" height="7" rx="1.5"/>
    <path d="M14 14h3v3h-3zM18 18h3v3h-3zM14 19h2M19 14v2"/>
  </svg>
);

// arrow.triangle.2.circlepath — sync entitlement icon // 2026.11.04 Naron
const IcSync = ({ size = 16, color = '#15803D' }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
    <path d="M20 11A8.1 8.1 0 0 0 4.5 9M4 5v4h4"/>
    <path d="M4 13a8.1 8.1 0 0 0 15.5 2m.5 4v-4h-4"/>
  </svg>
);

// rectangle.portrait.and.arrow.right — logout icon // 2026.11.04 Naron
const IcLogout = ({ size = 16, color = '#64748B' }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
    <path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/>
    <polyline points="16 17 21 12 16 7"/>
    <line x1="21" y1="12" x2="9" y2="12"/>
  </svg>
);

// iphone / smartphone — device management icon // 2026.11.04 Naron
const IcDevicePhone = ({ size = 16, color = '#15803D' }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
    <rect x="6" y="2" width="12" height="20" rx="2.5"/>
    <line x1="10" y1="18.5" x2="14" y2="18.5"/>
  </svg>
);

// Tab-bar glyphs ───────────────────────────────────────────────
const TabClock = ({ size = 24, color }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M3.5 9a9 9 0 1 1-1 4.2"/><path d="M3.5 4v5h5"/><path d="M12 7.5V12l3 2"/></svg>
);
const TabChart = ({ size = 24, color }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M4 4v16h16"/><path d="M7 15l3.5-4 3 2.5L19 7"/></svg>
);
const TabGear = ({ size = 24, color, fill = false }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill={fill ? color : 'none'} stroke={color} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
    <circle cx="12" cy="12" r="3.2" fill={fill ? '#fff' : 'none'} stroke={fill ? 'none' : color}/>
    <path d="M12 2.5l1.4 2.2 2.6-.5.6 2.6 2.3 1.2-1 2.4 1 2.4-2.3 1.2-.6 2.6-2.6-.5L12 21.5l-1.4-2.2-2.6.5-.6-2.6L5.1 16l1-2.4-1-2.4 2.3-1.2.6-2.6 2.6.5L12 2.5z" fill="none"/>
  </svg>
);

// Crown — membership mark (same 24 grid / 1.8 stroke family)
const IcCrown = ({ size = 24, color, filled }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill={filled ? color : 'none'} stroke={color} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
    <path d="M4 17.5 3 8l4.8 3.6L12 5.5l4.2 6.1L21 8l-1 9.5z"/><path d="M4.5 20h15"/>
  </svg>
);

// ─── Tab glyph family — one rope + two dots, three characters (§7) ───
const TabRope = ({ size = 24, color, on = false, variant = on ? 'b' : 'a' }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
    {variant === 'a' ? <>
      <path d="M5 4.4 8.2 8.4M19 4.4l-3.2 4"/>
      <path d="M4.1 5.6 5.9 3.2M19.9 5.6l-1.8-2.4" strokeWidth="2.4"/>
    </> : <>
      <path d="M4.6 1.8 7.4 6.6M19.4 1.8l-2.8 4.8"/>
      <path d="M6 7.4l2.8-1.6M18 7.4l-2.8-1.6" strokeWidth="2.4"/>
    </>}
    <path d="M4.5 11.5c0 6.2 3.4 9.5 7.5 9.5s7.5-3.3 7.5-9.5"/>
  </svg>
);
const TabLoops = ({ size = 24, color, on = false }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
    <g transform="translate(12 12) scale(1.15) translate(-12 -12)">{on ? <>
      <path d="M3 18.5c2.2 0 2.2-5 4.4-5s2.2 5 4.4 5 2.2-8 4.4-8 2.2 8 4.4 8"/>
      <path d="M14.8 6.2 17.2 5l.8 2.6"/>
      <path d="M3 18.5v2.2" strokeWidth="2.4"/>
    </> : <>
      <path d="M4 12h16" strokeWidth="1.4"/>
      <path d="M2.5 12h3.5M18 12h3.5" strokeWidth="3.6" strokeLinecap="butt"/>
    </>}</g>
  </svg>
);
const TabFace = ({ size = 24, color, on = false }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
    {on ? <>
      <path d="M2.5 19c2.5-6 6-8 9.5-8s7 2 9.5 8"/>
      <circle cx="12" cy="4.6" r="3.8" fill={color} stroke="none"/>
      <circle cx="2.5" cy="19" r="1.5" fill={color} stroke="none"/><circle cx="21.5" cy="19" r="1.5" fill={color} stroke="none"/>
    </> : <>
      <path d="M2.5 19c2.5-6 6-8 9.5-8s7 2 9.5 8"/>
      <circle cx="12" cy="4.6" r="2.9"/>
      <circle cx="2.5" cy="19" r="1.5" fill={color} stroke="none"/><circle cx="21.5" cy="19" r="1.5" fill={color} stroke="none"/>
    </>}
  </svg>
);

const Skipping = ({ width = 300, height = 380, opacity = 0.18 }) => (
  <img src="skipping.png" alt="" width={width} height={height} style={{
    width, height, opacity, objectFit: 'contain', pointerEvents: 'none', display: 'block',
  }}/>
);

const SkipIcon = ({ size = 24, color }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color}
    strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
    <path d="M5 11.5C5 4.5 19 4.5 19 11.5" opacity="0.9"/>
    <circle cx="12" cy="7.4" r="1.9" fill={color} stroke="none"/>
    <path d="M12 9.4v5.2"/>
    <path d="M5.4 11.4 12 11.2l6.6.2"/>
    <path d="M12 14.6 9.4 20M12 14.6 14.6 20"/>
  </svg>
);

function CameraFeed({ dark, radius = 24 }) {
  return (
    <div style={{
      position: 'absolute', inset: 0, borderRadius: radius, overflow: 'hidden',
      background: dark
        ? 'linear-gradient(170deg,#33302b 0%,#211e1a 50%,#0f0c09 100%)'
        : 'linear-gradient(170deg,#dad1c4 0%,#bcb1a0 52%,#8a7d68 100%)',
    }}>
      <div style={{ position: 'absolute', left: 0, right: 0, top: '64%', height: 1, background: dark ? 'rgba(255,255,255,0.06)' : 'rgba(0,0,0,0.07)' }}/>
      <div style={{ position: 'absolute', left: '8%', top: '-4%', width: 120, height: 220,
        background: dark ? 'radial-gradient(ellipse,rgba(255,255,255,0.05),transparent 70%)' : 'radial-gradient(ellipse,rgba(255,255,255,0.22),transparent 70%)',
        filter: 'blur(22px)' }}/>
      <div style={{ position: 'absolute', inset: 0, boxShadow: 'inset 0 0 90px rgba(0,0,0,0.28)' }}/>
    </div>
  );
}

function CamScrim({ radius = 24 }) {
  return (
    <React.Fragment>
      <div style={{ position: 'absolute', left: 0, right: 0, top: 0, height: 88, borderTopLeftRadius: radius, borderTopRightRadius: radius,
        background: 'linear-gradient(180deg,rgba(0,0,0,0.40),rgba(0,0,0,0))', pointerEvents: 'none' }}/>
      <div style={{ position: 'absolute', left: 0, right: 0, bottom: 0, height: 110, borderBottomLeftRadius: radius, borderBottomRightRadius: radius,
        background: 'linear-gradient(0deg,rgba(0,0,0,0.45),rgba(0,0,0,0))', pointerEvents: 'none' }}/>
    </React.Fragment>
  );
}

function buildWave(W, H, spikes, base, peakY) {
  const pts = [];
  for (let x = 0; x <= W; x += 2) {
    let y = base;
    for (const s of spikes) {
      const d = x - s.x;
      if (d >= -s.rise && d <= 0) {
        y = Math.min(y, base - (base - peakY) * s.amp * (1 + d / s.rise));
      } else if (d > 0 && d <= s.decay) {
        y = Math.min(y, base - (base - peakY) * s.amp * (1 - d / s.decay));
      }
    }
    y += Math.sin(x * 0.18) * 1.1;
    pts.push(`${x},${y.toFixed(1)}`);
  }
  return pts.join(' ');
}

function Waveform({ width, height, accent, muted, live = false, flat = false, cadence = 142 }) {
  const W = width, H = height;
  const base = H * 0.74, peakY = H * 0.16;
  const [, force] = useState(0);
  const phase = useRef(0);
  const raf = useRef(0);

  const spacing = W / 9.5;
  const amps = useRef(Array.from({ length: 16 }, () => 0.82 + Math.random() * 0.18));

  useEffect(() => {
    if (!live || flat) return;
    let last = performance.now();
    const speed = spacing * (cadence / 60) / 1000;
    const loop = (now) => {
      phase.current += (now - last) * speed;
      last = now;
      force((n) => (n + 1) & 1023);
      raf.current = requestAnimationFrame(loop);
    };
    raf.current = requestAnimationFrame(loop);
    return () => cancelAnimationFrame(raf.current);
  }, [live, flat, spacing, cadence]);

  let poly;
  if (flat) {
    poly = `0,${base} ${W},${base}`;
  } else {
    const off = phase.current % spacing;
    const spikes = [];
    for (let i = -1; i < 12; i++) {
      const x = W - off - i * spacing;
      const amp = amps.current[(i + 2) % amps.current.length];
      spikes.push({ x, amp, rise: 4, decay: 9 });
    }
    poly = buildWave(W, H, spikes, base, peakY);
  }

  return (
    <svg width={W} height={H} viewBox={`0 0 ${W} ${H}`} style={{ display: 'block', borderRadius: 8, background: muted }}>
      <defs>
        <linearGradient id="wffill" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor={accent} stopOpacity="0.22"/>
          <stop offset="100%" stopColor={accent} stopOpacity="0"/>
        </linearGradient>
      </defs>
      {!flat && <polygon points={`0,${H} ${poly} ${W},${H}`} fill="url(#wffill)"/>}
      <polyline points={poly} fill="none" stroke={flat ? muted : accent} strokeWidth="1.6"
        strokeLinejoin="round" strokeLinecap="round" style={flat ? { stroke: accent, opacity: 0.32 } : undefined}/>
      {live && !flat && <circle cx={W - (phase.current % spacing)} cy={peakY} r="2.4" fill={accent}/>}
    </svg>
  );
}

Object.assign(window, {
  IcGradCap, IcPause, IcChevDown, IcChevRight, IcX, IcCheck, IcArrowUp, IcTrash,
  IcHeart, IcShare, IcSpeaker, IcMapPin, IcGift, IcUserCircle, IcWechat, IcQrCode, IcSync, IcLogout, IcDevicePhone,
  TabClock, TabChart, TabGear, TabFace, TabRope, TabLoops, IcCrown,
  Skipping, SkipIcon, CameraFeed, CamScrim, Waveform,
});
