/* ── Design tokens (matching chat UI) ─────────────────────────── */
:root {
  --bg-primary:   #0d0d0d;
  --bg-secondary: #1a1a1a;
  --bg-surface:   #242424;
  --bg-hover:     #2e2e2e;
  --bg-input:     #1e1e1e;
  --border:       #333;
  --border-focus: #555;
  --text-primary: #e8e8e8;
  --text-muted:   #a0a0a0;
  --text-dim:     #666;
  --accent:       #6366f1;
  --accent-hover: #818cf8;
  --red:          #ef4444;
  --red-dim:      #b91c1c;
  --green:        #22c55e;
  --green-dim:    #166534;
  --yellow:       #eab308;
  --radius:       6px;
  --radius-lg:    10px;
  --sidebar-w:    220px;
  --header-h:     48px;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ── Sidebar ──────────────────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  transition: margin-left 0.2s;
}
#sidebar.collapsed { margin-left: calc(-1 * var(--sidebar-w)); }

.sidebar-header {
  padding: 16px 16px 12px;
  border-bottom: 1px solid var(--border);
}
.logo-text {
  font-size: 18px;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -0.5px;
}

.nav-section { padding: 8px 0; }
.nav-section-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-dim);
  padding: 8px 16px 4px;
}

.nav-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 8px 16px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 13px;
  cursor: pointer;
  text-align: left;
  position: relative;
  transition: background 0.15s, color 0.15s;
}
.nav-btn:hover { background: var(--bg-hover); color: var(--text-primary); }
.nav-btn.active {
  background: var(--bg-surface);
  color: var(--text-primary);
  border-left: 2px solid var(--accent);
}
.nav-icon { font-size: 12px; width: 16px; text-align: center; }
.nav-sub {
  margin-left: auto;
  font-size: 10px;
  color: var(--text-dim);
}
.nav-divider { height: 1px; background: var(--border); margin: 4px 12px; }
.nav-btn.disabled { opacity: 0.35; cursor: not-allowed; }

/* Flash warning on disabled nav buttons */
.nav-btn .nav-flash {
  display: none;
  position: absolute;
  inset: 0;
  background: rgba(239, 68, 68, 0.9);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  align-items: center;
  justify-content: center;
  border-radius: 0;
  z-index: 1;
  animation: flash-fade 1.5s ease forwards;
}
.nav-btn .nav-flash.show { display: flex; }
@keyframes flash-fade {
  0%   { opacity: 1; }
  70%  { opacity: 1; }
  100% { opacity: 0; }
}

.sidebar-section { padding: 4px 0; }
.gpu-info { padding: 4px 16px 12px; }
.gpu-item { font-size: 12px; color: var(--text-muted); padding: 2px 0; }
.text-dim { color: var(--text-dim); }

/* ── Main ─────────────────────────────────────────────────────── */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

#header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-secondary);
}
#toggle-sidebar {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 18px;
  cursor: pointer;
  padding: 4px;
}
#toggle-sidebar:hover { color: var(--text-primary); }
#header-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-muted);
}

#content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ── Upper panel (tabs + content) ─────────────────────────────── */
#upper-panel {
  flex: 0 0 auto;
  height: 55%;
  min-height: 80px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Tab bar */
#tab-bar {
  display: flex;
  align-items: center;
  gap: 0;
  padding: 0 24px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.tab-bar-left {
  display: flex;
  gap: 0;
}
.tab-btn {
  padding: 10px 18px;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}
.tab-btn:hover { color: var(--text-primary); }
.tab-btn.active {
  color: var(--text-primary);
  border-bottom-color: var(--accent);
}
.tab-bar-desc {
  margin-left: auto;
  font-size: 12px;
  color: var(--text-dim);
}

/* Tab content panels */
.tab-content {
  display: none;
  flex: 1;
  overflow-y: auto;
}
.tab-content.active { display: flex; flex-direction: column; }

/* ── Form panel ───────────────────────────────────────────────── */
#form-panel {
  padding: 20px 24px;
}

