// PainPoint, Features, WhyQlivo, FinalCTA, Footer

// ===== 02 Pain Point — Before / After (with scatter motion) =====
const MESSY_INBOX = [
  { f: "Newsletter Weekly", s: "週報合輯：本週 12 則新創", t: "06:00" },
  { f: "凱盛電子 / 林佳穎", s: "MLCC 報價需求（5/22 前）", t: "09:24", urgent: true },
  { f: "GitHub", s: "[qlivo] 5 new comments on PR #234", t: "09:31" },
  { f: "Allen Hsu", s: "Q3 OKR Review 會議", t: "08:51" },
  { f: "AWS Billing", s: "Your invoice is ready", t: "07:12" },
  { f: "LinkedIn", s: "You have 7 new connection requests", t: "yesterday" },
  { f: "Slack", s: "Daniel mentioned you in #design", t: "10:02" },
  { f: "Notion", s: "weekly digest of changes", t: "yesterday" },
];

// Deterministic random for scatter rotations
const scatterFor = (i) => {
  const seed = (i * 9301 + 49297) % 233280;
  const r = seed / 233280;
  return {
    rot: (r - 0.5) * 60,         // -30 ~ 30 deg
    dx: (r - 0.5) * 200,         // -100 ~ 100 px
    dy: 120 + r * 80,            // 120 ~ 200 px down
  };
};

const QuoteCard = ({ tone, quote, author, role }) => {
  const isAfter = tone === "after";
  return (
    <div style={{
      padding: "20px 22px",
      borderRadius: 14,
      background: isAfter ? "rgba(34,211,238,0.04)" : "rgba(248,113,113,0.04)",
      border: `1px solid ${isAfter ? "rgba(34,211,238,0.18)" : "rgba(248,113,113,0.18)"}`,
      position: "relative",
    }}>
      <div style={{
        position: "absolute", top: -6, left: 14,
        fontSize: 56, lineHeight: 1, fontFamily: "Georgia, serif",
        color: isAfter ? "rgba(34,211,238,0.35)" : "rgba(248,113,113,0.35)",
        fontWeight: 700,
        userSelect: "none",
      }}>“</div>
      <div style={{
        fontSize: 17, lineHeight: 1.5, fontWeight: 600,
        color: isAfter ? "var(--text)" : "var(--text-2)",
        letterSpacing: "-0.005em",
        paddingLeft: 18, paddingTop: 6,
        fontStyle: "normal",
      }}>{quote}</div>
      <div style={{
        marginTop: 14, paddingLeft: 18,
        display: "flex", alignItems: "center", gap: 10,
      }}>
        <div style={{
          width: 28, height: 28, borderRadius: 14,
          background: isAfter ? "linear-gradient(135deg, #3B82F6, #22D3EE)" : "linear-gradient(135deg, #525266, #2A2A36)",
          display: "flex", alignItems: "center", justifyContent: "center",
          fontSize: 11, fontWeight: 700, color: "white",
          flexShrink: 0,
        }}>{author[0]}</div>
        <div style={{ display: "flex", flexDirection: "column", lineHeight: 1.2 }}>
          <span style={{ fontSize: 12, fontWeight: 700, color: "var(--text)" }}>{author}</span>
          <span className="mono" style={{ fontSize: 10, color: "var(--text-3)", letterSpacing: "0.06em", marginTop: 2 }}>{role}</span>
        </div>
      </div>
    </div>
  );
};

