// =============================================================
// Design Brains — Services Pages
// =============================================================

function ServicesPage() {
  const list = [SERVICES.web, SERVICES.app, SERVICES.ai];
  return (
    <div className="page-enter">
      <PageHero
        eyebrow="Services · What we do"
        title={<>Three practices, built around <span style={{ color: "var(--brand)" }}>shipping</span>.</>}
        subtitle="We organise the studio around three disciplines — websites, apps, and AI automation. Most projects pull from two of them. The good ones pull from all three."
      >
        <div style={{ display: "flex", gap: 12, flexWrap: "wrap" }}>
          <Button to="/quote">Start a project →</Button>
          <Button variant="ghost" to="/contact">Book a call</Button>
        </div>
      </PageHero>

      <section style={{ paddingTop: 40 }}>
        <div className="container">
          <div style={{ display: "flex", flexDirection: "column", gap: 20 }}>
            {list.map((s, i) => (
              <Reveal key={s.slug} delay={i * 60}>
                <Link to={`/services/${s.slug}`}>
                  <div className="card card-glow" style={{ padding: 0, overflow: "hidden" }}>
                    <div style={{ display: "grid", gridTemplateColumns: "1.4fr 1fr", gap: 0 }} className="svc-row">
                      <div style={{ padding: "48px clamp(28px, 4vw, 56px)" }}>
                        <div style={{ display: "flex", alignItems: "center", gap: 16, marginBottom: 24 }}>
                          <ServiceIcon kind={s.icon} size={44} />
                          <div className="font-mono" style={{ color: "var(--muted)", fontSize: 12, letterSpacing: "0.16em" }}>0{i + 1} / 03</div>
                        </div>
                        <h2 className="font-display" style={{ fontSize: "clamp(32px, 4vw, 48px)", letterSpacing: "-0.03em", marginBottom: 12 }}>
                          {s.name}
                        </h2>
                        <p className="lead" style={{ marginBottom: 28 }}>{s.description}</p>
                        <div style={{ color: "var(--brand)", fontSize: 14, fontWeight: 600, display: "inline-flex", alignItems: "center", gap: 8 }}>
                          Explore {s.name.toLowerCase()} <span className="arrow">→</span>
                        </div>
                      </div>
                      <div style={{
                        background: "linear-gradient(135deg, rgba(108,191,0,0.06), transparent)",
                        borderLeft: "1px solid var(--border)",
                        padding: "48px clamp(28px, 4vw, 56px)",
                        display: "flex", flexDirection: "column", gap: 14,
                      }}>
                        <div className="font-mono" style={{ color: "var(--muted)", fontSize: 12, letterSpacing: "0.16em", textTransform: "uppercase", marginBottom: 4 }}>
                          Includes
                        </div>
                        {s.subServices.map((sub) => (
                          <div key={sub.slug} style={{ display: "flex", gap: 12, alignItems: "flex-start" }}>
                            <div style={{ width: 5, height: 5, borderRadius: "50%", background: "var(--brand)", marginTop: 9, flexShrink: 0 }}/>
                            <div>
                              <div style={{ fontSize: 15, fontWeight: 600 }}>{sub.name}</div>
                            </div>
                          </div>
                        ))}
                      </div>
                    </div>
                  </div>
                </Link>
              </Reveal>
            ))}
          </div>
          <style>{`
            @media (max-width: 880px) { .svc-row { grid-template-columns: 1fr !important; } .svc-row > div:nth-child(2) { border-left: none !important; border-top: 1px solid var(--border) !important; } }
          `}</style>
        </div>
      </section>
    </div>
  );
}

