// AppleCell — SVG apple icon with a number inside. Three shape variants.
// Used both inside the game grid and as a brand element.

function AppleCell({ n = 1, size = 44, shape = "round", selected = false, ghost = false, leaf = true }) {
  if (n === 0) return <div style={{ width: size, height: size }} />;
  const s = size;
  const fontSize = Math.round(s * 0.56);
  // 흰 숫자 + 사과보다 진한 외곽선: 밝은 사과 색에서도 숫자가 묻히지 않게
  const numProps = ghost ? {} : { stroke: 'var(--apple-deep, #8f2428)', strokeWidth: s * 0.045, strokeLinejoin: 'round', paintOrder: 'stroke' };
  const ring = selected ? (
    <circle cx={s / 2} cy={s / 2 + s * 0.05} r={s * 0.46} fill="none"
            stroke="#2f6fdb" strokeWidth={s * 0.05} />
  ) : null;

  if (shape === "square") {
    return (
      <svg width={s} height={s} viewBox={`0 0 ${s} ${s}`} style={{ display: 'block' }}>
        {ring}
        <rect x={s*0.12} y={s*0.18} width={s*0.76} height={s*0.76} rx={s*0.12}
              fill={ghost ? "#EBDFC4" : "var(--apple, #D62828)"} />
        {leaf && !ghost && (
          <path d={`M ${s*0.62} ${s*0.18} q ${s*0.08} -${s*0.10} ${s*0.18} -${s*0.04} q -${s*0.04} ${s*0.12} -${s*0.18} ${s*0.06} z`}
                fill="var(--leaf, #2D6A4F)" />
        )}
        <text x={s/2} y={s/2 + s*0.02} textAnchor="middle" dominantBaseline="middle"
              fontFamily="var(--font-num)" fontSize={fontSize} fill={ghost ? "#B8A98A" : "#fff"}
              {...numProps} style={{ fontWeight: 800, letterSpacing: '-0.02em' }}>{n}</text>
      </svg>
    );
  }

  if (shape === "realistic") {
    // Apple body with subtle indent at top, stem, leaf
    return (
      <svg width={s} height={s} viewBox={`0 0 ${s} ${s}`} style={{ display: 'block' }}>
        {ring}
        {/* stem */}
        <path d={`M ${s*0.50} ${s*0.18} Q ${s*0.53} ${s*0.10} ${s*0.56} ${s*0.13}`}
              stroke={ghost ? "#A6936E" : "var(--bark, #6B4226)"} strokeWidth={s*0.04} strokeLinecap="round" fill="none" />
        {/* leaf */}
        {leaf && (
          <path d={`M ${s*0.55} ${s*0.16} Q ${s*0.72} ${s*0.08} ${s*0.78} ${s*0.22} Q ${s*0.66} ${s*0.24} ${s*0.55} ${s*0.20} Z`}
                fill={ghost ? "#B8B093" : "var(--leaf, #2D6A4F)"} />
        )}
        {/* body */}
        <path d={`
          M ${s*0.50} ${s*0.22}
          C ${s*0.20} ${s*0.20} ${s*0.10} ${s*0.46} ${s*0.14} ${s*0.66}
          C ${s*0.18} ${s*0.86} ${s*0.34} ${s*0.94} ${s*0.50} ${s*0.92}
          C ${s*0.66} ${s*0.94} ${s*0.82} ${s*0.86} ${s*0.86} ${s*0.66}
          C ${s*0.90} ${s*0.46} ${s*0.80} ${s*0.20} ${s*0.50} ${s*0.22} Z`}
          fill={ghost ? "#E0D2B0" : "var(--apple, #D62828)"} />
        <text x={s*0.50} y={s*0.60} textAnchor="middle" dominantBaseline="middle"
              fontFamily="var(--font-num)" fontSize={fontSize}
              fill={ghost ? "#A89875" : "#fff"}
              {...numProps} style={{ fontWeight: 800, letterSpacing: '-0.02em' }}>{n}</text>
      </svg>
    );
  }

  if (shape === "3d") {
    return (
      <svg width={s} height={s} viewBox={`0 0 ${s} ${s}`} style={{ display: 'block', overflow: 'visible' }}>
        <defs>
          <radialGradient id={`apple-grad-${n}`} cx="35%" cy="35%" r="65%">
            <stop offset="0%" stopColor="#fff" stopOpacity="0.5" />
            <stop offset="15%" stopColor={ghost ? "#E0D2B0" : "var(--apple, #D62828)"} />
            <stop offset="90%" stopColor={ghost ? "#B8A98A" : "var(--apple-deep, #A91D29)"} />
            <stop offset="100%" stopColor="#000" stopOpacity="0.2" />
          </radialGradient>
          <filter id={`shadow-${n}`} x="-20%" y="-20%" width="150%" height="150%">
            <feDropShadow dx="0" dy={s*0.06} stdDeviation={s*0.04} floodOpacity="0.3" />
          </filter>
        </defs>
        {ring}
        {/* stem */}
        <path d={`M ${s*0.50} ${s*0.18} Q ${s*0.53} ${s*0.10} ${s*0.56} ${s*0.13}`}
              stroke={ghost ? "#A6936E" : "var(--bark, #6B4226)"} strokeWidth={s*0.04} strokeLinecap="round" fill="none" filter={`url(#shadow-${n})`} />
        {/* leaf */}
        {leaf && (
          <path d={`M ${s*0.55} ${s*0.16} Q ${s*0.72} ${s*0.08} ${s*0.78} ${s*0.22} Q ${s*0.66} ${s*0.24} ${s*0.55} ${s*0.20} Z`}
                fill={ghost ? "#B8B093" : "var(--leaf, #2D6A4F)"} filter={`url(#shadow-${n})`} />
        )}
        {/* body */}
        <path d={`
          M ${s*0.50} ${s*0.22}
          C ${s*0.20} ${s*0.20} ${s*0.10} ${s*0.46} ${s*0.14} ${s*0.66}
          C ${s*0.18} ${s*0.86} ${s*0.34} ${s*0.94} ${s*0.50} ${s*0.92}
          C ${s*0.66} ${s*0.94} ${s*0.82} ${s*0.86} ${s*0.86} ${s*0.66}
          C ${s*0.90} ${s*0.46} ${s*0.80} ${s*0.20} ${s*0.50} ${s*0.22} Z`}
          fill={`url(#apple-grad-${n})`} filter={`url(#shadow-${n})`} />
        <text x={s*0.50} y={s*0.60} textAnchor="middle" dominantBaseline="middle"
              fontFamily="var(--font-num)" fontSize={fontSize}
              fill={ghost ? "#A89875" : "#fff"}
              {...numProps} style={{ fontWeight: 800, letterSpacing: '-0.02em', textShadow: '0 2px 4px rgba(0,0,0,0.3)' }}>{n}</text>
      </svg>
    );
  }

  // "round" — 칸을 거의 채우는 원 + 짧은 꼭지, 숫자 가독성 우선
  return (
    <svg width={s} height={s} viewBox={`0 0 ${s} ${s}`} style={{ display: 'block' }}>
      {ring}
      {!ghost && <line x1={s*0.5} y1={s*0.03} x2={s*0.5} y2={s*0.13} stroke="var(--bark, #6B5A3A)" strokeWidth={Math.max(1.5, s*0.035)} strokeLinecap="round" />}
      <circle cx={s/2} cy={s*0.53} r={s*0.44} fill={ghost ? "#E0D2B0" : "var(--apple, #e8575d)"} />
      <text x={s/2} y={s*0.55} textAnchor="middle" dominantBaseline="middle"
            fontFamily='"Pretendard Variable", Pretendard, var(--font-num)' fontSize={Math.round(s * 0.56)}
            fill={ghost ? "#A89875" : "#fff"}
            {...(ghost ? {} : { stroke: 'var(--apple-deep, #8f2428)', strokeWidth: Math.max(1.2, s * 0.025), strokeLinejoin: 'round', paintOrder: 'stroke' })}
            style={{ fontWeight: 800, fontVariantNumeric: 'tabular-nums' }}>{n}</text>
    </svg>
  );
}

