// Phone bezel + static recreations of the *current* (iOS 26 Liquid Glass) Oona screens.
// Ported from elliott99ukhb/Oona/ios-native — SwiftUI screens recreated faithfully in HTML.
// Exposes: PhoneFrame, PhoneHome, PhoneHistory, PhoneInsights

const { Plus, Home, History: HistoryIcon, Insights: InsightsIcon, Family: FamilyIcon,
        Bell, Bottle, Moon, Droplet, Note, Pump, ChevronDown, ChevronRight, More,
        PauseIcon, StopIcon, ArrowLeft, Settings, Check } = window.Icons;

const EVT = {
  feed:  { c: '#EBC39E', s: '#F7E6D4', label: 'Feed',  ill: 'bottle' },
  sleep: { c: '#B8A9D1', s: '#E8E1F0', label: 'Sleep', ill: 'moon'   },
  nappy: { c: '#B8BEAA', s: '#E2E4D8', label: 'Nappy', ill: 'nappy'  },
  pump:  { c: '#D9B7C6', s: '#F1E0E7', label: 'Pump',  ill: 'pump'   },
  note:  { c: '#D8CABE', s: '#ECE3D8', label: 'Note',  ill: 'notebook' },
};

// ─── Phone bezel ─────────────────────────────────────────────
function PhoneFrame({ children, scale = 1, style }) {
  const W = 360, H = 760;
  return (
    <div style={{
      width: W * scale, height: H * scale,
      borderRadius: 44 * scale, padding: 6 * scale,
      background: 'linear-gradient(160deg, #2A2530 0%, #16131B 100%)',
      boxShadow: `0 ${30 * scale}px ${60 * scale}px rgba(40, 30, 50, 0.18), 0 ${4 * scale}px ${10 * scale}px rgba(40,30,50,0.08), inset 0 0 0 1px rgba(255,255,255,0.05)`,
      ...style,
    }}>
      <div style={{
        width: '100%', height: '100%', borderRadius: 38 * scale,
        background: 'var(--bg-app)', overflow: 'hidden', position: 'relative',
        boxShadow: 'inset 0 0 0 1px rgba(255,255,255,0.04)',
      }}>
        <div style={{
          transform: `scale(${scale})`, transformOrigin: 'top left',
          width: W, height: H, position: 'relative',
        }}>
          {/* Status bar */}
          <div style={{
            position: 'absolute', top: 0, left: 0, right: 0, zIndex: 5,
            display: 'flex', alignItems: 'center', justifyContent: 'space-between',
            padding: '18px 32px 0', height: 50,
            font: '600 15px/1 -apple-system, "SF Pro", system-ui', color: 'var(--fg-1)',
          }}>
            <span>9:41</span>
            <span style={{ display: 'flex', alignItems: 'center', gap: 6, opacity: 0.7 }}>
              <svg width="16" height="10" viewBox="0 0 16 10"><rect x="0" y="6" width="3" height="4" rx="0.7" fill="currentColor"/><rect x="4" y="4" width="3" height="6" rx="0.7" fill="currentColor"/><rect x="8" y="2" width="3" height="8" rx="0.7" fill="currentColor"/><rect x="12" y="0" width="3" height="10" rx="0.7" fill="currentColor"/></svg>
              <svg width="22" height="10" viewBox="0 0 22 10"><rect x="0" y="0" width="19" height="10" rx="3" fill="none" stroke="currentColor" strokeOpacity="0.35"/><rect x="2" y="2" width="14" height="6" rx="1.5" fill="currentColor"/></svg>
            </span>
          </div>
          {/* Dynamic island */}
          <div style={{
            position: 'absolute', top: 9, left: '50%', transform: 'translateX(-50%)',
            width: 110, height: 30, borderRadius: 20, background: '#000', zIndex: 8,
          }}/>
          {children}
          {/* Home indicator */}
          <div style={{
            position: 'absolute', bottom: 7, left: '50%', transform: 'translateX(-50%)',
            width: 110, height: 4, borderRadius: 2, background: 'rgba(47,42,54,0.30)', zIndex: 10,
          }}/>
        </div>
      </div>
    </div>
  );
}