const PainPoint = () => {
  const isPhone = useIsMobile(600);
  const sectionRef = useRef(null);
  const itemRefs = useRef([]);
  const targetRef = useRef(null);
  const inViewRef = useRef(false);
  const timers = useRef([]);
  const [deltas, setDeltas] = useState([]);
  const [phase, setPhase] = useState("rest"); // rest | flying | after
  const [cycle, setCycle] = useState(0);

  const measure = () => {
    if (!targetRef.current) return;
    const t = targetRef.current.getBoundingClientRect();
    const tx = t.left + t.width / 2;
    const ty = t.top + t.height / 2;
    const next = itemRefs.current.map((el, i) => {
      if (!el) return { dx: 0, dy: 0, r: 0 };
      const r = el.getBoundingClientRect();
      const cx = r.left + r.width / 2;
      const cy = r.top + r.height / 2;
      const baseR = ((i * 37) % 14) - 7;
      return { dx: tx - cx, dy: ty - cy, r: baseR };
    });
    setDeltas(next);
  };

  useEffect(() => {
    const id = setTimeout(measure, 80);
    window.addEventListener("resize", measure);
    return () => {
      clearTimeout(id);
      window.removeEventListener("resize", measure);
    };
  }, []);

  const clearTimers = () => {
    timers.current.forEach(clearTimeout);
    timers.current = [];
  };

  const runLoop = () => {
    if (!inViewRef.current) return;
    clearTimers();
    measure();
    setPhase("rest");
    setCycle((c) => c + 1);
    timers.current.push(setTimeout(() => {
      if (!inViewRef.current) return;
      setPhase("flying");
    }, 900));
    timers.current.push(setTimeout(() => {
      if (!inViewRef.current) return;
      setPhase("after");
    }, 900 + 1500 + MESSY_INBOX.length * 80 - 250));
    timers.current.push(setTimeout(() => {
      if (!inViewRef.current) return;
      runLoop();
    }, 6800));
  };

  useEffect(() => {
    if (!sectionRef.current) return;
    const obs = new IntersectionObserver(([entry]) => {
      inViewRef.current = entry.isIntersecting;
      if (entry.isIntersecting) runLoop();
      else clearTimers();
    }, { threshold: 0.2 });
    obs.observe(sectionRef.current);
    return () => { obs.disconnect(); clearTimers(); };
    // eslint-disable-next-line
  }, []);

  const replay = () => {
    inViewRef.current = true;
    runLoop();
  };

  const flying = phase === "flying";
  const showAfter = phase === "after";
  const itemsHidden = phase === "after";

  return (
    <section ref={sectionRef} style={{ position: "relative" }} className="section-pad">
      <div className="container">
        <SectionHeader
          tag="01 / 痛點"
          title={<>差別不在 142 封信，<br/>在你打開的<span style={{ color: "var(--text-3)" }}>那一瞬間</span>。</>}
          sub={isPhone
            ? <>Qlivo 把判斷交給 AI — 你打開的是<span style={{ color: "var(--text)" }}>已排好序的待辦清單</span>。</>
            : <>同樣 142 封信，整理過跟沒整理過，是「不想開始工作」與「知道下一步」的差別。Qlivo 把判斷外包給 AI — 你打開的不是收件匣，是<span style={{ color: "var(--text)" }}>已經排好序的待辦清單</span>。</>}
          maxWidth={780}
        />

        <div className="grid-painpoint" style={{ marginTop: 72 }}>
          {/* ============ BEFORE ============ */}
          <div className="pain-panel" style={{
            background: "var(--bg-2)",
            border: "1px solid var(--border-soft)",
            borderRadius: 18,
            padding: 32,
            position: "relative",
            overflow: "visible",
            display: "flex", flexDirection: "column",
            zIndex: 2,
          }}>
            {/* Background fog (subtle red haze for anxiety) */}
            <div style={{
              position: "absolute", inset: 0, pointerEvents: "none",
              background: "radial-gradient(ellipse at 70% 20%, rgba(248,113,113,0.06), transparent 60%)",
              borderRadius: 18,
              overflow: "hidden",
            }}/>

            <div style={{ position: "relative", display: "flex", alignItems: "center", gap: 10, marginBottom: 20 }}>
              <span className="mono" style={{
                fontSize: 10, padding: "4px 10px", borderRadius: 999,
                background: "rgba(248,113,113,0.1)", color: "var(--red)",
                border: "1px solid rgba(248,113,113,0.3)", letterSpacing: "0.1em",
              }}>BEFORE</span>
              <span className="mono" style={{ fontSize: 11, color: "var(--text-3)", letterSpacing: "0.08em" }}>沒整理過 · 傳統收件匣</span>
              <span style={{ marginLeft: "auto" }}>
                <span className="mono" style={{
                  fontSize: 10, fontWeight: 800, padding: "4px 9px",
                  background: "var(--red)", color: "white", borderRadius: 999,
                  letterSpacing: "0.05em",
                }}>142 未讀</span>
              </span>
            </div>

            <QuoteCard
              tone="before"
              quote="點開信箱就有點不想工作。"
              author="王志明"
              role="業務主管 · 導入 QLIVO 前"
            />

            {/* Messy list — items fly to After */}
            <div style={{ marginTop: 24, position: "relative", flex: 1 }}>
              <div className="mono" style={{ fontSize: 10, color: "var(--text-4)", letterSpacing: "0.12em", marginBottom: 12 }}>INBOX · 主管／客戶／系統通知全部攪在一起</div>
              <div style={{ display: "flex", flexDirection: "column", gap: 4, position: "relative" }}>
                {MESSY_INBOX.map((m, i) => {
                  const d = deltas[i] || { dx: 0, dy: 0, r: 0 };
                  return (
                    <div
                      key={`${cycle}-${i}`}
                      ref={(el) => (itemRefs.current[i] = el)}
                      className={flying ? "fly-item" : ""}
                      style={{
                        display: "grid", gridTemplateColumns: "auto 1fr auto",
                        gap: 12, padding: "9px 10px",
                        borderRadius: 8,
                        background: "var(--bg-3)",
                        border: "1px solid var(--border-soft)",
                        alignItems: "center",
                        position: "relative",
                        zIndex: flying ? 50 : 1,
                        "--dx": `${d.dx}px`,
                        "--dy": `${d.dy}px`,
                        "--r": `${d.r}deg`,
                        "--delay": `${i * 80}ms`,
                        opacity: itemsHidden ? 0 : undefined,
                        transition: itemsHidden ? "opacity .2s ease" : undefined,
                      }}
                    >
                      <span style={{ width: 7, height: 7, borderRadius: 4, background: m.urgent ? "var(--red)" : "var(--text-3)", flexShrink: 0, position: "relative", zIndex: 2 }} />
                      <div style={{ minWidth: 0, overflow: "hidden", position: "relative", zIndex: 2 }}>
                        <div style={{ fontSize: 12, fontWeight: 600, color: "var(--text-2)", whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }}>{m.f}</div>
                        <div style={{ fontSize: 11, color: "var(--text-4)", whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }}>{m.s}</div>
                      </div>
                      <div className="mono" style={{ fontSize: 9, color: "var(--text-4)", position: "relative", zIndex: 2 }}>{m.t}</div>
                      {flying && <span className="ai-scan-bar" aria-hidden />}
                    </div>
                  );
                })}
              </div>
            </div>
          </div>

          {/* ============ Arrow ============ */}
          <div className="pain-arrow-col">
            <div style={{
              width: 38, height: 38, borderRadius: 19,
              background: "var(--accent)",
              display: "flex", alignItems: "center", justifyContent: "center",
              color: "white",
              boxShadow: (flying || showAfter)
                ? "0 0 0 8px rgba(59,130,246,0.18), 0 8px 28px -6px rgba(59,130,246,0.7)"
                : "0 0 0 4px rgba(59,130,246,0.1), 0 4px 16px -4px rgba(59,130,246,0.4)",
              transition: "box-shadow .6s ease",
              position: "relative",
              zIndex: 2,
            }}><IconArrowRight size={18}/></div>
            <div className="pain-arrow-line" style={{
              position: "absolute", top: "50%", left: 0, right: 0, height: 1,
              background: "linear-gradient(90deg, transparent, var(--accent-2), transparent)",
              opacity: flying ? 1 : 0,
              transition: "opacity .4s ease",
              zIndex: 1,
            }}/>
          </div>

          {/* ============ AFTER ============ */}
          <div className="pain-panel" style={{
            background: "linear-gradient(180deg, var(--bg-2), var(--bg-3))",
            border: "1px solid var(--border)",
            borderRadius: 18,
            padding: 32,
            position: "relative",
            overflow: "hidden",
            display: "flex", flexDirection: "column",
            boxShadow: showAfter
              ? "0 0 0 1px rgba(59,130,246,0.22), 0 40px 100px -20px rgba(59,130,246,0.4)"
              : flying
              ? "0 0 0 1px rgba(59,130,246,0.15), 0 20px 60px -20px rgba(59,130,246,0.25)"
              : "0 0 0 1px rgba(59,130,246,0.08), 0 20px 60px -20px rgba(59,130,246,0.1)",
            transition: "box-shadow .8s ease",
            zIndex: 1,
          }}>
            {/* Background subtle cyan */}
            <div style={{
              position: "absolute", inset: 0, pointerEvents: "none",
              background: "radial-gradient(ellipse at 30% 20%, rgba(34,211,238,0.06), transparent 60%)",
            }}/>

            <div style={{ position: "relative", display: "flex", alignItems: "center", gap: 10, marginBottom: 20 }}>
              <span className="mono" style={{
                fontSize: 10, padding: "4px 10px", borderRadius: 999,
                background: "rgba(34,211,238,0.1)", color: "var(--cyan)",
                border: "1px solid rgba(34,211,238,0.3)", letterSpacing: "0.1em",
              }}>AFTER</span>
              <span className="mono" style={{ fontSize: 11, color: "var(--text-3)", letterSpacing: "0.08em" }}>已整理 · QLIVO 處理後</span>
              <span style={{ marginLeft: "auto" }}>
                <span className="mono" style={{
                  fontSize: 10, fontWeight: 800, padding: "4px 9px",
                  background: "rgba(52,211,153,0.15)", color: "var(--green)",
                  border: "1px solid rgba(52,211,153,0.35)",
                  borderRadius: 999, letterSpacing: "0.05em",
                }}>已標好優先級</span>
              </span>
            </div>

            <QuoteCard
              tone="after"
              quote="一打開信箱，就知道下一步該做什麼。"
              author="王志明"
              role="業務主管 · 導入 QLIVO 第 1 週"
            />

            {/* FOCUS area — flight target + blueprint + assembled card */}
            <div style={{ marginTop: 24, position: "relative", flex: 1, minHeight: 360 }}>
              <div className="mono" style={{ fontSize: 10, color: "var(--text-4)", letterSpacing: "0.12em", marginBottom: 12 }}>FOCUS · 接下來這封</div>

              {/* Invisible anchor — flight target point */}
              <div
                ref={targetRef}
                style={{
                  position: "absolute",
                  left: "50%", top: "50%",
                  transform: "translate(-50%, -40%)",
                  width: 1, height: 1,
                  pointerEvents: "none",
                }}
              />

              {/* Blueprint scaffold — visible during 'flying' */}
              <div
                className="bp-frame"
                style={{
                  position: "absolute",
                  left: 0, right: 0,
                  top: 26, height: 312,
                  opacity: flying ? 1 : 0,
                  transition: "opacity .4s ease",
                  overflow: "hidden",
                  pointerEvents: "none",
                }}
              >
                <div style={{ padding: 22, display: "flex", flexDirection: "column", gap: 14 }}>
                  <div style={{ display: "flex", gap: 10, alignItems: "center" }}>
                    <div style={{ width: 30, height: 30, borderRadius: 8, background: "rgba(59,130,246,0.12)" }} />
                    <div style={{ flex: 1, height: 8, background: "rgba(59,130,246,0.1)", borderRadius: 4 }} />
                  </div>
                  <div style={{ height: 14, width: "85%", background: "rgba(59,130,246,0.14)", borderRadius: 4 }} />
                  <div style={{ display: "flex", gap: 6 }}>
                    <div style={{ width: 60, height: 14, background: "rgba(59,130,246,0.1)", borderRadius: 7 }} />
                    <div style={{ width: 50, height: 14, background: "rgba(59,130,246,0.1)", borderRadius: 7 }} />
                    <div style={{ width: 70, height: 14, background: "rgba(59,130,246,0.1)", borderRadius: 7 }} />
                  </div>
                  <div style={{ height: 70, background: "rgba(34,211,238,0.06)", border: "1px solid rgba(34,211,238,0.18)", borderRadius: 10 }} />
                  <div style={{ height: 8, width: "60%", background: "rgba(59,130,246,0.1)", borderRadius: 4 }} />
                </div>
                <div className="bp-scanline" />
              </div>

              {/* Assembled real card — visible during 'after' */}
              <div
                key={`assemble-${cycle}`}
                className={showAfter ? "assemble-on" : ""}
                style={{
                  position: "relative",
                  opacity: showAfter ? 1 : 0,
                  transition: "opacity .3s ease",
                  pointerEvents: showAfter ? "auto" : "none",
                }}
              >
                {/* Decorative stacked back card */}
                <div
                  style={{
                    position: "absolute", inset: 0,
                    transform: "rotate(1.5deg) scale(0.97) translateY(6px)",
                    background: "var(--panel)", border: "1px solid var(--border-soft)",
                    borderRadius: 18, opacity: showAfter ? 0.5 : 0,
                    transition: "opacity .4s ease .2s",
                  }}
                />
                <AssembledEmailCard email={SAMPLE_EMAILS[0]} active={showAfter} />

                {/* Priority badge — pops in last */}
                <div
                  className={showAfter ? "priority-pop" : ""}
                  style={{
                    position: "absolute", top: -10, right: -10,
                    display: "inline-flex", alignItems: "center", gap: 6,
                    padding: "6px 12px 6px 8px",
                    background: "var(--bg)",
                    border: "1px solid rgba(248,113,113,0.55)",
                    borderRadius: 999,
                    boxShadow: "0 0 0 4px rgba(248,113,113,0.12), 0 6px 18px -4px rgba(248,113,113,0.4)",
                    zIndex: 5,
                    opacity: showAfter ? 1 : 0,
                    animationDelay: "750ms",
                  }}
                >
                  <span style={{
                    width: 8, height: 8, borderRadius: 4,
                    background: "var(--red)",
                    boxShadow: "0 0 0 3px rgba(248,113,113,0.25)",
                  }} className="pulse-dot" />
                  <span style={{ fontSize: 12, fontWeight: 800, color: "var(--red)", letterSpacing: "0.05em" }}>急</span>
                </div>
              </div>
            </div>

            {/* Replay button (subtle) */}
            <button
              onClick={replay}
              className="mono"
              style={{
                position: "absolute", bottom: 18, right: 18,
                background: "transparent", border: "1px solid var(--border-soft)",
                color: "var(--text-3)", padding: "6px 12px", borderRadius: 999,
                fontSize: 10, letterSpacing: "0.12em", cursor: "pointer", fontFamily: "var(--font-mono)",
                display: "inline-flex", alignItems: "center", gap: 6,
              }}
            ><IconRotateLeft size={11}/> 重播動畫</button>
          </div>
        </div>
      </div>
    </section>
  );
};

