/* Sidebar shell — sized for breath, animated indicator, route switcher */
const NAV = [
  { id: 'calendar',  label: 'Calendar',  icon: 'calendar_month' },
  { id: 'compose',   label: 'Compose',   icon: 'auto_awesome' },
  { id: 'mediathek', label: 'Mediathek', icon: 'photo_library' },
  { id: 'inbox',     label: 'Inbox',     icon: 'forum',  badge: 8 },
  { id: 'accounts',  label: 'Accounts',  icon: 'link' },
  { id: 'brand',     label: 'Brand',     icon: 'palette' },
];

function Sidebar({ route, setRoute }) {
  const itemRefs = useRef({});
  const [indicator, setIndicator] = useState({ top: 0, height: 0, opacity: 0 });

  useLayoutEffect(() => {
    const el = itemRefs.current[route];
    if (el) {
      // Center a fixed-height pill on the active item — the pill rides above
      // the button background so it stays a clean accent rather than getting
      // chewed into corner-notches by the button's rounded corners.
      const PILL_H = 20;
      const top = el.offsetTop + (el.offsetHeight - PILL_H) / 2;
      setIndicator({ top, height: PILL_H, opacity: 1 });
    }
  }, [route]);

  return (
    <aside style={{
      width: 248, flexShrink: 0, height: '100vh',
      background: 'var(--bg)',
      borderRight: '1px solid var(--line)',
      display: 'flex', flexDirection: 'column',
      padding: '20px 14px',
    }}>
      {/* Wordmark */}
      <div style={{ display: 'flex', alignItems: 'center', gap: 10, padding: '6px 8px 22px' }}>
        <div style={{
          width: 28, height: 28, borderRadius: 8,
          background: 'var(--fg)', color: '#0e0e10',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          fontFamily: 'Hanken Grotesk', fontWeight: 800, fontSize: 16, letterSpacing: -0.04,
        }}>H</div>
        <div className="display" style={{ fontSize: 18 }}>Hookline</div>
      </div>

      {/* Workspace */}
      <button className="press lift" style={{
        display: 'flex', alignItems: 'center', gap: 10,
        background: 'var(--bg-1)', border: '1px solid var(--line)',
        borderRadius: 12, padding: '10px 12px', cursor: 'pointer', color: 'var(--fg)',
        marginBottom: 18, width: '100%', textAlign: 'left',
      }}>
        <div style={{
          width: 30, height: 30, borderRadius: 8,
          background: 'var(--bg-3)', border: '1px solid var(--line-2)',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
        }}>
          <Icon name="domain" size={16} />
        </div>
        <div style={{ flex: 1, minWidth: 0 }}>
          <div style={{ fontSize: 13, fontWeight: 500 }}>Global Marketing</div>
          <div className="mono" style={{ fontSize: 10.5, color: 'var(--fg-3)' }}>12 channels · 6 markets</div>
        </div>
        <Icon name="unfold_more" size={16} style={{ color: 'var(--fg-3)' }} />
      </button>

      {/* Compose CTA */}
      <button
        onClick={() => setRoute('compose')}
        className="press"
        style={{
          background: 'var(--fg)', color: '#0e0e10',
          border: 'none', borderRadius: 12,
          height: 40, marginBottom: 22,
          display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8,
          fontFamily: 'Geist', fontWeight: 600, fontSize: 13.5,
          cursor: 'pointer', boxShadow: 'var(--shadow-sm)',
        }}>
        <Icon name="add" size={18} /> New Post
      </button>

      {/* Nav */}
      <nav style={{ position: 'relative', display: 'flex', flexDirection: 'column', gap: 2 }}>
        <span className="nav-indicator" style={{
          top: indicator.top, height: indicator.height, opacity: indicator.opacity,
        }} />
        {NAV.map(n => {
          const active = route === n.id;
          return (
            <button
              key={n.id}
              ref={el => (itemRefs.current[n.id] = el)}
              onClick={() => setRoute(n.id)}
              className="press"
              style={{
                display: 'flex', alignItems: 'center', gap: 12,
                padding: '10px 12px', borderRadius: 10,
                background: active ? 'var(--bg-2)' : 'transparent',
                color: active ? 'var(--fg)' : 'var(--fg-2)',
                border: 'none', cursor: 'pointer', textAlign: 'left',
                fontFamily: 'Geist', fontSize: 13.5, fontWeight: active ? 500 : 400,
                transition: 'background .25s var(--ease), color .2s var(--ease)',
                position: 'relative',
              }}>
              <Icon name={n.icon} size={20} fill={active} style={{ color: active ? 'var(--fg)' : 'var(--fg-3)' }} />
              <span style={{ flex: 1 }}>{n.label}</span>
              {n.badge && (
                <span className="chip accent" style={{ padding: '0 6px', fontSize: 10, height: 16 }}>{n.badge}</span>
              )}
            </button>
          );
        })}
      </nav>

      <div style={{ flex: 1 }} />

      {/* Footer profile */}
      <div className="hairline-t" style={{ paddingTop: 14, display: 'flex', alignItems: 'center', gap: 10 }}>
        <div style={{
          width: 32, height: 32, borderRadius: 99,
          background: 'var(--bg-3)', border: '1px solid var(--line-2)',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          fontFamily: 'Hanken Grotesk', fontWeight: 600, fontSize: 12, color: 'var(--fg)',
          border: '1px solid var(--line-2)',
        }}>EM</div>
        <div style={{ flex: 1, minWidth: 0 }}>
          <div style={{ fontSize: 12.5, fontWeight: 500 }}>Elena M.</div>
          <div className="mono" style={{ fontSize: 10.5, color: 'var(--fg-3)' }}>Marketing Lead</div>
        </div>
        <button className="press" style={{
          background: 'transparent', border: 'none', color: 'var(--fg-3)',
          width: 28, height: 28, borderRadius: 8, cursor: 'pointer',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
        }}>
          <Icon name="settings" size={16} />
        </button>
      </div>
    </aside>
  );
}

