/*
 * Alex Web UI — Sprint 5 stylesheet.
 *
 * Design principles:
 *   1. Mobile-first: base styles target a 360-wide viewport; desktop adds.
 *   2. Dark by default.  Light mode is available via prefers-color-scheme.
 *   3. Adaptive layouts, not "responsive hacks".  Mobile = Command Deck
 *      (single focused surface, bottom nav).  Desktop = Workshop (multi-pane,
 *      side nav, side panel for artifacts).
 *   4. Zero framework CSS.  All primitives are defined below.
 *   5. Motion is subtle — animations <200ms, respect prefers-reduced-motion.
 *
 * File is organised top-down:  tokens → base → layout → components → utilities.
 */

/* ── Design Tokens ────────────────────────────────────────────────────── */

:root {
  color-scheme: dark;

  /* Palette — cool charcoal base, cyan accent, warm orange for decisions. */
  --bg-0: #0a0a0a;
  --bg-1: #141418;
  --bg-2: #1c1c22;
  --bg-3: #26262e;
  --border: #2e2e38;
  --text: #e8e8ea;
  --text-dim: #9a9aa5;
  --text-faint: #606069;
  --accent: #7dd3fc;
  --accent-strong: #38bdf8;
  --warn: #fbbf24;
  --danger: #f87171;
  --success: #4ade80;

  /* Typography. */
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter,
               system-ui, Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "JetBrains Mono", Consolas,
               "Liberation Mono", monospace;

  /* Spacing. */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --space-7: 48px;

  /* Radii, shadows, depth. */
  --radius-1: 6px;
  --radius-2: 10px;
  --radius-3: 16px;
  --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-2: 0 4px 16px rgba(0, 0, 0, 0.35);

  /* Timing. */
  --dur-fast: 120ms;
  --dur-med:  200ms;

  /* Layout. */
  --nav-height-mobile: 56px;
  --nav-width-desktop: 220px;
}

@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;
    --bg-0: #fafafa;
    --bg-1: #ffffff;
    --bg-2: #f4f4f6;
    --bg-3: #e6e6ea;
    --border: #d4d4d8;
    --text: #18181b;
    --text-dim: #52525b;
    --text-faint: #a1a1aa;
    --accent: #0284c7;
    --accent-strong: #0369a1;
  }
}

/* ── Base ─────────────────────────────────────────────────────────────── */

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg-0);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overflow: hidden;
}

body {
  /* Safe-area insets for iOS home-bar / notch. */
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

#app { height: 100%; display: flex; flex-direction: column; }

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-strong); text-decoration: underline; }

button {
  font: inherit;
  cursor: pointer;
  background: var(--bg-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-1);
  padding: var(--space-2) var(--space-4);
  transition: background var(--dur-fast) ease, border-color var(--dur-fast) ease;
}
button:hover:not(:disabled)  { background: var(--bg-3); border-color: var(--text-faint); }
button:focus-visible         { outline: 2px solid var(--accent); outline-offset: 2px; }
button:disabled              { opacity: 0.45; cursor: not-allowed; }

.button-primary {
  background: var(--accent);
  color: #0a0a0a;
  border-color: var(--accent);
  font-weight: 600;
}
.button-primary:hover:not(:disabled) {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
}

input, textarea {
  font: inherit;
  color: var(--text);
  background: var(--bg-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-1);
  padding: var(--space-3);
  width: 100%;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

code, pre { font-family: var(--font-mono); font-size: 0.92em; }
pre {
  background: var(--bg-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-2);
  padding: var(--space-3);
  overflow-x: auto;
}

/* Scrollbars stay subtle — chrome shouldn't fight the content. */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--bg-3); border-radius: 999px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-faint); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0ms !important;
    transition-duration: 0ms !important;
  }
}

.noscript-panel {
  padding: var(--space-5);
  background: var(--bg-2);
  color: var(--warn);
  text-align: center;
}

/* ── App Shell Layout ─────────────────────────────────────────────────── */

.shell {
  display: grid;
  height: 100%;
  width: 100%;
  /* Mobile default: single column + bottom nav. */
  grid-template-rows: 1fr var(--nav-height-mobile);
  grid-template-columns: 1fr;
  grid-template-areas:
    "main"
    "nav";
}

.shell-main {
  grid-area: main;
  overflow-y: auto;
  overflow-x: hidden;
  /* Flex column so the sticky StatusBar stacks above a flexible page
   * region below it.  Pages that fill the viewport (Chat) use
   * ``flex: 1 1 auto; min-height: 0`` to share the remaining height
   * with the status bar without pushing their own docked footers
   * (e.g. the chat input bar) below the fold. */
  display: flex;
  flex-direction: column;
}

.shell-nav {
  grid-area: nav;
  background: var(--bg-1);
  border-top: 1px solid var(--border);
  display: flex;
}

