// VoxLens case showcase — sits directly under the VoxLens project card.
// "同一套引擎产出什么": ① flagship case ② product docs ③ market & competitive insight.
const { useState: useVx, useRef: useRefVx, useEffect: useEffectVx } = React;

const VX_BLUE = "#4d7cfe";
const VX_AMBER = "#e8a13a";

function VxArrow({ hover }) {
  return (
    <svg width="15" height="15" viewBox="0 0 24 24" fill="none" aria-hidden="true"
      style={{ transform: hover ? "translateX(3px)" : "none", transition: "transform .25s" }}>
      <path d="M5 12h13M13 6l6 6-6 6" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
    </svg>);
}

function VxLink({ children, color = "var(--text)", href, onClick }) {
  const [h, setH] = useVx(false);
  const Tag = href ? "a" : "span";
  return (
    <Tag href={href} onClick={onClick} onMouseEnter={() => setH(true)} onMouseLeave={() => setH(false)}
      style={{ display: "inline-flex", alignItems: "center", gap: 7, fontSize: 14.5, fontWeight: 600,
        color: h ? "#fff" : color, cursor: "pointer", transition: "color .2s", letterSpacing: "-0.01em", textDecoration: "none" }}>
      <span>{children}</span><VxArrow hover={h} />
    </Tag>);
}

// Remember the portfolio's scroll position before navigating to the full case,
// so returning lands the user back on the flagship card.
function saveReturnScroll() {
  try {
    var sc = document.querySelector('main');
    sessionStorage.setItem('wiloReturnScroll', String(sc ? sc.scrollTop : 0));
  } catch (e) {}
}

function VxFileIcon() {
  return (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
      <path d="M14 3H7a1 1 0 0 0-1 1v16a1 1 0 0 0 1 1h10a1 1 0 0 0 1-1V8z" />
      <path d="M14 3v5h5" /><path d="M9.5 13h5M9.5 16.5h5" />
    </svg>);
}

function VxIdeaIcon() {
  return (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
      <path d="M9 18h6" /><path d="M10 21h4" />
      <path d="M12 3a6 6 0 0 0-3.6 10.8c.5.4.9 1 .9 1.7V16h5.4v-.5c0-.7.4-1.3.9-1.7A6 6 0 0 0 12 3Z" />
    </svg>);
}

// Stacked "button card" — icon + title + one-line subtitle (direction B)
function VxBtnCard({ icon, title, sub, href, onClick, primary }) {
  const [h, setH] = useVx(false);
  const skin = primary ?
    { background: "linear-gradient(135deg,#5b86ff,#3b63f0)", color: "#fff", border: "1px solid transparent" } :
    { background: h ? "var(--panel-3)" : "var(--panel-2)", color: "var(--text)", border: "1px solid var(--line-2)" };
  return (
    <a href={href} onClick={onClick} onMouseEnter={() => setH(true)} onMouseLeave={() => setH(false)}
      style={{
        display: "flex", alignItems: "center", gap: 13, padding: "13px 15px", borderRadius: 14, textDecoration: "none",
        transition: "transform .2s, background .2s, box-shadow .2s", transform: h ? "translateY(-2px)" : "none",
        boxShadow: h && primary ? "0 18px 36px -16px rgba(59,99,240,0.85)" : "none", ...skin
      }}>
      <span style={{
        width: 38, height: 38, borderRadius: 10, flex: "0 0 auto", display: "grid", placeItems: "center",
        background: primary ? "rgba(255,255,255,0.16)" : "rgba(255,255,255,0.05)",
        border: primary ? "1px solid rgba(255,255,255,0.26)" : "1px solid var(--line)"
      }}>{icon}</span>
      <span style={{ display: "flex", flexDirection: "column", gap: 3, minWidth: 0 }}>
        <b style={{ fontSize: 14.5, fontWeight: 700, letterSpacing: "-0.01em" }}>{title}</b>
        {sub && <span style={{ fontSize: 11.5, fontFamily: "var(--font-mono)", opacity: 0.72, whiteSpace: "nowrap" }}>{sub}</span>}
      </span>
      <span style={{ marginLeft: "auto", opacity: 0.85, display: "inline-flex" }}><VxArrow hover={h} /></span>
    </a>);
}

function VxImg({ id, ph, radius = 12, src, aspect = "16 / 9", fit = "cover" }) {
  return (
    <div style={{ width: "100%", aspectRatio: aspect, borderRadius: radius, overflow: "hidden" }}>
      <image-slot id={id} placeholder={ph} shape="rounded" radius={String(radius)} fit={fit}
        {...(src ? { src } : {})}
        style={{ display: "block", width: "100%", height: "100%" }}></image-slot>
    </div>);
}

