// Solari · Landing · Animated "Tonight" loop
// Story: Today hero (salmon) → Start cooking → Brief → Mise en place → Bake (timer) → Done + voice note
// Self-contained; relies on PhoneShell from landing-plan-anim.jsx and window.LandingTabBar.

const { useState: useStateT, useEffect: useEffectT, useRef: useRefT } = React;

// ─── Tokens ────────────────────────────────────────────────────
const TC = {
  ink:      'var(--ink-900, #1f1f1c)',
  ink2:     'var(--ink-700, #3b3b35)',
  ink3:     'var(--ink-500, #76736a)',
  ink4:     'var(--ink-400, #a39f93)',
  paper:    'var(--paper-50, #faf6e9)',
  paper2:   'var(--paper-100, #f3ecd6)',
  paper3:   'var(--paper-200, #e8dfcc)',
  paper4:   'var(--paper-300, #d8cfb8)',
  sage:     'var(--sage-700, #4f7a3a)',
  sageWash: 'var(--sage-100, #e8efd9)',
  amber:    'var(--amber-500, #e29831)',
  amberD:   'var(--amber-700, #c77a1f)',
  amberS:   'var(--amber-100, #f5e3c2)',
  clay:     'var(--terra-600, #b8542e)',
  dark:     '#1f1f1c',
};

// ─── Choreography ──────────────────────────────────────────────
const TONIGHT_SEQUENCE = [
  { t: 0,     state: 'today-rest',      caption: 'It’s 5:18. You haven’t thought about dinner.' },
  { t: 2400,  state: 'today-tap-cook',  caption: 'Solari already has a plan.' },
  { t: 4200,  state: 'brief-show',      caption: 'The brief. Everything in one glance.' },
  { t: 7600,  state: 'brief-tap-begin', caption: 'Begin · the screen stays awake.' },
  { t: 9000,  state: 'mise',            caption: 'Set yourself up. Three things ready.' },
  { t: 12600, state: 'bake-tick',       caption: 'In the oven. Solari watches the clock.' },
  { t: 16400, state: 'bake-meanwhile',  caption: 'Boil water. Set the table. Pour something.' },
  { t: 19400, state: 'done-rest',       caption: 'Plated in 26 minutes. Bon appétit.' },
  { t: 22000, state: 'done-record',     caption: 'A thought from the cook. Saved for next time.' },
  { t: 24800, state: 'done-saved',      caption: '“Double the recipe next time. Great leftovers to top a salad.”' },
];
const TONIGHT_LOOP = 28000;

const useChoreoT = (sequence, duration) => {
  const [t, setT] = useStateT(0);
  const startRef = useRefT(performance.now());
  useEffectT(() => {
    let raf;
    const tick = (now) => { setT((now - startRef.current) % duration); raf = requestAnimationFrame(tick); };
    raf = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(raf);
  }, [duration]);
  let current = sequence[0];
  for (const ev of sequence) { if (ev.t <= t) current = ev; else break; }
  return { t, state: current.state, caption: current.caption || '' };
};

// ─── Tiny glyphs ───────────────────────────────────────────────
const ChevR = ({ size = 13, color = 'currentColor' }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><polyline points="9 18 15 12 9 6"/></svg>
);
const Bell = ({ size = 16, color = 'currentColor' }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M18 8a6 6 0 0 0-12 0c0 7-3 9-3 9h18s-3-2-3-9"/><path d="M13.7 21a2 2 0 0 1-3.4 0"/></svg>
);
const Clock = ({ size = 11, color = 'currentColor' }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="9"/><polyline points="12 7 12 12 15 14"/></svg>
);
const Mic = ({ size = 14, color = 'currentColor' }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round"><rect x="9" y="3" width="6" height="12" rx="3"/><path d="M5 11a7 7 0 0 0 14 0"/><line x1="12" y1="18" x2="12" y2="22"/></svg>
);

