/* AI 宠物组件 — 数字花园主题 */

/* ── 容器 ── */
.ai-pet {
  position: fixed;
  bottom: 24px;
  z-index: 9999;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  transition: opacity 0.3s ease;
}

.ai-pet:active {
  cursor: grabbing;
}

.ai-pet.is-hidden {
  display: none;
}

/* ── 机器人本体（60x60 纯 CSS） ── */
.ai-pet-body {
  width: 60px;
  height: 60px;
  position: relative;
}

/* 主体 */
.ai-pet-torso {
  width: 48px;
  height: 42px;
  background: linear-gradient(135deg, #0d9488 0%, #6366f1 100%);
  border-radius: 16px 16px 12px 12px;
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
  box-shadow: 0 4px 12px rgba(13, 148, 136, 0.3);
}

/* 天线 */
.ai-pet-antenna {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 2px;
  height: 10px;
  background: #6366f1;
}

.ai-pet-antenna::after {
  content: "";
  position: absolute;
  top: -4px;
  left: 50%;
  transform: translateX(-50%);
  width: 8px;
  height: 8px;
  background: #f59e0b;
  border-radius: 50%;
  box-shadow: 0 0 6px rgba(245, 158, 11, 0.5);
  animation: pet-antenna-glow 2s ease-in-out infinite;
}

@keyframes pet-antenna-glow {
  0%, 100% { box-shadow: 0 0 6px rgba(245, 158, 11, 0.5); }
  50% { box-shadow: 0 0 12px rgba(245, 158, 11, 0.8); }
}

/* 眼睛 */
.ai-pet-eyes {
  position: absolute;
  top: 18px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 10px;
}

.ai-pet-eye {
  width: 8px;
  height: 8px;
  background: #ffffff;
  border-radius: 50%;
  position: relative;
  transition: all 0.3s ease;
}

.ai-pet-eye::after {
  content: "";
  position: absolute;
  width: 4px;
  height: 4px;
  background: #0f172a;
  border-radius: 50%;
  top: 2px;
  left: 2px;
  transition: all 0.2s ease;
}

/* 嘴 */
.ai-pet-mouth {
  position: absolute;
  top: 30px;
  left: 50%;
  transform: translateX(-50%);
  width: 10px;
  height: 5px;
  border-bottom: 2px solid rgba(255, 255, 255, 0.8);
  border-radius: 0 0 5px 5px;
  transition: all 0.3s ease;
}

/* 脚 */
.ai-pet-feet {
  position: absolute;
  bottom: 2px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 14px;
}

.ai-pet-foot {
  width: 12px;
  height: 8px;
  background: linear-gradient(135deg, #0d9488, #6366f1);
  border-radius: 0 0 6px 6px;
  transition: transform 0.15s ease;
}

/* ── 情绪系统 ── */

/* idle: 默认 */
.ai-pet.mood-idle .ai-pet-eye::after {
  top: 2px;
  left: 2px;
}

/* happy: 开心 — 弯眼 + 微笑 */
.ai-pet.mood-happy .ai-pet-eye {
  height: 4px;
  border-radius: 4px 4px 0 0;
}

.ai-pet.mood-happy .ai-pet-eye::after {
  display: none;
}

.ai-pet.mood-happy .ai-pet-mouth {
  width: 12px;
  height: 6px;
  border-bottom: 2px solid #ffffff;
  border-radius: 0 0 6px 6px;
}

/* think: 思考 — 抬眼 + O嘴 */
.ai-pet.mood-think .ai-pet-eye::after {
  top: 0;
  left: 2px;
}

.ai-pet.mood-think .ai-pet-mouth {
  width: 6px;
  height: 6px;
  border: 2px solid rgba(255, 255, 255, 0.8);
  border-radius: 50%;
  border-bottom: 2px solid rgba(255, 255, 255, 0.8);
}

/* confused: 困惑 — 一大一小眼 */
.ai-pet.mood-confused .ai-pet-eye:first-child {
  width: 10px;
  height: 10px;
}

.ai-pet.mood-confused .ai-pet-eye:first-child::after {
  width: 5px;
  height: 5px;
  top: 2.5px;
  left: 2.5px;
}

.ai-pet.mood-confused .ai-pet-eye:last-child {
  width: 6px;
  height: 6px;
}

.ai-pet.mood-confused .ai-pet-eye:last-child::after {
  width: 3px;
  height: 3px;
  top: 1.5px;
  left: 1.5px;
}

.ai-pet.mood-confused .ai-pet-mouth {
  width: 8px;
  height: 2px;
  border-bottom: 2px solid rgba(255, 255, 255, 0.6);
  border-radius: 0;
  transform: translateX(-50%) rotate(-5deg);
}

/* excited: 兴奋 — 大眼 + 弹跳 */
.ai-pet.mood-excited .ai-pet-eye {
  width: 10px;
  height: 10px;
}

.ai-pet.mood-excited .ai-pet-eye::after {
  width: 3px;
  height: 3px;
  top: 2px;
  left: 2px;
  animation: pet-eye-sparkle 0.4s ease-in-out infinite alternate;
}

@keyframes pet-eye-sparkle {
  0% { transform: scale(1); }
  100% { transform: scale(1.3); }
}

.ai-pet.mood-excited .ai-pet-mouth {
  width: 14px;
  height: 8px;
  border-bottom: 2px solid #ffffff;
  border-radius: 0 0 7px 7px;
}

.ai-pet.mood-excited .ai-pet-body {
  animation: pet-excited-bounce 0.5s ease-in-out infinite;
}

@keyframes pet-excited-bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}

/* sleepy: 困倦 — 半闭眼 */
.ai-pet.mood-sleepy .ai-pet-eye {
  height: 3px;
  border-radius: 3px;
}

.ai-pet.mood-sleepy .ai-pet-eye::after {
  display: none;
}

.ai-pet.mood-sleepy .ai-pet-mouth {
  width: 6px;
  height: 3px;
  border-bottom: 2px solid rgba(255, 255, 255, 0.5);
  border-radius: 0 0 3px 3px;
}

.ai-pet.mood-sleepy .ai-pet-body {
  animation: pet-sleepy-nod 3s ease-in-out infinite;
}

@keyframes pet-sleepy-nod {
  0%, 100% { transform: rotate(0deg); }
  30% { transform: rotate(3deg); }
  60% { transform: rotate(-2deg); }
}

/* ── 行走动画 ── */
.ai-pet.is-walking .ai-pet-body {
  animation: pet-walk-bounce 0.35s ease-in-out infinite;
}

@keyframes pet-walk-bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-3px); }
}

