// ui_kits/mobile/HomeTab.jsx
// Swipeable home with 5 sub-pages: Home (greeting + hero + stack),
// Activity, Bills, Budgets, Goals.
// Header is shared: GreetingHeader crossfades to TopNavHeader on swipe.

const { useState: useStateHT, useRef: useRefHT, useEffect: useEffectHT } = React;

// ─────────────────────────────────────────────────────────────
// GreetingHeader — avatar + "Hi, Asha" + date · bell on right
// ─────────────────────────────────────────────────────────────
function GreetingHeader({ onOpenProfile, onOpenSettings, hidden, onToggleHidden }) {
  return (
    <div style={{
      display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      padding: '10px 12px 10px 16px', height: 48,
    }}>
      <button onClick={onOpenProfile} aria-label="Open profile" style={{
        display: 'flex', alignItems: 'center', gap: 10,
        background: 'transparent', border: 'none', cursor: 'pointer', padding: 0,
      }}>
        <div style={{
          width: 28, height: 28, borderRadius: 999,
          background: 'var(--goji-gradient)',
          display: 'grid', placeItems: 'center',
          fontFamily: 'var(--font-sans)', fontSize: 12, fontWeight: 600,
          color: 'var(--goji-btn-text)',
          boxShadow: 'inset 0 1px 0 rgba(255, 255, 255, 0.3)',
        }}>A</div>
        <div style={{
          fontFamily: 'var(--font-sans)', fontSize: 14, fontWeight: 500,
          color: 'var(--color-text-primary)',
        }}>Hi, <span style={{ color: 'var(--color-text-body)', fontWeight: 400 }}>Asha</span></div>
      </button>
      <HeaderActions hidden={hidden} onToggleHidden={onToggleHidden} onOpenSettings={onOpenSettings} showBell />
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// TopNavHeader — minimal: back arrow · title · spacer
// ─────────────────────────────────────────────────────────────
function TopNavHeader({ title, onBack, hidden, onToggleHidden }) {
  return (
    <div style={{
      display: 'flex', alignItems: 'center',
      padding: '10px 12px 10px', height: 48,
      position: 'relative',
    }}>
      <button onClick={onBack} aria-label="Back" style={{
        width: 32, height: 32, borderRadius: 999,
        background: 'transparent', border: 'none', cursor: 'pointer',
        color: 'var(--color-text-primary)', display: 'grid', placeItems: 'center',
      }}>
        <Icon name="chevronLeft" size={20} stroke={2} />
      </button>
      <div style={{
        position: 'absolute', left: 0, right: 0, top: 0, bottom: 0,
        display: 'grid', placeItems: 'center',
        pointerEvents: 'none',
      }}>
        <span style={{
          fontFamily: 'var(--font-sans)', fontWeight: 600, fontSize: 14,
          letterSpacing: '-0.01em', color: 'var(--color-text-primary)',
        }}>{title}</span>
      </div>
      <div style={{ marginLeft: 'auto' }}>
        <HeaderActions hidden={hidden} onToggleHidden={onToggleHidden} />
      </div>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// PageDots — iOS-style indicator. Active dot expands to a pill.
// ─────────────────────────────────────────────────────────────
function PageDots({ total, active, onJump }) {
  return (
    <div style={{
      display: 'flex', gap: 6, justifyContent: 'center',
      padding: '0 20px 8px',
    }}>
      {Array.from({ length: total }).map((_, i) => (
        <button key={i}
          onClick={() => onJump(i)}
          aria-label={`Go to page ${i + 1}`}
          style={{
            background: 'transparent', border: 'none', cursor: 'pointer', padding: 4,
          }}>
          <div style={{
            width: i === active ? 18 : 5,
            height: 5,
            borderRadius: 999,
            background: i === active ? 'var(--goji-accent)' : 'var(--color-border-strong)',
            transition: 'width 320ms var(--ease-smooth), background 320ms var(--ease-smooth)',
          }} />
        </button>
      ))}
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// HomeLanding — page 0. Hero + quick actions + stacked cards.
// ─────────────────────────────────────────────────────────────
function HomeLanding({ onOpenInsights, onShowInfo }) {
  return (
    <Page>
      <HeroCard
        amount="84"
        safeToSend={1420}
        dailyAvg={42}
        daysToPayday={4}
        onShowInfo={onShowInfo}
      />

      <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 10, padding: '20px 20px 0' }}>
        <QuickAction icon="send" label="Send" />
        <QuickAction icon="target" label="Set a goal" />
        <QuickAction icon="bill" label="Pay bill" />
      </div>

      <StackedCards
        title="For you"
        action="View all"
        onAction={onOpenInsights}
        cards={[
          { id: 'overdue-utility', priority: 100, tone: 'danger',  eyebrow: 'OVERDUE',                  title: 'Comcast Internet',      body: '2 days late',                       amount: -79.99, cta: 'Pay now' },
          { id: 'bill-rent',       priority: 95,  tone: 'warn',    eyebrow: 'BILLS \u00b7 DUE IN 3 DAYS',title: 'Rent',                   body: 'Scheduled for Jan 1',               amount: -1650, cta: 'Pay early' },
          { id: 'income',          priority: 90,  tone: 'success', eyebrow: 'INCOME',                   title: 'Salary arrived',         body: 'Acme Inc. \u00b7 today',             amount: 4210 },
          { id: 'insight-grocery', priority: 75,  tone: 'insight', eyebrow: 'INSIGHT',                  title: 'Grocery spend up 18%',   body: 'Mostly at Whole Foods this month',  cta: 'See more' },
          { id: 'account-wf',      priority: 50,  tone: 'account', eyebrow: 'WELLS FARGO \u00b7 USD',  title: '$4,218.40',              body: 'Checking \u00b7 +$214 this week',    cta: 'Open' },
          { id: 'fx-npr',          priority: 40,  tone: 'info',    eyebrow: 'FX \u00b7 NPR \u2192 USD',title: 'Good moment to top up',  body: 'NPR up 1.4% this week',             cta: 'Send' },
          { id: 'learn-rem',       priority: 20,  tone: 'learn',   eyebrow: 'LEARN \u00b7 4 MIN',      title: 'Sending money home calmly', body: 'Corridor rates compared',         cta: 'Read' },
        ]}
        onCardAction={() => {}}
      />

      <WeeklyNarrative sent={340} spent={214} saved={126} />
    </Page>
  );
}

// ─────────────────────────────────────────────────────────────
// WeeklyNarrative — Fraunces-led story of the week. Money values
// inline, gold-accent, masked by eye toggle.
// ─────────────────────────────────────────────────────────────
function WeeklyNarrative({ sent, spent, saved }) {
  const hidden = useAmountMask();
  const fmt = (v) => hidden
    ? '$•••'
    : `$${v.toLocaleString('en-US', { minimumFractionDigits: 0, maximumFractionDigits: 0 })}`;

  const total = sent + spent + saved || 1;
  const moments = [
    { key: 'sent',  label: 'Sent home', amount: sent,  color: '#9B7BD6' },
    { key: 'spent', label: 'Spent',     amount: spent, color: '#E0876A' },
    { key: 'saved', label: 'Saved',     amount: saved, color: 'var(--goji-accent)' },
  ];
  const cols = moments.map(m => `${(m.amount / total) * 100}fr`).join(' ');

  return (
    <>
      <SectionTitle action="Learn more">This week</SectionTitle>
      <div style={{ padding: '0 20px' }}>
        {/* Single proportional ribbon — the three flows shown as one breath */}
        <div style={{
          display: 'grid', gridTemplateColumns: cols, gap: 3,
          height: 8, borderRadius: 9999, overflow: 'hidden',
        }}>
          {moments.map((m, i) => (
            <div key={m.key} style={{
              background: m.key === 'saved' ? 'var(--goji-gradient)' : m.color,
              borderRadius: i === 0 ? '9999px 2px 2px 9999px'
                : i === moments.length - 1 ? '2px 9999px 9999px 2px'
                : 2,
              boxShadow: m.key === 'saved' ? '0 0 12px rgba(227, 183, 136, 0.35)' : 'none',
            }} />
          ))}
        </div>

        {/* Three columns aligned to the segments above */}
        <div style={{
          display: 'grid', gridTemplateColumns: cols, gap: 3, marginTop: 12,
        }}>
          {moments.map(m => (
            <div key={m.key} style={{
              display: 'flex', flexDirection: 'column', gap: 4,
              minWidth: 0,
            }}>
              <div style={{
                display: 'flex', alignItems: 'center', gap: 6,
                fontFamily: 'var(--font-sans)', fontSize: 10, fontWeight: 500,
                letterSpacing: '0.10em', textTransform: 'uppercase',
                color: 'var(--color-text-muted)',
                overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap',
              }}>
                <span style={{
                  width: 5, height: 5, borderRadius: 999,
                  background: m.key === 'saved' ? 'var(--goji-accent)' : m.color,
                  flexShrink: 0,
                }} />
                {m.label}
              </div>
              <Money amount={m.amount} size="15px" color="var(--color-text-primary)" />
            </div>
          ))}
        </div>
      </div>
    </>
  );
}

// ─────────────────────────────────────────────────────────────
// FamilyTransfers — horizontal scroll of people. Send-again entry first.
// ─────────────────────────────────────────────────────────────
function FamilyTransfers({ people }) {
  const hidden = useAmountMask();
  const fmt = (v) => hidden ? '$•••' : `$${v}`;
  return (
    <>
      <SectionTitle action="Send" onAction={() => {}}>Family</SectionTitle>
      <div style={{
        display: 'flex', gap: 12, overflowX: 'auto',
        padding: '0 20px 4px',
        scrollbarWidth: 'none',
      }}>
        <button style={{
          flexShrink: 0,
          width: 78,
          background: 'transparent', border: 'none', cursor: 'pointer',
          display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 8,
          padding: 0,
        }}>
          <div style={{
            width: 56, height: 56, borderRadius: 999,
            background:
              'linear-gradient(180deg, rgba(255,255,255,0.06) 0%, rgba(255,255,255,0.01) 100%), rgba(193,160,129,0.08)',
            boxShadow:
              'inset 0 1px 0 rgba(255,255,255,0.10), inset 0 -1px 0 rgba(0,0,0,0.18)',
            backdropFilter: 'blur(12px) saturate(1.3)',
            WebkitBackdropFilter: 'blur(12px) saturate(1.3)',
            color: 'var(--goji-accent)',
            display: 'grid', placeItems: 'center',
          }}>
            <Icon name="plus" size={22} stroke={2.25} />
          </div>
          <span style={{
            fontFamily: 'var(--font-sans)', fontSize: 12, fontWeight: 500,
            color: 'var(--color-text-muted)',
          }}>Send</span>
        </button>

        {people.map(p => (
          <button key={p.id} style={{
            flexShrink: 0,
            width: 78,
            background: 'transparent', border: 'none', cursor: 'pointer',
            display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 6,
            padding: 0,
          }}>
            <div style={{
              width: 56, height: 56, borderRadius: 999,
              background: `${p.tone}28`,
              border: `1px solid ${p.tone}50`,
              color: p.tone,
              display: 'grid', placeItems: 'center',
              fontFamily: 'var(--font-serif)', fontSize: 20, fontWeight: 600,
              letterSpacing: '-0.02em',
              boxShadow: 'inset 0 1px 0 rgba(255,255,255,0.10)',
            }}>{p.initial}</div>
            <span style={{
              fontFamily: 'var(--font-sans)', fontSize: 12, fontWeight: 500,
              color: 'var(--color-text-primary)',
              maxWidth: 76, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap',
            }}>{p.name}</span>
            <span style={{
              fontFamily: 'var(--font-sans)', fontSize: 11,
              color: 'var(--color-text-muted)',
              fontVariantNumeric: 'tabular-nums',
              fontFeatureSettings: '"tnum" 1',
            }}>{fmt(p.lastAmount)} · {p.lastWhen}</span>
          </button>
        ))}
      </div>
    </>
  );
}

// ─────────────────────────────────────────────────────────────
// ActivityScreen — grouped transaction history
// ─────────────────────────────────────────────────────────────
function ActivityScreen() {
  return (
    <Page>
      <div style={{ padding: '4px 20px 0', display: 'flex', gap: 8, overflowX: 'auto' }}>
        <Chip active>All</Chip>
        <Chip color="#E0876A">Food</Chip>
        <Chip color="#9B7BD6">Family</Chip>
        <Chip color="#7E6896">Bills</Chip>
        <Chip color="#6FCF97">Income</Chip>
      </div>

      <SectionTitle>Today</SectionTitle>
      <div style={{
        margin: '0 20px',
        background: 'var(--color-surface)',
        border: '1px solid var(--color-border)',
        borderRadius: 'var(--radius-card)',
        boxShadow: 'var(--shadow-premium)',
        overflow: 'hidden',
      }}>
        <TransactionRow icon="coffee" color="#E0876A" name="Blue Bottle Coffee" meta="9:41 AM" amount={-6.50} />
        <TransactionRow icon="family" color="#9B7BD6" name="Sent to Mom" meta="Family" amount={-340} divider={false} />
      </div>

      <SectionTitle>Yesterday</SectionTitle>
      <div style={{
        margin: '0 20px',
        background: 'var(--color-surface)',
        border: '1px solid var(--color-border)',
        borderRadius: 'var(--radius-card)',
        boxShadow: 'var(--shadow-premium)',
        overflow: 'hidden',
      }}>
        <TransactionRow icon="coffee" color="#E0876A" name="Whole Foods Market" meta="Groceries" amount={-42.18} divider={false} />
      </div>

      <SectionTitle>Mon</SectionTitle>
      <div style={{
        margin: '0 20px',
        background: 'var(--color-surface)',
        border: '1px solid var(--color-border)',
        borderRadius: 'var(--radius-card)',
        boxShadow: 'var(--shadow-premium)',
        overflow: 'hidden',
      }}>
        <TransactionRow icon="bill" color="#7E6896" name="Comcast Internet" meta="Bills" amount={-79.99} />
        <TransactionRow icon="salary" color="#6FCF97" name="Salary — Acme Inc." meta="Income" amount={4210} positive />
        <TransactionRow icon="home2" color="#7E6896" name="Rent" meta="Housing" amount={-1650} divider={false} />
      </div>
    </Page>
  );
}

// ─────────────────────────────────────────────────────────────
// BillsScreen — upcoming bills, grouped by urgency
// ─────────────────────────────────────────────────────────────
function BillsScreen() {
  return (
    <Page>
      <div style={{ padding: '4px 20px 0' }}>
        <div style={{
          background: 'var(--color-surface)',
          border: '1px solid var(--color-border)',
          borderRadius: 'var(--radius-card)',
          padding: '20px 22px',
          boxShadow: 'var(--shadow-premium)',
        }}>
          <div style={{
            fontFamily: 'var(--font-sans)', fontSize: 11, fontWeight: 500,
            letterSpacing: '0.12em', textTransform: 'uppercase',
            color: 'var(--color-text-muted)', marginBottom: 6,
          }}>Due this month</div>
          <Money amount={-1740.98} size="32px" color="var(--color-text-primary)" />
          <div style={{ fontFamily: 'var(--font-sans)', fontSize: 12, color: 'var(--color-text-muted)', marginTop: 6 }}>
            Across 3 bills · 1 overdue
          </div>
        </div>
      </div>

      <SectionTitle>Overdue</SectionTitle>
      <div style={{ padding: '0 20px' }}>
        <BillRow tone="danger" name="Comcast Internet" meta="Was due Jan 8 · 2 days late" amount={-79.99} cta="Pay now" />
      </div>

      <SectionTitle>This week</SectionTitle>
      <div style={{ padding: '0 20px', display: 'flex', flexDirection: 'column', gap: 10 }}>
        <BillRow tone="warn" name="Rent" meta="Scheduled · Jan 1" amount={-1650} cta="Pay early" />
      </div>

      <SectionTitle>Later this month</SectionTitle>
      <div style={{ padding: '0 20px', display: 'flex', flexDirection: 'column', gap: 10 }}>
        <BillRow tone="info" name="Spotify Family" meta="Auto · Jan 22" amount={-10.99} />
      </div>
    </Page>
  );
}

function BillRow({ tone, name, meta, amount, cta }) {
  const t = CARD_TONES[tone] || CARD_TONES.account;
  return (
    <div style={{
      background: 'var(--color-surface)',
      border: `1px solid ${t.border}`,
      borderRadius: 'var(--radius-card)',
      padding: '14px 16px',
      boxShadow: 'var(--shadow-premium)',
      display: 'flex', alignItems: 'center', gap: 12,
      position: 'relative', overflow: 'hidden',
    }}>
      <div style={{
        position: 'absolute', top: -40, right: -40, width: 140, height: 140,
        background: `radial-gradient(circle, ${t.glow} 0%, transparent 70%)`,
        pointerEvents: 'none',
      }} />
      <div style={{
        position: 'relative',
        width: 38, height: 38, borderRadius: 12,
        background: `${t.dot}1a`, border: `1px solid ${t.dot}40`,
        color: t.fg, display: 'grid', placeItems: 'center',
      }}>
        <Icon name="bill" size={18} />
      </div>
      <div style={{ position: 'relative', flex: 1, minWidth: 0 }}>
        <div style={{ fontFamily: 'var(--font-sans)', fontSize: 14, fontWeight: 600, color: 'var(--color-text-primary)' }}>{name}</div>
        <div style={{ fontFamily: 'var(--font-sans)', fontSize: 12, color: 'var(--color-text-muted)', marginTop: 2 }}>{meta}</div>
      </div>
      <div style={{ position: 'relative', display: 'flex', flexDirection: 'column', alignItems: 'flex-end', gap: 6 }}>
        <Money amount={amount} sign size="15px" color={tone === 'danger' ? '#EB5757' : 'var(--color-text-primary)'} />
        {cta && (
          <button style={{
            background: 'var(--color-input-bg)', border: '1px solid var(--color-border-strong)',
            borderRadius: 9999, padding: '5px 12px',
            fontFamily: 'var(--font-sans)', fontSize: 11, fontWeight: 600,
            color: 'var(--color-text-primary)', cursor: 'pointer', whiteSpace: 'nowrap',
          }}>{cta}</button>
        )}
      </div>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// BudgetsScreen — category budgets with progress bars
// ─────────────────────────────────────────────────────────────
function BudgetsScreen() {
  const budgets = [
    { name: 'Groceries',        spent: 324, cap: 500, color: '#E0876A' },
    { name: 'Coffee & dining',  spent: 48,  cap: 80,  color: '#D4AA45' },
    { name: 'Transport',        spent: 112, cap: 200, color: '#7DBEB7' },
    { name: 'Subscriptions',    spent: 56,  cap: 60,  color: '#9B7BD6' },
    { name: 'Family transfers', spent: 340, cap: 400, color: '#C17A74' },
  ];
  return (
    <Page>
      <div style={{ padding: '4px 20px 0' }}>
        <div style={{
          background: 'var(--color-surface)',
          border: '1px solid var(--color-border)',
          borderRadius: 'var(--radius-card)',
          padding: '20px 22px',
          boxShadow: 'var(--shadow-premium)',
        }}>
          <div style={{
            fontFamily: 'var(--font-sans)', fontSize: 11, fontWeight: 500,
            letterSpacing: '0.12em', textTransform: 'uppercase',
            color: 'var(--color-text-muted)', marginBottom: 6,
          }}>This month</div>
          <Money amount={-880} size="32px" color="var(--color-text-primary)" />
          <div style={{ fontFamily: 'var(--font-sans)', fontSize: 12, color: 'var(--color-text-muted)', marginTop: 6 }}>
            <Money amount={1240} size="12px" color="var(--color-text-body)" /> left across all budgets
          </div>
        </div>
      </div>

      <SectionTitle action="Edit">Categories</SectionTitle>
      <div style={{ padding: '0 20px', display: 'flex', flexDirection: 'column', gap: 12 }}>
        {budgets.map(b => {
          const pct = Math.min(100, (b.spent / b.cap) * 100);
          const overWarn = pct >= 85;
          return (
            <div key={b.name} style={{
              background: 'var(--color-surface)',
              border: '1px solid var(--color-border)',
              borderRadius: 'var(--radius-card)',
              padding: '14px 18px',
              boxShadow: 'var(--shadow-premium)',
            }}>
              <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 10 }}>
                <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
                  <span style={{ width: 8, height: 8, borderRadius: 999, background: b.color }} />
                  <span style={{ fontFamily: 'var(--font-sans)', fontSize: 14, fontWeight: 500, color: 'var(--color-text-primary)' }}>{b.name}</span>
                </div>
                <div style={{ fontFamily: 'var(--font-sans)', fontSize: 13, color: 'var(--color-text-body)', fontFeatureSettings: '"tnum" 1' }}>
                  <span style={{ color: overWarn ? '#F2C94C' : 'var(--color-text-primary)', fontWeight: 600 }}>${b.spent}</span>
                  <span style={{ color: 'var(--color-text-muted)' }}> of ${b.cap}</span>
                </div>
              </div>
              <div style={{
                height: 6, borderRadius: 999,
                background: 'var(--color-input-bg)',
                overflow: 'hidden', position: 'relative',
              }}>
                <div style={{
                  height: '100%', width: `${pct}%`,
                  background: overWarn ? 'linear-gradient(90deg, #C1A081 0%, #F2C94C 100%)' : b.color,
                  borderRadius: 999,
                  transition: 'width 600ms var(--ease-smooth)',
                }} />
              </div>
            </div>
          );
        })}
      </div>
    </Page>
  );
}

// ─────────────────────────────────────────────────────────────
// GoalsScreen — saving goals with ring progress
// ─────────────────────────────────────────────────────────────
function GoalsScreen() {
  const goals = [
    { name: 'Emergency fund',  saved: 3200, target: 5000, eta: 'Sep 2026',  icon: 'target' },
    { name: 'Trip home',       saved: 890,  target: 2500, eta: 'Mar 2027',  icon: 'family' },
    { name: 'New laptop',      saved: 1200, target: 1800, eta: 'Aug 2026',  icon: 'sparkle' },
  ];
  return (
    <Page>
      <div style={{ padding: '4px 20px 0', display: 'flex', flexDirection: 'column', gap: 14 }}>
        {goals.map(g => {
          const pct = Math.round((g.saved / g.target) * 100);
          return (
            <div key={g.name} style={{
              background: 'var(--color-surface)',
              border: '1px solid var(--color-border)',
              borderRadius: 'var(--radius-card)',
              padding: '20px 20px',
              boxShadow: 'var(--shadow-premium)',
              display: 'flex', alignItems: 'center', gap: 18,
            }}>
              <ProgressRing pct={pct} />
              <div style={{ flex: 1, minWidth: 0 }}>
                <div style={{ fontFamily: 'var(--font-sans)', fontSize: 11, color: 'var(--color-text-muted)', letterSpacing: '0.12em', textTransform: 'uppercase' }}>
                  {pct}% complete
                </div>
                <div style={{ fontFamily: 'var(--font-sans)', fontSize: 15, fontWeight: 600, color: 'var(--color-text-primary)', letterSpacing: '-0.01em', marginTop: 4 }}>
                  {g.name}
                </div>
                <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', marginTop: 6, gap: 8 }}>
                  <Money amount={g.saved} size="14px" color="var(--color-text-primary)" />
                  <span style={{ fontFamily: 'var(--font-sans)', fontSize: 12, color: 'var(--color-text-muted)' }}>of <Money amount={g.target} size="12px" color="var(--color-text-body)" /> · {g.eta}</span>
                </div>
              </div>
            </div>
          );
        })}
        <button style={{
          background: 'transparent',
          border: '1px dashed var(--color-border-strong)',
          borderRadius: 'var(--radius-card)',
          padding: '18px',
          fontFamily: 'var(--font-sans)', fontSize: 14, fontWeight: 500,
          color: 'var(--goji-accent)', cursor: 'pointer',
          display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8,
        }}>
          <Icon name="plus" size={16} stroke={2.25} />
          Set a new goal
        </button>
      </div>
    </Page>
  );
}

function ProgressRing({ pct, size = 56 }) {
  const stroke = 5;
  const r = (size - stroke) / 2;
  const c = 2 * Math.PI * r;
  const offset = c * (1 - pct / 100);
  return (
    <svg width={size} height={size} style={{ flexShrink: 0 }}>
      <defs>
        <linearGradient id={`pr-${pct}`} x1="0" y1="0" x2="1" y2="1">
          <stop offset="0%" stopColor="#9B7558"/>
          <stop offset="100%" stopColor="#E3B788"/>
        </linearGradient>
      </defs>
      <circle cx={size / 2} cy={size / 2} r={r}
        stroke="var(--color-input-bg)" strokeWidth={stroke} fill="none" />
      <circle cx={size / 2} cy={size / 2} r={r}
        stroke={`url(#pr-${pct})`} strokeWidth={stroke} fill="none"
        strokeDasharray={c} strokeDashoffset={offset}
        strokeLinecap="round"
        transform={`rotate(-90 ${size / 2} ${size / 2})`}
        style={{ transition: 'stroke-dashoffset 700ms var(--ease-smooth)' }}
      />
    </svg>
  );
}

// ─────────────────────────────────────────────────────────────
// HomeTabContainer — pages between the 5 home screens.
// Header crossfades between Greeting (page 0) and TopNav (pages 1-4).
// ─────────────────────────────────────────────────────────────
const HOME_PAGES = [
  { id: 'home',     title: '' },                // greeting header
  { id: 'activity', title: 'Activity' },
  { id: 'bills',    title: 'Bills' },
  { id: 'budgets',  title: 'Budgets' },
  { id: 'goals',    title: 'Goals' },
];

function HomeTabContainer({ onOpenInsights, onOpenProfile, onOpenSettings, onShowInfo, hidden, onToggleHidden }) {
  const [page, setPage] = useStateHT(0);

  const isHome = page === 0;
  const currentTitle = HOME_PAGES[page].title;

  return (
    <div style={{ flex: 1, minHeight: 0, display: 'flex', flexDirection: 'column' }}>
      {/* Shared header — crossfades between greeting and top-nav */}
      <div style={{ position: 'relative', height: 48, flexShrink: 0 }}>
        <div style={{
          position: 'absolute', inset: 0,
          opacity: isHome ? 1 : 0,
          transform: isHome ? 'translateY(0)' : 'translateY(-4px)',
          transition: 'opacity 280ms var(--ease-smooth), transform 320ms var(--ease-smooth)',
          pointerEvents: isHome ? 'auto' : 'none',
        }}>
          <GreetingHeader
            onOpenProfile={onOpenProfile}
            onOpenSettings={onOpenSettings}
            hidden={hidden}
            onToggleHidden={onToggleHidden}
          />
        </div>
        <div style={{
          position: 'absolute', inset: 0,
          opacity: isHome ? 0 : 1,
          transform: isHome ? 'translateY(4px)' : 'translateY(0)',
          transition: 'opacity 280ms var(--ease-smooth), transform 320ms var(--ease-smooth)',
          pointerEvents: isHome ? 'none' : 'auto',
        }}>
          <TopNavHeader
            title={currentTitle}
            onBack={() => setPage(0)}
            hidden={hidden}
            onToggleHidden={onToggleHidden}
          />
        </div>
      </div>

      <div style={{
        height: 21,
        opacity: isHome ? 0 : 1,
        transition: 'opacity 220ms var(--ease-smooth)',
        flexShrink: 0,
      }}>
        <PageDots total={HOME_PAGES.length} active={page} onJump={setPage} />
      </div>

      <SwipePager page={page} onPageChange={setPage}>
        <HomeLanding onOpenInsights={onOpenInsights} onShowInfo={onShowInfo} />
        <ActivityScreen />
        <BillsScreen />
        <BudgetsScreen />
        <GoalsScreen />
      </SwipePager>
    </div>
  );
}

Object.assign(window, {
  HomeTabContainer,
  GreetingHeader, TopNavHeader, PageDots, ProgressRing, BillRow,
  HomeLanding, ActivityScreen, BillsScreen, BudgetsScreen, GoalsScreen,
});
