// Warm Coach — Control habits page + Habit detail popup
// Self-contained: only relies on globals from shared.jsx (A, AWrap, AHeader,
// ADateStrip, ATabBar, Iq, ICheck, IFire, IArrowUp, IArrowDown) and screens.jsx
// (A_Discipline, for the dimmed backdrop behind the detail sheet).

// ── local helpers (uniquely named to avoid collisions) ──
function CCard({ children, style, accent }) {
  return (
    <div style={{
      background: A.card, borderRadius: 22, padding: 18,
      border: `1px solid ${A.rule}`, position: 'relative',
      ...(accent ? { borderColor: accent, borderWidth: 1.5 } : {}),
      ...style,
    }}>{children}</div>
  );
}

function CPill({ tone = 'good', children, icon }) {
  const map = {
    bad:  { bg: '#FCE6E6', fg: A.bad },
    warn: { bg: '#FFF1DC', fg: A.warn },
    good: { bg: '#E2F7EC', fg: A.good },
    ink:  { bg: A.ink, fg: '#fff' },
  }[tone];
  return (
    <div style={{
      display: 'inline-flex', alignItems: 'center', gap: 5,
      padding: '5px 10px', borderRadius: 999, background: map.bg, color: map.fg,
      fontSize: 10.5, fontWeight: 700, letterSpacing: '0.1em', textTransform: 'uppercase',
      fontFamily: A.body,
    }}>{icon}<span>{children}</span></div>
  );
}

