// Tempo — icons, sidebar, screen wrappers, sample data

// ─── Theme (light/dark) ──────────────────────────────────────────
const THEME_KEY = 'tempo:theme';

function applyTheme(theme) {
  const t = theme === 'dark' ? 'dark' : 'light';
  if (typeof document !== 'undefined' && document.documentElement) {
    if (t === 'dark') document.documentElement.setAttribute('data-theme', 'dark');
    else document.documentElement.removeAttribute('data-theme');
    const meta = document.querySelector('meta[name="theme-color"]');
    if (meta) meta.setAttribute('content', t === 'dark' ? '#0a0a0a' : '#ffffff');
  }
}

function useTheme() {
  const [theme, setTheme] = React.useState(() => {
    try { return localStorage.getItem(THEME_KEY) === 'dark' ? 'dark' : 'light'; }
    catch (e) { return 'light'; }
  });
  React.useEffect(() => {
    applyTheme(theme);
    try { localStorage.setItem(THEME_KEY, theme); } catch (e) {}
  }, [theme]);
  const toggle = React.useCallback(() => setTheme(t => t === 'dark' ? 'light' : 'dark'), []);
  return { theme, toggle };
}

// Apply persisted theme as soon as this script runs (before React mounts) to avoid a flash.
try {
  applyTheme(localStorage.getItem(THEME_KEY) === 'dark' ? 'dark' : 'light');
} catch (e) {}

// ─── Market status (CME futures, Paris time) ──────────────────────
// CME Globex trading week :
//   - Opens Sunday 17h CT = Monday 00h Paris (year-round, DST aligned)
//   - Closes Friday 16h CT = Friday 23h Paris
//   - Daily maintenance break 16h-17h CT = 23h-00h Paris (Mon-Thu nights)
// → Closed Sat all day, Sun all day, and 23h-00h Paris on weekday nights.
function isCMEFuturesOpen(d) {
  const paris = new Date((d || new Date()).toLocaleString('en-US', { timeZone: 'Europe/Paris' }));
  const day = paris.getDay(); // 0=Sun ... 6=Sat
  const hour = paris.getHours();
  if (day === 6) return false;            // Saturday : closed
  if (day === 0) return false;            // Sunday : closed (opens Mon 00h)
  if (hour === 23) return false;          // Daily maintenance break (Mon-Fri)
  return true;
}
// Alias kept for backwards-compat with anything that still imports isForexOpen
const isForexOpen = isCMEFuturesOpen;

function useMarketOpen() {
  const [open, setOpen] = React.useState(() => isCMEFuturesOpen(new Date()));
  React.useEffect(() => {
    const t = setInterval(() => setOpen(isCMEFuturesOpen(new Date())), 30 * 1000);
    return () => clearInterval(t);
  }, []);
  return open;
}

