// Hero — tilted tablet mockup with Inkly app UI + live drawable canvas
const Wordmark = () => (
  <a href="#" className="flex items-center gap-2.5 select-none">
    <span className="w-7 h-7 rounded-md overflow-hidden flex items-center justify-center bg-ink ring-1 ring-ink/10 shadow-sm">
      <img src="assets/inkly-icon.png" alt="" className="w-full h-full object-cover" />
    </span>
    <span className="text-[22px] font-semibold tracking-tighter2 text-ink">Inkly</span>
  </a>
);

const Nav = () => {
  const [scrolled, setScrolled] = React.useState(false);
  React.useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 8);
    onScroll();
    window.addEventListener("scroll", onScroll, { passive: true });
    return () => window.removeEventListener("scroll", onScroll);
  }, []);
  return (
    <header className={"fixed top-0 left-0 right-0 z-40 transition-all duration-500 " + (scrolled ? "py-3" : "py-5")}>
      <div className="mx-auto max-w-[1240px] px-6 flex items-center justify-between">
        <div className={"flex items-center justify-between w-full px-5 py-2.5 rounded-full transition-all duration-500 " + (scrolled ? "glass" : "bg-transparent")}>
          <Wordmark />
          <nav className="hidden md:flex items-center gap-7 text-[13.5px] text-ink2">
            <a href="#features" className="link-u">Features</a>
            <a href="#performance" className="link-u">Performance</a>
            <a href="#ecosystem" className="link-u">Ecosystem</a>
            <a href="#faq" className="link-u">FAQ</a>
          </nav>
          <div className="flex items-center gap-2">
            <a href="#" className="hidden sm:inline-flex text-[13.5px] text-ink2 px-3 py-1.5 link-u">Sign in</a>
            <a href="#" className="inline-flex items-center gap-1.5 text-[13.5px] font-medium bg-ink text-paper px-4 py-2 rounded-full hover:bg-violet-deep transition-colors">
              Download <Icon name="arrow-down" size={14} strokeWidth={2} />
            </a>
          </div>
        </div>
      </div>
    </header>
  );
};

// === Live drawable canvas (the paper layer) ===
const InkSurface = () => {
  const canvasRef = React.useRef(null);
  const drawing = React.useRef(false);
  const last = React.useRef(null);
  const [hasUserDrawn, setHasUserDrawn] = React.useState(false);

  React.useEffect(() => {
    const c = canvasRef.current;
    if (!c) return;
    const dpr = window.devicePixelRatio || 1;
    c.width = (c.offsetWidth || 760) * dpr;
    c.height = (c.offsetHeight || 380) * dpr;
    const ctx = c.getContext("2d");
    ctx.scale(dpr, dpr);
    ctx.lineCap = "round";
    ctx.lineJoin = "round";
  }, []);

  const pos = (e) => {
    const c = canvasRef.current;
    const r = c.getBoundingClientRect();
    const t = e.touches ? e.touches[0] : e;
    return {
      x: t.clientX - r.left,
      y: t.clientY - r.top,
    };
  };
  const start = (e) => { e.preventDefault(); drawing.current = true; last.current = pos(e); setHasUserDrawn(true); };
  const move = (e) => {
    if (!drawing.current) return;
    e.preventDefault();
    const p = pos(e);
    const ctx = canvasRef.current.getContext("2d");
    ctx.strokeStyle = "#B8941F";
    ctx.lineWidth = 2.4;
    ctx.beginPath();
    ctx.moveTo(last.current.x, last.current.y);
    ctx.lineTo(p.x, p.y);
    ctx.stroke();
    last.current = p;
  };
  const end = () => { drawing.current = false; last.current = null; };
  const clear = () => {
    const c = canvasRef.current;
    c.getContext("2d").clearRect(0, 0, c.width, c.height);
    setHasUserDrawn(false);
  };

  return (
    <div className="absolute inset-0">
      {!hasUserDrawn && (
        <svg viewBox="0 0 760 380" className="absolute inset-0 w-full h-full pointer-events-none" preserveAspectRatio="xMidYMid meet">
          <text x="50%" y="62%" textAnchor="middle" fill="#B8941F" fontFamily="'Just Me Again Down Here', cursive" fontSize="160" fontWeight="400" style={{ transform: 'scaleY(1.05) scaleX(0.85)', transformOrigin: 'center', WebkitTextStrokeWidth: 0, opacity: 0.85 }}>Inkly</text>
          <text x="50%" y="92%" textAnchor="middle" fill="oklch(0.55 0.012 280)" fontFamily="Inter" fontSize="14" fontWeight="400">— draw anywhere on this page</text>
        </svg>
      )}
      <canvas
        ref={canvasRef}
        className="absolute inset-0 w-full h-full cursor-crosshair touch-none"
        onMouseDown={start} onMouseMove={move} onMouseUp={end} onMouseLeave={end}
        onTouchStart={start} onTouchMove={move} onTouchEnd={end}
      />
      <button onClick={clear} className="absolute top-2 right-2 text-[10px] font-mono uppercase tracking-[0.14em] text-ink3 hover:text-violet bg-paper/80 backdrop-blur px-2 py-0.5 rounded-full border border-rule">Clear</button>
    </div>
  );
};

