:root {
  color-scheme: light;
  --bg: #f4f5f7;
  --panel: #ffffff;
  --border: #e3e5e9;
  --text: #1a1d23;
  --muted: #6b7280;
  --accent: #2f6fed;
  --accent-hover: #2559c4;
  --accent-soft: #e8f0fe;
  --bubble-user: #2f6fed;
  --bubble-user-text: #ffffff;
  --bubble-bot: #ffffff;
  --bubble-bot-text: #1a1d23;
  --bubble-bot-border: #e3e5e9;
  --danger: #c0392b;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06);
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #16181d;
    --panel: #1e2127;
    --border: #2c303a;
    --text: #eceef1;
    --muted: #9aa0aa;
    --accent: #5b8def;
    --accent-hover: #7aa2f2;
    --accent-soft: #23304a;
    --bubble-user: #5b8def;
    --bubble-user-text: #0d1117;
    --bubble-bot: #262a32;
    --bubble-bot-text: #eceef1;
    --bubble-bot-border: #2c303a;
    --danger: #ff6b5e;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

button {
  font-family: inherit;
}

.icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

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

/* ---------- Sidebar ---------- */

.sidebar {
  width: 272px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 14px;
  gap: 10px;
  overflow-y: auto;
  transition: transform 0.2s ease;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 4px 8px;
}

.sidebar-brand .brand-mark {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 14px;
  flex-shrink: 0;
}

.sidebar-brand .brand-name {
  font-weight: 600;
  font-size: 15px;
}

.sidebar-close {
  display: none;
  margin-left: auto;
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 4px;
}

.new-chat-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: background 0.15s ease;
}

.new-chat-btn:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.conversation-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow-y: auto;
}

.conversation-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conversation-item .icon {
  width: 15px;
  height: 15px;
  color: var(--muted);
  flex-shrink: 0;
}

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

.conversation-item.active {
  background: var(--accent-soft);
  color: var(--accent);
}

.conversation-item.active .icon {
  color: var(--accent);
}

.conversation-empty {
  color: var(--muted);
  font-size: 12px;
  padding: 10px;
  text-align: center;
}

.sidebar-footer {
  margin-top: auto;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text);
}

.user-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 12px;
  flex-shrink: 0;
  text-transform: uppercase;
}

#username-label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.logout-btn {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 6px;
  border-radius: 6px;
  display: flex;
}

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

.login-link {
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
  text-decoration: none;
  padding: 6px 10px;
  border-radius: 6px;
}

.login-link:hover {
  background: var(--accent-soft);
}

.sidebar-backdrop {
  display: none;
}

/* ---------- Chat main ---------- */

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

.chat-topbar {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.menu-toggle {
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  padding: 6px;
  display: flex;
}

.chat-topbar .brand-name {
  font-weight: 600;
  font-size: 14px;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.empty-state {
  margin: auto;
  text-align: center;
  max-width: 480px;
  color: var(--muted);
}

.empty-state .empty-icon {
  width: 44px;
  height: 44px;
  color: var(--accent);
  margin-bottom: 12px;
}

.empty-state h2 {
  color: var(--text);
  font-size: 18px;
  margin: 0 0 6px;
}

.empty-state p {
  margin: 0 0 18px;
  font-size: 13px;
}

.suggestion-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.suggestion-chip {
  padding: 8px 14px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 999px;
  font-size: 13px;
  cursor: pointer;
  box-shadow: var(--shadow);
}

.suggestion-chip:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.message-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  max-width: 78%;
}

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

.message-row.assistant {
  align-self: flex-start;
}

.message-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: #fff;
}

.message-row.user .message-avatar {
  background: var(--bubble-user);
}

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

.message-avatar .icon {
  width: 15px;
  height: 15px;
}

.message-content {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.message-row.user .message-content {
  align-items: flex-end;
}

.message-row.assistant .message-content {
  align-items: flex-start;
}

.message-bubble {
  padding: 10px 14px;
  border-radius: 14px;
  line-height: 1.5;
  font-size: 14px;
  white-space: pre-wrap;
  word-wrap: break-word;
  box-shadow: var(--shadow);
}

.message-row.user .message-bubble {
  background: var(--bubble-user);
  color: var(--bubble-user-text);
  border-bottom-right-radius: 4px;
}

.message-row.assistant .message-bubble {
  background: var(--bubble-bot);
  color: var(--bubble-bot-text);
  border: 1px solid var(--bubble-bot-border);
  border-bottom-left-radius: 4px;
}

.message-row.pending .message-bubble {
  padding: 14px;
}

.typing-dots {
  display: inline-flex;
  gap: 4px;
  align-items: center;
}

.typing-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--muted);
  animation: typing-bounce 1.2s infinite ease-in-out;
}

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

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

@keyframes typing-bounce {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.5;
  }
  30% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

