// Classic — Chatwoot-inspired dense layout, modernized visuals.
// - Refined typography (Plus Jakarta Sans), softer borders
// - Single combined KPI strip (replaces 4 separate cards)
// - More breathing room while keeping table-heavy, icon-sidebar structure

function IconChat({ size = 18 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <path d="M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5z" />
    </svg>);

}
function IconDashboard({ size = 18 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <rect x="3" y="3" width="7" height="9" rx="1.5" />
      <rect x="14" y="3" width="7" height="5" rx="1.5" />
      <rect x="14" y="12" width="7" height="9" rx="1.5" />
      <rect x="3" y="16" width="7" height="5" rx="1.5" />
    </svg>);

}
function IconInbox({ size = 18 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <polyline points="22 12 16 12 14 15 10 15 8 12 2 12" />
      <path d="M5.45 5.11L2 12v6a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2v-6l-3.45-6.89A2 2 0 0 0 16.76 4H7.24a2 2 0 0 0-1.79 1.11z" />
    </svg>);

}
function IconContacts({ size = 18 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2" />
      <circle cx="9" cy="7" r="4" />
      <path d="M23 21v-2a4 4 0 0 0-3-3.87" />
      <path d="M16 3.13a4 4 0 0 1 0 7.75" />
    </svg>);

}
function IconReports({ size = 18 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <path d="M3 3v18h18"/>
      <path d="M7 14l3-3 3 3 5-5"/>
      <circle cx="7" cy="14" r="1.4" fill="currentColor" stroke="none"/>
      <circle cx="10" cy="11" r="1.4" fill="currentColor" stroke="none"/>
      <circle cx="13" cy="14" r="1.4" fill="currentColor" stroke="none"/>
      <circle cx="18" cy="9" r="1.4" fill="currentColor" stroke="none"/>
    </svg>);

}
function IconCampaign({ size = 18 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <path d="M3 11h3l9-7v16l-9-7H3z" /><path d="M19 7a5 5 0 0 1 0 10" />
    </svg>);

}
function IconSettings({ size = 18 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <circle cx="12" cy="12" r="3" />
      <path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 1 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 1 1-2.83-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 1 1 2.83-2.83l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 1 1 2.83 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z" />
    </svg>);

}
function IconBell({ size = 18 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <path d="M18 8A6 6 0 0 0 6 8c0 7-3 9-3 9h18s-3-2-3-9" /><path d="M13.73 21a2 2 0 0 1-3.46 0" />
    </svg>);

}
function IconSearch({ size = 14 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
      <circle cx="11" cy="11" r="8" /><line x1="21" y1="21" x2="16.65" y2="16.65" />
    </svg>);

}
function IconWA({ size = 14 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="#25D366"><path d="M17.5 14.4c-.3-.1-1.7-.8-2-.9-.3-.1-.5-.1-.7.1s-.8.9-.9 1.1c-.2.2-.3.2-.6.1-.3-.1-1.2-.4-2.3-1.4-.8-.7-1.4-1.6-1.6-1.9-.2-.3 0-.5.1-.6l.5-.5c.2-.2.2-.3.3-.5.1-.2 0-.4 0-.5s-.7-1.6-.9-2.2c-.2-.6-.5-.5-.7-.5h-.6c-.2 0-.5.1-.7.4-.2.3-.9.9-.9 2.1 0 1.3.9 2.5 1 2.7.1.2 1.7 2.6 4.2 3.7.6.3 1 .4 1.4.5.6.2 1.1.2 1.5.1.5-.1 1.4-.6 1.6-1.1.2-.5.2-1 .1-1.1-.1-.1-.3-.2-.6-.3zM12 2C6.5 2 2 6.5 2 12c0 1.7.4 3.3 1.2 4.7L2 22l5.4-1.2C8.8 21.6 10.4 22 12 22c5.5 0 10-4.5 10-10S17.5 2 12 2z" /></svg>);

}
function IconMail({ size = 14 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z" /><polyline points="22,6 12,13 2,6" />
    </svg>);

}
function IconGlobe({ size = 14 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <circle cx="12" cy="12" r="10" /><line x1="2" y1="12" x2="22" y2="12" /><path d="M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z" />
    </svg>);

}
function ChannelIcon({ channel }) {
  if (channel === "whatsapp") return <IconWA />;
  if (channel === "email") return <IconMail />;
  return <IconGlobe />;
}

