// Animated review mockup, narrates document collaboration and negotiation:
// 1) Sofia highlights "unlimited liability" and comments asking for a cap
// 2) Lukas (Legal) joins the document
// 3) Lukas suggests 3x annual fees cap
// 4) Sofia accepts Lukas's counter-proposal
// 5) The text in the document updates with redline strike-through and green insertion
// 6) Resolution toast appears at the bottom
// 7) Hold and loop

function ReviewAnimation({ bgImage }) {
  const [tick, setTick] = React.useState(0);
  React.useEffect(() => {
    const id = setInterval(() => setTick((t) => (t + 1) % 9), 1200);
    return () => clearInterval(id);
  }, []);
  const show = (n) => tick >= n;

  return (
    <div className="hero-mockup-container">
      <div
        style={{
          width: "100%",
          height: "540px",
          border: "1px solid var(--line-strong)",
          borderRadius: "28px",
          boxShadow: "0 20px 40px rgba(14,14,16,0.03), 0 1px 3px rgba(14,14,16,0.01)",
          position: "relative",
          overflow: "hidden",
          display: "flex",
          alignItems: "center",
          justifyContent: "center",
          padding: "24px",
        }}
      >
        {/* Background Image Layer */}
        <div
          style={{
            position: "absolute",
            inset: 0,
            backgroundImage: "url('" + (bgImage || "/images/review_bg.png") + "')",
            backgroundSize: "cover",
            backgroundPosition: "center",
            zIndex: 0,
            pointerEvents: "none",
          }}
        />
        {/* Subtle white wash overlay */}
        <div
          style={{
            position: "absolute",
            inset: 0,
            background: "rgba(255, 255, 255, 0.12)",
            zIndex: 1,
            pointerEvents: "none",
          }}
        />

        {/* Card wrapper */}
        <div style={{ position: "relative", zIndex: 2, width: "100%", maxWidth: "420px" }}>
    <div style={{ position: "relative", width: "100%" }}>
      <MockPanel
        title="Partnership_Agreement_v2.docx"
        sub={
          <div style={{ display: "flex", alignItems: "center", gap: 6 }}>
            {/* Editor avatars */}
            <div style={{ display: "flex", alignItems: "center", marginRight: 4 }}>
              {/* Sofia */}
              <div style={{
                width: 18, height: 18, borderRadius: "50%",
                background: "linear-gradient(135deg, #3B82F6, #1D4ED8)",
                color: "#fff", fontSize: 9, fontWeight: 600,
                display: "flex", alignItems: "center", justifyContent: "center",
                border: "1.5px solid var(--bg-elevated)",
                zIndex: 3
              }}>S</div>
              {/* Marcus */}
              <div style={{
                width: 18, height: 18, borderRadius: "50%",
                background: "linear-gradient(135deg, #F59E0B, #D97706)",
                color: "#fff", fontSize: 9, fontWeight: 600,
                display: "flex", alignItems: "center", justifyContent: "center",
                border: "1.5px solid var(--bg-elevated)",
                marginLeft: -6,
                zIndex: 2
              }}>M</div>
              {/* Lukas - appears at tick >= 2 */}
              <div style={{
                width: 18, height: 18, borderRadius: "50%",
                background: "linear-gradient(135deg, #7B61FF, #4A2FB4)",
                color: "#fff", fontSize: 9, fontWeight: 600,
                display: "flex", alignItems: "center", justifyContent: "center",
                border: "1.5px solid var(--bg-elevated)",
                marginLeft: -6,
                zIndex: 1,
                opacity: show(2) ? 1 : 0,
                transform: show(2) ? "scale(1)" : "scale(0.5)",
                transition: "all 300ms cubic-bezier(.2,.8,.2,1)"
              }}>L</div>
            </div>
            <span style={{ fontSize: 11, color: "var(--ink-3)", fontWeight: 500, fontFamily: "var(--font-sans)" }}>
              {show(2) ? "3 editing" : "2 editing"}
            </span>
          </div>
        }
      >
        {/* Document Editor Toolbar */}
        <div style={{
          display: "flex",
          alignItems: "center",
          gap: 12,
          borderBottom: "1px solid var(--line)",
          paddingBottom: 10,
          marginBottom: 14,
          color: "var(--ink-4)"
        }}>
          <ToolbarIcon name="undo" />
          <ToolbarIcon name="redo" />
          <div style={{ width: 1, height: 14, background: "var(--line)" }} />
          <span style={{ fontSize: 11, fontWeight: 500, color: "var(--ink-2)", fontFamily: "var(--font-sans)" }}>Normal text</span>
          <div style={{ width: 1, height: 14, background: "var(--line)" }} />
          <ToolbarIcon name="bold" />
          <ToolbarIcon name="italic" />
          <ToolbarIcon name="underline" />
          <ToolbarIcon name="comment" active={show(1) && !show(6)} />
        </div>

        {/* Workspace Area: Page on Left, Comments on Right */}
        <div style={{ display: "flex", gap: 14, minHeight: 230, position: "relative" }}>
          {/* Document Page Column */}
          <div style={{ flex: 1.15, minWidth: 0 }}>
            {/* Page Paper */}
            <div style={{
              background: "var(--bg-elevated)",
              border: "1px solid var(--line)",
              borderRadius: 4,
              padding: "16px 14px",
              minHeight: 210,
              boxShadow: "0 2px 8px rgba(0,0,0,0.02)"
            }}>
              <div style={{ fontSize: 9, color: "var(--ink-4)", textTransform: "uppercase", letterSpacing: "0.05em", marginBottom: 6, fontWeight: 600, fontFamily: "var(--font-sans)" }}>
                Section 12 · Indemnification
              </div>
              <h4 style={{ fontSize: 11.5, fontWeight: 600, color: "var(--ink-1)", margin: "0 0 8px", fontFamily: "var(--font-sans)" }}>
                12.4 Limitation of Liability
              </h4>
              <p style={{ fontSize: 11, color: "var(--ink-2)", lineHeight: 1.5, margin: 0, fontFamily: "var(--font-sans)" }}>
                Except for breaches of Section 8 (Confidentiality), each party's total aggregate liability under this Agreement shall be{" "}
                <span style={{
                  background: show(5) ? "transparent" : show(1) ? "rgba(245, 158, 11, 0.16)" : "transparent",
                  borderBottom: show(5) ? "none" : show(1) ? "1.5px solid #F59E0B" : "none",
                  transition: "all 300ms ease"
                }}>
                  {show(5) ? (
                    <span style={{ textDecoration: "line-through", color: "var(--ink-4)", marginRight: 4 }}>
                      unlimited.
                    </span>
                  ) : (
                    "unlimited."
                  )}
                </span>{" "}
                {show(5) && (
                  <span style={{
                    background: "rgba(34, 197, 94, 0.15)",
                    color: "#0F7B3F",
                    padding: "2px 4px",
                    borderRadius: 3,
                    fontWeight: 500,
                    whiteSpace: "nowrap",
                    display: "inline-block",
                    border: "1px solid rgba(34, 197, 94, 0.25)",
                    animation: "collabHighlightPulse 1.5s ease-out infinite alternate"
                  }}>
                    capped at 3x annual contract value.
                  </span>
                )}
              </p>
            </div>
          </div>

          {/* Comments Sidebar Column */}
          <div style={{ flex: 0.85, minWidth: 0, display: "flex", flexDirection: "column", gap: 8 }}>
            {/* Sofia's Comment Card */}
            <div style={{
              background: "var(--bg-soft)",
              border: "1px solid var(--line)",
              borderRadius: 6,
              padding: 8,
              fontSize: 10.5,
              opacity: show(1) ? 1 : 0,
              transform: show(1) ? "translateY(0)" : "translateY(8px)",
              transition: "all 300ms cubic-bezier(.2,.8,.2,1)",
              fontFamily: "var(--font-sans)"
            }}>
              <div style={{ display: "flex", justifyContent: "space-between", marginBottom: 4, alignItems: "center" }}>
                <span style={{ fontWeight: 600, color: "var(--ink-1)", display: "flex", alignItems: "center", gap: 4 }}>
                  <span style={{ width: 12, height: 12, borderRadius: "50%", background: "#3B82F6", color: "#fff", fontSize: 7, display: "flex", alignItems: "center", justifyContent: "center" }}>S</span>
                  Sofia
                </span>
                <span style={{ color: "var(--ink-4)", fontSize: 9 }}>10:14 AM</span>
              </div>
              <div style={{ color: "var(--ink-3)", lineHeight: 1.3 }}>
                Our board won't accept unlimited liability. Can we cap this?
              </div>
            </div>

            {/* Lukas's Reply Card */}
            <div style={{
              background: "var(--bg-soft)",
              border: "1px solid " + (show(4) ? "rgba(34, 197, 94, 0.3)" : "var(--line)"),
              borderRadius: 6,
              padding: 8,
              fontSize: 10.5,
              marginLeft: 8,
              position: "relative",
              opacity: show(3) ? 1 : 0,
              transform: show(3) ? "translateY(0)" : "translateY(8px)",
              transition: "all 300ms cubic-bezier(.2,.8,.2,1), border-color 300ms",
              fontFamily: "var(--font-sans)"
            }}>
              <div style={{ display: "flex", justifyContent: "space-between", marginBottom: 4, alignItems: "center" }}>
                <span style={{ fontWeight: 600, color: "#7B61FF", display: "flex", alignItems: "center", gap: 4 }}>
                  <span style={{ width: 12, height: 12, borderRadius: "50%", background: "#7B61FF", color: "#fff", fontSize: 7, display: "flex", alignItems: "center", justifyContent: "center" }}>L</span>
                  Lukas (Legal)
                </span>
                <span style={{ color: "var(--ink-4)", fontSize: 9 }}>10:15 AM</span>
              </div>
              <div style={{ color: "var(--ink-3)", lineHeight: 1.3 }}>
                Confidentiality and IP capped at 3x annual fees.
              </div>
              
              {/* Acceptance Badge */}
              {show(4) && (
                <div style={{
                  display: "flex",
                  alignItems: "center",
                  gap: 4,
                  fontSize: 9.5,
                  fontWeight: 600,
                  color: "#0F7B3F",
                  marginTop: 6,
                  background: "rgba(34, 197, 94, 0.08)",
                  padding: "2px 6px",
                  borderRadius: 4,
                  width: "fit-content",
                  border: "1px solid rgba(34, 197, 94, 0.15)"
                }}>
                  <svg width="9" height="9" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="3.5">
                    <polyline points="20 6 9 17 4 12" />
                  </svg>
                  Accepted by Sofia
                </div>
              )}
            </div>
          </div>
        </div>

        {/* Resolution Toast */}
        <div style={{
          position: "absolute",
          bottom: 12,
          left: "50%",
          transform: show(6) ? "translate(-50%, 0)" : "translate(-50%, 16px)",
          opacity: show(6) ? 1 : 0,
          transition: "all 400ms cubic-bezier(.2,.8,.2,1)",
          background: "#0E0E10",
          color: "#fff",
          borderRadius: 8,
          padding: "8px 14px",
          fontSize: 11,
          fontWeight: 500,
          display: "flex",
          alignItems: "center",
          gap: 8,
          boxShadow: "0 10px 24px -6px rgba(0,0,0,0.30)",
          zIndex: 10,
          whiteSpace: "nowrap",
          fontFamily: "var(--font-sans)"
        }}>
          <span style={{ color: "#22C55E", fontSize: 12 }}>✓</span>
          <span>Negotiation resolved</span>
          <span style={{ width: 1, height: 10, background: "rgba(255,255,255,0.15)" }} />
          <span style={{ color: "#C7BFFF" }}>Redlines merged</span>
        </div>
      </MockPanel>
    </div>
    </div>
      </div>
      <style>{`
        @keyframes collabHighlightPulse {
          0%   { background-color: rgba(34, 197, 94, 0.12); }
          100% { background-color: rgba(34, 197, 94, 0.22); }
        }
      `}</style>
    </div>
  );
}

