AiAssistantBall.vue 26.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
<template>
  <!-- #ifdef APP-PLUS -->
  <view v-if="visible" class="ai-ball-root">
    <!-- 每日首次引导气泡 -->
    <view
      v-if="guideVisible"
      class="guide-bubble"
      :class="{ 'guide-fade': guideFading }"
    >
      <text class="guide-text">点击 AI 助手,获取接待话术与实时分析</text>
      <view class="guide-arrow" />
    </view>

    <!-- 异常持久气泡 -->
    <view
      v-if="abnormalBubbleVisible"
      class="abnormal-bubble"
      @tap.stop="onAbnormalBubbleTap"
    >
      <text class="abnormal-title">分析异常</text>
      <text class="abnormal-msg">{{ abnormalMessage }}</text>
      <text class="abnormal-hint">点击关闭</text>
    </view>

    <!-- AI 悬浮球(按原型:待机圆形呼吸 / 思考中展开条形 + 声波 + 取消) -->
    <view
      class="fab-container"
      :class="{ 'state-idle': uiState !== 'thinking', 'state-active': uiState === 'thinking' }"
      @tap.stop="onBallTap"
    >
      <view class="fab-content-idle">
        <text class="ai-text">AI</text>
      </view>
      <view class="fab-content-active">
        <view class="sound-wave" aria-hidden="true">
          <view class="bar" />
          <view class="bar" />
          <view class="bar" />
          <view class="bar" />
        </view>
        <text class="fab-active-text">正在思考中,请稍候...</text>
        <view class="fab-cancel-btn" @tap.stop="closeResult">✕</view>
      </view>
    </view>

    <!-- 操作菜单(按原型:锚定 FAB 上方的 popup;不使用全屏遮罩) -->
    <view v-if="menuVisible" class="popup-menu" @tap.stop>
      <view class="popup-header-row">
        <text class="popup-header">您需要什么帮助?</text>
        <view class="popup-close-btn" @tap.stop="closeMenu">✕</view>
      </view>
      <view class="popup-btn primary" @tap="onHelpAnswer">
        <text class="popup-btn-title">🎤 帮我回答顾客刚才的问题(近1分钟)</text>
      </view>
      <view class="popup-btn" @tap="onUsageIntro">
        <text class="popup-btn-title">📖 介绍下谈客助手该如何使用</text>
      </view>
      <view class="popup-divider" />
      <view class="popup-skip" @tap.stop="toggleSkipMenu">
        <view :class="['skip-check', { checked: skipMenu }]" />
        <text class="popup-skip-text">下次点击直接开始思考,不再显示此菜单</text>
      </view>
    </view>

    <!-- 使用介绍(按原型:锚定 FAB 上方的 popup;不使用全屏遮罩) -->
    <view v-if="introVisible" class="popup-menu" @tap.stop>
      <view class="popup-header-row">
        <text class="popup-header">使用介绍</text>
        <view class="popup-close-btn" @tap.stop="introVisible = false">✕</view>
      </view>
      <text class="popup-body">{{ introText }}</text>
    </view>

    <!-- 多问题选择(按原型:锚定 FAB 上方的 popup;不使用全屏遮罩) -->
    <view v-if="questionMenuVisible" class="popup-menu" @tap.stop>
      <view class="popup-header-row">
        <text class="popup-header">客户当前更关注:</text>
        <view class="popup-close-btn" @tap.stop="closeQuestionMenu">✕</view>
      </view>
      <view
        v-for="(item, idx) in qaList"
        :key="'qmenu-' + idx"
        class="popup-btn"
        @tap.stop="selectQa(idx)"
      >
        <text class="popup-btn-title">{{ item.query }}</text>
      </view>
    </view>

    <!-- AI 流式结果(mp-html 渲染 markdown) -->
    <view v-if="resultVisible" class="menu-mask" @tap="closeResult">
      <view class="result-panel" @tap.stop>
        <view class="result-head">
          <view class="result-head-main">
            <text class="result-title">AI 建议</text>
            <text class="result-status">
              {{ streamStatusText }}<text v-if="costText">({{ costText }})</text>
            </text>
          </view>
          <text class="result-close" @tap.stop="closeResult">关闭</text>
        </view>
        <scroll-view
          scroll-y
          class="result-scroll"
          :scroll-top="resultScrollTop"
          :scroll-with-animation="true"
        >
          <view v-if="!streamMarkdown && streamStatus === 'uploading'" class="result-loading">
            <text>正在分析录音,等待 AI 回复…</text>
          </view>
          <view v-if="qaList.length" class="qa-root">
            <!-- 问题列表页:未选择问题时展示 -->
            <view v-if="selectedQaIndex < 0">
              <text class="qa-title">识别到的问题</text>
              <view class="qa-list">
                <view
                  v-for="(item, idx) in qaList"
                  :key="'qa-' + idx"
                  class="qa-item"
                  @tap.stop="selectQa(idx)"
                >
                  <text class="qa-q">{{ item.query }}</text>
                </view>
              </view>
              <text class="qa-hint">点击一个问题查看答案</text>
            </view>

            <!-- 答案页:选择后只展示答案,隐藏问题 -->
            <view v-else class="qa-answer-page">
              <view class="qa-answer-head">
                <text class="qa-back" @tap.stop="backToQaList">返回问题列表</text>
                <text class="qa-answer-title">答案</text>
              </view>
              <text class="qa-a">{{ selectedQa && selectedQa.answer }}</text>
            </view>
          </view>
          <!-- 始终挂载 mp-html,靠 :content 增量更新实现流式渲染(v2.5.2 差量 diff) -->
          <mp-html
            v-show="!!streamMarkdown && !qaList.length"
            ref="streamHtml"
            :content="streamMarkdown"
            :markdown="true"
            :selectable="true"
            container-style="padding:0 8rpx;line-height:1.65;font-size:26rpx;color:#1f2937;"
          />
        </scroll-view>
      </view>
    </view>
  </view>
  <!-- #endif -->