// ─── View 1 · Today (mirrors A-today TodayDefault) ─────────────
const TodayView = ({ tapping }) => (
  <div style={{ position: 'absolute', inset: 0, paddingTop: 50, background: TC.paper, padding: '50px 20px 0' }}>
    {/* Header row */}
    <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginTop: 4 }}>
      <div style={{
        fontFamily: 'var(--font-sans, system-ui)', fontSize: 10, fontWeight: 600,
        letterSpacing: '0.12em', color: TC.ink3, textTransform: 'uppercase',
      }}>MON · 5:18 PM</div>
      <Bell size={15} color={TC.ink2} />
    </div>

    {/* Hero text */}
    <div style={{
      fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 24, lineHeight: 1.05,
      letterSpacing: '-0.02em', color: TC.ink, marginTop: 14, whiteSpace: 'nowrap',
    }}>
      Tonight, the <em style={{ color: TC.sage, fontStyle: 'italic' }}>salmon</em>.
    </div>
    <div style={{
      fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 12, fontStyle: 'italic',
      color: TC.ink3, marginTop: 4,
    }}>Saved last Sunday. You have everything.</div>

    {/* Hero card */}
    <div style={{
      marginTop: 14, background: TC.paper, borderRadius: 16, overflow: 'hidden',
      boxShadow: `0 6px 18px rgba(31,31,28,0.08), inset 0 0 0 1px ${TC.paper3}`,
    }}>
      <div style={{
        height: 110,
        backgroundImage: 'url(assets/photo-salmon.jpg)',
        backgroundSize: 'cover', backgroundPosition: 'center',
      }} />
      <div style={{ padding: '11px 13px 12px' }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 6, marginBottom: 4 }}>
          <span style={{
            fontFamily: 'var(--font-sans, system-ui)', fontSize: 8.5, fontWeight: 700,
            letterSpacing: '0.12em', color: TC.sage, background: TC.sageWash,
            padding: '2px 7px', borderRadius: 999,
          }}>MON · DINNER</span>
          <span style={{
            fontFamily: 'var(--font-sans, system-ui)', fontSize: 8.5, fontWeight: 700,
            letterSpacing: '0.12em', color: TC.ink3,
            padding: '2px 7px', borderRadius: 999, boxShadow: `inset 0 0 0 1px ${TC.paper4}`,
          }}>v3</span>
        </div>
        <div style={{
          fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 16, fontWeight: 600,
          color: TC.ink, lineHeight: 1.15,
        }}>Smith Family Salmon</div>
        <div style={{
          fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 11, fontStyle: 'italic',
          color: TC.ink3, marginTop: 3, display: 'inline-flex', alignItems: 'center', gap: 5,
        }}>
          <Clock size={10} color={TC.ink3} /> 30 min · serves 4 · everything's in the fridge
        </div>
        <div style={{
          fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 11.5, fontStyle: 'italic',
          color: TC.sage, marginTop: 6,
        }}>✦ Lily-approved · from Mike's mom</div>

        <div style={{ display: 'grid', gridTemplateColumns: '1fr 1.4fr', gap: 7, marginTop: 11, position: 'relative' }}>
          <div style={{
            padding: '9px 10px', borderRadius: 10, background: TC.paper2, color: TC.ink2,
            fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 12, fontWeight: 600,
            textAlign: 'center',
          }}>Recipe</div>
          <div style={{
            padding: '9px 10px', borderRadius: 10, background: TC.sage, color: TC.paper,
            fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 12, fontWeight: 600,
            textAlign: 'center', position: 'relative',
            boxShadow: tapping ? `0 0 0 3px rgba(79,122,58,.18)` : 'none',
            transition: 'box-shadow 240ms ease',
          }}>
            Start cooking →
            {tapping && (
              <div style={{
                position: 'absolute', left: '50%', top: '50%',
                width: 60, height: 60, borderRadius: '50%',
                border: `1.5px solid ${TC.sage}`,
                marginLeft: -30, marginTop: -30,
                opacity: 0.55, animation: 'shopRipple 700ms ease-out infinite',
                pointerEvents: 'none',
              }} />
            )}
          </div>
        </div>
      </div>
    </div>

    {/* Audible CTA */}
    <div style={{
      marginTop: 10, padding: '11px 14px',
      borderRadius: 12, background: 'transparent',
      boxShadow: `inset 0 0 0 1.5px ${TC.paper3}`,
      borderStyle: 'dashed',
      display: 'flex', alignItems: 'center', justifyContent: 'space-between',
    }}>
      <span style={{
        fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 13, fontWeight: 500,
        color: TC.ink2, whiteSpace: 'nowrap',
      }}>
        Need to call an <em style={{ color: TC.sage, fontStyle: 'italic' }}>audible</em>?
      </span>
      <ChevR size={13} color={TC.ink3} />
    </div>
  </div>
);