/* ── Data panel ──────────────────────────────────────────────── */
#data-panel { padding: 0; }
#data-content {
  display: flex;
  flex: 1;
  overflow: hidden;
}
#data-table-wrap {
  flex: 0 0 510px;
  min-width: 180px;
  overflow-y: auto;
  padding: 16px;
}

/* Vertical splitter in data panel */
.data-splitter {
  flex: 0 0 6px;
  cursor: col-resize;
  background: var(--bg-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  transition: background 0.15s;
}
.data-splitter:hover, .data-splitter.dragging { background: var(--accent); }
.data-splitter-grip {
  width: 2px;
  height: 32px;
  background: var(--text-dim);
  border-radius: 1px;
}
.data-splitter:hover .data-splitter-grip,
.data-splitter.dragging .data-splitter-grip { background: #fff; }
body.data-splitter-active { cursor: col-resize !important; user-select: none; }
#data-graph-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  padding: 12px;
  background: #000;
}
#data-graph {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
#data-graph svg, #data-graph img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

/* Measurement table */
#data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
#data-table thead th {
  text-align: left;
  padding: 8px 10px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-dim);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--bg-primary);
}
#data-table tbody td {
  padding: 7px 10px;
  color: var(--text-primary);
  border-bottom: 1px solid var(--border);
  font-family: "SF Mono", "Fira Code", "Consolas", monospace;
  font-size: 12px;
}
#data-table tbody tr:hover { background: var(--bg-hover); }
.data-empty {
  color: var(--text-dim);
  font-size: 13px;
  padding: 24px 0;
}
.data-graph-placeholder {
  color: var(--text-dim);
  font-size: 13px;
}
.data-pending { color: var(--text-dim); font-style: italic; }

#form-header { margin-bottom: 16px; }
#form-header h2 {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 2px;
}
#form-header p {
  font-size: 13px;
  color: var(--text-muted);
}

.form-section-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-dim);
  margin: 16px 0 8px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.form-row { margin-bottom: 12px; }
.form-label {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-primary);
  margin-bottom: 4px;
}
.required-star { color: var(--red); }
.form-help {
  font-size: 11px;
  color: var(--text-dim);
  margin-bottom: 4px;
}

.form-input, .form-select {
  width: 100%;
  max-width: 460px;
  padding: 7px 10px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 13px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s;
}
.form-input:focus, .form-select:focus {
  border-color: var(--accent);
}
.form-input.input-error {
  border-color: var(--red);
}
.form-input::placeholder { color: var(--text-dim); }

.form-select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M3 5l3 3 3-3' fill='none' stroke='%23666' stroke-width='1.5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  padding-right: 28px;
}
.form-select option { background: var(--bg-surface); }

/* Path input with browse button */
.path-input-wrap {
  display: flex;
  gap: 6px;
  max-width: 460px;
}
.path-input-wrap .form-input { flex: 1; }
.browse-btn {
  padding: 7px 14px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-muted);
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
}
.browse-btn:hover { background: var(--bg-hover); color: var(--text-primary); }

/* Toggle grid */
.toggle-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 20px;
}
.toggle-item {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-size: 12px;
}
.toggle-item input[type="checkbox"] {
  display: none;
}
.toggle-slider {
  width: 32px;
  height: 18px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 9px;
  position: relative;
  transition: background 0.2s, border-color 0.2s;
  flex-shrink: 0;
}
.toggle-slider::after {
  content: "";
  position: absolute;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--text-dim);
  top: 2px;
  left: 2px;
  transition: transform 0.2s, background 0.2s;
}
.toggle-item input:checked + .toggle-slider {
  background: var(--accent);
  border-color: var(--accent);
}
.toggle-item input:checked + .toggle-slider::after {
  transform: translateX(14px);
  background: #fff;
}
.toggle-label-text { color: var(--text-muted); }

