// Shared chrome: PhoneShell (status bar + body container), TopBar, BottomNav,
// background-ornament layer per theme, decorative atoms (stars, sparkles).
// All purely visual — every component takes `t` (a theme object).

const PHONE_W = 320;
const PHONE_H = 680;

// ─── Status bar — flat, matches the references (no notch/dynamic island,
// just 9:41 + signal/battery on a transparent strip).
function StatusBar({ t }) {
  const c = t.ink;
  return (
    <div style={{
      display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      padding: '10px 22px 4px', fontSize: 13, fontWeight: 600,
      fontFamily: '-apple-system, "SF Pro", system-ui',
      color: c, letterSpacing: '-0.02em',
    }}>
      <span>9:41</span>
      <span style={{ display: 'flex', gap: 5, alignItems: 'center' }}>
        <svg width="16" height="10" viewBox="0 0 16 10" fill={c}>
          <rect x="0" y="6" width="2.5" height="4" rx="0.5"/>
          <rect x="4" y="4" width="2.5" height="6" rx="0.5"/>
          <rect x="8" y="2" width="2.5" height="8" rx="0.5"/>
          <rect x="12" y="0" width="2.5" height="10" rx="0.5"/>
        </svg>
        <svg width="14" height="10" viewBox="0 0 14 10" fill={c}>
          <path d="M7 1c2 0 3.7.8 5 2l-1 1A5 5 0 0 0 7 2.5 5 5 0 0 0 3 4L2 3a7 7 0 0 1 5-2zm0 3c1.3 0 2.5.5 3.3 1.4l-1 1A3 3 0 0 0 7 5.4a3 3 0 0 0-2.3 1l-1-1A4.5 4.5 0 0 1 7 4zm0 3a1.5 1.5 0 0 1 1 .4l-1 1-1-1A1.5 1.5 0 0 1 7 7z"/>
        </svg>
        <svg width="22" height="10" viewBox="0 0 22 10">
          <rect x="0.5" y="0.5" width="18" height="9" rx="2.5" stroke={c} fill="none" opacity="0.4"/>
          <rect x="2" y="2" width="15" height="6" rx="1.5" fill={c}/>
          <path d="M19.5 3.5v3c.6-.2 1-.7 1-1.5s-.4-1.3-1-1.5z" fill={c} opacity="0.5"/>
        </svg>
      </span>
    </div>
  );
}

