:root {
  /* Light — dịu hơn (bớt trắng chói khi dùng buổi tối): mặt phẳng off-white thay #fff */
  color-scheme: light;
  --bg: #eef1f6;
  --surface: #f9fafb;
  --surface-2: #e9edf2;
  --sidebar-bg: #f4f6f9;
  --border: #e2e6ec;
  --text: #1c2024;
  --text-soft: #5b6471;
  --accent: #1d9bf0;
  --accent-soft: #e8f4fd;
  --btn-bg: #1d9bf0;
  --btn-bg-hover: #0ea5e9;
  --green: #16a34a;
  --green-soft: #dcfce7;
  --yellow-soft: #fef9c3;
  --user-bubble: #d8f3df;
  --blockquote-bg: #eef1f5;
  --blockquote-border: #c3ccd8;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 1px 3px rgba(0, 0, 0, 0.06);
}
[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f141a;
  --surface: #181d24;
  --surface-2: #232b34;
  --sidebar-bg: #14181e;
  --border: #2b333d;
  --text: #e6e9ef;
  --text-soft: #97a1ae;
  --accent: #4aa8f0;
  --accent-soft: #14304a;
  --btn-bg: #2c6aa0;
  --btn-bg-hover: #357cb5;
  --green: #2ea043;
  --green-soft: #14301e;
  --yellow-soft: #3a3416;
  --user-bubble: #1c3a2a;
  --blockquote-bg: #1f2630;
  --blockquote-border: #39434f;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 1px 3px rgba(0, 0, 0, 0.45);
}
* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%; overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Inter', 'Segoe UI', system-ui, sans-serif;
  background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.55;
}

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