/* Desktop: side nav, multi-pane-ready layout. */
@media (min-width: 960px) {
  .shell {
    grid-template-rows: 1fr;
    grid-template-columns: var(--nav-width-desktop) 1fr;
    grid-template-areas: "nav main";
  }
  .shell-nav {
    border-top: none;
    border-right: 1px solid var(--border);
    flex-direction: column;
    padding: var(--space-4) 0;
  }
}

/* ── Nav Component ────────────────────────────────────────────────────── */

.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: var(--space-2);
  color: var(--text-dim);
  text-decoration: none;
  border: none;
  background: transparent;
  border-radius: 0;
  font-size: 12px;
  position: relative;
  transition: color var(--dur-fast) ease, background var(--dur-fast) ease;
}
.nav-item:hover { color: var(--text); background: var(--bg-2); }
.nav-item.active { color: var(--accent); }
.nav-item.active::before {
  content: "";
  position: absolute;
  inset-inline: 20%;
  top: 0;
  height: 2px;
  background: var(--accent);
  border-radius: 2px;
}

.nav-icon { font-size: 18px; line-height: 1; }
.nav-label { font-size: 11px; letter-spacing: 0.02em; }
.nav-badge {
  position: absolute;
  top: 6px;
  right: 22%;
  background: var(--warn);
  color: #0a0a0a;
  font-size: 10px;
  font-weight: 700;
  border-radius: 999px;
  padding: 1px 6px;
  min-width: 18px;
  text-align: center;
  line-height: 1.4;
}

@media (min-width: 960px) {
  .nav-item {
    flex: 0 0 auto;
    flex-direction: row;
    justify-content: flex-start;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-5);
    font-size: 14px;
  }
  .nav-label { font-size: 14px; }
  .nav-item.active::before {
    inset-inline: auto;
    left: 0;
    top: 10%;
    bottom: 10%;
    height: auto;
    width: 3px;
  }
  .nav-badge {
    position: static;
    margin-left: auto;
  }
  .nav-brand {
    padding: 0 var(--space-5) var(--space-4);
    font-weight: 700;
    color: var(--text);
    letter-spacing: 0.02em;
    border-bottom: 1px solid var(--border);
    margin-bottom: var(--space-3);
  }
}

/* ── Status Bar (mission-control strip, sticky above every page) ─────── */

.status-bar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-4);
  background: var(--bg-1);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  /* Tinted left stripe mirrors the state tone — quick peripheral read. */
  border-left: 3px solid var(--border);
  transition: border-left-color var(--dur-med) ease;
}
.status-bar.tone-ok     { border-left-color: var(--success); }
.status-bar.tone-info   { border-left-color: var(--accent); }
.status-bar.tone-warn   { border-left-color: var(--warn); }
.status-bar.tone-danger { border-left-color: var(--danger); }
.status-bar.tone-dim    { border-left-color: var(--text-faint); }

.status-bar-main {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-width: 0;
  flex: 1 1 320px;
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex: 0 0 auto;
  background: var(--text-faint);
}
.status-dot.dot-running  { background: var(--success); }
.status-dot.dot-planning {
  background: var(--accent);
  animation: status-breath 1.8s ease-in-out infinite;
}
.status-dot.dot-waiting  { background: var(--warn); }
.status-dot.dot-paused   { background: var(--danger); }
.status-dot.dot-unknown  { background: var(--text-faint); }

@keyframes status-breath {
  0%, 100% { opacity: 0.45; transform: scale(0.9); }
  50%      { opacity: 1;    transform: scale(1.1); }
}
@media (prefers-reduced-motion: reduce) {
  .status-dot.dot-planning { animation: none; }
}

