Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

68.0 KB
Raw Blame
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { Hash, Plus, CheckCircle2, ShieldCheck, KeyRound, Loader2, Users, ChevronDown, Settings, Settings2, X, Crown, UserPlus, Pencil, Inbox, Search, Split, Hand, MoreHorizontal, UserMinus, MessageSquare, KanbanSquare, CalendarClock } from 'lucide-react';
import { ModernAvatar, useModelBadge } from './ModernAvatar';
import { playSfx } from '../audio/chiptune';
import { badgeText } from '../away';
import { canRunNow, engineTip, isUsable, pinnedCantRun, type RunsOn } from '../engines';
import { statusOf, statusText, type BotStatus } from '../botStatus';
import { EnginesApi, useEngines } from '../useEngines';
import { claudeNeedsSignIn } from '../claudeLogin';
import { Space } from '../spaces';
import { teamNamed } from '../botHome';
import { SpaceSwitcher, SpaceTile } from './Spaces';
import { isMac } from './LayaStatus';
import { ApprovalsOffBadge } from './ApprovalsSetting';
import { approvalsOff } from '../approvalMode';
import { teamChannelOf } from '../teamChannel';
import { layaRoutes } from '../channels';
import { PhoneAppTip } from './PhoneAppTip';
import { trapFocusTarget } from '../imports';
import { type AttentionRow, type NeedsYouRow, PINNED_RANK, NEEDS_YOU_LABEL, workingIds, directIds, rollupLabel } from '../chatsTab';
import { askAction, askKind, askLine, isAsk } from '../askHere';
import { approvalLine } from '../approvals';
import { localClock } from '../clock';
import type { InboxItem } from '../api/inbox';
import { useDevMode } from './DevModeSwitch';
import { MovedPointer, SpaceSubline } from './FirstScreenBits';
import { DevDeskRow } from './DevDesk';
import { GlobeSlot, WatchGlobe, useFadeOut, useLingeringIds } from './WatchLive';
import type { Browsing } from '../watchLive';
import { IdeaJarRow } from './IdeaJarRow';
import { ReadyTeamsSidebarBox } from './ReadyTeamsEntry';
import { BudgetMeter, useBudgets } from './BotBudget';
import { PAUSED_ROW_LINE } from '../botBudget';
import { PlaceChipRow } from './PlaceChip';

/** A bot's name in its row; the tooltip says what it runs on in the same words as its avatar's model badge. */
const BotName: React.FC<{ kwant: Kwant; className: string }> = ({ kwant: k, className }) => {
  const badge = useModelBadge(k.id);
  return (
    <span data-bot-name className={className} title={`${k.name} · ${badge?.label ?? engineTip(k.preferred_model)}`}>
      {k.name}
    </span>
  );
};

export interface Kwant {
  id: string;
  name: string;
  title: string;
  persona_prompt: string;
  soul?: string;
  avatar_sprite: string;
  driver_type: string;
  preferred_model: string;
  /** What its newest reply ran on (GET /api/kwants, 0.9.6+); null when it never replied. */
  last_ran_on?: RunsOn | null;
  /** Pinned LLM profile; null uses the engine's default. */
  profile_id?: string | null;
  /** Whether turns may move to the engine's next profile when this one is out of usage. */
  profile_failover?: boolean;
  /** Its own approvals setting, and what it comes to with the space's; absent on older servers. */
  approvals?: 'inherit' | 'ask' | 'smart' | 'off';
  approvals_effective?: 'ask' | 'smart' | 'off';
  /** "Good for" / "Not for" phrases routing reads; absent on older servers. */
  covers?: string[];
  avoids?: string[];
  /** Which sites its own browser may open (browser-borrowed/contract.md); absent on older servers. */
  browser_allowed_sites?: string[];
  browser_blocked_sites?: string[];
  browser_block_private_network?: boolean;
}

export interface Channel {
  id: string;
  name: string;
  description?: string;
  lead_kwant_id: string;
  member_kwants: string[];
  routing_mode: string;
  /** Pinned for every turn in this channel; null when unset. */
  instructions?: string | null;
}

export interface Team {
  id: string;
  name: string;
  description?: string;
  workspace_path?: string;
  repo_url?: string;
  icon: string;
  lead_kwant_id: string;
  member_kwants: string[];
  /** Pinned for every turn in the team's channels; null when unset. */
  instructions?: string | null;
  created_at?: string | null;
  /** 0.7.7 (api.md §0.8): which ready-made team made it, and the action Undo reverses. */
  created_from_template?: 'inbox' | 'sales' | 'coding' | 'suggested' | null;
  template_action_id?: string | null;
}

/** What the sidebar shows: everything, or one phone tab. */
export type SidebarSection = 'all' | 'chats' | 'bots';

export interface SidebarProps {
  space: Space;
  spaces: Space[];
  onSwitchSpace: (id: string) => void;
  onCreateSpace: () => void;
  onOpenSpaceSettings: () => void;
  /** The phone tip: Settings › All spaces, at "Use kwant bots on your phone". */
  onOpenPhoneApp?: () => void;
  onOpenArchived: () => void;
  onOpenSearch: () => void;
  /** Phones show one tab at a time, full width. */
  section?: SidebarSection;
  phone?: boolean;
  kwants: Kwant[];
  channels: Channel[];
  teams: Team[];
  selectedId: string;
  isChannel: boolean;
  selectedTeamId?: string | null;
  /** What busy bots are doing ("Working on …", "Working here"), shown under their name. */
  busyKwants?: Record<string, string>;
  /** The full line behind a short busy line (an audit's who and why, 14a), for its tooltip. */
  busyTitles?: Record<string, string>;
  /** Teammates who already replied in a team turn now waiting on the person: "Done · replied in @team". */
  doneKwants?: Record<string, string>;
  /** Busy bots' status for the avatar dot; bots not listed are Available (or Can't run). */
  botStatus?: Record<string, BotStatus>;
  /** Open browser steps per bot ("Needs you" pill). */
  needsYou?: Record<string, number>;
  /** Phone Chats tab: bots waiting on you, one row each, first in the list. */
  needsYouRows?: NeedsYouRow[];
  /** W1: chats whose browser is active, by owner (team, channel, or bot for its direct chat). */
  browsingOwners?: Record<string, Browsing>;
  /** W1: bots whose running turn is in one of those chats, with that chat's browser. */
  browsingBots?: Record<string, Browsing>;
  /** Opens the owner's chat with the browser panel open (the W2 address). */
  onWatchBrowser?: (owner: string) => void;
  /** Owners whose browser holds an open step for the person: their rows show that, not the globe. */
  browserNeedsYou?: ReadonlySet<string>;
  /**
   * H5: every bot in the one attention order (`attentionRows`); undefined until the first status
   * fetch lands. Desktop pins ranks 1–4 above Channels; the phone's Chats tab moves bots into sections.
   */
  attention?: AttentionRow<Kwant>[];
  /** 0.9 V1: a row from a review item opens that item in Inbox › New. */
  onOpenReview?: (item: InboxItem) => void;
  /** Opens a Needs-you row's chat on its card. */
  onOpenNeedsYou?: (row: NeedsYouRow) => void;
  onSelectKwant: (kwant: Kwant) => void;
  onSelectChannel: (channel: Channel) => void;
  onSelectTeam: (team: Team) => void;
  onManageTeam?: (team: Team) => void;
  onUpdateTeamMembers?: (team: Team, memberIds: string[]) => void;
  onEditKwant?: (kwant: Kwant) => void;
  /** Opens the new-bot form; with a team, the new bot joins it. */
  onSummonKwant: (team?: Team) => void;
  onCreateTeam: () => void;
  /** Opens the new-channel form. */
  onCreateChannel?: () => void;
  /** Inbox: unseen count for the badge, whether it's open, and how to open it. */
  inboxCount?: number;
  inboxActive?: boolean;
  /** 31c: App's page is the chat. On Inbox, Mission board and the other pages no channel, team or bot row is "here". */
  onChat?: boolean;
  onOpenInbox?: () => void;
  /** Parallel Universe, a plain row (14c: the one-item "More" menu is gone). */
  parallelActive?: boolean;
  onOpenParallel?: () => void;
  /** Shows the Mission board row; the server's mission_board setting decides. */
  missionBoard?: boolean;
  missionsActive?: boolean;
  onOpenMissions?: () => void;
  /** Routines setup A (0.9.9): opens Space settings › Routines. Without it the row isn't drawn. */
  onOpenRoutines?: () => void;
  /** Dev mode (0.7): the Dev desk row's page is open, and how to open it. */
  devDeskActive?: boolean;
  onOpenDevDesk?: () => void;
  /** Dev mode Connect (flow F5): opens Loose ends on the GitHub form; after connecting the app returns to the Dev desk. */
  onConnectDevGitHub?: () => void;
  /** First-screen A (0.9.8 and 0.9.9): "Dev mode and Approvals moved to this space's settings." for past users. */
  movedPointer?: boolean;
  onOpenMoved?: () => void;
  onHideMoved?: () => void;
  /** Idea jar (0.7): ready takes for the violet badge, whether its page is open, how to open it, and the quick-add. Hidden on phone, which has the Jar tab. */
  ideaJarCount?: number;
  ideaJarActive?: boolean;
  onOpenIdeaJar?: () => void;
  onDropIdea?: (text: string) => Promise<unknown>;
  /** Opens Space settings → Accounts from the footer. */
  onOpenAccounts?: () => void;
  /** Opens Accounts at "Sign in with Claude", when Claude being signed out is why nothing runs. */
  onSignInClaude?: () => void;
  /** Changes when Space settings closes, so the footer reads account status again. */
  enginesRefreshKey?: number;
  /** The space's engine checks when the app already loads them; the sidebar loads its own otherwise. */
  engines?: EnginesApi;
}

