:root {
  --bg: #0f1117;
  --bg-sidebar: #171923;
  --bg-elevated: #1e2130;
  --bg-input: #232635;
  --border: #2a2e40;
  --text: #e7e9ee;
  --text-muted: #9aa0b0;
  --accent: #6d5efc;
  --accent-hover: #5a4be8;
  --user-bubble: #6d5efc;
  --assistant-bubble: #1e2130;
  --danger: #ef4444;
  --success: #22c55e;
  --radius: 12px;
  --sidebar-width: 280px;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family:
    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

.app {
  display: flex;
  height: 100dvh;
  width: 100%;
}

/* ---------- Sidebar ---------- */
.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: transform 0.25s ease;
  z-index: 40;
}

.sidebar-header {
  padding: 12px;
}

.new-chat-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.new-chat-btn:hover {
  background: var(--accent-hover);
}

.chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 10px;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.9rem;
  transition:
    background 0.15s,
    color 0.15s;
  position: relative;
}

.chat-item:hover {
  background: var(--bg-elevated);
  color: var(--text);
}

.chat-item.active {
  background: var(--bg-elevated);
  color: var(--text);
}

.chat-item-title {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-item-delete {
  opacity: 0;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px;
  border-radius: 6px;
  display: flex;
  transition:
    opacity 0.15s,
    color 0.15s;
}

.chat-item:hover .chat-item-delete {
  opacity: 1;
}

.chat-item-delete:hover {
  color: var(--danger);
}

.sidebar-footer {
  padding: 12px;
  border-top: 1px solid var(--border);
}

.sidebar-footer-btn {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: none;
  border: none;
  color: var(--text-muted);
  border-radius: 10px;
  font-size: 0.9rem;
  cursor: pointer;
  transition:
    background 0.15s,
    color 0.15s;
}

.sidebar-footer-btn:hover {
  background: var(--bg-elevated);
  color: var(--text);
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 35;
}

.sidebar-overlay.show {
  display: block;
}

/* ---------- Main ---------- */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.topbar-title {
  flex: 1;
  font-size: 1rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.model-badge {
  font-size: 0.7rem;
  color: var(--danger);
  transition: color 0.3s;
}

.model-badge.online {
  color: var(--success);
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
  display: flex;
  transition:
    background 0.15s,
    color 0.15s;
}

.icon-btn:hover {
  background: var(--bg-elevated);
  color: var(--text);
}

.menu-btn {
  display: none;
}

/* ---------- Messages ---------- */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 0;
  scroll-behavior: smooth;
}

.message {
  max-width: 780px;
  margin: 0 auto 20px;
  padding: 0 20px;
  display: flex;
  gap: 14px;
  animation: fadeIn 0.25s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  font-weight: 600;
}

.message.user .avatar {
  background: var(--user-bubble);
  color: #fff;
}

.message.assistant .avatar {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
}

.avatar svg {
  width: 20px;
  height: 20px;
}

.message-body {
  flex: 1;
  min-width: 0;
}

.message-role {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.message-content {
  line-height: 1.6;
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.message.error .message-content {
  color: var(--danger);
}

/* ---------- Markdown rendering (assistant messages) ---------- */
.message.assistant .message-content {
  white-space: normal;
}

.message-content > *:first-child {
  margin-top: 0;
}

.message-content > *:last-child {
  margin-bottom: 0;
}

.message-content p {
  margin: 0 0 10px;
}

.message-content h3,
.message-content h4,
.message-content h5,
.message-content h6 {
  margin: 14px 0 8px;
  line-height: 1.3;
}

.message-content h3 {
  font-size: 1.15rem;
}
.message-content h4 {
  font-size: 1.05rem;
}
.message-content h5 {
  font-size: 0.98rem;
}
.message-content h6 {
  font-size: 0.92rem;
  color: var(--text-muted);
}

.message-content ul,
.message-content ol {
  margin: 0 0 10px;
  padding-left: 22px;
}

.message-content li {
  margin: 2px 0;
}

.message-content a {
  color: var(--accent);
  text-decoration: underline;
}

.message-content blockquote {
  margin: 0 0 10px;
  padding: 4px 12px;
  border-left: 3px solid var(--border);
  color: var(--text-muted);
}

.message-content hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 12px 0;
}

.message-content strong {
  font-weight: 700;
}

.message-content del {
  opacity: 0.7;
}

.inline-code {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.88em;
}

.code-block {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  overflow-x: auto;
  margin: 0 0 10px;
}

.code-block code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.85rem;
  line-height: 1.5;
  white-space: pre;
  color: var(--text);
}

.error-box {
  border: 1px solid rgba(239, 68, 68, 0.4);
  background: rgba(239, 68, 68, 0.08);
  border-radius: 10px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.error-box.context {
  border-color: rgba(245, 158, 11, 0.45);
  background: rgba(245, 158, 11, 0.08);
}

.error-box strong {
  color: var(--text);
  font-size: 0.95rem;
}

.error-box.context strong {
  color: #f59e0b;
}

.error-box p {
  color: var(--text-muted);
  font-size: 0.88rem;
  line-height: 1.5;
}

.error-detail {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.75rem;
  color: var(--text-muted);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  word-break: break-word;
  white-space: pre-wrap;
}

/* Typing indicator */
.typing-indicator {
  display: inline-flex;
  gap: 5px;
  padding: 6px 0;
}

.typing-dot {
  width: 8px;
  height: 8px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: bounce 1.4s infinite ease-in-out both;
}

.typing-dot:nth-child(1) {
  animation-delay: -0.32s;
}
.typing-dot:nth-child(2) {
  animation-delay: -0.16s;
}

@keyframes bounce {
  0%,
  80%,
  100% {
    transform: scale(0);
  }
  40% {
    transform: scale(1);
  }
}

/* Empty state */
.empty-state {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--text-muted);
  padding: 20px;
}

.empty-icon {
  width: 72px;
  height: 72px;
  margin-bottom: 12px;
}

.empty-icon svg {
  width: 100%;
  height: 100%;
}

.empty-state h2 {
  color: var(--text);
  margin-bottom: 6px;
  font-size: 1.4rem;
}

/* ---------- Composer ---------- */
.composer {
  border-top: 1px solid var(--border);
  padding: 14px 16px 10px;
  background: var(--bg);
}

.composer-inner {
  max-width: 780px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 8px 8px 8px 8px;
  transition: border-color 0.2s;
}

.composer-inner:focus-within {
  border-color: var(--accent);
}

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.attach-btn {
  width: 38px;
  height: 38px;
  flex-shrink: 0;
  background: none;
  color: var(--text-muted);
  border: none;
  border-radius: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition:
    background 0.2s,
    color 0.2s;
}

.attach-btn:hover {
  background: var(--bg-elevated);
  color: var(--text);
}

/* Attachment preview (before sending) */
.attachment-preview {
  display: none;
  flex-wrap: wrap;
  gap: 8px;
  padding: 4px 6px 0;
}

.attachment-preview.has-items {
  display: flex;
}

.attachment-thumb {
  position: relative;
  width: 64px;
  height: 64px;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.attachment-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.attachment-remove {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 18px;
  height: 18px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.65);
  color: #fff;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.attachment-remove:hover {
  background: var(--danger);
}

/* Images inside sent messages */
.message-images {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}

.message-image {
  max-width: 240px;
  max-height: 240px;
  border-radius: 10px;
  border: 1px solid var(--border);
  object-fit: cover;
}

#user-input {
  flex: 1;
  background: none;
  border: none;
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
  resize: none;
  outline: none;
  max-height: 200px;
  line-height: 1.5;
  padding: 6px 0;
}