// === Inkly App UI inside the tablet ===
const PenIcon = ({ tip = "fine", color = "currentColor" }) => {
  // simple stylus glyph variants
  const tips = {
    fine: <path d="M14 2 L18 6 L8 16 L4 17 L5 13 Z M12 4 L16 8" />,
    medium: <path d="M14 2 L18 6 L8 16 L3 18 L5 13 Z" />,
    brush: <path d="M14 2 C16 4 18 6 18 8 L8 16 L4 17 L5 13 L14 2 Z" />,
    marker: <path d="M14 2 L18 6 L9 15 L4 17 L5 13 Z M9 15 L13 19" />,
    highlight: <path d="M3 16 L13 6 L17 10 L7 20 Z M5 18 L19 4" />,
  };
  return (
    <svg viewBox="0 0 22 22" width="22" height="22" fill="none" stroke={color} strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      {tips[tip] || tips.fine}
    </svg>
  );
};

const ToolbarBtn = ({ children, active, onClick, className = "", title }) => (
  <button
    onClick={onClick}
    title={title}
    className={"shrink-0 w-9 h-9 rounded-md flex items-center justify-center transition-colors " + (active ? "bg-[oklch(0.94_0.07_70)] text-ink ring-1 ring-[oklch(0.78_0.12_70)]" : "text-ink2 hover:bg-paper2") + " " + className}
  >
    {children}
  </button>
);

