/* ============================================================
   Ariston Vietnam Chatbot — Main Stylesheet
   ============================================================ */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary:        #E2001A;
  --primary-dark:   #B80016;
  --primary-light:  #FFF0F2;
  --accent:         #FF4444;
  --bg:             #F7F8FA;
  --sidebar-bg:     #FFFFFF;
  --chat-bg:        #F7F8FA;
  --bubble-user:    #E2001A;
  --bubble-ai:      #FFFFFF;
  --text:           #1A1A1A;
  --text-muted:     #64748B;
  --border:         #E2E8F0;
  --danger:         #DC2626;
  --success:        #16A34A;
  --radius:         12px;
  --shadow:         0 2px 12px rgba(0,0,0,.08);
  --sidebar-w:      280px;
}

html, body { height: 100%; font-family: 'Segoe UI', system-ui, sans-serif; background: var(--bg); color: var(--text); }

/* ── Layout ─────────────────────────────────────── */
.app { display: flex; height: 100vh; overflow: hidden; }

/* ── Sidebar ─────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
}

.sidebar-header {
  padding: 20px 16px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--primary);
  color: #fff;
}
.sidebar-header .logo-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 6px;
}
.sidebar-header .logo-icon {
  width: 36px; height: 36px;
  background: rgba(255,255,255,.2);
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-size: 20px;
}
.sidebar-header h1 { font-size: 15px; font-weight: 700; line-height: 1.2; }
.sidebar-header p  { font-size: 11px; opacity: .8; margin-top: 2px; }

.new-chat-btn {
  display: flex; align-items: center; gap: 8px;
  width: calc(100% - 32px); margin: 12px 16px 8px;
  padding: 10px 14px;
  background: var(--primary-light);
  color: var(--primary);
  border: 1.5px dashed var(--primary);
  border-radius: 8px;
  cursor: pointer; font-size: 13px; font-weight: 600;
  transition: background .15s;
}
.new-chat-btn:hover { background: #BBDEFB; }

.sidebar-section { padding: 8px 16px 4px; font-size: 11px; font-weight: 700; color: var(--text-muted); text-transform: uppercase; letter-spacing: .06em; }
.clear-history-btn {
  background: none; border: none; cursor: pointer;
  font-size: 11px; color: var(--text-muted); padding: 2px 6px;
  border-radius: 4px; transition: color .12s, background .12s;
}
.clear-history-btn:hover { color: var(--danger); background: #FEE2E2; }

.category-list { padding: 0 8px; }
.category-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 7px 10px; border-radius: 7px; cursor: pointer;
  font-size: 13px; color: var(--text-muted);
  transition: background .12s, color .12s;
}
.category-item:hover, .category-item.active {
  background: var(--primary-light); color: var(--primary);
}
.category-item .badge {
  background: var(--border); color: var(--text-muted);
  border-radius: 20px; padding: 1px 7px; font-size: 11px; font-weight: 600;
}
.category-item.active .badge { background: var(--primary); color: #fff; }

.session-list { flex: 1; overflow-y: auto; padding: 0 8px 8px; }
.session-item {
  padding: 8px 10px; border-radius: 7px; cursor: pointer;
  font-size: 12.5px; color: var(--text); line-height: 1.4;
  transition: background .12s;
  display: flex; align-items: center; gap: 8px;
}
.session-item:hover { background: var(--bg); }
.session-item.active { background: var(--primary-light); color: var(--primary); }
.session-item .session-icon { font-size: 14px; flex-shrink: 0; }
.session-item .session-title { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; flex: 1; }
.session-del-btn {
  background: none; border: none; cursor: pointer;
  font-size: 12px; padding: 2px 4px; border-radius: 4px;
  color: var(--text-muted); flex-shrink: 0;
  opacity: 0; transition: opacity .12s, color .12s, background .12s;
}
.session-item:hover .session-del-btn { opacity: 1; }
.session-del-btn:hover { color: var(--danger); background: #FEE2E2; }

.sidebar-footer {
  border-top: 1px solid var(--border);
  padding: 12px 16px;
  font-size: 12px; color: var(--text-muted);
  text-align: center;
}
.sidebar-footer a { color: var(--primary); text-decoration: none; }
.hotline { font-size: 16px; font-weight: 700; color: var(--danger); display: block; margin-top: 4px; }

/* ── Main chat area ────────────────────────────────── */
.chat-main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.chat-header {
  background: #fff;
  border-bottom: 1px solid var(--border);
  padding: 14px 24px;
  display: flex; align-items: center; justify-content: space-between;
  flex-shrink: 0;
  box-shadow: 0 1px 4px rgba(0,0,0,.04);
}
.chat-header h2 { font-size: 16px; font-weight: 700; }
.chat-header .subtitle { font-size: 12px; color: var(--text-muted); margin-top: 1px; }
.header-actions { display: flex; gap: 8px; }
.btn-icon {
  background: none; border: 1px solid var(--border); border-radius: 7px;
  padding: 6px 10px; cursor: pointer; font-size: 12px; color: var(--text-muted);
  display: flex; align-items: center; gap: 5px; transition: all .12s;
}
.btn-icon:hover { background: var(--bg); color: var(--text); }

