:root {
  --bg: #f6f4ef;
  --panel: #ffffff;
  --ink: #1f2328;
  --muted: #6b6f76;
  --line: #e3dfd6;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #e0f2ef;
  --user: #0f766e;
  --user-ink: #ffffff;
  --bot: #ffffff;
  --code: #f1efe9;
  --err: #b42318;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.04);
  /* Đồ thị: 3 màu phân loại đã kiểm tra phân biệt được (kể cả mù màu) */
  --g-s1: #2a78d6; --g-s2: #eb6834; --g-s3: #1baf7a;
  --g-ink: #2b2d31; --g-gray: #9a9ea6; --g-line: rgba(31,35,40,.16); --g-line-hi: rgba(31,35,40,.75);
  --g-label: #3a3d42; --g-surface: #f6f4ef; --g-focus: #1f2328;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171a; --panel: #1d2024; --ink: #e8e6e1; --muted: #9a9ea6; --line: #2e3238;
    --accent: #2dd4bf; --accent-ink: #062b27; --accent-soft: #123a36;
    --user: #1f5f58; --user-ink: #f0fdfa; --bot: #1d2024; --code: #262a30; --err: #f97066;
    --shadow: none;
    --g-s1: #3987e5; --g-s2: #d95926; --g-s3: #199e70;
    --g-ink: #e8e6e1; --g-gray: #7d828a; --g-line: rgba(232,230,225,.16); --g-line-hi: rgba(232,230,225,.75);
    --g-label: #d6d4cf; --g-surface: #15171a; --g-focus: #f0efec;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 15px/1.6 "Be Vietnam Pro", system-ui, -apple-system, "Segoe UI", sans-serif;
}
button { font: inherit; color: inherit; cursor: pointer; }
.muted { color: var(--muted); }
.pad { padding: 16px; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; letter-spacing: -.01em; }
.logo { font-size: 20px; }

/* ---------- Đăng nhập ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 28px; box-shadow: var(--shadow); display: grid; gap: 10px;
}
.login-card .brand { font-size: 20px; }
.login-card label { font-weight: 600; font-size: 13px; }
.login-card input {
  padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--ink); font: inherit;
}
.err { color: var(--err); margin: 0; font-size: 14px; }
.btn { padding: 11px 14px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); font-weight: 600; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

/* ---------- Khung app ---------- */
.app { height: 100dvh; display: flex; flex-direction: column; }
.top {
  display: flex; align-items: center; gap: 16px; padding: 10px 16px; border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.tabs { display: flex; gap: 4px; background: var(--bg); padding: 3px; border-radius: 10px; margin-left: auto; }
.tab { border: 0; background: transparent; padding: 6px 12px; border-radius: 8px; font-weight: 600; font-size: 14px; color: var(--muted); }
.tab.active { background: var(--panel); color: var(--ink); box-shadow: var(--shadow); }
.logout { color: var(--muted); text-decoration: none; font-size: 14px; }
.view { display: none; flex: 1; min-height: 0; }
.view.active { display: flex; }

/* ---------- Chat ---------- */
.view-chat { flex-direction: column; max-width: 860px; width: 100%; margin: 0 auto; }
.messages { flex: 1; overflow-y: auto; padding: 20px 16px 8px; display: flex; flex-direction: column; gap: 14px; }
.welcome { margin: auto 0; text-align: center; padding: 24px 8px; }
.welcome h1 { font-size: 24px; margin: 0 0 8px; letter-spacing: -.02em; }
.welcome p { max-width: 520px; margin: 0 auto 16px; }
.suggest { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.chip { border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: 6px 12px; font-size: 14px; }
.chip:hover { border-color: var(--accent); }

.msg { max-width: 88%; padding: 10px 14px; border-radius: var(--radius); word-wrap: break-word; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--user); color: var(--user-ink); border-bottom-right-radius: 4px; white-space: pre-wrap; }
.msg.user .tag { display: block; font-size: 11px; opacity: .8; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.msg.bot { align-self: flex-start; background: var(--bot); border: 1px solid var(--line); border-bottom-left-radius: 4px; box-shadow: var(--shadow); }
.msg.bot.error { border-color: var(--err); color: var(--err); white-space: pre-wrap; }
.msg.bot .meta { font-size: 12px; color: var(--muted); margin-top: 6px; }
.msg.pending { color: var(--muted); display: flex; align-items: center; gap: 10px; }
.dots span { display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: var(--muted); margin-right: 3px; animation: blink 1.2s infinite both; }
.dots span:nth-child(2) { animation-delay: .2s; } .dots span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .2; } 40% { opacity: 1; } }

