/* global React */
const { useState, useEffect, useRef } = React;

// ---------- Mir Consulting mark (real logo) ----------
function MirMark({ size = 30 }) {
  return (
    <img src="assets/mir-logo.png" alt="Mir Consulting"
      style={{height:size, width:'auto', display:'block'}} />
  );
}

// ---------- Logo (Mir Consulting) ----------
function Logo({ size = 30, sub = true }) {
  return (
    <a href="#top" style={{display:'flex', alignItems:'center', gap:11, color:'var(--cothon-cream)'}}>
      <MirMark size={size} />
      <span style={{display:'flex', flexDirection:'column', lineHeight:1}}>
        <span style={{fontSize: size * 0.6, fontWeight: 600, letterSpacing:'-0.015em'}}>Mir Consulting</span>
        {sub && <span style={{fontSize: size*0.3, color:'var(--cothon-sand)', letterSpacing:'0.1em', marginTop:4, fontFamily:'JetBrains Mono, monospace'}}>APPLIED AI</span>}
      </span>
    </a>
  );
}

// ---------- CTA button ----------
function CTAButton({ children, size='lg', variant='primary', icon=true, onClick, href, style={} }) {
  const variants = {
    primary: {
      background: 'linear-gradient(90deg, #EF8D88, #E07B74)',
      color: '#0D2B52',
      border: '1px solid transparent',
      boxShadow: '0 10px 25px -8px rgba(239,141,136,0.4), inset 0 1px 0 rgba(255,255,255,0.25)',
    },
    outline: {
      background: 'transparent',
      color: 'var(--cothon-cream)',
      border: '1px solid var(--cothon-navy-border)',
    },
    ghost: {
      background: 'transparent',
      color: 'var(--cothon-cream)',
      border: '1px solid transparent',
    },
  };
  const sizes = {
    sm: { height:36, padding:'0 14px', fontSize:13, borderRadius:8 },
    md: { height:44, padding:'0 20px', fontSize:14, borderRadius:10 },
    lg: { height:54, padding:'0 26px', fontSize:15, borderRadius:12 },
  };
  const As = href ? 'a' : 'button';
  return (
    <As onClick={onClick} href={href} style={{
      display:'inline-flex', alignItems:'center', gap:8,
      fontFamily:'Inter', fontWeight:600, cursor:'pointer', textDecoration:'none',
      transition:'transform 180ms cubic-bezier(0.4,0,0.2,1), filter 180ms, box-shadow 180ms',
      ...variants[variant], ...sizes[size], ...style
    }}
    onMouseEnter={e => { e.currentTarget.style.transform='translateY(-1px)'; e.currentTarget.style.filter='brightness(1.06)'; }}
    onMouseLeave={e => { e.currentTarget.style.transform=''; e.currentTarget.style.filter=''; }}>
      <span>{children}</span>
      {icon && <span style={{display:'inline-flex', transform:'translateY(-0.5px)'}}><Arrow size={14}/></span>}
    </As>
  );
}