// EmailCard that assembles piece-by-piece when `active` becomes true
const AssembledEmailCard = ({ email, active }) => {
  const priorityColor = "var(--red)";
  const pieces = [
    { delay: "60ms", node: (
      <div style={{ display: "flex", alignItems: "flex-start", justifyContent: "space-between", gap: 12 }}>
        <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
          <div style={{ width: 36, height: 36, borderRadius: 10, background: email.color, display: "flex", alignItems: "center", justifyContent: "center", color: "white", fontWeight: 700, fontSize: 13 }}>{email.initials}</div>
          <div>
            <div style={{ fontSize: 13, fontWeight: 600 }}>{email.senderName}</div>
            <div className="mono" style={{ fontSize: 10.5, color: "var(--text-4)" }}>{email.senderEmail}</div>
          </div>
        </div>
        <div className="mono" style={{ fontSize: 10.5, color: "var(--text-4)" }}>{email.time}</div>
      </div>
    ) },
    { delay: "180ms", node: (
      <div style={{ fontSize: 17, fontWeight: 700, lineHeight: 1.3, letterSpacing: "-0.01em" }}>{email.subject}</div>
    ) },
    { delay: "300ms", node: (
      <div style={{ display: "flex", flexWrap: "wrap", gap: 6 }}>
        <span className="mono" style={{ fontSize: 10, padding: "5px 9px", borderRadius: 999, background: "rgba(59,130,246,0.12)", color: "var(--accent-2)", border: "1px solid rgba(59,130,246,0.25)", fontWeight: 600, letterSpacing: "0.05em" }}>{email.category}</span>
        <span className="mono" style={{ fontSize: 10, padding: "5px 9px", borderRadius: 999, background: "rgba(255,255,255,0.03)", color: priorityColor, border: `1px solid ${priorityColor}33`, fontWeight: 600, letterSpacing: "0.05em" }}>優先 · {email.priority}</span>
        {email.deadline && (
          <span className="mono" style={{ fontSize: 10, padding: "5px 9px", borderRadius: 999, background: "rgba(255,255,255,0.03)", color: "var(--text-3)", border: "1px solid var(--border)", fontWeight: 600, letterSpacing: "0.05em", display: "inline-flex", gap: 5, alignItems: "center" }}><IconClock size={10}/> {email.deadline}</span>
        )}
      </div>
    ) },
    { delay: "440ms", node: (
      <div style={{ background: "rgba(34,211,238,0.05)", border: "1px solid rgba(34,211,238,0.18)", borderRadius: 12, padding: 14 }}>
        <div className="mono" style={{ fontSize: 10, color: "var(--cyan)", letterSpacing: "0.12em", marginBottom: 8, display: "flex", gap: 6, alignItems: "center" }}>
          <IconSpark size={11}/> AI 摘要
        </div>
        <div style={{ fontSize: 13, lineHeight: 1.55, color: "var(--text-2)" }}>{email.summary}</div>
      </div>
    ) },
    { delay: "580ms", node: (
      <div style={{ display: "flex", gap: 10, alignItems: "flex-start", padding: "10px 0 0", borderTop: "1px dashed var(--border)" }}>
        <div className="mono" style={{ fontSize: 10, color: "var(--text-4)", letterSpacing: "0.12em", paddingTop: 2 }}>NEXT</div>
        <div style={{ fontSize: 13, color: "var(--text-2)", lineHeight: 1.5 }}>{email.nextStep}</div>
      </div>
    ) },
  ];
  return (
    <div
      style={{
        position: "relative",
        background: "var(--panel)",
        border: "1px solid var(--border)",
        borderRadius: 18,
        padding: 22,
        display: "flex", flexDirection: "column", gap: 14,
        boxShadow: "0 30px 60px -12px rgba(0,0,0,0.6), 0 0 0 1px rgba(255,255,255,0.04) inset",
      }}
    >
      {pieces.map((p, i) => (
        <div key={i} className="assemble-piece" style={{ animationDelay: p.delay }}>
          {p.node}
        </div>
      ))}
    </div>
  );
};

