/* ============================================================
   Atlas AI Studio — full-page chat layout (OpenWebUI-style)
   Depends on: styles.css (design tokens), ai.css (msg / ai-step /
   ai-result-card / ai-metrics / stat / ai-regime-badge classes)
   ============================================================ */

/* Prevent body scroll — the columns scroll independently */
body { overflow: hidden; }

/* ── Shell / layout ────────────────────────────────────────── */
.studio-shell {
  display: grid;
  grid-template-rows: auto 1fr;
  height: 100vh;
  height: 100dvh;
}

/* Studio-specific surfaces — kill the grey-on-grey wash in LIGHT mode:
   canvas = pure white, AI bubbles = soft neutral card with a hairline border.
   Dark mode keeps the existing deep palette. */
:root {
  --studio-canvas: #ffffff;
  --studio-bubble: #f4f5f7;
}
[data-theme="dark"] {
  --studio-canvas: var(--bg-soft);
  --studio-bubble: var(--panel-2);
}

/* Override sticky so the header is part of the grid row */
.studio-shell .site-header { position: relative; }

.studio-main {
  display: grid;
  grid-template-columns: 1fr 300px;
  overflow: hidden;
  min-height: 0;
}

/* ── Chat column ───────────────────────────────────────────── */
.studio-chat-col {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
  border-right: 1px solid var(--line);
}

/* ── Pipeline strip ────────────────────────────────────────── */
.studio-pipeline {
  flex-shrink: 0;
  padding: 0.55rem 1rem;
  background: var(--panel-2);
  border-bottom: 1px solid var(--line);
}

/* ── Thread (scrollable message area) ─────────────────────── */
.studio-thread {
  flex: 1;
  overflow-y: auto;
  padding: 1.4rem 1.5rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
  min-height: 0;
  scroll-behavior: smooth;
  background: var(--studio-canvas);
}

/* Message entrance animation */
@keyframes fade-up {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.studio-thread .msg {
  animation: fade-up 0.22s ease both;
}

.studio-thread .msg.user {
  align-self: flex-end;
  max-width: min(76%, 580px);
}

.studio-thread .msg.ai {
  align-self: flex-start;
  max-width: min(82%, 660px);
  background: var(--studio-bubble);
  border: 1px solid var(--line);
}

/* Inline code inside AI bubbles */
.studio-thread .msg.ai code {
  font-family: var(--font-mono);
  font-size: 0.83em;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0.1em 0.35em;
}

/* ── Empty state ───────────────────────────────────────────── */
.studio-empty {
  margin: auto;
  text-align: center;
  padding: 2.5rem 1.5rem;
  max-width: 440px;
}
.studio-empty-icon {
  font-size: 2.8rem;
  margin-bottom: 0.85rem;
  display: block;
}
.studio-empty h2 {
  font-size: 1.5rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  margin-bottom: 0.5rem;
}
.studio-empty p {
  color: var(--ink-soft);
  font-size: 0.94rem;
  line-height: 1.58;
}
.studio-empty .eyebrow {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  font-family: var(--font-mono);
  font-size: 0.7rem;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--accent);
  font-weight: 600;
  margin-bottom: 0.9rem;
}
.studio-empty .eyebrow::before {
  content: "";
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
  animation: pulse 2.4s ease-in-out infinite;
}

/* ── Reconnecting hint ─────────────────────────────────────── */
.studio-reconnect {
  align-self: center;
  font-size: 0.73rem;
  color: var(--warn);
  font-family: var(--font-mono);
  padding: 0.22rem 0.7rem;
  background: rgba(217,119,6,.1);
  border: 1px solid rgba(217,119,6,.3);
  border-radius: 999px;
  animation: fade-up 0.2s ease;
}

/* ── Confirmed chip in thread ──────────────────────────────── */
.confirmed-chip {
  align-self: flex-start;
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.72rem;
  font-weight: 600;
  font-family: var(--font-mono);
  color: var(--up);
  background: var(--up-soft);
  border: 1px solid color-mix(in srgb, var(--up) 30%, transparent);
  padding: 0.2rem 0.65rem;
  border-radius: 999px;
  animation: fade-up 0.2s ease;
}

/* ── Result card in chat thread ────────────────────────────── */
.studio-result-wrap {
  align-self: flex-start;
  width: min(100%, 600px);
  animation: fade-up 0.25s ease;
}
.studio-result-wrap .ai-result-card { margin-top: 0; }

/* ── Input wrap (bottom of chat col) ──────────────────────── */
.studio-input-wrap {
  flex-shrink: 0;
  border-top: 1px solid var(--line);
  background: var(--panel);
}

.studio-chips {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
  padding: 0.55rem 0.75rem;
  border-bottom: 1px solid var(--line);
  background: var(--panel-2);
}

.studio-input-row {
  display: flex;
  gap: 0.55rem;
  padding: 0.65rem 0.75rem;
  background: var(--panel);
  border-top: none; /* override ai.css */
}