function Spark({ data, color, h = 28, w = 88 }) {
  const max = Math.max(...data);
  const pts = data.map((v, i) => `${i / (data.length - 1) * w},${h - v / max * (h - 4) - 2}`).join(" ");
  const area = `0,${h} ${pts} ${w},${h}`;
  return (
    <svg width={w} height={h} style={{ display: 'block' }}>
      <polyline points={area} fill={color + "22"} />
      <polyline points={pts} fill="none" stroke={color} strokeWidth="1.6" strokeLinejoin="round" strokeLinecap="round" />
    </svg>);

}

function StatusPill({ status, dark = true }) {
  const map = {
    open: { label: "Open", bg: "rgba(59,130,246,0.14)", fg: "#60A5FA", dot: "#3B82F6" },
    pending: { label: "Pending", bg: "rgba(245,158,11,0.14)", fg: "#FBBF24", dot: "#F59E0B" },
    snoozed: { label: "Snoozed", bg: "rgba(148,163,184,0.16)", fg: "#94A3B8", dot: "#94A3B8" },
    resolved: { label: "Resolved", bg: "rgba(16,185,129,0.14)", fg: "#34D399", dot: "#10B981" }
  };
  const s = map[status] || map.open;
  return (
    <span style={{ display: "inline-flex", alignItems: "center", gap: 6, padding: "3px 9px", borderRadius: 999, background: s.bg, color: s.fg, fontSize: 11, fontWeight: 600, letterSpacing: 0.1 }}>
      <span style={{ width: 6, height: 6, borderRadius: 999, background: s.dot }}></span>
      {s.label}
    </span>);

}
function PriorityDot({ p }) {
  const c = p === "urgent" ? "#EF4444" : p === "high" ? "#F59E0B" : p === "medium" ? "#3B82F6" : "#64748B";
  return <span style={{ display: "inline-block", width: 8, height: 8, borderRadius: 999, background: c }}></span>;
}

function PriorityPill({ p }) {
  const map = {
    urgent: { label: "Urgent", fg: "#F87171", bg: "rgba(239,68,68,0.14)",  dot: "#EF4444" },
    high:   { label: "High",   fg: "#FBBF24", bg: "rgba(245,158,11,0.14)", dot: "#F59E0B" },
    medium: { label: "Medium", fg: "#60A5FA", bg: "rgba(59,130,246,0.14)", dot: "#3B82F6" },
    low:    { label: "Low",    fg: "#94A3B8", bg: "rgba(148,163,184,0.16)",dot: "#94A3B8" },
  };
  const s = map[p] || map.low;
  return (
    <span style={{ display: "inline-flex", alignItems: "center", gap: 6, padding: "3px 9px", borderRadius: 999, background: s.bg, color: s.fg, fontSize: 11, fontWeight: 600, letterSpacing: 0.1 }}>
      <span style={{ width: 6, height: 6, borderRadius: 999, background: s.dot }}></span>
      {s.label}
    </span>
  );
}