/** A bot's own approvals setting differs from its space's: the only time its row says anything about it. */
export const approvalsDiffers = (k: { approvals_effective?: string | null }, spaceApprovals: string | undefined): boolean =>
  !!k.approvals_effective && !!spaceApprovals && k.approvals_effective !== spaceApprovals;

// A bot's name wraps only between words or after a hyphen, on up to two lines, never inside a word
// ("Researche / r"); a word too long for its line ends in "…". Used by the sidebar row and the
// Add member picker. Prose, results and errors keep [overflow-wrap:anywhere] for long URLs.
const NAME_WRAP = 'line-clamp-2 [overflow-wrap:normal] [word-break:normal] text-ellipsis';

const navRow = (active: boolean) =>
  `w-full flex items-center gap-2.5 px-2.5 py-1.5 rounded-xl text-body font-medium transition-colors text-left focus-visible:outline focus-visible:outline-2 focus-visible:outline-sky-400 ${
    active ? 'bg-accent-soft text-zinc-50 shadow-card border border-sky-300/25' : 'text-zinc-300 hover:text-zinc-50 hover:bg-white/[0.05] border border-transparent'
  }`;

export const memberIdsOf = (t: Team): string[] =>
  Array.isArray(t.member_kwants)
    ? t.member_kwants
    : typeof t.member_kwants === 'string'
    ? (JSON.parse(t.member_kwants || '[]') as string[])
    : [];

/**
 * The bot list: one group per team (its members, lead first), then the bots in no team. A bot in
 * two teams shows under each, because that's where you look for it.
 */
export function botGroups(teams: Team[], kwants: Kwant[]): Array<{ team: Team | null; bots: Kwant[] }> {
  const byId = new Map(kwants.map((k) => [k.id, k]));
  const placed = new Set<string>();
  const groups: Array<{ team: Team | null; bots: Kwant[] }> = teams.map((t) => {
    const ids = memberIdsOf(t);
    const ordered = [...ids.filter((id) => id === t.lead_kwant_id), ...ids.filter((id) => id !== t.lead_kwant_id)];
    const bots = ordered.map((id) => byId.get(id)).filter((k): k is Kwant => !!k);
    for (const k of bots) placed.add(k.id);
    return { team: t, bots };
  });
  groups.push({ team: null, bots: kwants.filter((k) => !placed.has(k.id)) });
  return groups;
}

/** H5: the most rows the desktop's pinned block shows before "and n more in Inbox". */
export const PINNED_MAX = 5;

/** 31c: a real ask is rank 1–2 (a question, an approval, a browser step, a failed or stuck bot). The pinned heading,
 *  the Inbox pill and the phone's Inbox badge all go amber only when there is one. */
export const realAskCount = (rows?: AttentionRow<Kwant>[]): number => (rows ?? []).filter((a) => a.rank <= 2).length;

/** 31c: the Inbox count pill: amber when something really needs you, else a calm neutral count. */
export const inboxPillClass = (ask: boolean) => (ask ? 'bg-amber-500 text-zinc-950' : 'bg-white/10 text-zinc-200');

/**
 * H5 desktop: ranks 1–4 pinned above Channels. Gone when empty. 30z: the heading says "Needs you · n" (14c: the phone's
 * name too) in amber with a hand only for real asks (rank 1–2); with only things to review or read it is a calm
 * "New · n" with the Inbox icon. The words are announced politely, at most once per 10 s, from a live region holding
 * only the heading's words.
 */
const NeedsAttention: React.FC<{ rows: AttentionRow<Kwant>[]; row: (a: AttentionRow<Kwant>) => React.ReactNode; onOpenInbox?: () => void }> = ({ rows, row, onOpenInbox }) => {
  const n = rows.length;
  const asks = realAskCount(rows);
  const label = asks > 0 ? NEEDS_YOU_LABEL : 'New';
  const count = asks > 0 ? asks : n;
  const words = `${label}, ${count}`;
  const [said, setSaid] = useState(words);
  const lastSaid = React.useRef(0);
  useEffect(() => {
    if (words === said) return;
    const wait = Math.max(0, lastSaid.current + 10_000 - Date.now());
    const t = setTimeout(() => {
      lastSaid.current = Date.now();
      setSaid(words);
    }, wait);
    return () => clearTimeout(t);
  }, [words, said]);
  const more = n - PINNED_MAX;
  return (
    <section data-needs-attention aria-labelledby="needs-attention-head" className="rounded-xl border border-hairline bg-white/[0.015] my-1.5 pb-1.5">
      <h2
        id="needs-attention-head"
        data-needs-attention-head={asks > 0 ? 'asks' : 'new'}
        className={`px-2 pt-1.5 pb-1 flex items-center gap-1.5 text-[11px] font-semibold uppercase tracking-[0.06em] ${asks > 0 ? 'text-amber-300' : 'text-zinc-400'}`}
        aria-label={words}
      >
        {asks > 0 ? <Hand size={12} aria-hidden="true" /> : <Inbox size={12} aria-hidden="true" />}
        <span aria-hidden="true">{label} · {count}</span>
      </h2>
      <span className="sr-only" aria-live="polite" data-needs-attention-live>
        {said !== words || lastSaid.current ? said : ''}
      </span>
      <div className="space-y-0.5 px-0.5">
        {rows.slice(0, PINNED_MAX).map((a) => (
          <div key={a.kwant.id} className="kw-pin-enter">
            {row(a)}
          </div>
        ))}
      </div>
      {more > 0 && (
        <button
          type="button"
          data-needs-attention-more
          onClick={onOpenInbox}
          disabled={!onOpenInbox}
          className="ml-2 mt-0.5 px-1 rounded text-[12px] text-sky-300 hover:underline focus-visible:outline focus-visible:outline-2 focus-visible:outline-sky-400"
        >
          and {more} more in Inbox
        </button>
      )}
    </section>
  );
};