.studio-input-row textarea {
  flex: 1;
  resize: none;
  min-height: 40px;
  max-height: 160px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--panel-2);
  color: var(--ink);
  padding: 0.55rem 0.8rem;
  font: inherit;
  font-size: 0.9rem;
  line-height: 1.5;
  transition: border-color 0.15s, box-shadow 0.15s;
  overflow-y: auto;
}
.studio-input-row textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.studio-input-row textarea::placeholder { color: var(--muted); }
.studio-input-row .ai-send {
  align-self: flex-end;
  height: 40px;
  padding: 0 1.1rem;
  white-space: nowrap;
}

/* ── Run backtest row (after confirmation) ─────────────────── */
.studio-run-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  padding: 0.5rem 0.75rem;
  border-top: 1px solid var(--line);
  background: var(--panel-2);
}

.studio-confirm-chip {
  font-size: 0.74rem;
  font-weight: 600;
  color: var(--up);
  font-family: var(--font-mono);
  background: var(--up-soft);
  border: 1px solid color-mix(in srgb, var(--up) 30%, transparent);
  padding: 0.2rem 0.65rem;
  border-radius: 999px;
}

.studio-run-btn {
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  font-size: 0.84rem;
  font-weight: 700;
  padding: 0.38rem 1.1rem;
  border-radius: 9px;
  cursor: pointer;
  transition: filter 0.15s, box-shadow 0.15s;
  box-shadow: 0 2px 8px var(--accent-soft);
}
.studio-run-btn:hover { filter: brightness(1.08); box-shadow: 0 3px 14px var(--accent-soft); }
.studio-run-btn:disabled { opacity: 0.5; cursor: not-allowed; filter: none; box-shadow: none; }

/* ── Sidebar ───────────────────────────────────────────────── */
.studio-sidebar {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
  background: var(--panel-2);
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 0.9rem;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
  background: var(--panel);
}

.sidebar-title {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--ink-soft);
  font-family: var(--font-mono);
}

.sidebar-refresh {
  border: 1px solid var(--line-strong);
  background: var(--panel-2);
  color: var(--ink-soft);
  width: 28px; height: 28px;
  border-radius: 7px;
  cursor: pointer;
  font-size: 1rem;
  display: grid;
  place-items: center;
  transition: color 0.15s, border-color 0.15s;
}
.sidebar-refresh:hover { color: var(--accent); border-color: var(--accent); }

.sidebar-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.55rem;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.sidebar-loading,
.sidebar-empty {
  font-size: 0.78rem;
  color: var(--muted);
  text-align: center;
  padding: 1.75rem 1rem;
  font-family: var(--font-mono);
  line-height: 1.6;
}

/* ── Job cards ─────────────────────────────────────────────── */
.job-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.6rem 0.75rem;
  cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s, background 0.15s;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}
.job-card:hover { border-color: var(--accent); box-shadow: var(--shadow); }
.job-card.active { border-color: var(--accent); background: var(--accent-soft); }
.job-card.non-clickable { cursor: default; }
.job-card.non-clickable:hover { border-color: var(--line); box-shadow: none; }

.job-card-head {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.job-status-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
}
.job-status-dot.done    { background: var(--up); }
.job-status-dot.error   { background: var(--down); }
.job-status-dot.running,
.job-status-dot.queued  {
  background: var(--accent);
  animation: pulse 1.2s ease-in-out infinite;
}

.job-family {
  font-family: var(--font-mono);
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--ink-soft);
  font-weight: 600;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.job-date {
  font-family: var(--font-mono);
  font-size: 0.62rem;
  color: var(--muted);
  white-space: nowrap;
}

.job-trades {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  color: var(--muted);
}

.job-error-label {
  font-size: 0.67rem;
  color: var(--down);
  font-family: var(--font-mono);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ── Toast ─────────────────────────────────────────────────── */
.toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel);
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  padding: 0.55rem 1.2rem;
  font-size: 0.84rem;
  font-weight: 600;
  box-shadow: var(--shadow-lift);
  z-index: 500;
  white-space: nowrap;
  animation: fade-up 0.2s ease;
}
.toast-error  { border-color: var(--down); color: var(--down); background: var(--down-soft); }
.toast-success { border-color: var(--up);  color: var(--up);   background: var(--up-soft); }

/* ── Responsive ────────────────────────────────────────────── */
@media (max-width: 800px) {
  .studio-main {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 220px;
  }
  .studio-sidebar {
    border-left: none;
    border-top: 1px solid var(--line);
  }
}

