// PULS3 shared primitives — icons, glass surfaces, coach avatar, chips, bubbles.
// Imported by every screen file. Keep zero deps and stateless.

const PULS3_COLORS = {
  deepTeal: '#1A3E3E',
  deepTeal2: '#0D2626',
  cyan: '#00D4FF',
  lavender: '#A78BDB',
  sleep: '#A78BDB',
  movement: '#FAC257',
  nutrition: '#F28059',
  stress: '#00D4FF',
  mood: '#D97B87',
  sage: '#8CDDB3',
  error: '#E8836B',
  trust: '#7BA8C9',
  menopause: '#9C5CC7',
};

/* ============================================================
   Glow background — the "dim room at 3am" atmosphere
   ============================================================ */
function GlowBG({ opacity = 0.22, tint = 'default' }) {
  // tint variants pick slightly different glow colors per pillar
  const variants = {
    default: { a: '#00D4FF', b: '#A78BDB' },
    sleep:   { a: '#A78BDB', b: '#7B6BBF' },
    food:    { a: '#F28059', b: '#A78BDB' },
    move:    { a: '#FAC257', b: '#00D4FF' },
    mood:    { a: '#D97B87', b: '#A78BDB' },
    trust:   { a: '#7BA8C9', b: '#A78BDB' },
    celebration: { a: '#00D4FF', b: '#8CDDB3' },
  };
  const v = variants[tint] || variants.default;
  return (
    <>
      <div style={{
        position: 'absolute', inset: 0, zIndex: 0, pointerEvents: 'none',
        background: 'radial-gradient(ellipse at top, rgba(26,78,78,0.6), rgba(13,38,38,0.3))',
      }} />
      <div style={{
        position: 'absolute', borderRadius: '50%', filter: 'blur(120px)', opacity,
        background: v.a, width: 320, height: 320, top: -120, left: -80, zIndex: 0,
      }} />
      <div style={{
        position: 'absolute', borderRadius: '50%', filter: 'blur(120px)', opacity,
        background: v.b, width: 280, height: 280, bottom: -80, right: -60, zIndex: 0,
      }} />
      <div style={{
        position: 'absolute', inset: 0, zIndex: 0, pointerEvents: 'none',
        background: 'radial-gradient(ellipse at top, rgba(255,255,255,0.06), transparent 60%, rgba(0,0,0,0.28))',
      }} />
    </>
  );
}

/* ============================================================
   Icons — stroked Feather/Lucide style
   ============================================================ */
