// ui_kits/mobile/MoneyTab.jsx
// Money tab with three swipeable sub-screens: Summary, FX rates, Household.

const { useState: useStateMT } = React;

const MONEY_SUB_PAGES = [
  { key: 'summary',   label: 'Summary' },
  { key: 'accounts',  label: 'Accounts' },
  { key: 'fx',        label: 'FX rates' },
  { key: 'household', label: 'Household' },
];

function MoneyTabContainer({ hidden, onToggleHidden }) {
  const [page, setPage] = useStateMT(0);
  const isLanding = page === 0;
  const currentTitle = MONEY_SUB_PAGES[page].label;
  return (
    <div style={{ flex: 1, minHeight: 0, display: 'flex', flexDirection: 'column' }}>
      {/* Crossfading header — Money title on landing, back+title on sub-pages */}
      <div style={{ position: 'relative', height: 48, flexShrink: 0 }}>
        <div style={{
          position: 'absolute', inset: 0,
          opacity: isLanding ? 1 : 0,
          transform: isLanding ? 'translateY(0)' : 'translateY(-4px)',
          transition: 'opacity 280ms var(--ease-smooth), transform 320ms var(--ease-smooth)',
          pointerEvents: isLanding ? 'auto' : 'none',
        }}>
          <div style={{
            display: 'flex', alignItems: 'center',
            padding: '10px 12px 10px 20px', height: 48,
          }}>
            <span style={{
              fontFamily: 'var(--font-sans)', fontWeight: 600, fontSize: 14,
              letterSpacing: '-0.01em', color: 'var(--color-text-primary)',
            }}>Money</span>

            {/* More-screens cue — tabular count + chevron, tappable */}
            <button
              onClick={() => setPage(1)}
              aria-label={`More — ${MONEY_SUB_PAGES[1].label}`}
              style={{
                marginLeft: 10,
                display: 'inline-flex', alignItems: 'center', gap: 5,
                height: 22, padding: '0 8px',
                borderRadius: 9999,
                background: 'transparent',
                border: '1px solid var(--color-border)',
                cursor: 'pointer',
                fontFamily: 'var(--font-sans)', fontSize: 10, fontWeight: 500,
                letterSpacing: '0.06em',
                color: 'var(--color-text-muted)',
                fontVariantNumeric: 'tabular-nums',
                fontFeatureSettings: '"tnum" 1',
              }}>
              1 / {MONEY_SUB_PAGES.length}
              <span style={{
                display: 'inline-grid', placeItems: 'center',
                animation: 'goji-peek-right 2.2s var(--ease-smooth) infinite',
              }}>
                <Icon name="chevronRight" size={12} />
              </span>
            </button>

            <div style={{ marginLeft: 'auto' }}>
              <HeaderActions hidden={hidden} onToggleHidden={onToggleHidden} />
            </div>
          </div>
        </div>
        <div style={{
          position: 'absolute', inset: 0,
          opacity: isLanding ? 0 : 1,
          transform: isLanding ? 'translateY(4px)' : 'translateY(0)',
          transition: 'opacity 280ms var(--ease-smooth), transform 320ms var(--ease-smooth)',
          pointerEvents: isLanding ? 'none' : 'auto',
        }}>
          <TopNavHeader
            title={currentTitle}
            onBack={() => setPage(0)}
            hidden={hidden}
            onToggleHidden={onToggleHidden}
          />
        </div>
      </div>

      {/* Page dots — hidden on landing, same as Home */}
      <div style={{
        height: 21,
        opacity: isLanding ? 0 : 1,
        transition: 'opacity 220ms var(--ease-smooth)',
        flexShrink: 0,
      }}>
        <PageDots total={MONEY_SUB_PAGES.length} active={page} onJump={setPage} />
      </div>

      <SwipePager page={page} onPageChange={setPage}>
        <MoneySummary />
        <AccountsScreen />
        <FXRatesScreen />
        <HouseholdScreen />
      </SwipePager>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// MoneySummary — this-week card + accounts + activity
// (Same content as the prior MoneyScreen, just lifted out of the header.)
// ─────────────────────────────────────────────────────────────
function MoneySummary() {
  const [range, setRange] = useStateMT('week');

  const DATA = {
    week:  { income: 4210, expense: 2168, savings: 2042, savingsRate: 48,
             buckets: [
               { label: 'Mon', income: 4210, expense: 1650 },
               { label: 'Tue', income: 0,    expense: 420 },
               { label: 'Wed', income: 0,    expense: 28 },
               { label: 'Thu', income: 0,    expense: 6 },
               { label: 'Fri', income: 0,    expense: 64 },
               { label: 'Sat', income: 0,    expense: 0 },
               { label: 'Sun', income: 0,    expense: 0 },
             ],
             categories: [
               { name: 'Housing',     color: '#7E6896', amount: 1650 },
               { name: 'Family',      color: '#9B7BD6', amount: 340 },
               { name: 'Subscriptions', color: '#9B7BD6', amount: 80 },
               { name: 'Food',        color: '#E0876A', amount: 64 },
               { name: 'Coffee',      color: '#D4AA45', amount: 28 },
               { name: 'Other',       color: '#888888', amount: 6 },
             ]
           },
    month: { income: 12630, expense: 8420, savings: 4210, savingsRate: 33,
             buckets: [
               { label: 'W1', income: 4210, expense: 2168 },
               { label: 'W2', income: 4210, expense: 2940 },
               { label: 'W3', income: 0,    expense: 1820 },
               { label: 'W4', income: 4210, expense: 1492 },
             ],
             categories: [
               { name: 'Housing',     color: '#7E6896', amount: 3300 },
               { name: 'Family',      color: '#9B7BD6', amount: 1360 },
               { name: 'Food',        color: '#E0876A', amount: 1240 },
               { name: 'Transport',   color: '#7BD6C9', amount: 820 },
               { name: 'Subscriptions', color: '#9B7BD6', amount: 320 },
               { name: 'Other',       color: '#888888', amount: 1380 },
             ]
           },
    year:  { income: 50520, expense: 38240, savings: 12280, savingsRate: 24,
             buckets: 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(' ').map((m, i) => ({
               label: m,
               income: 4210,
               expense: [2168, 2940, 3120, 2850, 3680, 3120, 2980, 3420, 3460, 3580, 3210, 3712][i],
             })),
             categories: [
               { name: 'Housing',     color: '#7E6896', amount: 19800 },
               { name: 'Food',        color: '#E0876A', amount: 6240 },
               { name: 'Family',      color: '#9B7BD6', amount: 4080 },
               { name: 'Transport',   color: '#7BD6C9', amount: 3120 },
               { name: 'Subscriptions', color: '#9B7BD6', amount: 960 },
               { name: 'Other',       color: '#888888', amount: 4040 },
             ]
           },
  };
  const d = DATA[range];
  const max = Math.max(...d.buckets.map(b => Math.max(b.income, b.expense)));
  const catMax = d.categories[0].amount;
  const [expandedCat, setExpandedCat] = useStateMT(null);

  // Sample activity per category (per range).
  const CATEGORY_TXN = {
    week: {
      Housing:      [{ icon: 'home2',  color: '#7E6896', name: 'Rent',              meta: 'Mon 13',         amount: -1650 }],
      Family:       [{ icon: 'family', color: '#9B7BD6', name: 'Sent to Mom',        meta: 'Tue 14 · NPR',   amount: -340 }],
      Subscriptions:[{ icon: 'bill',   color: '#9B7BD6', name: 'Comcast Internet',   meta: 'Tue 14',         amount: -79.99 }],
      Food:         [{ icon: 'coffee', color: '#E0876A', name: 'Whole Foods Market', meta: 'Wed 15',         amount: -42.18 },
                     { icon: 'coffee', color: '#E0876A', name: "Trader Joe's",       meta: 'Sat 18',         amount: -21.82 }],
      Coffee:       [{ icon: 'coffee', color: '#D4AA45', name: 'Blue Bottle Coffee', meta: 'Today',          amount: -6.50 },
                     { icon: 'coffee', color: '#D4AA45', name: 'Sightglass',         meta: 'Thu 16',         amount: -21.50 }],
      Other:        [{ icon: 'salary', color: '#888888', name: 'Misc',               meta: 'Fri 17',         amount: -6 }],
    },
    month: {
      Housing:      [{ icon: 'home2',  color: '#7E6896', name: 'Rent',     meta: 'Jan · Feb',   amount: -3300 }],
      Family:       [{ icon: 'family', color: '#9B7BD6', name: 'Sent to Mom',     meta: '4 transfers',  amount: -1360 }],
      Food:         [{ icon: 'coffee', color: '#E0876A', name: 'Whole Foods Market', meta: '8 visits',  amount: -680 },
                     { icon: 'coffee', color: '#E0876A', name: "Trader Joe's", meta: '6 visits',  amount: -360 },
                     { icon: 'coffee', color: '#E0876A', name: 'Restaurants', meta: '5 visits',  amount: -200 }],
      Transport:    [{ icon: 'send',   color: '#7BD6C9', name: 'Uber',     meta: '12 trips',     amount: -480 },
                     { icon: 'send',   color: '#7BD6C9', name: 'Caltrain', meta: 'Monthly pass', amount: -340 }],
      Subscriptions:[{ icon: 'bill',   color: '#9B7BD6', name: 'Spotify Family',   meta: 'Monthly', amount: -16 },
                     { icon: 'bill',   color: '#9B7BD6', name: 'Apple Music',      meta: 'Monthly', amount: -11 },
                     { icon: 'bill',   color: '#9B7BD6', name: 'iCloud + ChatGPT', meta: 'Monthly', amount: -293 }],
      Other:        [{ icon: 'salary', color: '#888888', name: 'Misc spending', meta: 'Various', amount: -1380 }],
    },
    year: {
      Housing:      [{ icon: 'home2',  color: '#7E6896', name: 'Rent',     meta: '12 months', amount: -19800 }],
      Food:         [{ icon: 'coffee', color: '#E0876A', name: 'Groceries',   meta: 'Avg $420/mo', amount: -5040 },
                     { icon: 'coffee', color: '#E0876A', name: 'Restaurants', meta: 'Avg $100/mo', amount: -1200 }],
      Family:       [{ icon: 'family', color: '#9B7BD6', name: 'Sent to Mom',  meta: '48 transfers · NPR', amount: -4080 }],
      Transport:    [{ icon: 'send',   color: '#7BD6C9', name: 'Uber + transit', meta: 'Avg $260/mo', amount: -3120 }],
      Subscriptions:[{ icon: 'bill',   color: '#9B7BD6', name: '3 services',  meta: 'Avg $80/mo',  amount: -960 }],
      Other:        [{ icon: 'salary', color: '#888888', name: 'Misc',        meta: 'Various', amount: -4040 }],
    },
  };
  const txnsFor = (catName) => (CATEGORY_TXN[range] || {})[catName] || [];

  return (
    <Page>
      {/* Range toggle */}
      <div style={{ padding: '4px 20px 0' }}>
        <SegmentedControl
          items={[{ key: 'week', label: 'Week' }, { key: 'month', label: 'Month' }, { key: 'year', label: 'Year' }]}
          active={range}
          onChange={setRange}
        />
      </div>

      {/* Income vs Expense hero — only meaningful when there are multiple buckets.
          The week view tells a different story below. */}
      {range !== 'week' && (
      <div style={{ padding: '20px 20px 0' }}>
        <div style={{
          background: 'var(--color-surface)',
          border: '1px solid var(--color-border)',
          borderRadius: 'var(--radius-card)',
          padding: '18px 20px',
          boxShadow: 'var(--shadow-premium)',
          position: 'relative', overflow: 'hidden',
        }}>
          <div style={{
            position: 'absolute', top: -60, right: -50, width: 180, height: 180,
            background: 'radial-gradient(circle, var(--color-glow-subtle) 0%, transparent 70%)',
            pointerEvents: 'none',
          }} />
          <div style={{ position: 'relative', display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 18 }}>
            <div>
              <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)',
              }}>
                <span style={{ width: 6, height: 6, borderRadius: 999, background: '#6FCF97' }} />
                Income
              </div>
              <Money amount={d.income} size="22px" color="var(--color-text-primary)" />
            </div>
            <div>
              <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)',
              }}>
                <span style={{ width: 6, height: 6, borderRadius: 999, background: '#E0876A' }} />
                Expenses
              </div>
              <Money amount={-d.expense} sign size="22px" color="var(--color-text-primary)" />
            </div>
          </div>

          <div style={{
            position: 'relative',
            marginTop: 18, padding: '14px 14px',
            background: 'var(--color-surface-2)', borderRadius: 'var(--radius-input)',
            border: '1px solid var(--color-border)',
          }}>
            <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'space-between', marginBottom: 8 }}>
              <span style={{
                fontFamily: 'var(--font-sans)', fontSize: 11, fontWeight: 500,
                letterSpacing: '0.12em', textTransform: 'uppercase',
                color: 'var(--color-text-muted)',
              }}>Saved · {d.savingsRate}%</span>
              <Money amount={d.savings} size="15px" color="var(--color-text-primary)" />
            </div>
            <div style={{
              height: 6, borderRadius: 9999, background: 'var(--color-input-bg)', overflow: 'hidden',
            }}>
              <div style={{
                height: '100%', width: `${d.savingsRate}%`,
                background: 'var(--goji-gradient)',
                borderRadius: 9999,
                boxShadow: '0 0 12px rgba(227, 183, 136, 0.35)',
                transition: 'width 500ms var(--ease-smooth)',
              }} />
            </div>
          </div>
        </div>
      </div>
      )}

      {/* WEEK — diaspora-specific Here vs There flow */}
      {range === 'week' && <DiasporaFlow here={1828} there={340} rate={132.99} />}

      {/* MONTH / YEAR — bar chart */}
      {range !== 'week' && (
      <>
      <SectionTitle>Income vs expenses</SectionTitle>
      <div style={{ padding: '0 20px' }}>
        <div style={{
          background: 'var(--color-surface)',
          border: '1px solid var(--color-border)',
          borderRadius: 'var(--radius-card)',
          padding: '18px 20px 14px',
          boxShadow: 'var(--shadow-premium)',
        }}>
          <div style={{
            display: 'grid',
            gridTemplateColumns: `repeat(${d.buckets.length}, 1fr)`,
            alignItems: 'end', gap: 6, height: 140,
          }}>
            {d.buckets.map(b => (
              <div key={b.label} style={{
                display: 'flex', alignItems: 'flex-end', justifyContent: 'center',
                gap: 3, height: '100%',
              }}>
                <div style={{
                  width: '40%', maxWidth: 12,
                  height: `${(b.income / max) * 100 || 1}%`,
                  background: 'linear-gradient(180deg, #7BD6A9 0%, #6FCF97 100%)',
                  borderRadius: '3px 3px 0 0',
                  transition: 'height 500ms var(--ease-smooth)',
                  minHeight: b.income > 0 ? 3 : 0,
                  opacity: b.income > 0 ? 1 : 0,
                }} />
                <div style={{
                  width: '40%', maxWidth: 12,
                  height: `${(b.expense / max) * 100 || 1}%`,
                  background: 'linear-gradient(180deg, #E0876A 0%, #C17A5A 100%)',
                  borderRadius: '3px 3px 0 0',
                  transition: 'height 500ms var(--ease-smooth)',
                  minHeight: b.expense > 0 ? 3 : 0,
                  opacity: b.expense > 0 ? 1 : 0.2,
                }} />
              </div>
            ))}
          </div>

          {/* Labels */}
          <div style={{
            display: 'grid',
            gridTemplateColumns: `repeat(${d.buckets.length}, 1fr)`,
            gap: 6, marginTop: 10,
          }}>
            {d.buckets.map(b => (
              <div key={b.label} style={{
                textAlign: 'center',
                fontFamily: 'var(--font-sans)', fontSize: 10, fontWeight: 500,
                color: 'var(--color-text-muted)', letterSpacing: '0.04em',
                overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap',
              }}>{b.label}</div>
            ))}
          </div>

          {/* Legend */}
          <div style={{
            display: 'flex', gap: 16, marginTop: 14, paddingTop: 12,
            borderTop: '1px solid var(--color-border-subtle)',
          }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
              <span style={{ width: 8, height: 8, borderRadius: 2, background: '#6FCF97' }} />
              <span style={{ fontFamily: 'var(--font-sans)', fontSize: 11, color: 'var(--color-text-body)' }}>Income</span>
            </div>
            <div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
              <span style={{ width: 8, height: 8, borderRadius: 2, background: '#E0876A' }} />
              <span style={{ fontFamily: 'var(--font-sans)', fontSize: 11, color: 'var(--color-text-body)' }}>Expenses</span>
            </div>
          </div>
        </div>
      </div>

      {/* Top categories */}
      </>
      )}
      <SectionTitle>Top categories</SectionTitle>
      <div style={{ padding: '0 20px' }}>
        <div style={{
          background: 'var(--color-surface)',
          border: '1px solid var(--color-border)',
          borderRadius: 'var(--radius-card)',
          boxShadow: 'var(--shadow-premium)',
          overflow: 'hidden',
        }}>
          {d.categories.map((c, i, arr) => {
            const open = expandedCat === c.name;
            const txns = txnsFor(c.name);
            return (
              <div key={c.name} style={{
                borderBottom: i < arr.length - 1 ? '1px solid var(--color-border-subtle)' : 'none',
              }}>
                <button
                  onClick={() => setExpandedCat(open ? null : c.name)}
                  style={{
                    width: '100%', textAlign: 'left',
                    background: 'transparent', border: 'none', cursor: 'pointer',
                    padding: '12px 18px',
                  }}>
                  <div style={{
                    display: 'flex', alignItems: 'center', justifyContent: 'space-between',
                    marginBottom: 6,
                  }}>
                    <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
                      <span style={{ width: 8, height: 8, borderRadius: 999, background: c.color }} />
                      <span style={{ fontFamily: 'var(--font-sans)', fontSize: 13, color: 'var(--color-text-primary)', fontWeight: 500 }}>{c.name}</span>
                    </div>
                    <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
                      <Money amount={-c.amount} size="13px" color="var(--color-text-primary)" />
                      <span style={{
                        color: 'var(--color-text-muted)',
                        display: 'grid', placeItems: 'center',
                        transform: open ? 'rotate(90deg)' : 'rotate(0deg)',
                        transition: 'transform 320ms var(--ease-smooth)',
                      }}>
                        <Icon name="chevronRight" size={14} />
                      </span>
                    </div>
                  </div>
                  <div style={{
                    height: 4, borderRadius: 9999,
                    background: 'var(--color-input-bg)', overflow: 'hidden',
                  }}>
                    <div style={{
                      height: '100%', width: `${(c.amount / catMax) * 100}%`,
                      background: c.color, borderRadius: 9999,
                      transition: 'width 500ms var(--ease-smooth)',
                    }} />
                  </div>
                </button>

                {/* Expanded transactions */}
                <div style={{
                  maxHeight: open ? `${Math.max(60, txns.length * 64 + 8)}px` : 0,
                  opacity: open ? 1 : 0,
                  overflow: 'hidden',
                  transition: 'max-height 380ms var(--ease-smooth), opacity 280ms var(--ease-smooth)',
                  background: 'var(--color-surface-2)',
                }}>
                  {txns.length === 0 ? (
                    <div style={{
                      padding: '14px 18px',
                      fontFamily: 'var(--font-sans)', fontSize: 12,
                      color: 'var(--color-text-muted)',
                    }}>No transactions in this range.</div>
                  ) : (
                    txns.map((t, ti) => (
                      <TransactionRow
                        key={ti}
                        icon={t.icon}
                        color={t.color}
                        name={t.name}
                        meta={t.meta}
                        amount={t.amount}
                        divider={ti < txns.length - 1}
                      />
                    ))
                  )}
                </div>
              </div>
            );
          })}
        </div>
      </div>
    </Page>
  );
}