const InklyAppUI = () => {
  const [tool, setTool] = React.useState(1); // 0..4 pens
  const [color, setColor] = React.useState(0); // 0..5
  const [showPalette, setShowPalette] = React.useState(true);

  const colors = [
    "oklch(0.18 0.01 280)",       // black
    "oklch(0.62 0.21 28)",        // red
    "oklch(0.58 0.18 250)",       // blue
    "oklch(0.66 0.16 150)",       // green
    null, // rainbow
  ];

  const tools = ["fine", "medium", "brush", "marker", "highlight"];

  return (
    <div className="absolute inset-0 flex flex-col bg-paper text-ink select-none">
      {/* === Top app bar === */}
      <div className="h-10 flex items-center px-3 gap-2 border-b hairline bg-paper">
        <button className="w-7 h-7 rounded-md flex items-center justify-center text-ink2 hover:bg-paper2">
          <Icon name="chevron-left" size={18} strokeWidth={1.6} />
        </button>
        <div className="flex-1 text-[13px] italic text-ink3">Tytuł notatki…</div>
        <button className="w-7 h-7 rounded-md flex items-center justify-center text-ink2 hover:bg-paper2"><Icon name="grid-2x2" size={15} strokeWidth={1.6} /></button>
        <button className="w-7 h-7 rounded-md flex items-center justify-center text-ink2 hover:bg-paper2"><Icon name="undo-2" size={16} strokeWidth={1.6} /></button>
        <button className="w-7 h-7 rounded-md flex items-center justify-center text-ink3/50"><Icon name="redo-2" size={16} strokeWidth={1.6} /></button>
        <button className="w-7 h-7 rounded-md flex items-center justify-center text-ink2 hover:bg-paper2"><Icon name="chevron-down" size={16} strokeWidth={1.6} /></button>
        <button className="w-7 h-7 rounded-md flex items-center justify-center text-ink2 hover:bg-paper2"><Icon name="more-horizontal" size={16} strokeWidth={1.6} /></button>
      </div>

      {/* === Tools row === */}
      <div className="h-12 flex items-center px-2 gap-1 border-b hairline bg-paper overflow-x-auto">
        {/* pen variants */}
        {tools.map((t, i) => (
          <ToolbarBtn key={t} active={tool === i} onClick={() => setTool(i)}>
            <PenIcon tip={t} color={tool === i ? "oklch(0.55 0.18 60)" : "oklch(0.36 0.012 280)"} />
          </ToolbarBtn>
        ))}
        {/* tape/highlight chunky */}
        <ToolbarBtn>
          <svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" strokeWidth="1.4">
            <path d="M3 14 L17 5 L21 9 L7 18 Z" fill="oklch(0.92 0.005 280)" />
          </svg>
        </ToolbarBtn>
        <div className="w-px h-6 bg-rule mx-1" />
        {/* lasso, eraser pieces */}
        <ToolbarBtn><svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" strokeWidth="1.5"><path d="M6 6 C 4 10 5 16 10 18 C 12 19 13 19 13 21" /><circle cx="14" cy="6" r="2" /></svg></ToolbarBtn>
        <ToolbarBtn><svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" strokeWidth="1.5"><path d="M16 4 L20 8 L10 18 L4 18 L4 14 Z" /><path d="M3 22 L21 22" /></svg></ToolbarBtn>
        <ToolbarBtn><svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" strokeWidth="1.5"><path d="M16 4 L20 8 L10 18 L4 18 L4 14 Z" /></svg></ToolbarBtn>

        {/* "Bazgroł" chip */}
        <button className="ml-1 shrink-0 inline-flex items-center gap-1.5 h-9 px-3 rounded-md bg-[oklch(0.97_0.04_70)] ring-1 ring-[oklch(0.85_0.10_70)] text-[12px] font-medium text-[oklch(0.42_0.14_60)]">
          <svg viewBox="0 0 16 16" width="13" height="13" fill="none" stroke="currentColor" strokeWidth="1.5"><path d="M2 12 Q 5 6, 8 10 T 14 8" /><path d="M11 3 L13 5" /></svg>
          Bazgroł
        </button>

        <div className="w-px h-6 bg-rule mx-1" />

        {/* color circles */}
        {[0,1,2,3,4].map((i) => (
          <button
            key={i}
            onClick={() => setColor(i)}
            className={"shrink-0 w-7 h-7 rounded-full flex items-center justify-center transition-all " + (color === i ? "ring-2 ring-offset-1 ring-ink/70" : "")}
          >
            {i === 4 ? (
              <span className="w-6 h-6 rounded-full" style={{ background: 'conic-gradient(from 0deg, #ef4444, #f59e0b, #84cc16, #22c55e, #06b6d4, #3b82f6, #8b5cf6, #ec4899, #ef4444)' }} />
            ) : (
              <span className="w-6 h-6 rounded-full" style={{ background: colors[i] }} />
            )}
          </button>
        ))}

        <div className="w-px h-6 bg-rule mx-1" />

        {/* % */}
        <ToolbarBtn className="text-[14px] font-mono"><span className="text-ink2">%</span></ToolbarBtn>

        {/* Fill chip */}
        <button className="shrink-0 inline-flex items-center gap-1.5 h-8 px-3 rounded-md ring-1 ring-rule bg-paper text-[12px] text-ink2 hover:bg-paper2">
          <svg viewBox="0 0 16 16" width="13" height="13" fill="oklch(0.55 0.18 60)" stroke="oklch(0.42 0.14 60)" strokeWidth="1"><path d="M3 8 L8 3 L13 8 L8 13 Z" /></svg>
          Fill
        </button>
        <button className="shrink-0 inline-flex items-center gap-1.5 h-8 px-3 rounded-md ring-1 ring-rule bg-paper text-[12px] text-ink2 hover:bg-paper2">
          <Icon name="sparkles" size={12} strokeWidth={1.6} />
          Auto
        </button>

        <div className="w-px h-6 bg-rule mx-1" />

        {/* Insert tools */}
        <ToolbarBtn><span className="font-serif text-[15px] leading-none"><span className="text-ink">T</span><span className="text-ink2 text-[10px]">t</span></span></ToolbarBtn>
        <ToolbarBtn><Icon name="image" size={16} strokeWidth={1.5} /></ToolbarBtn>
        <ToolbarBtn><Icon name="layout-grid" size={16} strokeWidth={1.5} /></ToolbarBtn>
        <ToolbarBtn><Icon name="smile" size={16} strokeWidth={1.5} /></ToolbarBtn>
        <ToolbarBtn><Icon name="pin" size={16} strokeWidth={1.5} /></ToolbarBtn>
        <ToolbarBtn><Icon name="layers" size={16} strokeWidth={1.5} /></ToolbarBtn>
      </div>

      {/* === Canvas area === */}
      <div className="relative flex-1 overflow-hidden" style={{
        backgroundImage: 'radial-gradient(circle at 1px 1px, oklch(0.88 0.005 280 / 0.7) 1px, transparent 0)',
        backgroundSize: '22px 22px',
      }}>
        <InkSurface />

        {/* Floating pen palette (left side) */}
        {showPalette && (
          <div className="absolute left-2 top-6 w-11 rounded-xl bg-paper/95 backdrop-blur border hairline shadow-[0_8px_24px_-12px_oklch(0.30_0.05_280/0.25)] py-1.5 flex flex-col items-center gap-1 z-10">
            <span className="w-6 h-1 rounded-full bg-ink3/30 mb-1" />
            {[
              { tip: 'fine', stroke: 'oklch(0.85 0.005 280)' },
              { tip: 'medium', stroke: 'oklch(0.70 0.18 60)' },
              { tip: 'brush', stroke: 'oklch(0.70 0.16 150)' },
            ].map((p, i) => (
              <button key={i} className={"w-9 h-9 rounded-lg flex items-center justify-center " + (i === 1 ? "bg-[oklch(0.97_0.05_70)] ring-1 ring-[oklch(0.82_0.10_70)]" : "")}>
                <PenIcon tip={p.tip} color={p.stroke} />
              </button>
            ))}
          </div>
        )}

        {/* status pill */}
        <div className="absolute bottom-3 left-1/2 -translate-x-1/2 flex items-center gap-2 text-[10px] font-mono uppercase tracking-[0.14em] text-ink3 bg-paper/85 backdrop-blur px-3 py-1 rounded-full border hairline">
          <span className="w-1.5 h-1.5 rounded-full bg-violet pulse-dot" />
          <span>120 Hz · {tools[tool]} · stylus + finger</span>
        </div>

        {/* gesture indicator (bottom home pill) */}
        <div className="absolute bottom-1 left-1/2 -translate-x-1/2 w-24 h-0.5 rounded-full bg-ink/20" />
      </div>
    </div>
  );
};