// ─── Icons ────────────────────────────────────────────────────────
const Ico = {
  home: <svg className="ico" width="18" height="18" viewBox="0 0 20 20"><path d="M3 9l7-6 7 6v8a1 1 0 01-1 1h-4v-6H8v6H4a1 1 0 01-1-1z"/></svg>,
  connections: <svg className="ico" width="18" height="18" viewBox="0 0 20 20"><circle cx="5" cy="10" r="2.4"/><circle cx="15" cy="5" r="2.4"/><circle cx="15" cy="15" r="2.4"/><path d="M7 9l6-3M7 11l6 3"/></svg>,
  cockpit: <svg className="ico" width="18" height="18" viewBox="0 0 20 20"><circle cx="10" cy="10" r="7"/><path d="M10 5v5l3 2M5 10h1M14 10h1M10 4v1M10 15v1"/></svg>,
  groups: <svg className="ico" width="18" height="18" viewBox="0 0 20 20"><circle cx="7" cy="8" r="2.6"/><circle cx="14" cy="9" r="2.2"/><path d="M3 16c.6-2.4 2.4-3.5 4-3.5s3.4 1.1 4 3.5M11 15c.5-1.6 1.8-2.5 3-2.5s2.5.9 3 2.5"/></svg>,
  risk: <svg className="ico" width="18" height="18" viewBox="0 0 20 20"><path d="M10 2l7 3v5c0 4-3 7-7 8-4-1-7-4-7-8V5z"/><path d="M7.5 10.5l1.7 1.7L13 8"/></svg>,
  econ: <svg className="ico" width="18" height="18" viewBox="0 0 20 20"><rect x="3" y="4" width="14" height="13" rx="1.5"/><path d="M3 8h14M7 3v3M13 3v3"/></svg>,
  journaling: <svg className="ico" width="18" height="18" viewBox="0 0 20 20"><path d="M5 3h9l3 3v11a1 1 0 01-1 1H5a1 1 0 01-1-1V4a1 1 0 011-1z"/><path d="M14 3v3h3M7 10h7M7 13h5M7 16h3"/></svg>,
  dashboard: <svg className="ico" width="18" height="18" viewBox="0 0 20 20"><rect x="3" y="3" width="7" height="9" rx="1.4"/><rect x="3" y="14" width="7" height="3" rx="1"/><rect x="12" y="3" width="5" height="6" rx="1.4"/><rect x="12" y="11" width="5" height="6" rx="1.4"/></svg>,
  trades: <svg className="ico" width="18" height="18" viewBox="0 0 20 20"><path d="M3 15l4-4 3 2 7-8"/><path d="M13 5h4v4"/></svg>,
  daily: <svg className="ico" width="18" height="18" viewBox="0 0 20 20"><rect x="3" y="4" width="14" height="13" rx="1.5"/><path d="M3 8h14M10 11v3M7 11v3M13 11v3"/></svg>,
  weekly: <svg className="ico" width="18" height="18" viewBox="0 0 20 20"><rect x="3" y="4" width="14" height="13" rx="1.5"/><path d="M3 8h14M6 12h8"/></svg>,
  stats: <svg className="ico" width="18" height="18" viewBox="0 0 20 20"><path d="M3 3v13a1 1 0 001 1h13"/><path d="M7 13l3-4 2.5 2.5L17 6"/></svg>,
  strategy: <svg className="ico" width="18" height="18" viewBox="0 0 20 20"><path d="M5 3h7l3 3v11a1 1 0 01-1 1H5a1 1 0 01-1-1V4a1 1 0 011-1z"/><path d="M12 3v3h3"/><path d="M7 10h2M7 13h6"/><circle cx="12.5" cy="11" r="1"/></svg>,
  data: <svg className="ico" width="18" height="18" viewBox="0 0 20 20"><ellipse cx="10" cy="5" rx="6.5" ry="2.4"/><path d="M3.5 5v5c0 1.3 2.9 2.4 6.5 2.4s6.5-1.1 6.5-2.4V5"/><path d="M3.5 10v5c0 1.3 2.9 2.4 6.5 2.4s6.5-1.1 6.5-2.4v-5"/></svg>,
  prop: <svg className="ico" width="18" height="18" viewBox="0 0 20 20"><rect x="3" y="6" width="14" height="11" rx="1.5"/><path d="M7 6V4a1.5 1.5 0 011.5-1.5h3A1.5 1.5 0 0113 4v2M3 10h14"/></svg>,
  affiliate: <svg className="ico" width="18" height="18" viewBox="0 0 20 20"><path d="M10 4v12M6 8a2 2 0 012-2h2v4H8a2 2 0 01-2-2zM14 12a2 2 0 01-2 2h-2v-4h2a2 2 0 012 2z"/></svg>,
  settings: <svg className="ico" width="18" height="18" viewBox="0 0 24 24"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 1 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 1 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 1 1-2.83-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 1 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 1 1 2.83-2.83l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1-1.51V3a2 2 0 1 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 1 1 2.83 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 1 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/></svg>,
  plus: <svg className="ico" width="16" height="16" viewBox="0 0 16 16"><path d="M8 2v12M2 8h12"/></svg>,
  search: <svg className="ico" width="16" height="16" viewBox="0 0 16 16"><circle cx="7" cy="7" r="5"/><path d="M11 11l4 4"/></svg>,
  chev: <svg className="ico" width="14" height="14" viewBox="0 0 14 14"><path d="M5 2l5 5-5 5"/></svg>,
  chevDown: <svg className="ico" width="12" height="12" viewBox="0 0 12 12"><path d="M2 4.5l4 4 4-4"/></svg>,
  arrL: <svg className="ico" width="14" height="14" viewBox="0 0 14 14"><path d="M9 2L4 7l5 5"/></svg>,
  arrR: <svg className="ico" width="14" height="14" viewBox="0 0 14 14"><path d="M5 2l5 5-5 5"/></svg>,
  check: <svg className="ico" width="13" height="13" viewBox="0 0 13 13"><path d="M2 6.5l3 3 6-7"/></svg>,
  x: <svg className="ico" width="12" height="12" viewBox="0 0 12 12"><path d="M2 2l8 8M10 2l-8 8"/></svg>,
  upload: <svg className="ico" width="16" height="16" viewBox="0 0 18 18"><path d="M9 12V2M5 6l4-4 4 4"/><path d="M2 12v3a1 1 0 001 1h12a1 1 0 001-1v-3"/></svg>,
  filter: <svg className="ico" width="14" height="14" viewBox="0 0 14 14"><path d="M1 3h12M3 7h8M5 11h4"/></svg>,
  bell: <svg className="ico" width="16" height="16" viewBox="0 0 16 16"><path d="M3 12V7a5 5 0 0110 0v5M1 12h14M6.5 14a1.5 1.5 0 003 0"/></svg>,
  help: <svg className="ico" width="16" height="16" viewBox="0 0 16 16"><circle cx="8" cy="8" r="6"/><path d="M6.2 6.2a2 2 0 113.6 1.3c-.4.5-1.3.6-1.3 1.7"/><circle cx="8" cy="11.5" r=".7" fill="currentColor"/></svg>,
  sun: <svg className="ico" width="16" height="16" viewBox="0 0 16 16"><circle cx="8" cy="8" r="3"/><path d="M8 1v2M8 13v2M1 8h2M13 8h2M2.8 2.8l1.4 1.4M11.8 11.8l1.4 1.4M2.8 13.2l1.4-1.4M11.8 4.2l1.4-1.4"/></svg>,
  moon: <svg className="ico" width="16" height="16" viewBox="0 0 16 16"><path d="M14 9.5A6 6 0 1 1 6.5 2a4.5 4.5 0 0 0 7.5 7.5z"/></svg>,
  cam: <svg className="ico" width="14" height="14" viewBox="0 0 16 16"><rect x="1.5" y="4" width="13" height="9" rx="1.5"/><circle cx="8" cy="8.5" r="2.5"/><path d="M5 4l1-1.5h4L11 4"/></svg>,
  book: <svg className="ico" width="14" height="14" viewBox="0 0 16 16"><path d="M2 3h5a2 2 0 012 2v9a1 1 0 00-1-1H2zM14 3H9a2 2 0 00-2 2v9a1 1 0 011-1h5z"/></svg>,
  sync: <svg className="ico" width="14" height="14" viewBox="0 0 16 16"><path d="M2 8a6 6 0 0110-4.5M14 8a6 6 0 01-10 4.5"/><path d="M11 2v3.5h-3M5 14v-3.5h3"/></svg>,
  refresh: <svg className="ico" width="14" height="14" viewBox="0 0 16 16"><path d="M14 3v4h-4M2 13v-4h4"/><path d="M3.5 7a5 5 0 018.5-2L14 7M12.5 9a5 5 0 01-8.5 2L2 9"/></svg>,
  backtest: <svg className="ico" width="18" height="18" viewBox="0 0 20 20"><path d="M3 3v13a1 1 0 001 1h13"/><path d="M7 13l2.5-3 2 1.5L16 6"/><path d="M8.5 15.5l3 1.8v-3.6z" fill="currentColor" stroke="none"/></svg>,
  fondamental: <svg className="ico" width="18" height="18" viewBox="0 0 20 20"><circle cx="10" cy="10" r="7"/><path d="M3 10h14"/><ellipse cx="10" cy="10" rx="3.2" ry="7"/></svg>,
};

