/* global React */

// ─── Snowfall (CSS-driven, lightweight) ─────────────────────
const Snowfall = ({ count = 40, seed = 1, speedMin = 6, speedMax = 16 }) => {
  const r = (n) => { const x = Math.sin((n+1) * 9013 + seed*53) * 10000; return x - Math.floor(x); };
  const flakes = ['❄','✦','·','*'];
  return (
    <div className="sc-snow-layer" aria-hidden="true">
      {Array.from({length: count}).map((_, i) => {
        const size = 6 + r(i)*16;
        const dur = speedMin + r(i+1)*(speedMax-speedMin);
        const delay = -r(i+2)*dur;
        const left = r(i+3)*100;
        const op = 0.35 + r(i+4)*0.55;
        const ch = flakes[Math.floor(r(i+5)*flakes.length)];
        return (
          <span key={i} style={{
            left: `${left}%`, fontSize: size, opacity: op,
            animationDuration: `${dur}s`, animationDelay: `${delay}s`,
          }}>{ch}</span>
        );
      })}
    </div>
  );
};

// ─── String lights along the top ────────────────────────────
const StringLights = ({ count = 14, colors }) => {
  const palette = colors || ['#e85267','#e8c266','#5fb37e','#9bd6f0','#e8884a'];
  return (
    <svg width="100%" height="38" viewBox={`0 0 ${count*40} 38`} preserveAspectRatio="none" style={{ display: 'block' }}>
      {/* sagging wire */}
      <path d={`M0 6 ${Array.from({length: count}).map((_,i)=>`Q${i*40+10} ${i%2?20:24} ${i*40+20} ${i%2?16:20} T${i*40+40} 8`).join(' ')}`}
            fill="none" stroke="#3d2c27" strokeWidth="1.5"/>
      {Array.from({length: count}).map((_,i) => {
        const cx = i*40+20;
        const cy = i%2 ? 22 : 18;
        const c = palette[i % palette.length];
        return (
          <g key={i} className="sc-twinkle" style={{ animationDelay: `${(i*0.13).toFixed(2)}s`, color: c, transformOrigin: `${cx}px ${cy}px` }}>
            {/* cap */}
            <rect x={cx-4} y={cy-9} width="8" height="4" rx="1" fill="#3d2c27"/>
            {/* bulb */}
            <ellipse cx={cx} cy={cy} rx="6" ry="8" fill={c} stroke="rgba(0,0,0,0.4)" strokeWidth="0.5"
                     filter={`drop-shadow(0 0 6px ${c})`}/>
            <ellipse cx={cx-1.5} cy={cy-2} rx="1.6" ry="2.4" fill="rgba(255,255,255,0.55)"/>
          </g>
        );
      })}
    </svg>
  );
};

// ─── Ornament ──────────────────────────────────────────────
const Ornament2 = ({ size = 64, color = 'var(--sc-candy)', sway = true, label, glow = true }) => (
  <div style={{ width: size, height: size + 14, position: 'relative', display: 'inline-block' }}>
    <div className={sway ? 'sc-sway' : ''} style={{ position: 'absolute', inset: 0 }}>
      {/* hanger string */}
      <div style={{ position: 'absolute', top: 0, left: '50%', width: 1, height: 12, background: 'rgba(245,235,217,0.55)' }}/>
      {/* cap */}
      <div style={{ position: 'absolute', top: 8, left: '50%', transform: 'translateX(-50%)',
        width: size*0.32, height: 8, background: 'linear-gradient(180deg,#e8c266,#9c7a30)',
        borderRadius: '3px 3px 1px 1px',
      }}/>
      {/* loop */}
      <div style={{ position: 'absolute', top: 4, left: '50%', transform: 'translateX(-50%)',
        width: 8, height: 8, border: '1.5px solid #e8c266', borderRadius: '50%' }}/>
      {/* ball */}
      <div style={{
        position: 'absolute', top: 14, left: 0, width: size, height: size, borderRadius: '50%',
        background: `radial-gradient(circle at 35% 30%, ${color}, #2a0a10 120%)`,
        boxShadow: glow ? `0 0 24px ${color}66, inset 0 -8px 16px rgba(0,0,0,0.4), inset 0 6px 14px rgba(255,255,255,0.18)` : 'inset 0 -8px 16px rgba(0,0,0,0.4)',
      }}>
        {/* highlight */}
        <div style={{ position: 'absolute', top: '18%', left: '22%', width: '24%', height: '20%',
          background: 'rgba(255,255,255,0.45)', borderRadius: '50%', filter: 'blur(2px)' }}/>
        {label && (
          <div style={{ position: 'absolute', inset: 0, display: 'flex', alignItems: 'center', justifyContent: 'center',
            fontFamily: 'var(--sc-display)', fontStyle: 'italic', color: 'var(--sc-foil)',
            fontWeight: 600, fontSize: size*0.36, textShadow: '0 1px 2px rgba(0,0,0,0.6)' }}>
            {label}
          </div>
        )}
      </div>
    </div>
  </div>
);

