// Tasks Tab — List + Kanban views
// Tasks are tied to scopes of work (SOW-XX), have assignees, due dates,
// priority, status, optional CO link, dependencies, and progress.

const TASK_STATUS = {
  "Backlog":     { tone: "neutral", order: 0 },
  "To Do":       { tone: "info",    order: 1 },
  "In Progress": { tone: "purple",  order: 2 },
  "Blocked":     { tone: "danger",  order: 3 },
  "Review":      { tone: "warning", order: 4 },
  "Done":        { tone: "success", order: 5 },
};

const TASK_PRIORITY = {
  "Urgent": { tone: "danger",  icon: "alert",     label: "P1" },
  "High":   { tone: "warning", icon: "flag",      label: "P2" },
  "Med":    { tone: "info",    icon: "flag",      label: "P3" },
  "Low":    { tone: "neutral", icon: "flag",      label: "P4" },
};

const TASKS = [
  { id: "T-2041", title: "Verify pile coordinates against revised civil drawings", scope: "SOW-02 Pile Driving", scopeId: "SOW-02",
    status: "In Progress", priority: "Urgent", progress: 65,
    due: "05/06/2026", dueDays: 2, est: 8, logged: 5.5,
    assignee: { initials: "RF", name: "Robert Fox", color: "#76ADFF" },
    watchers: [{ initials: "EC", color: "#22863A" }, { initials: "NM", color: "#B279FF" }],
    tags: ["survey", "qa"], coLink: null, subtasks: 4, subtasksDone: 2, comments: 3, attachments: 2,
    blockedBy: null, dependsOn: ["T-2034"],
  },
  { id: "T-2042", title: "Predrill 480 piles — north section refusal zone", scope: "SOW-02 Pile Driving", scopeId: "SOW-02",
    status: "In Progress", priority: "High", progress: 42,
    due: "05/14/2026", dueDays: 10, est: 64, logged: 28,
    assignee: { initials: "JL", name: "Jordan Lin", color: "#FF9143" },
    watchers: [{ initials: "RF", color: "#76ADFF" }],
    tags: ["field-work"], coLink: "CO-013", subtasks: 6, subtasksDone: 2, comments: 8, attachments: 5,
  },
  { id: "T-2043", title: "Submit lien waivers — Apex MEP, Cornerstone Glazing", scope: "SOW-05 Electrical", scopeId: "SOW-05",
    status: "Blocked", priority: "Urgent", progress: 30,
    due: "05/05/2026", dueDays: 1, est: 4, logged: 2,
    assignee: { initials: "EC", name: "Emma Connor", color: "#22863A" },
    watchers: [],
    tags: ["compliance", "billing-blocker"], coLink: null, subtasks: 2, subtasksDone: 0, comments: 5, attachments: 1,
    blockedBy: "Awaiting signed waiver from Apex MEP",
  },
  { id: "T-2044", title: "Receive & inspect 240-module shipment from Solis Energy", scope: "SOW-04 Module Installation", scopeId: "SOW-04",
    status: "To Do", priority: "High", progress: 0,
    due: "05/12/2026", dueDays: 8, est: 12, logged: 0,
    assignee: { initials: "RF", name: "Robert Fox", color: "#76ADFF" },
    watchers: [{ initials: "EC", color: "#22863A" }],
    tags: ["receiving", "qa"], coLink: "CO-014", subtasks: 3, subtasksDone: 0, comments: 1, attachments: 0,
  },
  { id: "T-2045", title: "Install torque tubes — rows A1–A12 east tracker", scope: "SOW-03 Racking", scopeId: "SOW-03",
    status: "In Progress", priority: "High", progress: 78,
    due: "05/08/2026", dueDays: 4, est: 80, logged: 62,
    assignee: { initials: "JL", name: "Jordan Lin", color: "#FF9143" },
    watchers: [{ initials: "RF", color: "#76ADFF" }, { initials: "EC", color: "#22863A" }],
    tags: ["field-work"], coLink: null, subtasks: 12, subtasksDone: 9, comments: 4, attachments: 3,
  },
  { id: "T-2046", title: "Schedule SCADA commissioning walk-through with owner", scope: "SOW-06 Commissioning", scopeId: "SOW-06",
    status: "To Do", priority: "Med", progress: 0,
    due: "05/22/2026", dueDays: 18, est: 6, logged: 0,
    assignee: { initials: "NM", name: "Nick Merley", color: "#B279FF" },
    watchers: [],
    tags: ["commissioning"], coLink: "CO-010", subtasks: 2, subtasksDone: 0, comments: 0, attachments: 0,
  },
  { id: "T-2047", title: "Update SWPPP for 60-day extension — submit to county", scope: "SOW-01 Site Prep", scopeId: "SOW-01",
    status: "Review", priority: "Med", progress: 90,
    due: "05/07/2026", dueDays: 3, est: 6, logged: 5,
    assignee: { initials: "EC", name: "Emma Connor", color: "#22863A" },
    watchers: [{ initials: "RF", color: "#76ADFF" }],
    tags: ["compliance", "permit"], coLink: "CO-009", subtasks: 1, subtasksDone: 1, comments: 2, attachments: 4,
  },
  { id: "T-2048", title: "Review CO-014 expansion impact on SOW-04 schedule", scope: "SOW-04 Module Installation", scopeId: "SOW-04",
    status: "Review", priority: "High", progress: 60,
    due: "05/06/2026", dueDays: 2, est: 4, logged: 2.5,
    assignee: { initials: "EC", name: "Emma Connor", color: "#22863A" },
    watchers: [{ initials: "NM", color: "#B279FF" }],
    tags: ["change-order", "scheduling"], coLink: "CO-014", subtasks: 2, subtasksDone: 1, comments: 6, attachments: 1,
  },
  { id: "T-2049", title: "Photograph & log east tracker row foundations", scope: "SOW-02 Pile Driving", scopeId: "SOW-02",
    status: "Done", priority: "Low", progress: 100,
    due: "05/01/2026", dueDays: -3, est: 3, logged: 3,
    assignee: { initials: "RF", name: "Robert Fox", color: "#76ADFF" },
    watchers: [],
    tags: ["documentation"], coLink: null, subtasks: 1, subtasksDone: 1, comments: 1, attachments: 8,
    completedDate: "05/01/2026",
  },
  { id: "T-2050", title: "Issue RFP for galvanized racking substitute (Unirac)", scope: "SOW-03 Racking", scopeId: "SOW-03",
    status: "Done", priority: "High", progress: 100,
    due: "04/24/2026", dueDays: -10, est: 8, logged: 7,
    assignee: { initials: "JL", name: "Jordan Lin", color: "#FF9143" },
    watchers: [{ initials: "EC", color: "#22863A" }],
    tags: ["procurement"], coLink: "CO-012", subtasks: 3, subtasksDone: 3, comments: 4, attachments: 2,
    completedDate: "04/22/2026",
  },
  { id: "T-2051", title: "Reconcile certified payroll — week ending 04/26", scope: "SOW-05 Electrical", scopeId: "SOW-05",
    status: "Backlog", priority: "Med", progress: 0,
    due: "05/15/2026", dueDays: 11, est: 4, logged: 0,
    assignee: { initials: "EC", name: "Emma Connor", color: "#22863A" },
    watchers: [],
    tags: ["compliance", "payroll"], coLink: null, subtasks: 0, subtasksDone: 0, comments: 0, attachments: 0,
  },
  { id: "T-2052", title: "Coordinate transformer pad delivery with utility", scope: "SOW-05 Electrical", scopeId: "SOW-05",
    status: "Backlog", priority: "Low", progress: 0,
    due: "05/28/2026", dueDays: 24, est: 10, logged: 0,
    assignee: { initials: "NM", name: "Nick Merley", color: "#B279FF" },
    watchers: [],
    tags: ["coordination"], coLink: null, subtasks: 0, subtasksDone: 0, comments: 0, attachments: 0,
  },
  { id: "T-2053", title: "Walk down DC combiner reconfiguration plan with electricians", scope: "SOW-05 Electrical", scopeId: "SOW-05",
    status: "To Do", priority: "Med", progress: 0,
    due: "05/11/2026", dueDays: 7, est: 4, logged: 0,
    assignee: { initials: "NM", name: "Nick Merley", color: "#B279FF" },
    watchers: [{ initials: "JL", color: "#FF9143" }],
    tags: ["coordination", "field-work"], coLink: "CO-014", subtasks: 1, subtasksDone: 0, comments: 2, attachments: 0,
  },
  { id: "T-2054", title: "Tracker controller Modbus integration test", scope: "SOW-06 Commissioning", scopeId: "SOW-06",
    status: "Backlog", priority: "High", progress: 0,
    due: "06/04/2026", dueDays: 31, est: 16, logged: 0,
    assignee: { initials: "NM", name: "Nick Merley", color: "#B279FF" },
    watchers: [],
    tags: ["commissioning", "testing"], coLink: "CO-010", subtasks: 4, subtasksDone: 0, comments: 0, attachments: 1,
  },
  { id: "T-2055", title: "Daily progress report — site walk 05/04", scope: "SOW-01 Site Prep", scopeId: "SOW-01",
    status: "Done", priority: "Low", progress: 100,
    due: "05/04/2026", dueDays: 0, est: 1, logged: 1,
    assignee: { initials: "RF", name: "Robert Fox", color: "#76ADFF" },
    watchers: [],
    tags: ["documentation", "daily-log"], coLink: null, subtasks: 0, subtasksDone: 0, comments: 0, attachments: 6,
    completedDate: "05/04/2026",
  },
];

