// tow-screens-b.jsx — Results hub (Offers / Map / Dispatcher), Confirm sheet, Tracking

const RADIUS_PX = { 3: 70, 5: 105, 10: 150, 20: 200 };

// ── RESULTS HUB ──────────────────────────────────────────────────────
function Results({ go, count, cardStyle, defaultRadius, onPick }) {
  const accent = window.useAccent();
  const [tab, setTab] = React.useState('offers');
  const TABS = [
    { id: 'offers', label: 'Офферы', icon: 'thumbs-up' },
    { id: 'map', label: 'Карта', icon: 'map' },
    { id: 'dispatch', label: 'Диспетчер', icon: 'headset' },
  ];
  return (
    <div data-screen-label="Результаты" style={{ flex: 1, display: 'flex', flexDirection: 'column', minHeight: 0, background: '#fff' }}>
      <window.StatusBar/>
      <window.SubHeader
        title="Эвакуаторы рядом"
        subtitle="Алматы · Самал-2"
        onBack={() => go('hero')}
        right={
          <button onClick={() => go('searching')} style={{ border: 'none', background: C.surface, width: 36, height: 36, borderRadius: 999, display: 'grid', placeItems: 'center', cursor: 'pointer' }}>
            <window.TIcon name="refresh" size={17} color={C.text}/>
          </button>
        }
      />
      {/* segmented */}
      <div style={{ padding: '0 16px 10px' }}>
        <div style={{ display: 'flex', gap: 4, padding: 4, background: C.surface, borderRadius: 13 }}>
          {TABS.map(t => {
            const on = tab === t.id;
            return (
              <button key={t.id} onClick={() => setTab(t.id)} style={{
                flex: 1, height: 38, borderRadius: 10, border: 'none', cursor: 'pointer',
                background: on ? '#fff' : 'transparent', color: on ? accent : C.textMuted,
                fontFamily: 'Inter', fontSize: 13, fontWeight: on ? 700 : 600,
                display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 6,
                boxShadow: on ? '0 1px 4px rgba(0,0,0,0.1)' : 'none',
              }}>
                <window.TIcon name={t.icon} size={15} color={on ? accent : C.textMuted} strokeWidth={2}/>
                {t.label}
              </button>
            );
          })}
        </div>
      </div>

      {tab === 'offers' && <OffersTab count={count} cardStyle={cardStyle} accent={accent} onPick={onPick}/>}
      {tab === 'map' && <MapTab count={count} defaultRadius={defaultRadius} accent={accent} onPick={onPick}/>}
      {tab === 'dispatch' && <DispatcherTab count={count} accent={accent} onPick={onPick}/>}
    </div>
  );
}
window.Results = Results;

// ── OFFERS TAB ───────────────────────────────────────────────────────
function OffersTab({ count, cardStyle, accent, onPick }) {
  const tows = window.TOWS.slice(0, count);
  const [sort, setSort] = React.useState('eta');
  const sorted = [...tows].sort((a, b) => sort === 'eta' ? a.eta - b.eta : sort === 'price' ? a.price - b.price : b.rating - a.rating);
  const SORTS = [['eta', 'Быстрее'], ['price', 'Дешевле'], ['rating', 'Рейтинг']];

  return (
    <div style={{ flex: 1, overflowY: 'auto', padding: '0 16px 20px' }}>
      <div style={{ padding: '0 2px 10px' }}><window.ProtectBanner compact/></div>

      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 10 }}>
        <div style={{ fontFamily: 'Inter', fontWeight: 800, fontSize: 15, color: C.text }}>
          {count} {count === 1 ? 'предложение' : count < 5 ? 'предложения' : 'предложений'}
        </div>
        <div style={{ display: 'flex', gap: 4 }}>
          {SORTS.map(([id, label]) => {
            const on = sort === id;
            return (
              <button key={id} onClick={() => setSort(id)} style={{
                padding: '6px 11px', borderRadius: 999, cursor: 'pointer', fontFamily: 'Inter', fontSize: 12, fontWeight: on ? 700 : 600,
                border: 'none', background: on ? window.tint(accent, 0.1) : 'transparent', color: on ? accent : C.textMuted,
              }}>{label}</button>
            );
          })}
        </div>
      </div>

      <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
        {sorted.map((tow) => (
          <window.TowCard key={tow.id} tow={tow} cardStyle={cardStyle} accent={accent} onSend={() => onPick(tow, {})}/>
        ))}
      </div>

      <div style={{ marginTop: 14, textAlign: 'center', fontFamily: 'Inter', fontSize: 12.5, color: C.textFaint, display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 6 }}>
        <span style={{ width: 6, height: 6, borderRadius: 999, background: C.greenSolid }}/>
        Предложения обновляются в реальном времени
      </div>
    </div>
  );
}

