/* ═══════════════ Closink Landing · CORE (icons, atoms, hooks) ═══════════════ */
const { useState, useEffect, useRef, useCallback } = React;

/* ----- icon factory (lucide-style, inline SVG) ----- */
const Ic = ({ d, s = 22, c = 'currentColor', sw = 1.7, fill = 'none', style }) => (
  <svg viewBox="0 0 24 24" width={s} height={s} fill={fill} stroke={fill === 'none' ? c : 'none'}
    strokeWidth={sw} strokeLinecap="round" strokeLinejoin="round" style={style}>{d}</svg>
);
const IconShield   = (p) => <Ic {...p} d={<path d="M12 3l7.5 2.7v5.3c0 4.6-3.2 7.9-7.5 8.7-4.3-.8-7.5-4.1-7.5-8.7V5.7z"/>} />;
const IconShieldOk = (p) => <Ic {...p} d={<><path d="M12 3l7.5 2.7v5.3c0 4.6-3.2 7.9-7.5 8.7-4.3-.8-7.5-4.1-7.5-8.7V5.7z"/><path d="M8.6 12.1l2.2 2.2 4.6-4.8"/></>} />;
const ShieldFill   = ({ s = 22, c = '#fff' }) => <svg viewBox="0 0 24 24" width={s} height={s}><path d="M12 2.5l8 2.9v5.6c0 4.9-3.4 8.4-8 9.2-4.6-.8-8-4.3-8-9.2V5.4z" fill={c}/></svg>;
const IconLock     = (p) => <Ic {...p} d={<><rect x="4.5" y="11" width="15" height="9.5" rx="2.2"/><path d="M8 11V8a4 4 0 018 0v3"/></>} />;
const IconMapPin   = (p) => <Ic {...p} d={<><path d="M12 21c4.5-4.2 7-7.4 7-11a7 7 0 10-14 0c0 3.6 2.5 6.8 7 11z"/><circle cx="12" cy="10" r="2.6"/></>} />;
const IconPinFill  = ({ s = 22, c = '#fff' }) => <svg viewBox="0 0 24 24" width={s} height={s}><path d="M12 22c4.6-4.4 7-7.6 7-11.2A7 7 0 105 10.8C5 14.4 7.4 17.6 12 22z" fill={c}/><circle cx="12" cy="10" r="2.4" fill="rgba(0,0,0,.28)"/></svg>;
const IconNav      = (p) => <Ic {...p} fill={p.c || 'currentColor'} d={<path d="M3.5 11l17-7.5-7.5 17-2-7z"/>} />;
const IconHeart    = (p) => <Ic {...p} d={<path d="M12 20s-7-4.4-9.2-9C1.3 8 3 4.7 6.2 4.7c2 0 3.2 1.2 3.8 2.3.6-1.1 1.8-2.3 3.8-2.3 3.2 0 4.9 3.3 3.4 6.3C19 15.6 12 20 12 20z"/>} />;
const HeartFill    = ({ s = 22, c = '#EF4444' }) => <svg viewBox="0 0 24 24" width={s} height={s}><path d="M12 20.5S3.6 15.5 2.2 9.7C1.3 6 3.4 3.5 6.3 3.5c2 0 3.5 1.1 4.4 2.6.9-1.5 2.4-2.6 4.4-2.6 2.9 0 5 2.5 4.1 6.2C20.4 15.5 12 20.5 12 20.5z" fill={c}/></svg>;
const IconBell     = (p) => <Ic {...p} d={<><path d="M6 9a6 6 0 0112 0c0 5 2 6 2 6H4s2-1 2-6z"/><path d="M10.5 20a2 2 0 003 0"/></>} />;
const IconRoute    = (p) => <Ic {...p} d={<><circle cx="6" cy="18.5" r="2.2"/><circle cx="18" cy="5.5" r="2.2"/><path d="M8 18.5h6a3.5 3.5 0 000-7H10a3.5 3.5 0 010-7h6"/></>} />;
const IconCar      = (p) => <Ic {...p} d={<><path d="M3 13l1.7-4.6A2 2 0 016.6 7h10.8a2 2 0 011.9 1.4L21 13v5a1 1 0 01-1 1h-1a1 1 0 01-1-1v-1H6v1a1 1 0 01-1 1H4a1 1 0 01-1-1z"/><path d="M3 13h18"/><circle cx="7" cy="16.5" r="0.4"/><circle cx="17" cy="16.5" r="0.4"/></>} />;
const IconGauge    = (p) => <Ic {...p} d={<><path d="M5 18a8 8 0 1114 0"/><path d="M12 18l4-5"/></>} />;
const IconZap      = (p) => <Ic {...p} fill={p.c || 'currentColor'} d={<path d="M13 2L4 14h6l-1 8 9-12h-6z"/>} />;
const IconCheck    = (p) => <Ic {...p} d={<path d="M5 12.5l4.2 4.3L19 6.5"/>} />;
const IconChevR    = (p) => <Ic {...p} d={<path d="M9 5l7 7-7 7"/>} />;
const IconArrowR   = (p) => <Ic {...p} d={<><path d="M4 12h15"/><path d="M13 6l6 6-6 6"/></>} />;
const IconEye      = (p) => <Ic {...p} d={<><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7-10-7-10-7z"/><circle cx="12" cy="12" r="3"/></>} />;
const IconUsers    = (p) => <Ic {...p} d={<><circle cx="9" cy="8" r="3.2"/><path d="M3 20c0-3.3 2.7-5.5 6-5.5s6 2.2 6 5.5"/><path d="M16 4.2a3.2 3.2 0 010 6.1M21 20c0-2.7-1.6-4.6-3.8-5.2"/></>} />;
const IconSpark    = (p) => <Ic {...p} d={<path d="M12 3l1.8 5.4L19 10l-5.2 1.6L12 17l-1.8-5.4L5 10l5.2-1.6z"/>} />;
const IconPlay     = ({ s = 22, c = '#fff' }) => <svg viewBox="0 0 24 24" width={s} height={s}><path d="M7 4.5l13 7.5-13 7.5z" fill={c}/></svg>;
const IconStar     = ({ s = 16, c = '#E6B450' }) => <svg viewBox="0 0 24 24" width={s} height={s}><path d="M12 2.5l2.9 6 6.6.6-5 4.4 1.5 6.5L12 17.6 6 20l1.5-6.5-5-4.4 6.6-.6z" fill={c}/></svg>;
const IconAlert    = (p) => <Ic {...p} d={<><circle cx="12" cy="12" r="9"/><path d="M12 7.5v5.5"/><circle cx="12" cy="16.3" r="0.3" fill="currentColor" stroke="none"/></>} />;
const IconVideo    = (p) => <Ic {...p} d={<><rect x="3" y="6" width="13" height="12" rx="2.4"/><path d="M16 10l5-3v10l-5-3z"/></>} />;
const IconMic      = (p) => <Ic {...p} d={<><rect x="9" y="3" width="6" height="11" rx="3"/><path d="M5 11a7 7 0 0014 0M12 18v3"/></>} />;
const IconLayers   = (p) => <Ic {...p} d={<><path d="M12 3l9 5-9 5-9-5z"/><path d="M3 13l9 5 9-5"/></>} />;
const IconBolt112  = (p) => <Ic {...p} d={<><path d="M12 2.5l7 3v5c0 4.4-3 7.6-7 8.6-4-1-7-4.2-7-8.6v-5z"/><path d="M9 11l2 2 4-4"/></>} />;
const IconFinger   = (p) => <Ic {...p} d={<><path d="M12 11v3a4 4 0 01-4 4"/><path d="M8 11a4 4 0 018 0v2a7 7 0 01-1 3.6"/><path d="M5 13v-2a7 7 0 0114 0v2"/></>} />;
const IconClock    = (p) => <Ic {...p} d={<><circle cx="12" cy="12" r="9"/><path d="M12 7.5V12l3 2"/></>} />;
const IconCalendar = (p) => <Ic {...p} d={<><rect x="4" y="5" width="16" height="16" rx="2.5"/><path d="M4 9.5h16M8 3v4M16 3v4"/></>} />;
const IconGlobe    = (p) => <Ic {...p} d={<><circle cx="12" cy="12" r="9"/><path d="M3 12h18M12 3c2.6 2.6 2.6 15.4 0 18M12 3c-2.6 2.6-2.6 15.4 0 18"/></>} />;

/* ----- store badges (inline SVG, no external) ----- */
const AppleBadge = () => (
  <a href="#indir" className="group inline-flex items-center gap-3 rounded-2xl px-5 h-[58px] bg-white text-[#0F172A] ring-soft transition-transform duration-200 hover:-translate-y-0.5">
    <svg viewBox="0 0 384 512" width="24" height="24" fill="#0F172A"><path d="M318.7 268c-.5-58 47.4-85.9 49.5-87.2-27-39.5-69-44.9-83.9-45.5-35.7-3.6-69.7 21-87.8 21-18 0-46-20.5-75.7-20-39 .6-74.9 22.6-94.9 57.5-40.5 70.2-10.4 174 29 231 19.2 27.8 42.1 59 72.1 57.9 28.9-1.1 39.8-18.7 74.8-18.7s44.9 18.7 75.6 18.1c31.2-.5 51-28.3 70.1-56.2 22.1-32.1 31.2-63.2 31.7-64.8-.7-.3-60.8-23.3-61.3-92.1zM260.6 96.7c16-19.4 26.8-46.3 23.8-73.1-23 1-50.9 15.3-67.4 34.6-14.8 17.1-27.8 44.5-24.3 70.8 25.7 2 51.9-13 67.9-32.3z"/></svg>
    <span className="flex flex-col leading-none text-left whitespace-nowrap">
      <span className="text-[10px] font-medium opacity-60 mono">İNDİR</span>
      <span className="text-[17px] font-extrabold -mt-0.5 tracking-tight">App Store</span>
    </span>
  </a>
);
const GoogleBadge = () => (
  <a href="#indir" className="group inline-flex items-center gap-3 rounded-2xl px-5 h-[58px] bg-white text-[#0F172A] ring-soft transition-transform duration-200 hover:-translate-y-0.5">
    <svg viewBox="0 0 512 512" width="22" height="22"><path d="M48 60c-4 4-7 11-7 20v352c0 9 3 16 7 20l201-216z" fill="#34A853"/><path d="M325 254L78 53c-8-4-16-3-22 1l201 216z" fill="#EA4335"/><path d="M325 254L56 458c6 4 14 5 22 1l247-142z" fill="#FBBC04"/><path d="M325 254l78 45c20 12 20 30 0 42l-78 45-49-91z" fill="#4285F4"/></svg>
    <span className="flex flex-col leading-none text-left whitespace-nowrap">
      <span className="text-[10px] font-medium opacity-60 mono">İNDİR</span>
      <span className="text-[17px] font-extrabold -mt-0.5 tracking-tight">Google Play</span>
    </span>
  </a>
);

/* ----- shared atoms ----- */
const Eyebrow = ({ children, color = '#3678F9', dot = true }) => (
  <span className="inline-flex items-center gap-2 mono text-[12px] font-semibold tracking-[0.22em] uppercase whitespace-nowrap" style={{ color }}>
    {dot && <span className="w-1.5 h-1.5 rounded-full" style={{ background: color, boxShadow: `0 0 10px ${color}` }}></span>}
    {children}
  </span>
);

const Glass = ({ children, className = '', style }) => (
  <div className={`glass glass-edge rounded-3xl ${className}`} style={style}>{children}</div>
);

const PillBadge = ({ icon, children, tone = 'cyan', dark = false }) => {
  const light = {
    cyan:  ['rgba(56,189,248,.14)', 'rgba(56,189,248,.36)', '#0369A1'],
    green: ['rgba(34,197,94,.14)', 'rgba(34,197,94,.38)', '#15803D'],
    gold:  ['rgba(230,180,80,.16)', 'rgba(230,180,80,.46)', '#B45309'],
    red:   ['rgba(239,68,68,.12)', 'rgba(239,68,68,.4)', '#DC2626'],
  };
  const darkMap = {
    cyan:  ['rgba(56,189,248,.12)', 'rgba(56,189,248,.32)', '#7DD3FC'],
    green: ['rgba(34,197,94,.12)', 'rgba(34,197,94,.34)', '#86EFAC'],
    gold:  ['rgba(230,180,80,.12)', 'rgba(230,180,80,.4)', '#FCD34D'],
    red:   ['rgba(239,68,68,.14)', 'rgba(239,68,68,.4)', '#FCA5A5'],
  };
  const map = (dark ? darkMap : light)[tone];
  return (
    <span className="inline-flex items-center gap-2 rounded-full px-3.5 py-1.5 text-[13px] font-semibold whitespace-nowrap"
      style={{ background: map[0], border: `1px solid ${map[1]}`, color: map[2] }}>
      {icon}{children}
    </span>
  );
};

/* ----- aurora / mesh background blobs ----- */
const Aurora = ({ variant = 'blue' }) => {
  const sets = {
    blue: [['#1D4ED8', '8%', '6%', 560], ['#38BDF8', '78%', '14%', 460], ['#7C5CFC', '52%', '70%', 520]],
    cyan: [['#38BDF8', '70%', '4%', 520], ['#1D4ED8', '6%', '40%', 560]],
    red:  [['#E11D48', '50%', '20%', 640], ['#7C5CFC', '12%', '70%', 420]],
    gold: [['#E6B450', '50%', '0%', 540], ['#1D4ED8', '14%', '60%', 460]],
  }[variant];
  return (
    <div className="absolute inset-0 overflow-hidden pointer-events-none" aria-hidden="true">
      {sets.map(([c, l, t, sz], i) => (
        <div key={i} className="absolute rounded-full" style={{
          left: l, top: t, width: sz, height: sz,
          background: `radial-gradient(circle, ${c}26 0%, ${c}0d 40%, transparent 70%)`,
          filter: 'blur(24px)', opacity: 0.9, animation: `auroraShift ${16 + i * 4}s ease-in-out infinite`,
        }}></div>
      ))}
    </div>
  );
};

/* ----- hooks ----- */
function useInView() {
  const ref = useRef(null);
  const [seen, setSeen] = useState(false);
  useEffect(() => {
    const el = ref.current; if (!el) return;
    const vh = window.innerHeight || document.documentElement.clientHeight || 800;
    const inView = () => { const r = el.getBoundingClientRect(); return r.top < vh * 0.92 && r.bottom > 0; };
    if (inView()) { setSeen(true); return; }
    if (typeof IntersectionObserver === 'undefined') { setSeen(true); return; }
    const ob = new IntersectionObserver((es) => { if (es.some((e) => e.isIntersecting)) { setSeen(true); ob.disconnect(); } }, { threshold: 0.1, rootMargin: '0px 0px -7% 0px' });
    ob.observe(el);
    return () => ob.disconnect();
  }, []);
  return [ref, seen];
}

/* Reveal wrapper — fades/slides children in on scroll */
const Reveal = ({ children, delay = 0, className = '', as: Tag = 'div', style }) => {
  const [ref, seen] = useInView();
  const dc = delay ? ` d${delay}` : '';
  return <Tag ref={ref} className={`reveal${dc}${seen ? ' in' : ''} ${className}`} style={style}>{children}</Tag>;
};

/* CountUp — animates a number when in view */
function CountUp({ to, dur = 1500, decimals = 0, suffix = '', prefix = '', className = '', style }) {
  const [ref, seen] = useInView({ threshold: 0.4 });
  const [val, setVal] = useState(0);
  useEffect(() => {
    if (!seen) return;
    if ((window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches) || document.visibilityState === 'hidden') { setVal(to); return; }
    let raf, t0;
    const tick = (t) => {
      if (!t0) t0 = t;
      const p = Math.min(1, (t - t0) / dur);
      const e = 1 - Math.pow(1 - p, 3);
      setVal(to * e);
      if (p < 1) raf = requestAnimationFrame(tick);
    };
    raf = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(raf);
  }, [seen, to]);
  const txt = val.toLocaleString('tr-TR', { minimumFractionDigits: decimals, maximumFractionDigits: decimals });
  return <span ref={ref} className={className} style={style}>{prefix}{txt}{suffix}</span>;
}

/* 3D tilt-on-hover wrapper */
function Tilt({ children, max = 8, className = '', style }) {
  const ref = useRef(null);
  const onMove = (e) => {
    const el = ref.current; if (!el) return;
    const r = el.getBoundingClientRect();
    const px = (e.clientX - r.left) / r.width - 0.5;
    const py = (e.clientY - r.top) / r.height - 0.5;
    el.style.transform = `perspective(900px) rotateX(${-py * max}deg) rotateY(${px * max}deg)`;
  };
  const reset = () => { if (ref.current) ref.current.style.transform = 'perspective(900px) rotateX(0) rotateY(0)'; };
  return (
    <div ref={ref} onMouseMove={onMove} onMouseLeave={reset} className={className}
      style={{ transition: 'transform .25s cubic-bezier(.16,1,.3,1)', transformStyle: 'preserve-3d', ...style }}>
      {children}
    </div>
  );
}

Object.assign(window, {
  React, useState, useEffect, useRef, useCallback,
  Ic, IconShield, IconShieldOk, ShieldFill, IconLock, IconMapPin, IconPinFill, IconNav,
  IconHeart, HeartFill, IconBell, IconRoute, IconCar, IconGauge, IconZap, IconCheck, IconChevR,
  IconArrowR, IconEye, IconUsers, IconSpark, IconPlay, IconStar, IconAlert, IconVideo, IconMic,
  IconLayers, IconBolt112, IconFinger, IconClock, IconCalendar, IconGlobe,
  AppleBadge, GoogleBadge, Eyebrow, Glass, PillBadge, Aurora, useInView, Reveal, CountUp, Tilt,
});
