:root {
  --bg: #1e1e1e;
  --bg-elevated: #252526;
  --bg-hover: #2a2d2e;
  --border: #3c3c3c;
  --border-subtle: #2d2d2d;
  --text: #e4e4e4;
  --muted: #8d8d8d;
  --accent: #3794ff;
  --accent-dim: #1e5a99;
  --danger: #f14c4c;
  --success: #73c991;
  --warn: #cca700;
  --log-err: #f48771;
  --log-out: #9cdcfe;
  --font-sans: "IBM Plex Sans", system-ui, sans-serif;
  --font-mono: "IBM Plex Mono", "SFMono-Regular", ui-monospace, monospace;
  --topbar-h: 44px;
  --composer-h: min(160px, 28vh);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 13px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

body {
  overflow: hidden;
}

#app {
  height: 100%;
}

.mono {
  font-family: var(--font-mono);
  font-size: 12px;
}

.muted {
  color: var(--muted);
}

.small {
  font-size: 11px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.workspace {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}

/* Top bar */
.topbar {
  flex: 0 0 var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elevated);
  min-height: var(--topbar-h);
}

.topbar-left,
.topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.topbar-center {
  flex: 1;
  min-width: 0;
  display: flex;
  justify-content: center;
}

.brand {
  font-weight: 600;
  letter-spacing: 0.06em;
  font-size: 12px;
  color: var(--muted);
  padding-right: 8px;
  border-right: 1px solid var(--border);
}

.session-picker {
  display: flex;
  align-items: center;
  gap: 8px;
}