.md > :first-child { margin-top: 0; } .md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol { margin: .5em 0; }
.md ul, .md ol { padding-left: 1.3em; }
.md h1, .md h2, .md h3 { line-height: 1.3; margin: 1em 0 .4em; letter-spacing: -.01em; }
.md h1 { font-size: 1.45em; } .md h2 { font-size: 1.2em; } .md h3 { font-size: 1.05em; }
.md code { background: var(--code); padding: 1px 5px; border-radius: 5px; font-size: .9em; }
.md pre { background: var(--code); padding: 10px; border-radius: 8px; overflow-x: auto; }
.md pre code { background: none; padding: 0; }
.md blockquote { margin: .6em 0; padding-left: 12px; border-left: 3px solid var(--line); color: var(--muted); }
.md table { border-collapse: collapse; display: block; overflow-x: auto; max-width: 100%; font-size: .92em; }
.md th, .md td { border: 1px solid var(--line); padding: 5px 8px; text-align: left; vertical-align: top; }
.md a { color: var(--accent); }
.md img { max-width: 100%; }
a.wikilink { color: var(--accent); text-decoration: none; background: var(--accent-soft); padding: 0 4px; border-radius: 4px; cursor: pointer; }
a.wikilink.missing { opacity: .6; text-decoration: line-through dotted; }

.composer { padding: 10px 16px calc(12px + env(safe-area-inset-bottom)); display: grid; gap: 8px; }
.composer-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }
.mode { display: flex; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 3px; }
.mode-btn { border: 0; background: transparent; padding: 4px 14px; border-radius: 7px; font-weight: 600; font-size: 14px; color: var(--muted); }
.mode-btn.active { background: var(--accent); color: var(--accent-ink); }
.actions { display: flex; gap: 4px; }
.link-btn { border: 0; background: transparent; padding: 4px 8px; border-radius: 8px; font-size: 13px; color: var(--muted); }
.link-btn:hover { background: var(--panel); color: var(--ink); }
.composer-row {
  display: flex; align-items: flex-end; gap: 8px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 16px; padding: 6px 6px 6px 8px; box-shadow: var(--shadow);
}
.composer-row:focus-within { border-color: var(--accent); }
.attach { display: grid; place-items: center; width: 36px; height: 36px; border-radius: 10px; cursor: pointer; flex: none; font-size: 18px; }
.attach:hover { background: var(--bg); }
.attach input { display: none; }
textarea#input {
  flex: 1; border: 0; outline: none; resize: none; background: transparent; color: var(--ink);
  font: inherit; padding: 7px 0; max-height: 180px; min-width: 0;
}
.send { flex: none; width: 36px; height: 36px; border-radius: 10px; border: 0; background: var(--accent); color: var(--accent-ink); font-size: 15px; }
.send:disabled { opacity: .45; cursor: default; }
.file-pill { display: inline-flex; align-items: center; gap: 8px; justify-self: start; background: var(--accent-soft); border-radius: 999px; padding: 3px 6px 3px 12px; font-size: 13px; max-width: 100%; }
.file-pill[hidden] { display: none; }
.file-pill span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-pill button { border: 0; background: transparent; padding: 0 4px; }

