/* ── Tokens ────────────────────────────────────────────────────── */
:root {
  --bg-primary:   #0d0d0d;
  --bg-secondary: #1a1a1a;
  --bg-surface:   #242424;
  --text-primary: #e8e8e8;
  --text-muted:   #a0a0a0;
  --text-dim:     #666;
  --accent:       #6366f1;
  --accent-hover: #818cf8;
  --border:       #2a2a2a;
  --border-light: #333;
  --red:          #ef4444;
  --font:         -apple-system, BlinkMacSystemFont, 'Inter', 'Segoe UI', sans-serif;
  --mono:         'SF Mono', 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
  --sidebar-w:    340px;
  --content-w:    900px;
  --radius:       10px;
  --radius-sm:    6px;
}
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; background: var(--bg-primary); color: var(--text-primary); font-family: var(--font); }

/* ── Layout ────────────────────────────────────────────────────── */
body { display: flex; overflow: hidden; }
#sidebar {
  width: var(--sidebar-w); flex-shrink: 0;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  overflow-y: auto; padding: 16px 20px;
  transition: margin-left .25s ease;
}
#sidebar.hidden { margin-left: calc(-1 * var(--sidebar-w)); }
#main {
  flex: 1; display: flex; flex-direction: column;
  min-width: 0;
}
#header {
  height: 48px; display: flex; align-items: center;
  padding: 0 16px; border-bottom: 1px solid var(--border);
  gap: 12px; flex-shrink: 0;
}
#toggle-sidebar {
  background: none; border: none; color: var(--text-muted);
  font-size: 20px; cursor: pointer; padding: 4px 8px;
}
#header-title { font-weight: 600; font-size: 14px; }
#header-model { color: var(--text-dim); font-size: 12px; margin-left: auto; font-family: var(--mono); }

#chat-area {
  flex: 1; overflow-y: auto; scroll-behavior: smooth;
  padding: 0 100px;
}
#messages {
  max-width: var(--content-w); margin: 0 auto;
  padding: 24px 0 16px;
  display: flex; flex-direction: column; gap: 6px;
}

/* ── Sidebar widgets ───────────────────────────────────────────── */
#sidebar h2 {
  font-size: 11px; text-transform: uppercase; letter-spacing: .08em;
  color: var(--text-dim); margin: 20px 0 8px;
}
#sidebar h2:first-child { margin-top: 0; }
.sb-section { display: flex; flex-direction: column; gap: 10px; }
.sb-row { display: flex; flex-direction: column; gap: 4px; }
.sb-label { font-size: 12px; color: var(--text-muted); }
.sb-textarea, #sidebar input[type="text"], #sidebar input[type="number"] {
  background: var(--bg-surface); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 8px 10px;
  color: var(--text-primary); font-size: 13px; font-family: var(--font);
  outline: none; width: 100%;
}
.sb-textarea:focus, #sidebar input:focus { border-color: var(--accent); }
.sb-textarea { resize: vertical; min-height: 60px; line-height: 1.4; }
#sidebar select {
  background: var(--bg-surface); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 6px 8px;
  color: var(--text-primary); font-size: 13px; outline: none; width: 100%;
}
.slider-row { display: flex; align-items: center; gap: 10px; }
.slider-row input[type="range"] { flex: 1; accent-color: var(--accent); }
.slider-val {
  font-size: 12px; color: var(--text-muted); font-family: var(--mono);
  min-width: 48px; text-align: right; cursor: pointer;
  padding: 2px 4px; border-radius: 3px;
  border: 1px solid transparent; transition: border-color .15s;
}
.slider-val:hover { border-color: var(--border-light); }
.slider-val-input {
  font-size: 12px; font-family: var(--mono);
  width: 56px; text-align: right;
  background: var(--bg-surface); color: var(--text-primary);
  border: 1px solid var(--accent); border-radius: 3px;
  padding: 2px 4px; outline: none;
}