// ---------- Tiny inline SVG icons (Lucide-style, 1.5 stroke) ----------
function Stroke({ children, size=16, color='currentColor', extra={} }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none"
      stroke={color} strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"
      style={{display:'inline-block', verticalAlign:'middle', ...extra}}>{children}</svg>
  );
}
const Arrow      = (p) => <Stroke {...p}><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></Stroke>;
const ArrowUR    = (p) => <Stroke {...p}><line x1="7" y1="17" x2="17" y2="7"/><polyline points="7 7 17 7 17 17"/></Stroke>;
const Check      = (p) => <Stroke {...p}><polyline points="20 6 9 17 4 12"/></Stroke>;
const CheckCirc  = (p) => <Stroke {...p}><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></Stroke>;
const Clock      = (p) => <Stroke {...p}><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></Stroke>;
const Alert      = (p) => <Stroke {...p}><path d="M10.29 3.86 1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12" y2="17.01"/></Stroke>;
const XCirc      = (p) => <Stroke {...p}><circle cx="12" cy="12" r="10"/><line x1="15" y1="9" x2="9" y2="15"/><line x1="9" y1="9" x2="15" y2="15"/></Stroke>;
const Sparkle    = (p) => <Stroke {...p}><path d="M12 3v3M12 18v3M3 12h3M18 12h3M5.6 5.6l2.1 2.1M16.3 16.3l2.1 2.1M5.6 18.4l2.1-2.1M16.3 7.7l2.1-2.1"/></Stroke>;
const Search     = (p) => <Stroke {...p}><circle cx="11" cy="11" r="7"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></Stroke>;
const Bell       = (p) => <Stroke {...p}><path d="M18 8a6 6 0 0 0-12 0c0 7-3 9-3 9h18s-3-2-3-9"/><path d="M13.73 21a2 2 0 0 1-3.46 0"/></Stroke>;
const Globe      = (p) => <Stroke {...p}><circle cx="12" cy="12" r="10"/><line x1="2" y1="12" x2="22" y2="12"/><path d="M12 2a15 15 0 0 1 4 10 15 15 0 0 1-4 10 15 15 0 0 1-4-10 15 15 0 0 1 4-10z"/></Stroke>;
const Trend      = (p) => <Stroke {...p}><polyline points="22 7 13.5 15.5 8.5 10.5 2 17"/><polyline points="16 7 22 7 22 13"/></Stroke>;
const Dollar     = (p) => <Stroke {...p}><line x1="12" y1="1" x2="12" y2="23"/><path d="M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6"/></Stroke>;
const Bldg       = (p) => <Stroke {...p}><rect x="4" y="2" width="16" height="20" rx="2"/><line x1="9" y1="22" x2="9" y2="18"/><line x1="15" y1="22" x2="15" y2="18"/><line x1="8" y1="6" x2="10" y2="6"/><line x1="14" y1="6" x2="16" y2="6"/><line x1="8" y1="10" x2="10" y2="10"/><line x1="14" y1="10" x2="16" y2="10"/></Stroke>;
const Shield     = (p) => <Stroke {...p}><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></Stroke>;
const Target     = (p) => <Stroke {...p}><circle cx="12" cy="12" r="10"/><circle cx="12" cy="12" r="6"/><circle cx="12" cy="12" r="2"/></Stroke>;
const Users      = (p) => <Stroke {...p}><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></Stroke>;
const Brain      = (p) => <Stroke {...p}><path d="M12 5a3 3 0 1 0-5.997.142 4 4 0 0 0-2.526 5.77 4 4 0 0 0 .556 6.588A4 4 0 0 0 12 21Z"/><path d="M12 5a3 3 0 1 1 5.997.142 4 4 0 0 1 2.526 5.77 4 4 0 0 1-.556 6.588A4 4 0 0 1 12 21Z"/></Stroke>;
const FileText   = (p) => <Stroke {...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"/><line x1="9" y1="13" x2="15" y2="13"/><line x1="9" y1="17" x2="13" y2="17"/></Stroke>;
const Lock       = (p) => <Stroke {...p}><rect x="3" y="11" width="18" height="11" rx="2"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/></Stroke>;
const Pin        = (p) => <Stroke {...p}><line x1="12" y1="17" x2="12" y2="22"/><path d="M5 17h14l-2-5H7l-2 5z"/><path d="M9 12V7a3 3 0 0 1 6 0v5"/></Stroke>;
const Minus      = (p) => <Stroke {...p}><line x1="5" y1="12" x2="19" y2="12"/></Stroke>;
const Plus       = (p) => <Stroke {...p}><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></Stroke>;
const Layers     = (p) => <Stroke {...p}><polygon points="12 2 2 7 12 12 22 7 12 2"/><polyline points="2 17 12 22 22 17"/><polyline points="2 12 12 17 22 12"/></Stroke>;
const Zap        = (p) => <Stroke {...p}><polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2"/></Stroke>;
const MessageSq  = (p) => <Stroke {...p}><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></Stroke>;
const Workflow   = (p) => <Stroke {...p}><rect x="3" y="3" width="7" height="7" rx="1"/><rect x="14" y="14" width="7" height="7" rx="1"/><path d="M10 6.5h4a3 3 0 0 1 3 3v4"/></Stroke>;
const Cpu        = (p) => <Stroke {...p}><rect x="4" y="4" width="16" height="16" rx="2"/><rect x="9" y="9" width="6" height="6"/><line x1="9" y1="1" x2="9" y2="4"/><line x1="15" y1="1" x2="15" y2="4"/><line x1="9" y1="20" x2="9" y2="23"/><line x1="15" y1="20" x2="15" y2="23"/><line x1="20" y1="9" x2="23" y2="9"/><line x1="20" y1="14" x2="23" y2="14"/><line x1="1" y1="9" x2="4" y2="9"/><line x1="1" y1="14" x2="4" y2="14"/></Stroke>;

// ---------- Section wrapper ----------
function Section({ children, id, dividerTop=true, py='var(--s-24)', maxWidth=1240, style={} }) {
  return (
    <section id={id} style={{
      position:'relative',
      padding: `${typeof py==='string'?py:py+'px'} 0`,
      borderTop: dividerTop ? '1px solid var(--cothon-navy-border)' : 'none',
      ...style,
    }}>
      <div style={{maxWidth, margin:'0 auto', padding:'0 32px'}}>{children}</div>
    </section>
  );
}

// ---------- Micro label / eyebrow ----------
function Eyebrow({ children, color='var(--cothon-sand)', mark }) {
  return (
    <div style={{
      display:'inline-flex', alignItems:'center', gap:10,
      fontSize:11, fontWeight:600, letterSpacing:'0.14em', textTransform:'uppercase',
      color, fontFamily:'JetBrains Mono, monospace',
    }}>
      {mark && <span style={{width:8, height:1, background:color}}/>}
      {children}
    </div>
  );
}

// ---------- Hairline status pill ----------
function StatusPill({ children, color='var(--cothon-mint)' }) {
  return (
    <span style={{
      display:'inline-flex', alignItems:'center', gap:8,
      padding:'5px 11px', borderRadius:9999,
      border:'1px solid rgba(240,234,218,0.10)',
      background:'rgba(16,49,90,0.55)',
      fontSize:12, fontWeight:500, color:'var(--cothon-cream)',
    }}>
      <span className="live-dot" style={{
        width:6, height:6, borderRadius:9999,
        background: color, display:'inline-block'
      }}/>
      {children}
    </span>
  );
}

// ---------- Gradient text ----------
function Gradient({ children, from='#EF8D88', to='#D68A59', style={} }) {
  return (
    <span style={{
      background:`linear-gradient(95deg, ${from}, ${to})`,
      WebkitBackgroundClip:'text', backgroundClip:'text', color:'transparent',
      ...style
    }}>{children}</span>
  );
}

// ---------- Scroll-reveal hook + wrapper ----------
function useInView(opts = {}) {
  const ref = React.useRef(null);
  const [seen, setSeen] = React.useState(false);
  React.useEffect(() => {
    if (!ref.current || seen) return;
    const io = new IntersectionObserver((entries) => {
      entries.forEach(e => {
        if (e.isIntersecting) { setSeen(true); io.disconnect(); }
      });
    }, { threshold: 0.12, rootMargin: '-40px 0px', ...opts });
    io.observe(ref.current);
    return () => io.disconnect();
  }, []);
  return [ref, seen];
}

function Reveal({ children, delay = 0, y = 24, duration = 700, as: As = 'div', style = {}, ...rest }) {
  const [ref, seen] = useInView();
  return (
    <As ref={ref} style={{
      opacity: seen ? 1 : 0,
      transform: seen ? 'translateY(0)' : `translateY(${y}px)`,
      transition: `opacity ${duration}ms cubic-bezier(0.2, 0.7, 0.2, 1) ${delay}ms, transform ${duration}ms cubic-bezier(0.2, 0.7, 0.2, 1) ${delay}ms`,
      willChange:'opacity, transform',
      ...style,
    }} {...rest}>{children}</As>
  );
}

// ---------- CountUp ----------
function CountUp({ to = 100, duration = 1500, prefix = '', suffix = '', format = (v) => Math.round(v).toLocaleString(), trigger = true }) {
  const [v, setV] = React.useState(0);
  React.useEffect(() => {
    if (!trigger) return;
    let raf;
    const start = performance.now();
    const step = (t) => {
      const p = Math.min(1, (t - start) / duration);
      const eased = 1 - Math.pow(1 - p, 3);
      setV(eased * to);
      if (p < 1) raf = requestAnimationFrame(step);
    };
    raf = requestAnimationFrame(step);
    return () => cancelAnimationFrame(raf);
  }, [to, duration, trigger]);
  return <span style={{fontVariantNumeric:'tabular-nums'}}>{prefix}{format(v)}{suffix}</span>;
}

Object.assign(window, {
  Logo, MirMark, CTAButton, Section, Eyebrow, StatusPill, Gradient,
  Arrow, ArrowUR, Check, CheckCirc, Clock, Alert, XCirc, Sparkle,
  Search, Bell, Globe, Trend, Dollar, Bldg, Shield, Target, Users, Brain,
  FileText, Lock, Pin, Minus, Plus, Layers, Zap, MessageSq, Workflow, Cpu,
  Reveal, CountUp, useInView
});