function Avatar({ initials, color, online, size = 28, ring }) {
  return (
    <span style={{ position: "relative", display: "inline-flex" }}>
      <span style={{
        width: size, height: size, borderRadius: 999,
        background: `linear-gradient(135deg, ${color}, ${color}cc)`,
        color: "#0b0e13", fontWeight: 700, display: "flex", alignItems: "center", justifyContent: "center",
        fontSize: size * 0.4, letterSpacing: -0.3,
        boxShadow: ring ? `0 0 0 2px ${ring}` : "none"
      }}>
        {initials}
      </span>
      {online !== undefined &&
      <span style={{ position: "absolute", right: -1, bottom: -1, width: 8, height: 8, borderRadius: 999, background: online ? "#22c55e" : "#475569", border: "2px solid var(--c-surface)" }}></span>
      }
    </span>);

}

function DashboardClassic({ tweaks }) {
  const D = window.DASHBOARD_DATA;
  const t = tweaks || { dark: true, density: "regular", accent: "#1F93FF" };
  const dark = t.dark !== false;
  const dens = t.density || "regular";
  const accent = t.accent || "#1F93FF";

  // Padding scale by density
  const pad = dens === "compact" ? 14 : dens === "comfortable" ? 22 : 18;
  const rowPad = dens === "compact" ? "9px 16px" : dens === "comfortable" ? "15px 18px" : "12px 16px";

  const C = dark ?
  { bg: "#0B1220", surface: "#111827", surface2: "#1F2937", surface3: "#374151", border: "#1F2937", borderSoft: "#1A2233", text: "#F1F5F9", textDim: "#CBD5E1", textMuted: "#94A3B8", sidebar: "#0F172A", sidebarFg: "#94A3B8", sidebarActive: "rgba(255,255,255,0.08)" } :
  { bg: "#F9FAFB", surface: "#FFFFFF", surface2: "#F3F4F6", surface3: "#E5E7EB", border: "#E5E7EB", borderSoft: "#F3F4F6", text: "#0F172A", textDim: "#334155", textMuted: "#64748B", sidebar: "#0F172A", sidebarFg: "#94A3B8", sidebarActive: "rgba(255,255,255,0.08)" };

  const card = {
    background: C.surface,
    border: `1px solid ${C.border}`,
    borderRadius: 14,
    overflow: "hidden",
    boxShadow: dark ? "0 1px 0 rgba(255,255,255,0.02) inset" : "0 1px 2px rgba(15,23,42,0.04)"
  };
  const cardHeader = {
    padding: `${pad - 2}px ${pad + 2}px ${pad - 6}px`,
    borderBottom: `1px solid ${C.borderSoft}`,
    display: "flex",
    alignItems: "center",
    justifyContent: "space-between"
  };
  const sectionTitle = { fontSize: 13.5, fontWeight: 700, color: C.text, letterSpacing: -0.1 };
  const sectionSub = { fontSize: 11.5, color: C.textMuted, marginTop: 2, fontWeight: 500 };

  const kpis = [
  { label: "Unassigned", val: D.stats.unassigned, color: "#F87171", trend: "+3", trendUp: false, data: [4, 5, 3, 6, 8, 6, 7, 9, 12], hint: "butuh perhatian", prev: 9 },
  { label: "Today", val: D.stats.today, color: accent, trend: "+12%", trendUp: true, data: D.hourly, hint: "tiket hari ini", prev: 42 },
  { label: "In Progress", val: D.stats.inProgress, color: "#FBBF24", trend: "0", trendUp: null, data: [22, 24, 25, 23, 26, 27, 28, 28, 28], hint: "sedang dikerjakan", prev: 28 },
  { label: "Closed", val: D.stats.closed, color: "#34D399", trend: "+18", trendUp: true, data: [80, 90, 95, 100, 105, 110, 115, 118, 119], hint: "diselesaikan", prev: 101 },
  { label: "Response Time", val: D.stats.avgResponse, color: "#22D3EE", trend: "\u221222s", trendUp: true, data: [380, 360, 340, 330, 310, 295, 280, 275, 272], hint: "target \u2264 5m", prev: "4m 54s" }];

  const kpiMode = t.kpiVis || "compare";


  return (
    <div style={{
      "--c-bg": C.bg, "--c-surface": C.surface, "--c-text": C.text, "--c-border": C.border, "--accent": accent,
      width: 1920, height: 1080, background: C.bg, color: C.text,
      fontFamily: "'Inter', system-ui, -apple-system, sans-serif",
      display: "flex", overflow: "hidden"
    }}>
      {/* Sidebar — narrow icon rail */}
      <div style={{ width: 68, background: C.sidebar, display: "flex", flexDirection: "column", alignItems: "center", padding: "18px 0", gap: 4, flexShrink: 0, borderRight: `1px solid ${dark ? "#000" : C.border}` }}>
        <div style={{ width: 38, height: 38, borderRadius: 11, background: `linear-gradient(135deg, ${accent}, ${shiftColor(accent, -18)})`, display: "flex", alignItems: "center", justifyContent: "center", marginBottom: 16, boxShadow: `0 6px 18px ${accent}50` }}>
          <svg width="19" height="19" viewBox="0 0 24 24" fill="white"><path d="M12 2C6.5 2 2 6.5 2 12c0 1.7.4 3.3 1.2 4.7L2 22l5.4-1.2C8.8 21.6 10.4 22 12 22c5.5 0 10-4.5 10-10S17.5 2 12 2z" /></svg>
        </div>
        {[
        { i: <IconDashboard />, label: "Dashboard", active: true, href: "/" },
        { i: <IconReports />, label: "Reports", active: false, href: "/report-agent" }].
        map((it, i) =>
        <button key={i} title={it.label} onClick={() => { window.top.location.href = it.href; }} style={{ position: "relative", width: 44, height: 44, borderRadius: 10, display: "flex", alignItems: "center", justifyContent: "center", color: it.active ? "white" : C.sidebarFg, background: it.active ? C.sidebarActive : "transparent", border: "none", cursor: "pointer" }}>
            {it.active && <span style={{ position: "absolute", left: -10, top: 8, bottom: 8, width: 3, borderRadius: 2, background: accent }}></span>}
            {it.i}
            {it.badge && <span style={{ position: "absolute", top: 5, right: 5, fontSize: 9, fontWeight: 700, padding: "1px 5px", background: "#EF4444", color: "white", borderRadius: 999, lineHeight: 1.3 }}>{it.badge}</span>}
          </button>
        )}
        <div style={{ flex: 1 }} />
        <div style={{ width: 44, height: 44, borderRadius: 10, display: "flex", alignItems: "center", justifyContent: "center", color: C.sidebarFg }}>
          <IconSettings />
        </div>
        <Avatar initials="MH" color="#A78BFA" online={true} size={32} ring={C.sidebar} />
      </div>

      {/* Main */}
      <div style={{ flex: 1, display: "flex", flexDirection: "column", overflow: "hidden" }}>
        {/* Topbar */}
        <div style={{ height: 64, borderBottom: `1px solid ${C.border}`, padding: "0 26px", display: "flex", alignItems: "center", justifyContent: "space-between", background: C.surface, flexShrink: 0 }}>
          <div style={{ display: "flex", alignItems: "center", gap: 14 }}>
            <div>
              <div style={{ fontSize: 10.5, color: C.textMuted, fontWeight: 600, letterSpacing: 0.6, textTransform: "uppercase" }}>Reports · Overview</div>
              <div style={{ fontSize: 18, fontWeight: 800, color: C.text, marginTop: 2, letterSpacing: -0.3 }}>Dashboard Tiket</div>
            </div>
            <div style={{ display: "flex", alignItems: "center", gap: 6, padding: "7px 12px", background: C.surface2, borderRadius: 9, fontSize: 11.5, color: C.textDim, marginLeft: 18 }}>
              <IconSearch />
              <span style={{ color: C.textMuted }}>Cari tiket, customer, agent...</span>
              <span style={{ padding: "1px 6px", border: `1px solid ${C.border}`, borderRadius: 5, fontSize: 10, color: C.textMuted, marginLeft: 30 }}>⌘K</span>
            </div>
          </div>
          <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
            <div style={{ display: "flex", alignItems: "center", gap: 7, padding: "7px 13px", background: C.surface2, borderRadius: 9, fontSize: 12, color: C.textDim, fontWeight: 500 }}>
              <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><rect x="3" y="4" width="18" height="18" rx="2" /><line x1="16" y1="2" x2="16" y2="6" /><line x1="8" y1="2" x2="8" y2="6" /><line x1="3" y1="10" x2="21" y2="10" /></svg>
              Hari ini · 19 Mei
            </div>
            <div style={{ position: "relative", width: 36, height: 36, background: C.surface2, borderRadius: 9, display: "flex", alignItems: "center", justifyContent: "center", color: C.textDim }}>
              <IconBell size={16} />
              <span style={{ position: "absolute", top: 8, right: 9, width: 7, height: 7, borderRadius: 999, background: "#EF4444", border: `2px solid ${C.surface}` }}></span>
            </div>
            <button style={{ padding: "8px 14px", background: accent, color: "white", border: "none", borderRadius: 9, fontSize: 12.5, fontWeight: 700, cursor: "pointer", boxShadow: `0 4px 12px ${accent}40` }}>+ New Conversation</button>
          </div>
        </div>

        {/* Content */}
        <div style={{ flex: 1, padding: 22, overflow: "hidden", display: "flex", flexDirection: "column", gap: 16 }}>
          {/* Combined KPI Strip — single horizontal card */}
          <div style={{ ...card, padding: 0, display: "flex", alignItems: "stretch" }}>
            {kpis.map((k, i) =>
            <React.Fragment key={i}>
                <div style={{ flex: 1, padding: "10px 14px", display: "flex", alignItems: "center", gap: 10, position: "relative", fontFamily: "\"Plus Jakarta Sans\"" }}>
                  <div style={{ width: 26, height: 26, borderRadius: 7, background: k.color + "1A", display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0 }}>
                    <span style={{ width: 6, height: 6, borderRadius: 999, background: k.color, boxShadow: `0 0 0 2.5px ${k.color}25` }}></span>
                  </div>
                  <div style={{ flex: 1, minWidth: 0 }}>
                    <div style={{ display: "flex", alignItems: "baseline", gap: 5 }}>
                      <div style={{ fontWeight: 800, color: C.text, letterSpacing: -0.8, lineHeight: 1, fontSize: "20px" }}>{k.val}</div>
                      <div style={{
                      fontSize: 9, fontWeight: 700,
                      color: k.trendUp === null ? C.textMuted : k.trendUp ? "#34D399" : "#F87171",
                      padding: "1px 5px", borderRadius: 999,
                      background: k.trendUp === null ? C.surface2 : k.trendUp ? "#10B98118" : "#EF444418"
                    }}>
                        {k.trendUp === true ? "↑" : k.trendUp === false ? "↑" : ""} {k.trend}
                      </div>
                    </div>
                    <div style={{ fontSize: 10.5, color: C.textDim, fontWeight: 600, marginTop: 3 }}>{k.label}</div>
                    <div style={{ fontSize: 9.5, color: C.textMuted, marginTop: 1 }}>{k.hint}</div>
                  </div>
                  {kpiMode === "spark" && <Spark data={k.data} color={k.color} w={42} h={20} />}
                  {kpiMode === "compare" && (
                    <div style={{ textAlign: "right", flexShrink: 0 }}>
                      <div style={{ fontSize: 8.5, color: C.textMuted, fontWeight: 700, letterSpacing: 0.3, textTransform: "uppercase" }}>kemarin</div>
                      <div style={{ fontSize: 11, fontWeight: 700, color: C.textDim, marginTop: 2, fontVariantNumeric: "tabular-nums" }}>{k.prev}</div>
                    </div>
                  )}
                </div>
                {i < kpis.length - 1 && <div style={{ width: 1, background: C.borderSoft }}></div>}
              </React.Fragment>
            )}
          </div>

          {/* Middle: Status Table + Issues per Agent */}
          <div style={{ display: "grid", gridTemplateColumns: "1.65fr 1fr", gap: 16, flex: 1, minHeight: 0 }}>
            {/* Running Tickets Table */}
            <div style={{ ...card, display: "flex", flexDirection: "column", minHeight: 0 }}>
              <div style={cardHeader}>
                <div>
                  <div style={sectionTitle}>Status Tiket Berjalan</div>
                  <div style={sectionSub}>{D.running.length} tiket aktif sedang ditangani agent</div>
                </div>
                <div style={{ display: "flex", padding: 3, background: C.surface2, borderRadius: 8 }}>
                  {["All", "Open", "Pending", "Snoozed"].map((tab, i) =>
                  <button key={i} style={{ padding: "5px 11px", fontSize: 11, fontWeight: 600, borderRadius: 6, border: "none", background: i === 0 ? C.surface : "transparent", color: i === 0 ? C.text : C.textMuted, cursor: "pointer", boxShadow: i === 0 ? dark ? "0 1px 2px rgba(0,0,0,0.3)" : "0 1px 2px rgba(15,23,42,0.06)" : "none" }}>{tab}</button>
                  )}
                </div>
              </div>
              <div style={{ flex: 1, overflow: "hidden" }}>
                <table style={{ width: "100%", borderCollapse: "collapse", fontSize: 12 }}>
                  <thead>
                    <tr style={{ background: dark ? "rgba(255,255,255,0.015)" : C.surface2 }}>
                      <th style={{ ...rowHead(C, rowPad), width: 36 }}>No</th>
                      <th style={{ ...rowHead(C, rowPad), width: 86 }}>Tiket</th>
                      <th style={{ ...rowHead(C, rowPad) }}>Subject</th>
                      <th style={{ ...rowHead(C, rowPad), width: 170 }}>Agent</th>
                      <th style={{ ...rowHead(C, rowPad), width: 100 }}>Priority</th>
                      <th style={{ ...rowHead(C, rowPad), width: 100 }}>Status</th>
                    </tr>
                  </thead>
                  <tbody>
                    {D.running.map((r) => {
                      const ag = D.agents.find((a) => a.name === r.agent) || {};
                      return (
                        <tr key={r.no} style={{ borderTop: `1px solid ${C.borderSoft}` }}>
                          <td style={{ ...rowCell(C, rowPad), color: C.textMuted, fontWeight: 600 }}>{r.no}</td>
                          <td style={{ ...rowCell(C, rowPad), fontFamily: "ui-monospace, SFMono-Regular, Menlo, monospace", fontWeight: 700, color: accent }}>{r.ticket}</td>
                          <td style={{ ...rowCell(C, rowPad), color: C.text, maxWidth: 0, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap", fontWeight: 500 }}>{r.subject}</td>
                          <td style={rowCell(C, rowPad)}>
                            <div style={{ display: "flex", alignItems: "center", gap: 8 }}>
                              <Avatar initials={r.agentInit} color={ag.color || "#94A3B8"} online={ag.online} size={24} ring={C.surface} />
                              <span style={{ color: C.text, fontWeight: 500 }}>{r.agent}</span>
                            </div>
                          </td>
                          <td style={rowCell(C, rowPad)}><PriorityPill p={r.priority} /></td>
                          <td style={rowCell(C, rowPad)}><StatusPill status={r.status} /></td>
                        </tr>);

                    })}
                  </tbody>
                </table>
              </div>
            </div>

            {/* Issues per Agent */}
            <div style={{ ...card, display: "flex", flexDirection: "column", minHeight: 0 }}>
              <div style={cardHeader}>
                <div>
                  <div style={sectionTitle}>Issue per Agent</div>
                  <div style={sectionSub}>Jumlah issue diambil hari ini</div>
                </div>
                <span style={{ fontSize: 11, color: C.textMuted, fontWeight: 600, padding: "3px 9px", background: C.surface2, borderRadius: 999 }}>{D.agents.reduce((a, b) => a + b.taken, 0)} total</span>
              </div>
              <div style={{ flex: 1, overflow: "auto" }}>
                {[...D.agents].sort((a, b) => b.taken - a.taken).map((a, i) => {
                  const max = Math.max(...D.agents.map((x) => x.taken));
                  const pct = a.taken / max * 100;
                  return (
                    <div key={i} style={{ padding: rowPad, display: "flex", alignItems: "center", gap: 12, borderTop: i ? `1px solid ${C.borderSoft}` : "none" }}>
                      <Avatar initials={a.initials} color={a.color} online={a.online} size={32} ring={C.surface} />
                      <div style={{ flex: 1, minWidth: 0 }}>
                        <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", gap: 8 }}>
                          <div style={{ fontSize: 12.5, fontWeight: 600, color: C.text, whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }}>{a.name}</div>
                          <div style={{ fontSize: 13.5, fontWeight: 800, color: C.text, letterSpacing: -0.3 }}>{a.taken}</div>
                        </div>
                        <div style={{ display: "flex", alignItems: "center", gap: 8, marginTop: 5 }}>
                          <div style={{ flex: 1, height: 4, background: C.surface2, borderRadius: 999, overflow: "hidden" }}>
                            <div style={{ width: pct + "%", height: "100%", background: `linear-gradient(90deg, ${a.color}, ${a.color}aa)`, borderRadius: 999 }}></div>
                          </div>
                          <div style={{ fontSize: 10.5, color: C.textMuted, whiteSpace: "nowrap", fontWeight: 500 }}>{a.resolved} closed</div>
                        </div>
                      </div>
                    </div>);

                })}
              </div>
            </div>
          </div>

          {/* Bottom: Unassigned + Closed */}
          <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 16, height: 290 }}>
            {/* Unassigned */}
            <div style={{ ...card, display: "flex", flexDirection: "column", minHeight: 0 }}>
              <div style={cardHeader}>
                <div>
                  <div style={sectionTitle}>Tiket Belum Di-Assign</div>
                  <div style={sectionSub}>{D.unassigned.length} tiket menunggu agent</div>
                </div>
                <button style={{ padding: "4px 10px", fontSize: 11, fontWeight: 700, color: accent, background: accent + "1A", border: "none", borderRadius: 7, cursor: "pointer" }}>Assign All</button>
              </div>
              <div style={{ flex: 1, overflow: "auto" }}>
                {D.unassigned.slice(0, 5).map((u, i) =>
                <div key={u.id} style={{ padding: rowPad, display: "flex", alignItems: "center", gap: 10, borderTop: i ? `1px solid ${C.borderSoft}` : "none" }}>
                    <PriorityDot p={u.priority} />
                    <span style={{ fontFamily: "ui-monospace, SFMono-Regular, Menlo, monospace", fontSize: 11, fontWeight: 700, color: accent, width: 50 }}>{u.id}</span>
                    <div style={{ width: 24, height: 24, borderRadius: 7, background: C.surface2, display: "flex", alignItems: "center", justifyContent: "center", color: C.textDim }}><ChannelIcon channel={u.channel} /></div>
                    <div style={{ flex: 1, minWidth: 0 }}>
                      <div style={{ fontSize: 12, color: C.text, fontWeight: 500, whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }}>{u.subject}</div>
                      <div style={{ fontSize: 10.5, color: C.textMuted, marginTop: 1 }}>{u.customer}</div>
                    </div>
                    <span style={{ fontSize: 10.5, color: C.textMuted, fontWeight: 600, padding: "2px 7px", background: C.surface2, borderRadius: 999 }}>{u.age}</span>
                  </div>
                )}
              </div>
            </div>

            {/* Closed */}
            <div style={{ ...card, display: "flex", flexDirection: "column", minHeight: 0 }}>
              <div style={cardHeader}>
                <div>
                  <div style={sectionTitle}>Tiket Sudah Closed</div>
                  <div style={sectionSub}>Diselesaikan dalam 24 jam terakhir</div>
                </div>
                <span style={{ fontSize: 11, color: "#34D399", fontWeight: 700, padding: "3px 9px", background: "#10B98118", borderRadius: 999, display: "flex", alignItems: "center", gap: 5 }}>
                  <span style={{ width: 6, height: 6, borderRadius: 999, background: "#10B981" }}></span>
                  {D.stats.closed} closed
                </span>
              </div>
              <div style={{ flex: 1, overflow: "auto" }}>
                {D.closed.slice(0, 5).map((u, i) => {
                  const ag = D.agents.find((a) => a.initials === u.agent) || {};
                  return (
                    <div key={u.id} style={{ padding: rowPad, display: "flex", alignItems: "center", gap: 10, borderTop: i ? `1px solid ${C.borderSoft}` : "none" }}>
                      <span style={{ width: 8, height: 8, borderRadius: 999, background: "#10B981" }}></span>
                      <span style={{ fontFamily: "ui-monospace, SFMono-Regular, Menlo, monospace", fontSize: 11, fontWeight: 700, color: C.textDim, width: 50 }}>{u.id}</span>
                      <div style={{ flex: 1, minWidth: 0 }}>
                        <div style={{ fontSize: 12, color: C.text, fontWeight: 500, whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }}>{u.subject}</div>
                        <div style={{ fontSize: 10.5, color: C.textMuted, marginTop: 1, display: "flex", alignItems: "center", gap: 5 }}>
                          <Avatar initials={u.agent} color={ag.color || "#94A3B8"} size={14} />
                          {u.agentName} · {u.resolvedIn}
                        </div>
                      </div>
                      <span style={{ display: "flex", gap: 1 }}>
                        {[1, 2, 3, 4, 5].map((n) =>
                        <svg key={n} width="10" height="10" viewBox="0 0 24 24" fill={n <= u.rating ? "#FBBF24" : C.surface2}><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2" /></svg>
                        )}
                      </span>
                    </div>);

                })}
              </div>
            </div>

          </div>
        </div>
      </div>
    </div>);

}

