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

:root {
  --bg-primary: #212121;
  --bg-secondary: #171717;
  --bg-tertiary: #2f2f2f;
  --bg-hover: #383838;
  --text-primary: #ececec;
  --text-secondary: #b4b4b4;
  --text-muted: #8e8e8e;
  --accent: #10a37f;
  --accent-hover: #0d8c6d;
  --border: #424242;
  --user-bg: #2f2f2f;
  --sidebar-width: 260px;
  --topbar-height: 52px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

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

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

textarea {
  font-family: inherit;
}

.app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

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

.sidebar-brand {
  display: flex;
  justify-content: center;
  padding: 16px 12px 8px;
  text-decoration: none;
}

.sidebar-logo {
  width: 120px;
  height: auto;
  object-fit: contain;
}

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px 12px;
}

.btn-new-chat {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  transition: background 0.15s;
}

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

.btn-edit-chats {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-secondary);
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s;
}

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

.sidebar-edit-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 12px 10px;
}

.sidebar-edit-toolbar[hidden] {
  display: none;
}

.btn-select-all {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  color: var(--text-secondary);
  white-space: nowrap;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s;
}

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

.btn-select-all:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-cancel-edit {
  flex: 1;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  color: var(--text-secondary);
  transition: background 0.15s, color 0.15s;
}

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

.btn-bulk-delete {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-secondary);
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.btn-bulk-delete:hover {
  background: rgba(255, 107, 107, 0.12);
  border-color: #ff6b6b;
  color: #ff6b6b;
}

.btn-cancel-edit.danger {
  background: rgba(255, 107, 107, 0.15);
  border-color: #ff6b6b;
  color: #ff6b6b;
}

.btn-cancel-edit.danger:hover {
  background: rgba(255, 107, 107, 0.25);
  color: #ff4444;
}

.btn-bulk-delete.is-cancel {
  min-width: 64px;
  font-size: 14px;
}

.btn-bulk-delete.is-cancel:hover {
  background: var(--bg-hover);
  border-color: var(--border);
  color: var(--text-primary);
}

.btn-bulk-delete-cancel[hidden],
.btn-bulk-delete-trash[hidden] {
  display: none;
}

.delete-confirm-hint {
  padding: 0 12px 8px;
  font-size: 12px;
  color: #ff8a8a;
  line-height: 1.4;
}

.delete-confirm-hint[hidden] {
  display: none;
}

.btn-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  color: var(--text-secondary);
  transition: background 0.15s, color 0.15s;
}

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

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

.conversation-item {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 14px;
  color: var(--text-secondary);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  margin-bottom: 2px;
}

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

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

.conversation-list.edit-mode .conversation-item.active {
  background: transparent;
  color: var(--text-secondary);
}

.conversation-list.edit-mode .conversation-item.active.selected {
  background: rgba(100, 180, 255, 0.28);
  color: var(--text-primary);
}

.conversation-item.selected {
  background: rgba(100, 180, 255, 0.28);
  color: var(--text-primary);
}

.conversation-item-check {
  width: 18px;
  height: 18px;
  border: 2px solid var(--text-muted);
  border-radius: 50%;
  flex-shrink: 0;
  transition: border-color 0.15s, background 0.15s;
}

.conversation-item.selected .conversation-item-check {
  border-color: #5eb3ff;
  background: #5eb3ff;
  box-shadow: inset 0 0 0 3px var(--bg-secondary);
}

.conversation-list.edit-mode .conversation-item-delete {
  display: none;
}

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

.conversation-item-delete {
  opacity: 0;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  color: var(--text-muted);
  flex-shrink: 0;
  transition: opacity 0.15s, background 0.15s, color 0.15s;
}

.conversation-item:hover .conversation-item-delete {
  opacity: 1;
}

.conversation-item-delete:hover {
  background: #5c2020;
  color: #ff6b6b;
}

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

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

#btnOpenSidebar {
  display: none;
}

.topbar-logo {
  width: 28px;
  height: 28px;
  object-fit: contain;
  border-radius: 6px;
  flex-shrink: 0;
}

