/* =========================================================
   Avado AI Chat Bubble + Modal
   - FAB: giữa cạnh phải (không đè KPI bubble dưới)
   - Modal: giữa màn hình, hỗ trợ light / dark mode
   ========================================================= */

#aai-root {
  position: fixed;
  z-index: 999999;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  pointer-events: none;
}
#aai-root > * {
  pointer-events: auto;
}

/* ----- FAB (bubble tím) — giữa cạnh phải ----- */
.aai-fab,
#aai-fab {
  position: fixed;
  right: 22px;
  top: 50%;
  bottom: auto;
  left: auto;
  transform: translateY(-50%);
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  background: linear-gradient(145deg, #8b5cf6 0%, #6366f1 55%, #4f46e5 100%);
  color: #fff;
  font-size: 24px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow:
    0 4px 14px rgba(99, 102, 241, 0.45),
    0 0 0 3px rgba(255, 255, 255, 0.35);
  transition: transform 0.18s ease, box-shadow 0.18s ease, filter 0.18s ease;
  z-index: 1000000;
  padding: 0;
  overflow: visible;
}
.aai-fab:hover,
#aai-fab:hover {
  transform: translateY(-50%) scale(1.08);
  box-shadow:
    0 8px 24px rgba(99, 102, 241, 0.55),
    0 0 0 3px rgba(255, 255, 255, 0.45);
  filter: brightness(1.05);
}
.aai-fab:active,
#aai-fab:active {
  transform: translateY(-50%) scale(0.96);
}

/* ----- Modal panel — giữa màn hình ----- */
.aai-panel,
#aai-panel {
  position: fixed;
  left: 50%;
  top: 50%;
  right: auto;
  bottom: auto;
  transform: translate(-50%, -50%);
  width: min(920px, calc(100vw - 32px));
  height: min(640px, calc(100vh - 64px));
  z-index: 1000001;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border-radius: 16px;
  /* Light mode defaults */
  background: #ffffff;
  color: #1a1d26;
  border: 1px solid #e5e7eb;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.18), 0 0 0 1px rgba(0, 0, 0, 0.04);
}
.aai-panel[hidden],
#aai-panel[hidden],
#aai-panel.aai-hidden {
  display: none !important;
}

/* Header */
.aai-panel-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  flex-shrink: 0;
  background: linear-gradient(90deg, #f5f3ff 0%, #eef2ff 100%);
  border-bottom: 1px solid #e5e7eb;
  color: #1e1b4b;
}
.aai-panel-head strong {
  flex: 1;
  font-size: 15px;
  font-weight: 700;
  color: #312e81;
}
.aai-panel-head select {
  font-size: 12px;
  border-radius: 8px;
  border: 1px solid #c7d2fe;
  padding: 5px 8px;
  background: #fff;
  color: #1e1b4b;
  max-width: 140px;
}
.aai-panel-head button {
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  color: #4f46e5;
  padding: 4px 8px;
  border-radius: 8px;
}
.aai-panel-head button:hover {
  background: rgba(79, 70, 229, 0.1);
}
.aai-panel-head #aai-close {
  font-size: 20px;
  line-height: 1;
  color: #6b7280;
  padding: 2px 6px;
}
.aai-panel-head #aai-close:hover {
  color: #111827;
  background: rgba(0, 0, 0, 0.06);
}

/* Body layout */
.aai-panel-body {
  display: flex;
  flex: 1;
  min-height: 0;
  background: #fff;
}

/* History sidebar */
.aai-history {
  width: 200px;
  flex-shrink: 0;
  overflow: auto;
  border-right: 1px solid #e5e7eb;
  padding: 8px;
  font-size: 13px;
  background: #fafafa;
  color: #374151;
}
.aai-hist-item {
  padding: 8px 10px;
  border-radius: 10px;
  cursor: pointer;
  margin-bottom: 4px;
  display: flex;
  gap: 6px;
  align-items: center;
  color: #4b5563;
}
.aai-hist-item:hover,
.aai-hist-item.active {
  background: #eef2ff;
  color: #312e81;
}
.aai-hist-item .t {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.aai-hist-item .del {
  border: none;
  background: transparent;
  cursor: pointer;
  color: #9ca3af;
  font-size: 12px;
  padding: 0 2px;
  line-height: 1;
}
.aai-hist-item .del:hover {
  color: #ef4444;
}

/* Chat area */
.aai-chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: #ffffff;
}
.aai-messages {
  flex: 1;
  overflow: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: #f9fafb;
}
.aai-msg {
  max-width: 88%;
  margin-bottom: 0;
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}
.aai-msg.user {
  align-self: flex-end;
  background: #4f46e5;
  color: #ffffff;
  border-bottom-right-radius: 4px;
  margin-left: 24px;
}
.aai-msg.assistant {
  align-self: flex-start;
  background: #ffffff;
  color: #1f2937;
  border: 1px solid #e5e7eb;
  border-bottom-left-radius: 4px;
  margin-right: 24px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}
.aai-msg.system {
  align-self: center;
  background: transparent;
  color: #6b7280;
  font-size: 13px;
  text-align: center;
  border: none;
  padding: 6px 10px;
}