.ai-pet.is-walking .ai-pet-foot:first-child {
  animation: pet-foot-left 0.35s ease-in-out infinite;
}

.ai-pet.is-walking .ai-pet-foot:last-child {
  animation: pet-foot-right 0.35s ease-in-out infinite;
}

@keyframes pet-foot-left {
  0%, 100% { transform: translateY(0) rotate(0); }
  50% { transform: translateY(-2px) rotate(-8deg); }
}

@keyframes pet-foot-right {
  0%, 100% { transform: translateY(-2px) rotate(8deg); }
  50% { transform: translateY(0) rotate(0); }
}

/* 朝向翻转 */
.ai-pet.facing-left .ai-pet-body {
  transform: scaleX(-1);
}

/* ── 气泡对话 ── */
.ai-pet-bubble {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--garden-surface, #ffffff);
  border: 1px solid var(--garden-border, #e5e7eb);
  border-radius: 12px;
  padding: 8px 12px;
  min-width: 120px;
  max-width: 220px;
  font-size: 0.82rem;
  line-height: 1.5;
  color: var(--garden-text, #1f2937);
  box-shadow: 0 8px 20px rgba(15, 23, 42, 0.12);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease, transform 0.3s ease;
  transform: translateX(-50%) translateY(4px);
  white-space: pre-wrap;
  word-break: break-word;
}

/* 气泡三角 */
.ai-pet-bubble::after {
  content: "";
  position: absolute;
  bottom: -6px;
  left: 50%;
  transform: translateX(-50%) rotate(45deg);
  width: 10px;
  height: 10px;
  background: var(--garden-surface, #ffffff);
  border-right: 1px solid var(--garden-border, #e5e7eb);
  border-bottom: 1px solid var(--garden-border, #e5e7eb);
}

.ai-pet-bubble.is-visible {
  opacity: 1;
  pointer-events: auto;
  transform: translateX(-50%) translateY(0);
}

/* 打字光标 */
.ai-pet-bubble .typing-cursor {
  display: inline-block;
  width: 2px;
  height: 1em;
  background: var(--garden-primary, #0d9488);
  vertical-align: text-bottom;
  animation: pet-cursor-blink 0.6s step-end infinite;
  margin-left: 1px;
}

@keyframes pet-cursor-blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

/* ── 控制按钮 ── */
.ai-pet-controls {
  position: absolute;
  top: -6px;
  right: -6px;
  display: flex;
  gap: 3px;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.ai-pet:hover .ai-pet-controls {
  opacity: 1;
}

.ai-pet-btn {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 1px solid var(--garden-border, #e5e7eb);
  background: var(--garden-surface, #ffffff);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 10px;
  line-height: 1;
  color: var(--garden-muted, #6b7280);
  transition: all 0.2s ease;
  padding: 0;
}

.ai-pet-btn:hover {
  background: var(--garden-primary-soft, rgba(13, 148, 136, 0.1));
  color: var(--garden-primary, #0d9488);
  border-color: var(--garden-primary, #0d9488);
}

/* ── 最小化状态 ── */
.ai-pet-minimized {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 9999;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: linear-gradient(135deg, #0d9488 0%, #6366f1 100%);
  box-shadow: 0 4px 12px rgba(13, 148, 136, 0.3);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: pet-breathe 3s ease-in-out infinite;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.ai-pet-minimized:hover {
  transform: scale(1.1);
  box-shadow: 0 6px 18px rgba(13, 148, 136, 0.45);
}

.ai-pet-minimized.is-hidden {
  display: none;
}

@keyframes pet-breathe {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.06); }
}

/* 最小化图标 — 简化小眼睛 */
.ai-pet-minimized::before,
.ai-pet-minimized::after {
  content: "";
  width: 5px;
  height: 5px;
  background: #ffffff;
  border-radius: 50%;
  position: absolute;
}

.ai-pet-minimized::before {
  top: 13px;
  left: 10px;
}

.ai-pet-minimized::after {
  top: 13px;
  right: 10px;
}

/* ── 拖拽时禁用过渡 ── */
.ai-pet.is-dragging {
  transition: none !important;
}

.ai-pet.is-dragging .ai-pet-body {
  animation: none !important;
}

/* ── 深色模式 ── */
html.book-color-scheme-dark .ai-pet-bubble,
html[data-theme="dark"] .ai-pet-bubble,
.dark .ai-pet-bubble {
  background: #111827;
  border-color: #1f2937;
  color: #e5e7eb;
  box-shadow: 0 8px 24px rgba(2, 8, 23, 0.5);
}

html.book-color-scheme-dark .ai-pet-bubble::after,
html[data-theme="dark"] .ai-pet-bubble::after,
.dark .ai-pet-bubble::after {
  background: #111827;
  border-color: #1f2937;
}

html.book-color-scheme-dark .ai-pet-btn,
html[data-theme="dark"] .ai-pet-btn,
.dark .ai-pet-btn {
  background: #1f2937;
  border-color: #374151;
  color: #9ca3af;
}

html.book-color-scheme-dark .ai-pet-btn:hover,
html[data-theme="dark"] .ai-pet-btn:hover,
.dark .ai-pet-btn:hover {
  background: rgba(45, 212, 191, 0.15);
  color: #2dd4bf;
  border-color: #2dd4bf;
}

html.book-color-scheme-dark .ai-pet-torso,
html[data-theme="dark"] .ai-pet-torso,
.dark .ai-pet-torso {
  background: linear-gradient(135deg, #2dd4bf 0%, #a5b4fc 100%);
  box-shadow: 0 4px 12px rgba(45, 212, 191, 0.3);
}

html.book-color-scheme-dark .ai-pet-minimized,
html[data-theme="dark"] .ai-pet-minimized,
.dark .ai-pet-minimized {
  background: linear-gradient(135deg, #2dd4bf 0%, #a5b4fc 100%);
  box-shadow: 0 4px 12px rgba(45, 212, 191, 0.3);
}

html.book-color-scheme-dark .ai-pet-foot,
html[data-theme="dark"] .ai-pet-foot,
.dark .ai-pet-foot {
  background: linear-gradient(135deg, #2dd4bf, #a5b4fc);
}

html.book-color-scheme-dark .ai-pet-antenna,
html[data-theme="dark"] .ai-pet-antenna,
.dark .ai-pet-antenna {
  background: #a5b4fc;
}

html.book-color-scheme-dark .ai-pet-antenna::after,
html[data-theme="dark"] .ai-pet-antenna::after,
.dark .ai-pet-antenna::after {
  background: #fbbf24;
}

/* ── 响应式 ── */
@media (max-width: 640px) {
  .ai-pet-bubble {
    max-width: 180px;
    font-size: 0.78rem;
  }
}

/* ── 无障碍：减少动画 ── */
@media (prefers-reduced-motion: reduce) {
  .ai-pet *,
  .ai-pet-minimized {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