export const Sidebar: React.FC<SidebarProps> = ({
  space,
  spaces,
  onSwitchSpace,
  onCreateSpace,
  onOpenSpaceSettings,
  onOpenPhoneApp,
  onOpenArchived,
  onOpenSearch,
  section = 'all',
  phone = false,
  kwants,
  channels,
  teams = [],
  selectedId,
  isChannel,
  selectedTeamId,
  busyKwants = {},
  busyTitles,
  doneKwants = {},
  botStatus = {},
  needsYou = {},
  browsingOwners = {},
  browsingBots = {},
  onWatchBrowser,
  browserNeedsYou,
  needsYouRows = [],
  attention,
  onOpenReview,
  onOpenNeedsYou,
  onSelectKwant,
  onSelectChannel,
  onSelectTeam,
  onManageTeam,
  onUpdateTeamMembers,
  onEditKwant,
  onSummonKwant,
  onCreateTeam,
  onCreateChannel,
  inboxCount = 0,
  inboxActive = false,
  onChat = true,
  onOpenInbox,
  parallelActive = false,
  onOpenParallel,
  missionBoard = false,
  missionsActive = false,
  devDeskActive = false,
  onOpenDevDesk,
  movedPointer = false,
  onOpenMoved,
  onHideMoved,
  onOpenMissions,
  onOpenRoutines,
  ideaJarCount = 0,
  ideaJarActive = false,
  onOpenIdeaJar,
  onDropIdea,
  onOpenAccounts,
  onSignInClaude,
  enginesRefreshKey = 0,
  engines: sharedEngines,
}) => {
  const ownEngines = useEngines(!sharedEngines);
  const engines = sharedEngines ?? ownEngines;
  const { reload: reloadEngines } = engines;
  useEffect(() => {
    if (enginesRefreshKey) reloadEngines();
  }, [enginesRefreshKey, reloadEngines]);
  const ready = engines.engines?.filter(isUsable) ?? null;
  const readyCount = ready?.length ?? null;
  const runsOn = ready?.map((e) => e.uses?.label ?? e.name) ?? [];
  // Nothing can run and Claude only needs signing in: the footer goes straight to that.
  const offerClaude = readyCount === 0 && !!onSignInClaude && claudeNeedsSignIn(engines.engines);
  const canRun = canRunNow(engines.engines);
  const [collapsedTeams, setCollapsedTeams] = useState<Set<string>>(new Set());
  const [addingTo, setAddingTo] = useState<string | null>(null);
  const [switcherOpen, setSwitcherOpen] = useState(false);
  /** Phones: the member whose actions sheet is open, and whether "Remove" is waiting for a confirm. */
  const [memberMenu, setMemberMenu] = useState<{ team?: Team; kwant: Kwant; confirmRemove?: boolean } | null>(null);
  // Cancel, Escape or Remove: focus goes back to the member's ✕ (or ⋯ on phones), or to the bot's
  // row when it has left the team.
  const asideRef = React.useRef<HTMLElement>(null);
  const memberReturn = React.useRef<{ team: string; kwant: string } | null>(null);
  const closeMemberMenu = () => {
    if (memberMenu) memberReturn.current = { team: memberMenu.team?.id ?? 'none', kwant: memberMenu.kwant.id };
    setMemberMenu(null);
  };
  React.useLayoutEffect(() => {
    const r = memberReturn.current;
    if (memberMenu || !r) return;
    memberReturn.current = null;
    const box = asideRef.current;
    (box?.querySelector<HTMLElement>(`[data-member-opener="${r.team}:${r.kwant}"]`) ?? box?.querySelector<HTMLElement>(`[data-bot-row="${r.kwant}"]`))?.focus();
  }, [memberMenu]);
  const closeSwitcher = useCallback(() => setSwitcherOpen(false), []);
  const showChats = section !== 'bots';
  const showBots = section !== 'chats';
  // The phone's Chats tab is a launcher for everything you talk to (B1): who needs you, who is
  // working, channels, team chats, then every other bot's direct chat. Nothing in it stays lit.
  const chatsTab = phone && section === 'chats';
  const needsRows = chatsTab ? needsYouRows.filter((r) => kwants.some((k) => k.id === r.kwant_id)) : [];
  const needsIds = new Set(needsRows.map((r) => r.kwant_id));
  const { byBot: budgetsByBot } = useBudgets();
  const busyNow = chatsTab ? workingIds(kwants, busyKwants, needsIds) : [];
  // A bot whose turn just ended stays in Working for its globe's fade-out, instead of jumping to Direct
  // chats (a new row, with no globe) in the same frame (QA F2 on phone)
  const lingering = useLingeringIds(busyNow.map((k) => k.id));
  const workingBots = chatsTab && lingering.size ? workingIds(kwants, { ...Object.fromEntries([...lingering].map((id) => [id, 'done'])), ...busyKwants }, needsIds) : busyNow;
  const directBotsOld = chatsTab ? directIds(kwants, new Set([...needsIds, ...workingBots.map((k) => k.id)])) : [];
  // H5: with the attention order, the Chats tab has a section per rank 1–6 and bots move into them (no
  // copies); a bot that finished and is read goes back to Direct chats, so no lingering under Working.
  const attOn = !!attention;
  const byRank = (r: number) => (chatsTab && attention ? attention.filter((a) => a.rank === r) : []);
  const phoneSections = chatsTab && attention
    ? ([
        { key: 'needs', head: NEEDS_YOU_LABEL, tone: '!text-[11px] !leading-4 !tracking-[0.06em] !text-warn', rows: byRank(1) },
        { key: 'stuck', head: 'Stuck', tone: '!text-rose-300', rows: byRank(2) },
        { key: 'review', head: 'To review', tone: '!text-sky-300', rows: byRank(3) },
        { key: 'finished', head: 'Finished', tone: '', rows: byRank(4) },
        { key: 'working', head: 'Working', tone: '', rows: byRank(5) },
        { key: 'waiting', head: 'Waiting', tone: '', rows: byRank(6) },
      ] as const)
    : [];
  const directBots = chatsTab && attention ? directIds(kwants, new Set(attention.filter((a) => a.rank < 7).map((a) => a.kwant.id))) : directBotsOld;
  // Desktop: ranks 1–4 (0.9 V1 adds To review), pinned above Channels (copies: the team list below keeps every bot in place).
  const inboxAsk = realAskCount(attention) > 0;
  const pinned = !phone && attention ? attention.filter((a) => a.rank <= PINNED_RANK) : [];
  const rankOf = new Map((attention ?? []).map((a) => [a.kwant.id, a.rank]));

  const toggleTeam = (id: string) =>
    setCollapsedTeams((prev) => {
      const next = new Set(prev);
      if (next.has(id)) next.delete(id);
      else next.add(id);
      return next;
    });

  // Channels to display: hide auto-created team channels to avoid duplication with Teams section
  const displayChannels = channels.filter(
    (c) => !teams.some((t) => teamChannelOf(channels, t)?.id === c.id)
  );

  const isTeamActive = (team: Team) => {
    if (phone || !onChat) return false;
    if (selectedTeamId !== undefined && selectedTeamId !== null) {
      return selectedTeamId === team.id;
    }
    return (
      (isChannel && selectedId === team.id) ||
      (!!selectedId && teamChannelOf(channels, team)?.id === selectedId)
    );
  };

  const isChannelActive = (c: Channel) => {
    if (phone || !onChat || !isChannel) return false;
    if (selectedTeamId && teams.some((t) => t.id === selectedTeamId)) {
      return false;
    }
    return selectedId === c.id;
  };

  // Dev mode and Approvals live in Space settings › General (first-screen A); the header names them.
  const dev = useDevMode(space.id);
  const devOn = !!dev?.on;
  // Phones: section titles stay pinned while their list scrolls, so they are never cut off
  const sectionHead = `px-2 py-1 min-w-0 flex items-center justify-between gap-2 text-2xs font-semibold text-zinc-400 uppercase tracking-wider${
    phone ? ' kw-sticky-head sticky top-0 z-10' : ''
  }`;
  // Phones: "+ Team" / "+ Bot" as plain text buttons, at least 44x44 to tap
  const phoneAdd = (word: string, label: string, onClick: () => void) => (
    <button
      type="button"
      onClick={onClick}
      aria-label={label}
      title={label}
      className="-mr-1 min-h-11 px-3 shrink-0 whitespace-nowrap inline-flex items-center gap-1 rounded-full text-meta font-medium normal-case tracking-normal text-sky-400 hover:text-sky-300 hover:bg-sky-500/10 active:bg-sky-500/15 transition-colors focus-visible:outline focus-visible:outline-2 focus-visible:outline-sky-500"
    >
      <Plus size={14} aria-hidden="true" />
      <span>{word}</span>
    </button>
  );

  // One row per bot: avatar with its status dot, name, what it is doing, and its actions. In a
  // team the lead is marked and others can be removed; on phones those sit in a sheet.
  // On the phone's Chats tab a row is a plain launcher (`chat`): taller, no actions, and a
  // Needs-you row says in amber what the bot is waiting for and opens the chat on that card.
  // 22i: the pinned row's tooltip (design.md §4): 300 ms after hover, at once on keyboard focus; to the
  // right of the row, or below it when the window has less than 328 px there.
  const [rowTip, setRowTip] = useState<{ id: string; top: number; left: number } | null>(null);
  const rowTipTimer = useRef<number | null>(null);
  const showRowTip = (id: string, el: HTMLElement, delay: number) => {
    if (rowTipTimer.current) window.clearTimeout(rowTipTimer.current);
    const place = () => {
      const r = el.getBoundingClientRect();
      const below = window.innerWidth - r.right < 328;
      setRowTip({ id, top: below ? r.bottom + 8 : r.top, left: below ? r.left : r.right + 8 });
    };
    if (delay) rowTipTimer.current = window.setTimeout(place, delay);
    else place();
  };
  const hideRowTip = () => {
    if (rowTipTimer.current) window.clearTimeout(rowTipTimer.current);
    rowTipTimer.current = null;
    setRowTip(null);
  };
  useEffect(() => () => {
    if (rowTipTimer.current) window.clearTimeout(rowTipTimer.current);
  }, []);
  const botRow = (k: Kwant, team?: Team, chat?: { needs?: NeedsYouRow; att?: AttentionRow<Kwant> }, pin?: { att: AttentionRow<Kwant>; alsoIn?: string }) => {
    // Phones never light a row: the list is a launcher, and coming back to it shows no stale selection
    const active = !phone && onChat && !isChannel && selectedId === k.id;
    const att = pin?.att ?? chat?.att;
    const asked = chat?.needs ?? (att?.rank === 1 ? att.needs : undefined);
    // H5 ranks 2–4 say what broke, what waits for review (0.9 V1) or what finished, in rose, lavender (info) or
    // zinc-200; a review that needs you says so in amber. Review's dot is a hollow lavender ring, so it never
    // reads as working's filled, pulsing lavender dot.
    const attLine = att && (att.rank === 2 || att.rank === 3 || att.rank === 4 || (att.rank === 1 && att.review)) ? att.line : undefined;
    const attTone = !att ? '' : att.rank === 1 ? 'text-warn' : att.rank === 2 ? 'text-rose-300' : att.rank === 3 ? 'text-info' : 'text-zinc-200';
    const attDot = !att ? '' : att.rank === 1 ? 'kw-status-warn' : att.rank === 2 ? 'bg-rose-400' : att.rank === 3 ? 'border-[1.5px] border-sky-300 bg-transparent' : 'kw-status-ok';
    // Pinned to an engine that can't run, with failover off: its turn would fail, so no green dot
    const pinnedTip = pinnedCantRun(k, engines.engines);
    const status = asked ? 'needs_you' : statusOf(k.id, botStatus, pinnedTip ? false : canRun);
    const isLead = !!team && k.id === team.lead_kwant_id;
    const canRemove = !!team && !isLead && !!onUpdateTeamMembers;
    const hasRowActions = !!onEditKwant || canRemove;
    // Desktop: the one-line status under the name makes room for Edit and Remove on hover or focus.
    // It never wraps, so the room costs a few letters of the status, never the row's height.
    const actionRoom = !phone && hasRowActions ? ' group-hover/fleet:pr-9 group-focus-within/fleet:pr-9' : '';
    // An open browser step: the status line says so in words while it is open (the name line only has the hand)
    const needs = needsYou[k.id] ?? 0;
    const needsTip = `${k.name} needs you in the browser`;
    // 0.9.5: a bot paused at its monthly budget says so where its status goes; at 80% a small amber meter
    const budget = budgetsByBot[k.id];
    const budgetPaused = budget?.state === 'paused';
    const warn = needs > 0 || status === 'needs_you' || budgetPaused;
    const busyLine = asked
      ? asked.line
      : attLine
        ? attLine
      : needs > 0 && !busyKwants[k.id]?.startsWith('Needs you in the browser')
        ? 'Needs you in the browser'
        : budgetPaused && !busyKwants[k.id]
          ? PAUSED_ROW_LINE
          : busyKwants[k.id];
    // A teammate who finished its part of a team turn that now waits on the person (UX audit #2)
    const doneLine = !busyLine && !warn ? doneKwants[k.id] : undefined;
    // 22i: a pinned row's line is cut to one line; hover and focus give the whole ask (the stopped
    // reply's full sentence, the approval's question), as a tooltip and as the row's name.
    // 22i: a pinned row's line is cut to one line; hover and focus show the whole ask in a small tooltip
    // (design.md §4): "Quant · Stopped · 18:44" over the full line. A native title never shows on focus.
    const rowItem = asked?.item ?? att?.item;
    const fullLine = pin && busyLine ? (rowItem && isAsk(rowItem) ? (approvalLine(rowItem) ?? askLine(rowItem)) || busyLine : busyLine) : undefined;
    const tipKind = !att
      ? undefined
      : att.rank === 3
        ? 'To review'
        : att.rank === 4
          ? 'Finished'
          : rowItem
            ? askKind(rowItem, askAction(rowItem))
            : att.rank === 2
              ? 'Failed'
              : needs > 0
                ? 'Browser step'
                : undefined;
    const tipHead = fullLine ? [k.name, tipKind, rowItem ? localClock(rowItem.timestamp) : null].filter(Boolean).join(' · ') : undefined;
    const tipId = fullLine ? `pin-tip-${k.id}` : undefined;
    const tipOpen = !!tipId && rowTip?.id === k.id;
    // A stuck or finished row opens where that happened (its thread or chat), like its Inbox card
    // A review row opens its item in the Inbox
    const open = asked && onOpenNeedsYou
      ? () => onOpenNeedsYou(asked)
      : att?.review && onOpenReview
        ? () => onOpenReview(att.review!)
      : attLine && att?.item && onOpenNeedsYou
        ? () => onOpenNeedsYou({ kwant_id: k.id, line: attLine, item: att.item })
        : () => onSelectKwant(k);
    // The globe steps aside while the row says it needs you in the browser (that line leads there), and
    // while the browser it shows holds an open step: one browser per chat, so every row on it hides (Mira 3)
    const botWatch = browsingBots[k.id] ?? browsingOwners[k.id];
    const botBrowsing = onWatchBrowser && !warn && !asked && botWatch && !browserNeedsYou?.has(botWatch.owner) ? botWatch : undefined;
    return (
      <div
        key={`${pin ? 'pin' : chat ? 'chat' : team?.id ?? 'none'}:${k.id}`}
        role="button"
        tabIndex={0}
        onClick={open}
        // Only the row's own Enter: Enter on its ✕ or Edit is for that button, not for opening the chat.
        onKeyDown={(e) => {
          if (tipId && e.key === 'Escape') hideRowTip();
          if (e.key === 'Enter' && e.target === e.currentTarget) open?.();
        }}
        onMouseEnter={tipId ? (e) => showRowTip(k.id, e.currentTarget, 300) : undefined}
        onMouseLeave={tipId ? hideRowTip : undefined}
        onFocus={tipId ? (e) => e.target === e.currentTarget && e.currentTarget.matches?.(':focus-visible') && showRowTip(k.id, e.currentTarget, 0) : undefined}
        onBlur={tipId ? hideRowTip : undefined}
        // The pinned copy is not the bot's row: focus returns to the one in its team
        data-bot-row={pin ? undefined : k.id}
        data-pin-row={pin ? k.id : undefined}
        // The pinned copy lights with it but isn't "here"; the bot's own row is
        aria-current={active && !pin ? 'page' : undefined}
        data-rank={att?.rank}
        aria-describedby={[tipId, pin?.alsoIn ? `pin-also-${k.id}` : null].filter(Boolean).join(' ') || undefined}
        data-chat-row={chat ? (asked ? 'needs' : 'bot') : undefined}
        className={`group/fleet relative w-full flex items-center gap-2.5 px-2 ${chat ? 'min-h-14 py-2' : 'py-1.5'} rounded-xl text-left cursor-pointer transition-colors focus-visible:outline focus-visible:outline-2 focus-visible:outline-sky-400 ${
          active ? 'bg-accent-soft text-zinc-50 shadow-card border border-sky-300/25' : 'text-zinc-300 hover:text-zinc-50 hover:bg-white/[0.05] border border-transparent'
        }`}
      >
        <span className="shrink-0" title={status === 'cant_run' ? pinnedTip ?? "Can't run yet: no account works here" : statusText(k.name, status)}>
          <ModernAvatar role={k.avatar_sprite} name={k.name} size={chat ? 32 : 28} status={status} kwantId={k.id} />
        </span>
        <div className="flex-1 min-w-0">
          {/* The name line never changes on hover: padding it here made the name wrap and the row jump */}
          <div data-bot-name-line className="flex items-start gap-1">
            {/* The full name, on up to two lines (QA a), wrapping only between words or after a hyphen:
                never inside a word ("Researche / r"). A word too long for the line ends in "…"; the
                whole name and what it runs on are in the tooltip */}
            <BotName kwant={k} className={`min-w-0 text-body font-semibold leading-5 text-zinc-100 ${NAME_WRAP}`} />
            {isLead && (
              <span className="shrink-0 flex items-center gap-0.5 h-5 text-2xs uppercase tracking-wide text-amber-300" title={`Leads @${team!.name}`}>
                <Crown size={10} aria-hidden="true" />
                lead
              </span>
            )}
            {/* Compact on the name line (the hand, and a count past one), so it never squeezes the name;
                the words "Needs you in the browser" are on the status line below */}
            {needs > 0 && !asked && (
              <span
                data-bot-needs-you={needs}
                role="img"
                // Mira's compact pill: 20 px high, hand 14 px, count 12 px/600 in tabular figures; flat amber, never holographic
                className="shrink-0 inline-flex items-center gap-1 h-5 px-1.5 rounded-full text-[12px] font-semibold tabular-nums leading-none text-[#fbbf24] bg-[rgba(251,191,36,0.12)] border border-[rgba(251,191,36,0.35)]"
                title={needsTip}
                aria-label={`Needs you (${needs})`}
              >
                <Hand size={14} aria-hidden="true" />
                <span aria-hidden="true">{needs}</span>
              </span>
            )}
            {/* The space header already says when approvals are off for everyone: a bot only gets its own mark when it differs */}
            {approvalsDiffers(k, space.approvals) &&
              (approvalsOff(k) ? (
                <ApprovalsOffBadge compact title={`${k.name} acts without asking. Payments still ask you.`} />
              ) : (
                <span className="shrink-0 inline-flex items-center text-teal-300" title={`${k.name} still asks before acting, unlike the rest of ${space.name}.`}>
                  <ShieldCheck size={12} aria-hidden="true" />
                  <span className="sr-only">Asks before acting</span>
                </span>
              ))}
            {/* W1: this bot's current work is in a browser; the click opens the chat that browser belongs to.
                Last on the name line and pushed right, so every row's globe lines up in one column */}
            {budget?.state === 'warn' && <BudgetMeter budget={budget!} size="row" className="ml-0.5 self-center" />}
            {onWatchBrowser && <GlobeSlot browsing={botBrowsing} name={k.name} phone={phone} className="ml-auto mt-1" onWatch={onWatchBrowser} />}
            {/* Edit and Remove sit at the right end of the one-line status below the name, which makes room
                for them on hover: the name keeps its width and the row its height */}
            {!phone && (
              <span data-bot-row-actions className="absolute right-1.5 bottom-2 flex items-center gap-0.5 rounded-md bg-zinc-800/95 opacity-0 group-hover/fleet:opacity-100 group-focus-within/fleet:opacity-100 empty:hidden">
                {onEditKwant && (
                  <button
                    type="button"
                    onClick={(e) => {
                      e.stopPropagation();
                      onEditKwant(k);
                    }}
                    className="opacity-0 group-hover/fleet:opacity-100 group-focus-within/fleet:opacity-100 focus-visible:opacity-100 focus-visible:outline focus-visible:outline-2 focus-visible:outline-sky-400 p-0.5 rounded text-zinc-500 hover:text-sky-300 hover:bg-sky-500/10 transition-all"
                    title={`Edit or delete ${k.name}`}
                    aria-label={`Edit or delete ${k.name}`}
                  >
                    <Pencil size={11} />
                  </button>
                )}
                {canRemove && (
                  <button
                    type="button"
                    data-remove-member={k.id}
                    data-member-opener={`${team!.id}:${k.id}`}
                    onClick={(e) => {
                      e.stopPropagation();
                      // Asks first, like the phone sheet: a stray click can't take a bot out of its team.
                      setMemberMenu({ team: team!, kwant: k, confirmRemove: true });
                    }}
                    className="opacity-0 group-hover/fleet:opacity-100 group-focus-within/fleet:opacity-100 focus-visible:opacity-100 focus-visible:outline focus-visible:outline-2 focus-visible:outline-sky-400 p-0.5 rounded text-zinc-500 hover:text-rose-300 hover:bg-rose-500/10 transition-all"
                    title={`Remove ${k.name} from @${team!.name}`}
                    aria-label={`Remove ${k.name} from @${team!.name}`}
                  >
                    <X size={11} />
                  </button>
                )}
              </span>
            )}
          </div>
          {tipId && (
            <div
              role="tooltip"
              id={tipId}
              data-pin-tip={k.id}
              hidden={!tipOpen}
              style={tipOpen ? { top: rowTip!.top, left: rowTip!.left } : undefined}
              className="fixed z-50 max-w-[320px] rounded-lg border border-hairline-strong bg-surface-overlay px-2.5 py-2 text-meta shadow-[0_8px_24px_rgb(0_0_0/0.5)] pointer-events-none"
            >
              <span className="block text-zinc-400">{tipHead}</span>
              <span className="block text-zinc-100 break-words whitespace-normal">{fullLine}</span>
            </div>
          )}
          {pin?.alsoIn && (
            <span id={`pin-also-${k.id}`} className="sr-only">
              Also listed in @{pin.alsoIn}
            </span>
          )}
          {busyLine ? (
            <p
              className={`flex items-center gap-1.5 text-meta truncate${actionRoom} ${
                warn || asked ? 'text-warn' : attLine ? attTone : status === 'queued' ? 'text-zinc-300' : 'text-[#b8adff]'
              }`}
              title={fullLine ? undefined : busyLine === busyKwants[k.id] ? busyTitles?.[k.id] ?? busyLine : busyLine}
              data-bot-busy
            >
              <span
                className={`kw-status-dot !h-1.5 !w-1.5 ${
                  warn || asked ? 'kw-status-warn' : attLine ? attDot : status === 'queued' ? 'kw-status-idle' : 'bg-[#9d8fff] kw-pulse'
                }`}
                aria-hidden="true"
              />
              <span className={chat && (asked || attLine) ? 'line-clamp-2 whitespace-normal' : 'truncate'}>{busyLine}</span>
            </p>
          ) : doneLine ? (
            <p className={`flex items-center gap-1.5 text-meta text-zinc-400 truncate${actionRoom}`} title={doneLine} data-bot-done="">
              <span className="kw-status-dot !h-1.5 !w-1.5 kw-status-ok" aria-hidden="true" />
              <span className="truncate">{doneLine}</span>
            </p>
          ) : (
            // Keeps its one line even with no title, so Edit and Remove sit under the name, never on it (#290 N1)
            <p className={`text-meta text-zinc-400 truncate${actionRoom}${!phone && hasRowActions ? ' min-h-[1.125rem]' : ''}`}>{k.title}</p>
          )}
        </div>
        {/* Phones' Bots tab: the same ⋯ at every row's end (B4), 44×44; it opens the bot's actions,
            with Edit, and Remove when it is in a team. The Chats tab's rows have no actions. */}
        {phone && !chat && (onEditKwant || (team && onUpdateTeamMembers && !isLead)) && (
          <button
            type="button"
            onClick={(e) => {
              e.stopPropagation();
              playSfx('click');
              setMemberMenu({ team, kwant: k });
            }}
            data-member-opener={`${team?.id ?? 'none'}:${k.id}`}
            data-row-more
            aria-haspopup="dialog"
            aria-label={`More for ${k.name}`}
            title={`More for ${k.name}`}
            className="-mr-1.5 -my-1 w-11 h-11 shrink-0 inline-flex items-center justify-center rounded-lg text-[#a39dcc] hover:text-zinc-100 hover:bg-white/[0.06] active:bg-white/[0.08] transition-colors focus-visible:outline focus-visible:outline-2 focus-visible:outline-sky-400"
          >
            <MoreHorizontal size={16} aria-hidden="true" />
          </button>
        )}
      </div>
    );
  };

  // H5: a team header's rollup, one mini dot + count for each of ranks 1–4; else the working count.
  const teamRollup = (t: Team) => {
    if (!attention) return null;
    const c = { needs: 0, stuck: 0, review: 0, finished: 0, working: 0 };
    for (const id of new Set(memberIdsOf(t))) {
      const r = rankOf.get(id);
      if (r === 1) c.needs++;
      else if (r === 2) c.stuck++;
      else if (r === 3) c.review++;
      else if (r === 4) c.finished++;
      else if (r === 5) c.working++;
    }
    const dots: Array<[number, string]> = c.needs || c.stuck || c.review || c.finished
      ? ([[c.needs, 'bg-amber-400'], [c.stuck, 'bg-rose-400'], [c.review, 'border-[1.5px] border-sky-300 bg-transparent'], [c.finished, 'kw-status-ok']] as Array<[number, string]>).filter(([n]) => n > 0)
      : c.working
        ? [[c.working, 'bg-[#9d8fff]']]
        : [];
    if (!dots.length) return null;
    return (
      <span data-team-rollup={t.id} role="img" aria-label={rollupLabel(c)} title={rollupLabel(c)} className="flex items-center gap-2 mr-1">
        {dots.map(([n, dot]) => (
          <span key={dot} className="flex items-center gap-1" aria-hidden="true">
            <span className={`h-1.5 w-1.5 rounded-full ${dot}`} />
            <span className="text-[11px] tabular-nums text-zinc-400">{n}</span>
          </span>
        ))}
      </span>
    );
  };

  // A team's chat row: opens the team chat; collapses its members, opens its settings.
  const teamRow = (t: Team, withMembers: boolean) => {
    const active = isTeamActive(t);
    const count = memberIdsOf(t).length;
    const expanded = withMembers && !collapsedTeams.has(t.id);
    return (
      <div
        role="button"
        tabIndex={0}
        onClick={() => onSelectTeam(t)}
        onKeyDown={(e) => e.key === 'Enter' && e.target === e.currentTarget && onSelectTeam(t)}
        data-team-row={t.id}
        aria-current={active ? 'page' : undefined}
        title={`Open the @${teamNamed(t.name)} chat`}
        className={`w-full flex items-center justify-between px-1.5 py-1.5 rounded-lg text-body font-medium transition-colors text-left group cursor-pointer ${
          active ? 'bg-accent-soft text-zinc-50 shadow-card border border-sky-300/25' : 'text-zinc-300 hover:text-zinc-50 hover:bg-white/[0.05] border border-transparent'
        }`}
      >
        <div className="flex items-center gap-1.5 min-w-0">
          {withMembers && (
            <button
              type="button"
              onClick={(e) => {
                e.stopPropagation();
                toggleTeam(t.id);
              }}
              aria-expanded={expanded}
              aria-label={expanded ? `Hide @${t.name} members` : `Show @${t.name} members`}
              className="p-0.5 rounded text-zinc-500 hover:text-zinc-200 hover:bg-zinc-700/60"
            >
              <ChevronDown size={12} className={`transition-transform duration-150 ${expanded ? '' : '-rotate-90'}`} />
            </button>
          )}
          <Users size={14} className={active ? 'text-sky-400' : 'text-zinc-500 group-hover:text-zinc-400'} aria-hidden="true" />
          <span className="truncate">@{t.name}</span>
        </div>
        <div className="flex items-center gap-1 flex-shrink-0">
          {withMembers && !phone && teamRollup(t)}
          {(!expanded || !withMembers) && (
            <span className="text-2xs px-1.5 py-0.5 rounded-full tabular-nums bg-white/[0.05] text-zinc-300 border border-hairline" title={`${count} ${count === 1 ? 'member' : 'members'}`}>
              {count}
            </span>
          )}
          {onManageTeam && (
            <button
              type="button"
              onClick={(e) => {
                e.stopPropagation();
                playSfx('click');
                onManageTeam(t);
              }}
              className="opacity-0 group-hover:opacity-100 focus-visible:opacity-100 max-md:opacity-100 p-1 hover:text-zinc-100 hover:bg-zinc-700/60 rounded transition-all text-zinc-400"
              title={`Team settings for @${t.name}`}
              aria-label={`Team settings for @${t.name}`}
            >
              <Settings2 size={12} />
            </button>
          )}
          {/* Last in the row, like the bot rows' globe, so every globe lines up in one column (QA N1) */}
          {onWatchBrowser && (
            <GlobeSlot browsing={!browserNeedsYou?.has(t.id) ? browsingOwners[t.id] : undefined} name={`@${t.name}`} phone={phone} className="mr-px" onWatch={onWatchBrowser} />
          )}
        </div>
      </div>
    );
  };

  const addMember = (t: Team) => {
    const memberIds = memberIdsOf(t);
    const candidates = kwants.filter((k) => !memberIds.includes(k.id));
    if (!onUpdateTeamMembers) return null;
    if (addingTo !== t.id) {
      return (
        <button
          type="button"
          onClick={() => {
            setAddingTo(t.id);
            playSfx('click');
          }}
          className="w-full flex items-center gap-2 px-2 py-1 rounded-md text-[11px] text-sky-400/90 hover:text-sky-300 hover:bg-sky-500/10 transition-colors"
        >
          <span className="w-[18px] h-[18px] flex items-center justify-center rounded border border-dashed border-sky-500/40">
            <Plus size={11} />
          </span>
          <span>Add member</span>
        </button>
      );
    }
    return (
      <div className="rounded-lg border border-zinc-700/60 bg-zinc-900/90 p-1 space-y-0.5">
        <div className="flex items-center justify-between px-1.5 pt-0.5 pb-1 text-[10px] uppercase tracking-wider text-zinc-500">
          <span>Add to @{t.name}</span>
          <button type="button" onClick={() => setAddingTo(null)} className="p-0.5 rounded hover:text-zinc-200 hover:bg-zinc-700/60" title="Cancel" aria-label="Cancel">
            <X size={11} />
          </button>
        </div>
        {candidates.map((k) => (
          <button
            key={k.id}
            type="button"
            onClick={() => {
              onUpdateTeamMembers(t, [...memberIds, k.id]);
              setAddingTo(null);
            }}
            className="w-full flex items-center gap-2 px-1.5 py-1 rounded-md text-[11px] text-zinc-300 hover:bg-sky-500/10 hover:text-zinc-100 text-left"
          >
            <ModernAvatar role={k.avatar_sprite} name={k.name} size={18} kwantId={k.id} />
            <span data-pick-name className={`flex-1 min-w-0 ${NAME_WRAP}`} title={k.name}>{k.name}</span>
            <span className="shrink-0 text-[10px] text-zinc-500 truncate max-w-[80px]">{k.title}</span>
          </button>
        ))}
        {candidates.length === 0 && <div className="px-1.5 py-1 text-[11px] text-zinc-500">Every bot is already in this team.</div>}
        <button
          type="button"
          onClick={() => {
            setAddingTo(null);
            onSummonKwant(t);
          }}
          className="w-full flex items-center gap-2 px-1.5 py-1 rounded-md text-[11px] text-sky-400 hover:bg-sky-500/10 border-t border-zinc-800 mt-0.5 pt-1.5"
        >
          <UserPlus size={13} />
          <span>Create a new bot…</span>
        </button>
      </div>
    );
  };

  const groups = botGroups(teams, kwants);
  const teamOf = (id: string) => groups.find((g) => g.team && g.bots.some((b) => b.id === id))?.team?.name;
  // "+ Team", "+ Bot", "+ Channel": the + says new and the heading says what kind; the full phrase is the label.
  const headButton = (word: string, label: string, title: string, onClick: () => void) =>
    phone ? (
      phoneAdd(word, label, onClick)
    ) : (
      <button
        type="button"
        onClick={onClick}
        aria-label={label}
        className="shrink-0 whitespace-nowrap flex items-center gap-1 text-2xs font-medium text-sky-300 hover:text-sky-200 hover:bg-sky-500/10 px-2 py-0.5 rounded-full border border-sky-400/25 transition-all normal-case tracking-normal focus-visible:outline focus-visible:outline-2 focus-visible:outline-sky-500"
        title={title}
      >
        <Plus size={11} aria-hidden="true" />
        <span>{word}</span>
      </button>
    );

  return (
    <aside
      ref={asideRef}
      aria-label={`${space.name} chats and bots`}
      data-kw-sidebar
      className={`${phone ? 'w-full flex-1 min-h-0' : 'w-64 h-full border-r border-hairline'} bg-surface-side flex flex-col select-none flex-shrink-0`}
    >
      {/* Which place this window is (This Mac or a server) and the way to the place picker; desktop app only. */}
      <PlaceChipRow phone={phone} />
      {/* The open space. Desktop switches spaces on the rail, so this is its name and settings;
          phones have no rail, so here it is their one switcher. */}
      <div className="relative p-2 border-b border-zinc-800/60">
        {phone ? (
          <div className="flex items-center gap-1">
          <button
            type="button"
            onClick={() => {
              playSfx('click');
              setSwitcherOpen((v) => !v);
            }}
            data-space-toggle
            aria-haspopup="menu"
            aria-expanded={switcherOpen}
            className="w-full flex items-center gap-2.5 px-1.5 py-1.5 rounded-lg text-left hover:bg-zinc-800/60 transition-colors focus-visible:outline focus-visible:outline-2 focus-visible:outline-sky-500"
          >
            <SpaceTile space={space} size={32} dev={false} />
            <span className="flex-1 min-w-0">
              <span className="block text-body font-semibold text-zinc-50 truncate">{space.name}</span>
              <SpaceSubline counts={space.counts} devOn={devOn} approvals={space.approvals} />
            </span>
            <ChevronDown size={14} className={`shrink-0 text-zinc-500 transition-transform ${switcherOpen ? 'rotate-180' : ''}`} aria-hidden="true" />
          </button>
          </div>
        ) : (
          <div className="flex items-center gap-2.5 px-1.5 py-1.5" data-space-header>
            <SpaceTile space={space} size={32} dev={false} />
            <span className="flex-1 min-w-0">
              <h1 className="text-body font-semibold text-zinc-50 truncate">{space.name}</h1>
              <SpaceSubline counts={space.counts} devOn={devOn} approvals={space.approvals} />
            </span>
            <button
              type="button"
              onClick={() => {
                playSfx('click');
                onOpenSpaceSettings();
              }}
              data-space-settings-open
              aria-label={`Settings for ${space.name}`}
              title={`Settings for ${space.name}`}
              className="shrink-0 p-1.5 rounded-lg text-zinc-400 hover:text-zinc-100 hover:bg-zinc-800 transition-colors focus-visible:outline focus-visible:outline-2 focus-visible:outline-sky-500"
            >
              <Settings size={15} aria-hidden="true" />
            </button>
          </div>
        )}
        {movedPointer && onOpenMoved && onHideMoved && <MovedPointer phone={phone} onOpen={onOpenMoved} onHide={onHideMoved} />}
        {phone && switcherOpen && (
          <SpaceSwitcher
            spaces={spaces}
            currentId={space.id}
            onSwitch={onSwitchSpace}
            onCreate={onCreateSpace}
            onOpenSpaceSettings={onOpenSpaceSettings}
            onOpenArchived={onOpenArchived}
            onClose={closeSwitcher}
            sheet={phone}
          />
        )}
      </div>

      <div className="flex-1 overflow-y-auto p-2 space-y-4">
        {showChats && (
        <div className="space-y-0.5" data-utility-rows>
          <button type="button" onClick={onOpenSearch} className={navRow(false)}>
            <Search size={15} className="text-zinc-500" aria-hidden="true" />
            <span className="truncate flex-1">Search {space.name}</span>
            {!phone && <kbd className="shrink-0 rounded border border-hairline-strong px-1 text-2xs font-sans text-zinc-400">{isMac ? '⌘K' : 'Ctrl K'}</kbd>}
          </button>
          {/* Phones have Inbox as a tab */}
          {onOpenInbox && !phone && (
          <button
            type="button"
            onClick={onOpenInbox}
            aria-current={inboxActive ? 'page' : undefined}
            className={navRow(inboxActive)}
          >
            <Inbox size={15} className={inboxActive ? 'text-sky-400' : 'text-zinc-500'} aria-hidden="true" />
            <span className="truncate flex-1">Inbox</span>
            {inboxCount > 0 && (
              <span data-inbox-pill={inboxAsk ? 'ask' : 'calm'} className={`shrink-0 min-w-[18px] px-1.5 py-px rounded-full ${inboxPillClass(inboxAsk)} text-[10px] font-semibold text-center`}>
                {badgeText(inboxCount)}
                <span className="sr-only"> new</span>
              </span>
            )}
          </button>
          )}
          {missionBoard && onOpenMissions && (
          <button type="button" onClick={onOpenMissions} aria-current={missionsActive ? 'page' : undefined} className={navRow(missionsActive)}>
            <KanbanSquare size={15} className={missionsActive ? 'text-sky-400' : 'text-zinc-500'} aria-hidden="true" />
            <span className="truncate flex-1">Mission board</span>
          </button>
          )}
          {/* Routines setup A: no count or badge; Settings covers the sidebar while open, so never aria-current. */}
          {onOpenRoutines && (
          <button type="button" onClick={onOpenRoutines} className={navRow(false)} data-routines-row>
            <CalendarClock size={15} className="text-zinc-500" aria-hidden="true" />
            <span className="truncate flex-1">Routines</span>
          </button>
          )}
          {onOpenIdeaJar && onDropIdea && !phone && (
            <IdeaJarRow active={ideaJarActive} count={ideaJarCount} onOpen={onOpenIdeaJar} onDrop={onDropIdea} className={navRow(ideaJarActive)} />
          )}
          {onOpenParallel && (
            // 14c: "More" held only this one place, so it is a plain row, like Routines.
            <button type="button" onClick={onOpenParallel} aria-current={parallelActive ? 'page' : undefined} className={navRow(parallelActive)} data-parallel-row>
              <Split size={15} className={parallelActive ? 'text-sky-400' : 'text-zinc-500'} aria-hidden="true" />
              <span className="truncate flex-1">Parallel Universe</span>
            </button>
          )}
        </div>
        )}

        {/* H5 phone Chats tab: Needs you, Stuck, Finished, Working, Waiting; an empty section is not drawn */}
        {phoneSections.map(
          (sec) =>
            sec.rows.length > 0 && (
              <section key={sec.key} data-chats-section={sec.key} aria-labelledby={`chats-${sec.key}-head`}>
                <h2 id={`chats-${sec.key}-head`} className={`${sectionHead} ${sec.tone}`}>
                  {sec.head} · {sec.rows.length}
                </h2>
                <div className="mt-1 space-y-0.5">{sec.rows.map((a) => botRow(a.kwant, undefined, { att: a, needs: a.needs }))}</div>
              </section>
            ),
        )}
        {/* Phones' Chats tab: who needs you, then who is working, above the channels */}
        {!attOn && needsRows.length > 0 && (
          <section data-chats-section="needs" aria-labelledby="chats-needs-head">
            <h2 id="chats-needs-head" className={`${sectionHead} !text-[11px] !leading-4 !tracking-[0.06em] !text-warn`}>
              {NEEDS_YOU_LABEL} · {needsRows.length}
            </h2>
            <div className="mt-1 space-y-0.5">
              {needsRows.map((r) => botRow(kwants.find((k) => k.id === r.kwant_id)!, undefined, { needs: r }))}
            </div>
          </section>
        )}
        {!attOn && workingBots.length > 0 && (
          <section data-chats-section="working" aria-labelledby="chats-working-head">
            <h2 id="chats-working-head" className={sectionHead}>Working</h2>
            <div className="mt-1 space-y-0.5">{workingBots.map((k) => botRow(k, undefined, {}))}</div>
          </section>
        )}
        {devOn && onOpenDevDesk && showChats && (
          <div data-dev-desk-pin>
            <DevDeskRow spaceId={space.id} active={devDeskActive} onOpen={onOpenDevDesk} className={navRow(devDeskActive)} />
          </div>
        )}

        {pinned.length > 0 && <NeedsAttention rows={pinned} row={(a) => botRow(a.kwant, undefined, undefined, { att: a, alsoIn: teamOf(a.kwant.id) })} onOpenInbox={onOpenInbox} />}

        {/* Channels Section */}
        {showChats && (
        <div data-chats-section="channels">
          <div className={sectionHead}>
            <span className="whitespace-nowrap shrink-0">Channels</span>
            {onCreateChannel && kwants.length > 0 && headButton('Channel', 'New channel', `New channel in ${space.name}`, onCreateChannel)}
          </div>
          <div className="mt-1 space-y-0.5">
            {displayChannels.map((c) => {
              const active = isChannelActive(c);
              const watching = onWatchBrowser && browsingOwners[c.id] && !browserNeedsYou?.has(c.id) ? browsingOwners[c.id] : null;
              return (
                <ChannelRowGlobe key={c.id} channelId={c.id} watching={watching} phone={phone} name={`#${c.name}`} onWatch={onWatchBrowser}>
                  {(pad) => (
                <button
                  key={c.id}
                  onClick={() => onSelectChannel(c)}
                  aria-current={active ? 'page' : undefined}
                  // The channel's header says it too; no pill on every row
                  title={layaRoutes(c, kwants.length) ? 'The best fit answers here' : undefined}
                  className={`w-full flex items-center gap-2.5 px-2.5 py-1.5 rounded-xl text-body font-medium transition-colors text-left ${
                    active
                      ? 'bg-zinc-800 text-zinc-100 shadow-sm border border-zinc-700/50'
                      : 'text-zinc-400 hover:text-zinc-200 hover:bg-zinc-800/50 border border-transparent'
                  }${pad ? (phone ? ' pr-12' : ' pr-8') : ''}`}
                >
                  <Hash size={15} className={active ? 'text-sky-400' : 'text-zinc-500'} />
                  <span className="truncate flex-1">{c.name}</span>
                </button>
                  )}
                </ChannelRowGlobe>
              );
            })}
            {displayChannels.length === 0 && (
              <div className="px-2.5 py-1.5 text-[11px] text-zinc-500">#general opens once {space.name} has a bot.</div>
            )}
          </div>
        </div>
        )}

        {/* Phones' Chats tab: team chats are chats too; their members live on the Bots tab */}
        {section === 'chats' && teams.length > 0 && (
        <div data-chats-section="teams">
          <div className={sectionHead}>
            <span>Team chats</span>
          </div>
          <div className="mt-1 space-y-0.5">
            {teams.map((t) => (
              <React.Fragment key={t.id}>{teamRow(t, false)}</React.Fragment>
            ))}
          </div>
        </div>
        )}

        {/* Phones' Chats tab: every other bot's direct chat, by name */}
        {directBots.length > 0 && (
          <section data-chats-section="direct" aria-labelledby="chats-direct-head">
            <h2 id="chats-direct-head" className={sectionHead}>Direct chats</h2>
            <div className="mt-1 space-y-0.5">{directBots.map((k) => botRow(k, undefined, {}))}</div>
          </section>
        )}

        {/* Every bot once, grouped by team; bots in no team last */}
        {showBots && (
        <div data-bot-list>
          <div className={sectionHead}>
            <h2 className="whitespace-nowrap shrink-0">Bots ({kwants.length})</h2>
            <span className="flex items-center gap-1 shrink-0">
              {headButton('Team', 'New team', `New team in ${space.name}`, onCreateTeam)}
              {headButton('Bot', 'New bot', `New bot in ${space.name}`, () => onSummonKwant())}
            </span>
          </div>
          <div className="mt-1 space-y-2">
            {kwants.length === 0 && teams.length === 0 && (
              <div className="px-2.5 py-1.5 text-[11px] text-zinc-500">No bots in {space.name} yet.</div>
            )}
            {groups.map((g) =>
              g.team ? (
                <div key={g.team.id} data-team-group={g.team.id}>
                  {teamRow(g.team, true)}
                  {!collapsedTeams.has(g.team.id) && (
                    <div className="ml-[13px] pl-1.5 border-l border-zinc-800 mt-0.5 space-y-0.5">
                      {g.bots.map((k) => botRow(k, g.team!))}
                      {addMember(g.team)}
                    </div>
                  )}
                </div>
              ) : (
                g.bots.length > 0 && (
                  <div key="no-team" data-team-group="none">
                    {teams.length > 0 && (
                      <div className="px-2 pt-1 pb-0.5 text-[10px] font-semibold uppercase tracking-wider text-zinc-500">No team</div>
                    )}
                    <div className="space-y-0.5">{g.bots.map((k) => botRow(k))}</div>
                  </div>
                )
              ),
            )}
          </div>
          <ReadyTeamsSidebarBox teamCount={teams.length} />
        </div>
        )}

        {/* Desktop: a one-time tip; Settings › All spaces keeps "Use kwant bots on your phone" for later */}
        {section === 'all' && onOpenPhoneApp && <PhoneAppTip className={navRow(false)} onOpen={onOpenPhoneApp} />}
      </div>

      {/* Footer: what this space's bots run on, from the real checks */}
      {/* On the phone's Chats tab only while nothing can run, so setup is never hidden there */}
      {engines.engines?.length !== 0 && (section !== 'chats' || canRun === false) ? (
        <div className="p-3 border-t border-hairline bg-surface-rail/60 text-meta text-zinc-400">
          <button
            type="button"
            onClick={offerClaude ? onSignInClaude : onOpenAccounts}
            disabled={!onOpenAccounts && !offerClaude}
            className="w-full flex items-center gap-1.5 min-w-0 hover:text-zinc-300 disabled:hover:text-zinc-500 transition-colors"
            title={runsOn.length > 1 ? `Runs on ${runsOn.join(', ')}` : undefined}
          >
            {readyCount === null ? (
              <Loader2 size={13} className="text-sky-400 animate-spin motion-reduce:animate-none" aria-hidden="true" />
            ) : readyCount > 0 ? (
              <CheckCircle2 size={13} className="text-ok" aria-hidden="true" />
            ) : (
              <KeyRound size={13} className="text-info" aria-hidden="true" />
            )}
            {/* The prompt wraps, so its fix is never cut off */}
            <span className={readyCount === 0 ? 'text-left' : 'truncate'}>
              {readyCount === null
                ? 'Checking accounts…'
                : readyCount === 0
                  ? 'No account yet · Getting started'
                  : `${runsOn[0]} · signed in${runsOn.length > 1 ? ` +${runsOn.length - 1}` : ''}`}
            </span>
          </button>
        </div>
      ) : null}
      {memberMenu && (
        <MemberSheet
          team={memberMenu.team}
          kwant={memberMenu.kwant}
          isLead={!!memberMenu.team && memberMenu.kwant.id === memberMenu.team.lead_kwant_id}
          confirmRemove={!!memberMenu.confirmRemove}
          onClose={closeMemberMenu}
          onChat={() => {
            setMemberMenu(null);
            onSelectKwant(memberMenu.kwant);
          }}
          onEdit={
            onEditKwant &&
            (() => {
              setMemberMenu(null);
              onEditKwant(memberMenu.kwant);
            })
          }
          onAskRemove={memberMenu.team && onUpdateTeamMembers && (() => setMemberMenu({ ...memberMenu, confirmRemove: true }))}
          onRemove={
            memberMenu.team &&
            onUpdateTeamMembers &&
            (() => {
              const { kwant } = memberMenu;
              const team = memberMenu.team!;
              closeMemberMenu();
              onUpdateTeamMembers(team, memberIdsOf(team).filter((id) => id !== kwant.id));
            })
          }
        />
      )}
    </aside>
  );
};

