:root {
  --bg-primary: #0d0d0f;
  --bg-secondary: #16161a;
  --bg-tertiary: #1e1e24;
  --bg-hover: #26262e;
  --bg-active: #2e2e38;
  --surface: #1a1a20;
  --surface-raised: #222228;
  --border: #2a2a32;
  --border-subtle: #1f1f27;
  --accent: #d4a574;
  --accent-hover: #e0b585;
  --accent-dim: rgba(212, 165, 116, 0.15);
  --text-primary: #ececf1;
  --text-secondary: #a1a1aa;
  --text-muted: #71717a;
  --text-inverse: #0d0d0f;
  --error: #ef4444;
  --error-bg: rgba(239, 68, 68, 0.1);
  --success: #22c55e;
  --user-bubble: #2a2a35;
  --assistant-bubble: transparent;
  --sidebar-width: 280px;
  --input-height: 56px;
  --header-height: 56px;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-xl: 24px;
  --transition: 200ms ease;
}

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

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

/* ========== Layout ========== */
.app {
  display: flex;
  height: 100vh;
  height: 100dvh;
}

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

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

.sidebar-header h1 {
  font-size: 16px;
  font-weight: 600;
  color: var(--accent);
  flex: 1;
}

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

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

.sidebar-search {
  padding: 8px 12px;
}

.sidebar-search input {
  width: 100%;
  padding: 8px 12px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 13px;
  outline: none;
  transition: border-color var(--transition);
}

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

.sidebar-search input::placeholder {
  color: var(--text-muted);
}

.conversation-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.conversation-list::-webkit-scrollbar { width: 4px; }
.conversation-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.conv-group-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 8px 8px 4px;
}

.conv-item {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: background var(--transition);
  position: relative;
}

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

.conv-item-title {
  flex: 1;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-item-actions {
  display: none;
  gap: 2px;
}

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

.conv-item-actions button {
  width: 28px;
  height: 28px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
}

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

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

.sidebar-footer button {
  flex: 1;
  padding: 8px;
  border: 1px solid var(--border);
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  font-size: 13px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all var(--transition);
}

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

/* ========== Main Content ========== */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.chat-header {
  height: var(--header-height);
  padding: 0 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-primary);
  flex-shrink: 0;
}

.chat-header .model-selector {
  padding: 6px 12px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 13px;
  cursor: pointer;
  outline: none;
  max-width: 200px;
}

.chat-header .model-selector:focus { border-color: var(--accent); }

.header-spacer { flex: 1; }

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

.messages-container::-webkit-scrollbar { width: 6px; }
.messages-container::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.messages-inner {
  max-width: 800px;
  margin: 0 auto;
  padding: 24px 16px 120px;
}

.welcome-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  text-align: center;
}

.welcome-screen .logo {
  width: 64px;
  height: 64px;
  margin-bottom: 24px;
  border-radius: 16px;
}

.welcome-screen h2 {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 8px;
}

.welcome-screen p {
  color: var(--text-secondary);
  font-size: 15px;
  max-width: 400px;
}

.message {
  margin-bottom: 24px;
  display: flex;
  gap: 12px;
  animation: fadeIn 200ms ease;
}

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

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

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

.message.assistant .message-avatar {
  background: var(--surface-raised);
  color: var(--accent);
}

.message-content {
  flex: 1;
  min-width: 0;
  line-height: 1.6;
  font-size: 15px;
}

.message.user .message-content {
  background: var(--user-bubble);
  padding: 12px 16px;
  border-radius: var(--radius-md);
  border-top-left-radius: 4px;
}

.message.assistant .message-content {
  padding: 4px 0;
}

.message-content p { margin-bottom: 12px; }
.message-content p:last-child { margin-bottom: 0; }
.message-content h1, .message-content h2, .message-content h3,
.message-content h4, .message-content h5, .message-content h6 {
  margin: 20px 0 8px;
  font-weight: 600;
}
.message-content h1 { font-size: 1.4em; }
.message-content h2 { font-size: 1.25em; }
.message-content h3 { font-size: 1.1em; }

.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);
  padding: 8px 16px;
  margin: 12px 0;
  color: var(--text-secondary);
  background: var(--accent-dim);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

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

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

.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);
  padding: 8px 12px;
  text-align: left;
}

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

.message-content .inline-code {
  background: var(--bg-tertiary);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace;
  font-size: 0.9em;
  color: var(--accent);
}

.code-block {
  position: relative;
  margin: 12px 0;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.code-block .code-lang {
  display: block;
  padding: 6px 12px;
  font-size: 11px;
  color: var(--text-muted);
  background: var(--bg-tertiary);
  border-bottom: 1px solid var(--border);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.code-block pre {
  margin: 0;
  padding: 12px 16px;
  overflow-x: auto;
}

.code-block code {
  font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace;
  font-size: 13px;
  line-height: 1.5;
  color: var(--text-primary);
}

.copy-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 32px;
  height: 32px;
  border: none;
  background: var(--bg-tertiary);
  color: var(--text-muted);
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: all var(--transition);
}

.code-block:hover .copy-btn { opacity: 1; }
.copy-btn:hover { background: var(--bg-hover); color: var(--text-primary); }
.copy-btn.copied { color: var(--success); }

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

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

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

@keyframes bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

.error-message {
  background: var(--error-bg);
  border: 1px solid var(--error);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  color: var(--error);
  font-size: 14px;
  margin: 8px 0;
}

/* ========== Input Area ========== */
.input-area {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 16px;
  background: linear-gradient(transparent, var(--bg-primary) 30%);
  pointer-events: none;
}

.input-wrapper {
  max-width: 800px;
  margin: 0 auto;
  pointer-events: all;
}

.input-box {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 8px 12px;
  transition: border-color var(--transition);
}

.input-box:focus-within { border-color: var(--accent); }

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

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

.send-btn {
  width: 36px;
  height: 36px;
  border: none;
  background: var(--accent);
  color: var(--text-inverse);
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition);
  flex-shrink: 0;
}

