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

:root {
  --bg: #0f0f0f;
  --surface: #1a1a1a;
  --surface2: #242424;
  --border: #2e2e2e;
  --text: #e8e8e8;
  --text-muted: #888;
  --accent: #7c6ff7;
  --accent-hover: #9b96f9;
  --user-bubble: #2a2640;
  --assistant-bubble: #1e1e1e;
  --error: #e06c75;
  --tool: #3a3a2e;
  --tool-text: #e5c07b;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --radius: 12px;
}

html,
body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
}

/* Layout */
.app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  width: 260px;
  min-width: 260px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid var(--border);
}
.sidebar-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--accent);
}
.btn-new {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  width: 28px;
  height: 28px;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.btn-new:hover {
  background: var(--accent-hover);
}
.sidebar-loading,
.sidebar-empty {
  padding: 24px 16px;
  color: var(--text-muted);
  font-size: 13px;
  line-height: 1.5;
}
.thread-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  list-style: none;
}
.thread-item {
  position: relative;
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  margin-bottom: 2px;
}
.thread-item:hover {
  background: var(--surface2);
}
.thread-item-active {
  background: var(--surface2);
}
.thread-title {
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-right: 20px;
}
.thread-meta {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 3px;
}
.thread-delete {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 16px;
  opacity: 0;
  transition: opacity 0.15s;
  padding: 2px 4px;
}
.thread-item:hover .thread-delete {
  opacity: 1;
}
.thread-delete:hover {
  color: var(--error);
}

/* Main */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.main-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.active-thread {
  font-size: 12px;
  color: var(--text-muted);
  font-family: monospace;
}
.btn-config {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 6px;
  padding: 4px 10px;
  cursor: pointer;
  font-size: 12px;
}
.btn-config:hover {
  color: var(--text);
  border-color: var(--text-muted);
}

/* Error bar */
.error-bar {
  background: #3a1a1a;
  color: var(--error);
  padding: 8px 16px;
  font-size: 13px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
}
.error-dismiss {
  font-size: 16px;
}

/* Chat */
.chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.message-list {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.chat-empty {
  color: var(--text-muted);
  text-align: center;
  margin-top: 40px;
  font-size: 13px;
}

/* Messages */
.message {
  display: flex;
  max-width: 80%;
}
.message-user {
  align-self: flex-end;
}
.message-assistant {
  align-self: flex-start;
}
.message-bubble {
  padding: 10px 14px;
  border-radius: var(--radius);
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}
.message-user .message-bubble {
  background: var(--user-bubble);
  border-bottom-right-radius: 4px;
}
.message-assistant .message-bubble {
  background: var(--assistant-bubble);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.streaming .message-bubble {
  border-color: var(--accent);
}
.cursor {
  animation: blink 1s step-end infinite;
}
@keyframes blink {
  50% {
    opacity: 0;
  }
}

/* Markdown rendered content */
.markdown {
  line-height: 1.6;
  color: var(--text);
}
.markdown p {
  margin-bottom: 0.75em;
}
.markdown p:last-child {
  margin-bottom: 0;
}
.markdown strong {
  font-weight: 600;
  color: #fff;
}
.markdown em {
  font-style: italic;
}
.markdown h1,
.markdown h2,
.markdown h3,
.markdown h4 {
  font-weight: 600;
  margin: 1em 0 0.4em;
  color: #fff;
}
.markdown h1 {
  font-size: 1.2em;
}
.markdown h2 {
  font-size: 1.1em;
}
.markdown h3 {
  font-size: 1em;
}
.markdown ul,
.markdown ol {
  padding-left: 1.4em;
  margin-bottom: 0.75em;
}
.markdown li {
  margin-bottom: 0.25em;
}
.markdown code {
  background: #2a2a2a;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
  font-family: monospace;
  font-size: 0.88em;
}
.markdown pre {
  background: #1a1a1a;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  overflow-x: auto;
  margin-bottom: 0.75em;
}
.markdown pre code {
  background: none;
  border: none;
  padding: 0;
}
.markdown blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 12px;
  color: var(--text-muted);
  margin: 0.5em 0;
}
.markdown a {
  color: var(--accent);
  text-decoration: underline;
}
.markdown table {
  border-collapse: collapse;
  width: 100%;
  margin-bottom: 0.75em;
  font-size: 13px;
}
.markdown th,
.markdown td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}
.markdown th {
  background: var(--surface2);
  font-weight: 600;
}
.streaming .markdown {
  display: inline;
}

/* Tool / status events */
.tool-event {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  font-size: 12px;
  color: var(--tool-text);
  background: var(--tool);
  border-radius: 8px;
  padding: 6px 10px;
  align-self: flex-start;
}
.tool-result-event {
  opacity: 0.7;
}
.tool-icon {
  font-size: 11px;
  margin-top: 1px;
}
.tool-name {
  font-family: monospace;
}
.tool-args {
  font-family: monospace;
  font-size: 11px;
  margin-top: 4px;
  white-space: pre-wrap;
  max-height: 120px;
  overflow-y: auto;
  color: var(--text-muted);
}
.status-event {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-muted);
  align-self: flex-start;
  padding: 4px 0;
}
.status-spinner {
  animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.3;
  }
}