.status-text { min-width: 0; }
.status-label {
  font-weight: 600;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.status-task-title,
.status-reason {
  font-weight: 400;
  color: var(--text-dim);
  margin-left: var(--space-1);
}
.status-sub {
  font-size: 12px;
  margin-top: 1px;
}

.status-cost {
  text-align: right;
  flex: 0 0 auto;
  padding: 0 var(--space-3);
  border-left: 1px solid var(--border);
  line-height: 1.2;
}
.cost-value {
  font-family: var(--font-mono);
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
}
.cost-meta { font-size: 11px; }

.status-controls {
  display: flex;
  gap: var(--space-2);
  flex: 0 0 auto;
}
.status-controls .ctl {
  font-size: 12px;
  padding: var(--space-1) var(--space-3);
  min-height: 32px;
}
.ctl-start  { background: var(--success); color: #0a0a0a; border-color: var(--success); }
.ctl-start:hover:not(:disabled) { filter: brightness(1.1); }
.ctl-pause  { }
.ctl-stop   { color: var(--danger); border-color: var(--danger); }
.ctl-stop:hover:not(:disabled) { background: var(--danger); color: #0a0a0a; }
.status-controls .ctl:disabled { opacity: 0.45; cursor: not-allowed; }

.status-error {
  width: 100%;
  color: var(--danger);
  font-size: 12px;
}

@media (max-width: 520px) {
  /* Tighter on phones: stack cost + controls below the main label. */
  .status-bar { padding: var(--space-2) var(--space-3); gap: var(--space-2); }
  .status-cost { padding-left: 0; border-left: none; text-align: left; }
}

/* ── Login Screen ─────────────────────────────────────────────────────── */

.login-screen {
  min-height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-5);
  background: radial-gradient(ellipse at top,
                              rgba(125, 211, 252, 0.08) 0%,
                              transparent 60%),
              var(--bg-0);
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-3);
  padding: var(--space-6);
  box-shadow: var(--shadow-2);
}

.login-brand {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: var(--space-1);
}
.login-tag {
  color: var(--text-dim);
  margin-bottom: var(--space-6);
  font-size: 14px;
}

.login-field { margin-bottom: var(--space-4); }
.login-field label {
  display: block;
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: var(--space-2);
}

.login-error {
  color: var(--danger);
  font-size: 13px;
  margin-top: var(--space-3);
  min-height: 1.2em;
}

/* ── Page Scaffolding ─────────────────────────────────────────────────── */

.page {
  padding: var(--space-4);
  max-width: 960px;
  margin: 0 auto;
  width: 100%;
}

.page-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: var(--space-5);
}
.page-title { font-size: 20px; font-weight: 700; margin: 0; }
.page-subtitle { color: var(--text-dim); font-size: 14px; }

.loading, .empty {
  color: var(--text-dim);
  text-align: center;
  padding: var(--space-6);
  font-size: 14px;
}

/* ── Utility Classes ──────────────────────────────────────────────────── */

.stack-2 > * + * { margin-top: var(--space-2); }
.stack-3 > * + * { margin-top: var(--space-3); }
.stack-4 > * + * { margin-top: var(--space-4); }
.stack-5 > * + * { margin-top: var(--space-5); }

.text-dim   { color: var(--text-dim); }
.text-faint { color: var(--text-faint); }
.text-warn  { color: var(--warn); }
.text-danger{ color: var(--danger); }
.text-success { color: var(--success); }
.text-mono  { font-family: var(--font-mono); }
.text-sm    { font-size: 13px; }
.text-xs    { font-size: 11px; }

.flex       { display: flex; }
.flex-col   { flex-direction: column; }
.gap-2      { gap: var(--space-2); }
.gap-3      { gap: var(--space-3); }
.gap-4      { gap: var(--space-4); }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }

.hidden-mobile  { display: none; }
.hidden-desktop { display: initial; }
@media (min-width: 960px) {
  .hidden-mobile  { display: initial; }
  .hidden-desktop { display: none; }
}

/* ── Chat Page ────────────────────────────────────────────────────────── */
/*
 * Mobile: full-bleed chat, thread switcher in a top bar, input docked
 * at the bottom.  Desktop: three-column — thread list / conversation /
 * side panel (Sprint 6 ships the first two; side panel is stubbed).
 */

.chat-shell {
  display: grid;
  /* ``flex: 1`` + ``min-height: 0`` lets the grid fill the remaining
   * space inside ``.shell-main`` after the sticky StatusBar claims its
   * share, while still allowing the ``conversation`` row to scroll
   * and the ``input`` row to stay docked at the bottom. */
  flex: 1 1 auto;
  min-height: 0;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header"
    "conversation"
    "input";
}

.chat-header {
  grid-area: header;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border);
  background: var(--bg-1);
  position: sticky;
  top: 0;
  z-index: 5;
}

.thread-switcher {
  flex: 1 1 auto;
  min-width: 0;
  background: transparent;
  border: none;
  padding: var(--space-2);
  text-align: left;
  color: var(--text);
  font-weight: 600;
  font-size: 15px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  overflow: hidden;
}
.thread-switcher:hover { background: var(--bg-2); }
.thread-switcher .caret { color: var(--text-dim); font-size: 11px; }
.thread-switcher .title-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.thread-new {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: var(--space-2) var(--space-3);
  font-size: 13px;
}
.thread-new:hover { color: var(--text); }

.thread-drawer {
  position: absolute;
  top: 100%;
  left: var(--space-4);
  right: var(--space-4);
  max-height: 60vh;
  overflow-y: auto;
  background: var(--bg-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-2);
  box-shadow: var(--shadow-2);
  z-index: 10;
  padding: var(--space-2);
}
.thread-drawer-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: var(--space-3);
  border-radius: var(--radius-1);
  cursor: pointer;
  color: var(--text);
  text-decoration: none;
}
.thread-drawer-item:hover { background: var(--bg-2); text-decoration: none; }
.thread-drawer-item.active { background: var(--bg-3); }
.thread-drawer-item .t-title { font-weight: 500; }
.thread-drawer-item .t-meta  { color: var(--text-faint); font-size: 12px; }

