// VoicePage — Voice Agent product detail page.
// Composes: Nav (reused) → Hero → Problem → Workflow → Capabilities → IntegrationsStrip (reused) → CaseStudy → FAQ → FooterCTA (reused) → Footer (reused)

const { useState: useVS, useEffect: useVE, useRef: useVR } = React;

// ---------------------------------------------------------------------------
// HERO
// ---------------------------------------------------------------------------
function VoiceHero({ t }) {
  return (
    <section data-screen-label="voice-hero" style={{
      position: "relative",
      background: "var(--bg-ink)",
      color: "var(--fg-cream)",
      overflow: "hidden",
      padding: "84px 0 0",
      borderBottom: "1px solid var(--border-dark)"
    }}>
      <div className="container" style={{
        display: "grid",
        gridTemplateColumns: "minmax(0, 1.05fr) minmax(0, 0.95fr)",
        gap: 64,
        alignItems: "center",
        paddingBottom: 80
      }} id="voice-hero-grid">

        {/* LEFT */}
        <div>
          <div className="eyebrow" style={{ color: "var(--accent-teal)" }}>
            {t.hero.eyebrow}
          </div>
          <h1 className="display-tight" style={{
            fontSize: "clamp(48px, 6vw, 88px)",
            margin: "20px 0 22px",
            maxWidth: "13ch",
            color: "var(--fg-cream)"
          }}>
            {t.hero.h1Pre} <span style={{ color: "var(--accent-teal)" }}>{t.hero.h1Accent}</span>
          </h1>
          <p style={{
            fontSize: 19, lineHeight: 1.5,
            color: "var(--muted-cream)",
            maxWidth: "54ch", margin: 0
          }}>
            {t.hero.sub}
          </p>
          <div style={{ marginTop: 30 }}>
            <a href="#book" className="btn" style={{
              fontSize: 15, padding: "13px 22px",
              background: "var(--accent-teal)", color: "var(--fg-ink)", fontWeight: 600
            }}>
              {t.hero.cta} <Icon.Arrow size={14} />
            </a>
          </div>

          {/* stats row */}
          <div style={{
            display: "grid", gridTemplateColumns: "repeat(3, 1fr)",
            gap: 14, marginTop: 38,
            paddingTop: 26, borderTop: "1px solid var(--border-dark)"
          }} id="voice-hero-stats">
            {t.hero.stats.map((s, i) => (
              <div key={i}>
                <div className="display" style={{
                  fontSize: 32, fontWeight: 700, letterSpacing: "-0.03em",
                  color: "var(--accent-teal)"
                }}>{s.value}</div>
                <div className="mono" style={{
                  fontSize: 10.5, letterSpacing: "0.14em",
                  color: "var(--muted-cream)", textTransform: "uppercase",
                  marginTop: 4
                }}>{s.label}</div>
              </div>
            ))}
          </div>
        </div>

        {/* RIGHT — incoming call visual */}
        <VoiceHeroCall />
      </div>

      <style>{`
        @media (max-width: 960px) {
          #voice-hero-grid { grid-template-columns: 1fr !important; gap: 56px !important; }
          #voice-hero-stats { grid-template-columns: repeat(3, 1fr) !important; }
        }
        @media (max-width: 540px) {
          #voice-hero-stats { grid-template-columns: 1fr !important; }
        }
      `}</style>
    </section>
  );
}