/* Toggles */
.toggle-row { flex-direction: row; align-items: center; justify-content: space-between; }
.toggle {
  width: 36px; height: 20px; border-radius: 10px;
  background: var(--bg-surface); border: 1px solid var(--border);
  cursor: pointer; position: relative; transition: background .2s;
}
.toggle::after {
  content: ''; position: absolute; top: 2px; left: 2px;
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--text-dim); transition: transform .2s, background .2s;
}
.toggle.on { background: var(--accent); border-color: var(--accent); }
.toggle.on::after { transform: translateX(16px); background: #fff; }

/* Chips */
.chip-list { display: flex; flex-wrap: wrap; gap: 4px; }
.chip {
  display: inline-flex; align-items: center; gap: 4px;
  background: var(--bg-surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 2px 8px; font-size: 12px;
}
.chip .x { cursor: pointer; color: var(--text-dim); font-size: 14px; }
.chip .x:hover { color: var(--red); }

/* Buttons */
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.sb-btn {
  background: var(--bg-surface); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 6px 12px;
  color: var(--text-muted); font-size: 12px; cursor: pointer;
  transition: border-color .15s;
}
.sb-btn:hover { border-color: var(--text-dim); color: var(--text-primary); }
.sb-btn.danger:hover { border-color: var(--red); color: var(--red); }

.model-info { font-size: 12px; color: var(--text-muted); line-height: 1.6; word-break: break-all; }

/* ── Dashboard switch button ──────────────────────────────────── */
#dashboard-switch {
  margin-bottom: 12px;
}
.switch-btn {
  width: 100%; padding: 10px 12px;
  background: var(--bg-surface); border: 1px solid var(--border);
  border-radius: var(--radius-sm); color: var(--text-muted);
  font-size: 12px; font-weight: 600; cursor: pointer;
  text-transform: uppercase; letter-spacing: .06em;
  position: relative; overflow: hidden;
  transition: border-color .15s, color .15s;
}
.switch-btn:hover { border-color: var(--accent); color: var(--text-primary); }
.switch-btn.disabled {
  opacity: 0.35; cursor: not-allowed;
}
.switch-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;
  z-index: 1;
  animation: switch-fade 1.5s ease forwards;
}
.switch-flash.show { display: flex; }
@keyframes switch-fade {
  0%   { opacity: 1; }
  70%  { opacity: 1; }
  100% { opacity: 0; }
}

/* ── Model Panel ──────────────────────────────────────────────── */
#model-panel {
  margin-bottom: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}
.section-header {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; cursor: pointer;
  background: var(--bg-surface);
  user-select: none;
}
.section-header span:first-child {
  font-size: 12px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .06em; color: var(--text-muted);
}
.section-header .chevron {
  margin-left: auto; font-size: 10px; color: var(--text-dim);
}
.panel-badge {
  font-size: 11px; padding: 1px 8px; border-radius: 10px;
  background: var(--bg-primary); color: var(--text-dim);
  border: 1px solid var(--border);
  font-family: var(--mono);
}
.panel-badge.loaded {
  color: #4ade80; border-color: #166534; background: #052e16;
}
.section-body {
  padding: 12px;
  display: flex; flex-direction: column; gap: 8px;
}

/* File browser */
#browser-path-input {
  font-family: var(--mono); font-size: 12px;
}
#browser-list {
  max-height: 240px; overflow-y: auto;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--bg-primary);
}
.browser-entry {
  display: flex; align-items: center; gap: 6px;
  padding: 5px 10px; font-size: 12px; cursor: default;
  border-bottom: 1px solid var(--border);
  transition: background .1s;
}
.browser-entry:last-child { border-bottom: none; }
.browser-dir { cursor: pointer; }
.browser-dir:hover { background: var(--bg-surface); }
.browser-icon { font-size: 13px; flex-shrink: 0; }
.browser-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.browser-file { color: var(--text-dim); }
.browser-loading, .browser-error {
  padding: 12px; font-size: 12px; color: var(--text-dim); text-align: center;
}
.browser-error { color: var(--red); }
.model-indicator {
  font-size: 12px; color: #4ade80; padding: 4px 0;
}

