/* ==========================================================================
   Chat widget · 在线客服浮窗
   Bilingual AI customer-service widget — floating bottom-right on every page.
   中英双语 AI 在线客服浮窗 — 固定于每个页面右下角。

   Brand tokens mirror static/css/style.css (navy / gold / warm).
   颜色变量与 static/css/style.css 的品牌色保持一致。
   ========================================================================== */

.cw {
  --cw-navy: #1b2a4a;
  --cw-navy-deep: #142036;
  --cw-gold: #c9a961;
  --cw-gold-dark: #a9873f;
  --cw-warm: #f8f7f3;
  --cw-ink: #242b36;
  --cw-muted: #687080;
  --cw-border: #e7e8ec;
  --cw-error: #b3261e;
  position: fixed;
  right: calc(20px + env(safe-area-inset-right, 0px));
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  z-index: 9990; /* above page content, below nothing else on this site */
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 12px;
  max-width: calc(100vw - 32px); /* never causes horizontal scrolling */
  font-family: "Noto Sans SC", "PingFang SC", "Microsoft YaHei", system-ui,
    -apple-system, "Segoe UI", sans-serif;
  line-height: 1.6;
}

/* ---- Launcher button · 启动按钮 ---------------------------------------- */

.cw-launcher {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 18px 10px 10px;
  border-radius: 999px;
  background: linear-gradient(135deg, #d8b96f, var(--cw-gold-dark));
  color: var(--cw-navy-deep);
  font-size: 0.9rem;
  font-weight: 700;
  text-decoration: none;
  box-shadow: 0 6px 18px rgba(20, 32, 54, 0.28), 0 2px 6px rgba(20, 32, 54, 0.18);
  transition: transform 0.18s ease, box-shadow 0.18s ease;
}

.cw-launcher:hover,
.cw-launcher:focus-visible {
  transform: translateY(-2px);
  box-shadow: 0 10px 24px rgba(20, 32, 54, 0.32), 0 3px 8px rgba(20, 32, 54, 0.2);
  color: var(--cw-navy-deep);
  text-decoration: none;
}

.cw-launcher:focus-visible {
  outline: 3px solid var(--cw-navy);
  outline-offset: 2px;
}

.cw-launcher-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--cw-navy);
  color: var(--cw-gold);
}

.cw-launcher-label {
  white-space: nowrap;
}

/* ---- Panel · 面板 ------------------------------------------------------- */

.cw-panel {
  display: flex;
  flex-direction: column;
  width: 380px;
  max-width: calc(100vw - 32px);
  height: min(600px, calc(100vh - 130px));
  min-height: 380px;
  max-height: calc(100vh - 130px);
  background: #fff;
  border: 1px solid var(--cw-border);
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 18px 48px rgba(20, 32, 54, 0.3), 0 4px 14px rgba(20, 32, 54, 0.16);
}

.cw-panel[hidden] {
  display: none;
}

/* Any widget element carrying the hidden attribute must not render — author
   styles such as display:flex would otherwise override the browser's default
   [hidden] handling and keep sections visible (e.g. the quick-topic area
   after a recommendation is selected).
   浮窗内所有带 hidden 属性的元素一律不渲染——否则 display:flex 等作者样式
   会覆盖浏览器对 [hidden] 的默认处理，导致区块仍然可见（例如选择推荐后
   快捷问题区仍显示）。 */
.cw [hidden] {
  display: none !important;
}

.cw-panel.cw-minimized {
  display: none;
}

/* ---- Header · 页头 ------------------------------------------------------ */

.cw-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  background: var(--cw-navy);
  color: #fff;
}

.cw-logo {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  background: #fff;
  flex: none;
}

.cw-heading {
  flex: 1 1 auto;
  min-width: 0;
}

.cw-title {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.cw-status {
  margin: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.78rem;
  color: #d7dce6;
}

.cw-status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--cw-gold);
  flex: none;
}

.cw-head-actions {
  display: flex;
  align-items: center;
  gap: 2px;
  flex: none;
}

.cw-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: #cfd6e2;
  cursor: pointer;
  font-size: 0.85rem;
}

.cw-icon-btn:hover {
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
}

.cw-icon-btn:focus-visible {
  outline: 2px solid var(--cw-gold);
  outline-offset: -2px;
}

.cw-lang-btn {
  width: auto;
  padding: 0 8px;
  font-weight: 700;
  color: var(--cw-gold);
}

/* ---- Views · 视图切换 --------------------------------------------------- */

.cw-view {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
}

.cw-view[hidden] {
  display: none;
}

/* ---- Messages · 消息区 --------------------------------------------------- */