function TopBar({ route }) {
  const titles = {
    calendar:  { t: 'Calendar',  s: 'Plan, schedule and review posts across every market.' },
    compose:   { t: 'Compose',   s: 'Draft a post or generate creative — upload first, AI when you need it.' },
    mediathek: { t: 'Mediathek', s: 'Every creative organised by campaign and channel. Drop briefs by email.' },
    inbox:     { t: 'Inbox',     s: 'Unified DMs, comments and mentions from every connected channel.' },
    accounts:  { t: 'Accounts',  s: 'OAuth connections across Instagram and TikTok.' },
    brand:     { t: 'Brand',     s: 'Tone, USPs and per-market overrides.' },
  };
  const c = titles[route] || titles.calendar;
  return (
    <header style={{
      display: 'flex', alignItems: 'center', gap: 16,
      padding: '20px 36px 16px',
      borderBottom: '1px solid var(--line)',
      background: 'var(--bg)',
      flexShrink: 0,
    }}>
      <div style={{ flex: 1, minWidth: 0 }}>
        <div className="display" style={{ fontSize: 30, lineHeight: 1.1 }} key={route + '-t'}>
          {c.t}
        </div>
        <div style={{ color: 'var(--fg-3)', fontSize: 13.5, marginTop: 2 }}>{c.s}</div>
      </div>

      <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
        {/* Search */}
        <div style={{
          display: 'flex', alignItems: 'center', gap: 8,
          padding: '0 12px', height: 36,
          background: 'var(--bg-1)', border: '1px solid var(--line)',
          borderRadius: 10, width: 240,
        }}>
          <Icon name="search" size={16} style={{ color: 'var(--fg-3)' }} />
          <input
            placeholder="Search…"
            style={{
              flex: 1, background: 'transparent', border: 'none', outline: 'none',
              color: 'var(--fg)', fontFamily: 'Geist', fontSize: 13,
            }}
          />
          <span className="mono" style={{ fontSize: 10, color: 'var(--fg-4)', padding: '2px 5px', border: '1px solid var(--line)', borderRadius: 4 }}>⌘ K</span>
        </div>

        <button className="btn ghost press" title="Notifications" style={{ width: 36, padding: 0, justifyContent: 'center', position: 'relative' }}>
          <Icon name="notifications" size={18} />
          <span style={{
            position: 'absolute', top: 7, right: 8,
            width: 7, height: 7, borderRadius: 99,
            background: 'var(--accent)',
          }} />
        </button>
      </div>
    </header>
  );
}

function Shell({ route, setRoute, children }) {
  return (
    <div style={{ display: 'flex', height: '100vh', width: '100vw', overflow: 'hidden' }}>
      <Sidebar route={route} setRoute={setRoute} />
      <main style={{ flex: 1, display: 'flex', flexDirection: 'column', minWidth: 0, background: 'var(--bg)' }}>
        <TopBar route={route} />
        <div key={route} className="route-anim" style={{ flex: 1, overflow: 'auto', minHeight: 0 }}>
          {children}
        </div>
      </main>
    </div>
  );
}

Object.assign(window, { Sidebar, TopBar, Shell, NAV });