// ─── Liquid Glass tab bar (iOS 26 native style — no FAB) ─────
function LiquidGlassTabBar({ active = 'today' }) {
  const items = [
    { id: 'today',    Icon: Home,         label: 'Today' },
    { id: 'history',  Icon: HistoryIcon,  label: 'History' },
    { id: 'insights', Icon: InsightsIcon, label: 'Insights' },
    { id: 'family',   Icon: FamilyIcon,   label: 'Family' },
  ];
  return (
    <div style={{
      position: 'absolute', left: 14, right: 14, bottom: 18, zIndex: 30,
    }}>
      <div style={{
        background: 'rgba(255,249,244,0.55)',
        backdropFilter: 'blur(30px) saturate(180%)',
        WebkitBackdropFilter: 'blur(30px) saturate(180%)',
        borderRadius: 28, padding: '6px 8px',
        display: 'flex', alignItems: 'center',
        boxShadow: '0 10px 30px rgba(75,60,75,0.10), inset 0 1px 0 rgba(255,255,255,0.6)',
        border: '0.5px solid rgba(255,255,255,0.4)',
      }}>
        {items.map(it => <TabBarItem key={it.id} {...it} active={active===it.id}/>)}
      </div>
    </div>
  );
}
function TabBarItem({ Icon, label, active }) {
  const c = active ? 'var(--oona-primary)' : 'var(--fg-2)';
  return (
    <div style={{
      flex: 1, display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 3,
      color: c, font: `${active ? 600 : 500} 10px/1 "DM Sans"`,
      padding: '8px 0 6px',
    }}>
      <Icon size={22} strokeWidth={active ? 2.2 : 1.75}/>
      <span>{label}</span>
    </div>
  );
}

// ─── Baby header (top bar) ───────────────────────────────────
function BabyHeader() {
  return (
    <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
      <div style={{ width: 28, height: 28, borderRadius: 999, background: '#EBC39E', display: 'flex', alignItems: 'center', justifyContent: 'center', font: '600 12px/1 "Newsreader",serif', color: '#5F567A' }}>W</div>
      <div>
        <div style={{ font: '600 13px/1.1 "DM Sans"' }}>Wren</div>
        <div style={{ font: '400 10px/1.1 "DM Sans"', color: 'var(--fg-2)' }}>4 mo 12 d</div>
      </div>
      <ChevronDown size={14} style={{ color: 'var(--fg-2)' }}/>
    </div>
  );
}