const TASK_KANBAN_COLS = ["Backlog", "To Do", "In Progress", "Blocked", "Review", "Done"];

const TasksTab = ({ p }) => {
  const [view, setView] = React.useState("list"); // "list" | "kanban"
  const [tasks, setTasks] = React.useState(TASKS);
  const [search, setSearch] = React.useState("");
  const [statusFilter, setStatusFilter] = React.useState("All");
  const [scopeFilter, setScopeFilter] = React.useState("All");
  const [assigneeFilter, setAssigneeFilter] = React.useState("All");
  const [priorityFilter, setPriorityFilter] = React.useState("All");
  const [dueSoonOnly, setDueSoonOnly] = React.useState(false);
  const [groupBy, setGroupBy] = React.useState("Status"); // List grouping
  const [selectedId, setSelectedId] = React.useState(null);

  // Move a task to a new status (used by Kanban drag-and-drop)
  const moveTask = React.useCallback((taskId, newStatus) => {
    setTasks(prev => prev.map(t => {
      if (t.id !== taskId || t.status === newStatus) return t;
      const next = { ...t, status: newStatus };
      // Sensible derived updates when moving to Done
      if (newStatus === "Done") {
        next.progress = 100;
        next.completedDate = next.completedDate || "Today";
      } else if (t.status === "Done" && newStatus !== "Done") {
        // Coming out of Done — reset to in-progress-ish if we were 100%
        if (next.progress === 100) next.progress = 90;
        next.completedDate = undefined;
      }
      return next;
    }));
  }, []);

  const filtered = tasks.filter(t => {
    if (statusFilter !== "All" && t.status !== statusFilter) return false;
    if (scopeFilter !== "All" && t.scopeId !== scopeFilter) return false;
    if (assigneeFilter !== "All" && t.assignee.name !== assigneeFilter) return false;
    if (priorityFilter !== "All" && t.priority !== priorityFilter) return false;
    if (dueSoonOnly && !(t.dueDays >= 0 && t.dueDays <= 3 && t.status !== "Done")) return false;
    if (search && !(`${t.id} ${t.title} ${t.tags.join(" ")}`.toLowerCase()).includes(search.toLowerCase())) return false;
    return true;
  });

  // KPIs
  const open = tasks.filter(t => t.status !== "Done");
  const overdue = tasks.filter(t => t.dueDays < 0 && t.status !== "Done");
  const dueThisWeek = tasks.filter(t => t.dueDays >= 0 && t.dueDays <= 7 && t.status !== "Done");
  const blocked = tasks.filter(t => t.status === "Blocked");
  const inReview = tasks.filter(t => t.status === "Review");
  const doneCount = tasks.filter(t => t.status === "Done").length;
  const completionPct = Math.round((doneCount / tasks.length) * 100);

  const assignees = ["All", ...Array.from(new Set(tasks.map(t => t.assignee.name)))];
  const scopes = ["All", ...Array.from(new Set(tasks.map(t => t.scopeId)))];

  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 16 }}>
      {/* KPI strip */}
      <div style={{ display: "grid", gridTemplateColumns: "repeat(5, 1fr)", gap: 12 }}>
        <COKPI label="Open Tasks" value={`${open.length}`} sub={`of ${tasks.length} total`} icon="list" />
        <COKPI
          label="Overdue"
          value={`${overdue.length}`}
          sub={overdue.length > 0 ? "Need immediate attention" : "All on schedule"}
          accent={overdue.length > 0 ? "rgb(192,47,50)" : "rgb(34,134,58)"}
          accentBg={overdue.length > 0 ? "rgba(252,226,226,.5)" : undefined}
          urgent={overdue.length > 0}
        />
        <COKPI
          label="Due This Week"
          value={`${dueThisWeek.length}`}
          sub={`${dueThisWeek.filter(t => t.priority === "Urgent" || t.priority === "High").length} high priority`}
          accent="rgb(178,121,26)"
        />
        <COKPI
          label="Blocked / In Review"
          value={`${blocked.length} · ${inReview.length}`}
          sub="Awaiting unblock or sign-off"
          accent="rgb(83,74,255)"
        />
        <COKPI
          label="Completion"
          value={`${completionPct}%`}
          sub={`${doneCount} done · ${open.length} remaining`}
          accent="rgb(34,134,58)"
        />
      </div>

      {/* Toolbar */}
      <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", gap: 12, flexWrap: "wrap" }}>
        <div style={{ display: "flex", alignItems: "center", gap: 8, flexWrap: "wrap" }}>
          <div style={{ position: "relative", width: 260 }}>
            <span style={{ position: "absolute", left: 10, top: 8, opacity: 0.5 }}>
              <Icon name="search" size={14} color="rgb(106,115,131)" />
            </span>
            <input value={search} onChange={e => setSearch(e.target.value)} placeholder="Search tasks, tags, ID…" style={{
              width: "100%", boxSizing: "border-box", paddingLeft: 30, paddingRight: 10, height: 30, borderRadius: 9999,
              border: "1px solid rgb(213,219,225)", fontSize: 13, fontFamily: "inherit", outline: "none", background: "#fff"
            }} />
          </div>
          {view === "list" && <FilterPill2Click label="Status" value={statusFilter} options={["All", ...Object.keys(TASK_STATUS)]} onChange={setStatusFilter} icon="flag" />}
          <FilterPill2Click label="Scope" value={scopeFilter} options={scopes} onChange={setScopeFilter} icon="layers" />
          <FilterPill2Click label="Assignee" value={assigneeFilter} options={assignees} onChange={setAssigneeFilter} icon="user" />
          <FilterPill2Click label="Priority" value={priorityFilter} options={["All", ...Object.keys(TASK_PRIORITY)]} onChange={setPriorityFilter} icon="alert" />
          <button onClick={() => setDueSoonOnly(!dueSoonOnly)} style={{
            height: 28, padding: "0 12px", borderRadius: 9999,
            background: dueSoonOnly ? "rgb(255,243,210)" : "#fff",
            border: `1px solid ${dueSoonOnly ? "rgb(228,182,68)" : "rgb(213,219,225)"}`,
            color: dueSoonOnly ? "rgb(178,121,26)" : "rgb(65,69,82)",
            fontSize: 12, fontWeight: 500, fontFamily: "inherit", cursor: "pointer",
            display: "inline-flex", alignItems: "center", gap: 5, whiteSpace: "nowrap"
          }}>
            <Icon name="clock" size={12} />
            Due in 3 days
            {dueSoonOnly && <Icon name="cross" size={10} />}
          </button>
        </div>
        <div style={{ display: "flex", gap: 8, alignItems: "center" }}>
          {/* View switcher */}
          <ViewToggle view={view} onChange={setView} />
          <span style={{ width: 1, height: 22, background: "rgb(227,229,232)" }} />
          <Button kind="ghost" size="sm" icon="sparkles">AI Plan</Button>
          <Button kind="primary" size="sm" icon="plus">New Task</Button>
        </div>
      </div>

      {/* Body */}
      {view === "list" ? (
        <TaskListView
          tasks={filtered}
          groupBy={groupBy}
          setGroupBy={setGroupBy}
          selectedId={selectedId}
          setSelectedId={setSelectedId}
        />
      ) : (
        <TaskKanbanView tasks={filtered} setSelectedId={setSelectedId} onMoveTask={moveTask} />
      )}
    </div>
  );
};

