/* Adapted for in-browser Babel mock: IIFE + window export
   (source: repetir_next/src/components/Style.jsx). */
(function () {
// ======================================================================
// STYLE — Stylé, the premium Repetir mascot, unlocked by tiers.
// A prestige writing tool with FOUR finishes that climb in luxury:
//   bronze  → hex wooden pencil, bronze trim + star
//   argent  → silver mechanical pencil (clip, knurled grip, crown)
//   or      → thick gold-trimmed pencil, full crown, gold-dipped tip
//   cristal → holographic crystal-gem stylus (ultimate)
// All four share the same neutral face + arms + pose system.
// ======================================================================
// Usage: <Style tier="or" pose="cheer" size={200} />
// Tiers: bronze | argent | or | cristal
// Poses: hello | write | cheer | sleep | thumbs | sad | levelup | wave
// ======================================================================

const NAVY = ["#3a3d6e", "#23244a", "#14152e"];

const TIERS = {
  bronze:  { met: ["#f3cd9e", "#cf8a45", "#8f561d"], mid: "#cf8a45", deep: "#8f561d", glow: "#d99a5a", glowOp: 0.26, spark: "#f3cd9e", sparks: 1, arm: [78, 122] },
  argent:  { met: ["#f4f7fb", "#c2ccd9", "#8b97a8"], mid: "#aab4c2", deep: "#8b97a8", glow: "#cfd8e6", glowOp: 0.36, spark: "#eef3f9", sparks: 2, arm: [84, 116] },
  or:      { met: ["#fff0b8", "#ffce5a", "#d99812"], mid: "#ffce5a", deep: "#b9810c", glow: "#ffd56b", glowOp: 0.5,  spark: "#ffe9a8", sparks: 3, arm: [76, 124] },
  cristal: { met: ["#d6f4ff", "#7fd0ff", "#4b8fe0"], mid: "#bfe8ff", deep: "#4b8fe0", glow: "#b97fe8", glowOp: 0.5,  spark: "#ffffff", sparks: 3, arm: [82, 118] },
};

const STP = { white: "#fff", eye: "#2d2d2d", arm: "#2a2c52" };

const STCONFIGS = {
  hello:   { tilt: -8,  bounceDur: "2.4s", eye: "open",    mouth: "smile",       leftArm: "wave",    rightArm: "down",    extras: null },
  write:   { tilt: 18,  bounceDur: "1.6s", eye: "focused", mouth: "concentrate", leftArm: "down",    rightArm: "holding", extras: "line" },
  cheer:   { tilt: 0,   bounceDur: "0.6s", eye: "happy",   mouth: "openBig",     leftArm: "up",      rightArm: "up",      extras: "confetti" },
  sleep:   { tilt: 12,  bounceDur: "4s",   eye: "closed",  mouth: "zzz",         leftArm: "down",    rightArm: "down",    extras: "zzz" },
  thumbs:  { tilt: -4,  bounceDur: "2s",   eye: "wink",    mouth: "smirk",       leftArm: "down",    rightArm: "thumbs",  extras: null },
  sad:     { tilt: 6,   bounceDur: "3s",   eye: "sad",     mouth: "frown",       leftArm: "down",    rightArm: "down",    extras: "tear" },
  levelup: { tilt: 0,   bounceDur: "0.9s", eye: "star",    mouth: "openBig",     leftArm: "up",      rightArm: "up",      extras: "stars" },
  wave:    { tilt: -12, bounceDur: "1.6s", eye: "open",    mouth: "smile",       leftArm: "waveBig", rightArm: "down",    extras: null },
};

function StyleEye({ cx, cy, variant, T }) {
  if (variant === "closed" || variant === "zzz" || (variant === "wink" && cx > 100))
    return <path d={`M ${cx - 7} ${cy} Q ${cx} ${cy - 4} ${cx + 7} ${cy}`} stroke={STP.white} strokeWidth="3" strokeLinecap="round" fill="none" />;
  if (variant === "happy")
    return <path d={`M ${cx - 7} ${cy + 2} Q ${cx} ${cy - 5} ${cx + 7} ${cy + 2}`} stroke={STP.white} strokeWidth="3" strokeLinecap="round" fill="none" />;
  if (variant === "star")
    return <path transform={`translate(${cx - 7} ${cy - 7})`} d="M7 0 L9 5 L14 5 L10 8 L12 13 L7 10 L2 13 L4 8 L0 5 L5 5 Z" fill={T.met[0]} stroke={T.deep} strokeWidth="0.5" />;
  if (variant === "sad")
    return <g><ellipse cx={cx} cy={cy + 1} rx="4.6" ry="5.6" fill={STP.white} /><circle cx={cx} cy={cy + 1} r="2.6" fill={STP.eye} /></g>;
  if (variant === "focused")
    return <g><ellipse cx={cx} cy={cy} rx="4.4" ry="5.4" fill={STP.white} /><circle cx={cx} cy={cy} r="2.4" fill={STP.eye} /></g>;
  return <g><ellipse cx={cx} cy={cy} rx="4.8" ry="6" fill={STP.white} /><circle cx={cx} cy={cy} r="3.1" fill={STP.eye} /><circle cx={cx + 1.2} cy={cy - 1.5} r="1.3" fill={STP.white} /></g>;
}

function StyleBrow({ cx, variant, T }) {
  const inner = cx < 100 ? 1 : -1;
  let y = 126, rot = 0;
  if (variant === "happy" || variant === "star") y = 121;
  if (variant === "focused") { y = 129; rot = inner * 13; }
  if (variant === "sad") { y = 123; rot = -inner * 15; }
  return <g transform={`rotate(${rot} ${cx} ${y})`}><path d={`M ${cx - 7} ${y} q 7 -3.5 14 0`} stroke={T.mid} strokeWidth="2.6" fill="none" strokeLinecap="round" /></g>;
}

function StyleMouth({ variant, T }) {
  const cx = 100, cy = 154, col = T.mid;
  if (variant === "smile")       return <path d={`M ${cx - 9} ${cy} Q ${cx} ${cy + 7} ${cx + 9} ${cy}`} stroke={col} strokeWidth="3" fill="none" strokeLinecap="round" />;
  if (variant === "smirk")       return <path d={`M ${cx - 8} ${cy} Q ${cx - 2} ${cy + 5} ${cx + 9} ${cy - 2}`} stroke={col} strokeWidth="3" fill="none" strokeLinecap="round" />;
  if (variant === "concentrate") return <path d={`M ${cx - 6} ${cy + 1} L ${cx + 6} ${cy + 1}`} stroke={col} strokeWidth="3" fill="none" strokeLinecap="round" />;
  if (variant === "openBig")     return <ellipse cx={cx} cy={cy + 2} rx="6.5" ry="7" fill={col} />;
  if (variant === "frown")       return <path d={`M ${cx - 8} ${cy + 4} Q ${cx} ${cy - 3} ${cx + 8} ${cy + 4}`} stroke={col} strokeWidth="3" fill="none" strokeLinecap="round" />;
  if (variant === "zzz")         return <path d={`M ${cx - 5} ${cy} Q ${cx} ${cy + 3} ${cx + 5} ${cy}`} stroke={col} strokeWidth="2.5" fill="none" strokeLinecap="round" />;
  return null;
}

function StyleArm({ side, variant, arm }) {
  const x0 = side === "left" ? arm[0] : arm[1];
  const sign = side === "left" ? -1 : 1;
  const paths = {
    down:    { d: `M ${x0} 150 Q ${x0 + sign * 8} 164, ${x0 + sign * 12} 182`, hand: [x0 + sign * 12, 182] },
    up:      { d: `M ${x0} 150 Q ${x0 + sign * 18} 128, ${x0 + sign * 26} 110`, hand: [x0 + sign * 26, 110] },
    wave:    { d: `M ${x0} 150 Q ${x0 + sign * 18} 132, ${x0 + sign * 27} 120`, hand: [x0 + sign * 27, 120] },
    waveBig: { d: `M ${x0} 150 Q ${x0 + sign * 24} 120, ${x0 + sign * 34} 104`, hand: [x0 + sign * 34, 104] },
    thumbs:  { d: `M ${x0} 150 Q ${x0 + sign * 14} 142, ${x0 + sign * 21} 132`, hand: [x0 + sign * 21, 132], thumb: true },
    holding: { d: `M ${x0} 150 Q ${x0 + sign * 16} 162, ${x0 + sign * 28} 176`, hand: [x0 + sign * 28, 176] },
  };
  const p = paths[variant] ?? paths.down;
  return (
    <g>
      <path d={p.d} stroke={STP.arm} strokeWidth="7.5" fill="none" strokeLinecap="round" />
      <circle cx={p.hand[0]} cy={p.hand[1]} r="7" fill={STP.white} stroke={STP.eye} strokeWidth="1.5" />
      {p.thumb && <path d={`M ${p.hand[0] + sign * 4} ${p.hand[1] - 4} l ${sign * 3} -6`} stroke={STP.eye} strokeWidth="2" strokeLinecap="round" />}
    </g>
  );
}

function StyleExtras({ kind, T }) {
  if (kind === "line") {
    return (
      <g>
        <path d="M 150 246 Q 175 242, 198 248" stroke={T.mid} strokeWidth="3" fill="none" strokeLinecap="round" />
        <path d="M 140 241 L 150 246" stroke={T.mid} strokeWidth="3" strokeLinecap="round" />
      </g>
    );
  }
  if (kind === "confetti") {
    return (
      <g>
        <circle cx="28" cy="34" r="4" fill={T.mid} />
        <circle cx="172" cy="44" r="4" fill="#9aa0ff" />
        <rect x="20" y="104" width="7" height="7" fill={T.mid} transform="rotate(20 23 107)" />
        <rect x="176" y="120" width="7" height="7" fill="#ee5a6f" transform="rotate(-15 179 123)" />
      </g>
    );
  }
  if (kind === "zzz") {
    return (
      <g fontFamily="'Dancing Script', cursive" fontWeight="700" fill={T.mid}>
        <text x="140" y="66" fontSize="22">z</text>
        <text x="155" y="46" fontSize="28">z</text>
        <text x="172" y="24" fontSize="34">Z</text>
      </g>
    );
  }
  if (kind === "tear") return <path d="M 88 148 Q 86 158, 88 164 Q 90 158, 88 148 Z" fill="#7fd0ff" opacity=".85" />;
  if (kind === "stars") {
    return (
      <g fill={T.mid}>
        <path d="M22 70 l3 7 l7 1 l-5 5 l1 7 l-6 -3 l-6 3 l1 -7 l-5 -5 l7 -1 z" />
        <path d="M178 56 l2 5 l5 1 l-4 3 l1 5 l-4 -2 l-4 2 l1 -5 l-4 -3 l5 -1 z" />
        <path d="M182 172 l3 7 l7 1 l-5 5 l1 7 l-6 -3 l-6 3 l1 -7 l-5 -5 l7 -1 z" />
      </g>
    );
  }
  return null;
}

function StyleBody({ tier, navy, met, gem }) {
  if (tier === "bronze") {
    return (
      <g>
        <path d="M100 16 l3 7 l7.6 .6 l-5.8 5 l1.8 7.4 l-6.6 -4 l-6.6 4 l1.8 -7.4 l-5.8 -5 l7.6 -.6 z" fill={met} stroke="#8f561d" strokeWidth="0.8" />
        <rect x="82" y="40" width="36" height="14" rx="6" fill={met} />
        <rect x="76" y="54" width="48" height="14" rx="2" fill={met} />
        <line x1="76" y1="58" x2="124" y2="58" stroke="#8f561d" strokeWidth="1.2" opacity=".5" />
        <line x1="76" y1="64" x2="124" y2="64" stroke="#8f561d" strokeWidth="1.2" opacity=".5" />
        <polygon points="76,68 90,68 88,210 78,210" fill="#2e3160" />
        <polygon points="90,68 110,68 112,210 88,210" fill={navy} />
        <polygon points="110,68 124,68 122,210 112,210" fill="#181a38" />
        <rect x="93" y="74" width="5" height="132" rx="2.5" fill="#6a6ea8" opacity=".5" />
        <polygon points="78,210 122,210 110,238 90,238" fill="#e8c08a" />
        <polygon points="78,210 100,210 100,238 90,238" fill="#f3d2a6" />
        <polygon points="100,210 122,210 110,238 100,238" fill="#d6a868" />
        <polygon points="90,238 110,238 100,256" fill="#2d2d2d" />
        <polygon points="90,238 100,238 100,256" fill="#4a4a4a" />
      </g>
    );
  }
  if (tier === "argent") {
    return (
      <g>
        <path d="M 86 30 L 90 18 L 100 26 L 110 18 L 114 30 Z" fill={met} stroke="#8b97a8" strokeWidth="0.8" />
        <circle cx="90" cy="18" r="1.8" fill="#7fd0ff" /><circle cx="110" cy="18" r="1.8" fill="#7fd0ff" />
        <rect x="92" y="30" width="16" height="12" rx="3" fill={met} />
        <rect x="84" y="42" width="32" height="134" rx="5" fill={navy} />
        <rect x="89" y="46" width="5" height="126" rx="2.5" fill="#6a6ea8" opacity=".5" />
        <path d="M115 46 q9 0 9 10 l0 42 q0 6 -6 6 l0 -8 q3 0 3 -3 l0 -36 q0 -3 -3 -3 z" fill={met} />
        <rect x="84" y="176" width="32" height="30" fill={met} />
        <g stroke="#8b97a8" strokeWidth="1" opacity=".5">
          <line x1="86" y1="180" x2="98" y2="192" /><line x1="92" y1="180" x2="104" y2="192" />
          <line x1="98" y1="180" x2="110" y2="192" /><line x1="104" y1="180" x2="114" y2="192" />
          <line x1="86" y1="192" x2="98" y2="204" /><line x1="92" y1="192" x2="104" y2="204" />
          <line x1="98" y1="192" x2="110" y2="204" /><line x1="104" y1="192" x2="114" y2="204" />
        </g>
        <polygon points="86,206 114,206 106,238 94,238" fill={met} />
        <rect x="97" y="238" width="6" height="9" rx="1" fill="#c2ccd9" />
        <line x1="100" y1="247" x2="100" y2="256" stroke="#2d2d2d" strokeWidth="2.4" strokeLinecap="round" />
      </g>
    );
  }
  if (tier === "cristal") {
    return (
      <g>
        <path d="M 84 40 L 116 40 L 124 54 L 100 82 L 76 54 Z" fill={gem} stroke="#bfe8ff" strokeWidth="1.5" />
        <path d="M 84 40 L 116 40 L 110 54 L 90 54 Z" fill="#eafaff" opacity=".9" />
        <path d="M 76 54 L 90 54 L 100 82 Z" fill="#4b8fe0" opacity=".6" />
        <path d="M 124 54 L 110 54 L 100 82 Z" fill="#3f7fd0" opacity=".7" />
        <line x1="100" y1="40" x2="100" y2="82" stroke="#bfe8ff" strokeWidth="1" opacity=".7" />
        <path d="M 80 84 L 120 84 L 113 210 L 87 210 Z" fill={navy} />
        <path d="M 84 86 L 120 84 L 118 116 L 84 120 Z" fill="#fff" opacity=".18" />
        <path d="M 86 88 L 92 88 L 89 206 L 86 206 Z" fill="#fff" opacity=".4" />
        <path d="M 108 92 L 112 92 L 109 200 L 106 200 Z" fill="#fff" opacity=".22" />
        <path d="M 87 210 L 113 210 L 100 256 Z" fill={navy} />
        <path d="M 87 210 L 100 210 L 100 256 Z" fill="#fff" opacity=".2" />
        <path d="M 100 210 L 113 210 L 100 256 Z" fill="#000" opacity=".12" />
      </g>
    );
  }
  // or
  return (
    <g>
      <path d="M 82 32 L 86 16 L 93 27 L 100 13 L 107 27 L 114 16 L 118 32 Z" fill={met} stroke="#b9810c" strokeWidth="1" />
      <circle cx="100" cy="18" r="2.4" fill="#ee5a6f" /><circle cx="86" cy="16" r="2" fill="#7fd0ff" /><circle cx="114" cy="16" r="2" fill="#7fd0ff" />
      <rect x="74" y="32" width="52" height="12" rx="3" fill={met} />
      <line x1="74" y1="38" x2="126" y2="38" stroke="#b9810c" strokeWidth="1.2" opacity=".5" />
      <polygon points="74,44 92,44 90,210 76,210" fill="#2e3160" />
      <polygon points="92,44 108,44 110,210 90,210" fill={navy} />
      <polygon points="108,44 126,44 124,210 110,210" fill="#181a38" />
      <rect x="94" y="50" width="6" height="120" rx="3" fill="#6a6ea8" opacity=".5" />
      <rect x="74" y="172" width="52" height="9" fill={met} />
      <rect x="74" y="172" width="52" height="2.5" fill="#fff0b8" opacity=".7" />
      <rect x="94" y="181" width="6" height="25" rx="3" fill="#6a6ea8" opacity=".5" />
      <polygon points="76,210 124,210 108,238 92,238" fill={met} />
      <polygon points="76,210 100,210 100,238 92,238" fill="#fff0b8" opacity=".4" />
      <polygon points="92,238 108,238 100,256" fill="#2d2d2d" />
      <polygon points="92,238 100,238 100,256" fill="#4a4a4a" />
    </g>
  );
}

/**
 * @param {object} props
 * @param {"bronze"|"argent"|"or"|"cristal"} [props.tier]
 * @param {"hello"|"write"|"cheer"|"sleep"|"thumbs"|"sad"|"levelup"|"wave"} [props.pose]
 * @param {number} [props.size]
 * @param {React.CSSProperties} [props.style]
 * @param {string} [props.speaking]
 */
function Style({ tier = "or", pose = "hello", size = 200, style, speaking }) {
  const s = size;
  const T = TIERS[tier] ?? TIERS.or;
  const c = STCONFIGS[pose] ?? STCONFIGS.hello;
  const animId = `style-${tier}-${pose}`;

  const sparkPts = [
    "M44 78 l2 6 l6 2 l-6 2 l-2 6 l-2 -6 l-6 -2 l6 -2 z",
    "M164 104 l1.6 5 l5 1.6 l-5 1.6 l-1.6 5 l-1.6 -5 l-5 -1.6 l5 -1.6 z",
    "M156 196 l1.4 4 l4 1.4 l-4 1.4 l-1.4 4 l-1.4 -4 l-4 -1.4 l4 -1.4 z",
  ].slice(0, T.sparks);

  return (
    <div style={{ display: "inline-block", ...style }}>
      <style>{`
        @keyframes ${animId} {
          0%, 100% { transform: translateY(0) rotate(${c.tilt}deg); }
          50%      { transform: translateY(-6px) rotate(${c.tilt}deg); }
        }
        .${animId}-root { animation: ${animId} ${c.bounceDur} ease-in-out infinite; transform-origin: 100px 254px; }
        @keyframes ${animId}-tw { 0%, 100% { opacity: .35; transform: scale(.8); } 50% { opacity: 1; transform: scale(1.15); } }
        .${animId}-tw { animation: ${animId}-tw 2.2s ease-in-out infinite; transform-box: fill-box; transform-origin: center; }
        @media (prefers-reduced-motion: reduce) {
          .${animId}-root { animation: none; transform: rotate(${c.tilt}deg); }
          .${animId}-tw { animation: none; }
        }
      `}</style>
      <svg viewBox="0 0 200 260" width={s} height={s * 260 / 200} overflow="visible">
        <defs>
          <linearGradient id={`${animId}-navy`} x1="0" y1="0" x2="1" y2="0">
            <stop offset="0" stopColor={tier === "cristal" ? "#9aa0ff" : NAVY[0]} />
            <stop offset="0.5" stopColor={tier === "cristal" ? "#b97fe8" : NAVY[1]} />
            <stop offset="1" stopColor={tier === "cristal" ? "#ff9f43" : NAVY[2]} />
          </linearGradient>
          <linearGradient id={`${animId}-met`} x1="0" y1="0" x2="0" y2="1">
            <stop offset="0" stopColor={T.met[0]} />
            <stop offset="0.45" stopColor={T.met[1]} />
            <stop offset="1" stopColor={T.met[2]} />
          </linearGradient>
          <linearGradient id={`${animId}-gem`} x1="0" y1="0" x2="0" y2="1">
            <stop offset="0" stopColor="#d6f4ff" />
            <stop offset="0.5" stopColor="#7fd0ff" />
            <stop offset="1" stopColor="#4b8fe0" />
          </linearGradient>
          <radialGradient id={`${animId}-glow`} cx="0.5" cy="0.45" r="0.55">
            <stop offset="0" stopColor={T.glow} stopOpacity={T.glowOp} />
            <stop offset="1" stopColor={T.glow} stopOpacity="0" />
          </radialGradient>
        </defs>

        <ellipse cx="100" cy="146" rx="92" ry="116" fill={`url(#${animId}-glow)`} />
        <g fill={T.spark}>
          {sparkPts.map((d, i) => <path key={i} className={`${animId}-tw`} d={d} style={{ animationDelay: `${i * 0.5}s` }} />)}
        </g>

        <StyleExtras kind={c.extras} T={T} />
        <g className={`${animId}-root`}>
          <ellipse cx="100" cy="258" rx="30" ry="4.5" fill="#000" opacity=".28" />
          <StyleBody tier={tier} navy={`url(#${animId}-navy)`} met={`url(#${animId}-met)`} gem={`url(#${animId}-gem)`} />
          <g>
            <StyleBrow cx={91} variant={c.eye} T={T} />
            <StyleBrow cx={109} variant={c.eye} T={T} />
            <StyleEye cx={91} cy={140} variant={c.eye} T={T} />
            <StyleEye cx={109} cy={140} variant={c.eye} T={T} />
            <StyleMouth variant={c.mouth} T={T} />
          </g>
          <StyleArm side="left" variant={c.leftArm} arm={T.arm} />
          <StyleArm side="right" variant={c.rightArm} arm={T.arm} />
        </g>

        {speaking && (
          <g transform="translate(130, 8)">
            <rect x="0" y="0" width="130" height="44" rx="16" fill={STP.white} stroke={STP.eye} strokeWidth="2" />
            <path d="M 20 44 L 16 56 L 32 44 Z" fill={STP.white} stroke={STP.eye} strokeWidth="2" />
            <text x="12" y="28" fontFamily="Nunito, sans-serif" fontWeight="800" fontSize="16" fill={STP.eye}>{speaking}</text>
          </g>
        )}
      </svg>
    </div>
  );
}

window.Style = Style;
})();