// ─── View 2 · Cook Brief (dark, mirrors A-cookmode CookBrief) ──
const BriefView = ({ tapping }) => (
  <div style={{ position: 'absolute', inset: 0, background: TC.dark, padding: '50px 20px 0', color: TC.paper }}>
    {/* Header */}
    <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 12 }}>
      <span style={{ fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 11, fontStyle: 'italic', color: 'rgba(250,246,233,.65)' }}>‹ Tonight</span>
      <span style={{ fontFamily: 'var(--font-sans, system-ui)', fontSize: 8.5, fontWeight: 700, letterSpacing: '.16em', color: TC.amber }}>THE BRIEF · BEFORE YOU START</span>
      <span style={{ fontFamily: 'var(--font-sans, system-ui)', fontSize: 10.5, fontWeight: 600, color: 'rgba(250,246,233,.5)' }}>30 min</span>
    </div>

    {/* Title */}
    <div style={{
      fontFamily: 'var(--font-sans, system-ui)', fontSize: 9, fontWeight: 700,
      letterSpacing: '.16em', color: TC.amber,
    }}>TONIGHT</div>
    <div style={{
      fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 26, lineHeight: 1.05,
      letterSpacing: '-.02em', marginTop: 4, color: TC.paper,
    }}>Smith family <em style={{ color: TC.amber, fontStyle: 'italic' }}>salmon</em>.</div>
    <div style={{
      display: 'flex', gap: 10, marginTop: 6,
      fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 11, fontStyle: 'italic',
      color: 'rgba(250,246,233,.6)',
    }}>
      <span>30 min</span><span>·</span><span>serves 4</span><span>·</span><span>sheet-pan</span>
    </div>

    {/* You need / tools */}
    <div style={{
      marginTop: 16, padding: 12, borderRadius: 12,
      background: 'rgba(250,246,233,.05)', boxShadow: 'inset 0 0 0 1px rgba(250,246,233,.08)',
      display: 'grid', gridTemplateColumns: '1.4fr 1fr', gap: 12,
    }}>
      <div>
        <div style={{ fontFamily: 'var(--font-sans, system-ui)', fontSize: 8.5, fontWeight: 700, letterSpacing: '.14em', color: TC.amber }}>YOU NEED</div>
        <div style={{ fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 11.5, lineHeight: 1.6, marginTop: 5, color: TC.paper }}>
          <div>4 · salmon fillets</div>
          <div>2 · lemons</div>
          <div>2 Tbsp · honey</div>
          <div>3 · garlic</div>
          <div style={{ color: 'rgba(250,246,233,.6)' }}>· olive oil, salt</div>
          <div style={{ color: TC.sage, fontStyle: 'italic', marginTop: 3, fontSize: 11 }}>+ parsley · half a bunch from Sun</div>
        </div>
      </div>
      <div style={{ borderLeft: '1px dashed rgba(250,246,233,.15)', paddingLeft: 12 }}>
        <div style={{ fontFamily: 'var(--font-sans, system-ui)', fontSize: 8.5, fontWeight: 700, letterSpacing: '.14em', color: TC.amber }}>TOOLS</div>
        <div style={{ fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 11.5, lineHeight: 1.6, marginTop: 5, color: TC.paper }}>
          <div>· oven</div>
          <div>· sheet pan</div>
          <div>· small bowl</div>
          <div>· microplane</div>
        </div>
      </div>
    </div>

    {/* Lily's plate */}
    <div style={{
      marginTop: 10, padding: 11, borderRadius: 11,
      background: 'rgba(125,156,93,.14)', boxShadow: 'inset 0 0 0 1px rgba(125,156,93,.32)',
    }}>
      <div style={{ fontFamily: 'var(--font-sans, system-ui)', fontSize: 8.5, fontWeight: 700, letterSpacing: '.14em', color: TC.sage, marginBottom: 3 }}>LILY'S PLATE</div>
      <div style={{ fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 11.5, fontStyle: 'italic', color: 'rgba(250,246,233,.92)', lineHeight: 1.4 }}>
        Not eating salmon tonight. <span style={{ fontStyle: 'normal', color: TC.paper }}>Pasta · butter · parm</span>, separate.
      </div>
    </div>

    {/* Begin button */}
    <div style={{
      position: 'absolute', left: 20, right: 20, bottom: 100,
      padding: '13px 16px', borderRadius: 12,
      background: TC.amber, color: TC.dark, textAlign: 'center',
      fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 13.5, fontWeight: 600,
      boxShadow: '0 8px 20px rgba(226,152,49,.35)',
    }}>
      Begin · screen stays awake
      {tapping && (
        <div style={{
          position: 'absolute', left: '50%', top: '50%',
          width: 80, height: 60, borderRadius: 14,
          border: `1.5px solid ${TC.amber}`,
          marginLeft: -40, marginTop: -30,
          opacity: 0.55, animation: 'shopRipple 700ms ease-out infinite',
          pointerEvents: 'none',
        }} />
      )}
    </div>
  </div>
);

