/* global React */
const { useState: useDMState, useEffect: useDMEffect } = React;
const { motion: _dmFm } = window.Motion || {};
const _DMMD = (_dmFm && _dmFm.div) || 'div';
const _dmReduced = window.matchMedia('(prefers-reduced-motion: reduce)').matches;

/* =========================================================
   App design tokens — exact match to Recall dark theme
   ========================================================= */
const C = {
  bg:        'var(--bg)',
  surface:   'var(--surface)',
  s2:        'var(--surface-2)',
  s3:        'var(--surface-3)',
  border:    'var(--border)',
  borderStr: 'var(--border-strong)',
  fg:        'var(--fg)',
  fg2:       'var(--fg-2)',
  fg3:       'var(--fg-3)',
  fg4:       'var(--fg-4)',
  green:     'var(--accent)',
  red:       'var(--surface)',
  redSoft:   'rgba(255,107,107,0.75)',
  flame:     'var(--accent-warm)',
  amber:     'var(--accent-warm)',
};

const MONO = "'Geist Mono', 'JetBrains Mono', ui-monospace, monospace";
const DISP = "'Inter', system-ui, -apple-system, sans-serif";

const DM_VIEWS    = ['home', 'decks', 'stats', 'import'];
const DM_CYCLE_MS = 3500;
const DM_FADE_MS  = 320;
const VIEW_LABELS = { home: 'Home', decks: 'All Decks', stats: 'Stats', import: 'Import' };

/* ---- Flame icon (streak only) ---- */
const MiniFlame = ({ size = 14 }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" aria-hidden>
    <defs>
      <linearGradient id="dm-fg" x1="12" y1="2" x2="12" y2="22" gradientUnits="userSpaceOnUse">
        <stop offset="0" stopColor="#ffe7a8" />
        <stop offset="0.35" stopColor="#ffb14a" />
        <stop offset="0.75" stopColor="#ff6a1a" />
        <stop offset="1" stopColor="#c9320b" />
      </linearGradient>
    </defs>
    <path fill="url(#dm-fg)" d="M8.5 14.5A2.5 2.5 0 0 0 11 17c1.5 0 2.5-1.25 2.5-2.5 0-1.39-.5-2.5-1.5-3.5-2-2-.5-5.5-.5-5.5s4 2 4 6.5a5.5 5.5 0 1 1-11 0c0-2.61 1.5-4 2.5-5 .5-.5 1-1 1-1.5 0 1 1 2 1.5 2.5C10 8.5 8.5 10 8.5 14.5Z" />
  </svg>
);

/* ---- StatCard — matches real app: font-mono, 34px, weight 500 ---- */
const StatCard = ({ label, value, sub, sup }) => (
  <div style={{ background: C.surface, border: `1px solid ${C.border}`, borderRadius: 10, padding: '13px 15px' }}>
    <div style={{ fontFamily: MONO, fontSize: 8.5, color: C.fg3, letterSpacing: '0.12em', textTransform: 'uppercase', marginBottom: 7 }}>{label}</div>
    <div style={{ fontFamily: MONO, fontWeight: 500, fontSize: 34, color: C.fg, lineHeight: 1.05, letterSpacing: '-0.02em', fontVariantNumeric: 'tabular-nums' }}>
      {value}{sup && <span style={{ fontSize: 18, color: C.fg3, marginLeft: 2, fontWeight: 400 }}>{sup}</span>}
    </div>
    {sub && <div style={{ fontSize: 11, color: C.fg3, marginTop: 6 }}>{sub}</div>}
  </div>
);

