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

:root {
  --bg: #0f0f13;
  --bg2: #1a1a26;
  --bg3: #1e1e2e;
  --border: #2a2a3a;
  --text: #e8e8f0;
  --muted: #6b6b8a;
  --accent: #4c6ef5;
  --accent-hover: #3b5bdb;
  --green: #2b8a3e;
  --green-text: #b2f2bb;
  --red: #862e2e;
  --red-text: #ffa8a8;
  --blue: #1864ab;
  --blue-text: #a5d8ff;
  --yellow: #7a5c00;
  --yellow-text: #ffe066;
  --code: #c3e88d;
}

html, body { height: 100%; }
body {
  display: flex; flex-direction: column;
  background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, sans-serif;
  font-size: 14px; line-height: 1.5;
}

/* ── Nav ──────────────────────────────────────────────────────── */
nav {
  display: flex; align-items: center; gap: 4px;
  padding: 10px 16px; border-bottom: 1px solid var(--border);
  flex-shrink: 0; background: var(--bg2);
}
.nav-logo {
  font-size: 1rem; font-weight: 700; letter-spacing: -0.3px;
  margin-right: 16px; color: var(--text);
}
.nav-logo span { color: var(--accent); }
nav button {
  padding: 5px 14px; border: 1px solid transparent;
  background: none; color: var(--muted); border-radius: 6px;
  cursor: pointer; font-size: 0.83rem; font-weight: 500;
  transition: all .12s;
}
nav button:hover { color: var(--text); border-color: var(--border); }
nav button.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.nav-spacer { flex: 1; }
.nav-logout {
  padding: 4px 12px; font-size: 0.78rem; color: var(--muted) !important;
  border: 1px solid var(--border) !important;
}
.nav-logout:hover { color: var(--red-text) !important; border-color: var(--red) !important; }

/* ── Views ────────────────────────────────────────────────────── */
.view { display: none; flex: 1; overflow: hidden; }
.view.active { display: flex; }

/* ── Studio View ──────────────────────────────────────────────── */
#view-studio { flex-direction: row; }

/* Chat panel */
#chat-panel {
  flex: 1; display: flex; flex-direction: column; min-width: 0;
  border-right: 1px solid var(--border);
}
#chat-log {
  flex: 1; overflow-y: auto; padding: 20px 16px;
  display: flex; flex-direction: column; gap: 12px;
}
#chat-log::-webkit-scrollbar { width: 4px; }
#chat-log::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.message {
  max-width: 78%; padding: 10px 14px; border-radius: 12px;
  line-height: 1.55; font-size: 0.88rem; word-break: break-word;
}
.message.user { align-self: flex-end; background: var(--accent); color: #fff; border-radius: 12px 12px 2px 12px; }
.message.assistant {
  align-self: flex-start; background: var(--bg3);
  border: 1px solid var(--border); border-radius: 12px 12px 12px 2px;
}
.message.assistant.streaming::after {
  content: '▋'; animation: blink .7s step-end infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* Markdown rendering inside assistant bubbles */
.message.assistant p { margin: 0 0 8px; }
.message.assistant p:last-child { margin-bottom: 0; }
.message.assistant ul, .message.assistant ol {
  margin: 6px 0 8px 18px; padding: 0;
}
.message.assistant li { margin-bottom: 3px; }
.message.assistant li:last-child { margin-bottom: 0; }
.message.assistant strong { font-weight: 700; color: var(--text); }
.message.assistant em { font-style: italic; opacity: .85; }
.message.assistant code {
  background: rgba(0,0,0,.3); padding: 1px 5px; border-radius: 3px;
  font-family: 'SF Mono', Menlo, monospace; font-size: 0.82em; color: var(--code);
}
.message.assistant pre {
  background: rgba(0,0,0,.3); padding: 10px 12px; border-radius: 6px;
  overflow-x: auto; margin: 8px 0; font-size: 0.78rem;
}
.message.assistant pre code { background: none; padding: 0; color: var(--code); }
.message.assistant h1, .message.assistant h2, .message.assistant h3 {
  font-weight: 700; margin: 10px 0 4px; color: var(--text);
}
.message.assistant h1 { font-size: 1rem; }
.message.assistant h2 { font-size: 0.93rem; }
.message.assistant h3 { font-size: 0.88rem; }
.message.assistant blockquote {
  border-left: 3px solid var(--accent); margin: 6px 0; padding-left: 10px;
  color: var(--muted); font-style: italic;
}
.message.assistant hr { border: none; border-top: 1px solid var(--border); margin: 8px 0; }

.message.system-info {
  align-self: center; background: none; border: none;
  color: var(--muted); font-size: 0.75rem; font-style: italic; max-width: 100%;
}

#input-bar {
  display: flex; gap: 8px; padding: 12px 16px;
  border-top: 1px solid var(--border); flex-shrink: 0;
}
#text-input {
  flex: 1; background: var(--bg3); border: 1px solid var(--border);
  color: var(--text); padding: 9px 12px; border-radius: 8px;
  font-size: 0.88rem; outline: none; resize: none;
  transition: border-color .12s; font-family: inherit;
}
#text-input:focus { border-color: var(--accent); }
#send-btn {
  padding: 9px 18px; background: var(--accent); border: none;
  border-radius: 8px; color: #fff; font-size: 0.85rem;
  font-weight: 600; cursor: pointer; transition: background .12s;
  white-space: nowrap;
}
#send-btn:hover { background: var(--accent-hover); }
#send-btn:disabled { background: var(--border); cursor: not-allowed; }

