:root {
  --bg: #f4f6fb; --panel: #ffffff; --text: #182033; --muted: #5b6478; --line: #e1e5ef;
  --brand: #1e4fd8; --brand-ink: #ffffff; --brand-soft: #e8eefc; --user: #1e4fd8; --user-ink: #fff;
  --bot: #ffffff; --ok: #0f7a4a; --ok-soft: #e6f5ee; --err: #b42318; --err-soft: #fdecea;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 16px rgba(16, 24, 40, .06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1420; --panel: #171e2d; --text: #e6e9f2; --muted: #9aa3b7; --line: #263048;
    --brand: #6d93ff; --brand-ink: #0b1020; --brand-soft: #1d2a4a; --user: #3d6bf0; --user-ink: #fff;
    --bot: #1b2334; --ok: #5fd39b; --ok-soft: #12301f; --err: #ff8a80; --err-soft: #3a1714;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3); color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.55 "Be Vietnam Pro", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column;
}
a { color: var(--brand); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.top {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 20px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand img { flex: none; }
.brand-title { font-weight: 700; font-size: 17px; }
.brand-sub { color: var(--muted); font-size: 12.5px; }

.layout {
  flex: 1; min-height: 0; width: 100%; max-width: 1180px; margin: 0 auto;
  display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 20px; padding: 16px 20px;
}
.chat {
  display: flex; flex-direction: column; min-height: 0; background: var(--panel);
  border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); overflow: hidden;
}
.messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.msg { display: flex; }
.msg.user { justify-content: flex-end; }
.bubble { max-width: min(720px, 88%); padding: 10px 14px; border-radius: 14px; overflow-wrap: anywhere; }
.msg.bot .bubble { background: var(--bot); border: 1px solid var(--line); border-top-left-radius: 4px; }
.msg.user .bubble { background: var(--user); color: var(--user-ink); border-top-right-radius: 4px; white-space: pre-wrap; }
.bubble p { margin: 0 0 8px; } .bubble p:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin: 4px 0 8px; padding-left: 22px; }
.bubble li { margin: 2px 0; }
.bubble h3, .bubble h4 { margin: 8px 0 6px; font-size: 15px; }
.bubble code { background: var(--brand-soft); padding: 0 4px; border-radius: 4px; }
.bubble em { color: var(--muted); }
.msg.bot .bubble.error { background: var(--err-soft); color: var(--err); border-color: transparent; }
.tag { display: inline-block; margin-top: 6px; font-size: 11.5px; color: var(--muted); }
.typing { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); opacity: .4; animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; } .typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 50% { opacity: 1; } }

.suggest { display: flex; gap: 8px; padding: 0 20px 10px; overflow-x: auto; scrollbar-width: thin; }
.suggest button {
  flex: none; border: 1px solid var(--line); background: var(--brand-soft); color: var(--text);
  padding: 6px 12px; border-radius: 999px; font: inherit; font-size: 13.5px; cursor: pointer;
}
.suggest button:hover { border-color: var(--brand); }
.ask { display: flex; gap: 8px; align-items: flex-end; padding: 10px 14px; border-top: 1px solid var(--line); }
.ask textarea {
  flex: 1; resize: none; max-height: 160px; font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; outline: none;
}
.ask textarea:focus, .card input:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.btn-send {
  flex: none; width: 44px; height: 44px; border: 0; border-radius: 12px; cursor: pointer;
  background: var(--brand); color: var(--brand-ink); display: grid; place-items: center;
}
.btn-send:disabled { opacity: .5; cursor: default; }
.note { margin: 0; padding: 0 16px 10px; color: var(--muted); font-size: 12px; }

.side { display: flex; flex-direction: column; gap: 16px; overflow-y: auto; min-height: 0; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 16px; box-shadow: var(--shadow); }
.card h2 { margin: 0 0 10px; font-size: 15px; }
.card label { display: block; font-size: 13px; color: var(--muted); margin: 8px 0 4px; }
.card input {
  width: 100%; font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; outline: none;
}
.btn {
  margin-top: 12px; width: 100%; border: 0; border-radius: 10px; padding: 9px; cursor: pointer;
  background: var(--brand); color: var(--brand-ink); font: inherit; font-weight: 600;
}
.btn-ghost { border: 1px solid var(--line); background: transparent; color: var(--text); border-radius: 10px; padding: 7px 12px; font: inherit; font-size: 13px; cursor: pointer; }
.small { color: var(--muted); font-size: 12px; margin: 10px 0 0; }
.lookup-result:empty { display: none; }
.lookup-result { margin-top: 12px; padding: 10px 12px; border-radius: 10px; font-size: 14px; }
.lookup-result.ok { background: var(--ok-soft); }
.lookup-result.ok strong { color: var(--ok); }
.lookup-result.err { background: var(--err-soft); color: var(--err); }
.lookup-result a { font-weight: 600; }
.files { list-style: none; margin: 0; padding: 0; font-size: 14px; }
.files li a { display: flex; justify-content: space-between; gap: 8px; padding: 6px 0; text-decoration: none; color: var(--text); border-bottom: 1px dashed var(--line); }
.files li a:hover { color: var(--brand); }
.files li a span { color: var(--muted); font-size: 11px; font-weight: 600; flex: none; }
.files .group { margin-top: 10px; font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }

.only-mobile { display: none; }
@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; padding: 0; gap: 0; }
  .chat { border-radius: 0; border-left: 0; border-right: 0; }
  .side { display: none; padding: 16px; overflow: visible; }
  body.side-open .side { display: flex; }
  body.side-open .chat { display: none; }
  .only-mobile { display: inline-block; }
  .top { padding: 10px 16px; }
  .brand-sub { display: none; }
  .messages { padding: 16px; }
  .suggest { padding: 0 16px 10px; }
  .bubble { max-width: 92%; }
}

/* ---- Thanh trên: chọn đợt, tài khoản ---- */
.top-actions { display: flex; align-items: center; gap: 8px; }
.top-actions select {
  font: inherit; font-size: 13.5px; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 10px; padding: 7px 10px;
}
.user-menu { position: relative; }
.user-menu summary {
  list-style: none; cursor: pointer; border: 1px solid var(--line); border-radius: 999px;
  padding: 6px 12px; font-size: 13.5px; font-weight: 600; max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.user-menu summary::-webkit-details-marker { display: none; }
.user-pop {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 10; min-width: 220px; display: flex; flex-direction: column; gap: 4px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px; box-shadow: var(--shadow);
}
.user-pop a, .user-pop button {
  text-align: left; font: inherit; font-size: 14px; color: var(--text); background: none; border: 0; padding: 6px 4px; cursor: pointer; text-decoration: none; border-radius: 6px;
}
.user-pop a:hover, .user-pop button:hover { background: var(--brand-soft); }
.banner { max-width: 1180px; margin: 12px auto 0; padding: 10px 14px; border-radius: 12px; background: var(--brand-soft); font-size: 14px; width: calc(100% - 40px); }

/* ---- Dưới câu trả lời ---- */
.related { margin-top: 10px; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.related .small { width: 100%; margin: 0; }
.related button {
  border: 1px solid var(--line); background: var(--brand-soft); color: var(--text); text-align: left;
  padding: 5px 10px; border-radius: 10px; font: inherit; font-size: 13px; cursor: pointer;
}
.related button:hover { border-color: var(--brand); }
.feedback { margin-top: 8px; display: flex; align-items: center; gap: 6px; }
.feedback .small { margin: 0; }
.feedback button { border: 1px solid var(--line); background: transparent; border-radius: 8px; padding: 2px 8px; cursor: pointer; font-size: 14px; }
.feedback button:hover { border-color: var(--brand); }

/* ---- Trang đăng nhập ---- */
.login-body { align-items: center; justify-content: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 440px; background: var(--panel); border: 1px solid var(--line); border-radius: 18px;
  padding: 28px 24px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 12px;
}
.login-card h1 { font-size: 20px; margin: 8px 0 0; }
.login-card p { margin: 0; color: var(--muted); }
#g-dang-nhap { min-height: 44px; margin: 6px 0; }
.dev-login { display: flex; flex-direction: column; gap: 6px; border-top: 1px dashed var(--line); padding-top: 12px; }
.dev-login label { font-size: 13px; color: var(--muted); }
.dev-login input { font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--text); }
.msg-err { color: var(--err) !important; background: var(--err-soft); padding: 8px 10px; border-radius: 10px; }

/* ---- Quản trị ---- */
.admin { width: 100%; max-width: 900px; margin: 16px auto; padding: 0 20px; display: flex; flex-direction: column; gap: 16px; }
.kv { border-collapse: collapse; width: 100%; font-size: 14px; }
.kv th { text-align: left; color: var(--muted); font-weight: 500; padding: 6px 12px 6px 0; vertical-align: top; width: 38%; }
.kv td { padding: 6px 0; overflow-wrap: anywhere; }
.kv .err, .errors { color: var(--err); }
.errors { font-size: 13.5px; padding-left: 18px; }
.admin .btn { width: auto; padding: 9px 16px; }
a.btn-ghost { text-decoration: none; }
@media (max-width: 860px) {
  .top-actions select { max-width: 110px; }
  .banner { width: calc(100% - 32px); }
}
@media (max-width: 860px) {
  .brand { flex: 1 1 auto; min-width: 0; }
  .brand > div { min-width: 0; }
  .brand-title { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .brand img { width: 30px; height: 30px; }
  .top { gap: 8px; }
}
@media (max-width: 480px) {
  .brand > div { display: none; }
  .top-actions { flex: 1; justify-content: flex-end; }
}
@media (max-width: 480px) { .brand { flex: 0 0 auto; } }