/* GPU config */
.gpu-auto-label {
  font-size: 12px; color: var(--text-muted); display: flex; align-items: center; gap: 6px;
  cursor: pointer;
}
.gpu-auto-label input { accent-color: var(--accent); }
#gpu-list { display: flex; flex-direction: column; gap: 4px; margin-top: 4px; }
.gpu-row {
  display: flex; align-items: center; gap: 6px;
}
.gpu-label {
  display: flex; align-items: center; gap: 6px; font-size: 12px;
  color: var(--text-muted); flex: 1; min-width: 0; cursor: pointer;
}
.gpu-label input { accent-color: var(--accent); }
.gpu-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.gpu-vram { font-family: var(--mono); font-size: 11px; color: var(--text-dim); flex-shrink: 0; }
.gpu-ratio {
  width: 52px; font-size: 11px; font-family: var(--mono);
  background: var(--bg-surface); border: 1px solid var(--border);
  border-radius: 3px; padding: 2px 4px; color: var(--text-primary);
  text-align: right; outline: none;
}
.gpu-ratio:focus { border-color: var(--accent); }
.gpu-none { font-size: 12px; color: var(--text-dim); padding: 4px 0; }

/* Model config row */
.model-config-row {
  display: flex; gap: 8px; margin-top: 4px;
}
.model-config-field { flex: 1; display: flex; flex-direction: column; gap: 2px; }
.model-config-field input {
  font-family: var(--mono); font-size: 12px;
}

/* Model actions */
.model-actions {
  display: flex; gap: 8px; margin-top: 4px;
}
.model-actions .sb-btn { flex: 1; text-align: center; }
.model-loading {
  font-size: 12px; color: var(--text-dim); padding: 4px 0;
}

/* ── Messages ──────────────────────────────────────────────────── */
.msg {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0;
  padding: 12px 0;
  position: relative;
}
.msg-role {
  position: absolute;
  right: calc(100% + 12px);
  width: 72px;
  font-size: 11px; text-transform: uppercase; letter-spacing: .05em;
  color: var(--text-dim); padding-top: 2px; text-align: right;
  font-weight: 600;
  white-space: nowrap;
}
.msg-role.user { color: var(--accent); }
.msg-body {
  grid-column: 1;
  font-size: 14px; line-height: 1.7;
  overflow-wrap: break-word;
  min-width: 0;
}
.msg-body p { margin: 0 0 8px; }
.msg-body p:last-child { margin-bottom: 0; }
.msg-body ul, .msg-body ol { margin: 4px 0 8px 20px; }
.msg-body blockquote {
  border-left: 3px solid var(--border-light);
  padding-left: 12px; color: var(--text-muted);
  margin: 8px 0;
}
.msg-body table { border-collapse: collapse; margin: 8px 0; font-size: 13px; }
.msg-body th, .msg-body td { border: 1px solid var(--border-light); padding: 6px 10px; }
.msg-body th { background: var(--bg-surface); }

/* Code blocks */
.msg-body pre {
  background: #111;
  border: 1px solid var(--border-light);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  overflow-x: auto;
  margin: 10px 0;
  font-size: 13px;
  line-height: 1.5;
}
.msg-body code {
  font-family: var(--mono);
  font-size: 0.9em;
}
.msg-body :not(pre) > code {
  background: var(--bg-surface);
  padding: 2px 6px;
  border-radius: 3px;
  border: 1px solid var(--border);
}

