const BEndDashboardView = ({ industry }) => {
  const [activeRole, setActiveRole] = React.useState(Object.keys(roleDefinitions[industry])[0]);
  const [nav, setNav] = React.useState('overview');

  React.useEffect(() => {
    setActiveRole(Object.keys(roleDefinitions[industry])[0]);
    setNav('overview');
  }, [industry]);

  const roleData = roleDefinitions[industry][activeRole];
  const isManager = activeRole === 'manager' || activeRole === 'gm';
  const data = dashboardData[industry];

  return (
    <div className="dashboard-shell animate-fade-in-up w-full max-w-6xl mx-auto surface-card rounded-3xl shadow-2xl flex flex-col overflow-hidden text-sm relative">
      <DashboardTopbar industry={industry} activeRole={activeRole} setActiveRole={setActiveRole} roleData={roleData} />
      <div className="flex flex-1 overflow-hidden">
        <DashboardSidebar nav={nav} setNav={setNav} />
        <div className="dashboard-main flex-1 p-8 overflow-y-auto bg-[#f4f1ea]/60">
          {nav === 'overview'
            ? <OverviewPanel industry={industry} roleData={roleData} isManager={isManager} data={data} onOpenAgent={() => setNav('agent')} />
            : <AgentPanel industry={industry} />}
        </div>
      </div>
    </div>
  );
};

const DashboardTopbar = ({ industry, activeRole, setActiveRole, roleData }) => (
  <div className="h-16 border-b border-slate-200 bg-white flex items-center justify-between gap-3 px-4 sm:px-6 shrink-0 z-20">
    <div className="flex items-center gap-3 min-w-0 flex-1">
      <Brandmark size={32} />
      <div className="min-w-0 hidden sm:block">
        <div className="font-bold text-[15px] text-slate-900 tracking-tight leading-none truncate">NextGen 营运控制台</div>
        <div className="text-[11px] text-slate-400 font-medium mt-1 hidden sm:flex items-center gap-1.5"><LiveDot tone="emerald" /> 数据每 30 秒刷新 · 截至 22:00</div>
      </div>
    </div>
    <div className="flex items-center gap-2 sm:gap-5 shrink-0">
      <div className="bg-slate-50 p-1 rounded-xl flex gap-1 border border-slate-200">
        {Object.entries(roleDefinitions[industry]).map(([roleId, role]) => (
          <button
            key={roleId}
            onClick={() => setActiveRole(roleId)}
            className={`px-2.5 sm:px-4 py-1.5 text-xs font-bold rounded-lg transition-all cursor-pointer flex items-center gap-1.5 whitespace-nowrap ${activeRole === roleId ? 'bg-slate-900 text-slate-50 shadow-sm' : 'text-slate-500 hover:text-slate-900'}`}
          >
            <Icon name={role.icon} className={`w-3.5 h-3.5 ${activeRole === roleId ? 'text-brand-300' : ''}`} /> {role.title}
          </button>
        ))}
      </div>
      <div className="w-px h-6 bg-slate-200 hidden sm:block" />
      <div className="flex items-center gap-3">
        <div className="text-right hidden md:block">
          <div className="text-xs font-bold text-slate-900 leading-none mb-1">{roleData.title}</div>
          <div className="text-[10px] text-slate-500 font-medium">{roleData.desc}</div>
        </div>
        <div className="w-9 h-9 rounded-full ink-panel text-brand-300 flex items-center justify-center font-bold text-xs shadow-sm ring-1 ring-slate-200 shrink-0">
          <Icon name={roleData.icon} className="w-4 h-4" />
        </div>
      </div>
    </div>
  </div>
);