.conversation {
  grid-area: conversation;
  overflow-y: auto;
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.msg {
  max-width: min(720px, 88vw);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-2);
  background: var(--bg-1);
  border: 1px solid var(--border);
  line-height: 1.55;
  white-space: pre-wrap;
  word-wrap: break-word;
  position: relative;
}
.msg.user {
  align-self: flex-end;
  background: var(--accent);
  color: #0a0a0a;
  border-color: var(--accent);
}
.msg.assistant {
  align-self: flex-start;
  background: var(--bg-1);
}
.msg.partial { opacity: 0.65; font-style: italic; }
.msg.redirect { background: var(--bg-2); border-color: var(--warn); }
.msg.streaming::after {
  content: "▎";
  display: inline-block;
  margin-left: 2px;
  color: var(--accent);
  animation: blink 1s steps(2, start) infinite;
}
@keyframes blink { to { visibility: hidden; } }

.msg .meta-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  margin-bottom: var(--space-2);
  font-size: 11px;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.msg.user .meta-row { color: rgba(10, 10, 10, 0.7); }

.msg .pin-btn {
  background: transparent;
  border: none;
  color: var(--text-faint);
  padding: 2px 4px;
  font-size: 12px;
  border-radius: 4px;
}
.msg .pin-btn:hover { color: var(--text); background: rgba(255,255,255,0.05); }
.msg.pinned::before {
  content: "📌";
  position: absolute;
  top: -6px;
  left: -6px;
  background: var(--bg-0);
  padding: 2px 4px;
  border-radius: 50%;
  font-size: 10px;
  border: 1px solid var(--border);
}

.msg-markdown code {
  background: var(--bg-2);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.92em;
}
.msg-markdown pre code { background: none; padding: 0; }
.msg-markdown pre { margin: var(--space-2) 0; }
.msg-markdown ul, .msg-markdown ol { padding-left: 1.5em; }
.msg-markdown a { color: var(--accent); word-break: break-all; }

/* Tool indicators — inline, ambient, subtle. */
.tool-strip {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  margin: var(--space-2) 0;
}
.tool-indicator {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: 13px;
  color: var(--text-dim);
  padding: 2px 8px;
  border-radius: var(--radius-1);
  background: var(--bg-2);
  border: 1px solid var(--border);
  cursor: pointer;
  user-select: none;
}
.tool-indicator.running .spinner {
  width: 10px; height: 10px;
  border: 2px solid var(--text-faint);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 600ms linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.tool-indicator.done::before {
  content: "✓";
  color: var(--success);
  font-weight: 700;
}
.tool-indicator .tool-detail {
  margin-top: var(--space-2);
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text-dim);
  white-space: pre-wrap;
  padding: var(--space-2);
  background: var(--bg-0);
  border-radius: var(--radius-1);
  max-height: 240px;
  overflow-y: auto;
  width: 100%;
}

.chat-input-bar {
  grid-area: input;
  padding: var(--space-3) var(--space-4);
  border-top: 1px solid var(--border);
  background: var(--bg-1);
  display: flex;
  gap: var(--space-3);
  align-items: flex-end;
}
.chat-input-bar textarea {
  flex: 1;
  resize: none;
  min-height: 44px;
  max-height: 180px;
  overflow-y: auto;
  padding: var(--space-3);
  border-radius: var(--radius-2);
}
.chat-actions {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}
.chat-send, .chat-stop {
  min-width: 72px;
}
.chat-stop  { border-color: var(--warn); color: var(--warn); }
.chat-stop:hover:not(:disabled) {
  background: rgba(251, 191, 36, 0.12); border-color: var(--warn);
}

.redirect-hint {
  padding: var(--space-2) var(--space-4);
  font-size: 12px;
  color: var(--warn);
  background: rgba(251, 191, 36, 0.08);
  border-top: 1px solid rgba(251, 191, 36, 0.25);
}

.redirect-mark {
  align-self: center;
  font-size: 11px;
  color: var(--warn);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding: 2px 8px;
  border-top: 1px dashed rgba(251, 191, 36, 0.4);
  border-bottom: 1px dashed rgba(251, 191, 36, 0.4);
  width: 100%;
  max-width: 480px;
  text-align: center;
}

/* ── Dashboard ────────────────────────────────────────────────────────── */

.dashboard {
  display: grid;
  gap: var(--space-4);
  padding: var(--space-4);
  max-width: 960px;
  margin: 0 auto;
  width: 100%;
  grid-template-columns: 1fr;
}

@media (min-width: 960px) {
  .dashboard { grid-template-columns: repeat(2, 1fr); }
  /* The decisions card always spans the full width — it's highest-priority. */
  .dash-card.priority-1 { grid-column: 1 / -1; }
  .dash-card.span-2     { grid-column: 1 / -1; }
}

