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

:root {
  --brand: #5B4CF5;
  --brand-light: #EEF0FF;
  --brand-dark: #4339C8;
  --bg: #F4F5F7;
  --surface: #FFFFFF;
  --border: #E2E4E9;
  --text: #1A1D23;
  --text-muted: #6B7280;
  --danger: #DC2626;
  --danger-light: #FEF2F2;
  --success: #16A34A;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 6px rgba(0,0,0,0.06), 0 2px 4px rgba(0,0,0,0.04);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

/* Auth gate */
.auth-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.auth-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: 2.5rem;
  width: 100%;
  max-width: 380px;
  text-align: center;
}

.auth-card .logo {
  width: 48px;
  height: 48px;
  background: var(--brand);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 1.25rem;
  font-size: 1.4rem;
}

.auth-card h1 {
  font-size: 1.25rem;
  font-weight: 700;
  margin-bottom: 0.25rem;
}

.auth-card p {
  color: var(--text-muted);
  font-size: 0.875rem;
  margin-bottom: 1.5rem;
}

.auth-card input {
  width: 100%;
  padding: 0.75rem 1rem;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.9375rem;
  outline: none;
  transition: border-color 0.15s;
  margin-bottom: 0.75rem;
}

.auth-card input:focus { border-color: var(--brand); }

.auth-card .error {
  color: var(--danger);
  font-size: 0.8125rem;
  margin-bottom: 0.75rem;
  min-height: 1.25rem;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.625rem 1.25rem;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  font-weight: 600;
  border: none;
  cursor: pointer;
  transition: all 0.15s;
  text-decoration: none;
}

.btn-primary {
  background: var(--brand);
  color: #fff;
  width: 100%;
  justify-content: center;
  padding: 0.75rem;
  font-size: 0.9375rem;
}
.btn-primary:hover { background: var(--brand-dark); }

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border: 1.5px solid var(--border);
}
.btn-secondary:hover { background: var(--bg); }

.btn-danger {
  background: var(--danger-light);
  color: var(--danger);
}
.btn-danger:hover { background: #FEE2E2; }

.btn-sm {
  padding: 0.375rem 0.75rem;
  font-size: 0.8125rem;
}

/* Header */
.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 1.5rem;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.app-header .brand {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  font-weight: 700;
  font-size: 1rem;
}

.app-header .brand-icon {
  width: 30px;
  height: 30px;
  background: var(--brand);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.875rem;
  color: #fff;
}

.app-header .badge {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-weight: 400;
}

/* Chat layout */
.chat-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
  padding: 0 1rem;
  min-height: 0;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem 0;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.message {
  display: flex;
  gap: 0.75rem;
  animation: fadeIn 0.2s ease;
}

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

.message.user { flex-direction: row-reverse; }

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

.message.assistant .avatar { background: var(--brand-light); color: var(--brand); }
.message.user .avatar { background: var(--brand); color: #fff; }

.bubble {
  max-width: 82%;
  padding: 0.875rem 1.125rem;
  border-radius: 16px;
  font-size: 0.9375rem;
  line-height: 1.6;
}

.message.assistant .bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  border-top-left-radius: 4px;
  box-shadow: var(--shadow);
}

.message.user .bubble {
  background: var(--brand);
  color: #fff;
  border-top-right-radius: 4px;
}

.bubble p { margin-bottom: 0.5rem; }
.bubble p:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { padding-left: 1.25rem; margin: 0.375rem 0; }
.bubble li { margin-bottom: 0.25rem; }
.bubble code {
  background: rgba(0,0,0,0.06);
  padding: 0.125rem 0.375rem;
  border-radius: 4px;
  font-size: 0.875em;
  font-family: 'SF Mono', Menlo, monospace;
}
.message.user .bubble code { background: rgba(255,255,255,0.2); }

.typing-indicator {
  display: flex;
  gap: 4px;
  align-items: center;
  padding: 0.875rem 1.125rem;
}

.typing-indicator span {
  width: 7px;
  height: 7px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: bounce 1.2s infinite;
}

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

@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.5; }
  40% { transform: translateY(-6px); opacity: 1; }
}

