// ui_kits/mobile/LearnTab.jsx
// Learn hub: How-to / News / Literacy / Simulator. Same nav system as Money tab.

const { useState: useStateLT } = React;

const LEARN_PAGES = [
  { key: 'howto',     label: 'How-to' },
  { key: 'news',      label: 'Diaspora news' },
  { key: 'literacy',  label: 'Financial literacy' },
  { key: 'simulator', label: 'Spending sim' },
];

function LearnTabContainer({ hidden, onToggleHidden }) {
  const [page, setPage] = useStateLT(0);
  const isLanding = page === 0;
  const currentTitle = LEARN_PAGES[page].label;
  return (
    <div style={{ flex: 1, minHeight: 0, display: 'flex', flexDirection: 'column' }}>
      {/* Crossfading header */}
      <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)',
            }}>Learn</span>
            <button
              onClick={() => setPage(1)}
              aria-label={`More — ${LEARN_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 / {LEARN_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 */}
      <div style={{
        height: 21,
        opacity: isLanding ? 0 : 1,
        transition: 'opacity 220ms var(--ease-smooth)',
        flexShrink: 0,
      }}>
        <PageDots total={LEARN_PAGES.length} active={page} onJump={setPage} />
      </div>

      <SwipePager page={page} onPageChange={setPage}>
        <HowToScreen />
        <DiasporaNewsScreen />
        <FinancialLiteracyScreen />
        <SpendingSimulator />
      </SwipePager>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// HowToScreen — Goji-specific tours, "what is Safe to Spend", etc.
// ─────────────────────────────────────────────────────────────
function HowToScreen() {
  const tours = [
    { eyebrow: 'Tour · 90s',   title: 'What "Safe to Spend" means',         body: 'The one number on your home screen — what it counts, what it deliberately ignores.', icon: 'sparkle' },
    { eyebrow: 'How-to · 2m',  title: 'Send money home for less',           body: 'Compare corridor rates, set FX alerts, and time transfers when NPR is strong.',     icon: 'send' },
    { eyebrow: 'How-to · 1m',  title: 'Set a goal that actually sticks',    body: 'Three honest questions to ask before setting any savings target.',                  icon: 'target' },
    { eyebrow: 'How-to · 90s', title: 'Voice add: bills, goals, expenses',  body: 'Hold the FAB, speak naturally — Goji parses the intent.',                            icon: 'mic' },
    { eyebrow: 'How-to · 2m',  title: 'Hide amounts in public',             body: 'Tap the eye in the header to mask all dollar values app-wide.',                      icon: 'eye' },
  ];
  return (
    <Page>
      {/* Featured tour — gradient hero */}
      <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: '22px',
          boxShadow: 'var(--shadow-premium)',
          position: 'relative', overflow: 'hidden',
        }}>
          <div style={{
            position: 'absolute', top: -60, right: -60, width: 180, height: 180,
            background: 'radial-gradient(circle, rgba(227,183,136,0.16) 0%, transparent 70%)',
            pointerEvents: 'none',
          }} />
          <div style={{ position: 'relative' }}>
            <div style={{
              display: 'flex', alignItems: 'center', gap: 8,
              fontFamily: 'var(--font-sans)', fontSize: 11, fontWeight: 500,
              letterSpacing: '0.12em', textTransform: 'uppercase',
              color: 'var(--goji-accent)',
            }}>
              <span style={{ width: 6, height: 6, borderRadius: 999, background: 'var(--goji-accent)' }} />
              Welcome tour · 3 min
            </div>
            <h3 style={{
              margin: '10px 0 8px', fontFamily: 'var(--font-serif)', fontSize: 22,
              fontWeight: 600, letterSpacing: '-0.02em', color: 'var(--color-text-primary)',
              lineHeight: 1.2,
            }}>Take the calm-money tour</h3>
            <p style={{
              margin: 0, fontFamily: 'var(--font-sans)', fontSize: 13,
              color: 'var(--color-text-body)', lineHeight: 1.5,
            }}>Five short lessons covering Safe to Spend, sending home, goals, voice, and privacy.</p>
            <button style={{
              marginTop: 14, height: 40, padding: '0 18px', borderRadius: 9999,
              background: 'var(--goji-gradient)', border: 'none', cursor: 'pointer',
              color: 'var(--goji-btn-text)',
              fontFamily: 'var(--font-sans)', fontSize: 13, fontWeight: 600,
              display: 'inline-flex', alignItems: 'center', gap: 6,
              boxShadow: 'var(--shadow-fab)',
            }}>
              Start tour <Icon name="arrowRight" size={14} stroke={2.25} />
            </button>
          </div>
        </div>
      </div>

      <SectionTitle>Quick how-tos</SectionTitle>
      <div style={{ padding: '0 20px', display: 'flex', flexDirection: 'column', gap: 10 }}>
        {tours.map(t => (
          <article key={t.title} style={{
            background: 'var(--color-surface)',
            border: '1px solid var(--color-border)',
            borderRadius: 'var(--radius-card)',
            padding: '14px 16px',
            boxShadow: 'var(--shadow-premium)',
            display: 'grid', gridTemplateColumns: '40px 1fr auto', alignItems: 'center', gap: 14,
            cursor: 'pointer',
          }}>
            <div style={{
              width: 40, height: 40, 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={t.icon} size={18} />
            </div>
            <div>
              <div style={{
                fontFamily: 'var(--font-sans)', fontSize: 10, fontWeight: 500,
                letterSpacing: '0.12em', textTransform: 'uppercase',
                color: 'var(--color-text-muted)', marginBottom: 4,
              }}>{t.eyebrow}</div>
              <div style={{ fontFamily: 'var(--font-sans)', fontSize: 14, fontWeight: 600, color: 'var(--color-text-primary)', lineHeight: 1.3 }}>{t.title}</div>
              <div style={{ fontFamily: 'var(--font-sans)', fontSize: 12, color: 'var(--color-text-body)', marginTop: 4, lineHeight: 1.4 }}>{t.body}</div>
            </div>
            <Icon name="chevronRight" size={14} style={{ color: 'var(--color-text-muted)' }} />
          </article>
        ))}
      </div>
    </Page>
  );
}

// ─────────────────────────────────────────────────────────────
// DiasporaNewsScreen — curated diaspora finance + remittance headlines
// ─────────────────────────────────────────────────────────────
function DiasporaNewsScreen() {
  const featured = {
    eyebrow: 'Lead · today',
    source: 'Kathmandu Post',
    title: 'NPR strengthens against USD as remittances hit record month',
    body: 'November inflows rose 14% year-on-year, putting upward pressure on the rupee.',
  };
  const stories = [
    { source: 'Reuters · 2h',           title: 'World Bank: global remittance fees fall under 6% for first time',          tag: 'Fees' },
    { source: 'Setopati · 5h',          title: 'Nepal Rastra Bank tightens foreign currency disclosure rules',             tag: 'Policy' },
    { source: 'NPR · yesterday',         title: 'How diaspora investors are reshaping Lalitpur real estate',                  tag: 'Real estate' },
    { source: 'Economist · 1d',          title: 'The new generation of mobile-first cross-border banks',                     tag: 'Fintech' },
    { source: 'Online Khabar · 2d',     title: 'Migration data: where Nepalis are sending home from in 2026',                tag: 'Migration' },
  ];
  return (
    <Page>
      <div style={{ padding: '4px 20px 0' }}>
        <article 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: -50, right: -50, width: 160, height: 160,
            background: 'radial-gradient(circle, var(--color-glow-subtle) 0%, transparent 70%)',
            pointerEvents: 'none',
          }} />
          <div style={{ position: 'relative' }}>
            <div style={{
              display: 'flex', alignItems: 'center', gap: 8,
              fontFamily: 'var(--font-sans)', fontSize: 10, fontWeight: 600,
              letterSpacing: '0.12em', textTransform: 'uppercase',
              color: 'var(--goji-accent)',
            }}>
              {featured.eyebrow}
            </div>
            <h3 style={{
              margin: '10px 0 8px', fontFamily: 'var(--font-serif)', fontSize: 20,
              fontWeight: 600, letterSpacing: '-0.02em', color: 'var(--color-text-primary)',
              lineHeight: 1.25,
            }}>{featured.title}</h3>
            <p style={{
              margin: 0, fontFamily: 'var(--font-sans)', fontSize: 13,
              color: 'var(--color-text-body)', lineHeight: 1.5,
            }}>{featured.body}</p>
            <div style={{
              marginTop: 12, fontFamily: 'var(--font-sans)', fontSize: 11,
              color: 'var(--color-text-muted)', letterSpacing: '0.04em',
            }}>{featured.source}</div>
          </div>
        </article>
      </div>

      <SectionTitle action="See all">Headlines</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',
      }}>
        {stories.map((s, i) => (
          <div key={s.title} style={{
            padding: '14px 18px',
            borderBottom: i < stories.length - 1 ? '1px solid var(--color-border-subtle)' : 'none',
            display: 'grid', gridTemplateColumns: '1fr auto', gap: 12, alignItems: 'flex-start',
          }}>
            <div>
              <div style={{
                fontFamily: 'var(--font-sans)', fontSize: 10, fontWeight: 500,
                letterSpacing: '0.08em', textTransform: 'uppercase',
                color: 'var(--color-text-muted)', marginBottom: 4,
              }}>{s.source}</div>
              <div style={{ fontFamily: 'var(--font-sans)', fontSize: 14, fontWeight: 500, color: 'var(--color-text-primary)', lineHeight: 1.35 }}>{s.title}</div>
            </div>
            <span style={{
              fontFamily: 'var(--font-sans)', fontSize: 10, fontWeight: 600,
              padding: '3px 8px', borderRadius: 9999,
              background: 'rgba(193,160,129,0.08)',
              border: '1px solid rgba(193,160,129,0.20)',
              color: 'var(--goji-accent)',
              whiteSpace: 'nowrap',
            }}>{s.tag}</span>
          </div>
        ))}
      </div>
    </Page>
  );
}

// ─────────────────────────────────────────────────────────────
// FinancialLiteracyScreen — structured lessons + progress
// ─────────────────────────────────────────────────────────────
function FinancialLiteracyScreen() {
  const tracks = [
    { title: 'Budgeting basics',        steps: 6, done: 4, color: '#E0876A' },
    { title: 'Credit & credit scores',  steps: 5, done: 2, color: '#9B7BD6' },
    { title: 'Emergency funds',         steps: 4, done: 4, color: '#6FCF97' },
    { title: 'Investing fundamentals',  steps: 8, done: 1, color: '#7BD6C9' },
    { title: 'Taxes for diaspora',      steps: 7, done: 0, color: '#7E6896' },
  ];
  return (
    <Page>
      {/* Progress hero */}
      <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)',
          display: 'flex', alignItems: 'center', gap: 18,
        }}>
          <ProgressRing pct={37} size={64} />
          <div style={{ flex: 1, minWidth: 0 }}>
            <div style={{
              fontFamily: 'var(--font-sans)', fontSize: 11, fontWeight: 500,
              letterSpacing: '0.12em', textTransform: 'uppercase',
              color: 'var(--color-text-muted)',
            }}>Your literacy</div>
            <div style={{
              fontFamily: 'var(--font-sans)', fontSize: 18, fontWeight: 600,
              color: 'var(--color-text-primary)', marginTop: 4,
            }}>11 of 30 lessons</div>
            <div style={{
              fontFamily: 'var(--font-sans)', fontSize: 12, color: 'var(--color-text-muted)',
              marginTop: 4,
            }}>~3 hours total · 18 min average per lesson</div>
          </div>
        </div>
      </div>

      <SectionTitle>Tracks</SectionTitle>
      <div style={{ padding: '0 20px', display: 'flex', flexDirection: 'column', gap: 10 }}>
        {tracks.map(t => {
          const pct = Math.round((t.done / t.steps) * 100);
          return (
            <div key={t.title} style={{
              background: 'var(--color-surface)',
              border: '1px solid var(--color-border)',
              borderRadius: 'var(--radius-card)',
              padding: '14px 16px',
              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: t.color }} />
                  <span style={{ fontFamily: 'var(--font-sans)', fontSize: 14, fontWeight: 600, color: 'var(--color-text-primary)' }}>{t.title}</span>
                </div>
                <span style={{
                  fontFamily: 'var(--font-sans)', fontSize: 12,
                  color: pct === 100 ? '#6FCF97' : 'var(--color-text-muted)',
                  fontVariantNumeric: 'tabular-nums', fontFeatureSettings: '"tnum" 1',
                }}>{t.done} / {t.steps}{pct === 100 ? ' · done' : ''}</span>
              </div>
              <div style={{
                height: 4, borderRadius: 9999,
                background: 'var(--color-input-bg)', overflow: 'hidden',
              }}>
                <div style={{
                  height: '100%', width: `${pct}%`,
                  background: pct === 100 ? 'var(--goji-gradient)' : t.color,
                  borderRadius: 9999,
                  transition: 'width 500ms var(--ease-smooth)',
                }} />
              </div>
            </div>
          );
        })}
      </div>
    </Page>
  );
}

// ─────────────────────────────────────────────────────────────
// SpendingSimulator — "what if" projector. Add/remove categories,
// swap between weekly and monthly horizons.
// ─────────────────────────────────────────────────────────────
const SIM_CATEGORY_LIBRARY = [
  { name: 'Coffee & dining',  color: '#D4AA45', defaultWeek: 20,  defaultMonth: 80 },
  { name: 'Delivery / takeout', color: '#E0876A', defaultWeek: 30,  defaultMonth: 120 },
  { name: 'Subscriptions',    color: '#9B7BD6', defaultWeek: 15,  defaultMonth: 60 },
  { name: 'Rideshare',        color: '#7BD6C9', defaultWeek: 20,  defaultMonth: 80 },
  { name: 'Groceries',        color: '#92A878', defaultWeek: 80,  defaultMonth: 320 },
  { name: 'Shopping',         color: '#C17A74', defaultWeek: 40,  defaultMonth: 160 },
  { name: 'Entertainment',    color: '#6E7FD4', defaultWeek: 20,  defaultMonth: 80 },
  { name: 'Personal care',    color: '#7DBEB7', defaultWeek: 15,  defaultMonth: 60 },
];

function SpendingSimulator() {
  const [horizon, setHorizon] = useStateLT('month'); // 'week' | 'month'
  // Active categories — list of { name, value }
  const [items, setItems] = useStateLT([
    { name: 'Coffee & dining',  value: { week: 20,  month: 80 } },
    { name: 'Delivery / takeout', value: { week: 30,  month: 120 } },
    { name: 'Subscriptions',    value: { week: 15,  month: 60 } },
  ]);
  const [addOpen, setAddOpen] = useStateLT(false);

  const income     = horizon === 'week' ? 1052 : 4210;
  const fixedSpend = horizon === 'week' ? 600  : 2400;
  const goalAmount = 2500;
  const goalLabel  = 'trip goal';

  const setItemValue = (name, v) => {
    setItems(arr => arr.map(it => it.name === name
      ? { ...it, value: { ...it.value, [horizon]: v } }
      : it
    ));
  };
  const removeItem = (name) => setItems(arr => arr.filter(it => it.name !== name));
  const addItem = (cat) => {
    setItems(arr => arr.concat([{ name: cat.name, value: { week: cat.defaultWeek, month: cat.defaultMonth } }]));
    setAddOpen(false);
  };

  const totalDiscretionary = items.reduce((s, it) => s + (it.value[horizon] || 0), 0);
  const totalSpend = fixedSpend + totalDiscretionary;
  const periodSavings = income - totalSpend;
  const savingsRate = Math.max(0, Math.round((periodSavings / income) * 100));
  // To project to goal, normalize to monthly first.
  const monthlySavings = horizon === 'week' ? periodSavings * 4.33 : periodSavings;
  const periodsToGoal = monthlySavings > 0
    ? Math.ceil(goalAmount / monthlySavings * (horizon === 'week' ? 4.33 : 1))
    : 99;
  const periodUnit = horizon === 'week' ? 'wk' : 'mo';

  const colorFor = (name) => (SIM_CATEGORY_LIBRARY.find(c => c.name === name) || {}).color || '#888888';
  const availableToAdd = SIM_CATEGORY_LIBRARY.filter(c => !items.some(it => it.name === c.name));

  return (
    <Page>
      {/* Horizon toggle */}
      <div style={{ padding: '4px 20px 0' }}>
        <SegmentedControl
          items={[{ key: 'week', label: 'Per week' }, { key: 'month', label: 'Per month' }]}
          active={horizon}
          onChange={setHorizon}
        />
      </div>

      {/* Projection hero */}
      <div style={{ padding: '16px 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', top: -50, right: -50, width: 180, height: 180,
            background: 'radial-gradient(circle, var(--color-glow-subtle) 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)',
          }}>If you keep this up</div>

          <div style={{
            position: 'relative', display: 'grid', gridTemplateColumns: '1fr 1fr',
            gap: 18, marginTop: 12,
          }}>
            <div>
              <div style={{
                fontFamily: 'var(--font-serif)', fontWeight: 600,
                fontSize: 36, lineHeight: 1, letterSpacing: '-0.04em',
                background: 'var(--goji-gradient)',
                WebkitBackgroundClip: 'text', backgroundClip: 'text',
                WebkitTextFillColor: 'transparent',
                fontVariantNumeric: 'tabular-nums', fontFeatureSettings: '"tnum" 1',
              }}>{savingsRate}%</div>
              <div style={{
                fontFamily: 'var(--font-sans)', fontSize: 11, color: 'var(--color-text-muted)',
                marginTop: 4, letterSpacing: '0.08em', textTransform: 'uppercase',
              }}>Saved per {horizon}</div>
            </div>
            <div>
              <div style={{
                fontFamily: 'var(--font-serif)', fontWeight: 600,
                fontSize: 36, lineHeight: 1, letterSpacing: '-0.04em',
                color: 'var(--color-text-primary)',
                fontVariantNumeric: 'tabular-nums', fontFeatureSettings: '"tnum" 1',
              }}>{periodsToGoal}<span style={{ fontSize: 18, marginLeft: 4, fontFamily: 'var(--font-sans)', fontWeight: 500, color: 'var(--color-text-muted)' }}>{periodUnit}</span></div>
              <div style={{
                fontFamily: 'var(--font-sans)', fontSize: 11, color: 'var(--color-text-muted)',
                marginTop: 4, letterSpacing: '0.08em', textTransform: 'uppercase',
              }}>To $2,500 {goalLabel}</div>
            </div>
          </div>

          <div style={{
            position: 'relative', marginTop: 16,
            height: 6, borderRadius: 9999, background: 'var(--color-input-bg)', overflow: 'hidden',
          }}>
            <div style={{
              height: '100%', width: `${savingsRate}%`,
              background: 'var(--goji-gradient)',
              borderRadius: 9999,
              boxShadow: '0 0 12px rgba(227, 183, 136, 0.35)',
              transition: 'width 350ms var(--ease-smooth)',
            }} />
          </div>
        </div>
      </div>

      <SectionTitle action={availableToAdd.length ? '+ Add' : undefined}
        onAction={availableToAdd.length ? () => setAddOpen(o => !o) : undefined}>
        Tweak your {horizon}
      </SectionTitle>

      {/* Add-category drawer */}
      <div style={{
        maxHeight: addOpen ? 200 : 0,
        opacity: addOpen ? 1 : 0,
        overflow: 'hidden',
        transition: 'max-height 380ms var(--ease-smooth), opacity 240ms var(--ease-smooth)',
        padding: '0 20px',
      }}>
        <div style={{
          display: 'flex', flexWrap: 'wrap', gap: 8,
          padding: '4px 0 12px',
        }}>
          {availableToAdd.map(cat => (
            <button key={cat.name}
              onClick={() => addItem(cat)}
              style={{
                display: 'inline-flex', alignItems: 'center', gap: 6,
                height: 32, padding: '0 12px', borderRadius: 9999,
                background: `${cat.color}1a`,
                border: `1px solid ${cat.color}55`,
                color: cat.color,
                cursor: 'pointer',
                fontFamily: 'var(--font-sans)', fontSize: 12, fontWeight: 500,
              }}>
              <Icon name="plus" size={12} stroke={2.25} />
              {cat.name}
            </button>
          ))}
        </div>
      </div>

      <div style={{ padding: '0 20px', display: 'flex', flexDirection: 'column', gap: 12 }}>
        {items.length === 0 ? (
          <div style={{
            padding: '24px 18px', textAlign: 'center',
            background: 'var(--color-surface)',
            border: '1px dashed var(--color-border-strong)',
            borderRadius: 'var(--radius-card)',
            fontFamily: 'var(--font-sans)', fontSize: 13, color: 'var(--color-text-muted)',
          }}>
            No categories yet. Tap <b style={{ color: 'var(--goji-accent)' }}>+ Add</b> above to start tweaking.
          </div>
        ) : (
          items.map(it => (
            <SimSlider
              key={it.name}
              label={it.name}
              value={it.value[horizon] || 0}
              min={0}
              max={horizon === 'week' ? 200 : 800}
              onChange={(v) => setItemValue(it.name, v)}
              onRemove={() => removeItem(it.name)}
              color={colorFor(it.name)}
            />
          ))
        )}
      </div>

      {items.length > 0 && (
        <>
          <SectionTitle>If you cut this in half</SectionTitle>
          <div style={{ padding: '0 20px' }}>
            <div style={{
              background: 'var(--color-surface)',
              border: '1px solid var(--color-border)',
              borderRadius: 'var(--radius-card)',
              padding: '16px 18px',
              boxShadow: 'var(--shadow-premium)',
              fontFamily: 'var(--font-serif)', fontSize: 14, fontStyle: 'italic',
              color: 'var(--color-text-body)', lineHeight: 1.5,
              textWrap: 'pretty',
            }}>
              Trimming discretionary by 50% would save an extra <b style={{ color: 'var(--goji-accent)', fontStyle: 'normal', fontFamily: 'var(--font-sans)', fontWeight: 600 }}>${Math.round(totalDiscretionary / 2)}</b> per {horizon}. That's <b style={{ color: 'var(--goji-accent)', fontStyle: 'normal', fontFamily: 'var(--font-sans)', fontWeight: 600 }}>${Math.round((horizon === 'week' ? (totalDiscretionary / 2) * 4.33 : totalDiscretionary / 2) * 12)}</b> a year toward your goal.
            </div>
          </div>
        </>
      )}
    </Page>
  );
}

function SimSlider({ label, value, min, max, onChange, color, onRemove }) {
  return (
    <div style={{
      background: 'var(--color-surface)',
      border: '1px solid var(--color-border)',
      borderRadius: 'var(--radius-card)',
      padding: '14px 16px',
      boxShadow: 'var(--shadow-premium)',
    }}>
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', marginBottom: 10, gap: 10 }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 10, flex: 1, minWidth: 0 }}>
          <span style={{ width: 8, height: 8, borderRadius: 999, background: color, flexShrink: 0 }} />
          <span style={{
            fontFamily: 'var(--font-sans)', fontSize: 13, fontWeight: 500, color: 'var(--color-text-primary)',
            overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap',
          }}>{label}</span>
        </div>
        <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
          <Money amount={value} size="14px" color="var(--color-text-primary)" />
          {onRemove && (
            <button
              onClick={onRemove}
              aria-label={`Remove ${label}`}
              style={{
                width: 24, height: 24, borderRadius: 999,
                background: 'transparent', border: 'none', cursor: 'pointer',
                color: 'var(--color-text-muted)',
                display: 'grid', placeItems: 'center',
              }}>
              <Icon name="close" size={12} />
            </button>
          )}
        </div>
      </div>
      <input
        type="range"
        min={min} max={max} value={value}
        onChange={(e) => onChange(Number(e.target.value))}
        style={{ width: '100%', accentColor: color }}
      />
    </div>
  );
}

Object.assign(window, {
  LearnTabContainer, HowToScreen, DiasporaNewsScreen,
  FinancialLiteracyScreen, SpendingSimulator, SimSlider,
});
