/* Home page */

const { useState, useEffect, useMemo } = React;

const Ticker = ({ items }) =>
<div className="ticker">
    <div className="ticker-track">
      {[...items, ...items, ...items].map((t, i) =>
    <span key={i} className="ticker-item">
          {t}
          <span className="ticker-dot" />
        </span>
    )}
    </div>
  </div>;


const Hero = () => {
  const lines = useMemo(() => [
  "$ m2 init --client=you",
  "→ scoping requirements...",
  "→ sketching flows...",
  "→ writing the code you'll love",
  "✓ ready to ship"],
  []);
  const [vis, setVis] = useState(0);
  useEffect(() => {
    const id = setInterval(() => setVis((v) => (v + 1) % (lines.length + 2)), 1400);
    return () => clearInterval(id);
  }, [lines.length]);

  return (
    <section style={{ padding: "140px 0 60px", position: "relative", overflow: "hidden" }}>
      {/* decorative hero backdrop (uploaded asset) — sits behind everything, ignores pointer events */}
      <div
        aria-hidden="true"
        style={{
          position: "absolute", inset: 0, zIndex: 0, pointerEvents: "none",
          backgroundImage: "url('assets/hero-bg.png')",
          backgroundSize: "cover",
          backgroundPosition: "top right",
          backgroundRepeat: "no-repeat",
          opacity: 0.5,
          mixBlendMode: "multiply",
          maskImage: "linear-gradient(to bottom, rgba(0,0,0,1) 40%, rgba(0,0,0,0) 100%)",
          WebkitMaskImage: "linear-gradient(to bottom, rgba(0,0,0,1) 40%, rgba(0,0,0,0) 100%)"
        }}
      />
      <div className="wrap" style={{ position: "relative", zIndex: 1 }}>
        <h1 className="display" style={{
          fontSize: "clamp(56px, 11vw, 180px)",
          margin: 0,
          letterSpacing: "-0.03em", fontFamily: "Roboto"
        }}>
          We build the<br />
          <span style={{ display: "inline-block", position: "relative" }}>
            <span style={{ position: "relative", zIndex: 2 }}>
              software
            </span>
            <span style={{
              position: "absolute", left: 0, right: 0, bottom: "0.06em",
              height: "0.32em", background: "var(--accent)", zIndex: 1,
              transform: "skewX(-8deg)"
            }} />
          </span>{" "}
          <em style={{ fontStyle: "italic", fontWeight: 400 }}>people</em><br />
          actually use.
        </h1>

        <div style={{ display: "grid", gridTemplateColumns: "1.4fr 1fr", gap: 60, marginTop: 60, alignItems: "end" }} className="hero-bottom">
          <div style={{ display: "flex", gap: 12, flexWrap: "wrap" }}>
            <a href="#/work" onClick={(e) => {e.preventDefault();navigate("work");}} className="btn btn-primary">
              See our work
              <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M5 12h14M13 5l7 7-7 7" /></svg>
            </a>
            <a href="#/contact" onClick={(e) => {e.preventDefault();navigate("contact");}} className="btn btn-ghost">
              Start a project
            </a>
          </div>
          <div style={{ fontFamily: "var(--f-mono)", fontSize: 12, background: "var(--ink)", color: "var(--paper)", padding: 18, borderRadius: 10, minHeight: 160 }}>
            <div style={{ display: "flex", gap: 6, marginBottom: 12 }}>
              <span style={{ width: 10, height: 10, borderRadius: 999, background: "#ff5f57" }} />
              <span style={{ width: 10, height: 10, borderRadius: 999, background: "#febc2e" }} />
              <span style={{ width: 10, height: 10, borderRadius: 999, background: "#28c840" }} />
            </div>
            {lines.slice(0, vis).map((l, i) =>
            <div key={i} style={{ color: l.startsWith("✓") ? "var(--accent)" : "var(--paper)", opacity: i < vis ? 1 : 0, lineHeight: 1.7 }}>
                {l}
              </div>
            )}
            {vis > 0 && vis <= lines.length &&
            <span style={{ display: "inline-block", width: 8, height: 14, background: "var(--accent)", verticalAlign: "middle", animation: "blink 0.9s step-end infinite" }} />
            }
          </div>
        </div>
      </div>
      <style>{`
        @media (max-width: 860px) {
          .hero-bottom { grid-template-columns: 1fr !important; gap: 32px !important; }
        }
        @keyframes blink { 50% { opacity: 0; } }
      `}</style>
    </section>);

};