/* ── Messages area ─────────────────────────────────── */
.chat-messages {
  flex: 1; overflow-y: auto; padding: 24px 0;
  scroll-behavior: smooth;
}

.message-wrapper {
  max-width: 820px; margin: 0 auto; padding: 0 24px;
  display: flex; gap: 12px; margin-bottom: 14px;
}
.message-wrapper.user { flex-direction: row-reverse; }

.avatar {
  width: 36px; height: 36px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center; font-size: 18px;
  background: var(--primary-light);
}
.message-wrapper.user .avatar { background: var(--primary); color: #fff; font-size: 14px; font-weight: 700; }

.bubble {
  max-width: 78%;
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 14px; line-height: 1.65;
  box-shadow: var(--shadow);
}
.message-wrapper.assistant .bubble {
  background: var(--bubble-ai);
  border-radius: 2px var(--radius) var(--radius) var(--radius);
  color: var(--text);
}
.message-wrapper.user .bubble {
  background: var(--bubble-user);
  color: #fff;
  border-radius: var(--radius) 2px var(--radius) var(--radius);
}

/* Markdown-like formatting inside bubbles */
.bubble strong { font-weight: 700; }
.bubble ul, .bubble ol { padding-left: 18px; margin: 3px 0; }
.bubble li { margin: 2px 0; line-height: 1.55; }
.bubble p { margin: 3px 0; }
.bubble p:first-child { margin-top: 0; }
.bubble p:last-child  { margin-bottom: 0; }
/* Headings generated from ### ## # in Gemini responses */
.bubble h1.md-h, .bubble h2.md-h, .bubble h3.md-h, .bubble h4.md-h {
  font-weight: 700; margin: 10px 0 3px; line-height: 1.4;
  color: var(--primary);
}
.bubble h2.md-h:first-child,
.bubble h3.md-h:first-child { margin-top: 0; }
.bubble h1.md-h { font-size: 16px; }
.bubble h2.md-h { font-size: 15px; }
.bubble h3.md-h { font-size: 14px; border-bottom: 1px solid var(--border); padding-bottom: 2px; }
.bubble h4.md-h { font-size: 13px; color: var(--text-muted); text-transform: none; }
.message-wrapper.user .bubble h1.md-h,
.message-wrapper.user .bubble h2.md-h,
.message-wrapper.user .bubble h3.md-h,
.message-wrapper.user .bubble h4.md-h { color: rgba(255,255,255,.9); border-color: rgba(255,255,255,.3); }
.bubble code {
  background: rgba(0,0,0,.07); border-radius: 4px;
  padding: 1px 5px; font-family: monospace; font-size: 12.5px;
}
.message-wrapper.user .bubble code { background: rgba(255,255,255,.2); }

/* Sources block */
.sources {
  margin-top: 10px; padding-top: 10px;
  border-top: 1px solid var(--border);
  font-size: 12px; color: var(--text-muted);
}
.sources-label { font-weight: 600; margin-bottom: 5px; }
.source-chip {
  display: inline-flex; align-items: center; gap: 4px;
  background: var(--primary-light); color: var(--primary);
  border-radius: 5px; padding: 3px 8px; margin: 2px 3px 2px 0;
  font-size: 11px; text-decoration: none;
  border: 1px solid #BBDEFB; transition: background .12s;
}
.source-chip:hover { background: #BBDEFB; }
/* Highlight flash when clicking a source-ref in text */
@keyframes chip-flash {
  0%,100% { box-shadow: none; outline: none; }
  30%,70%  { box-shadow: 0 0 0 3px rgba(21,101,192,.45); outline: 2px solid var(--primary); }
}
.source-chip.highlight { animation: chip-flash .7s ease 2; }

/* Inline source reference links "(Theo Nguồn N)" inside chat text */
.source-ref {
  display: inline-flex; align-items: center; gap: 2px;
  font-size: 11px; font-weight: 700;
  color: var(--primary); background: var(--primary-light);
  border: 1px solid #BBDEFB; border-radius: 4px;
  padding: 1px 6px; margin: 0 1px;
  text-decoration: none; cursor: pointer;
  transition: background .12s, box-shadow .12s;
  white-space: nowrap; vertical-align: baseline;
}
.source-ref:hover { background: #BBDEFB; box-shadow: 0 1px 4px rgba(21,101,192,.25); }

/* Error / busy message inside chat bubble */
.error-msg {
  color: var(--text-muted);
  font-style: italic;
}

/* Page thumbnail strip */
.source-thumb-strip {
  display: flex; gap: 8px;
  overflow-x: auto; padding: 8px 0 4px;
  scrollbar-width: thin;
}
.source-thumb-link { flex: 0 0 auto; position: relative; }
.source-thumb {
  display: block;
  height: 140px; width: auto;
  border-radius: 4px;
  border: 1px solid var(--border);
  object-fit: contain;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,0.12);
  cursor: zoom-in;
  transition: transform .15s, box-shadow .15s;
}
.source-thumb:hover { transform: scale(1.04); box-shadow: 0 3px 10px rgba(0,0,0,0.22); }
/* Zoom hint badge on hover */
.source-thumb-link::after {
  content: '🔍';
  position: absolute; bottom: 6px; right: 6px;
  background: rgba(0,0,0,.55); color: #fff;
  font-size: 13px; line-height: 1;
  padding: 3px 5px; border-radius: 4px;
  opacity: 0; pointer-events: none;
  transition: opacity .15s;
}
.source-thumb-link:hover::after { opacity: 1; }

/* Lightbox */
#lightbox {
  display: none; position: fixed; inset: 0; z-index: 9000;
  align-items: center; justify-content: center;
}
#lightbox.open { display: flex; }
.lb-backdrop {
  position: absolute; inset: 0;
  background: rgba(0,0,0,.82);
  cursor: zoom-out;
}
.lb-box {
  position: relative; z-index: 1;
  display: flex; flex-direction: column;
  max-width: 92vw; max-height: 94vh;
  animation: lb-in .18s ease;
}
@keyframes lb-in {
  from { opacity: 0; transform: scale(.95); }
  to   { opacity: 1; transform: scale(1); }
}
.lb-toolbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 0 0 8px;
}
.lb-title {
  color: #fff; font-size: 13px; font-weight: 600;
  opacity: .9; flex: 1; min-width: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.lb-btn {
  display: inline-flex; align-items: center; gap: 5px;
  background: rgba(255,255,255,.15); border: 1px solid rgba(255,255,255,.25);
  color: #fff; border-radius: 6px; padding: 5px 12px;
  font-size: 12px; font-weight: 600; cursor: pointer;
  text-decoration: none; white-space: nowrap;
  transition: background .12s;
}
.lb-btn:hover { background: rgba(255,255,255,.28); color: #fff; }
.lb-counter {
  font-size: 12px; color: rgba(255,255,255,.65);
  white-space: nowrap; min-width: 32px; text-align: center;
}
.lb-img-wrap {
  flex: 1; min-height: 0; position: relative;
  display: flex; align-items: center; justify-content: center;
}
.lb-nav {
  position: absolute; top: 50%; transform: translateY(-50%);
  width: 40px; height: 40px;
  background: rgba(255,255,255,.15); border: 1px solid rgba(255,255,255,.3);
  color: #fff; border-radius: 50%; font-size: 16px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s, opacity .15s;
  z-index: 2;
}
.lb-nav:hover:not(:disabled) { background: rgba(255,255,255,.32); }
.lb-nav:disabled { opacity: .2; cursor: default; pointer-events: none; }
.lb-prev { left: 10px; }
.lb-next { right: 10px; }
#lbImg {
  max-width: calc(100% - 100px); max-height: 85vh;
  object-fit: contain;
  border-radius: 4px;
  box-shadow: 0 8px 40px rgba(0,0,0,.6);
  display: block;
  transition: opacity .15s;
}

/* Typing indicator */
.typing-bubble {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 12px 16px;
  background: var(--bubble-ai);
  border-radius: 2px var(--radius) var(--radius) var(--radius);
  box-shadow: var(--shadow);
  width: fit-content;
  min-width: 190px;
}
.typing-indicator {
  display: flex; align-items: center; gap: 6px;
}
.typing-status {
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 240px;
  animation: fadeStatus .25s ease;
}
@keyframes fadeStatus { from { opacity: 0; transform: translateY(3px); } to { opacity: 1; transform: translateY(0); } }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-muted); animation: bounce .9s infinite; }
.dot:nth-child(2) { animation-delay: .15s; }
.dot:nth-child(3) { animation-delay: .30s; }
@keyframes bounce { 0%,80%,100%{transform:translateY(0)} 40%{transform:translateY(-6px)} }