const DashboardSidebar = ({ nav, setNav }) => (
  <div className="w-56 border-r border-slate-200 bg-white hidden md:flex flex-col py-6 shrink-0 z-10">
    <div className="px-5 mb-3 text-[10px] font-bold text-slate-400 uppercase tracking-kicker">系统管理</div>
    <div className="space-y-1 px-3">
      <SidebarButton icon="layout-dashboard" label="调度概览" active={nav === 'overview'} onClick={() => setNav('overview')} />
      <SidebarButton icon="list-todo" label="流转中心" badge="24" disabled />
      <SidebarButton icon="git-merge" label="意图配置" disabled />
    </div>
    <div className="px-5 mt-7 mb-3 text-[10px] font-bold text-slate-400 uppercase tracking-kicker">智能组件</div>
    <div className="space-y-1 px-3">
      <SidebarButton icon="sparkles" label="业务 Copilot" active={nav === 'agent'} onClick={() => setNav('agent')} accent />
      <SidebarButton icon="users" label="CRM 客档画像" disabled />
      <SidebarButton icon="shield-check" label="风控中心" disabled />
    </div>
    <div className="mt-auto px-4">
      <div className="rounded-xl ink-panel p-4 text-slate-50 relative overflow-hidden">
        <Icon name="sparkles" className="absolute -right-2 -bottom-2 w-14 h-14 text-white/5" />
        <div className="text-[11px] font-bold text-brand-300 relative z-10">AI 已节约</div>
        <div className="font-display text-[22px] font-semibold mt-1 relative z-10 tnum">372 <span className="text-[12px] text-slate-400 font-sans">人时 / 周</span></div>
      </div>
    </div>
  </div>
);

const SidebarButton = ({ icon, label, active, onClick, badge, disabled, accent }) => (
  <button
    onClick={disabled ? undefined : onClick}
    className={`w-full flex items-center justify-between px-4 py-2.5 rounded-xl font-bold text-[13px] transition-all duration-200 ${disabled ? 'cursor-not-allowed opacity-45 text-slate-500' : active ? 'bg-slate-900 text-slate-50 shadow-sm cursor-pointer' : 'text-slate-600 hover:bg-slate-50 cursor-pointer'}`}
  >
    <div className="flex items-center gap-3">
      <Icon name={icon} className={`w-[18px] h-[18px] ${active && accent ? 'text-brand-300' : active ? 'text-brand-300' : ''}`} /> {label}
    </div>
    {badge && <span className="text-[10px] font-bold bg-slate-100 text-slate-500 border border-slate-200 rounded px-1.5 py-0.5 tnum">{badge}</span>}
  </button>
);