function VoiceHeroCall() {
  // simple looped timer
  const [tick, setTick] = useVS(0);
  useVE(() => {
    const id = setInterval(() => setTick(t => (t + 1) % 1000), 100);
    return () => clearInterval(id);
  }, []);
  const seconds = Math.floor(tick / 10) % 90 + 12;
  const mm = String(Math.floor(seconds / 60)).padStart(2, "0");
  const ss = String(seconds % 60).padStart(2, "0");

  // halo
  return (
    <div style={{ position: "relative", width: "100%", maxWidth: 520, marginLeft: "auto" }}>
      <div aria-hidden="true" style={{
        position: "absolute", inset: "-22px", borderRadius: 36,
        background: "rgba(155, 201, 210, 0.40)",
        filter: "blur(28px)", pointerEvents: "none", zIndex: 0
      }} />
      <div style={{
        position: "relative", zIndex: 1,
        background: "rgba(255, 255, 255, 0.96)",
        backdropFilter: "blur(24px) saturate(140%)",
        WebkitBackdropFilter: "blur(24px) saturate(140%)",
        border: "1px solid rgba(231, 228, 216, 0.55)",
        borderRadius: 24,
        boxShadow:
          "0 30px 60px -30px rgba(0,0,0,0.55), " +
          "0 0 40px rgba(155, 201, 210, 0.28), " +
          "inset 0 1px 0 rgba(255,255,255,0.6)",
        padding: "22px 24px 22px",
        color: "var(--fg-ink)"
      }}>
        {/* header */}
        <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between" }}>
          <span style={{ display: "inline-flex", alignItems: "center", gap: 9 }}>
            <span className="pulse-dot" style={{ width: 8, height: 8 }} />
            <span className="mono" style={{ fontSize: 10.5, letterSpacing: "0.18em", color: "var(--fg-ink)", fontWeight: 600 }}>
              LIVE · ON CALL
            </span>
          </span>
          <span className="mono" style={{ fontSize: 12, color: "var(--muted-ink)", fontVariantNumeric: "tabular-nums" }}>
            {mm}:{ss}
          </span>
        </div>

        {/* caller */}
        <div style={{ display: "flex", alignItems: "center", gap: 14, marginTop: 22 }}>
          <div style={{
            width: 46, height: 46, borderRadius: 999,
            background: "rgba(96,165,250,0.14)",
            border: "1px solid rgba(96,165,250,0.32)",
            display: "grid", placeItems: "center",
            color: "var(--cat-blue)"
          }}>
            <Icon.Phone size={20} />
          </div>
          <div>
            <div className="display" style={{ fontSize: 19, fontWeight: 600, letterSpacing: "-0.015em" }}>
              +39 348 ··· 1027
            </div>
            <div style={{ fontSize: 12.5, color: "var(--muted-ink)", marginTop: 2 }}>
              Unknown number · possibly new patient
            </div>
          </div>
        </div>

        {/* waveform */}
        <div style={{
          marginTop: 18, padding: "14px 16px",
          background: "rgba(155, 201, 210, 0.10)",
          border: "1px solid rgba(155, 201, 210, 0.22)",
          borderRadius: 14,
          display: "flex", alignItems: "center", justifyContent: "center"
        }}>
          <Waveform bars={36} height={40} color="#5B9EAD" />
        </div>

        {/* transcript */}
        <div style={{ marginTop: 18 }}>
          <div className="mono" style={{ fontSize: 9.5, letterSpacing: "0.18em", color: "var(--muted-ink)", marginBottom: 8 }}>
            LIVE TRANSCRIPT
          </div>
          <div style={{ display: "flex", flexDirection: "column", gap: 8, fontSize: 13, lineHeight: 1.45 }}>
            <Line who="patient" text="Hello, I'd like to book a dental hygiene visit for my son." />
            <Line who="sonaro" text="Of course, I'll help you right away. May I have your son's full name and date of birth?" />
            <Line who="patient" text="Luca Rossi, March 14, 2014." />
            <Line who="sonaro" typing />
          </div>
        </div>

        {/* footer */}
        <div style={{
          display: "grid", gridTemplateColumns: "1fr 1fr", gap: 14,
          marginTop: 20, paddingTop: 16,
          borderTop: "1px solid var(--border-light)"
        }}>
          <FootCell label="INTENT" value="Book hygiene · pediatric" valueColor="var(--fg-ink)" />
          <FootCell label="STATUS" value="Slot found · 17:30" valueColor="var(--cat-green)" />
        </div>
      </div>
    </div>
  );
}

function Line({ who, text, typing }) {
  return (
    <div style={{ display: "flex", gap: 8 }}>
      <span className="mono" style={{
        width: 14, flex: "none",
        color: who === "sonaro" ? "#5B9EAD" : "var(--muted-ink)",
        fontWeight: 600
      }}>
        {who === "sonaro" ? "S" : "P"}
      </span>
      {typing ? (
        <span>
          <span className="typing-dot" style={{ background: "#5B9EAD" }} />
          <span className="typing-dot" style={{ background: "#5B9EAD" }} />
          <span className="typing-dot" style={{ background: "#5B9EAD" }} />
        </span>
      ) : (
        <span style={{ color: who === "sonaro" ? "var(--fg-ink)" : "var(--muted-ink)" }}>{text}</span>
      )}
    </div>
  );
}

