// ============ 玄境 SVG art ============
const { useRef, useEffect, useState } = React;

// cloud logo mark
function CloudMark({ size = 30, color = '#C9322A' }) {
  return (
    <svg width={size} height={size * 0.72} viewBox="0 0 40 29" fill="none">
      <path d="M11 24h17c4 0 7-3 7-6.6 0-3.5-2.8-6.4-6.4-6.5C27.6 6.4 23.7 3 19 3c-4.2 0-7.7 2.8-8.8 6.6C5.9 9.9 2.5 13.4 2.5 17.6 2.5 21.1 5.3 24 11 24Z"
        stroke={color} strokeWidth="2.1" strokeLinejoin="round" fill="none" />
    </svg>
  );
}

// Taiji yin-yang
function YinYang({ size = 150, spin = true }) {
  return (
    <svg width={size} height={size} viewBox="0 0 100 100" className={spin ? 'spin-slow' : ''}>
      <defs><clipPath id="yyc"><circle cx="50" cy="50" r="48" /></clipPath></defs>
      <g clipPath="url(#yyc)">
        <rect width="100" height="100" fill="#F4ECD8" />
        <path d="M50 2a48 48 0 0 1 0 96 24 24 0 0 1 0-48 24 24 0 0 0 0-48z" fill="#141210" />
        <circle cx="50" cy="26" r="7" fill="#141210" />
        <circle cx="50" cy="74" r="7" fill="#F4ECD8" />
      </g>
      <circle cx="50" cy="50" r="48" fill="none" stroke="#141210" strokeWidth="2.4" />
    </svg>
  );
}

const TRIGRAMS = {
  乾:[1,1,1], 兌:[0,1,1], 離:[1,0,1], 震:[0,0,1],
  巽:[1,1,0], 坎:[0,1,0], 艮:[1,0,0], 坤:[0,0,0],
};
// 先天八卦 order clockwise from top
const BAGUA_ORDER = ['乾','兌','離','震','坤','艮','坎','巽'];

function Trigram({ lines, color }) {
  // local coords: bars centered at x=0, stacked, width 34
  return (
    <g>
      {lines.map((yang, i) => {
        const y = i * 9;
        return yang
          ? <rect key={i} x={-17} y={y} width={34} height={5} fill={color} />
          : (<g key={i}>
              <rect x={-17} y={y} width={13} height={5} fill={color} />
              <rect x={4} y={y} width={13} height={5} fill={color} />
            </g>);
      })}
    </g>
  );
}

function Bagua({ size = 360 }) {
  const c = size / 2, R = size * 0.36;
  return (
    <div style={{ position: 'relative', width: size, height: size }}>
      <svg width={size} height={size} viewBox={`0 0 ${size} ${size}`} className="bagua-rotor"
        style={{ position: 'absolute', inset: 0 }}>
        {BAGUA_ORDER.map((name, i) => {
          const ang = (i / 8) * 360;
          const isWhite = i % 2 === 1;
          return (
            <g key={name} transform={`rotate(${ang} ${c} ${c}) translate(${c} ${c - R})`}>
              <g transform="translate(0 -13)">
                <Trigram lines={TRIGRAMS[name]} color={isWhite ? '#F4ECD8' : '#141210'} />
              </g>
            </g>
          );
        })}
      </svg>
      <div style={{
        position: 'absolute', left: '50%', top: '50%', transform: 'translate(-50%,-50%)',
      }}>
        <YinYang size={size * 0.42} />
      </div>
    </div>
  );
}

// 玄簽 fortune tube
function FortuneTube({ shaking = false }) {
  return (
    <svg width="118" height="150" viewBox="0 0 118 150" className={shaking ? 'tube-shake' : ''}>
      {/* sticks */}
      <g stroke="#8E5B2E" strokeWidth="3" strokeLinecap="round">
        <line x1="44" y1="44" x2="40" y2="14" />
        <line x1="59" y1="42" x2="59" y2="8" />
        <line x1="74" y1="44" x2="80" y2="16" />
      </g>
      <g stroke="#9A2117" strokeWidth="3" strokeLinecap="round">
        <line x1="51" y1="43" x2="49" y2="20" />
        <line x1="67" y1="43" x2="71" y2="22" />
      </g>
      {/* tube body */}
      <path d="M30 46 L88 46 L82 142 L36 142 Z" fill="#D8BE86" stroke="#4A3318" strokeWidth="2.4" strokeLinejoin="round" />
      <line x1="32" y1="70" x2="86" y2="70" stroke="#4A3318" strokeWidth="1.6" opacity=".5" />
      <line x1="34" y1="108" x2="84" y2="108" stroke="#4A3318" strokeWidth="1.6" opacity=".5" />
      {/* red label */}
      <rect x="50" y="60" width="18" height="60" rx="2" fill="#9A2117" stroke="#4A3318" strokeWidth="1.4" />
      <text x="59" y="80" textAnchor="middle" fill="#F2E2C0" fontFamily="'Noto Serif SC',serif" fontSize="13" fontWeight="700">玄</text>
      <text x="59" y="100" textAnchor="middle" fill="#F2E2C0" fontFamily="'Noto Serif SC',serif" fontSize="13" fontWeight="700">簽</text>
    </svg>
  );
}

