/* ------------------------------------------------------------------
   Little Healthy Plates — hand-drawn recipe illustrations
   Warm line-art motifs in the brand palette. Used in place of photos.
   Each recipe can set `art: "<motif>"`, else one is chosen by category.
------------------------------------------------------------------ */

const ART_INK = "#2a1d12";
const ART_SOFT = "#5a4632";
const ART_TERRA = "#c4633e";
const ART_TERRA_DEEP = "#9b4724";
const ART_SAGE = "#7e8a6a";
const ART_SAGE_DEEP = "#5e6a4a";
const ART_BUTTER = "#ecb858";
const ART_EGGWHITE = "#fffdf6";

/* tint behind each motif (soft, warm) */
const ART_TINT = {
  egg:       "#f3ead4",
  pancakes:  "#f1e3c8",
  jar:       "#eee6d6",
  bowl:      "#e8ecdc",
  soup:      "#f1e6d2",
  plate:     "#eee4cf",
  pot:       "#ece4d4",
  fish:      "#e6ecdf",
  bread:     "#f0e4cc",
  cracker:   "#eee7d2",
  cookie:    "#efe2cc",
  icecream:  "#f1e7da",
  mug:       "#f0e7d6",
  pasta:     "#f1e6cf",
  portrait:  "#eae7d8",
};

const ART_BY_CAT = {
  breakfast: ["egg", "pancakes", "jar"],
  lunch:     ["bowl", "soup"],
  dinner:    ["plate", "pot", "fish"],
  sides:     ["bread"],
  snacks:    ["cracker"],
  desserts:  ["cookie", "icecream"],
};

function artHash(s) {
  let h = 0;
  for (let i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) >>> 0;
  return h;
}
function pickMotif(recipe) {
  if (recipe.art && ART_TINT[recipe.art]) return recipe.art;
  const pool = ART_BY_CAT[recipe.category] || ["plate"];
  return pool[artHash(recipe.id) % pool.length];
}