/* Sidebar */
.sidebar {
  width: var(--sidebar-w, 312px); flex-shrink: 0; background: var(--sidebar-bg);
  border-right: 1px solid var(--border); display: flex; flex-direction: column;
  padding: 16px 12px; overflow-y: auto;
}
/* Tay kéo rộng sidebar (min 200 / max 460 enforce trong JS) */
.sidebar-resizer {
  width: 6px; flex-shrink: 0; cursor: col-resize; background: transparent;
  margin-left: -3px; z-index: 5;
}
.sidebar-resizer:hover, .sidebar-resizer.dragging { background: var(--accent); opacity: 0.45; }
body.resizing-sidebar { cursor: col-resize; user-select: none; }
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 16px; }
.brand-logo {
  width: 36px; height: 36px; border-radius: 10px;
  background: linear-gradient(135deg, #4ed4f8 0%, #16a34a 100%);
  color: white; font-weight: 800; display: flex; align-items: center; justify-content: center; font-size: 18px;
}
.brand-title { font-weight: 700; font-size: 15px; }
.brand-sub { font-size: 11px; color: var(--text-soft); }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex; align-items: center; gap: 10px; padding: 9px 12px;
  border-radius: 8px; cursor: pointer; font-weight: 500; font-size: 13.5px;
  background: none; border: none; text-align: left; color: var(--text); width: 100%;
}
.nav-item:hover { background: var(--surface-2); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-icon { width: 20px; text-align: center; }

.sidebar-spacer { flex: 1; }

.user-block {
  border-top: 1px solid var(--border); padding-top: 12px; margin-top: 8px;
  display: flex; flex-direction: column; gap: 4px;
}
.user-name { font-weight: 700; font-size: 13px; }
.user-role { font-size: 11px; color: var(--text-soft); }
.link-btn {
  background: none; border: none; text-align: left; padding: 6px 8px;
  border-radius: 6px; cursor: pointer; font-size: 12px; color: var(--text-soft);
}
.link-btn:hover { background: var(--surface-2); }

/* Main */
.main { flex: 1; display: flex; flex-direction: column; overflow: hidden; min-width: 0; min-height: 0; }
.topbar {
  padding: 14px 28px; border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center;
  background: var(--bg); flex-shrink: 0;
}
.topbar-title { font-weight: 600; }
.icon-btn {
  background: var(--accent-soft); color: var(--accent); border: none;
  padding: 6px 12px; border-radius: 14px; font-size: 12px; font-weight: 600; cursor: pointer;
}
.topbar-right { display: flex; align-items: center; gap: 8px; }
.theme-toggle {
  background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
  width: 34px; height: 34px; border-radius: 50%; font-size: 15px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; line-height: 1;
}
.theme-toggle:hover { border-color: var(--accent); }

/* View container */
.view { flex: 1; overflow-y: auto; display: flex; flex-direction: column; min-height: 0; }
.view-header {
  padding: 20px 28px 8px; display: flex; justify-content: space-between; align-items: center;
  max-width: 1100px; margin: 0 auto; width: 100%;
}
.view-header h2 { margin: 0; }

/* Chat thread */
.view-chat { padding: 0; }
.thread {
  flex: 1 1 0; overflow-y: auto; overflow-x: hidden;
  padding: 20px 28px 16px; display: flex; flex-direction: column; gap: 16px;
  max-width: 920px; margin: 0 auto; width: 100%; min-height: 0;
}
.bot-card {
  display: flex; align-items: center; gap: 10px; padding: 14px 16px;
  background: var(--surface); border-radius: 12px; border: 1px solid var(--border);
}
.bot-avatar {
  width: 36px; height: 36px; border-radius: 50%;
  background: linear-gradient(135deg, #4ed4f8 0%, #16a34a 100%);
  color: white; font-weight: 700; display: flex; align-items: center; justify-content: center;
}
.bot-name { font-weight: 600; }
.bot-by { font-size: 12px; color: var(--text-soft); }
.messages { display: flex; flex-direction: column; gap: 14px; padding: 4px 0; }
.empty-state { text-align: center; color: var(--text-soft); padding: 24px 0; }
.user-bubble {
  align-self: flex-end; background: var(--user-bubble);
  border-radius: 14px; padding: 10px 14px; max-width: 60%;
  display: flex; flex-direction: column; gap: 8px;
}
.user-bubble img { max-width: 240px; border-radius: 6px; cursor: zoom-in; }
.user-bubble img:hover { opacity: 0.92; }

.composer-thumb img { cursor: zoom-in; }
.composer-thumb img:hover { opacity: 0.92; }

/* Progress steps */
.progress-step {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 14px; display: flex; align-items: center; gap: 8px; font-size: 13px;
}
.progress-step.thinking { color: var(--text-soft); }
.progress-step .sparkle { color: var(--accent); font-size: 14px; }
.progress-step .step-status { font-weight: 600; color: var(--text); margin-right: 4px; }
.progress-step.thinking .step-status { color: var(--accent); }
.progress-step.done .step-status { color: var(--green); }

/* Output markdown */
.output {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 24px 28px; font-size: 14.5px; line-height: 1.7;
}
.output h1, .output h2, .output h3 { margin-top: 1.2em; margin-bottom: 0.4em; font-weight: 700; }
.output h1 { font-size: 18px; } .output h2 { font-size: 16px; } .output h3 { font-size: 15px; }
/* Khối quy tắc / ghi nhớ: trung tính, gọn — viền trái mảnh xám, không nền màu.
   Phân biệt 2 khối bằng icon 📌 / ⭐, không bằng màu. */
.output blockquote {
  border-left: 3px solid var(--blockquote-border); background: var(--blockquote-bg);
  padding: 6px 14px; margin: 10px 0; border-radius: 0 6px 6px 0; color: var(--text);
}
.output blockquote p { margin: 2px 0; }
.output blockquote strong { color: var(--text); }
.output blockquote ul { margin: 4px 0 0 0; padding-left: 18px; }
/* .ghi-nho-block (do markGhiNhoBlocks gắn) kế thừa cùng look trung tính — chủ ý không override màu. */
.output code { background: var(--surface-2); padding: 1px 5px; border-radius: 3px; font-size: 13px; }
.output pre { background: #1f2937; color: #e5e7eb; padding: 12px 14px; border-radius: 8px; overflow-x: auto; font-size: 13px; }
.output ul { padding-left: 22px; }
.output table {
  border-collapse: collapse; margin: 12px 0; font-size: 13.5px;
  display: block; max-width: 100%; overflow-x: auto;
}
.output table th, .output table td {
  border: 1px solid var(--border); padding: 6px 12px; text-align: left;
}
.output table th { background: var(--surface-2); font-weight: 600; }
.output table tr:nth-child(even) td { background: var(--surface-2); }
.output hr { border: none; border-top: 1px solid var(--border); margin: 16px 0; }
.output .katex-error { color: #dc2626 !important; background: #fee2e2; padding: 1px 4px; border-radius: 3px; }

/* Composer */
.composer {
  padding: 12px 28px 18px; background: var(--bg); border-top: 1px solid var(--border);
  max-width: 920px; margin: 0 auto; width: 100%; flex-shrink: 0;
}
.composer-box { background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 12px 16px; box-shadow: var(--shadow); }
.composer-thumb {
  display: flex; align-items: center; gap: 10px; margin-bottom: 8px;
  background: var(--surface-2); border-radius: 8px; padding: 8px 10px;
}
.composer-thumb img { max-height: 60px; border-radius: 6px; border: 1px solid var(--border); }
.thumb-info { flex: 1; font-size: 12px; color: var(--text-soft); }
.thumb-x { background: rgba(0, 0, 0, 0.6); color: white; border: none; width: 22px; height: 22px; border-radius: 50%; cursor: pointer; font-size: 11px; }
.composer-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.composer-text { flex: 1; color: var(--text-soft); font-size: 14px; min-width: 100px; }
.composer-input {
  flex: 1; min-width: 200px; min-height: 24px; max-height: 160px;
  border: none; outline: none; font-family: inherit; font-size: 14px;
  resize: none; padding: 8px 4px; line-height: 1.5; background: transparent;
  color: var(--text);
}
.composer-input::placeholder { color: var(--text-soft); }
.composer-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.composer-icon { background: var(--surface-2); border: none; font-size: 12px; cursor: pointer; padding: 6px 10px; border-radius: 14px; font-weight: 500; }
.composer-icon.icon-only { padding: 6px 9px; font-size: 16px; line-height: 1; border-radius: 50%; width: 32px; height: 32px; display: inline-flex; align-items: center; justify-content: center; }
.composer-icon:hover { background: var(--border); }
.composer-icon svg { display: block; width: 18px; height: 18px; }
.mode-tabs { display: inline-flex; background: var(--surface-2); padding: 3px; border-radius: 18px; }
.mode-tab { background: none; border: none; padding: 6px 14px; border-radius: 14px; cursor: pointer; font-size: 13px; color: var(--text-soft); font-weight: 500; }
.mode-tab.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); font-weight: 600; }
.mode-tab.disabled-mode { opacity: 0.4; cursor: not-allowed; }
.msg-counter {
  font-size: 11px; color: var(--text-soft); font-weight: 600;
  background: var(--surface-2); padding: 4px 8px; border-radius: 10px;
}
.msg-counter.warn { background: #fef3c7; color: #92400e; }
.send-btn { background: var(--green); color: white; border: none; width: 36px; height: 36px; border-radius: 50%; font-size: 16px; cursor: pointer; font-weight: 700; }
.send-btn:disabled { background: var(--surface-2); color: var(--text-soft); cursor: not-allowed; }
.composer-foot { text-align: center; font-size: 11px; color: var(--text-soft); margin-top: 8px; }

/* Sample board: vertical scroll page, mỗi Lớp = 1 ROW ngang */
.sample-board {
  display: flex; flex-direction: column; gap: 24px;
  padding: 16px 28px;
  overflow-y: auto; overflow-x: hidden;
  width: 100%; max-width: 1400px; margin: 0 auto;
}
.sample-row {
  display: flex; flex-direction: column; gap: 10px;
}
.sample-row-header {
  font-weight: 700; font-size: 14px; color: var(--text);
  border-bottom: 2px solid var(--accent); padding-bottom: 6px;
}
.sample-row-count { font-size: 11px; color: var(--text-soft); margin-left: 6px; font-weight: 500; }
.sample-row-scroller {
  display: flex; gap: 14px;
  overflow-x: auto; overflow-y: hidden;
  padding: 4px 0 8px;
}
.sample-row-scroller > .sample-card {
  flex: 0 0 320px;
}
.sample-row-scroller::-webkit-scrollbar { height: 8px; }
.sample-row-scroller::-webkit-scrollbar-track { background: var(--surface-2); }
.sample-row-scroller::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 4px; }
.sample-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  overflow: hidden; display: flex; flex-direction: column; box-shadow: var(--shadow);
  width: 100%;
}
.sample-card-img {
  width: 100%;
  background-size: 100% auto;     /* full column width, image tự nhiên tỉ lệ */
  background-position: top left;
  background-color: var(--surface-2);
  background-repeat: no-repeat;
  cursor: zoom-in;
  border-bottom: 1px solid var(--border);
  min-height: 100px;              /* fallback nếu ảnh chưa load */
}
.sample-card-img.dynamic { background-size: contain; background-position: top center; }
.sample-card-img:hover { opacity: 0.92; }
.sample-card-body { padding: 12px 14px; flex: 1; }
.sample-card-title { font-weight: 600; font-size: 14px; margin-bottom: 4px; }
.sample-card-meta { font-size: 12px; color: var(--text-soft); }
.sample-card-pick {
  background: var(--btn-bg); color: white; border: none; padding: 10px 14px;
  cursor: pointer; font-weight: 600; font-size: 13px;
}
.sample-card-pick:hover { background: var(--btn-bg-hover); }

/* Scroll bar custom */
.sample-board::-webkit-scrollbar { height: 10px; }
.sample-board::-webkit-scrollbar-track { background: var(--surface-2); }
.sample-board::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 5px; }

