// Shared chrome: phone screen shell, status bar, bottom tab bar.
// All four variants render their content inside <PhoneShell> so the device
// chrome stays consistent and the artboards visually compare apples-to-apples.

const PHONE_W = 393;
const SAFE_TOP = 47;          // status bar
const SAFE_BOTTOM = 34;       // home indicator

function StatusBar({ time = '9:41', dark = false }) {
  const c = dark ? '#fff' : '#000';
  return (
    <div style={{
      height: SAFE_TOP, padding: '0 24px', display: 'flex',
      alignItems: 'center', justifyContent: 'space-between',
      fontFamily: RVU.fontUI, position: 'relative', zIndex: 5,
    }}>
      <span style={{ fontSize: 16, fontWeight: 600, color: c, fontVariantNumeric: 'tabular-nums', paddingTop: 14 }}>{time}</span>
      <div style={{ paddingTop: 14, display: 'flex', alignItems: 'center', gap: 6 }}>
        <svg width="17" height="11" viewBox="0 0 17 11">
          <rect x="0" y="6.5" width="3" height="4.5" rx="0.7" fill={c}/>
          <rect x="4.5" y="4" width="3" height="7" rx="0.7" fill={c}/>
          <rect x="9" y="1.5" width="3" height="9.5" rx="0.7" fill={c}/>
          <rect x="13.5" y="-1" width="3" height="12" rx="0.7" fill={c}/>
        </svg>
        <svg width="15" height="11" viewBox="0 0 15 11">
          <path d="M7.5 2.8C9.6 2.8 11.4 3.6 12.7 5L13.7 4C12.1 2.3 10 1.3 7.5 1.3S2.9 2.3 1.3 4l1 1c1.3-1.4 3.1-2.2 5.2-2.2z" fill={c}/>
          <path d="M7.5 5.9C8.7 5.9 9.8 6.4 10.6 7.2L11.6 6.2C10.5 5.1 9.1 4.4 7.5 4.4S4.5 5.1 3.4 6.2l1 1c.8-.8 1.9-1.3 3.1-1.3z" fill={c}/>
          <circle cx="7.5" cy="9.2" r="1.3" fill={c}/>
        </svg>
        <svg width="24" height="11" viewBox="0 0 24 11">
          <rect x="0.5" y="0.5" width="20" height="10" rx="2.8" stroke={c} strokeOpacity="0.4" fill="none"/>
          <rect x="2" y="2" width="17" height="7" rx="1.6" fill={c}/>
          <path d="M22 3.7v3.6c.7-.3 1.3-1.1 1.3-1.8 0-.7-.6-1.5-1.3-1.8z" fill={c} fillOpacity="0.5"/>
        </svg>
      </div>
    </div>
  );
}

function HomeIndicator({ dark = false }) {
  return (
    <div style={{
      height: SAFE_BOTTOM, display: 'flex', alignItems: 'flex-end',
      justifyContent: 'center', paddingBottom: 8,
      background: dark ? 'transparent' : 'transparent',
    }}>
      <div style={{
        width: 134, height: 5, borderRadius: 100,
        background: dark ? 'rgba(255,255,255,0.7)' : 'rgba(0,0,0,0.3)',
      }}/>
    </div>
  );
}

// Bottom tab bar matching navigation/index.tsx (4 tabs, Tracker active)
function TabBar({ active = 'Tracker', tint = RVU.navy }) {
  const tabs = [
    { id: 'Tracker',     label: 'My Tracker', icon: 'stats' },
    { id: 'CPTLookup',   label: 'CPT Lookup', icon: 'search' },
    { id: 'Analyze',     label: 'Analyze',    icon: 'spark' },
    { id: 'BillingNews', label: 'Billing News', icon: 'news' },
  ];
  return (
    <div style={{
      borderTop: `1px solid ${RVU.border}`,
      background: RVU.surface,
      paddingTop: 8, paddingBottom: 4,
      display: 'flex', alignItems: 'flex-start', justifyContent: 'space-around',
      fontFamily: RVU.fontUI,
    }}>
      {tabs.map(t => {
        const on = t.id === active;
        const c = on ? tint : RVU.textMuted;
        return (
          <div key={t.id} style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 2, paddingTop: 4, flex: 1 }}>
            <TabIcon name={t.icon} color={c} filled={on}/>
            <span style={{ fontSize: 10.5, fontWeight: 500, color: c, letterSpacing: 0.1 }}>{t.label}</span>
          </div>
        );
      })}
    </div>
  );
}

