/* CSS Variables */
:root {
  --bg-primary: #ffffff;
  --bg-secondary: #f9fafb;
  --bg-sidebar: #f3f4f6;
  --bg-hover: #e5e7eb;
  --bg-message-user: #eff6ff;
  --bg-message-assistant: #ffffff;
  --bg-input: #ffffff;
  --bg-card: #ffffff;
  --border-color: #e5e7eb;
  --border-input: #d1d5db;
  --text-primary: #111827;
  --text-secondary: #6b7280;
  --text-placeholder: #9ca3af;
  --accent-color: #2563eb;
  --accent-hover: #1d4ed8;
  --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
  --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --sidebar-width: 260px;
  --header-height: 56px;
  --input-height: auto;
  --transition: 0.2s ease;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: "SF Mono", Monaco, "Cascadia Code", "Roboto Mono", Consolas, "Courier New", monospace;
}

[data-theme="dark"] {
  --bg-primary: #0d0d0d;
  --bg-secondary: #171717;
  --bg-sidebar: #1a1a1a;
  --bg-hover: #262626;
  --bg-message-user: #1e3a5f;
  --bg-message-assistant: #1a1a1a;
  --bg-input: #262626;
  --bg-card: #1a1a1a;
  --border-color: #333333;
  --border-input: #404040;
  --text-primary: #f3f4f6;
  --text-secondary: #a1a1aa;
  --text-placeholder: #71717a;
  --accent-color: #3b82f6;
  --accent-hover: #60a5fa;
  --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.3);
  --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.4), 0 2px 4px -2px rgb(0 0 0 / 0.4);
  --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.5), 0 4px 6px -4px rgb(0 0 0 / 0.5);
}

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

html, body {
  height: 100%;
  font-family: var(--font-sans);
  background: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
}

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

/* Sidebar */
.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: transform var(--transition);
  z-index: 100;
}

.sidebar-header {
  padding: 12px;
  border-bottom: 1px solid var(--border-color);
}

.btn-new-chat {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 14px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  background: var(--bg-card);
  color: var(--text-primary);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
}

.btn-new-chat:hover {
  background: var(--bg-hover);
  border-color: var(--text-secondary);
}

.sidebar-history {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--transition);
  margin-bottom: 2px;
  position: relative;
  user-select: none;
}

.history-item:hover {
  background: var(--bg-hover);
}

.history-item.active {
  background: var(--bg-hover);
}

.history-item-icon {
  flex-shrink: 0;
  width: 16px;
  height: 16px;
  color: var(--text-secondary);
}

.history-item-text {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  color: var(--text-primary);
}

.history-item-actions {
  display: none;
  gap: 4px;
}

.history-item:hover .history-item-actions {
  display: flex;
}

.history-item-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 2px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
}

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

.sidebar-footer {
  padding: 12px;
  border-top: 1px solid var(--border-color);
  display: flex;
  gap: 8px;
  justify-content: space-between;
}

/* Main content */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg-primary);
}

/* Top bar */
.topbar {
  height: var(--header-height);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  background: var(--bg-primary);
  flex-shrink: 0;
  gap: 12px;
}

.btn-menu {
  display: none;
  background: none;
  border: none;
  color: var(--text-primary);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius-sm);
}

.btn-menu:hover {
  background: var(--bg-hover);
}

.topbar-center {
  flex: 1;
  display: flex;
  justify-content: center;
  min-width: 0;
}

.select-group {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 4px 12px;
  max-width: 420px;
  width: 100%;
}

.select-platform {
  background: transparent;
  border: none;
  color: var(--text-primary);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  outline: none;
  padding: 4px 0;
  flex-shrink: 0;
}

.input-model {
  flex: 1;
  background: transparent;
  border: none;
  border-left: 1px solid var(--border-color);
  color: var(--text-primary);
  font-size: 13px;
  padding: 4px 0 4px 10px;
  outline: none;
  min-width: 0;
}