.message-error {
  color: var(--danger);
}

.message-actions {
  display: flex;
  gap: 2px;
  margin-top: 4px;
  opacity: 0;
  transition: opacity 0.12s ease;
}

.message-row:hover .message-actions,
.message-actions:focus-within {
  opacity: 1;
}

.message-row.pending .message-actions {
  display: none;
}

.message-action-btn {
  width: 26px;
  height: 26px;
  border: none;
  background: none;
  color: var(--muted);
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.message-action-btn .icon {
  width: 14px;
  height: 14px;
}

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

.message-action-btn:disabled {
  opacity: 0.5;
  cursor: default;
}

.message-action-btn.copied {
  color: #1a9e5c;
}

@media (hover: none) {
  .message-actions {
    opacity: 1;
  }
}

.message-sources {
  margin-top: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.source-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--bg);
  color: var(--muted);
  border: 1px solid var(--border);
}

.message-row.user .source-chip {
  background: rgba(255, 255, 255, 0.15);
  color: var(--bubble-user-text);
  border-color: transparent;
}

.source-chip .icon {
  width: 11px;
  height: 11px;
}

.source-action-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-left: 2px;
  padding: 2px;
  border: none;
  border-radius: 999px;
  background: transparent;
  color: inherit;
  cursor: pointer;
}

.source-action-btn:hover {
  background: rgba(0, 0, 0, 0.08);
}

.message-row.user .source-action-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}

.source-action-btn .icon {
  width: 12px;
  height: 12px;
}

.model-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 6px 24px 0;
  font-size: 12px;
  color: var(--muted);
}

.model-select {
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
}

.chat-form {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 14px 24px 18px;
  background: var(--bg);
}

.chat-form-inner {
  flex: 1;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 8px 8px 8px 16px;
  box-shadow: var(--shadow);
  max-width: 780px;
  margin: 0 auto;
  width: 100%;
}

.chat-form textarea {
  flex: 1;
  resize: none;
  border: none;
  outline: none;
  background: transparent;
  padding: 6px 0;
  font-size: 14px;
  font-family: inherit;
  color: var(--text);
  max-height: 160px;
}

.chat-form textarea::placeholder {
  color: var(--muted);
}

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

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

.send-btn:disabled {
  opacity: 0.5;
  cursor: default;
}

.mic-btn {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease, color 0.15s ease;
}

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

.mic-btn.recording {
  color: #fff;
  background: var(--danger);
  animation: mic-pulse 1.4s ease-in-out infinite;
}

.mic-btn.transcribing {
  opacity: 0.6;
  cursor: wait;
}

.mic-btn:disabled {
  cursor: wait;
}

@keyframes mic-pulse {
  0%, 100% {
    box-shadow: 0 0 0 0 rgba(192, 57, 43, 0.45);
  }
  50% {
    box-shadow: 0 0 0 8px rgba(192, 57, 43, 0);
  }
}

/* ---------- Auth pages ---------- */

.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.auth-wrap {
  width: 100%;
  max-width: 380px;
  padding: 28px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
}

.auth-brand {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
}

.auth-brand .brand-mark {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 18px;
}

.auth-wrap h1 {
  font-size: 17px;
  margin: 0;
  text-align: center;
}

.auth-wrap form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.auth-field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.auth-field label {
  font-size: 12px;
  color: var(--muted);
}

.auth-wrap input {
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s ease;
}

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

.auth-wrap button {
  padding: 11px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  margin-top: 4px;
  transition: background 0.15s ease;
}

.auth-wrap button:hover:not(:disabled) {
  background: var(--accent-hover);
}

.auth-wrap button:disabled {
  opacity: 0.6;
  cursor: default;
}

.auth-error {
  color: var(--danger);
  font-size: 13px;
  min-height: 16px;
}

.auth-toggle {
  margin-top: 16px;
  font-size: 13px;
  text-align: center;
  color: var(--muted);
}

.auth-toggle a {
  color: var(--accent);
  cursor: pointer;
  text-decoration: none;
  font-weight: 500;
}

.auth-toggle a:hover {
  text-decoration: underline;
}

/* ---------- Responsive ---------- */

@media (max-width: 860px) {
  .chat-topbar {
    display: flex;
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 20;
    transform: translateX(-100%);
    box-shadow: 0 0 0 rgba(0, 0, 0, 0);
  }

  .layout.sidebar-open .sidebar {
    transform: translateX(0);
    box-shadow: 4px 0 16px rgba(0, 0, 0, 0.15);
  }

  .sidebar-close {
    display: flex;
  }

  .layout.sidebar-open .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.35);
    z-index: 15;
  }

  .message-row {
    max-width: 92%;
  }

  .chat-form {
    padding: 10px 12px 14px;
  }
}