// ───────────────────────────────────────────────────────
// 1) CONTROL HABITS  — quitting / resisting bad habits
// ───────────────────────────────────────────────────────
function A_Control() {
  const settings = (
    <Iq width={18} height={18}><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 00.33 1.82l.06.06a2 2 0 11-2.83 2.83l-.06-.06a1.65 1.65 0 00-1.82-.33 1.65 1.65 0 00-1 1.51V21a2 2 0 11-4 0v-.09a1.65 1.65 0 00-1-1.51 1.65 1.65 0 00-1.82.33l-.06.06a2 2 0 11-2.83-2.83l.06-.06a1.65 1.65 0 00.33-1.82 1.65 1.65 0 00-1.51-1H3a2 2 0 110-4h.09a1.65 1.65 0 001.51-1 1.65 1.65 0 00-.33-1.82l-.06-.06a2 2 0 112.83-2.83l.06.06a1.65 1.65 0 001.82.33H9a1.65 1.65 0 001-1.51V3a2 2 0 114 0v.09a1.65 1.65 0 001 1.51 1.65 1.65 0 001.82-.33l.06-.06a2 2 0 112.83 2.83l-.06.06a1.65 1.65 0 00-.33 1.82V9a1.65 1.65 0 001.51 1H21a2 2 0 110 4h-.09a1.65 1.65 0 00-1.51 1z"/></Iq>
  );

  const controlled = [
    { name: 'SUGAR',          emoji: '🍩', clean: 3,  state: 'resisted' },
    { name: 'DOOMSCROLLING',  emoji: '📱', clean: 0,  state: 'slipped' },
    { name: 'ALCOHOL',        emoji: '🍺', clean: 21, state: 'resisted' },
    { name: 'LATE NIGHTS',    emoji: '🌙', clean: 6,  state: 'resisted' },
  ];

  return (
    <AWrap>
      <AHeader mode="DISCIPLINE MODE" title="LOCK IN" action={settings} />
      <ADateStrip selected={16} />
      <div className="scr-body" style={{ paddingTop: 4 }}>

        {/* BUILD / CONTROL toggle — CONTROL active */}
        <div style={{ display: 'flex', gap: 4, padding: 4, borderRadius: 14, background: A.rest, marginBottom: 14 }}>
          {['BUILD', 'CONTROL'].map((t, i) => (
            <div key={t} style={{
              flex: 1, textAlign: 'center', padding: '10px 0', borderRadius: 10,
              background: i === 1 ? A.card : 'transparent',
              boxShadow: i === 1 ? '0 1px 2px rgba(0,0,0,0.06)' : 'none',
              color: i === 1 ? A.ink : A.ink2,
              fontFamily: A.display, fontWeight: 700, fontSize: 13, letterSpacing: '0.12em',
            }}>{t}</div>
          ))}
        </div>

        {/* Featured: QUIT SMOKING hero */}
        <CCard accent={A.good} style={{ padding: 0, overflow: 'hidden' }}>
          <div style={{ padding: '16px 18px' }}>
            <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start' }}>
              <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
                <div style={{ width: 40, height: 40, borderRadius: 12, background: '#E2F7EC', display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 20 }}>🚭</div>
                <div>
                  <div className="eyebrow" style={{ color: A.ink3 }}>QUITTING</div>
                  <div style={{ fontFamily: A.display, fontWeight: 800, fontSize: 18, color: A.ink, letterSpacing: '0.01em' }}>SMOKING</div>
                </div>
              </div>
              <CPill tone="good" icon={<IArrowUp />}>STAYING CLEAN</CPill>
            </div>

            <div style={{ display: 'flex', alignItems: 'baseline', gap: 8, marginTop: 14 }}>
              <span style={{ color: A.warn, transform: 'translateY(2px)' }}><IFire /></span>
              <div style={{ fontFamily: A.display, fontWeight: 800, fontSize: 58, color: A.ink, lineHeight: 0.9, letterSpacing: '-0.04em' }}>12</div>
              <div style={{ fontFamily: A.display, fontWeight: 800, fontSize: 15, color: A.ink3, letterSpacing: '0.12em' }}>DAYS CLEAN</div>
            </div>

            {/* mini stats */}
            <div style={{ display: 'grid', gridTemplateColumns: 'repeat(2,1fr)', gap: 8, marginTop: 16 }}>
              {[
                { v: '240', l: 'CIGS AVOIDED' },
                { v: '$96', l: 'SAVED' },
                { v: '21d', l: 'BEST STREAK' },
                { v: '23', l: 'CIGS THIS MONTH' },
              ].map((s) => (
                <div key={s.l} style={{ background: A.rest, borderRadius: 12, padding: '10px 10px', textAlign: 'center' }}>
                  <div style={{ fontFamily: A.display, fontWeight: 800, fontSize: 18, color: A.ink, lineHeight: 1 }}>{s.v}</div>
                  <div style={{ fontSize: 9, color: A.ink3, fontWeight: 700, letterSpacing: '0.08em', marginTop: 4 }}>{s.l}</div>
                </div>
              ))}
            </div>

            {/* identity insight */}
            <div style={{ display: 'flex', flexDirection: 'column', gap: 4, marginTop: 12, padding: '10px 12px', borderRadius: 12, background: '#E2F7EC' }}>
              <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
                <span style={{ color: A.good }}><IArrowUp /></span>
                <div style={{ fontSize: 13, color: A.ink, fontWeight: 700, lineHeight: 1.3 }}>
                  21 days of choosing yourself.
                </div>
              </div>
              <div style={{ fontSize: 11, color: A.ink2, fontWeight: 500, lineHeight: 1.3, paddingLeft: 24 }}>
                You're doing better than <strong>64%</strong> of people trying to quit smoking.
              </div>
            </div>

            {/* milestone bar */}
            <div style={{ marginTop: 16 }}>
              <div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: 6 }}>
                <span className="eyebrow" style={{ color: A.ink3 }}>NEXT MILESTONE</span>
                <span className="eyebrow" style={{ color: A.good }}>14-DAY · 2 LEFT</span>
              </div>
              <div style={{ height: 8, borderRadius: 6, background: A.rest, overflow: 'hidden' }}>
                <div style={{ height: '100%', width: '86%', background: A.good, borderRadius: 6 }} />
              </div>
            </div>
          </div>
        </CCard>

        {/* resist list */}
        <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', margin: '20px 4px 10px' }}>
          <div className="eyebrow">TODAY · DID YOU RESIST?</div>
          <div style={{ fontSize: 10.5, color: A.ink3, fontWeight: 600 }}>3 / 4 CLEAN</div>
        </div>

        <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
          {controlled.map((h) => {
            const resisted = h.state === 'resisted';
            return (
              <CCard key={h.name} style={{ padding: 12, display: 'flex', alignItems: 'center', gap: 12 }}>
                <div style={{ width: 38, height: 38, borderRadius: 11, background: A.rest, display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 18, flexShrink: 0 }}>{h.emoji}</div>
                <div style={{ flex: 1, minWidth: 0 }}>
                  <div style={{ fontFamily: A.display, fontWeight: 700, fontSize: 14, color: A.ink, letterSpacing: '0.02em' }}>{h.name}</div>
                  <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginTop: 3 }}>
                    {h.clean > 0 ? (
                      <span style={{ display: 'inline-flex', alignItems: 'center', gap: 3, fontSize: 10.5, color: A.ink3, fontWeight: 600 }}>
                        <span style={{ color: A.warn }}><IFire /></span>{h.clean}d clean
                      </span>
                    ) : (
                      <span style={{ fontSize: 10.5, color: A.bad, fontWeight: 700, letterSpacing: '0.04em' }}>STREAK RESET</span>
                    )}
                  </div>
                </div>
                {/* resisted / slipped toggle */}
                <div style={{ display: 'flex', gap: 4, background: A.rest, padding: 3, borderRadius: 10 }}>
                  <div style={{
                    width: 30, height: 30, borderRadius: 8,
                    background: resisted ? A.good : 'transparent', color: resisted ? '#fff' : A.ink3,
                    display: 'flex', alignItems: 'center', justifyContent: 'center',
                  }}><ICheck /></div>
                  <div style={{
                    width: 30, height: 30, borderRadius: 8,
                    background: !resisted ? A.bad : 'transparent', color: !resisted ? '#fff' : A.ink3,
                    display: 'flex', alignItems: 'center', justifyContent: 'center',
                  }}><Iq width={14} height={14} strokeWidth="2.5"><path d="M6 6l12 12M18 6L6 18"/></Iq></div>
                </div>
              </CCard>
            );
          })}
        </div>

        {/* identity analysis — who you're becoming, not just streaks */}
        <div style={{ background: A.ink, borderRadius: 22, padding: '18px 18px 20px', marginTop: 20 }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
            <span style={{
              width: 30, height: 30, borderRadius: 10, background: A.hype, color: A.ink,
              display: 'flex', alignItems: 'center', justifyContent: 'center', flex: '0 0 auto',
            }}><ISpark /></span>
            <div style={{ fontFamily: A.mono, fontSize: 10.5, fontWeight: 600, letterSpacing: '0.14em', color: A.hype, textTransform: 'uppercase' }}>who you're becoming</div>
          </div>
          <div style={{ marginTop: 14, display: 'flex', flexDirection: 'column', gap: 10 }}>
            {[
              "You're taking your evenings back.",
              'The cravings are losing.',
              'Focus is returning.',
              "You've saved 14 hours this month.",
            ].map((line) => (
              <div key={line} style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
                <span style={{ color: A.hype, flex: '0 0 auto' }}><ICheck /></span>
                <div style={{ fontFamily: A.body, fontSize: 13.5, color: 'rgba(255,255,255,0.85)', lineHeight: 1.4 }}>{line}</div>
              </div>
            ))}
          </div>
        </div>

        {/* add controlled habit */}
        <button style={{
          width: '100%', marginTop: 12, padding: '13px 0', borderRadius: 14,
          border: `1.5px dashed ${A.rule}`, background: 'transparent', color: A.ink2,
          fontFamily: A.display, fontWeight: 700, fontSize: 12.5, letterSpacing: '0.16em',
          display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8,
        }}>
          <Iq width={14} height={14} strokeWidth="2.5"><path d="M12 5v14M5 12h14"/></Iq>
          ADD A HABIT TO QUIT
        </button>
        <div style={{ height: 30 }} />
      </div>
      <ATabBar active="home" />
    </AWrap>
  );
}