// ─── View 3 · Mise en place (dark) ─────────────────────────────
const MiseView = ({ t, baseT }) => {
  const elapsed = Math.max(0, t - baseT);
  const items = [
    { q: '4',       n: 'salmon fillets',  s: 'pat very dry',  doneAt: 0    },
    { q: '2 Tbsp',  n: 'honey · in bowl', s: '',              doneAt: 600  },
    { q: '2',       n: 'lemons · halved', s: '1 zested first', doneAt: 1300 },
    { q: '3 cloves',n: 'garlic · grated', s: 'microplane',    doneAt: 9999 },
    { q: '·',       n: 'olive oil, salt', s: 'within reach',  doneAt: 9999 },
  ];
  const dots = Array.from({ length: 5 }, (_, i) => i);
  return (
    <div style={{ position: 'absolute', inset: 0, background: TC.dark, padding: '50px 20px 0', color: TC.paper }}>
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 10 }}>
        <span style={{ fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 11, fontStyle: 'italic', color: 'rgba(250,246,233,.65)' }}>‹ Tonight</span>
        <span style={{ fontFamily: 'var(--font-sans, system-ui)', fontSize: 8.5, fontWeight: 700, letterSpacing: '.16em', color: TC.amber }}>STEP 1 OF 5</span>
        <span style={{ fontFamily: 'var(--font-sans, system-ui)', fontSize: 10.5, fontWeight: 600, color: TC.sage }}>0:02</span>
      </div>
      {/* Progress dots */}
      <div style={{ display: 'flex', gap: 4, padding: '0 0 12px' }}>
        {dots.map(i => (
          <div key={i} style={{
            flex: 1, height: 3, borderRadius: 3,
            background: i === 0 ? TC.paper : 'rgba(250,246,233,.18)',
          }} />
        ))}
      </div>

      <div style={{
        fontFamily: 'var(--font-sans, system-ui)', fontSize: 9, fontWeight: 700,
        letterSpacing: '.16em', color: TC.amber,
      }}>STEP 1 · MISE EN PLACE</div>
      <div style={{
        fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 26, lineHeight: 1.05,
        letterSpacing: '-.02em', marginTop: 4, color: TC.paper,
      }}>Set yourself <em style={{ color: TC.amber, fontStyle: 'italic' }}>up</em>.</div>

      <div style={{
        marginTop: 14, borderRadius: 12, overflow: 'hidden',
        background: 'rgba(250,246,233,.05)', boxShadow: 'inset 0 0 0 1px rgba(250,246,233,.08)',
      }}>
        {items.map((r, i) => {
          const done = elapsed >= r.doneAt;
          return (
            <div key={i} style={{
              display: 'flex', alignItems: 'center', gap: 10,
              padding: '11px 13px',
              borderTop: i ? '1px solid rgba(250,246,233,.06)' : 'none',
              opacity: done ? 0.55 : 1,
              transition: 'opacity 320ms ease',
            }}>
              <div style={{
                width: 18, height: 18, borderRadius: 5,
                border: '1.5px solid rgba(250,246,233,.4)',
                background: done ? TC.amber : 'transparent',
                display: 'flex', alignItems: 'center', justifyContent: 'center',
                color: TC.dark, fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 11,
                transition: 'background 280ms ease',
              }}>{done ? '✓' : ''}</div>
              <span style={{
                width: 56, fontFamily: 'var(--font-sans, system-ui)', fontSize: 9.5, fontWeight: 700,
                color: TC.amber, textDecoration: done ? 'line-through' : 'none',
              }}>{r.q}</span>
              <span style={{
                flex: 1, fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 12.5, color: TC.paper,
                textDecoration: done ? 'line-through' : 'none',
              }}>{r.n}</span>
              {r.s && <span style={{ fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 10, fontStyle: 'italic', color: 'rgba(250,246,233,.55)' }}>{r.s}</span>}
            </div>
          );
        })}
      </div>

      <div style={{
        marginTop: 10, padding: 11, borderRadius: 11,
        background: 'rgba(125,156,93,.12)', boxShadow: 'inset 0 0 0 1px rgba(125,156,93,.32)',
      }}>
        <div style={{ fontFamily: 'var(--font-sans, system-ui)', fontSize: 8.5, fontWeight: 700, letterSpacing: '.14em', color: TC.sage, marginBottom: 3 }}>WHILE YOU PREP</div>
        <div style={{ fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 11.5, color: TC.paper, lineHeight: 1.4 }}>
          Oven on · <strong style={{ color: TC.amber, fontWeight: 700 }}>425°</strong>. <span style={{ fontStyle: 'italic', color: 'rgba(250,246,233,.65)' }}>~7 min to preheat.</span>
        </div>
      </div>
    </div>
  );
};