// -------- Service Detail Page --------
function ServiceDetailPage({ slug, subSlug }) {
  const s = SERVICES[slug];
  if (!s) return <NotFound />;
  const sub = subSlug ? s.subServices.find((x) => x.slug === subSlug) : null;

  const heroTitle = sub ? sub.name : s.name;
  const heroSubtitle = sub ? (sub.long || sub.desc) : s.description;

  return (
    <div className="page-enter">
      <PageHero
        eyebrow={<>
          <Link to="/services" style={{ color: "var(--brand)" }}>Services</Link>
          <span style={{ color: "var(--muted)", margin: "0 8px" }}>/</span>
          {sub ? <>
            <Link to={`/services/${slug}`} style={{ color: "var(--brand)" }}>{s.name}</Link>
            <span style={{ color: "var(--muted)", margin: "0 8px" }}>/</span>
            {sub.name}
          </> : s.name}
        </>}
        title={<>{heroTitle}<span style={{ color: "var(--brand)" }}>.</span></>}
        subtitle={heroSubtitle}
      >
        <div style={{ display: "flex", gap: 12, flexWrap: "wrap" }}>
          <Button to="/quote">Request a quote →</Button>
          <Button variant="ghost" to="/contact">Talk to us</Button>
        </div>
      </PageHero>

      {/* Sub-services strip (only if not on a sub-page) */}
      {!sub && (
        <section style={{ paddingTop: 40 }}>
          <div className="container">
            <SectionHeader eyebrow="Sub-practices" title={`Inside ${s.name.toLowerCase()}.`} />
            <div style={{ display: "grid", gridTemplateColumns: "repeat(auto-fill, minmax(280px, 1fr))", gap: 20 }}>
              {s.subServices.map((sb, i) => (
                <Reveal key={sb.slug} delay={i * 50}>
                  <Link to={`/services/${slug}/${sb.slug}`}>
                    <div className="card card-glow" style={{ height: "100%" }}>
                      <div className="font-mono" style={{ color: "var(--muted)", fontSize: 12, letterSpacing: "0.12em", marginBottom: 16 }}>0{i + 1}</div>
                      <h3 className="h-card" style={{ marginBottom: 10 }}>{sb.name}</h3>
                      <p style={{ color: "var(--fg-2)", margin: 0, fontSize: 14, marginBottom: 20 }}>{sb.desc}</p>
                      <div style={{ color: "var(--brand)", fontSize: 13, fontWeight: 600 }}>Learn more →</div>
                    </div>
                  </Link>
                </Reveal>
              ))}
            </div>
          </div>
        </section>
      )}

      {sub && <SubServiceDetail sub={sub} parent={s} />}
      <ServiceFeatures s={s} />
      <ServiceProcess />
      <ServiceTools s={s} />
      <ServiceFAQ slug={slug} />
      <ServiceQuoteForm s={s} />
    </div>
  );
}

function SubServiceDetail({ sub, parent }) {
  return (
    <section style={{ paddingTop: 40 }}>
      <div className="container">
        <div style={{ display: "grid", gridTemplateColumns: "1.4fr 1fr", gap: 48, alignItems: "start" }} className="sub-grid">
          <div>
            <SectionHeader eyebrow="What's included" title={`Inside a ${sub.name.toLowerCase()} engagement.`} />
            <div style={{ display: "flex", flexDirection: "column", gap: 0 }}>
              {sub.deliverables.map((d, i) => (
                <Reveal key={i} delay={i * 50}>
                  <div style={{ display: "flex", gap: 16, alignItems: "flex-start", padding: "20px 0", borderTop: "1px solid var(--border)" }}>
                    <div className="font-mono" style={{ color: "var(--muted)", fontSize: 12, paddingTop: 4, minWidth: 32 }}>
                      0{i + 1}
                    </div>
                    <div style={{ fontFamily: "DM Sans", fontSize: 17, fontWeight: 500, letterSpacing: "-0.01em", lineHeight: 1.4 }}>{d}</div>
                  </div>
                </Reveal>
              ))}
              <div style={{ borderTop: "1px solid var(--border)" }}/>
            </div>
          </div>
          <Reveal delay={120}>
            <div className="card" style={{ padding: 32, position: "sticky", top: 120 }}>
              <div className="eyebrow" style={{ marginBottom: 24 }}>At a glance</div>
              <div style={{ display: "grid", gap: 24 }}>
                <div>
                  <div className="font-mono" style={{ fontSize: 12, color: "var(--muted)", letterSpacing: "0.12em", textTransform: "uppercase", marginBottom: 6 }}>Timeline</div>
                  <div className="font-display" style={{ fontSize: 24, fontWeight: 600 }}>{sub.timeline}</div>
                </div>
                <div>
                  <div className="font-mono" style={{ fontSize: 12, color: "var(--muted)", letterSpacing: "0.12em", textTransform: "uppercase", marginBottom: 6 }}>Investment</div>
                  <div className="font-display" style={{ fontSize: 24, fontWeight: 600, color: "var(--brand)" }}>{sub.priceFrom}</div>
                </div>
                <div>
                  <div className="font-mono" style={{ fontSize: 12, color: "var(--muted)", letterSpacing: "0.12em", textTransform: "uppercase", marginBottom: 6 }}>Practice</div>
                  <Link to={`/services/${parent.slug}`} style={{ fontSize: 16, fontWeight: 500, color: "var(--brand)" }}>{parent.name} →</Link>
                </div>
                <div style={{ borderTop: "1px solid var(--border)", paddingTop: 20, marginTop: 4 }}>
                  <Button to="/quote">Request a quote →</Button>
                </div>
              </div>
            </div>
          </Reveal>
        </div>

        {/* Sibling navigation */}
        <div style={{ marginTop: 80 }}>
          <div className="font-mono" style={{ fontSize: 12, color: "var(--muted)", letterSpacing: "0.16em", textTransform: "uppercase", marginBottom: 20 }}>Other {parent.name.toLowerCase()} services</div>
          <div style={{ display: "grid", gridTemplateColumns: "repeat(auto-fill, minmax(240px, 1fr))", gap: 12 }}>
            {parent.subServices.filter(x => x.slug !== sub.slug).map((sb) => (
              <Link key={sb.slug} to={`/services/${parent.slug}/${sb.slug}`}>
                <div className="card" style={{ padding: 20 }}>
                  <div style={{ fontSize: 15, fontWeight: 600, marginBottom: 4 }}>{sb.name}</div>
                  <div style={{ color: "var(--brand)", fontSize: 13, fontFamily: "JetBrains Mono, monospace" }}>View →</div>
                </div>
              </Link>
            ))}
          </div>
        </div>
        <style>{`@media (max-width: 880px) { .sub-grid { grid-template-columns: 1fr !important; } .sub-grid > div:last-child > div { position: static !important; } }`}</style>
      </div>
    </section>
  );
}