// ===== 03 Core Features =====
const FeatureRow = ({ idx, tag, title, body, bullets, visual, reverse }) => (
  <div className={`grid-feat${reverse ? " reverse" : ""}`}>
    <div style={{ direction: "ltr" }}>
      <div className="mono" style={{ fontSize: 11, color: "var(--accent-2)", letterSpacing: "0.18em", marginBottom: 18 }}>
        F.0{idx} / {tag}
      </div>
      <h3 className="feat-title">{title}</h3>
      <p className="feat-body">{body}</p>
      <ul style={{ listStyle: "none", padding: 0, margin: 0, display: "flex", flexDirection: "column", gap: 10 }}>
        {bullets.map((b, i) => (
          <li key={i} style={{ display: "flex", gap: 12, alignItems: "flex-start", fontSize: 14.5, color: "var(--text-2)" }}>
            <span style={{
              width: 18, height: 18, borderRadius: 9, marginTop: 2,
              background: "rgba(59,130,246,0.15)", color: "var(--accent-2)",
              display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0,
            }}><IconCheck size={11}/></span>
            <span>{b}</span>
          </li>
        ))}
      </ul>
    </div>
    <div style={{ direction: "ltr" }}>{visual}</div>
  </div>
);

const Features = () => {
  return (
    <section id="features" style={{ position: "relative" }} className="section-pad">
      <div className="container">
        <div className="features-header" style={{ display: "flex", justifyContent: "space-between", alignItems: "flex-end", flexWrap: "wrap", gap: 24, marginBottom: 80 }}>
          <SectionHeader
            tag="02 / 核心功能"
            title={<>四個動作，<br/>處理完一整天的信。</>}
            maxWidth={620}
          />
          <p className="mono" style={{ fontSize: 11, color: "var(--text-3)", letterSpacing: "0.15em", maxWidth: 280 }}>
            QLIVO 把 Email triage 重新拆解成<br/>四個獨立但連貫的工作流模組。
          </p>
        </div>

        <div className="features-gap">
          {/* F1 滑卡分類 */}
          <FeatureRow
            idx="1" tag="SWIPE TRIAGE"
            title="滑卡分類：用一根手指處理完一整封信"
            body="把每封信變成一張卡片。左滑稍後處理、右滑立即回覆。背景的兩張卡是接下來的工作 — 你永遠看得到下一步。"
            bullets={[
              "Tinder 式互動，配合手感回饋與動作標記",
              "支援滑鼠拖曳、鍵盤快捷鍵與行動裝置觸控",
              "「稍後處理」會自動聚集成一個提醒徽章，不會被遺漏",
            ]}
            visual={
              <MockBrowser height={420} label="qlivo.app/triage">
                <div style={{ position: "absolute", inset: 0, background: "var(--bg-2)" }} className="grid-bg-fine" />
                <div style={{ position: "absolute", inset: 0, display: "flex", alignItems: "center", justifyContent: "center" }}>
                  <div className="feat-mock-inner" style={{ position: "relative", width: 300, height: 380, maxWidth: "100%", margin: "0 auto" }}>
                    <div style={{
                      position: "absolute", inset: 0, transform: "rotate(3deg) scale(0.95)",
                      background: "var(--panel)", border: "1px solid var(--border-soft)", borderRadius: 18, opacity: 0.5,
                    }}/>
                    <div style={{
                      position: "absolute", inset: 0, transform: "rotate(-2deg) scale(0.97)",
                      background: "var(--panel)", border: "1px solid var(--border-soft)", borderRadius: 18, opacity: 0.8,
                    }}/>
                    <div className="feat-mock-swipe stack-shadow" style={{
                      position: "absolute", inset: 0,
                    }}>
                      <EmailCard email={SAMPLE_EMAILS[0]} compact />
                    </div>
                    <div style={{
                      position: "absolute", top: 20, left: -16,
                      padding: "8px 14px", borderRadius: 10,
                      border: "2px solid var(--green)", color: "var(--green)",
                      fontWeight: 800, fontSize: 14, letterSpacing: "0.05em",
                      transform: "rotate(-8deg)", background: "rgba(0,0,0,0.5)",
                    }}>立即回覆</div>
                  </div>
                </div>
              </MockBrowser>
            }
          />

          {/* F2 AI 摘要 */}
          <FeatureRow
            idx="2" tag="AI SUMMARY" reverse
            title="一鍵摘要：1,000 字的信，3 行讀完。"
            body="AI 解析正文、附件與對話脈絡，產出三段式摘要：發生什麼事、為什麼重要、你該做什麼。"
            bullets={[
              "自動辨識客戶詢價、會議邀請、行銷推播等 12 種類型",
              "標註截止日與優先級（急 / 高 / 中 / 低）",
              "「下一步」直接寫成可執行的祈使句，不只是描述",
            ]}
            visual={
              <MockBrowser height={420} label="qlivo.app/summary">
                <div style={{ position: "absolute", inset: 0, background: "var(--bg-2)", padding: 24 }}>
                  <div className="mono" style={{ fontSize: 10, color: "var(--text-4)", letterSpacing: "0.15em", marginBottom: 12 }}>SUBJECT</div>
                  <div style={{ fontSize: 16, fontWeight: 700, marginBottom: 24, color: "var(--text)" }}>MLCC 電容 FC-104K-50V 報價需求（5/22 前）</div>

                  <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr 1fr", gap: 10, marginBottom: 18 }}>
                    {[
                      { l: "類型", v: "客戶詢價", c: "var(--accent-2)" },
                      { l: "優先級", v: "急", c: "var(--red)" },
                      { l: "截止", v: "5/22 12:00", c: "var(--orange)" },
                    ].map((m, i) => (
                      <div key={i} style={{
                        padding: 12, borderRadius: 10,
                        background: "rgba(255,255,255,0.02)", border: "1px solid var(--border-soft)",
                      }}>
                        <div className="mono" style={{ fontSize: 9, color: "var(--text-4)", letterSpacing: "0.12em" }}>{m.l}</div>
                        <div style={{ marginTop: 6, fontSize: 14, fontWeight: 700, color: m.c }}>{m.v}</div>
                      </div>
                    ))}
                  </div>

                  <div style={{
                    background: "rgba(34,211,238,0.05)",
                    border: "1px solid rgba(34,211,238,0.2)",
                    borderRadius: 12, padding: 16,
                  }}>
                    <div className="mono" style={{ fontSize: 10, color: "var(--cyan)", letterSpacing: "0.12em", marginBottom: 10, display: "flex", gap: 6, alignItems: "center" }}>
                      <IconSpark size={11}/> AI 摘要 · 自動產生
                    </div>
                    <ol style={{ paddingLeft: 18, margin: 0, fontSize: 13.5, lineHeight: 1.7, color: "var(--text-2)" }}>
                      <li>凱盛採購想要 50,000 pcs MLCC，要求 5/22 前完成報價評估。</li>
                      <li>需提供：單價／級距、MOQ、交期、現貨狀況、付款條件。</li>
                      <li><span style={{ color: "var(--cyan)" }}>下一步：</span>請業務確認現貨後，回覆完整報價單與付款條件。</li>
                    </ol>
                  </div>
                </div>
              </MockBrowser>
            }
          />

          {/* F3 浮動 AI 對話 */}
          <FeatureRow
            idx="3" tag="ASK ACROSS INBOX"
            title="跨郵件問答：「客戶上個月說了什麼？」直接問 AI。"
            body="角落的浮動對話視窗，可以對整個信箱發問。Qlivo 會引用具體郵件、給出時間線、並幫你跳轉到那封信。"
            bullets={[
              "自然語言查詢，不需要記得關鍵字",
              "回覆會附上引用來源，每一句都能追溯",
              "支援延伸動作：「幫我寫一封跟進信」、「列出所有未回覆的詢價」",
            ]}
            visual={
              <MockBrowser height={420} label="qlivo.app/chat">
                <div style={{ position: "absolute", inset: 0, background: "var(--bg-2)" }} className="grid-bg-fine" />
                <div style={{
                  position: "absolute", bottom: 20, right: 20, width: 320, maxWidth: "90%",
                  background: "var(--panel)", border: "1px solid var(--border)",
                  borderRadius: 16, overflow: "hidden",
                  boxShadow: "0 20px 50px -10px rgba(0,0,0,0.6)",
                }}>
                  <div style={{ padding: "12px 16px", borderBottom: "1px solid var(--border-soft)", display: "flex", alignItems: "center", gap: 8 }}>
                    <div style={{ width: 24, height: 24, borderRadius: 8, background: "linear-gradient(135deg, #3B82F6, #22D3EE)", display: "flex", alignItems: "center", justifyContent: "center" }}>
                      <IconBot size={13} style={{ color: "white" }} />
                    </div>
                    <div style={{ fontSize: 13, fontWeight: 700 }}>Qlivo Assistant</div>
                    <span style={{ marginLeft: "auto", width: 6, height: 6, borderRadius: 3, background: "var(--green)" }} className="pulse-dot" />
                  </div>
                  <div style={{ padding: 16, display: "flex", flexDirection: "column", gap: 12 }}>
                    <div style={{
                      alignSelf: "flex-end", maxWidth: "85%",
                      background: "var(--accent)", color: "white",
                      padding: "8px 12px", borderRadius: "12px 12px 4px 12px",
                      fontSize: 13, lineHeight: 1.5,
                    }}>凱盛電子最近在問什麼？</div>
                    <div style={{
                      maxWidth: "90%",
                      background: "var(--bg-3)", color: "var(--text-2)",
                      padding: "10px 12px", borderRadius: "12px 12px 12px 4px",
                      fontSize: 13, lineHeight: 1.55,
                      border: "1px solid var(--border-soft)",
                    }}>
                      5 月以來凱盛採購林佳穎來信兩封，主題都是 MLCC 報價，最新一封 5/17 要求 5/22 前回覆。
                      <button style={{
                        display: "block", marginTop: 8,
                        background: "var(--accent)", color: "white",
                        border: "none", padding: "6px 10px", borderRadius: 8,
                        fontSize: 11, fontWeight: 700, cursor: "pointer",
                        fontFamily: "inherit",
                      }}>→ 跳轉到該郵件</button>
                    </div>
                  </div>
                  <div style={{ padding: 12, borderTop: "1px solid var(--border-soft)", display: "flex", gap: 8, alignItems: "center" }}>
                    <input placeholder="問點什麼..." style={{
                      flex: 1, background: "transparent", border: "none", outline: "none",
                      color: "var(--text)", fontSize: 13, fontFamily: "inherit",
                    }} />
                    <div style={{ width: 28, height: 28, borderRadius: 8, background: "var(--accent)", display: "flex", alignItems: "center", justifyContent: "center", color: "white" }}>
                      <IconSend size={13}/>
                    </div>
                  </div>
                </div>
              </MockBrowser>
            }
          />

          {/* F4 智能回覆 */}
          <FeatureRow
            idx="4" tag="REPLY DRAFTING" reverse
            title="智能回覆：點一下生成草稿，調語氣、長度、語言。"
            body="Qlivo 讀懂信件脈絡與你的歷史回覆風格，產出 80 分的草稿。剩下的 20 分，你只負責加上個人判斷。"
            bullets={[
              "三種語氣：正式 / 中性 / 友善，可即時切換",
              "支援多語生成（繁中、英、日），用詞符合商務情境",
              "保留你常用的開場與結尾簽名，不會每次都從零生成",
            ]}
            visual={
              <MockBrowser height={420} label="qlivo.app/reply">
                <div style={{ position: "absolute", inset: 0, background: "var(--bg-2)", padding: 20, display: "flex", flexDirection: "column", gap: 14 }}>
                  <div style={{ display: "flex", gap: 8, flexWrap: "wrap" }}>
                    {["正式", "中性", "友善"].map((t, i) => (
                      <span key={i} style={{
                        padding: "6px 12px", borderRadius: 8, fontSize: 12, fontWeight: 600,
                        background: i === 0 ? "var(--accent)" : "var(--bg-3)",
                        color: i === 0 ? "white" : "var(--text-2)",
                        border: i === 0 ? "1px solid var(--accent)" : "1px solid var(--border-soft)",
                      }}>{t}</span>
                    ))}
                    <span style={{ marginLeft: "auto", display: "flex", gap: 8 }}>
                      <span style={{ padding: "6px 12px", borderRadius: 8, fontSize: 12, fontWeight: 600, background: "var(--bg-3)", color: "var(--text-2)", border: "1px solid var(--border-soft)", display: "inline-flex", alignItems: "center", gap: 6 }}>
                        <IconWand size={12}/> 重新生成
                      </span>
                    </span>
                  </div>

                  <div style={{ flex: 1, background: "var(--panel)", border: "1px solid var(--border-soft)", borderRadius: 12, padding: 16, fontSize: 13, lineHeight: 1.7, color: "var(--text-2)", overflowY: "auto" }}>
                    <div>林採購您好，</div>
                    <div style={{ marginTop: 10 }}>感謝您的詢價來信。針對 MLCC 電容 FC-104K-50V，依您 50,000 pcs 的需求，<span style={{ background: "rgba(59,130,246,0.18)", borderRadius: 3, padding: "0 3px", color: "var(--text)" }}>我們建議單價為 NT$0.42 / pcs，級距為 30K / 50K / 100K</span>，標準交期 4 週，目前現貨足以支應首批 30K。</div>
                    <div style={{ marginTop: 10 }}>付款條件 T/T 30 days，運費由本公司負擔。完整報價單將於今日下午另附 PDF 寄出，請查收。</div>
                    <div style={{ marginTop: 10 }}>若有任何問題，歡迎隨時來信或來電。<br/>謝謝<br/>業務 / 王志明</div>
                  </div>

                  <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
                    <div className="mono" style={{ fontSize: 10, color: "var(--text-4)", letterSpacing: "0.1em" }}>AI 生成 · 0.7s</div>
                    <div style={{
                      padding: "10px 16px", borderRadius: 10,
                      background: "var(--accent)", color: "white",
                      fontSize: 13, fontWeight: 700, display: "inline-flex", alignItems: "center", gap: 8,
                    }}><IconSend size={13}/> 送出回覆</div>
                  </div>
                </div>
              </MockBrowser>
            }
          />
        </div>
      </div>
    </section>
  );
};

