/*
 * 大肥鱼桌宠（dsh-pet）挂件样式 —— 纯静态前端，无 Live2D / 无后端。
 *
 * 出处：GitHub PC2005-cloud/dsh-pet（代码 MIT；动画素材仅限开源 / 非商业使用）。
 * 本项目只借用它的动画素材，不引入它的 Electron 插件框架、DSH 通信与 API 代码。
 * 动画已由 .webm（VP9 + Alpha）转成透明背景的 WebP 动图（8fps / 80 帧 / 210×194），
 * 用 <img> 渲染 —— WebKit（macOS Safari、所有 iOS 浏览器）不会出现黑底方块。
 *
 * 状态类由 dsh-pet.js 切换：
 *   is-dragging  正在被拖 / 正被甩出去（悬空姿势）
 *   is-flying    抛掷飞行中（物理下落）
 *   is-flipped   朝向翻转（东张西望后翻身、飞行方向朝左）
 *   is-sleeping  睡觉中
 *   is-menu-open 右键 / 长按菜单展开
 *   is-collapsed 折叠成右下角小鲸鱼胶囊
 */

#dsh-pet {
  /* 桌面端约 180px，移动端在下面的媒体查询里缩小 */
  --dsh-pet-width: 180px;
  --dsh-pet-gap-right: 16px;
  --dsh-pet-gap-bottom: 14px;

  position: fixed;
  right: var(--dsh-pet-gap-right);
  bottom: var(--dsh-pet-gap-bottom);
  width: var(--dsh-pet-width);
  /* 高于旧 Live2D 舞台（z-index: 9999），保证拖到它上方时不会被盖住 */
  z-index: 10000;

  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  /* 指针落在宠物上时不要触发页面滚动，否则拖不动 */
  touch-action: none;
  transition: right 0.35s ease;
}

/* 注：站内原有的 Live2D 看板娘（#oml2d-wrapper）固定在左下角，
   本挂件占右下角，两者互不遮挡，不需要额外避让。 */

/* ---------- 动画层：两层 <img> 交叉淡入，切换动作时不闪白 ---------- */
#dsh-pet .dsh-pet__stack {
  position: relative;
  width: 100%;
  /* 素材 210 × 194，用 padding 撑高（比 aspect-ratio 兼容面更宽） */
  padding-top: 92.38%;
  cursor: grab;
  filter: drop-shadow(0 8px 12px rgba(30, 44, 78, 0.16));
  transition: transform 0.18s ease, filter 0.18s ease;
}

/* 拖拽 / 飞行：放大一点并加重投影，读起来像被拎离桌面 */
#dsh-pet.is-dragging .dsh-pet__stack,
#dsh-pet.is-flying .dsh-pet__stack {
  cursor: grabbing;
  transform: scale(1.03);
  filter: drop-shadow(0 14px 18px rgba(30, 44, 78, 0.22));
}

/* 翻身：只镜像角色本体，气泡与按钮不跟着翻 */
#dsh-pet.is-flipped .dsh-pet__stack {
  transform: scaleX(-1);
}

#dsh-pet.is-dragging.is-flipped .dsh-pet__stack,
#dsh-pet.is-flying.is-flipped .dsh-pet__stack {
  transform: scaleX(-1) scale(1.03);
}

#dsh-pet .dsh-pet__frame {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  opacity: 0;
  transition: opacity 0.2s linear;
  pointer-events: none;
  -webkit-user-drag: none;
  user-select: none;
}

#dsh-pet .dsh-pet__frame.is-active {
  opacity: 1;
}

/* ---------- 台词气泡 ---------- */
#dsh-pet .dsh-pet__bubble {
  position: absolute;
  left: 50%;
  bottom: calc(100% - 4px);
  margin: 0;
  max-width: 176px;
  padding: 7px 11px;
  border-radius: 12px;
  border: 1px solid rgba(83, 109, 168, 0.18);
  background: rgba(255, 255, 255, 0.96);
  box-shadow: 0 8px 20px rgba(31, 45, 80, 0.16);
  color: #2b3552;
  font-size: 12px;
  line-height: 1.5;
  text-align: center;
  white-space: normal;
  opacity: 0;
  visibility: hidden;
  transform: translate(-50%, 6px);
  transition: opacity 0.2s ease, transform 0.2s ease, visibility 0.2s;
  pointer-events: none;
  z-index: 2;
}

