/* ============================================================
   chat.css — trợ lý AI nổi. Dùng token OKLCH từ tokens.css nên tự
   theo theme sáng/tối. z-index 900: trên nav (300), dưới preloader (1000).
   ============================================================ */

.chat-bubble {
  position: fixed;
  right: clamp(1rem, 3vw, 1.75rem);
  bottom: clamp(1rem, 3vw, 1.75rem);
  z-index: 900;
  width: 56px;
  height: 56px;
  border-radius: var(--radius-pill);
  border: 1px solid var(--color-rule);
  background: var(--color-paper-2);
  color: var(--color-ink);
  cursor: pointer;
  display: grid;
  place-items: center;
  box-shadow: var(--shadow-card);
  opacity: 0;
  transform: translateY(12px) scale(0.9);
  pointer-events: none;
  transition:
    opacity var(--dur-long) var(--ease-out),
    transform var(--dur-long) var(--ease-out),
    box-shadow var(--dur-short) var(--ease-out),
    background var(--dur-short) var(--ease-out);
}

.chat-bubble--ready {
  opacity: 1;
  transform: none;
  pointer-events: auto;
}

/* chừa chỗ cho bubble — đỡ đè link footer khi cuộn hết trang.
   Class do js/chat.js gắn sau preloader nên bản noscript không bị ảnh hưởng. */
html.chat-on .foot { margin-block-end: calc(56px + clamp(1rem, 3vw, 1.75rem)); }

.chat-bubble:hover { box-shadow: var(--shadow-card-hover); }

.chat-bubble:active { transform: scale(0.94); }

.chat-bubble--hidden { opacity: 0; pointer-events: none; }

.chat-bubble svg {
  width: 24px;
  height: 24px;
  stroke: currentColor;
}

.chat-bubble__dot {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 9px;
  height: 9px;
  border-radius: var(--radius-pill);
  background: var(--color-silver);
  box-shadow: 0 0 0 3px var(--color-paper-2);
}

.chat-bubble:focus-visible {
  outline: 2px solid var(--color-focus);
  outline-offset: 2px;
}

/* ---------- panel ---------- */

.chat {
  position: fixed;
  right: clamp(1rem, 3vw, 1.75rem);
  bottom: clamp(1rem, 3vw, 1.75rem);
  z-index: 900;
  width: min(380px, calc(100vw - 2rem));
  height: min(560px, calc(100dvh - 6rem));
  display: flex;
  flex-direction: column;
  border-radius: var(--radius-lg);
  border: 1px solid var(--color-rule);
  background: var(--color-nav-bg);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  box-shadow: var(--shadow-card-hover);
  overflow: hidden;
  opacity: 0;
  transform: translateY(16px) scale(0.97);
  pointer-events: none;
  visibility: hidden;
  transition:
    opacity var(--dur-short) var(--ease-out),
    transform var(--dur-short) var(--ease-out),
    visibility 0s linear var(--dur-short);
}

.chat--open {
  opacity: 1;
  transform: none;
  pointer-events: auto;
  visibility: visible;
  transition:
    opacity var(--dur-long) var(--ease-out),
    transform var(--dur-long) var(--ease-out),
    visibility 0s;
}

.chat__head {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  padding: var(--space-sm) var(--space-md);
  border-bottom: 1px solid var(--color-rule);
  flex: none;
}

.chat__avatar {
  width: 30px;
  height: 30px;
  border-radius: var(--radius-pill);
  display: grid;
  place-items: center;
  background: var(--color-accent);
  color: var(--color-accent-ink);
  font-family: var(--font-display);
  font-size: var(--text-sm);
  font-weight: 600;
  flex: none;
}

.chat__title-wrap { display: flex; flex-direction: column; min-width: 0; flex: 1; }

.chat__title {
  margin: 0;
  font-family: var(--font-display);
  font-size: var(--text-base);
  font-weight: 600;
  line-height: 1.2;
}

.chat__sub {
  font-size: var(--text-sm);
  color: var(--color-muted);
  line-height: 1.3;
}

.chat__close {
  flex: none;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--color-neutral);
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background var(--dur-micro) var(--ease-out), color var(--dur-micro) var(--ease-out);
}

.chat__close:hover { background: var(--color-paper-3); color: var(--color-ink); }

.chat__close svg { width: 16px; height: 16px; }

/* nút hội thoại mới — cùng hàng nút đóng */
.chat__new {
  flex: none;
  width: 32px;
  height: 32px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--color-neutral);
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background var(--dur-micro) var(--ease-out), color var(--dur-micro) var(--ease-out), border-color var(--dur-micro) var(--ease-out);
}

.chat__new:hover { background: var(--color-paper-3); color: var(--color-ink); }

.chat__new svg { width: 16px; height: 16px; }

/* trạng thái armed: bấm lần 1, chờ bấm lần 2 để xoá */
.chat__new--armed {
  border-color: var(--color-focus);
  color: var(--color-focus);
  background: var(--color-paper-3);
}

/* ---------- messages ---------- */

.chat__msgs {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-md);
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
  overscroll-behavior: contain;
  scrollbar-width: thin;
}

.chat__msg { max-width: 88%; animation: chat-pop var(--dur-short) var(--ease-out); position: relative; }

/* nút copy — hiện khi hover bubble (desktop), mobile luôn mờ */
.chat__copy {
  position: absolute;
  top: var(--space-3xs);
  right: var(--space-3xs);
  width: 26px;
  height: 26px;
  border: 1px solid var(--color-rule);
  border-radius: var(--radius-sm);
  background: var(--color-paper-2);
  color: var(--color-neutral);
  cursor: pointer;
  display: grid;
  place-items: center;
  opacity: 0;
  transition: opacity var(--dur-short) var(--ease-out), color var(--dur-short) var(--ease-out);
}