// --- View Toggle (List | Kanban) ---
const ViewToggle = ({ view, onChange }) => (
  <div style={{
    display: "inline-flex", background: "rgb(241,242,244)", borderRadius: 9999, padding: 2,
    border: "1px solid rgb(231,233,236)"
  }}>
    {[
      { id: "list",   icon: "list",   label: "List" },
      { id: "kanban", icon: "layers", label: "Kanban" },
    ].map(opt => {
      const active = view === opt.id;
      return (
        <button key={opt.id} onClick={() => onChange(opt.id)} style={{
          height: 24, padding: "0 12px", borderRadius: 9999, border: 0,
          background: active ? "#fff" : "transparent",
          color: active ? "rgb(83,74,255)" : "rgb(65,69,82)",
          fontSize: 12, fontWeight: 600, fontFamily: "inherit", cursor: "pointer",
          display: "inline-flex", alignItems: "center", gap: 5,
          boxShadow: active ? "0 1px 2px rgba(43,46,83,.08)" : "none",
          transition: "all 120ms",
        }}>
          <Icon name={opt.icon} size={12} />
          {opt.label}
        </button>
      );
    })}
  </div>
);

// =================================================================
// LIST VIEW — grouped table with assignee, due, priority, progress, etc.
// =================================================================
const TaskListView = ({ tasks, groupBy, setGroupBy, selectedId, setSelectedId }) => {
  const groups = React.useMemo(() => {
    if (groupBy === "None") return [{ key: "All", label: `All Tasks (${tasks.length})`, items: tasks }];
    if (groupBy === "Status") {
      return Object.keys(TASK_STATUS)
        .map(s => ({ key: s, label: s, items: tasks.filter(t => t.status === s), tone: TASK_STATUS[s].tone }))
        .filter(g => g.items.length > 0);
    }
    if (groupBy === "Scope") {
      const scopes = Array.from(new Set(tasks.map(t => t.scope)));
      return scopes.map(s => ({ key: s, label: s, items: tasks.filter(t => t.scope === s) }));
    }
    if (groupBy === "Assignee") {
      const people = Array.from(new Set(tasks.map(t => t.assignee.name)));
      return people.map(p => ({
        key: p, label: p,
        items: tasks.filter(t => t.assignee.name === p),
        avatar: tasks.find(t => t.assignee.name === p)?.assignee
      }));
    }
    if (groupBy === "Priority") {
      return Object.keys(TASK_PRIORITY)
        .map(p => ({ key: p, label: p, items: tasks.filter(t => t.priority === p), tone: TASK_PRIORITY[p].tone }))
        .filter(g => g.items.length > 0);
    }
    return [{ key: "All", label: "All", items: tasks }];
  }, [tasks, groupBy]);

  const [collapsed, setCollapsed] = React.useState({});
  const toggleGroup = (k) => setCollapsed(c => ({ ...c, [k]: !c[k] }));

  return (
    <div style={{ display: "grid", gridTemplateColumns: selectedId ? "minmax(0,1fr) minmax(0,420px)" : "1fr", gap: 16, alignItems: "flex-start" }}>
      <Card padding={0}>
        {/* Sub-toolbar with group-by */}
        <div style={{
          padding: "8px 14px", display: "flex", alignItems: "center", justifyContent: "space-between",
          background: "rgb(252,252,253)", borderBottom: "1px solid rgb(239,240,242)"
        }}>
          <div style={{ display: "inline-flex", alignItems: "center", gap: 6, fontSize: 11, fontWeight: 600, color: "rgb(106,115,131)", textTransform: "uppercase", letterSpacing: ".04em" }}>
            <span>{tasks.length} task{tasks.length === 1 ? "" : "s"}</span>
          </div>
          <div style={{ display: "inline-flex", alignItems: "center", gap: 8 }}>
            <span style={{ fontSize: 11, color: "rgb(106,115,131)", fontWeight: 500 }}>Group by</span>
            <div style={{ display: "inline-flex", background: "#fff", borderRadius: 9999, padding: 2, border: "1px solid rgb(227,229,232)" }}>
              {["None", "Status", "Scope", "Assignee", "Priority"].map(g => (
                <button key={g} onClick={() => setGroupBy(g)} style={{
                  height: 22, padding: "0 10px", borderRadius: 9999, border: 0,
                  background: groupBy === g ? "rgb(232,233,246)" : "transparent",
                  color: groupBy === g ? "rgb(83,74,255)" : "rgb(65,69,82)",
                  fontSize: 11, fontWeight: 600, fontFamily: "inherit", cursor: "pointer"
                }}>{g}</button>
              ))}
            </div>
          </div>
        </div>

        {/* Column header */}
        <TaskListHeader />

        {/* Groups */}
        {groups.map(g => {
          const isCollapsed = collapsed[g.key];
          return (
            <div key={g.key}>
              <div onClick={() => toggleGroup(g.key)} style={{
                padding: "8px 14px",
                display: "flex", alignItems: "center", gap: 8,
                background: "rgb(252,252,253)",
                borderBottom: "1px solid rgb(239,240,242)",
                cursor: "pointer", fontSize: 12, color: "rgb(65,69,82)", fontWeight: 600,
                userSelect: "none"
              }}>
                <Icon name={isCollapsed ? "chevronRight" : "expandDown"} size={11} color="rgb(106,115,131)" />
                {g.tone && <span style={{ width: 8, height: 8, borderRadius: "50%", background: TONES[g.tone].fg }} />}
                {g.avatar && <Avatar size={18} initials={g.avatar.initials} color={g.avatar.color} />}
                <span>{g.label}</span>
                <span style={{ fontSize: 11, padding: "1px 7px", borderRadius: 9999, background: "rgb(241,242,244)", color: "rgb(106,115,131)", fontWeight: 500 }}>
                  {g.items.length}
                </span>
              </div>
              {!isCollapsed && g.items.map((t, i) => (
                <TaskListRow
                  key={t.id}
                  task={t}
                  isSelected={t.id === selectedId}
                  onClick={() => setSelectedId(t.id === selectedId ? null : t.id)}
                  isLast={i === g.items.length - 1}
                />
              ))}
            </div>
          );
        })}

        {tasks.length === 0 && (
          <div style={{ padding: "40px 16px", textAlign: "center", color: "rgb(106,115,131)", fontSize: 13 }}>
            No tasks match these filters.
          </div>
        )}
      </Card>

      {selectedId && (
        <TaskDetailPanel
          task={tasks.find(t => t.id === selectedId)}
          onClose={() => setSelectedId(null)}
        />
      )}
    </div>
  );
};