/* ---- individual motifs, centered around (100, 76) in a 200x150 box ---- */
function Motif({ name }) {
  const s = { fill: "none", stroke: ART_INK, strokeWidth: 3, strokeLinecap: "round", strokeLinejoin: "round" };

  switch (name) {
    case "egg":
      return (
        <g>
          <ellipse cx="100" cy="92" rx="58" ry="13" fill={ART_INK} opacity="0.06" />
          <path d="M70 88 C56 64 88 54 101 64 C114 52 144 60 135 84 C150 98 122 117 100 107 C80 119 56 104 70 88 Z" fill={ART_EGGWHITE} stroke={ART_INK} strokeWidth="3" strokeLinejoin="round" />
          <circle cx="101" cy="84" r="16" fill={ART_BUTTER} stroke={ART_TERRA_DEEP} strokeWidth="2.5" />
          <circle cx="95" cy="78" r="4.5" fill="#fff" opacity="0.65" />
          <path d="M150 44 q7 -9 1 -18" {...s} strokeWidth="2.5" stroke={ART_TERRA} opacity="0.7" />
          <path d="M160 50 q6 -8 1 -15" {...s} strokeWidth="2.5" stroke={ART_TERRA} opacity="0.55" />
        </g>
      );

    case "pancakes":
      return (
        <g>
          <ellipse cx="100" cy="108" rx="56" ry="11" fill={ART_INK} opacity="0.06" />
          {[100, 88, 76].map((cy, i) => (
            <ellipse key={i} cx="100" cy={cy} rx="46" ry="13" fill="#e7c98c" stroke={ART_INK} strokeWidth="3" />
          ))}
          {/* butter pat */}
          <rect x="88" y="58" width="24" height="16" rx="3" fill={ART_BUTTER} stroke={ART_TERRA_DEEP} strokeWidth="2.5" />
          {/* syrup drip */}
          <path d="M70 70 q-4 12 4 16 q6 4 4 12" {...s} strokeWidth="2.5" stroke={ART_TERRA} opacity="0.8" />
        </g>
      );

    case "jar":
      return (
        <g>
          <ellipse cx="100" cy="120" rx="40" ry="9" fill={ART_INK} opacity="0.06" />
          {/* lid */}
          <rect x="72" y="34" width="56" height="12" rx="3" fill={ART_SAGE} stroke={ART_INK} strokeWidth="3" />
          {/* jar body */}
          <path d="M75 46 L75 112 a25 8 0 0 0 50 0 L125 46" fill={ART_EGGWHITE} stroke={ART_INK} strokeWidth="3" />
          {/* layers */}
          <path d="M76 96 a24 7 0 0 0 48 0" fill="none" stroke={ART_TERRA} strokeWidth="2.5" opacity="0.6" />
          <path d="M77 78 a23 6 0 0 0 46 0" fill="none" stroke={ART_SAGE_DEEP} strokeWidth="2.5" opacity="0.6" />
          {/* seeds dots */}
          {[[88, 104], [100, 108], [112, 103], [94, 100], [106, 99]].map(([x, y], i) => (
            <circle key={i} cx={x} cy={y} r="2" fill={ART_INK} opacity="0.5" />
          ))}
          {/* berry on top */}
          <circle cx="100" cy="62" r="8" fill={ART_TERRA} stroke={ART_TERRA_DEEP} strokeWidth="2" />
        </g>
      );

    case "bowl":
      return (
        <g>
          <ellipse cx="100" cy="116" rx="50" ry="9" fill={ART_INK} opacity="0.06" />
          {/* greens poking out */}
          <path d="M78 70 q-6 -20 6 -30 q6 12 2 26" fill={ART_SAGE} stroke={ART_INK} strokeWidth="2.5" strokeLinejoin="round" />
          <path d="M118 68 q8 -22 -4 -32 q-8 12 -2 28" fill={ART_SAGE_DEEP} stroke={ART_INK} strokeWidth="2.5" strokeLinejoin="round" />
          <path d="M100 62 q0 -24 0 -30 q8 12 4 28" fill={ART_SAGE} stroke={ART_INK} strokeWidth="2.5" strokeLinejoin="round" />
          {/* cherry tomatoes */}
          <circle cx="86" cy="78" r="6" fill={ART_TERRA} stroke={ART_TERRA_DEEP} strokeWidth="2" />
          <circle cx="112" cy="80" r="6" fill={ART_TERRA} stroke={ART_TERRA_DEEP} strokeWidth="2" />
          {/* bowl */}
          <path d="M58 76 L142 76 a42 42 0 0 1 -84 0 Z" fill={ART_EGGWHITE} stroke={ART_INK} strokeWidth="3" />
          <path d="M58 76 L142 76" stroke={ART_INK} strokeWidth="3" strokeLinecap="round" />
        </g>
      );

    case "soup":
      return (
        <g>
          <ellipse cx="100" cy="118" rx="52" ry="9" fill={ART_INK} opacity="0.06" />
          {/* steam */}
          <path d="M86 46 q7 -10 0 -20" fill="none" stroke={ART_TERRA} strokeWidth="2.5" strokeLinecap="round" opacity="0.6" />
          <path d="M114 46 q-7 -10 0 -20" fill="none" stroke={ART_TERRA} strokeWidth="2.5" strokeLinecap="round" opacity="0.6" />
          {/* bowl */}
          <path d="M50 72 L150 72 a50 44 0 0 1 -100 0 Z" fill={ART_EGGWHITE} stroke={ART_INK} strokeWidth="3" />
          {/* soup surface */}
          <ellipse cx="100" cy="72" rx="50" ry="9" fill={ART_TERRA} stroke={ART_INK} strokeWidth="3" />
          <ellipse cx="100" cy="72" rx="50" ry="9" fill={ART_BUTTER} opacity="0.35" />
          {/* swirl */}
          <path d="M84 72 q16 -8 32 0" fill="none" stroke={ART_EGGWHITE} strokeWidth="2.5" opacity="0.8" />
        </g>
      );

    case "plate":
      return (
        <g>
          <ellipse cx="100" cy="80" rx="48" ry="46" fill={ART_INK} opacity="0.05" />
          {/* fork */}
          <g stroke={ART_INK} strokeWidth="2.6" strokeLinecap="round" fill="none">
            <line x1="40" y1="44" x2="40" y2="60" />
            <line x1="46" y1="44" x2="46" y2="60" />
            <line x1="52" y1="44" x2="52" y2="60" />
            <path d="M40 60 q6 4 6 -2 M52 60 q-6 4 -6 -2" />
            <line x1="46" y1="60" x2="46" y2="116" />
          </g>
          {/* knife */}
          <g stroke={ART_INK} strokeWidth="2.6" strokeLinecap="round" fill="none">
            <path d="M160 42 q8 6 4 30 q-4 6 -4 0 Z" fill={ART_EGGWHITE} />
            <line x1="160" y1="72" x2="160" y2="116" />
          </g>
          {/* plate */}
          <circle cx="100" cy="80" r="42" fill={ART_EGGWHITE} stroke={ART_INK} strokeWidth="3" />
          <circle cx="100" cy="80" r="30" fill="none" stroke={ART_SOFT} strokeWidth="1.5" opacity="0.5" strokeDasharray="2 4" />
          {/* food: a protein + greens */}
          <ellipse cx="94" cy="80" rx="17" ry="12" fill={ART_TERRA} stroke={ART_TERRA_DEEP} strokeWidth="2.5" transform="rotate(-12 94 80)" />
          <path d="M116 74 q10 -2 14 6 q-9 4 -14 -2 Z" fill={ART_SAGE} stroke={ART_SAGE_DEEP} strokeWidth="2" />
          <circle cx="108" cy="92" r="4" fill={ART_BUTTER} stroke={ART_TERRA_DEEP} strokeWidth="1.5" />
        </g>
      );

    case "pot":
      return (
        <g>
          <ellipse cx="100" cy="118" rx="50" ry="9" fill={ART_INK} opacity="0.06" />
          {/* steam */}
          <path d="M100 40 q8 -10 0 -20" fill="none" stroke={ART_TERRA} strokeWidth="2.5" strokeLinecap="round" opacity="0.6" />
          {/* lid */}
          <path d="M62 58 a38 14 0 0 1 76 0 Z" fill={ART_SAGE} stroke={ART_INK} strokeWidth="3" />
          <circle cx="100" cy="48" r="4" fill={ART_BUTTER} stroke={ART_INK} strokeWidth="2.5" />
          {/* body */}
          <path d="M60 62 L60 96 a40 18 0 0 0 80 0 L140 62" fill={ART_TERRA} stroke={ART_INK} strokeWidth="3" />
          <path d="M60 62 L140 62" stroke={ART_INK} strokeWidth="3" />
          {/* handles */}
          <path d="M60 70 q-12 0 -12 10 q0 6 6 6" fill="none" stroke={ART_INK} strokeWidth="3" />
          <path d="M140 70 q12 0 12 10 q0 6 -6 6" fill="none" stroke={ART_INK} strokeWidth="3" />
        </g>
      );

    case "fish":
      return (
        <g>
          <ellipse cx="100" cy="100" rx="56" ry="10" fill={ART_INK} opacity="0.06" />
          {/* body */}
          <path d="M44 76 Q90 44 132 76 Q90 108 44 76 Z" fill={ART_SAGE} stroke={ART_INK} strokeWidth="3" strokeLinejoin="round" />
          {/* tail */}
          <path d="M132 76 L156 60 L150 76 L156 92 Z" fill={ART_SAGE_DEEP} stroke={ART_INK} strokeWidth="3" strokeLinejoin="round" />
          {/* eye */}
          <circle cx="64" cy="72" r="3.5" fill={ART_INK} />
          {/* gill + scales */}
          <path d="M78 58 q-6 18 0 36" fill="none" stroke={ART_INK} strokeWidth="2.2" opacity="0.7" />
          <path d="M92 70 q8 6 0 12 M104 68 q8 8 0 16 M116 72 q6 4 0 8" fill="none" stroke={ART_INK} strokeWidth="2" opacity="0.6" />
          {/* lemon wedge */}
          <path d="M40 96 a14 14 0 0 1 26 0 Z" fill={ART_BUTTER} stroke={ART_TERRA_DEEP} strokeWidth="2.5" transform="translate(108 -2)" />
        </g>
      );

    case "bread":
      return (
        <g>
          <ellipse cx="100" cy="110" rx="52" ry="9" fill={ART_INK} opacity="0.06" />
          {/* loaf */}
          <path d="M52 96 L52 78 a48 30 0 0 1 96 0 L148 96 Z" fill="#e3b977" stroke={ART_INK} strokeWidth="3" strokeLinejoin="round" />
          <path d="M52 96 L148 96" stroke={ART_INK} strokeWidth="3" />
          {/* scoring */}
          <path d="M74 60 q6 10 0 22 M92 54 q6 12 0 26 M110 56 q6 11 0 24 M126 62 q6 9 0 20" fill="none" stroke={ART_TERRA_DEEP} strokeWidth="2.2" opacity="0.7" />
          {/* wheat sprig */}
          <g stroke={ART_SAGE_DEEP} strokeWidth="2.2" fill="none" strokeLinecap="round">
            <line x1="150" y1="40" x2="138" y2="64" />
            <path d="M146 46 q6 -2 7 -8 M143 52 q6 -2 7 -8 M140 58 q6 -2 7 -8" />
          </g>
        </g>
      );

    case "cracker":
      return (
        <g>
          <ellipse cx="100" cy="112" rx="50" ry="9" fill={ART_INK} opacity="0.06" />
          {/* two crackers, slightly rotated */}
          <g transform="rotate(-8 80 80)">
            <rect x="50" y="56" width="58" height="50" rx="5" fill="#e9cd92" stroke={ART_INK} strokeWidth="3" />
            {[[66, 72], [88, 72], [66, 92], [88, 92], [77, 82]].map(([x, y], i) => (
              <circle key={i} cx={x} cy={y} r="2.4" fill={ART_TERRA_DEEP} opacity="0.55" />
            ))}
          </g>
          <g transform="rotate(7 130 86)">
            <rect x="104" y="64" width="52" height="46" rx="5" fill="#edd49e" stroke={ART_INK} strokeWidth="3" />
            {[[118, 78], [138, 78], [118, 96], [138, 96]].map(([x, y], i) => (
              <circle key={i} cx={x} cy={y} r="2.4" fill={ART_TERRA_DEEP} opacity="0.55" />
            ))}
          </g>
        </g>
      );

    case "cookie":
      return (
        <g>
          <ellipse cx="100" cy="112" rx="44" ry="8" fill={ART_INK} opacity="0.06" />
          <circle cx="100" cy="78" r="40" fill="#d99a5b" stroke={ART_INK} strokeWidth="3" />
          {/* chips */}
          {[[84, 64], [110, 60], [120, 84], [92, 96], [74, 82], [104, 92], [96, 76]].map(([x, y], i) => (
            <path key={i} d={`M${x} ${y} q5 -3 8 2 q2 5 -4 5 q-6 0 -4 -7 Z`} fill={ART_INK} opacity="0.78" />
          ))}
        </g>
      );

    case "icecream":
      return (
        <g>
          <ellipse cx="100" cy="120" rx="40" ry="8" fill={ART_INK} opacity="0.06" />
          {/* coupe glass */}
          <path d="M64 86 a36 30 0 0 0 72 0 Z" fill={ART_EGGWHITE} stroke={ART_INK} strokeWidth="3" />
          <line x1="100" y1="116" x2="100" y2="128" stroke={ART_INK} strokeWidth="3" />
          <ellipse cx="100" cy="130" rx="18" ry="5" fill="none" stroke={ART_INK} strokeWidth="3" />
          {/* scoops */}
          <circle cx="84" cy="78" r="18" fill={ART_TERRA} stroke={ART_INK} strokeWidth="3" />
          <circle cx="114" cy="78" r="18" fill={ART_BUTTER} stroke={ART_INK} strokeWidth="3" />
          <circle cx="99" cy="64" r="16" fill="#e7c2a6" stroke={ART_INK} strokeWidth="3" />
          {/* cherry */}
          <circle cx="99" cy="46" r="6" fill={ART_TERRA_DEEP} stroke={ART_INK} strokeWidth="2" />
          <path d="M99 40 q6 -8 12 -6" fill="none" stroke={ART_SAGE_DEEP} strokeWidth="2.2" strokeLinecap="round" />
        </g>
      );

    case "mug":
      return (
        <g>
          <ellipse cx="100" cy="120" rx="46" ry="9" fill={ART_INK} opacity="0.06" />
          {/* steam */}
          <path d="M88 44 q8 -11 0 -22 M104 44 q-8 -11 0 -22" fill="none" stroke={ART_TERRA} strokeWidth="2.5" strokeLinecap="round" opacity="0.6" />
          {/* mug body */}
          <path d="M62 54 L62 96 a30 18 0 0 0 60 0 L122 54 Z" fill={ART_TERRA} stroke={ART_INK} strokeWidth="3" strokeLinejoin="round" />
          <ellipse cx="92" cy="54" rx="30" ry="9" fill={ART_EGGWHITE} stroke={ART_INK} strokeWidth="3" />
          {/* coffee surface */}
          <ellipse cx="92" cy="54" rx="22" ry="6" fill="#6f4a31" />
          {/* handle */}
          <path d="M122 64 q22 2 22 18 q0 14 -20 16" fill="none" stroke={ART_INK} strokeWidth="3" />
          {/* heart on mug */}
          <path d="M92 76 q-6 -8 -12 -2 q-5 5 12 16 q17 -11 12 -16 q-6 -6 -12 2 Z" fill={ART_BUTTER} stroke={ART_TERRA_DEEP} strokeWidth="2" />
        </g>
      );

    case "pasta":
      return (
        <g>
          <ellipse cx="100" cy="104" rx="56" ry="14" fill={ART_INK} opacity="0.05" />
          {/* steam */}
          <path d="M100 38 q9 -11 0 -22" fill="none" stroke={ART_TERRA} strokeWidth="2.5" strokeLinecap="round" opacity="0.55" />
          {/* plate */}
          <ellipse cx="100" cy="90" rx="60" ry="22" fill={ART_EGGWHITE} stroke={ART_INK} strokeWidth="3" />
          <ellipse cx="100" cy="86" rx="44" ry="15" fill="#edc36f" stroke={ART_INK} strokeWidth="2.5" />
          {/* noodle swirls */}
          <g fill="none" stroke={ART_TERRA_DEEP} strokeWidth="2" opacity="0.6" strokeLinecap="round">
            <path d="M74 84 q12 -8 26 0 t26 0" />
            <path d="M76 90 q12 6 24 0 t24 0" />
          </g>
          {/* meatballs / tomatoes */}
          <circle cx="86" cy="86" r="7" fill={ART_TERRA} stroke={ART_TERRA_DEEP} strokeWidth="2" />
          <circle cx="110" cy="88" r="7" fill={ART_TERRA} stroke={ART_TERRA_DEEP} strokeWidth="2" />
          {/* basil */}
          <path d="M100 78 q6 -6 12 -3 q-4 7 -12 4 Z" fill={ART_SAGE} stroke={ART_SAGE_DEEP} strokeWidth="1.8" />
        </g>
      );

    case "portrait":
      return (
        <g>
          {/* hair */}
          <path d="M58 92 q-6 -56 42 -56 q48 0 42 56 q-8 -30 -42 -30 q-34 0 -42 30 Z" fill={ART_SAGE_DEEP} stroke={ART_INK} strokeWidth="3" strokeLinejoin="round" />
          {/* neck + shoulders */}
          <path d="M84 104 L84 120 q16 8 32 0 L116 104 Z" fill="#e7c2a6" stroke={ART_INK} strokeWidth="3" strokeLinejoin="round" />
          <path d="M58 138 q0 -28 26 -34 q16 10 32 0 q26 6 26 34 Z" fill={ART_TERRA} stroke={ART_INK} strokeWidth="3" strokeLinejoin="round" />
          {/* face */}
          <path d="M70 80 q0 36 30 36 q30 0 30 -36 q0 -30 -30 -30 q-30 0 -30 30 Z" fill="#eccaae" stroke={ART_INK} strokeWidth="3" />
          {/* fringe */}
          <path d="M70 74 q10 -16 30 -16 q20 0 30 16 q-12 -8 -30 -8 q-18 0 -30 8 Z" fill={ART_SAGE_DEEP} stroke={ART_INK} strokeWidth="2.5" strokeLinejoin="round" />
          {/* eyes + smile */}
          <circle cx="87" cy="84" r="2.6" fill={ART_INK} />
          <circle cx="113" cy="84" r="2.6" fill={ART_INK} />
          <path d="M90 98 q10 8 20 0" fill="none" stroke={ART_INK} strokeWidth="2.4" strokeLinecap="round" />
          {/* cheeks */}
          <circle cx="82" cy="93" r="4" fill={ART_TERRA} opacity="0.3" />
          <circle cx="118" cy="93" r="4" fill={ART_TERRA} opacity="0.3" />
        </g>
      );

    default:
      return null;
  }
}

