// Homepage sections — used both standalone and inside canvas variations.

const { useEffect: useEffHome, useRef: useRefHome, useState: useStateHome } = React;

/* ---------- Hero copy (variation can swap visual) ---------- */
function HeroSection({
  visual = "A",
  headlineColor,
  motion = true,
  primaryHue = "sage",
  accentHue = "terra",
  orbScale = 1,
  orbPosition = "right",
} = {}) {
  const Visual = visual === "A" ? Hero3D_A : visual === "B" ? Hero3D_B : Hero3D_C;
  const layoutCenter = visual === "C";
  const visualProps = visual === "A" ? { motion, primaryHue, accentHue, orbScale, position: orbPosition } : {};

  return (
    <section style={{
      paddingTop: 80, paddingBottom: 0,
      position: "relative",
      minHeight: "min(820px, 92vh)",
      display: "flex", alignItems: "stretch",
    }}>
      <Visual {...visualProps} />
      <div className="wrap" style={{
        position: "relative", zIndex: 2, width: "100%",
        display: "flex", flexDirection: "column", justifyContent: "center",
        textAlign: layoutCenter ? "center" : "left",
        alignItems: layoutCenter ? "center" : "flex-start",
        paddingTop: 120, paddingBottom: 120,
      }}>
        <div className="reveal" style={{ marginBottom: 28 }}>
          <Eyebrow accent="sage">A boutique BPO for scaling commerce</Eyebrow>
        </div>
        <h1 className="display reveal d1" style={{ maxWidth: layoutCenter ? "16ch" : "13ch", marginBottom: 28, color: headlineColor, position: "relative", zIndex: 3 }}>
          Harmonizing<br/>businesses with<br/>their <em>clients.</em>
        </h1>
        <p className="lede reveal d2" style={{ marginBottom: 40, maxWidth: "44ch" }}>
          Enterprise-grade customer support and sales for brands that haven't reached
          enterprise scale &mdash; yet. Whatever outcome you set as right, becomes our north star.
        </p>
        <div className="reveal d3" style={{ display: "flex", gap: 12, flexWrap: "wrap" }}>
          <a href="contact.html" className="btn btn-primary">
            Let's chat
            <svg className="arr" width="12" height="12" viewBox="0 0 12 12" fill="none">
              <path d="M2 10L10 2M10 2H4M10 2V8" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round"/>
            </svg>
          </a>
          <a href="services.html" className="btn btn-ghost">View our services</a>
        </div>

        {/* Tiny meta strip */}
        <div className="reveal d4" style={{
          marginTop: 80,
          display: "flex", gap: 40, alignItems: "center",
          fontFamily: "var(--mono)", fontSize: 11, letterSpacing: "0.14em",
          textTransform: "uppercase", color: "var(--ink-soft)",
        }}>
          <span><span style={{ color: "var(--sage-deep)" }}>●</span> 24/5 coverage</span>
          <span style={{ opacity: 0.4 }}>—</span>
          <span>Sales + support, one team</span>
          <span style={{ opacity: 0.4 }}>—</span>
          <span>First reply &lt; 2 min</span>
        </div>
      </div>
    </section>
  );
}

