// Sidebar — exact layout copy from new site's chat-sidebar.tsx
function Sidebar({ open, chats, activeId, onSelectChat, onNewChat, onOpenSettings, onOpenSearch, onToggle, tier, onUpgrade, user, onDeleteChat, onPinChat, onSignOut }) {
  const S = window.SOL;
  const [search, setSearch] = React.useState('');
  const [acctOpen, setAcctOpen] = React.useState(false);
  const [emailRevealed, setEmailRevealed] = React.useState(false);

  // Close account dropdown on outside click
  React.useEffect(() => {
    if (!acctOpen) return;
    const h = (e) => { if (!e.target.closest('[data-acct]')) setAcctOpen(false); };
    setTimeout(() => document.addEventListener('click', h), 0);
    return () => document.removeEventListener('click', h);
  }, [acctOpen]);

  // Group chats by date — matches new site GROUP_ORDER
  const GROUP_ORDER = ['Today', 'Yesterday', 'This Week', 'This Month', 'Older'];
  const now = new Date();
  const groups = {};
  GROUP_ORDER.forEach(g => groups[g] = []);

  const filtered = chats.filter(c => !search || c.title.toLowerCase().includes(search.toLowerCase()));
  filtered.forEach(c => {
    const d = new Date(c.updated_at || 0);
    const diff = Math.floor((now - d) / 86400000);
    if (diff === 0) groups['Today'].push(c);
    else if (diff === 1) groups['Yesterday'].push(c);
    else if (diff < 7) groups['This Week'].push(c);
    else if (diff < 30) groups['This Month'].push(c);
    else groups['Older'].push(c);
  });
  // Fallback: no date info → put everything in "Recent"
  const hasDate = chats.some(c => c.updated_at);
  const renderGroups = hasDate
    ? GROUP_ORDER.filter(g => groups[g].length > 0).map(g => [g, groups[g]])
    : [['Recent', filtered]];

  const displayName = user?.display_name || null;
  const email = user?.email || '';

  return (
    <aside style={{
      width: open ? 288 : 0,
      flexShrink: 0,
      background: S.cream2,
      borderRight: `1px solid ${S.line}`,
      transition: 'width 260ms cubic-bezier(0.2,0.8,0.2,1)',
      overflow: 'hidden',
      display: 'flex',
      flexDirection: 'column',
    }}>
      {/* Fixed-width inner so content doesn't squish during animation */}
      <div style={{ width: 288, height: '100%', display: 'flex', flexDirection: 'column', padding: '12px 8px 12px', boxSizing: 'border-box', minHeight: 0 }}>

        {/* ── Top: logo + "Solunce" (keep) + collapse button (keep) ── */}
        <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 12, padding: '8px 6px' }}>
          <a href="/" style={{ display: 'flex', flex: 1, minWidth: 0, alignItems: 'center', gap: 8, borderRadius: 8, padding: '4px 0', textDecoration: 'none' }}>
            <window.SolunceLogo size={28} style={{ flexShrink: 0 }}/>
            <span style={{ fontFamily: S.sans, fontSize: 15, fontWeight: 500, color: S.ink, letterSpacing: 0.2, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>Solunce</span>
          </a>
          <button onClick={onToggle} title="Collapse sidebar" style={{
            width: 36, height: 36, minHeight: 36, borderRadius: 10, border: 'none', background: 'transparent',
            display: 'flex', alignItems: 'center', justifyContent: 'center', cursor: 'pointer', flexShrink: 0,
          }}>
            <window.Icon name="sidebar" size={18} color={S.ink2}/>
          </button>
        </div>

        {/* ── New chat button ── */}
        <button
          onClick={onNewChat}
          title="New chat"
          style={{
            flexShrink: 0, width: '100%', display: 'flex', alignItems: 'center', justifyContent: 'flex-start', gap: 8,
            height: 40, padding: '0 12px', borderRadius: 12, cursor: 'pointer',
            border: `1px solid ${S.line2}`, background: S.paper,
            fontFamily: S.sans, fontSize: 13.5, fontWeight: 500, color: S.ink,
            boxShadow: '0 1px 3px rgba(0,0,0,0.04)', marginBottom: 8,
            transition: 'border-color 0.2s, box-shadow 0.2s',
          }}
          onMouseEnter={e => {
            e.currentTarget.style.borderColor = 'rgba(232,155,60,0.42)';
            e.currentTarget.style.boxShadow = '0 2px 12px -4px rgba(232,155,60,0.35)';
          }}
          onMouseLeave={e => {
            e.currentTarget.style.borderColor = S.line2;
            e.currentTarget.style.boxShadow = '0 1px 3px rgba(0,0,0,0.04)';
          }}
        >
          <window.Icon name="plus" size={16} color={S.ink} strokeWidth={2}/>
          <span>New chat</span>
        </button>

        {/* ── Search input ── */}
        <div style={{ position: 'relative', padding: '0 4px', marginBottom: 12, flexShrink: 0 }}>
          <div style={{ position: 'absolute', left: 12, top: '50%', transform: 'translateY(-50%)', pointerEvents: 'none', zIndex: 1 }}>
            <window.Icon name="search" size={16} color={S.ink3}/>
          </div>
          <input
            type="text"
            placeholder="Search chats"
            value={search}
            onChange={e => setSearch(e.target.value)}
            style={{
              width: '100%', boxSizing: 'border-box', height: 40,
              paddingLeft: 36, paddingRight: 12, borderRadius: 12,
              border: `1px solid ${S.line2}`, background: S.paper,
              fontFamily: S.sans, fontSize: 13, color: S.ink, outline: 'none',
              boxShadow: '0 1px 3px rgba(0,0,0,0.04)',
            }}
          />
        </div>

        {/* ── Chat list ── */}
        <div style={{ flex: 1, minHeight: 0, overflowY: 'auto', overflowX: 'hidden', padding: '0 4px' }}>
          {filtered.length === 0 ? (
            <div style={{ fontFamily: S.sans, fontSize: 13, color: S.ink3, textAlign: 'center', padding: '24px 8px' }}>
              {search ? 'No chats found' : 'No conversations yet'}
            </div>
          ) : (
            renderGroups.map(([group, items]) => (
              <div key={group} style={{ marginBottom: 16 }}>
                {/* Group label — text-[11px] font-semibold uppercase tracking-[0.12em] text-muted-foreground */}
                <div style={{
                  padding: '4px 8px 6px', fontFamily: S.mono, fontSize: 11,
                  fontWeight: 600, textTransform: 'uppercase', letterSpacing: '0.12em', color: S.ink3,
                }}>{group}</div>
                {items.map(c => (
                  <ConvRow key={c.id} chat={c} active={c.id === activeId}
                    onOpen={() => onSelectChat(c.id)}
                    onDelete={() => onDeleteChat && onDeleteChat(c.id)}/>
                ))}
              </div>
            ))
          )}
        </div>

        {/* ── Footer ── */}
        <div style={{ marginTop: 8, paddingTop: 12, borderTop: `1px solid ${S.line}`, flexShrink: 0 }}>

          {/* User row → dropdown */}
          <div style={{ position: 'relative' }} data-acct>
            <button
              data-acct
              onClick={() => setAcctOpen(v => !v)}
              style={{
                width: '100%', display: 'flex', alignItems: 'center', justifyContent: 'flex-start', gap: 8,
                height: 'auto', padding: '8px 10px', borderRadius: 12, border: 'none',
                background: 'transparent', cursor: 'pointer', fontFamily: S.sans,
              }}
              onMouseEnter={e => e.currentTarget.style.background = S.paper}
              onMouseLeave={e => e.currentTarget.style.background = 'transparent'}
            >
              {/* Avatar circle with initial — bg-primary */}
              <div style={{
                width: 32, height: 32, borderRadius: '50%', flexShrink: 0,
                background: `radial-gradient(circle at 30% 30%, #F8D596, ${S.amber})`,
                display: 'flex', alignItems: 'center', justifyContent: 'center',
                fontFamily: S.sans, fontSize: 13, color: '#1C1A17', fontWeight: 700,
              }}>{(displayName || email || 'U')[0].toUpperCase()}</div>

              {/* Name + email with blur reveal */}
              <div style={{ flex: 1, minWidth: 0, display: 'flex', flexDirection: 'column', alignItems: 'flex-start' }}>
                {displayName ? (
                  <>
                    <span style={{ fontFamily: S.sans, fontSize: 13, fontWeight: 500, color: S.ink, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', maxWidth: '100%' }}>{displayName}</span>
                    <span style={{ fontFamily: S.sans, fontSize: 11, color: S.ink3, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', maxWidth: '100%', filter: emailRevealed ? 'none' : 'blur(5px)', userSelect: emailRevealed ? 'auto' : 'none', transition: 'filter 0.2s' }}>{email || '—'}</span>
                  </>
                ) : (
                  <div style={{ display: 'flex', alignItems: 'center', gap: 4, width: '100%', minWidth: 0 }}>
                    <span style={{ fontFamily: S.sans, fontSize: 13, color: S.ink, flex: 1, minWidth: 0, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', filter: emailRevealed ? 'none' : 'blur(5px)', userSelect: emailRevealed ? 'auto' : 'none', transition: 'filter 0.2s' }}>{email || 'User'}</span>
                    <span role="button" tabIndex={0}
                      onClick={e => { e.stopPropagation(); setEmailRevealed(v => !v); }}
                      onKeyDown={e => { if (e.key === 'Enter' || e.key === ' ') { e.stopPropagation(); setEmailRevealed(v => !v); } }}
                      style={{ flexShrink: 0, cursor: 'pointer', color: S.ink3, lineHeight: 0 }}
                      aria-label={emailRevealed ? 'Hide email' : 'Show email'}
                    >
                      {emailRevealed
                        ? <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M17.94 17.94A10.07 10.07 0 0112 20c-7 0-11-8-11-8a18.45 18.45 0 015.06-5.94"/><path d="M9.9 4.24A9.12 9.12 0 0112 4c7 0 11 8 11 8a18.5 18.5 0 01-2.16 3.19"/><line x1="1" y1="1" x2="23" y2="23"/></svg>
                        : <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/></svg>
                      }
                    </span>
                  </div>
                )}
              </div>

              <window.Icon name="chevDown" size={14} color={S.ink3} style={{ flexShrink: 0 }}/>
            </button>

            {acctOpen && (
              <div data-acct style={{
                position: 'absolute', bottom: 'calc(100% + 6px)', left: 0, right: 0,
                background: S.paper, border: `1px solid ${S.line2}`, borderRadius: 12,
                boxShadow: '0 12px 40px rgba(28,26,23,0.16)', padding: 6, zIndex: 50,
                animation: 'sol-fade-in 0.12s ease',
              }}>
                {[
                  { icon: 'info', label: 'Usage', href: '/usage' },
                  { icon: 'settings', label: 'Settings', action: () => { setAcctOpen(false); onOpenSettings(); } },
                  ...(!['plus','studio'].includes(tier) ? [{ icon: 'sparkle', label: 'Plans', href: '/pricing' }] : []),
                ].map(item => (
                  item.href
                    ? <a key={item.label} href={item.href} style={menuItemStyle(S)} onMouseEnter={e => e.currentTarget.style.background = S.cream2} onMouseLeave={e => e.currentTarget.style.background = 'transparent'}>
                        <window.Icon name={item.icon} size={15} color={S.ink2}/>{item.label}
                      </a>
                    : <button key={item.label} onClick={item.action} style={{ ...menuItemStyle(S), border: 'none', width: '100%', textAlign: 'left' }} onMouseEnter={e => e.currentTarget.style.background = S.cream2} onMouseLeave={e => e.currentTarget.style.background = 'transparent'}>
                        <window.Icon name={item.icon} size={15} color={S.ink2}/>{item.label}
                      </button>
                ))}
                <div style={{ height: 1, background: S.line, margin: '4px 0' }}/>
                <button
                  onClick={() => { setAcctOpen(false); onSignOut && onSignOut(); }}
                  style={{ ...menuItemStyle(S), border: 'none', width: '100%', textAlign: 'left', color: '#c0392b' }}
                  onMouseEnter={e => e.currentTarget.style.background = S.cream2}
                  onMouseLeave={e => e.currentTarget.style.background = 'transparent'}
                >
                  <window.Icon name="logout" size={15} color="#c0392b"/>Sign out
                </button>
              </div>
            )}
          </div>
        </div>
      </div>
    </aside>
  );
}

function menuItemStyle(S) {
  return {
    display: 'flex', alignItems: 'center', gap: 8, padding: '8px 10px', borderRadius: 8,
    fontFamily: S.sans, fontSize: 13, color: S.ink, textDecoration: 'none',
    background: 'transparent', cursor: 'pointer', transition: 'background 0.1s',
  };
}

// ConvRow — exact structure from new site's ConvRow
function ConvRow({ chat, active, onOpen, onDelete }) {
  const S = window.SOL;
  const [hovered, setHovered] = React.useState(false);

  return (
    <div
      onClick={onOpen}
      onMouseEnter={() => setHovered(true)}
      onMouseLeave={() => setHovered(false)}
      style={{
        marginBottom: 4,
        display: 'flex', alignItems: 'center', gap: 8,
        borderRadius: 12, padding: '8px 8px',
        cursor: 'pointer', transition: 'background 0.15s',
        border: active ? `1px solid ${S.line2}` : '1px solid transparent',
        background: active ? S.paper : hovered ? S.paper : 'transparent',
        boxShadow: active ? '0 1px 3px rgba(0,0,0,0.06)' : 'none',
      }}
    >
      {/* Model icon — SunMark variant based on chat.model */}
      <window.SunMark size={14} variant={chat.model || 'solus'} style={{ flexShrink: 0 }}/>

      {/* Title — flex-1 truncate text-sm */}
      <span style={{
        flex: 1, minWidth: 0, fontFamily: S.sans, fontSize: 13,
        color: S.ink, fontWeight: active ? 500 : 400,
        overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap',
      }}>{chat.title}</span>

      {/* Trash button — opacity-0 group-hover:opacity-100 */}
      <button
        onClick={e => { e.stopPropagation(); onDelete(); }}
        style={{
          width: 28, height: 28, borderRadius: 8, border: 'none', background: 'transparent',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          cursor: 'pointer', flexShrink: 0, opacity: hovered ? 1 : 0,
          transition: 'opacity 0.15s',
        }}
        title="Delete chat"
      >
        <svg width="14" height="14" viewBox="0 0 24 24" fill="none"
          stroke={S.ink3} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
          <polyline points="3 6 5 6 21 6"/>
          <path d="M19 6l-1 14a2 2 0 01-2 2H8a2 2 0 01-2-2L5 6"/>
          <path d="M10 11v6M14 11v6"/>
          <path d="M9 6V4a1 1 0 011-1h4a1 1 0 011 1v2"/>
        </svg>
      </button>
    </div>
  );
}

window.Sidebar = Sidebar;