#dsh-pet .dsh-pet__bubble.is-visible {
  opacity: 1;
  visibility: visible;
  transform: translate(-50%, 0);
}

#dsh-pet .dsh-pet__bubble::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -5px;
  width: 9px;
  height: 9px;
  margin-left: -5px;
  border-right: 1px solid rgba(83, 109, 168, 0.18);
  border-bottom: 1px solid rgba(83, 109, 168, 0.18);
  background: rgba(255, 255, 255, 0.96);
  transform: rotate(45deg);
}

/* ---------- 右键 / 长按菜单：贴着宠物头顶弹出 ---------- */
#dsh-pet .dsh-pet__menu {
  position: absolute;
  right: 0;
  bottom: calc(100% + 8px);
  z-index: 5;
  min-width: 108px;
  /* 菜单项有 11 条（节日限定当天才出现），小屏上允许滚动，别顶出视口 */
  max-height: calc(100vh - 24px);
  overflow-y: auto;
  margin: 0;
  padding: 5px;
  list-style: none;
  border: 1px solid rgba(83, 109, 168, 0.18);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.98);
  box-shadow: 0 10px 24px rgba(31, 45, 80, 0.18);
  font-size: 12px;
  line-height: 1;
  transform-origin: bottom right;
  animation: dsh-pet-menu-in 0.12s ease-out;
  overscroll-behavior: contain;
}

#dsh-pet .dsh-pet__menu[hidden] {
  display: none;
}

/* 按钮自身设了 display:block，会盖掉 hidden 属性，这里补回来 */
#dsh-pet .dsh-pet__menu button[hidden] {
  display: none;
}

/* 宠物贴着视口顶部时，由 JS 加这个类改成向下弹出 */
#dsh-pet.is-menu-below .dsh-pet__menu {
  top: calc(100% + 8px);
  bottom: auto;
  transform-origin: top right;
}

#dsh-pet .dsh-pet__menu button {
  display: block;
  width: 100%;
  padding: 7px 10px;
  border: 0;
  border-radius: 8px;
  background: none;
  color: #2b3552;
  font: inherit;
  line-height: 1.2;
  text-align: left;
  white-space: nowrap;
  cursor: pointer;
}

#dsh-pet .dsh-pet__menu button:hover,
#dsh-pet .dsh-pet__menu button:focus-visible {
  background: rgba(83, 109, 168, 0.12);
  color: #35507e;
  outline: none;
}

@keyframes dsh-pet-menu-in {
  from { opacity: 0; transform: translateY(4px) scale(0.96); }
  to   { opacity: 1; transform: none; }
}

/* ---------- 选动作面板：按分组列出全部动作，可搜索 ---------- */
#dsh-pet .dsh-pet__picker {
  position: absolute;
  right: 0;
  bottom: calc(100% + 8px);
  z-index: 6;
  display: flex;
  flex-direction: column;
  width: 208px;
  max-height: min(70vh, 360px);
  padding: 6px;
  border: 1px solid rgba(83, 109, 168, 0.18);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.985);
  box-shadow: 0 12px 28px rgba(31, 45, 80, 0.2);
  font-size: 12px;
  transform-origin: bottom right;
  animation: dsh-pet-menu-in 0.12s ease-out;
}

#dsh-pet .dsh-pet__picker[hidden] {
  display: none;
}

/* 宠物贴着视口顶就向下弹；贴着左边就把面板改成左对齐，别顶出屏幕 */
#dsh-pet.is-picker-below .dsh-pet__picker {
  top: calc(100% + 8px);
  bottom: auto;
  transform-origin: top right;
}

#dsh-pet.is-picker-left .dsh-pet__picker {
  right: auto;
  left: 0;
}

#dsh-pet .dsh-pet__picker-search {
  flex: none;
  width: 100%;
  box-sizing: border-box;
  margin: 0 0 5px;
  padding: 6px 8px;
  border: 1px solid rgba(83, 109, 168, 0.2);
  border-radius: 8px;
  background: rgba(244, 247, 252, 0.9);
  color: #2b3552;
  font: inherit;
}