// ── MAP TAB ──────────────────────────────────────────────────────────
function MapTab({ count, defaultRadius, accent, onPick }) {
  const all = window.TOWS.slice(0, count);
  const [radius, setRadius] = React.useState(defaultRadius);
  const [selId, setSelId] = React.useState(null);
  const inRadius = all.filter(t => t.dist <= radius);
  const selected = inRadius.find(t => t.id === selId);

  return (
    <div style={{ flex: 1, position: 'relative', minHeight: 0, display: 'flex', flexDirection: 'column' }}>
      {/* radius selector */}
      <div style={{ padding: '0 16px 10px' }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 8 }}>
          <span style={{ fontFamily: 'Inter', fontSize: 12, fontWeight: 700, color: C.textMuted, textTransform: 'uppercase', letterSpacing: 0.4 }}>Радиус поиска</span>
          <span style={{ fontFamily: 'Inter', fontSize: 12, color: C.textFaint, marginLeft: 'auto' }}>{inRadius.length} онлайн в радиусе</span>
        </div>
        <window.RadiusSelector value={radius} onChange={r => { setRadius(r); setSelId(null); }} accent={accent}/>
      </div>

      {/* map */}
      <div style={{ flex: 1, position: 'relative', overflow: 'hidden', minHeight: 0 }}>
        <window.TowMap tows={inRadius} selectedId={selId} onSelect={setSelId} radiusPx={RADIUS_PX[radius]} accent={accent}/>

        {/* selected floating card */}
        {selected ? (
          <div style={{ position: 'absolute', left: 12, right: 12, bottom: 12, animation: 'tow-rise .25s both' }}>
            <FloatingTow tow={selected} accent={accent} onClose={() => setSelId(null)} onSend={() => onPick(selected, {})}/>
            <style>{`@keyframes tow-rise { from { opacity: 0; transform: translateY(10px) } to { opacity: 1; transform: none } }`}</style>
          </div>
        ) : (
          <div style={{
            position: 'absolute', left: 12, right: 12, bottom: 12, padding: '12px 14px', borderRadius: 14,
            background: 'rgba(255,255,255,0.95)', backdropFilter: 'blur(8px)', boxShadow: '0 8px 24px rgba(0,0,0,0.12)',
            fontFamily: 'Inter', fontSize: 13, color: C.textMuted, display: 'flex', alignItems: 'center', gap: 9,
          }}>
            <window.TIcon name="pin" size={18} color={accent}/>
            Нажмите на эвакуатор на карте, чтобы выбрать
          </div>
        )}
      </div>
    </div>
  );
}

