/* ── Reset & variables ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:         #212121;
  --surface:    #2f2f2f;
  --surface-2:  #3a3a3a;
  --surface-3:  #484848;
  --border:     rgba(255,255,255,.08);
  --border-md:  rgba(255,255,255,.13);
  --accent:     #0d9488;
  --accent-2:   #14b8a6;
  --text:       #ececec;
  --text-2:     #b4b4b4;
  --muted:      #858585;
  --red:          #f87171;
  --code-inline:  #e0b97d;
  --code-block:   #d4d4d4;
  --table-stripe: rgba(255,255,255,.025);
}

/* ── Light theme ── */
body.light {
  --bg:        #ffffff;
  --surface:   #f4f4f4;
  --surface-2: #ececec;
  --surface-3: #e0e0e0;
  --border:    rgba(0,0,0,.06);
  --border-md: rgba(0,0,0,.12);
  --text:      #1a1a1a;
  --text-2:    #4a4a4a;
  --muted:        #909090;
  --red:          #dc2626;
  --code-inline:  #b45309;
  --code-block:   #374151;
  --table-stripe: rgba(0,0,0,.03);
}

html, body { height: 100%; overflow: hidden; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 14px;
  background: var(--bg); color: var(--text);
  display: flex; flex-direction: column;
  transition: background .2s, color .2s;
}

/* ── App ── */
#app { display: flex; flex-direction: column; height: 100vh; }

/* ── Split view ── */
#split-view { flex: 1; display: flex; overflow: hidden; min-height: 0; }

/* ── Image lightbox (xem ảnh cỡ đầy đủ) ── */
.img-lightbox-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.82);
  display: flex; align-items: center; justify-content: center;
  z-index: 10001; padding: 32px; cursor: zoom-out;
}
.img-lightbox-img {
  max-width: 100%; max-height: 100%; object-fit: contain;
  border-radius: 8px; box-shadow: 0 12px 40px rgba(0,0,0,.5);
  cursor: default;
}
.img-lightbox-close {
  position: fixed; top: 18px; right: 24px;
  background: rgba(255,255,255,.12); border: none; color: #fff;
  font-size: 24px; line-height: 1; cursor: pointer;
  width: 40px; height: 40px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
}
.img-lightbox-close:hover { background: rgba(255,255,255,.22); }

/* ═══════════════════════ RIGHT PANEL ═══════════════════════ */
#chat-panel {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column;
  background: var(--bg); overflow: hidden;
  position: relative;
}

/* ── Welcome ── */
.welcome-screen {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 40px 24px; text-align: center;
}
.welcome-logo {
  width: 60px; height: 60px; border-radius: 16px; overflow: hidden;
  margin-bottom: 20px; background: var(--surface-2); padding: 8px;
  border: 1.5px solid var(--border-md);
  display: flex; align-items: center; justify-content: center;
}
.welcome-logo img { width: 100%; height: 100%; object-fit: contain; border-radius: 8px; }
.welcome-screen h2 { font-size: 21px; font-weight: 600; color: var(--text); margin-bottom: 10px; line-height: 1.35; }
.welcome-screen p  { font-size: 15px; color: var(--muted); line-height: 1.7; max-width: 420px; }
.suggestions {
  display: flex; flex-wrap: wrap; gap: 8px;
  margin-top: 26px; justify-content: center; max-width: 560px;
}
.suggestion-btn {
  background: var(--surface); border: 1px solid var(--border-md);
  color: var(--text-2); padding: 9px 16px; border-radius: 20px;
  cursor: pointer; font-size: 16px; font-family: inherit;
  transition: all .15s;
}
.suggestion-btn:hover { background: var(--surface-2); border-color: var(--accent); color: var(--text); }

/* ── Messages ── */
#messages {
  flex: 1; overflow-y: auto; padding: 20px 0 24px; display: none; scroll-behavior: smooth;
}
#messages::-webkit-scrollbar { width: 5px; }
#messages::-webkit-scrollbar-thumb { background: var(--surface-3); border-radius: 3px; }

.message-row {
  display: flex; padding: 10px 20px; gap: 14px;
  max-width: 720px; margin: 0 auto; width: 100%;
}
.message-row.user { flex-direction: row-reverse; }

.avatar {
  width: 30px; height: 30px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700; align-self: flex-start; margin-top: 3px;
}
.avatar.bot { display: none; }
.avatar.bot img { display: none; }
.avatar.user-av { display: none; }

.msg-content { display: flex; flex-direction: column; gap: 5px; min-width: 0; flex: 1; }
.message-row.user .msg-content { flex: 0 1 auto; max-width: 80%; }

