/* Flow C — morning growth nudge. */

function ScreenC1({ onSarthiHoldStart, openCoach, showFabLabel, holding }) {
  return (
    <div className="screen-in absolute inset-0 bg-surface flex flex-col">
      <StatusBar/>
      <TopHeader name="Rajesh"/>
      <div className="flex-1 overflow-y-auto no-scrollbar px-4 pt-4 pb-28">

        <div className="rounded-2xl bg-white border border-ink-200 p-3.5 shadow-card flex items-center gap-3">
          <div className="w-11 h-11 rounded-xl bg-amber-100 grid place-items-center text-[20px]">☀️</div>
          <div className="flex-1">
            <div className="text-[14px] font-bold text-ink-900">Good morning</div>
            <div className="text-[11px] text-ink-500">Tuesday · 26 May · Hadapsar zone</div>
          </div>
          <div className="text-right">
            <div className="text-[10px] text-ink-500 font-semibold uppercase tracking-wider">Shift starts</div>
            <div className="text-[14px] font-bold text-ink-900 tabular-nums">10:00 AM</div>
          </div>
        </div>

        <div className="mt-3 rounded-2xl p-4 relative overflow-hidden"
             style={{
               background: 'linear-gradient(135deg, #FFF4EC 0%, #FFF7ED 60%, #FEF3C7 100%)',
               border: '1px solid #FED7AA',
             }}>
          <div className="absolute -right-6 -top-6 w-28 h-28 rounded-full"
               style={{ background: 'radial-gradient(circle, rgba(252,128,25,0.15) 0%, rgba(252,128,25,0) 70%)' }}/>
          <div className="flex items-start gap-3">
            <SarthiOrb size={44}/>
            <div className="flex-1">
              <div className="flex items-center gap-1.5">
                <span className="text-[11px] font-bold text-sarthi-deep uppercase tracking-wider">Sarthi · Morning brief</span>
                <span className="px-1.5 py-0.5 rounded-full bg-orange-100 text-orange-800 text-[9px] font-bold uppercase tracking-wider">New</span>
              </div>
              <div className="mt-1.5 text-[15px] font-bold text-ink-900 leading-snug deva">
                Kal raat ka mushkil order?
              </div>
              <div className="text-[12px] text-ink-600 mt-0.5 deva">
                Top partners kya karte hain — 2 min suno.
              </div>

              <div className="mt-3 flex items-center gap-2">
                <button onClick={openCoach}
                  className="guide-c1-audio h-10 px-3.5 rounded-full bg-white border-2 border-sarthi-from text-sarthi-deep font-semibold text-[13px] press flex items-center gap-1.5 shadow-card">
                  <Play size={13} stroke="#FC8019" fill="#FC8019"/>
                  Play audio · 2 min
                </button>
                <span className="text-[12px] font-semibold text-ink-400">Later</span>
              </div>
            </div>
          </div>
        </div>

        <div className="mt-3 rounded-2xl bg-white border border-ink-200 p-3.5 shadow-card">
          <div className="flex items-center justify-between mb-2">
            <div className="text-[13px] font-bold text-ink-900">Today's plan</div>
            <span className="text-[11px] font-semibold text-ink-400 flex items-center gap-1">Adjust <ChevR size={12} stroke="#94A3B8"/></span>
          </div>
          <div className="grid grid-cols-3 gap-2">
            <div className="rounded-xl bg-ink-50 p-2.5">
              <div className="text-[10px] text-ink-500 font-medium">Target</div>
              <div className="text-[16px] font-extrabold text-ink-900 tabular-nums">₹650</div>
              <div className="text-[10px] text-ink-500">10 orders</div>
            </div>
            <div className="rounded-xl bg-orange-50 p-2.5 border border-orange-100">
              <div className="text-[10px] text-orange-700 font-medium">Hot zone</div>
              <div className="text-[14px] font-extrabold text-orange-900">Magarpatta</div>
              <div className="text-[10px] text-orange-700">+30% orders</div>
            </div>
            <div className="rounded-xl bg-amber-50 p-2.5 border border-amber-100">
              <div className="text-[10px] text-amber-700 font-medium">Weather</div>
              <div className="text-[14px] font-extrabold text-amber-900">Light rain</div>
              <div className="text-[10px] text-amber-700">11 AM – 1 PM</div>
            </div>
          </div>
        </div>

        <div className="mt-3 rounded-2xl bg-white border border-ink-200 p-3.5 shadow-card">
          <div className="flex items-center justify-between">
            <div>
              <div className="text-[10px] font-semibold text-ink-500 uppercase tracking-wider">This week</div>
              <div className="text-[22px] font-extrabold text-ink-900 tabular-nums">₹3,840</div>
              <div className="text-[11px] text-sw-orange-dark font-semibold">+₹420 vs last week</div>
            </div>
            <svg viewBox="0 0 140 56" className="h-12 w-32">
              {[28,36,22,40,30,46,38].map((h, i) => (
                <rect key={i} x={i * 20 + 2} y={56 - h} width="12" height={h} rx="3"
                  fill={i === 5 ? '#FC8019' : '#FFEDD5'}/>
              ))}
            </svg>
          </div>
        </div>

        <div className="mt-3 rounded-2xl bg-gradient-to-br from-orange-50 to-amber-50 border border-amber-200 p-3.5 shadow-card flex items-center gap-3">
          <div className="text-[28px]">🔥</div>
          <div className="flex-1">
            <div className="text-[14px] font-bold text-amber-900">5-day streak active</div>
            <div className="text-[11px] text-amber-700">2 more days for ₹150 weekly bonus</div>
          </div>
          <div className="text-right">
            <div className="text-[18px] font-extrabold text-amber-900 tabular-nums">5</div>
            <div className="text-[10px] text-amber-700 font-semibold">/ 7 days</div>
          </div>
        </div>

      </div>

      <SarthiFAB onHoldStart={onSarthiHoldStart} showFabLabel={showFabLabel} holding={holding}/>
      <BottomNav active="home"/>
    </div>
  );
}