function TabIcon({ name, color, filled }) {
  const s = 24;
  const sw = filled ? 0 : 1.7;
  const fill = filled ? color : 'none';
  if (name === 'stats') return (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none">
      <rect x="3" y="11" width="4" height="9" rx="1" fill={fill} stroke={color} strokeWidth={sw}/>
      <rect x="10" y="6" width="4" height="14" rx="1" fill={fill} stroke={color} strokeWidth={sw}/>
      <rect x="17" y="3" width="4" height="17" rx="1" fill={fill} stroke={color} strokeWidth={sw}/>
    </svg>
  );
  if (name === 'search') return (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none">
      <circle cx="11" cy="11" r="6.5" fill={fill} stroke={color} strokeWidth={sw}/>
      {filled && <circle cx="11" cy="11" r="3.8" fill={RVU.surface}/>}
      <path d="M16 16L21 21" stroke={color} strokeWidth={1.8} strokeLinecap="round"/>
    </svg>
  );
  if (name === 'spark') return (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none">
      <path d="M12 3l1.7 5.3L19 10l-5.3 1.7L12 17l-1.7-5.3L5 10l5.3-1.7L12 3z" fill={fill} stroke={color} strokeWidth={sw} strokeLinejoin="round"/>
      <path d="M19 17l.8 2.5L22 20l-2.2.5L19 23l-.8-2.5L16 20l2.2-.5L19 17z" fill={fill} stroke={color} strokeWidth={sw} strokeLinejoin="round"/>
    </svg>
  );
  if (name === 'news') return (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none">
      <rect x="3" y="4" width="18" height="16" rx="2" fill={fill} stroke={color} strokeWidth={sw}/>
      <line x1="7" y1="9"  x2="17" y2="9"  stroke={filled ? RVU.surface : color} strokeWidth={1.7} strokeLinecap="round"/>
      <line x1="7" y1="13" x2="17" y2="13" stroke={filled ? RVU.surface : color} strokeWidth={1.7} strokeLinecap="round"/>
      <line x1="7" y1="17" x2="13" y2="17" stroke={filled ? RVU.surface : color} strokeWidth={1.7} strokeLinecap="round"/>
    </svg>
  );
  return null;
}

// Phone screen shell — no bezel, just the rounded screen rectangle.
// width fixed at 393; height grows with children.
function PhoneShell({ children, bg = RVU.bg, dark = false, time = '9:41' }) {
  return (
    <div style={{
      width: PHONE_W, background: bg,
      fontFamily: RVU.fontUI, color: RVU.text,
      WebkitFontSmoothing: 'antialiased',
      MozOsxFontSmoothing: 'grayscale',
      display: 'flex', flexDirection: 'column',
      position: 'relative',
    }}>
      <StatusBar time={time} dark={dark}/>
      <div style={{ flex: 1 }}>{children}</div>
      <HomeIndicator dark={dark}/>
    </div>
  );
}