// 30 (handle) | flex (title+meta) | 90 (priority) | 130 (assignee) | 130 (due) | 130 (progress) | 110 (links)
const COL_TEMPLATE = "26px minmax(0,1fr) 92px 130px 130px 130px 110px";

const TaskListHeader = () => (
  <div style={{
    display: "grid", gridTemplateColumns: COL_TEMPLATE, gap: 12,
    padding: "8px 14px",
    fontSize: 10, color: "rgb(106,115,131)", fontWeight: 600,
    textTransform: "uppercase", letterSpacing: ".05em",
    background: "#fff",
    borderBottom: "1px solid rgb(239,240,242)",
    alignItems: "center"
  }}>
    <span></span>
    <span>Task</span>
    <span>Priority</span>
    <span>Assignee</span>
    <span>Due</span>
    <span>Progress</span>
    <span style={{ textAlign: "right" }}>Activity</span>
  </div>
);

const TaskListRow = ({ task, isSelected, onClick, isLast }) => {
  const [hover, setHover] = React.useState(false);
  const [checked, setChecked] = React.useState(task.status === "Done");
  const overdue = task.dueDays < 0 && task.status !== "Done";
  const dueSoon = task.dueDays >= 0 && task.dueDays <= 2 && task.status !== "Done";
  const pri = TASK_PRIORITY[task.priority];
  const stat = TASK_STATUS[task.status];

  return (
    <div onClick={onClick}
      onMouseEnter={() => setHover(true)} onMouseLeave={() => setHover(false)}
      style={{
        display: "grid", gridTemplateColumns: COL_TEMPLATE, gap: 12,
        padding: "12px 14px",
        borderBottom: isLast ? "none" : "1px solid rgb(239,240,242)",
        background: isSelected ? "rgba(232,233,246,.4)" : hover ? "rgb(252,252,253)" : "#fff",
        borderLeft: isSelected ? "3px solid rgb(83,74,255)" : "3px solid transparent",
        paddingLeft: isSelected ? 11 : 14,
        cursor: "pointer", alignItems: "center",
        fontSize: 13, transition: "background 100ms"
      }}>
      {/* Checkbox / status dot */}
      <div onClick={e => { e.stopPropagation(); setChecked(!checked); }} style={{
        width: 18, height: 18, borderRadius: 5,
        border: checked ? "0" : `1.5px solid ${overdue ? "rgb(192,47,50)" : "rgb(213,219,225)"}`,
        background: checked ? "rgb(34,134,58)" : "#fff",
        display: "flex", alignItems: "center", justifyContent: "center",
        flexShrink: 0, transition: "all 120ms"
      }}>
        {checked && <Icon name="check" size={11} color="#fff" />}
      </div>

      {/* Title + meta */}
      <div style={{ minWidth: 0, display: "flex", flexDirection: "column", gap: 4 }}>
        <div style={{ display: "flex", alignItems: "center", gap: 8 }}>
          <span style={{ fontSize: 11, color: "rgb(106,115,131)", fontWeight: 600, fontVariantNumeric: "tabular-nums", flexShrink: 0 }}>{task.id}</span>
          <Chip tone={stat.tone} dot>{task.status}</Chip>
        </div>
        <div style={{
          fontSize: 13.5, fontWeight: 500, color: checked || task.status === "Done" ? "rgb(106,115,131)" : "rgb(26,27,37)",
          textDecoration: checked || task.status === "Done" ? "line-through" : "none",
          lineHeight: 1.35,
          overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap"
        }}>{task.title}</div>
        <div style={{ display: "flex", alignItems: "center", gap: 10, fontSize: 11, color: "rgb(106,115,131)", flexWrap: "wrap" }}>
          <span style={{ display: "inline-flex", alignItems: "center", gap: 4 }}>
            <Icon name="layers" size={10} />
            {task.scope}
          </span>
          {task.coLink && (
            <span style={{ display: "inline-flex", alignItems: "center", gap: 4, color: "rgb(83,74,255)", fontWeight: 500 }}>
              <Icon name="refresh" size={10} />
              {task.coLink}
            </span>
          )}
          {task.tags.slice(0, 2).map(tag => (
            <span key={tag} style={{ padding: "1px 6px", borderRadius: 4, background: "rgb(241,242,244)", fontSize: 10, fontWeight: 500, color: "rgb(65,69,82)" }}>{tag}</span>
          ))}
        </div>
      </div>

      {/* Priority */}
      <div>
        <PriorityPill priority={task.priority} />
      </div>

      {/* Assignee */}
      <div style={{ display: "flex", alignItems: "center", gap: 6, minWidth: 0 }}>
        <Avatar size={22} initials={task.assignee.initials} color={task.assignee.color} />
        <span style={{ fontSize: 12, color: "rgb(65,69,82)", overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }}>{task.assignee.name}</span>
      </div>

      {/* Due */}
      <div style={{ display: "flex", flexDirection: "column", gap: 1 }}>
        <span style={{
          fontSize: 12, fontWeight: 600, fontVariantNumeric: "tabular-nums",
          color: overdue ? "rgb(192,47,50)" : dueSoon ? "rgb(178,121,26)" : "rgb(26,27,37)"
        }}>
          {task.due}
        </span>
        <span style={{ fontSize: 11, color: overdue ? "rgb(192,47,50)" : "rgb(106,115,131)" }}>
          {task.status === "Done"
            ? `Done ${task.completedDate || ""}`
            : overdue ? `${Math.abs(task.dueDays)}d overdue`
            : task.dueDays === 0 ? "Today"
            : task.dueDays === 1 ? "Tomorrow"
            : `in ${task.dueDays}d`}
        </span>
      </div>

      {/* Progress */}
      <div style={{ display: "flex", flexDirection: "column", gap: 3 }}>
        <div style={{ display: "flex", justifyContent: "space-between", fontSize: 10, color: "rgb(106,115,131)", fontWeight: 500 }}>
          <span>{task.progress}%</span>
          <span style={{ fontVariantNumeric: "tabular-nums" }}>{task.logged}/{task.est}h</span>
        </div>
        <ProgressBar
          value={task.progress}
          color={task.status === "Done" ? "rgb(34,134,58)" : task.status === "Blocked" ? "rgb(192,47,50)" : "rgb(83,74,255)"}
          height={4}
        />
        {task.subtasks > 0 && (
          <span style={{ fontSize: 10, color: "rgb(106,115,131)" }}>{task.subtasksDone}/{task.subtasks} subtasks</span>
        )}
      </div>

      {/* Activity meta */}
      <div style={{ display: "flex", alignItems: "center", gap: 10, justifyContent: "flex-end", color: "rgb(106,115,131)", fontSize: 11 }}>
        {task.comments > 0 && (
          <span style={{ display: "inline-flex", alignItems: "center", gap: 3 }}>
            <Icon name="comment" size={11} /> {task.comments}
          </span>
        )}
        {task.attachments > 0 && (
          <span style={{ display: "inline-flex", alignItems: "center", gap: 3 }}>
            <Icon name="paperclip" size={11} /> {task.attachments}
          </span>
        )}
        {task.watchers.length > 0 && (
          <AvatarGroup people={task.watchers} size={18} max={3} />
        )}
      </div>
    </div>
  );
};

