/* global React, HeroPreview, IconArrowRight, IconArrowUpRight, IconSparkles, IconShieldCheck,
   IconTarget, IconBarChart, IconCompass, IconUsers, IconBook, IconStar, IconCheck,
   IconQuote, IconClock, IconLock, IconLayers, IconLightning, SITE */

const HOW_ICON = {
  Compass:  <IconCompass size={20} />,
  BarChart: <IconBarChart size={20} />,
  Book:     <IconBook size={20} />,
  Users:    <IconUsers size={20} />,
};

// ============================================================
// HERO
// ============================================================
const Hero = () => {
  return (
    <section style={{ position: 'relative', overflow: 'hidden' }}>
      {/* Ambient warm gradients */}
      <div aria-hidden style={{
        position: 'absolute', inset: 0, pointerEvents: 'none', zIndex: 0,
      }}>
        <div className="ambient-1" style={{
          position: 'absolute', top: -160, right: -120, width: 520, height: 520,
          background: 'radial-gradient(circle, color-mix(in oklab, var(--color-primary) 22%, transparent), transparent 65%)',
          filter: 'blur(40px)',
        }} />
        <div className="ambient-2" style={{
          position: 'absolute', bottom: -200, left: -160, width: 600, height: 600,
          background: 'radial-gradient(circle, color-mix(in oklab, #C99A3F 18%, transparent), transparent 65%)',
          filter: 'blur(50px)',
        }} />
      </div>

      <div className="container" style={{ position: 'relative', zIndex: 1, padding: '80px 32px 96px' }}>
        <div className="hero-grid" style={{
          display: 'grid',
          gridTemplateColumns: '1.15fr 1fr',
          gap: 80,
          alignItems: 'center',
        }}>
          <div className="hero-copy fade-up">
            <div style={{
              display: 'inline-flex', alignItems: 'center', gap: 8,
              padding: '6px 14px 6px 8px',
              background: 'var(--color-canvas)',
              border: '1px solid var(--color-hairline)',
              borderRadius: 100,
              marginBottom: 28,
            }}>
              <span style={{
                width: 22, height: 22, borderRadius: 100,
                background: 'var(--color-primary-soft)',
                color: 'var(--color-primary)',
                display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
              }}>
                <IconSparkles size={11} />
              </span>
              <span style={{
                fontFamily: 'var(--font-sans)', fontSize: 12, fontWeight: 500,
                color: 'var(--color-body)',
              }}>
                Guidance open for 2026 JoSAA &amp; BITSAT cycle
              </span>
            </div>

            <h1 className="display-mega hero-headline" style={{ marginBottom: 26, maxWidth: 640 }}>
              Stop guessing your<br />
              college{' '}
              <span className="serif-italic" style={{ color: 'var(--color-primary)' }}>choices.</span>
            </h1>

            <p className="body-lg" style={{ maxWidth: 520, marginBottom: 36, fontSize: 19 }}>
              Personalised college guidance for JEE Main, JEE Advanced and BITSAT students &mdash; built by IIT and BITS alumni who&apos;ve been through it.
            </p>

            <div style={{ display: 'flex', gap: 12, flexWrap: 'wrap' }}>
              <a href="#/counselling" className="btn btn-primary btn-lg">
                Get college guidance
                <IconArrowRight size={16} />
              </a>
              <a href="#how" className="btn btn-secondary btn-lg">
                How it works
              </a>
            </div>

            <div style={{
              marginTop: 44, display: 'flex', alignItems: 'center', gap: 16, flexWrap: 'wrap',
            }}>
              <div style={{ display: 'flex', alignItems: 'center' }}>
                {[1, 2, 3, 4].map((i) => (
                  <div key={i} style={{
                    width: 32, height: 32, borderRadius: 100,
                    background: ['#C99A3F', '#B8541A', '#4F7C5A', '#27201A'][i - 1],
                    border: '2px solid var(--color-canvas)',
                    marginLeft: i === 1 ? 0 : -10,
                    color: 'white', fontFamily: 'var(--font-sans)', fontSize: 11, fontWeight: 600,
                    display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
                  }}>
                    {['RL', 'AA', 'BA', 'MC'][i - 1]}
                  </div>
                ))}
              </div>
              <div className="caption" style={{ fontSize: 13 }}>
                Guided by IIT &amp; BITS alumni
              </div>
            </div>
          </div>

          <div className="hero-preview-col" style={{ display: 'flex', justifyContent: 'center', paddingRight: 28 }}>
            <HeroPreview />
          </div>
        </div>
      </div>

      <style>{`
        @media (max-width: 980px) {
          .hero-grid { grid-template-columns: 1fr !important; gap: 80px !important; }
          .hero-preview-col { padding-right: 0 !important; padding-top: 24px; }
          .hero-headline { font-size: 64px !important; line-height: 1 !important; }
        }
        @media (max-width: 640px) {
          .hero-headline { font-size: 48px !important; letter-spacing: -1.5px !important; }
        }
      `}</style>
    </section>
  );
};