.dash-card {
  background: var(--bg-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-2);
  padding: var(--space-4);
}
.dash-card.priority-1 { border-color: var(--warn); }
.dash-card.priority-1 .card-title { color: var(--warn); }

.card-title {
  margin: 0 0 var(--space-3);
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
}

.card-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: var(--space-3) 0;
  border-bottom: 1px solid var(--border);
  gap: var(--space-3);
}
.card-row:last-child { border-bottom: none; }
.card-row-title { font-weight: 500; }
.card-row-meta { color: var(--text-faint); font-size: 12px; }

.card-actions { display: flex; gap: var(--space-2); flex-shrink: 0; }

.action-approve { background: var(--success); color: #0a0a0a; border-color: var(--success); font-weight: 600; }
.action-approve:hover:not(:disabled) { background: #22c55e; border-color: #22c55e; }
.action-deny    { background: transparent; color: var(--danger); border-color: var(--danger); font-weight: 600; }
.action-deny:hover:not(:disabled) { background: rgba(248, 113, 113, 0.1); }
.action-read    { background: transparent; color: var(--text-dim); border-color: var(--border); font-size: 12px; }

.card-current-activity {
  display: flex;
  gap: var(--space-3);
  align-items: center;
}
.card-current-activity .pulse {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 0 0 rgba(74, 222, 128, 0.7);
  animation: pulse 1.6s infinite;
  flex-shrink: 0;
}
.card-current-activity.idle .pulse {
  background: var(--text-faint);
  animation: none;
  box-shadow: none;
}
@keyframes pulse {
  0%   { box-shadow: 0 0 0 0 rgba(74, 222, 128, 0.7); }
  70%  { box-shadow: 0 0 0 12px rgba(74, 222, 128, 0); }
  100% { box-shadow: 0 0 0 0 rgba(74, 222, 128, 0); }
}

/* ── Budget Bar ───────────────────────────────────────────────────────── */

.budget-bar {
  margin-top: var(--space-2);
}
.budget-bar-track {
  position: relative;
  height: 8px;
  background: var(--bg-2);
  border-radius: 999px;
  overflow: hidden;
}
.budget-bar-fill {
  position: absolute;
  inset: 0 auto 0 0;
  background: var(--accent);
  transition: width var(--dur-med) ease;
}
.budget-bar-fill.warn    { background: var(--warn); }
.budget-bar-fill.danger  { background: var(--danger); }

.budget-bar-meta {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-faint);
  margin-top: var(--space-2);
}

/* ── Reports Viewer ───────────────────────────────────────────────────── */

.reports-layout {
  display: grid;
  /* See ``.chat-shell`` note — flex child of ``.shell-main`` so the
   * sticky StatusBar shares the viewport cleanly. */
  flex: 1 1 auto;
  min-height: 0;
  gap: var(--space-4);
  padding: var(--space-4);
  grid-template-columns: 1fr;
}

@media (min-width: 960px) {
  .reports-layout {
    grid-template-columns: 320px 1fr;
    max-width: 1200px;
    margin: 0 auto;
    width: 100%;
  }
}

.reports-list { display: flex; flex-direction: column; gap: var(--space-2); }
.report-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: var(--space-3);
  background: var(--bg-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-1);
  cursor: pointer;
}
.report-item:hover { border-color: var(--text-faint); }
.report-item.active { background: var(--bg-2); border-color: var(--accent); }
.report-item.unread { border-left: 3px solid var(--warn); }
.report-item-title  { font-weight: 500; }
.report-item-meta   { color: var(--text-faint); font-size: 12px; }

.report-detail {
  background: var(--bg-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-2);
  padding: var(--space-5);
  overflow-y: auto;
  max-height: calc(100vh - 180px);
}

.report-body {
  margin-top: var(--space-4);
  line-height: 1.6;
}

/* Deny-reason modal. */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: var(--space-4);
}
.modal {
  background: var(--bg-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-2);
  padding: var(--space-5);
  max-width: 480px;
  width: 100%;
}
.modal-title { font-size: 16px; font-weight: 600; margin: 0 0 var(--space-3); }
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-3);
  margin-top: var(--space-4);
}

/* ── Activity feed (Sprint 2) ───────────────────────────────────────────
 *
 * Live push-feed of task/tool/memory/operator events.  Designed to fit
 * inside a ``.dash-card.span-2`` so it stretches full width on the
 * dashboard while keeping a comfortable line length for the event text.
 * The left tone border gives scanability at a glance — green for good,
 * red for failure, yellow for warn, grey for muted background activity.
 */

.activity-feed { display: flex; flex-direction: column; gap: var(--space-3); }

.activity-feed-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: var(--space-3);
}
.activity-feed-title {
  margin: 0;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
}
.feed-status { font-size: 11px; letter-spacing: 0.02em; }
.feed-status.feed-ok  { color: var(--success); }
.feed-status.feed-off { color: var(--text-faint); }