// ─── Garland — pine sprig with red berries ──────────────────
const Garland = ({ width = 200 }) => (
  <svg width={width} height="20" viewBox="0 0 200 20" preserveAspectRatio="none">
    <line x1="0" y1="10" x2="200" y2="10" stroke="#1f4a33" strokeWidth="2"/>
    {Array.from({length: 16}).map((_,i) => {
      const x = 10 + i*12;
      return (
        <g key={i}>
          <line x1={x} y1="10" x2={x-4} y2={i%2?3:17} stroke="#2f6647" strokeWidth="1.8" strokeLinecap="round"/>
          <line x1={x} y1="10" x2={x+5} y2={i%2?17:3} stroke="#2f6647" strokeWidth="1.8" strokeLinecap="round"/>
          {i%3===0 && <circle cx={x+2} cy={i%2?14:6} r="1.6" fill="#c4243a"/>}
        </g>
      );
    })}
  </svg>
);

// ─── Wax seal ─────────────────────────────────────────────
const WaxSeal = ({ size = 56, letter = 'S' }) => (
  <div className="sc-wax" style={{
    width: size, height: size, borderRadius: '50%',
    display: 'flex', alignItems: 'center', justifyContent: 'center',
    fontFamily: 'var(--sc-display)', fontStyle: 'italic', fontWeight: 600,
    fontSize: size*0.55,
    transform: 'rotate(-6deg)',
    flexShrink: 0,
  }}>
    {letter}
  </div>
);

// ─── Ribbon banner ────────────────────────────────────────
const RibbonBanner = ({ children, color = 'var(--sc-candy)' }) => (
  <div style={{ position: 'relative', display: 'inline-block', filter: 'drop-shadow(0 6px 10px rgba(0,0,0,0.35))' }}>
    <svg viewBox="0 0 220 48" width="220" height="48" style={{ display: 'block' }} preserveAspectRatio="none">
      <path d="M10 8 H210 L200 24 L210 40 H10 L20 24 Z" fill={color}/>
      <path d="M0 14 L10 8 L20 24 L10 40 L0 34 Z" fill="#761722"/>
      <path d="M220 14 L210 8 L200 24 L210 40 L220 34 Z" fill="#761722"/>
    </svg>
    <div style={{ position: 'absolute', inset: 0, display: 'flex', alignItems: 'center', justifyContent: 'center',
      fontFamily: 'var(--sc-display)', fontStyle: 'italic', fontWeight: 600, fontSize: 16, color: '#fbeed1',
      letterSpacing: '0.04em',
    }}>{children}</div>
  </div>
);