// Helper component for styled editor toolbar buttons
function ToolbarIcon({ name, active }) {
  const icons = {
    undo: (
      <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5">
        <path d="M3 7v6h6" />
        <path d="M21 17a9 9 0 0 0-9-9 9 9 0 0 0-6 2.3L3 13" />
      </svg>
    ),
    redo: (
      <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5">
        <path d="M21 7v6h-6" />
        <path d="M3 17a9 9 0 0 1 9-9 9 9 0 0 1 6 2.3l3 5" />
      </svg>
    ),
    bold: (
      <span style={{ fontWeight: 700, fontSize: 11.5, fontFamily: "serif" }}>B</span>
    ),
    italic: (
      <span style={{ fontStyle: "italic", fontSize: 11.5, fontFamily: "serif" }}>I</span>
    ),
    underline: (
      <span style={{ textDecoration: "underline", fontSize: 11.5, fontFamily: "serif" }}>U</span>
    ),
    comment: (
      <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5">
        <path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z" />
      </svg>
    )
  };

  return (
    <div style={{
      width: 22,
      height: 22,
      display: "flex",
      alignItems: "center",
      justifyContent: "center",
      borderRadius: 4,
      background: active ? "var(--accent-soft)" : "transparent",
      color: active ? "var(--accent)" : "var(--ink-4)",
      cursor: "pointer",
      transition: "all 120ms ease"
    }}>
      {icons[name]}
    </div>
  );
}