.activity-feed-empty {
  padding: var(--space-4) var(--space-2);
  font-size: 13px;
}

.activity-feed-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  max-height: 380px;
  overflow-y: auto;
  /* Fade the bottom edge so cut-off lines feel intentional. */
  mask-image: linear-gradient(to bottom, #000 0, #000 calc(100% - 24px), transparent 100%);
  -webkit-mask-image: linear-gradient(to bottom, #000 0, #000 calc(100% - 24px), transparent 100%);
}

.activity-item {
  display: grid;
  grid-template-columns: 18px 1fr auto;
  align-items: baseline;
  gap: var(--space-3);
  padding: 6px var(--space-2);
  font-size: 13px;
  line-height: 1.45;
  border-left: 2px solid var(--border);
}
.activity-item + .activity-item { border-top: 1px dashed var(--border); }
.activity-item.tone-ok     { border-left-color: var(--success); }
.activity-item.tone-info   { border-left-color: var(--accent, #60a5fa); }
.activity-item.tone-warn   { border-left-color: var(--warn); }
.activity-item.tone-danger { border-left-color: var(--danger); }
.activity-item.tone-muted  { border-left-color: var(--border); }

.activity-icon {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
  color: var(--text-dim);
  text-align: center;
}
.activity-text   { overflow-wrap: anywhere; }
.activity-time   {
  font-variant-numeric: tabular-nums;
  font-size: 11px;
  white-space: nowrap;
}

/* ── Sprint 3A: Home = chat + rails ────────────────────────────────────
 *
 * Three-column layout for the chat home:
 *   - ThreadRail   (left)
 *   - .chat-shell  (center, existing grid)
 *   - PulseRail    (right)
 *
 * On narrow viewports both rails collapse to toggles so the chat
 * always has room to breathe.  The .home-shell occupies the full
 * height remaining after the sticky StatusBar.
 */

.home-shell {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: var(--thread-rail-w, 240px) 1fr var(--pulse-rail-w, 340px);
  gap: 0;
  width: 100%;
  /* Allow grid children to shrink below their intrinsic min-content. */
  min-width: 0;
}

/* Thread rail (left) */
.thread-rail {
  border-right: 1px solid var(--border);
  background: var(--bg-1);
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}
.thread-rail.is-collapsed { --thread-rail-w: 40px; }
.thread-rail-header {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--border);
}
.rail-title {
  margin: 0;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-dim);
  flex: 1 1 auto;
}
.rail-collapse-btn,
.rail-new-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  min-width: 28px;
  height: 28px;
  padding: 0 var(--space-2);
  border-radius: var(--radius-1);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
}
.rail-collapse-btn:hover,
.rail-new-btn:hover { color: var(--text); background: var(--bg-2); }
.thread-rail.is-collapsed .thread-rail-header { justify-content: center; padding: var(--space-2) 0; }

.thread-rail-body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: var(--space-2);
}
.rail-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.rail-item {
  display: block;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-1);
  color: var(--text);
  text-decoration: none;
  transition: background var(--dur-fast) ease;
}
.rail-item:hover { background: var(--bg-2); }
.rail-item.active { background: var(--bg-2); outline: 1px solid var(--border); }
.rail-item-title {
  display: flex; align-items: center; gap: var(--space-1);
  font-size: 13px; font-weight: 600;
  overflow: hidden;
}
.rail-title-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rail-pin { font-size: 11px; }
.rail-item-meta { font-size: 11px; margin-top: 2px; }
.rail-empty { padding: var(--space-3); text-align: center; }

/* Mobile toggle / backdrop — hidden on desktop */
.thread-rail-mobile-toggle {
  display: none;
  position: fixed;
  left: var(--space-3);
  bottom: var(--space-4);
  z-index: 30;
  background: var(--bg-2);
  color: var(--text);
  border: 1px solid var(--border);
  width: 40px; height: 40px;
  border-radius: 50%;
  font-size: 18px;
  cursor: pointer;
  box-shadow: var(--shadow-2);
}
.thread-rail-backdrop {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 25;
}

/* Pulse rail (right) */
.pulse-rail {
  border-left: 1px solid var(--border);
  background: var(--bg-1);
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}
.pulse-rail.is-collapsed { --pulse-rail-w: 40px; }
.pulse-rail-header {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--border);
}
.pulse-rail.is-collapsed .pulse-rail-header { justify-content: center; padding: var(--space-2) 0; }
.pulse-rail-tabs {
  display: flex;
  gap: 2px;
  flex: 1 1 auto;
  min-width: 0;
  overflow-x: auto;
}
.pulse-tab {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 11px;
  font-weight: 600;
  padding: 4px var(--space-2);
  border-radius: var(--radius-1);
  cursor: pointer;
  white-space: nowrap;
  letter-spacing: 0.02em;
}
.pulse-tab:hover { color: var(--text); }
.pulse-tab.active {
  color: var(--text);
  background: var(--bg-2);
  border-color: var(--accent);
}
.pulse-rail-body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: var(--space-3);
}
.pulse-empty {
  padding: var(--space-3);
  line-height: 1.5;
}
.pulse-empty p + p { margin-top: var(--space-2); }
.pulse-empty code {
  background: var(--bg-2);
  padding: 1px 4px;
  border-radius: 3px;
  font-size: 11px;
}