/* ---------------- Overview ---------------- */
const OverviewPanel = ({ industry, roleData, isManager, data, onOpenAgent }) => (
  <div className="animate-fade-in space-y-6">
    <div className="flex flex-col sm:flex-row sm:items-end sm:justify-between gap-3">
      <div>
        <Kicker className="mb-2.5">Operations · AI 营运概览</Kicker>
        <h2 className="font-display text-[26px] font-semibold text-slate-900 tracking-tight">{isManager ? '全局经营与风控驾驶舱' : '现场调度与工单看板'}</h2>
        <p className="text-sm text-slate-500 mt-1 font-medium">以 <span className="font-bold text-slate-900">{roleData.title}</span> 视角查看 · 数据为今日实时模拟</p>
      </div>
      <button onClick={onOpenAgent} className="inline-flex items-center gap-2 bg-slate-900 text-slate-50 text-[13px] font-bold px-4 py-2.5 rounded-xl shadow-md hover:bg-slate-800 transition-colors cursor-pointer shrink-0">
        <Icon name="sparkles" className="w-4 h-4 text-brand-300" /> 唤起业务 Copilot
      </button>
    </div>

    {isManager && (
      <div className="rounded-2xl border border-red-200 bg-red-50/70 p-4 flex flex-col sm:flex-row sm:items-center gap-3 sm:gap-4">
        <div className="flex items-start gap-4 flex-1 min-w-0">
          <div className="w-11 h-11 rounded-xl bg-red-100 text-red-600 flex items-center justify-center shrink-0 pulse-glow"><Icon name="shield-alert" className="w-5 h-5" /></div>
          <div className="flex-1 min-w-0">
            <div className="flex flex-wrap items-center gap-x-2 gap-y-0.5">
              <span className="text-[13px] font-bold text-red-700">{data.risk.level}风控预警 · {data.risk.title}</span>
              <span className="text-[10px] font-mono font-bold text-red-500">{data.risk.code}</span>
            </div>
            <div className="text-[12px] text-red-600/90 font-medium mt-0.5">{data.risk.detail}</div>
          </div>
        </div>
        <button className="text-[12px] font-bold text-red-700 bg-white border border-red-200 px-3 py-2 rounded-lg hover:bg-red-100 transition-colors cursor-pointer shrink-0 w-full sm:w-auto">立即处置</button>
      </div>
    )}

    <div className="grid grid-cols-2 lg:grid-cols-4 gap-4">
      {data.kpis.map(k => <KpiCard key={k.key} kpi={k} />)}
    </div>

    <div className="grid grid-cols-1 lg:grid-cols-3 gap-5">
      <div className="lg:col-span-2 surface-card rounded-2xl p-5">
        <div className="flex items-center justify-between mb-4">
          <div>
            <div className="text-[13px] font-bold text-slate-900">营收转化与会话量趋势</div>
            <div className="text-[11px] text-slate-400 font-medium mt-0.5">今日 08:00 – 22:00 · 双轴</div>
          </div>
          <div className="flex gap-1 bg-slate-50 p-1 rounded-lg border border-slate-200">
            {['今日', '本周', '本月'].map((t, i) => (
              <span key={t} className={`px-2.5 py-1 rounded-md text-[11px] font-bold ${i === 0 ? 'bg-white text-slate-900 shadow-sm border border-slate-200' : 'text-slate-400'}`}>{t}</span>
            ))}
          </div>
        </div>
        <TrendChart labels={data.trend.labels} primary={data.trend.revenue} secondary={data.trend.sessions.map(s => s / 2.3)} />
        <div className="flex items-center gap-5 mt-2 text-[11px] font-semibold text-slate-500">
          <span className="inline-flex items-center gap-1.5"><span className="h-2 w-4 rounded-full bg-brand-500" /> 转化营收（千元）</span>
          <span className="inline-flex items-center gap-1.5"><span className="h-2 w-4 rounded-full bg-slate-300" /> 会话量（折算）</span>
        </div>
      </div>

      <div className="surface-card rounded-2xl p-5 flex flex-col">
        <div className="text-[13px] font-bold text-slate-900 mb-1">AI 意图分发结构</div>
        <div className="text-[11px] text-slate-400 font-medium mb-3">按三类能力 + 人工兜底</div>
        <div className="relative flex items-center justify-center my-2">
          <Donut segments={data.intentSplit} size={158} stroke={19} />
          <div className="absolute inset-0 flex flex-col items-center justify-center">
            <div className="font-display text-[24px] font-semibold text-slate-900 tnum">{100 - data.intentSplit[3].value}%</div>
            <div className="text-[10px] text-slate-400 font-semibold">自动闭环</div>
          </div>
        </div>
        <div className="space-y-2 mt-3">
          {data.intentSplit.map(s => (
            <div key={s.label} className="flex items-center justify-between text-[12px]">
              <span className="flex items-center gap-2 text-slate-600 font-medium"><span className="h-2.5 w-2.5 rounded-sm" style={{ background: s.color }} /> {s.label}</span>
              <span className="font-bold text-slate-900 tnum">{s.value}%</span>
            </div>
          ))}
        </div>
      </div>
    </div>

    <div className="grid grid-cols-1 lg:grid-cols-3 gap-5">
      <div className="surface-card rounded-2xl p-5">
        <div className="text-[13px] font-bold text-slate-900 mb-4">全渠道转化漏斗</div>
        <div className="space-y-3.5">
          {data.funnel.map((f, i) => (
            <div key={f.stage}>
              <div className="flex items-center justify-between text-[12px] mb-1.5">
                <span className="font-semibold text-slate-600">{f.stage}</span>
                <span className="font-bold text-slate-900 tnum">{f.value.toLocaleString()} <span className="text-slate-400 font-medium">· {f.pct}%</span></span>
              </div>
              <BarMeter value={f.pct} tone={i === data.funnel.length - 1 ? 'gold' : i === 0 ? 'ink' : 'gold'} />
            </div>
          ))}
        </div>
      </div>

      <div className="lg:col-span-2 surface-card rounded-2xl p-5 flex flex-col">
        <div className="flex items-center justify-between mb-3">
          <div className="text-[13px] font-bold text-slate-900">实时流转事件</div>
          <span className="inline-flex items-center gap-1.5 text-[11px] font-semibold text-emerald-600"><LiveDot tone="emerald" /> Live</span>
        </div>
        <div className="space-y-1 flex-1">
          {data.activity.map((a, i) => (
            <div key={i} className="flex items-start gap-3 py-2 border-b border-slate-100 last:border-0">
              <div className={`w-7 h-7 rounded-lg flex items-center justify-center shrink-0 ${a.type === 'risk' ? 'bg-red-50 text-red-600' : a.type === 'done' ? 'bg-emerald-50 text-emerald-600' : a.type === 'knowledge' ? 'bg-slate-100 text-slate-600' : 'bg-brand-50 text-brand-600'}`}>
                <Icon name={a.type === 'risk' ? 'shield-alert' : a.type === 'done' ? 'check' : a.type === 'knowledge' ? 'book-open' : a.type === 'workflow' ? 'git-merge' : 'zap'} className="w-3.5 h-3.5" />
              </div>
              <div className="flex-1 min-w-0">
                <div className="text-[12.5px] text-slate-800 font-medium leading-snug">{a.text}</div>
                <div className="text-[10.5px] text-slate-400 font-semibold mt-0.5">{a.who} · {a.time}</div>
              </div>
            </div>
          ))}
        </div>
      </div>
    </div>

    <div className="surface-card rounded-2xl p-5">
      <div className="flex items-center justify-between mb-4">
        <div className="text-[13px] font-bold text-slate-900">团队处置绩效榜</div>
        <span className="text-[11px] font-semibold text-slate-400">今日 · 按完结量</span>
      </div>
      <div className="overflow-x-auto rounded-xl border border-slate-200 no-scrollbar">
        <table className="w-full min-w-[460px] text-[12.5px]">
          <thead>
            <tr className="bg-slate-50 text-slate-500 font-bold text-[11px] uppercase tracking-wide">
              <th className="text-left px-4 py-2.5">排名 / 成员</th>
              <th className="text-left px-4 py-2.5">岗位</th>
              <th className="text-right px-4 py-2.5">完结量</th>
              <th className="text-left px-4 py-2.5 w-[38%]">达成率</th>
            </tr>
          </thead>
          <tbody>
            {data.leaderboard.map((m, i) => (
              <tr key={m.name} className="border-t border-slate-100">
                <td className="px-4 py-3">
                  <div className="flex items-center gap-2.5">
                    <span className={`w-6 h-6 rounded-md flex items-center justify-center text-[11px] font-bold font-mono ${i === 0 ? 'bg-brand-500 text-slate-950' : 'bg-slate-100 text-slate-500'}`}>{i + 1}</span>
                    <span className="font-bold text-slate-900">{m.name}</span>
                  </div>
                </td>
                <td className="px-4 py-3 text-slate-500 font-medium">{m.role}</td>
                <td className="px-4 py-3 text-right font-bold text-slate-900 tnum">{m.handled}</td>
                <td className="px-4 py-3">
                  <div className="flex items-center gap-2.5">
                    <div className="flex-1"><BarMeter value={m.rate} tone="gold" /></div>
                    <span className="font-bold text-slate-900 tnum w-9 text-right">{m.rate}%</span>
                  </div>
                </td>
              </tr>
            ))}
          </tbody>
        </table>
      </div>
    </div>
  </div>
);

