/* ============================================================
   AI精灵「小光」— 悬浮助理 (ai-tokens.cn)  v6
   可拖动 / 访问主动打招呼 / 招呼语随 日期·时间·天气 变化
   助理面板：流式对话 · 思考过程 · Markdown · 会话管理 · 消息操作
   纯前端，零依赖；后端 /api/elf/chat(SSE) /ask /greet /status /feedback
   ============================================================ */
#ai-elf {
  position: fixed;
  z-index: 2147483000;
  right: 22px;
  bottom: 22px;
  width: 66px;
  height: 77px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  -webkit-user-select: none;
  user-select: none;
  touch-action: none;
}
#ai-elf .elf-sprite {
  width: 66px;
  height: 77px;
  cursor: grab;
  position: relative;
  z-index: 2;
  filter: drop-shadow(0 6px 14px rgba(80, 60, 200, .32));
  animation: elf-bob 3.2s ease-in-out infinite;
  transition: transform .15s;
}
#ai-elf.dragging .elf-sprite { cursor: grabbing; animation: none; }
#ai-elf .elf-sprite:active { transform: scale(.94); }
@keyframes elf-bob { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-6px); } }
/* 眨眼：作用于眼睛分组，fill-box 让 transform-origin 相对于眼睛自身包围盒 */
#ai-elf .elf-eye {
  transform-box: fill-box;
  transform-origin: center;
  animation: elf-blink 4.5s infinite;
}
@keyframes elf-blink { 0%, 92%, 100% { transform: scaleY(1); } 96% { transform: scaleY(.12); } }

/* ---------------- 打招呼气泡 ---------------- */
#ai-elf .elf-bubble {
  position: absolute;
  bottom: 88px;
  right: 0;
  max-width: 264px;
  min-width: 168px;
  background: #fff;
  color: #1f2330;
  border-radius: 14px;
  padding: 12px 14px 13px;
  font-size: 13.5px;
  line-height: 1.62;
  box-shadow: 0 10px 30px rgba(40, 30, 120, .25);
  border: 1px solid rgba(124, 92, 255, .25);
  opacity: 0;
  transform: translateY(8px) scale(.96);
  pointer-events: none;
  transition: opacity .25s, transform .25s;
  z-index: 3;
}
#ai-elf.show .elf-bubble { opacity: 1; transform: translateY(0) scale(1); pointer-events: auto; }
#ai-elf .elf-bubble:after {
  content: "";
  position: absolute;
  right: 22px;
  bottom: -7px;
  width: 14px;
  height: 14px;
  background: #fff;
  border-right: 1px solid rgba(124, 92, 255, .25);
  border-bottom: 1px solid rgba(124, 92, 255, .25);
  transform: rotate(45deg);
  border-radius: 0 0 4px 0;
}
#ai-elf .elf-bubble .elf-close {
  position: absolute;
  top: 5px;
  right: 9px;
  cursor: pointer;
  color: #9aa0b4;
  font-size: 15px;
  line-height: 1;
}
#ai-elf .elf-bubble .elf-close:hover { color: #7c5cff; }
#ai-elf .elf-bubble .elf-name {
  display: inline-block;
  font-size: 11px;
  color: #7c5cff;
  font-weight: 700;
  margin-bottom: 4px;
  letter-spacing: .3px;
}
#ai-elf .elf-bubble .elf-text { color: #2a2e3c; word-break: break-word; }

/* ---------------- 助理面板 ---------------- */
#ai-elf .elf-panel {
  position: absolute;
  bottom: 88px;
  right: 0;
  width: 344px;
  max-width: calc(100vw - 28px);
  max-height: var(--elf-maxh, 470px);
  display: flex;
  flex-direction: column;
  background: #fff;
  color: #1f2330;
  border-radius: 16px;
  border: 1px solid rgba(124, 92, 255, .22);
  box-shadow: 0 18px 48px rgba(40, 30, 120, .28);
  overflow: hidden;
  z-index: 4;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform: translate(var(--elf-shift, 0px), 8px) scale(.97);
  transform-origin: bottom right;
  transition: opacity .22s, transform .22s, visibility .22s;
  touch-action: auto;
  -webkit-user-select: auto;
  user-select: auto;
}
#ai-elf .elf-panel.open {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transform: translate(var(--elf-shift, 0px), 0) scale(1);
}
#ai-elf .elf-panel--left { right: auto; left: 0; transform-origin: bottom left; }
#ai-elf .elf-panel--below { bottom: auto; top: 88px; transform-origin: top right; }
#ai-elf .elf-panel--below.elf-panel--left { transform-origin: top left; }