/* Inline system-event pill inside the conversation (chat.inject surface) */
.msg-system {
  display: grid;
  grid-template-columns: 22px 1fr;
  gap: var(--space-2);
  align-items: baseline;
  margin: var(--space-2) 0;
  padding: 6px var(--space-3);
  font-size: 12px;
  line-height: 1.4;
  color: var(--text-dim);
  background: var(--bg-2);
  border-left: 2px solid var(--border);
  border-radius: 0 var(--radius-1) var(--radius-1) 0;
}
.msg-system.tone-ok     { border-left-color: var(--success); }
.msg-system.tone-info   { border-left-color: var(--accent); }
.msg-system.tone-warn   { border-left-color: var(--warn); }
.msg-system.tone-danger { border-left-color: var(--danger); }
.msg-system .sys-icon { text-align: center; font-size: 12px; }
.msg-system .sys-text { overflow-wrap: anywhere; }

/* Chat header slim — thread switcher moved to the rail, so the header
 * just shows the active thread title now. */
.chat-header .chat-title {
  font-weight: 600;
  font-size: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1 1 auto;
  min-width: 0;
}

/* ── Sprint 3B: Agents page ───────────────────────────────────────────── */

.agents-page { padding: var(--space-4); display: flex; flex-direction: column; gap: var(--space-3); }
.agents-page .page-header {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: var(--space-3);
}
.agents-page .page-title { margin: 0; font-size: 22px; font-weight: 700; }
.agents-page .page-header-meta { letter-spacing: 0.01em; }

.agent-filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  padding: var(--space-2) 0;
  border-bottom: 1px solid var(--border);
}
.filter-chip {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 12px;
  font-weight: 600;
  padding: 5px var(--space-3);
  border-radius: 999px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: color var(--dur-fast), border-color var(--dur-fast);
}
.filter-chip:hover { color: var(--text); }
.filter-chip.active {
  color: var(--text);
  background: var(--bg-2);
  border-color: var(--accent);
}
.filter-count {
  font-size: 11px;
  background: var(--bg-2);
  padding: 1px 6px;
  border-radius: 10px;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}
.filter-chip.active .filter-count { background: var(--bg-0); color: var(--text); }

.agents-empty {
  padding: var(--space-5) var(--space-4);
  text-align: center;
  border: 1px dashed var(--border);
  border-radius: var(--radius-2);
}

/* Responsive card grid: auto-fit one to three cards per row, never
 * wider than 480px each so long descriptions don't sprawl. */
.agent-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: var(--space-3);
}

.agent-card {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  background: var(--bg-1);
  border: 1px solid var(--border);
  border-left: 3px solid var(--border);
  border-radius: var(--radius-2);
  padding: var(--space-3) var(--space-4);
  min-width: 0;
}
.agent-card.tone-ok     { border-left-color: var(--success); }
.agent-card.tone-info   { border-left-color: var(--accent); }
.agent-card.tone-warn   { border-left-color: var(--warn); }
.agent-card.tone-danger { border-left-color: var(--danger); }
.agent-card.tone-muted  { border-left-color: var(--border); }
.agent-card.is-paused   { background: var(--bg-2); }
.agent-card.is-terminated { opacity: 0.7; }