/* Welcome screen */
.welcome {
  max-width: 620px; margin: 32px auto; padding: 0 24px; text-align: center;
}
.welcome .welcome-icon { font-size: 56px; margin-bottom: 12px; }
.welcome h3 { font-size: 20px; font-weight: 700; color: var(--primary); margin-bottom: 8px; }
.welcome p  { font-size: 14px; color: var(--text-muted); line-height: 1.6; margin-bottom: 20px; }
.suggestions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.suggestion {
  background: #fff; border: 1.5px solid var(--border);
  border-radius: 20px; padding: 7px 14px;
  font-size: 13px; cursor: pointer; color: var(--text);
  transition: all .12s;
}
.suggestion:hover { border-color: var(--primary); color: var(--primary); background: var(--primary-light); }

/* ── Input area ─────────────────────────────────────── */
.chat-input-area {
  background: #fff;
  border-top: 1px solid var(--border);
  padding: 16px 24px;
  flex-shrink: 0;
}
.input-container {
  max-width: 820px; margin: 0 auto;
  display: flex; gap: 10px; align-items: flex-end;
}
.input-wrapper { flex: 1; position: relative; }
#messageInput {
  width: 100%; resize: none; overflow-y: auto;
  padding: 12px 46px 12px 16px; max-height: 150px; min-height: 48px;
  border: 1.5px solid var(--border); border-radius: 10px;
  font-size: 14px; font-family: inherit; line-height: 1.5;
  transition: border-color .15s;
  background: #fff;
}
#messageInput:focus { outline: none; border-color: var(--primary); }
#messageInput::placeholder { color: var(--text-muted); }
#sendBtn {
  position: absolute; right: 8px; bottom: 9px;
  width: 32px; height: 32px; border-radius: 7px;
  background: var(--primary); color: #fff;
  border: none; cursor: pointer; display: flex; align-items: center; justify-content: center;
  font-size: 15px; transition: background .12s;
}
#sendBtn:hover { background: var(--primary-dark); }
#sendBtn:disabled { background: var(--border); cursor: not-allowed; }
.input-hint { font-size: 11px; color: var(--text-muted); margin-top: 6px; text-align: center; }

