/* ═══════════════════════════════════════════════════════
   AI Chat Widget
   ═══════════════════════════════════════════════════════ */

/* ── Floating action button ── */
.aic-fab {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  z-index: 900;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: none;
  background: var(--color-link);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.18);
  transition: transform 0.2s var(--ease-out-expo), box-shadow 0.2s ease, opacity 0.2s ease;
}
.aic-fab:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.24);
}
.aic-fab:active { transform: scale(0.96); }
.aic-fab[aria-expanded="true"] { opacity: 0; pointer-events: none; }

/* ── Panel ── */
.aic-panel {
  position: fixed;
  bottom: 0;
  right: 0;
  z-index: 901;
  width: 380px;
  max-width: 100vw;
  height: 520px;
  max-height: calc(100dvh - 2rem);
  display: flex;
  flex-direction: column;
  background: var(--color-bg);
  border-radius: 14px 14px 0 0;
  box-shadow: 0 -4px 32px rgba(0, 0, 0, 0.14);
  transform: translateY(calc(100% + 4px));
  transition: transform 0.3s var(--ease-out-expo);
  overflow: hidden;
}
.aic-panel.is-open { transform: translateY(0); }
.aic-panel[aria-hidden="true"] { pointer-events: none; }

@media (min-width: 600px) {
  .aic-panel {
    bottom: 1.5rem;
    right: 1.5rem;
    border-radius: 14px;
    border: none;
    transform: translateX(calc(100% + 1.5rem + 16px));
    height: 560px;
  }
  .aic-panel.is-open { transform: translateX(0); }
}

/* ── Panel header ── */
.aic-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
}
.aic-head-title {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--color-text);
  opacity: 0.85;
}
.aic-close {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--color-text-muted);
  padding: 0.2rem;
  border-radius: 4px;
  display: flex;
  align-items: center;
  transition: color 0.15s, background 0.15s;
}
.aic-close:hover { color: var(--color-text); background: var(--color-surface); }

/* ── Action bar ── */
.aic-action-bar {
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
  display: flex;
  gap: 0.5rem;
}
.aic-action-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.3rem 0.75rem;
  border: 1px solid var(--color-border);
  border-radius: 20px;
  background: var(--color-surface);
  color: var(--color-text);
  font-size: 0.8rem;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
  line-height: 1.5;
}
.aic-action-btn:hover {
  background: var(--color-link);
  border-color: var(--color-link);
  color: #fff;
}
.aic-action-btn:disabled { opacity: 0.45; cursor: not-allowed; }

/* ── Messages ── */
.aic-messages {
  flex: 1;
  overflow-y: auto;
  padding: 0.75rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  scroll-behavior: smooth;
}
.aic-messages:empty::before {
  content: 'สวัสดี! ถามอะไรเกี่ยวกับบทความนี้ได้เลย หรือกด "สรุปเนื้อหา" เพื่อให้ AI สรุปให้';
  color: var(--color-text-muted);
  font-size: 0.83rem;
  line-height: 1.6;
  display: block;
  margin-top: 0.5rem;
}

/* empty message for non-post pages */
.aic-messages.no-post:empty::before {
  content: 'สวัสดี! ถามอะไรก็ได้เลย';
}

.aic-msg {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  max-width: 88%;
}
.aic-msg--user { align-self: flex-end; }
.aic-msg--ai { align-self: flex-start; }

.aic-bubble {
  padding: 0.5rem 0.8rem;
  border-radius: 12px;
  font-size: 0.84rem;
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
}
.aic-msg--user .aic-bubble {
  background: var(--color-link);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.aic-msg--ai .aic-bubble {
  background: var(--color-surface);
  color: var(--color-text);
  border-bottom-left-radius: 4px;
  border: 1px solid var(--color-border);
}

/* Cached badge */
.aic-cached {
  font-size: 0.7rem;
  color: var(--color-text-muted);
  padding-left: 0.2rem;
}

/* Loading dots */
.aic-msg--loading .aic-bubble {
  display: flex;
  gap: 5px;
  align-items: center;
  padding: 0.6rem 1rem;
}
.aic-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--color-text-muted);
  animation: aic-bounce 1.2s infinite ease-in-out;
}
.aic-dot:nth-child(2) { animation-delay: 0.2s; }
.aic-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes aic-bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
  40%            { transform: translateY(-6px); opacity: 1; }
}

/* ── Input form ── */
.aic-form {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  padding: 0.65rem 1rem;
  border-top: 1px solid var(--color-border);
  flex-shrink: 0;
}
.aic-textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  padding: 0.45rem 0.7rem;
  font-size: 0.84rem;
  font-family: inherit;
  background: var(--color-surface);
  color: var(--color-text);
  line-height: 1.5;
  max-height: 120px;
  overflow-y: auto;
  transition: border-color 0.15s;
}
.aic-textarea:focus {
  outline: none;
  border-color: var(--color-link);
}
.aic-send {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: none;
  background: var(--color-link);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: opacity 0.15s;
}
.aic-send:hover { opacity: 0.85; }
.aic-send:disabled { opacity: 0.4; cursor: not-allowed; }

/* ── Selection tooltip ── */
.aic-sel-tip {
  position: fixed;
  z-index: 950;
  transform: translateX(-50%) translateY(-100%);
  margin-top: -8px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s ease;
}
.aic-sel-tip.is-visible {
  pointer-events: auto;
  opacity: 1;
}
.aic-sel-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.3rem 0.65rem;
  background: var(--color-text);
  color: var(--color-bg);
  border: none;
  border-radius: 6px;
  font-size: 0.78rem;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
  box-shadow: 0 2px 8px rgba(0,0,0,0.2);
  transition: opacity 0.15s;
}
.aic-sel-btn:hover { opacity: 0.85; }
.aic-sel-btn::after {
  content: '';
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  border: 5px solid transparent;
  border-top-color: var(--color-text);
}

/* ── Scrollbar styling ── */
.aic-messages::-webkit-scrollbar { width: 4px; }
.aic-messages::-webkit-scrollbar-track { background: transparent; }
.aic-messages::-webkit-scrollbar-thumb { background: var(--color-border); border-radius: 2px; }