/* Dialogue quotes */
.dialogue { color: #d4f1f9; font-weight: 600; }

/* Think blocks */
.think-block {
  border-left: 2px solid var(--border-light);
  margin: 8px 0;
  padding: 0;
}
.think-toggle {
  background: none; border: none; color: var(--text-dim);
  cursor: pointer; font-size: 12px; padding: 4px 8px;
  font-family: var(--font);
}
.think-toggle:hover { color: var(--text-muted); }
.think-content {
  padding: 8px 12px;
  color: var(--text-dim);
  font-size: 13px;
  line-height: 1.6;
  display: none;
}
.think-content.open { display: block; }

/* TPS badge */
.msg-tps {
  grid-column: 1;
  font-size: 11px; color: var(--text-dim);
  margin-top: 6px; font-family: var(--mono);
}

/* Streaming cursor */
.cursor {
  display: inline-block; width: 7px; height: 16px;
  background: var(--accent); margin-left: 2px;
  animation: blink .6s infinite;
  vertical-align: text-bottom;
}
@keyframes blink { 50% { opacity: 0; } }

/* ── Message wrapper with side arrows ──────────────────────────── */
.msg-wrap {
  position: relative;
}
.msg-arrow {
  position: absolute; top: 0; bottom: 0; width: 28px;
  display: flex; align-items: center; justify-content: center;
  opacity: 0; transition: opacity .15s;
  cursor: pointer; z-index: 1;
}
.msg-wrap:hover .msg-arrow { opacity: 1; }
.msg-arrow:hover { opacity: 1 !important; }
.msg-arrow-left { left: -32px; }
.msg-arrow-right { right: -32px; }
.msg-arrow button {
  background: none; border: 1px solid var(--border);
  color: var(--text-muted); cursor: pointer;
  width: 24px; height: 40px; border-radius: 4px;
  font-size: 14px; display: flex; align-items: center; justify-content: center;
  transition: color .1s, border-color .1s, background .1s;
}
.msg-arrow button:hover {
  color: var(--text-primary); border-color: var(--text-muted);
  background: var(--bg-surface);
}
.msg-arrow-right button.regen-arrow {
  border-color: var(--accent); color: var(--accent);
}
.msg-arrow-right button.regen-arrow:hover {
  background: var(--accent); color: #fff;
}

/* ── Branch navigation (header) ────────────────────────────────── */
.msg-header {
  display: flex; align-items: center; gap: 8px;
  grid-column: 1;
}
.branch-counter {
  font-size: 11px; color: var(--text-dim); font-family: var(--mono);
  min-width: 28px;
}

/* ── Message actions (inline in header) ────────────────────────── */
.msg-actions-inline {
  display: inline-flex; gap: 4px;
  opacity: 0; transition: opacity .15s;
}
.msg-wrap:hover .msg-actions-inline { opacity: 1; }
.msg-actions-inline button {
  background: none; border: 1px solid transparent;
  color: var(--text-dim); cursor: pointer; padding: 2px 7px;
  border-radius: 3px; font-size: 11px;
  transition: color .1s, border-color .1s;
}
.msg-actions-inline button:hover { color: var(--text-primary); border-color: var(--border-light); }
.msg-actions-inline .danger:hover { color: var(--red); border-color: var(--red); }

/* Disable all actions during editing */
#messages.editing .msg-actions-inline button,
#messages.editing .msg-arrow button {
  opacity: 0.3; pointer-events: none;
}
#messages.editing .msg-actions-inline { opacity: 0.3; }
#messages.editing .msg-wrap:hover .msg-actions-inline { opacity: 0.3; }
#messages.editing .msg-wrap:hover .msg-arrow { opacity: 0.3; }

/* ── Edit overlay ──────────────────────────────────────────────── */
.edit-assistant-textarea {
  background: var(--bg-surface) !important;
  border: 1px solid var(--accent) !important;
  border-radius: var(--radius-sm);
  padding: 10px 12px; color: var(--text-primary);
  font-size: 14px; font-family: var(--font);
  resize: vertical; min-height: 80px; line-height: 1.5;
  outline: none; box-sizing: border-box;
}
.edit-btns {
  display: flex; gap: 6px; margin-top: 6px;
}
.edit-btns button {
  padding: 4px 14px; border-radius: var(--radius-sm);
  font-size: 12px; cursor: pointer; border: 1px solid var(--border);
}
.edit-btns .save-btn {
  background: var(--accent); color: #fff; border-color: var(--accent);
}
.edit-btns .save-btn:hover { background: var(--accent-hover); }
.edit-btns .cancel-btn {
  background: var(--bg-surface); color: var(--text-muted);
}
.edit-btns .cancel-btn:hover { color: var(--text-primary); }