const KpiCard = ({ kpi }) => (
  <div className="surface-card rounded-2xl p-4 sm:p-5 flex flex-col min-w-0">
    <div className="flex items-start justify-between gap-2 mb-2.5">
      <div className="text-[12px] font-semibold text-slate-500 min-w-0 truncate">{kpi.label}</div>
      <Delta value={kpi.delta} tone={kpi.tone} />
    </div>
    <div className="font-display text-[24px] sm:text-[26px] font-semibold text-slate-900 tracking-tight leading-none truncate">
      <CountUp value={kpi.value} decimals={kpi.value % 1 !== 0 ? 2 : 0} prefix={kpi.prefix || ''} />
      <span className="text-[12px] text-slate-400 font-sans font-medium ml-1">{kpi.unit}</span>
    </div>
    <div className="mt-3 w-full"><Sparkline data={kpi.spark} /></div>
    <div className="text-[11px] text-slate-400 font-medium mt-3 pt-3 border-t border-slate-100 leading-snug">{kpi.desc}</div>
  </div>
);

/* ---------------- Copilot ---------------- */
const AgentPanel = ({ industry }) => {
  const script = copilotScripts[industry];
  const [turns, setTurns] = React.useState([]);
  const [phase, setPhase] = React.useState('idle'); // idle | thinking | done
  const [thinkStep, setThinkStep] = React.useState(0);
  const [activeQ, setActiveQ] = React.useState(null);
  const scrollRef = React.useRef(null);
  const timers = React.useRef([]);

  const clear = () => timers.current.forEach(clearTimeout);
  const push = (fn, ms) => { const t = setTimeout(fn, ms); timers.current.push(t); };

  React.useEffect(() => { clear(); setTurns([]); setPhase('idle'); setThinkStep(0); setActiveQ(null); }, [industry]);
  React.useEffect(() => { if (scrollRef.current) scrollRef.current.scrollTop = scrollRef.current.scrollHeight; }, [turns, thinkStep, phase]);

  const run = (q) => {
    const session = script.sessions[q];
    if (!session || phase === 'thinking') return;
    clear();
    setActiveQ(q);
    setTurns(prev => [...prev, { role: 'user', text: q }]);
    setPhase('thinking');
    setThinkStep(0);
    session.steps.forEach((_, i) => push(() => setThinkStep(i + 1), 500 + i * 620));
    push(() => {
      setPhase('done');
      setTurns(prev => [...prev, { role: 'ai', text: session.answer, artifact: session.artifact, followups: session.followups }]);
    }, 500 + session.steps.length * 620 + 300);
  };

  const scripted = Object.keys(script.sessions);

  return (
    <div className="agent-panel animate-fade-in flex flex-col h-full max-w-4xl mx-auto">
      <div className="flex items-center justify-between mb-5">
        <div>
          <Kicker className="mb-2.5">Copilot · 自然语言工作台</Kicker>
          <h2 className="font-display text-[24px] font-semibold text-slate-900 tracking-tight flex items-center gap-2.5">业务 Copilot <Badge variant="gold">Beta</Badge></h2>
          <p className="text-sm text-slate-500 mt-1 font-medium">下达自然语言指令，自动制表、归因洞察并可直接执行下发。</p>
        </div>
      </div>

      <div className="surface-card rounded-2xl flex flex-col flex-1 overflow-hidden">
        <div ref={scrollRef} className="flex-1 p-6 overflow-y-auto no-scrollbar">
          {turns.length === 0 && phase === 'idle' ? (
            <div className="h-full flex flex-col items-center justify-center text-center">
              <div className="w-16 h-16 ink-panel rounded-2xl flex items-center justify-center mb-5 float-soft"><Icon name="sparkles" className="w-8 h-8 text-brand-300" /></div>
              <h4 className="font-display text-[20px] font-semibold text-slate-900 mb-2">有什么我可以帮您？</h4>
              <p className="text-sm text-slate-500 mb-7 max-w-sm font-medium">直接下达指令，例如拉取数据、归因分析或草拟文案。点击下方任一指令开始体验：</p>
              <div className="flex flex-wrap gap-2.5 justify-center max-w-lg">
                {script.suggestions.map(s => {
                  const runnable = scripted.includes(s);
                  return (
                    <button key={s} onClick={() => run(s)} className={`px-4 py-2 rounded-xl text-[13px] font-bold border transition-all cursor-pointer ${runnable ? 'bg-white border-brand-200 text-brand-700 hover:bg-brand-50 hover:border-brand-300' : 'bg-white border-slate-200 text-slate-500 hover:border-slate-300'}`}>
                      {runnable && <Icon name="play" className="w-3 h-3 inline mr-1.5 -mt-0.5" />}{s}
                    </button>
                  );
                })}
              </div>
            </div>
          ) : (
            <div className="space-y-5">
              {turns.map((t, i) => <CopilotBubble key={i} turn={t} onFollow={run} runnable={scripted} />)}
              {phase === 'thinking' && <CopilotThinking steps={script.sessions[activeQ].steps} step={thinkStep} />}
            </div>
          )}
        </div>

        <div className="p-4 bg-slate-50 border-t border-slate-200">
          {turns.length > 0 && (
            <div className="flex flex-wrap gap-2 mb-3">
              {scripted.map(s => (
                <button key={s} onClick={() => run(s)} disabled={phase === 'thinking'} className="text-[11px] font-bold px-3 py-1.5 rounded-lg bg-white border border-slate-200 text-slate-600 hover:border-brand-300 hover:text-brand-700 transition-colors cursor-pointer disabled:opacity-50">{s}</button>
              ))}
            </div>
          )}
          <div className="flex items-center gap-3">
            <div className="flex-1 bg-white border border-slate-200 rounded-xl h-12 px-4 flex items-center text-slate-400 text-sm shadow-sm font-medium">
              <Icon name="sparkles" className="w-4 h-4 mr-2 text-brand-400" /> 告诉 Copilot 你想做什么…
            </div>
            <button className="h-12 w-12 bg-slate-900 text-slate-50 rounded-xl flex items-center justify-center shadow-md hover:bg-slate-800 transition-colors cursor-pointer"><Icon name="arrow-up" className="w-5 h-5 text-brand-300" /></button>
          </div>
        </div>
      </div>
    </div>
  );
};