.topbar-title {
  flex: 1;
  font-size: 16px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar-user {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-left: auto;
  flex-shrink: 0;
}

.topbar-user-name {
  font-size: 13px;
  color: var(--text-secondary);
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.btn-logout {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  color: var(--text-secondary);
  transition: background 0.15s, color 0.15s;
}

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

/* Chat area */
.chat-area {
  flex: 1;
  overflow-y: auto;
  scroll-behavior: smooth;
}

.welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 32px;
  text-align: center;
  gap: 12px;
}

.welcome-logo {
  width: 160px;
  height: auto;
  object-fit: contain;
  margin-bottom: 8px;
}

.welcome h2 {
  font-size: 28px;
  font-weight: 600;
}

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

.messages {
  max-width: 768px;
  margin: 0 auto;
  padding: 24px 16px 32px;
}

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

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

.message-avatar {
  width: 32px;
  height: 32px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 600;
  flex-shrink: 0;
}

.message.user .message-avatar {
  background: #5436da;
}

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

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

.message-text.streaming::after {
  content: '▋';
  animation: blink 0.8s step-end infinite;
  color: var(--accent);
  margin-left: 2px;
}

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

/* Input */
.input-area {
  padding: 12px 16px 20px;
  flex-shrink: 0;
}

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

.input-wrapper {
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 12px 12px 10px 16px;
  transition: border-color 0.15s;
}

.input-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.input-wrapper:focus-within {
  border-color: var(--text-muted);
}

/* Full-page drop zone */
.drop-overlay {
  position: fixed;
  inset: 0;
  background: rgba(16, 163, 127, 0.12);
  border: 2px dashed var(--accent);
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  animation: dropOverlayIn 0.15s ease;
}

.drop-overlay[hidden] {
  display: none;
}

.drop-overlay-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 32px 48px;
  background: var(--bg-secondary);
  border: 1px solid var(--accent);
  border-radius: 16px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
  color: var(--accent);
  text-align: center;
}

.drop-overlay-content p {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
}

.drop-overlay-content span {
  font-size: 13px;
  color: var(--text-secondary);
}

@keyframes dropOverlayIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.toolbar-left {
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 0;
  flex: 1;
}

.btn-attach {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  color: var(--text-secondary);
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s;
}

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

.btn-attach:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.attachment-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding-bottom: 4px;
}

.attachment-preview[hidden] {
  display: none;
}

.attachment-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px 6px 6px;
  background: var(--bg-hover);
  border: 1px solid var(--border);
  border-radius: 10px;
  max-width: 200px;
}

.attachment-chip-thumb {
  width: 40px;
  height: 40px;
  border-radius: 6px;
  object-fit: cover;
  flex-shrink: 0;
}

.attachment-chip-icon {
  width: 40px;
  height: 40px;
  border-radius: 6px;
  background: var(--bg-tertiary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
}

.attachment-chip-info {
  flex: 1;
  min-width: 0;
}

.attachment-chip-name {
  font-size: 12px;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.attachment-chip-size {
  font-size: 11px;
  color: var(--text-muted);
}

.attachment-chip-remove {
  width: 22px;
  height: 22px;
  border-radius: 6px;
  color: var(--text-muted);
  flex-shrink: 0;
  font-size: 12px;
  transition: background 0.15s, color 0.15s;
}

.attachment-chip-remove:hover {
  background: #5c2020;
  color: #ff6b6b;
}

.message-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
}

.message-attachment {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  background: var(--bg-hover);
  border: 1px solid var(--border);
  border-radius: 10px;
  max-width: 280px;
  text-decoration: none;
  color: inherit;
  transition: border-color 0.15s;
}

.message-attachment:hover {
  border-color: var(--text-muted);
}

.message-attachment img {
  width: 64px;
  height: 64px;
  border-radius: 8px;
  object-fit: cover;
  flex-shrink: 0;
}

.message-attachment-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  background: var(--bg-tertiary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
}

.message-attachment-info {
  min-width: 0;
}

.message-attachment-name {
  font-size: 13px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.message-attachment-meta {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

.message-text {
  white-space: pre-wrap;
  word-break: break-word;
}

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

#messageInput {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-primary);
  font-size: 15px;
  line-height: 1.5;
  resize: none;
  max-height: 200px;
  overflow-y: auto;
}

#messageInput::placeholder {
  color: var(--text-muted);
}

.btn-send {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: var(--accent);
  color: white;
  flex-shrink: 0;
  transition: background 0.15s, opacity 0.15s;
}

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

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

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

/* Model selector */
.model-selector {
  position: relative;
}

.model-selector-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 13px;
  color: var(--text-secondary);
  transition: background 0.15s, color 0.15s;
}

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

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

