:root {
  color-scheme: light;
  --bg: #f5f7f8;
  --surface: #ffffff;
  --surface-alt: #eef3f1;
  --border: #d4dddc;
  --text: #17201f;
  --muted: #637170;
  --accent: #0f766e;
  --accent-dark: #115e59;
  --blue: #285f9f;
  --red: #a43b3b;
  --amber: #91651d;
  --shadow: 0 10px 24px rgba(23, 32, 31, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
textarea {
  font: inherit;
}

button {
  min-height: 36px;
  border: 1px solid var(--accent-dark);
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  padding: 0 12px;
  cursor: pointer;
}

button.secondary {
  background: var(--surface);
  color: var(--accent-dark);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.6;
}

input,
textarea {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  padding: 9px 10px;
}

textarea {
  resize: vertical;
}

label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-weight: 600;
}

summary {
  cursor: pointer;
  color: var(--accent-dark);
  font-weight: 700;
  padding: 4px 0;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(260px, 320px) minmax(420px, 1fr) minmax(280px, 360px);
  gap: 14px;
  min-height: 100vh;
  padding: 14px;
}

.sidebar,
.right-rail,
.workspace {
  min-width: 0;
}

.sidebar,
.right-rail {
  display: grid;
  align-content: start;
  gap: 14px;
}

.workspace {
  display: grid;
  align-content: start;
  gap: 14px;
}

.brand {
  padding: 10px 2px 2px;
}

.brand h1,
.thread-header h2,
.panel h2,
.panel h3 {
  margin: 0;
  letter-spacing: 0;
}

.brand h1 {
  font-size: 30px;
}

.brand p,
.eyebrow,
.status-text,
.gap-note {
  color: var(--muted);
}

.brand p,
.eyebrow {
  margin: 2px 0 0;
}

.panel {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 14px;
}

.panel-heading,
.thread-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.panel h2 {
  font-size: 17px;
}

.panel h3 {
  font-size: 14px;
  margin-bottom: 8px;
}

.stack-form,
.turn-form {
  display: grid;
  gap: 10px;
}

.item-list,
.library-list {
  display: grid;
  gap: 8px;
  margin-top: 12px;
}

.item-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: stretch;
}

.item-button,
.agent-card {
  width: 100%;
  min-height: 54px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  padding: 10px;
  text-align: left;
}

.delete-button {
  min-height: 54px;
  border-color: var(--red);
  background: var(--surface);
  color: var(--red);
}

.delete-button:hover,
.delete-button:focus {
  background: #f6e2e2;
}

.compact-delete-button {
  min-height: 32px;
  padding: 0 10px;
}

.item-button.is-active,
.agent-card.is-active {
  border-color: var(--accent);
  box-shadow: inset 3px 0 0 var(--accent);
}

.item-title,
.agent-name,
.turn-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.item-title,
.agent-name {
  font-weight: 800;
}

.item-subtitle,
.agent-detail,
.usage-line {
  color: var(--muted);
  font-size: 12px;
}

.thread-header {
  min-height: 74px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  padding: 16px;
  box-shadow: var(--shadow);
}

.usage-rollup {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.project-usage {
  display: grid;
  gap: 8px;
}

.usage-summary-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-alt);
  padding: 8px;
}

.usage-pill,
.agent-badge,
.status-badge {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  border-radius: 999px;
  padding: 2px 9px;
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}

.usage-pill {
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
}

.agent-badge {
  background: #dcefed;
  color: var(--accent-dark);
}

.status-badge {
  background: #e6edf7;
  color: var(--blue);
}

.status-badge.failed,
.status-badge.blocked {
  background: #f6e2e2;
  color: var(--red);
}

.status-badge.completed {
  background: #dfeee5;
  color: #28613b;
}

.status-badge.not_implemented {
  background: #f2e8d4;
  color: var(--amber);
}

.agent-picker {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 10px;
  margin-bottom: 12px;
}

.turn-form {
  grid-template-columns: minmax(140px, 180px) 1fr;
  align-items: end;
}

.turn-form label:nth-child(2),
.turn-form button {
  grid-column: 1 / -1;
}

.turn-history {
  display: grid;
  gap: 12px;
}

.turn-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  padding: 12px;
}

.turn-card pre {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  margin: 10px 0 0;
  border-left: 3px solid var(--border);
  padding-left: 10px;
}

.prompt-block {
  color: #263230;
}

.response-block {
  color: #101817;
}

.empty-state {
  color: var(--muted);
  padding: 12px 0;
}

.library-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
}

.library-item-header,
.turn-actions,
.notepad-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.library-item-header .item-title {
  min-width: 0;
  flex: 1;
}

.notepad-actions {
  justify-content: flex-start;
  flex-wrap: wrap;
}

.notepad-panel {
  display: grid;
  gap: 10px;
}

.terminal-panel {
  display: grid;
  gap: 10px;
  position: fixed;
  left: max(14px, env(safe-area-inset-left));
  right: max(14px, env(safe-area-inset-right));
  bottom: max(14px, env(safe-area-inset-bottom));
  z-index: 50;
  /* The panel itself (header + Connect/Collapse controls) stays always
     visible as a docked bar - only .terminal-host below collapses/expands.
     Connect must stay reachable to ever open the drawer in the first place. */
}

.terminal-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 8px;
}

.terminal-host {
  min-height: 0;
  max-height: 0;
  overflow: hidden;
  border: 1px solid #111817;
  border-radius: 8px;
  background: #101817;
  padding: 0 8px;
  opacity: 0;
  transition: max-height 220ms ease, opacity 180ms ease, padding 220ms ease;
}

.terminal-panel.is-open .terminal-host {
  min-height: 320px;
  max-height: min(50vh, 500px);
  opacity: 1;
  padding: 8px;
}

.terminal-host .xterm {
  height: 100%;
}

.toast {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 60;
  max-width: min(420px, calc(100vw - 32px));
  border-radius: 8px;
  background: #17201f;
  color: #fff;
  padding: 12px 14px;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 150ms ease, transform 150ms ease;
  pointer-events: none;
}

.toast.is-visible {
  opacity: 1;
  transform: translateY(0);
}

@media (max-width: 1100px) {
  .app-shell {
    grid-template-columns: minmax(240px, 310px) 1fr;
  }

  .right-rail {
    grid-column: 1 / -1;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 760px) {
  .app-shell,
  .right-rail {
    grid-template-columns: 1fr;
  }

  .thread-header,
  .panel-heading,
  .turn-form {
    display: grid;
    grid-template-columns: 1fr;
  }

  .usage-rollup {
    justify-content: flex-start;
  }

  .terminal-panel {
    left: 8px;
    right: 8px;
    bottom: 8px;
  }

  .terminal-panel.is-open .terminal-host {
    min-height: 260px;
    max-height: min(52vh, 420px);
  }

  .item-row {
    grid-template-columns: 1fr;
  }
}