// ===== 05 Why Qlivo =====
const WhyQlivo = () => {
  const reasons = [
    {
      icon: <IconBot size={22}/>,
      title: "真實 AI，不是模板",
      body: "由先進的 AI 引擎驅動，理解上下文與業務脈絡。不是把信件塞進固定範本，而是真的讀懂每一封。",
      stat: "12 種", statLabel: "自動辨識的郵件類型",
    },
    {
      icon: <IconShield size={22}/>,
      title: "你的資料只屬於你",
      body: "直接連線到貴公司既有的郵件伺服器（IMAP / SMTP），資料不留存於 Qlivo 伺服器。",
      stat: "0 byte", statLabel: "永久儲存的郵件內容",
    },
    {
      icon: <IconClock size={22}/>,
      title: "為決策者設計",
      body: "30 秒看完一天信箱。Qlivo 不是讓你「處理更多」，而是讓你「處理更少」— 把判斷力留給真正重要的決定。",
      stat: "3 分鐘", statLabel: "處理 142 封信的中位時間",
    },
  ];

  return (
    <section id="why" style={{ position: "relative", background: "var(--bg-2)", borderTop: "1px solid var(--border-soft)", borderBottom: "1px solid var(--border-soft)" }} className="section-pad">
      <div className="container">
        <SectionHeader
          tag="04 / 為什麼是 QLIVO"
          title="這不是另一個 AI 工具。"
          sub="市場上已經有太多用 GPT 包裝的「智慧助理」。Qlivo 在三件事上做了不同的選擇。"
          align="center"
          maxWidth={680}
        />

        <div className="grid-why" style={{ marginTop: 80 }}>
          {reasons.map((r, i) => (
            <div key={i} style={{
              background: "var(--bg)",
              border: "1px solid var(--border-soft)",
              borderRadius: 18,
              padding: 32,
              display: "flex", flexDirection: "column",
              minHeight: 320,
            }}>
              <div style={{ display: "flex", alignItems: "center", gap: 12, marginBottom: 24 }}>
                <div style={{
                  width: 44, height: 44, borderRadius: 12,
                  background: "rgba(59,130,246,0.1)",
                  border: "1px solid rgba(59,130,246,0.2)",
                  color: "var(--accent-2)",
                  display: "flex", alignItems: "center", justifyContent: "center",
                }}>{r.icon}</div>
                <div className="mono" style={{ fontSize: 10, color: "var(--text-4)", letterSpacing: "0.18em" }}>0{i+1}</div>
              </div>
              <h3 style={{ fontSize: 22, fontWeight: 700, lineHeight: 1.3, letterSpacing: "-0.02em", marginBottom: 14 }}>{r.title}</h3>
              <p style={{ fontSize: 14.5, lineHeight: 1.65, color: "var(--text-2)", flex: 1 }}>{r.body}</p>
              <div style={{ marginTop: 24, paddingTop: 20, borderTop: "1px solid var(--border-soft)" }}>
                <div style={{ fontSize: 30, fontWeight: 800, letterSpacing: "-0.02em", color: "var(--text)" }}>{r.stat}</div>
                <div className="mono" style={{ fontSize: 10.5, color: "var(--text-3)", letterSpacing: "0.12em", marginTop: 4, textTransform: "uppercase" }}>{r.statLabel}</div>
              </div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
};

// ===== Final CTA + Footer =====
const FinalCTA = () => {
  const [form, setForm] = useState({ company: "", name: "", email: "", size: "10-50" });
  const [submitted, setSubmitted] = useState(false);
  return (
    <section id="cta" style={{ position: "relative", overflow: "hidden" }} className="section-pad">
      <div className="hide-mobile-decor glow-blue" style={{
        position: "absolute", left: "50%", top: "30%", transform: "translateX(-50%)",
        width: "min(900px, 120vw)", height: 600, pointerEvents: "none",
      }} />
      <div className="container" style={{ position: "relative" }}>
        <div className="grid-cta cta-panel" style={{
          background: "linear-gradient(180deg, var(--bg-2), var(--bg-3))",
          border: "1px solid var(--border)",
          borderRadius: 28,
          boxShadow: "0 0 0 1px rgba(59,130,246,0.1), 0 40px 100px -20px rgba(59,130,246,0.25)",
          position: "relative", overflow: "hidden",
        }}>
          <div className="grid-bg-fine" style={{ position: "absolute", inset: 0, opacity: 0.5, pointerEvents: "none" }} />
          <div style={{ position: "relative" }}>
            <div className="mono" style={{ fontSize: 11, color: "var(--accent-2)", letterSpacing: "0.18em", marginBottom: 20 }}>
              05 / READY?
            </div>
            <h2 style={{ fontSize: "clamp(34px, 4vw, 52px)", lineHeight: 1.05, letterSpacing: "-0.03em", fontWeight: 800, marginBottom: 24 }}>
              準備好把每天 2 小時，<br/>還給自己了嗎？
            </h2>
            <p style={{ fontSize: 17, lineHeight: 1.65, color: "var(--text-2)", maxWidth: 460, marginBottom: 32 }}>
              30 分鐘的 Demo，我們會用你團隊真實的信件樣本，現場示範整個工作流。沒有銷售話術，只有實際對比。
            </p>
            <div style={{ display: "flex", gap: 24, flexWrap: "wrap", marginTop: 28 }}>
              {[
                { k: "30 分鐘", v: "1 對 1 線上 Demo" },
                { k: "免費", v: "POC 試用期 14 天" },
                { k: "中文", v: "原生繁中業務支援" },
              ].map((t, i) => (
                <div key={i}>
                  <div style={{ fontSize: 20, fontWeight: 800, letterSpacing: "-0.02em" }}>{t.k}</div>
                  <div className="mono" style={{ fontSize: 10, color: "var(--text-3)", letterSpacing: "0.1em", marginTop: 2 }}>{t.v}</div>
                </div>
              ))}
            </div>
          </div>

          <div style={{ position: "relative", background: "var(--panel)", border: "1px solid var(--border)", borderRadius: 18, padding: 28 }}>
            {!submitted ? (
              <form onSubmit={(e) => { e.preventDefault(); setSubmitted(true); }}>
                <div className="mono" style={{ fontSize: 10, color: "var(--text-3)", letterSpacing: "0.18em", marginBottom: 20 }}>預約 DEMO · 30 秒填完</div>
                {[
                  { k: "company", l: "公司名稱", p: "例如：凱盛電子" },
                  { k: "name", l: "您的稱呼", p: "例如：王志明 / 業務主管" },
                  { k: "email", l: "聯絡 Email", p: "name@company.com" },
                ].map((f, i) => (
                  <div key={i} style={{ marginBottom: 16 }}>
                    <label className="mono" style={{ display: "block", fontSize: 10, color: "var(--text-3)", letterSpacing: "0.12em", marginBottom: 6, textTransform: "uppercase" }}>{f.l}</label>
                    <input
                      required
                      value={form[f.k]}
                      onChange={(e) => setForm({ ...form, [f.k]: e.target.value })}
                      placeholder={f.p}
                      style={{
                        width: "100%", padding: "12px 14px",
                        background: "var(--bg-2)", border: "1px solid var(--border-soft)",
                        borderRadius: 10, color: "var(--text)", fontSize: 14,
                        fontFamily: "inherit", outline: "none",
                      }}
                    />
                  </div>
                ))}
                <div style={{ marginBottom: 24 }}>
                  <label className="mono" style={{ display: "block", fontSize: 10, color: "var(--text-3)", letterSpacing: "0.12em", marginBottom: 8, textTransform: "uppercase" }}>團隊規模</label>
                  <div className="cta-form-size-grid" style={{ display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: 6 }}>
                    {["1-10", "10-50", "50-200", "200+"].map((s) => (
                      <button key={s} type="button"
                        onClick={() => setForm({ ...form, size: s })}
                        style={{
                          padding: "10px 0",
                          border: "1px solid " + (form.size === s ? "var(--accent)" : "var(--border-soft)"),
                          background: form.size === s ? "rgba(59,130,246,0.12)" : "var(--bg-2)",
                          color: form.size === s ? "var(--text)" : "var(--text-2)",
                          borderRadius: 8, fontSize: 12, fontWeight: 600, cursor: "pointer", fontFamily: "inherit",
                        }}>{s}</button>
                    ))}
                  </div>
                </div>
                <button type="submit" className="btn btn-primary" style={{ width: "100%", justifyContent: "center", padding: "16px 24px" }}>
                  預約 30 分鐘 Demo <IconArrowRight size={16}/>
                </button>
                <div className="mono" style={{ marginTop: 14, fontSize: 10, color: "var(--text-4)", letterSpacing: "0.08em", textAlign: "center" }}>
                  我們會在 1 個工作日內聯繫您 · 不會發送行銷郵件
                </div>
              </form>
            ) : (
              <div style={{ textAlign: "center", padding: "20px 0" }}>
                <div style={{ width: 56, height: 56, borderRadius: 28, background: "rgba(52,211,153,0.15)", color: "var(--green)", display: "inline-flex", alignItems: "center", justifyContent: "center", marginBottom: 20 }}>
                  <IconCheck size={26}/>
                </div>
                <div style={{ fontSize: 22, fontWeight: 800, letterSpacing: "-0.02em", marginBottom: 10 }}>已收到您的預約！</div>
                <p style={{ fontSize: 14, color: "var(--text-2)", lineHeight: 1.6 }}>業務團隊會在 1 個工作日內透過 Email 與您聯繫，敲定 Demo 時段。</p>
              </div>
            )}
          </div>
        </div>
      </div>
    </section>
  );
};

const Footer = () => (
  <footer style={{ borderTop: "1px solid var(--border-soft)", padding: "48px 0 36px" }}>
    <div className="container">
      <div className="grid-footer">
        <div>
          <div style={{ display: "flex", alignItems: "center", gap: 10, marginBottom: 16 }}>
            <div style={{
              width: 28, height: 28, borderRadius: 8,
              background: "linear-gradient(135deg, #3B82F6, #22D3EE)",
              display: "flex", alignItems: "center", justifyContent: "center",
            }}><IconZap size={14} style={{ color: "white" }}/></div>
            <span style={{ fontWeight: 900, fontSize: 17, letterSpacing: "-0.02em" }}>Qlivo</span>
          </div>
          <p style={{ fontSize: 13, lineHeight: 1.6, color: "var(--text-3)", maxWidth: 320 }}>
            智慧郵件管理與 AI 助理解決方案。<br/>Qlivo your work，把判斷力還給人。
          </p>
        </div>
        {[
          { h: "產品", l: ["核心功能", "互動 Demo", "定價方案 (Coming)", "API 文件 (Coming)"] },
          { h: "公司", l: ["關於我們", "聯絡業務", "Blog", "招募中"] },
          { h: "法律", l: ["隱私權政策", "服務條款", "資料處理協議", "Cookie 設定"] },
        ].map((c, i) => (
          <div key={i}>
            <div className="mono" style={{ fontSize: 10, color: "var(--text-3)", letterSpacing: "0.15em", marginBottom: 14, textTransform: "uppercase" }}>{c.h}</div>
            <ul style={{ listStyle: "none", padding: 0, margin: 0, display: "flex", flexDirection: "column", gap: 8 }}>
              {c.l.map((item, j) => (
                <li key={j} style={{ fontSize: 13, color: "var(--text-2)", cursor: "pointer" }}>{item}</li>
              ))}
            </ul>
          </div>
        ))}
      </div>
      <div className="h-line" />
      <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", marginTop: 24, flexWrap: "wrap", gap: 16 }}>
        <div className="mono" style={{ fontSize: 11, color: "var(--text-4)", letterSpacing: "0.08em" }}>
          © 2026 Qlivo Inc. · contact@qlivo.app · 台北市信義區 (placeholder)
        </div>
        <div style={{ display: "flex", gap: 14, color: "var(--text-3)" }}>
          <span className="mono" style={{ fontSize: 11, letterSpacing: "0.08em" }}>POWERED BY AI</span>
          <span style={{ width: 6, height: 6, borderRadius: 3, background: "var(--green)" }} className="pulse-dot" />
          <span className="mono" style={{ fontSize: 11, letterSpacing: "0.08em" }}>SYSTEM ONLINE</span>
        </div>
      </div>
    </div>
  </footer>
);

Object.assign(window, { PainPoint, Features, WhyQlivo, FinalCTA, Footer });