</template>

<script>
// #ifdef APP-PLUS
import {
  getSkipMenu,
  setSkipMenu,
  shouldShowDailyGuide,
  markDailyGuideShown
} from '@/utils/aiAssistant/AiAssistantPref.js'
import RecordService from '@/utils/recordManager/RecordService.js'
import { getAiStreamSession } from '@/utils/aiAssistant/aiStreamSession.js'
import { TransportType } from '@/utils/socket'
import { serializeIfObject } from '@/utils/utils.js'
const DEMO_CAPTURE_SECONDS = 60
const INTRO_TEXT =
  '1. 开始接待后,AI 球出现在左下角\n2. 点击「帮我回答」,将截取最近录音并发送至 AI 分析\n3. 分析结果将展示在界面中\n4. 可勾选「跳过菜单」以快速进入分析'
// #endif

/** idle | menu | thinking */
export default {
  name: 'AiAssistantBall',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      uiState: 'idle',
      menuVisible: false,
      introVisible: false,
      questionMenuVisible: false,
      guideVisible: false,
      guideFading: false,
      skipMenu: false,
      abnormalBubbleVisible: false,
      abnormalMessage: '',
      _guideTimer: null,
      _pendingBallTap: false,
      introText: INTRO_TEXT,
      resultVisible: false,
      streamMarkdown: '',
      streamStatus: 'idle',
      pcmCostMs: 0,
      encodeCostMs: 0,
      uploadCostMs: 0,
      resultCostMs: 0,
      totalCostMs: 0,
      resultScrollTop: 0,
      _scrollPending: false,
      qaList: [],
      selectedQaIndex: -1
    }
  },
  computed: {
    ballLabel() {
      if (this.uiState === 'thinking') return '思考'
      return 'AI'
    },
    streamStatusText() {
      const map = {
        idle: '',
        uploading: '上传录音中…',
        streaming: '生成中…',
        done: '完成',
        error: '出错了'
      }
      return map[this.streamStatus] || ''
    },
    costText() {
      const p = this.pcmCostMs > 0 ? `截取${this.pcmCostMs}ms` : ''
      const e = this.encodeCostMs > 0 ? `编码${this.encodeCostMs}ms` : ''
      const u = this.uploadCostMs > 0 ? `后端${this.uploadCostMs}ms` : ''
      const r = this.resultCostMs > 0 ? `渲染${this.resultCostMs}ms` : ''
      const t = this.totalCostMs > 0 ? `总${this.totalCostMs}ms` : ''
      return [p, e, u, r, t].filter(Boolean).join(' | ')
    },
    selectedQa() {
      if (!this.qaList.length) return null
      if (this.selectedQaIndex < 0) return null
      return this.qaList[this.selectedQaIndex] || null
    }
  },
  watch: {
    visible: {
      immediate: true,
      handler(val) {
        // #ifdef APP-PLUS
        if (val) {
          this.onEnterReception()
        } else {
          this.resetAll()
        }
        // #endif
      }
    }
  },
  beforeDestroy() {
    this.clearGuideTimer()
    getAiStreamSession().cancel()
  },
  methods: {
    onEnterReception() {
      // #ifdef APP-PLUS
      this.skipMenu = getSkipMenu()
      this.tryShowDailyGuide()
      // #endif
    },
    tryShowDailyGuide() {
      // #ifdef APP-PLUS
      if (!shouldShowDailyGuide()) return
      this.guideVisible = true
      this.guideFading = false
      markDailyGuideShown()
      this.clearGuideTimer()
      this._guideTimer = setTimeout(() => {
        this.guideFading = true
        setTimeout(() => {
          this.guideVisible = false
          this.guideFading = false
        }, 400)
      }, 6000)
      // #endif
    },
    clearGuideTimer() {
      if (this._guideTimer) {
        clearTimeout(this._guideTimer)
        this._guideTimer = null
      }
    },
    onBallTap() {
      // #ifdef APP-PLUS
      if (this.abnormalBubbleVisible) {
        this.abnormalBubbleVisible = false
        this._pendingBallTap = true
        this.$nextTick(() => {
          if (this._pendingBallTap) {
            this._pendingBallTap = false
            this.handleBallAction()
          }
        })
        return
      }
      this.handleBallAction()
      // #endif
    },
    onAbnormalBubbleTap() {
      this.abnormalBubbleVisible = false
    },
    handleBallAction() {
      // #ifdef APP-PLUS
      if (this.uiState === 'thinking') return
      if (this.skipMenu) {
        this.startThinking()
        return
      }
      this.uiState = 'menu'
      this.menuVisible = true
      // #endif
    },
    closeMenu() {
      this.menuVisible = false
      if (this.uiState === 'menu') {
        this.uiState = 'idle'
      }
    },
    closeQuestionMenu() {
      this.questionMenuVisible = false
    },
    toggleSkipMenu() {
      // #ifdef APP-PLUS
      this.skipMenu = !this.skipMenu
      setSkipMenu(this.skipMenu)
      // #endif
    },
    onHelpAnswer() {
      this.closeMenu()
      this.startThinking()
    },
    onUsageIntro() {
      this.menuVisible = false
      this.uiState = 'idle'
      this.introVisible = true
    },
    async startThinking() {
      // #ifdef APP-PLUS
      this.uiState = 'thinking'
      this.menuVisible = false
      this.introVisible = false
      this.questionMenuVisible = false
      // 按原型:进入思考态只展开 FAB,不弹出全屏结果面板
      this.resultVisible = false
      this.streamMarkdown = ''
      this.streamStatus = 'uploading'
      this.pcmCostMs = 0
      this.encodeCostMs = 0
      this.uploadCostMs = 0
      this.resultCostMs = 0
      this.totalCostMs = 0
      this.resultScrollTop = 0
      this.qaList = []
      this.selectedQaIndex = -1

      try {
        await this.runHelpAnswerFlowHttp()
        this.streamStatus = 'done'
        this.uiState = 'idle'
      } catch (e) {
        this.streamStatus = 'error'
        this.uiState = 'idle'
        this.resultVisible = false
        this.showAbnormalBubble(e.message || '分析失败,请稍后重试')
      }
      // #endif
    },
    selectQa(idx) {
      this.selectedQaIndex = idx
      if (this.questionMenuVisible) {
        this.questionMenuVisible = false
        this.resultVisible = true
      }
    },
    backToQaList() {
      this.selectedQaIndex = -1
    },
    /** 打开面板即开始监听/模拟流式 markdown,边收边渲染 */
    beginStreamSession() {
      // #ifdef APP-PLUS
      return new Promise((resolve, reject) => {
        getAiStreamSession().start({
          onUpdate: ({ markdown }) => {
            if (!markdown) return
            this.streamStatus = 'streaming'
            this.streamMarkdown = markdown
            this.scrollStreamToBottom()
          },
          onDone: () => {
            this.streamStatus = 'done'
            this.uiState = 'idle'
            this.scrollStreamToBottom()
            resolve()
          },
          onError: (err) => {
            reject(err)
          }
        })
      })
      // #endif
    },
    scrollStreamToBottom() {
      if (this._scrollPending) return
      this._scrollPending = true
      this.$nextTick(() => {
        this.resultScrollTop = this.resultScrollTop === 99999 ? 99998 : 99999
        this._scrollPending = false
      })
    },
    async runHelpAnswerFlowHttp() {
      // #ifdef APP-PLUS
      // 先确保有录音数据(避免空跑)
      const recordService = RecordService.getInstance()
      const tTotalStart = Date.now()
      const tPcmStart = Date.now()
      const buffer = recordService.captureRecentAudio(DEMO_CAPTURE_SECONDS, 'pcm')
      this.pcmCostMs = Date.now() - tPcmStart
      if (!buffer?.byteLength) {
        throw new Error('暂无录音数据,请确认接待录音已开始')
      }

      const client = recordService.getAiClient()
      if (!client) {
        throw new Error('AI 未初始化(请先开始接待)')
      }
      if (client.transport !== TransportType.HTTP) {
        throw new Error('当前接待使用 WebSocket 模式,HTTP 请求未启用')
      }

      const tEncodeStart = Date.now()
      const audioBase64 = uni.arrayBufferToBase64(buffer)
      this.encodeCostMs = Date.now() - tEncodeStart
      uni.$logger?.local?.info('[AiAssistant][HTTP] request POST /api/audio/chat')
      let res
      const tReqStart = Date.now()
      try {
        res = await client.request({
          url: '/api/audio/chat',
          method: 'POST',
          timeout: 300000,
          data: {
            message: '将语音的内容转写,并提取客户提出的问题',
            audio: audioBase64,
            sample_rate: 16000,
            sample_width: 2,
            channels: 1
          }
        })
      } catch (e) {
        const errMsg = e?.errMsg || e?.message || ''
        this.uploadCostMs = Date.now() - tReqStart
        uni.$logger?.local?.error(
          `[AiAssistant][HTTP] request /api/audio/chat failed: ${errMsg} ${serializeIfObject(e)}`
        )
        throw e
      }
      this.uploadCostMs = Date.now() - tReqStart

      const status = res?.statusCode
      if (typeof status === 'number' && (status < 200 || status >= 300)) {
        const bodyPreview = (() => {
          try {
            return JSON.stringify(res?.data ?? {}).slice(0, 800)
          } catch {
            return String(res?.data ?? '')
          }
        })()
        uni.$logger?.local?.error(
          `[AiAssistant][HTTP] response /api/audio/chat non-2xx status=${status} body=${bodyPreview}`
        )
        throw new Error(`HTTP ${status}`)
      }
      const tParseStart = Date.now()
      const payload = res?.data
      try {
        const preview = JSON.stringify(payload || {}).slice(0, 800)
        uni.$logger?.local?.info(`[AiAssistant][HTTP] response /api/audio/chat => ${preview}`)
        // #ifdef APP-PLUS
        if (typeof plus !== 'undefined' && plus?.console?.log) {
          plus.console.log(`[AiAssistant][HTTP] response /api/audio/chat => ${preview}`)
        }
        // #endif
      } catch (e) {
        uni.$logger?.local?.warn('[AiAssistant][HTTP] response stringify failed')
      }
      const list = Array.isArray(payload)
        ? payload
        : payload?.type === 'result' && Array.isArray(payload.content)
          ? payload.content
          : null
      if (!Array.isArray(list)) {
        throw new Error(`AI 接口返回异常: ${JSON.stringify(payload || {}).slice(0, 240)}`)
      }

      this.qaList = list
        .map((x) => ({
          query: x?.query || '',
          answer: x?.answer || ''
        }))
        .filter((x) => x.query || x.answer)
      this.resultCostMs = Date.now() - tParseStart
      this.totalCostMs = Date.now() - tTotalStart

      if (!this.qaList.length) {
        throw new Error('AI 未返回问题列表')
      }

      // 多问题:按原型弹出选择 popup(隐藏结果面板)
      if (this.qaList.length > 1) {
        this.selectedQaIndex = -1
        this.questionMenuVisible = true
        this.resultVisible = false
      } else {
        // 单问题:直接进入答案页
        this.questionMenuVisible = false
        this.selectedQaIndex = 0
        this.resultVisible = true
      }
      // #endif
    },
    closeResult() {
      this.resultVisible = false
      getAiStreamSession().cancel()
      if (this.uiState === 'thinking') {
        this.uiState = 'idle'
      }
      this.streamStatus = 'idle'
    },
    showAbnormalBubble(message) {
      this.abnormalMessage = message
      this.abnormalBubbleVisible = true
      uni.$logger?.local?.warn(`[AiAssistant] ${message}`)
    },
    resetAll() {
      this.clearGuideTimer()
      getAiStreamSession().cancel()
      this.uiState = 'idle'
      this.menuVisible = false
      this.introVisible = false
      this.questionMenuVisible = false
      this.resultVisible = false
      this.streamMarkdown = ''
      this.streamStatus = 'idle'
      this.pcmCostMs = 0
      this.encodeCostMs = 0
      this.uploadCostMs = 0
      this.resultCostMs = 0
      this.totalCostMs = 0
      this.guideVisible = false
      this.guideFading = false
      this.abnormalBubbleVisible = false
      this._pendingBallTap = false
      this.qaList = []
      this.selectedQaIndex = -1
    }
  }
}
</script>