.model-chevron {
  transition: transform 0.2s;
}

.model-selector-btn.open .model-chevron {
  transform: rotate(180deg);
}

.model-dropdown {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 0;
  min-width: 260px;
  max-height: 320px;
  overflow-y: auto;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 6px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  z-index: 20;
}

.model-dropdown[hidden] {
  display: none;
}

.model-option {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  text-align: left;
  transition: background 0.15s;
}

.model-option:hover {
  background: var(--bg-hover);
}

.model-option.active {
  background: var(--bg-tertiary);
}

.model-option-name {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-primary);
}

.model-option-desc {
  font-size: 12px;
  color: var(--text-muted);
}

.model-option-check {
  display: none;
  color: var(--accent);
  font-size: 14px;
  flex-shrink: 0;
}

.model-option.active .model-option-check {
  display: block;
}

.model-option-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
  min-width: 0;
}

/* Overlay (mobile) */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 9;
}

.overlay[hidden] {
  display: none;
}

/* Mobile */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    z-index: 10;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
  }

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

  #btnOpenSidebar {
    display: flex;
  }

  .sidebar-toggle {
    display: flex;
  }
}

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

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

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

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

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

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: flex-start;
  gap: 12px;
  max-width: min(480px, calc(100vw - 32px));
  padding: 14px 16px;
  background: #3d1f1f;
  border: 1px solid #6b3030;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  z-index: 100;
  animation: toastIn 0.2s ease;
}

.toast[hidden] {
  display: none;
}

.toast-message {
  flex: 1;
  font-size: 14px;
  line-height: 1.5;
  color: #ffb4b4;
  white-space: pre-wrap;
  word-break: break-word;
}

.toast-close {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  color: #ffb4b4;
  flex-shrink: 0;
  font-size: 14px;
  transition: background 0.15s;
}

.toast-close:hover {
  background: rgba(255, 255, 255, 0.1);
}

@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(8px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* Confirm dialog */
.confirm-dialog {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 20px;
  animation: confirmIn 0.15s ease;
}

.confirm-dialog[hidden] {
  display: none;
}

.confirm-dialog-box {
  width: 100%;
  max-width: 360px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.5);
}

.confirm-dialog-message {
  font-size: 15px;
  line-height: 1.5;
  color: var(--text-primary);
  margin-bottom: 8px;
}

.confirm-dialog-hint {
  font-size: 13px;
  color: #ff8a8a;
  margin-bottom: 18px;
}

.confirm-dialog-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

.btn-confirm-cancel,
.btn-confirm-ok {
  padding: 9px 16px;
  border-radius: 8px;
  font-size: 14px;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.btn-confirm-cancel {
  border: 1px solid var(--border);
  color: var(--text-secondary);
}

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

.btn-confirm-ok {
  background: rgba(255, 107, 107, 0.18);
  border: 1px solid #ff6b6b;
  color: #ff6b6b;
}

.btn-confirm-ok:hover {
  background: rgba(255, 107, 107, 0.3);
  color: #ff4444;
}

@keyframes confirmIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* Login page */
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--bg-primary);
}

.login-card {
  width: 100%;
  max-width: 400px;
  padding: 36px 32px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--bg-secondary);
  text-align: center;
}

.login-logo {
  width: 64px;
  height: 64px;
  object-fit: contain;
  border-radius: 12px;
  margin-bottom: 16px;
}

.login-title {
  font-size: 22px;
  font-weight: 600;
  margin-bottom: 8px;
}

.login-subtitle {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 24px;
}

.login-form {
  text-align: left;
}

.login-label {
  display: block;
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.login-input {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-tertiary);
  color: var(--text-primary);
  font-size: 15px;
  outline: none;
  transition: border-color 0.15s;
}

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

.login-error {
  margin-top: 10px;
  font-size: 13px;
  color: #ff6b6b;
}

.login-submit {
  width: 100%;
  margin-top: 16px;
  padding: 12px 16px;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  transition: background 0.15s;
}

.login-submit:hover:not(:disabled) {
  background: var(--accent-hover);
}

.login-submit:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.login-hint {
  margin-top: 20px;
  font-size: 12px;
  color: var(--text-muted);
}