/* History */
.history-list { padding: 16px 28px; display: flex; flex-direction: column; gap: 10px; max-width: 920px; margin: 0 auto; width: 100%; }
.history-card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 12px 16px; cursor: pointer; }
.history-card:hover { border-color: var(--accent); }
.history-meta { display: flex; justify-content: space-between; font-size: 12px; color: var(--text-soft); margin-bottom: 4px; }
.history-time { color: var(--text-soft); }
.history-title { font-weight: 600; }
.history-grade { font-size: 12px; color: var(--text-soft); }

.primary-btn { background: var(--btn-bg); color: white; border: none; padding: 8px 16px; border-radius: 8px; font-weight: 600; cursor: pointer; }

/* Image viewer modal — cao hơn mọi modal khác (sample-modal etc.).
   Compound selector `.modal.image-modal` để override `.modal { z-index: 100 }`
   bên dưới (CSS specificity 2 classes > 1 class). */
.modal.image-modal {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.88);
  display: flex; align-items: center; justify-content: center;
  z-index: 500; cursor: zoom-out;
}
.image-modal img {
  max-width: 95vw; max-height: 95vh; object-fit: contain; border-radius: 6px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.6);
}
.image-modal-close {
  position: absolute; top: 16px; right: 20px;
  background: rgba(255, 255, 255, 0.15); color: white;
  border: none; width: 36px; height: 36px; border-radius: 50%;
  font-size: 18px; cursor: pointer; backdrop-filter: blur(8px);
}
.image-modal-close:hover { background: rgba(255, 255, 255, 0.3); }