.input-model::placeholder {
  color: var(--text-placeholder);
}

.topbar-actions {
  display: flex;
  gap: 4px;
}

/* Messages area */
.messages-container {
  flex: 1;
  overflow-y: auto;
  scroll-behavior: smooth;
}

.welcome-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100%;
  padding: 40px 20px;
}

.welcome-content {
  text-align: center;
  max-width: 600px;
  width: 100%;
}

.welcome-title {
  font-size: 36px;
  font-weight: 700;
  margin-bottom: 8px;
  background: linear-gradient(135deg, var(--accent-color), #8b5cf6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.welcome-subtitle {
  font-size: 16px;
  color: var(--text-secondary);
  margin-bottom: 40px;
}

.welcome-cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

.welcome-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 24px 16px;
  cursor: pointer;
  transition: all var(--transition);
}

.welcome-card:hover {
  border-color: var(--accent-color);
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.welcome-card-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 700;
  font-size: 18px;
  margin: 0 auto 12px;
}

.welcome-card-title {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 4px;
}

.welcome-card-desc {
  font-size: 12px;
  color: var(--text-secondary);
}

.messages-list {
  display: flex;
  flex-direction: column;
  padding-bottom: 20px;
}

.message {
  display: flex;
  gap: 16px;
  padding: 20px;
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.message.user {
  background: var(--bg-message-user);
}

.message.assistant {
  background: var(--bg-message-assistant);
  border-bottom: 1px solid var(--border-color);
}

.message-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 600;
  color: #fff;
  flex-shrink: 0;
  margin-top: 2px;
}

.message-avatar.user {
  background: var(--accent-color);
}

.message-avatar.assistant {
  background: #10b981;
}

.message-content {
  flex: 1;
  min-width: 0;
  line-height: 1.7;
  font-size: 15px;
  color: var(--text-primary);
}

.message-content p {
  margin-bottom: 0.75em;
}

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

.message-content pre {
  background: #1e1e1e;
  border-radius: var(--radius-md);
  padding: 16px;
  overflow-x: auto;
  margin: 12px 0;
  position: relative;
}

.message-content pre code {
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.6;
  background: transparent;
  padding: 0;
}

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

.message-content pre code {
  padding: 0;
  background: transparent;
}

.message-content .code-block-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  background: #2d2d2d;
  border-radius: var(--radius-md) var(--radius-md) 0 0;
  font-size: 12px;
  color: #a1a1aa;
  margin-bottom: -12px;
}

.message-content .code-block-header + pre {
  border-radius: 0 0 var(--radius-md) var(--radius-md);
  margin-top: 0;
}

.message-content .btn-copy-code {
  background: none;
  border: 1px solid #555;
  color: #a1a1aa;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.15s;
}

.message-content .btn-copy-code:hover {
  border-color: #888;
  color: #fff;
}

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

.message-content li {
  margin: 4px 0;
}

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

.message-content table {
  width: 100%;
  border-collapse: collapse;
  margin: 12px 0;
  font-size: 14px;
}

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

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

.message-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  font-size: 12px;
  color: var(--text-secondary);
}

.message-actions {
  display: flex;
  gap: 4px;
  margin-top: 8px;
}

.message-actions button {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 4px;
  transition: all var(--transition);
}

.message-actions button:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.typing-indicator {
  display: flex;
  gap: 4px;
  padding: 8px 0;
}

.typing-indicator span {
  width: 8px;
  height: 8px;
  background: var(--text-placeholder);
  border-radius: 50%;
  animation: typingBounce 1.4s infinite ease-in-out both;
}

.typing-indicator span:nth-child(1) { animation-delay: -0.32s; }
.typing-indicator span:nth-child(2) { animation-delay: -0.16s; }

@keyframes typingBounce {
  0%, 80%, 100% { transform: scale(0); }
  40% { transform: scale(1); }
}