// ─── View 4 · Bake (timer) ─────────────────────────────────────
const BakeView = ({ t, baseT, showMeanwhile }) => {
  const elapsed = Math.max(0, t - baseT);
  // Tick the timer down from 8:00 over the dwell
  const totalSec = Math.max(0, 480 - Math.floor(elapsed / 1000) * 12);
  const mm = Math.floor(totalSec / 60);
  const ss = String(totalSec % 60).padStart(2, '0');
  const fillFrac = 1 - (totalSec / 480) * 0.55;  // appears partially filled
  const dashOffset = 276 * (1 - fillFrac);

  return (
    <div style={{ position: 'absolute', inset: 0, background: TC.dark, padding: '50px 20px 0', color: TC.paper }}>
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 10 }}>
        <span style={{ fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 11, fontStyle: 'italic', color: 'rgba(250,246,233,.65)' }}>‹ Tonight</span>
        <span style={{ fontFamily: 'var(--font-sans, system-ui)', fontSize: 8.5, fontWeight: 700, letterSpacing: '.16em', color: TC.amber }}>STEP 4 · BAKE</span>
        <span style={{ fontFamily: 'var(--font-sans, system-ui)', fontSize: 10.5, fontWeight: 600, color: TC.sage }}>0:12 in</span>
      </div>
      <div style={{ display: 'flex', gap: 4, padding: '0 0 16px' }}>
        {[0,1,2,3,4].map(i => (
          <div key={i} style={{
            flex: 1, height: 3, borderRadius: 3,
            background: i < 3 ? TC.amber : i === 3 ? TC.paper : 'rgba(250,246,233,.18)',
          }} />
        ))}
      </div>

      {/* Timer ring */}
      <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', marginTop: 10 }}>
        <div style={{ fontFamily: 'var(--font-sans, system-ui)', fontSize: 9, fontWeight: 700, letterSpacing: '.16em', color: TC.amber }}>TIME LEFT</div>
        <div style={{ position: 'relative', width: 168, height: 168, marginTop: 10 }}>
          <svg viewBox="0 0 100 100" style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', transform: 'rotate(-90deg)' }}>
            <circle cx="50" cy="50" r="44" fill="none" stroke="rgba(250,246,233,.10)" strokeWidth="3" />
            <circle cx="50" cy="50" r="44" fill="none" stroke={TC.amber} strokeWidth="3"
              strokeDasharray="276" strokeDashoffset={dashOffset}
              strokeLinecap="round"
              style={{ transition: 'stroke-dashoffset 800ms linear' }} />
          </svg>
          <div style={{ position: 'absolute', inset: 0, display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center' }}>
            <div style={{
              fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 46, lineHeight: 1, color: TC.paper,
              letterSpacing: '-.03em', fontVariantNumeric: 'tabular-nums',
            }}>{mm}:{ss}</div>
            <div style={{ fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 11, fontStyle: 'italic', color: 'rgba(250,246,233,.55)', marginTop: 5 }}>of 14 min total</div>
          </div>
        </div>
      </div>

      {/* Meanwhile / Watch-for */}
      <div style={{
        marginTop: 18, padding: 12, borderRadius: 12,
        background: 'rgba(125,156,93,.12)', boxShadow: 'inset 0 0 0 1px rgba(125,156,93,.32)',
        opacity: showMeanwhile ? 1 : 0.55,
        transform: showMeanwhile ? 'translateY(0)' : 'translateY(4px)',
        transition: 'opacity 320ms ease, transform 320ms ease',
      }}>
        <div style={{ fontFamily: 'var(--font-sans, system-ui)', fontSize: 8.5, fontWeight: 700, letterSpacing: '.14em', color: TC.sage, marginBottom: 3 }}>MEANWHILE</div>
        <div style={{ fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 12, color: TC.paper, lineHeight: 1.4 }}>
          Boil water for Lily's pasta. Set the table. Pour yourself something.
        </div>
      </div>
      <div style={{
        marginTop: 8, padding: 12, borderRadius: 12,
        background: 'rgba(180,84,54,.14)', boxShadow: 'inset 0 0 0 1px rgba(180,84,54,.4)',
      }}>
        <div style={{ fontFamily: 'var(--font-sans, system-ui)', fontSize: 8.5, fontWeight: 700, letterSpacing: '.14em', color: TC.clay, marginBottom: 3 }}>WATCH FOR</div>
        <div style={{ fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 12, color: TC.paper, lineHeight: 1.4 }}>
          Edges browning. <span style={{ fontStyle: 'italic', color: 'rgba(250,246,233,.65)' }}>Pull at 12 min if it looks done.</span>
        </div>
      </div>
    </div>
  );
};