.select {
  min-width: 180px;
  max-width: 280px;
  padding: 6px 10px;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

.activity-line {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: min(520px, 50vw);
}

/* Main panes */
.main-panes {
  flex: 1;
  display: flex;
  min-height: 0;
}

.pane-chat {
  flex: 1.65;
  min-width: 0;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
}

.pane-files {
  flex: 1;
  min-width: 280px;
  max-width: 52vw;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.pane-chat-inner {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.thread-scroll {
  flex: 1;
  overflow: auto;
  padding: 16px 20px 12px;
  scroll-behavior: smooth;
}

.thread-empty {
  color: var(--muted);
  padding: 24px 8px;
  font-size: 13px;
}

.thread-item {
  margin-bottom: 12px;
  padding: 10px 12px;
  border-radius: 6px;
  border: 1px solid var(--border-subtle);
  background: var(--bg-elevated);
}

.thread-item--user {
  border-color: var(--accent-dim);
  margin-left: 12%;
}

.thread-item--assistant {
  margin-right: 8%;
}

.thread-item--pending {
  opacity: 0.85;
}

.thread-item--log {
  padding: 6px 10px;
  border-left: 3px solid var(--border);
  background: rgba(0, 0, 0, 0.25);
}

.thread-item--log-err {
  border-left-color: var(--log-err);
}

.thread-item--log-out {
  border-left-color: var(--log-out);
}

.thread-meta {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 6px;
  font-size: 11px;
  color: var(--muted);
}

.thread-meta time {
  font-variant-numeric: tabular-nums;
}

.thread-body {
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 12px;
  line-height: 1.55;
}

.thread-log {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 11px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--text);
}

.thread-item--permission {
  border-color: rgba(204, 167, 0, 0.45);
  background: rgba(204, 167, 0, 0.06);
}

.perm-title {
  font-weight: 500;
  margin-bottom: 6px;
}

.perm-details summary {
  cursor: pointer;
  color: var(--muted);
  font-size: 11px;
}

.perm-details pre {
  margin: 8px 0 0;
  max-height: 200px;
  overflow: auto;
}

.perm-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.thread-item--task {
  padding: 0;
  border: 1px solid var(--border);
  background: var(--bg);
}

.task-summary {
  cursor: pointer;
  padding: 8px 12px;
  font-weight: 500;
  list-style: none;
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.task-summary::-webkit-details-marker {
  display: none;
}

.task-list {
  margin: 0;
  padding: 8px 12px 12px 28px;
  font-size: 11px;
  color: var(--text);
}

.thread-item--delta,
.thread-item--status,
.thread-item--file {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  padding: 6px 10px;
}

.delta-tag,
.file-tag {
  font-family: var(--font-mono);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.thread-item--warn {
  border-color: rgba(241, 76, 76, 0.35);
  background: rgba(241, 76, 76, 0.06);
}

.thread-item--stats {
  padding: 4px 8px;
  border: 0;
  background: transparent;
}

.thread-dag {
  margin-top: 16px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  padding: 0;
}

.thread-dag summary {
  cursor: pointer;
  padding: 8px 12px;
  font-weight: 500;
  color: var(--muted);
  list-style: none;
}

.thread-dag summary::-webkit-details-marker {
  display: none;
}

.dag-wrap {
  padding: 8px;
  overflow: auto;
  max-height: 360px;
  border-top: 1px solid var(--border);
}

.dag-edge {
  stroke: var(--border);
  stroke-width: 1.5;
  fill: none;
}

.dag-node rect {
  fill: var(--bg-elevated);
  stroke: var(--border);
}

.dag-node.running rect,
.dag-node.completed rect {
  stroke: var(--success);
}

.dag-node.pending rect,
.dag-node.starting rect,
.dag-node.created rect {
  stroke: var(--warn);
}

.dag-node.failed rect,
.dag-node.stopped rect {
  stroke: var(--danger);
}

.dag-node text {
  fill: var(--text);
  font-size: 11px;
  font-family: var(--font-sans);
}

.dag-status {
  fill: var(--muted) !important;
  font-size: 10px;
}

/* Composer */
.composer {
  flex: 0 0 auto;
  border-top: 1px solid var(--border);
  background: var(--bg-elevated);
  padding: 10px 12px;
}

.composer-form {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.composer-input {
  width: 100%;
  min-height: 72px;
  max-height: var(--composer-h);
  resize: vertical;
  padding: 10px 12px;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  line-height: 1.45;
}

.composer-input:focus {
  outline: 1px solid var(--accent);
  outline-offset: 0;
}

.composer-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

/* File pane */
.files-toolbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elevated);
}

.files-title {
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.files-split {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(140px, 34%) minmax(0, 1fr);
  min-height: 0;
}

.files-tree {
  border-right: 1px solid var(--border);
  overflow: auto;
  padding: 8px 4px 12px 8px;
  background: var(--bg);
}

.tree-row {
  --depth: 0;
}

.tree-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 6px 4px calc(6px + var(--depth) * 12px);
  border: 0;
  border-radius: 3px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 12px;
  text-align: left;
  cursor: pointer;
}

.tree-item:hover {
  background: var(--bg-hover);
}

.tree-item[data-type="directory"] {
  color: var(--accent);
}

.tree-chevron {
  width: 12px;
  opacity: 0.5;
  font-size: 10px;
}

.tree-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tree-empty {
  color: var(--muted);
  font-size: 12px;
  padding: 8px;
}

.files-editor {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.tabs-bar {
  flex: 0 0 auto;
  display: flex;
  flex-wrap: nowrap;
  gap: 0;
  overflow-x: auto;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elevated);
}

.tabs-bar--empty {
  padding: 8px 12px;
}

.tab {
  display: flex;
  align-items: center;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  background: var(--bg);
}

.tab.is-active {
  background: var(--bg);
  border-bottom: 2px solid var(--accent);
  margin-bottom: -1px;
}

.tab-label {
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 11px;
  padding: 8px 10px;
  cursor: pointer;
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tab.is-active .tab-label {
  color: var(--text);
}

.tab-close {
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  padding: 4px 8px;
  font-size: 14px;
  line-height: 1;
}

.tab-close:hover {
  color: var(--text);
  background: var(--bg-hover);
}

.editor-body {
  flex: 1;
  overflow: auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.editor-meta {
  flex: 0 0 auto;
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 11px;
  color: var(--muted);
}

.editor-pre {
  flex: 1;
  margin: 0;
  padding: 12px;
  overflow: auto;
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.5;
}

.editor-pre code {
  font-family: inherit;
}

.editor-empty {
  padding: 24px;
  color: var(--muted);
  font-size: 13px;
}

/* Buttons */
.btn {
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 6px 12px;
  font: inherit;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  background: var(--bg);
  color: var(--text);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-small {
  padding: 4px 10px;
  font-size: 11px;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #0b1118;
}

.btn-primary:hover:not(:disabled) {
  filter: brightness(1.06);
}

.btn-secondary {
  background: var(--bg-hover);
}

.btn-danger {
  border-color: rgba(241, 76, 76, 0.5);
  color: #ffb4b4;
  background: rgba(241, 76, 76, 0.12);
}

.btn-danger:hover:not(:disabled) {
  background: rgba(241, 76, 76, 0.22);
}

.btn-ghost {
  border-color: transparent;
  background: transparent;
  color: var(--muted);
}

.btn-ghost:hover:not(:disabled) {
  color: var(--text);
  background: var(--bg-hover);
}

/* Drawer */
.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 40;
}

.drawer {
  position: fixed;
  top: 0;
  right: 0;
  width: min(440px, 100vw - 40px);
  height: 100%;
  background: var(--bg-elevated);
  border-left: 1px solid var(--border);
  z-index: 50;
  display: flex;
  flex-direction: column;
  box-shadow: -8px 0 32px rgba(0, 0, 0, 0.35);
}

.drawer-head {
  flex: 0 0 auto;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
}

.drawer-body {
  flex: 1;
  overflow: auto;
  padding: 12px 16px 32px;
}

.drawer-title {
  margin: 0 0 16px;
  font-size: 15px;
  font-weight: 600;
}

.drawer-caption {
  margin: -8px 0 16px;
  line-height: 1.45;
  font-size: 11px;
}

.form-stack {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 20px;
}

.form-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.field span {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.field input,
.field select,
.field textarea {
  padding: 8px 10px;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 12px;
}

.field textarea {
  font-family: var(--font-mono);
  resize: vertical;
}

.drawer-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.drawer-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg);
}

.drawer-row--btn {
  width: 100%;
  cursor: pointer;
  text-align: left;
}

.drawer-row--btn.is-active {
  border-color: var(--accent);
}

.drawer-row-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.badge {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 3px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--muted);
}

.badge--running,
.badge--completed {
  border-color: rgba(115, 201, 145, 0.45);
  color: var(--success);
}

.badge--failed,
.badge--stopped {
  border-color: rgba(241, 76, 76, 0.45);
  color: #ff9d9d;
}

.badge--created,
.badge--starting {
  border-color: rgba(204, 167, 0, 0.45);
  color: #ffdf7a;
}

.settings-grid {
  display: grid;
  gap: 12px;
  font-size: 12px;
}

.status-pill {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 2px 8px;
  border-radius: 4px;
  border: 1px solid var(--border);
  color: var(--muted);
}

/* Toast */
.toast {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 100;
  min-width: 220px;
  max-width: min(420px, calc(100vw - 32px));
  padding: 10px 14px;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: opacity 0.15s ease, transform 0.15s ease;
  font-size: 12px;
}

.toast.visible {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

.toast-inner {
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.toast-text {
  flex: 1;
  min-width: 0;
  word-break: break-word;
}

.toast-copy {
  flex-shrink: 0;
  margin-top: -2px;
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg);
  color: var(--muted);
  font: inherit;
  font-size: 11px;
  cursor: pointer;
}

.toast-copy:hover {
  color: var(--text);
  border-color: var(--accent);
}

/* highlight.js overrides for editor */
.editor-pre .hljs {
  background: transparent !important;
  padding: 0 !important;
}

/* Responsive */
@media (max-width: 960px) {
  .main-panes {
    flex-direction: column;
  }

  .pane-chat {
    flex: 1;
    min-height: 46vh;
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }

  .pane-files {
    max-width: none;
    min-height: 40vh;
  }

  .files-split {
    grid-template-columns: 1fr;
  }

  .files-tree {
    max-height: 200px;
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }
}
