/* ===== CSS Variables / Theme ===== */
:root {
  --bg-primary: #212121;
  --bg-secondary: #171717;
  --bg-tertiary: #2f2f2f;
  --bg-hover: #3a3a3a;
  --bg-active: #424242;
  --text-primary: #ececec;
  --text-secondary: #b4b4b4;
  --text-tertiary: #8e8e8e;
  --text-placeholder: #8e8e8e;
  --accent: #a855f7;
  --accent-hover: #9333ea;
  --border: #424242;
  --border-light: #2f2f2f;
  --input-bg: #404040;
  --input-focus: #4a4a4a;
  --sidebar-bg: #171717;
  --sidebar-hover: #212121;
  --sidebar-active: #343434;
  --scrollbar-thumb: #565656;
  --scrollbar-track: transparent;
  --shadow: 0 2px 12px rgba(0,0,0,0.4);
  --radius: 12px;
  --radius-sm: 8px;
  --transition: 0.2s ease;
  --sidebar-width: 260px;
  --font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-size: 16px;
}

/* Light Theme */
[data-theme="light"] {
  --bg-primary: #ffffff;
  --bg-secondary: #f7f7f8;
  --bg-tertiary: #ececec;
  --bg-hover: #e0e0e0;
  --bg-active: #d4d4d4;
  --text-primary: #1a1a1a;
  --text-secondary: #555555;
  --text-tertiary: #888888;
  --text-placeholder: #999999;
  --accent: #9333ea;
  --accent-hover: #7e22ce;
  --border: #e0e0e0;
  --border-light: #ececec;
  --input-bg: #f0f0f0;
  --input-focus: #e5e5e5;
  --sidebar-bg: #f7f7f8;
  --sidebar-hover: #ececec;
  --sidebar-active: #e0e0e0;
  --scrollbar-thumb: #c0c0c0;
  --scrollbar-track: transparent;
  --shadow: 0 2px 12px rgba(0,0,0,0.08);
}

/* ===== Reset & Base ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font-family);
  font-size: var(--font-size, 16px);
  color: var(--text-primary);
  background: var(--bg-primary);
  display: flex;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  transition: background 0.3s ease, color 0.3s ease;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

textarea {
  font-family: inherit;
  resize: none;
  border: none;
  outline: none;
  background: none;
  color: inherit;
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar {
  width: 6px;
}
::-webkit-scrollbar-track {
  background: var(--scrollbar-track);
}
::-webkit-scrollbar-thumb {
  background: var(--scrollbar-thumb);
  border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
  background: #6a6a6a;
}

/* ===== Sidebar ===== */
.sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  height: 100vh;
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  transition: margin-left var(--transition), opacity var(--transition), background 0.3s ease;
  z-index: 100;
  border-right: 1px solid var(--border-light);
}

.sidebar.collapsed {
  margin-left: calc(-1 * var(--sidebar-width));
  opacity: 0;
  pointer-events: none;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 10px 10px 10px;
  gap: 4px;
}

.new-chat-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  color: var(--text-primary);
  font-size: 14px;
  font-weight: 500;
  flex: 1;
  transition: background var(--transition);
  white-space: nowrap;
}

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

.sidebar-toggle-btn {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  transition: background var(--transition);
  flex-shrink: 0;
}

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

/* Chat List */
.sidebar-chats {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px;
}

.chat-item {
  display: flex;
  align-items: center;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 14px;
  color: var(--text-secondary);
  transition: background var(--transition);
  gap: 8px;
  position: relative;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-item:hover {
  background: var(--sidebar-hover);
}

.chat-item.active {
  background: var(--sidebar-active);
  color: var(--text-primary);
}

.chat-item-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}

.chat-item-delete {
  display: none;
  width: 28px;
  height: 28px;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  flex-shrink: 0;
  transition: background var(--transition);
}

.chat-item:hover .chat-item-delete {
  display: flex;
}

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

.chat-item-delete svg {
  width: 16px;
  height: 16px;
  color: var(--text-tertiary);
}

.chat-date-header {
  padding: 8px 12px 4px;
  font-size: 12px;
  color: var(--text-tertiary);
  font-weight: 600;
}

/* Sidebar Footer */
/* Settings Button */
.sidebar-footer {
  padding: 10px;
  border-top: 1px solid var(--border-light);
}

