{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "memory-cards-widget",
  "title": "Memory Cards Widget",
  "description": "A floating-action-button popup that opens a color-matching memory game, 3 difficulty levels, a live timer/move counter, best-score tracking, subtle Web Audio sound effects, and a confetti win screen.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/memory-cards-widget/memory-cards-widget.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\ninterface CardItem {\n  color: string;\n  uniqueId: number;\n}\n\ntype Difficulty = \"easy\" | \"medium\" | \"hard\";\ntype PopupPosition = \"bottom-right\" | \"bottom-left\";\n\nconst ALL_COLORS = [\n  \"#FF2D55\",\n  \"#007AFF\",\n  \"#34C759\",\n  \"#FF9500\",\n  \"#AF52DE\",\n  \"#FFCC00\",\n  \"#5AC8FA\",\n  \"#FF3B30\",\n  \"#30D158\",\n  \"#BF5AF2\",\n  \"#64D2FF\",\n  \"#FF9F0A\",\n];\n\n// Spoken names for the card colors, so a screen reader can tell the pairs apart.\nconst COLOR_NAMES: Record<string, string> = {\n  \"#FF2D55\": \"pink\",\n  \"#007AFF\": \"blue\",\n  \"#34C759\": \"green\",\n  \"#FF9500\": \"orange\",\n  \"#AF52DE\": \"purple\",\n  \"#FFCC00\": \"yellow\",\n  \"#5AC8FA\": \"sky blue\",\n  \"#FF3B30\": \"red\",\n  \"#30D158\": \"mint\",\n  \"#BF5AF2\": \"lilac\",\n  \"#64D2FF\": \"cyan\",\n  \"#FF9F0A\": \"amber\",\n};\n\nfunction shuffle<T>(array: T[]): T[] {\n  const arr = [...array];\n  for (let i = arr.length - 1; i > 0; i--) {\n    const j = Math.floor(Math.random() * (i + 1));\n    [arr[i], arr[j]] = [arr[j], arr[i]];\n  }\n  return arr;\n}\n\nconst playSound = (type: \"flip\" | \"match\" | \"win\") => {\n  try {\n    const AudioContextClass = window.AudioContext || (window as Window & { webkitAudioContext?: typeof AudioContext }).webkitAudioContext;\n    if (!AudioContextClass) return;\n    const ctx = new AudioContextClass();\n    const osc = ctx.createOscillator();\n    const gain = ctx.createGain();\n    osc.connect(gain);\n    gain.connect(ctx.destination);\n\n    if (type === \"flip\") {\n      osc.frequency.value = 380;\n      gain.gain.value = 0.04;\n      osc.start();\n      osc.stop(ctx.currentTime + 0.07);\n    } else if (type === \"match\") {\n      osc.frequency.value = 700;\n      gain.gain.value = 0.07;\n      osc.start();\n      osc.frequency.linearRampToValueAtTime(1100, ctx.currentTime + 0.12);\n      osc.stop(ctx.currentTime + 0.12);\n    } else {\n      osc.frequency.value = 500;\n      gain.gain.value = 0.09;\n      osc.start();\n      osc.frequency.linearRampToValueAtTime(1300, ctx.currentTime + 0.35);\n      osc.stop(ctx.currentTime + 0.35);\n    }\n  } catch {\n    // audio unsupported or blocked — silently skip\n  }\n};\n\nfunction CardsIcon() {\n  return (\n    <svg width=\"22\" height=\"22\" viewBox=\"0 0 24 24\" fill=\"none\">\n      <rect x=\"2\" y=\"4\" width=\"9\" height=\"12\" rx=\"2\" transform=\"rotate(-8 6.5 10)\" fill=\"white\" fillOpacity=\"0.9\" />\n      <rect x=\"12\" y=\"6\" width=\"10\" height=\"14\" rx=\"2.5\" fill=\"white\" />\n    </svg>\n  );\n}\n\nfunction Confetti() {\n  return (\n    <div className=\"pointer-events-none absolute inset-0 overflow-hidden\">\n      {Array.from({ length: 50 }).map((_, i) => (\n        <div\n          key={i}\n          className=\"absolute\"\n          style={{\n            width: 6 + Math.random() * 6,\n            height: 6 + Math.random() * 6,\n            background: ALL_COLORS[i % ALL_COLORS.length],\n            left: Math.random() * 100 + \"%\",\n            top: -20,\n            borderRadius: Math.random() > 0.5 ? \"50%\" : 2,\n            animation: `mcw-confetti-fall ${2 + Math.random() * 2}s linear forwards`,\n            animationDelay: Math.random() * 0.5 + \"s\",\n          }}\n        />\n      ))}\n    </div>\n  );\n}\n\nexport interface MemoryCardsWidgetProps extends Omit<React.ComponentPropsWithoutRef<\"div\">, \"children\"> {\n  difficulty?: Difficulty;\n  position?: PopupPosition;\n  defaultOpen?: boolean;\n}\n\nexport function MemoryCardsWidget({\n  className,\n  style,\n  difficulty: propDifficulty = \"medium\",\n  position = \"bottom-right\",\n  defaultOpen = false,\n  ...props\n}: MemoryCardsWidgetProps) {\n  const [isOpen, setIsOpen] = React.useState(defaultOpen);\n  const [difficulty, setDifficulty] = React.useState<Difficulty>(propDifficulty);\n  const pairCount = difficulty === \"easy\" ? 6 : difficulty === \"hard\" ? 12 : 8;\n\n  const [cards, setCards] = React.useState<CardItem[]>([]);\n  const [flipped, setFlipped] = React.useState<number[]>([]);\n  const [matched, setMatched] = React.useState<string[]>([]);\n  const [moves, setMoves] = React.useState(0);\n  const [time, setTime] = React.useState(0);\n  const [isRunning, setIsRunning] = React.useState(false);\n  const [showModal, setShowModal] = React.useState(false);\n  const [bestScore, setBestScore] = React.useState<number | null>(null);\n\n  const timerRef = React.useRef<ReturnType<typeof setInterval> | null>(null);\n  const instanceId = React.useId();\n\n  React.useEffect(() => {\n    const saved = localStorage.getItem(\"memory-cards-widget-best\");\n    if (saved) setBestScore(Number(saved));\n  }, []);\n\n  React.useEffect(() => {\n    setDifficulty(propDifficulty);\n  }, [propDifficulty]);\n\n  const startGame = React.useCallback(() => {\n    const selectedColors = ALL_COLORS.slice(0, pairCount);\n    const doubled = [...selectedColors, ...selectedColors];\n    const shuffled = shuffle(doubled).map((color, index) => ({ color, uniqueId: index }));\n    setCards(shuffled);\n    setFlipped([]);\n    setMatched([]);\n    setMoves(0);\n    setTime(0);\n    setIsRunning(false);\n    setShowModal(false);\n  }, [pairCount]);\n\n  React.useEffect(() => {\n    startGame();\n  }, [difficulty, startGame]);\n\n  React.useEffect(() => {\n    if (!isRunning) return;\n    timerRef.current = setInterval(() => setTime((t) => t + 1), 1000);\n    return () => {\n      if (timerRef.current) clearInterval(timerRef.current);\n    };\n  }, [isRunning]);\n\n  const handleCardClick = (uniqueId: number, color: string) => {\n    if (flipped.length === 2 || flipped.includes(uniqueId) || matched.includes(color)) return;\n\n    if (!isRunning) setIsRunning(true);\n    playSound(\"flip\");\n\n    const newFlipped = [...flipped, uniqueId];\n    setFlipped(newFlipped);\n\n    if (newFlipped.length === 2) {\n      setMoves((m) => m + 1);\n      const first = cards.find((c) => c.uniqueId === newFlipped[0]);\n      const second = cards.find((c) => c.uniqueId === newFlipped[1]);\n      if (!first || !second) return;\n\n      if (first.color === second.color) {\n        playSound(\"match\");\n        setMatched((prev) => [...prev, first.color]);\n        setFlipped([]);\n\n        if (matched.length + 1 === pairCount) {\n          setIsRunning(false);\n          playSound(\"win\");\n          setTimeout(() => {\n            setShowModal(true);\n            if (!bestScore || moves + 1 < bestScore) {\n              localStorage.setItem(\"memory-cards-widget-best\", String(moves + 1));\n              setBestScore(moves + 1);\n            }\n          }, 400);\n        }\n      } else {\n        setTimeout(() => setFlipped([]), 600);\n      }\n    }\n  };\n\n  const columns = pairCount <= 6 ? 4 : pairCount <= 8 ? 4 : 6;\n\n  return (\n    <div data-mcw={instanceId} className={cn(\"relative\", className)} style={{ width: \"100%\", maxWidth: 440, height: 500, fontFamily: \"Inter, system-ui, sans-serif\", ...style }} {...props}>\n      <style>{`\n        [data-mcw=\"${instanceId}\"] * { box-sizing: border-box; }\n\n        [data-mcw=\"${instanceId}\"] .mcw-card:focus-visible {\n          outline: 2px solid #5AC8FA;\n          outline-offset: 2px;\n        }\n\n        [data-mcw=\"${instanceId}\"] .mcw-fab {\n          position: absolute;\n          bottom: 0;\n          width: 56px;\n          height: 56px;\n          border-radius: 50%;\n          border: none;\n          cursor: pointer;\n          background: linear-gradient(135deg, #FF2D55, #AF52DE);\n          display: flex;\n          align-items: center;\n          justify-content: center;\n          box-shadow: 0 8px 24px rgba(0,0,0,0.35);\n          transition: transform 0.2s ease, opacity 0.2s ease;\n          z-index: 10;\n        }\n        [data-mcw=\"${instanceId}\"] .mcw-fab.bottom-right { right: 0; }\n        [data-mcw=\"${instanceId}\"] .mcw-fab.bottom-left { left: 0; }\n        [data-mcw=\"${instanceId}\"] .mcw-fab:hover { transform: scale(1.06); }\n        [data-mcw=\"${instanceId}\"] .mcw-fab.mcw-hidden {\n          opacity: 0;\n          pointer-events: none;\n          transform: scale(0.8);\n        }\n\n        [data-mcw=\"${instanceId}\"] .mcw-panel {\n          position: absolute;\n          bottom: 0;\n          width: 100%;\n          height: 100%;\n          border-radius: 16px;\n          overflow: hidden;\n          box-shadow: 0 20px 60px rgba(0,0,0,0.5);\n          opacity: 0;\n          transform: scale(0.85) translateY(12px);\n          pointer-events: none;\n          transition: transform 0.28s cubic-bezier(0.4,0,0.2,1), opacity 0.2s ease;\n          z-index: 20;\n          background: #0a0a0a;\n          color: white;\n          display: flex;\n          flex-direction: column;\n        }\n        [data-mcw=\"${instanceId}\"] .mcw-panel.bottom-right { right: 0; transform-origin: bottom right; }\n        [data-mcw=\"${instanceId}\"] .mcw-panel.bottom-left { left: 0; transform-origin: bottom left; }\n        [data-mcw=\"${instanceId}\"] .mcw-panel.mcw-open {\n          opacity: 1;\n          transform: scale(1) translateY(0);\n          pointer-events: auto;\n        }\n\n        [data-mcw=\"${instanceId}\"] .mcw-close-btn {\n          background: rgba(255,255,255,0.12);\n          border: none;\n          color: white;\n          width: 26px;\n          height: 26px;\n          border-radius: 8px;\n          cursor: pointer;\n          font-size: 15px;\n          line-height: 1;\n          display: flex;\n          align-items: center;\n          justify-content: center;\n        }\n        [data-mcw=\"${instanceId}\"] .mcw-close-btn:hover {\n          background: rgba(255,255,255,0.2);\n        }\n\n        [data-mcw=\"${instanceId}\"] .mcw-header {\n          flex-shrink: 0;\n          display: flex;\n          flex-wrap: wrap;\n          justify-content: space-between;\n          align-items: center;\n          padding: 10px 14px;\n          background: rgba(255,255,255,0.06);\n          font-size: 13px;\n          font-weight: 600;\n          gap: 8px 12px;\n        }\n\n        [data-mcw=\"${instanceId}\"] .mcw-header-left {\n          display: flex;\n          flex-wrap: wrap;\n          gap: 10px 14px;\n        }\n\n        [data-mcw=\"${instanceId}\"] .mcw-header-right {\n          display: flex;\n          align-items: center;\n          gap: 8px;\n        }\n\n        [data-mcw=\"${instanceId}\"] .mcw-stat-label {\n          opacity: 0.5;\n          font-weight: 500;\n          font-size: 11px;\n          text-transform: uppercase;\n          letter-spacing: 0.03em;\n          margin-right: 2px;\n        }\n\n        [data-mcw=\"${instanceId}\"] .mcw-reset-btn {\n          background: rgba(255,255,255,0.12);\n          border: none;\n          color: white;\n          font-size: 12px;\n          font-weight: 600;\n          padding: 5px 12px;\n          border-radius: 8px;\n          cursor: pointer;\n        }\n        [data-mcw=\"${instanceId}\"] .mcw-reset-btn:hover {\n          background: rgba(255,255,255,0.2);\n        }\n\n        [data-mcw=\"${instanceId}\"] .mcw-difficulty-select {\n          background: rgba(255,255,255,0.12);\n          border: none;\n          color: white;\n          font-size: 12px;\n          font-weight: 600;\n          padding: 5px 10px;\n          border-radius: 8px;\n          cursor: pointer;\n        }\n        [data-mcw=\"${instanceId}\"] .mcw-difficulty-select:focus-visible {\n          outline: 2px solid #5AC8FA;\n          outline-offset: 2px;\n        }\n        [data-mcw=\"${instanceId}\"] .mcw-difficulty-select option {\n          background: #1c1c1e;\n          color: white;\n        }\n\n        [data-mcw=\"${instanceId}\"] .mcw-game {\n          flex: 1;\n          min-height: 0;\n          display: grid;\n          grid-template-columns: repeat(${columns}, minmax(0, 1fr));\n          gap: 8px;\n          padding: 10px;\n          overflow: hidden;\n        }\n\n        [data-mcw=\"${instanceId}\"] .mcw-card {\n          min-width: 0;\n          min-height: 0;\n          perspective: 800px;\n          cursor: pointer;\n        }\n\n        [data-mcw=\"${instanceId}\"] .mcw-inside {\n          width: 100%;\n          height: 100%;\n          position: relative;\n          transform-style: preserve-3d;\n          transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);\n          border-radius: 12px;\n        }\n\n        [data-mcw=\"${instanceId}\"] .mcw-inside.mcw-flipped {\n          transform: rotateY(180deg);\n        }\n\n        [data-mcw=\"${instanceId}\"] .mcw-inside.mcw-matched {\n          animation: mcw-match-pulse 0.4s ease;\n        }\n\n        @keyframes mcw-match-pulse {\n          0%   { transform: rotateY(180deg) scale(1); }\n          50%  { transform: rotateY(180deg) scale(1.06); }\n          100% { transform: rotateY(180deg) scale(1); }\n        }\n\n        [data-mcw=\"${instanceId}\"] .mcw-front, [data-mcw=\"${instanceId}\"] .mcw-back {\n          position: absolute;\n          inset: 0;\n          backface-visibility: hidden;\n          border-radius: 12px;\n        }\n\n        [data-mcw=\"${instanceId}\"] .mcw-back {\n          background: white;\n          box-shadow: 0 3px 10px rgba(0,0,0,0.25);\n        }\n\n        [data-mcw=\"${instanceId}\"] .mcw-front {\n          transform: rotateY(180deg);\n        }\n\n        [data-mcw=\"${instanceId}\"] .mcw-modal-overlay {\n          position: absolute;\n          inset: 0;\n          background: rgba(0,0,0,0.88);\n          display: flex;\n          align-items: center;\n          justify-content: center;\n          z-index: 50;\n        }\n\n        [data-mcw=\"${instanceId}\"] .mcw-modal {\n          background: #1c1c1e;\n          padding: 32px 24px;\n          border-radius: 18px;\n          text-align: center;\n          width: 90%;\n          max-width: 320px;\n          border: 1px solid rgba(255,255,255,0.08);\n        }\n\n        [data-mcw=\"${instanceId}\"] .mcw-modal h2 {\n          font-size: 28px;\n          font-weight: 700;\n          margin-bottom: 8px;\n          background: linear-gradient(90deg, #FF2D55, #AF52DE);\n          -webkit-background-clip: text;\n          -webkit-text-fill-color: transparent;\n        }\n\n        [data-mcw=\"${instanceId}\"] .mcw-stats {\n          margin: 16px 0;\n          font-size: 15px;\n          opacity: 0.9;\n          line-height: 1.5;\n        }\n\n        [data-mcw=\"${instanceId}\"] .mcw-restart {\n          background: linear-gradient(90deg, #FF2D55, #AF52DE);\n          border: none;\n          color: white;\n          font-weight: 700;\n          font-size: 15px;\n          padding: 12px 26px;\n          border-radius: 11px;\n          cursor: pointer;\n        }\n\n        @keyframes mcw-confetti-fall {\n          0%   { transform: translateY(0) rotate(0deg); opacity: 1; }\n          100% { transform: translateY(110vh) rotate(720deg); opacity: 0; }\n        }\n      `}</style>\n\n      <button className={`mcw-fab ${position} ${isOpen ? \"mcw-hidden\" : \"\"}`} onClick={() => setIsOpen(true)} aria-label=\"Open memory game\">\n        <CardsIcon />\n      </button>\n\n      <div className={`mcw-panel ${position} ${isOpen ? \"mcw-open\" : \"\"}`}>\n        <div className=\"mcw-header\">\n          <div className=\"mcw-header-left\">\n            <span>\n              <span className=\"mcw-stat-label\">Time</span> {time}s\n            </span>\n            <span>\n              <span className=\"mcw-stat-label\">Moves</span> {moves}\n            </span>\n            <span>\n              <span className=\"mcw-stat-label\">Best</span> {bestScore ?? \"-\"}\n            </span>\n          </div>\n\n          <div className=\"mcw-header-right\">\n            <button className=\"mcw-reset-btn\" onClick={startGame}>\n              Reset\n            </button>\n\n            <select className=\"mcw-difficulty-select\" value={difficulty} onChange={(e) => setDifficulty(e.target.value as Difficulty)}>\n              <option value=\"easy\">Easy</option>\n              <option value=\"medium\">Medium</option>\n              <option value=\"hard\">Hard</option>\n            </select>\n\n            <button className=\"mcw-close-btn\" onClick={() => setIsOpen(false)} aria-label=\"Close memory game\">\n              ×\n            </button>\n          </div>\n        </div>\n\n        <div className=\"mcw-game\">\n          {cards.map((card) => {\n            const isFlipped = flipped.includes(card.uniqueId) || matched.includes(card.color);\n            return (\n              <div\n                key={card.uniqueId}\n                className=\"mcw-card\"\n                role=\"button\"\n                tabIndex={0}\n                aria-pressed={isFlipped}\n                aria-label={isFlipped ? `Memory card, ${COLOR_NAMES[card.color] ?? \"revealed\"}` : \"Memory card, hidden\"}\n                onClick={() => handleCardClick(card.uniqueId, card.color)}\n                onKeyDown={(e) => {\n                  if (e.key === \"Enter\" || e.key === \" \") {\n                    e.preventDefault();\n                    handleCardClick(card.uniqueId, card.color);\n                  }\n                }}\n              >\n                <div className={`mcw-inside ${isFlipped ? \"mcw-flipped\" : \"\"} ${matched.includes(card.color) ? \"mcw-matched\" : \"\"}`}>\n                  <div className=\"mcw-back\" />\n                  <div className=\"mcw-front\" style={{ background: card.color }} />\n                </div>\n              </div>\n            );\n          })}\n        </div>\n\n        {showModal && (\n          <div className=\"mcw-modal-overlay\">\n            <Confetti />\n            <div className=\"mcw-modal\">\n              <h2>You Rock!</h2>\n              <div className=\"mcw-stats\">\n                <div>{moves} moves</div>\n                <div>{time} seconds</div>\n                {bestScore === moves && <div style={{ marginTop: 6, color: \"#FFD700\" }}>New record!</div>}\n              </div>\n              <button className=\"mcw-restart\" onClick={startGame}>\n                Play Again\n              </button>\n            </div>\n          </div>\n        )}\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}