// ───────────────────────────────────────────────────────
// 2) HABIT DETAIL  — bottom sheet over the discipline list
// ───────────────────────────────────────────────────────
function A_HabitDetail() {
  // 30-day heatmap (1 = done, 0 = missed, null = future)
  const days = [
    1,1,0,1,1,1,1,
    1,0,1,1,1,1,1,
    0,1,1,1,0,1,1,
    1,1,1,1,1,0,1,
    1,1,null,null,
  ];

  return (
    <div style={{ position: 'relative', width: '100%', height: '100%', overflow: 'hidden', background: A.bg }}>
      {/* dimmed discipline list behind */}
      <div style={{ opacity: 0.4, pointerEvents: 'none' }}><A_Discipline /></div>
      <div style={{ position: 'absolute', inset: 0, background: 'rgba(20,17,13,0.34)' }} />

      {/* sheet */}
      <div style={{
        position: 'absolute', left: 0, right: 0, bottom: 0,
        background: A.card, borderRadius: '24px 24px 0 0',
        padding: '12px 18px 36px', boxShadow: '0 -8px 30px rgba(20,17,13,0.25)',
        maxHeight: '86%', overflowY: 'auto',
      }}>
        <div style={{ display: 'flex', justifyContent: 'center', padding: '4px 0 12px' }}>
          <div style={{ width: 38, height: 4, borderRadius: 2, background: A.ink3, opacity: 0.45 }} />
        </div>

        {/* header */}
        <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 14 }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
            <div style={{ width: 46, height: 46, borderRadius: 13, background: '#EAF2FF', display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 22 }}>🚿</div>
            <div>
              <div style={{ fontFamily: A.display, fontWeight: 800, fontSize: 20, color: A.ink, letterSpacing: '0.01em', lineHeight: 1 }}>COLD SHOWERS</div>
              <div style={{ marginTop: 5 }}><CPill tone="good">+5% DISCIPLINE</CPill></div>
            </div>
          </div>
          <div style={{ width: 34, height: 34, borderRadius: 11, background: A.rest, display: 'flex', alignItems: 'center', justifyContent: 'center', color: A.ink }}>
            <Iq width={16} height={16} strokeWidth="2.5"><path d="M6 6l12 12M18 6L6 18"/></Iq>
          </div>
        </div>

        {/* stat grid */}
        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(4,1fr)', gap: 8 }}>
          {[
            { v: '4', l: 'STREAK', fire: true },
            { v: '21', l: 'BEST' },
            { v: '26', l: 'TOTAL' },
            { v: '88%', l: 'RATE' },
          ].map((s) => (
            <div key={s.l} style={{ background: A.rest, borderRadius: 14, padding: '12px 6px', textAlign: 'center' }}>
              <div style={{ fontFamily: A.display, fontWeight: 800, fontSize: 22, color: A.ink, lineHeight: 1, display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 2 }}>
                {s.fire && <span style={{ color: A.warn, fontSize: 12 }}><IFire /></span>}{s.v}
              </div>
              <div style={{ fontSize: 9, color: A.ink3, fontWeight: 700, letterSpacing: '0.08em', marginTop: 5 }}>{s.l}</div>
            </div>
          ))}
        </div>

        {/* heatmap */}
        <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', margin: '20px 2px 10px' }}>
          <div className="eyebrow">LAST 30 DAYS</div>
          <div style={{ fontSize: 10.5, color: A.ink3, fontWeight: 600 }}>26 DONE · 4 MISSED</div>
        </div>
        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(7,1fr)', gap: 6 }}>
          {days.map((d, i) => (
            <div key={i} style={{
              aspectRatio: '1', borderRadius: 7,
              background: d === 1 ? A.ink : d === 0 ? '#FCE6E6' : A.rest,
              border: i === 29 ? `1.5px solid ${A.hype}` : 'none',
              opacity: d === null ? 0.5 : 1,
              display: 'flex', alignItems: 'center', justifyContent: 'center',
            }}>
              {d === 1 && <span style={{ color: A.hype, display: 'flex' }}><ICheck /></span>}
              {d === 0 && <span style={{ color: A.bad, fontSize: 10, fontWeight: 800 }}>·</span>}
            </div>
          ))}
        </div>

        {/* why it matters */}
        <div style={{
          marginTop: 18, padding: '14px 16px', borderRadius: 14, background: '#FFFBEC',
          border: `1px solid ${A.rule}`, display: 'flex', gap: 12, alignItems: 'flex-start',
        }}>
          <span style={{ fontSize: 18, lineHeight: 1 }}>🧊</span>
          <div>
            <div style={{ fontFamily: A.display, fontWeight: 700, fontSize: 13, color: A.ink, letterSpacing: '0.04em' }}>WHY THIS MATTERS</div>
            <div style={{ fontSize: 12.5, color: A.ink2, marginTop: 3, lineHeight: 1.45 }}>
              It increases your testosterone production, reduces stress, clears brain fog, and increases blood flow.
            </div>
            <div style={{ fontSize: 11.5, color: A.good, fontWeight: 700, marginTop: 6 }}>
              You're doing better than 74% of people
            </div>
          </div>
        </div>

        {/* actions */}
        <div style={{ display: 'flex', gap: 8, marginTop: 16 }}>
          <button style={{
            width: 52, height: 52, borderRadius: 14, border: `1px solid ${A.rule}`,
            background: A.card, color: A.ink2, display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0,
          }}>
            <Iq width={18} height={18}><path d="M12 20h9M16.5 3.5a2.1 2.1 0 013 3L7 19l-4 1 1-4 12.5-12.5z"/></Iq>
          </button>
          <button style={{
            width: 52, height: 52, borderRadius: 14, border: `1px solid ${A.rule}`,
            background: A.card, color: A.bad, display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0,
          }}>
            <Iq width={18} height={18}><path d="M3 6h18M19 6l-2 14a2 2 0 01-2 2H9a2 2 0 01-2-2L5 6M10 11v6M14 11v6M8 6V4a2 2 0 012-2h4a2 2 0 012 2v2"/></Iq>
          </button>
          <button style={{
            flex: 1, padding: '0 0', height: 52, borderRadius: 14, border: 'none',
            background: A.ink, color: A.hype,
            fontFamily: A.display, fontWeight: 700, fontSize: 13.5, letterSpacing: '0.18em',
            display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 10,
            boxShadow: '0 6px 18px rgba(20,17,13,0.2)',
          }}>
            MARK DONE TODAY
            <span style={{ width: 24, height: 24, borderRadius: 12, background: A.hype, color: A.ink, display: 'inline-flex', alignItems: 'center', justifyContent: 'center' }}>
              <ICheck />
            </span>
          </button>
        </div>
      </div>
    </div>
  );
}