// ─── Background ornament layer — per-theme decorations (clouds, moons,
// flowers, prism). Rendered behind the screen content.
function Backdrop({ t }) {
  if (t.bgOrnament === 'clouds') {
    return (
      <svg viewBox="0 0 320 680" width="100%" height="100%" style={{ position: 'absolute', inset: 0, pointerEvents: 'none' }}>
        <defs>
          <radialGradient id="cl1" cx="50%" cy="50%" r="50%">
            <stop offset="0%" stopColor="#f7c8db" stopOpacity="0.55"/>
            <stop offset="100%" stopColor="#f7c8db" stopOpacity="0"/>
          </radialGradient>
          <radialGradient id="cl2" cx="50%" cy="50%" r="50%">
            <stop offset="0%" stopColor="#c9b6e8" stopOpacity="0.55"/>
            <stop offset="100%" stopColor="#c9b6e8" stopOpacity="0"/>
          </radialGradient>
        </defs>
        <ellipse cx="40" cy="620" rx="180" ry="60" fill="url(#cl1)"/>
        <ellipse cx="270" cy="640" rx="160" ry="55" fill="url(#cl2)"/>
        <ellipse cx="280" cy="60" rx="90" ry="35" fill="url(#cl1)" opacity="0.5"/>
        <ellipse cx="20" cy="80" rx="80" ry="30" fill="url(#cl2)" opacity="0.4"/>
        {/* gold arc */}
        <path d="M 30 100 Q 160 30 290 100" stroke={t.gold} strokeWidth="0.6" fill="none" opacity="0.6"/>
        {/* sparkles */}
        {[[40,150,4],[280,180,5],[60,300,3],[290,360,4],[30,500,4],[280,520,3],[150,80,4],[230,250,3],[80,420,3]].map(([x,y,r],i) => (
          <g key={i} transform={`translate(${x} ${y})`} opacity="0.8">
            <path d={`M 0 -${r*2} L ${r*0.4} -${r*0.4} L ${r*2} 0 L ${r*0.4} ${r*0.4} L 0 ${r*2} L -${r*0.4} ${r*0.4} L -${r*2} 0 L -${r*0.4} -${r*0.4} Z`} fill={t.gold}/>
          </g>
        ))}
      </svg>
    );
  }
  if (t.bgOrnament === 'moon') {
    return (
      <svg viewBox="0 0 320 680" width="100%" height="100%" style={{ position: 'absolute', inset: 0, pointerEvents: 'none' }}>
        <defs>
          <radialGradient id="mo1"><stop offset="0%" stopColor="#dcd0f5" stopOpacity="0.6"/><stop offset="100%" stopOpacity="0"/></radialGradient>
        </defs>
        <ellipse cx="160" cy="640" rx="220" ry="80" fill="url(#mo1)"/>
        {/* crescent moon top right */}
        <g transform="translate(260 90)" opacity="0.55">
          <circle r="32" fill="#fff8e4"/>
          <circle r="32" cx="10" cy="-6" fill={t.bg.includes('f9f4ff') ? '#eee6fb' : '#fff'} />
        </g>
        {/* stars */}
        {[[30,60,2],[80,130,3],[290,200,2],[40,280,2.5],[260,320,3],[20,420,2],[280,460,2.5],[60,540,3],[290,580,2]].map(([x,y,r],i) => (
          <circle key={i} cx={x} cy={y} r={r} fill={t.gold} opacity="0.7"/>
        ))}
        {/* large soft sparkle */}
        <g transform="translate(50 90)" opacity="0.6">
          <path d="M 0 -10 L 2 -2 L 10 0 L 2 2 L 0 10 L -2 2 L -10 0 L -2 -2 Z" fill={t.gold}/>
        </g>
      </svg>
    );
  }
  if (t.bgOrnament === 'petals') {
    return (
      <svg viewBox="0 0 320 680" width="100%" height="100%" style={{ position: 'absolute', inset: 0, pointerEvents: 'none' }}>
        {/* drifting petals */}
        {[[30,90,8,-20],[280,140,6,15],[60,230,7,40],[290,310,8,-10],[20,400,6,25],[270,470,7,-30],[40,560,8,10],[290,600,6,35],[150,60,6,0]].map(([x,y,r,rot],i)=> (
          <g key={i} transform={`translate(${x} ${y}) rotate(${rot})`} opacity="0.55">
            <path d={`M 0 -${r} Q ${r*0.6} 0 0 ${r} Q -${r*0.6} 0 0 -${r} Z`} fill="#f0b9c5"/>
          </g>
        ))}
        {/* leaf sprigs at bottom corners */}
        <g transform="translate(-10 640) rotate(-15)" opacity="0.4">
          <path d="M 0 0 Q 30 -20 60 -10 Q 70 5 50 20 Q 25 25 0 0 Z" fill="#c5b48a"/>
        </g>
        <g transform="translate(330 660) rotate(195)" opacity="0.4">
          <path d="M 0 0 Q 30 -20 60 -10 Q 70 5 50 20 Q 25 25 0 0 Z" fill="#c5b48a"/>
        </g>
      </svg>
    );
  }
  // prism — V4: subtle vertical lines + a refracted accent
  return (
    <svg viewBox="0 0 320 680" width="100%" height="100%" style={{ position: 'absolute', inset: 0, pointerEvents: 'none' }}>
      <defs>
        <linearGradient id="px" x1="0" x2="1">
          <stop offset="0" stopColor="#e8b4cb" stopOpacity="0.4"/>
          <stop offset=".5" stopColor="#c9a8c8" stopOpacity="0.3"/>
          <stop offset="1" stopColor="#e8c8a8" stopOpacity="0.4"/>
        </linearGradient>
      </defs>
      <rect x="0" y="640" width="320" height="40" fill="url(#px)"/>
      <line x1="20" y1="150" x2="20" y2="600" stroke={t.gold} strokeWidth="0.4" opacity="0.4"/>
      <line x1="300" y1="150" x2="300" y2="600" stroke={t.gold} strokeWidth="0.4" opacity="0.4"/>
      <g transform="translate(280 70)" opacity="0.5">
        <polygon points="0,-18 16,9 -16,9" fill="none" stroke={t.gold} strokeWidth="0.6"/>
      </g>
    </svg>
  );
}