/* ---- DeckRow — SVG icon, correct button states ---- */
const DeckRow = ({ icon, name, sub, status, pct, due }) => (
  <div style={{ background: C.surface, border: `1px solid ${C.border}`, borderRadius: 10, padding: '14px 18px', display: 'flex', alignItems: 'center', gap: 14 }}>
    <div style={{ width: 38, height: 38, borderRadius: 8, background: C.s2, border: `1px solid ${C.border}`, display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0, color: C.fg2 }}>
      <NavIcon name={icon} size={18} />
    </div>
    <div style={{ flex: 1, minWidth: 0 }}>
      <div style={{ fontFamily: DISP, fontWeight: 800, fontSize: 15.5, letterSpacing: '-0.02em', color: C.fg, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{name}</div>
      <div style={{ fontFamily: MONO, fontSize: 10.5, color: C.fg3, marginTop: 2 }}>{sub}</div>
    </div>
    {status === 'caught' ? (
      <span style={{ padding: '3px 9px', background: C.s3, border: `1px solid ${C.border}`, borderRadius: 999, fontFamily: MONO, fontSize: 10, color: C.fg3, letterSpacing: '0.06em', textTransform: 'uppercase', flexShrink: 0 }}>caught up</span>
    ) : (
      <span style={{ padding: '3px 9px', background: C.fg, color: 'var(--bg)', borderRadius: 999, fontFamily: MONO, fontSize: 10, fontWeight: 600, flexShrink: 0 }}>{due}</span>
    )}
    <span style={{ fontFamily: MONO, fontSize: 12, color: C.fg2, flexShrink: 0, width: 36, textAlign: 'right', fontVariantNumeric: 'tabular-nums' }}>{pct}%</span>
    <div style={{
      padding: '5px 11px',
      background: status === 'caught' ? C.s2 : C.fg,
      border: status === 'caught' ? `1px solid ${C.border}` : 'none',
      color: status === 'caught' ? C.fg2 : 'var(--bg)',
      borderRadius: 7, fontSize: 11.5, fontWeight: 600, flexShrink: 0,
    }}>
      {status === 'caught' ? 'Review' : 'Study'}
    </div>
  </div>
);

/* ---- Heatmap — gap and cell height match real app ---- */
const Heatmap = ({ weeks = 28 }) => {
  const rows = 7;
  const palette = [
    C.s3,
    'rgba(212,212,212,0.12)',
    'rgba(212,212,212,0.26)',
    'rgba(212,212,212,0.44)',
    'rgba(212,212,212,0.66)',
    'rgba(212,212,212,0.88)',
  ];
  return (
    <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
      <div style={{
        display: 'grid',
        gridTemplateColumns: `repeat(${weeks}, 1fr)`,
        gridTemplateRows: `repeat(${rows}, 10px)`,
        gridAutoFlow: 'column',
        gap: '3px 4px',
      }}>
        {Array.from({ length: weeks * rows }, (_, i) => {
          const sinVal = Math.sin(i * 12.9898 + 1.1) * 43758.5453;
          const v = Math.max(0, Math.min(5, Math.abs(Math.floor(sinVal % 6))));
          const isToday = i === weeks * rows - 1;
          return (
            <div key={i} style={{
              borderRadius: 2,
              background: isToday ? C.fg : palette[v],
              boxShadow: (v === 5 || isToday) ? '0 0 4px rgba(212,212,212,0.4)' : 'none',
            }} />
          );
        })}
      </div>
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', color: C.fg3, fontFamily: MONO, fontSize: 9.5 }}>
        <span>28 weeks</span>
        <span style={{ display: 'inline-flex', alignItems: 'center', gap: 4 }}>
          Less
          {[1, 2, 3, 4, 5].map(v => (
            <span key={v} style={{ width: 8, height: 8, borderRadius: 2, background: palette[v] }} />
          ))}
          More
        </span>
      </div>
    </div>
  );
};

/* ---- Nav icons (sidebar + misc) ---- */
const NavIcon = ({ name, size = 15 }) => {
  const paths = {
    home:     <><path d="M3 9 12 2l9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/><polyline points="9 22 9 12 15 12 15 22"/></>,
    book:     <><path d="M4 19.5A2.5 2.5 0 0 1 6.5 17H20"/><path d="M6.5 2H20v20H6.5A2.5 2.5 0 0 1 4 19.5v-15A2.5 2.5 0 0 1 6.5 2z"/></>,
    bar:      <><line x1="12" y1="20" x2="12" y2="10"/><line x1="18" y1="20" x2="18" y2="4"/><line x1="6" y1="20" x2="6" y2="14"/></>,
    upload:   <><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/></>,
    settings: <><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 0 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 0 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 0 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 0 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 0 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 0 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 0 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 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/></>,
    layers:     <><polygon points="12 2 2 7 12 12 22 7 12 2"/><polyline points="2 17 12 22 22 17"/><polyline points="2 12 12 17 22 12"/></>,
    csv:        <><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/></>,
    microscope: <><path d="M2 22h20"/><path d="M10 22v-8"/><circle cx="10" cy="11" r="4"/><path d="M10 7V3"/><path d="M8 3h4"/><path d="M10 15h10"/></>,
    globe:      <><circle cx="12" cy="12" r="10"/><line x1="2" y1="12" x2="22" y2="12"/><path d="M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z"/></>,
    graduation: <><path d="M22 10v6M2 10l10-5 10 5-10 5z"/><path d="M6 12v5c3 3 9 3 12 0v-5"/></>,
    atom:       <><circle cx="12" cy="12" r="1"/><path d="M20.2 20.2c2-2 0-7.4-4.5-11.9S6 1.8 4 3.8c-2 2 0 7.4 4.5 11.9s9.7 6.5 11.7 4.5z"/><path d="M15.7 15.7c4.5-4.5 6.5-9.7 4.5-11.7-2-2-7.4 0-11.9 4.5S1.8 18 3.8 20c2 2 7.4 0 11.9-4.3z"/></>,
    chevron:  <polyline points="9 18 15 12 9 6"/>,
    plus:     <><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></>,
    search:   <><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></>,
    download: <><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/></>,
  };
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5"
      strokeLinecap="round" strokeLinejoin="round" style={{ flexShrink: 0 }}>
      {paths[name] || null}
    </svg>
  );
};

/* ---- Sidebar — matches real Sidebar.tsx ---- */
const Sidebar = ({ view }) => {
  const items = [
    { id: 'home',     label: 'Home',      icon: 'home',     due: 18 },
    { id: 'decks',    label: 'All decks', icon: 'book',     count: 4 },
    { id: 'stats',    label: 'Stats',     icon: 'bar'       },
    { id: 'import',   label: 'Import',    icon: 'upload'    },
    { id: 'settings', label: 'Settings',  icon: 'settings'  },
  ];

  const decks = [
    { name: 'Biology',  due: 0  },
    { name: 'Spanish',  due: 8  },
    { name: 'History',  due: 12 },
    { name: 'Physics',  due: 0  },
  ];

  return (
    <div style={{
      width: 184, borderRight: `1px solid ${C.border}`,
      padding: '12px 10px', display: 'flex', flexDirection: 'column', gap: 1,
      background: C.bg, flexShrink: 0,
    }}>
      {/* Brand */}
      <div style={{ display: 'flex', alignItems: 'center', gap: 9, padding: '5px 8px', marginBottom: 8 }}>
        <img src="assets/logo-mark.png" alt="Recall" style={{ width: 26, height: 26, borderRadius: 7, objectFit: 'cover', flexShrink: 0 }} />
        <span style={{ fontFamily: DISP, fontWeight: 800, fontSize: 14, color: C.fg, letterSpacing: '-0.02em' }}>Recall</span>
      </div>

      {/* Nav items */}
      {items.map((it) => {
        const isActive = view === it.id;
        return (
          <div key={it.id} style={{
            display: 'flex', alignItems: 'center', gap: 9,
            padding: '7px 9px', borderRadius: 7, fontSize: 13,
            background: isActive ? C.s2 : 'transparent',
            color: isActive ? C.fg : C.fg2,
            fontWeight: isActive ? 500 : 400,
          }}>
            <NavIcon name={it.icon} size={15} />
            <span style={{ flex: 1 }}>{it.label}</span>
            {it.id === 'home' && it.due > 0 && (
              <span style={{ fontFamily: MONO, fontSize: 10, color: C.fg3 }}>{it.due} due</span>
            )}
            {it.id === 'decks' && it.count > 0 && (
              <span style={{ fontFamily: MONO, fontSize: 10, color: C.fg3 }}>{it.count}</span>
            )}
          </div>
        );
      })}

      {/* Decks section */}
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '8px 9px 3px', fontFamily: MONO, fontSize: 9, color: C.fg3, letterSpacing: '0.1em', textTransform: 'uppercase' }}>
        <span>Decks</span>
        <NavIcon name="plus" size={11} />
      </div>

      {/* Deck list — dot + name, matches sidebar-deck-item */}
      {decks.map((d) => (
        <div key={d.name} style={{
          display: 'flex', alignItems: 'center', gap: 8,
          padding: '4px 9px 4px 8px', fontSize: 12, color: C.fg3, cursor: 'default',
        }}>
          <span style={{ width: 5, height: 5, borderRadius: '50%', background: C.fg3, flexShrink: 0, display: 'inline-block' }} />
          <span style={{ flex: 1, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{d.name}</span>
          {d.due > 0 && (
            <span style={{ fontFamily: MONO, fontSize: 9.5, color: C.fg3 }}>{d.due}</span>
          )}
        </div>
      ))}

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

      {/* Profile pill — name + initials + chevron, no trial text */}
      <div style={{ display: 'flex', alignItems: 'center', gap: 9, padding: '8px 10px', borderRadius: 8, background: C.s2, border: `1px solid ${C.border}`, marginTop: 4 }}>
        <div style={{ width: 26, height: 26, borderRadius: '50%', background: 'var(--surface-3)', display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 10, color: C.fg, fontFamily: MONO, flexShrink: 0, fontWeight: 600 }}>AK</div>
        <span style={{ flex: 1, fontSize: 13, color: C.fg, fontWeight: 500, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>Alex</span>
        <NavIcon name="chevron" size={14} />
      </div>
    </div>
  );
};

/* ============================================================
   Eyebrow — matches .eyebrow class from app
   ============================================================ */
const Eyebrow = ({ children }) => (
  <div style={{ fontFamily: MONO, fontSize: 9, color: C.fg3, letterSpacing: '0.12em', textTransform: 'uppercase' }}>
    {children}
  </div>
);

/* ============================================================
   Slide 1: Home
   ============================================================ */
const DMHome = () => (
  <>
    {/* Greeting row — title left, chips right (matches real HomeScreen) */}
    <div style={{ display: 'flex', alignItems: 'flex-end', justifyContent: 'space-between', gap: 16, flexWrap: 'wrap' }}>
      <div style={{ display: 'flex', flexDirection: 'column', gap: 8, minWidth: 0 }}>
        <Eyebrow>Monday · June 1</Eyebrow>
        <div style={{ fontFamily: DISP, fontWeight: 800, fontSize: 38, letterSpacing: '-0.03em', color: C.fg, lineHeight: 1.05 }}>
          Good evening, Alex.
        </div>
        <div style={{ fontSize: 13.5, color: C.fg2, marginTop: 2, lineHeight: 1.5 }}>
          You have <strong style={{ color: C.fg }}>18 cards due</strong> across 4 decks today. About 5 minutes.
        </div>
      </div>
      {/* Streak + Level chips */}
      <div style={{ display: 'flex', gap: 8, flexShrink: 0 }}>
        <div style={{ display: 'inline-flex', alignItems: 'center', gap: 7, padding: '7px 13px 7px 10px', background: C.surface, border: `1px solid ${C.border}`, borderRadius: 999, fontSize: 13, color: C.fg }}>
          <MiniFlame size={16} /> 14 day streak
        </div>
        <div style={{ display: 'inline-flex', alignItems: 'center', gap: 8, padding: '7px 13px', background: C.surface, border: `1px solid ${C.border}`, borderRadius: 999, fontSize: 13, color: C.fg }}>
          <span style={{ fontWeight: 500 }}>Level 6</span>
          <span style={{ width: 56, height: 4, background: C.s3, borderRadius: 999, overflow: 'hidden', display: 'inline-block' }}>
            <span style={{ display: 'block', width: '65%', height: '100%', background: C.fg, borderRadius: 999 }} />
          </span>
          <span style={{ fontFamily: MONO, fontSize: 10, color: C.fg2 }}>65%</span>
        </div>
      </div>
    </div>

    {/* Today's review CTA — radial-gradient surface, matches real HomeScreen */}
    <div style={{
      background: `radial-gradient(80% 100% at 0% 0%, rgba(212,212,212,0.06) 0%, transparent 60%), ${C.surface}`,
      border: `1px solid ${C.borderStr}`,
      borderRadius: 12, padding: '18px 22px',
      display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 16,
    }}>
      <div style={{ display: 'flex', flexDirection: 'column', gap: 5 }}>
        <Eyebrow>Today's review</Eyebrow>
        <div style={{ display: 'flex', alignItems: 'baseline', gap: 12 }}>
          <span style={{ fontFamily: MONO, fontWeight: 500, fontSize: 52, color: C.fg, letterSpacing: '-0.04em', lineHeight: 1, fontVariantNumeric: 'tabular-nums' }}>18</span>
          <span style={{ fontSize: 15, color: C.fg2 }}>cards due now</span>
        </div>
        <div style={{ fontFamily: MONO, fontSize: 10, color: C.fg3, marginTop: 3 }}>Mixed from 4 decks · FSRS-6 scheduled · ~5 min</div>
      </div>
      <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 7, padding: '10px 18px', background: C.fg, color: C.bg, borderRadius: 8, fontSize: 13, fontWeight: 600, flexShrink: 0 }}>
          <svg width="10" height="10" viewBox="0 0 24 24" fill="currentColor"><path d="M5 3l14 9-14 9V3z"/></svg>
          Start review
        </div>
        <div style={{ width: 34, height: 34, display: 'flex', alignItems: 'center', justifyContent: 'center', background: 'transparent', border: `1px solid ${C.border}`, borderRadius: 7, color: C.fg3 }}>
          <NavIcon name="settings" size={14} />
        </div>
      </div>
    </div>

    {/* Stats row */}
    <div style={{ display: 'grid', gridTemplateColumns: 'repeat(4,1fr)', gap: 10 }}>
      <StatCard label="Reviewed today" value="34" sub="cards" />
      <StatCard label="Accuracy" value="86" sup="%" sub="today" />
      <StatCard label="Streak" value="14" sup="d" sub="best: 21d" />
      <StatCard label="Cards mastered" value="89" sub="of 340" />
    </div>

    {/* Activity heatmap */}
    <div style={{ background: C.surface, border: `1px solid ${C.border}`, borderRadius: 12, padding: '16px 18px' }}>
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 14 }}>
        <span style={{ fontFamily: DISP, fontWeight: 800, fontSize: 16, letterSpacing: '-0.02em', color: C.fg }}>Activity</span>
        <span style={{ fontFamily: MONO, fontSize: 9, color: C.fg3 }}>Active days: 14</span>
      </div>
      <Heatmap weeks={28} />
    </div>
  </>
);