function FootCell({ label, value, valueColor }) {
  return (
    <div>
      <div className="mono" style={{ fontSize: 9.5, letterSpacing: "0.18em", color: "var(--muted-ink)" }}>{label}</div>
      <div style={{ fontSize: 13.5, fontWeight: 500, marginTop: 3, color: valueColor || "var(--fg-ink)", display: "inline-flex", alignItems: "center", gap: 6 }}>
        {label === "STATUS" && <span style={{ width: 6, height: 6, borderRadius: 999, background: valueColor || "var(--cat-green)" }} />}
        {value}
      </div>
    </div>
  );
}

// ---------------------------------------------------------------------------
// PROBLEM
// ---------------------------------------------------------------------------
function VoiceProblem({ t }) {
  const p = t.problem;
  return (
    <section data-screen-label="voice-problem" style={{
      background: "var(--bg-cream)", padding: "120px 0 100px"
    }}>
      <div className="container">
        <div style={{ display: "grid", gridTemplateColumns: "minmax(0, 1.05fr) minmax(0, 0.95fr)", gap: 56, alignItems: "start" }} id="vp-grid">
          <div>
            <div className="eyebrow" style={{ color: "var(--muted-ink)" }}>{p.eyebrow}</div>
            <h2 className="display-tight" style={{ fontSize: "clamp(40px, 5vw, 62px)", margin: "18px 0 20px" }}>
              {p.h2Pre} <span style={{ color: "#5B9EAD" }}>{p.h2Accent}</span>
            </h2>
            <p style={{ fontSize: 17.5, lineHeight: 1.55, color: "var(--muted-ink)", margin: 0, maxWidth: "54ch" }}>
              {p.body}
            </p>
          </div>
          <div style={{ display: "flex", flexDirection: "column", gap: 12 }}>
            {p.stats.map((s, i) => (
              <div key={i} style={{
                background: "var(--bg-white)",
                border: "1px solid var(--border-light)",
                borderRadius: 16,
                padding: "20px 22px",
                display: "flex", alignItems: "baseline", gap: 18
              }}>
                <div className="display" style={{
                  fontSize: 38, fontWeight: 700, letterSpacing: "-0.035em",
                  color: "var(--soft-rose-fg)", flex: "none", minWidth: 90
                }}>
                  {s.value}
                </div>
                <div style={{ fontSize: 14, color: "var(--muted-ink)", lineHeight: 1.45 }}>
                  {s.label}
                </div>
              </div>
            ))}
          </div>
        </div>
      </div>
      <style>{`@media (max-width: 960px) { #vp-grid { grid-template-columns: 1fr !important; } }`}</style>
    </section>
  );
}

