/* ============================================================
   可言Ai 像素小龙虾客服宠物 - 样式
   ============================================================ */
:root {
  --keyan-pet-size: 56px;
  --keyan-pet-pink: #FF5391;
  --keyan-pet-pink-soft: #FFD7E7;
  --keyan-pet-ink: #2C1A24;
}

#keyan-pet-root,
#keyan-pet-root * {
  box-sizing: border-box;
}

#keyan-pet-root {
  position: fixed;
  right: 18px;
  bottom: calc(var(--keyan-pet-bottom, 18px) + env(safe-area-inset-bottom));
  z-index: 2147483000;
  width: var(--keyan-pet-size);
  height: var(--keyan-pet-size);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC',
    'Microsoft YaHei', 'Noto Sans SC', sans-serif;
  -webkit-tap-highlight-color: transparent;
}

/* 向下滚动后缩小成低调的客服入口 */
#keyan-pet-root.is-compact {
  transform: scale(0.82);
  transform-origin: bottom right;
  transition: transform 0.3s ease;
  opacity: 0.92;
}

@media (prefers-reduced-motion: reduce) {
  #keyan-pet-root.is-compact {
    transition: none;
  }
}

#keyan-pet-root.is-hidden {
  display: none;
}

.keyan-pet-stage {
  position: relative;
  width: 100%;
  height: 100%;
}

.keyan-pet {
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
  padding: 0;
  background: transparent;
  cursor: pointer;
  outline: none;
}

.keyan-pet-sprite {
  width: 100%;
  height: 100%;
  background-repeat: no-repeat;
  background-size: 800% 800%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  transition: transform 0.18s ease;
}

.keyan-pet:hover .keyan-pet-sprite {
  transform: translateY(-2px);
}

/* 精灵图加载失败时的静态回退 */
#keyan-pet-root.no-sprite .keyan-pet-sprite {
  background-image: none !important;
}

#keyan-pet-root.no-sprite .keyan-pet-sprite::before {
  content: '🦞';
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  font-size: calc(var(--keyan-pet-size) * 0.62);
  line-height: 1;
}

/* ---- 对话气泡 ---- */
.keyan-pet-bubble {
  position: absolute;
  right: 0;
  bottom: calc(100% + 12px);
  z-index: 2;
  width: min(280px, calc(100vw - 32px));
  max-height: 150px;
  overflow: auto;
  padding: 10px 13px;
  background: #fff;
  border: 2px solid var(--keyan-pet-pink-soft);
  border-radius: 16px 16px 16px 4px;
  color: var(--keyan-pet-ink);
  font-size: 14px;
  line-height: 1.55;
  box-shadow: 0 8px 24px rgba(255, 83, 145, 0.10);
  cursor: pointer;
  animation: keyan-bubble-in 0.25s ease-out;
}

.keyan-pet-bubble::after {
  content: '';
  position: absolute;
  bottom: -8px;
  right: 24px;
  border: 8px solid transparent;
  border-top-color: var(--keyan-pet-pink-soft);
  border-bottom: 0;
}

#keyan-pet-root.panel-left .keyan-pet-bubble {
  right: auto;
  left: 0;
}

#keyan-pet-root.panel-left .keyan-pet-bubble::after {
  right: auto;
  left: 24px;
}

.keyan-pet-bubble[hidden],
.keyan-pet-chat[hidden] {
  display: none !important;
}

/* ---- 聊天面板 ---- */
.keyan-pet-chat {
  position: absolute;
  right: 0;
  bottom: calc(100% + 14px);
  z-index: 3;
  width: 340px;
  max-width: calc(100vw - 24px);
  height: min(480px, calc(100vh - var(--keyan-pet-size) - 70px));
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: #fff;
  border: 2px solid #FFE1EE;
  border-radius: 18px;
  box-shadow: 0 14px 40px rgba(255, 83, 145, 0.12);
  animation: keyan-bubble-in 0.22s ease-out;
}

#keyan-pet-root.panel-left .keyan-pet-chat {
  right: auto;
  left: 0;
}

.keyan-pet-chat-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: linear-gradient(135deg, var(--keyan-pet-pink), #FF9CDA);
  color: #fff;
}

.keyan-pet-chat-title {
  flex: 1;
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.keyan-pet-chat-btn {
  width: 28px;
  height: 28px;
  border: 0;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.28);
  color: #fff;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
}

.keyan-pet-chat-btn:hover {
  background: rgba(255, 255, 255, 0.45);
}

.keyan-pet-chat-msgs {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 9px;
  background: #FDF3F8;
  scroll-behavior: smooth;
}

.keyan-pet-msg {
  display: flex;
}

.keyan-pet-msg-user {
  justify-content: flex-end;
}