// ─── HOME (with Quick Start tiles) ───────────────────────────
function PhoneHome() {
  const events = [
    { type: 'feed',  title: 'Feed · left, 14 min',  meta: '07:42',  time: '23m', by: 'Mum' },
    { type: 'nappy', title: 'Nappy · wet',          meta: '07:15',  time: '50m', by: 'Dad' },
    { type: 'sleep', title: 'Nap · 1h 24m',         meta: '05:50',  time: '2h',  by: 'auto' },
  ];
  return (
    <div style={{ paddingTop: 50, paddingBottom: 90 }}>
      {/* Top bar */}
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '12px 18px 8px' }}>
        <BabyHeader/>
        <More size={20} style={{ color: 'var(--fg-1)' }}/>
      </div>

      {/* Greeting */}
      <div style={{ padding: '8px 18px 6px' }}>
        <div style={{ font: '400 13px/1.4 "DM Sans"', color: 'var(--fg-2)' }}>Morning.</div>
        <div style={{ font: '500 28px/1.1 "Newsreader",serif', letterSpacing: '-0.01em', marginTop: 4 }}>Today, thursday.</div>
      </div>

      {/* Compact "since last feed" line */}
      <div style={{ padding: '4px 18px 14px', font: '500 12px/1.3 "DM Sans"', color: 'var(--fg-2)' }}>
        Last fed 23m ago <span style={{ color: 'var(--fg-3)', fontWeight: 400 }}>· at 07:42</span>
      </div>

      {/* 2×2 quick tiles */}
      <div style={{ padding: '0 18px', display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8 }}>
        <QuickTile type="feed"/>
        <QuickTile type="sleep" running="00:23:47"/>
        <QuickTile type="pump"/>
        <QuickTile type="nappy"/>
      </div>

      {/* Add a note */}
      <div style={{ padding: '10px 18px 0' }}>
        <div style={{
          background: 'var(--bg-card)', borderRadius: 14, padding: '8px 12px',
          display: 'flex', alignItems: 'center', gap: 10,
          boxShadow: 'var(--shadow-1)',
        }}>
          <div style={{ width: 26, height: 26, borderRadius: 8, background: 'var(--bg-sunken)', display: 'flex', alignItems: 'center', justifyContent: 'center', color: 'var(--oona-primary-deep)' }}><Note size={14}/></div>
          <span style={{ flex: 1, font: '500 13px/1 "DM Sans"' }}>Add a note</span>
          <ChevronRight size={14} style={{ color: 'var(--fg-2)' }}/>
        </div>
      </div>

      {/* Today's log */}
      <div style={{ padding: '16px 18px 0' }}>
        <div style={{ font: '600 10px/1 "DM Sans"', textTransform: 'uppercase', letterSpacing: '0.08em', color: 'var(--fg-2)', margin: '0 0 8px' }}>Today's log</div>
        <div style={{ display: 'flex', flexDirection: 'column', gap: 6 }}>
          {events.map((ev, i) => <EventRow key={i} ev={ev}/>)}
        </div>
      </div>

      <LiquidGlassTabBar active="today"/>
    </div>
  );
}

function QuickTile({ type, running }) {
  const m = EVT[type];
  const isRunning = !!running;
  return (
    <div style={{
      background: m.s, borderRadius: 18, padding: 14,
      minHeight: 116,
      display: 'flex', flexDirection: 'column',
      border: isRunning ? `2px solid ${m.c}` : '2px solid transparent',
      boxShadow: 'var(--shadow-1)',
      position: 'relative',
    }}>
      <div style={{ display: 'flex', alignItems: 'flex-start', justifyContent: 'space-between' }}>
        <div style={{
          width: 44, height: 44, borderRadius: 12, background: 'rgba(255,249,244,0.7)',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
        }}>
          <img src={`assets/illustration-${m.ill}.svg`} alt="" style={{ width: 32, height: 32 }}/>
        </div>
        {isRunning && <LivePill/>}
      </div>
      <div style={{ flex: 1 }}/>
      <div style={{ font: '600 17px/1.1 "Newsreader",serif', color: 'var(--fg-1)', letterSpacing: '-0.01em' }}>
        {isRunning ? `End ${m.label.toLowerCase()}` : m.label}
      </div>
      <div style={{
        font: isRunning ? '500 13px/1 "DM Mono",monospace' : '400 11px/1.3 "DM Sans"',
        color: isRunning ? 'var(--oona-primary-deep)' : 'var(--fg-2)',
        marginTop: 3,
      }}>
        {isRunning ? running : (type === 'nappy' ? 'Wet · dirty · both' : 'Tap to start')}
      </div>
    </div>
  );
}

function LivePill() {
  return (
    <div style={{
      display: 'flex', alignItems: 'center', gap: 4,
      background: 'var(--bg-card)', borderRadius: 999, padding: '2px 8px',
      border: '1px solid rgba(126,115,159,0.3)',
    }}>
      <div style={{ width: 6, height: 6, borderRadius: 999, background: 'var(--oona-primary)' }}/>
      <span style={{ font: '600 9px/1 "DM Sans"', color: 'var(--oona-primary)', letterSpacing: '0.08em' }}>LIVE</span>
    </div>
  );
}