window.ReviewAnimation = ReviewAnimation;

function NegotiationAnimation() {
  const [tick, setTick] = React.useState(0);
  React.useEffect(() => {
    const id = setInterval(() => setTick((t) => (t + 1) % 8), 1400);
    return () => clearInterval(id);
  }, []);
  const show = (n) => tick >= n;

  const versions = [
    { v: "v1", by: "You", date: "Jun 2", status: "Sent" },
    { v: "v2", by: "Counterparty", date: "Jun 5", status: "Redlined" },
    { v: "v3", by: "You", date: "Jun 7", status: show(6) ? "Final" : show(4) ? "Pending" : "Draft" },
  ];

  const statusDot = show(6) ? "#1F8A4C" : show(4) ? "#D97706" : "#7B61FF";
  const statusLabel = show(6) ? "ready to sign" : show(4) ? "approval pending" : "negotiating";

  return (
    <div className="hero-mockup-container">
      <div style={{
        width: "100%", height: "540px",
        border: "1px solid var(--line-strong)", borderRadius: "28px",
        boxShadow: "0 20px 40px rgba(14,14,16,0.03), 0 1px 3px rgba(14,14,16,0.01)",
        position: "relative", overflow: "hidden",
        display: "flex", alignItems: "center", justifyContent: "center", padding: "24px",
      }}>
        <div style={{ position: "absolute", inset: 0, backgroundImage: "url('/images/negotiation_bg.png')", backgroundSize: "cover", backgroundPosition: "center", zIndex: 0, pointerEvents: "none" }} />
        <div style={{ position: "absolute", inset: 0, background: "rgba(255, 255, 255, 0.12)", zIndex: 1, pointerEvents: "none" }} />

        <div style={{ position: "relative", zIndex: 2, width: "100%", maxWidth: "420px" }}>
      <MockPanel
        title="Vendor_MSA_Negotiation"
        sub={
          <span style={{ display: "inline-flex", alignItems: "center", gap: 6 }}>
            <span style={{ width: 6, height: 6, borderRadius: "50%", background: statusDot, boxShadow: show(6) ? "0 0 0 4px rgba(31,138,76,0.18)" : "none", transition: "background 300ms, box-shadow 300ms" }} />
            {statusLabel}
          </span>
        }>

        {/* Version timeline */}
        <div style={{ display: "flex", flexDirection: "column", gap: 0, marginBottom: 14 }}>
          {versions.map((ver, i) => {
            const isCounterparty = i === 1;
            const visible = i < 2 || show(1);
            const statusColor = ver.status === "Final" ? "#1F8A4C" : ver.status === "Redlined" || ver.status === "Pending" ? "#D97706" : "var(--ink-4)";
            return (
              <div key={i} style={{
                display: "flex", alignItems: "center", gap: 10,
                padding: "8px 10px",
                background: i === 2 && show(1) ? "rgba(110,86,207,0.06)" : "transparent",
                borderRadius: 6, opacity: visible ? 1 : 0,
                transform: visible ? "translateY(0)" : "translateY(6px)",
                transition: "all 300ms ease"
              }}>
                <div style={{ display: "flex", flexDirection: "column", alignItems: "center", width: 12 }}>
                  <div style={{ width: 8, height: 8, borderRadius: "50%", background: isCounterparty ? "#F59E0B" : "var(--accent)", border: "2px solid var(--bg-elevated)", flexShrink: 0 }} />
                  {i < 2 && <div style={{ width: 1, height: 16, background: "var(--line)", marginTop: 2 }} />}
                </div>
                <div style={{ flex: 1, minWidth: 0 }}>
                  <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
                    <span style={{ fontSize: 11.5, fontWeight: 600, color: "var(--ink-1)", fontFamily: "var(--font-sans)" }}>
                      {ver.v} {isCounterparty && <span style={{ fontSize: 9, fontWeight: 500, color: "#D97706", background: "rgba(217,119,6,0.10)", padding: "1px 5px", borderRadius: 3, marginLeft: 4 }}>external</span>}
                    </span>
                    <span style={{ fontSize: 10, color: statusColor, fontWeight: 500, fontFamily: "var(--font-sans)" }}>{ver.status}</span>
                  </div>
                  <div style={{ fontSize: 10, color: "var(--ink-4)", fontFamily: "var(--font-sans)" }}>{ver.by} · {ver.date}</div>
                </div>
              </div>
            );
          })}
        </div>

        {/* Diff section */}
        <div style={{
          background: "var(--bg-soft)", borderRadius: 8, padding: 12,
          border: "1px solid var(--line)",
          opacity: show(2) ? 1 : 0.3, transition: "opacity 300ms", marginBottom: 12
        }}>
          <div style={{ fontSize: 9, fontWeight: 600, letterSpacing: "0.06em", textTransform: "uppercase", color: "var(--ink-4)", marginBottom: 8, fontFamily: "var(--font-sans)" }}>Changes in v2</div>
          <div style={{ fontSize: 11, lineHeight: 1.6, color: "var(--ink-2)", fontFamily: "var(--font-sans)" }}>
            <span>Payment terms: </span>
            {show(2) && <span style={{ textDecoration: "line-through", color: "var(--ink-4)", background: "rgba(225,29,72,0.08)", padding: "0 2px", borderRadius: 2 }}>Net 30</span>}
            {show(2) && " "}
            {show(2) && <span style={{ color: "#0F7B3F", background: "rgba(34,197,94,0.12)", padding: "0 3px", borderRadius: 2, fontWeight: 500 }}>Net 45</span>}
          </div>
          <div style={{ fontSize: 11, lineHeight: 1.6, color: "var(--ink-2)", marginTop: 4, fontFamily: "var(--font-sans)" }}>
            <span>Liability cap: </span>
            {show(2) && <span style={{ textDecoration: "line-through", color: "var(--ink-4)", background: "rgba(225,29,72,0.08)", padding: "0 2px", borderRadius: 2 }}>Unlimited</span>}
            {show(2) && " "}
            {show(2) && <span style={{ color: "#0F7B3F", background: "rgba(34,197,94,0.12)", padding: "0 3px", borderRadius: 2, fontWeight: 500 }}>2x annual fees</span>}
          </div>
        </div>

        {/* Internal note */}
        <div style={{
          background: "rgba(110,86,207,0.06)", border: "1px solid rgba(110,86,207,0.18)",
          borderRadius: 6, padding: "8px 10px", fontSize: 10.5,
          opacity: show(3) ? 1 : 0, transform: show(3) ? "translateY(0)" : "translateY(6px)",
          transition: "all 300ms ease", marginBottom: 12, fontFamily: "var(--font-sans)"
        }}>
          <div style={{ display: "flex", alignItems: "center", gap: 4, marginBottom: 3 }}>
            <span style={{ fontSize: 8, fontWeight: 700, letterSpacing: "0.06em", textTransform: "uppercase", color: "#7B61FF", background: "rgba(123,97,255,0.15)", padding: "1px 4px", borderRadius: 3 }}>Internal</span>
            <span style={{ fontWeight: 600, color: "var(--ink-1)", fontSize: 10.5 }}>Erik</span>
            <span style={{ color: "var(--ink-4)", fontSize: 9, marginLeft: "auto" }}>10:22 AM</span>
          </div>
          <div style={{ color: "var(--ink-3)", lineHeight: 1.35 }}>Net 45 is acceptable. Push back on the liability cap, request 3x.</div>
        </div>

        {/* Approval status bar */}
        <div style={{
          padding: "9px 11px", borderRadius: 8, display: "flex", alignItems: "center", gap: 8, fontSize: 11,
          transition: "all 360ms",
          background: show(5) ? "rgba(31,138,76,0.10)" : show(4) ? "rgba(217,119,6,0.10)" : "var(--bg-soft)",
          border: show(5) ? "1px solid rgba(31,138,76,0.30)" : show(4) ? "1px solid rgba(217,119,6,0.30)" : "1px solid var(--line)",
          color: show(5) ? "var(--success)" : show(4) ? "#B45309" : "var(--ink-4)",
          fontFamily: "var(--font-sans)"
        }}>
          {show(5) ? (
            <>
              <svg width="13" height="13" viewBox="0 0 14 14" style={{ flexShrink: 0 }}><circle cx="7" cy="7" r="6.5" fill="rgba(31,138,76,0.15)" stroke="rgba(31,138,76,0.45)" /><path d="M4 7.2 L6.2 9.2 L10 5.2" fill="none" stroke="#1F8A4C" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" /></svg>
              <span>Approved by Legal · <strong style={{ fontWeight: 600 }}>ready to sign</strong></span>
            </>
          ) : show(4) ? (
            <>
              <span style={{ width: 12, height: 12, borderRadius: "50%", border: "1.6px solid rgba(217,119,6,0.25)", borderTopColor: "#D97706", animation: "negoSpin 0.9s linear infinite", display: "inline-block" }} />
              <span>Awaiting Legal approval</span>
            </>
          ) : (
            <>
              <span style={{ width: 12, height: 12, borderRadius: "50%", border: "1px dashed var(--ink-5)" }} />
              <span>2 open redlines remaining</span>
            </>
          )}
        </div>
      </MockPanel>
      </div>
      </div>
      <style>{`@keyframes negoSpin { to { transform: rotate(360deg); } }`}</style>
    </div>
  );
}
window.NegotiationAnimation = NegotiationAnimation;
