:root {
  --bg-primary: #0d1117;
  --bg-secondary: #161b22;
  --bg-tertiary: #21262d;
  --border: #30363d;
  --text-primary: #e6edf3;
  --text-secondary: #8b949e;
  --accent: #58a6ff;
  --accent-hover: #79c0ff;
  --success: #3fb950;
  --error: #f85149;
  --warning: #d29922;
  --user-msg-bg: #1f3a5f;
  --agent-msg-bg: #1c2128;
  --font-mono: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace;
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
  --sidebar-width: 240px;
  --radius: 8px;
}

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

body {
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  color: var(--text-primary);
  background: var(--bg-primary);
  height: 100vh;
  overflow: hidden;
}

#app {
  display: flex;
  height: 100vh;
}

/* --- Sidebar --- */

#sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--bg-secondary);
  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-header h2 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

#new-session-btn {
  width: 28px;
  height: 28px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-tertiary);
  color: var(--text-primary);
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, border-color 0.15s;
}

#new-session-btn:hover {
  background: var(--border);
  border-color: var(--text-secondary);
}

#session-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.session-item {
  padding: 10px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 13px;
  color: var(--text-secondary);
  transition: background 0.15s, color 0.15s;
  border-left: 3px solid transparent;
  margin-bottom: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-item:hover {
  background: var(--bg-tertiary);
  color: var(--text-primary);
}

.session-item.active {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border-left-color: var(--accent);
}

.session-time {
  display: block;
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* --- Chat Area --- */

#chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  scroll-padding-bottom: 80px;
}

#messages:empty::after {
  content: 'Start a conversation...';
  display: block;
  text-align: center;
  color: var(--text-secondary);
  margin-top: 40vh;
  font-size: 16px;
}

/* --- Messages --- */

.message {
  max-width: 75%;
  margin-bottom: 16px;
  padding: 12px 16px;
  border-radius: var(--radius);
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.message-user {
  background: var(--user-msg-bg);
  margin-left: auto;
  border-bottom-right-radius: 2px;
}

.message-agent {
  background: var(--agent-msg-bg);
  margin-right: auto;
  border-bottom-left-radius: 2px;
  border: 1px solid var(--border);
}

.message-error {
  background: transparent;
  border: 1px solid var(--error);
  color: var(--error);
  margin-right: auto;
  font-size: 13px;
}

/* --- Markdown Content --- */

.message h1, .message h2, .message h3, .message h4 {
  margin-top: 16px;
  margin-bottom: 8px;
  font-weight: 600;
}

.message h1 { font-size: 1.4em; }
.message h2 { font-size: 1.2em; }
.message h3 { font-size: 1.1em; }

.message h1:first-child,
.message h2:first-child,
.message h3:first-child {
  margin-top: 0;
}

.message p {
  margin-bottom: 8px;
}

.message p:last-child {
  margin-bottom: 0;
}

.message ul, .message ol {
  margin: 8px 0;
  padding-left: 24px;
}

.message li {
  margin-bottom: 4px;
}

.message a {
  color: var(--accent);
  text-decoration: none;
}

.message a:hover {
  text-decoration: underline;
}

.message strong {
  font-weight: 600;
}

.message em {
  font-style: italic;
}

.message blockquote {
  border-left: 3px solid var(--border);
  padding-left: 12px;
  margin: 8px 0;
  color: var(--text-secondary);
}

.message code {
  font-family: var(--font-mono);
  font-size: 0.9em;
  background: var(--bg-tertiary);
  padding: 2px 6px;
  border-radius: 4px;
}

.message pre {
  margin: 8px 0;
  padding: 12px 16px;
  background: var(--bg-tertiary);
  border-radius: var(--radius);
  overflow-x: auto;
  border: 1px solid var(--border);
}

.message pre code {
  background: none;
  padding: 0;
  font-size: 13px;
  line-height: 1.5;
}

.message table {
  border-collapse: collapse;
  margin: 8px 0;
  width: 100%;
}

.message th, .message td {
  border: 1px solid var(--border);
  padding: 6px 12px;
  text-align: left;
}

.message th {
  background: var(--bg-tertiary);
  font-weight: 600;
}

.message hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 12px 0;
}

/* --- Tool Calls --- */

.tool-call {
  border-left: 3px solid var(--accent);
  padding: 8px 12px;
  margin: 8px 0;
  background: var(--bg-tertiary);
  border-radius: 0 var(--radius) var(--radius) 0;
  font-size: 13px;
}

.tool-call-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  color: var(--accent);
}