.settings-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  width: 100%;
  font-size: 14px;
  color: var(--text-secondary);
  transition: background var(--transition), color var(--transition);
  margin-bottom: 4px;
}

.settings-btn:hover {
  background: var(--sidebar-hover);
  color: var(--text-primary);
}

.user-profile {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--transition);
  font-size: 14px;
}

.user-profile:hover {
  background: var(--sidebar-hover);
}

/* ===== Settings Modal ===== */
.settings-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
  animation: fadeIn 0.2s ease;
}

.settings-overlay.hidden {
  display: none;
}

.settings-modal {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 16px;
  width: 90%;
  max-width: 480px;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
  animation: modalIn 0.25s ease;
}

@keyframes modalIn {
  from { opacity: 0; transform: scale(0.95) translateY(10px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

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

.settings-header h2 {
  font-size: 18px;
  font-weight: 600;
  margin: 0;
  color: var(--text-primary);
}

.settings-close {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  color: var(--text-tertiary);
  transition: background var(--transition), color var(--transition);
}

.settings-close:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.settings-body {
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.setting-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

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

.setting-icon {
  font-size: 22px;
  flex-shrink: 0;
}

.setting-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
}

.setting-desc {
  font-size: 13px;
  color: var(--text-tertiary);
  margin-top: 2px;
}

/* Theme Options */
.theme-options {
  display: flex;
  gap: 12px;
}

.theme-option {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 12px;
  border-radius: var(--radius-sm);
  border: 2px solid var(--border);
  background: transparent;
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 500;
  transition: all var(--transition);
}

.theme-option:hover {
  border-color: var(--text-tertiary);
}

.theme-option.active {
  border-color: var(--accent);
  color: var(--text-primary);
  background: color-mix(in srgb, var(--accent) 10%, transparent);
}

.theme-preview {
  width: 100%;
  height: 60px;
  border-radius: 6px;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.dark-preview {
  background: #1a1a2e;
}

.light-preview {
  background: #f0f0f0;
  border: 1px solid #ddd;
}

.tp-bar {
  width: 100%;
  height: 8px;
  border-radius: 2px;
}

.dark-preview .tp-bar { background: #2a2a4a; }
.light-preview .tp-bar { background: #ddd; }

.tp-line {
  width: 70%;
  height: 5px;
  border-radius: 2px;
}

.dark-preview .tp-line { background: #3a3a5a; }
.light-preview .tp-line { background: #ccc; }

.tp-line.short { width: 45%; }

/* Font Size Options */
.font-size-options {
  display: flex;
  gap: 8px;
}

.font-btn {
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  border: 2px solid var(--border);
  background: transparent;
  color: var(--text-secondary);
  font-weight: 600;
  transition: all var(--transition);
}

.font-btn[data-size="14"] { font-size: 12px; }
.font-btn[data-size="16"] { font-size: 14px; }
.font-btn[data-size="18"] { font-size: 16px; }

.font-btn:hover {
  border-color: var(--text-tertiary);
}

.font-btn.active {
  border-color: var(--accent);
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 10%, transparent);
}

/* Danger Button */
.danger-btn {
  padding: 10px 20px;
  border-radius: var(--radius-sm);
  border: 1px solid #ef4444;
  background: transparent;
  color: #ef4444;
  font-size: 14px;
  font-weight: 500;
  transition: all var(--transition);
  align-self: flex-start;
}

.danger-btn:hover {
  background: #ef4444;
  color: white;
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
  color: white;
  flex-shrink: 0;
}

/* ===== Main Content ===== */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
  position: relative;
}

/* Welcome Screen */
.welcome-screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  gap: 24px;
}

.welcome-logo {
  color: var(--text-primary);
  animation: logoFloat 3s ease-in-out infinite;
}

@keyframes logoFloat {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}

.welcome-screen h1 {
  font-size: 28px;
  font-weight: 600;
  color: var(--text-primary);
  text-align: center;
}

.welcome-title .welcome-accent {
  background: linear-gradient(135deg, #a855f7, #06b6d4);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.welcome-suggestions {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  max-width: 580px;
  width: 100%;
  margin-top: 8px;
}

@media (max-width: 640px) {
  .welcome-suggestions {
    grid-template-columns: 1fr;
  }
}

.suggestion-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-secondary);
  font-size: 14px;
  text-align: left;
  transition: all var(--transition);
  line-height: 1.4;
}

.suggestion-card:hover {
  background: var(--bg-tertiary);
  border-color: var(--text-tertiary);
  color: var(--text-primary);
}

.suggestion-icon {
  font-size: 20px;
  flex-shrink: 0;
}

.suggestion-text {
  flex: 1;
}

/* ===== Chat Messages ===== */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  display: none;
  flex-direction: column;
}

.chat-messages.active {
  display: flex;
}

.message {
  display: flex;
  padding: 24px 0;
  width: 100%;
  max-width: 768px;
  margin: 0 auto;
  animation: fadeIn 0.3s ease;
}

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

.message + .message {
  border-top: 1px solid var(--border-light);
}

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

.message.user-message .message-avatar {
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  color: white;
}

.message.assistant-message .message-avatar {
  background: linear-gradient(135deg, #7c3aed, #a855f7, #06b6d4);
  color: white;
  padding: 0;
}

.message-content {
  flex: 1;
  min-width: 0;
}

.message-role {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 4px;
  color: var(--text-primary);
}

.message-text {
  font-size: var(--font-size, 16px);
  line-height: 1.75;
  color: var(--text-primary);
  word-wrap: break-word;
}

.message-text p {
  margin-bottom: 12px;
}

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

.message-text code {
  background: var(--bg-tertiary);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 14px;
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
}

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

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

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

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

.message-text strong {
  font-weight: 600;
}

.message-text em {
  font-style: italic;
  color: var(--text-secondary);
}

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

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

/* Typing indicator */
.typing-indicator {
  display: flex;
  gap: 4px;
  padding: 4px 0;
}

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

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

.typing-indicator span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes typingBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-8px); opacity: 1; }
}

/* Message actions */
.message-actions {
  display: flex;
  gap: 4px;
  margin-top: 8px;
  opacity: 0;
  transition: opacity var(--transition);
}

.message:hover .message-actions {
  opacity: 1;
}

.action-btn {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  transition: background var(--transition);
  color: var(--text-tertiary);
}

.action-btn:hover {
  background: var(--bg-tertiary);
  color: var(--text-secondary);
}

.action-btn svg {
  width: 16px;
  height: 16px;
}

/* ===== Input Area ===== */
.input-area {
  padding: 0 24px 24px;
  position: relative;
}

.input-container {
  max-width: 768px;
  margin: 0 auto;
}

.input-wrapper {
  display: flex;
  align-items: flex-end;
  background: var(--input-bg);
  border-radius: var(--radius);
  padding: 8px 12px;
  transition: border-color var(--transition), box-shadow var(--transition), background 0.3s ease;
  border: 1px solid transparent;
}

.input-wrapper:focus-within {
  border-color: var(--border);
  box-shadow: 0 0 0 1px var(--border);
}

.input-wrapper textarea {
  flex: 1;
  font-size: 16px;
  line-height: 1.5;
  color: var(--text-primary);
  max-height: 200px;
  min-height: 24px;
  padding: 4px 0;
  overflow-y: auto;
}

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

.send-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  background: var(--text-primary);
  color: var(--bg-primary);
  transition: opacity var(--transition), transform var(--transition);
  flex-shrink: 0;
  margin-left: 4px;
}

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

.send-btn:not(:disabled):hover {
  opacity: 0.85;
}

.stop-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  background: var(--text-primary);
  color: var(--bg-primary);
  flex-shrink: 0;
  margin-left: 4px;
  transition: opacity var(--transition);
}

.stop-btn:hover {
  opacity: 0.85;
}

.hidden {
  display: none !important;
}

.input-disclaimer {
  text-align: center;
  font-size: 12px;
  color: var(--text-tertiary);
  margin-top: 8px;
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 200;
    box-shadow: var(--shadow);
  }

  .sidebar.collapsed {
    margin-left: calc(-1 * var(--sidebar-width));
  }

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

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

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

/* ===== Markdown Helpers ===== */
.md-table {
  border-collapse: collapse;
  margin: 12px 0;
  width: 100%;
}

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

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

.md-table tr:nth-child(even) {
  background: var(--bg-secondary);
}

/* Horizontal rule */
.message-text hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 16px 0;
}