const PriorityPill = ({ priority }) => {
  const cfg = TASK_PRIORITY[priority];
  const t = TONES[cfg.tone];
  return (
    <span style={{
      display: "inline-flex", alignItems: "center", gap: 4,
      padding: "2px 8px", borderRadius: 9999,
      background: t.bg, color: t.fg,
      fontSize: 11, fontWeight: 600
    }}>
      <Icon name={cfg.icon} size={10} />
      {priority}
    </span>
  );
};

// =================================================================
// KANBAN VIEW — with HTML5 drag-and-drop between status columns
// =================================================================
const TaskKanbanView = ({ tasks, setSelectedId, onMoveTask }) => {
  const [draggingId, setDraggingId] = React.useState(null);
  const [dragOverCol, setDragOverCol] = React.useState(null);

  const cols = TASK_KANBAN_COLS.map(s => ({
    status: s,
    items: tasks.filter(t => t.status === s).sort((a, b) => {
      const order = { Urgent: 0, High: 1, Med: 2, Low: 3 };
      return order[a.priority] - order[b.priority];
    }),
  }));

  const handleDragStart = (taskId) => setDraggingId(taskId);
  const handleDragEnd = () => { setDraggingId(null); setDragOverCol(null); };
  const handleDragOverCol = (status) => setDragOverCol(status);
  const handleDropOnCol = (status) => {
    if (draggingId) onMoveTask(draggingId, status);
    setDraggingId(null);
    setDragOverCol(null);
  };

  return (
    <div style={{
      display: "grid",
      gridTemplateColumns: `repeat(${TASK_KANBAN_COLS.length}, minmax(260px, 1fr))`,
      gap: 12,
      overflowX: "auto", paddingBottom: 4
    }}>
      {cols.map(col => (
        <KanbanColumn
          key={col.status}
          status={col.status}
          items={col.items}
          setSelectedId={setSelectedId}
          isDragOver={dragOverCol === col.status}
          draggingId={draggingId}
          onDragStart={handleDragStart}
          onDragEnd={handleDragEnd}
          onDragOverCol={handleDragOverCol}
          onDropOnCol={handleDropOnCol}
        />
      ))}
    </div>
  );
};

