'use client';

// Hero — Meadow — NiceUI (MIT) · https://niceui.dev/c/marketing/hero-meadow
// One self-contained file: needs only React and Tailwind CSS. Nothing else to install.

import { useEffect, useRef, useState, createElement, type SVGProps } from 'react';

function cn(...classes: Array<string | false | null | undefined>) {
  return classes.filter(Boolean).join(' ');
}

// Icons from Lucide (ISC licence, lucide.dev), inlined so there is nothing to install.
type NiceIconProps = SVGProps<SVGSVGElement> & { size?: number | string; absoluteStrokeWidth?: boolean };
type LucideIcon = (props: NiceIconProps) => JSX.Element;
type LucideProps = NiceIconProps;
const niceIcon = (node: Array<[string, Record<string, string>]>): LucideIcon =>
  function Icon({ size = 24, strokeWidth = 2, absoluteStrokeWidth, children, ...rest }: NiceIconProps) {
    const sw = absoluteStrokeWidth ? (Number(strokeWidth) * 24) / Number(size) : strokeWidth;
    return createElement('svg', { xmlns: 'http://www.w3.org/2000/svg', width: size, height: size, viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', strokeWidth: sw, strokeLinecap: 'round', strokeLinejoin: 'round', 'aria-hidden': true, ...rest }, ...node.map(([tag, attrs], i) => createElement(tag, { key: i, ...attrs })), children);
  };
const ArrowUpRight = niceIcon([["path",{"d":"M7 7h10v10"}],["path",{"d":"M7 17 17 7"}]]);
const Bell = niceIcon([["path",{"d":"M6 8a6 6 0 0 1 12 0c0 7 3 9 3 9H3s3-2 3-9"}],["path",{"d":"M10.3 21a1.94 1.94 0 0 0 3.4 0"}]]);
const CalendarDays = niceIcon([["path",{"d":"M8 2v4"}],["path",{"d":"M16 2v4"}],["rect",{"width":"18","height":"18","x":"3","y":"4","rx":"2"}],["path",{"d":"M3 10h18"}],["path",{"d":"M8 14h.01"}],["path",{"d":"M12 14h.01"}],["path",{"d":"M16 14h.01"}],["path",{"d":"M8 18h.01"}],["path",{"d":"M12 18h.01"}],["path",{"d":"M16 18h.01"}]]);
const FlaskConical = niceIcon([["path",{"d":"M10 2v7.527a2 2 0 0 1-.211.896L4.72 20.55a1 1 0 0 0 .9 1.45h12.76a1 1 0 0 0 .9-1.45l-5.069-10.127A2 2 0 0 1 14 9.527V2"}],["path",{"d":"M8.5 2h7"}],["path",{"d":"M7 16h10"}]]);
const History = niceIcon([["path",{"d":"M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"}],["path",{"d":"M3 3v5h5"}],["path",{"d":"M12 7v5l4 2"}]]);
const LayoutGrid = niceIcon([["rect",{"width":"7","height":"7","x":"3","y":"3","rx":"1"}],["rect",{"width":"7","height":"7","x":"14","y":"3","rx":"1"}],["rect",{"width":"7","height":"7","x":"14","y":"14","rx":"1"}],["rect",{"width":"7","height":"7","x":"3","y":"14","rx":"1"}]]);
const Lightbulb = niceIcon([["path",{"d":"M15 14c.2-1 .7-1.7 1.5-2.5 1-.9 1.5-2.2 1.5-3.5A6 6 0 0 0 6 8c0 1 .2 2.2 1.5 3.5.7.7 1.3 1.5 1.5 2.5"}],["path",{"d":"M9 18h6"}],["path",{"d":"M10 22h4"}]]);
const Menu = niceIcon([["line",{"x1":"4","x2":"20","y1":"12","y2":"12"}],["line",{"x1":"4","x2":"20","y1":"6","y2":"6"}],["line",{"x1":"4","x2":"20","y1":"18","y2":"18"}]]);
const PanelLeft = niceIcon([["rect",{"width":"18","height":"18","x":"3","y":"3","rx":"2"}],["path",{"d":"M9 3v18"}]]);
const PieChart = niceIcon([["path",{"d":"M21 12c.552 0 1.005-.449.95-.998a10 10 0 0 0-8.953-8.951c-.55-.055-.998.398-.998.95v8a1 1 0 0 0 1 1z"}],["path",{"d":"M21.21 15.89A10 10 0 1 1 8 2.83"}]]);
const Search = niceIcon([["circle",{"cx":"11","cy":"11","r":"8"}],["path",{"d":"m21 21-4.3-4.3"}]]);
const Sparkles = niceIcon([["path",{"d":"M9.937 15.5A2 2 0 0 0 8.5 14.063l-6.135-1.582a.5.5 0 0 1 0-.962L8.5 9.936A2 2 0 0 0 9.937 8.5l1.582-6.135a.5.5 0 0 1 .963 0L14.063 8.5A2 2 0 0 0 15.5 9.937l6.135 1.581a.5.5 0 0 1 0 .964L15.5 14.063a2 2 0 0 0-1.437 1.437l-1.582 6.135a.5.5 0 0 1-.963 0z"}],["path",{"d":"M20 3v4"}],["path",{"d":"M22 5h-4"}],["path",{"d":"M4 17v2"}],["path",{"d":"M5 18H3"}]]);
const Star = niceIcon([["polygon",{"points":"12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"}]]);
const Sun = niceIcon([["circle",{"cx":"12","cy":"12","r":"4"}],["path",{"d":"M12 2v2"}],["path",{"d":"M12 20v2"}],["path",{"d":"m4.93 4.93 1.41 1.41"}],["path",{"d":"m17.66 17.66 1.41 1.41"}],["path",{"d":"M2 12h2"}],["path",{"d":"M20 12h2"}],["path",{"d":"m6.34 17.66-1.41 1.41"}],["path",{"d":"m19.07 4.93-1.41 1.41"}]]);
const TrendingUp = niceIcon([["polyline",{"points":"22 7 13.5 15.5 8.5 10.5 2 17"}],["polyline",{"points":"16 7 22 7 22 13"}]]);
const Users = niceIcon([["path",{"d":"M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"}],["circle",{"cx":"9","cy":"7","r":"4"}],["path",{"d":"M22 21v-2a4 4 0 0 0-3-3.87"}],["path",{"d":"M16 3.13a4 4 0 0 1 0 7.75"}]]);
const X = niceIcon([["path",{"d":"M18 6 6 18"}],["path",{"d":"m6 6 12 12"}]]);

// ── components/marketing/heroes/scenery.ts ──────────────────────
/*
 * Procedural scenery for the scenic heroes, painted once to a canvas and cached
 * as a data URL. Swap `src` for your own photo in production.
 */

type RGB = [number, number, number];
const hex = (h: string): RGB => {
  const v = parseInt(h.slice(1), 16);
  return [(v >> 16) & 255, (v >> 8) & 255, v & 255];
};
const clamp = (x: number, a = 0, b = 1) => (x < a ? a : x > b ? b : x);
const smooth = (a: number, b: number, x: number) => {
  const t = clamp((x - a) / (b - a));
  return t * t * (3 - 2 * t);
};
const ramp = (stops: [number, RGB][], t: number): RGB => {
  if (t <= stops[0][0]) return stops[0][1];
  for (let i = 1; i < stops.length; i++) {
    const [p, c] = stops[i];
    if (t <= p) {
      const [p0, c0] = stops[i - 1];
      const k = (t - p0) / (p - p0);
      return [c0[0] + (c[0] - c0[0]) * k, c0[1] + (c[1] - c0[1]) * k, c0[2] + (c[2] - c0[2]) * k];
    }
  }
  return stops[stops.length - 1][1];
};
let seed = 1;
const rnd = () => {
  seed = (seed * 1664525 + 1013904223) >>> 0;
  return seed / 4294967296;
};
const hash = (x: number, y: number) => {
  const h = Math.sin(x * 127.1 + y * 311.7) * 43758.5453;
  return h - Math.floor(h);
};
// smooth 1D value noise
const vnoise = (x: number, s: number) => {
  const i = Math.floor(x);
  const f = x - i;
  const a = hash(i, s);
  const b = hash(i + 1, s);
  return a + (b - a) * f * f * (3 - 2 * f);
};

/* ——— Dusk dunes ——— */

const SKY: [number, RGB][] = [
  [0, hex('#132c33')],
  [0.28, hex('#24434a')],
  [0.5, hex('#5f5a52')],
  [0.68, hex('#b06a48')],
  [0.82, hex('#de8a57')],
  [1, hex('#efa36c')],
];
const SAND: [number, RGB][] = [
  [0, hex('#0f0b0a')],
  [0.16, hex('#1d1311')],
  [0.36, hex('#4a2113')],
  [0.58, hex('#9c3f19')],
  [0.78, hex('#de6527')],
  [0.92, hex('#f28a45')],
  [1, hex('#f8b27a')],
];

function paintDunes(W: number, H: number) {
  const c = document.createElement('canvas');
  c.width = W;
  c.height = H;
  const g = c.getContext('2d')!;
  const img = g.createImageData(W, H);
  const d = img.data;
  // back dune crest: high on the left, sweeping down to the right
  const back = (u: number) => 0.385 - 0.03 * Math.exp(-(((u - 0.36) / 0.16) ** 2)) + 0.24 * smooth(0.42, 1.08, u) + 0.07 * Math.exp(-(((u + 0.02) / 0.14) ** 2)) + 0.006 * Math.sin(u * 23);
  // front dune: a dark shoulder bottom-left that drops away to the right
  const front = (u: number) => 0.62 + 0.05 * Math.sin(u * 2.4 + 0.6) + 0.3 * smooth(0.3, 1.0, u);

  for (let y = 0; y < H; y++) {
    const v = y / H;
    for (let x = 0; x < W; x++) {
      const u = x / W;
      let col: RGB;
      const yb = back(u);
      const yf = front(u);
      if (v < yb) {
        col = ramp(SKY, clamp(v / 0.62));
        // low sun glow on the right horizon
        const glow = Math.exp(-(((u - 0.86) / 0.32) ** 2) - ((v - 0.6) / 0.16) ** 2);
        col = [col[0] + 70 * glow, col[1] + 38 * glow, col[2] + 16 * glow];
      } else {
        const depth = v - yb;
        // lit face: brightest just under the crest toward the sun, falling into shade
        let lit = smooth(0.18, 0.95, u - 0.75 * depth + 0.05) * Math.exp(-depth * 2.1);
        lit = lit * 0.92 + 0.04;
        // wind ripples running diagonally down the slope
        const warp = vnoise(u * 9 + v * 3, 3) * 2.4;
        const rip = Math.sin(v * 210 - u * 70 + warp + Math.sin(u * 13) * 1.4);
        const fine = Math.sin(v * 560 - u * 150 + warp * 1.7);
        lit *= 1 + rip * 0.13 * smooth(0.05, 0.5, lit) + fine * 0.05;
        // a soft rim of light along the crest
        lit += Math.exp(-depth * 90) * 0.18 * smooth(0.2, 0.7, u);
        col = ramp(SAND, clamp(lit));
        if (v > yf) {
          const fd = v - yf;
          const rim = Math.exp(-fd * 120) * 0.35 * smooth(0.35, 0.9, u);
          const sh = 0.05 + rim + 0.04 * Math.sin(v * 260 - u * 40 + warp);
          col = ramp(SAND, clamp(sh));
        }
      }
      // vignette + grain
      const vig = 1 - 0.38 * ((u - 0.5) ** 2 * 1.6 + (v - 0.45) ** 2 * 1.4);
      const n = (hash(x, y) - 0.5) * 22;
      const o = (y * W + x) * 4;
      d[o] = col[0] * vig + n;
      d[o + 1] = col[1] * vig + n;
      d[o + 2] = col[2] * vig + n;
      d[o + 3] = 255;
    }
  }
  g.putImageData(img, 0, 0);
  return c.toDataURL('image/jpeg', 0.88);
}

/* ——— Spring meadow ——— */

const FLOWERS = ['#f472b6', '#fb923c', '#a78bfa', '#facc15', '#ffffff', '#f9a8d4', '#fdba74', '#c084fc'];

function paintMeadow(W: number, H: number) {
  const c = document.createElement('canvas');
  c.width = W;
  c.height = H;
  const g = c.getContext('2d')!;
  seed = 7;
  // sky
  const sky = g.createLinearGradient(0, 0, 0, H);
  sky.addColorStop(0, '#3f8fe6');
  sky.addColorStop(0.35, '#74b3f0');
  sky.addColorStop(0.62, '#bfe0f8');
  sky.addColorStop(0.75, '#e6f3fb');
  g.fillStyle = sky;
  g.fillRect(0, 0, W, H);
  /*
   * Blur is applied once per layer: draw the layer sharp on its own canvas,
   * then composite it with a single blurred drawImage. Setting ctx.filter and
   * then issuing thousands of strokes would blur the whole canvas per stroke,
   * which freezes (and can crash) the tab.
   */
  const layer = document.createElement('canvas');
  layer.width = W;
  layer.height = H;
  const lg = layer.getContext('2d')!;
  const inLayer = (blur: number, draw: (ctx: CanvasRenderingContext2D) => void) => {
    if (!blur) return draw(g);
    lg.clearRect(0, 0, W, H);
    draw(lg);
    g.filter = `blur(${blur}px)`;
    g.drawImage(layer, 0, 0);
    g.filter = 'none';
  };
  // haze clouds
  inLayer(Math.round(W / 40), (g) => {
    for (let i = 0; i < 7; i++) {
      g.fillStyle = `rgba(255,255,255,${0.18 + rnd() * 0.2})`;
      g.beginPath();
      g.ellipse(rnd() * W, H * (0.2 + rnd() * 0.35), W * (0.12 + rnd() * 0.18), H * (0.04 + rnd() * 0.05), 0, 0, Math.PI * 2);
      g.fill();
    }
  });

  // hills, back to front
  const layers = [
    {
      base: 0.6,
      amp: 0.05,
      f: 1.6,
      ph: 0.4,
      top: '#9cc9a2',
      bot: '#86b98e',
      blur: 4,
      flowers: 260,
      size: 1.1,
    },
    {
      base: 0.66,
      amp: 0.06,
      f: 2.3,
      ph: 2.1,
      top: '#7dbb6c',
      bot: '#5d9f4c',
      blur: 2,
      flowers: 700,
      size: 1.6,
    },
    {
      base: 0.73,
      amp: 0.07,
      f: 1.4,
      ph: 4.2,
      top: '#62ad48',
      bot: '#3e8a2c',
      blur: 1,
      flowers: 1400,
      size: 2.4,
    },
    {
      base: 0.84,
      amp: 0.1,
      f: 1.1,
      ph: 1.0,
      top: '#4f9e36',
      bot: '#2c6e1f',
      blur: 0,
      flowers: 2400,
      size: 3.6,
    },
  ];
  layers.forEach((L, li) => {
    const ridge = (u: number) => H * (L.base - L.amp * (Math.sin(u * L.f * Math.PI + L.ph) * 0.6 + Math.sin(u * L.f * 2.7 + L.ph * 2) * 0.4));
    inLayer(L.blur, (g) => {
      const grad = g.createLinearGradient(0, H * (L.base - L.amp), 0, H);
      grad.addColorStop(0, L.top);
      grad.addColorStop(1, L.bot);
      g.fillStyle = grad;
      g.beginPath();
      g.moveTo(0, H);
      for (let x = 0; x <= W; x += 6) g.lineTo(x, ridge(x / W));
      g.lineTo(W, H);
      g.closePath();
      g.fill();
      // grass strokes
      g.lineWidth = 0.6 + li * 0.35;
      for (let i = 0; i < L.flowers * 1.4; i++) {
        const x = rnd() * W;
        const top = ridge(x / W);
        const y = top + Math.pow(rnd(), 1.3) * (H - top);
        const len = (2 + rnd() * 5) * (0.6 + li * 0.45) * (0.6 + (y / H) * 0.8);
        g.strokeStyle = rnd() > 0.5 ? 'rgba(170,220,120,0.35)' : 'rgba(30,80,20,0.3)';
        g.beginPath();
        g.moveTo(x, y);
        g.lineTo(x + (rnd() - 0.5) * 2, y - len);
        g.stroke();
      }
      // flowers, denser and larger toward the viewer
      for (let i = 0; i < L.flowers; i++) {
        const x = rnd() * W;
        const top = ridge(x / W);
        const t = Math.pow(rnd(), 0.8);
        const y = top + 4 + t * (H - top);
        const r = L.size * (0.5 + rnd() * 0.8) * (0.7 + (y / H) * 0.6);
        const col = FLOWERS[Math.floor(rnd() * FLOWERS.length)];
        g.globalAlpha = 0.75 + rnd() * 0.25;
        g.fillStyle = col;
        g.beginPath();
        g.arc(x, y, r, 0, Math.PI * 2);
        g.fill();
      }
      g.globalAlpha = 1;
    });
  });
  // out-of-focus foreground blooms in the corners
  inLayer(Math.round(W / 140), (g) => {
    for (let i = 0; i < 70; i++) {
      const side = rnd() > 0.5 ? rnd() * 0.22 : 0.78 + rnd() * 0.22;
      const x = side * W;
      const y = H * (0.82 + rnd() * 0.2);
      g.globalAlpha = 0.85;
      g.fillStyle = FLOWERS[Math.floor(rnd() * FLOWERS.length)];
      g.beginPath();
      g.arc(x, y, W * (0.006 + rnd() * 0.01), 0, Math.PI * 2);
      g.fill();
    }
    g.globalAlpha = 1;
  });
  // light grain
  const img = g.getImageData(0, 0, W, H);
  const dd = img.data;
  for (let i = 0; i < dd.length; i += 4) {
    const n = (rnd() - 0.5) * 10;
    dd[i] += n;
    dd[i + 1] += n;
    dd[i + 2] += n;
  }
  g.putImageData(img, 0, 0);
  return c.toDataURL('image/jpeg', 0.88);
}

const cache = new Map<string, string>();

/** Paints a scene after first paint and returns its data URL (empty until ready). */
function useScene(kind: 'dunes' | 'meadow', w = 1440, h = 900) {
  const key = `${kind}-${w}-${h}`;
  const [src, setSrc] = useState(() => cache.get(key) ?? '');
  useEffect(() => {
    if (src) return;
    const id = window.setTimeout(() => {
      const url = kind === 'dunes' ? paintDunes(w, h) : paintMeadow(w, h);
      cache.set(key, url);
      setSrc(url);
    }, 30);
    return () => window.clearTimeout(id);
  }, [key, kind, w, h, src]);
  return src;
}

// ── components/marketing/heroes/HeroMeadow.tsx ──────────────────
const WORDS = ['Momentum', 'Revenue', 'Readers'];
const NAV = ['Features', 'Use cases', 'Pricing', 'Integrations'];

function SproutMark({ className }: { className?: string }) {
  return (
    <svg viewBox="0 0 24 24" className={className} aria-hidden>
      <path d="M12 21V11" stroke="#15803d" strokeWidth="2.2" strokeLinecap="round" />
      <path d="M12 12C12 7 8.5 4 3.5 4 3.5 9 7 12 12 12z" fill="#22c55e" />
      <path d="M12 10c0-4 3-6.5 8.5-6.5C20.5 8 17.5 10 12 10z" fill="#16a34a" />
    </svg>
  );
}

/** Counts up once when it scrolls into view. */
function Count({ to, decimals = 0, suffix = '' }: { to: number; decimals?: number; suffix?: string }) {
  const ref = useRef<HTMLSpanElement>(null);
  const [v, setV] = useState(0);
  useEffect(() => {
    const el = ref.current;
    if (!el) return;
    let raf = 0;
    let started = false;
    const start = () => {
      if (started) return;
      started = true;
      io.disconnect();
      const t0 = performance.now();
      const step = (now: number) => {
        const k = Math.min(1, (now - t0) / 1400);
        setV(to * (1 - Math.pow(1 - k, 3)));
        if (k < 1) raf = requestAnimationFrame(step);
      };
      raf = requestAnimationFrame(step);
    };
    const io = new IntersectionObserver(([e]) => e.isIntersecting && start());
    io.observe(el);
    // clipped previews never report an intersection, so start anyway
    const fallback = window.setTimeout(start, 2200);
    return () => {
      window.clearTimeout(fallback);
      io.disconnect();
      cancelAnimationFrame(raf);
    };
  }, [to]);
  return (
    <span ref={ref} className="tabular-nums">
      {v.toLocaleString('en-US', { minimumFractionDigits: decimals, maximumFractionDigits: decimals })}
      {suffix}
    </span>
  );
}

const KPIS = [
  { label: 'Experiments live', value: 128, delta: '+9.4%', up: true },
  { label: 'Weekly signups', value: 18406, delta: '+15.2%', up: true },
  { label: 'Activation rate', value: 41.8, decimals: 1, suffix: '%', delta: '-0.6%', up: false },
  { label: 'Ideas in backlog', value: 312, delta: '+4.1%', up: true },
];
const BARS = [38, 52, 46, 61, 58, 72, 69, 84, 78, 92, 88, 100];
const PLAN = [
  { day: 'Mon', title: 'Pricing page A/B test', tag: 'Live', tone: 'bg-emerald-100 text-emerald-700' },
  { day: 'Wed', title: 'Onboarding checklist v2', tag: 'Review', tone: 'bg-amber-100 text-amber-700' },
  { day: 'Fri', title: 'Referral reward email', tag: 'Draft', tone: 'bg-zinc-100 text-zinc-600' },
];

function Dashboard() {
  const [hover, setHover] = useState<number | null>(null);
  return (
    <div className="flex overflow-hidden rounded-[18px] border border-white/70 bg-[#f6f6f2] text-zinc-900 shadow-[0_30px_80px_-20px_rgba(20,60,30,.45),0_0_0_1px_rgba(0,0,0,.04)]">
      <aside className="hidden w-[210px] shrink-0 flex-col gap-1 border-r border-zinc-200/70 bg-white/70 p-3 md:flex">
        <div className="mb-3 flex items-center gap-2 px-2 pt-1 text-[15px] font-medium">
          <SproutMark className="h-5 w-5" /> Tendril
        </div>
        {[
          { icon: LayoutGrid, label: 'Overview', on: true },
          { icon: FlaskConical, label: 'Experiments' },
          { icon: Users, label: 'Audiences' },
          { icon: PieChart, label: 'Reports' },
          { icon: Sparkles, label: 'Automations' },
        ].map(({ icon: Icon, label, on }) => (
          <span key={label} className={cn('flex items-center gap-2.5 rounded-lg px-2.5 py-2 text-[13px]', on ? 'bg-emerald-100/80 text-emerald-900' : 'text-zinc-600')}>
            <Icon className="h-4 w-4" strokeWidth={1.7} /> {label}
          </span>
        ))}
      </aside>
      <div className="min-w-0 flex-1 p-4 md:p-6">
        <div className="flex items-center gap-3 text-[12px] text-zinc-500">
          <PanelLeft className="h-4 w-4" strokeWidth={1.6} />
          <Star className="h-4 w-4" strokeWidth={1.6} />
          <span className="hidden sm:inline">Workspaces</span>
          <span className="hidden text-zinc-300 sm:inline">/</span>
          <span className="text-zinc-800">Growth</span>
          <span className="ml-auto hidden items-center gap-2 rounded-md bg-zinc-100 px-2.5 py-1.5 text-zinc-400 sm:flex">
            <Search className="h-3.5 w-3.5" /> Search <kbd className="ml-6 rounded border border-zinc-200 px-1 text-[10px]">/</kbd>
          </span>
          <Sun className="ml-auto h-4 w-4 sm:ml-0" strokeWidth={1.6} />
          <History className="h-4 w-4" strokeWidth={1.6} />
          <Bell className="h-4 w-4" strokeWidth={1.6} />
        </div>
        <div className="mt-6 flex items-end justify-between gap-4">
          <div>
            <h3 className="font-serif text-[28px] leading-none tracking-[-0.01em] md:text-[34px]">Overview</h3>
            <p className="mt-2 text-[12px] text-zinc-500">
              <span className="font-medium text-zinc-800">Morning, Ana.</span> Here&rsquo;s how this week&rsquo;s bets are doing.
            </p>
          </div>
          <button className="shrink-0 rounded-lg bg-zinc-900 px-3.5 py-2 text-[12px] font-medium text-white transition-transform active:scale-95">New experiment</button>
        </div>
        <div className="mt-5 grid grid-cols-2 gap-3 lg:grid-cols-4">
          {KPIS.map((k) => (
            <div key={k.label} className="rounded-xl border border-zinc-200/80 bg-white p-3.5">
              <p className="text-[11px] text-zinc-500">{k.label}</p>
              <div className="mt-2 flex items-end justify-between gap-2">
                <span className="font-serif text-[22px] leading-none md:text-[26px]">
                  <Count to={k.value} decimals={k.decimals} suffix={k.suffix} />
                </span>
                <span className={cn('text-[10px] font-medium', k.up ? 'text-emerald-600' : 'text-rose-500')}>
                  {k.delta} {k.up ? '↗' : '↘'}
                </span>
              </div>
            </div>
          ))}
        </div>
        <div className="mt-3 grid gap-3 lg:grid-cols-[1.4fr_1fr]">
          <div className="rounded-xl border border-zinc-200/80 bg-white p-4">
            <p className="text-[12px] font-medium text-zinc-700">Signups</p>
            <p className="mt-1 font-serif text-[24px] leading-none">
              <Count to={74219} /> this quarter
            </p>
            <p className="mt-1.5 text-[11px] text-zinc-400">
              <span className="text-emerald-600">+12% ↗</span> vs last quarter
            </p>
            <div className="mt-4 flex h-28 items-end gap-1.5" onMouseLeave={() => setHover(null)}>
              {BARS.map((b, i) => (
                <div key={i} className="relative flex-1" style={{ height: '100%' }} onMouseEnter={() => setHover(i)}>
                  {hover === i && (
                    <span className="absolute -top-6 left-1/2 -translate-x-1/2 whitespace-nowrap rounded bg-zinc-900 px-1.5 py-0.5 text-[10px] text-white">{(b * 61).toLocaleString()}</span>
                  )}
                  <div
                    className={cn('meadow-bar absolute inset-x-0 bottom-0 rounded-t-[5px] transition-colors', hover === i ? 'bg-emerald-500' : i === BARS.length - 1 ? 'bg-emerald-400' : 'bg-emerald-200/90')}
                    style={{ height: `${b}%`, animationDelay: `${0.6 + i * 0.05}s` }}
                  />
                </div>
              ))}
            </div>
          </div>
          <div className="rounded-xl border border-zinc-200/80 bg-white p-4">
            <p className="text-[12px] font-medium text-zinc-700">Launch planner</p>
            <p className="mt-1 font-serif text-[24px] leading-none">3 launches this week</p>
            <div className="mt-3 flex items-center gap-2 rounded-lg border border-zinc-200 px-2.5 py-2 text-[12px] text-zinc-600">
              <CalendarDays className="h-3.5 w-3.5" /> November 2026
            </div>
            <ul className="mt-2.5 space-y-2">
              {PLAN.map((p) => (
                <li key={p.title} className="flex items-center gap-3 rounded-lg px-1 py-1 text-[12px]">
                  <span className="w-8 text-zinc-400">{p.day}</span>
                  <span className="min-w-0 flex-1 truncate text-zinc-800">{p.title}</span>
                  <span className={cn('rounded-full px-2 py-0.5 text-[10px] font-medium', p.tone)}>{p.tag}</span>
                </li>
              ))}
            </ul>
          </div>
        </div>
      </div>
    </div>
  );
}

export type HeroMeadowProps = {
  /** Words that rotate inside the second chip. */
  words?: string[];
  subtitle?: string;
  cta?: string;
  /** Optional photo URL; a painted spring meadow is used when omitted. */
  image?: string;
  className?: string;
};

/**
 * A bright, airy hero: sky and flower hills, a floating pill nav, a headline
 * with tilted serif word chips (the second one rotates), a pressable CTA and a
 * live product dashboard rising out of the meadow.
 */
export function HeroMeadow({
  words = WORDS,
  subtitle = 'Plan, ship and measure growth experiments in one calm place, without spreadsheets or status meetings.',
  cta = 'Start growing',
  image,
  className,
}: HeroMeadowProps) {
  const painted = useScene('meadow', 1600, 1000);
  const src = image ?? painted;
  const [wi, setWi] = useState(0);
  const [menu, setMenu] = useState(false);

  useEffect(() => {
    if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;
    const id = window.setInterval(() => setWi((i) => (i + 1) % words.length), 2600);
    return () => window.clearInterval(id);
  }, [words.length]);

  return (
    <section data-fill className={cn('relative isolate overflow-hidden bg-[#9fcdf3] font-sans text-zinc-900 [color-scheme:light]', className)}>
      <style>{`@keyframes meadow-up{from{opacity:0;transform:translateY(18px)}to{opacity:1;transform:none}}@keyframes meadow-chip{from{opacity:0;transform:translateY(10px) rotate(var(--r)) scale(.9)}to{opacity:1;transform:rotate(var(--r))}}@keyframes meadow-word{from{opacity:0;filter:blur(6px);transform:translateY(40%)}to{opacity:1;filter:none;transform:none}}@keyframes meadow-rise{from{opacity:0;transform:perspective(1400px) translateY(60px) rotateX(14deg)}to{opacity:1;transform:perspective(1400px)}}@keyframes meadow-bar{from{transform:scaleY(0)}to{transform:scaleY(1)}}.meadow-bar{transform-origin:bottom;animation:meadow-bar .7s cubic-bezier(.2,.8,.2,1) both}@media (prefers-reduced-motion:reduce){.meadow-anim,.meadow-bar{animation:none!important}}`}</style>

      {/* scenery */}
      <div className="absolute inset-0 -z-20 bg-[linear-gradient(180deg,#3f8fe6,#9fd0f5_55%,#7fbf6a_75%,#3e8a2c)]">
        {src && <img src={src} alt="" className="meadow-anim h-full w-full object-cover object-bottom" style={{ animation: 'meadow-up 1.6s ease both' }} />}
      </div>
      {/* dot grid and faint guides */}
      <div
        aria-hidden
        className="absolute inset-0 -z-10 [mask-image:linear-gradient(180deg,#000_0%,#000_45%,transparent_70%)]"
        style={{ backgroundImage: 'radial-gradient(rgba(255,255,255,.55) 1px, transparent 1.4px)', backgroundSize: '26px 26px' }}
      />
      <div aria-hidden className="absolute inset-0 -z-10 bg-[linear-gradient(90deg,transparent_calc(50%-480px),rgba(255,255,255,.18)_calc(50%-480px),transparent_calc(50%-479px),transparent_calc(50%+479px),rgba(255,255,255,.18)_calc(50%+479px),transparent_calc(50%+480px))]" />

      {/* nav */}
      <header className="relative z-20 flex justify-center px-4 pt-6 md:pt-8">
        <nav aria-label="Primary" className="flex items-center gap-1 rounded-2xl bg-white/85 p-1.5 pl-4 shadow-[0_8px_30px_-12px_rgba(30,80,140,.35),inset_0_0_0_1px_rgba(255,255,255,.9)] backdrop-blur-xl">
          <a href="#" className="mr-3 flex items-center gap-2 text-[16px] font-medium">
            <SproutMark className="h-6 w-6" /> Tendril
          </a>
          {NAV.map((n) => (
            <a key={n} href="#" className="hidden rounded-xl px-3.5 py-2 text-[15px] text-zinc-700 transition-colors hover:bg-zinc-900/5 hover:text-zinc-950 md:block">
              {n}
            </a>
          ))}
          <a href="#" className="ml-1 hidden rounded-xl bg-zinc-900 px-3.5 py-2 text-[14px] font-medium text-white sm:block">Sign in</a>
          <button onClick={() => setMenu((m) => !m)} aria-expanded={menu} aria-label={menu ? 'Close menu' : 'Open menu'} className="ml-1 flex h-9 w-9 items-center justify-center rounded-xl hover:bg-zinc-900/5 md:hidden">
            {menu ? <X className="h-4 w-4" /> : <Menu className="h-4 w-4" />}
          </button>
        </nav>
        {menu && (
          <div className="meadow-anim absolute inset-x-4 top-[76px] rounded-2xl bg-white/95 p-2 shadow-xl backdrop-blur-xl md:hidden" style={{ animation: 'meadow-up .25s ease both' }}>
            {NAV.map((n) => (
              <a key={n} href="#" className="block rounded-xl px-4 py-3 text-[15px] text-zinc-700 hover:bg-zinc-100">
                {n}
              </a>
            ))}
          </div>
        )}
      </header>

      {/* headline */}
      <div className="relative z-10 mx-auto max-w-5xl px-5 pt-16 text-center md:pt-24">
        <h1 className="text-[34px] font-light leading-[1.12] tracking-[-0.045em] text-zinc-800 min-[420px]:text-[42px] sm:text-[64px] lg:text-[84px]">
          <span className="inline-flex flex-wrap items-center justify-center gap-x-2.5 gap-y-3 sm:gap-x-4">
            <span>Turn</span>
            <span
              className="meadow-anim inline-flex items-center gap-2 sm:gap-3 rounded-[16px] bg-[#d9dcff]/90 px-2.5 py-1 sm:rounded-[22px] sm:px-4 sm:py-1.5 shadow-[0_10px_30px_-12px_rgba(90,90,220,.5),inset_0_0_0_1px_rgba(255,255,255,.7)] sm:px-5"
              style={{ ['--r' as string]: '-1.5deg', animation: 'meadow-chip .7s .3s cubic-bezier(.3,1.4,.5,1) both' }}
            >
              <span className="grid h-7 w-7 place-items-center rounded-lg sm:h-9 sm:w-9 sm:rounded-xl bg-indigo-400/90 text-white sm:h-12 sm:w-12">
                <Lightbulb className="h-4 w-4 sm:h-7 sm:w-7" strokeWidth={1.8} />
              </span>
              <span className="font-serif italic tracking-[-0.02em] text-indigo-500">Ideas</span>
            </span>
            <span>into</span>
          </span>
          <span className="mt-3 inline-flex flex-wrap items-center justify-center gap-x-2.5 gap-y-3 sm:gap-x-4">
            <span className="font-normal text-zinc-950">lasting</span>
            <span
              className="meadow-anim inline-flex items-center gap-2 sm:gap-3 rounded-[16px] bg-[#ffe6cf]/95 px-2.5 py-1 sm:rounded-[22px] sm:px-4 sm:py-1.5 shadow-[0_14px_34px_-14px_rgba(234,88,12,.55),inset_0_0_0_1px_rgba(255,255,255,.8)] sm:px-5"
              style={{ ['--r' as string]: '2deg', animation: 'meadow-chip .7s .5s cubic-bezier(.3,1.4,.5,1) both' }}
            >
              <span className="grid h-7 w-7 place-items-center rounded-lg sm:h-9 sm:w-9 sm:rounded-xl bg-orange-500 text-white shadow-inner sm:h-12 sm:w-12">
                <TrendingUp className="h-4 w-4 sm:h-7 sm:w-7" strokeWidth={2} />
              </span>
              <span className="relative inline-block overflow-hidden pb-1 font-serif italic tracking-[-0.02em] text-orange-600" aria-live="polite">
                <span key={wi} className="meadow-anim inline-block" style={{ animation: 'meadow-word .5s cubic-bezier(.2,.8,.2,1) both' }}>
                  {words[wi]}
                </span>
              </span>
            </span>
          </span>
        </h1>
        <p className="meadow-anim mx-auto mt-8 max-w-xl text-[17px] leading-relaxed text-zinc-700 md:text-[20px]" style={{ animation: 'meadow-up .8s .7s ease both' }}>
          {subtitle}
        </p>
        <div className="meadow-anim mt-9 flex flex-col items-center gap-4" style={{ animation: 'meadow-up .8s .85s ease both' }}>
          <a
            href="#"
            className="group inline-flex h-14 items-center gap-2 rounded-2xl bg-green-500 px-7 text-[17px] font-medium text-white shadow-[inset_0_1px_0_rgba(255,255,255,.35),0_4px_0_#15803d,0_14px_30px_-10px_rgba(22,163,74,.7)] transition-all hover:bg-green-400 active:translate-y-[3px] active:shadow-[inset_0_1px_0_rgba(255,255,255,.35),0_1px_0_#15803d,0_8px_20px_-10px_rgba(22,163,74,.7)] focus-visible:outline-none focus-visible:ring-4 focus-visible:ring-white/70"
          >
            {cta}
            <ArrowUpRight className="h-5 w-5 transition-transform group-hover:-translate-y-0.5 group-hover:translate-x-0.5" />
          </a>
          <span className="flex items-center gap-2 text-[13px] text-zinc-700">
            <span className="flex -space-x-2">
              {['#f9a8d4', '#fdba74', '#a5b4fc', '#86efac'].map((c) => (
                <span key={c} className="h-6 w-6 rounded-full border-2 border-white" style={{ background: c }} />
              ))}
            </span>
            Trusted by 4,000+ product teams
          </span>
        </div>
      </div>

      {/* product */}
      <div className="relative z-10 mx-auto mt-14 max-w-[1120px] px-4 md:mt-20 md:px-8">
        <div className="meadow-anim" style={{ animation: 'meadow-rise 1.2s .9s cubic-bezier(.2,.8,.2,1) both' }}>
          <div className="rounded-[24px] bg-white/35 p-2 backdrop-blur-md [mask-image:linear-gradient(180deg,#000_70%,transparent)]">
            <Dashboard />
          </div>
        </div>
      </div>
    </section>
  );
}

export function HeroMeadowDemo() {
  return <HeroMeadow />;
}