function rowHead(C, rowPad) {
  return { textAlign: "left", padding: rowPad, fontSize: 10, fontWeight: 700, color: C.textMuted, textTransform: "uppercase", letterSpacing: 0.6 };
}
function rowCell(C, rowPad) {
  return { padding: rowPad, fontSize: 12, color: C.text, verticalAlign: "middle" };
}

function shiftColor(hex, pct) {
  const h = hex.replace("#", "");
  const r = parseInt(h.slice(0, 2), 16);
  const g = parseInt(h.slice(2, 4), 16);
  const b = parseInt(h.slice(4, 6), 16);
  const f = (c) => Math.max(0, Math.min(255, Math.round(c + pct / 100 * 255)));
  return `#${[f(r), f(g), f(b)].map((x) => x.toString(16).padStart(2, "0")).join("")}`;
}

Object.assign(window, {
  DashboardClassic, ChatwootAvatar: Avatar, ChatwootStatusPill: StatusPill, ChatwootPriorityDot: PriorityDot, ChatwootPriorityPill: PriorityPill,
  ChatwootChannelIcon: ChannelIcon, ChatwootSpark: Spark,
  ChatwootIcons: { IconDashboard, IconChat, IconInbox, IconContacts, IconReports, IconCampaign, IconSettings, IconWA, IconMail, IconGlobe }
});