const KanbanColumn = ({ status, items, setSelectedId, isDragOver, draggingId, onDragStart, onDragEnd, onDragOverCol, onDropOnCol }) => {
  const cfg = TASK_STATUS[status];
  const t = TONES[cfg.tone];
  const isDraggingActive = draggingId != null;

  return (
    <div
      onDragEnter={(e) => { if (isDraggingActive) { e.preventDefault(); onDragOverCol(status); } }}
      onDragOver={(e) => { if (isDraggingActive) { e.preventDefault(); e.dataTransfer.dropEffect = "move"; onDragOverCol(status); } }}
      onDrop={(e) => { e.preventDefault(); onDropOnCol(status); }}
      style={{
        background: isDragOver ? "rgba(232,233,246,.7)" : "rgb(248,248,251)",
        border: `1px ${isDragOver ? "dashed" : "solid"} ${isDragOver ? "rgb(83,74,255)" : "rgb(231,233,236)"}`,
        borderRadius: 12,
        display: "flex", flexDirection: "column", minHeight: 200, minWidth: 0,
        transition: "background 120ms, border-color 120ms",
      }}>
      {/* Column header */}
      <div style={{
        padding: "12px 14px",
        display: "flex", alignItems: "center", justifyContent: "space-between",
        borderBottom: "1px solid rgb(239,240,242)",
        background: "#fff", borderRadius: "12px 12px 0 0"
      }}>
        <div style={{ display: "inline-flex", alignItems: "center", gap: 8 }}>
          <span style={{ width: 8, height: 8, borderRadius: "50%", background: t.fg }} />
          <span style={{ fontSize: 12, fontWeight: 600, color: "rgb(26,27,37)" }}>{status}</span>
          <span style={{ fontSize: 11, padding: "1px 7px", borderRadius: 9999, background: "rgb(241,242,244)", color: "rgb(106,115,131)", fontWeight: 500 }}>
            {items.length}
          </span>
        </div>
        <IconBtn icon="plus" size={22} />
      </div>

      {/* Cards */}
      <div style={{ display: "flex", flexDirection: "column", gap: 8, padding: 10, minHeight: 80, flex: 1 }}>
        {items.map(t => (
          <KanbanCard
            key={t.id}
            task={t}
            onClick={() => setSelectedId(t.id)}
            isDragging={draggingId === t.id}
            onDragStart={onDragStart}
            onDragEnd={onDragEnd}
          />
        ))}
        {items.length === 0 && (
          <div style={{
            padding: "20px 12px", textAlign: "center", fontSize: 12,
            color: isDragOver ? "rgb(83,74,255)" : "rgb(163,172,186)",
            border: `1.5px dashed ${isDragOver ? "rgb(83,74,255)" : "rgb(213,219,225)"}`,
            borderRadius: 8, background: "transparent",
            fontWeight: isDragOver ? 600 : 400,
          }}>
            {isDragOver ? `Drop to move to ${status}` : "No tasks"}
          </div>
        )}
      </div>
    </div>
  );
};