/* Sidebar: link Quản trị (chỉ hiện với admin) */
.admin-link {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 4px;
  color: var(--muted);
  text-decoration: none;
  font-size: 13px;
  border-radius: 6px;
}

.admin-link:hover {
  background: var(--bg);
  color: var(--text);
}

/* Trang /admin.html — form nạp tài liệu */
.admin-page {
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
}

.admin-topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 14px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.admin-back {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  text-decoration: none;
  font-size: 14px;
}

.admin-back:hover {
  color: var(--accent);
}

.admin-main {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 32px;
  padding: 32px 16px 60px;
}

.admin-form {
  width: 100%;
  max-width: 640px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.admin-tabs {
  display: flex;
  gap: 8px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 12px;
}

.admin-tab {
  flex: 1;
  padding: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--muted);
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
}

.admin-tab.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.admin-row {
  display: flex;
  gap: 16px;
}

.admin-row .admin-field {
  flex: 1;
  min-width: 0;
}

.admin-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.admin-field label {
  font-size: 13px;
  color: var(--muted);
}

.admin-field input {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
}

.admin-field input:focus {
  outline: none;
  border-color: var(--accent);
}

.admin-submit-btn {
  padding: 12px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.admin-submit-btn:hover {
  background: var(--accent-hover);
}

.admin-submit-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.admin-message {
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 13px;
}

.admin-message-success {
  background: var(--accent-soft);
  color: var(--accent);
}

.admin-message-error {
  background: rgba(192, 57, 43, 0.12);
  color: var(--danger);
}

@media (max-width: 640px) {
  .admin-row {
    flex-direction: column;
    gap: 16px;
  }
}

.admin-list-section {
  width: 100%;
  max-width: 1400px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 20px 24px;
}

.admin-list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.admin-list-header h2 {
  font-size: 16px;
  margin: 0;
}

.admin-refresh-btn {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 6px;
  border-radius: 6px;
  display: flex;
}

.admin-refresh-btn:hover {
  background: var(--bg);
  color: var(--accent);
}

.admin-table-wrap {
  overflow-x: auto;
}

.admin-table {
  width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
  font-size: 13px;
}

.admin-table th,
.admin-table td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  overflow-wrap: break-word;
}

/* ID, Nội bộ, Quyền, Số đoạn, Ngày nạp, actions: giá trị ngắn, giữ 1 dòng. Tiêu đề/Nguồn/License
   (nội dung dài, không kiểm soát được độ dài) được để mặc định wrap nhiều dòng thay vì ellipsis,
   để bảng luôn vừa chiều rộng màn hình máy tính, không cần cuộn ngang. */
.admin-table th:nth-child(1),
.admin-table td:nth-child(1) {
  width: 5%;
  white-space: nowrap;
}

.admin-table th:nth-child(2),
.admin-table td:nth-child(2) {
  width: 19%;
}

.admin-table th:nth-child(3),
.admin-table td:nth-child(3) {
  width: 15%;
}

.admin-table th:nth-child(4),
.admin-table td:nth-child(4) {
  width: 18%;
}

.admin-table th:nth-child(5),
.admin-table td:nth-child(5) {
  width: 6%;
  white-space: nowrap;
}

.admin-table th:nth-child(6),
.admin-table td:nth-child(6) {
  width: 7%;
  white-space: nowrap;
}

.admin-table th:nth-child(7),
.admin-table td:nth-child(7) {
  width: 6%;
  white-space: nowrap;
}

.admin-table th:nth-child(8),
.admin-table td:nth-child(8) {
  width: 9%;
  white-space: nowrap;
}

.admin-table th:nth-child(9),
.admin-table td:nth-child(9) {
  width: 15%;
}

.admin-table th {
  color: var(--muted);
  font-weight: 600;
}

.admin-table-empty {
  text-align: center;
  color: var(--muted);
  white-space: normal;
}

.admin-delete-btn {
  padding: 5px 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--danger);
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
}

.admin-delete-btn:hover {
  background: var(--danger);
  color: #fff;
  border-color: var(--danger);
}

.admin-table-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.admin-edit-btn {
  padding: 5px 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--accent);
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
}

.admin-edit-btn:hover {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.admin-pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-top: 16px;
  font-size: 13px;
  color: var(--muted);
}

.admin-pagination button {
  padding: 6px 14px;
}

.admin-pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.admin-dialog {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0;
  background: var(--panel);
  color: var(--text);
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 640px;
}

.admin-dialog::backdrop {
  background: rgba(0, 0, 0, 0.45);
}

.admin-dialog-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 24px;
  max-height: 85vh;
  overflow-y: auto;
}

.admin-dialog-form h3 {
  margin: 0;
  font-size: 16px;
}

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

.admin-dialog-actions .admin-submit-btn {
  padding: 10px 20px;
}