// ---------------------------------------------------------------------------
// WORKFLOW — 5 steps
// ---------------------------------------------------------------------------
function VoiceWorkflow({ t }) {
  const w = t.workflow;
  return (
    <section data-screen-label="voice-workflow" style={{
      background: "var(--bg-ink)", color: "var(--fg-cream)",
      padding: "120px 0 120px"
    }}>
      <div className="container">
        <div style={{ maxWidth: 760, marginBottom: 56 }}>
          <div className="eyebrow" style={{ color: "var(--muted-cream)" }}>{w.eyebrow}</div>
          <h2 className="display-tight" style={{ fontSize: "clamp(40px, 5vw, 62px)", margin: "18px 0 0", color: "var(--fg-cream)" }}>
            {w.h2Pre} <span style={{ color: "var(--accent-teal)" }}>{w.h2Accent}</span>
          </h2>
        </div>
        <ol style={{
          listStyle: "none", padding: 0, margin: 0,
          display: "grid", gridTemplateColumns: "repeat(5, minmax(0, 1fr))",
          gap: 12, position: "relative"
        }} id="vw-grid">
          {w.steps.map((step, i) => (
            <li key={i} style={{
              background: "rgba(255,255,255,0.025)",
              border: "1px solid var(--border-dark)",
              borderRadius: 16,
              padding: "20px 18px 22px",
              position: "relative"
            }}>
              <div style={{
                width: 28, height: 28, borderRadius: 999,
                background: "var(--accent-teal)", color: "var(--fg-ink)",
                display: "grid", placeItems: "center",
                fontFamily: "var(--font-display)", fontWeight: 700, fontSize: 13,
                marginBottom: 12
              }}>
                {String(i + 1).padStart(2, "0")}
              </div>
              <div className="mono" style={{ fontSize: 10, letterSpacing: "0.16em", color: "var(--accent-teal)" }}>
                {step.time}
              </div>
              <div className="display" style={{ fontSize: 19, fontWeight: 600, letterSpacing: "-0.015em", marginTop: 10, marginBottom: 8, color: "var(--fg-cream)" }}>
                {step.title}
              </div>
              <div style={{ fontSize: 13.5, color: "var(--muted-cream)", lineHeight: 1.5 }}>
                {step.body}
              </div>
            </li>
          ))}
        </ol>
      </div>
      <style>{`
        @media (max-width: 1080px) { #vw-grid { grid-template-columns: repeat(2, 1fr) !important; } }
        @media (max-width: 540px) { #vw-grid { grid-template-columns: 1fr !important; } }
      `}</style>
    </section>
  );
}

// ---------------------------------------------------------------------------
// CAPABILITIES — 2x2 grid
// ---------------------------------------------------------------------------
function VoiceCapabilities({ t }) {
  const c = t.capabilities;
  return (
    <section data-screen-label="voice-caps" style={{ background: "var(--bg-cream)", padding: "120px 0" }}>
      <div className="container">
        <div style={{ maxWidth: 760, marginBottom: 48 }}>
          <div className="eyebrow" style={{ color: "var(--muted-ink)" }}>{c.eyebrow}</div>
          <h2 className="display-tight" style={{ fontSize: "clamp(36px, 4.6vw, 56px)", margin: "18px 0 0" }}>
            {c.h2}
          </h2>
        </div>
        <div style={{ display: "grid", gridTemplateColumns: "repeat(2, minmax(0, 1fr))", gap: 18 }} id="vc-grid">
          {c.items.map((item, i) => (
            <div key={i} style={{
              background: "var(--bg-white)",
              border: "1px solid var(--border-light)",
              borderRadius: 22,
              padding: "30px 30px 30px",
              position: "relative", overflow: "hidden",
              minHeight: 220
            }}>
              <div style={{
                position: "absolute", inset: 0, pointerEvents: "none",
                background: "radial-gradient(45% 50% at 92% 8%, rgba(155, 201, 210, 0.14), transparent 70%)"
              }} />
              <div style={{ position: "relative" }}>
                <div className="mono" style={{ fontSize: 10.5, letterSpacing: "0.16em", color: "#5B9EAD", fontWeight: 600 }}>
                  {item.kicker}
                </div>
                <div className="display" style={{
                  fontSize: "clamp(20px, 2vw, 26px)", fontWeight: 600, letterSpacing: "-0.02em",
                  margin: "12px 0 12px", maxWidth: "26ch"
                }}>
                  {item.title}
                </div>
                <p style={{ fontSize: 14.5, color: "var(--muted-ink)", lineHeight: 1.55, margin: 0, maxWidth: "44ch" }}>
                  {item.body}
                </p>
              </div>
            </div>
          ))}
        </div>
      </div>
      <style>{`@media (max-width: 880px) { #vc-grid { grid-template-columns: 1fr !important; } }`}</style>
    </section>
  );
}