const KanbanCard = ({ task, onClick, isDragging, onDragStart, onDragEnd }) => {
  const [hover, setHover] = React.useState(false);
  const overdue = task.dueDays < 0 && task.status !== "Done";
  const dueSoon = task.dueDays >= 0 && task.dueDays <= 2 && task.status !== "Done";
  const stat = TASK_STATUS[task.status];

  // Left accent stripe by priority
  const priCfg = TASK_PRIORITY[task.priority];
  const priColor = TONES[priCfg.tone].fg;

  return (
    <div onClick={onClick}
      draggable
      onDragStart={(e) => {
        e.dataTransfer.effectAllowed = "move";
        try { e.dataTransfer.setData("text/plain", task.id); } catch (_) {}
        onDragStart && onDragStart(task.id);
      }}
      onDragEnd={() => onDragEnd && onDragEnd()}
      onMouseEnter={() => setHover(true)} onMouseLeave={() => setHover(false)}
      style={{
        background: "#fff",
        border: "1px solid rgb(231,233,236)",
        borderLeft: `3px solid ${priColor}`,
        borderRadius: 8,
        padding: "10px 12px",
        cursor: isDragging ? "grabbing" : "grab",
        boxShadow: isDragging ? "0 8px 20px rgba(43,46,83,.18)" : hover ? "0 4px 12px rgba(43,46,83,.08)" : "0 1px 2px rgba(43,46,83,.04)",
        transform: isDragging ? "rotate(1.5deg)" : hover ? "translateY(-1px)" : "none",
        opacity: isDragging ? 0.6 : 1,
        transition: "box-shadow 140ms, transform 140ms, opacity 140ms",
        display: "flex", flexDirection: "column", gap: 8,
        userSelect: "none",
      }}>
      {/* Top row: id + priority + tags */}
      <div style={{ display: "flex", alignItems: "center", gap: 6, justifyContent: "space-between" }}>
        <span style={{ fontSize: 11, color: "rgb(106,115,131)", fontWeight: 600, fontVariantNumeric: "tabular-nums" }}>{task.id}</span>
        <PriorityPill priority={task.priority} />
      </div>

      {/* Title */}
      <div style={{
        fontSize: 13, fontWeight: 500, color: "rgb(26,27,37)", lineHeight: 1.4,
        textDecoration: task.status === "Done" ? "line-through" : "none",
        opacity: task.status === "Done" ? 0.6 : 1,
        overflow: "hidden", display: "-webkit-box",
        WebkitLineClamp: 2, WebkitBoxOrient: "vertical"
      }}>{task.title}</div>

      {/* Scope + CO link chip row */}
      <div style={{ display: "flex", alignItems: "center", gap: 6, flexWrap: "wrap" }}>
        <span style={{ display: "inline-flex", alignItems: "center", gap: 4, fontSize: 10.5, padding: "2px 7px", borderRadius: 9999, background: "rgb(241,242,244)", color: "rgb(65,69,82)", fontWeight: 500 }}>
          <Icon name="layers" size={9} />
          {task.scopeId}
        </span>
        {task.coLink && (
          <span style={{ display: "inline-flex", alignItems: "center", gap: 4, fontSize: 10.5, padding: "2px 7px", borderRadius: 9999, background: "rgb(232,233,246)", color: "rgb(83,74,255)", fontWeight: 600 }}>
            <Icon name="refresh" size={9} />
            {task.coLink}
          </span>
        )}
      </div>

      {/* Blocked banner */}
      {task.status === "Blocked" && task.blockedBy && (
        <div style={{
          padding: "6px 8px", borderRadius: 6,
          background: "rgba(252,226,226,.5)",
          border: "1px solid rgba(192,47,50,.2)",
          fontSize: 11, color: "rgb(120,28,30)",
          display: "flex", alignItems: "flex-start", gap: 5,
          lineHeight: 1.4
        }}>
          <Icon name="alert" size={11} color="rgb(192,47,50)" style={{ marginTop: 1, flexShrink: 0 }} />
          {task.blockedBy}
        </div>
      )}

      {/* Progress bar (only if in-flight) */}
      {task.progress > 0 && task.progress < 100 && (
        <div style={{ display: "flex", flexDirection: "column", gap: 3 }}>
          <div style={{ display: "flex", justifyContent: "space-between", fontSize: 10, color: "rgb(106,115,131)" }}>
            <span style={{ fontWeight: 500 }}>{task.progress}%</span>
            {task.subtasks > 0 && <span>{task.subtasksDone}/{task.subtasks} subtasks</span>}
          </div>
          <ProgressBar
            value={task.progress}
            color={task.status === "Blocked" ? "rgb(192,47,50)" : "rgb(83,74,255)"}
            height={3}
          />
        </div>
      )}

      {/* Footer: due, assignee, comments */}
      <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", gap: 6, paddingTop: 4, borderTop: "1px solid rgb(239,240,242)" }}>
        <div style={{
          display: "inline-flex", alignItems: "center", gap: 4,
          fontSize: 11, fontWeight: 500,
          color: overdue ? "rgb(192,47,50)" : dueSoon ? "rgb(178,121,26)" : "rgb(106,115,131)"
        }}>
          <Icon name="calendar" size={11} />
          <span style={{ fontVariantNumeric: "tabular-nums" }}>
            {task.status === "Done"
              ? `Done`
              : overdue ? `${Math.abs(task.dueDays)}d overdue`
              : task.dueDays === 0 ? "Today"
              : task.dueDays === 1 ? "Tomorrow"
              : `${task.due.slice(0,5)}`}
          </span>
        </div>
        <div style={{ display: "inline-flex", alignItems: "center", gap: 8, color: "rgb(106,115,131)", fontSize: 11 }}>
          {task.comments > 0 && (
            <span style={{ display: "inline-flex", alignItems: "center", gap: 2 }}>
              <Icon name="comment" size={10} /> {task.comments}
            </span>
          )}
          {task.attachments > 0 && (
            <span style={{ display: "inline-flex", alignItems: "center", gap: 2 }}>
              <Icon name="paperclip" size={10} /> {task.attachments}
            </span>
          )}
          <Avatar size={20} initials={task.assignee.initials} color={task.assignee.color} />
        </div>
      </div>
    </div>
  );
};