function ScreenC2Sheet({ onDismiss, onCrossSell }) {
  const [playing, setPlaying] = _us(false);
  const [progress, setProgress] = _us(0);
  const [duration, setDuration] = _us(0);
  const [voiceReady, setVoiceReady] = _us(false);
  const tickRef = React.useRef(null);

  const attachProgress = (audio) => {
    if (!audio || tickRef.current) return;
    const update = () => {
      if (audio.duration && isFinite(audio.duration)) {
        setDuration(audio.duration);
        setProgress(audio.currentTime);
        setVoiceReady(true);
      }
      if (audio.ended) setPlaying(false);
    };
    tickRef.current = setInterval(update, 200);
    update();
  };

  _ue(() => {
    let cancelled = false;

    const t = setTimeout(() => {
      Voice.speak(DEMO.C2_COACHING, {
        onReady: (audio) => {
          if (!cancelled) attachProgress(audio);
        },
        onStart: (audio) => {
          if (!cancelled) {
            setPlaying(true);
            attachProgress(audio);
          }
        },
        onEnd: () => { if (!cancelled) setPlaying(false); },
        onError: () => { if (!cancelled) setVoiceReady(false); },
      });
    }, 120);

    return () => {
      cancelled = true;
      clearTimeout(t);
      if (tickRef.current) clearInterval(tickRef.current);
      Voice.stopSpeaking();
    };
  }, []);

  const togglePlay = () => {
    const audio = Voice.getCurrentAudio();
    if (!audio) {
      Voice.speak(DEMO.C2_COACHING, { onError: () => {} });
      setPlaying(true);
      return;
    }
    if (playing) {
      audio.pause();
      setPlaying(false);
    } else {
      audio.play();
      setPlaying(true);
    }
  };

  const mm = (s) => `${Math.floor(s / 60)}:${String(Math.floor(s) % 60).padStart(2, '0')}`;
  const pct = duration > 0 ? (progress / duration) * 100 : 0;

  return (
    <div className="absolute inset-0 z-40 backdrop-in" style={{ background: 'rgba(8,5,18,0.55)', backdropFilter: 'blur(6px)' }}>
      <div className="absolute inset-x-0 bottom-0 sheet-in">
        <div className="rounded-t-[28px] bg-white px-5 pt-5 pb-7 shadow-lift">
          <div className="absolute left-1/2 -translate-x-1/2 top-2 w-10 h-1.5 rounded-full bg-ink-200"></div>

          <div className="mt-2 flex items-center gap-3">
            <SarthiOrb size={56}/>
            <div className="flex-1">
              <div className="text-[11px] font-bold text-sarthi-deep uppercase tracking-wider">Sarthi · Coaching</div>
              <div className="text-[16px] font-bold text-ink-900 leading-tight deva mt-0.5">
                Mushkil customer — kya karein
              </div>
            </div>
            <button onClick={() => { Voice.stopSpeaking(); onDismiss(); }}
                    className="w-8 h-8 rounded-full bg-ink-100 grid place-items-center press">
              <X size={16}/>
            </button>
          </div>

          <div className="mt-5 rounded-2xl bg-gradient-to-br from-orange-50 to-ink-50 border border-orange-100 p-4">
            <div className="flex items-center gap-3">
              <button onClick={togglePlay}
                className="w-12 h-12 rounded-full grid place-items-center text-white press shadow-orb orb-grad">
                {playing ? <Pause size={18} stroke="#fff"/> : <Play size={18} stroke="#fff"/>}
              </button>
              <div className="flex-1">
                <div className="h-1.5 rounded-full bg-ink-200 overflow-hidden">
                  <div className="h-full rounded-full transition-all duration-200"
                       style={{ width: `${pct}%`, background: 'linear-gradient(90deg, #FF8A3D, #FC8019)' }}/>
                </div>
                <div className="flex justify-between text-[10px] text-ink-500 font-medium mt-1.5 tabular-nums">
                  <span>{voiceReady ? mm(progress) : '0:00'}</span>
                  <span>{voiceReady && duration ? mm(duration) : '—'}</span>
                </div>
              </div>
            </div>
            <div className="mt-3 flex items-end justify-center gap-1 h-6">
              {[0,1,2,3,4,5,6,7,8,9,10,11].map(i => (
                <div key={i} className={`rounded-full ${playing || !voiceReady ? 'wave-bar' : ''}`}
                     style={{
                       width: 3, height: 6 + (i % 4) * 5,
                       background: 'linear-gradient(180deg, #FFB07A, #FC8019)',
                       animationDelay: `${i * 55}ms`,
                       opacity: (playing || !voiceReady) ? 1 : 0.4,
                     }}/>
              ))}
            </div>
          </div>

          <div className="mt-4">
            <div className="text-[11px] font-bold text-ink-500 uppercase tracking-wider">Key takeaways</div>
            <div className="mt-2 space-y-2">
              {[
                'Shaant raho — tone mat copy karo.',
                'Pehle "sir" ya "madam" bolo.',
                'Gaali ho toh Sarthi incident file karo.',
              ].map((line, i) => (
                <div key={i} className="flex gap-2.5 rounded-xl bg-ink-50 px-3 py-2.5">
                  <span className="w-5 h-5 rounded-full bg-orange-100 grid place-items-center shrink-0 text-[10px] font-bold text-sarthi-deep">{i + 1}</span>
                  <div className="text-[13px] font-semibold text-ink-900 deva leading-snug">{line}</div>
                </div>
              ))}
            </div>
          </div>

          <button onClick={onCrossSell}
            className="mt-4 w-full rounded-2xl bg-ink-900 text-white p-3.5 flex items-center gap-3 press shadow-card">
            <div className="w-9 h-9 rounded-xl bg-white/10 grid place-items-center"><Bolt size={18} stroke="#FBBF24"/></div>
            <div className="flex-1 text-left">
              <div className="text-[13px] font-semibold">Samajh gaya — Instamart earnings?</div>
              <div className="text-[11px] text-white/60">Category compare (demo)</div>
            </div>
            <ChevR size={18} stroke="#fff"/>
          </button>

          <div className="text-center text-[10px] text-ink-500 mt-3">Sarthi will check in tomorrow morning</div>
        </div>
      </div>
    </div>
  );
}

Object.assign(window, { ScreenC1, ScreenC2Sheet });