// ─────────────────────────────────────────────────────────────
// AccountsScreen — connected accounts + recent activity across all
// ─────────────────────────────────────────────────────────────
function AccountsScreen() {
  const [filter, setFilter] = useStateMT('all');
  return (
    <Page>
      <div style={{ padding: '4px 20px 0', display: 'flex', flexDirection: 'column', gap: 12 }}>
        <AccountCard bank="Wells Fargo" mark="WF" color="#7DBEB7" currency="USD" amount={4218.40} delta={214} deltaPositive />
        <AccountCard bank="Nabil Bank" mark="NB" color="#E4B499" currency="NPR" currencySymbol="रू " amount={38450} lastSynced="2h ago" />
        <AccountCard bank="Ally Savings" mark="AL" color="#9B7BD6" currency="USD" amount={288.00} delta={0} deltaPositive />
      </div>

      <SectionTitle>Recent activity</SectionTitle>
      <div style={{ padding: '0 20px', display: 'flex', gap: 8, overflowX: 'auto' }}>
        <Chip active={filter==='all'}  onClick={()=>setFilter('all')}>All accounts</Chip>
        <Chip active={filter==='wf'}   onClick={()=>setFilter('wf')}>Wells Fargo</Chip>
        <Chip active={filter==='nb'}   onClick={()=>setFilter('nb')}>Nabil</Chip>
        <Chip active={filter==='ally'} onClick={()=>setFilter('ally')}>Ally</Chip>
      </div>
      <div style={{
        margin: '16px 20px 0',
        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="Today · Wells Fargo" amount={-6.50} />
        <TransactionRow icon="family" color="#9B7BD6" name="Sent to Mom" meta="Tue 14 · Nabil → NPR" amount={-340} />
        <TransactionRow icon="bill"   color="#7E6896" name="Comcast Internet" meta="Tue 14 · Wells Fargo" amount={-79.99} />
        <TransactionRow icon="salary" color="#6FCF97" name="Salary — Acme Inc." meta="Mon 13 · Wells Fargo" amount={4210} positive />
        <TransactionRow icon="home2"  color="#7E6896" name="Rent" meta="Mon 13 · Wells Fargo" amount={-1650} divider={false} />
      </div>
    </Page>
  );
}

// ─────────────────────────────────────────────────────────────
// FXRatesScreen — featured corridor + corridor list + alerts
// ─────────────────────────────────────────────────────────────
function FXRatesScreen() {
  const pairs = [
    { from: 'USD', to: 'EUR', rate: 0.92,   delta: -0.3, points: [10,12,14,13,15,12,11,9] },
    { from: 'USD', to: 'INR', rate: 84.10,  delta: 0.2,  points: [40,42,41,43,44,45,46,45] },
    { from: 'USD', to: 'GBP', rate: 0.78,   delta: 0.1,  points: [22,20,22,24,23,22,24,25] },
    { from: 'NPR', to: 'USD', rate: 0.0075, delta: -1.4, points: [30,32,31,28,26,25,24,22] },
  ];
  return (
    <Page>
      {/* Featured pair: USD → NPR */}
      <div style={{ padding: '4px 20px 0' }}>
        <div style={{
          background: 'var(--color-surface-2)',
          border: '1px solid var(--color-border-accent)',
          borderRadius: 'var(--radius-card)',
          padding: '20px 22px',
          boxShadow: 'var(--shadow-premium)',
          position: 'relative', overflow: 'hidden',
        }}>
          <div style={{
            position: 'absolute', inset: '-30% 10% auto 10%', height: 220,
            background: 'radial-gradient(ellipse, var(--color-glow-subtle) 0%, transparent 70%)',
            pointerEvents: 'none',
          }} />
          <div style={{ position: 'relative', display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
            <span style={{
              fontFamily: 'var(--font-sans)', fontSize: 11, fontWeight: 500,
              letterSpacing: '0.12em', textTransform: 'uppercase',
              color: 'var(--color-text-muted)',
            }}>Your corridor</span>
            <span style={{
              display: 'inline-flex', alignItems: 'center', gap: 5,
              fontFamily: 'var(--font-sans)', fontSize: 11, fontWeight: 600,
              padding: '3px 9px', borderRadius: 9999,
              background: 'rgba(111, 207, 151, 0.10)',
              border: '1px solid rgba(111, 207, 151, 0.25)',
              color: '#6FCF97',
              fontVariantNumeric: 'tabular-nums', fontFeatureSettings: '"tnum" 1',
            }}>
              <Icon name="arrowUp" size={11} stroke={2.5} /> +1.4% / 7d
            </span>
          </div>
          <div style={{
            position: 'relative', display: 'flex', alignItems: 'baseline', gap: 10, marginTop: 10,
          }}>
            <div style={{
              fontFamily: 'var(--font-serif)', fontWeight: 600,
              fontSize: 56, lineHeight: 1, letterSpacing: '-0.04em',
              background: 'var(--goji-gradient)',
              WebkitBackgroundClip: 'text', backgroundClip: 'text',
              WebkitTextFillColor: 'transparent',
              fontVariantNumeric: 'tabular-nums', fontFeatureSettings: '"tnum" 1',
            }}>132.99</div>
            <div style={{ fontFamily: 'var(--font-sans)', fontSize: 13, color: 'var(--color-text-muted)' }}>
              <div>USD → NPR</div>
              <div style={{ fontSize: 11, marginTop: 2 }}>1 USD buys रू 132.99</div>
            </div>
          </div>
          {/* Sparkline */}
          <svg viewBox="0 0 320 60" width="100%" height="48"
            style={{ marginTop: 12, position: 'relative', display: 'block' }}
            preserveAspectRatio="none">
            <defs>
              <linearGradient id="fxSparkFill" x1="0" x2="0" y1="0" y2="1">
                <stop offset="0%" stopColor="#C1A081" stopOpacity="0.25"/>
                <stop offset="100%" stopColor="#C1A081" stopOpacity="0"/>
              </linearGradient>
            </defs>
            <path d="M0,42 C20,40 40,38 60,32 C80,28 100,30 120,28 C140,24 160,28 180,22 C200,18 220,22 240,18 C260,12 280,16 300,10 L320,8 L320,60 L0,60 Z" fill="url(#fxSparkFill)"/>
            <path d="M0,42 C20,40 40,38 60,32 C80,28 100,30 120,28 C140,24 160,28 180,22 C200,18 220,22 240,18 C260,12 280,16 300,10 L320,8" stroke="#C1A081" strokeWidth="1.5" fill="none" vectorEffect="non-scaling-stroke"/>
          </svg>
          {/* Insight + CTA */}
          <div style={{
            position: 'relative', marginTop: 14,
            display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 12,
          }}>
            <span style={{ fontFamily: 'var(--font-sans)', fontSize: 12, color: 'var(--color-text-body)' }}>
              Good moment to top up.
            </span>
            <button style={{
              display: 'inline-flex', alignItems: 'center', gap: 6,
              height: 36, padding: '0 14px', borderRadius: 9999,
              background: 'var(--goji-gradient)', border: 'none',
              color: 'var(--goji-btn-text)', cursor: 'pointer',
              fontFamily: 'var(--font-sans)', fontSize: 12, fontWeight: 600,
              boxShadow: 'var(--shadow-fab)',
            }}>
              <Icon name="send" size={13} stroke={2.25} />
              Send NPR
            </button>
          </div>
        </div>
      </div>

      <SectionTitle>Other corridors</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',
      }}>
        {pairs.map((p, i) => (
          <FXPairRow key={`${p.from}-${p.to}`} pair={p} divider={i < pairs.length - 1} />
        ))}
      </div>

      <SectionTitle action="Add">Rate alerts</SectionTitle>
      <div style={{ padding: '0 20px' }}>
        <div style={{
          background: 'var(--color-surface)',
          border: '1px solid var(--color-border)',
          borderRadius: 'var(--radius-card)',
          padding: '14px 16px',
          boxShadow: 'var(--shadow-premium)',
          display: 'flex', alignItems: 'center', gap: 12,
        }}>
          <div style={{
            width: 36, height: 36, borderRadius: 10,
            background: 'rgba(193,160,129,0.08)',
            border: '1px solid rgba(193,160,129,0.18)',
            color: 'var(--goji-accent)',
            display: 'grid', placeItems: 'center', flexShrink: 0,
          }}>
            <Icon name="bell" size={16} />
          </div>
          <div style={{ flex: 1 }}>
            <div style={{ fontFamily: 'var(--font-sans)', fontSize: 14, fontWeight: 500, color: 'var(--color-text-primary)' }}>USD → NPR at 134.00</div>
            <div style={{ fontFamily: 'var(--font-sans)', fontSize: 12, color: 'var(--color-text-muted)', marginTop: 2 }}>Notify when reached</div>
          </div>
          <div style={{
            fontFamily: 'var(--font-sans)', fontSize: 12, color: 'var(--color-text-body)',
            padding: '3px 8px', borderRadius: 9999,
            background: 'var(--color-input-bg)', border: '1px solid var(--color-border)',
          }}>0.8% away</div>
        </div>
      </div>
    </Page>
  );
}

function FXPairRow({ pair, divider }) {
  const positive = pair.delta >= 0;
  const w = 80, h = 28;
  const max = Math.max(...pair.points), min = Math.min(...pair.points);
  const range = max - min || 1;
  const path = pair.points.map((p, i) => {
    const x = (i / (pair.points.length - 1)) * w;
    const y = h - ((p - min) / range) * h;
    return `${i === 0 ? 'M' : 'L'} ${x} ${y}`;
  }).join(' ');
  return (
    <div style={{
      display: 'grid', gridTemplateColumns: '1fr 90px auto', alignItems: 'center', gap: 12,
      padding: '14px 18px',
      borderBottom: divider ? '1px solid var(--color-border-subtle)' : 'none',
    }}>
      <div>
        <div style={{ fontFamily: 'var(--font-sans)', fontSize: 14, fontWeight: 500, color: 'var(--color-text-primary)', letterSpacing: '0.02em' }}>
          {pair.from} → {pair.to}
        </div>
        <div style={{
          fontFamily: 'var(--font-sans)', fontSize: 11, color: positive ? '#6FCF97' : '#EB5757',
          marginTop: 2, fontVariantNumeric: 'tabular-nums', fontFeatureSettings: '"tnum" 1',
        }}>{positive ? '+' : ''}{pair.delta.toFixed(1)}% · 7d</div>
      </div>
      <svg viewBox={`0 0 ${w} ${h}`} width={w} height={h}>
        <path d={path} stroke={positive ? '#6FCF97' : '#EB5757'} strokeWidth="1.5" fill="none" strokeLinecap="round" strokeLinejoin="round"/>
      </svg>
      <div style={{
        fontFamily: 'var(--font-sans)', fontSize: 15, fontWeight: 600, color: 'var(--color-text-primary)',
        fontVariantNumeric: 'tabular-nums lining-nums', fontFeatureSettings: '"tnum" 1, "lnum" 1',
        letterSpacing: '-0.01em', textAlign: 'right',
      }}>{pair.rate.toFixed(pair.rate < 1 ? 4 : 2)}</div>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// HouseholdScreen — diaspora household-back-home overview
// ─────────────────────────────────────────────────────────────
function HouseholdScreen() {
  const members = [
    { id: 'mom',    name: 'Mom',          role: 'Rent + groceries', tone: '#9B7BD6', initial: 'M', monthly: 200, last: '2d',  status: 'sent' },
    { id: 'dad',    name: 'Dad',          role: 'Medical',          tone: '#7BD6C9', initial: 'D', monthly: 80,  last: '5d',  status: 'sent' },
    { id: 'bahini', name: 'Bahini',       role: 'College tuition',  tone: '#E0876A', initial: 'B', monthly: 150, last: '12d', status: 'sent' },
    { id: 'daai',   name: 'Daai',         role: 'Family bills',     tone: '#D4AA45', initial: 'D', monthly: 50,  last: 'due', status: 'due' },
  ];
  const total = members.reduce((s, m) => s + m.monthly, 0);
  return (
    <Page>
      {/* Monthly support card */}
      <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)',
          position: 'relative', overflow: 'hidden',
        }}>
          <div style={{
            position: 'absolute', top: -60, right: -50, width: 180, height: 180,
            background: 'radial-gradient(circle, rgba(155, 123, 214, 0.10) 0%, transparent 70%)',
            pointerEvents: 'none',
          }} />
          <div style={{
            position: 'relative',
            fontFamily: 'var(--font-sans)', fontSize: 11, fontWeight: 500,
            letterSpacing: '0.12em', textTransform: 'uppercase',
            color: 'var(--color-text-muted)',
          }}>Household · this month</div>
          <div style={{ position: 'relative', margin: '10px 0 4px' }}>
            <Money amount={total} size="40px" color="var(--color-text-primary)" />
          </div>
          <div style={{
            position: 'relative',
            fontFamily: 'var(--font-sans)', fontSize: 12, color: 'var(--color-text-muted)',
          }}>
            Supporting <b style={{ color: 'var(--color-text-primary)', fontWeight: 600 }}>{members.length}</b> people · <Money amount={total * 12} size="12px" color="var(--color-text-body)" /> annual
          </div>
        </div>
      </div>

      <SectionTitle action="Add">Members</SectionTitle>
      <div style={{ padding: '0 20px', display: 'flex', flexDirection: 'column', gap: 10 }}>
        {members.map(m => (
          <div key={m.id} style={{
            background: 'var(--color-surface)',
            border: '1px solid var(--color-border)',
            borderRadius: 'var(--radius-card)',
            padding: '14px 16px',
            boxShadow: 'var(--shadow-premium)',
            display: 'flex', alignItems: 'center', gap: 12,
          }}>
            <div style={{
              width: 44, height: 44, borderRadius: 999,
              background: `${m.tone}28`, border: `1px solid ${m.tone}50`,
              color: m.tone,
              display: 'grid', placeItems: 'center',
              fontFamily: 'var(--font-serif)', fontSize: 18, fontWeight: 600,
              letterSpacing: '-0.02em',
              boxShadow: 'inset 0 1px 0 rgba(255,255,255,0.10)',
              flexShrink: 0,
            }}>{m.initial}</div>
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
                <span style={{ fontFamily: 'var(--font-sans)', fontSize: 14, fontWeight: 600, color: 'var(--color-text-primary)' }}>{m.name}</span>
                {m.status === 'due' && (
                  <span style={{
                    fontFamily: 'var(--font-sans)', fontSize: 9, fontWeight: 700,
                    letterSpacing: '0.06em', padding: '2px 6px', borderRadius: 4,
                    background: 'rgba(242, 201, 76, 0.12)', border: '1px solid rgba(242, 201, 76, 0.30)',
                    color: '#F2C94C',
                  }}>DUE</span>
                )}
              </div>
              <div style={{ fontFamily: 'var(--font-sans)', fontSize: 12, color: 'var(--color-text-muted)', marginTop: 2 }}>
                {m.role} · last {m.last === 'due' ? 'pending' : m.last}
              </div>
            </div>
            <div style={{ textAlign: 'right' }}>
              <Money amount={m.monthly} size="15px" color="var(--color-text-primary)" />
              <div style={{ fontFamily: 'var(--font-sans)', fontSize: 11, color: 'var(--color-text-muted)', marginTop: 2 }}>/ month</div>
            </div>
          </div>
        ))}
      </div>

      <SectionTitle>Recurring schedule</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',
      }}>
        {[
          { label: 'Rent for parents',  meta: 'Monthly · 1st',  amount: 200 },
          { label: "Sister's tuition",  meta: 'Quarterly · Jan/Apr/Jul/Oct', amount: 150 },
          { label: 'Family WiFi',       meta: 'Monthly · 15th', amount: 30 },
        ].map((r, i, arr) => (
          <div key={r.label} style={{
            display: 'grid', gridTemplateColumns: '38px 1fr auto',
            alignItems: 'center', gap: 14, padding: '14px 18px',
            borderBottom: i < arr.length - 1 ? '1px solid var(--color-border-subtle)' : 'none',
          }}>
            <div style={{
              width: 36, height: 36, borderRadius: 10,
              background: 'rgba(193,160,129,0.08)',
              border: '1px solid rgba(193,160,129,0.18)',
              color: 'var(--goji-accent)',
              display: 'grid', placeItems: 'center',
            }}>
              <Icon name="calendar" size={16} />
            </div>
            <div>
              <div style={{ fontFamily: 'var(--font-sans)', fontSize: 14, fontWeight: 500, color: 'var(--color-text-primary)' }}>{r.label}</div>
              <div style={{ fontFamily: 'var(--font-sans)', fontSize: 12, color: 'var(--color-text-muted)', marginTop: 2 }}>{r.meta}</div>
            </div>
            <Money amount={r.amount} size="14px" color="var(--color-text-primary)" />
          </div>
        ))}
      </div>
    </Page>
  );
}