/* ── Scrollbar ──────────────────────────────────────── */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #CBD5E1; }

/* ── Document Browser Modal ─────────────────────────── */
.doc-drawer {
  position: fixed; inset: 0; z-index: 500;
  display: flex; align-items: center; justify-content: center;
}
.doc-drawer-backdrop {
  position: absolute; inset: 0;
  background: rgba(0,0,0,.45);
  animation: fadein .18s ease;
}
@keyframes fadein { from { opacity: 0; } to { opacity: 1; } }
.doc-drawer-panel {
  position: relative; z-index: 1;
  width: 560px; max-width: calc(100vw - 32px);
  max-height: 80vh;
  background: #fff; border-radius: 14px;
  box-shadow: 0 8px 40px rgba(0,0,0,.18);
  display: flex; flex-direction: column;
  animation: slideup .2s ease;
}
@keyframes slideup { from { opacity: 0; transform: translateY(16px); } to { opacity: 1; transform: translateY(0); } }
.doc-drawer-header {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 16px 20px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.doc-drawer-title {
  display: flex; align-items: center; gap: 8px;
  font-size: 15px; font-weight: 700; color: var(--primary);
}
.doc-drawer-close {
  background: none; border: none; cursor: pointer;
  font-size: 16px; color: var(--text-muted); padding: 4px 8px;
  border-radius: 6px; transition: background .12s;
}
.doc-drawer-close:hover { background: var(--bg); color: var(--text); }
.doc-drawer-body {
  flex: 1; overflow-y: auto; padding: 12px 20px;
}
.doc-drawer-loading {
  text-align: center; color: var(--text-muted);
  padding: 32px 0; font-size: 14px;
}
.doc-item {
  display: flex; align-items: flex-start; gap: 12px;
  padding: 10px 12px; border-radius: 8px;
  border: 1px solid var(--border); margin-bottom: 8px;
  transition: background .12s;
}
.doc-item:last-child { margin-bottom: 0; }
.doc-item:hover { background: var(--bg); }
.doc-item-icon { font-size: 22px; flex-shrink: 0; margin-top: 1px; }
.doc-item-body { flex: 1; min-width: 0; }
.doc-item-title {
  font-size: 13.5px; font-weight: 600; color: var(--text);
  line-height: 1.4; margin-bottom: 6px;
  display: -webkit-box; -webkit-line-clamp: 2;
  -webkit-box-orient: vertical; overflow: hidden;
}
.doc-item-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.doc-action-btn {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 11.5px; font-weight: 600; padding: 3px 10px;
  border-radius: 20px; cursor: pointer; text-decoration: none;
  border: 1.5px solid; transition: all .12s;
}
.doc-action-btn.download {
  color: var(--primary); border-color: var(--primary);
  background: var(--primary-light);
}
.doc-action-btn.download:hover { background: #BBDEFB; }
.doc-action-btn.source {
  color: var(--text-muted); border-color: var(--border);
  background: #fff;
}
.doc-action-btn.source:hover { border-color: var(--text-muted); color: var(--text); }
.doc-drawer-footer {
  padding: 12px 20px;
  border-top: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  flex-shrink: 0;
}
.doc-drawer-info { font-size: 12px; color: var(--text-muted); }
.doc-drawer-pages { display: flex; gap: 4px; }
.doc-drawer-pages .page-btn {
  min-width: 28px; height: 28px; padding: 0 6px;
  border: 1px solid var(--border); border-radius: 6px;
  background: #fff; font-size: 12px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  transition: all .12s;
}
.doc-drawer-pages .page-btn:hover:not(:disabled) { border-color: var(--primary); color: var(--primary); }
.doc-drawer-pages .page-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); font-weight: 700; cursor: default; }
.doc-drawer-pages .page-btn.pg-ellipsis { border: none; background: none; cursor: default; }

/* ── Responsive ─────────────────────────────────────── */
@media (max-width: 640px) {
  .sidebar { display: none; }
  .chat-header { padding: 12px 16px; }
  .message-wrapper { padding: 0 12px; }
  .chat-input-area { padding: 12px; }
  .bubble { max-width: 90%; }
}

/* ── Admin badge ─────────────────────────────────────── */
.admin-link {
  display: block; text-align: center; padding: 8px;
  font-size: 12px; color: var(--text-muted); text-decoration: none;
}
.admin-link:hover { color: var(--primary); }