.edit-area {
  grid-column: 1;
}
.edit-area textarea {
  width: 100%; background: var(--bg-surface);
  border: 1px solid var(--accent); border-radius: var(--radius-sm);
  padding: 10px 12px; color: var(--text-primary);
  font-size: 14px; font-family: var(--font);
  resize: vertical; min-height: 60px; line-height: 1.5;
  outline: none;
}
.edit-area .edit-btns {
  display: flex; gap: 6px; margin-top: 6px;
}
.edit-area .edit-btns button {
  padding: 4px 14px; border-radius: var(--radius-sm);
  font-size: 12px; cursor: pointer; border: 1px solid var(--border);
}
.edit-area .edit-btns .save-btn {
  background: var(--accent); color: #fff; border-color: var(--accent);
}
.edit-area .edit-btns .save-btn:hover { background: var(--accent-hover); }
.edit-area .edit-btns .cancel-btn {
  background: var(--bg-surface); color: var(--text-muted);
}
.edit-area .edit-btns .cancel-btn:hover { color: var(--text-primary); }

/* ── Empty state ───────────────────────────────────────────────── */
#empty-state {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  color: var(--text-dim); gap: 8px;
}
#empty-state .logo { font-size: 32px; opacity: .3; }
#empty-state .hint { font-size: 13px; }

/* ── Input area ─────────────────────────────────────────────────── */
#input-area {
  padding: 12px 24px 16px;
  background: var(--bg-primary);
  border-top: 1px solid var(--border);
}
#input-wrap {
  max-width: var(--content-w); margin: 0 auto;
  display: flex; align-items: flex-end; gap: 10px;
}
#input-box {
  flex: 1; background: var(--bg-surface);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  padding: 12px 16px;
  color: var(--text-primary);
  font-size: 14px; font-family: var(--font);
  resize: none; outline: none;
  min-height: 44px; max-height: 200px;
  line-height: 1.5;
  transition: border-color .15s;
}
#input-box:focus { border-color: var(--accent); }
#input-box::placeholder { color: var(--text-dim); }

#send-btn, #stop-btn {
  width: 42px; height: 42px;
  border-radius: var(--radius);
  border: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s;
  flex-shrink: 0;
}
#send-btn {
  background: var(--accent); color: #fff;
}
#send-btn:hover { background: var(--accent-hover); }
#send-btn:disabled { background: var(--bg-surface); color: var(--text-dim); cursor: default; }
#stop-btn {
  background: var(--red); color: #fff; display: none;
}
#stop-btn:hover { background: #dc2626; }

/* ── Auto-scroll toggle ────────────────────────────────────────── */
.input-footer {
  max-width: var(--content-w); margin: 4px auto 0;
  display: flex; align-items: center; gap: 12px;
  font-size: 11px; color: var(--text-dim);
}
.input-footer label {
  display: flex; align-items: center; gap: 4px; cursor: pointer;
}
.input-footer input[type="checkbox"] { accent-color: var(--accent); }

/* ── Scrollbar ──────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-light); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }

/* ── Responsive ─────────────────────────────────────────────────── */
@media (max-width: 768px) {
  :root { --sidebar-w: 280px; }
  #sidebar { position: fixed; left: 0; top: 0; bottom: 0; z-index: 100; }
  #sidebar.hidden { margin-left: 0; transform: translateX(-100%); }
  #chat-area { padding: 0 16px; }
  #messages { padding: 0; }
  /* Collapse role label from absolute overhang to inline above body */
  .msg-role {
    position: static; width: auto; text-align: left;
    margin-bottom: 2px;
  }
  .msg-arrow-left { left: -16px; }
  .msg-arrow-right { right: -16px; }
  #input-area { padding: 10px 16px 12px; }
}