#mic-btn {
  width: 38px; height: 38px; border-radius: 50%; border: 2px solid var(--border);
  background: var(--bg3); color: var(--muted); font-size: 1rem;
  cursor: pointer; flex-shrink: 0; transition: all .12s; display: flex;
  align-items: center; justify-content: center;
}
#mic-btn:hover { border-color: var(--accent); color: var(--text); }
#mic-btn.recording {
  background: #e03131; border-color: #e03131; color: #fff;
  animation: mic-pulse 1s ease-in-out infinite;
}
#mic-btn.processing { background: var(--yellow); border-color: var(--yellow); color: #fff; }
#tts-btn {
  width: 38px; height: 38px; border-radius: 50%; border: 2px solid var(--border);
  background: var(--bg3); color: var(--muted); font-size: 1rem;
  cursor: pointer; flex-shrink: 0; transition: all .12s; display: flex;
  align-items: center; justify-content: center;
}
#tts-btn:hover { border-color: var(--accent); color: var(--text); }
#tts-btn.tts-on { background: var(--accent); border-color: var(--accent); color: #fff; }
@keyframes mic-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(224,49,49,.5); }
  50%       { box-shadow: 0 0 0 6px rgba(224,49,49,0); }
}

/* YAML panel */
#yaml-panel {
  width: 420px; flex-shrink: 0; display: flex; flex-direction: column;
  background: var(--bg2);
}
#yaml-header {
  padding: 11px 16px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  flex-shrink: 0;
}
#yaml-header .label {
  font-size: 0.75rem; font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: .5px;
}
#yaml-preview {
  flex: 1; overflow: auto; padding: 14px 16px;
  font-family: 'SF Mono', Menlo, 'Cascadia Code', monospace;
  font-size: 0.76rem; line-height: 1.65; white-space: pre;
  color: var(--code);
}
#yaml-preview::-webkit-scrollbar { width: 4px; }
#yaml-preview::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
#yaml-placeholder {
  color: var(--muted); font-style: italic; font-size: 0.82rem;
  white-space: normal; line-height: 1.6;
}
#yaml-errors {
  padding: 8px 14px; margin: 0; font-size: .78rem; line-height: 1.5;
  background: #3a1c1c; border-top: 1px solid #c0392b; color: #e74c3c;
}
#yaml-errors ul { margin: 4px 0 0 16px; padding: 0; }
#yaml-errors li { margin-bottom: 2px; }
#yaml-errors .fix-link { color: #f39c12; cursor: pointer; text-decoration: underline; margin-left: 6px; }
.yaml-actions {
  padding: 10px 16px; display: flex; gap: 8px;
  border-top: 1px solid var(--border); flex-shrink: 0;
}
.yaml-actions button {
  flex: 1; padding: 7px 10px; background: var(--bg3);
  border: 1px solid var(--border); color: var(--text);
  border-radius: 6px; cursor: pointer; font-size: 0.78rem;
  transition: background .12s;
}
.yaml-actions button:hover { background: #2a2a3a; }
.yaml-actions button:disabled { opacity: .4; cursor: not-allowed; }
#save-study-btn { background: var(--accent) !important; border-color: var(--accent) !important; }
#save-study-btn:hover { background: var(--accent-hover) !important; }

/* Reset btn */
#reset-btn {
  padding: 5px 10px; background: none; border: 1px solid var(--border);
  border-radius: 6px; color: var(--muted); font-size: 0.75rem;
  cursor: pointer;
}
#reset-btn:hover { color: var(--text); border-color: var(--text); }

/* ── Studies & Runs Views ─────────────────────────────────────── */
#view-studies, #view-runs {
  flex-direction: column; overflow-y: auto; padding: 28px 32px;
}
.view-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 20px;
}
.view-title { font-size: 1.1rem; font-weight: 700; }