/* ---------- Wiki ---------- */
.view-wiki { max-width: 1200px; width: 100%; margin: 0 auto; }
.wiki-list { width: 300px; flex: none; border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.wiki-list input {
  margin: 12px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel); color: var(--ink); font: inherit;
}
.storage { margin: -4px 14px 4px; font-size: 12px; line-height: 1.4; overflow-wrap: anywhere; }
.tree { overflow-y: auto; padding: 0 8px 16px; }
.tree h4 { margin: 14px 8px 4px; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.tree button { display: block; width: 100%; text-align: left; border: 0; background: transparent; padding: 5px 8px; border-radius: 7px; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree button:hover { background: var(--panel); }
.tree button.active { background: var(--accent-soft); color: var(--ink); font-weight: 600; }
.wiki-page { flex: 1; overflow-y: auto; padding: 24px 32px 48px; min-width: 0; }
.wiki-page .path { font-size: 12px; color: var(--muted); margin-bottom: 4px; display: flex; gap: 10px; align-items: center; }
.wiki-page .fm { display: flex; flex-wrap: wrap; gap: 6px; margin: 4px 0 16px; }
.wiki-page .fm span { font-size: 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 1px 9px; color: var(--muted); }
.back { display: none; border: 0; background: transparent; color: var(--accent); padding: 0; font-weight: 600; }

@media (max-width: 720px) {
  .top { gap: 10px; padding: 8px 12px; }
  .top .brand span:last-child { display: none; }
  .messages { padding: 14px 12px 6px; }
  .composer { padding-left: 12px; padding-right: 12px; }
  .msg { max-width: 94%; }
  .view-wiki.active { display: block; overflow-y: auto; }
  .wiki-list { width: 100%; border-right: 0; }
  .view-wiki.reading .wiki-list { display: none; }
  .view-wiki:not(.reading) .wiki-page { display: none; }
  .wiki-page { padding: 16px; }
  .back { display: inline; }
}

/* ---------- Đồ thị ---------- */
.view-graph { flex-direction: column; min-height: 0; }
.g-toolbar { display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center; padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--panel); }
.g-toolbar input[type=search] { width: 200px; padding: 6px 10px; border: 1px solid var(--line); border-radius: 9px; background: var(--bg); color: var(--ink); font: inherit; font-size: 14px; }
.g-legend { display: flex; flex-wrap: wrap; gap: 4px; flex: 1 1 100%; min-width: 0; order: 3; }
.g-chip { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: var(--bg); border-radius: 999px; padding: 2px 10px 2px 8px; font-size: 12.5px; }
.g-chip.off { opacity: .45; text-decoration: line-through; }
.g-count { color: var(--muted); font-size: 11.5px; }
.g-sw { width: 10px; height: 10px; flex: none; display: inline-block; box-sizing: border-box; }
.g-sw-circle { border-radius: 50%; background: var(--sw); }
.g-sw-square { background: var(--sw); border-radius: 1px; }
.g-sw-diamond { background: var(--sw); transform: rotate(45deg) scale(.85); }
.g-sw-ring { border-radius: 50%; border: 2.5px solid var(--sw); }
.g-sw-dashed { border-radius: 50%; border: 1.5px dashed var(--sw); }
.g-tools { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; margin-left: auto; }
.g-toggle { font-size: 13px; color: var(--muted); display: inline-flex; gap: 4px; align-items: center; cursor: pointer; }
.g-stage { position: relative; flex: 1; min-height: 0; overflow: hidden; background: var(--g-surface); touch-action: none; }
.g-stage canvas { position: absolute; inset: 0; display: block; cursor: grab; }
.g-tip { position: absolute; top: 0; left: 0; pointer-events: none; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; font-size: 13px; box-shadow: var(--shadow); max-width: 260px; }
.g-tip .muted { font-size: 12px; }
.g-panel { position: absolute; left: 12px; bottom: 12px; width: min(340px, calc(100% - 24px)); background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; box-shadow: var(--shadow); display: grid; gap: 6px; }
.g-panel[hidden], .g-tip[hidden], .g-empty[hidden] { display: none; }
.g-panel-title { font-weight: 700; line-height: 1.35; }
.g-panel-path { font-size: 12px; overflow-wrap: anywhere; }
.g-panel-stats { font-size: 13px; display: grid; gap: 6px; }
.g-nb { display: flex; flex-wrap: wrap; gap: 4px; max-height: 92px; overflow-y: auto; }
.g-nb .chip { font-size: 12px; padding: 2px 9px; }
.g-panel-actions { display: flex; gap: 8px; align-items: center; }
.g-panel-actions select { padding: 5px 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); font: inherit; font-size: 13px; flex: 1; min-width: 0; }
.g-panel-actions .link-btn { margin-left: auto; }
.btn.small { padding: 5px 12px; font-size: 13px; }
.g-empty { position: absolute; inset: 0; display: grid; place-items: center; margin: 0; padding: 16px; text-align: center; }
.g-hint { position: absolute; right: 12px; bottom: 8px; margin: 0; font-size: 11.5px; pointer-events: none; }
@media (max-width: 720px) {
  .g-toolbar { padding: 8px 12px; }
  .g-toolbar input[type=search] { width: 100%; }
  .g-legend { flex-basis: 100%; }
  .g-hint { display: none; }
  .g-nb { max-height: 58px; }
  .g-panel { padding: 10px 12px; }
}

