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

:root {
  --bg: #0a0a0f;
  --surface: #141420;
  --surface-2: #1c1c2e;
  --border: #2a2a3e;
  --text: #e4e4f0;
  --text-dim: #8888a0;
  --primary: #6366f1;
  --primary-hover: #4f46e5;
  --danger: #ef4444;
  --success: #22c55e;
  --warning: #f59e0b;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

.auth-container {
  max-width: 420px;
  margin: 10vh auto;
  padding: 2rem;
  text-align: center;
}

.auth-container h1 { font-size: 2rem; margin-bottom: 0.5rem; }
.subtitle { color: var(--text-dim); margin-bottom: 2rem; }

.auth-forms {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2rem;
}

.form-section { margin: 1rem 0; }
.form-section h2 { font-size: 1.1rem; margin-bottom: 1rem; }

.divider {
  display: flex; align-items: center; margin: 1.5rem 0;
  color: var(--text-dim); font-size: 0.85rem;
}
.divider::before, .divider::after { content: ''; flex: 1; border-top: 1px solid var(--border); }
.divider span { padding: 0 1rem; }

input[type="text"], input[type="password"], textarea, select {
  width: 100%;
  padding: 0.75rem 1rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.95rem;
  margin-bottom: 0.75rem;
  outline: none;
  transition: border-color 0.2s;
}

input:focus, textarea:focus, select:focus { border-color: var(--primary); }

.btn {
  padding: 0.7rem 1.4rem;
  border: none;
  border-radius: 8px;
  font-size: 0.95rem;
  cursor: pointer;
  transition: all 0.2s;
  font-weight: 500;
}

.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { border-color: var(--primary); }
.btn-danger { background: var(--danger); color: white; }
.btn-sm { padding: 0.4rem 0.8rem; font-size: 0.8rem; }

.error { color: var(--danger); margin-top: 1rem; padding: 0.75rem; background: rgba(239,68,68,0.1); border-radius: 8px; }
.info { color: var(--success); margin-top: 1rem; padding: 0.75rem; background: rgba(34,197,94,0.1); border-radius: 8px; }
.hidden { display: none; }

/* App layout */
.app-layout { display: flex; height: 100vh; }

.sidebar {
  width: 280px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

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

.sidebar-header h2 { font-size: 1.1rem; }

.conversations-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
}

.conv-item {
  padding: 0.7rem 1rem;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2px;
}

.conv-item:hover { background: var(--surface-2); }
.conv-item.active { background: var(--surface-2); border: 1px solid var(--border); }
.conv-title { font-size: 0.85rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.conv-delete { background: none; border: none; color: var(--text-dim); cursor: pointer; font-size: 1.2rem; padding: 0 0.3rem; }
.conv-delete:hover { color: var(--danger); }

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

/* Chat area */
.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
}

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

.model-select {
  width: auto;
  min-width: 200px;
  margin-bottom: 0;
}

.encryption-badge {
  font-size: 0.8rem;
  color: var(--success);
  background: rgba(34,197,94,0.1);
  padding: 0.3rem 0.7rem;
  border-radius: 20px;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-dim);
}

.empty-state h3 { margin-bottom: 0.5rem; color: var(--text); }

.message {
  margin-bottom: 1.5rem;
  max-width: 80%;
}

.message.user { margin-left: auto; }
.message.assistant { margin-right: auto; }

.message-role {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  color: var(--text-dim);
  margin-bottom: 0.3rem;
}