function RecipeArt({ recipe, className, style }) {
  const motif = pickMotif(recipe);
  const tint = ART_TINT[motif] || "#eee4cf";
  return (
    <div
      className={className}
      style={{ position: "relative", overflow: "hidden", background: tint, ...style }}
      aria-hidden="true"
    >
      <svg
        viewBox="0 0 200 150"
        preserveAspectRatio="xMidYMid slice"
        style={{ position: "absolute", inset: 0, width: "100%", height: "100%", display: "block" }}
      >
        <defs>
          <pattern id="lhpDots" width="11" height="11" patternUnits="userSpaceOnUse">
            <circle cx="2" cy="2" r="1" fill={ART_INK} opacity="0.05" />
          </pattern>
        </defs>
        <rect width="200" height="150" fill="url(#lhpDots)" />
        {/* hand-drawn frame ticks in corners */}
        <Motif name={motif} />
      </svg>
      {recipe.category && (
        <span style={{
          position: "absolute", left: 10, bottom: 10,
          fontFamily: "var(--f-hand)", fontSize: 19, lineHeight: 1,
          color: "var(--ink-soft)", opacity: 0.7,
        }}>{recipe.category}</span>
      )}
    </div>
  );
}

/* Standalone scene art — pass an explicit motif, no recipe object needed */
function SceneArt({ motif, className, style }) {
  const tint = ART_TINT[motif] || "#eee4cf";
  return (
    <div
      className={className}
      style={{ position: "relative", overflow: "hidden", background: tint, ...style }}
      aria-hidden="true"
    >
      <svg
        viewBox="0 0 200 150"
        preserveAspectRatio="xMidYMid slice"
        style={{ position: "absolute", inset: 0, width: "100%", height: "100%", display: "block" }}
      >
        <defs>
          <pattern id="lhpDotsScene" width="11" height="11" patternUnits="userSpaceOnUse">
            <circle cx="2" cy="2" r="1" fill={ART_INK} opacity="0.05" />
          </pattern>
        </defs>
        <rect width="200" height="150" fill="url(#lhpDotsScene)" />
        <Motif name={motif} />
      </svg>
    </div>
  );
}

Object.assign(window, { RecipeArt, SceneArt, pickMotif });