.bubble {
  padding: 11px 15px; border-radius: 18px;
  font-size: 16px; line-height: 1.75; word-break: break-word;
}
.message-row.bot .bubble {
  flex: 1;
  background: transparent;
  border: none;
  border-radius: 0;
  padding: 0;
  color: var(--text);
}
.message-row.user .bubble {
  background: var(--surface-2);
  color: var(--text);
  border-radius: 18px;
  box-shadow: none;
  margin-left: auto;
  max-width: 80%;
}

/* Markdown */
.bubble a { color: var(--accent-2); text-decoration: none; }
.bubble a:hover { text-decoration: underline; }
.bubble ol, .bubble ul { padding-left: 20px; margin: 7px 0; }
.bubble li { margin: 4px 0; line-height: 1.65; }
.bubble strong { color: inherit; }
.bubble em     { color: var(--text-2); }
.bubble p { margin: 7px 0; }
.bubble p:first-child { margin-top: 0; }
.bubble p:last-child  { margin-bottom: 0; }
.bubble img {
  display: block; max-width: 100%; height: auto; max-height: 420px;
  object-fit: contain; border-radius: 10px; margin: 8px 0;
  border: 1px solid var(--border-md); cursor: zoom-in;
  transition: opacity .15s;
}
.bubble img:hover { opacity: .88; }
.bubble h1,.bubble h2,.bubble h3,.bubble h4 { color: var(--text); font-weight: 600; margin: 14px 0 6px; }
.bubble h1 { font-size: 17px; border-bottom: 1px solid var(--border-md); padding-bottom: 6px; }
.bubble h2 { font-size: 15px; }
.bubble h3 { font-size: 14px; }
.bubble hr { border: none; border-top: 1px solid var(--border-md); margin: 10px 0; }
.bubble code {
  background: var(--surface-3); border-radius: 4px; padding: 1px 6px;
  font-size: 12.5px; font-family: 'Consolas','Menlo',monospace; color: var(--code-inline);
}
.bubble pre {
  background: var(--surface-3); border-radius: 10px;
  padding: 14px 16px; overflow-x: auto; margin: 8px 0;
}
.bubble pre code { background: none; color: var(--code-block); padding: 0; font-size: 12.5px; }
.bubble blockquote {
  border-left: 3px solid var(--accent); margin: 8px 0;
  padding: 4px 12px; color: var(--muted);
  background: var(--surface-2); border-radius: 0 6px 6px 0;
}
.bubble table { border-collapse: collapse; width: 100%; margin: 8px 0; font-size: 13px; }
.bubble th { background: var(--surface-3); color: var(--text); padding: 7px 11px; border: 1px solid var(--border-md); font-weight: 600; }
.bubble td { padding: 6px 11px; border: 1px solid var(--border); color: var(--text-2); }
.bubble tr:nth-child(even) td { background: var(--table-stripe); }

/* Typing */
.typing .bubble { display: flex !important; align-items: center; gap: 5px; padding: 6px 0 !important; border-radius: 0 !important; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: bounce 1.2s ease-in-out infinite; }
.dot:nth-child(2) { animation-delay: .2s; }
.dot:nth-child(3) { animation-delay: .4s; }
@keyframes bounce { 0%,60%,100%{transform:translateY(0)} 30%{transform:translateY(-7px)} }

.status-msg {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; color: var(--muted); font-style: italic;
}
.status-dots { display: flex; align-items: center; gap: 3px; flex-shrink: 0; }
.status-dots span {
  width: 5px; height: 5px; border-radius: 50%; background: var(--accent);
  animation: bounce 1.2s ease-in-out infinite;
}
.status-dots span:nth-child(2) { animation-delay: .2s; }
.status-dots span:nth-child(3) { animation-delay: .4s; }

/* Citations */
.citations { display: flex; flex-direction: column; gap: 3px; }
.citations-label { font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin-bottom: 2px; }
.citation-link {
  display: inline-flex; align-items: center; gap: 5px;
  background: rgba(13,148,136,.08); border: 1px solid rgba(13,148,136,.2);
  border-radius: 6px; padding: 3px 9px; font-size: 11px; color: var(--accent-2);
  text-decoration: none; max-width: fit-content; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap; transition: background .12s;
}
.citation-link:hover { background: rgba(13,148,136,.15); }

