/*
 * 在线客服访客 widget 样式（CS-00-11）
 * 契约：docs/04-接口契约/在线客服模块接口契约-v0.1.md §4
 *
 * 约定：
 * 1. 所有类名以 ai-chat- 前缀，避免与主题、Bootstrap 组件冲突。
 * 2. 颜色只取主题/Bootstrap 已有的 CSS 变量（--bs-*），不写死品牌色，
 *    换主题或换站时样式自动跟随；回退值仅用于变量缺失时保底。
 * 3. 不依赖任何框架、外部字体或 CDN；字体默认 inherit，跟随主题。
 * 4. 移动端为底部全宽面板，输入区与面板底部使用 env(safe-area-inset-*)。
 */

.ai-chat {
  --ai-chat-primary: var(--bs-primary, #0d6efd);
  --ai-chat-on-primary: #fff;
  --ai-chat-bg: var(--bs-body-bg, #fff);
  --ai-chat-fg: var(--bs-body-color, #212529);
  --ai-chat-muted: var(--bs-secondary-color, #6c757d);
  --ai-chat-border: var(--bs-border-color, #dee2e6);
  --ai-chat-soft: var(--bs-secondary-bg, #f1f3f5);
  --ai-chat-danger: var(--bs-danger, #dc3545);

  position: fixed;
  right: calc(20px + env(safe-area-inset-right, 0px));
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  z-index: 1080;
  color: var(--ai-chat-fg);
  font-family: inherit;
  font-size: 14px;
  line-height: 1.5;
  text-align: left;
}

/* 根节点被 Blade 的 hidden 属性隐藏时，确保不被任何布局规则覆盖 */
.ai-chat[hidden] {
  display: none !important;
}

.ai-chat *,
.ai-chat *::before,
.ai-chat *::after {
  box-sizing: border-box;
}

.ai-chat [hidden] {
  display: none !important;
}

.ai-chat button {
  font: inherit;
  color: inherit;
}

/* ---------- 启动按钮 ---------- */

.ai-chat-launcher {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 56px;
  height: 56px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--ai-chat-primary);
  color: var(--ai-chat-on-primary);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.18);
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.ai-chat-launcher:hover {
  transform: translateY(-1px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.22);
}

.ai-chat-launcher:focus-visible {
  outline: 3px solid var(--ai-chat-primary);
  outline-offset: 3px;
}

.ai-chat-launcher-icon {
  display: block;
  width: 26px;
  height: 26px;
}

.ai-chat-badge {
  position: absolute;
  top: -2px;
  right: -2px;
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--ai-chat-danger);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  line-height: 20px;
  text-align: center;
}

/* ---------- 面板 ---------- */

.ai-chat-panel {
  position: absolute;
  right: 0;
  bottom: calc(100% + 12px);
  display: flex;
  flex-direction: column;
  width: 360px;
  max-width: calc(100vw - 40px);
  height: min(560px, calc(100vh - 140px));
  height: min(560px, calc(100dvh - 140px));
  overflow: hidden;
  border: 1px solid var(--ai-chat-border);
  border-radius: 16px;
  background: var(--ai-chat-bg);
  color: var(--ai-chat-fg);
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.18);
}

.ai-chat-header {
  display: flex;
  flex: 0 0 auto;
  align-items: flex-start;
  gap: 8px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--ai-chat-border);
  background: var(--ai-chat-bg);
}

.ai-chat-header-text {
  flex: 1 1 auto;
  min-width: 0;
}

.ai-chat-title {
  margin: 0;
  color: var(--ai-chat-fg);
  font-size: 15px;
  font-weight: 600;
  line-height: 1.35;
}

.ai-chat-status {
  margin: 2px 0 0;
  color: var(--ai-chat-muted);
  font-size: 12px;
  line-height: 1.4;
}

.ai-chat-close {
  flex: 0 0 auto;
  width: 32px;
  height: 32px;
  margin: -4px -6px 0 0;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--ai-chat-muted);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
}

.ai-chat-close:hover {
  background: var(--ai-chat-soft);
  color: var(--ai-chat-fg);
}

.ai-chat-close:focus-visible {
  outline: 2px solid var(--ai-chat-primary);
  outline-offset: 1px;
}

.ai-chat-notice {
  flex: 0 0 auto;
  margin: 0;
  padding: 8px 16px;
  border-bottom: 1px solid var(--ai-chat-border);
  background: var(--ai-chat-soft);
  color: var(--ai-chat-muted);
  font-size: 12px;
  line-height: 1.45;
}

/* ---------- 消息区 ---------- */

.ai-chat-messages {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  padding: 14px 16px;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.ai-chat-msg {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  max-width: 100%;
}

.ai-chat-msg--visitor {
  align-items: flex-end;
}

.ai-chat-msg--system {
  align-items: center;
}

.ai-chat-msg-meta {
  display: flex;
  gap: 6px;
  margin-bottom: 3px;
  color: var(--ai-chat-muted);
  font-size: 11px;
  line-height: 1.3;
}

.ai-chat-msg-bubble {
  max-width: 82%;
  padding: 8px 12px;
  border-radius: 12px;
  background: var(--ai-chat-soft);
  color: var(--ai-chat-fg);
  overflow-wrap: anywhere;
  word-break: break-word;
  white-space: pre-wrap;
}

.ai-chat-msg--visitor .ai-chat-msg-bubble {
  border-bottom-right-radius: 4px;
  background: var(--ai-chat-primary);
  color: var(--ai-chat-on-primary);
}

.ai-chat-msg--operator .ai-chat-msg-bubble {
  border-bottom-left-radius: 4px;
}

.ai-chat-msg--system .ai-chat-msg-bubble {
  max-width: 100%;
  padding: 2px 8px;
  background: transparent;
  color: var(--ai-chat-muted);
  font-size: 12px;
  text-align: center;
}

/* 乐观插入中 / 发送失败 */
.ai-chat-msg--pending .ai-chat-msg-bubble {
  opacity: 0.7;
}

.ai-chat-msg--failed .ai-chat-msg-bubble {
  border: 1px solid var(--ai-chat-danger);
  background: transparent;
  color: var(--ai-chat-fg);
}

.ai-chat-msg-status {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
  color: var(--ai-chat-muted);
  font-size: 11px;
  line-height: 1.4;
}

.ai-chat-msg--failed .ai-chat-msg-status,
.ai-chat-msg--error .ai-chat-msg-status {
  color: var(--ai-chat-danger);
}

.ai-chat-msg-retry {
  padding: 1px 10px;
  border: 1px solid currentColor;
  border-radius: 999px;
  background: transparent;
  color: inherit;
  font-size: 11px;
  line-height: 1.5;
  cursor: pointer;
}

.ai-chat-msg-retry:hover {
  opacity: 0.8;
}

.ai-chat-msg-retry:focus-visible {
  outline: 2px solid var(--ai-chat-primary);
  outline-offset: 1px;
}

.ai-chat-msg--error .ai-chat-msg-retry {
  margin-top: 6px;
  color: var(--ai-chat-primary);
}

/* ---------- 输入区 ---------- */

.ai-chat-composer {
  display: flex;
  flex: 0 0 auto;
  align-items: flex-end;
  gap: 8px;
  margin: 0;
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--ai-chat-border);
  background: var(--ai-chat-bg);
}

.ai-chat-input {
  flex: 1 1 auto;
  min-width: 0;
  max-height: 120px;
  padding: 9px 12px;
  border: 1px solid var(--ai-chat-border);
  border-radius: 12px;
  background: var(--ai-chat-bg);
  color: var(--ai-chat-fg);
  font: inherit;
  line-height: 1.4;
  resize: none;
}

.ai-chat-input:focus {
  border-color: var(--ai-chat-primary);
  outline: 2px solid var(--ai-chat-primary);
  outline-offset: -1px;
}

.ai-chat-input:disabled {
  background: var(--ai-chat-soft);
}

.ai-chat-send {
  flex: 0 0 auto;
  padding: 9px 16px;
  border: 0;
  border-radius: 10px;
  background: var(--ai-chat-primary);
  color: var(--ai-chat-on-primary);
  font-weight: 600;
  cursor: pointer;
}

.ai-chat-send:hover:not(:disabled) {
  opacity: 0.92;
}

.ai-chat-send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.ai-chat-send:focus-visible {
  outline: 2px solid var(--ai-chat-primary);
  outline-offset: 2px;
}

.ai-chat-error {
  flex: 0 0 auto;
  margin: 0;
  padding: 0 16px 10px;
  color: var(--ai-chat-danger);
  font-size: 12px;
  line-height: 1.45;
}

.ai-chat-sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
  white-space: nowrap;
}

/* ---------- 移动端 ---------- */

@media (max-width: 575.98px) {
  .ai-chat {
    right: calc(12px + env(safe-area-inset-right, 0px));
    bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  }

  .ai-chat-launcher {
    width: 52px;
    height: 52px;
  }

  .ai-chat-panel {
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    width: auto;
    max-width: none;
    height: min(85vh, 640px);
    height: min(85dvh, 640px);
    max-height: calc(100vh - env(safe-area-inset-top, 0px));
    max-height: calc(100dvh - env(safe-area-inset-top, 0px));
    border-right: 0;
    border-bottom: 0;
    border-left: 0;
    border-radius: 16px 16px 0 0;
  }

  /* 面板占满底部时隐藏启动按钮，避免与关闭按钮叠压 */
  .ai-chat--open .ai-chat-launcher {
    opacity: 0;
    pointer-events: none;
  }

  /* iOS 对 <16px 的输入框会在聚焦时自动放大页面，移动端固定 16px */
  .ai-chat-input {
    font-size: 16px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .ai-chat *,
  .ai-chat *::before,
  .ai-chat *::after {
    animation: none !important;
    transition: none !important;
  }
}