.keyan-pet-msg-bubble {
  max-width: 82%;
  padding: 8px 11px;
  border-radius: 14px;
  font-size: 14px;
  line-height: 1.55;
  word-break: break-word;
  white-space: normal;
}

.keyan-pet-msg-user .keyan-pet-msg-bubble {
  background: var(--keyan-pet-pink);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.keyan-pet-msg-bot .keyan-pet-msg-bubble {
  background: #fff;
  color: var(--keyan-pet-ink);
  border: 1px solid var(--keyan-pet-pink-soft);
  border-bottom-left-radius: 4px;
}

.keyan-pet-hint {
  text-align: center;
  font-size: 12px;
  line-height: 1.5;
  color: #a67a92;
  padding: 2px 10px 8px;
}

.keyan-pet-typing {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 11px 14px;
  background: #fff;
  border: 1px solid var(--keyan-pet-pink-soft);
  border-radius: 14px;
  border-bottom-left-radius: 4px;
}

.keyan-pet-typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #FF9CDA;
  animation: keyan-dot 1s infinite ease-in-out;
}

.keyan-pet-typing span:nth-child(2) {
  animation-delay: 0.15s;
}

.keyan-pet-typing span:nth-child(3) {
  animation-delay: 0.3s;
}

.keyan-pet-chat-foot {
  display: flex;
  gap: 8px;
  padding: 10px;
  background: #fff;
  border-top: 1px solid #FFE1EE;
}

.keyan-pet-chat-input {
  flex: 1;
  min-height: 38px;
  max-height: 120px;
  resize: none;
  border: 1.5px solid var(--keyan-pet-pink-soft);
  border-radius: 12px;
  padding: 8px 10px;
  font-size: 14px;
  font-family: inherit;
  line-height: 1.45;
  color: var(--keyan-pet-ink);
  outline: none;
}

.keyan-pet-chat-input:focus {
  border-color: var(--keyan-pet-pink);
}

.keyan-pet-send {
  border: 0;
  border-radius: 12px;
  padding: 0 16px;
  background: var(--keyan-pet-pink);
  color: #fff;
  font-size: 14px;
  cursor: pointer;
}

.keyan-pet-send:disabled {
  opacity: 0.55;
  cursor: default;
}

/* ---- 隐藏后的呼出按钮 ---- */
.keyan-pet-reopen {
  position: fixed;
  right: 18px;
  bottom: calc(var(--keyan-pet-bottom, 18px) + env(safe-area-inset-bottom));
  z-index: 2147483000;
  width: 48px;
  height: 48px;
  border: 2px solid var(--keyan-pet-pink-soft);
  border-radius: 50%;
  background: #fff;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 8px 22px rgba(255, 83, 145, 0.12);
  display: flex;
  align-items: center;
  justify-content: center;
}

.keyan-pet-reopen[hidden] {
  display: none !important;
}

/* ---- 动画 ---- */
@keyframes keyan-bubble-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes keyan-dot {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.5;
  }
  30% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

/* ---- 移动端 ---- */
@media (max-width: 768px) {
  #keyan-pet-root {
    --keyan-pet-size: 48px;
    right: 16px;
    bottom: calc(var(--keyan-pet-bottom, 16px) + env(safe-area-inset-bottom));
  }

  .keyan-pet-bubble {
    width: min(250px, calc(100vw - 24px));
    bottom: calc(100% + 10px);
  }

  .keyan-pet-chat {
    position: fixed;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    width: auto;
    max-width: none;
    height: auto;
    border: 0;
    border-radius: 0;
  }

  .keyan-pet-chat-head {
    padding-top: calc(10px + env(safe-area-inset-top));
  }

  .keyan-pet-chat-foot {
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
  }

  .keyan-pet-chat-input {
    font-size: 16px; /* 防止 iOS 聚焦自动缩放 */
  }

  .keyan-pet-chat-btn {
    width: 32px;
    height: 32px;
  }

  body.keyan-pet-body-lock {
    overflow: hidden;
  }

  .keyan-pet-reopen {
    right: 16px;
    bottom: calc(var(--keyan-pet-bottom, 16px) + env(safe-area-inset-bottom));
  }
}

/* ---- 减少动态效果 ---- */
@media (prefers-reduced-motion: reduce) {
  .keyan-pet-bubble,
  .keyan-pet-chat,
  .keyan-pet-typing span,
  .keyan-pet-sprite {
    animation: none !important;
    transition: none !important;
  }
}

/* ============================================================
   全局样式隔离：防止站点通用 CSS（如 mobile.css 的按钮/输入框
   强制规则）干扰宠物组件的输入框与按钮布局，保证全站样式一致
   ============================================================ */
