/** The Conscious Code — atoms: Logo, buttons, badges, eyebrow chips. */

const { useEffect, useRef, useState } = React;

/* The hexagonal isometric cube logo with asterisk. Pass `large` for hero use,
   `iconOnly` to drop the wordmark, `glowing` to add the pulsing asterisk glow. */
function Logo({ large = false, iconOnly = false, minimized = false, glowing = false }) {
  const size = large ? 176 : minimized ? 36 : 52;
  const wordSize = large ? 48 : 22;
  return (
    <div style={{
      display: 'flex',
      alignItems: 'center',
      gap: large ? 28 : 12,
      flexDirection: large ? 'column' : 'row'
    }}>
      <div style={{ width: size, height: size, position: 'relative', flexShrink: 0 }}>
        <svg viewBox="0 0 120 120" style={{
          width: '100%', height: '100%',
          filter: 'drop-shadow(0 0 12px rgba(0,255,213,0.4))'
        }}>
          <polygon fill="#1C3246" points="16.7,35 60,10 103.3,35 60,60" />
          <polygon fill="#142534" points="16.7,85 16.7,35 60,60 60,110" />
          <polygon fill="#0D1925" points="60,60 103.3,35 103.3,85 60,110" />
          <polygon fill="none" stroke="#00FFD5" strokeWidth="1.5" opacity="0.9"
                   points="60,10 103.3,35 103.3,85 60,110 16.7,85 16.7,35"
                   style={{ filter: 'drop-shadow(0 0 4px rgba(0,255,213,0.8))' }} />
        </svg>
        <div style={{
          position: 'absolute', inset: 0,
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          fontFamily: 'JetBrains Mono, monospace', fontWeight: 700,
          color: '#00FFD5', pointerEvents: 'none'
        }}>
          <span style={{ fontSize: size * 0.13, marginTop: -2 }}>{'{'}</span>
          <span style={{
            fontSize: size * 0.32,
            margin: `0 ${size * 0.04}px`,
            textShadow: `0 0 ${glowing ? 30 : 12}px rgba(0,255,213,${glowing ? 1 : 0.7})`,
            transition: 'text-shadow 600ms'
          }}>*</span>
          <span style={{ fontSize: size * 0.13, marginTop: -2 }}>{'}'}</span>
        </div>
      </div>
      {!iconOnly && !minimized && (
        <div style={{
          fontFamily: 'Plus Jakarta Sans, sans-serif',
          fontSize: wordSize,
          letterSpacing: '-0.025em',
          lineHeight: 1,
          display: 'inline-flex',
          gap: '0.25em'
        }}>
          <span style={{ color: '#fff', fontWeight: 300 }}>the</span>
          <span style={{ color: '#fff', fontWeight: 700 }}>conscious</span>
          <span style={{ color: '#3d5a7a', fontWeight: 600 }}>code</span>
        </div>
      )}
    </div>
  );
}

function PrimaryButton({ children, onClick, disabled, fullWidth, sm }) {
  return (
    <button
      onClick={onClick}
      disabled={disabled}
      className={`btn btn-primary${sm ? ' btn-sm' : ''}`}
      style={{ width: fullWidth ? '100%' : undefined }}
    >{children}</button>
  );
}

function GlassButton({ children, onClick, fullWidth, sm }) {
  return (
    <button
      onClick={onClick}
      className={`btn btn-glass${sm ? ' btn-sm' : ''}`}
      style={{ width: fullWidth ? '100%' : undefined }}
    >{children}</button>
  );
}

/* Eyebrow chip with a pulsing status dot — pattern used above every hero/section. */
function EyebrowChip({ pulse = true, children }) {
  return (
    <span className="eyebrow eyebrow-chip">
      {pulse && <span className="dot-pulse" />}
      {children}
    </span>
  );
}

function Eyebrow({ children }) {
  return <div className="eyebrow" style={{ marginBottom: 18 }}>{children}</div>;
}

function PhaseLabel({ children, phase = 1 }) {
  const color = phase === 1 ? 'var(--tcc-primary)' : 'var(--tcc-on-surface-dim)';
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', gap: 8,
      fontFamily: 'JetBrains Mono, monospace',
      fontSize: 10, fontWeight: 700,
      letterSpacing: '0.15em', textTransform: 'uppercase',
      color
    }}>
      <span style={{ width: 6, height: 6, borderRadius: '50%', background: 'currentColor' }} />
      {children}
    </span>
  );
}

/* Triple-weight heading — the signature pattern (light / bold / light-muted). */
function TripleHeading({ the, bold, tail, size = 'h2', centered = false }) {
  const styles = {
    h2: { fontSize: 'clamp(2rem, 4.2vw, 4.5rem)' },
    h3: { fontSize: 'clamp(1.75rem, 3vw, 3rem)' },
    h4: { fontSize: 'clamp(1.5rem, 2.4vw, 2.25rem)' },
  };
  return (
    <h2 className="tw-h" style={{
      ...styles[size],
      textAlign: centered ? 'center' : 'left',
      maxWidth: centered ? '20ch' : 'none',
      margin: centered ? '0 auto' : undefined
    }}>
      <span className="tw-the">{the} </span>
      <span className="tw-bold">{bold} </span>
      <span className="tw-tail">{tail}</span>
    </h2>
  );
}