<style lang="scss" scoped>
.ai-ball-root {
  position: fixed;
  right: 32rpx;
  bottom: 32rpx;
  z-index: 100015;
  pointer-events: none;
}

.guide-bubble {
  position: absolute;
  right: 0;
  bottom: 110rpx;
  max-width: 520rpx;
  padding: 18rpx 22rpx;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16rpx;
  pointer-events: none;
  opacity: 1;
  transition: all 0.3s ease;
  border: 1px solid rgba(255, 255, 255, 0.8);
  box-shadow: 0 12rpx 32rpx rgba(0, 0, 0, 0.15);
  &.guide-fade {
    opacity: 0;
  }
}
.guide-text {
  color: #1f2937;
  font-size: 24rpx;
  line-height: 1.5;
}
.guide-arrow {
  position: absolute;
  bottom: -10rpx;
  right: 44rpx;
  width: 20rpx;
  height: 20rpx;
  background: rgba(255, 255, 255, 0.95);
  transform: rotate(45deg);
  box-shadow: 4rpx 4rpx 8rpx rgba(0, 0, 0, 0.05);
}

.abnormal-bubble {
  position: absolute;
  right: 0;
  bottom: 110rpx;
  width: 520rpx;
  padding: 18rpx 18rpx 18rpx 22rpx;
  background: rgba(255, 255, 255, 0.98);
  border: 1px solid rgba(255, 255, 255, 0.8);
  border-radius: 16rpx;
  pointer-events: auto;
  box-shadow: 0 12rpx 32rpx rgba(0, 0, 0, 0.15);
  display: flex;
  align-items: center;
  gap: 14rpx;
}
.abnormal-title {
  display: none;
}
.abnormal-msg {
  display: block;
  flex: 1;
  color: #1f2937;
  font-size: 24rpx;
  line-height: 1.4;
}
.abnormal-hint {
  display: none;
}