/* Inline field toggle (toggleable fields like Catbench) */
.form-label-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}
.form-label-row .form-label { margin-bottom: 0; }
.header-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}
.header-toggle input[type="checkbox"] { display: none; }
.header-toggle .toggle-slider {
  width: 28px;
  height: 16px;
  border-radius: 8px;
}
.header-toggle .toggle-slider::after {
  width: 10px;
  height: 10px;
  top: 2px;
  left: 2px;
}
.header-toggle input:checked + .toggle-slider {
  background: var(--accent);
  border-color: var(--accent);
}
.header-toggle input:checked + .toggle-slider::after {
  transform: translateX(12px);
  background: #fff;
}
.header-toggle-label {
  font-size: 11px;
  color: var(--text-dim);
  font-weight: 500;
}
.header-toggle input:checked ~ .header-toggle-label {
  color: var(--accent);
}
.field-toggle {
  display: flex;
  align-items: center;
  cursor: pointer;
}
.field-toggle input[type="checkbox"] { display: none; }
.field-toggle .toggle-slider {
  width: 28px;
  height: 16px;
  border-radius: 8px;
}
.field-toggle .toggle-slider::after {
  width: 10px;
  height: 10px;
  top: 2px;
  left: 2px;
}
.field-toggle input:checked + .toggle-slider {
  background: var(--accent);
  border-color: var(--accent);
}
.field-toggle input:checked + .toggle-slider::after {
  transform: translateX(12px);
  background: #fff;
}
.form-row.field-disabled .form-label,
.form-row.field-disabled .form-help { opacity: 0.35; }
.form-row.field-disabled .form-input,
.form-row.field-disabled .form-select {
  opacity: 0.35;
  pointer-events: none;
}

/* Form body layout (side-by-side with metadata panel) */
#form-body {
  display: flex;
  gap: 20px;
}
#form-body > #form-fields { flex: 0 1 480px; min-width: 0; }

/* ── Metadata panel ─────────────────────────────────────────────── */
.metadata-panel {
  flex: 0 0 260px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px 16px;
  align-self: flex-start;
}
.metadata-title {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}
.meta-field { margin-bottom: 10px; }
.meta-field:last-child { margin-bottom: 0; }
.meta-field-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 3px;
}
.meta-label {
  font-size: 11px;
  font-weight: 500;
  color: var(--text-primary);
}
.meta-help {
  font-size: 10px;
  color: var(--text-dim);
  margin-bottom: 3px;
}
.meta-input-row {
  display: flex;
  gap: 6px;
  align-items: center;
}
.meta-input-row .meta-input { flex: 1; min-width: 0; }
.meta-input {
  width: 100%;
  padding: 5px 8px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 12px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s, background 0.15s, opacity 0.15s;
}
.meta-input:focus { border-color: var(--accent); }

/* Lock button */
.meta-lock {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
  color: var(--text-dim);
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}
.meta-lock:hover { background: var(--bg-hover); color: var(--text-muted); }
.meta-lock.locked {
  background: var(--green-dim);
  border-color: var(--green-dim);
  color: #fff;
}
.meta-lock.locked:hover {
  background: var(--green);
  border-color: var(--green);
}
/* Disable unlock during a run */
.meta-lock.run-locked {
  pointer-events: none;
  opacity: 0.6;
}

/* Locked field: readonly appearance */
.meta-field.locked .meta-input {
  border-color: var(--green-dim);
  background: rgba(34, 197, 94, 0.04);
  opacity: 0.7;
}

/* Waiting state — pulse glow */
.metadata-panel.metadata-waiting {
  border-color: var(--accent);
  animation: metadata-pulse 2s ease-in-out infinite;
}
@keyframes metadata-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(99, 102, 241, 0); }
  50% { box-shadow: 0 0 14px 3px rgba(99, 102, 241, 0.25); }
}
.metadata-confirm-btn {
  width: 100%;
  margin-top: 14px;
}