/* IntersectionObserver-driven reveal. */
function Reveal({ children, delay = 0, as: As = 'div' }) {
  const ref = useRef(null);
  const [active, setActive] = useState(false);
  useEffect(() => {
    const obs = new IntersectionObserver(
      ([e]) => { if (e.isIntersecting) { setActive(true); obs.disconnect(); } },
      { threshold: 0.12 }
    );
    if (ref.current) obs.observe(ref.current);
    return () => obs.disconnect();
  }, []);
  return (
    <As ref={ref} className={`reveal${active ? ' active' : ''}`} style={{ transitionDelay: `${delay}ms` }}>
      {children}
    </As>
  );
}

/* Animated starscape canvas — sparse, throttled, pauses when not visible. */
function Starscape() {
  const ref = useRef(null);
  useEffect(() => {
    const c = ref.current; if (!c) return;
    const ctx = c.getContext('2d', { alpha: true }); if (!ctx) return;
    let raf, stars = [], lastDraw = 0, running = true;
    const FPS = 20;
    const FRAME_MS = 1000 / FPS;

    const resize = () => {
      // Lower fixed resolution; CSS scales to viewport. Don't apply DPR.
      const W = Math.min(1280, window.innerWidth);
      const H = Math.min(800, window.innerHeight);
      c.width = W; c.height = H;
      c.style.width = window.innerWidth + 'px';
      c.style.height = window.innerHeight + 'px';
      stars = [];
      // Even sparser: 1 star per 18000 px²
      const n = Math.floor((W * H) / 18000);
      for (let i = 0; i < n; i++) {
        stars.push({
          x: Math.random() * W, y: Math.random() * H,
          r: Math.random() * 1.2 + 0.3,
          a: Math.random(),
          s: Math.random() * 0.015 + 0.005,
        });
      }
    };

    const draw = (t) => {
      raf = requestAnimationFrame(draw);
      if (!running) return;
      if (t - lastDraw < FRAME_MS) return;
      lastDraw = t;
      ctx.clearRect(0, 0, c.width, c.height);
      stars.forEach(st => {
        st.a += st.s;
        if (st.a > 1 || st.a < 0.1) st.s = -st.s;
        st.y -= 0.3;
        if (st.y < 0) st.y = c.height;
        ctx.beginPath();
        ctx.arc(st.x, st.y, st.r, 0, Math.PI * 2);
        ctx.fillStyle = `rgba(0,255,204,${st.a * 0.25})`;
        ctx.fill();
      });
    };

    // Pause when document is hidden (tab not focused).
    const visHandler = () => { running = !document.hidden; };

    window.addEventListener('resize', resize);
    document.addEventListener('visibilitychange', visHandler);
    resize(); raf = requestAnimationFrame(draw);
    return () => {
      window.removeEventListener('resize', resize);
      document.removeEventListener('visibilitychange', visHandler);
      cancelAnimationFrame(raf);
    };
  }, []);
  return <canvas id="starscape" ref={ref} />;
}