// ---------------------------------------------------------------------------
// CASE STUDY (theme-aware: light or dark; quote optional)
// ---------------------------------------------------------------------------
function ProductCaseStudy({ data, variant, theme = "dark", showQuote = true }) {
  const accent = "#5B9EAD"; // slate teal — ties to brand
  const isDark = theme === "dark";

  const bgSection = isDark ? "var(--bg-ink)" : "var(--bg-cream)";
  const fgHead    = isDark ? "var(--fg-cream)" : "var(--fg-ink)";
  const fgBody    = isDark ? "var(--muted-cream)" : "var(--muted-ink)";
  const cardBg    = isDark ? "rgba(255,255,255,0.03)" : "var(--bg-white)";
  const cardBor   = isDark ? "var(--border-dark)" : "var(--border-light)";
  const railBg    = isDark ? "rgba(255,255,255,0.06)" : "var(--border-light)";

  return (
    <section data-screen-label="case" style={{
      background: bgSection,
      color: fgHead,
      padding: "120px 0"
    }}>
      <div className="container">
        <div className="eyebrow" style={{ color: isDark ? "var(--accent-teal)" : "var(--muted-ink)" }}>{data.eyebrow}</div>
        <h2 className="display-tight" style={{
          fontSize: "clamp(40px, 5vw, 68px)",
          margin: "18px 0 18px",
          color: fgHead,
          maxWidth: "20ch"
        }}>
          {data.h2Pre} <span style={{ color: accent }}>{data.h2Accent}</span>
        </h2>
        <div style={{ fontSize: 13, color: fgBody, fontFamily: "var(--font-mono)", letterSpacing: "0.06em", marginBottom: 20 }}>
          {data.clientFull}
        </div>
        <p style={{ fontSize: 18, color: fgBody, lineHeight: 1.55, margin: 0, maxWidth: "62ch" }}>
          {data.body}
        </p>

        {/* metrics row */}
        <div style={{
          marginTop: 48,
          display: "grid", gridTemplateColumns: "repeat(4, minmax(0, 1fr))", gap: 14
        }} id="cs-metrics">
          {data.metrics.map((m, i) => (
            <div key={i} style={{
              background: cardBg,
              border: `1px solid ${cardBor}`,
              borderRadius: 16, padding: "22px 22px"
            }}>
              <div className="display" style={{
                fontSize: 40, fontWeight: 700, letterSpacing: "-0.035em",
                color: accent
              }}>
                {m.value}
              </div>
              <div className="mono" style={{
                fontSize: 10.5, letterSpacing: "0.14em", color: fgHead,
                marginTop: 10, textTransform: "uppercase", fontWeight: 600
              }}>
                {m.label}
              </div>
              {m.sub && (
                <div style={{ fontSize: 12, color: fgBody, marginTop: 5, lineHeight: 1.4 }}>
                  {m.sub}
                </div>
              )}
            </div>
          ))}
        </div>

        {/* breakdown — full width when no quote, paired otherwise */}
        <div style={{
          marginTop: 28,
          display: "grid",
          gridTemplateColumns: showQuote ? "minmax(0, 1.05fr) minmax(0, 0.95fr)" : "1fr",
          gap: 18
        }} id="cs-detail">
          {showQuote && (
            <div style={{
              background: cardBg,
              border: `1px solid ${cardBor}`,
              borderRadius: 18,
              padding: "30px 32px",
              display: "flex", flexDirection: "column", gap: 20
            }}>
              <span style={{ fontSize: 38, color: accent, lineHeight: 0.8, fontFamily: "var(--font-display)", fontWeight: 700 }}>&ldquo;</span>
              <p style={{
                fontSize: 18, lineHeight: 1.5, color: fgHead,
                margin: 0, fontWeight: 400, letterSpacing: "-0.005em"
              }}>
                {data.quote.text}
              </p>
              <div style={{ marginTop: "auto", fontSize: 13, color: fgBody, fontFamily: "var(--font-mono)", letterSpacing: "0.04em" }}>
                — {data.quote.attrib}
              </div>
            </div>
          )}

          <div style={{
            background: cardBg,
            border: `1px solid ${cardBor}`,
            borderRadius: 18,
            padding: "30px 32px"
          }}>
            <div className="mono" style={{ fontSize: 10.5, letterSpacing: "0.16em", color: fgBody, marginBottom: 16 }}>
              BREAKDOWN
            </div>
            <div className="display" style={{ fontSize: 19, fontWeight: 600, letterSpacing: "-0.015em", color: fgHead, marginBottom: 18 }}>
              {data.breakdown.title}
            </div>

            <BreakdownRows rows={data.breakdown.rows} accent={accent} railBg={railBg} fgHead={fgHead} fgBody={fgBody} />

            {data.breakdown.footnote && (
              <div style={{ fontSize: 12.5, color: fgBody, marginTop: 18, lineHeight: 1.5 }}>
                {data.breakdown.footnote}
              </div>
            )}
          </div>
        </div>

        <div style={{ marginTop: 36 }}>
          <a href="#book" className="btn" style={{
            fontSize: 15, padding: "13px 22px",
            background: accent, color: "var(--fg-ink)", fontWeight: 600
          }}>
            {data.cta} <Icon.Arrow size={14} />
          </a>
        </div>
      </div>
      <style>{`
        @media (max-width: 960px) {
          #cs-metrics { grid-template-columns: repeat(2, 1fr) !important; }
          #cs-detail { grid-template-columns: 1fr !important; }
        }
        @media (max-width: 540px) {
          #cs-metrics { grid-template-columns: 1fr !important; }
        }
      `}</style>
    </section>
  );
}