// hand holding flame (master section)
function HandFlame() {
  return (
    <svg width="150" height="270" viewBox="0 0 150 270" fill="none" stroke="#C24A1E" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round">
      <rect x="6" y="6" width="138" height="258" stroke="#C24A1E" strokeWidth="1.6" fill="none" />
      {/* flame */}
      <path d="M75 36c10 14 20 22 20 36 0 13-9 22-20 22s-20-9-20-22c0-9 5-15 9-22 3 8 6 9 8 4 2-4 1-12 3-18z" fill="#E06A2A" stroke="#C24A1E" />
      <circle cx="75" cy="120" r="9" fill="#D8550F" stroke="#C24A1E" />
      {/* hand / mudra */}
      <path d="M44 250c0-26 2-44 8-58 4-9 9-14 16-15" />
      <path d="M106 250c0-24-1-40-6-54-3-9-8-15-15-17" />
      <path d="M58 140c-6-8-13-12-19-9-5 3-4 11 1 17 4 5 10 9 16 11" />
      <path d="M92 140c6-8 13-12 19-9 5 3 4 11-1 17-4 5-10 9-16 11" />
      <path d="M75 128c-7 0-12 6-12 16 0 8 5 14 12 14s12-6 12-14c0-10-5-16-12-16z" />
      <path d="M62 168c-3 8-3 18 0 30M88 168c3 8 3 18 0 30" />
    </svg>
  );
}

// 罗盘 / 奇门 woodblock wheel
function Luopan() {
  const rings = [86, 66, 46];
  const spokes = Array.from({ length: 8 });
  const labels8 = ['乾','坎','艮','震','巽','離','坤','兌'];
  return (
    <svg viewBox="0 0 220 220" width="100%" style={{ display:'block' }}>
      <rect width="220" height="220" fill="#F3ECDD" />
      <g transform="translate(110 110)" stroke="#241B12" fill="none" strokeWidth="1.3">
        {rings.map((r, i) => <circle key={i} r={r} />)}
        <circle r="22" fill="#241B12" />
        {spokes.map((_, i) => {
          const a = (i / 8) * Math.PI * 2;
          return <line key={i} x1={Math.cos(a)*22} y1={Math.sin(a)*22} x2={Math.cos(a)*86} y2={Math.sin(a)*86} />;
        })}
        {labels8.map((t, i) => {
          const a = (i / 8) * Math.PI * 2 - Math.PI/2 + Math.PI/8;
          const r = 76;
          return <text key={i} x={Math.cos(a)*r} y={Math.sin(a)*r + 4} textAnchor="middle"
            fontFamily="'Noto Serif SC',serif" fontSize="11" fill="#241B12" stroke="none">{t}</text>;
        })}
        <text x="0" y="5" textAnchor="middle" fontFamily="'Ma Shan Zheng',serif" fontSize="20" fill="#F3ECDD" stroke="none">陰</text>
      </g>
    </svg>
  );
}

// 五行 five elements cycle
function Wuxing() {
  const els = [
    { c:'木', col:'#4E7B3A' }, { c:'火', col:'#B23B22' }, { c:'土', col:'#A8772A' },
    { c:'金', col:'#9C8A52' }, { c:'水', col:'#2E5670' },
  ];
  const R = 72, cx = 110, cy = 108;
  const pts = els.map((_, i) => {
    const a = (i / 5) * Math.PI * 2 - Math.PI / 2;
    return [cx + Math.cos(a) * R, cy + Math.sin(a) * R];
  });
  return (
    <svg viewBox="0 0 220 220" width="100%" style={{ display:'block' }}>
      <rect width="220" height="220" fill="#F4EFE6" />
      <polygon points={pts.map(p => p.join(',')).join(' ')} fill="none" stroke="#C2B79E" strokeWidth="1.4" />
      {pts.map((p, i) => {
        const q = pts[(i + 2) % 5];
        return <line key={i} x1={p[0]} y1={p[1]} x2={q[0]} y2={q[1]} stroke="#E0D8C4" strokeWidth="1" strokeDasharray="3 3" />;
      })}
      {els.map((e, i) => (
        <g key={i}>
          <circle cx={pts[i][0]} cy={pts[i][1]} r="19" fill={e.col} />
          <text x={pts[i][0]} y={pts[i][1] + 7} textAnchor="middle" fill="#F4EFE6"
            fontFamily="'Noto Serif SC',serif" fontSize="19" fontWeight="700">{e.c}</text>
        </g>
      ))}
      <text x="110" y="115" textAnchor="middle" fill="#7A6E55" fontFamily="'Noto Serif SC',serif" fontSize="13" letterSpacing="2">五行</text>
    </svg>
  );
}