.chat__copy svg { width: 13px; height: 13px; }

.chat__msg:hover .chat__copy,
.chat__copy:focus-visible { opacity: 1; }

.chat__copy--done { color: var(--color-focus); border-color: var(--color-focus); }

/* câu hỏi gợi ý — row chip, chỉ hội thoại mới */
.chat__suggests {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2xs);
  padding-block: var(--space-2xs) var(--space-xs);
}

.chat__suggest {
  border: 1px solid var(--color-rule);
  border-radius: var(--radius-pill);
  background: var(--color-paper-2);
  color: var(--color-muted);
  font: inherit;
  font-size: var(--text-sm);
  line-height: 1.4;
  padding: var(--space-2xs) var(--space-sm);
  cursor: pointer;
  transition: background var(--dur-micro) var(--ease-out), color var(--dur-micro) var(--ease-out);
}

.chat__suggest:hover { background: var(--color-paper-3); color: var(--color-ink); }

.chat__msg--user { align-self: flex-end; }

.chat__msg--assistant { align-self: flex-start; }

.chat__msg-text {
  margin: 0;
  padding: var(--space-xs) var(--space-sm);
  border-radius: var(--radius-md);
  font-size: var(--text-base);
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: break-word;
}

.chat__msg--user .chat__msg-text {
  background: var(--color-accent);
  color: var(--color-accent-ink);
  border-bottom-right-radius: var(--space-2xs);
}

.chat__msg--assistant .chat__msg-text {
  background: var(--color-paper-3);
  color: var(--color-ink);
  border-bottom-left-radius: var(--space-2xs);
}

/* con trỏ nhấp nháy khi đang stream */
.chat__msg--assistant .chat__msg-text:empty::after {
  content: '▍';
  animation: chat-blink 1s steps(2) infinite;
  color: var(--color-neutral);
}

@keyframes chat-pop {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

@keyframes chat-blink {
  50% { opacity: 0; }
}

/* typing indicator: 3 chấm nhảy trong bubble khi AI chưa trả token đầu */
.chat__typing {
  display: inline-flex;
  gap: var(--space-2xs);
  padding: var(--space-xs) 0;
}
.chat__dot {
  inline-size: 6px;
  block-size: 6px;
  border-radius: var(--radius-pill);
  background: var(--color-neutral);
  animation: chat-dot 1s var(--ease-in-out) infinite;
}
.chat__dot:nth-child(2) { animation-delay: 0.15s; }
.chat__dot:nth-child(3) { animation-delay: 0.3s; }

@keyframes chat-dot {
  30% { translate: 0 -4px; }
  50% { translate: 0 0; }
}

/* ---------- status note ---------- */

.chat__status {
  flex: none;
  margin: 0;
  padding: var(--space-2xs) var(--space-md);
  font-size: var(--text-sm);
  color: var(--color-muted);
  background: var(--color-paper-3);
  text-align: center;
  max-height: 0;
  opacity: 0;
  overflow: hidden;
  transition: max-height var(--dur-short) var(--ease-out), opacity var(--dur-short) var(--ease-out);
}

.chat__status--show { max-height: 4em; opacity: 1; }

/* ---------- input ---------- */

.chat__input-row {
  flex: none;
  display: flex;
  align-items: flex-end;
  gap: var(--space-2xs);
  padding: var(--space-xs) var(--space-sm) var(--space-sm);
  border-top: 1px solid var(--color-rule);
}

.chat__input {
  flex: 1;
  resize: none;
  border: 1px solid var(--color-rule);
  border-radius: var(--radius-md);
  background: var(--color-paper-2);
  color: var(--color-ink);
  font: inherit;
  font-size: var(--text-base);
  line-height: 1.5;
  padding: var(--space-xs) var(--space-sm);
  max-height: 120px;
  transition: border-color var(--dur-micro) var(--ease-out);
}

.chat__input::placeholder { color: var(--color-neutral); }

.chat__input:focus-visible {
  outline: none;
  border-color: var(--color-focus);
}

.chat__send {
  flex: none;
  width: 40px;
  height: 40px;
  border: 1px solid var(--color-rule);
  border-radius: var(--radius-md);
  background: var(--color-accent);
  color: var(--color-accent-ink);
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform var(--dur-micro) var(--ease-out), background var(--dur-short) var(--ease-out);
}

.chat__send:hover { transform: translateY(-1px); }

.chat__send:active { transform: scale(0.94); }

.chat__send svg { width: 18px; height: 18px; }

/* nút đang stream biến thành nút dừng */
.chat__send--stop svg { display: none; }

.chat__send--stop::after {
  content: '';
  width: 12px;
  height: 12px;
  border-radius: 3px;
  background: currentColor;
}

/* ---------- mobile: panel gần full màn ---------- */

@media (max-width: 640px) {
  .chat {
    right: 0.5rem;
    left: 0.5rem;
    bottom: 0.5rem;
    width: auto;
    height: min(560px, calc(100dvh - 4.5rem));
  }
  /* không hover trên cảm ứng — nút copy luôn hiện mờ */
  .chat__copy { opacity: 0.55; }
}

/* ---------- reduced motion ---------- */

@media (prefers-reduced-motion: reduce) {
  .chat-bubble, .chat {
    transition: opacity var(--dur-micro) linear, visibility 0s;
    transform: none;
  }
  .chat__msg { animation: none; }
  .chat__msg--assistant .chat__msg-text:empty::after { animation: none; }
  /* dot tĩnh, giảm dần độ đậm để vẫn đọc được trạng thái đang gõ */
  .chat__dot { animation: none; }
  .chat__dot:nth-child(2) { opacity: 0.6; }
  .chat__dot:nth-child(3) { opacity: 0.4; }
}