.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--text-muted);
  padding: 2rem;
  gap: 0.75rem;
}

.empty-state .icon { font-size: 2.5rem; }
.empty-state h2 { font-size: 1.125rem; font-weight: 600; color: var(--text); }
.empty-state p { font-size: 0.875rem; max-width: 320px; }

/* Input area */
.input-area {
  padding: 1rem 0 1.25rem;
  flex-shrink: 0;
}

.input-row {
  display: flex;
  gap: 0.625rem;
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  padding: 0.5rem 0.5rem 0.5rem 1rem;
  box-shadow: var(--shadow);
  transition: border-color 0.15s;
}

.input-row:focus-within { border-color: var(--brand); }

.input-row textarea {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  font-family: inherit;
  font-size: 0.9375rem;
  line-height: 1.5;
  max-height: 160px;
  background: transparent;
  color: var(--text);
}

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

.send-btn {
  width: 36px;
  height: 36px;
  background: var(--brand);
  border: none;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  align-self: flex-end;
  flex-shrink: 0;
  transition: background 0.15s;
  color: #fff;
}

.send-btn:hover:not(:disabled) { background: var(--brand-dark); }
.send-btn:disabled { background: var(--border); cursor: not-allowed; }

.input-hint {
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 0.5rem;
}

/* Admin layout */
.admin-layout {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.admin-main {
  flex: 1;
  padding: 1.5rem;
  max-width: 860px;
  width: 100%;
  margin: 0 auto;
  overflow-y: auto;
}

.admin-section {
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  margin-bottom: 1.5rem;
}

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

.admin-section-header h2 {
  font-size: 0.9375rem;
  font-weight: 600;
}

.admin-section-body { padding: 1.25rem; }

/* File list */
.file-list { display: flex; flex-direction: column; gap: 0.5rem; }

.file-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  gap: 1rem;
  transition: background 0.1s;
}

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

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

.file-name {
  font-weight: 500;
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.file-meta { font-size: 0.78rem; color: var(--text-muted); margin-top: 2px; }

.file-actions { display: flex; gap: 0.375rem; flex-shrink: 0; }

.empty-files {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.875rem;
  padding: 2rem 1rem;
}

/* Upload zone */
.upload-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius-sm);
  padding: 2rem;
  text-align: center;
  cursor: pointer;
  transition: all 0.15s;
  color: var(--text-muted);
  font-size: 0.875rem;
}

.upload-zone:hover, .upload-zone.drag-over {
  border-color: var(--brand);
  background: var(--brand-light);
  color: var(--brand);
}

.upload-zone .icon { font-size: 1.75rem; margin-bottom: 0.5rem; display: block; }

/* Editor modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 1rem;
}

.modal {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0,0,0,0.15);
  width: 100%;
  max-width: 700px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}

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

.modal-header h3 { font-size: 1rem; font-weight: 600; }

.modal-body {
  flex: 1;
  padding: 1.25rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.modal-body label { font-size: 0.8125rem; font-weight: 600; color: var(--text-muted); }

.modal-body input, .modal-body textarea {
  width: 100%;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.625rem 0.875rem;
  font-size: 0.9rem;
  font-family: 'SF Mono', Menlo, monospace;
  outline: none;
  transition: border-color 0.15s;
  color: var(--text);
}

.modal-body input:focus, .modal-body textarea:focus { border-color: var(--brand); }

.modal-body textarea {
  min-height: 320px;
  resize: vertical;
  line-height: 1.6;
}

.modal-footer {
  padding: 1rem 1.25rem;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 0.625rem;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  background: var(--text);
  color: #fff;
  padding: 0.75rem 1.25rem;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  z-index: 300;
  animation: slideUp 0.2s ease;
}

.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

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

.hidden { display: none !important; }

/* Responsive */
@media (max-width: 600px) {
  .admin-main { padding: 1rem; }
  .bubble { max-width: 92%; }
}