table { width: 100%; border-collapse: collapse; }
thead tr { border-bottom: 2px solid var(--border); }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--accent); }
th.sortable.sort-asc::after { content: " ▲"; font-size: .6rem; }
th.sortable.sort-desc::after { content: " ▼"; font-size: .6rem; }
th {
  padding: 9px 14px; text-align: left; font-size: 0.75rem;
  font-weight: 600; color: var(--muted); text-transform: uppercase;
  letter-spacing: .4px; white-space: nowrap;
}
td { padding: 10px 14px; font-size: 0.85rem; border-bottom: 1px solid var(--border); }
tbody tr:hover { background: rgba(255,255,255,.02); }
.truncate { max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.btn-sm {
  padding: 4px 10px; border: 1px solid var(--border); background: none;
  color: var(--muted); border-radius: 5px; cursor: pointer;
  font-size: 0.75rem; transition: all .1s; white-space: nowrap;
}
.btn-sm:hover { background: var(--border); color: var(--text); }
.btn-danger:hover { background: var(--red) !important; color: var(--red-text) !important; border-color: var(--red) !important; }
.btn-primary { background: var(--accent) !important; border-color: var(--accent) !important; color: #fff !important; }
.btn-primary:hover { background: var(--accent-hover) !important; }
.btn-warning { background: #e67700 !important; border-color: #e67700 !important; color: #fff !important; }
.btn-warning:hover { background: #d9480f !important; }
.reconcile-table th { text-align: left; padding: 4px 8px; border-bottom: 1px solid var(--border); }
.reconcile-table td { padding: 4px 8px; border-bottom: 1px solid var(--border); }
.actions-cell { display: flex; gap: 6px; flex-wrap: wrap; }

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 4px;
  font-size: 0.7rem; font-weight: 600;
}
.badge-done    { background: var(--green); color: var(--green-text); }
.badge-running { background: var(--blue);  color: var(--blue-text); }
.badge-failed  { background: var(--red);   color: var(--red-text); }
.badge-interrupted { background: #e67700; color: #fff; }
.badge-pending { background: var(--yellow); color: var(--yellow-text); }
.badge-stage { background: #2d2d44; color: var(--muted); font-weight: 400; font-style: italic; }
.badge-type    { background: var(--bg3); border: 1px solid var(--border); color: var(--muted); }

.progress-bar-wrap {
  height: 4px; background: var(--border); border-radius: 2px;
  margin-top: 5px; overflow: hidden; min-width: 80px;
}
.progress-bar-fill {
  height: 100%; background: var(--accent);
  border-radius: 2px; transition: width .4s ease;
}

/* ── Modal ────────────────────────────────────────────────────── */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.7);
  display: flex; align-items: center; justify-content: center;
  z-index: 100; padding: 20px;
}
.modal {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: 12px; padding: 28px; width: 100%;
  max-width: 720px; max-height: 85vh; overflow-y: auto;
  display: flex; flex-direction: column; gap: 16px;
}
.modal h3 { font-size: 1rem; font-weight: 700; }
.modal pre {
  background: var(--bg); padding: 16px; border-radius: 8px;
  font-size: 0.76rem; line-height: 1.6; overflow: auto;
  max-height: 400px; color: var(--code);
  font-family: 'SF Mono', Menlo, monospace; white-space: pre;
}
.modal-footer { display: flex; gap: 8px; justify-content: flex-end; }
.modal-footer button {
  padding: 8px 18px; border-radius: 7px; cursor: pointer;
  font-size: 0.85rem; border: 1px solid var(--border);
}
.modal-close { background: none; color: var(--muted); }
.modal-close:hover { color: var(--text); }
.modal-action { background: var(--accent); border-color: var(--accent); color: #fff; }
.modal-action:hover { background: var(--accent-hover); }
.modal label { font-size: 0.82rem; color: var(--muted); }
.modal input[type="text"] {
  width: 100%; padding: 8px 12px; background: var(--bg);
  border: 1px solid var(--border); border-radius: 7px;
  color: var(--text); font-size: 0.88rem; outline: none;
  margin-top: 4px;
}
.modal input[type="text"]:focus { border-color: var(--accent); }
.estimate-block { background: var(--bg); padding: 14px; border-radius: 8px; }
.estimate-block .cost { font-size: 1.5rem; font-weight: 700; color: var(--code); }
.estimate-block .details { margin-top: 8px; color: var(--muted); font-size: 0.82rem; line-height: 1.8; }

/* ── Toast ────────────────────────────────────────────────────── */
#toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--bg3); border: 1px solid var(--border);
  padding: 9px 20px; border-radius: 8px; font-size: 0.83rem;
  color: var(--text); pointer-events: none; opacity: 0;
  transition: opacity .2s; z-index: 200;
}
#toast.show { opacity: 1; }
#toast.ok { border-color: var(--green); color: var(--green-text); }
#toast.err { border-color: var(--red); color: var(--red-text); }

/* ── Spinner ──────────────────────────────────────────────────── */
.spinner {
  display: inline-block; width: 14px; height: 14px;
  border: 2px solid var(--border); border-top-color: var(--accent);
  border-radius: 50%; animation: spin .6s linear infinite;
  vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Empty / loading states ───────────────────────────────────── */
.empty-state {
  text-align: center; padding: 60px 20px; color: var(--muted);
  font-size: 0.9rem;
}
.empty-state big { display: block; font-size: 2rem; margin-bottom: 8px; }

.loading-row td { color: var(--muted); font-style: italic; }