/* Form actions — flows below form content, scrolls with it */
#form-actions {
  display: flex;
  gap: 8px;
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.action-btn {
  padding: 8px 20px;
  border: none;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
}
.action-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.action-btn.primary { background: var(--accent); color: #fff; }
.action-btn.primary:hover:not(:disabled) { background: var(--accent-hover); }
.action-btn.danger { background: var(--red-dim); color: #fff; }
.action-btn.danger:hover:not(:disabled) { background: var(--red); }
.action-btn.secondary { background: var(--bg-surface); color: var(--text-muted); border: 1px solid var(--border); }
.action-btn.secondary:hover:not(:disabled) { background: var(--bg-hover); color: var(--text-primary); }

/* ── Splitter ─────────────────────────────────────────────────── */
#splitter {
  flex: 0 0 6px;
  background: #111;
  cursor: row-resize;
  display: flex;
  align-items: center;
  justify-content: center;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid #222;
  position: relative;
  z-index: 2;
  transition: background 0.15s;
}
#splitter:hover, #splitter.dragging { background: var(--accent); }
.splitter-grip {
  width: 32px;
  height: 2px;
  background: var(--text-dim);
  border-radius: 1px;
}
#splitter:hover .splitter-grip,
#splitter.dragging .splitter-grip { background: #fff; }
/* Prevent text selection while dragging */
body.splitter-active { cursor: row-resize !important; user-select: none; }

/* ── Terminal panel ───────────────────────────────────────────── */
#terminal-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 80px;
  background: #000;
}

#terminal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  background: #111;
}
.terminal-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-dim);
}
.terminal-status {
  font-size: 11px;
  font-weight: 500;
}
.terminal-status.running { color: var(--yellow); }
.terminal-status.success { color: var(--green); }
.terminal-status.error { color: var(--red); }

#terminal-output {
  flex: 1;
  padding: 12px 16px;
  overflow-y: auto;
  font-family: "SF Mono", "Fira Code", "Cascadia Code", "Consolas", monospace;
  font-size: 12px;
  line-height: 1.6;
  color: #ccc;
  white-space: pre-wrap;
  word-break: break-all;
}

.term-progress { display: block; }
.term-cmd { color: var(--accent); font-weight: 500; }
.term-stderr { color: #f87171; }
.term-success { color: var(--green); font-weight: 500; }
.term-error { color: var(--red); font-weight: 500; }

/* ── File browser modal ───────────────────────────────────────── */
#browser-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.browser-dialog {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  width: 560px;
  max-width: 90vw;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}
.browser-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.browser-title { font-size: 14px; font-weight: 600; }
.browser-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 20px;
  cursor: pointer;
  padding: 0 4px;
}
.browser-close:hover { color: var(--text-primary); }

.browser-path-input {
  margin: 10px 16px 0;
  padding: 7px 10px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 13px;
  font-family: monospace;
  outline: none;
}
.browser-path-input:focus { border-color: var(--accent); }

.browser-current-path {
  padding: 6px 16px;
  font-size: 11px;
  color: var(--text-dim);
  font-family: monospace;
}

.browser-indicator {
  padding: 4px 16px;
  font-size: 11px;
}
.browser-indicator.model-valid { color: var(--green); }

.browser-entries {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0;
  max-height: 400px;
}

.browser-entry {
  padding: 6px 16px;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.1s;
}
.browser-entry:hover { background: var(--bg-hover); }
.browser-dir { color: var(--accent); }
.browser-dir::before { content: "\1F4C1 "; font-size: 12px; }
.browser-file { color: var(--text-dim); font-size: 12px; }
.browser-loading, .browser-empty { padding: 16px; color: var(--text-dim); font-size: 13px; }
.browser-error { padding: 16px; color: var(--red); font-size: 13px; }

.browser-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
}

/* ── Responsive ───────────────────────────────────────────────── */
@media (max-width: 768px) {
  #sidebar {
    position: fixed;
    z-index: 50;
    height: 100vh;
    left: 0;
  }
  #sidebar.collapsed { margin-left: calc(-1 * var(--sidebar-w)); }
  #form-panel { padding: 16px; }
  .browser-dialog { width: 95vw; }
}