function EventRow({ ev }) {
  const m = EVT[ev.type];
  return (
    <div style={{
      position: 'relative', background: 'var(--bg-card)', borderRadius: 14, padding: '10px 12px 10px 16px',
      display: 'flex', alignItems: 'center', gap: 10, overflow: 'hidden',
      boxShadow: 'var(--shadow-1)',
    }}>
      <div style={{ position: 'absolute', left: 0, top: 0, bottom: 0, width: 3, background: m.c }}/>
      <div style={{ width: 28, height: 28, borderRadius: 8, background: m.s, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
        <img src={`assets/illustration-${m.ill}.svg`} alt="" style={{ width: 20, height: 20 }}/>
      </div>
      <div style={{ flex: 1, minWidth: 0 }}>
        <div style={{ font: '500 12px/1.3 "DM Sans"' }}>{ev.title}</div>
        <div style={{ font: '400 10px/1.3 "DM Sans"', color: 'var(--fg-2)', marginTop: 1 }}>{ev.meta} · {ev.by}</div>
      </div>
      <div style={{ font: '500 11px/1 "DM Mono",monospace', color: 'var(--fg-2)' }}>{ev.time}</div>
    </div>
  );
}

// ─── HISTORY ─────────────────────────────────────────────────
function PhoneHistory() {
  const groups = [
    { day: 'Today',     date: 'Thu 14 May', events: [
      { type: 'feed', title: 'Feed · left, 14 min', meta: '07:42', time: '23m', by: 'Mum' },
      { type: 'nappy', title: 'Nappy · wet',         meta: '07:15', time: '50m', by: 'Dad' },
      { type: 'sleep', title: 'Nap · 1h 24m',        meta: '05:50–07:14', time: '2h', by: 'auto' },
    ]},
    { day: 'Yesterday', date: 'Wed 13 May', events: [
      { type: 'pump', title: 'Pump · 110 ml',     meta: '22:40', time: 'yest', by: 'Mum' },
      { type: 'note', title: '"First giggle."',   meta: '19:30', time: 'yest', by: 'Mum' },
    ]},
  ];

  return (
    <div style={{ paddingTop: 50, paddingBottom: 90 }}>
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '12px 18px 8px' }}>
        <div style={{ width: 20 }}/>
        <div style={{ font: '600 15px/1 "DM Sans"' }}>History</div>
        <Settings size={18} style={{ color: 'var(--fg-1)' }}/>
      </div>

      <div style={{ padding: '4px 18px 12px', display: 'flex', gap: 6, overflow: 'hidden' }}>
        {[['All', true],['Feeds'],['Sleep'],['Nappies'],['Pumps']].map(([t, a], i) => (
          <div key={i} style={{
            font: `${a ? 600 : 500} 11px/1 "DM Sans"`, padding: '7px 12px', borderRadius: 999,
            background: a ? 'var(--oona-primary)' : 'var(--bg-card)',
            color: a ? 'var(--oona-text-on-primary)' : 'var(--fg-1)',
            border: a ? 0 : '1px solid var(--border-1)', flexShrink: 0,
          }}>{t}</div>
        ))}
      </div>

      {groups.map(g => (
        <div key={g.day} style={{ padding: '0 18px 4px' }}>
          <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'space-between', margin: '12px 0 8px' }}>
            <span style={{ font: '500 16px/1.2 "Newsreader",serif' }}>{g.day}</span>
            <span style={{ font: '400 11px/1 "DM Sans"', color: 'var(--fg-2)' }}>{g.date}</span>
          </div>
          <div style={{ display: 'flex', flexDirection: 'column', gap: 6 }}>
            {g.events.map((ev, i) => <EventRow key={i} ev={ev}/>)}
          </div>
        </div>
      ))}
      <LiquidGlassTabBar active="history"/>
    </div>
  );
}