/* ============================================================
   Slide 2: All Decks
   ============================================================ */
const DMDecks = () => (
  <>
    {/* Toolbar */}
    <div style={{ display: 'flex', gap: 8, flexWrap: 'wrap', alignItems: 'center' }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 7, height: 34, padding: '0 11px', background: C.surface, border: `1px solid ${C.border}`, borderRadius: 8, flex: '1 1 160px', minWidth: 0 }}>
        <NavIcon name="search" size={12} />
        <span style={{ fontSize: 12, color: C.fg3 }}>Filter decks…</span>
      </div>
      {/* Sort segmented */}
      <div style={{ display: 'flex', background: C.surface, border: `1px solid ${C.border}`, borderRadius: 8, overflow: 'hidden', flexShrink: 0 }}>
        {['Due', 'Recent', 'A–Z', 'Accuracy'].map((s, i) => (
          <div key={s} style={{ padding: '6px 10px', background: i === 0 ? C.s3 : 'transparent', fontSize: 11.5, color: i === 0 ? C.fg : C.fg3, borderRight: i < 3 ? `1px solid ${C.border}` : 'none' }}>{s}</div>
        ))}
      </div>
      {/* View segmented */}
      <div style={{ display: 'flex', background: C.surface, border: `1px solid ${C.border}`, borderRadius: 8, overflow: 'hidden', flexShrink: 0 }}>
        {['List', 'Grid'].map((s, i) => (
          <div key={s} style={{ padding: '6px 10px', background: i === 0 ? C.s3 : 'transparent', fontSize: 11.5, color: i === 0 ? C.fg : C.fg3, borderRight: i === 0 ? `1px solid ${C.border}` : 'none' }}>{s}</div>
        ))}
      </div>
      {/* Action buttons */}
      <div style={{ display: 'flex', gap: 6, flexShrink: 0, marginLeft: 'auto' }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 5, padding: '5px 11px', background: C.surface, border: `1px solid ${C.border}`, borderRadius: 7, fontSize: 12, color: C.fg2 }}>
          <NavIcon name="download" size={11} />Export all
        </div>
        <div style={{ display: 'flex', alignItems: 'center', gap: 5, padding: '5px 11px', background: C.surface, border: `1px solid ${C.border}`, borderRadius: 7, fontSize: 12, color: C.fg2 }}>
          <NavIcon name="upload" size={11} />Import
        </div>
        <div style={{ display: 'flex', alignItems: 'center', gap: 5, padding: '5px 11px', background: C.fg, color: '#000', borderRadius: 7, fontSize: 12, fontWeight: 600 }}>
          <NavIcon name="plus" size={11} />New deck
        </div>
      </div>
    </div>

    {/* Deck list */}
    <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
      <DeckRow icon="microscope" name="Biology"  sub="45 cards · 2h ago"  status="caught" pct={87} />
      <DeckRow icon="globe"      name="Spanish"  sub="62 cards · 1d ago"  due="8 due"    pct={79} />
      <DeckRow icon="graduation" name="History"  sub="38 cards · 3d ago"  due="12 due"   pct={91} />
      <DeckRow icon="atom"       name="Physics"  sub="29 cards · 5d ago"  status="caught" pct={84} />
    </div>
  </>
);