.agent-card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-2);
}
.agent-name { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.agent-title {
  font-size: 16px;
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.agent-type {
  font-size: 11px;
  color: var(--text-dim);
  text-transform: lowercase;
  letter-spacing: 0.02em;
}
.agent-type code {
  background: var(--bg-2);
  padding: 0 4px;
  border-radius: 3px;
  font-size: 11px;
}

.agent-status-badge {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--bg-2);
  color: var(--text-dim);
  white-space: nowrap;
  flex-shrink: 0;
}
.agent-status-badge.status-running,
.agent-status-badge.status-busy     { color: var(--success); }
.agent-status-badge.status-paused,
.agent-status-badge.status-waiting  { color: var(--warn); }
.agent-status-badge.status-failed   { color: var(--danger); }
.agent-status-badge.status-planning,
.agent-status-badge.status-starting { color: var(--accent); }

.agent-body { display: flex; flex-direction: column; gap: var(--space-2); }
.agent-description { font-size: 13px; line-height: 1.4; color: var(--text); }
.agent-reason { font-size: 12px; line-height: 1.4; }

.agent-current-task {
  background: var(--bg-2);
  border-radius: var(--radius-1);
  padding: var(--space-2) var(--space-3);
  font-size: 12px;
  line-height: 1.4;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.agent-current-task .label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  font-weight: 700;
}
.agent-current-task .value { overflow-wrap: anywhere; }

.agent-meta {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px var(--space-3);
  margin: 0;
  font-size: 12px;
}
.agent-meta dt {
  color: var(--text-dim);
  font-weight: 600;
  white-space: nowrap;
}
.agent-meta dd { margin: 0; overflow-wrap: anywhere; }

.agent-tool-chips { display: flex; flex-wrap: wrap; gap: 4px; }
.tool-chip {
  background: var(--bg-2);
  padding: 1px 6px;
  border-radius: 3px;
  font-size: 11px;
}
.tool-chip-more {
  font-size: 11px;
  color: var(--text-dim);
  padding: 1px 4px;
}

.budget-bar-inline {
  display: inline-block;
  width: 80px;
  height: 6px;
  background: var(--bg-2);
  border-radius: 3px;
  overflow: hidden;
  vertical-align: middle;
  margin-right: 8px;
}
.budget-bar-fill {
  display: block;
  height: 100%;
  background: var(--accent);
  transition: width var(--dur-med) ease;
}
.budget-text {
  font-variant-numeric: tabular-nums;
  font-size: 11px;
  color: var(--text-dim);
}

.agent-actions {
  display: flex;
  gap: var(--space-2);
  flex-wrap: wrap;
  border-top: 1px solid var(--border);
  padding-top: var(--space-2);
}
.agent-actions .button-primary,
.agent-actions .button-secondary,
.agent-actions .button-danger {
  font-size: 12px;
  padding: 5px var(--space-3);
  border-radius: var(--radius-1);
  cursor: pointer;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  transition: background var(--dur-fast);
}
.agent-actions .button-primary {
  background: var(--accent);
  color: var(--bg-0);
  border-color: var(--accent);
  font-weight: 600;
}
.agent-actions .button-primary:hover:not(:disabled) { filter: brightness(1.1); }
.agent-actions .button-secondary:hover:not(:disabled) { background: var(--bg-2); }
.agent-actions .button-danger {
  background: var(--danger);
  color: #fff;
  border-color: var(--danger);
  font-weight: 600;
}
.agent-actions .button-danger:hover:not(:disabled) { filter: brightness(1.1); }
.agent-actions button:disabled { opacity: 0.5; cursor: not-allowed; }
.agent-actions .button-link {
  background: transparent;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 5px var(--space-2);
  font-size: 12px;
  text-decoration: underline;
}
.agent-actions .button-link:hover { color: var(--text); }

.alert {
  padding: var(--space-3);
  border-radius: var(--radius-2);
  border: 1px solid var(--border);
  font-size: 13px;
  line-height: 1.4;
}
.alert.alert-danger {
  background: color-mix(in srgb, var(--danger) 10%, var(--bg-1));
  border-color: var(--danger);
  color: var(--danger);
}

/* Page stubs — Agents / Tasks / Runs / Alerts / Tokens until they ship */
.page-stub .stub-card {
  border: 1px dashed var(--border);
  border-radius: var(--radius-2);
  background: var(--bg-1);
  padding: var(--space-4);
  max-width: 560px;
}
.page-stub .stub-hint { font-size: 15px; margin: 0 0 var(--space-2) 0; }
.page-stub .stub-meta { margin: 0; }

/* Responsive breakpoints for the three-column home */
@media (max-width: 1180px) {
  .home-shell { grid-template-columns: var(--thread-rail-w, 220px) 1fr var(--pulse-rail-w, 40px); }
  .pulse-rail { --pulse-rail-w: 40px; }
  .pulse-rail:not(.is-collapsed) { --pulse-rail-w: 320px; }
}

@media (max-width: 900px) {
  .home-shell { grid-template-columns: 1fr; }
  .thread-rail {
    position: fixed;
    top: 0; bottom: 0; left: 0;
    width: 280px;
    max-width: 80vw;
    z-index: 26;
    transform: translateX(-110%);
    transition: transform var(--dur-med) ease;
    border-right: 1px solid var(--border);
  }
  .thread-rail.is-mobile-open { transform: translateX(0); }
  .thread-rail.is-collapsed { display: none; }
  .thread-rail-mobile-toggle { display: inline-flex; align-items: center; justify-content: center; }
  .thread-rail.is-mobile-open ~ .thread-rail-backdrop,
  .thread-rail-backdrop { display: block; }
  .pulse-rail {
    /* On phones, the pulse rail collapses to a thin strip anchored to
     * the right.  A future sprint turns this into a bottom sheet. */
    border-top: 1px solid var(--border);
    border-left: none;
    max-height: 44px;
  }
  .pulse-rail.is-collapsed { max-height: 44px; }
  .pulse-rail:not(.is-collapsed) { max-height: 50vh; }
}
