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

:root {
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --bg: #f8fafc;
  --bg-card: #ffffff;
  --bg-sidebar: #f1f5f9;
  --text: #1e293b;
  --text-secondary: #64748b;
  --border: #e2e8f0;
  --danger: #ef4444;
  --success: #22c55e;
  --warn: #f59e0b;
  --shadow: 0 1px 3px rgba(0,0,0,.1);
  --radius: 8px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-card);
  color: var(--text);
  cursor: pointer;
  font-size: 14px;
  transition: all .2s;
  gap: 6px;
}
.btn:hover { background: #f1f5f9; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { color: var(--danger); border-color: var(--danger); }
.btn-danger:hover { background: #fef2f2; }
.btn-sm { padding: 4px 10px; font-size: 13px; }
.btn-block { width: 100%; }
.btn-outline { border-color: var(--border); background: transparent; }

/* ─── Layout ─── */

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

.sidebar {
  width: 280px;
  min-width: 280px;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px;
  gap: 12px;
}

.sidebar-header h1 {
  font-size: 20px;
  font-weight: 700;
  color: var(--primary);
}
.sidebar-header .subtitle {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 2px;
}

.btn-new-chat {
  width: 100%;
  background: var(--primary);
  color: #fff;
  border: none;
  padding: 10px;
  font-weight: 500;
}
.btn-new-chat:hover { background: var(--primary-hover); }

.btn-admin {
  width: 100%;
  text-align: center;
  color: var(--text-secondary);
  font-size: 13px;
}
.btn-admin:hover { color: var(--primary); }

.session-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.session-item {
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: background .15s;
}
.session-item:hover { background: #e2e8f0; }
.session-item.active { background: #dbeafe; color: var(--primary); }

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

.session-delete {
  background: none;
  border: none;
  color: #94a3b8;
  cursor: pointer;
  font-size: 16px;
  padding: 0 4px;
  visibility: hidden;
}
.session-item:hover .session-delete { visibility: visible; }
.session-delete:hover { color: var(--danger); }

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

/* ─── Main Chat ─── */

.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.chat-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  max-width: 860px;
  width: 100%;
  margin: 0 auto;
  padding: 0 20px;
  min-height: 0;
}

.chat-messages {
  flex: 1 1 0%;
  overflow-y: auto;
  padding: 20px 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-height: 0;
}

.welcome-message {
  text-align: center;
  padding: 60px 20px;
  margin: auto;
}
.welcome-icon { font-size: 48px; margin-bottom: 12px; }
.welcome-message h2 { font-size: 24px; margin-bottom: 8px; }
.welcome-message p { color: var(--text-secondary); margin-bottom: 24px; }

.suggestion-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  max-width: 600px;
  margin: 0 auto;
}

.suggestion-btn {
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-card);
  cursor: pointer;
  font-size: 13px;
  text-align: left;
  transition: all .2s;
  color: var(--text);
}
.suggestion-btn:hover {
  border-color: var(--primary);
  color: var(--primary);
  box-shadow: var(--shadow);
}

/* ─── Messages ─── */

.message {
  display: flex;
  gap: 12px;
  animation: fadeIn .3s;
}

@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: 16px;
  flex-shrink: 0;
  background: #f1f5f9;
}

.message-body {
  flex: 1;
  min-width: 0;
}

.message-header {
  font-size: 12px;
  font-weight: 600;
  margin-bottom: 4px;
  color: var(--text-secondary);
}

.message-content {
  font-size: 14px;
  line-height: 1.75;
  white-space: pre-wrap;
}

.message-content h3, .message-content h4 {
  margin-top: 12px;
  margin-bottom: 4px;
}

.warning-tag {
  display: inline-block;
  background: #fef3c7;
  color: #92400e;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  margin-bottom: 8px;
}

.sources {
  margin-top: 12px;
  padding: 10px 14px;
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 12px;
}
.sources ul { margin: 4px 0 0 16px; }
.sources li { margin: 2px 0; }

.loading-dots {
  display: flex;
  gap: 4px;
  padding: 8px 0;
}
.loading-dots span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #94a3b8;
  animation: bounce 1.4s infinite;
}
.loading-dots span:nth-child(2) { animation-delay: .2s; }
.loading-dots span:nth-child(3) { animation-delay: .4s; }
@keyframes bounce {
  0%, 80%, 100% { transform: scale(0); }
  40% { transform: scale(1); }
}

/* ─── Chat Input ─── */

.chat-input-area {
  border-top: 1px solid var(--border);
  padding: 16px 0;
  flex: 0 0 auto;
}

.input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 12px;
  transition: border-color .2s;
}
.input-wrapper:focus-within { border-color: var(--primary); }

.input-wrapper textarea {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  font-size: 14px;
  font-family: inherit;
  line-height: 1.5;
  max-height: 150px;
  padding: 4px 0;
  background: transparent;
}

.btn-send {
  width: 36px;
  height: 36px;
  padding: 0;
  border: none;
  background: var(--primary);
  color: #fff;
  border-radius: 6px;
  flex-shrink: 0;
}
.btn-send:hover { background: var(--primary-hover); }
.btn-send:disabled { background: #94a3b8; }

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

/* ─── Admin ─── */

.admin-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.admin-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  height: 56px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  gap: 16px;
}

.nav-brand {
  display: flex;
  align-items: center;
  gap: 8px;
}
.nav-brand a { font-weight: 700; color: var(--primary); font-size: 18px; }
.nav-brand span { color: var(--text-secondary); font-size: 13px; }

.nav-tabs { display: flex; gap: 4px; }