#ai-elf .elf-panel .elf-hd {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 9px 10px 9px 12px;
  background: linear-gradient(135deg, #7c5cff, #9b7cff);
  color: #fff;
  font-size: 12.5px;
  font-weight: 700;
  flex: 0 0 auto;
}
#ai-elf .elf-panel .elf-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: #7ed957; box-shadow: 0 0 0 2px rgba(255, 255, 255, .35);
}
#ai-elf.elf-busy .elf-panel .elf-dot { background: #ffd166; animation: elf-pulse 1s ease-in-out infinite; }
#ai-elf .elf-panel .elf-title { letter-spacing: .3px; }
#ai-elf .elf-panel .elf-mode {
  font-weight: 500; font-size: 10.5px; opacity: .82;
  border: 1px solid rgba(255, 255, 255, .45);
  border-radius: 999px; padding: 1px 7px; margin-left: 2px;
}
#ai-elf .elf-panel .elf-mode.on { background: rgba(255, 255, 255, .22); opacity: 1; }
#ai-elf .elf-panel .elf-act {
  background: transparent; border: 0; color: #fff;
  font-size: 15px; line-height: 1; cursor: pointer;
  padding: 2px 6px; border-radius: 7px; opacity: .85; font-family: inherit;
}
#ai-elf .elf-panel .elf-new { margin-left: auto; }
#ai-elf .elf-panel .elf-act.elf-min { font-size: 17px; }
#ai-elf .elf-panel .elf-act:hover { background: rgba(255, 255, 255, .2); opacity: 1; }

/* ---------------- ⚡ 能力 / 历史 弹层 ---------------- */
#ai-elf .elf-pop {
  flex: 0 0 auto;
  max-height: 236px;
  overflow-y: auto;
  padding: 9px 12px 11px;
  background: #fbfaff;
  border-bottom: 1px solid #eceafd;
  font-size: 12px;
  color: #3a3d50;
}
#ai-elf .elf-pop-h {
  font-size: 10.5px; font-weight: 700; color: #8b8fa6;
  text-transform: uppercase; letter-spacing: .6px;
  margin: 6px 0 4px;
}
#ai-elf .elf-pop-h:first-child { margin-top: 0; }
#ai-elf .elf-caps div { line-height: 1.65; color: #4a4d61; }
#ai-elf .elf-hist { display: flex; flex-direction: column; gap: 3px; }
#ai-elf .elf-hist-i {
  text-align: left; font: inherit; font-size: 11.5px;
  background: #fff; color: #4a4d61;
  border: 1px solid #e8e5f8; border-radius: 8px;
  padding: 5px 8px; cursor: pointer;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
#ai-elf .elf-hist-i:hover { border-color: #c8bdfa; color: #6a49f2; }
#ai-elf .elf-hist-i.on { background: #f0eefe; border-color: #7c5cff; color: #5a3ce0; font-weight: 700; }
#ai-elf .elf-pop-a { display: flex; gap: 6px; margin-top: 9px; }
#ai-elf .elf-pop-a button {
  flex: 1 1 0; font: inherit; font-size: 11.5px; font-weight: 600;
  color: #6a49f2; background: #fff;
  border: 1px solid rgba(124, 92, 255, .35); border-radius: 9px;
  padding: 6px 8px; cursor: pointer;
}
#ai-elf .elf-pop-a button:hover { background: #f3efff; }