// =================================================================
// DETAIL PANEL — opens on row click in list view
// =================================================================
const TaskDetailPanel = ({ task, onClose }) => {
  if (!task) return null;
  const overdue = task.dueDays < 0 && task.status !== "Done";
  const stat = TASK_STATUS[task.status];

  const subtaskList = [
    { name: "Confirm survey reference points with civil",  done: true },
    { name: "Cross-check 480 coordinates against rev-3 PDF", done: true },
    { name: "Flag 12 outliers for re-survey by JL crew", done: false },
    { name: "Sign off on revised CSV for pile-driving rig", done: false },
  ].slice(0, task.subtasks);

  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 12, position: "sticky", top: 12 }}>
      <Card padding={0}>
        {/* Header */}
        <div style={{ padding: "16px 18px", borderBottom: "1px solid rgb(239,240,242)" }}>
          <div style={{ display: "flex", alignItems: "flex-start", justifyContent: "space-between", gap: 10, marginBottom: 8 }}>
            <div style={{ minWidth: 0, flex: 1 }}>
              <div style={{ display: "flex", alignItems: "center", gap: 8, marginBottom: 6, flexWrap: "wrap" }}>
                <span style={{ fontSize: 12, color: "rgb(106,115,131)", fontWeight: 600, fontVariantNumeric: "tabular-nums" }}>{task.id}</span>
                <Chip tone={stat.tone} dot>{task.status}</Chip>
                <PriorityPill priority={task.priority} />
              </div>
              <h2 style={{ fontSize: 16, fontWeight: 700, margin: 0, letterSpacing: "-.01em", lineHeight: 1.35 }}>{task.title}</h2>
            </div>
            <IconBtn icon="cross" size={26} onClick={onClose} />
          </div>
          <div style={{ display: "flex", gap: 6, flexWrap: "wrap", paddingTop: 10, borderTop: "1px solid rgb(239,240,242)" }}>
            {task.status === "Blocked" && <Button kind="primary" size="sm" icon="check">Unblock</Button>}
            {task.status === "Review" && <Button kind="primary" size="sm" icon="check">Approve</Button>}
            {!["Done", "Blocked", "Review"].includes(task.status) && <Button kind="primary" size="sm" icon="check">Mark Complete</Button>}
            <Button kind="secondary" size="sm" icon="user">Reassign</Button>
            <Button kind="ghost" size="sm" icon="comment">Comment</Button>
          </div>
        </div>

        {/* Properties */}
        <div style={{ padding: "14px 18px", display: "grid", gridTemplateColumns: "1fr 1fr", gap: "12px 16px" }}>
          <PropRow label="Assignee">
            <div style={{ display: "inline-flex", alignItems: "center", gap: 6 }}>
              <Avatar size={20} initials={task.assignee.initials} color={task.assignee.color} />
              <span style={{ fontSize: 13, fontWeight: 500 }}>{task.assignee.name}</span>
            </div>
          </PropRow>
          <PropRow label="Due Date">
            <span style={{
              fontSize: 13, fontWeight: 600, fontVariantNumeric: "tabular-nums",
              color: overdue ? "rgb(192,47,50)" : "rgb(26,27,37)"
            }}>
              {task.due}
              <span style={{ marginLeft: 6, fontSize: 11, fontWeight: 500, color: overdue ? "rgb(192,47,50)" : "rgb(106,115,131)" }}>
                {task.status === "Done"
                  ? "·  Done"
                  : overdue ? `· ${Math.abs(task.dueDays)}d overdue`
                  : `· in ${task.dueDays}d`}
              </span>
            </span>
          </PropRow>
          <PropRow label="Scope">
            <div style={{ display: "inline-flex", alignItems: "center", gap: 5, fontSize: 13 }}>
              <Icon name="layers" size={11} color="rgb(83,74,255)" />
              {task.scope}
            </div>
          </PropRow>
          <PropRow label="Change Order">
            {task.coLink
              ? <span style={{ display: "inline-flex", alignItems: "center", gap: 4, fontSize: 13, color: "rgb(83,74,255)", fontWeight: 600 }}>
                  <Icon name="refresh" size={11} /> {task.coLink}
                </span>
              : <span style={{ fontSize: 13, color: "rgb(106,115,131)" }}>—</span>}
          </PropRow>
          <PropRow label="Estimate">
            <span style={{ fontSize: 13, fontWeight: 500, fontVariantNumeric: "tabular-nums" }}>{task.logged} / {task.est}h logged</span>
          </PropRow>
          <PropRow label="Watchers">
            {task.watchers.length > 0
              ? <AvatarGroup people={task.watchers} size={20} max={4} />
              : <span style={{ fontSize: 13, color: "rgb(106,115,131)" }}>—</span>}
          </PropRow>
        </div>

        {/* Tags */}
        {task.tags.length > 0 && (
          <div style={{ padding: "0 18px 14px", display: "flex", gap: 6, flexWrap: "wrap" }}>
            {task.tags.map(tag => (
              <span key={tag} style={{ padding: "2px 8px", borderRadius: 4, background: "rgb(241,242,244)", fontSize: 11, fontWeight: 500, color: "rgb(65,69,82)" }}>{tag}</span>
            ))}
          </div>
        )}
      </Card>

      {/* Blocker callout */}
      {task.status === "Blocked" && task.blockedBy && (
        <Card>
          <div style={{ display: "flex", alignItems: "flex-start", gap: 10 }}>
            <div style={{ width: 28, height: 28, borderRadius: 7, background: "rgba(252,226,226,.6)", display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0 }}>
              <Icon name="alert" size={14} color="rgb(192,47,50)" />
            </div>
            <div style={{ flex: 1 }}>
              <div style={{ fontSize: 12, fontWeight: 600, color: "rgb(192,47,50)", marginBottom: 2 }}>Blocked</div>
              <div style={{ fontSize: 13, color: "rgb(26,27,37)", lineHeight: 1.5 }}>{task.blockedBy}</div>
            </div>
          </div>
        </Card>
      )}

      {/* Progress + subtasks */}
      <Card>
        <div style={{ fontSize: 13, fontWeight: 600, marginBottom: 10 }}>Progress</div>
        <div style={{ display: "flex", justifyContent: "space-between", fontSize: 12, color: "rgb(106,115,131)", marginBottom: 6 }}>
          <span style={{ fontWeight: 600, color: "rgb(26,27,37)" }}>{task.progress}% complete</span>
          <span style={{ fontVariantNumeric: "tabular-nums" }}>{task.subtasksDone}/{task.subtasks} subtasks</span>
        </div>
        <ProgressBar
          value={task.progress}
          color={task.status === "Done" ? "rgb(34,134,58)" : task.status === "Blocked" ? "rgb(192,47,50)" : "rgb(83,74,255)"}
          height={6}
        />
        {subtaskList.length > 0 && (
          <div style={{ marginTop: 14, display: "flex", flexDirection: "column", gap: 6 }}>
            {subtaskList.map((s, i) => (
              <div key={i} style={{ display: "flex", alignItems: "center", gap: 8, padding: "6px 0" }}>
                <div style={{
                  width: 16, height: 16, borderRadius: 4,
                  border: s.done ? "0" : "1.5px solid rgb(213,219,225)",
                  background: s.done ? "rgb(34,134,58)" : "#fff",
                  display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0
                }}>
                  {s.done && <Icon name="check" size={10} color="#fff" />}
                </div>
                <span style={{
                  fontSize: 13, color: s.done ? "rgb(106,115,131)" : "rgb(26,27,37)",
                  textDecoration: s.done ? "line-through" : "none"
                }}>{s.name}</span>
              </div>
            ))}
          </div>
        )}
      </Card>

      {/* Activity */}
      <Card padding={0}>
        <div style={{ padding: "12px 16px", borderBottom: "1px solid rgb(239,240,242)" }}>
          <span style={{ fontSize: 13, fontWeight: 600 }}>Activity</span>
        </div>
        <div style={{ padding: "10px 16px 14px", display: "flex", flexDirection: "column", gap: 12 }}>
          {[
            { who: task.assignee.name, when: "2h ago", text: "Logged 1.5h on coordinate verification.", icon: "clock", tone: "info" },
            { who: "AI",                when: "Yesterday", text: `Suggested splitting subtask 3 into per-row chunks.`, icon: "sparkles", tone: "purple" },
            { who: "Emma Connor",       when: "2d ago", text: "Moved task to In Progress.", icon: "activity", tone: "success" },
          ].map((a, i) => (
            <div key={i} style={{ display: "flex", gap: 10 }}>
              <div style={{
                width: 26, height: 26, borderRadius: "50%",
                background: TONES[a.tone].bg,
                display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0
              }}>
                <Icon name={a.icon} size={12} color={TONES[a.tone].fg} />
              </div>
              <div style={{ flex: 1, minWidth: 0 }}>
                <div style={{ fontSize: 13, color: "rgb(26,27,37)" }}>{a.text}</div>
                <div style={{ fontSize: 11, color: "rgb(106,115,131)", marginTop: 2 }}>{a.who} · {a.when}</div>
              </div>
            </div>
          ))}
        </div>
      </Card>
    </div>
  );
};

const PropRow = ({ label, children }) => (
  <div style={{ display: "flex", flexDirection: "column", gap: 4, minWidth: 0 }}>
    <span style={{ fontSize: 10.5, color: "rgb(106,115,131)", fontWeight: 600, textTransform: "uppercase", letterSpacing: ".05em" }}>{label}</span>
    <div style={{ minWidth: 0 }}>{children}</div>
  </div>
);

window.TasksTab = TasksTab;
