// companions.jsx — flat-vector food mascots. Varied expressions + slight head
// tilt per character for liveliness. Exposes window.Companion + window.COMPANIONS.
(function () {
  // ── face system ──────────────────────────────────────────────────
  // mode: 'open' (bright round eyes), 'happy' (smiling closed arcs),
  //       'sleepy' (calm lowered lids), 'wink' (one open, one closed)
  function Face({ cx = 50, cy = 72, s = 1, ink = '#2b2a28', cheek = '#ff8fa3', mode = 'open', mouth = 'smile' }) {
    const ex = 7.6 * s, ey = cy;
    const eye = (x, kind) => {
      if (kind === 'open') return (
        <g key={x}>
          <circle cx={x} cy={ey} r={3.3 * s} fill={ink} />
          <circle cx={x - 1.1 * s} cy={ey - 1.2 * s} r={1.15 * s} fill="#fff" opacity="0.95" />
        </g>
      );
      if (kind === 'sleepy') return (
        <path key={x} d={`M${x - 4.4 * s} ${ey + 0.5 * s} q ${4.4 * s} ${-4.2 * s} ${8.8 * s} 0`} fill="none" stroke={ink} strokeWidth={2.3 * s} strokeLinecap="round" />
      );
      // happy / closed — upward smiling arc
      return (
        <path key={x} d={`M${x - 4.4 * s} ${ey} q ${4.4 * s} ${5 * s} ${8.8 * s} 0`} fill="none" stroke={ink} strokeWidth={2.4 * s} strokeLinecap="round" />
      );
    };
    const L = mode === 'wink' ? 'open' : mode;
    const R = mode === 'wink' ? 'happy' : mode;
    return (
      <g>
        {eye(cx - ex, L)}
        {eye(cx + ex, R)}
        <ellipse cx={cx - 13 * s} cy={ey + 6.5 * s} rx={3.4 * s} ry={2.3 * s} fill={cheek} opacity="0.5" />
        <ellipse cx={cx + 13 * s} cy={ey + 6.5 * s} rx={3.4 * s} ry={2.3 * s} fill={cheek} opacity="0.5" />
        {mouth === 'small'
          ? <path d={`M${cx - 3.4 * s} ${ey + 8 * s} q ${3.4 * s} ${4 * s} ${6.8 * s} 0`} fill="none" stroke={ink} strokeWidth={2 * s} strokeLinecap="round" />
          : <path d={`M${cx - 5 * s} ${ey + 7.5 * s} q ${5 * s} ${7 * s} ${10 * s} 0 z`} fill={ink} />}
      </g>
    );
  }

  const Brock = () => (
    <g>
      <path d="M34 86 q-12 -2 -16 8" fill="none" stroke="#bcdc98" strokeWidth="7" strokeLinecap="round" />
      <path d="M66 86 q12 -8 20 -18" fill="none" stroke="#bcdc98" strokeWidth="7" strokeLinecap="round" />
      <rect x="36" y="58" width="28" height="50" rx="14" fill="#cfe6a8" />
      <g fill="#2f8f3e"><circle cx="34" cy="42" r="16" /><circle cx="66" cy="42" r="16" /><circle cx="50" cy="32" r="17" /></g>
      <g fill="#62c24a"><circle cx="30" cy="46" r="12" /><circle cx="50" cy="40" r="14" /><circle cx="70" cy="46" r="12" /><circle cx="40" cy="57" r="12" /><circle cx="60" cy="57" r="12" /></g>
      <Face cx={50} cy={82} ink="#2e6b33" mode="open" />
    </g>
  );

  const Avo = () => (
    <g>
      <path d="M33 84 q-12 0 -15 11" fill="none" stroke="#9fce6e" strokeWidth="7" strokeLinecap="round" />
      <path d="M67 84 q12 0 15 11" fill="none" stroke="#9fce6e" strokeWidth="7" strokeLinecap="round" />
      <path d="M50 18 C70 18 80 44 80 70 C80 98 66 112 50 112 C34 112 20 98 20 70 C20 44 30 18 50 18 Z" fill="#3f8f3f" />
      <path d="M50 30 C64 30 71 50 71 71 C71 94 61 104 50 104 C39 104 29 94 29 71 C29 50 36 30 50 30 Z" fill="#d2ec9c" />
      <circle cx="50" cy="86" r="15" fill="#b07a44" />
      <Face cx={50} cy={52} ink="#5b7a30" mode="sleepy" mouth="small" />
    </g>
  );

  const Nana = () => (
    <g>
      <path d="M34 74 q-12 3 -14 14" fill="none" stroke="#f2c63b" strokeWidth="6.5" strokeLinecap="round" />
      <path d="M74 74 q12 3 14 14" fill="none" stroke="#f2c63b" strokeWidth="6.5" strokeLinecap="round" />
      <path d="M33 34 C27 66 46 90 76 88 C83 87.6 86 81 82 77 C58 80 44 60 47 36 C48 29 35 28 33 34 Z" fill="#ffd23f" />
      <path d="M33 34 C27 66 46 90 76 88" fill="none" stroke="#ecb21f" strokeWidth="2.4" opacity="0.45" />
      <path d="M40 33 q-1 -8 3 -12" fill="none" stroke="#7a5a2e" strokeWidth="4.5" strokeLinecap="round" />
      <path d="M77 86 q5 1 6 5" fill="none" stroke="#7a5a2e" strokeWidth="4.5" strokeLinecap="round" />
      <Face cx={55} cy={70} ink="#9a6a1e" mode="wink" />
    </g>
  );

  const Cara = () => (
    <g>
      <path d="M30 70 q-11 2 -13 13" fill="none" stroke="#f59a3e" strokeWidth="6.5" strokeLinecap="round" />
      <path d="M70 70 q11 2 13 13" fill="none" stroke="#f59a3e" strokeWidth="6.5" strokeLinecap="round" />
      <g fill="#4caf50">
        <path d="M42 46 C38 26 32 20 27 18 C33 27 34 37 39 47 Z" />
        <path d="M50 46 C50 22 50 16 50 12 C53 22 55 36 55 47 Z" />
        <path d="M58 46 C62 26 68 20 73 18 C67 27 66 37 61 47 Z" />
      </g>
      <path d="M32 44 L68 44 L52 108 C51 111 49 111 48 108 Z" fill="#f5872e" />
      <g stroke="#e0701c" strokeWidth="2" opacity="0.55" strokeLinecap="round"><line x1="40" y1="58" x2="46" y2="58" /><line x1="56" y1="66" x2="62" y2="66" /><line x1="44" y1="78" x2="50" y2="78" /></g>
      <Face cx={50} cy={58} ink="#a8531a" mode="open" />
    </g>
  );

  const Toma = () => (
    <g>
      <path d="M30 82 q-12 0 -15 11" fill="none" stroke="#e85a48" strokeWidth="7" strokeLinecap="round" />
      <path d="M70 82 q12 0 15 11" fill="none" stroke="#e85a48" strokeWidth="7" strokeLinecap="round" />
      <circle cx="50" cy="74" r="34" fill="#ec4a3a" />
      <ellipse cx="40" cy="62" rx="9" ry="6" fill="#ff7a68" opacity="0.5" />
      <g fill="#4caf50"><path d="M50 42 L44 30 L50 36 L56 30 Z" /><path d="M50 42 C40 42 34 38 30 34 C40 36 44 38 50 40 Z" /><path d="M50 42 C60 42 66 38 70 34 C60 36 56 38 50 40 Z" /></g>
      <rect x="48" y="26" width="4" height="9" rx="2" fill="#3d8b41" />
      <Face cx={50} cy={74} ink="#9c2f25" cheek="#ffd0c2" mode="open" />
    </g>
  );

  // Lemon — TALL oval, pointed nubs top & bottom, bright yellow
  const Lemon = () => (
    <g>
      <path d="M28 74 q-11 -1 -13 9" fill="none" stroke="#ecc81f" strokeWidth="7" strokeLinecap="round" />
      <path d="M72 74 q11 -1 13 9" fill="none" stroke="#ecc81f" strokeWidth="7" strokeLinecap="round" />
      <path d="M50 21 L43 40 Q50 37 57 40 Z" fill="#eaca18" />
      <path d="M50 111 L43 92 Q50 95 57 92 Z" fill="#eaca18" />
      <ellipse cx="50" cy="66" rx="25" ry="33" fill="#ffd91f" />
      <ellipse cx="40" cy="52" rx="7" ry="11" fill="#fff09a" opacity="0.55" transform="rotate(-18 40 52)" />
      <Face cx={50} cy={64} ink="#96790f" mode="open" />
    </g>
  );

  // Lime — ROUND, smaller, deeper green, glossy highlight + leaf
  const Lime = () => (
    <g>
      <path d="M30 80 q-10 1 -12 11" fill="none" stroke="#3f9b30" strokeWidth="6.5" strokeLinecap="round" />
      <path d="M70 80 q10 1 12 11" fill="none" stroke="#3f9b30" strokeWidth="6.5" strokeLinecap="round" />
      <path d="M22 63 L14 59 Q12 64 15 68 Z" fill="#3a8a2a" />
      <path d="M78 67 L85 64 Q87 68 84 71 Z" fill="#3a8a2a" />
      <ellipse cx="50" cy="66" rx="31" ry="27" fill="#4faf3a" />
      <ellipse cx="39" cy="55" rx="12" ry="7" fill="#9fde78" opacity="0.5" transform="rotate(-22 39 55)" />
      <circle cx="35" cy="52" r="2.8" fill="#ffffff" opacity="0.5" />
      <Face cx={51} cy={68} ink="#2f6b22" mode="wink" />
    </g>
  );

  const Chilli = () => (
    <g>
      <path d="M40 80 q-12 2 -14 13" fill="none" stroke="#e23b2f" strokeWidth="6.5" strokeLinecap="round" />
      <path d="M80 76 q12 3 13 14" fill="none" stroke="#e23b2f" strokeWidth="6.5" strokeLinecap="round" />
      <path d="M50 40 C39 40 35 50 37 61 C39 82 53 99 70 96 C84 93 87 80 82 69 C77 56 69 45 60 41 C56 39 53 40 50 40 Z" fill="#e23b2f" />
      <path d="M46 54 C43 66 49 82 66 86" fill="none" stroke="#ff7d6e" strokeWidth="4" strokeLinecap="round" opacity="0.55" />
      <path d="M50 41 C49 31 53 25 60 23 C58 31 59 37 56 42 Z" fill="#4caf50" />
      <rect x="53" y="20" width="4.5" height="9" rx="2" fill="#3d8b41" transform="rotate(-14 55 24)" />
      <Face cx={58} cy={70} ink="#9c2317" cheek="#ffb3a4" mode="open" />
    </g>
  );

  // Cheese — wedge with darker rind band + holes (reads as cheese, not pizza)
  const Cheese = () => (
    <g>
      <path d="M30 84 q-12 1 -14 12" fill="none" stroke="#e0a52a" strokeWidth="7" strokeLinecap="round" />
      <path d="M72 84 q12 1 14 12" fill="none" stroke="#e0a52a" strokeWidth="7" strokeLinecap="round" />
      <path d="M50 40 Q53 40 55 44 L77 84 Q79 88 74 88 L26 88 Q21 88 23 84 L45 44 Q47 40 50 40 Z" fill="#ffce4a" />
      <path d="M27 80 L73 80 Q79 80 78 86 Q78 88 74 88 L26 88 Q22 88 22 84 Q22 80 27 80 Z" fill="#e6a824" />
      <circle cx="39" cy="74" r="3.8" fill="#eebb34" />
      <circle cx="61" cy="76" r="3.2" fill="#eebb34" />
      <Face cx={50} cy={60} ink="#946a16" mode="sleepy" mouth="small" />
    </g>
  );

  const Cupcake = () => (
    <g>
      <path d="M26 84 q-11 1 -13 12" fill="none" stroke="#d98cae" strokeWidth="7" strokeLinecap="round" />
      <path d="M74 84 q11 1 13 12" fill="none" stroke="#d98cae" strokeWidth="7" strokeLinecap="round" />
      <path d="M30 70 L70 70 L65 104 C64 107 36 107 35 104 Z" fill="#e58cb0" />
      <g stroke="#d2779e" strokeWidth="2.4" opacity="0.7" strokeLinecap="round"><line x1="42" y1="74" x2="40" y2="104" /><line x1="50" y1="74" x2="50" y2="104" /><line x1="58" y1="74" x2="60" y2="104" /></g>
      <g fill="#fbb6ce"><circle cx="37" cy="62" r="13" /><circle cx="63" cy="62" r="13" /><circle cx="50" cy="52" r="15" /></g>
      <ellipse cx="44" cy="50" rx="6" ry="4" fill="#ffd1e0" opacity="0.7" />
      <circle cx="50" cy="36" r="6.5" fill="#e23b3b" />
      <path d="M52 30 q4 -8 10 -9" fill="none" stroke="#3d8b41" strokeWidth="2.4" strokeLinecap="round" />
      <Face cx={50} cy={66} ink="#a85076" cheek="#ff7da6" mode="happy" />
    </g>
  );

  const MAP = { brock: Brock, avo: Avo, nana: Nana, cara: Cara, toma: Toma, lemon: Lemon, lime: Lime, chilli: Chilli, cheese: Cheese, cupcake: Cupcake };
  const TILT = { brock: -4, avo: 4, nana: -5, cara: 4, toma: -2, lemon: 3, lime: -4, chilli: 5, cheese: -3, cupcake: 2 };
  window.COMPANIONS = [
    { id: 'brock', name: 'Brock' },
    { id: 'avo', name: 'Avo' },
    { id: 'cara', name: 'Cara' },
    { id: 'toma', name: 'Toma' },
    { id: 'lemon', name: 'Zest' },
    { id: 'lime', name: 'Limo' },
    { id: 'cupcake', name: 'Muffin' },
  ];

  window.Companion = function ({ id = 'brock', size = 120, style }) {
    const C = MAP[id] || Brock;
    const tilt = TILT[id] || 0;
    return (
      <svg viewBox="0 0 100 120" width={size} height={size * 1.2} style={style} aria-hidden="true">
        <g transform={`rotate(${tilt} 50 66)`}><C /></g>
      </svg>
    );
  };
})();
// companions build 5