// ───────────────────────────────────────────────────────
// 3) CHALLENGE DETAIL — bottom sheet over the challenges list,
//    with the crowd drop-off graph + an AI analysis CTA.
//    Reuses InsSection + SurvivalChart from insights.jsx.
// ───────────────────────────────────────────────────────
function A_ChallengeDetail() {
  return (
    <div style={{ position: 'relative', width: '100%', height: '100%', overflow: 'hidden', background: A.bg }}>
      {/* dimmed challenges list behind */}
      <div style={{ opacity: 0.4, pointerEvents: 'none' }}><A_Challenges /></div>
      <div style={{ position: 'absolute', inset: 0, background: 'rgba(20,17,13,0.34)' }} />

      {/* sheet */}
      <div style={{
        position: 'absolute', left: 0, right: 0, bottom: 0,
        background: A.card, borderRadius: '24px 24px 0 0',
        padding: '12px 18px 36px', boxShadow: '0 -8px 30px rgba(20,17,13,0.25)',
        maxHeight: '90%', overflowY: 'auto',
      }}>
        <div style={{ display: 'flex', justifyContent: 'center', padding: '4px 0 12px' }}>
          <div style={{ width: 38, height: 4, borderRadius: 2, background: A.ink3, opacity: 0.45 }} />
        </div>

        {/* header */}
        <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 14 }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
            <div style={{ width: 46, height: 46, borderRadius: 13, background: '#FFF1DC', display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 22 }}>🏃</div>
            <div>
              <div style={{ fontFamily: A.display, fontWeight: 800, fontSize: 20, color: A.ink, letterSpacing: '0.01em', lineHeight: 1 }}>PHYSICAL ACTIVITY</div>
              <div style={{ marginTop: 5 }}><CPill tone="warn">HEALTH · 21 DAYS</CPill></div>
            </div>
          </div>
          <div style={{ width: 34, height: 34, borderRadius: 11, background: A.rest, display: 'flex', alignItems: 'center', justifyContent: 'center', color: A.ink }}>
            <Iq width={16} height={16} strokeWidth="2.5"><path d="M6 6l12 12M18 6L6 18"/></Iq>
          </div>
        </div>

        {/* description */}
        <div style={{ fontSize: 12.5, color: A.ink2, lineHeight: 1.5 }}>
          Move for at least 20 minutes, every day for 21 days. Walk, run, lift — anything that raises your heart rate counts.
        </div>

        {/* the graph — where people drop off */}
        <div style={{ marginTop: 18 }}>
          <InsSection>The drop-off curve</InsSection>
          <SurvivalChart />
        </div>

        {/* transformation — what this says about who you're becoming */}
        <div style={{ background: A.ink, borderRadius: 22, padding: '18px 18px 20px', marginTop: 18 }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
            <span style={{
              width: 30, height: 30, borderRadius: 10, background: A.hype, color: A.ink,
              display: 'flex', alignItems: 'center', justifyContent: 'center', flex: '0 0 auto',
            }}><ISpark /></span>
            <div style={{ fontFamily: A.mono, fontSize: 10.5, fontWeight: 600, letterSpacing: '0.14em', color: A.hype, textTransform: 'uppercase' }}>the transformation</div>
          </div>
          <div style={{ fontFamily: A.display, fontWeight: 800, fontSize: 19, color: '#fff', marginTop: 14, lineHeight: 1.25 }}>
            You stayed consistent when most people quit.
          </div>
          <div style={{ marginTop: 14, display: 'flex', flexDirection: 'column', gap: 10 }}>
            {[
              'The limits you believed in are fading.',
              "You've proven more to yourself this month than last.",
              'Hard things are becoming normal.',
            ].map((line) => (
              <div key={line} style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
                <span style={{ color: A.hype, flex: '0 0 auto' }}><ICheck /></span>
                <div style={{ fontFamily: A.body, fontSize: 13.5, color: 'rgba(255,255,255,0.85)', lineHeight: 1.4 }}>{line}</div>
              </div>
            ))}
          </div>
        </div>

        {/* AI analysis CTA */}
        <button style={{
          width: '100%', marginTop: 16, height: 52, padding: 0, border: 'none', borderRadius: 14,
          background: A.ink, color: A.hype,
          fontFamily: A.display, fontWeight: 700, fontSize: 13, letterSpacing: '0.18em',
          display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 10,
          boxShadow: '0 6px 18px rgba(20,17,13,0.2)',
        }}>
          <ISpark /> AI ANALYSIS
        </button>
      </div>
    </div>
  );
}

Object.assign(window, { A_Control, A_HabitDetail, A_ChallengeDetail });