// ─── Tiny visual atoms reused all over.
function Sparkle({ size = 10, color, style }) {
  return (
    <svg width={size} height={size} viewBox="-12 -12 24 24" style={style}>
      <path d="M 0 -10 L 2 -2 L 10 0 L 2 2 L 0 10 L -2 2 L -10 0 L -2 -2 Z" fill={color}/>
    </svg>
  );
}

function GoldRule({ color, width = '100%', height = 1, style }) {
  return <div style={{ width, height, background: `linear-gradient(90deg, transparent, ${color}, transparent)`, opacity: 0.5, ...style }}/>;
}

// Decorative card border — thin gold inset stroke with corner ornaments.
function Bordered({ t, children, padding = 18, style = {} }) {
  return (
    <div style={{
      background: t.cardBg, borderRadius: t.cardRadius,
      padding,
      boxShadow: `0 2px 14px rgba(180, 130, 160, 0.08), inset 0 0 0 1px ${t.border}`,
      position: 'relative', ...style,
    }}>{children}</div>
  );
}

// ─── Bottom nav — pictogram + label. The single active tab is colored,
// the others are inkMuted. Always 3 tabs: 今日 / これから / マイページ.
function BottomNav({ t, active = '今日を占う', paid = false }) {
  const tabs = paid
    ? ['今日を占う', 'これからを占う', '占い師に相談', 'マイページ']
    : ['今日を占う', 'これからを占う', 'マイページ'];
  const glyph = (label, on) => {
    const c = on ? t.accentDeep : t.inkMuted;
    if (label === '今日を占う')
      return <svg width="22" height="22" viewBox="0 0 24 24" fill="none"><path d="M12 3l2 5h5l-4 3 1.5 5L12 13.5 7.5 16 9 11 5 8h5z" stroke={c} strokeWidth="1.4" fill={on ? c : 'none'} strokeLinejoin="round"/></svg>;
    if (label === 'これからを占う')
      return <svg width="22" height="22" viewBox="0 0 24 24" fill="none"><path d="M19 13a8 8 0 1 1-9-9 6 6 0 0 0 9 9z" stroke={c} strokeWidth="1.4" fill={on ? c : 'none'}/></svg>;
    if (label === '占い師に相談')
      return <svg width="22" height="22" viewBox="0 0 24 24" fill="none"><path d="M21 12c0 4.4-4 8-9 8a10 10 0 0 1-3.5-.6L4 21l1-4A8.5 8.5 0 0 1 3 12c0-4.4 4-8 9-8s9 3.6 9 8z" stroke={c} strokeWidth="1.4" fill="none"/></svg>;
    return <svg width="22" height="22" viewBox="0 0 24 24" fill="none"><circle cx="12" cy="9" r="3.5" stroke={c} strokeWidth="1.4" fill={on ? c : 'none'}/><path d="M5 20a7 7 0 0 1 14 0" stroke={c} strokeWidth="1.4" fill="none"/></svg>;
  };
  return (
    <div style={{
      position: 'absolute', left: 0, right: 0, bottom: 0,
      paddingTop: 8, paddingBottom: 14,
      background: 'linear-gradient(180deg, transparent, rgba(255,253,250,0.85) 30%, rgba(255,253,250,0.95))',
      backdropFilter: 'blur(8px)',
      borderTop: `1px solid ${t.border}`,
      display: 'flex',
    }}>
      {tabs.map(tab => {
        const on = tab === active;
        return (
          <div key={tab} style={{
            flex: 1, display: 'flex', flexDirection: 'column',
            alignItems: 'center', gap: 4,
            color: on ? t.accentDeep : t.inkMuted,
            fontFamily: t.bodyFont, fontSize: 10,
            fontWeight: on ? 600 : 500,
          }}>
            {glyph(tab, on)}
            <span>{tab}</span>
          </div>
        );
      })}
    </div>
  );
}