function FloatingTow({ tow, accent, onClose, onSend }) {
  return (
    <div style={{ background: '#fff', borderRadius: 18, padding: 14, boxShadow: '0 14px 36px rgba(0,0,0,0.2)', fontFamily: 'Inter' }}>
      <div style={{ display: 'flex', alignItems: 'flex-start', gap: 12 }}>
        <window.TowAvatar tone={tow.tone} size={46} radius={13}/>
        <div style={{ flex: 1, minWidth: 0 }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
            <span style={{ fontWeight: 700, fontSize: 15, color: C.text }}>{tow.name}</span>
            {tow.verified && <window.Verified accent={accent}/>}
          </div>
          <div style={{ marginTop: 3 }}><window.Rating value={tow.rating} orders={tow.orders}/></div>
        </div>
        <button onClick={onClose} style={{ border: 'none', background: C.surface, width: 28, height: 28, borderRadius: 999, display: 'grid', placeItems: 'center', cursor: 'pointer', flexShrink: 0 }}>
          <window.Icon name="x" size={14} color={C.text}/>
        </button>
      </div>
      <div style={{ display: 'flex', gap: 14, margin: '12px 0', fontSize: 12.5, color: C.textMuted }}>
        <span><b style={{ color: C.text }}>{tow.eta} мин</b> подача</span>
        <span>·</span><span>{tow.dist} км</span>
        <span style={{ marginLeft: 'auto', color: accent, fontWeight: 800 }}>≈ {window.fmtTg(tow.price)}</span>
      </div>
      <button onClick={onSend} style={{
        width: '100%', height: 44, borderRadius: 12, border: 'none', cursor: 'pointer', background: accent, color: '#fff',
        fontFamily: 'Inter', fontSize: 14, fontWeight: 700, display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 7,
      }}>
        Отправить запрос <window.Icon name="arrow-right" size={15} color="#fff" strokeWidth={2.2}/>
      </button>
    </div>
  );
}

// ── DISPATCHER TAB ───────────────────────────────────────────────────
function DispatcherTab({ count, accent, onPick }) {
  const best = window.TOWS.slice(0, count).reduce((a, b) => (a.eta <= b.eta ? a : b));
  const steps = [
    { icon: 'check', label: 'Заявка принята диспетчером', done: true },
    { icon: 'sliders', label: `Анализирует ${count} вариантов рядом`, active: true },
    { icon: 'tag', label: 'Согласует стоимость и подачу' },
    { icon: 'shield-check', label: 'Контролирует выполнение заказа' },
  ];
  return (
    <div style={{ flex: 1, overflowY: 'auto', padding: '0 16px 20px' }}>
      {/* dispatcher hero card */}
      <div style={{ background: C.navy, borderRadius: 20, padding: 18, color: '#fff', position: 'relative', overflow: 'hidden' }}>
        <div style={{ position: 'absolute', right: -30, top: -30, width: 140, height: 140, borderRadius: 999, background: window.tint(accent === 'rgb(24,32,87)' ? '#ffffff' : accent, 0.1) }}/>
        <div style={{ display: 'flex', alignItems: 'center', gap: 12, position: 'relative' }}>
          <div style={{ width: 50, height: 50, borderRadius: 15, background: 'rgba(255,255,255,0.12)', display: 'grid', placeItems: 'center' }}>
            <window.TIcon name="headset" size={26} color={C.yellow} strokeWidth={1.8}/>
          </div>
          <div style={{ flex: 1 }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 7 }}>
              <span style={{ fontFamily: 'Inter', fontWeight: 800, fontSize: 17 }}>Диспетчер AIDAPP</span>
              <span style={{ width: 7, height: 7, borderRadius: 999, background: C.greenSolid, boxShadow: '0 0 0 4px rgba(20,174,92,0.25)' }}/>
            </div>
            <div style={{ fontFamily: 'Inter', fontSize: 12.5, opacity: 0.7, marginTop: 2 }}>Подбирает оптимальный эвакуатор за вас</div>
          </div>
        </div>
        <div style={{ display: 'flex', gap: 8, marginTop: 14, position: 'relative' }}>
          {[['Среднее время', '~2 мин'], ['Сэкономлено', 'до 15%'], ['Контроль', '24/7']].map(([k, v]) => (
            <div key={k} style={{ flex: 1, background: 'rgba(255,255,255,0.08)', borderRadius: 12, padding: '9px 10px' }}>
              <div style={{ fontFamily: 'Inter', fontWeight: 800, fontSize: 15, color: C.yellow }}>{v}</div>
              <div style={{ fontFamily: 'Inter', fontSize: 10.5, opacity: 0.7, marginTop: 1 }}>{k}</div>
            </div>
          ))}
        </div>
      </div>

      {/* live status */}
      <div style={{ marginTop: 14, background: '#fff', border: `1px solid ${C.hairline}`, borderRadius: 18, padding: 16 }}>
        <div style={{ fontFamily: 'Inter', fontWeight: 700, fontSize: 13, color: C.textMuted, textTransform: 'uppercase', letterSpacing: 0.4, marginBottom: 12 }}>Статус подбора</div>
        {steps.map((s, i) => (
          <div key={i} style={{ display: 'flex', alignItems: 'center', gap: 12, padding: '7px 0' }}>
            <div style={{
              width: 30, height: 30, borderRadius: 9, flexShrink: 0, display: 'grid', placeItems: 'center',
              background: s.done ? window.tint(C.greenSolid, 0.14) : s.active ? window.tint(accent, 0.1) : C.surface,
            }}>
              {s.done
                ? <window.Icon name="check" size={15} color={C.greenText} strokeWidth={2.6}/>
                : <window.TIcon name={s.icon} size={15} color={s.active ? accent : C.textFaint} strokeWidth={2}/>}
            </div>
            <div style={{ flex: 1, fontFamily: 'Inter', fontSize: 13.5, fontWeight: s.active ? 700 : 600, color: s.done || s.active ? C.text : C.textMuted }}>{s.label}</div>
            {s.active && (
              <span style={{ display: 'inline-flex', gap: 3 }}>
                {[0, 1, 2].map(d => <span key={d} style={{ width: 5, height: 5, borderRadius: 999, background: accent, animation: `tow-bounce 1.2s ${d * 0.15}s infinite` }}/>)}
                <style>{`@keyframes tow-bounce { 0%,80%,100% { opacity: .4 } 40% { opacity: 1 } }`}</style>
              </span>
            )}
          </div>
        ))}
      </div>

      <div style={{ marginTop: 14 }}><window.ProtectBanner compact/></div>

      <button onClick={() => onPick(best, { via: 'dispatcher' })} style={{
        width: '100%', height: 52, borderRadius: 16, border: 'none', cursor: 'pointer', marginTop: 14,
        background: accent, color: '#fff', fontFamily: 'Inter', fontSize: 15.5, fontWeight: 800,
        display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8,
      }}>
        <window.TIcon name="headset" size={18} color="#fff" strokeWidth={2}/>
        Доверить подбор диспетчеру
      </button>
      <div style={{ textAlign: 'center', fontFamily: 'Inter', fontSize: 12, color: C.textFaint, marginTop: 8 }}>
        Диспетчер согласует лучший вариант и подтвердит заказ
      </div>
    </div>
  );
}