function BreakdownRows({ rows, accent, railBg, fgHead, fgBody }) {
  const max = rows.reduce((m, r) => Math.max(m, r.value), 0) || 1;
  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 10 }}>
      {rows.map((r, i) => {
        const pct = (r.value / max) * 100;
        return (
          <div key={i}>
            <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", marginBottom: 6 }}>
              <div style={{ fontSize: 13.5, color: fgHead }}>{r.label}</div>
              <div style={{ fontSize: 13, color: fgBody, fontFamily: "var(--font-mono)" }}>
                {r.value}{r.eur ? <span style={{ color: accent, marginLeft: 8, fontWeight: 600 }}>{r.eur}</span> : null}
              </div>
            </div>
            <div style={{ height: 6, borderRadius: 999, background: railBg, overflow: "hidden" }}>
              <div style={{
                width: `${pct}%`, height: "100%",
                background: accent, transition: "width 0.8s ease"
              }} />
            </div>
          </div>
        );
      })}
    </div>
  );
}

// ---------------------------------------------------------------------------
// PRODUCT FAQ (reuses the same accordion CSS as the main FAQ)
// ---------------------------------------------------------------------------
function ProductFAQ({ data }) {
  return (
    <section data-screen-label="prod-faq" style={{ background: "var(--bg-cream)", padding: "120px 0" }}>
      <div className="container" style={{ maxWidth: 960 }}>
        <div style={{ marginBottom: 32 }}>
          <div className="eyebrow" style={{ color: "var(--muted-ink)" }}>{data.eyebrow}</div>
          <h2 className="display-tight" style={{ fontSize: "clamp(32px, 4vw, 48px)", margin: "16px 0 0" }}>
            {data.h2}
          </h2>
        </div>
        <div>
          {data.items.map((item, i) => (
            <details key={i} className="faq-item" {...(i === 0 ? { open: true } : {})}>
              <summary className="faq-summary">
                <span>{item.q}</span>
                <span className="faq-icon"><Icon.Plus size={14} /></span>
              </summary>
              <div className="faq-body">{item.a}</div>
            </details>
          ))}
        </div>
      </div>
    </section>
  );
}

// ---------------------------------------------------------------------------
// PAGE ROOT
// ---------------------------------------------------------------------------
function VoicePage() {
  // Lang comes from the HTML wrapper: /it/voice.html inlines
  // window.__sonaroLang = "it" before loading scripts. Root /voice.html
  // leaves it undefined → defaults to "en".
  const lang = (typeof window !== "undefined" && window.__sonaroLang) || "en";
  const dict = (window.CONTENT && window.CONTENT[lang]) || window.CONTENT.en;
  const t = dict.voicePage;
  return (
    <div id="top">
      <Nav t={dict} lang={lang} setLang={() => {}} />
      <VoiceHero        t={t} />
      <IntegrationsStrip t={dict} />
      <VoiceProblem     t={t} />
      <VoiceWorkflow    t={t} />
      <VoiceCapabilities t={t} />
      <ProductCaseStudy data={t.caseStudy} variant="voice" theme="dark" showQuote={false} />
      <ProductFAQ       data={t.faq} />
      <FooterCTA        t={dict} />
      <Footer           t={dict} />
    </div>
  );
}

Object.assign(window, { VoicePage, ProductCaseStudy, ProductFAQ });