// ─── Top bar — centered title with optional back / kebab.
function TopBar({ t, title, back = false, action, subtitle }) {
  return (
    <div style={{ padding: '4px 14px 6px', position: 'relative' }}>
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', minHeight: 32 }}>
        <div style={{ width: 28, color: t.ink }}>
          {back && (
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none">
              <path d="M14 6l-6 6 6 6" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"/>
            </svg>
          )}
        </div>
        <div style={{
          fontFamily: t.titleFont, fontWeight: t.titleWeight,
          letterSpacing: t.titleTracking, color: t.ink,
          fontSize: 17, textAlign: 'center',
        }}>{title}</div>
        <div style={{ width: 28, color: t.ink, display: 'flex', justifyContent: 'flex-end' }}>
          {action}
        </div>
      </div>
      {subtitle && (
        <div style={{
          textAlign: 'center', color: t.inkSoft, fontFamily: t.bodyFont,
          fontSize: 10, letterSpacing: '0.05em', marginTop: 2,
        }}>{subtitle}</div>
      )}
    </div>
  );
}

// Section header used on home / menu / mypage.
function SectionHead({ t, label, action }) {
  return (
    <div style={{
      display: 'flex', alignItems: 'baseline', justifyContent: 'space-between',
      marginBottom: 10,
    }}>
      <div style={{
        fontFamily: t.titleFont, fontWeight: t.titleWeight,
        color: t.ink, fontSize: 14, letterSpacing: '0.06em',
        display: 'flex', alignItems: 'center', gap: 6,
      }}>
        {t.ornamentMotif === 'prism' ? <span style={{ width: 14, height: 1, background: t.gold }}/> : null}
        {label}
      </div>
      {action && (
        <div style={{
          fontFamily: t.bodyFont, fontSize: 10, color: t.accent,
          borderBottom: `0.5px solid ${t.accent}`, paddingBottom: 1,
        }}>{action}</div>
      )}
    </div>
  );
}

// Pretty CTA button — large pill with soft gradient.
function CTAButton({ t, children, secondary = false, style = {}, small = false }) {
  if (secondary) return (
    <div style={{
      display: 'inline-flex', alignItems: 'center', justifyContent: 'center', gap: 6,
      padding: small ? '8px 16px' : '12px 22px',
      borderRadius: 999, background: t.surfaceStrong,
      border: `1px solid ${t.border}`, color: t.ink,
      fontFamily: t.bodyFont, fontWeight: 600,
      fontSize: small ? 11 : 13, ...style,
    }}>{children}</div>
  );
  return (
    <div style={{
      display: 'inline-flex', alignItems: 'center', justifyContent: 'center', gap: 6,
      padding: small ? '10px 18px' : '14px 26px',
      borderRadius: 999,
      background: `linear-gradient(135deg, ${t.accent}, ${t.accentDeep})`,
      color: '#fff',
      fontFamily: t.bodyFont, fontWeight: 600,
      fontSize: small ? 12 : 14,
      letterSpacing: '0.04em',
      boxShadow: `0 6px 16px ${t.accent}40`,
      ...style,
    }}>{children}</div>
  );
}

// ─── The phone shell every screen goes into.
function Phone({ t, children, label, nav = '今日を占う', paid = false, hideStatus = false }) {
  return (
    <div style={{
      width: PHONE_W, height: PHONE_H,
      borderRadius: 32, overflow: 'hidden',
      position: 'relative',
      background: t.bg,
      fontFamily: t.bodyFont,
      color: t.ink,
      boxShadow: '0 20px 50px rgba(120, 90, 140, 0.15), 0 0 0 1px rgba(0,0,0,0.05)',
    }}>
      <Backdrop t={t}/>
      <div style={{ position: 'absolute', inset: 0, display: 'flex', flexDirection: 'column' }}>
        {!hideStatus && <StatusBar t={t}/>}
        <div style={{ flex: 1, overflow: 'hidden', position: 'relative' }}>{children}</div>
        {nav && <BottomNav t={t} active={nav} paid={paid}/>}
      </div>
    </div>
  );
}

Object.assign(window, {
  PHONE_W, PHONE_H, Phone, StatusBar, BottomNav, TopBar,
  SectionHead, CTAButton, Bordered, Sparkle, GoldRule, Backdrop,
});