/* Reasoning / Thinking */
.reasoning-block {
  align-self: flex-start;
  max-width: 80%;
  font-size: 12px;
  color: var(--text-muted);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 8px;
  padding: 6px 10px;
}
.reasoning-block summary {
  cursor: pointer;
  font-style: italic;
  user-select: none;
}
.reasoning-block[open] summary {
  margin-bottom: 4px;
}
.reasoning-text {
  white-space: pre-wrap;
  margin-top: 2px;
  line-height: 1.5;
}
.reasoning-text .markdown {
  font-size: 12px;
  color: var(--text-muted);
}

/* Input bar */
.input-bar {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.input-text {
  flex: 1;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  padding: 10px 14px;
  font-size: 14px;
  font-family: var(--font);
  resize: none;
  outline: none;
  max-height: 120px;
  overflow-y: auto;
}
.input-text:focus {
  border-color: var(--accent);
}
.input-text:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.btn-send {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 10px 18px;
  font-size: 14px;
  cursor: pointer;
  font-family: var(--font);
}
.btn-send:hover:not(:disabled) {
  background: var(--accent-hover);
}
.btn-send:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* Config overlay */
.config-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.config-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  width: 460px;
  max-width: 95vw;
}
.config-panel h2 {
  font-size: 16px;
  margin-bottom: 8px;
}
.config-hint {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 20px;
}
.config-panel label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 14px;
}
.config-panel input,
.config-panel textarea {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 8px 12px;
  font-size: 13px;
  font-family: monospace;
  outline: none;
}
.config-panel input:focus,
.config-panel textarea:focus {
  border-color: var(--accent);
}
.btn-save {
  width: 100%;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 10px;
  font-size: 14px;
  cursor: pointer;
  margin-top: 4px;
}
.btn-save:hover {
  background: var(--accent-hover);
}

/* Environment selector */
.env-selector {
  display: flex;
  gap: 6px;
  margin-top: 2px;
}
.env-option {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 5px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-muted);
  flex-direction: row;
  margin-bottom: 0;
}
.env-option input[type="radio"] {
  display: none;
}
.env-option:hover {
  border-color: var(--text-muted);
  color: var(--text);
}
.env-option-active {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(124, 111, 247, 0.08);
}

/* Header actions */
.header-actions {
  display: flex;
  gap: 6px;
  align-items: center;
}
.btn-debug {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 6px;
  padding: 4px 10px;
  cursor: pointer;
  font-size: 12px;
  font-family: monospace;
}
.btn-debug:hover {
  color: var(--text);
  border-color: var(--text-muted);
}
.btn-debug-active {
  color: var(--accent);
  border-color: var(--accent);
}

/* Main content area (chat + debug side-by-side) */
.main-content {
  flex: 1;
  display: flex;
  overflow: hidden;
}
.main-content > .chat {
  flex: 1;
  min-width: 0;
}

/* Debug panel */
.debug-panel {
  width: 420px;
  min-width: 320px;
  max-width: 50vw;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.debug-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface2);
}
.debug-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}
.debug-tabs {
  display: flex;
  gap: 2px;
  flex: 1;
  margin-left: 8px;
}
.debug-tab {
  background: none;
  border: 1px solid transparent;
  color: var(--text-muted);
  border-radius: 6px;
  padding: 3px 10px;
  cursor: pointer;
  font-size: 12px;
}
.debug-tab:hover {
  color: var(--text);
}
.debug-tab-active {
  color: var(--accent);
  border-color: var(--accent);
  background: rgba(124, 111, 247, 0.08);
}
.debug-close {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 14px;
  padding: 2px 6px;
  margin-left: auto;
  font-family: monospace;
}
.debug-close:hover {
  color: var(--text);
}
.debug-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
}
.debug-loading,
.debug-empty {
  color: var(--text-muted);
  font-size: 13px;
  padding: 20px 0;
  text-align: center;
}
.debug-section {
  font-size: 13px;
}
.debug-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 12px;
}
.debug-table td {
  padding: 4px 8px;
  border-bottom: 1px solid var(--border);
}
.debug-label {
  color: var(--text-muted);
  font-size: 12px;
  white-space: nowrap;
  width: 80px;
}
.debug-subsection {
  margin-top: 12px;
}
.debug-subtitle {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.debug-muted {
  color: var(--text-muted);
  font-size: 12px;
  font-style: italic;
  margin-top: 8px;
}
.debug-code {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px;
  font-family: monospace;
  font-size: 11px;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-y: auto;
  max-height: 600px;
  color: var(--text-muted);
  line-height: 1.5;
}
.debug-prompt {
  max-height: none;
}
.debug-prompt-stats {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 8px;
  font-family: monospace;
}