/* FAB - AI 悬浮球(按原型) */
.fab-container {
  position: relative;
  pointer-events: auto;
  height: 112rpx;
  border-radius: 56rpx;
  background: linear-gradient(135deg, #34d399, #0ea5e9);
  box-shadow: 0 12rpx 24rpx rgba(14, 165, 233, 0.4);
  border: 2rpx solid rgba(255, 255, 255, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  transition: all 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.fab-container.state-idle {
  width: 112rpx;
  animation: fab-breathing 3s infinite ease-in-out;
}
.fab-container.state-active {
  width: 520rpx;
  background: linear-gradient(135deg, #3b82f6, #06b6d4);
  box-shadow: 0 12rpx 32rpx rgba(59, 130, 246, 0.5);
  animation: none;
}
.fab-content-idle {
  display: flex;
  align-items: center;
  justify-content: center;
}
.ai-text {
  color: #fff;
  font-size: 40rpx;
  font-weight: 800;
  text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.2);
}
.fab-content-active {
  display: none;
  width: 100%;
  padding: 0 24rpx 0 28rpx;
  box-sizing: border-box;
  align-items: center;
  gap: 18rpx;
  white-space: nowrap;
  color: #fff;
  font-size: 26rpx;
  font-weight: 500;
}
.fab-container.state-active .fab-content-idle {
  display: none;
}
.fab-container.state-active .fab-content-active {
  display: flex;
}
.fab-active-text {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}
.fab-cancel-btn {
  width: 52rpx;
  height: 52rpx;
  border-radius: 26rpx;
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.25);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24rpx;
  flex-shrink: 0;
}

@keyframes fab-breathing {
  0% {
    transform: scale(1);
    box-shadow: 0 8rpx 24rpx rgba(14, 165, 233, 0.4);
  }
  50% {
    transform: scale(1.05);
    box-shadow: 0 16rpx 36rpx rgba(52, 211, 153, 0.6);
  }
  100% {
    transform: scale(1);
    box-shadow: 0 8rpx 24rpx rgba(14, 165, 233, 0.4);
  }
}

/* 声波动效 */
.sound-wave {
  display: flex;
  gap: 8rpx;
  height: 48rpx;
  align-items: center;
}
.sound-wave .bar {
  width: 6rpx;
  background: #fff;
  border-radius: 4rpx;
  animation: fab-wave 1s infinite ease-in-out;
}
.sound-wave .bar:nth-child(1) {
  height: 24rpx;
  animation-delay: 0s;
}
.sound-wave .bar:nth-child(2) {
  height: 48rpx;
  animation-delay: 0.2s;
}
.sound-wave .bar:nth-child(3) {
  height: 32rpx;
  animation-delay: 0.4s;
}
.sound-wave .bar:nth-child(4) {
  height: 16rpx;
  animation-delay: 0.6s;
}
@keyframes fab-wave {
  0%,
  100% {
    transform: scaleY(0.4);
  }
  50% {
    transform: scaleY(1);
  }
}

/* Popup(按原型:锚定 FAB 上方;不使用全屏 overlay) */
.popup-menu {
  position: absolute;
  right: 32rpx;
  bottom: 220rpx; /* FAB 上方 */
  width: 620rpx;
  background: rgba(255, 255, 255, 0.98);
  border: 1px solid rgba(255, 255, 255, 0.8);
  border-radius: 20rpx;
  box-shadow: 0 20rpx 40rpx rgba(0, 0, 0, 0.15);
  padding: 24rpx;
  backdrop-filter: blur(16px);
}
.popup-header-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16rpx;
  margin-bottom: 18rpx;
}
.popup-header {
  font-weight: 700;
  color: #111827;
  font-size: 30rpx;
}
.popup-close-btn {
  width: 56rpx;
  height: 56rpx;
  border-radius: 28rpx;
  background: #f3f4f6;
  color: #4b5563;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.popup-btn {
  width: 100%;
  border-radius: 16rpx;
  background: #f4f4f5;
  padding: 22rpx 24rpx;
  margin-bottom: 16rpx;
}
.popup-btn.primary {
  background: linear-gradient(135deg, #e0f2fe, #dcfce7);
  border: 1px solid #bae6fd;
}
.popup-btn-title {
  color: #1f2937;
  font-size: 28rpx;
  font-weight: 600;
  line-height: 1.35;
}
.popup-divider {
  height: 2rpx;
  background: #e5e7eb;
  margin: 18rpx 0;
}
.popup-skip {
  display: flex;
  align-items: center;
  gap: 16rpx;
}
.popup-skip-text {
  font-size: 24rpx;
  color: #6b7280;
  line-height: 1.35;
}
.popup-body {
  display: block;
  color: #4b5563;
  font-size: 26rpx;
  line-height: 1.7;
  white-space: pre-line;
}

.menu-mask {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  z-index: 100016;
  pointer-events: auto;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.menu-panel,
.intro-panel {
  width: 100%;
  padding: 32rpx 32rpx calc(32rpx + env(safe-area-inset-bottom));
  background: #fff;
  border-radius: 24rpx 24rpx 0 0;
  box-sizing: border-box;
}
.menu-title {
  display: block;
  font-size: 32rpx;
  font-weight: 700;
  color: #1a1a1a;
  margin-bottom: 24rpx;
}
.menu-item {
  padding: 24rpx;
  border-radius: 16rpx;
  background: #f5f5f5;
  margin-bottom: 16rpx;
  &.primary {
    background: linear-gradient(135deg, #ede9fe, #e0e7ff);
    border: 1px solid #c7d2fe;
  }
}
.menu-item-title {
  display: block;
  font-size: 30rpx;
  font-weight: 600;
  color: #1a1a1a;
}
.menu-item-desc {
  display: block;
  margin-top: 8rpx;
  font-size: 24rpx;
  color: #6b7280;
  line-height: 1.4;
}
.menu-skip {
  display: flex;
  align-items: center;
  gap: 16rpx;
  padding: 20rpx 8rpx;
  margin-top: 8rpx;
}
.skip-check {
  width: 36rpx;
  height: 36rpx;
  border-radius: 8rpx;
  border: 2rpx solid #9ca3af;
  flex-shrink: 0;
  &.checked {
    background: #6366f1;
    border-color: #6366f1;
    position: relative;
    &::after {
      content: '';
      position: absolute;
      left: 10rpx;
      top: 4rpx;
      width: 10rpx;
      height: 18rpx;
      border: solid #fff;
      border-width: 0 3rpx 3rpx 0;
      transform: rotate(45deg);
    }
  }
}
.skip-text {
  font-size: 26rpx;
  color: #4b5563;
}
.menu-close {
  display: block;
  text-align: center;
  margin-top: 24rpx;
  font-size: 28rpx;
  color: #6b7280;
  &.center {
    margin-top: 32rpx;
  }
}
.intro-body {
  display: block;
  font-size: 26rpx;
  color: #4b5563;
  line-height: 1.7;
  white-space: pre-line;
}

.result-panel {
  width: 100%;
  max-height: 78vh;
  background: #fff;
  border-radius: 24rpx 24rpx 0 0;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  padding-bottom: env(safe-area-inset-bottom);
}
.result-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 28rpx 32rpx 16rpx;
  border-bottom: 1px solid #eee;
}
.result-head-main {
  flex: 1;
  min-width: 0;
}
.result-title {
  display: block;
  font-size: 32rpx;
  font-weight: 700;
  color: #111827;
}
.result-status {
  display: block;
  margin-top: 8rpx;
  font-size: 24rpx;
  color: #6366f1;
}
.result-close {
  font-size: 28rpx;
  color: #6b7280;
  padding: 8rpx 0 8rpx 24rpx;
}
.result-scroll {
  flex: 1;
  max-height: 62vh;
  padding: 20rpx 24rpx 32rpx;
  box-sizing: border-box;
}
.qa-root {
  padding: 8rpx 8rpx 24rpx;
}
.qa-title {
  display: block;
  font-size: 26rpx;
  font-weight: 600;
  color: #111827;
  margin: 8rpx 0 12rpx;
}
.qa-list {
  display: flex;
  flex-direction: column;
  gap: 12rpx;
}
.qa-item {
  padding: 16rpx 18rpx;
  border-radius: 14rpx;
  background: #f3f4f6;
  border: 1px solid #e5e7eb;
}
.qa-item.active {
  background: #eef2ff;
  border-color: #c7d2fe;
}
.qa-q {
  color: #111827;
  font-size: 26rpx;
  line-height: 1.45;
}
.qa-answer {
  margin-top: 18rpx;
  padding: 16rpx 18rpx;
  border-radius: 14rpx;
  background: #ffffff;
  border: 1px solid #e5e7eb;
}
.qa-answer-title {
  display: block;
  color: #4f46e5;
  font-size: 24rpx;
  font-weight: 600;
  margin-bottom: 10rpx;
}
.qa-hint {
  display: block;
  margin-top: 14rpx;
  padding: 12rpx 16rpx;
  border-radius: 12rpx;
  background: #f9fafb;
  border: 1px dashed #e5e7eb;
  color: #6b7280;
  font-size: 24rpx;
}
.qa-answer-page {
  padding-top: 8rpx;
}
.qa-answer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16rpx;
  margin-bottom: 14rpx;
}
.qa-back {
  color: #4f46e5;
  font-size: 24rpx;
  padding: 10rpx 14rpx;
  border-radius: 999rpx;
  background: #eef2ff;
  border: 1px solid #c7d2fe;
}
.qa-answer-page .qa-answer-title {
  margin-bottom: 0;
}
.qa-a {
  color: #111827;
  font-size: 26rpx;
  line-height: 1.6;
  white-space: pre-wrap;
}
.result-loading {
  padding: 48rpx 0;
  text-align: center;
  color: #9ca3af;
  font-size: 26rpx;
}
</style>