// ─── View 5 · Done · chef notes + voice ────────────────────────
const DoneView = ({ recording, showSaved, t, baseT }) => {
  const elapsed = Math.max(0, t - baseT);
  const bars = Array.from({ length: 20 }, (_, i) => {
    const phase = (elapsed / 80) + i * 0.6;
    return recording ? (5 + Math.abs(Math.sin(phase)) * 18) : 4;
  });
  return (
    <div style={{ position: 'absolute', inset: 0, background: TC.dark, padding: '50px 20px 0', color: TC.paper }}>
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 10 }}>
        <span style={{ fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 11, fontStyle: 'italic', color: 'rgba(250,246,233,.65)' }}>‹ Tonight</span>
        <span style={{ fontFamily: 'var(--font-sans, system-ui)', fontSize: 8.5, fontWeight: 700, letterSpacing: '.16em', color: TC.amber }}>STEP 5 · DONE</span>
        <span style={{ fontFamily: 'var(--font-sans, system-ui)', fontSize: 10.5, fontWeight: 600, color: TC.sage }}>0:26 total</span>
      </div>
      <div style={{ display: 'flex', gap: 4, padding: '0 0 12px' }}>
        {[0,1,2,3,4].map(i => (
          <div key={i} style={{
            flex: 1, height: 3, borderRadius: 3,
            background: i < 4 ? TC.amber : TC.paper,
          }} />
        ))}
      </div>

      <div style={{ textAlign: 'center', marginTop: 8 }}>
        <div style={{ fontFamily: 'var(--font-sans, system-ui)', fontSize: 9, fontWeight: 700, letterSpacing: '.16em', color: TC.amber }}>YOU DID IT</div>
        <div style={{ fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 36, lineHeight: 1, letterSpacing: '-.02em', marginTop: 6, color: TC.paper }}>
          Bon <em style={{ color: TC.amber, fontStyle: 'italic' }}>appétit</em>.
        </div>
        <div style={{ fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 11.5, fontStyle: 'italic', color: 'rgba(250,246,233,.55)', marginTop: 6 }}>
          26 minutes, start to plate.
        </div>
      </div>

      {/* Chef's notes card */}
      <div style={{
        marginTop: 18, padding: 14, borderRadius: 13,
        background: 'rgba(250,246,233,.05)', boxShadow: 'inset 0 0 0 1px rgba(250,246,233,.08)',
      }}>
        <div style={{ fontFamily: 'var(--font-sans, system-ui)', fontSize: 9, fontWeight: 700, letterSpacing: '.14em', color: TC.amber }}>CHEF'S NOTES · FOR NEXT TIME</div>

        {/* Mic / waveform / saved transcript */}
        <div style={{
          marginTop: 10, padding: 12, borderRadius: 10,
          background: 'rgba(31,31,28,.45)', boxShadow: 'inset 0 0 0 1px rgba(250,246,233,.12)',
          minHeight: 70, display: 'flex', alignItems: 'center', gap: 10,
        }}>
          <div style={{
            width: 34, height: 34, borderRadius: '50%',
            background: recording ? TC.clay : (showSaved ? TC.sage : TC.amber),
            color: TC.paper,
            display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0,
            boxShadow: recording ? '0 0 0 5px rgba(184,84,46,0.22)' : 'none',
            transition: 'background 320ms ease, box-shadow 320ms ease',
          }}>
            {showSaved ? (
              <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke={TC.paper} strokeWidth="3" strokeLinecap="round" strokeLinejoin="round">
                <polyline points="20 6 9 17 4 12"/>
              </svg>
            ) : <Mic size={14} color={TC.paper} />}
          </div>

          {recording ? (
            <div style={{ flex: 1, height: 24, display: 'flex', alignItems: 'center', gap: 2 }}>
              {bars.map((h, i) => (
                <div key={i} style={{
                  width: 3, height: h, borderRadius: 2, background: TC.clay,
                  transition: 'height 120ms ease',
                }} />
              ))}
            </div>
          ) : showSaved ? (
            <div style={{ flex: 1 }}>
              <div style={{
                fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 12, fontStyle: 'italic',
                color: 'rgba(250,246,233,.92)', lineHeight: 1.4,
              }}>"Double the recipe next time as these would be great leftovers to top a salad."</div>
              <div style={{
                fontFamily: 'var(--font-sans, system-ui)', fontSize: 9, color: 'rgba(250,246,233,.55)',
                marginTop: 4, letterSpacing: '.06em',
              }}>SAVED TO RECIPE · v3 → v4</div>
            </div>
          ) : (
            <div style={{
              flex: 1, fontFamily: 'var(--font-display, Georgia, serif)', fontSize: 12, fontStyle: 'italic',
              color: 'rgba(250,246,233,.45)',
            }}>Hold the mic. Tell us how it went.</div>
          )}

          <div style={{
            fontFamily: 'var(--font-sans, system-ui)', fontSize: 10, fontWeight: 600,
            color: recording ? TC.clay : 'rgba(250,246,233,.5)',
            minWidth: recording ? 24 : 0, textAlign: 'right',
          }}>{recording ? '0:08' : ''}</div>
        </div>
      </div>
    </div>
  );
};