#keyan-pet-root .keyan-pet-chat-input {
  width: auto !important;
  min-width: 0 !important;
  max-width: none !important;
  flex: 1 1 auto !important;
  min-height: 38px !important;
  max-height: 120px !important;
  padding: 8px 10px !important;
  font-size: 14px !important;
  line-height: 1.45 !important;
  border: 1.5px solid var(--keyan-pet-pink-soft) !important;
  border-radius: 12px !important;
  background: #fff !important;
  color: var(--keyan-pet-ink) !important;
  resize: none !important;
  outline: none !important;
  box-shadow: none !important;
  box-sizing: border-box !important;
}

#keyan-pet-root .keyan-pet-send {
  width: auto !important;
  min-width: 0 !important;
  min-height: 38px !important;
  height: 38px !important;
  padding: 0 16px !important;
  font-size: 14px !important;
  line-height: 1 !important;
  border: 0 !important;
  border-radius: 12px !important;
  background: var(--keyan-pet-pink) !important;
  color: #fff !important;
  flex: 0 0 auto !important;
  box-shadow: none !important;
}

#keyan-pet-root .keyan-pet-chat-btn {
  width: 28px !important;
  height: 28px !important;
  min-width: 28px !important;
  min-height: 28px !important;
  padding: 0 !important;
  font-size: 16px !important;
  line-height: 1 !important;
  border: 0 !important;
  border-radius: 8px !important;
}

#keyan-pet-root .keyan-pet-chat-head {
  flex-wrap: nowrap !important;
  gap: 8px !important;
}

#keyan-pet-root .keyan-pet-chat-title {
  min-width: 0 !important;
  flex: 1 1 auto !important;
}

#keyan-pet-root .keyan-pet-chat-foot {
  display: flex !important;
  flex-wrap: nowrap !important;
  align-items: center !important;
  gap: 8px !important;
}

@media (max-width: 860px) {
  #keyan-pet-root .keyan-pet-chat-btn {
    width: 32px !important;
    height: 32px !important;
    min-width: 32px !important;
    min-height: 32px !important;
  }
}

@media (max-width: 640px) {
  #keyan-pet-root .keyan-pet-chat-input {
    font-size: 16px !important;
  }
}

/* ============================================================
   客服按钮模式：不显示龙虾精灵，改用圆形客服按钮
   ============================================================ */
#keyan-pet-root .keyan-pet-sprite {
  background: #fff !important;
  background-image: none !important;
  border-radius: 50%;
  border: 3px solid #fff;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow:
    0 6px 18px rgba(232, 71, 127, 0.14),
    inset 0 1px 0 rgba(255, 255, 255, 0.35);
}

.keyan-pet-btn-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

#keyan-pet-root .keyan-pet:hover .keyan-pet-sprite {
  transform: translateY(-2px) scale(1.04);
  box-shadow:
    0 8px 22px rgba(232, 71, 127, 0.20),
    inset 0 1px 0 rgba(255, 255, 255, 0.4);
}

.keyan-pet-reopen {
  border-color: #fff;
  background-size: cover;
  background-position: center;
  box-shadow: 0 6px 18px rgba(232, 71, 127, 0.14);
  color: transparent;
}

/* ============================================================
   移动端全局按钮样式隔离：保证客服按钮始终保持正圆
   ============================================================ */
#keyan-pet-root .keyan-pet {
  width: 100% !important;
  height: 100% !important;
  min-height: 0 !important;
  min-width: 0 !important;
  padding: 0 !important;
  border: 0 !important;
  border-radius: 50% !important;
  box-sizing: border-box !important;
}

#keyan-pet-root .keyan-pet-sprite {
  width: 100% !important;
  height: 100% !important;
  min-width: 0 !important;
  min-height: 0 !important;
  padding: 0 !important;
  border-radius: 50% !important;
  box-sizing: border-box !important;
}

.keyan-pet-reopen {
  box-sizing: border-box !important;
  width: 52px !important;
  height: 52px !important;
  min-width: 0 !important;
  min-height: 0 !important;
  padding: 0 !important;
  border-radius: 50% !important;
}

/* ============================================================
   站点全局 button 规则防御
   防止页面 CSS（如移动端 button{width:100%}）把宠物按钮、
   呼出按钮拉成满宽椭圆，或破坏聊天按钮尺寸
   ============================================================ */
#keyan-pet-root button.keyan-pet {
  width: 100% !important;
  height: 100% !important;
  flex: 0 0 auto !important;
  max-width: none !important;
}

button.keyan-pet-reopen {
  width: 52px !important;
  height: 52px !important;
  min-width: 52px !important;
  min-height: 52px !important;
  max-width: 52px !important;
  border-radius: 50% !important;
  justify-content: center !important;
  align-items: center !important;
  text-align: center !important;
}

#keyan-pet-root .keyan-pet-sprite,
#keyan-pet-root .keyan-pet-btn-img {
  width: 100% !important;
  height: 100% !important;
  border-radius: 50% !important;
  object-fit: cover !important;
}