/* ============================================================
   Slide 3: Stats
   ============================================================ */
const BAR_H    = [45, 62, 38, 71, 55, 83, 47, 66, 52, 78, 43, 69, 57, 100];
const BAR_DAYS = ['T','W','T','F','S','S','M','T','W','T','F','S','S','M'];

const DMStats = () => (
  <>
    {/* Title + range */}
    <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', flexWrap: 'wrap', gap: 8 }}>
      <div style={{ display: 'flex', alignItems: 'baseline', gap: 10 }}>
        <span style={{ fontFamily: DISP, fontWeight: 800, fontSize: 22, letterSpacing: '-0.025em', color: C.fg }}>Your statistics</span>
        <span style={{ fontFamily: MONO, fontSize: 9, color: C.fg3, letterSpacing: '0.1em', textTransform: 'uppercase' }}>All Decks</span>
      </div>
      <div style={{ display: 'flex', background: C.surface, border: `1px solid ${C.border}`, borderRadius: 7, overflow: 'hidden' }}>
        {['7 days', '30 days', '1 year', 'All'].map((s, i) => (
          <div key={s} style={{ padding: '5px 9px', background: i === 1 ? C.s3 : 'transparent', fontSize: 11, color: i === 1 ? C.fg : C.fg3, borderRight: i < 3 ? `1px solid ${C.border}` : 'none' }}>{s}</div>
        ))}
      </div>
    </div>

    {/* Headline stats */}
    <div style={{ display: 'grid', gridTemplateColumns: 'repeat(4,1fr)', gap: 10 }}>
      <StatCard label="Total reviews"   value="340" sub="28 active days" />
      <StatCard label="Current streak"  value="14"  sup="d" sub="best: 21" />
      <StatCard label="Level"           value="6"   sub="650/700 XP" />
      <StatCard label="Accuracy"        value="86"  sup="%" sub="all time" />
    </div>

    {/* Bar chart + donut — asymmetric 1.6fr : 1fr matches real StatsScreen */}
    <div style={{ display: 'grid', gridTemplateColumns: '1.6fr 1fr', gap: 10 }}>
      <div style={{ background: C.surface, border: `1px solid ${C.border}`, borderRadius: 12, padding: '14px 16px 12px', display: 'flex', flexDirection: 'column', gap: 14 }}>
        <div style={{ display: 'flex', alignItems: 'baseline', gap: 7 }}>
          <span style={{ fontFamily: DISP, fontWeight: 700, fontSize: 13, color: C.fg }}>Reviews per day</span>
          <span style={{ fontFamily: MONO, fontSize: 8.5, color: C.fg3, letterSpacing: '0.1em', textTransform: 'uppercase' }}>Last 14 days</span>
        </div>
        <div style={{ display: 'flex', alignItems: 'flex-end', gap: 3.5, height: 60 }}>
          {BAR_H.map((h, i) => (
            <div key={i} style={{ flex: 1, display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 3 }}>
              <div style={{ width: '100%', height: `${Math.max((h / 100) * 54, 2)}px`, background: i === 13 ? C.fg : C.s3, borderRadius: '2px 2px 0 0' }} />
              <div style={{ fontFamily: MONO, fontSize: 7.5, color: i === 13 ? C.fg2 : C.fg4 }}>{BAR_DAYS[i]}</div>
            </div>
          ))}
        </div>
      </div>
      <div style={{ background: C.surface, border: `1px solid ${C.border}`, borderRadius: 12, padding: '14px 16px', display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', gap: 10 }}>
        <svg width="90" height="90" viewBox="0 0 90 90">
          <circle cx="45" cy="45" r="36" fill="none" stroke={C.s3} strokeWidth="9" />
          <circle cx="45" cy="45" r="36" fill="none" stroke={C.fg} strokeWidth="9"
            strokeDasharray={`${0.86 * 2 * Math.PI * 36} ${2 * Math.PI * 36}`}
            strokeDashoffset={`${0.25 * 2 * Math.PI * 36}`}
            strokeLinecap="round"
            style={{ transform: 'rotate(-90deg)', transformOrigin: '45px 45px' }} />
          <text x="45" y="49" textAnchor="middle" fontSize="17" fontWeight="600" fill={C.fg} fontFamily="monospace">86%</text>
        </svg>
        <div style={{ textAlign: 'center' }}>
          <div style={{ fontFamily: MONO, fontSize: 9, color: C.fg3, letterSpacing: '0.1em', textTransform: 'uppercase' }}>Retention Rate</div>
          <div style={{ fontSize: 11, color: C.fg2, marginTop: 3 }}>293 / 340 correct</div>
        </div>
      </div>
    </div>

    {/* Deck breakdown + answer breakdown — 1.4fr : 1fr matches real */}
    <div style={{ display: 'grid', gridTemplateColumns: '1.4fr 1fr', gap: 10 }}>
      <div style={{ background: C.surface, border: `1px solid ${C.border}`, borderRadius: 12, padding: '14px 16px', display: 'flex', flexDirection: 'column', gap: 14 }}>
        <div style={{ display: 'flex', alignItems: 'baseline', gap: 7 }}>
          <span style={{ fontFamily: DISP, fontWeight: 700, fontSize: 13, color: C.fg }}>Strongest decks</span>
          <span style={{ fontFamily: MONO, fontSize: 8, color: C.fg3, letterSpacing: '0.1em', textTransform: 'uppercase' }}>By Accuracy</span>
        </div>
        <div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
          {[['graduation','History',91],['microscope','Biology',87],['atom','Physics',84],['globe','Spanish',79]].map(([icon,n,p]) => (
            <div key={n} style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
              <span style={{ width: 22, height: 22, display: 'inline-flex', alignItems: 'center', justifyContent: 'center', color: C.fg2, flexShrink: 0 }}><NavIcon name={icon} size={15} /></span>
              <span style={{ fontSize: 12.5, color: C.fg2, flex: 1, minWidth: 0, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{n}</span>
              <div style={{ width: 60, height: 8, background: C.s3, borderRadius: 999, overflow: 'hidden' }}>
                <div style={{ height: '100%', width: `${p}%`, background: C.fg, borderRadius: 999 }} />
              </div>
              <span style={{ fontFamily: MONO, fontSize: 11, color: C.fg3, width: 30, textAlign: 'right', flexShrink: 0, fontVariantNumeric: 'tabular-nums' }}>{p}%</span>
            </div>
          ))}
        </div>
      </div>
      <div style={{ background: C.surface, border: `1px solid ${C.border}`, borderRadius: 12, padding: '14px 16px', display: 'flex', flexDirection: 'column', gap: 14 }}>
        <div style={{ display: 'flex', alignItems: 'baseline', gap: 7 }}>
          <span style={{ fontFamily: DISP, fontWeight: 700, fontSize: 13, color: C.fg }}>Answer breakdown</span>
          <span style={{ fontFamily: MONO, fontSize: 8, color: C.fg3, letterSpacing: '0.1em', textTransform: 'uppercase' }}>All Time</span>
        </div>
        <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
          {[
            { l: 'Easy',  n: 45,  p: 13 },
            { l: 'Good',  n: 198, p: 58 },
            { l: 'Hard',  n: 62,  p: 18 },
            { l: 'Again', n: 35,  p: 10 },
          ].map(({ l, n, p }) => (
            <div key={l} style={{ display: 'flex', flexDirection: 'column', gap: 5 }}>
              <div style={{ display: 'flex', justifyContent: 'space-between', fontSize: 12 }}>
                <span style={{ color: l === 'Again' ? C.redSoft : C.fg2 }}>{l}</span>
                <span style={{ fontFamily: MONO, color: C.fg3, fontSize: 11 }}>{n} · {p}%</span>
              </div>
              <div style={{ height: 6, background: C.s3, borderRadius: 999, overflow: 'hidden' }}>
                <div style={{ height: '100%', width: `${p}%`, background: l === 'Again' ? C.red : C.fg2, borderRadius: 999 }} />
              </div>
            </div>
          ))}
        </div>
      </div>
    </div>
  </>
);

/* ============================================================
   Slide 4: Import
   ============================================================ */
const DMImport = () => (
  <>
    {/* Header — matches real ImportScreen: h3 + mono label */}
    <div style={{ display: 'flex', alignItems: 'baseline', gap: 12 }}>
      <span style={{ fontFamily: DISP, fontWeight: 800, fontSize: 22, letterSpacing: '-0.025em', color: C.fg }}>Import cards</span>
      <span style={{ fontFamily: MONO, fontSize: 9, color: C.fg3, letterSpacing: '0.08em', textTransform: 'uppercase' }}>new</span>
    </div>

    {/* Step indicator */}
    <div style={{ display: 'flex', alignItems: 'center', gap: 0 }}>
      {[
        { n: 1, label: 'Source', active: true },
        { n: 2, label: 'Extract', active: false },
        { n: 3, label: 'Review', active: false },
      ].map((s, i) => (
        <React.Fragment key={s.n}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
            <div style={{
              width: 22, height: 22, borderRadius: 999,
              background: s.active ? C.fg : C.s2,
              color: s.active ? C.bg : C.fg3,
              display: 'flex', alignItems: 'center', justifyContent: 'center',
              fontFamily: MONO, fontSize: 11, fontWeight: 600,
              border: `1px solid ${s.active ? 'transparent' : C.border}`,
              flexShrink: 0,
            }}>{s.n}</div>
            <span style={{ fontSize: 13, color: s.active ? C.fg : C.fg3, fontWeight: s.active ? 600 : 400 }}>{s.label}</span>
          </div>
          {i < 2 && <div style={{ flex: 1, height: 1, background: C.border, margin: '0 10px', minWidth: 20 }} />}
        </React.Fragment>
      ))}
    </div>

    {/* Source cards — icon container 44×44, padding 24, matches ImportScreen */}
    <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3,1fr)', gap: 14 }}>
      {[
        { icon: 'book',   title: 'Recall file (.recall)', hint: 'Restore a previously exported Recall deck' },
        { icon: 'layers', title: 'Anki deck (.apkg)',     hint: 'Import your existing Anki decks'           },
        { icon: 'csv',    title: 'CSV / spreadsheet',    hint: 'Front,Back columns mapped directly'        },
      ].map((card) => (
        <div key={card.icon} style={{
          background: C.surface, border: `1px solid ${C.border}`, borderRadius: 12,
          padding: '14px 20px', display: 'flex', flexDirection: 'column', gap: 12,
        }}>
          <div style={{ width: 44, height: 44, borderRadius: 10, background: C.s2, border: `1px solid ${C.border}`, display: 'flex', alignItems: 'center', justifyContent: 'center', color: C.fg2 }}>
            <NavIcon name={card.icon} size={20} />
          </div>
          <div>
            <div style={{ fontSize: 14.5, fontWeight: 600, color: C.fg, marginBottom: 4, letterSpacing: '-0.011em' }}>{card.title}</div>
            <div style={{ fontSize: 12.5, color: C.fg3, lineHeight: 1.5 }}>{card.hint}</div>
          </div>
        </div>
      ))}
    </div>
  </>
);

const DMBody = ({ view }) => {
  if (view === 'home')   return <DMHome />;
  if (view === 'decks')  return <DMDecks />;
  if (view === 'stats')  return <DMStats />;
  if (view === 'import') return <DMImport />;
  return null;
};

/* ============================================================
   Main DashboardMock
   ============================================================ */
const DashboardMock = () => {
  const [idx, setIdx] = useDMState(0);
  const [showing, setShowing] = useDMState(true);

  useDMEffect(() => {
    const id = setInterval(() => {
      setShowing(false);
      setTimeout(() => { setIdx((i) => (i + 1) % DM_VIEWS.length); setShowing(true); }, DM_FADE_MS);
    }, DM_CYCLE_MS);
    return () => clearInterval(id);
  }, []);

  const view = DM_VIEWS[idx];

  const floatProps = (!_dmReduced && _dmFm) ? {
    animate: { y: [0, -6, 0] },
    transition: { duration: 4.5, ease: 'easeInOut', repeat: Infinity, repeatType: 'loop' },
  } : {};

  return (
    <_DMMD {...floatProps} style={{ width: 'min(1100px, 94vw)', margin: '0 auto', position: 'relative' }}>
      <div aria-hidden style={{
        position: 'absolute', inset: '-60px -120px', zIndex: -1,
        background: 'radial-gradient(ellipse at 50% 30%, rgba(255,255,255,calc(0.09 * var(--site-glow-mul, 1))) 0%, rgba(255,255,255,0) 60%)',
      }} />
      <div style={{
        position: 'relative', background: C.bg,
        border: `1px solid rgba(255,255,255,0.06)`,
        borderRadius: 16, overflow: 'hidden',
        boxShadow: '0 32px 80px rgba(0,0,0,0.75), 0 0 0 1px rgba(255,255,255,0.05)',
        fontFamily: DISP,
      }}>
        {/* Title bar */}
        <div style={{
          height: 34, padding: '0 14px', display: 'flex', alignItems: 'center', gap: 10,
          borderBottom: `1px solid ${C.border}`, background: '#000000',
        }}>
          <div style={{ display: 'flex', gap: 5 }}>
            {['#ff5f57','#febc2e','#28c840'].map((col) => (
              <span key={col} style={{ width: 10, height: 10, borderRadius: '50%', background: col, opacity: 0.7 }} />
            ))}
          </div>
          <div style={{ flex: 1, textAlign: 'center', fontFamily: MONO, fontSize: 10.5, color: C.fg3, letterSpacing: '0.04em' }}>
            Recall — {VIEW_LABELS[view]}
          </div>
          <div style={{ width: 42 }} />
        </div>

        {/* App body */}
        <div style={{ display: 'flex', height: 620, overflow: 'hidden' }}>
          <Sidebar view={view} />
          <div style={{
            flex: 1, padding: '22px 24px', background: C.bg,
            display: 'flex', flexDirection: 'column', gap: 12,
            opacity: showing ? 1 : 0,
            transform: showing ? 'translateY(0)' : 'translateY(5px)',
            transition: `opacity ${DM_FADE_MS}ms cubic-bezier(0.4,0,0.2,1), transform ${DM_FADE_MS}ms cubic-bezier(0.4,0,0.2,1)`,
            overflow: 'hidden',
          }}>
            <DMBody view={view} />
          </div>
        </div>

        {/* Slide indicators */}
        <div style={{
          position: 'absolute', left: 0, right: 0, bottom: 10,
          display: 'flex', justifyContent: 'center', pointerEvents: 'none',
        }}>
          <div style={{ display: 'flex', gap: 5 }}>
            {DM_VIEWS.map((v, i) => (
              <span key={v} style={{
                width: i === idx ? 22 : 5, height: 4, borderRadius: 999,
                background: i === idx ? C.fg : 'rgba(212,212,212,0.18)',
                transition: 'all 240ms ease',
              }} />
            ))}
          </div>
        </div>
      </div>
    </_DMMD>
  );
};

Object.assign(window, { DashboardMock, MiniFlame });