const ServicesGrid = () => {
  const items = [
  {
    tag: "01",
    title: "Web Development",
    body: "Marketing sites, product sites, and custom web apps. Fast, accessible, and built to convert.",
    skills: ["React / Next.js", "CMS integrations", "Core Web Vitals", "SEO foundation"]
  },
  {
    tag: "02",
    title: "App Development",
    body: "Cross-platform mobile apps that feel native. Authentication, payments, offline — done right.",
    skills: ["iOS & Android", "Flutter", "Firebase / Supabase", "App Store launch"]
  },
  {
    tag: "03",
    title: "Game Development",
    body: "2D and 3D games across mobile, web and desktop. From prototype to polished release.",
    skills: ["Unity / Godot", "Gameplay systems", "Multiplayer", "LiveOps"]
  }];

  return (
    <section className="section">
      <div className="wrap">
        <div className="section-head reveal">
          <div>
            <div className="eyebrow" style={{ marginBottom: 18 }}>▸ What we do</div>
            <h2 style={{ fontFamily: "Roboto" }}>Three<br />disciplines,<br />one team.</h2>
          </div>
          <p>We're a compact studio of engineers and designers who ship. Every project runs with a clear scope, a fixed price, and a human on the other end of the thread.</p>
        </div>

        <div style={{ display: "grid", gridTemplateColumns: "repeat(3, 1fr)", gap: 24 }} className="svc-grid">
          {items.map((it, i) =>
          <article key={it.tag} className="reveal" style={{
            background: "var(--paper-2)",
            border: "1px solid var(--line)",
            borderRadius: 18,
            padding: 32,
            display: "flex",
            flexDirection: "column",
            gap: 20,
            minHeight: 420,
            transition: "all 0.3s var(--ease)",
            position: "relative",
            overflow: "hidden",
            transitionDelay: `${i * 80}ms`
          }}
          onMouseEnter={(e) => {e.currentTarget.style.background = "var(--ink)";e.currentTarget.style.color = "var(--paper)";e.currentTarget.querySelectorAll('.svc-shape').forEach((s) => s.style.transform = "scale(1.15)");}}
          onMouseLeave={(e) => {e.currentTarget.style.background = "var(--paper-2)";e.currentTarget.style.color = "";e.currentTarget.querySelectorAll('.svc-shape').forEach((s) => s.style.transform = "");}}>
            
              <div style={{ display: "flex", justifyContent: "space-between", alignItems: "start" }}>
                <span className="mono" style={{ fontSize: 12, letterSpacing: "0.12em", opacity: 0.5 }}>{it.tag} / 03</span>
                <ServiceIcon kind={["web", "app", "game"][i]} height={40} className="svc-shape" style={{ transition: "transform 0.5s var(--ease)" }} />
              </div>
              <h3 className="display" style={{ fontSize: 38, margin: 0, lineHeight: 1 }}>{it.title}</h3>
              <p style={{ fontSize: 15, margin: 0, opacity: 0.8, flex: 1 }}>{it.body}</p>
              <ul style={{ listStyle: "none", padding: 0, margin: 0, display: "flex", flexDirection: "column", gap: 8, fontSize: 13 }}>
                {it.skills.map((s) =>
              <li key={s} style={{ display: "flex", alignItems: "center", gap: 10, borderTop: "1px solid currentColor", borderColor: "rgba(0,0,0,0.08)", paddingTop: 8, opacity: 0.85 }}>
                    <span style={{ width: 6, height: 6, background: "var(--accent)", borderRadius: 999 }} />{s}
                  </li>
              )}
              </ul>
              <a href="#/services" onClick={(e) => {e.preventDefault();navigate("services");}} className="mono" style={{ fontSize: 12, letterSpacing: "0.12em", textTransform: "uppercase", marginTop: 6 }}>
                Learn more →
              </a>
            </article>
          )}
        </div>
      </div>
      <style>{`
        @media (max-width: 980px) { .svc-grid { grid-template-columns: 1fr !important; } }
      `}</style>
    </section>);

};