// 紫微 12-palace chart
function Ziwei() {
  const cells = ['命','兄','夫','子','財','疾','遷','友','官','田','福','父'];
  // arrange 12 around a 4x4 grid border
  const pos = [
    [0,0],[1,0],[2,0],[3,0],[3,1],[3,2],[3,3],[2,3],[1,3],[0,3],[0,2],[0,1]
  ];
  return (
    <svg viewBox="0 0 220 220" width="100%" style={{ display:'block' }}>
      <rect width="220" height="220" fill="#F3ECDD" />
      <g stroke="#241B12" strokeWidth="1.2" fill="none">
        <rect x="8" y="8" width="204" height="204" />
        {[1,2,3].map(i => <line key={'v'+i} x1={8 + i*51} y1="8" x2={8 + i*51} y2="212" />)}
        {[1,2,3].map(i => <line key={'h'+i} x1="8" y1={8 + i*51} x2="212" y2={8 + i*51} />)}
        <rect x="59" y="59" width="102" height="102" fill="#241B12" />
      </g>
      {pos.map(([x,y], i) => (
        <text key={i} x={8 + x*51 + 25} y={8 + y*51 + 30} textAnchor="middle"
          fontFamily="'Noto Serif SC',serif" fontSize="15" fill="#241B12">{cells[i]}</text>
      ))}
      <text x="110" y="105" textAnchor="middle" fill="#E2B044" fontFamily="'Ma Shan Zheng',serif" fontSize="22">紫微</text>
      <text x="110" y="130" textAnchor="middle" fill="#C9A24A" fontFamily="'Noto Serif SC',serif" fontSize="12" letterSpacing="3">斗數</text>
    </svg>
  );
}

