{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "coin-flip-game",
  "title": "Coin Flip Game",
  "description": "A real 3D CSS coin, click or tap to flip with a physical spin animation, a yes/no question field, flip history dots, and a heads/tails ratio readout.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/coin-flip-game/coin-flip-game.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, AnimatePresence } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type CoinFlipTheme = \"night\" | \"neon\" | \"gold\" | \"tide\";\nexport type CoinFlipTrigger = \"manual\" | \"delay\" | \"scroll\";\nexport type CoinFlipWidgetPosition = \"bottom-right\" | \"bottom-left\";\n\ninterface ThemeConfig {\n  title: string;\n  subtitle: string;\n  buttonText: string;\n  triggerText: string;\n  glow: string;\n  background: string;\n  lightBackground: string;\n  metal: string;\n  metalLight: string;\n  metalDark: string;\n  metalEdge: string;\n}\n\nconst THEMES: Record<CoinFlipTheme, ThemeConfig> = {\n  night: {\n    title: \"Flip\",\n    subtitle: \"Ask. Toss. Decide.\",\n    buttonText: \"Flip the coin\",\n    triggerText: \"Flip a coin\",\n    glow: \"rgba(255,255,255,0.16)\",\n    background: \"linear-gradient(160deg, #07080b 0%, #101319 100%)\",\n    lightBackground: \"linear-gradient(160deg, #f4f6fa 0%, #dfe6f0 100%)\",\n    metal: \"#b8b8b8\",\n    metalLight: \"#e8e8e8\",\n    metalDark: \"#6a6a6a\",\n    metalEdge: \"#d0d0d0\",\n  },\n  neon: {\n    title: \"Toss\",\n    subtitle: \"Heads yes. Tails no.\",\n    buttonText: \"Toss again\",\n    triggerText: \"Open Flip\",\n    glow: \"rgba(129,140,248,0.22)\",\n    background: \"linear-gradient(160deg, #06050c 0%, #100a1c 100%)\",\n    lightBackground: \"linear-gradient(160deg, #eef2ff 0%, #e0e7ff 100%)\",\n    metal: \"#9aa4c8\",\n    metalLight: \"#d7def7\",\n    metalDark: \"#4b556f\",\n    metalEdge: \"#c5ccef\",\n  },\n  gold: {\n    title: \"Coin\",\n    subtitle: \"One question. One toss.\",\n    buttonText: \"Flip again\",\n    triggerText: \"Toss Coin\",\n    glow: \"rgba(251,191,36,0.20)\",\n    background: \"linear-gradient(160deg, #0c0904 0%, #1a1206 100%)\",\n    lightBackground: \"linear-gradient(160deg, #fff7ed 0%, #fde8cf 100%)\",\n    metal: \"#d4af37\",\n    metalLight: \"#f5e6a3\",\n    metalDark: \"#8b6914\",\n    metalEdge: \"#e8c547\",\n  },\n  tide: {\n    title: \"Call\",\n    subtitle: \"Keep a short toss history\",\n    buttonText: \"Flip now\",\n    triggerText: \"Play Call\",\n    glow: \"rgba(45,212,191,0.20)\",\n    background: \"linear-gradient(160deg, #04080a 0%, #081619 100%)\",\n    lightBackground: \"linear-gradient(160deg, #ecfeff 0%, #cffafe 100%)\",\n    metal: \"#cd7f32\",\n    metalLight: \"#e8b07a\",\n    metalDark: \"#8b4513\",\n    metalEdge: \"#daa06d\",\n  },\n};\n\nfunction CoinIcon() {\n  return (\n    <svg width=\"20\" height=\"20\" viewBox=\"0 0 24 24\" fill=\"none\" aria-hidden=\"true\">\n      <circle cx=\"12\" cy=\"12\" r=\"8\" stroke=\"#ffc93c\" strokeWidth=\"1.6\" />\n      <circle cx=\"12\" cy=\"12\" r=\"3\" fill=\"#fffceb\" />\n    </svg>\n  );\n}\n\nexport interface CoinFlipGameProps extends Omit<React.ComponentPropsWithoutRef<\"div\">, \"children\"> {\n  theme?: CoinFlipTheme;\n  lightMode?: boolean;\n  accentColor?: string;\n  size?: number;\n  headsImage?: string;\n  tailsImage?: string;\n  defaultQuestion?: string;\n  asPopup?: boolean;\n  trigger?: CoinFlipTrigger;\n  delaySeconds?: number;\n  widgetPosition?: CoinFlipWidgetPosition;\n  widgetIcon?: string;\n}\n\nexport function CoinFlipGame({\n  theme = \"night\",\n  lightMode = false,\n  accentColor = \"#ffffff\",\n  size = 160,\n  headsImage,\n  tailsImage,\n  defaultQuestion = \"Should I ship this today?\",\n  asPopup = false,\n  trigger = \"manual\",\n  delaySeconds = 4,\n  widgetPosition = \"bottom-right\",\n  widgetIcon,\n  className,\n  style,\n  ...props\n}: CoinFlipGameProps) {\n  const uid = React.useId();\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const gameRootRef = React.useRef<HTMLDivElement>(null);\n  const widgetTriggerRef = React.useRef<HTMLButtonElement>(null);\n  const delayTimerRef = React.useRef<ReturnType<typeof setTimeout> | undefined>(undefined);\n  const wasPopupOpenRef = React.useRef(false);\n\n  const [flipping, setFlipping] = React.useState(false);\n  const [result, setResult] = React.useState<\"Heads\" | \"Tails\" | null>(null);\n  const [question, setQuestion] = React.useState(defaultQuestion);\n  const [history, setHistory] = React.useState<(\"Heads\" | \"Tails\")[]>([]);\n  const [rot, setRot] = React.useState(0);\n  const [popupOpen, setPopupOpen] = React.useState(!asPopup);\n  const [scrollTriggered, setScrollTriggered] = React.useState(false);\n  const [focused, setFocused] = React.useState(false);\n  const [prevAsPopup, setPrevAsPopup] = React.useState(asPopup);\n\n  if (asPopup !== prevAsPopup) {\n    setPrevAsPopup(asPopup);\n    setPopupOpen(!asPopup);\n  }\n\n  const activeTheme = THEMES[theme];\n  const coinSize = Math.max(120, Math.min(200, size));\n  const depth = coinSize / 12;\n  const widgetOnRight = widgetPosition !== \"bottom-left\";\n  const widgetGlowStrong = activeTheme.glow.replace(/,\\s*[\\d.]+\\)$/, \", 0.85)\");\n\n  const mode = lightMode\n    ? {\n        ink: \"#171a21\",\n        inkSoft: \"rgba(23,26,33,0.7)\",\n        inkFaint: \"rgba(23,26,33,0.4)\",\n        divider: \"rgba(23,26,33,0.18)\",\n        surfaceBorder: \"rgba(23,26,33,0.1)\",\n        cardBorder: \"rgba(23,26,33,0.08)\",\n        chromeBg: \"#ffffff\",\n        chromeBorder: \"rgba(23,26,33,0.1)\",\n        inputBg: \"rgba(255,255,255,0.7)\",\n        focusRing: \"0 0 0 2px rgba(23,26,33,0.35)\",\n      }\n    : {\n        ink: \"#ffffff\",\n        inkSoft: \"rgba(255,255,255,0.7)\",\n        inkFaint: \"rgba(255,255,255,0.4)\",\n        divider: \"rgba(255,255,255,0.3)\",\n        surfaceBorder: \"rgba(255,255,255,0.12)\",\n        cardBorder: \"rgba(255,255,255,0.08)\",\n        chromeBg: \"rgba(10,10,15,0.92)\",\n        chromeBorder: \"rgba(255,255,255,0.16)\",\n        inputBg: \"rgba(255,255,255,0.06)\",\n        focusRing: \"0 0 0 2px rgba(255,255,255,0.45)\",\n      };\n\n  React.useEffect(() => () => clearTimeout(delayTimerRef.current), []);\n\n  React.useEffect(() => {\n    if (!asPopup || trigger !== \"delay\") return;\n    clearTimeout(delayTimerRef.current);\n    delayTimerRef.current = setTimeout(() => setPopupOpen(true), delaySeconds * 1000);\n    return () => clearTimeout(delayTimerRef.current);\n  }, [asPopup, trigger, delaySeconds]);\n\n  React.useEffect(() => {\n    if (!asPopup || trigger !== \"scroll\" || !rootRef.current) return;\n    const el = rootRef.current;\n    const observer = new IntersectionObserver(\n      ([entry]) => {\n        if (entry.isIntersecting && !scrollTriggered) {\n          setScrollTriggered(true);\n          setPopupOpen(true);\n        }\n      },\n      { threshold: 0.4 },\n    );\n    observer.observe(el);\n    return () => observer.disconnect();\n  }, [asPopup, trigger, scrollTriggered]);\n\n  React.useEffect(() => {\n    // Only a popup takes focus as it opens; the inline widget must not grab focus (and scroll the\n    // page to itself) just by mounting.\n    if (asPopup && popupOpen) gameRootRef.current?.focus();\n    else if (wasPopupOpenRef.current) widgetTriggerRef.current?.focus();\n    wasPopupOpenRef.current = popupOpen;\n  }, [asPopup, popupOpen]);\n\n  const handleFlip = () => {\n    if (flipping) return;\n    setFlipping(true);\n    setResult(null);\n    const extra = Math.random() > 0.5 ? 720 : 900;\n    setRot((r) => r + extra);\n  };\n\n  const onFlipComplete = () => {\n    if (!flipping) return;\n    const turns = Math.round(rot / 180);\n    const finalResult: \"Heads\" | \"Tails\" = turns % 2 === 0 ? \"Heads\" : \"Tails\";\n    setFlipping(false);\n    setResult(finalResult);\n    setHistory((prev) => [finalResult, ...prev].slice(0, 12));\n  };\n\n  const resolvedAccent = accentColor || \"#ffffff\";\n  const headsCount = history.filter((h) => h === \"Heads\").length;\n  const total = history.length;\n  const headsPct = total ? Math.round((headsCount / total) * 100) : 0;\n  const tailsPct = total ? 100 - headsPct : 0;\n\n  const coinFace = (facing: \"heads\" | \"tails\", image: string | undefined, label: string) => (\n    <div\n      className=\"absolute inset-0 flex items-center justify-center overflow-hidden rounded-full\"\n      style={{\n        transform: facing === \"heads\" ? `translateZ(${depth}px)` : `translateZ(-${depth}px) rotateX(180deg)`,\n        backfaceVisibility: \"hidden\",\n        border: `8px solid ${activeTheme.metalEdge}`,\n        boxShadow: `inset 0 0 0 2px ${activeTheme.metalDark}`,\n        background: `radial-gradient(circle at 30% 28%, ${activeTheme.metalLight}, ${activeTheme.metal} 55%, ${activeTheme.metalDark})`,\n      }}\n    >\n      {image ? (\n        // eslint-disable-next-line @next/next/no-img-element\n        <img src={image} alt={label} className=\"h-full w-full object-cover\" />\n      ) : (\n        <span className=\"font-serif text-[54px] font-normal\" style={{ color: activeTheme.metalDark }}>\n          {label[0]}\n        </span>\n      )}\n    </div>\n  );\n\n  const gameCard = (\n    <div\n      ref={gameRootRef}\n      tabIndex={0}\n      role=\"application\"\n      aria-label={`${activeTheme.title} coin flip`}\n      onPointerDown={() => gameRootRef.current?.focus()}\n      onFocus={() => setFocused(true)}\n      onBlur={() => setFocused(false)}\n      className=\"relative box-border flex w-full flex-col items-center overflow-hidden rounded-[22px] px-5.5 py-7 font-sans outline-none select-none\"\n      style={{ background: lightMode ? activeTheme.lightBackground : activeTheme.background, border: `1px solid ${mode.cardBorder}`, boxShadow: focused ? mode.focusRing : \"none\" }}\n    >\n      <header className=\"mb-3.5 w-full text-center\">\n        <p className=\"m-0 mb-1.5 text-[9px] font-bold tracking-[0.2em] uppercase\" style={{ color: mode.inkFaint }}>\n          Browser tool\n        </p>\n        <h2 id={`${uid}-title`} className=\"m-0 font-serif text-[25px] font-normal tracking-[-0.02em]\" style={{ color: mode.ink }}>\n          {activeTheme.title}\n        </h2>\n        <div className=\"mx-auto my-3 h-px w-8\" style={{ background: mode.divider }} />\n        <p className=\"m-0 text-[10px] font-semibold tracking-[0.18em] uppercase\" style={{ color: mode.inkFaint }}>\n          {activeTheme.subtitle}\n        </p>\n      </header>\n\n      <label htmlFor={`${uid}-q`} className=\"mb-1.5 text-[9px] font-bold tracking-[0.16em] uppercase\" style={{ color: mode.inkFaint }}>\n        Question\n      </label>\n      <input\n        id={`${uid}-q`}\n        value={question}\n        onChange={(e) => setQuestion(e.target.value)}\n        placeholder=\"Type a yes or no question\"\n        className=\"box-border w-full max-w-[320px] rounded-[14px] px-4 py-3 text-center text-sm outline-none\"\n        style={{ background: mode.inputBg, border: `1px solid ${mode.surfaceBorder}`, color: mode.ink }}\n      />\n\n      <button\n        type=\"button\"\n        aria-label=\"Flip the coin\"\n        onClick={handleFlip}\n        disabled={flipping}\n        className=\"my-4.5 border-none bg-transparent p-0\"\n        style={{ width: coinSize, height: coinSize, cursor: flipping ? \"default\" : \"pointer\", perspective: 900 }}\n      >\n        <motion.div\n          className=\"relative h-full w-full\"\n          style={{ transformStyle: \"preserve-3d\" }}\n          animate={{ rotateX: rot }}\n          transition={{ duration: 1.05, ease: [0.2, 0.8, 0.2, 1] }}\n          onAnimationComplete={onFlipComplete}\n        >\n          {coinFace(\"heads\", headsImage, \"Heads\")}\n          {coinFace(\"tails\", tailsImage, \"Tails\")}\n        </motion.div>\n      </button>\n\n      <p className=\"m-0 mb-4 min-h-[20px] text-xs font-semibold tracking-[0.12em] uppercase\" style={{ color: mode.inkSoft }} aria-live=\"polite\">\n        {flipping ? \"Flipping\" : result ? `${result} · ${result === \"Heads\" ? \"Yes\" : \"No\"}` : \"Tap the coin\"}\n      </p>\n\n      <motion.button\n        type=\"button\"\n        onClick={handleFlip}\n        disabled={flipping}\n        whileHover={flipping ? undefined : { scale: 1.03 }}\n        whileTap={flipping ? undefined : { scale: 0.97 }}\n        className=\"mb-2 rounded-full border-none px-7.5 py-3.5 text-xs font-bold tracking-[0.1em] text-[#0a0a0f] uppercase\"\n        style={{ background: resolvedAccent, opacity: flipping ? 0.6 : 1 }}\n      >\n        {result || flipping ? \"Flip again\" : activeTheme.buttonText}\n      </motion.button>\n\n      {history.length > 0 && (\n        <div className=\"mt-5 w-full max-w-[300px] text-center\">\n          <p className=\"m-0 mb-2.5 text-[10px] font-bold tracking-[0.12em] uppercase\" style={{ color: mode.inkFaint }}>\n            Heads {headsPct}% · Tails {tailsPct}% · {total}\n          </p>\n          <div className=\"flex flex-wrap justify-center gap-1.5\">\n            {history.map((h, i) => (\n              <div key={i} title={h} className=\"h-2.5 w-2.5 rounded-full\" style={{ background: h === \"Heads\" ? activeTheme.metal : activeTheme.metalDark }} />\n            ))}\n          </div>\n        </div>\n      )}\n    </div>\n  );\n\n  if (!asPopup) {\n    return (\n      <div ref={rootRef} className={cn(\"w-full\", className)} style={style} {...props}>\n        {gameCard}\n      </div>\n    );\n  }\n\n  return (\n    <div ref={rootRef} className={cn(\"relative w-full\", className)} style={style} {...props}>\n      <div className={cn(\"flex\", widgetOnRight ? \"justify-end\" : \"justify-start\")}>\n        <motion.button\n          ref={widgetTriggerRef}\n          type=\"button\"\n          onClick={() => setPopupOpen(true)}\n          whileHover={{ scale: 1.04 }}\n          whileTap={{ scale: 0.97 }}\n          aria-label={activeTheme.triggerText}\n          className=\"flex items-center gap-3 rounded-full border-none bg-white py-1.5 pr-5 pl-1.5\"\n          style={{ boxShadow: `0 8px 30px ${activeTheme.glow}, 0 4px 16px rgba(0,0,0,0.4)` }}\n        >\n          <span className=\"flex h-11 w-11 shrink-0 items-center justify-center rounded-full border\" style={{ background: \"#121212\", borderColor: \"rgba(255,255,255,0.1)\" }}>\n            {widgetIcon ? (\n              // eslint-disable-next-line @next/next/no-img-element\n              <img src={widgetIcon} alt=\"\" className=\"h-6 w-6 rounded-full object-cover\" />\n            ) : (\n              <CoinIcon />\n            )}\n          </span>\n          <span className=\"flex flex-col gap-0.5 text-left\">\n            <span className=\"text-[9px] font-bold tracking-[0.1em] whitespace-nowrap uppercase\" style={{ color: \"rgba(23,26,33,0.4)\" }}>\n              {activeTheme.triggerText}\n            </span>\n            <motion.span\n              className=\"text-[13px] font-bold tracking-[0.02em] whitespace-nowrap text-[#171a21] uppercase\"\n              animate={{ textShadow: [`0 0 3px ${widgetGlowStrong}`, `0 0 10px ${widgetGlowStrong}`, `0 0 3px ${widgetGlowStrong}`] }}\n              transition={{ duration: 2, repeat: Infinity, ease: \"easeInOut\" }}\n            >\n              Play Now\n            </motion.span>\n          </span>\n        </motion.button>\n      </div>\n\n      <AnimatePresence>\n        {popupOpen && (\n          <motion.div\n            initial={{ opacity: 0 }}\n            animate={{ opacity: 1 }}\n            exit={{ opacity: 0 }}\n            onClick={(e) => {\n              if (e.target === e.currentTarget) setPopupOpen(false);\n            }}\n            className=\"fixed inset-0 z-[9999] flex items-center justify-center p-4\"\n            style={{ background: \"rgba(0,0,0,0.7)\", backdropFilter: \"blur(4px)\" }}\n          >\n            <motion.div\n              initial={{ scale: 0.94, y: 16 }}\n              animate={{ scale: 1, y: 0 }}\n              exit={{ scale: 0.96, opacity: 0 }}\n              transition={{ duration: 0.35, ease: [0.16, 1, 0.3, 1] }}\n              role=\"dialog\"\n              aria-modal=\"true\"\n              aria-labelledby={`${uid}-title`}\n              onKeyDown={(e) => {\n                if (e.key === \"Escape\") setPopupOpen(false);\n              }}\n              className=\"relative w-full max-w-[480px]\"\n            >\n              {gameCard}\n              <button\n                type=\"button\"\n                onClick={() => setPopupOpen(false)}\n                aria-label=\"Close flip\"\n                className=\"absolute -top-3.5 -right-3.5 flex h-8 w-8 items-center justify-center rounded-full border text-[17px] leading-[30px]\"\n                style={{ background: mode.chromeBg, borderColor: mode.chromeBorder, color: mode.ink }}\n              >\n                ×\n              </button>\n            </motion.div>\n          </motion.div>\n        )}\n      </AnimatePresence>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}