/* Compose */
.aai-compose {
  display: flex;
  gap: 8px;
  padding: 12px;
  border-top: 1px solid #e5e7eb;
  background: #ffffff;
  flex-shrink: 0;
}
.aai-compose textarea {
  flex: 1;
  border: 1px solid #d1d5db;
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 14px;
  resize: none;
  font-family: inherit;
  background: #f9fafb;
  color: #111827;
  line-height: 1.4;
}
.aai-compose textarea::placeholder {
  color: #9ca3af;
}
.aai-compose textarea:focus {
  outline: none;
  border-color: #818cf8;
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
  background: #fff;
}
.aai-compose button {
  background: #4f46e5;
  color: #ffffff;
  border: none;
  border-radius: 12px;
  padding: 0 18px;
  font-weight: 700;
  cursor: pointer;
  font-size: 14px;
  flex-shrink: 0;
}
.aai-compose button:hover {
  background: #4338ca;
}
.aai-compose button:disabled {
  opacity: 0.55;
  cursor: wait;
}

/* =========================================================
   Dark mode (.dark-mode on body/html)
   ========================================================= */
.dark-mode .aai-panel,
.dark-mode #aai-panel {
  background: #1c1f28;
  color: #f0f2f5;
  border-color: #2a2e3a;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.55), 0 0 0 1px rgba(255, 255, 255, 0.06);
}

.dark-mode .aai-panel-head {
  background: linear-gradient(90deg, #2e1065 0%, #1e1b4b 50%, #1e293b 100%);
  border-bottom-color: #3b3f4a;
  color: #e0e7ff;
}
.dark-mode .aai-panel-head strong {
  color: #e0e7ff;
}
.dark-mode .aai-panel-head select {
  background: #1c1f28;
  color: #e0e7ff;
  border-color: #4c1d95;
}
.dark-mode .aai-panel-head button {
  color: #a5b4fc;
}
.dark-mode .aai-panel-head button:hover {
  background: rgba(165, 180, 252, 0.15);
}
.dark-mode .aai-panel-head #aai-close {
  color: #9ca3af;
}
.dark-mode .aai-panel-head #aai-close:hover {
  color: #f3f4f6;
  background: rgba(255, 255, 255, 0.08);
}

.dark-mode .aai-panel-body {
  background: #1c1f28;
}

.dark-mode .aai-history {
  background: #16181f;
  border-right-color: #2a2e3a;
  color: #d1d5db;
}
.dark-mode .aai-hist-item {
  color: #9ca3af;
}
.dark-mode .aai-hist-item:hover,
.dark-mode .aai-hist-item.active {
  background: #2e1065;
  color: #e0e7ff;
}
.dark-mode .aai-hist-item .del {
  color: #6b7280;
}
.dark-mode .aai-hist-item .del:hover {
  color: #f87171;
}

.dark-mode .aai-chat {
  background: #1c1f28;
}
.dark-mode .aai-messages {
  background: #12141a;
}
.dark-mode .aai-msg.user {
  background: #6366f1;
  color: #ffffff;
}
.dark-mode .aai-msg.assistant {
  background: #252830;
  color: #f0f2f5;
  border-color: #2a2e3a;
  box-shadow: none;
}
.dark-mode .aai-msg.system {
  color: #9aa3b2;
}

.dark-mode .aai-compose {
  background: #1c1f28;
  border-top-color: #2a2e3a;
}
.dark-mode .aai-compose textarea {
  background: #12141a;
  color: #f0f2f5;
  border-color: #3b3f4a;
}
.dark-mode .aai-compose textarea::placeholder {
  color: #6b7280;
}
.dark-mode .aai-compose textarea:focus {
  border-color: #818cf8;
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.25);
  background: #16181f;
}
.dark-mode .aai-compose button {
  background: #6366f1;
  color: #ffffff;
}
.dark-mode .aai-compose button:hover {
  background: #4f46e5;
}

/* FAB giữ gradient tím, chỉ chỉnh ring cho dark */
.dark-mode .aai-fab,
.dark-mode #aai-fab {
  box-shadow:
    0 4px 16px rgba(99, 102, 241, 0.5),
    0 0 0 3px rgba(255, 255, 255, 0.12);
}
.dark-mode .aai-fab:hover,
.dark-mode #aai-fab:hover {
  box-shadow:
    0 8px 28px rgba(99, 102, 241, 0.6),
    0 0 0 3px rgba(255, 255, 255, 0.18);
}

/* Mobile */
@media (max-width: 720px) {
  .aai-fab,
  #aai-fab {
    right: 14px;
    width: 52px;
    height: 52px;
    font-size: 22px;
  }
  .aai-panel,
  #aai-panel {
    width: calc(100vw - 16px);
    height: min(78vh, 600px);
    border-radius: 14px;
  }
  .aai-history {
    display: none;
  }
}

/* Legacy selectors (nếu còn HTML cũ) — giữ tương thích tối thiểu */
#aai-bubble {
  position: fixed;
  right: 22px;
  top: 50%;
  transform: translateY(-50%);
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  background: linear-gradient(145deg, #8b5cf6, #4f46e5);
  color: #fff;
  box-shadow: 0 6px 22px rgba(123, 97, 255, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000000;
  padding: 0;
  overflow: hidden;
}
#aai-bubble:hover {
  transform: translateY(-50%) scale(1.06);
}