.cw-msgs {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 14px 14px 6px;
  background: var(--cw-warm);
  display: flex;
  flex-direction: column;
  gap: 10px;
  overscroll-behavior: contain;
}

.cw-msg {
  max-width: 86%;
  padding: 9px 12px;
  border-radius: 12px;
  font-size: 0.875rem;
  color: var(--cw-ink);
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: break-word;
}

.cw-msg--user {
  align-self: flex-end;
  background: var(--cw-navy);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.cw-msg--assistant {
  align-self: flex-start;
  background: #fff;
  border: 1px solid var(--cw-border);
  border-bottom-left-radius: 4px;
}

.cw-msg--error {
  align-self: flex-start;
  background: #fbedeb;
  border: 1px solid #ebc3be;
  color: var(--cw-error);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.cw-msg-time {
  display: block;
  margin-top: 4px;
  font-size: 0.68rem;
  color: var(--cw-muted);
}

.cw-msg--user .cw-msg-time {
  color: #aeb9cc;
  text-align: right;
}

.cw-retry {
  align-self: flex-start;
  border: 0;
  background: none;
  color: var(--cw-navy);
  font-size: 0.78rem;
  font-weight: 700;
  text-decoration: underline;
  cursor: pointer;
  padding: 0;
}

/* ---- Typing indicator · 正在输入 ---------------------------------------- */

.cw-typing {
  align-self: flex-start;
  display: inline-flex;
  gap: 4px;
  padding: 12px 14px;
  background: #fff;
  border: 1px solid var(--cw-border);
  border-radius: 12px;
  border-bottom-left-radius: 4px;
}

.cw-typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--cw-gold-dark);
  animation: cw-blink 1.2s infinite ease-in-out;
}

.cw-typing span:nth-child(2) {
  animation-delay: 0.2s;
}

.cw-typing span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes cw-blink {
  0%, 60%, 100% { opacity: 0.25; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}

/* ---- Quick topic buttons · 快捷问题 -------------------------------------- */

.cw-quick {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px 14px 4px;
  background: var(--cw-warm);
}

.cw-quick-hint {
  flex-basis: 100%;
  margin: 0;
  font-size: 0.78rem;
  color: var(--cw-muted);
}

.cw-quick-btn {
  border: 1px solid var(--cw-gold-dark);
  background: #fff;
  color: var(--cw-navy);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 0.8rem;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.cw-quick-btn:hover,
.cw-quick-btn:focus-visible {
  background: var(--cw-gold);
  border-color: var(--cw-gold);
  color: var(--cw-navy-deep);
}

.cw-quick-btn:focus-visible {
  outline: 2px solid var(--cw-navy);
  outline-offset: 1px;
}

/* Skip-suggestions action: a small, muted text button — deliberately less
   prominent than the recommendation pills and the send button.
   跳过推荐：小巧、低调的文字按钮——刻意弱于推荐按钮与发送按钮。 */
.cw-skip-btn {
  flex-basis: 100%;
  border: 0;
  background: none;
  padding: 2px 0 4px;
  font-size: 0.76rem;
  color: var(--cw-muted);
  text-align: center;
  text-decoration: underline;
  cursor: pointer;
}

.cw-skip-btn:hover,
.cw-skip-btn:focus-visible {
  color: var(--cw-navy);
}

.cw-skip-btn:focus-visible {
  outline: 2px solid var(--cw-gold-dark);
  outline-offset: 2px;
}

/* ---- Input row · 输入行 -------------------------------------------------- */

.cw-input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--cw-border);
  background: #fff;
}

.cw-input {
  flex: 1 1 auto;
  resize: none;
  border: 1px solid var(--cw-border);
  border-radius: 10px;
  padding: 9px 12px;
  font: inherit;
  font-size: 0.875rem;
  color: var(--cw-ink);
  background: #fff;
  max-height: 110px;
  min-height: 40px;
  line-height: 1.5;
}

.cw-input:focus-visible {
  outline: 2px solid var(--cw-gold-dark);
  outline-offset: -1px;
  border-color: var(--cw-gold-dark);
}

.cw-input:disabled {
  background: #f2f3f5;
}

.cw-send {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 10px;
  background: var(--cw-navy);
  color: var(--cw-gold);
  cursor: pointer;
  transition: background 0.15s ease;
}

.cw-send:hover:not(:disabled),
.cw-send:focus-visible {
  background: var(--cw-navy-deep);
}

.cw-send:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.cw-send:focus-visible {
  outline: 2px solid var(--cw-gold-dark);
  outline-offset: 2px;
}

/* ---- Inquiry form · 咨询表单 ---------------------------------------------- */