#dsh-pet .dsh-pet__picker-search:focus {
  outline: none;
  border-color: rgba(83, 109, 168, 0.5);
}

#dsh-pet .dsh-pet__picker-list {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding-right: 2px;
}

#dsh-pet .dsh-pet__picker-title {
  position: sticky;
  top: 0;
  z-index: 1;
  margin: 0;
  padding: 5px 4px 3px;
  background: rgba(255, 255, 255, 0.97);
  color: #7b87a8;
  font-size: 11px;
  letter-spacing: 0.04em;
}

#dsh-pet .dsh-pet__picker-group[hidden],
#dsh-pet .dsh-pet__picker button[hidden] {
  display: none;
}

#dsh-pet .dsh-pet__picker button {
  display: block;
  width: 100%;
  padding: 6px 8px;
  border: 0;
  border-radius: 7px;
  background: none;
  color: #2b3552;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

#dsh-pet .dsh-pet__picker button:hover,
#dsh-pet .dsh-pet__picker button:focus-visible {
  background: rgba(83, 109, 168, 0.12);
  outline: none;
}

#dsh-pet .dsh-pet__picker-empty {
  margin: 6px 4px;
  color: #8590ab;
}

/* ---------- 折叠按钮：平时几乎透明，hover / 触屏时才明显 ---------- */
#dsh-pet .dsh-pet__toggle {
  position: absolute;
  top: -9px;
  right: -7px;
  width: 20px;
  height: 20px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid rgba(83, 109, 168, 0.28);
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.92);
  color: #5c6b8f;
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.2s ease, color 0.2s ease;
  z-index: 3;
}

#dsh-pet:hover .dsh-pet__toggle,
#dsh-pet:focus-within .dsh-pet__toggle {
  opacity: 0.9;
}

#dsh-pet .dsh-pet__toggle:hover {
  color: #d9534f;
}

@media (hover: none) {
  #dsh-pet .dsh-pet__toggle {
    opacity: 0.55;
  }
}

/* ---------- 折叠后的小胶囊：点一下把宠物叫回来 ---------- */
#dsh-pet .dsh-pet__restore {
  display: none;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  margin-left: auto;   /* 胶囊贴住右下角，不要停在 180px 盒子的左边 */
  padding: 0;
  border: 1px solid rgba(83, 109, 168, 0.25);
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 4px 12px rgba(31, 45, 80, 0.14);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  opacity: 0.6;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

#dsh-pet .dsh-pet__restore:hover {
  opacity: 1;
  transform: scale(1.06);
}

#dsh-pet.is-collapsed .dsh-pet__restore {
  display: flex;
}

/* 折叠后盒子收到胶囊大小，否则 180px 宽的透明区域会挡住正文点击 */
#dsh-pet.is-collapsed {
  width: 34px;
}

#dsh-pet.is-collapsed .dsh-pet__stack,
#dsh-pet.is-collapsed .dsh-pet__bubble,
#dsh-pet.is-collapsed .dsh-pet__menu,
#dsh-pet.is-collapsed .dsh-pet__picker,
#dsh-pet.is-collapsed .dsh-pet__toggle {
  display: none;
}

/* ---------- 窄屏：缩小到不挡正文 ---------- */
@media (max-width: 767px) {
  #dsh-pet {
    --dsh-pet-width: 116px;
    --dsh-pet-gap-right: 8px;
    --dsh-pet-gap-bottom: 8px;
  }

  #dsh-pet .dsh-pet__bubble {
    max-width: 140px;
    padding: 6px 9px;
    font-size: 11px;
  }
}

/* ---------- 无障碍：降低动效偏好时不做淡入淡出 ---------- */
@media (prefers-reduced-motion: reduce) {
  #dsh-pet,
  #dsh-pet .dsh-pet__stack,
  #dsh-pet .dsh-pet__frame,
  #dsh-pet .dsh-pet__bubble,
  #dsh-pet .dsh-pet__menu,
  #dsh-pet .dsh-pet__picker,
  #dsh-pet .dsh-pet__toggle,
  #dsh-pet .dsh-pet__restore {
    transition: none;
  }

  #dsh-pet .dsh-pet__menu,
  #dsh-pet .dsh-pet__picker {
    animation: none;
  }
}