// arts card line icons
function ArtIcon({ name, size = 30 }) {
  const p = { width: size, height: size, viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', strokeWidth: 1.6, strokeLinecap: 'round', strokeLinejoin: 'round' };
  if (name === 'wind') return <svg {...p}><path d="M3 8h10a2.5 2.5 0 1 0-2.5-2.5" /><path d="M3 12h14a2.5 2.5 0 1 1-2.5 2.5" /><path d="M3 16h8a2.5 2.5 0 1 1-2.5 2.5" /></svg>;
  if (name === 'sun') return <svg {...p}><circle cx="12" cy="12" r="4.5" /><path d="M12 2v2.5M12 19.5V22M2 12h2.5M19.5 12H22M5 5l1.8 1.8M17.2 17.2 19 19M19 5l-1.8 1.8M6.8 17.2 5 19" /></svg>;
  if (name === 'star') return <svg {...p}><path d="M12 3l2.6 5.6 6 .7-4.5 4.1 1.2 6L12 16.9 6.7 19.5l1.2-6L3.4 9.3l6-.7L12 3z" /></svg>;
  return <svg {...p}><circle cx="12" cy="12" r="9.2" /><path d="M15.6 8.4 13.4 13.4 8.4 15.6 10.6 10.6z" fill="currentColor" stroke="none" /></svg>;
}

// atmospheric ink-cloud + dragon mural (hero fallback backdrop)
function CloudSwirl({ x, y, s = 1, c = '#2A1C06', o = 0.5 }) {
  return (
    <g transform={`translate(${x} ${y}) scale(${s})`} opacity={o} fill="none" stroke={c} strokeWidth="3" strokeLinecap="round">
      <path d="M0 0 C 20 -14, 44 -10, 50 8 C 56 26, 40 40, 22 36 C 6 32, 2 18, 14 12 C 24 7, 34 14, 30 24" />
      <path d="M52 4 C 70 -6, 92 2, 92 20 C 92 36, 76 44, 62 38" />
    </g>
  );
}

function DragonMural() {
  return (
    <svg viewBox="0 0 1200 600" width="100%" height="100%" preserveAspectRatio="xMidYMid slice"
      style={{ position: 'absolute', inset: 0 }}>
      {/* faint gold-leaf mottling */}
      <g opacity=".5">
        <CloudSwirl x={90} y={120} s={1.8} o={.32} />
        <CloudSwirl x={1000} y={90} s={2.1} o={.34} />
        <CloudSwirl x={150} y={430} s={1.6} o={.26} />
        <CloudSwirl x={930} y={420} s={1.9} o={.3} />
        <CloudSwirl x={560} y={70} s={1.3} o={.22} />
      </g>
      {/* dragon body */}
      <g stroke="#241803" strokeWidth="2" fill="#2A1C05" opacity=".62">
        <path d="M120 360
          C 240 250, 300 430, 430 360
          C 540 300, 560 200, 660 230
          C 760 260, 800 360, 900 320
          C 980 288, 1010 210, 1080 230
          C 1010 235, 990 300, 905 350
          C 800 405, 760 300, 665 282
          C 575 266, 560 360, 445 412
          C 320 470, 250 320, 150 392 Z" />
        {/* mane / head near center-right */}
        <path d="M1080 230 c 26 -18 54 -14 70 6 c -20 -6 -36 0 -44 16 c 22 -4 36 8 38 26 c -22 -14 -42 -8 -56 8 c 8 -22 -2 -44 -8 -62z" />
        <circle cx="1095" cy="244" r="6" fill="#0E0900" />
      </g>
      {/* scale shimmer dashes along body */}
      <g stroke="#C79328" strokeWidth="1.4" opacity=".4" fill="none" strokeLinecap="round">
        <path d="M180 350 q12 -16 24 0 M230 372 q12 -16 24 0 M300 360 q12 -16 24 0 M380 360 q12 -16 24 0 M470 350 q12 -16 24 0 M560 300 q12 -16 24 0 M650 250 q12 -16 24 0 M740 280 q12 -16 24 0 M840 330 q12 -16 24 0 M930 300 q12 -16 24 0" />
      </g>
      {/* drifting mist bottom */}
      <path d="M0 540 C 200 500, 360 560, 600 520 C 820 486, 1000 556, 1200 516 L1200 600 L0 600Z" fill="#6E4A0C" opacity=".35" />
    </svg>
  );
}
function Mountains() {
  return (
    <svg viewBox="0 0 1180 240" width="100%" height="100%" preserveAspectRatio="xMidYMax slice" style={{ display:'block' }}>
      <defs>
        <linearGradient id="mtA" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0" stopColor="#6A4A12" /><stop offset="1" stopColor="#1B130A" />
        </linearGradient>
        <linearGradient id="mtB" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0" stopColor="#B98A2E" /><stop offset="1" stopColor="#4A340F" />
        </linearGradient>
      </defs>
      <path d="M0 240 L0 150 C120 90 180 170 260 120 C340 70 420 30 500 110 C560 170 640 120 720 150 L720 240Z" fill="url(#mtB)" opacity=".8" />
      <path d="M720 240 L720 160 C820 110 900 160 1000 120 C1080 90 1140 150 1180 130 L1180 240Z" fill="url(#mtB)" opacity=".6" />
      <path d="M0 240 L0 200 C150 150 260 210 380 170 C500 130 600 210 720 180 C860 145 980 205 1180 175 L1180 240Z" fill="url(#mtA)" />
    </svg>
  );
}

// large faint dragon seal watermark
function DragonSeal({ size = 560 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 200 200" fill="none">
      <circle cx="100" cy="100" r="78" stroke="#C24A1E" strokeWidth="3" opacity=".55" />
      <circle cx="100" cy="100" r="66" stroke="#7A3410" strokeWidth="1.5" opacity=".5" />
      {Array.from({ length: 48 }).map((_, i) => {
        const a = (i / 48) * Math.PI * 2;
        return <line key={i} x1={100 + Math.cos(a)*66} y1={100 + Math.sin(a)*66} x2={100 + Math.cos(a)*72} y2={100 + Math.sin(a)*72} stroke="#7A3410" strokeWidth="2" opacity=".4" />;
      })}
      <path d="M100 44c-22 4-38 22-38 46 0 18 12 34 30 38-10-6-16-16-16-28 0-16 12-28 28-30 18-2 32 10 34 26 1 10-3 20-12 26 20-4 34-22 34-44 0-32-30-66-60-60z"
        fill="#7A3410" opacity=".42" />
      <circle cx="118" cy="86" r="4" fill="#C24A1E" opacity=".55" />
      {[[58,58],[150,56],[44,140],[158,138]].map((s,i)=>(
        <rect key={i} x={s[0]} y={s[1]} width="16" height="16" stroke="#7A3410" strokeWidth="1.6" opacity=".4" />
      ))}
    </svg>
  );
}

Object.assign(window, {
  CloudMark, YinYang, Bagua, FortuneTube, HandFlame,
  Luopan, Wuxing, Ziwei, ArtIcon, Mountains, DragonSeal, DragonMural,
});