/* Section label trong sidebar */
.section-label {
  font-size: 11px; color: var(--text-soft); text-transform: uppercase;
  font-weight: 700; margin: 16px 8px 6px; letter-spacing: 0.5px;
}

/* Recent list in sidebar */
.recent-list { display: flex; flex-direction: column; gap: 2px; padding: 0 4px; }
.recent-empty { font-size: 12px; color: var(--text-soft); padding: 6px 10px; }
.recent-item {
  background: none; border: none; text-align: left;
  padding: 8px 10px; border-radius: 6px; cursor: pointer;
  font-size: 12.5px; color: var(--text); width: 100%;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  display: flex; flex-direction: column; gap: 2px;
}
.recent-item:hover { background: var(--surface-2); }
.recent-item .recent-title { font-weight: 500; overflow: hidden; text-overflow: ellipsis; }
.recent-item .recent-time { font-size: 10px; color: var(--text-soft); }

/* (sample-group đã thay bằng sample-board + sample-column) */

/* Modals */
.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.4); display: flex; align-items: center; justify-content: center; z-index: 100; }
/* Login modal: dim + blur background app skeleton để user thấy bóng dáng UI */
#login-modal { background: rgba(15, 23, 42, 0.55); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); }
/* Khi chưa login app skeleton lộ ra — disable pointer events để tránh interact nhầm */
body.not-logged-in .app { pointer-events: none; user-select: none; }
.modal-card { background: var(--surface); padding: 24px 28px; border-radius: 16px; width: 460px; max-width: 92vw; max-height: 90vh; overflow-y: auto; }
.modal-card.wide { width: 620px; }
.modal-card h2 { margin-top: 0; }
.modal-card label { display: block; margin: 10px 0 4px; font-size: 13px; font-weight: 500; }
.modal-card input[type="text"], .modal-card input[type="file"] {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; font-size: 14px;
  background: var(--surface-2); color: var(--text);
}
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.modal-actions button { padding: 8px 18px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); cursor: pointer; font-size: 13px; font-weight: 500; }
.modal-actions button.primary { background: var(--green); color: white; border-color: var(--green); }
.modal-card .hint { font-size: 12px; color: var(--text-soft); margin-top: 12px; }
#sample-preview img { max-width: 100%; max-height: 400px; object-fit: contain; border-radius: 6px; margin-top: 8px; cursor: zoom-in; background: var(--surface-2); border: 1px solid var(--border); }
#sample-preview img:hover { opacity: 0.92; }
#sample-preview-info { font-size: 11px; color: var(--text-soft); margin-top: 4px; }
.modal-card select { width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; font-size: 14px; background: var(--surface); }
.btn-mini { background: var(--accent-soft); color: var(--accent); border: none; padding: 4px 10px; border-radius: 12px; font-size: 11px; font-weight: 600; cursor: pointer; margin-top: 6px; }
.btn-mini:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-mini:hover:not(:disabled) { background: #d4eaf9; }

/* History filter bar */
.history-filter-bar {
  display: flex; gap: 12px; align-items: center; flex-wrap: wrap;
  padding: 12px 28px; max-width: 1100px; margin: 0 auto; width: 100%;
  border-bottom: 1px solid var(--border);
}
.history-filter-bar select, .history-filter-bar label {
  font-size: 13px;
}
.history-filter-bar select {
  padding: 6px 10px; border: 1px solid var(--border); border-radius: 6px;
}
.history-user-group {
  padding: 12px 28px 0; max-width: 1100px; margin: 0 auto; width: 100%;
}
.history-user-group h4 {
  margin: 16px 0 8px; font-size: 13px; color: var(--text-soft);
  text-transform: uppercase; letter-spacing: 0.5px;
}
.history-card { position: relative; }
.history-card.is-deleted { opacity: 0.55; background: #fee2e2; }
.history-card.is-deleted .history-title::after { content: ' (đã xóa)'; color: #dc2626; font-size: 11px; }
.history-actions {
  position: absolute; top: 10px; right: 10px;
  display: none; gap: 6px;
}
.history-card:hover .history-actions { display: flex; }
.history-actions button {
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
  padding: 4px 8px; font-size: 11px; cursor: pointer;
}
.history-actions button:hover { background: #fee2e2; }
.history-actions .restore-btn:hover { background: #dcfce7; }

/* Recent item delete */
.recent-item-row {
  display: flex; align-items: center; gap: 4px;
}
.recent-item-row .recent-item { flex: 1; min-width: 0; }
.recent-item-row .recent-delete {
  background: none; border: none; color: var(--text-soft); cursor: pointer;
  padding: 4px 6px; border-radius: 4px; font-size: 12px; opacity: 0;
}
.recent-item-row:hover .recent-delete { opacity: 1; }
.recent-item-row .recent-delete:hover { background: #fee2e2; color: #dc2626; }

/* ===== Thumb + tooltip ảnh đề trong lịch sử ===== */
.hist-thumb {
  width: 40px; height: 40px; border-radius: 6px; object-fit: cover;
  background: var(--surface-2); border: 1px solid var(--border);
  flex-shrink: 0; cursor: zoom-in;
}
.hist-thumb.no-img { opacity: 0.45; }
.recent-item-row .hist-thumb { width: 34px; height: 34px; }
.hist-tip {
  position: fixed; z-index: 600; display: none; pointer-events: none;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 6px; box-shadow: 0 8px 28px rgba(0, 0, 0, 0.35);
}
.hist-tip img { display: block; max-width: 480px; max-height: 380px; object-fit: contain; border-radius: 6px; }
/* History card: layout flex, ảnh bên TRÁI (to hơn cho dễ xem) */
.history-card { display: flex; align-items: center; gap: 12px; }
.history-card-main { flex: 1; min-width: 0; }
.history-card .hist-thumb { width: 52px; height: 52px; }