/* ── Rendered markdown inside chat bubbles ───────────────────── */
#studioThread .msg.ai p { margin: 0.4rem 0; }
#studioThread .msg.ai h1, #studioThread .msg.ai h2,
#studioThread .msg.ai h3, #studioThread .msg.ai h4,
#studioThread .msg.ai h5, #studioThread .msg.ai h6 {
  margin: 0.9rem 0 0.4rem;
  line-height: 1.25;
  color: var(--ink);
}
#studioThread .msg.ai h1 { font-size: 1.25rem; }
#studioThread .msg.ai h2 { font-size: 1.15rem; }
#studioThread .msg.ai h3 { font-size: 1.05rem; }
#studioThread .msg.ai h4 { font-size: 1rem; }
#studioThread .msg.ai ul, #studioThread .msg.ai ol {
  margin: 0.45rem 0;
  padding-left: 1.35rem;
}
#studioThread .msg.ai li { margin: 0.2rem 0; }
#studioThread .msg.ai table {
  border-collapse: collapse;
  margin: 0.6rem 0;
  width: 100%;
  font-size: 0.85rem;
  display: block;
  overflow-x: auto;
}
#studioThread .msg.ai th, #studioThread .msg.ai td {
  border: 1px solid var(--line);
  padding: 0.35rem 0.55rem;
  text-align: left;
  white-space: normal;
  word-break: break-word;
  max-width: 340px;
}
#studioThread .msg.ai th { background: var(--panel-2); font-weight: 600; }
#studioThread .msg.ai code {
  background: var(--panel-2);
  border-radius: 4px;
  padding: 0.08rem 0.32rem;
  font-family: var(--font-mono);
  font-size: 0.85em;
}
#studioThread .msg.ai pre {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.7rem 0.85rem;
  overflow-x: auto;
  margin: 0.6rem 0;
  line-height: 1.45;
}
#studioThread .msg.ai pre code { background: none; padding: 0; border-radius: 0; }
#studioThread .msg.ai blockquote {
  border-left: 3px solid var(--accent);
  margin: 0.5rem 0;
  padding: 0.15rem 0.85rem;
  color: var(--ink-soft);
}
#studioThread .msg.ai a { color: var(--accent); text-decoration: none; }
#studioThread .msg.ai a:hover { text-decoration: underline; }
#studioThread .msg.ai hr { border: 0; border-top: 1px solid var(--line); margin: 0.7rem 0; }
#studioThread .msg.ai del { color: var(--muted); }

/* ── Live worker panel (progress + 3-line status) ─────────── */
.studio-worker { padding: 0.7rem 0.9rem; }
.studio-worker .wk-row {
  display: flex; justify-content: space-between; align-items: center;
  font-size: 0.82rem; font-weight: 600; margin-bottom: 0.45rem;
}
.studio-worker .wk-elapsed {
  font-family: var(--font-mono); font-size: 0.72rem; color: var(--muted);
}
.wk-bar {
  position: relative; height: 8px; border-radius: 99px;
  background: var(--panel-2); overflow: hidden; margin-bottom: 0.45rem;
}
.wk-fill {
  height: 100%; width: 0%; border-radius: 99px;
  background: linear-gradient(90deg, var(--accent), color-mix(in srgb, var(--accent) 60%, #fff));
  transition: width 0.6s ease;
}
.wk-fill.indeterminate {
  animation: wk-slide 1.4s ease-in-out infinite;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  width: 40% !important;
}
@keyframes wk-slide {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(350%); }
}
.wk-pct {
  font-family: var(--font-mono); font-size: 0.68rem; color: var(--muted);
}
.studio-worker .wk-line {
  font-family: var(--font-mono); font-size: 0.68rem; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.studio-worker.wk-failed { border-color: color-mix(in srgb, var(--down) 40%, transparent); }
.studio-worker.wk-failed .wk-line { color: var(--down); }
.wk-hint { font-size: 0.78rem; opacity: 0.85; }

/* ── Message timestamps ───────────────────────────────────── */
.studio-thread .msg .msg-meta {
  display: block; text-align: right;
  font-family: var(--font-mono); font-size: 0.58rem;
  color: var(--muted); opacity: 0.75; margin-top: 0.3rem;
}

/* ── Equity curve in result card ──────────────────────────── */
.ai-equity { margin-top: 0.7rem; }
.ai-equity-head {
  display: flex; justify-content: space-between; align-items: baseline;
  gap: 0.5rem; flex-wrap: wrap; margin-bottom: 0.35rem;
  font-size: 0.78rem; color: var(--muted);
}
.ai-capital b { color: var(--ink); }
.ai-capital i { opacity: 0.7; font-size: 0.72rem; }
.ai-equity-svg { width: 100%; height: 110px; display: block; }
.ai-equity-svg .eq-up   { stroke: var(--up, #16a34a); }
.ai-equity-svg .eq-down { stroke: var(--down, #dc2626); }
.ai-equity-svg .eq-zero { stroke: var(--line-strong); stroke-dasharray: 4 4; stroke-width: 1; }

/* Backtest id chip inside chat */
.msg code { font-family: var(--font-mono); font-size: 0.8em; }