// ─────────────────────────────────────────────────────────────
// DiasporaFlow — weekly "Here vs There" visualization. The story
// of where the money actually went this week, across borders.
// ─────────────────────────────────────────────────────────────
function DiasporaFlow({ here, there, rate }) {
  const total = here + there || 1;
  const therePct = Math.round((there / total) * 100);
  const herePct = 100 - therePct;
  const nprAmount = Math.round(there * rate);
  return (
    <>
      <SectionTitle>This week's flow</SectionTitle>
      <div style={{ padding: '0 20px' }}>
        <div style={{
          background: 'var(--color-surface)',
          border: '1px solid var(--color-border)',
          borderRadius: 'var(--radius-card)',
          padding: '20px 22px 18px',
          boxShadow: 'var(--shadow-premium)',
          position: 'relative', overflow: 'hidden',
        }}>
          {/* Atmospheric glow on the "there" side */}
          <div style={{
            position: 'absolute', top: -40, right: -60, width: 200, height: 200,
            background: 'radial-gradient(circle, var(--color-glow-subtle) 0%, transparent 70%)',
            pointerEvents: 'none',
          }} />

          {/* Country labels */}
          <div style={{
            position: 'relative', display: 'flex', justifyContent: 'space-between',
            marginBottom: 14,
          }}>
            <div>
              <div style={{
                fontFamily: 'var(--font-sans)', fontSize: 10, fontWeight: 600,
                letterSpacing: '0.16em', color: 'var(--color-text-muted)',
              }}>USD · HERE</div>
              <div style={{ display: 'flex', alignItems: 'baseline', gap: 6, marginTop: 4 }}>
                <Money amount={here} size="22px" color="var(--color-text-primary)" />
                <span style={{ fontFamily: 'var(--font-sans)', fontSize: 11, color: 'var(--color-text-muted)' }}>{herePct}%</span>
              </div>
            </div>
            <div style={{ textAlign: 'right' }}>
              <div style={{
                fontFamily: 'var(--font-sans)', fontSize: 10, fontWeight: 600,
                letterSpacing: '0.16em',
                background: 'var(--goji-gradient)',
                WebkitBackgroundClip: 'text', backgroundClip: 'text',
                WebkitTextFillColor: 'transparent',
              }}>SENT HOME · NPR</div>
              <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'flex-end', gap: 6, marginTop: 4 }}>
                <Money amount={there} size="22px" color="var(--color-text-primary)" />
                <span style={{ fontFamily: 'var(--font-sans)', fontSize: 11, color: 'var(--color-text-muted)' }}>{therePct}%</span>
              </div>
            </div>
          </div>

          {/* The flow ribbon — a horizontal split with a gradient bridge in the middle */}
          <div style={{
            position: 'relative',
            display: 'grid', gridTemplateColumns: `${herePct}fr ${therePct}fr`,
            gap: 0, height: 14, borderRadius: 9999, overflow: 'hidden',
          }}>
            <div style={{
              background: 'linear-gradient(90deg, #7E6896 0%, #9B7BD6 100%)',
              borderRadius: '9999px 4px 4px 9999px',
            }} />
            <div style={{
              background: 'var(--goji-gradient)',
              borderRadius: '4px 9999px 9999px 4px',
              boxShadow: '0 0 16px rgba(227, 183, 136, 0.45)',
            }} />
          </div>

          {/* FX bridge — context line */}
          <div style={{
            position: 'relative',
            marginTop: 16, padding: '12px 14px',
            background: 'var(--color-surface-2)', border: '1px solid var(--color-border)',
            borderRadius: 'var(--radius-input)',
            display: 'flex', alignItems: 'center', gap: 12,
          }}>
            <div style={{
              width: 32, height: 32, borderRadius: 10,
              background: 'rgba(193,160,129,0.10)',
              border: '1px solid rgba(193,160,129,0.20)',
              color: 'var(--goji-accent)',
              display: 'grid', placeItems: 'center', flexShrink: 0,
            }}>
              <Icon name="send" size={15} stroke={1.75} />
            </div>
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{
                fontFamily: 'var(--font-sans)', fontSize: 13,
                color: 'var(--color-text-primary)', fontWeight: 500,
              }}>1 transfer to Mom · रू {nprAmount.toLocaleString('en-IN')}</div>
              <div style={{
                fontFamily: 'var(--font-sans)', fontSize: 11, color: 'var(--color-text-muted)',
                marginTop: 2,
              }}>Rate {rate.toFixed(2)} · Tue 14</div>
            </div>
          </div>

          {/* Bottom insight */}
          <div style={{
            position: 'relative', marginTop: 12,
            fontFamily: 'var(--font-serif)', fontSize: 13, fontStyle: 'italic',
            color: 'var(--color-text-body)', letterSpacing: '-0.005em',
            lineHeight: 1.45, textWrap: 'pretty',
          }}>
            ~16% of this week's outflow crossed a border. You'd typically send
            <Money amount={310} size="13px" color="var(--color-text-primary)" />
            {' '}per week this year.
          </div>
        </div>
      </div>
    </>
  );
}

Object.assign(window, {
  MoneyTabContainer, MoneySummary, AccountsScreen,
  FXRatesScreen, FXPairRow, HouseholdScreen, DiasporaFlow,
});