// ─ Reusable Ionicons-style glyphs used across variants ──────────────────────
const Icon = {
  share: ({ c = RVU.navy, s = 20 }) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none">
      <path d="M12 3v12M8 7l4-4 4 4" stroke={c} strokeWidth={1.8} strokeLinecap="round" strokeLinejoin="round"/>
      <path d="M5 12v7a2 2 0 002 2h10a2 2 0 002-2v-7" stroke={c} strokeWidth={1.8} strokeLinecap="round"/>
    </svg>
  ),
  gear: ({ c = RVU.navy, s = 20 }) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none">
      <circle cx="12" cy="12" r="3" stroke={c} strokeWidth={1.8}/>
      <path d="M19.4 15a1.7 1.7 0 00.3 1.8l.1.1a2 2 0 11-2.8 2.8l-.1-.1a1.7 1.7 0 00-1.8-.3 1.7 1.7 0 00-1 1.5V21a2 2 0 11-4 0v-.1a1.7 1.7 0 00-1.1-1.5 1.7 1.7 0 00-1.8.3l-.1.1a2 2 0 11-2.8-2.8l.1-.1a1.7 1.7 0 00.3-1.8 1.7 1.7 0 00-1.5-1H3a2 2 0 110-4h.1a1.7 1.7 0 001.5-1.1 1.7 1.7 0 00-.3-1.8l-.1-.1a2 2 0 112.8-2.8l.1.1a1.7 1.7 0 001.8.3H9a1.7 1.7 0 001-1.5V3a2 2 0 114 0v.1a1.7 1.7 0 001 1.5 1.7 1.7 0 001.8-.3l.1-.1a2 2 0 112.8 2.8l-.1.1a1.7 1.7 0 00-.3 1.8V9a1.7 1.7 0 001.5 1H21a2 2 0 110 4h-.1a1.7 1.7 0 00-1.5 1z"
            stroke={c} strokeWidth={1.6} strokeLinejoin="round"/>
    </svg>
  ),
  chevR: ({ c = RVU.textMuted, s = 16 }) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none">
      <path d="M9 6l6 6-6 6" stroke={c} strokeWidth={2} strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  chevDown: ({ c = RVU.textMuted, s = 14 }) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none">
      <path d="M6 9l6 6 6-6" stroke={c} strokeWidth={2} strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  flag: ({ c = RVU.navy, s = 18 }) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none">
      <path d="M5 21V5a1 1 0 011-1h11l-3 5 3 5H6" stroke={c} strokeWidth={1.8} strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  info: ({ c = RVU.textMuted, s = 16 }) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none">
      <circle cx="12" cy="12" r="9" stroke={c} strokeWidth={1.6}/>
      <path d="M12 10v6M12 7.5v.5" stroke={c} strokeWidth={1.8} strokeLinecap="round"/>
    </svg>
  ),
  arrowUp: ({ c, s = 12 }) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none">
      <path d="M12 19V5M5 12l7-7 7 7" stroke={c} strokeWidth={2.4} strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  arrowDown: ({ c, s = 12 }) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none">
      <path d="M12 5v14M5 12l7 7 7-7" stroke={c} strokeWidth={2.4} strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  dot: ({ c = RVU.teal, s = 8 }) => (
    <svg width={s} height={s} viewBox="0 0 8 8"><circle cx="4" cy="4" r="4" fill={c}/></svg>
  ),
  ellipsis: ({ c = RVU.textMuted, s = 18 }) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none">
      <circle cx="5"  cy="12" r="1.6" fill={c}/>
      <circle cx="12" cy="12" r="1.6" fill={c}/>
      <circle cx="19" cy="12" r="1.6" fill={c}/>
    </svg>
  ),
  plus: ({ c = '#fff', s = 18 }) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none">
      <path d="M12 5v14M5 12h14" stroke={c} strokeWidth={2.4} strokeLinecap="round"/>
    </svg>
  ),
};

// Mini sparkline SVG — used in multiple variants
function Sparkline({ data, w = 80, h = 28, color = RVU.navy, fill = false, dotted = false }) {
  const max = Math.max(...data, 1);
  const pts = data.map((v, i) => {
    const x = (i / (data.length - 1)) * w;
    const y = h - (v / max) * (h - 4) - 2;
    return [x, y];
  });
  const d = pts.map((p, i) => (i === 0 ? `M${p[0]} ${p[1]}` : `L${p[0]} ${p[1]}`)).join(' ');
  const area = `${d} L${w} ${h} L0 ${h} Z`;
  return (
    <svg width={w} height={h} viewBox={`0 0 ${w} ${h}`} style={{ display: 'block' }}>
      {fill && <path d={area} fill={color} fillOpacity="0.12"/>}
      <path d={d} stroke={color} strokeWidth={1.5} fill="none" strokeLinejoin="round" strokeLinecap="round"
            strokeDasharray={dotted ? '2 2' : undefined}/>
      <circle cx={pts[pts.length-1][0]} cy={pts[pts.length-1][1]} r={2.4} fill={color}/>
    </svg>
  );
}

Object.assign(window, { PhoneShell, StatusBar, HomeIndicator, TabBar, TabIcon, Icon, Sparkline, PHONE_W });