// ── CONFIRM SHEET (overlay) ──────────────────────────────────────────
function ConfirmSheet({ tow, via, accent, onClose, onConfirm }) {
  if (!tow) return null;
  const dispatcher = via === 'dispatcher';
  return (
    <div onClick={onClose} style={{ position: 'absolute', inset: 0, zIndex: 200, background: 'rgba(0,0,0,0.45)', display: 'flex', flexDirection: 'column', justifyContent: 'flex-end' }}>
      <div onClick={e => e.stopPropagation()} style={{ background: '#fff', borderRadius: '22px 22px 0 0', padding: '8px 20px 22px', animation: 'tow-sheet .25s cubic-bezier(.3,.8,.4,1) both' }}>
        <style>{`@keyframes tow-sheet { from { transform: translateY(100%) } to { transform: none } }`}</style>
        <div style={{ width: 40, height: 4, borderRadius: 2, background: C.hairline, margin: '4px auto 16px' }}/>

        <div style={{ fontFamily: 'Inter', fontWeight: 800, fontSize: 20, color: C.text }}>
          {dispatcher ? 'Передать диспетчеру?' : 'Отправить запрос?'}
        </div>
        <div style={{ fontFamily: 'Inter', fontSize: 13.5, color: C.textMuted, marginTop: 4 }}>
          {dispatcher ? 'Диспетчер согласует и подтвердит заказ' : 'Эвакуатор увидит вашу заявку и подтвердит подачу'}
        </div>

        {/* tow summary */}
        <div style={{ display: 'flex', alignItems: 'center', gap: 12, padding: '14px 0', borderTop: `1px solid ${C.hairline}`, borderBottom: `1px solid ${C.hairline}`, margin: '16px 0' }}>
          <window.TowAvatar tone={tow.tone} size={50} radius={14}/>
          <div style={{ flex: 1, minWidth: 0 }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
              <span style={{ fontFamily: 'Inter', fontWeight: 700, fontSize: 16, color: C.text }}>{tow.name}</span>
              {tow.verified && <window.Verified accent={accent}/>}
            </div>
            <div style={{ marginTop: 3 }}><window.Rating value={tow.rating} orders={tow.orders}/></div>
          </div>
          <div style={{ textAlign: 'right' }}>
            <div style={{ fontFamily: 'Inter', fontWeight: 800, fontSize: 16, color: C.text }}>{tow.eta} мин</div>
            <div style={{ fontFamily: 'Inter', fontSize: 13, fontWeight: 700, color: accent }}>≈ {window.fmtTg(tow.price)}</div>
          </div>
        </div>

        {/* number reveal note */}
        <div style={{ display: 'flex', alignItems: 'center', gap: 10, padding: '11px 14px', borderRadius: 14, background: window.tint(accent, 0.06), marginBottom: 16 }}>
          <window.TIcon name="lock" size={17} color={accent} strokeWidth={2}/>
          <div style={{ fontFamily: 'Inter', fontSize: 12.5, color: C.text, lineHeight: 1.35 }}>
            Ваш номер откроется <b>только этому эвакуатору</b> после подтверждения. Остальные его не увидят.
          </div>
        </div>

        <button onClick={onConfirm} style={{
          width: '100%', height: 52, borderRadius: 16, border: 'none', cursor: 'pointer', background: accent, color: '#fff',
          fontFamily: 'Inter', fontSize: 16, fontWeight: 800, display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8,
        }}>
          <window.Icon name="check" size={19} color="#fff" strokeWidth={2.4}/>
          {dispatcher ? 'Передать диспетчеру' : 'Подтвердить выбор'}
        </button>
        <button onClick={onClose} style={{
          width: '100%', height: 46, borderRadius: 14, border: 'none', cursor: 'pointer', background: 'transparent', color: C.textMuted,
          fontFamily: 'Inter', fontSize: 14, fontWeight: 600, marginTop: 6,
        }}>Назад к списку</button>
      </div>
    </div>
  );
}
window.ConfirmSheet = ConfirmSheet;

// ── TRACKING ─────────────────────────────────────────────────────────
function Tracking({ go, tow, via, stage, setStage }) {
  const accent = window.useAccent();
  const cancelled = stage === 'cancelled';
  const idx = window.STATUS_FLOW.indexOf(stage);
  const hasTow = !cancelled && idx >= window.STATUS_FLOW.indexOf('accepted');
  const showCountdown = stage === 'assigned' || stage === 'arriving';
  const [eta, setEta] = React.useState(tow ? tow.eta : 12);
  React.useEffect(() => { setEta(stage === 'arriving' ? 3 : (tow ? tow.eta : 12)); }, [stage]);
  React.useEffect(() => {
    if (!showCountdown) return;
    const i = setInterval(() => setEta(e => Math.max(1, e - 1)), 1800);
    return () => clearInterval(i);
  }, [showCountdown]);

  return (
    <div data-screen-label="Трекинг" style={{ flex: 1, display: 'flex', flexDirection: 'column', minHeight: 0, background: '#fff' }}>
      <window.StatusBar/>
      <window.SubHeader
        title="Ваша заявка"
        subtitle={<>№ A-2481 · {cancelled ? 'отменена' : 'активна'}</>}
        onBack={() => go('hero')}
        right={<button style={{ border: 'none', background: C.surface, width: 36, height: 36, borderRadius: 999, display: 'grid', placeItems: 'center', cursor: 'pointer' }}><window.Icon name="more" size={18} color={C.text}/></button>}
      />

      <div style={{ flex: 1, overflowY: 'auto', padding: '0 20px 16px' }}>
        {/* status hero */}
        <div style={{
          borderRadius: 20, padding: 18, color: '#fff',
          background: cancelled ? C.redDeep : accent,
          position: 'relative', overflow: 'hidden',
        }}>
          <div style={{ position: 'absolute', right: -24, top: -24, width: 120, height: 120, borderRadius: 999, background: 'rgba(255,255,255,0.08)' }}/>
          <div style={{ position: 'relative' }}>
            <div style={{ fontFamily: 'Inter', fontSize: 12.5, opacity: 0.8 }}>{window.STATUS_META[stage].hint}</div>
            <div style={{ fontFamily: 'Inter', fontWeight: 800, fontSize: 23, marginTop: 3 }}>{window.STATUS_META[stage].label}</div>
            {showCountdown && (
              <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginTop: 12 }}>
                <window.Icon name="clock" size={18} color={C.yellow} strokeWidth={2}/>
                <span style={{ fontFamily: 'Inter', fontSize: 14 }}>Подача через</span>
                <span style={{ fontFamily: 'Inter', fontWeight: 800, fontSize: 20, color: C.yellow }}>~ {eta} мин</span>
              </div>
            )}
            {via === 'dispatcher' && !cancelled && (
              <div style={{ display: 'inline-flex', alignItems: 'center', gap: 6, marginTop: 12, padding: '5px 10px', borderRadius: 999, background: 'rgba(255,255,255,0.14)' }}>
                <window.TIcon name="headset" size={13} color="#fff" strokeWidth={2}/>
                <span style={{ fontFamily: 'Inter', fontSize: 12, fontWeight: 600 }}>Подобрано диспетчером AIDAPP</span>
              </div>
            )}
          </div>
        </div>

        {/* assigned tow card */}
        {hasTow && tow && (
          <div style={{ marginTop: 14, background: '#fff', border: `1px solid ${C.hairline}`, borderRadius: 18, padding: 14 }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
              <window.TowAvatar tone={tow.tone} size={50} radius={14}/>
              <div style={{ flex: 1, minWidth: 0 }}>
                <div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
                  <span style={{ fontFamily: 'Inter', fontWeight: 700, fontSize: 16, color: C.text }}>{tow.name}</span>
                  {tow.verified && <window.Verified accent={accent}/>}
                </div>
                <div style={{ fontFamily: 'Inter', fontSize: 12.5, color: C.textMuted, marginTop: 2 }}>{tow.driver} · {window.TOW_TYPES[tow.type].label}</div>
              </div>
              <div style={{ textAlign: 'right' }}>
                <div style={{ fontFamily: 'Inter', fontWeight: 800, fontSize: 15, color: C.text }}>≈ {window.fmtTg(tow.price)}</div>
                <div style={{ fontFamily: 'Inter', fontSize: 11.5, color: C.textMuted }}>ориент. цена</div>
              </div>
            </div>
            {/* contact now unlocked */}
            <div style={{ display: 'flex', gap: 8, marginTop: 14 }}>
              <button style={{ flex: 1, height: 46, borderRadius: 13, border: 'none', cursor: 'pointer', background: accent, color: '#fff', fontFamily: 'Inter', fontSize: 14, fontWeight: 700, display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 7 }}>
                <window.Icon name="phone" size={17} color="#fff" strokeWidth={2}/> Позвонить
              </button>
              <button style={{ flex: 1, height: 46, borderRadius: 13, border: `1px solid ${C.hairline}`, cursor: 'pointer', background: '#fff', color: C.text, fontFamily: 'Inter', fontSize: 14, fontWeight: 700, display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 7 }}>
                <window.Icon name="message-circle" size={17} color={C.text} strokeWidth={2}/> Чат
              </button>
            </div>
            <div style={{ marginTop: 12 }}><window.ProtectBanner unlocked compact/></div>
          </div>
        )}

        {/* timeline */}
        <div style={{ marginTop: 14, background: C.surface, borderRadius: 18, padding: '16px 16px 6px' }}>
          <div style={{ fontFamily: 'Inter', fontWeight: 700, fontSize: 12.5, color: C.textMuted, textTransform: 'uppercase', letterSpacing: 0.4, marginBottom: 6 }}>Этапы заявки</div>
          {window.STATUS_FLOW.map((st, i) => {
            const done = !cancelled && i < idx;
            const active = !cancelled && i === idx;
            const last = i === window.STATUS_FLOW.length - 1;
            return (
              <div key={st} style={{ display: 'flex', gap: 12 }}>
                <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
                  <div style={{
                    width: 24, height: 24, borderRadius: 999, flexShrink: 0, display: 'grid', placeItems: 'center',
                    background: done ? C.greenSolid : active ? '#fff' : '#fff',
                    border: active ? `2.5px solid ${accent}` : done ? 'none' : `2px solid ${C.hairline}`,
                  }}>
                    {done && <window.Icon name="check" size={14} color="#fff" strokeWidth={3}/>}
                    {active && <span style={{ width: 8, height: 8, borderRadius: 999, background: accent }}/>}
                  </div>
                  {!last && <div style={{ width: 2, flex: 1, minHeight: 18, background: done ? C.greenSolid : C.hairline, margin: '2px 0' }}/>}
                </div>
                <div style={{ paddingBottom: 14, paddingTop: 1 }}>
                  <div style={{ fontFamily: 'Inter', fontSize: 14, fontWeight: active ? 700 : 600, color: done || active ? C.text : C.textFaint }}>{window.STATUS_META[st].label}</div>
                  {active && <div style={{ fontFamily: 'Inter', fontSize: 12, color: C.textMuted, marginTop: 1 }}>{window.STATUS_META[st].hint}</div>}
                </div>
              </div>
            );
          })}
        </div>

        {/* actions */}
        {cancelled ? (
          <button onClick={() => go('hero')} style={{ width: '100%', height: 50, borderRadius: 15, border: 'none', cursor: 'pointer', background: accent, color: '#fff', fontFamily: 'Inter', fontSize: 15, fontWeight: 800, marginTop: 16 }}>
            Новый поиск
          </button>
        ) : stage === 'completed' ? (
          <button onClick={() => go('hero')} style={{ width: '100%', height: 50, borderRadius: 15, border: 'none', cursor: 'pointer', background: accent, color: '#fff', fontFamily: 'Inter', fontSize: 15, fontWeight: 800, marginTop: 16, display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8 }}>
            <window.Icon name="check-circle" size={19} color="#fff" strokeWidth={2}/> Готово · оценить поездку
          </button>
        ) : (
          <button onClick={() => setStage('cancelled')} style={{
            width: '100%', height: 48, borderRadius: 14, cursor: 'pointer', marginTop: 16,
            border: `1px solid ${C.hairline}`, background: '#fff', color: C.redDeep, fontFamily: 'Inter', fontSize: 14, fontWeight: 700,
          }}>Отменить заявку</button>
        )}
      </div>
    </div>
  );
}
window.Tracking = Tracking;