.send-btn:hover { background: var(--accent-hover); transform: scale(1.05); }
.send-btn:disabled { opacity: 0.3; cursor: not-allowed; transform: none; }

.send-btn.stop {
  background: var(--error);
}

.input-hint {
  text-align: center;
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 8px;
}

/* ========== Modals ========== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  animation: fadeIn 150ms ease;
  padding: 16px;
}

.modal {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 560px;
  max-height: 85vh;
  overflow-y: auto;
  animation: slideUp 200ms ease;
}

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

.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  background: var(--bg-secondary);
  z-index: 1;
}

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

.modal-body {
  padding: 20px;
}

.modal-footer {
  padding: 16px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.form-group {
  margin-bottom: 16px;
}

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

.form-group input,
.form-group textarea,
.form-group select {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 14px;
  font-family: inherit;
  outline: none;
  transition: border-color var(--transition);
}

.form-group input:focus,
.form-group textarea:focus,
.form-group select:focus {
  border-color: var(--accent);
}

.form-group textarea { resize: vertical; min-height: 80px; }

.form-group .hint {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 4px;
}

.btn {
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
}

.btn-primary {
  background: var(--accent);
  color: var(--text-inverse);
}
.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary {
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  border: 1px solid var(--border);
}
.btn-secondary:hover { background: var(--bg-hover); color: var(--text-primary); }

.btn-danger {
  background: var(--error-bg);
  color: var(--error);
  border: 1px solid var(--error);
}
.btn-danger:hover { background: var(--error); color: white; }

/* ========== Memory Items ========== */
.memory-item {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  margin-bottom: 8px;
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

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

.memory-item-title {
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 4px;
}

.memory-item-text {
  font-size: 13px;
  color: var(--text-secondary);
  white-space: pre-wrap;
  word-break: break-word;
}

.memory-item-actions {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

.toggle-switch {
  position: relative;
  width: 40px;
  height: 22px;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-switch .slider {
  position: absolute;
  inset: 0;
  background: var(--bg-active);
  border-radius: 11px;
  cursor: pointer;
  transition: background var(--transition);
}

.toggle-switch .slider::before {
  content: '';
  position: absolute;
  left: 2px;
  top: 2px;
  width: 18px;
  height: 18px;
  background: var(--text-primary);
  border-radius: 50%;
  transition: transform var(--transition);
}

.toggle-switch input:checked + .slider { background: var(--accent); }
.toggle-switch input:checked + .slider::before { transform: translateX(18px); }

/* ========== Model Cards ========== */
.model-card {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.model-card.default { border-color: var(--accent); }

.model-card-info { flex: 1; min-width: 0; }

.model-card-name {
  font-size: 14px;
  font-weight: 500;
}

.model-card-detail {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.model-card-actions {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

.badge {
  font-size: 10px;
  padding: 2px 8px;
  border-radius: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.badge-accent {
  background: var(--accent-dim);
  color: var(--accent);
}

/* ========== Empty States ========== */
.empty-state {
  text-align: center;
  padding: 32px 16px;
  color: var(--text-muted);
}

.empty-state p { font-size: 14px; margin-bottom: 12px; }

/* ========== Setup Screen ========== */
.setup-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 32px 20px;
  text-align: center;
}

.setup-screen .logo { width: 72px; height: 72px; margin-bottom: 24px; border-radius: 18px; }
.setup-screen h2 { font-size: 24px; margin-bottom: 8px; }
.setup-screen p { color: var(--text-secondary); margin-bottom: 32px; max-width: 400px; }

.setup-form {
  width: 100%;
  max-width: 440px;
  text-align: left;
}

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

  .sidebar.open {
    transform: translateX(0);
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.4);
  }

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

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

  .messages-inner { padding: 16px 12px 120px; }

  .message { gap: 8px; }
  .message-avatar { width: 28px; height: 28px; font-size: 12px; }

  .conv-item-actions { display: flex; }

  .chat-header { padding: 0 12px; }
}

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

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

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

@keyframes blink {
  0%, 50% { opacity: 1; }
  51%, 100% { opacity: 0; }
}

/* ========== Toast notifications ========== */
.toast-container {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 300;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 12px 16px;
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 14px;
  animation: slideIn 200ms ease;
  max-width: 320px;
}

.toast.error { border-color: var(--error); }
.toast.success { border-color: var(--success); }

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* ========== Auth Screen ========== */
.auth-screen {
  position: fixed;
  inset: 0;
  background: var(--bg-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
  padding: 16px;
}

.auth-card {
  width: 100%;
  max-width: 400px;
  text-align: center;
}

.auth-card .logo {
  width: 72px;
  height: 72px;
  border-radius: 18px;
  margin-bottom: 20px;
}

.auth-card h2 {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 8px;
}

.auth-card > p {
  color: var(--text-secondary);
  font-size: 14px;
  margin-bottom: 28px;
}

.auth-card form {
  text-align: left;
}

.auth-card .error-message {
  margin-bottom: 12px;
}

.auth-toggle {
  margin-top: 20px;
  font-size: 14px;
  color: var(--text-secondary);
}

.auth-toggle a {
  color: var(--accent);
  text-decoration: none;
  font-weight: 500;
}

.auth-toggle a:hover {
  text-decoration: underline;
}

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