// === Tilted tablet frame ===
const Tablet = ({ children }) => (
  <div className="relative" style={{ perspective: "1800px" }}>
    <div
      className="relative mx-auto rounded-[28px] bg-gradient-to-b from-paper to-paper3 device-shadow"
      style={{
        width: "min(900px, 100%)",
        aspectRatio: "16/10",
        transform: "rotateX(7deg) rotateY(-8deg) rotateZ(0.4deg)",
        transformStyle: "preserve-3d",
        padding: "10px",
      }}
    >
      <div className="absolute inset-2 rounded-[22px] bg-ink/95 overflow-hidden">
        <div className="absolute inset-[5px] rounded-[18px] bg-paper overflow-hidden">
          {children}
        </div>
      </div>
      <div className="absolute top-1.5 left-1/2 -translate-x-1/2 w-1.5 h-1.5 rounded-full bg-ink3/40" />
    </div>
    {/* stylus */}
    <div
      className="absolute hidden lg:block"
      style={{ right: "-3%", top: "62%", width: "240px", height: "10px", transform: "rotate(-22deg)" }}
    >
      <div className="relative w-full h-full">
        <div className="absolute inset-0 rounded-full bg-gradient-to-r from-paper3 via-paper to-paper2 device-shadow" />
        <div className="absolute right-0 top-0 bottom-0 w-3 rounded-r-full bg-ink/80" />
        <div className="absolute right-3 top-0 bottom-0 w-2 bg-paper3" />
      </div>
    </div>
  </div>
);

