// app.jsx — router + mount

function App() {
  const [state, setState] = React.useState(() => {
    try { const s = JSON.parse(localStorage.getItem('prism.view') || '{}'); return { view: s.view || 'briefing', oppId: s.oppId || 'disa-zt' }; }
    catch { return { view: 'briefing', oppId: 'disa-zt' }; }
  });
  const contentRef = React.useRef(null);

  const go = (view, oppId) => {
    setState(prev => {
      const next = { view, oppId: oppId || prev.oppId };
      try { localStorage.setItem('prism.view', JSON.stringify(next)); } catch {}
      return next;
    });
  };

  React.useEffect(() => { if (contentRef.current) contentRef.current.scrollTop = 0; }, [state.view, state.oppId]);

  const { view, oppId } = state;
  let content;
  switch (view) {
    case 'opportunities': content = <window.Opportunities go={go} />; break;
    case 'detail': content = <window.Detail oppId={oppId} go={go} />; break;
    case 'pipeline': content = <window.Pipeline go={go} />; break;
    case 'partners': content = <window.Partners go={go} />; break;
    case 'analytics': content = <window.Analytics go={go} />; break;
    case 'studio': content = <window.Studio go={go} />; break;
    default: content = <window.Briefing go={go} />;
  }

  return (
    <div className="app-scroll">
      <div className="app">
        <window.Sidebar view={view} go={go} />
        <div className="main">
          <window.Topbar view={view} />
          <div className="content" ref={contentRef}>
            {content}
          </div>
        </div>
      </div>
    </div>
  );
}

ReactDOM.createRoot(document.getElementById('root')).render(<App />);