/* ---------- Bố cục 2 nửa: trái Hỏi & Nạp / Wiki, phải Đồ thị ---------- */
.workspace { flex: 1; display: flex; min-height: 0; }
.pane-left, .pane-right { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.pane-left { flex: 0 0 var(--split, 50%); container-type: inline-size; container-name: left; }
.pane-right { flex: 1 1 0; }
.splitter {
  flex: none; width: 7px; margin: 0 -3px; z-index: 2; cursor: col-resize; position: relative;
  background: linear-gradient(var(--line), var(--line)) center / 1px 100% no-repeat;
}
.splitter:hover, .splitter:focus-visible, .workspace.resizing .splitter {
  background: linear-gradient(var(--accent), var(--accent)) center / 3px 100% no-repeat; outline: none;
}
.workspace.resizing { cursor: col-resize; user-select: none; }
.workspace.resizing canvas { pointer-events: none; }
.g-cited {
  position: absolute; top: 10px; left: 10px; display: inline-flex; align-items: center; gap: 4px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 2px 4px 2px 12px;
  font-size: 13px; box-shadow: var(--shadow);
}
.g-cited[hidden] { display: none; }

@media (min-width: 900px) {
  .tab[data-view="graph"] { display: none; }
  .pane-right .view-graph { display: flex; }
  .view-chat { max-width: none; }
  .view-wiki { max-width: none; }
}
@media (max-width: 899px) {
  .splitter { display: none; }
  .pane-left { flex: 1 1 auto; }
  .workspace[data-view="graph"] .pane-left { display: none; }
  .workspace:not([data-view="graph"]) .pane-right { display: none; }
}
/* Wiki khi nửa trái hẹp */
@container left (max-width: 680px) {
  .wiki-list { width: 210px; }
  .wiki-page { padding: 20px 20px 40px; }
}
@container left (max-width: 520px) {
  .view-wiki.active { display: block; overflow-y: auto; }
  .wiki-list { width: 100%; border-right: 0; }
  .view-wiki.reading .wiki-list { display: none; }
  .view-wiki:not(.reading) .wiki-page { display: none; }
  .back { display: inline; }
}

@media (min-width: 900px) {
  .g-panel { width: min(300px, calc(100% - 24px)); padding: 10px 12px; }
  .g-nb { max-height: 60px; }
  #gOpen { display: none; }
}
