/* Agent Host: a cmux-style workspace UI (vertical workspace tabs, split panes with tab bars). */
:root {
  --side: #1e1e1e;
  --bar: #1e1e1e;
  --term: #272822;
  --line: #2d2d2d;
  --line-strong: #3a3a3a;
  --hover: rgba(255, 255, 255, .06);
  --text: #e6e6e6;
  --muted: #a3a3a3;
  --dim: #707070;
  --accent: #0a84ff;
  --accent-soft: rgba(10, 132, 255, .18);
  --live: #32d74b;
  --halt: #ff453a;
  --sidebar-w: 250px;
  --ui: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  --mono: "JetBrains Mono", "SF Mono", Menlo, monospace;
  --app-h: 100dvh;
  color-scheme: dark;
  font: 13px/1.35 var(--ui);
  color: var(--text);
  background: var(--term);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; overflow: hidden; -webkit-font-smoothing: antialiased; }
h2, p { margin: 0; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; }
.spacer { flex: 1; }

.icon { width: 15px; height: 15px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn {
  display: inline-grid; place-items: center; width: 26px; height: 26px; padding: 0;
  border: 0; border-radius: 6px; background: transparent; color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.icon-btn:focus-visible, .tab:focus-visible, .ws-item:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

.app { display: flex; height: var(--app-h); }

/* --- sidebar (vertical workspace tabs) ------------------------------------------------- */

.sidebar {
  width: var(--sidebar-w); flex: none; display: flex; flex-direction: column;
  background: var(--side); border-right: 1px solid var(--line);
}
body.sidebar-hidden .sidebar { display: none; }
.sidebar-top { display: flex; align-items: center; gap: 4px; height: 38px; padding: 0 8px; }

.workspaces { flex: 1; overflow-y: auto; padding: 2px 6px 8px; }
.ws-item {
  position: relative; display: block; width: 100%; padding: 7px 26px 8px 10px; margin-bottom: 2px;
  border: 0; border-radius: 8px; background: transparent; text-align: left; cursor: pointer;
}
.ws-item:hover { background: var(--hover); }
.ws-item.selected { background: var(--accent); }
.ws-item.drop { box-shadow: inset 0 0 0 2px var(--accent); }
.ws-head { display: flex; align-items: center; gap: 6px; min-width: 0; }
.ws-name { font-weight: 600; font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ws-sub {
  margin-top: 2px; font-size: 12px; color: var(--muted);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.ws-meta { margin-top: 3px; font: 11px/1.3 var(--mono); color: var(--dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ws-item.selected .ws-sub, .ws-item.selected .ws-meta { color: rgba(255, 255, 255, .85); }
.ws-close { position: absolute; top: 6px; right: 4px; width: 20px; height: 20px; opacity: 0; }
.ws-close .icon { width: 13px; height: 13px; }
.ws-item:hover .ws-close, .ws-close:focus-visible { opacity: 1; }
.ws-item.selected .ws-close { color: #fff; }
.badge {
  display: inline-grid; place-items: center; min-width: 17px; height: 17px; padding: 0 5px; border-radius: 9px;
  font-size: 10.5px; font-weight: 600; background: var(--accent); color: #fff;
}
.ws-item.selected .badge { background: #fff; color: var(--accent); }
.state-dot { width: 7px; height: 7px; border-radius: 50%; flex: none; background: var(--live); }
.state-dot.halted { background: var(--halt); }

.other { border-top: 1px solid var(--line); padding: 8px 6px; max-height: 35%; overflow-y: auto; }
.workspaces .hint { padding: 8px 10px; font-size: 12px; color: var(--dim); }
.section-title { padding: 2px 10px 6px; font-size: 11px; font-weight: 600; color: var(--dim); text-transform: uppercase; letter-spacing: .06em; }
.count { color: var(--muted); }
.other-item {
  display: flex; align-items: center; gap: 8px; width: 100%; padding: 6px 10px; border: 0; border-radius: 6px;
  background: transparent; text-align: left; cursor: pointer; color: var(--muted);
}
.other-item:hover { background: var(--hover); color: var(--text); }
.other-item span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.other-item small { color: var(--dim); flex: none; margin-left: auto; white-space: nowrap; }

.sidebar-footer { display: flex; align-items: center; gap: 6px; padding: 8px; border-top: 1px solid var(--line); }
.me { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; padding: 3px 6px; border-radius: 6px; }
.me:focus-within { background: var(--hover); }
.me input { flex: 1; min-width: 0; border: 0; background: transparent; color: var(--text); font: inherit; outline: none; }
.avatar {
  --hue: 210; width: 22px; height: 22px; flex: none; border-radius: 50%; display: grid; place-items: center;
  font-size: 9.5px; font-weight: 700; color: hsl(var(--hue) 80% 85%); background: hsl(var(--hue) 40% 30%);
}

/* --- main: title bar and split panes --------------------------------------------------- */

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--term); position: relative; }
.titlebar {
  display: flex; align-items: center; gap: 8px; height: 38px; flex: none; padding: 0 10px;
  background: var(--bar); border-bottom: 1px solid var(--line);
}
#show-sidebar { display: none; }
body.sidebar-hidden #show-sidebar { display: inline-grid; }
.titlebar .folder { color: #5eb3ff; fill: rgba(94, 179, 255, .25); }
.ws-title {
  display: flex; align-items: center; gap: 6px; min-width: 0; padding: 0; border: 0; background: none; cursor: default;
  font-weight: 600; font-size: 13.5px;
}
.ws-title span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ws-title .caret { width: 15px; height: 15px; color: var(--dim); }

.workspace { flex: 1; min-height: 0; display: flex; }
.workspace > * { flex: 1; }
.split { display: flex; min-width: 0; min-height: 0; }
.split.row { flex-direction: row; }
.split.col { flex-direction: column; }
.split > .pane, .split > .split { min-width: 0; min-height: 0; }
.gutter { flex: none; position: relative; background: var(--line); z-index: 2; }
.split.row > .gutter { width: 1px; cursor: col-resize; }
.split.col > .gutter { height: 1px; cursor: row-resize; }
.gutter::before { content: ""; position: absolute; inset: -4px; }
.gutter:hover, .gutter.active { background: var(--accent); }
body.resizing { user-select: none; }
body.resizing .pane-body { pointer-events: none; }
body.resizing.row-resize { cursor: col-resize; }
body.resizing.col-resize { cursor: row-resize; }

.pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; position: relative; }
.tabbar {
  display: flex; align-items: stretch; height: 30px; flex: none;
  background: var(--bar); border-bottom: 1px solid var(--line);
}
.tabs { display: flex; min-width: 0; overflow-x: auto; scrollbar-width: none; flex: 1; }
.tabs::-webkit-scrollbar { display: none; }
.tab {
  position: relative; display: flex; align-items: center; gap: 6px; flex: 0 1 220px; min-width: 90px; padding: 0 6px 0 10px;
  border-right: 1px solid var(--line); color: var(--dim); cursor: pointer; user-select: none; font-size: 12px;
}
.tab .icon { width: 13px; height: 13px; }
.tab:hover { color: var(--muted); background: rgba(255, 255, 255, .02); }
.tab.active { color: var(--muted); background: var(--term); }
.pane.focused .tab.active { color: var(--text); }
.pane.focused .tab.active::after { content: ""; position: absolute; left: 0; right: 0; bottom: -1px; height: 1px; background: var(--term); }
.tab-title { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tab .flag { color: var(--dim); }
.tab .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--halt); flex: none; }
.tab .typing { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); flex: none; animation: pulse 1s ease-in-out infinite; }
.tab-close { width: 18px; height: 18px; border-radius: 4px; opacity: 0; }
.tab-close .icon { width: 12px; height: 12px; }
.tab:hover .tab-close, .tab.active .tab-close { opacity: 1; }
.tab.drop-before::before { content: ""; position: absolute; left: -1px; top: 4px; bottom: 4px; width: 2px; background: var(--accent); }
.tab.dragging { opacity: .4; }
.tab-actions { display: flex; align-items: center; gap: 1px; padding: 0 6px; flex: none; }
.tab-actions .icon-btn { width: 22px; height: 22px; color: var(--dim); }
.tab-actions .icon-btn:hover { color: var(--text); }
.tab-actions .icon { width: 14px; height: 14px; }
.viewers { display: flex; align-items: center; margin-right: 6px; }
.viewers .avatar { width: 18px; height: 18px; font-size: 8px; margin-left: -4px; box-shadow: 0 0 0 2px var(--bar); }

.pane-body { flex: 1; min-height: 0; position: relative; background: var(--term); overflow: auto; }
.term-host { position: absolute; inset: 4px 2px 2px 8px; }
.term-host .xterm { height: 100%; padding-right: 10px; }
.term-host .xterm-viewport { scrollbar-width: thin; scrollbar-color: #4a4a4a transparent; background-color: var(--term) !important; }
.term-host .xterm-viewport::-webkit-scrollbar { width: 8px; }
.term-host .xterm-viewport::-webkit-scrollbar-thumb { background: #4a4a4a; border-radius: 4px; }
.pane-empty { position: absolute; inset: 0; display: grid; place-items: center; color: var(--dim); font-size: 12px; }

.exit-banner {
  position: absolute; left: 10px; right: 10px; bottom: 10px; z-index: 3; display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: 8px; background: rgba(30, 30, 30, .96); border: 1px solid var(--line-strong);
  box-shadow: 0 8px 24px rgba(0, 0, 0, .4); font-size: 12.5px;
}
.exit-banner .icon { color: var(--halt); }
.exit-banner span { flex: 1; }
.btn {
  height: 26px; padding: 0 10px; border-radius: 6px; border: 1px solid var(--line-strong); background: #2a2a2a; cursor: pointer; font-size: 12px;
}
.btn:hover { background: #333; }
.btn.danger { color: var(--halt); }

/* Drop zones while dragging a tab over a pane. */
.drop-zone {
  position: absolute; z-index: 4; pointer-events: none; border-radius: 6px;
  background: var(--accent-soft); border: 2px solid var(--accent); transition: inset .08s;
}

.empty { margin: auto; text-align: center; max-width: 360px; padding: 24px; color: var(--muted); }
.empty img { margin-bottom: 14px; opacity: .9; }
.empty h2 { font-size: 16px; color: var(--text); margin-bottom: 6px; }
.empty-agents { display: grid; gap: 6px; margin-top: 18px; }
.agent-btn {
  display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 8px;
  border: 1px solid var(--line-strong); background: #232323; cursor: pointer; text-align: left;
}
.agent-btn:hover { background: #2b2b2b; }
.agent-btn small { margin-left: auto; font: 11px var(--mono); color: var(--dim); }
.agent-btn:disabled { opacity: .45; cursor: not-allowed; }

/* --- menus and toasts ------------------------------------------------------------------ */

.menu {
  position: fixed; z-index: 50; min-width: 220px; padding: 5px;
  background: #2a2a2a; border: 1px solid #3c3c3c; border-radius: 9px;
  box-shadow: 0 14px 40px rgba(0, 0, 0, .5); animation: pop .1s ease-out;
}
.menu-label { padding: 6px 10px 3px; font-size: 11px; color: var(--dim); }
.menu-sep { height: 1px; margin: 4px 6px; background: #3c3c3c; }
.menu-item {
  display: flex; align-items: center; gap: 9px; width: 100%; padding: 6px 10px; border: 0; border-radius: 5px;
  background: transparent; text-align: left; cursor: pointer; font-size: 13px;
}
.menu-item:hover, .menu-item:focus-visible { background: var(--accent); color: #fff; outline: none; }
.menu-item small { margin-left: auto; padding-left: 14px; font: 11px var(--mono); color: var(--dim); }
.menu-item:hover small { color: rgba(255, 255, 255, .8); }
.menu-item.danger { color: var(--halt); }
.menu-item.danger:hover { background: var(--halt); color: #fff; }
.menu-item:disabled { opacity: .45; pointer-events: none; }
.menu-item .check { margin-left: auto; }

.toasts { position: fixed; right: 14px; bottom: 14px; z-index: 60; display: grid; gap: 6px; }
.toast {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-radius: 8px; font-size: 12.5px;
  background: #2a2a2a; border: 1px solid #3c3c3c; box-shadow: 0 10px 30px rgba(0, 0, 0, .45); animation: pop .15s ease-out;
}
.toast .icon { color: var(--accent); }
.toast.error .icon { color: var(--halt); }
.toast.busy .icon { animation: spin .9s linear infinite; }
.toast.leaving { animation: fade-out .2s ease-in forwards; }

@keyframes pop { from { opacity: 0; transform: translateY(4px) scale(.98); } }
@keyframes fade-out { to { opacity: 0; } }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse { 50% { opacity: .3; } }

/* --- mobile terminal switcher (bottom sheet) ------------------------------------------- */

.sheet-scrim { position: fixed; inset: 0; z-index: 45; background: rgba(0, 0, 0, .45); }
.sheet {
  position: fixed; z-index: 46; left: 0; right: 0; bottom: 0; max-height: 75vh; overflow-y: auto;
  padding: 6px 10px calc(14px + env(safe-area-inset-bottom)); border-radius: 16px 16px 0 0;
  background: #242424; border-top: 1px solid #3c3c3c; box-shadow: 0 -20px 50px rgba(0, 0, 0, .5);
  animation: sheet-in .2s ease-out;
}
.sheet-grip { width: 38px; height: 5px; border-radius: 3px; background: #4a4a4a; margin: 4px auto 6px; }
.sheet .menu-label { padding: 12px 6px 6px; }
.sheet-row {
  display: flex; align-items: center; gap: 12px; min-height: 56px; padding: 8px 10px; border-radius: 10px; cursor: pointer;
}
.sheet-row:active { background: rgba(255, 255, 255, .08); }
.sheet-row.active { background: var(--accent-soft); }
.sheet-row .agent-tile { width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center; background: #2e2e2e; color: var(--muted); flex: none; }
.sheet-row.active .agent-tile { color: var(--accent); }
.sheet-text { flex: 1; min-width: 0; display: grid; gap: 2px; }
.sheet-text strong { font-size: 14.5px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sheet-text small { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sheet-check { color: var(--accent); width: 18px; height: 18px; }
.sheet-agents { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 8px; padding: 0 2px; }
.sheet-agents .agent-btn { min-height: 46px; }
@keyframes sheet-in { from { transform: translateY(30px); opacity: 0; } }

/* --- mobile ---------------------------------------------------------------------------- */

.mobile-only { display: none; }

@media (max-width: 760px) {
  .mobile-only { display: inline-grid; }
  .sidebar {
    position: fixed; z-index: 40; top: 0; bottom: 0; left: 0; width: min(84vw, 320px);
    transform: translateX(-100%); transition: transform .22s ease; box-shadow: 0 0 40px rgba(0, 0, 0, .5);
    padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom);
  }
  body.sidebar-hidden .sidebar { display: flex; }
  body.drawer-open .sidebar { transform: none; }
  .scrim { position: fixed; inset: 0; z-index: 39; background: rgba(0, 0, 0, .45); opacity: 0; pointer-events: none; transition: opacity .2s; }
  body.drawer-open .scrim { opacity: 1; pointer-events: auto; }
  #show-sidebar, body.sidebar-hidden #show-sidebar { display: inline-grid; }
  #hide-sidebar { display: none; }
  .titlebar { height: 44px; padding-top: env(safe-area-inset-top); height: calc(44px + env(safe-area-inset-top)); }
  .titlebar .icon-btn { width: 34px; height: 34px; }
  .titlebar .icon-btn .icon { width: 18px; height: 18px; }
  .ws-item { padding: 10px 30px 10px 12px; }
  .tabbar { height: 36px; }
  .tab { flex: 0 0 auto; min-width: 0; max-width: 60vw; padding: 0 8px 0 12px; font-size: 12.5px; }
  .tab-close { opacity: 1; }
  .tab-actions .icon-btn { width: 30px; height: 30px; }
  .term-host { inset: 2px 0 0 4px; touch-action: none; } /* swipes scroll the terminal (see app.js) */
  .ws-title { cursor: pointer; min-height: 34px; }
  .pane-body { overflow: hidden; }
  .toasts { left: 10px; right: 10px; bottom: calc(52px + env(safe-area-inset-bottom)); }

  body.no-workspace .keybar { display: none; }
  .keybar {
    display: flex; gap: 6px; padding: 6px 8px calc(6px + env(safe-area-inset-bottom)); overflow-x: auto;
    background: var(--bar); border-top: 1px solid var(--line); scrollbar-width: none; flex: none;
  }
  .keybar::-webkit-scrollbar { display: none; }
  .key {
    flex: none; min-width: 40px; height: 34px; padding: 0 10px; border-radius: 7px; border: 1px solid var(--line-strong);
    background: #2a2a2a; color: var(--text); font: 13px var(--mono); display: grid; place-items: center;
    touch-action: manipulation; -webkit-user-select: none; user-select: none;
  }
  .key:active { background: #3a3a3a; }
  .key.on { background: var(--accent); border-color: var(--accent); color: #fff; }
  .key .icon { width: 16px; height: 16px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