function ServiceFeatures({ s }) {
  return (
    <section>
      <div className="container">
        <SectionHeader
          eyebrow="What you get"
          title="Built-in by default."
          subtitle="Every project includes these — they aren't add-ons. We've shipped enough to know what 'good' looks like."
        />
        <div style={{ display: "grid", gridTemplateColumns: "repeat(2, 1fr)", gap: 20 }} className="feat-grid">
          {s.features.map((f, i) => (
            <Reveal key={i} delay={i * 40}>
              <div style={{ display: "flex", gap: 16, alignItems: "flex-start", padding: "20px 0", borderTop: i < 2 ? "none" : "1px solid var(--border)" }}>
                <div style={{
                  width: 32, height: 32, borderRadius: 10, flexShrink: 0,
                  background: "var(--brand-faint)", border: "1px solid rgba(108,191,0,0.2)",
                  display: "grid", placeItems: "center", color: "var(--brand)",
                }}>
                  <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><path d="M2 7L6 11L12 3" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/></svg>
                </div>
                <div style={{ paddingTop: 4 }}>
                  <div style={{ fontFamily: "DM Sans", fontSize: 18, fontWeight: 600, letterSpacing: "-0.01em" }}>{f}</div>
                </div>
              </div>
            </Reveal>
          ))}
        </div>
        <style>{`@media (max-width: 720px) { .feat-grid { grid-template-columns: 1fr !important; } }`}</style>
      </div>
    </section>
  );
}

function ServiceProcess() {
  const steps = [
    { n: "01", t: "Discovery", d: "Workshop, audit, and a written success criteria document. ~1 week." },
    { n: "02", t: "Design", d: "Wireframes, prototype, and brand-aligned visual direction. 1-3 weeks." },
    { n: "03", t: "Build", d: "Weekly demos, weekly invoices, daily Slack. 2-12 weeks depending on scope." },
    { n: "04", t: "Launch & care", d: "Smooth go-live, 30-day support window, optional ongoing care plan." },
  ];
  return (
    <section style={{ background: "var(--bg-2)", borderTop: "1px solid var(--border)", borderBottom: "1px solid var(--border)" }}>
      <div className="container">
        <SectionHeader eyebrow="Engagement" title="Four phases. No mystery." />
        <div style={{ display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: 16 }} className="proc4-grid">
          {steps.map((st, i) => (
            <Reveal key={st.n} delay={i * 50}>
              <div className="card" style={{ height: "100%" }}>
                <div className="font-mono" style={{ color: "var(--brand)", fontSize: 13, marginBottom: 16 }}>{st.n}</div>
                <h3 className="h-card" style={{ marginBottom: 10 }}>{st.t}</h3>
                <p style={{ color: "var(--fg-2)", margin: 0, fontSize: 14 }}>{st.d}</p>
              </div>
            </Reveal>
          ))}
        </div>
        <style>{`
          @media (max-width: 880px) { .proc4-grid { grid-template-columns: 1fr 1fr !important; } }
          @media (max-width: 500px) { .proc4-grid { grid-template-columns: 1fr !important; } }
        `}</style>
      </div>
    </section>
  );
}

function ServiceTools({ s }) {
  return (
    <section>
      <div className="container">
        <SectionHeader eyebrow="Tooling" title="The stack we'll likely reach for." subtitle="Not religious about any of it — but these are the tools that earn their keep on this kind of project." />
        <div style={{ display: "flex", flexWrap: "wrap", gap: 12 }}>
          {s.tools.map((t) => (
            <span key={t} className="chip" style={{ padding: "10px 18px", fontSize: 14 }}>{t}</span>
          ))}
        </div>
      </div>
    </section>
  );
}