const CopilotThinking = ({ steps, step }) => (
  <div className="flex gap-3 animate-fade-in">
    <div className="w-8 h-8 rounded-xl ink-panel text-brand-300 flex items-center justify-center shrink-0"><Icon name="sparkles" className="w-4 h-4" /></div>
    <div className="bg-slate-50 border border-slate-200 rounded-2xl rounded-tl-sm p-4 space-y-2 min-w-[260px]">
      {steps.map((s, i) => {
        const done = step > i, active = step === i;
        return (
          <div key={i} className="flex items-center gap-2.5">
            <div className={`w-5 h-5 rounded-md flex items-center justify-center shrink-0 ${done ? 'bg-emerald-100 text-emerald-600' : active ? 'bg-brand-100 text-brand-600' : 'bg-slate-200 text-slate-400'}`}>
              {done ? <Icon name="check" className="w-3 h-3" /> : <span className="text-[10px] font-bold font-mono">{i + 1}</span>}
            </div>
            <span className={`text-[12.5px] font-medium ${done || active ? 'text-slate-700' : 'text-slate-400'}`}>{s}</span>
            {active && <Icon name="loader" className="w-3 h-3 text-brand-500 animate-spin" />}
          </div>
        );
      })}
    </div>
  </div>
);

const CopilotBubble = ({ turn, onFollow, runnable }) => {
  if (turn.role === 'user') {
    return (
      <div className="flex gap-3 flex-row-reverse animate-fade-in-up">
        <div className="w-8 h-8 rounded-xl bg-white border border-slate-200 text-slate-600 flex items-center justify-center shrink-0"><Icon name="user" className="w-4 h-4" /></div>
        <div className="bg-slate-900 text-slate-50 px-4 py-2.5 rounded-2xl rounded-tr-sm text-[13px] font-medium shadow-sm">{turn.text}</div>
      </div>
    );
  }
  return (
    <div className="flex gap-3 animate-fade-in-up">
      <div className="w-8 h-8 rounded-xl ink-panel text-brand-300 flex items-center justify-center shrink-0"><Icon name="sparkles" className="w-4 h-4" /></div>
      <div className="flex-1 min-w-0 space-y-3">
        <div className="bg-white border border-slate-200 rounded-2xl rounded-tl-sm p-4 text-[13px] text-slate-800 leading-relaxed font-medium shadow-sm">{turn.text}</div>
        {turn.artifact && <CopilotArtifact a={turn.artifact} />}
        {turn.followups && (
          <div className="flex flex-wrap gap-2">
            {turn.followups.map(f => (
              <button key={f} onClick={() => runnable.includes(f) && onFollow(f)} className={`text-[11px] font-bold px-3 py-1.5 rounded-lg border transition-colors ${runnable.includes(f) ? 'bg-brand-50 border-brand-200 text-brand-700 hover:bg-brand-100 cursor-pointer' : 'bg-white border-slate-200 text-slate-400 cursor-default'}`}>
                <Icon name="corner-down-right" className="w-3 h-3 inline mr-1 -mt-0.5" />{f}
              </button>
            ))}
          </div>
        )}
      </div>
    </div>
  );
};