/* Input area */
.input-area {
  border-top: 1px solid var(--border-color);
  background: var(--bg-primary);
  padding: 16px 20px 12px;
  flex-shrink: 0;
}

.input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--bg-input);
  border: 1px solid var(--border-input);
  border-radius: var(--radius-lg);
  padding: 10px 14px;
  max-width: 768px;
  margin: 0 auto;
  transition: border-color var(--transition);
}

.input-wrapper:focus-within {
  border-color: var(--accent-color);
  box-shadow: 0 0 0 3px rgb(37 99 235 / 0.1);
}

.input-textarea {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  resize: none;
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.5;
  color: var(--text-primary);
  max-height: 200px;
  min-height: 24px;
  padding: 2px 0;
}

.input-textarea::placeholder {
  color: var(--text-placeholder);
}

.input-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
  padding-bottom: 2px;
}

.btn-send, .btn-stop {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all var(--transition);
  color: #fff;
}

.btn-send {
  background: var(--accent-color);
}

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

.btn-send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-stop {
  background: #ef4444;
}

.btn-stop:hover {
  background: #dc2626;
}

.input-hint {
  text-align: center;
  font-size: 12px;
  color: var(--text-placeholder);
  margin-top: 6px;
}

/* Buttons */
.btn-icon {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  border: none;
  background: none;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition);
}

.btn-icon:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

/* Modal */
.modal {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 1000;
  align-items: center;
  justify-content: center;
}

.modal.active {
  display: flex;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
}

.modal-content {
  position: relative;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  width: 90%;
  max-width: 440px;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
  animation: modalIn 0.2s ease;
}

@keyframes modalIn {
  from { opacity: 0; transform: scale(0.96); }
  to { opacity: 1; transform: scale(1); }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-color);
}

.modal-header h3 {
  font-size: 16px;
  font-weight: 600;
}

.btn-close {
  background: none;
  border: none;
  font-size: 22px;
  color: var(--text-secondary);
  cursor: pointer;
  line-height: 1;
  padding: 0 4px;
}

.btn-close:hover {
  color: var(--text-primary);
}

.modal-body {
  padding: 16px 20px;
}

.setting-item {
  margin-bottom: 16px;
}

.setting-item label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  margin-bottom: 6px;
  color: var(--text-secondary);
}

.setting-item input[type="number"],
.setting-item textarea {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border-input);
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  color: var(--text-primary);
  font-size: 14px;
  outline: none;
  font-family: var(--font-sans);
}

.setting-item input[type="number"]:focus,
.setting-item textarea:focus {
  border-color: var(--accent-color);
}

.setting-range {
  display: flex;
  align-items: center;
  gap: 12px;
}

.setting-range input[type="range"] {
  flex: 1;
  accent-color: var(--accent-color);
}

.setting-range span {
  font-size: 14px;
  font-variant-numeric: tabular-nums;
  min-width: 32px;
  text-align: right;
}

/* Sidebar overlay for mobile */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 99;
}

/* Scrollbar */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

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

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

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

/* Responsive */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    transform: translateX(-100%);
  }

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

  .sidebar-overlay.active {
    display: block;
  }

  .btn-menu {
    display: flex;
  }

  .welcome-cards {
    grid-template-columns: 1fr;
  }

  .message {
    padding: 16px 12px;
    gap: 12px;
  }

  .topbar {
    padding: 0 12px;
  }

  .select-group {
    padding: 4px 8px;
  }

  .input-area {
    padding: 12px;
  }
}

/* Empty state */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-secondary);
}

.empty-state svg {
  width: 48px;
  height: 48px;
  margin-bottom: 16px;
  opacity: 0.5;
}

/* Toast notification */
.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: #1f2937;
  color: #fff;
  padding: 10px 20px;
  border-radius: var(--radius-md);
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: all 0.3s ease;
  z-index: 2000;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