// Mascot — friendly apple character with face, used on main + result screens.
function AppleMascot({ size = 200, mood = "happy" }) {
  const s = size;
  const eyeOpen = mood !== "sleepy";
  const smile = mood === "happy" ? 1 : mood === "panic" ? -1 : 0.3;
  return (
    <svg width={s} height={s} viewBox="0 0 200 200" style={{ display: 'block' }}>
      {/* stem */}
      <path d="M 100 38 Q 108 22 116 28" stroke="#6B4226" strokeWidth="6" strokeLinecap="round" fill="none" />
      {/* leaf */}
      <path d="M 114 30 Q 150 16 160 50 Q 130 52 112 38 Z" fill="var(--leaf, #2D6A4F)" />
      <path d="M 120 36 Q 138 30 152 40" stroke="rgba(255,255,255,0.4)" strokeWidth="1.5" fill="none" />
      {/* body */}
      <path d="
        M 100 46
        C 40 42 22 96 28 130
        C 34 168 64 184 100 180
        C 136 184 166 168 172 130
        C 178 96 160 42 100 46 Z" fill="var(--apple, #D62828)" />
      {/* cheek blush */}
      <ellipse cx="58" cy="130" rx="14" ry="9" fill="#FFB199" opacity="0.55" />
      <ellipse cx="142" cy="130" rx="14" ry="9" fill="#FFB199" opacity="0.55" />
      {/* eyes */}
      {eyeOpen ? (
        <>
          <ellipse cx="76" cy="108" rx="6" ry="8" fill="#1F1812" />
          <ellipse cx="124" cy="108" rx="6" ry="8" fill="#1F1812" />
          <circle cx="78" cy="105" r="2" fill="#fff" />
          <circle cx="126" cy="105" r="2" fill="#fff" />
        </>
      ) : (
        <>
          <path d="M 70 108 Q 76 102 82 108" stroke="#1F1812" strokeWidth="3" fill="none" strokeLinecap="round" />
          <path d="M 118 108 Q 124 102 130 108" stroke="#1F1812" strokeWidth="3" fill="none" strokeLinecap="round" />
        </>
      )}
      {/* mouth */}
      {smile > 0 ? (
        <path d={`M 86 138 Q 100 ${138 + smile * 14} 114 138`} stroke="#1F1812" strokeWidth="3.5" fill="none" strokeLinecap="round" />
      ) : smile < 0 ? (
        <path d="M 86 142 Q 100 130 114 142" stroke="#1F1812" strokeWidth="3.5" fill="none" strokeLinecap="round" />
      ) : (
        <line x1="92" y1="140" x2="108" y2="140" stroke="#1F1812" strokeWidth="3.5" strokeLinecap="round" />
      )}
    </svg>
  );
}

Object.assign(window, { AppleCell, AppleMascot });