// ─── Sidebar ──────────────────────────────────────────────────────
function Sidebar({ active, onChange, journalingOpen = true, setJournalingOpen, user, displayName }) {
  const email = user?.email || '';
  const handle = email ? email.split('@')[0] : '—';
  // Initials prefer the display name, else the email handle.
  const initialsSource = (displayName || handle || '').trim();
  const initials = initialsSource.slice(0, 2).toUpperCase() || '—';
  const profileLabel = (displayName || '').trim() || 'Mon Profil';
  const main = [
    { id: 'home',        label: 'Accueil',          ico: Ico.home },
    { id: 'econ',        label: 'Calendrier éco.',  ico: Ico.econ },
    { id: 'routines',    label: 'Routines',         ico: Ico.book },
    { id: 'backtest',    label: 'Backtester',       ico: Ico.backtest },
    { id: 'fondamental', label: 'Fondamental',      ico: Ico.fondamental },
  ];
  const journaling = [
    { id: 'dashboard', label: 'Dashboard',     ico: Ico.dashboard },
    { id: 'trades',    label: 'Trades',        ico: Ico.trades },
    { id: 'daily',     label: 'Daily',         ico: Ico.daily },
    { id: 'stats',     label: 'Statistiques',  ico: Ico.stats },
    { id: 'weekly',    label: 'Weekly',        ico: Ico.weekly },
    { id: 'strategy',  label: 'Strategy',      ico: Ico.strategy },
    { id: 'data',      label: 'Manage Data',   ico: Ico.data },
  ];
  const isInJournaling = journaling.some(j => j.id === active);

  const Item = ({ it, sub, on }) => (
    <button
      onClick={() => onChange(it.id)}
      className="tap"
      style={{
        width: '100%',
        background: on ? 'var(--blue-soft)' : 'transparent',
        border: 'none',
        padding: sub ? '8px 12px 8px 36px' : '8px 12px',
        margin: '1px 0',
        cursor: 'pointer',
        display: 'flex', alignItems: 'center', gap: 11,
        color: on ? 'var(--blue-600)' : 'var(--fg-2)',
        textAlign: 'left',
        borderRadius: 8,
        fontFamily: 'inherit',
        position: 'relative',
      }}>
      {on && !sub && <span style={{ position: 'absolute', left: -8, top: 8, bottom: 8, width: 2.5, borderRadius: 2, background: 'var(--blue)' }}></span>}
      <span style={{ display: 'flex', opacity: on ? 1 : .7 }}>{it.ico}</span>
      <span style={{ fontSize: 13, fontWeight: on ? 600 : 500, letterSpacing: '-.01em', flex: 1 }}>{it.label}</span>
    </button>
  );

  return (
    <aside style={{
      width: 232, flexShrink: 0, height: '100%',
      borderRight: '1px solid var(--line)',
      display: 'flex', flexDirection: 'column',
      background: 'var(--bg-app)',
    }}>
      {/* Brand */}
      <div style={{ padding: '18px 16px 18px', display: 'flex', alignItems: 'center', gap: 9 }}>
        <div style={{ width: 28, height: 28, borderRadius: 8, background: 'var(--blue)', display: 'flex', alignItems: 'center', justifyContent: 'center', position: 'relative' }}>
          <div style={{ width: 1.5, height: 14, background: '#fff' }}></div>
          <div style={{ position: 'absolute', width: 10, height: 1.5, background: '#fff', top: 8 }}></div>
        </div>
        <span style={{ fontSize: 18, fontWeight: 700, letterSpacing: '-.02em', color: 'var(--blue-600)' }}>Tempo</span>
      </div>

      {/* Profile */}
      <button className="tap" onClick={() => onChange('settings')} style={{
        margin: '0 12px 14px',
        background: active === 'settings' ? 'var(--blue-soft)' : 'transparent',
        border: 'none', borderRadius: 10,
        padding: '8px 10px', cursor: 'pointer',
        display: 'flex', alignItems: 'center', gap: 10,
        color: active === 'settings' ? 'var(--blue-600)' : 'var(--fg)',
      }}>
        <div style={{ width: 32, height: 32, borderRadius: '50%', background: 'linear-gradient(135deg, #1e293b, #475569)', color: '#fff', display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 12, fontWeight: 600 }}>{initials}</div>
        <div style={{ display: 'flex', flexDirection: 'column', gap: 1, flex: 1, textAlign: 'left', overflow: 'hidden' }}>
          <span style={{ fontSize: 13, fontWeight: 600, letterSpacing: '-.01em', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{profileLabel}</span>
          <span style={{ fontSize: 11, color: 'var(--fg-3)', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{email || '—'}</span>
        </div>
      </button>

      <nav style={{ flex: 1, overflowY: 'auto', padding: '0 12px' }}>
        {main.map(it => <Item key={it.id} it={it} on={active === it.id}/>)}

        {/* Journaling group */}
        <div style={{ marginTop: 14 }}>
          <button
            onClick={() => setJournalingOpen && setJournalingOpen(!journalingOpen)}
            className="tap"
            style={{
              width: '100%', background: isInJournaling ? 'var(--blue-soft)' : 'transparent',
              border: 'none', padding: '8px 12px', cursor: 'pointer',
              display: 'flex', alignItems: 'center', gap: 11,
              color: isInJournaling ? 'var(--blue-600)' : 'var(--fg-2)',
              textAlign: 'left', borderRadius: 8, position: 'relative',
            }}>
            {isInJournaling && <span style={{ position: 'absolute', left: -8, top: 8, bottom: 8, width: 2.5, borderRadius: 2, background: 'var(--blue)' }}></span>}
            <span style={{ display: 'flex', opacity: isInJournaling ? 1 : .7 }}>{Ico.journaling}</span>
            <span style={{ fontSize: 13, fontWeight: isInJournaling ? 600 : 500, letterSpacing: '-.01em', flex: 1 }}>Journaling</span>
            <span style={{ display: 'flex', transform: journalingOpen ? 'rotate(180deg)' : 'rotate(0deg)', transition: 'transform .2s var(--ease)', color: 'var(--fg-3)' }}>{Ico.chevDown}</span>
          </button>
          {journalingOpen && journaling.map(it => <Item key={it.id} it={it} sub on={active === it.id}/>)}
        </div>

        <div style={{ marginTop: 14 }}>
        </div>
      </nav>

      <div style={{ padding: '4px 12px 14px', display: 'flex', flexDirection: 'column', gap: 2 }}>
        <Item it={{ id: 'settings', label: 'Réglages', ico: Ico.settings }} on={active === 'settings'}/>
      </div>
    </aside>
  );
}

// ─── Page header (sticky, shared across desktop pages) ────────────
function PageHeader({ title, right, left }) {
  const marketOpen = useMarketOpen();
  const { theme, toggle } = useTheme();
  return (
    <div style={{
      position: 'sticky', top: 0, zIndex: 10,
      height: 56, padding: '0 24px',
      display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      borderBottom: '1px solid var(--line)',
      background: 'var(--header-bg, rgba(255,255,255,0.86))', backdropFilter: 'blur(16px)', WebkitBackdropFilter: 'blur(16px)',
    }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 14 }}>
        <button className="tap" style={{ background: 'transparent', border: 'none', padding: 6, cursor: 'pointer', color: 'var(--fg-2)', display: 'flex' }}>
          <svg className="ico" width="18" height="18" viewBox="0 0 20 20"><rect x="3" y="4" width="14" height="12" rx="1.5"/><path d="M7 4v12"/></svg>
        </button>
        <span style={{ height: 16, width: 1, background: 'var(--line)' }}></span>
        <span style={{ fontSize: 16, fontWeight: 600, letterSpacing: '-.015em' }}>{title}</span>
        {left}
      </div>
      <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
        {marketOpen ? (
          <span title="CME Globex en cours — trading ouvert" style={{ display: 'inline-flex', alignItems: 'center', gap: 6, padding: '4px 10px', borderRadius: 999, background: 'rgba(22,163,74,0.10)', color: 'var(--green-text)', fontSize: 12, fontWeight: 500 }}>
            <span style={{ width: 6, height: 6, borderRadius: '50%', background: 'var(--green)', animation: 'pulse-dot 2s var(--ease) infinite' }}></span>
            CME ouvert
          </span>
        ) : (
          <span title="CME Globex en pause — daily maintenance ou week-end" style={{ display: 'inline-flex', alignItems: 'center', gap: 6, padding: '4px 10px', borderRadius: 999, background: 'var(--bg-elev)', color: 'var(--fg-3)', fontSize: 12, fontWeight: 500, border: '1px solid var(--line)' }}>
            <span style={{ width: 6, height: 6, borderRadius: '50%', background: 'var(--fg-4)' }}></span>
            CME fermé
          </span>
        )}
        <button onClick={toggle} title={theme === 'dark' ? 'Passer en clair' : 'Passer en sombre'} className="tap" style={{ background: 'transparent', border: '1px solid var(--line)', borderRadius: 8, width: 32, height: 32, display: 'flex', alignItems: 'center', justifyContent: 'center', cursor: 'pointer', color: 'var(--fg-2)' }}>{theme === 'dark' ? Ico.moon : Ico.sun}</button>
        <button className="tap" style={{ background: 'transparent', border: '1px solid var(--line)', borderRadius: 8, width: 32, height: 32, display: 'flex', alignItems: 'center', justifyContent: 'center', cursor: 'pointer', color: 'var(--fg-2)', position: 'relative' }}>
          {Ico.bell}
          <span style={{ position: 'absolute', top: 6, right: 6, width: 6, height: 6, borderRadius: '50%', background: 'var(--red)' }}></span>
        </button>
        <button className="tap" style={{ background: 'transparent', border: '1px solid var(--line)', borderRadius: 8, width: 32, height: 32, display: 'flex', alignItems: 'center', justifyContent: 'center', cursor: 'pointer', color: 'var(--fg-2)' }}>{Ico.help}</button>
        {right}
      </div>
    </div>
  );
}

// ─── Desktop screen wrapper ───────────────────────────────────────
function DesktopScreen({ children, active, onChange, journalingOpen, setJournalingOpen, user, displayName }) {
  return (
    <div style={{ width: '100%', height: '100%', display: 'flex', background: 'var(--bg-app)' }}>
      <Sidebar active={active} onChange={onChange} journalingOpen={journalingOpen} setJournalingOpen={setJournalingOpen} user={user} displayName={displayName}/>
      <main style={{ flex: 1, overflow: 'hidden', position: 'relative', background: 'var(--bg-app)' }}>{children}</main>
    </div>
  );
}

// ─── Mobile bottom nav ────────────────────────────────────────────
function BottomNav({ active, onChange }) {
  const items = [
    { id: 'home',     label: 'Accueil',  ico: Ico.home },
    { id: 'stats',    label: 'Stats',    ico: Ico.stats },
    { id: 'trades',   label: 'Trades',   ico: Ico.trades },
    { id: 'backtest', label: 'Backtest', ico: Ico.backtest },
    { id: 'settings', label: 'Plus',     ico: Ico.settings },
  ];
  return (
    <div style={{
      position: 'absolute', bottom: 0, left: 0, right: 0, zIndex: 40,
      background: 'var(--header-bg, rgba(255,255,255,0.92))',
      backdropFilter: 'blur(20px)', WebkitBackdropFilter: 'blur(20px)',
      borderTop: '1px solid var(--line)',
      paddingBottom: 30, paddingTop: 10,
      display: 'flex', justifyContent: 'space-around', alignItems: 'flex-end',
    }}>
      {items.map(it => (
        <button key={it.id} className="tap" onClick={() => onChange && onChange(it.id)} style={{
          background: 'transparent', border: 'none', padding: '4px 6px',
          display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 4,
          color: active === it.id ? 'var(--blue-600)' : 'var(--fg-3)', cursor: 'pointer', minWidth: 56,
        }}>
          {it.ico}
          <span style={{ fontSize: 9.5, fontWeight: 600, letterSpacing: '.04em' }}>{it.label}</span>
        </button>
      ))}
    </div>
  );
}

function IOSChrome({ time = '14:32' }) {
  return (
    <React.Fragment>
      <div className="status">
        <span>{time}</span>
        <span style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
          <svg width="17" height="11" viewBox="0 0 17 11"><rect x="0" y="7" width="3" height="4" rx=".5" fill="#0f172a"/><rect x="5" y="5" width="3" height="6" rx=".5" fill="#0f172a"/><rect x="10" y="3" width="3" height="8" rx=".5" fill="#0f172a"/><rect x="15" y="0" width="3" height="11" rx=".5" fill="#0f172a" opacity=".35"/></svg>
          <svg width="26" height="13" viewBox="0 0 26 13"><rect x=".5" y=".5" width="22" height="12" rx="3" fill="none" stroke="#0f172a" strokeOpacity=".4"/><rect x="2.5" y="2.5" width="18" height="8" rx="1.5" fill="#0f172a"/><path d="M24 4v5" stroke="#0f172a" strokeOpacity=".4" strokeLinecap="round"/></svg>
        </span>
      </div>
      <div className="home-bar"><div></div></div>
    </React.Fragment>
  );
}

function MobileScreen({ children, time = '14:32', active, onChange, showNav = true }) {
  return (
    <React.Fragment>
      <IOSChrome time={time}/>
      <div className="scroll" style={{
        position: 'absolute', top: 54, left: 0, right: 0,
        bottom: showNav ? 78 : 0, overflow: 'auto',
        background: 'var(--bg-app)',
      }}>{children}</div>
      {showNav && <BottomNav active={active} onChange={onChange}/>}
    </React.Fragment>
  );
}

// ─── Sample data ──────────────────────────────────────────────────
// P&L by day for May 2026 (sparse — most days empty as in screenshots)
const PNL = {
  4:  { p: 240, t: 3, w: 2, l: 1 },
  5:  { p: -180, t: 2, w: 0, l: 2 },
  6:  { p: 460, t: 4, w: 3, l: 1 },
  7:  { p: 120, t: 1, w: 1, l: 0 },
  8:  { p: -90, t: 3, w: 1, l: 2 },
  11: { p: 540, t: 5, w: 4, l: 1 },
  12: { p: 420, t: 4, w: 3, l: 1 },
  13: { p: -220, t: 4, w: 1, l: 3 },
  14: { p: 180, t: 2, w: 2, l: 0 },
  15: { p: 720, t: 4, w: 3, l: 1 },
  18: { p: 0,   t: 0, w: 0, l: 0 }, // journaled but no trades
  19: { p: 340, t: 3, w: 2, l: 1 },
  20: { p: -60, t: 2, w: 1, l: 1 },
  21: { p: 280, t: 3, w: 2, l: 1 },
  22: { p: -140, t: 2, w: 0, l: 2 },
  25: { p: 0,   t: 0, w: 0, l: 0 }, // today, no trades yet
};

const TRADES_TODAY = [];
const RECENT_TRADES = [
  { id: 1, date: '22 mai', time: '14:12', sym: 'NQ',     dir: 'L', entry: 18420.5, exit: 18395, lots: 1,   pnl: -140, r: -1.1, setup: 'News',     hold: '4 min'  },
  { id: 2, date: '22 mai', time: '11:48', sym: 'ES',     dir: 'S', entry: 5278,   exit: 5274,  lots: 2,   pnl: 200,  r: 1.3,  setup: 'Range',    hold: '12 min' },
  { id: 3, date: '21 mai', time: '15:42', sym: 'ES',     dir: 'L', entry: 5266,   exit: 5272.5, lots: 1,  pnl: 325,  r: 1.6,  setup: 'Breakout', hold: '18 min' },
  { id: 4, date: '21 mai', time: '11:20', sym: 'EURUSD', dir: 'S', entry: 1.0842, exit: 1.0828, lots: .5, pnl: 70,   r: 0.9,  setup: 'Range',    hold: '32 min' },
  { id: 5, date: '21 mai', time: '09:34', sym: 'NQ',     dir: 'L', entry: 18380,  exit: 18365, lots: 1,   pnl: -125, r: -0.8, setup: 'Reversal', hold: '6 min'  },
  { id: 6, date: '20 mai', time: '14:48', sym: 'GC',     dir: 'L', entry: 2364,   exit: 2370,  lots: 1,   pnl: 60,   r: 0.6,  setup: 'Pullback', hold: '21 min' },
  { id: 7, date: '20 mai', time: '10:12', sym: 'ES',     dir: 'S', entry: 5290,   exit: 5294,  lots: 1,   pnl: -120, r: -1.0, setup: 'News',     hold: '3 min'  },
  { id: 8, date: '19 mai', time: '14:55', sym: 'NQ',     dir: 'L', entry: 18250,  exit: 18290, lots: 1,   pnl: 340,  r: 2.1,  setup: 'Breakout', hold: '24 min' },
];

const NEWS = [
  { src: 'BLOOMBERG', t: 'Fed minutes signal patience on rate cuts as inflation lingers', time: '06:42' },
  { src: 'FT',        t: 'ECB members split on terminal rate path, eyes on May CPI print', time: '06:55' },
  { src: 'LE MONDE',  t: 'CAC 40 atteint un sommet historique sur résultats T1', time: '07:10' },
  { src: 'REUTERS',   t: 'Tech mega-caps lead Nasdaq futures higher pre-open', time: '07:18' },
];

const PRETRADE = [
  { id: 'p1', t: 'Plan défini', d: false },
  { id: 'p2', t: 'Risque < 1% du capital', d: false },
  { id: 'p3', t: 'Aucune news à haut impact dans 15min', d: false },
  { id: 'p4', t: 'État mental clair (sommeil, stress)', d: false },
  { id: 'p5', t: 'Aucune position ouverte concurrente', d: false },
];

const STRATEGIES = [
  { id: 's1', name: 'Breakout VWAP',  desc: 'Cassure propre du VWAP avec retest', wr: 0.71, n: 42, pnl: 2840, exp: 138 },
  { id: 's2', name: 'Range Asie',     desc: 'Rejet du haut/bas du range Asie',     wr: 0.54, n: 24, pnl: 480,  exp: 42  },
  { id: 's3', name: 'News Fade',      desc: 'Fade des extensions news >1.5%',      wr: 0.68, n: 16, pnl: 720,  exp: 95  },
  { id: 's4', name: 'Pullback Trend', desc: 'EMA20 retest dans une tendance',       wr: 0.61, n: 18, pnl: 540,  exp: 58  },
  { id: 's5', name: 'Open Reversal',  desc: 'Reversal sur les 30 premières min',    wr: 0.43, n: 14, pnl: -220, exp: -28 },
];

Object.assign(window, {
  Ico, Sidebar, PageHeader, DesktopScreen, BottomNav, IOSChrome, MobileScreen,
  PNL, RECENT_TRADES, NEWS, PRETRADE, STRATEGIES,
  useTheme, useMarketOpen, isCMEFuturesOpen, isForexOpen, applyTheme,
});