// ============================================================
// STATS BAND
// ============================================================
const useCountUp = (target, duration = 1600, startWhen = true) => {
  const [val, setVal] = React.useState(0);
  React.useEffect(() => {
    if (!startWhen) return;
    let raf, start;
    const step = (t) => {
      if (!start) start = t;
      const p = Math.min((t - start) / duration, 1);
      const eased = 1 - Math.pow(1 - p, 3);
      setVal(Math.floor(eased * target));
      if (p < 1) raf = requestAnimationFrame(step);
    };
    raf = requestAnimationFrame(step);
    return () => cancelAnimationFrame(raf);
  }, [target, duration, startWhen]);
  return val;
};

const Stat = ({ value, suffix, label, sub, startWhen }) => {
  const v = useCountUp(value, 1800, startWhen);
  return (
    <div style={{ padding: '8px 0' }}>
      <div className="mono" style={{
        fontSize: 56, fontWeight: 500, lineHeight: 1,
        letterSpacing: '-2px',
        color: 'var(--color-ink)',
        fontVariantNumeric: 'tabular-nums',
      }}>
        {v.toLocaleString('en-IN')}
        <span style={{ color: 'var(--color-primary)', marginLeft: 2 }}>{suffix}</span>
      </div>
      <div style={{
        marginTop: 16,
        fontFamily: 'var(--font-sans)', fontSize: 14, fontWeight: 600,
        color: 'var(--color-ink)',
      }}>
        {label}
      </div>
      <div style={{
        fontFamily: 'var(--font-sans)', fontSize: 13, color: 'var(--color-body)',
        marginTop: 4,
      }}>
        {sub}
      </div>
    </div>
  );
};

const StatsBand = () => {
  const ref = React.useRef(null);
  const [started, setStarted] = React.useState(false);
  React.useEffect(() => {
    if (!ref.current) return;
    const io = new IntersectionObserver((entries) => {
      entries.forEach((e) => { if (e.isIntersecting) setStarted(true); });
    }, { threshold: 0.3 });
    io.observe(ref.current);
    return () => io.disconnect();
  }, []);

  return (
    <section className="section-soft" ref={ref} style={{ padding: '80px 0', borderTop: '1px solid var(--color-hairline)', borderBottom: '1px solid var(--color-hairline)' }}>
      <div className="container">
        <div className="stats-grid" style={{
          display: 'grid',
          gridTemplateColumns: 'repeat(auto-fit, minmax(200px, 340px))',
          gap: 48,
          justifyContent: 'center',
        }}>
          {SITE.stats.map((s) => (
            <Stat key={s.label} value={s.value} suffix={s.suffix} label={s.label} sub={s.sub} startWhen={started} />
          ))}
        </div>
      </div>
      <style>{`
        @media (max-width: 900px) {
          .stats-grid { grid-template-columns: 1fr 1fr !important; gap: 40px !important; }
        }
        @media (max-width: 520px) {
          .stats-grid { grid-template-columns: 1fr !important; }
        }
      `}</style>
    </section>
  );
};

// ============================================================
// HOW IT WORKS
// ============================================================
const HowItWorks = () => {
  const steps = SITE.howItWorks;

  return (
    <section id="how" className="section">
      <div className="container">
        <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 64, marginBottom: 64, alignItems: 'end' }} className="how-head">
          <div>
            <div className="eyebrow-accent" style={{ marginBottom: 18 }}>How it works</div>
            <h2 className="display-lg" style={{ maxWidth: 540 }}>
              Four steps. No predictor tools, no guesswork,{' '}
              <span className="serif-italic">no surprises.</span>
            </h2>
          </div>
          <div>
            <p className="body-lg" style={{ maxWidth: 420 }}>
              Counselling season is short. Our process is built to give you a clear, defensible plan in seventy‑two hours &mdash; not a list of links to predictors.
            </p>
          </div>
        </div>

        <div className="how-grid" style={{
          display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: 24,
        }}>
          {steps.map((s) => (
            <div key={s.n} className="card" style={{ padding: 28, display: 'flex', flexDirection: 'column', minHeight: 280 }}>
              <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 32 }}>
                <div style={{
                  width: 44, height: 44, borderRadius: 100,
                  background: 'var(--color-surface-strong)',
                  color: 'var(--color-ink)',
                  display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
                }}>{HOW_ICON[s.icon]}</div>
                <span className="mono" style={{ fontSize: 13, color: 'var(--color-muted)' }}>{s.n}</span>
              </div>
              <div className="title-md" style={{ marginBottom: 10, fontSize: 17, lineHeight: 1.3 }}>{s.title}</div>
              <p className="body-sm" style={{ flexGrow: 1 }}>{s.body}</p>
            </div>
          ))}
        </div>
      </div>
      <style>{`
        @media (max-width: 900px) {
          .how-head { grid-template-columns: 1fr !important; gap: 32px !important; }
          .how-grid { grid-template-columns: 1fr 1fr !important; }
        }
        @media (max-width: 560px) {
          .how-grid { grid-template-columns: 1fr !important; }
        }
      `}</style>
    </section>
  );
};

window.Hero = Hero;
window.StatsBand = StatsBand;
window.HowItWorks = HowItWorks;