/* ---------------- 对话流 ---------------- */
#ai-elf .elf-panel .elf-chat {
  padding: 11px 11px 6px;
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 132px;
  font-size: 13px;
  line-height: 1.62;
  background: #fafaff;
  overscroll-behavior: contain;
}
#ai-elf .elf-panel .elf-chat::-webkit-scrollbar { width: 6px; }
#ai-elf .elf-panel .elf-chat::-webkit-scrollbar-thumb { background: #d8d3f2; border-radius: 3px; }
#ai-elf .elf-panel .elf-msg {
  margin: 0 0 9px;
  padding: 7px 10px;
  border-radius: 12px;
  white-space: pre-line;
  word-break: break-word;
  max-width: 94%;
  width: fit-content;
  animation: elf-in .18s ease-out;
}
@keyframes elf-in { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
#ai-elf .elf-panel .elf-msg--bot {
  background: #f1effe; color: #23263a;
  border: 1px solid rgba(124, 92, 255, .16);
  border-bottom-left-radius: 4px;
}
#ai-elf .elf-panel .elf-msg--me {
  background: #7c5cff; color: #fff;
  margin-left: auto; border-bottom-right-radius: 4px;
}

/* Markdown 内容 */
#ai-elf .elf-md p { margin: 0 0 5px; }
#ai-elf .elf-md p:last-child { margin-bottom: 0; }
#ai-elf .elf-md ul { margin: 2px 0 5px; padding-left: 16px; }
#ai-elf .elf-md li { margin: 0 0 2px; }
#ai-elf .elf-md b { font-weight: 700; color: #3a2f7a; }
#ai-elf .elf-md code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11.5px; background: #e8e4fb; border-radius: 4px; padding: 0 4px;
}
#ai-elf .elf-md a { color: #6a49f2; }
#ai-elf .elf-tb {
  border-collapse: collapse;
  width: 100%;
  margin: 3px 0 6px;
  font-size: 11.5px;
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
}
#ai-elf .elf-tb th, #ai-elf .elf-tb td {
  border: 1px solid #e6e2f8;
  padding: 3px 6px;
  text-align: left;
  white-space: nowrap;
}
#ai-elf .elf-tb th { background: #efeaff; color: #4a3a9e; font-weight: 700; }
#ai-elf .elf-caret {
  display: inline-block;
  width: 6px; height: 13px;
  margin-left: 1px;
  vertical-align: -2px;
  background: #7c5cff;
  border-radius: 1px;
  animation: elf-caret 1s steps(2, start) infinite;
}
@keyframes elf-caret { 0%, 50% { opacity: 1; } 50.01%, 100% { opacity: 0; } }

/* 思考过程 */
#ai-elf .elf-steps { margin: 0 0 6px; }
#ai-elf .elf-steps.is-running {
  border-left: 2px solid #cfc6f8;
  padding-left: 8px;
  color: #7a7e93;
  font-size: 11.5px;
  line-height: 1.7;
}
#ai-elf .elf-step { display: flex; align-items: center; gap: 6px; }
#ai-elf .elf-step.now { color: #5a3ce0; }
#ai-elf .elf-spin {
  width: 7px; height: 7px; border-radius: 50%;
  background: #b9aef2; flex: 0 0 auto;
}
#ai-elf .elf-step.now .elf-spin { background: #7c5cff; animation: elf-pulse 1s ease-in-out infinite; }
#ai-elf .elf-steps.is-done .elf-steps-hd {
  font: inherit; font-size: 11px;
  background: transparent; border: 0; padding: 0;
  color: #9a9db1; cursor: pointer;
}
#ai-elf .elf-steps.is-done .elf-steps-hd:hover { color: #6a49f2; }
#ai-elf .elf-steps-body { display: none; }
#ai-elf .elf-steps.is-done.open .elf-steps-body {
  display: block; color: #9a9db1; font-size: 11px; line-height: 1.65; margin-top: 2px;
}

/* 引用链接 */
#ai-elf .elf-panel .elf-links { display: flex; flex-wrap: wrap; gap: 5px; margin: 6px 0 2px; }
#ai-elf .elf-panel .elf-links a {
  display: inline-block;
  max-width: 100%;
  font-size: 11.5px;
  color: #6a49f2;
  background: #fff;
  border: 1px solid rgba(124, 92, 255, .35);
  border-radius: 999px;
  padding: 3px 9px;
  text-decoration: none;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
#ai-elf .elf-panel .elf-links a:hover { background: #f3efff; border-color: #7c5cff; }

/* 消息操作 */
#ai-elf .elf-acts { display: flex; align-items: center; gap: 2px; margin-top: 6px; }
#ai-elf .elf-abtn {
  font: inherit; font-size: 11.5px; line-height: 1;
  background: transparent; border: 0; cursor: pointer;
  color: #9a9db1; padding: 2px 5px; border-radius: 6px;
}
#ai-elf .elf-abtn:hover { background: #e6e1fb; color: #6a49f2; }
#ai-elf .elf-voted { font-size: 11px; color: #7a7e93; padding-left: 3px; }