/* ── Rating buttons ── */
.rating-row {
  display: flex; align-items: center; gap: 6px; margin-top: 8px;
}
.rating-btn {
  background: transparent; border: 1px solid var(--border-md);
  color: var(--muted); border-radius: 6px; padding: 4px 8px;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: all .15s; line-height: 1;
}
.rating-btn:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.rating-btn:disabled { cursor: default; }
.rating-btn.rating-good { border-color: #22c55e; color: #22c55e; background: rgba(34,197,94,.1); }
.rating-btn.rating-bad  { border-color: var(--red); color: var(--red); background: rgba(248,113,113,.1); }

/* ── Contact info box ── */
.contact-info-box {
  margin-top: 10px; padding-top: 10px;
  border-top: 1px solid var(--border-md);
}
.contact-info-header {
  display: flex; align-items: center; gap: 6px;
  font-size: 14px; font-weight: 700; color: var(--accent);
  text-transform: uppercase; letter-spacing: .04em; margin-bottom: 6px;
}
.contact-info-body {
  font-size: 14px; line-height: 1.7; color: var(--text-2);
}
.contact-info-body p { margin: 3px 0; }
.contact-info-body p:first-child { margin-top: 0; }
.contact-info-body p:last-child  { margin-bottom: 0; }
.contact-info-body strong { color: var(--text); }
.contact-info-body a { color: var(--accent-2); text-decoration: none; font-weight: 500; }
.contact-info-body a:hover { text-decoration: underline; }
.contact-info-body ul, .contact-info-body ol { margin: 4px 0; padding-left: 18px; }
.contact-info-body li { margin: 2px 0; }
.contact-info-body hr { border: none; border-top: 1px solid var(--border-md); margin: 8px 0; }

/* ── Input area ── */
#input-area {
  padding: 10px 18px 20px;
  border-top: none; flex-shrink: 0;
}
.input-wrapper {
  max-width: 720px; margin: 0 auto;
  background: var(--surface); border: 1px solid var(--border-md);
  border-radius: 16px; display: flex; align-items: center;
  padding: 10px 12px; gap: 8px;
  box-shadow: 0 2px 12px rgba(0,0,0,.2);
  transition: border-color .15s, box-shadow .15s;
}
.input-wrapper:focus-within {
  border-color: var(--border-md);
  box-shadow: 0 2px 18px rgba(0,0,0,.28);
}
#user-input {
  flex: 1; background: transparent; border: none; outline: none;
  color: var(--text); font-size: 16px; line-height: 1.55;
  resize: none; max-height: 160px; min-height: 22px; font-family: inherit;
}
#user-input::placeholder { color: var(--muted); }
#send-btn {
  width: 34px; height: 34px; background: var(--text);
  border: none; border-radius: 8px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  color: var(--bg); flex-shrink: 0; transition: opacity .15s;
}
#send-btn:disabled { background: var(--surface-3); color: var(--muted); opacity: 1; cursor: not-allowed; }
#send-btn:hover:not(:disabled) { opacity: .82; }
.input-hint {
  text-align: center; font-size: 13px; color: var(--muted);
  margin-top: 6px; max-width: 720px; margin-left: auto; margin-right: auto;
}

/* ── KaTeX math ── */
.bubble .katex { font-size: 1.05em; color: var(--text); }
.bubble .katex-display {
  margin: 12px 0; padding: 12px 16px;
  background: var(--surface-2); border-radius: 8px;
  border: 1px solid var(--border-md);
  overflow-x: auto;
}
.bubble .katex-display > .katex { font-size: 1.1em; }

/* ── Theme toggle button ── */
.ctrl-btns {
  position: absolute; top: 10px; right: 14px; z-index: 10;
  display: flex; gap: 6px;
}
.ctrl-btn {
  background: var(--surface); border: 1px solid var(--border-md);
  color: var(--text); border-radius: 8px; padding: 5px 10px;
  cursor: pointer; font-size: 15px; line-height: 1;
  transition: background .15s, opacity .15s; box-shadow: 0 1px 4px rgba(0,0,0,.15);
}
.ctrl-btn:hover { background: var(--surface-2); }

/* ── Toast ── */
#toast {
  position: fixed; bottom: 28px; left: 50%;
  transform: translateX(-50%) translateY(14px);
  background: var(--surface-2); border: 1px solid var(--accent); color: var(--accent-2);
  padding: 10px 20px; border-radius: 10px; font-size: 13px; font-weight: 600;
  white-space: nowrap; opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s; z-index: 9999;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.error { border-color: var(--red); color: var(--red); }

#no-app-error {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  height: 100vh; gap: 12px; text-align: center; padding: 24px;
}
.no-app-icon  { font-size: 48px; }
.no-app-title { font-size: 20px; font-weight: 700; color: var(--text); margin: 0; }
.no-app-desc  { font-size: 14px; color: var(--muted); line-height: 1.7; margin: 0; }
.no-app-desc code { background: var(--surface-2); padding: 1px 6px; border-radius: 4px; font-size: 13px; }