#user-input::placeholder {
  color: var(--text-muted);
}

.send-btn {
  width: 38px;
  height: 38px;
  flex-shrink: 0;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition:
    background 0.2s,
    opacity 0.2s;
}

.send-btn:hover:not(:disabled) {
  background: var(--accent-hover);
}

.send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.composer-hint {
  max-width: 780px;
  margin: 6px auto 0;
  text-align: center;
  font-size: 0.72rem;
  color: var(--text-muted);
}

/* ---------- Modal ---------- */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 50;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal-overlay.show {
  display: flex;
}

.modal {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 16px;
  width: 100%;
  max-width: 480px;
  max-height: 90vh;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-header h3 {
  font-size: 1.1rem;
}

.modal-body {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
}

.field label {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-muted);
}

.field input,
.field textarea {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
  outline: none;
  transition: border-color 0.2s;
}

.field input:focus,
.field textarea:focus {
  border-color: var(--accent);
}

.field textarea {
  resize: vertical;
}

.field-row {
  display: flex;
  gap: 12px;
}

.modal-footer {
  padding: 16px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 10px 20px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

/* ---------- Scrollbars ---------- */
::-webkit-scrollbar {
  width: 8px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
  background: #3a3f55;
}

/* ---------- Mobile ---------- */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100dvh;
    transform: translateX(-100%);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .menu-btn {
    display: flex;
  }

  .message {
    padding: 0 14px;
    gap: 10px;
  }

  .field-row {
    flex-direction: column;
  }
}