// ─── Magic meter (XP bar) ─────────────────────────────────
const MagicMeter = ({ pct = 60, label = 'Magic level' }) => (
  <div style={{ background: 'rgba(28,21,19,0.7)', border: '1px solid var(--sc-line)', borderRadius: 14, padding: '12px 14px' }}>
    <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', marginBottom: 8 }}>
      <span style={{ fontSize: 11, color: 'var(--sc-muted)', textTransform: 'uppercase', letterSpacing: '0.18em', fontWeight: 600 }}>{label}</span>
      <span style={{ fontFamily: 'var(--sc-display)', fontStyle: 'italic', color: 'var(--sc-foil)', fontSize: 18, fontWeight: 600 }}>
        {pct}%
      </span>
    </div>
    <div style={{ height: 12, borderRadius: 999, background: '#0d0807', position: 'relative', overflow: 'hidden', border: '1px solid #1d1411' }}>
      <div style={{
        position: 'absolute', inset: 0, width: `${pct}%`,
        background: 'linear-gradient(90deg,#c4243a 0%, #e85267 30%, #e8c266 70%, #fff2c2 100%)',
        borderRadius: 999,
        boxShadow: '0 0 16px rgba(232,194,102,0.55)',
      }}/>
      {/* sparkle on the leading edge */}
      <div style={{
        position: 'absolute', top: -4, left: `calc(${pct}% - 8px)`, width: 16, height: 20,
        color: '#fff', fontSize: 16, lineHeight: '20px', textAlign: 'center', filter: 'drop-shadow(0 0 6px #ffe28a)',
      }}>✦</div>
    </div>
  </div>
);

// ─── Stocking ─────────────────────────────────────────────
const Stocking = ({ name, color = 'var(--sc-candy)', size = 70 }) => (
  <div style={{ width: size, position: 'relative', display: 'inline-block' }}>
    <svg viewBox="0 0 70 110" width={size} height={size*1.55}>
      {/* hanger nail */}
      <circle cx="20" cy="6" r="2" fill="#9c8978"/>
      {/* loop */}
      <path d="M14 14 Q20 4 26 14" fill="none" stroke="#fbeed1" strokeWidth="1.4"/>
      {/* cuff */}
      <rect x="6" y="14" width="40" height="18" rx="3" fill="#fbeed1"/>
      <rect x="6" y="14" width="40" height="18" rx="3" fill="url(#fluff)" opacity="0.5"/>
      <defs>
        <pattern id="fluff" width="3" height="3" patternUnits="userSpaceOnUse">
          <circle cx="1.5" cy="1.5" r="1" fill="rgba(255,255,255,0.4)"/>
        </pattern>
      </defs>
      {/* boot */}
      <path d="M8 32 H44 V70 Q44 80 56 84 L62 90 Q66 96 60 100 H14 Q6 100 6 92 V32 Z"
            fill={color} stroke="#761722" strokeWidth="1"/>
      {/* embroidery line */}
      <path d="M10 38 H42" stroke="rgba(255,255,255,0.35)" strokeWidth="0.6"/>
    </svg>
    <div style={{
      position: 'absolute', top: 16, left: 0, right: 0, textAlign: 'center',
      fontFamily: 'var(--sc-display)', fontStyle: 'italic', fontWeight: 600, fontSize: 11,
      color: '#761722', letterSpacing: '0.04em', textTransform: 'uppercase',
    }}>{name}</div>
  </div>
);