const Hero = () => {
  return (
    <section className="relative pt-32 pb-24 md:pt-40 md:pb-32 overflow-hidden">
      <div className="absolute inset-0 paper-grid opacity-50 pointer-events-none" />
      <div className="absolute inset-x-0 -top-40 h-80 bg-gradient-to-b from-violet-soft/40 to-transparent pointer-events-none" />

      <div className="relative mx-auto max-w-[1240px] px-6">
        <div className="flex items-center gap-3 mb-8 reveal">
          <span className="inline-flex items-center gap-2 text-[11px] font-mono uppercase tracking-[0.18em] text-ink3 bg-paper2 border hairline rounded-full px-3 py-1">
            <span className="w-1.5 h-1.5 rounded-full bg-violet pulse-dot" />
            Now available — Android tablets, phones &amp; web
          </span>
        </div>

        <h1 className="reveal text-ink font-semibold tracking-tightest leading-[0.92] text-[14vw] sm:text-[10vw] md:text-[112px] lg:text-[136px] max-w-[14ch]">
          Writing at the<br />
          <span className="text-violet-deep italic font-medium" style={{ fontFamily: 'Inter', fontStyle: 'italic' }}>speed</span> of thought.
        </h1>

        <div className="mt-10 flex flex-col md:flex-row md:items-end md:justify-between gap-8 reveal">
          <p className="max-w-xl text-[19px] leading-[1.55] text-ink2">
            A handwriting and note-taking app engineered for the way ideas actually arrive — instantly, beautifully, everywhere. Native on Android tablets and phones, full-featured PWA on the desktop.
          </p>
          <div className="flex flex-col items-start gap-3">
            <div className="flex items-center gap-3">
              <a href="#" className="inline-flex items-center gap-2 bg-ink text-paper px-5 py-3 rounded-full text-[14px] font-medium hover:bg-violet-deep transition-colors">
                <Icon name="download" size={15} strokeWidth={2} /> Get Inkly — Free
              </a>
              <a href="#features" className="inline-flex items-center gap-2 text-ink2 px-4 py-3 text-[14px] font-medium link-u">
                Watch the demo <Icon name="arrow-right" size={14} />
              </a>
            </div>
            <div className="text-[12px] font-mono uppercase tracking-[0.14em] text-ink3">
              Android tablet · Android phone · Web (PWA)
            </div>
          </div>
        </div>

        <div className="mt-20 reveal">
          <Tablet>
            <InklyAppUI />
          </Tablet>
        </div>

        <div className="mt-20 grid grid-cols-2 md:grid-cols-4 gap-px bg-rule border hairline rounded-2xl overflow-hidden reveal">
          {[
            ["120 Hz", "High-refresh tuned"],
            ["<9 ms", "End-to-end latency"],
            ["∞ paper", "One endless canvas, no page breaks"],
            ["Offline", "Works on the plane, syncs later"],
          ].map(([n, l]) => (
            <div key={l} className="bg-paper p-6 md:p-8">
              <div className="text-[40px] md:text-[44px] font-semibold tracking-tightest text-ink leading-none">{n}</div>
              <div className="mt-2 text-[12px] font-mono uppercase tracking-[0.14em] text-ink3">{l}</div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
};

window.Hero = Hero;
window.Nav = Nav;