.cw-form {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 14px;
  background: #fff;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overscroll-behavior: contain;
}

.cw-form-title {
  margin: 0;
  font-size: 1rem;
  font-weight: 700;
  color: var(--cw-navy);
}

.cw-form-desc {
  margin: 0;
  font-size: 0.8rem;
  color: var(--cw-muted);
}

.cw-form-success {
  margin: 0;
  padding: 12px;
  border-radius: 10px;
  background: #eaf3ea;
  border: 1px solid #bfdcc2;
  color: #27572c;
  font-size: 0.875rem;
}

.cw-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.cw-label {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--cw-ink);
}

.cw-control {
  border: 1px solid var(--cw-border);
  border-radius: 8px;
  padding: 8px 10px;
  font: inherit;
  font-size: 0.875rem;
  color: var(--cw-ink);
  background: #fff;
  width: 100%;
  box-sizing: border-box;
}

.cw-control:focus-visible {
  outline: 2px solid var(--cw-gold-dark);
  outline-offset: -1px;
  border-color: var(--cw-gold-dark);
}

.cw-control[aria-invalid="true"] {
  border-color: var(--cw-error);
}

textarea.cw-control {
  resize: vertical;
  min-height: 84px;
}

.cw-field-error {
  margin: 0;
  font-size: 0.75rem;
  color: var(--cw-error);
}

.cw-consent {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  font-size: 0.8rem;
  color: var(--cw-ink);
}

.cw-consent input {
  margin-top: 3px;
  flex: none;
}

.cw-consent a {
  color: var(--cw-navy);
  text-decoration: underline;
}

.cw-submit {
  border: 0;
  border-radius: 10px;
  background: var(--cw-gold);
  color: var(--cw-navy-deep);
  font: inherit;
  font-size: 0.9rem;
  font-weight: 700;
  padding: 10px 16px;
  cursor: pointer;
  transition: background 0.15s ease;
}

.cw-submit:hover:not(:disabled),
.cw-submit:focus-visible {
  background: var(--cw-gold-dark);
  color: #fff;
}

.cw-submit:disabled {
  opacity: 0.55;
  cursor: wait;
}

.cw-submit:focus-visible {
  outline: 2px solid var(--cw-navy);
  outline-offset: 2px;
}

.cw-hp {
  position: absolute;
  left: -9999px;
  width: 1px;
  height: 1px;
  overflow: hidden;
}

/* ---- Footer · 页脚 ------------------------------------------------------- */

.cw-footer {
  border-top: 1px solid var(--cw-border);
  background: #fff;
  padding: 6px 12px;
}

.cw-footer-note {
  margin: 0;
  font-size: 0.72rem;
  color: var(--cw-muted);
  text-align: center;
}

.cw-footer-note a {
  color: var(--cw-navy);
  text-decoration: underline;
}

.cw-footer-actions {
  display: flex;
  justify-content: center;
  gap: 14px;
}

.cw-link-btn {
  border: 0;
  background: none;
  padding: 4px 0;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--cw-navy);
  cursor: pointer;
  text-decoration: underline;
}

.cw-link-btn:hover {
  color: var(--cw-gold-dark);
}

.cw-link-btn:focus-visible {
  outline: 2px solid var(--cw-gold-dark);
  outline-offset: 2px;
}

/* ---- Reduced motion · 减少动态效果 ---------------------------------------- */

@media (prefers-reduced-motion: reduce) {
  .cw-launcher,
  .cw-quick-btn,
  .cw-send,
  .cw-submit {
    transition: none;
  }
  .cw-typing span {
    animation: none;
    opacity: 0.5;
  }
}

/* ---- Mobile: bottom sheet · 移动端：底部面板 -------------------------------- */

@media (max-width: 480px) {
  .cw {
    right: calc(12px + env(safe-area-inset-right, 0px));
    bottom: calc(12px + env(safe-area-inset-bottom, 0px));
    max-width: calc(100vw - 24px);
  }

  .cw-launcher {
    padding: 9px 14px 9px 9px;
    font-size: 0.82rem;
  }

  .cw-launcher-icon {
    width: 30px;
    height: 30px;
  }

  .cw-panel {
    width: calc(100vw - 24px);
    height: min(72dvh, 560px);
    height: min(72vh, 560px); /* fallback for browsers without dvh */
    max-height: calc(100vh - 90px);
    border-radius: 16px;
  }

  .cw-msg {
    max-width: 92%;
  }

  .cw-quick-btn {
    padding: 8px 12px;
    font-size: 0.82rem;
  }

  .cw-send {
    width: 44px;
    height: 44px;
  }
}