.nav-tab {
  padding: 8px 16px;
  border: none;
  background: none;
  cursor: pointer;
  font-size: 14px;
  color: var(--text-secondary);
  border-radius: 6px;
  transition: all .15s;
}
.nav-tab:hover { background: #f1f5f9; color: var(--text); }
.nav-tab.active { background: #dbeafe; color: var(--primary); font-weight: 500; }

.nav-actions { display: flex; gap: 8px; }

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

.tab-content { display: none; }
.tab-content.active { display: block; }

.tab-content h2 { font-size: 20px; margin-bottom: 16px; }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  text-align: center;
}
.stat-value { font-size: 32px; font-weight: 700; color: var(--primary); }
.stat-label { font-size: 13px; color: var(--text-secondary); margin-top: 4px; }

.toolbar {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.toolbar input, .toolbar select {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  background: var(--bg-card);
  outline: none;
}
.toolbar input:focus, .toolbar select:focus { border-color: var(--primary); }

.table-container {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
th {
  padding: 10px 14px;
  text-align: left;
  background: #f8fafc;
  font-weight: 600;
  font-size: 12px;
  color: var(--text-secondary);
  text-transform: uppercase;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
td {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
tr:last-child td { border-bottom: none; }
tr:hover td { background: #f8fafc; }

.cell-title {
  max-width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
  color: var(--primary);
}
.cell-title:hover { text-decoration: underline; }

.cell-actions { display: flex; gap: 4px; white-space: nowrap; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 500;
  background: #e2e8f0;
  color: var(--text-secondary);
}
.badge-approved, .badge-success { background: #dcfce7; color: #166534; }
.badge-pending, .badge-warn { background: #fef3c7; color: #92400e; }
.badge-rejected, .badge-danger { background: #fee2e2; color: #991b1b; }

.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 16px;
  font-size: 13px;
  color: var(--text-secondary);
}

/* ─── Modal ─── */

.modal { display: none; }
.modal.active { display: block; }

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 100;
}
.modal-content {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: min(90vw, 700px);
  max-height: 80vh;
  background: var(--bg-card);
  border-radius: 12px;
  box-shadow: 0 20px 60px rgba(0,0,0,.15);
  z-index: 101;
  display: flex;
  flex-direction: column;
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-header h3 { font-size: 16px; }
.modal-close {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: var(--text-secondary);
  padding: 4px;
  line-height: 1;
}
.modal-close:hover { color: var(--text); }
.modal-body {
  padding: 20px;
  overflow-y: auto;
}

.form-group {
  margin-bottom: 16px;
}
.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  margin-bottom: 6px;
  color: var(--text-secondary);
}
.form-group input, .form-group textarea, .form-group select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  font-family: inherit;
  outline: none;
}
.form-group input:focus, .form-group textarea:focus, .form-group select:focus {
  border-color: var(--primary);
}

.session-msg {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 8px;
}
.session-msg strong { display: block; margin-bottom: 4px; }
.session-msg small { display: block; margin-top: 8px; color: var(--text-secondary); }

/* ─── Login ─── */

.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background: var(--bg);
}

.login-container { width: 100%; max-width: 380px; padding: 20px; }

.login-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 40px 32px;
  box-shadow: 0 4px 12px rgba(0,0,0,.05);
}
.login-card h1 {
  text-align: center;
  color: var(--primary);
  font-size: 24px;
  margin-bottom: 4px;
}
.login-card > p {
  text-align: center;
  color: var(--text-secondary);
  font-size: 14px;
  margin-bottom: 24px;
}

.back-link {
  display: block;
  text-align: center;
  margin-top: 16px;
  font-size: 13px;
  color: var(--text-secondary);
}
.back-link:hover { color: var(--primary); }

/* ─── User Status ─── */

.user-status {
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 8px;
}

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

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

/* ─── Knowledge Sources ─── */

.text-muted { color: var(--text-secondary); font-size: 13px; }

.sources-grid { margin-top: 16px; }

.source-category { margin-bottom: 24px; }
.source-category h3 { font-size: 16px; margin-bottom: 12px; padding-bottom: 8px; border-bottom: 1px solid var(--border); color: var(--primary); }

.source-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
  gap: 12px;
}

.source-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.source-card.source-disabled { opacity: 0.5; }

.source-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.source-name { font-weight: 600; font-size: 15px; }
.source-desc { font-size: 13px; color: var(--text-secondary); line-height: 1.5; }
.source-meta { display: flex; gap: 6px; align-items: center; font-size: 12px; }

.source-keywords { display: flex; flex-wrap: wrap; gap: 4px; }
.tag {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 11px;
  background: #f0f9ff;
  color: #0369a1;
  border: 1px solid #bae6fd;
}
.source-link { font-size: 12px; color: var(--primary); margin-top: auto; }

/* ─── Scrollbar ─── */

::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #94a3b8; }

/* ─── Streaming & Typing Animation ─── */

.message.streaming .message-content {
  min-height: 24px;
}

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

.typing-indicator span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--primary);
  animation: typingBounce 1.4s infinite ease-in-out;
}

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

@keyframes typingBounce {
  0%, 60%, 100% { 
    transform: translateY(0);
    opacity: 0.4;
  }
  30% { 
    transform: translateY(-8px);
    opacity: 1;
  }
}

.cursor {
  display: inline-block;
  color: var(--primary);
  font-weight: bold;
  animation: blink 1s infinite;
  margin-left: 2px;
}

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

/* Streaming message highlight */
.message.streaming {
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.95; }
}

/* Status indicator for long operations */
.status-indicator {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  background: #f0f9ff;
  border: 1px solid #bae6fd;
  border-radius: 4px;
  font-size: 12px;
  color: #0369a1;
  margin-top: 8px;
}

.status-indicator::before {
  content: '';
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #0ea5e9;
  animation: statusPulse 1.5s infinite;
}

@keyframes statusPulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.5; transform: scale(0.8); }
}