function VxBadge({ children, color }) {
  return (
    <span style={{ display: "inline-flex", alignItems: "center", whiteSpace: "nowrap", fontSize: 12.5, fontWeight: 600,
      padding: "5px 11px", borderRadius: 100, color,
      background: `color-mix(in srgb, ${color} 14%, transparent)`,
      border: `1px solid color-mix(in srgb, ${color} 32%, transparent)` }}>{children}</span>);
}

function VxChip({ children }) {
  return (
    <span style={{ fontSize: 13.5, fontWeight: 600, color: "#d7d7da", padding: "7px 13px", borderRadius: 9,
      background: "var(--panel-2)", border: "1px solid var(--line)", whiteSpace: "nowrap" }}>{children}</span>);
}

function VxSectionHead({ n, title, hint }) {
  return (
    <div style={{ display: "flex", alignItems: "center", gap: 12, marginBottom: 22 }}>
      <span style={{ width: 26, height: 26, borderRadius: "50%", border: "1px solid var(--line-2)", flex: "0 0 auto",
        display: "grid", placeItems: "center", fontSize: 13, fontWeight: 600, color: "var(--muted)", fontFamily: "var(--font-mono)" }}>{n}</span>
      <span style={{ fontSize: 19, fontWeight: 700, letterSpacing: "-0.01em" }}>{title}</span>
      <span style={{ fontSize: 14, color: "var(--muted-2)" }}>—&nbsp;&nbsp;{hint}</span>
    </div>);
}

const VX_LABEL = { fontFamily: "var(--font-mono)", fontSize: 12, letterSpacing: "0.04em", color: "var(--muted-2)", marginBottom: 12 };
const VX_DESC = { fontSize: 15, lineHeight: 1.7, color: "#b9b9be", textWrap: "pretty" };

/* ---- small / market card ---- */
function VxMarketCard({ id, title, sub, href, src }) {
  const [h, setH] = useVx(false);
  return (
    <div onMouseEnter={() => setH(true)} onMouseLeave={() => setH(false)}
      style={{ background: "var(--panel)", border: "1px solid " + (h ? "var(--line-2)" : "var(--line)"),
        borderRadius: 16, padding: 18, transition: "border-color .25s, transform .25s",
        transform: h ? "translateY(-3px)" : "none", display: "flex", flexDirection: "column", height: "100%" }}>
      <VxImg id={id} src={src} ph="图片位" radius={11} />
      <div style={{ ...VX_LABEL, marginTop: 16, marginBottom: 8 }}>VoxLens 产出</div>
      <div style={{ fontSize: 17, fontWeight: 700, letterSpacing: "-0.01em" }}>{title}</div>
      <div style={{ fontSize: 13.5, color: "var(--muted)", marginTop: 6, marginBottom: 16, lineHeight: 1.55 }}>{sub}</div>
      <div style={{ marginTop: "auto" }}><VxLink href={href} onClick={href ? saveReturnScroll : undefined}>查看详情</VxLink></div>
    </div>);
}

// Chapter marker — the connector between the VoxLens card and its reports (direction B)
function VxChapterMarker({ open, onToggle }) {
  const [h, setH] = useVx(false);
  return (
    <div onClick={onToggle} onMouseEnter={() => setH(true)} onMouseLeave={() => setH(false)}
      style={{ display: "flex", alignItems: "center", gap: 16, margin: "40px 0 0", cursor: "pointer", userSelect: "none" }}>
      <div style={{ flex: 1, height: 1, background: "var(--line)" }}></div>
      <span style={{
        display: "inline-flex", alignItems: "center", gap: 10, whiteSpace: "nowrap",
        border: "1px solid " + (h || open ? "var(--line-2)" : "var(--line)"),
        background: h ? "var(--panel-2)" : "var(--panel)",
        borderRadius: 100, padding: "9px 16px", fontFamily: "var(--font-mono)", fontSize: 12.5,
        color: open ? "var(--text)" : "var(--muted)", transition: "all .2s"
      }}>
        <span style={{ width: 7, height: 7, borderRadius: "50%", background: "var(--green)", boxShadow: "0 0 8px var(--green)" }}></span>
        {open ? "收起 VoxLens 产出" : "VoxLens 产出 · 3 类"}
        <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round"
          style={{ transition: "transform .4s", transform: open ? "rotate(180deg)" : "none" }}><path d="M6 9l6 6 6-6" /></svg>
      </span>
      <div style={{ flex: 1, height: 1, background: "var(--line)" }}></div>
    </div>);
}