/* Lucide-like icons (inline SVGs — stroke 2, currentColor). */
const Icon = {
  Sparkles:    () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M9.937 15.5A2 2 0 0 0 8.5 14.063l-6.135-1.582a.5.5 0 0 1 0-.962L8.5 9.936A2 2 0 0 0 9.937 8.5l1.582-6.135a.5.5 0 0 1 .963 0L14.063 8.5A2 2 0 0 0 15.5 9.937l6.135 1.582a.5.5 0 0 1 0 .963L15.5 14.063a2 2 0 0 0-1.437 1.437l-1.582 6.135a.5.5 0 0 1-.963 0z"/></svg>),
  Zap:         () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2"/></svg>),
  ShieldCheck: () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/><path d="m9 12 2 2 4-4"/></svg>),
  TrendingUp:  () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><polyline points="22 7 13.5 15.5 8.5 10.5 2 17"/><polyline points="16 7 22 7 22 13"/></svg>),
  BrainCircuit:() => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M12 5a3 3 0 1 0-5.997.125 4 4 0 0 0-2.526 5.77 4 4 0 0 0 .556 6.588A4 4 0 1 0 12 18Z"/><path d="M12 5a3 3 0 1 1 5.997.125 4 4 0 0 1 2.526 5.77 4 4 0 0 1-.556 6.588A4 4 0 1 1 12 18Z"/></svg>),
  Database:    () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><ellipse cx="12" cy="5" rx="9" ry="3"/><path d="M3 5v14a9 3 0 0 0 18 0V5"/><path d="M3 12a9 3 0 0 0 18 0"/></svg>),
  Lock:        () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><rect x="3" y="11" width="18" height="11" rx="2"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/></svg>),
  Users:       () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M22 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></svg>),
  Code:        () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><polyline points="16 18 22 12 16 6"/><polyline points="8 6 2 12 8 18"/></svg>),
  BarChart:    () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><line x1="12" y1="20" x2="12" y2="10"/><line x1="18" y1="20" x2="18" y2="4"/><line x1="6" y1="20" x2="6" y2="16"/></svg>),
  Workflow:    () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><rect x="3" y="3" width="8" height="8" rx="2"/><rect x="13" y="13" width="8" height="8" rx="2"/><path d="M7 11v4a2 2 0 0 0 2 2h4"/></svg>),
  Cpu:         () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><rect x="4" y="4" width="16" height="16" rx="2"/><rect x="9" y="9" width="6" height="6"/><line x1="9" y1="2" x2="9" y2="4"/><line x1="15" y1="2" x2="15" y2="4"/><line x1="9" y1="20" x2="9" y2="22"/><line x1="15" y1="20" x2="15" y2="22"/><line x1="20" y1="9" x2="22" y2="9"/><line x1="20" y1="14" x2="22" y2="14"/><line x1="2" y1="9" x2="4" y2="9"/><line x1="2" y1="14" x2="4" y2="14"/></svg>),
  Layers:      () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><polyline points="4.5 16.5 12 21 19.5 16.5"/><polyline points="4.5 12 12 16.5 19.5 12"/><polygon points="12 3 19.5 7.5 12 12 4.5 7.5 12 3"/></svg>),
  Rocket:      () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M4.5 16.5c-1.5 1.26-2 5-2 5s3.74-.5 5-2c.71-.84.7-2.13-.09-2.91a2.18 2.18 0 0 0-2.91-.09z"/><path d="M12 15l-3-3a22 22 0 0 1 2-3.95A12.88 12.88 0 0 1 22 2c0 2.72-.78 7.5-6 11a22.35 22.35 0 0 1-4 2z"/><path d="M9 12H4s.55-3.03 2-4c1.62-1.08 5 0 5 0"/><path d="M12 15v5s3.03-.55 4-2c1.08-1.62 0-5 0-5"/></svg>),
  Layout:      () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><rect x="3" y="3" width="18" height="18" rx="2"/><line x1="3" y1="9" x2="21" y2="9"/><line x1="9" y1="21" x2="9" y2="9"/></svg>),
  Globe2:      () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="10"/><line x1="2" y1="12" x2="22" y2="12"/><path d="M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z"/></svg>),
  ChevronRight:() => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><polyline points="9 18 15 12 9 6"/></svg>),
  X:           () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>),
  Whatsapp:    () => (<svg viewBox="0 0 24 24" fill="currentColor"><path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 0 1-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 0 1-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 0 1 2.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0 0 12.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 0 0 5.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893A11.821 11.821 0 0 0 20.464 3.488"/></svg>),
  Menu:        () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><line x1="3" y1="12" x2="21" y2="12"/><line x1="3" y1="6" x2="21" y2="6"/><line x1="3" y1="18" x2="21" y2="18"/></svg>),
  Search:      () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg>),
  CheckCircle2:() => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg>),
  Mail:        () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/><polyline points="22,6 12,13 2,6"/></svg>),
  DollarSign:  () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><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"/></svg>),
  MessageSquare:() => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>),
  Building2:   () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M6 22V4a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v18Z"/><path d="M6 12H4a2 2 0 0 0-2 2v8h4"/><path d="M18 9h2a2 2 0 0 1 2 2v11h-4"/><path d="M10 6h4"/><path d="M10 10h4"/><path d="M10 14h4"/><path d="M10 18h4"/></svg>),
  Plus:        () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>),
  Minus:       () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><line x1="5" y1="12" x2="19" y2="12"/></svg>),
  Loader:      () => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" style={{ animation: 'spin 1s linear infinite' }}><line x1="12" y1="2" x2="12" y2="6"/><line x1="12" y1="18" x2="12" y2="22"/><line x1="4.93" y1="4.93" x2="7.76" y2="7.76"/><line x1="16.24" y1="16.24" x2="19.07" y2="19.07"/><line x1="2" y1="12" x2="6" y2="12"/><line x1="18" y1="12" x2="22" y2="12"/><line x1="4.93" y1="19.07" x2="7.76" y2="16.24"/><line x1="16.24" y1="7.76" x2="19.07" y2="4.93"/></svg>),
};

Object.assign(window, {
  Logo, PrimaryButton, GlassButton, EyebrowChip, Eyebrow,
  PhaseLabel, TripleHeading, Reveal, Starscape, Icon,
});