.message-content {
  padding: 0.8rem 1.2rem;
  border-radius: 12px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.message.user .message-content { background: var(--primary); color: white; }
.message.assistant .message-content { background: var(--surface-2); border: 1px solid var(--border); }
.message.assistant .message-md { white-space: normal; }
.message.assistant .message-md h1 { font-size: 1.4rem; margin: 0.4em 0 0.5em; padding-bottom: 0.3em; border-bottom: 1px solid var(--border); }
.message.assistant .message-md h2 { font-size: 1.15rem; margin: 1.2em 0 0.5em; color: var(--text); }
.message.assistant .message-md h3 { font-size: 1rem; margin: 1em 0 0.4em; color: var(--text-dim); }
.message.assistant .message-md p { margin: 0.5em 0; }
.message.assistant .message-md ul, .message.assistant .message-md ol { padding-left: 1.5rem; margin: 0.5em 0; }
.message.assistant .message-md li { margin: 0.2em 0; }
.message.assistant .message-md table { border-collapse: collapse; width: 100%; margin: 0.75em 0; font-size: 0.85rem; }
.message.assistant .message-md th, .message.assistant .message-md td { border: 1px solid var(--border); padding: 0.4rem 0.7rem; text-align: left; }
.message.assistant .message-md th { background: rgba(99,102,241,0.1); font-weight: 600; }
.message.assistant .message-md hr { border: 0; border-top: 1px solid var(--border); margin: 1.5em 0; }
.message.assistant .message-md code { background: rgba(255,255,255,0.05); padding: 1px 4px; border-radius: 3px; font-size: 0.85em; }
.message.assistant .message-md pre { background: rgba(0,0,0,0.3); padding: 0.75rem; border-radius: 6px; overflow-x: auto; }
.message.assistant .message-md strong { color: var(--text); }
.message.pending .message-content { padding: 1rem 1.4rem; }

.message-actions {
  display: flex;
  gap: 0.4rem;
  margin-top: 0.5rem;
  flex-wrap: wrap;
}

.msg-action {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.3rem 0.7rem;
  border-radius: 6px;
  font-size: 0.78rem;
  cursor: pointer;
  transition: all 0.15s;
}

.msg-action:hover {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

.tool-activity {
  font-family: 'SF Mono', Menlo, Consolas, monospace;
  font-size: 0.75rem;
  color: var(--primary);
  background: rgba(99,102,241,0.08);
  border-left: 2px solid var(--primary);
  padding: 0.5rem 0.75rem;
  border-radius: 4px;
  margin-bottom: 0.5rem;
  white-space: pre-wrap;
  word-break: break-word;
}

.typing-dots {
  display: inline-flex;
  gap: 0.25rem;
  align-items: center;
}

.typing-dots span {
  width: 0.5rem;
  height: 0.5rem;
  background: var(--text-dim);
  border-radius: 50%;
  animation: typing 1.4s infinite ease-in-out;
}

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

@keyframes typing {
  0%, 60%, 100% { opacity: 0.3; transform: scale(0.8); }
  30% { opacity: 1; transform: scale(1); }
}

.chat-input-area {
  padding: 1rem;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.input-row {
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
}

.chat-input-area textarea {
  flex: 1;
  resize: none;
  margin-bottom: 0;
  min-height: 44px;
  max-height: 200px;
}

.btn-icon {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.6rem 0.8rem;
  font-size: 1rem;
  cursor: pointer;
  border-radius: 8px;
}

.btn-icon:hover { border-color: var(--primary); }

.attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.attachments:empty { display: none; }

.attachment-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.7rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 0.8rem;
  max-width: 320px;
}

.attachment-chip.ready { border-color: rgba(34,197,94,0.4); }
.attachment-chip.error { border-color: var(--danger); color: var(--danger); }
.attachment-chip.pending { border-color: rgba(99,102,241,0.5); }

.att-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 180px;
}

.att-size { color: var(--text-dim); font-size: 0.7rem; }
.att-status { font-size: 0.85rem; }

.att-remove {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 1.1rem;
  padding: 0 0.2rem;
  line-height: 1;
}

.att-remove:hover { color: var(--danger); }

.drop-overlay {
  position: absolute;
  inset: 0;
  background: rgba(99,102,241,0.12);
  backdrop-filter: blur(4px);
  border: 3px dashed var(--primary);
  border-radius: 12px;
  margin: 1rem;
  display: none;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 10;
}

.drop-overlay.active { display: flex; }

.drop-overlay-content {
  text-align: center;
  color: var(--text);
}

.drop-icon {
  font-size: 4rem;
  margin-bottom: 1rem;
}

.drop-text {
  font-size: 1.2rem;
  font-weight: 500;
}

.chat-area { position: relative; }

/* Knowledge bar (persistent attached files for the conversation) */
.knowledge-bar {
  display: none;
  flex-direction: column;
  gap: 0.4rem;
  padding: 0.6rem 0.75rem;
  background: rgba(99,102,241,0.06);
  border: 1px solid rgba(99,102,241,0.25);
  border-radius: 10px;
}

.knowledge-bar.has-items { display: flex; }

.knowledge-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.kn-label {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--primary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.kn-budget { font-weight: 500; }
.kn-budget-ok { color: var(--success); }
.kn-budget-warn { color: var(--warning); }
.kn-budget-danger { color: var(--danger); }

.knowledge-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.knowledge-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.35rem 0.7rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 0.8rem;
  cursor: pointer;
  user-select: none;
  transition: all 0.15s;
}

.knowledge-chip.enabled { border-color: rgba(99,102,241,0.5); }
.knowledge-chip.disabled { opacity: 0.5; }
.knowledge-chip.no-text { border-color: var(--warning); background: rgba(245,158,11,0.05); }
.knowledge-chip:hover { border-color: var(--primary); }

.kn-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.1rem;
  height: 1.1rem;
  border-radius: 50%;
  background: rgba(99,102,241,0.15);
  font-size: 0.7rem;
  font-weight: 700;
}