function ServiceFAQ({ slug }) {
  const FAQS = {
    web: [
      { q: "Do you build on a CMS or hand-code?", a: "Both — depends on the team behind the site. If you'll edit content weekly, we'll set up a headless CMS (usually Sanity or Payload). If it's mostly static, plain Next.js or Astro is faster and simpler." },
      { q: "Can you migrate from WordPress / Wix / Webflow?", a: "Yes, we do migrations regularly. We preserve URLs, redirect what we can't, and run a content QA pass before launch." },
      { q: "How fast will the site be?", a: "We target 95+ on Lighthouse and a sub-1s Largest Contentful Paint as a baseline. We'll show you the audit before launch." },
    ],
    app: [
      { q: "Native or cross-platform?", a: "We pick based on your team and roadmap. If you have iOS-only audience or hardware-heavy needs, native is usually right. For most B2C, React Native gets you 90% of the polish at 60% of the cost." },
      { q: "Will you handle App Store / Play Store submission?", a: "Yes — including screenshots, descriptions, and the back-and-forth with reviewers. We've shipped 30+ apps without a rejection we couldn't fix in 24 hours." },
      { q: "Can you take over an existing app?", a: "Often yes — we'll do a code audit first to make sure the codebase is salvageable. If it isn't, we'll say so." },
    ],
    ai: [
      { q: "Which model do you use?", a: "Whichever fits — we work with Anthropic, OpenAI, and open-weights via self-hosting. We benchmark on your data before recommending." },
      { q: "What about data privacy?", a: "We support private deployment, on-prem inference, and zero-retention API tiers. We'll match your compliance posture, not the other way around." },
      { q: "How do you avoid hallucinations?", a: "Eval suites before launch, retrieval-grounded prompts, and human-in-the-loop on anything customer-facing. We measure error rates and have rollback plans for every flow." },
    ],
  };
  const items = FAQS[slug] || [];
  const [open, setOpen] = useState(0);
  return (
    <section>
      <div className="container">
        <SectionHeader eyebrow="FAQ" title="Common questions, answered." />
        <div style={{ maxWidth: 820 }}>
          {items.map((it, i) => (
            <Reveal key={i} delay={i * 50}>
              <div onClick={() => setOpen(open === i ? -1 : i)} style={{ borderTop: "1px solid var(--border)", padding: "24px 0", cursor: "pointer" }}>
                <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", gap: 24 }}>
                  <h3 className="font-display" style={{ fontSize: 19, fontWeight: 600 }}>{it.q}</h3>
                  <div style={{
                    width: 32, height: 32, borderRadius: "50%", border: "1px solid var(--border-strong)",
                    display: "grid", placeItems: "center", flexShrink: 0,
                    color: open === i ? "var(--brand)" : "var(--fg-2)",
                    transform: open === i ? "rotate(45deg)" : "none",
                    transition: "all 0.3s var(--ease)",
                  }}>+</div>
                </div>
                <div style={{ overflow: "hidden", maxHeight: open === i ? 200 : 0, transition: "max-height 0.4s var(--ease), margin-top 0.4s var(--ease)", marginTop: open === i ? 16 : 0 }}>
                  <p style={{ color: "var(--fg-2)", margin: 0, maxWidth: 700 }}>{it.a}</p>
                </div>
              </div>
            </Reveal>
          ))}
          <div style={{ borderTop: "1px solid var(--border)" }}/>
        </div>
      </div>
    </section>
  );
}

function ServiceQuoteForm({ s }) {
  return (
    <section>
      <div className="container">
        <div className="card" style={{
          padding: "clamp(40px, 6vw, 72px)",
          background: "linear-gradient(135deg, rgba(108,191,0,0.1), rgba(108,191,0,0.02) 70%)",
          border: "1px solid rgba(108,191,0,0.2)",
          borderRadius: "var(--radius-xl)",
          textAlign: "center",
        }}>
          <div className="eyebrow" style={{ marginBottom: 20, justifyContent: "center" }}>Ready when you are</div>
          <h2 className="h-section" style={{ marginBottom: 16, maxWidth: 720, marginInline: "auto" }}>
            Let's talk about your <span style={{ color: "var(--brand)" }}>{s.name.toLowerCase()}</span> project.
          </h2>
          <p className="lead" style={{ marginInline: "auto", marginBottom: 32 }}>
            Send us a brief — we'll come back within 24 hours with a clear next step. No pitch deck, no follow-up dance.
          </p>
          <div style={{ display: "flex", gap: 12, justifyContent: "center", flexWrap: "wrap" }}>
            <Button to="/quote">Start a quote →</Button>
            <Button variant="ghost" to="/contact">Send a message</Button>
          </div>
        </div>
      </div>
    </section>
  );
}

function NotFound() {
  return (
    <div className="page-enter">
      <PageHero eyebrow="404" title="Page not found." subtitle="The link is wrong, or we moved something. Either way — sorry about that.">
        <Button to="/">← Back home</Button>
      </PageHero>
    </div>
  );
}

Object.assign(window, { ServicesPage, ServiceDetailPage, NotFound });