// ─── Snow globe ───────────────────────────────────────────
const SnowGlobe = ({ size = 240, name = 'EMMA', activity = 'decorating the tree' }) => (
  <div style={{ width: size, position: 'relative', display: 'inline-block' }}>
    {/* globe */}
    <div style={{
      width: size, height: size, borderRadius: '50%', position: 'relative', overflow: 'hidden',
      background: 'radial-gradient(circle at 35% 30%, #2a4358 0%, #0e1c2a 70%, #050a14 100%)',
      border: '3px solid rgba(247,243,236,0.18)',
      boxShadow: '0 0 0 1px rgba(247,243,236,0.08), 0 30px 70px -20px rgba(0,0,0,0.7), inset 0 0 60px rgba(255,255,255,0.05)',
    }}>
      {/* glass highlight */}
      <div style={{ position: 'absolute', top: '8%', left: '15%', width: '40%', height: '30%',
        background: 'radial-gradient(ellipse, rgba(255,255,255,0.35), transparent 70%)', filter: 'blur(8px)' }}/>
      {/* tree */}
      <svg width="100%" height="100%" viewBox="0 0 100 100" style={{ position: 'absolute', inset: 0 }}>
        <polygon points="50,20 38,42 46,42 32,62 44,62 28,82 72,82 56,62 68,62 54,42 62,42" fill="#1f4a33" stroke="#2f6647" strokeWidth="0.5"/>
        <circle cx="50" cy="20" r="2.5" fill="#e8c266"/>
        <circle cx="42" cy="50" r="1.4" fill="#e85267"/>
        <circle cx="58" cy="48" r="1.2" fill="#9bd6f0"/>
        <circle cx="48" cy="65" r="1.3" fill="#e8c266"/>
        <circle cx="56" cy="72" r="1.2" fill="#e85267"/>
        {/* snow dots */}
        {Array.from({length: 12}).map((_,i)=> {
          const r = (n)=>{const x=Math.sin(n*99.13+i*2.7)*1e4;return x-Math.floor(x);};
          return <circle key={i} cx={r(1)*100} cy={r(2)*82} r={0.6+r(3)*1.2} fill="#fbeed1" opacity="0.85"/>;
        })}
        {/* snow ground */}
        <ellipse cx="50" cy="86" rx="50" ry="5" fill="#fbeed1"/>
      </svg>
      {/* name plate floating */}
      <div style={{
        position: 'absolute', top: '38%', left: '50%', transform: 'translate(-50%,-50%)',
        fontFamily: 'var(--sc-display)', fontStyle: 'italic', fontWeight: 600,
        fontSize: size*0.13, color: '#fbeed1', textAlign: 'center',
        textShadow: '0 0 16px rgba(232,194,102,0.7), 0 2px 6px rgba(0,0,0,0.6)',
        letterSpacing: '0.04em',
      }}>
        {name}
        <div style={{ fontFamily: 'var(--sc-handwriting)', fontStyle: 'normal', fontSize: size*0.06,
          marginTop: 4, color: '#e8c266', opacity: 0.9 }}>
          {activity}
        </div>
      </div>
    </div>
    {/* base */}
    <div style={{
      width: size*0.88, height: size*0.18, margin: `-${size*0.08}px auto 0`, position: 'relative', zIndex: 1,
      background: 'linear-gradient(180deg, #4a2f25 0%, #2c1a14 100%)',
      borderRadius: '6px 6px 14px 14px',
      boxShadow: '0 18px 30px -10px rgba(0,0,0,0.6), 0 1px 0 rgba(255,255,255,0.08) inset',
    }}>
      <div style={{
        position: 'absolute', top: '50%', left: '50%', transform: 'translate(-50%,-50%)',
        fontFamily: 'var(--sc-display)', fontStyle: 'italic', color: 'var(--sc-foil)',
        fontSize: size*0.07, fontWeight: 600, letterSpacing: '0.18em', textTransform: 'uppercase',
      }}>Santa · Calls</div>
    </div>
  </div>
);

// ─── Gift tag ─────────────────────────────────────────────
const GiftTag = ({ to, from, color = '#fbeed1' }) => (
  <div style={{
    position: 'relative', width: 140, padding: '14px 14px 12px', borderRadius: 6,
    background: color, color: '#3d2620',
    fontFamily: 'var(--sc-handwriting)',
    boxShadow: '0 6px 14px -4px rgba(0,0,0,0.4)',
    transform: 'rotate(-3deg)',
  }}>
    <div style={{ position: 'absolute', top: 8, left: 8, width: 10, height: 10, borderRadius: '50%', background: '#3d2620' }}/>
    <div style={{ paddingLeft: 22, fontSize: 14 }}><b>To:</b> {to}</div>
    <div style={{ paddingLeft: 22, fontSize: 14 }}><b>From:</b> {from}</div>
  </div>
);

// ─── Jingle bell ──────────────────────────────────────────
const JingleBell = ({ size = 18 }) => (
  <svg width={size} height={size} viewBox="0 0 24 24">
    <path d="M12 4 C8 4 6 7 6 11 V14 L4 17 H20 L18 14 V11 C18 7 16 4 12 4 Z" fill="#e8c266" stroke="#9c7a30" strokeWidth="0.8"/>
    <path d="M10 18 Q12 21 14 18" fill="none" stroke="#9c7a30" strokeWidth="1.2" strokeLinecap="round"/>
    <line x1="12" y1="14" x2="12" y2="17" stroke="#9c7a30" strokeWidth="1"/>
  </svg>
);

Object.assign(window, {
  Snowfall, StringLights, Ornament2, Garland, WaxSeal, RibbonBanner,
  MagicMeter, Stocking, SnowGlobe, GiftTag, JingleBell,
});