const ProcessStrip = () => {
  const steps = [
  { n: "01", t: "Scope", d: "Quick call → written brief → fixed price." },
  { n: "02", t: "Design", d: "Clickable prototype before any code is shipped." },
  { n: "03", t: "Build", d: "Weekly demos. No surprises. Real progress." },
  { n: "04", t: "Launch", d: "We stay on for a month after, not a day less." }];

  return (
    <section style={{ padding: "120px 0", background: "var(--ink)", color: "var(--paper)" }}>
      <div className="wrap">
        <div className="section-head" style={{ marginBottom: 48 }}>
          <div>
            <div className="eyebrow" style={{ color: "rgba(246,244,239,0.5)", marginBottom: 18 }}>▸ How we work</div>
            <h2>A process built<br />for momentum.</h2>
          </div>
          <p style={{ color: "rgba(246,244,239,0.7)" }}>
            Most agencies stall between the brief and the build. We don't. 15 days or less from kickoff to launch on a landing page. Two weeks on an SMB site. A month on a full corporate platform.
          </p>
        </div>
        <div style={{ display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: 0, borderTop: "1px solid rgba(246,244,239,0.15)" }} className="proc-grid">
          {steps.map((s, i) =>
          <div key={s.n} className="reveal" style={{
            padding: "32px 24px 32px 0",
            borderRight: i < 3 ? "1px solid rgba(246,244,239,0.15)" : "none",
            paddingLeft: i > 0 ? 24 : 0,
            transitionDelay: `${i * 60}ms`
          }}>
              <div className="mono" style={{ color: "var(--accent)", fontSize: 12, letterSpacing: "0.14em", marginBottom: 24 }}>{s.n}</div>
              <h3 className="display" style={{ fontSize: 42, margin: "0 0 12px", lineHeight: 1 }}>{s.t}</h3>
              <p style={{ color: "rgba(246,244,239,0.7)", fontSize: 14, margin: 0 }}>{s.d}</p>
            </div>
          )}
        </div>
      </div>
      <style>{`
        @media (max-width: 900px) {
          .proc-grid { grid-template-columns: 1fr 1fr !important; }
          .proc-grid > *:nth-child(2) { border-right: none !important; }
          .proc-grid > *:nth-child(3) { border-right: 1px solid rgba(246,244,239,0.15) !important; }
        }
        @media (max-width: 560px) {
          .proc-grid { grid-template-columns: 1fr !important; }
          .proc-grid > * { border-right: none !important; padding-left: 0 !important; border-top: 1px solid rgba(246,244,239,0.15); }
        }
      `}</style>
    </section>);

};

const FeaturedWork = ({ openCase }) => {
  return (
    <section className="section">
      <div className="wrap">
        <div className="section-head reveal">
          <div>
            <div className="eyebrow" style={{ marginBottom: 18 }}>▸ Selected work</div>
            <h2>Recent<br />shipments.</h2>
          </div>
          <p>A loyalty platform for retail, a Portuguese corporate site, and the desktop dashboard that powers it all. Click any to see the full story.</p>
        </div>
        <div style={{ display: "grid", gridTemplateColumns: "2fr 1fr", gap: 20 }} className="fw-grid">
          <window.ProjectCard big p={window.PROJECTS[0]} onClick={() => openCase(window.PROJECTS[0])} />
          <div style={{ display: "grid", gridTemplateRows: "1fr 1fr", gap: 20 }}>
            <window.ProjectCard p={window.PROJECTS[1]} onClick={() => openCase(window.PROJECTS[1])} />
            <window.ProjectCard p={window.PROJECTS[2]} onClick={() => openCase(window.PROJECTS[2])} />
          </div>
        </div>
        <div style={{ display: "flex", justifyContent: "center", marginTop: 40 }}>
          <a href="#/work" onClick={(e) => {e.preventDefault();navigate("work");}} className="btn btn-ghost">
            View all projects
            <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M5 12h14M13 5l7 7-7 7" /></svg>
          </a>
        </div>
      </div>
      <style>{`
        @media (max-width: 900px) { .fw-grid { grid-template-columns: 1fr !important; } }
      `}</style>
    </section>);

};

const HomePage = ({ openCase }) =>
<>
    <Hero />
    <Ticker items={["Web Apps", "Mobile Apps", "Game Dev", "UX / UI", "E-commerce", "Dashboards", "Landing Pages", "CMS"]} />
    <ServicesGrid />
    <ProcessStrip />
    <FeaturedWork openCase={openCase} />
    <CtaBlock />
  </>;


const CtaBlock = () =>
  <section style={{ padding: "120px 0" }}>
    <div className="wrap">
      <div className="reveal cta-card">
        <div>
          <div className="eyebrow" style={{ marginBottom: 16 }}>▸ Ready when you are</div>
          <h3 className="display" style={{ fontSize: "clamp(40px, 6vw, 80px)", margin: 0, lineHeight: 0.95, fontFamily: "Roboto" }}>
            Got an idea?<br />Let's scope it<br /><em style={{ fontStyle: "italic", fontWeight: 400 }}>this week</em>.
          </h3>
        </div>
        <div className="cta-actions">
          <a href="#/contact" onClick={(e) => { e.preventDefault(); navigate("contact"); }} className="btn btn-primary">
            Start a project →
          </a>
          <a href="mailto:support@m2-tech.co.za" className="btn btn-ghost" style={{ borderColor: "var(--ink)" }}>
            support@m2-tech.co.za
          </a>
        </div>
      </div>
    </div>
  </section>;


Object.assign(window, { HomePage, CtaBlock });