const Ic = ({ d, size = 20, stroke = 1.5, fill = 'none' }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill={fill}
    stroke="currentColor" strokeWidth={stroke} strokeLinecap="round" strokeLinejoin="round">
    {d}
  </svg>
);
const IcSleep = (p) => <Ic {...p} d={<path d="M21 12.79A9 9 0 1 1 11.21 3a7 7 0 0 0 9.79 9.79z"/>} />;
const IcMove  = (p) => <Ic {...p} d={<><circle cx="13" cy="4" r="2"/><path d="M9 22l2-8 5 3v7"/><path d="M8 10V7l4-1 4 3 3 1"/></>} />;
const IcFood  = (p) => <Ic {...p} d={<path d="M17 8C8 10 5.9 16.17 3.82 21.34M17 8a4 4 0 0 0-4-4c-4 0-9 3-9 11a4 4 0 0 0 4 4c6 0 9-4 9-11z"/>} />;
const IcHeart = (p) => <Ic {...p} d={<path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z"/>} />;
const IcSpark = (p) => <Ic {...p} d={<path d="M12 2l1.8 5.4L19 9l-5.2 1.6L12 16l-1.8-5.4L5 9l5.2-1.6L12 2z"/>} />;
const IcShield= (p) => <Ic {...p} d={<><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/><polyline points="9 12 11 14 15 10"/></>} />;
const IcSend  = (p) => <Ic {...p} d={<><path d="M22 2 11 13"/><path d="M22 2 15 22l-4-9-9-4 20-7z"/></>} />;
const IcBolt  = (p) => <Ic {...p} d={<polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2"/>} />;
const IcChat  = (p) => <Ic {...p} d={<path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/>} />;
const IcCheck = (p) => <Ic {...p} d={<polyline points="20 6 9 17 4 12"/>} />;
const IcLock  = (p) => <Ic {...p} d={<><rect x="3" y="11" width="18" height="11" rx="2"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/></>} />;
const IcArrow = (p) => <Ic {...p} d={<><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></>} />;
const IcArrowUp = (p) => <Ic {...p} d={<><line x1="12" y1="19" x2="12" y2="5"/><polyline points="5 12 12 5 19 12"/></>} />;
const IcX     = (p) => <Ic {...p} d={<><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></>} />;
const IcMoon  = (p) => <Ic {...p} d={<path d="M21 12.79A9 9 0 1 1 11.21 3a7 7 0 0 0 9.79 9.79z"/>} />;
const IcSun   = (p) => <Ic {...p} d={<><circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M4.93 19.07l1.41-1.41M17.66 6.34l1.41-1.41"/></>} />;
const IcCoffee= (p) => <Ic {...p} d={<><path d="M18 8h1a4 4 0 0 1 0 8h-1"/><path d="M2 8h16v9a4 4 0 0 1-4 4H6a4 4 0 0 1-4-4V8z"/><line x1="6" y1="1" x2="6" y2="4"/><line x1="10" y1="1" x2="10" y2="4"/><line x1="14" y1="1" x2="14" y2="4"/></>} />;
const IcFlame = (p) => <Ic {...p} d={<path d="M8.5 14.5A2.5 2.5 0 0 0 11 12c0-1.38-.5-2-1-3-1.072-2.143-.224-4.054 2-6 .5 2.5 2 4.9 4 6.5 2 1.6 3 3.5 3 5.5a7 7 0 1 1-14 0c0-1.153.433-2.294 1-3a2.5 2.5 0 0 0 2.5 2.5z"/>} />;
const IcActivity = (p) => <Ic {...p} d={<polyline points="22 12 18 12 15 21 9 3 6 12 2 12"/>} />;
const IcCalendar = (p) => <Ic {...p} d={<><rect x="3" y="4" width="18" height="18" rx="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/></>} />;
const IcLeaf = IcFood;
const IcPause = (p) => <Ic {...p} d={<><rect x="6" y="4" width="4" height="16"/><rect x="14" y="4" width="4" height="16"/></>} fill="currentColor" />;
const IcMic = (p) => <Ic {...p} d={<><rect x="9" y="2" width="6" height="12" rx="3"/><path d="M5 10a7 7 0 0 0 14 0M12 19v3"/></>} />;

/* ============================================================
   Brand mark
   ============================================================ */
const BrandDot = ({ size = 10 }) => (
  <span style={{
    width: size, height: size, borderRadius: '50%',
    background: 'linear-gradient(135deg, #00D4FF, #A78BDB)',
    display: 'inline-block',
    boxShadow: '0 0 8px rgba(0,212,255,0.35)',
  }} />
);

const CoachAvatar = ({ size = 36, listening = false }) => (
  <div style={{
    width: size, height: size, borderRadius: '50%',
    background: 'linear-gradient(135deg, #A78BDB, #00D4FF)',
    display: 'flex', alignItems: 'center', justifyContent: 'center',
    color: '#fff', fontWeight: 700, fontSize: size * 0.42,
    boxShadow: listening
      ? '0 0 24px rgba(167,139,219,0.6), 0 0 4px rgba(0,212,255,0.4)'
      : '0 0 16px rgba(167,139,219,0.35)',
    flexShrink: 0, position: 'relative',
    fontFamily: '-apple-system, system-ui',
  }}>
    P
    {listening && (
      <div style={{
        position: 'absolute', inset: -4, borderRadius: '50%',
        border: '1px solid rgba(167,139,219,0.35)',
      }} />
    )}
  </div>
);

// Specialist avatar — colored monogram
const SpecialistAvatar = ({ specialist = 'sleep', size = 36 }) => {
  const map = {
    sleep:     { color: '#A78BDB', initial: 'S', Icon: IcMoon },
    nutrition: { color: '#F28059', initial: 'N', Icon: IcLeaf },
    movement:  { color: '#FAC257', initial: 'M', Icon: IcMove },
    stress:    { color: '#00D4FF', initial: 'R', Icon: IcHeart },
    mood:      { color: '#D97B87', initial: 'O', Icon: IcSpark },
    menopause: { color: '#9C5CC7', initial: 'Mn', Icon: IcSpark },
  };
  const s = map[specialist] || map.sleep;
  return (
    <div style={{
      width: size, height: size, borderRadius: '50%', flexShrink: 0,
      background: `${s.color}25`,
      border: `1px solid ${s.color}55`,
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      color: s.color,
    }}>
      <s.Icon size={size * 0.5} stroke={1.7} />
    </div>
  );
};

/* ============================================================
   Chat & chips
   ============================================================ */
const AssistantBubble = ({ children, subtle = false }) => (
  <div style={{
    alignSelf: 'flex-start', maxWidth: '86%',
    background: subtle ? 'rgba(255,255,255,0.04)' : 'rgba(255,255,255,0.08)',
    border: '1px solid rgba(255,255,255,0.12)',
    backdropFilter: 'blur(16px)', WebkitBackdropFilter: 'blur(16px)',
    color: '#fff', padding: '12px 16px', borderRadius: 24,
    fontSize: 15, lineHeight: 1.5,
    fontFamily: '-apple-system, system-ui',
  }}>{children}</div>
);

const UserBubble = ({ children }) => (
  <div style={{
    alignSelf: 'flex-end', maxWidth: '82%',
    background: 'linear-gradient(135deg, rgba(155,138,212,0.65), rgba(123,107,191,0.50))',
    color: '#fff', padding: '11px 15px', borderRadius: 24,
    fontSize: 15, lineHeight: 1.45,
    fontFamily: '-apple-system, system-ui',
  }}>{children}</div>
);

const ChipPanel = ({ children, tint = 'lavender' }) => {
  const bg = tint === 'lavender'
    ? { bg: 'rgba(155,138,212,0.12)', br: 'rgba(185,166,232,0.26)' }
    : { bg: 'rgba(255,255,255,0.04)', br: 'rgba(255,255,255,0.10)' };
  return (
    <div style={{
      padding: 10, borderRadius: 18,
      background: bg.bg, border: `1px solid ${bg.br}`,
      display: 'flex', flexWrap: 'wrap', gap: 8,
      alignSelf: 'stretch',
    }}>{children}</div>
  );
};

const Chip = ({ children, selected, onClick, size = 'md' }) => (
  <button
    onClick={onClick}
    style={{
      fontFamily: '-apple-system, system-ui', fontWeight: 600,
      fontSize: size === 'sm' ? 12 : 13,
      padding: size === 'sm' ? '6px 12px' : '8px 14px',
      borderRadius: 999, border: 'none', cursor: 'pointer',
      display: 'inline-flex', alignItems: 'center', gap: 5,
      background: selected ? 'rgba(255,255,255,0.94)' : 'rgba(255,255,255,0.12)',
      color: selected ? '#000' : '#fff',
      transition: 'background 0.18s ease, transform 0.15s ease',
    }}
  >
    {selected && (
      <svg width="11" height="11" viewBox="0 0 24 24" fill="none"
        stroke="currentColor" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round">
        <polyline points="20 6 9 17 4 12"/>
      </svg>
    )}
    {children}
  </button>
);

/* ============================================================
   Cards & surfaces
   ============================================================ */
const GlassCard = ({ children, style = {}, wash = false, glowColor = null }) => (
  <div style={{
    background: 'rgba(255,255,255,0.06)',
    border: '1px solid rgba(255,255,255,0.12)',
    borderRadius: 24, padding: 18,
    backdropFilter: 'blur(16px)', WebkitBackdropFilter: 'blur(16px)',
    boxShadow: '0 8px 32px rgba(0,0,0,0.30)',
    position: 'relative', overflow: 'hidden',
    ...style,
  }}>
    {wash && (
      <div style={{
        position: 'absolute', inset: 0, borderRadius: 'inherit', pointerEvents: 'none',
        background: 'linear-gradient(135deg, rgba(167,139,219,0.06), rgba(0,212,255,0.04))',
      }} />
    )}
    {glowColor && (
      <div style={{
        position: 'absolute', top: -40, right: -40,
        width: 160, height: 160, borderRadius: '50%',
        background: glowColor, filter: 'blur(60px)', opacity: 0.25,
        pointerEvents: 'none',
      }} />
    )}
    <div style={{ position: 'relative' }}>{children}</div>
  </div>
);

/* ============================================================
   Tab bar
   ============================================================ */
const TabBar = ({ active = 'Today' }) => {
  const items = [
    { name: 'Today', Icon: IcSpark },
    { name: 'Chat',  Icon: IcChat },
    { name: 'Trends',Icon: IcActivity },
    { name: 'You',   Icon: IcShield },
  ];
  return (
    <div style={{
      position: 'absolute', left: 12, right: 12, bottom: 22, zIndex: 5,
      height: 62, borderRadius: 22,
      background: 'rgba(13,38,38,0.72)',
      border: '1px solid rgba(255,255,255,0.10)',
      backdropFilter: 'blur(24px)', WebkitBackdropFilter: 'blur(24px)',
      display: 'flex', alignItems: 'center', justifyContent: 'space-around',
      padding: '0 8px',
      boxShadow: '0 8px 32px rgba(0,0,0,0.3)',
    }}>
      {items.map(it => {
        const isActive = it.name === active;
        return (
          <div key={it.name} style={{
            display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 3,
            color: isActive ? '#fff' : 'rgba(255,255,255,0.48)',
            fontFamily: '-apple-system, system-ui',
          }}>
            <it.Icon size={20} stroke={isActive ? 2 : 1.5} />
            <div style={{ fontSize: 10.5, fontWeight: 600 }}>{it.name}</div>
          </div>
        );
      })}
    </div>
  );
};

/* ============================================================
   Common screen scaffold
   ============================================================ */
const ScreenFrame = ({ children, tint = 'default' }) => (
  <div style={{
    height: '100%', position: 'relative', overflow: 'hidden',
    background: '#1A3E3E', color: '#fff',
    fontFamily: '-apple-system, "SF Pro", system-ui',
  }}>
    <GlowBG opacity={0.2} tint={tint} />
    {children}
  </div>
);

/* ============================================================
   Eyebrow / section labels
   ============================================================ */
const Eyebrow = ({ children, color = 'rgba(255,255,255,0.5)' }) => (
  <div style={{
    fontSize: 11, letterSpacing: '0.14em', textTransform: 'uppercase',
    color, fontWeight: 500, marginBottom: 8,
    fontFamily: '-apple-system, system-ui',
  }}>{children}</div>
);

/* ============================================================
   Primary CTA — gradient
   ============================================================ */
const PrimaryCTA = ({ children, onClick, disabled, Icon }) => (
  <button
    onClick={onClick}
    disabled={disabled}
    style={{
      width: '100%', padding: '14px 18px', borderRadius: 16,
      background: disabled
        ? 'rgba(255,255,255,0.08)'
        : 'linear-gradient(135deg, #00D4FF, #A78BDB)',
      color: disabled ? 'rgba(255,255,255,0.4)' : '#031019',
      border: 'none', fontSize: 16, fontWeight: 700,
      fontFamily: '-apple-system, system-ui',
      display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8,
      cursor: disabled ? 'default' : 'pointer',
      transition: 'transform 0.15s ease, filter 0.2s ease',
      boxShadow: disabled ? 'none' : '0 4px 16px rgba(0,212,255,0.2)',
    }}
    onMouseDown={e => !disabled && (e.currentTarget.style.transform = 'scale(0.985)')}
    onMouseUp={e => (e.currentTarget.style.transform = '')}
    onMouseLeave={e => (e.currentTarget.style.transform = '')}
  >
    {children}
    {Icon && <Icon size={17} stroke={2.2} />}
  </button>
);

const GhostCTA = ({ children, onClick }) => (
  <button
    onClick={onClick}
    style={{
      width: '100%', padding: '13px 18px', borderRadius: 16,
      background: 'transparent', color: 'rgba(255,255,255,0.7)',
      border: '1px solid rgba(255,255,255,0.12)', fontSize: 15, fontWeight: 600,
      fontFamily: '-apple-system, system-ui', cursor: 'pointer',
    }}
  >
    {children}
  </button>
);

/* ============================================================
   Status bar spacer (above content inside frame)
   ============================================================ */
const StatusBarSpacer = () => <div style={{ height: 62 }} />;

Object.assign(window, {
  PULS3_COLORS,
  GlowBG, Ic, IcSleep, IcMove, IcFood, IcHeart, IcSpark, IcShield, IcSend, IcBolt,
  IcChat, IcCheck, IcLock, IcArrow, IcArrowUp, IcX, IcMoon, IcSun, IcCoffee, IcFlame,
  IcActivity, IcCalendar, IcLeaf, IcPause, IcMic,
  BrandDot, CoachAvatar, SpecialistAvatar,
  AssistantBubble, UserBubble, ChipPanel, Chip,
  GlassCard, TabBar, ScreenFrame, Eyebrow,
  PrimaryCTA, GhostCTA, StatusBarSpacer,
});