/** A team member's actions as a bottom sheet on phones. Removing asks once more, away from the
 * other buttons, so a stray tap can't take a bot out of its team; desktop's Remove opens straight
 * on that question, as a small centred dialog. */
const MemberSheet: React.FC<{
  /** None for a bot in no team: the sheet then has Chat and Edit only. */
  team?: Team;
  kwant: Kwant;
  isLead: boolean;
  confirmRemove: boolean;
  onClose: () => void;
  onChat: () => void;
  onEdit?: () => void;
  onAskRemove?: () => void;
  onRemove?: () => void;
}> = ({ team, kwant, isLead, confirmRemove, onClose, onChat, onEdit, onAskRemove, onRemove }) => {
  const ref = React.useRef<HTMLDivElement>(null);
  useEffect(() => {
    const onKey = (e: KeyboardEvent) => e.key === 'Escape' && onClose();
    window.addEventListener('keydown', onKey);
    return () => window.removeEventListener('keydown', onKey);
  }, [onClose]);
  useEffect(() => {
    ref.current?.querySelector<HTMLElement>('button')?.focus();
  }, [confirmRemove]);
  // Tab stays inside the sheet.
  const onKeyDown = (e: React.KeyboardEvent) => {
    if (e.key !== 'Tab' || !ref.current) return;
    const items = Array.from(ref.current.querySelectorAll<HTMLElement>('button:not([disabled])'));
    const to = trapFocusTarget(items.length, items.indexOf(document.activeElement as HTMLElement), e.shiftKey);
    if (to !== null) {
      e.preventDefault();
      items[to]?.focus();
    }
  };
  const item = 'w-full flex items-center gap-3 px-3 py-2.5 rounded-lg text-left text-sm transition-colors focus-visible:outline focus-visible:outline-2 focus-visible:outline-sky-400';
  const titleId = `member-sheet-${kwant.id}`;
  return (
    <div className="fixed inset-0 z-50 bg-black/60 flex items-end md:items-center md:justify-center md:p-4" onMouseDown={(e) => e.target === e.currentTarget && onClose()}>
      <div
        ref={ref}
        role="dialog"
        aria-modal="true"
        aria-labelledby={titleId}
        onKeyDown={onKeyDown}
        className="kw-sheet w-full max-h-[80vh] overflow-y-auto rounded-t-2xl bg-zinc-900 border-t border-zinc-800 p-3 shadow-2xl md:max-w-sm md:rounded-2xl md:border"
      >
        <div className="mx-auto mb-2 h-1 w-10 rounded-full bg-zinc-700 md:hidden" aria-hidden="true" />
        <div className="flex items-center gap-3 px-3 pb-3">
          <ModernAvatar role={kwant.avatar_sprite} name={kwant.name} size={32} kwantId={kwant.id} />
          <div className="min-w-0">
            <p id={titleId} className="text-sm font-semibold text-zinc-100 truncate">{kwant.name}</p>
            <p className="text-xs text-zinc-500 truncate">
              {team ? `${isLead ? 'Leads' : 'In'} @${team.name}${kwant.title ? ` · ${kwant.title}` : ''}` : kwant.title || 'No team'}
            </p>
          </div>
        </div>
        {confirmRemove && onRemove && team ? (
          <div className="px-3 pb-1 space-y-3">
            <p className="text-sm text-zinc-300">
              Remove {kwant.name} from @{team.name}? The bot stays in this space and keeps its chats.
            </p>
            <div className="grid grid-cols-2 gap-2">
              <button type="button" onClick={onClose} className="rounded-lg border border-zinc-700 px-3 py-2 text-sm text-zinc-200 hover:bg-zinc-800">
                Cancel
              </button>
              <button type="button" onClick={onRemove} className="rounded-lg bg-rose-600 px-3 py-2 text-sm font-medium text-white hover:bg-rose-700">
                Remove
              </button>
            </div>
          </div>
        ) : (
          <div className="space-y-0.5">
            <button type="button" onClick={onChat} className={`${item} text-zinc-200 hover:bg-zinc-800`}>
              <MessageSquare size={16} className="text-zinc-400" aria-hidden="true" /> Chat with {kwant.name}
            </button>
            {onEdit && (
              <button type="button" onClick={onEdit} className={`${item} text-zinc-200 hover:bg-zinc-800`}>
                <Pencil size={16} className="text-zinc-400" aria-hidden="true" /> Edit or delete {kwant.name}
              </button>
            )}
            {team && onAskRemove && !isLead && (
              <>
                <div className="my-1 border-t border-zinc-800" />
                <button type="button" onClick={onAskRemove} className={`${item} text-rose-300 hover:bg-rose-500/10`}>
                  <UserMinus size={16} aria-hidden="true" /> Remove from @{team.name}
                </button>
              </>
            )}
            {isLead && (
              <p className="px-3 pt-1 text-xs text-zinc-500">The lead can&apos;t be removed. Pick another lead in team settings first.</p>
            )}
          </div>
        )}
      </div>
    </div>
  );
};

/**
 * W1 on a channel row: the globe is its own button beside the row's, never inside it, at the right end
 * of the name line like every other row's. It stays for its 150 ms fade-out.
 */
const ChannelRowGlobe: React.FC<{
  channelId: string;
  watching: Browsing | null;
  name: string;
  phone?: boolean;
  onWatch?: (owner: string) => void;
  children: (pad: boolean) => React.ReactElement;
}> = ({ channelId, watching, name, phone, onWatch, children }) => {
  const { value, leaving } = useFadeOut(watching);
  if (!value || !onWatch) return children(false);
  return (
    <div className="relative" data-channel-browsing={channelId}>
      {children(true)}
      <span className="absolute right-2 top-1/2 -translate-y-1/2 flex">
        <WatchGlobe name={name} host={value.host} phone={phone} leaving={leaving} onWatch={() => onWatch(value.owner)} />
      </span>
    </div>
  );
};