/* 快捷 chips */
#ai-elf .elf-panel .elf-chips {
  display: flex; flex-wrap: wrap; gap: 5px;
  padding: 7px 11px 8px;
  border-top: 1px solid #f0eefe;
  background: #fff;
  flex: 0 0 auto;
  max-height: 92px;
  overflow-y: auto;
}
#ai-elf .elf-panel .elf-chip {
  font-size: 11.5px; color: #5a5f75;
  background: #f6f5ff;
  border: 1px solid rgba(124, 92, 255, .2);
  border-radius: 999px;
  padding: 3px 9px;
  cursor: pointer;
  font-family: inherit;
}
#ai-elf .elf-panel .elf-chip:hover { background: #ece8ff; color: #6a49f2; border-color: rgba(124, 92, 255, .45); }

/* 输入区 */
#ai-elf .elf-panel .elf-form {
  display: flex; align-items: center; gap: 5px;
  padding: 8px 10px 6px;
  border-top: 1px solid #f0eefe;
  background: #fff;
  flex: 0 0 auto;
}
#ai-elf .elf-panel .elf-input {
  flex: 1 1 auto; min-width: 0;
  font: inherit; font-size: 13px;
  color: #1f2330;
  padding: 8px 10px;
  border: 1px solid #e2dff5;
  border-radius: 10px;
  background: #fbfaff;
  outline: none;
}
#ai-elf .elf-panel .elf-input:focus { border-color: #7c5cff; background: #fff; box-shadow: 0 0 0 3px rgba(124, 92, 255, .12); }
#ai-elf .elf-panel .elf-input:disabled { opacity: .6; }
#ai-elf .elf-panel .elf-send {
  flex: 0 0 auto;
  font: inherit; font-size: 12.5px; font-weight: 700;
  color: #fff; background: #7c5cff;
  border: 0; border-radius: 10px;
  padding: 8px 13px; cursor: pointer;
}
#ai-elf .elf-panel .elf-send:hover { background: #6a49f2; }
#ai-elf .elf-panel .elf-stop {
  flex: 0 0 auto;
  font: inherit; font-size: 12.5px; font-weight: 700;
  color: #fff; background: #f0655f;
  border: 0; border-radius: 10px;
  padding: 8px 11px; cursor: pointer;
}
#ai-elf .elf-panel .elf-stop:hover { background: #e0483f; }
#ai-elf .elf-panel .elf-mic {
  flex: 0 0 auto;
  font-size: 14px; line-height: 1;
  background: #f6f5ff; border: 1px solid #e2dff5;
  border-radius: 10px; padding: 7px 8px; cursor: pointer;
}
#ai-elf .elf-panel .elf-mic.on { background: #ffe3e1; border-color: #f0655f; animation: elf-pulse 1s ease-in-out infinite; }
#ai-elf .elf-panel .elf-foot {
  font-size: 10.5px; color: #a7abbe;
  padding: 2px 11px 9px;
  background: #fff;
  flex: 0 0 auto;
}

@media (prefers-reduced-motion: reduce) {
  #ai-elf .elf-sprite { animation: none; }
  #ai-elf .elf-eye { animation: none; }
  #ai-elf .elf-panel, #ai-elf .elf-panel.open { transition: none; }
  #ai-elf .elf-panel .elf-typing,
  #ai-elf.elf-busy .elf-panel .elf-dot,
  #ai-elf .elf-step.now .elf-spin,
  #ai-elf .elf-panel .elf-mic.on { animation: none; }
  #ai-elf .elf-panel .elf-msg { animation: none; }
  #ai-elf .elf-caret { animation: none; }
}
@media (max-width: 520px) {
  #ai-elf { width: 54px; height: 63px; right: 14px; bottom: 14px; }
  #ai-elf .elf-sprite { width: 54px; height: 63px; }
  #ai-elf .elf-bubble { max-width: 208px; bottom: 74px; }
  #ai-elf .elf-panel { width: min(344px, calc(100vw - 24px)); bottom: 74px; }
  #ai-elf .elf-panel--below { top: 74px; }
}