.kn-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
}

.kn-size { color: var(--text-dim); font-size: 0.7rem; }

.kn-detach {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 1.1rem;
  padding: 0 0.2rem;
  line-height: 1;
}

.kn-detach:hover { color: var(--danger); }

.kn-reextract {
  background: rgba(99,102,241,0.15);
  border: 1px solid rgba(99,102,241,0.4);
  color: var(--primary);
  cursor: pointer;
  font-size: 0.7rem;
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  font-weight: 500;
}

.kn-reextract:hover {
  background: var(--primary);
  color: white;
}

#toast-host {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  z-index: 1000;
  pointer-events: none;
}

.toast {
  padding: 0.75rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.85rem;
  max-width: 400px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
  animation: toastIn 0.2s ease-out;
  pointer-events: auto;
}

.toast-success { border-color: rgba(34,197,94,0.5); background: rgba(34,197,94,0.08); }
.toast-error { border-color: var(--danger); background: rgba(239,68,68,0.08); }
.toast-info { border-color: rgba(99,102,241,0.5); background: rgba(99,102,241,0.08); }
.toast.out { animation: toastOut 0.3s ease-in forwards; }

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

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: 90%;
  max-width: 600px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}

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

.modal-header h3 { font-size: 1.1rem; }

.modal-close {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 1.6rem;
  line-height: 1;
}

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

.modal-body {
  padding: 1rem 1.5rem;
  overflow-y: auto;
}

.library-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.library-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 0.9rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.library-item .lib-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.library-item .lib-size { color: var(--text-dim); font-size: 0.8rem; }
.library-item .lib-already { color: var(--success); font-size: 0.8rem; font-weight: 500; }

/* Admin panel */
.admin-panel, .files-panel { padding: 2rem; overflow-y: auto; max-height: 100vh; }
.admin-section { margin: 1.5rem 0; }
.admin-section h3 { margin-bottom: 1rem; }

.pending-user {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 1rem;
  background: var(--surface-2);
  border-radius: 8px;
  margin-bottom: 0.5rem;
}

.pending-user span { flex: 1; }

.admin-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.admin-table th, .admin-table td {
  padding: 0.75rem 1rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

.admin-table th { color: var(--text-dim); font-weight: 500; }

.badge {
  padding: 0.2rem 0.6rem;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 600;
}

.badge-approved { background: rgba(34,197,94,0.15); color: var(--success); }
.badge-pending { background: rgba(245,158,11,0.15); color: var(--warning); }

/* Files */
.upload-area {
  display: flex;
  gap: 1rem;
  align-items: center;
  margin: 1rem 0;
  padding: 1rem;
  background: var(--surface-2);
  border-radius: 8px;
}

.file-item {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 0.5rem;
}

.file-item span:first-child { flex: 1; }
.file-size { color: var(--text-dim); font-size: 0.8rem; }