.tool-call-spinner {
  width: 14px;
  height: 14px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.tool-call-done .tool-call-header {
  color: var(--success);
}

.tool-call-checkmark::before {
  content: '\2713';
  color: var(--success);
  font-weight: bold;
}

.tool-call-result {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  display: none;
  font-family: var(--font-mono);
  font-size: 12px;
  white-space: pre-wrap;
  color: var(--text-secondary);
  max-height: 200px;
  overflow-y: auto;
}

.tool-call-result.expanded {
  display: block;
}

.tool-call-toggle {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 12px;
  margin-top: 4px;
  padding: 0;
}

.tool-call-toggle:hover {
  color: var(--text-primary);
}

/* --- Thinking --- */

.thinking-block {
  display: none;
  margin: 8px 0;
}

.thinking-block.has-content {
  display: block;
}

.thinking-toggle {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 12px;
  padding: 4px 10px;
  transition: color 0.15s, border-color 0.15s;
}

.thinking-toggle:hover {
  color: var(--text-primary);
  border-color: var(--text-secondary);
}

.thinking-content {
  display: none;
  margin-top: 8px;
  padding: 10px 12px;
  background: var(--bg-tertiary);
  border-radius: var(--radius);
  font-size: 13px;
  color: var(--text-secondary);
  font-style: italic;
  border: 1px solid var(--border);
  max-height: 300px;
  overflow-y: auto;
}

.thinking-content.expanded {
  display: block;
}

/* --- Streaming Cursor --- */

.streaming-cursor::after {
  content: '';
  display: inline-block;
  width: 2px;
  height: 1em;
  background: var(--accent);
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: blink 1s step-end infinite;
}

@keyframes blink {
  50% { opacity: 0; }
}

/* --- Input Area --- */

#input-area {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 24px;
  border-top: 1px solid var(--border);
  background: var(--bg-secondary);
}

#message-input {
  flex: 1;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 14px;
  padding: 10px 14px;
  resize: none;
  max-height: 200px;
  line-height: 1.5;
  outline: none;
  transition: border-color 0.15s;
}

#message-input:focus {
  border-color: var(--accent);
}

#message-input::placeholder {
  color: var(--text-secondary);
}

#send-btn {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--bg-primary);
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
  flex-shrink: 0;
}

#send-btn:hover {
  background: var(--accent-hover);
}

#send-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* --- Status Bar --- */

#status-bar {
  padding: 4px 24px;
  font-size: 12px;
  color: var(--text-secondary);
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 6px;
}

#connection-status::before {
  content: '';
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 6px;
  vertical-align: middle;
}

#connection-status.connected::before {
  background: var(--success);
}

#connection-status.disconnected::before {
  background: var(--error);
}

#connection-status.reconnecting::before {
  background: var(--warning);
  animation: blink 1s step-end infinite;
}

/* --- Search Panel --- */

#search-panel {
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  max-height: 50vh;
  overflow: hidden;
  transition: max-height 0.25s ease;
}

#search-panel.collapsed {
  max-height: 0;
  border-top: none;
}

.search-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 24px;
  border-bottom: 1px solid var(--border);
}

#search-input {
  flex: 1;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 14px;
  padding: 8px 12px;
  outline: none;
}

#search-input:focus {
  border-color: var(--accent);
}

#search-close {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 20px;
  cursor: pointer;
  padding: 0 4px;
}

#search-close:hover {
  color: var(--text-primary);
}

#search-results {
  overflow-y: auto;
  max-height: calc(50vh - 60px);
  padding: 8px 24px;
}

.search-result-item {
  padding: 10px 12px;
  border-radius: var(--radius);
  margin-bottom: 8px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  cursor: pointer;
  transition: border-color 0.15s;
}

.search-result-item:hover {
  border-color: var(--accent);
}

.search-result-content {
  font-size: 13px;
  color: var(--text-primary);
  margin-bottom: 4px;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.search-result-meta {
  font-size: 11px;
  color: var(--text-secondary);
}

.search-result-score {
  color: var(--accent);
  font-weight: 600;
}

/* --- Scrollbar --- */

::-webkit-scrollbar {
  width: 8px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-secondary);
}

/* --- Mobile --- */

@media (max-width: 768px) {
  #sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 100;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
  }

  #sidebar.open {
    transform: translateX(0);
  }

  .sidebar-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 99;
    display: none;
  }

  .sidebar-backdrop.visible {
    display: block;
  }

  #mobile-menu-btn {
    display: flex;
  }

  .message {
    max-width: 90%;
  }

  #messages {
    padding: 16px;
  }

  #input-area {
    padding: 10px 16px;
  }

  #status-bar {
    padding: 4px 16px;
  }
}

@media (min-width: 769px) {
  #mobile-menu-btn {
    display: none;
  }
}