/* ---------- Manifesto (quiet luxury statement) ---------- */
function ManifestoSection() {
  return (
    <section style={{ paddingTop: 140, paddingBottom: 140 }}>
      <div className="wrap">
        <div className="section-head">
          <div className="meta reveal">
            <Eyebrow>Our promise</Eyebrow>
          </div>
          <div></div>
        </div>
        <div style={{ display: "grid", gridTemplateColumns: "280px 1fr", gap: 80 }}>
          <div></div>
          <div>
            <p className="reveal" style={{
              fontFamily: "var(--serif)", fontWeight: 300,
              fontSize: "clamp(28px, 3.2vw, 48px)",
              lineHeight: 1.18, letterSpacing: "-0.015em",
              maxWidth: "22ch",
              color: "var(--ink)",
            }}>
              We treat every conversation &mdash; sales or support &mdash; the way
              <em style={{ color: "var(--sage-deep)" }}>&nbsp;you would</em>: with patience,
              precision, and the assumption it's the one they'll remember.
            </p>

            <div className="reveal d2" style={{
              marginTop: 48,
              display: "grid", gridTemplateColumns: "1fr 1fr 1fr 1fr", gap: 32,
              paddingTop: 28, borderTop: "1px solid var(--line)",
            }}>
              <Stat figure="< 2m" label="Avg. first response" />
              <Stat figure="98%" label="CSAT, support partners" />
              <Stat figure="2.4×" label="Pipeline lift, sales partners" />
              <Stat figure="24/5" label="Coverage, both practices" />
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

function Stat({ figure, label }) {
  return (
    <div>
      <div style={{
        fontFamily: "var(--serif)", fontWeight: 300,
        fontSize: "clamp(26px, 2.4vw, 34px)", lineHeight: 1.05, letterSpacing: "-0.01em",
        color: "var(--ink)",
        marginBottom: 8,
      }}>{figure}</div>
      <div style={{
        fontFamily: "var(--mono)", fontSize: 11, letterSpacing: "0.14em",
        textTransform: "uppercase", color: "var(--ink-soft)",
        lineHeight: 1.4,
      }}>{label}</div>
    </div>
  );
}

/* ---------- Services breakdown (4 channels) ---------- */
const SERVICES = [
  {
    id: "email",
    name: "Email",
    tag: "01",
    desc: "Thoughtful, on-brand replies — never a templated wall of text. We write like your founder would.",
    bullets: ["Branded macros", "Same-day SLA", "Order, returns, escalations"],
    accent: "sage",
  },
  {
    id: "chat",
    name: "Live Chat",
    tag: "02",
    desc: "Real humans answering in under 30 seconds during your highest-intent moments — the cart, the checkout, the doubt.",
    bullets: ["< 30s pickup", "Cart recovery", "Pre-sale to post-purchase"],
    accent: "terra",
  },
  {
    id: "voice",
    name: "Voice",
    tag: "03",
    desc: "Phone support that sounds like home. Calm, capable, never reading from a script you didn't approve.",
    bullets: ["Inbound & outbound", "Local accents", "VIP retention calls"],
    accent: "ink",
  },
  {
    id: "social",
    name: "Social & DMs",
    tag: "04",
    desc: "Instagram, TikTok, X, Facebook — answered in the tone your community already loves you for.",
    bullets: ["Comment moderation", "DM triage", "Crisis playbooks"],
    accent: "sage",
  },
];

function ServicesSection() {
  const [active, setActive] = useStateHome(0);
  return (
    <section style={{ background: "var(--cream-warm)", paddingTop: 140, paddingBottom: 140 }}>
      <div className="wrap">
        <div className="section-head">
          <div className="meta reveal">
            <Eyebrow accent="terra">Services — four channels, one voice</Eyebrow>
          </div>
          <div className="reveal d1">
            <h2 className="h2" style={{ maxWidth: "16ch" }}>
              Wherever your customer reaches out, we're already <em>there</em>.
            </h2>
          </div>
        </div>

        <div style={{
          display: "grid", gridTemplateColumns: "minmax(280px, 360px) 1fr", gap: 60,
          alignItems: "start",
        }}>
          {/* Channel list */}
          <div className="reveal" style={{
            borderTop: "1px solid var(--line)",
          }}>
            {SERVICES.map((s, i) => (
              <button key={s.id}
                onClick={() => setActive(i)}
                style={{
                  width: "100%", textAlign: "left", cursor: "pointer",
                  background: "transparent", border: "none",
                  padding: "28px 0",
                  borderBottom: "1px solid var(--line)",
                  display: "flex", alignItems: "center", justifyContent: "space-between",
                  gap: 20,
                  fontFamily: "var(--serif)",
                  fontSize: active === i ? 36 : 28,
                  fontWeight: active === i ? 400 : 300,
                  color: active === i ? "var(--ink)" : "var(--ink-soft)",
                  transition: "all .35s cubic-bezier(.2,.7,.2,1)",
                }}>
                <span style={{ display: "flex", alignItems: "baseline", gap: 18 }}>
                  <span style={{
                    fontFamily: "var(--mono)", fontSize: 11, letterSpacing: "0.14em",
                    color: "var(--ink-soft)",
                  }}>{s.tag}</span>
                  {s.name}
                </span>
                <span style={{
                  width: 8, height: 8, borderRadius: 999,
                  background: active === i
                    ? (s.accent === "sage" ? "var(--sage)" : s.accent === "terra" ? "var(--terra)" : "var(--ink)")
                    : "transparent",
                  border: active === i ? "none" : "1px solid var(--line-strong)",
                  transition: "all .3s",
                }}></span>
              </button>
            ))}
          </div>

          {/* Detail panel */}
          <div className="reveal d1" style={{
            background: "var(--cream)",
            border: "1px solid var(--line)",
            borderRadius: 4,
            padding: 48,
            position: "relative",
            minHeight: 420,
            overflow: "hidden",
          }}>
            <ServicePanel service={SERVICES[active]} />
          </div>
        </div>
      </div>
    </section>
  );
}

function ServicePanel({ service }) {
  return (
    <div key={service.id} style={{
      animation: "fadeUp .5s cubic-bezier(.2,.7,.2,1)",
    }}>
      <style>{`@keyframes fadeUp { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }`}</style>

      <div style={{
        display: "flex", justifyContent: "space-between", alignItems: "flex-start",
        marginBottom: 40,
      }}>
        <Eyebrow accent={service.accent}>Channel · {service.name}</Eyebrow>
        <span style={{
          fontFamily: "var(--serif)", fontSize: 80, lineHeight: 1, fontWeight: 300,
          color: service.accent === "sage" ? "var(--sage)" : service.accent === "terra" ? "var(--terra)" : "var(--ink)",
          opacity: 0.7,
        }}>{service.tag}</span>
      </div>

      <h3 style={{
        fontFamily: "var(--serif)", fontWeight: 300,
        fontSize: "clamp(28px, 3vw, 44px)", lineHeight: 1.1, letterSpacing: "-0.02em",
        maxWidth: "20ch", marginBottom: 28,
      }}>{service.desc}</h3>

      <ul style={{
        listStyle: "none", padding: 0, margin: "40px 0 0",
        display: "grid", gridTemplateColumns: "1fr 1fr 1fr", gap: 20,
        paddingTop: 32, borderTop: "1px solid var(--line)",
      }}>
        {service.bullets.map((b) => (
          <li key={b} style={{
            fontFamily: "var(--mono)", fontSize: 11, letterSpacing: "0.12em",
            textTransform: "uppercase", color: "var(--ink-soft)",
            display: "flex", alignItems: "center", gap: 8,
          }}>
            <svg width="10" height="10" viewBox="0 0 10 10" fill="none">
              <path d="M2 5L4 7L8 3" stroke={service.accent === "sage" ? "var(--sage-deep)" : service.accent === "terra" ? "var(--terra-deep)" : "var(--ink)"} strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"/>
            </svg>
            {b}
          </li>
        ))}
      </ul>

      {/* sample message ui */}
      <div style={{
        marginTop: 40, padding: 24,
        background: "var(--cream-warm)", borderRadius: 4,
        display: "flex", flexDirection: "column", gap: 12,
      }}>
        <SampleBubble who="customer" text={
          service.id === "email" ? "Hi! My order #4821 hasn't arrived and the tracking hasn't updated since Tuesday."
          : service.id === "chat" ? "Will the linen sweater shrink in the wash?"
          : service.id === "voice" ? "I love the dress but it doesn't fit. Is there a way to keep the color?"
          : "obsessed with the new drop 😍 when does the moss green restock??"
        } />
        <SampleBubble who="us" text={
          service.id === "email" ? "Hi Maya — really sorry. I just pulled your record and reshipped a replacement on overnight, no charge. Tracking arrives in 20 min."
          : service.id === "chat" ? "Great question — it's pre-washed, so it'll soften but won't shrink past 2%. Cool wash, lay flat to dry. ✓"
          : service.id === "voice" ? "Totally — we can swap to a size up in the same Sage. I'll set up the return label and reserve the new one right now."
          : "you have impeccable taste 💚 moss green lands April 30 — want me to drop you a DM the second it's live?"
        } accent={service.accent} />
      </div>
    </div>
  );
}

function SampleBubble({ who, text, accent }) {
  const isUs = who === "us";
  const accentColor = accent === "sage" ? "var(--sage)" : accent === "terra" ? "var(--terra)" : "var(--ink)";
  return (
    <div style={{
      alignSelf: isUs ? "flex-end" : "flex-start",
      maxWidth: "78%",
      padding: "12px 16px",
      borderRadius: 10,
      borderTopLeftRadius: isUs ? 10 : 2,
      borderTopRightRadius: isUs ? 2 : 10,
      background: isUs ? "var(--ink)" : "var(--cream)",
      color: isUs ? "var(--cream)" : "var(--ink)",
      border: isUs ? "none" : "1px solid var(--line)",
      fontSize: 14, lineHeight: 1.5,
      position: "relative",
    }}>
      {isUs && <span style={{
        position: "absolute", left: -3, top: -3,
        width: 8, height: 8, borderRadius: 999, background: accentColor,
      }}></span>}
      {text}
    </div>
  );
}

/* ---------- Industries marquee ---------- */
function IndustriesSection() {
  const items = [
    "Apparel", "Footwear", "Skincare", "Beauty", "Home Goods",
    "Jewelry", "Wellness", "Supplements", "Coffee", "Pet",
    "Kitchen", "Outdoor", "Eyewear",
  ];
  const row = items.concat(items);

  return (
    <section style={{ paddingTop: 140, paddingBottom: 140 }}>
      <div className="wrap">
        <div className="section-head">
          <div className="meta reveal">
            <Eyebrow>Who we serve</Eyebrow>
          </div>
          <div className="reveal d1">
            <h2 className="h2" style={{ maxWidth: "20ch" }}>
              We work best with founders who've outgrown <em>"can someone reply to support today?"</em>
            </h2>
            <p className="lede" style={{ marginTop: 24 }}>
              Direct-to-consumer brands between $1M and $50M in annual revenue — scaling fast,
              still small enough to care about every reply.
            </p>
          </div>
        </div>
      </div>

      <div className="marquee reveal" style={{ marginTop: 40 }}>
        <div className="marquee-track">
          <span>{row.map((x, i) => (
            <span key={i} style={{ display: "inline-flex", alignItems: "center", gap: 80 }}>
              {x}
              <span className="star">✦</span>
            </span>
          ))}</span>
        </div>
      </div>

      {/* Tier callouts */}
      <div className="wrap" style={{ marginTop: 100 }}>
        <div style={{
          display: "grid", gridTemplateColumns: "1fr 1fr 1fr", gap: 1,
          background: "var(--line)", border: "1px solid var(--line)",
        }}>
          <Tier
            range="$1M — $5M"
            title="Founder-led brands"
            body="You're still answering tickets at midnight. Hand it off to a team that sounds like you, only awake."
          />
          <Tier
            range="$5M — $20M"
            title="Scaling teams"
            body="You have a CX lead and a backlog. We slot in as the pod that actually closes tickets, not just triages them."
            accent="terra"
          />
          <Tier
            range="$20M — $50M"
            title="Brand stewards"
            body="You've felt the cost of generic offshore. We're the boutique alternative — measured by the conversations you'd be proud of."
            accent="sage"
          />
        </div>
      </div>
    </section>
  );
}

function Tier({ range, title, body, accent }) {
  return (
    <div className="reveal" style={{
      background: "var(--cream)", padding: "48px 36px",
      display: "flex", flexDirection: "column", gap: 16,
      minHeight: 280,
    }}>
      <div style={{
        fontFamily: "var(--mono)", fontSize: 11, letterSpacing: "0.16em",
        textTransform: "uppercase",
        color: accent === "sage" ? "var(--sage-deep)" : accent === "terra" ? "var(--terra-deep)" : "var(--ink-soft)",
      }}>{range}</div>
      <h3 className="h3">{title}</h3>
      <p style={{ color: "var(--ink-soft)", fontSize: 15, lineHeight: 1.55, marginTop: "auto" }}>{body}</p>
    </div>
  );
}

/* ---------- Closing CTA ---------- */
function ClosingCTA() {
  return (
    <section style={{
      background: "var(--ink)", color: "var(--cream)",
      paddingTop: 140, paddingBottom: 140, position: "relative", overflow: "hidden",
    }}>
      {/* subtle ring */}
      <svg style={{ position: "absolute", right: -200, top: -200, opacity: 0.18 }} width="800" height="800" viewBox="0 0 800 800">
        <circle cx="400" cy="400" r="200" fill="none" stroke="var(--sage)" strokeWidth="1"/>
        <circle cx="400" cy="400" r="280" fill="none" stroke="var(--sage)" strokeWidth="1" strokeDasharray="2 6"/>
        <circle cx="400" cy="400" r="360" fill="none" stroke="var(--terra)" strokeWidth="1"/>
      </svg>

      <div className="wrap" style={{ position: "relative" }}>
        <div className="reveal" style={{ marginBottom: 28 }}>
          <Eyebrow accent="terra">Next step</Eyebrow>
        </div>
        <h2 className="reveal d1" style={{
          fontFamily: "var(--serif)", fontWeight: 300,
          fontSize: "clamp(48px, 6.4vw, 110px)", lineHeight: 0.98, letterSpacing: "-0.025em",
          color: "var(--cream)", maxWidth: "16ch", marginBottom: 48,
        }}>
          A 30-minute conversation. <em style={{ color: "var(--terra)" }}>Zero</em> sales theatre.
        </h2>
        <div className="reveal d2" style={{ display: "flex", gap: 12, flexWrap: "wrap" }}>
          <a href="contact.html" className="btn" style={{ background: "var(--cream)", color: "var(--ink)" }}>
            Let's chat
            <svg width="12" height="12" viewBox="0 0 12 12" fill="none">
              <path d="M2 10L10 2M10 2H4M10 2V8" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round"/>
            </svg>
          </a>
          <a href="services.html" className="btn" style={{ background: "transparent", color: "var(--cream)", border: "1px solid rgba(248,249,250,0.3)" }}>
            See what we do
          </a>
        </div>
      </div>
    </section>
  );
}

/* ---------- Service categories (homepage) — two big cards ---------- */
function ServiceCategoriesSection() {
  return (
    <section style={{ background: "var(--cream-warm)", paddingTop: 140, paddingBottom: 140 }}>
      <div className="wrap">
        <div className="section-head">
          <div className="meta reveal">
            <Eyebrow accent="terra">Services</Eyebrow>
          </div>
          <div className="reveal d1">
            <h2 className="h2" style={{ maxWidth: "20ch" }}>
              Three boutique practices. <em>One</em> standard of care.
            </h2>
            <p className="lede" style={{ marginTop: 24 }}>
              We run three specialized teams under the same roof — so the voice that closes the sale,
              the one that handles the ticket weeks later, and the right hand running your week
              all belong to the same brand.
            </p>
          </div>
        </div>

        <div className="reveal d2" style={{
          display: "grid", gridTemplateColumns: "repeat(3, 1fr)", gap: 24,
        }}>
          <CategoryCard
            href="customer-support.html"
            kicker="Practice 01 · Foundational"
            title="Customer Support"
            blurb="The voice your customers hear after the sale. Email, live chat, voice, and social — answered with patience, precision, and the assumption every conversation is the one they'll remember."
            channels={["Email", "Live Chat", "Voice", "Social & DMs"]}
            accent="sage"
          />
          <CategoryCard
            href="sales.html"
            kicker="Practice 02 · Growth-driving"
            title="Sales"
            blurb="A boutique sales team in a different time zone. Inbound, outbound, and full-cycle experts who learn your product like operators and sell like consultants — never closers reading a script."
            channels={["Inbound", "Outbound", "Full-Cycle"]}
            accent="terra"
          />
          <CategoryCard
            href="virtual-assistants.html"
            kicker="Practice 03 · Operational"
            title="Virtual Assistants"
            blurb="A remote executive assistant who learns your week like a chief of staff. An online secretary who keeps the back office quiet so the front of house can run — one dedicated person, time-zone matched."
            channels={["Executive Assistant", "Online Secretary", "Operations"]}
            accent="ink"
          />
        </div>
      </div>
    </section>
  );
}

function CategoryCard({ href, kicker, title, blurb, channels, accent }) {
  const [hover, setHover] = useStateHome(false);
  const accentColor = accent === "terra" ? "var(--terra)" : accent === "ink" ? "var(--ink)" : "var(--sage)";
  const accentDeep = accent === "terra" ? "var(--terra-deep)" : accent === "ink" ? "var(--ink)" : "var(--sage-deep)";
  return (
    <a href={href}
      onMouseEnter={() => setHover(true)}
      onMouseLeave={() => setHover(false)}
      style={{
        background: "var(--cream)",
        border: "1px solid var(--line)",
        padding: 48,
        display: "flex", flexDirection: "column", gap: 0,
        minHeight: 480,
        transition: "all .35s cubic-bezier(.2,.7,.2,1)",
        transform: hover ? "translateY(-4px)" : "none",
        boxShadow: hover ? "0 24px 60px rgba(47,62,70,0.08)" : "0 0 0 rgba(0,0,0,0)",
        position: "relative", overflow: "hidden",
      }}>
      {/* decorative orb */}
      <div style={{
        position: "absolute", right: -60, top: -60, opacity: 0.85,
        transition: "transform .8s cubic-bezier(.2,.7,.2,1)",
        transform: hover ? "translate(-12px, 12px) scale(1.05)" : "none",
      }}>
        <Orb size={200} x="0" y="0" hue={accent === "terra" ? "terra" : accent === "ink" ? "ink" : "sage"} depth={0} />
      </div>

      <div style={{
        fontFamily: "var(--mono)", fontSize: 11, letterSpacing: "0.16em",
        textTransform: "uppercase", color: accentDeep, marginBottom: 24,
        position: "relative", zIndex: 2,
      }}>{kicker}</div>

      <h3 style={{
        fontFamily: "var(--serif)", fontWeight: 300,
        fontSize: "clamp(40px, 4.4vw, 64px)", lineHeight: 1, letterSpacing: "-0.02em",
        marginBottom: 28, position: "relative", zIndex: 2,
      }}>{title}</h3>

      <p style={{
        fontSize: 16, lineHeight: 1.6, color: "var(--ink-soft)", margin: 0,
        maxWidth: "44ch", marginBottom: 36, position: "relative", zIndex: 2,
      }}>{blurb}</p>

      <ul style={{
        listStyle: "none", padding: 0, margin: 0,
        display: "flex", gap: 8, flexWrap: "wrap",
        marginBottom: 32, marginTop: "auto",
        position: "relative", zIndex: 2,
      }}>
        {channels.map((c) => (
          <li key={c} style={{
            fontFamily: "var(--mono)", fontSize: 11, letterSpacing: "0.12em",
            textTransform: "uppercase", color: "var(--ink-soft)",
            padding: "6px 12px", borderRadius: 999,
            border: "1px solid var(--line)",
            background: "var(--cream-warm)",
          }}>{c}</li>
        ))}
      </ul>

      <div style={{
        display: "flex", alignItems: "center", gap: 10,
        fontFamily: "var(--sans)", fontSize: 14, fontWeight: 500,
        color: "var(--ink)",
        position: "relative", zIndex: 2,
        paddingTop: 28, borderTop: "1px solid var(--line)",
      }}>
        Learn more
        <svg width="12" height="12" viewBox="0 0 12 12" fill="none" style={{
          transform: hover ? "translate(3px, -3px)" : "none",
          transition: "transform .25s",
        }}>
          <path d="M2 10L10 2M10 2H4M10 2V8" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round"/>
        </svg>
      </div>
    </a>
  );
}

Object.assign(window, {
  HeroSection, ManifestoSection, ServicesSection, ServiceCategoriesSection,
  IndustriesSection, ClosingCTA,
});