// Measured max-height reveal (robust across reflow / late-loading images)
function VxCollapsible({ open, children }) {
  const ref = useRefVx(null);
  const [mh, setMh] = useVx("0px");
  const first = useRefVx(true);
  useEffectVx(() => {
    const el = ref.current;if (!el) return;
    if (open) {
      first.current = false;
      setMh(el.scrollHeight + "px");
      const t = setTimeout(() => setMh("none"), 700);
      return () => clearTimeout(t);
    }
    if (first.current) {first.current = false;setMh("0px");return;}
    setMh(el.scrollHeight + "px");
    const r = requestAnimationFrame(() => requestAnimationFrame(() => setMh("0px")));
    return () => cancelAnimationFrame(r);
  }, [open]);
  return (
    <div style={{ overflow: "hidden", maxHeight: mh, opacity: open ? 1 : 0,
      transition: "max-height .65s cubic-bezier(.16,.84,.44,1), opacity .5s", marginTop: open ? 0 : 0 }}>
      <div ref={ref} style={{ paddingTop: 30 }}>{children}</div>
    </div>);
}

function VoxLensShowcase({ open, onToggle }) {
  return (
    <section style={{ maxWidth: 1080, margin: "0 auto" }}>
      {/* chapter marker — click 查看案例 (or this) to reveal the reports */}
      <VxChapterMarker open={open} onToggle={onToggle} />

      <VxCollapsible open={open}>
      {/* heading */}
      <Reveal delay={60}>
        <div style={{ textAlign: "center", marginBottom: 44 }}>
          <div style={{ fontFamily: "var(--font-mono)", fontSize: 13, letterSpacing: "0.04em", color: "var(--muted)", marginBottom: 14 }}>VoxLens 产出过什么</div>
          <h3 className="display" style={{ fontSize: "clamp(28px,3.2vw,40px)", fontWeight: 800, letterSpacing: "-0.01em", lineHeight: 1.2 }}>同一套引擎，不同深度的产品决策</h3>
          <p style={{ marginTop: 14, fontSize: 16, color: "var(--muted)" }}>下面这些都跑在 VoxLens 上：从完整的出海竞争研究，到基于调研下的产品定义。</p>
        </div>
      </Reveal>

      {/* ① flagship */}
      <Reveal delay={80}>
        <VxSectionHead n="1" title="出海竞争研究" hint="一份能落地的北美出海策略" />
        <div style={{ position: "relative", background: "var(--panel)", borderRadius: 22, padding: "clamp(20px,2.4vw,30px)",
          border: `1px solid color-mix(in srgb, ${VX_BLUE} 55%, transparent)`,
          boxShadow: `0 0 0 1px color-mix(in srgb, ${VX_BLUE} 14%, transparent), 0 30px 70px -30px color-mix(in srgb, ${VX_BLUE} 45%, transparent)` }}>
          <div style={{ display: "grid", gridTemplateColumns: "1.55fr 1fr", gap: "clamp(20px,2.4vw,32px)", alignItems: "center" }}>
            {/* left — the media is the hero */}
            <VxImg id="vox-flagship" src="assets/wilo/ring-hero.png" aspect="4 / 3" ph="智能戒指产品图（拖入可替换）" radius={14} />
            {/* right — text sidebar */}
            <div style={{ display: "flex", flexDirection: "column", gap: 13, minWidth: 0 }}>
              <div style={{ ...VX_LABEL, margin: 0 }}>VoxLens 产出 · 旗舰</div>
              <div style={{ display: "flex", alignItems: "center", gap: 10, flexWrap: "wrap" }}>
                <span style={{ fontSize: "clamp(20px,1.85vw,26px)", fontWeight: 800, letterSpacing: "-0.01em", lineHeight: 1.18 }}>智能戒指北美竞争研究</span>
                <VxBadge color={VX_AMBER}>0→1 出海策略</VxBadge>
              </div>
              <p style={{ ...VX_DESC, fontSize: 15 }}>
                一套面向北美首发的竞争研究：从 44 万条评论追踪 5.7 万名用户的迁移路径，输出首发人群、定价假设、承诺红线到渠道打法的完整出海策略。
              </p>
              <div style={{ display: "flex", gap: 8, flexWrap: "wrap" }}>
                <VxChip>44 万条评论</VxChip><VxChip>5.7 万名用户</VxChip><VxChip>5 类竞品框架</VxChip>
              </div>
              <div style={{ display: "flex", flexDirection: "column", gap: 11, marginTop: 5 }}>
                <VxBtnCard primary icon={<VxFileIcon />} title="查看完整案例"
                  href="案例 - WILO 北美出海策略白皮书.html" onClick={saveReturnScroll} />
                <VxBtnCard icon={<VxIdeaIcon />} title="思考复盘" sub="方法论 · 我是怎么想的"
                  href="案例 - 智能戒指研究思考复盘.html" onClick={saveReturnScroll} />
              </div>
            </div>
          </div>
        </div>
      </Reveal>

      {/* ② product docs */}
      <div style={{ marginTop: 56 }}>
        <Reveal>
          <VxSectionHead n="2" title="产品定义与决策" hint="把用户研究落成可执行的产品方案与指标" />
          <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 18 }}>
            <div style={{ background: "var(--panel)", border: "1px solid var(--line)", borderRadius: 18, padding: 20, display: "flex", flexDirection: "column" }}>
              <VxImg id="vox-doc-qiyu" src="assets/qiyu/card-cover.png" ph="实时位置社交 · 产品示意（拖入可替换）" />
              <div style={{ ...VX_LABEL, marginTop: 18, marginBottom: 10 }}>VoxLens 产出</div>
              <div style={{ fontSize: 20, fontWeight: 700, letterSpacing: "-0.01em", marginBottom: 12 }}>实时位置社交 · AI 产品设计</div>
              <p style={{ ...VX_DESC, marginBottom: 18 }}>一次完整的 0→1 产品定义：从 16,357 条声音判断赛道机会，定出「亲密关系的信任空间」的定位，再落成 3 个共享同一情境引擎的 AI 功能与产品指标。</p>
              <div style={{ marginTop: "auto" }}><VxLink href="案例 - 在场 实时位置社交产品设计.html" onClick={saveReturnScroll}>查看详情</VxLink></div>
            </div>
            <div style={{ background: "var(--panel)", border: "1px solid var(--line)", borderRadius: 18, padding: 20, display: "flex", flexDirection: "column" }}>
              <VxImg id="vox-doc-startbtn" src="assets/startbtn/card-cover.png" ph="Start Button · 四维验证示意（拖入可替换）" />
              <div style={{ ...VX_LABEL, marginTop: 18, marginBottom: 10 }}>VoxLens 产出</div>
              <div style={{ fontSize: 20, fontWeight: 700, letterSpacing: "-0.01em", marginBottom: 12 }}>Start Button</div>
              <p style={{ ...VX_DESC, marginBottom: 18 }}>把 4 份调研、5,992 篇论文用四维证据交叉验证，收敛成 10 个产品决策：做什么、不做什么、怎么定价、怎么冷启动。</p>
              <div style={{ marginTop: "auto" }}><VxLink href="案例 - Start Button 产品分析.html" onClick={saveReturnScroll}>查看详情</VxLink></div>
            </div>
          </div>
        </Reveal>
      </div>

      {/* ③ market & competitive insight */}
      <div style={{ marginTop: 56 }}>
        <Reveal>
          <VxSectionHead n="3" title="竞品与市场洞察" hint="跨市场、跨品类的竞争格局拆解" />
          <div style={{ display: "grid", gridTemplateColumns: "repeat(3,1fr)", gap: 18 }}>
            <VxMarketCard id="vox-mkt-insta360" title="运动相机品类竞争格局拆解" sub="用 47,352 条公开 VOC 拆 GoPro / DJI / Insta360 的竞争格局，产出一张「8 个进攻维度 vs 10 个防守维度」的攻防地图——把拍脑袋选打法变成用数据选战场。" href="案例 - 运动相机品类竞争格局.html" src="assets/actioncam/card-cover.png" />
            <VxMarketCard id="vox-mkt-robot" title="全球扫地机器人 · 四市场竞争格局" sub="跨北美、西欧、日本、中国四市场、6 种语言、11,467 条声音，拆清同一批品牌为何在不同市场口碑两极——一份给出海定区域打法的竞争分析。" href="案例 - 全球扫地机器人竞争格局.html" src="assets/robotvac/card-cover.png" />
            <VxMarketCard id="vox-mkt-power" title="AI 智能家居 · 用户声音解读" sub="从 20,493 条声音回答「AI 接入智能硬件第一年，到底变好还是变糟」：哪些场景跑通、哪些是鸡肋，最后落成一份 PM 行动清单。" href="案例 - AI 智能家居市场用户声音.html" src="assets/smarthome/card-cover.jpg" />
          </div>
        </Reveal>
      </div>
      </VxCollapsible>
    </section>);
}

window.VoxLensShowcase = VoxLensShowcase;