const CopilotArtifact = ({ a }) => {
  if (a.type === 'table') {
    return (
      <div className="bg-white border border-slate-200 rounded-2xl overflow-hidden shadow-sm animate-scale-in">
        <div className="px-4 py-2.5 bg-slate-50 border-b border-slate-200 text-[12px] font-bold text-slate-700 flex items-center gap-2">
          <Icon name="table-2" className="w-3.5 h-3.5 text-brand-600" /> {a.title}
        </div>
        <table className="w-full text-[12px]">
          <thead><tr className="text-slate-400 font-bold text-[11px]">
            {a.columns.map(c => <th key={c} className={`px-4 py-2 ${c === '指标' ? 'text-left' : 'text-left'}`}>{c}</th>)}
          </tr></thead>
          <tbody>
            {a.rows.map((r, i) => (
              <tr key={i} className="border-t border-slate-100">
                <td className="px-4 py-2.5 font-bold text-slate-900">{r[0]}</td>
                <td className="px-4 py-2.5 text-slate-700 font-semibold tnum">{r[1]}</td>
                <td className={`px-4 py-2.5 font-bold tnum ${r[3] === 'pos' ? 'text-emerald-600' : r[3] === 'risk' ? 'text-red-600' : 'text-slate-500'}`}>{r[2]}</td>
                <td className="px-4 py-2.5">
                  <span className={`text-[10px] font-bold px-1.5 py-0.5 rounded ${r[3] === 'pos' ? 'bg-emerald-50 text-emerald-600 border border-emerald-200' : r[3] === 'risk' ? 'bg-red-50 text-red-600 border border-red-200' : 'bg-slate-50 text-slate-500 border border-slate-200'}`}>
                    {r[3] === 'pos' ? '向好' : r[3] === 'risk' ? '关注' : '持平'}
                  </span>
                </td>
              </tr>
            ))}
          </tbody>
        </table>
      </div>
    );
  }
  // insight
  const max = Math.max(...a.bars.map(b => b.value));
  return (
    <div className="bg-white border border-slate-200 rounded-2xl p-4 shadow-sm animate-scale-in">
      <div className="text-[12px] font-bold text-slate-700 flex items-center gap-2 mb-4">
        <Icon name="bar-chart-3" className="w-3.5 h-3.5 text-brand-600" /> {a.title}
      </div>
      <div className="space-y-3">
        {a.bars.map(b => (
          <div key={b.label}>
            <div className="flex justify-between text-[12px] mb-1.5">
              <span className="font-semibold text-slate-600">{b.label}</span>
              <span className="font-bold text-slate-900 tnum">{b.value}%</span>
            </div>
            <BarMeter value={b.value} max={max} tone={b.tone} />
          </div>
        ))}
      </div>
      <div className="mt-4 rounded-xl bg-brand-50 border border-brand-100 p-3 text-[12px] text-brand-800 font-medium flex gap-2">
        <Icon name="lightbulb" className="w-4 h-4 text-brand-600 shrink-0 mt-0.5" /> {a.note}
      </div>
    </div>
  );
};