// ─── Main ──────────────────────────────────────────────────────
const TonightAnimatedPhone = () => {
  const { t, state, caption } = useChoreoT(TONIGHT_SEQUENCE, TONIGHT_LOOP);

  // View routing
  let view = 'today';
  if (state.startsWith('today')) view = 'today';
  else if (state.startsWith('brief')) view = 'brief';
  else if (state === 'mise') view = 'mise';
  else if (state.startsWith('bake')) view = 'bake';
  else if (state.startsWith('done')) view = 'done';

  // Base times for sub-animations
  const miseBase = TONIGHT_SEQUENCE.find(e => e.state === 'mise')?.t ?? 0;
  const bakeBase = TONIGHT_SEQUENCE.find(e => e.state === 'bake-tick')?.t ?? 0;
  const doneBase = TONIGHT_SEQUENCE.find(e => e.state === 'done-record')?.t ?? 0;

  // Tab-bar transition pulse (today→cook means we leave the tab bar, fades w/ view)
  const tabActive = view === 'today' ? 'today' : null;

  // Caption gradient base color depends on theme
  const isDark = view !== 'today';

  return (
    <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 28 }}>
      <PhoneShell>
        <div style={{ position: 'relative', height: '100%', overflow: 'hidden' }}>
          {view === 'today' && <TodayView tapping={state === 'today-tap-cook'} />}
          {view === 'brief' && <BriefView tapping={state === 'brief-tap-begin'} />}
          {view === 'mise'  && <MiseView t={t} baseT={miseBase} />}
          {view === 'bake'  && <BakeView t={t} baseT={bakeBase} showMeanwhile={state === 'bake-meanwhile'} />}
          {view === 'done'  && <DoneView t={t} baseT={doneBase} recording={state === 'done-record'} showSaved={state === 'done-saved'} />}

          {/* Tab bar (only on Today; cook mode is full-screen) */}
          {view === 'today' && window.LandingTabBar && <window.LandingTabBar active={tabActive} />}
        </div>
      </PhoneShell>

      {/* Caption — lifted out of the phone, editorial italic */}
      <div style={{
        maxWidth: 360, textAlign: 'center',
        fontFamily: 'var(--font-display, Georgia, serif)',
        fontSize: 18, fontStyle: 'italic',
        color: TC.ink, lineHeight: 1.35, letterSpacing: '-0.01em',
        minHeight: 56, padding: '0 16px',
      }}>{caption}</div>
    </div>
  );
};

// ─── Mount ─────────────────────────────────────────────────────
const mountTonightPhone = () => {
  const el = document.getElementById('tonight-anim-mount');
  if (!el) return;
  ReactDOM.createRoot(el).render(<TonightAnimatedPhone />);
};
if (document.readyState === 'loading') {
  document.addEventListener('DOMContentLoaded', mountTonightPhone);
} else {
  mountTonightPhone();
}