// ─── INSIGHTS / REPORTS ──────────────────────────────────────
function PhoneInsights() {
  const feeds = [6, 5, 7, 6, 8, 6, 7];
  const sleep = [11, 9, 12, 10, 11, 11, 10];
  const days = ['M','T','W','T','F','S','S'];

  const sleepMax = Math.max(...sleep);
  const sleepPath = sleep.map((v, i) => {
    const x = (i / (sleep.length - 1)) * 280;
    const y = 70 - (v / sleepMax) * 60 - 5;
    return `${i ? 'L' : 'M'}${x},${y}`;
  }).join(' ');

  return (
    <div style={{ paddingTop: 50, paddingBottom: 90 }}>
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '12px 18px 8px' }}>
        <div style={{ width: 20 }}/>
        <div style={{ font: '600 15px/1 "DM Sans"' }}>Insights</div>
        <More size={18} style={{ color: 'var(--fg-1)' }}/>
      </div>

      <div style={{ padding: '12px 18px', display: 'flex', flexDirection: 'column', gap: 12 }}>
        {/* Feeds bar card */}
        <div style={{ background: 'var(--bg-card)', borderRadius: 18, padding: 16, boxShadow: 'var(--shadow-1)' }}>
          <div style={{ font: '600 9px/1 "DM Sans"', textTransform: 'uppercase', letterSpacing: '0.08em', color: 'var(--fg-2)' }}>Feeds · last 7 days</div>
          <div style={{ font: '500 22px/1 "Newsreader",serif', marginTop: 6 }}>45 total</div>
          <div style={{ display: 'flex', alignItems: 'flex-end', gap: 6, height: 70, marginTop: 14 }}>
            {feeds.map((v, i) => (
              <div key={i} style={{ flex: 1, display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 4 }}>
                <div style={{ width: '100%', height: `${(v / Math.max(...feeds)) * 100}%`, background: '#EBC39E', borderRadius: '4px 4px 1px 1px' }}/>
                <div style={{ font: '500 9px/1 "DM Sans"', color: 'var(--fg-2)' }}>{days[i]}</div>
              </div>
            ))}
          </div>
        </div>

        {/* Sleep line card */}
        <div style={{ background: 'var(--bg-card)', borderRadius: 18, padding: 16, boxShadow: 'var(--shadow-1)' }}>
          <div style={{ font: '600 9px/1 "DM Sans"', textTransform: 'uppercase', letterSpacing: '0.08em', color: 'var(--fg-2)' }}>Sleep · hours per day</div>
          <div style={{ font: '500 22px/1 "Newsreader",serif', marginTop: 6 }}>10h 40m average</div>
          <svg viewBox="0 0 280 80" style={{ width: '100%', marginTop: 8 }}>
            <path d={`${sleepPath} L280,80 L0,80 Z`} fill="#B8A9D1" opacity="0.25"/>
            <path d={sleepPath} fill="none" stroke="#B8A9D1" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round"/>
            {sleep.map((v, i) => {
              const x = (i / (sleep.length - 1)) * 280;
              const y = 70 - (v / sleepMax) * 60 - 5;
              return <circle key={i} cx={x} cy={y} r="3.5" fill="#B8A9D1"/>;
            })}
          </svg>
          <div style={{ display: 'flex', justifyContent: 'space-between', marginTop: 2, font: '500 9px/1 "DM Sans"', color: 'var(--fg-2)' }}>
            {days.map((d, i) => <span key={i}>{d}</span>)}
          </div>
        </div>

        {/* Prose insight card */}
        <div style={{ background: 'var(--bg-card)', borderRadius: 18, padding: 16, boxShadow: 'var(--shadow-1)', display: 'flex', gap: 12, alignItems: 'flex-start' }}>
          <img src="assets/illustration-leaf.svg" alt="" style={{ width: 44, height: 44, flexShrink: 0 }}/>
          <div>
            <div style={{ font: '500 14px/1.3 "Newsreader",serif' }}>Sleep totals are landing.</div>
            <div style={{ font: '400 11px/1.45 "DM Sans"', color: 'var(--fg-2)', marginTop: 4 }}>
              45 feeds and roughly 75h of sleep across the last 7 days. Open a day in History for the full picture.
            </div>
          </div>
        </div>
      </div>
      <LiquidGlassTabBar active="insights"/>
    </div>
  );
}

Object.assign(window, { PhoneFrame, PhoneHome, PhoneHistory, PhoneInsights, LiquidGlassTabBar });

// Keep old PhoneFeed export as alias in case anything still imports it
window.PhoneFeed = PhoneInsights;
