<script setup>
import { ref, reactive, onMounted, watch, nextTick } from "vue";
import { useRoute } from "vue-router";
// VR
import "aframe";
// i18n
import { useI18n } from "vue-i18n";
// Axios
import axios from "axios";
// Moment
// import moment from "moment";
// Animate
import "animate.css";
// Swiper
// import { Swiper, SwiperSlide } from "swiper/vue";
// import { Navigation, Autoplay } from "swiper/modules";
// import "swiper/css";
// import "swiper/css/navigation";
// Recorder
import Recorder from "recorder-core";
import "recorder-core/src/engine/mp3";
import "recorder-core/src/engine/mp3-engine";
// QR Code
// import { QrcodeStream, QrcodeDropZone, QrcodeCapture } from "vue-qrcode-reader";
// Components
import Navbar from "../components/Navbar.vue";
// import TicketPurchase from "../components/TicketPurchase.vue";

const { t, locale } = useI18n();

const route = useRoute();
const routeParam = ref(null);

onMounted(() => {
  // 取得當前路由參數
  routeParam.value = route.path.replace("/", "");
  console.log("網址參數", routeParam.value);
  window.addEventListener("resize", handleResize);
});

const userMessage = ref("");
// let modules = [Navigation, Autoplay]; // Swiper

// AI 客服回覆訊息
let messages = ref([]);

watch(messages.value, (val) => {
  console.log("messages", val);
  scrollToBottom();
  updateMenuHeight();
});

let ad = ref({}); // 彈跳視窗廣告
let qaQuery = reactive([]);

const chatArea = ref(null); // 對話框

// 滾動到對話框底部
const scrollToBottom = () => {
  setTimeout(() => {
    console.log("chatArea.value", chatArea.value.scrollHeight);

    chatArea.value.scrollTo({
      top: chatArea.value.scrollHeight,
      behavior: "smooth",
    });
  }, 100);
};

let questionList = ref([]);

// 傳送訊息
async function sendMessage() {
  console.log("sendMessage", userMessage.value);

  // if (userMessage.value !== "") {
  //   questionList.value.push({
  //     q: userMessage.value,
  //     a: "",
  //   });
  // }

  let message = userMessage.value;
  //   userMessage.value = "";

  let url = `https://cmm.ai:8081/answer_with_history?question=${userMessage.value}`;

  if (userMessage.value !== "") {
    questionList.value.push({
      q: message,
      a: "",
    });

    // 使用者訊息
    messages.value.push({
      label: "text",
      author: "user",
      body: message,
    });

    userMessage.value = "";

    scrollToBottom();

    messages.value.push({
      label: "text",
      author: "ai",
      body: "回覆中…",
    });
  } else {
    return;
  }

  try {
    const response = await axios.post(url, questionList.value);
    console.log("response", response);

    if (response.status === 200) {
      console.log("video_cache", response.data.video_cache);

      // 回傳影片
      if (response.data.Answer !== "line_oa" && response.data.video_cache) {
        if (response.data.video_cache !== "") {
          videoSrc.value = response.data.video_cache;
        }

        // 暫停當前音訊
        if (currentAudio.value) {
          currentAudio.value.pause();
          currentAudio.value.currentTime = 0;
          currentAudio.value = null;
        }

        setTimeout(() => {
          isVideoPause.value = true;
          videoPlay();
        }, 500);
      } else if (response.data.Answer !== "line_oa") {
        handleTTS(response.data.Answer); // 取得語音回覆
      }

      messages.value.splice(-1, 1); // 移除回覆中
      setTimeout(() => {
        // AI 客服回傳訊息
        if (response.data.Answer === "line_oa") {
          messages.value.push({
            label: "line",
            author: "ai",
            body: "",
          });
        } else {
          messages.value.push({
            label: "text",
            author: "ai",
            body: response.data.Answer,
          });
        }

        scrollToBottom();
      }, 300);

      questionList.value[questionList.value.length - 1].a =
        response.data.Answer;
      console.log("questionList.value", questionList.value);
    }

    console.log("messages.value", messages.value);
  } catch (error) {
    console.log("error", error);
  }
}

let hideMenu = ref(true); // 底部選單
let showInput = ref(true); // 輸入框

function getLang() {
  let lang = localStorage.getItem("lang");
  let langVal = "";

  switch (lang) {
    case "zh-tw":
      langVal = "ch";
      break;
    case "en-us":
      langVal = "en";
      break;

    default:
      break;
  }

  return langVal;
}

let showAd = ref(false);

// 取得廣告
async function setAd() {
  let lang = getLang();
  console.log("lang", lang);
  let url = `https://cmm.ai:9101/ad?language=${lang}`;

  try {
    const response = await axios.get(url);
    console.log("Ad response", response);

    ad.value = response.data.data.body;
    console.log("Ad", ad.value);
  } catch (error) {
    console.log("error", error);
  }
}

let video = ref(null);

function videoPlay() {
  video.value.load();
  video.value.play();
}

// 底部選單
const menu = ref(null);
const menuHeight = ref(0);

let isRotate = ref(false);
let isLanguagePage = ref(true);
let selectLang = ref("");
let chatLoading = ref(true);

watch(isLanguagePage, (val) => {
  if (!val) {
    setTimeout(updateMenuHeight, 500);
  }
});

const handleResize = () => {
  updateMenuHeight();
};

// 取得底部選單高度
const updateMenuHeight = () => {
  nextTick(() => {
    if (menu.value) {
      menuHeight.value = menu.value.clientHeight;
      chatLoading.value = false;
    }
  });
};

function chooseLang(lang) {
  console.log("選擇語言：", lang);
  selectLang.value = lang;
  isLanguagePage.value = false;
  locale.value = lang; // i18n locale
  localStorage.setItem("lang", lang);

  messages.value.push({
    label: "text",
    author: "ai",
    body: t("prologue"),
  });

  messages.value.push({
    label: "text",
    author: "ai",
    body: t("service"),
  });

  // 判斷語言修改 title
  const language = localStorage.getItem("lang") || "zh-tw";
  console.log("language", language);
  if (language === "zh-tw") {
    document.title = "ChoozMo AI智能客服";
  } else if (language === "en-us") {
    document.title = "ChoozMo AI Intelligent Customer Service";
  }

  setAd();
  handleClick();

  // 影片路徑
  loadVideoSources();
  let sources;
  console.log("videoSources.value", videoSources.value);
  if (language === "zh-tw") {
    sources = videoSources.value;
  } else if (language === "en-us") {
    sources = videoSourcesEn.value;
  }

  const randomIndex = Math.floor(Math.random() * sources.length);
  videoSrc.value = sources[randomIndex];
  hideAnchorPrologue.value = true;
  videoIndex.value = randomIndex + 1;

  console.log("messages.value", messages.value);

  setTimeout(() => {
    showAd.value = true;
    videoPlay();
  }, 500);
}

// 動態引入視頻文件
const videoSources = ref([]); // 開場白影片(中)
const videoSourcesEn = ref([]); // 開場白影片(英)
const videoMuteSources = ref([]); // 點頭影片(靜音)
const videoSpeakSources = ref([]); // 動嘴型影片

const loadVideoSources = async () => {
  videoSources.value = ["https://cmm.ai/chatbot/video/start.mp4"];
  videoSourcesEn.value = [""];
  videoMuteSources.value = ["https://cmm.ai/chatbot/video/mute.mp4"];
  videoSpeakSources.value = ["https://cmm.ai/chatbot/video/speak.mp4"];
};

let videoSrc = ref("");
let hideAnchorPrologue = ref(false); // 顯示開場白 or 點頭影片
let videoIndex = ref(null); // 影片編號

// 計算使用次數
async function handleClick() {
  let url = "https://cmm.ai:9101/click";
  try {
    const response = await axios.get(url);
    console.log("Click", response);
  } catch (error) {
    console.log("error", error);
  }
}

let langList = reactive([
  {
    lang: "中文",
    value: "zh-tw",
  },
  // {
  //   lang: "English",
  //   value: "en-us",
  // },
  // {
  //   lang: "日本語",
  //   value: "ja-jp",
  // },
  // {
  //   lang: "한국어",
  //   value: "ko-kr",
  // },
]);

function getImageUrl(name) {
  return new URL(`../assets/img/icon/${name}`, import.meta.url).href;
}

let showAnchor = ref(false); // AI 主播影片
let currentAudio = ref(null); // 當前音訊
let audioDuration = ref(null); // 音訊秒數

// 文字轉語音 (TTS)
async function handleTTS(message) {
  console.log("handleTTS", message);

  let audioLang; // 音訊語言
  let lang = localStorage.getItem("lang");
  console.log("lang", lang);

  switch (lang) {
    case "zh-tw":
      audioLang = "cmn-TW";
      break;
    case "en-us":
      audioLang = "en-US";
      break;
    case "ja-jp":
      audioLang = "ja-JP";
      break;
    case "ko-kr":
      audioLang = "ko-KR";
      break;
    default:
      break;
  }

  let url = `https://cmm.ai:9001/gcp/text-to-speech?language_code=${audioLang}&gender=female`;

  const formData = new FormData();
  formData.append("text", message);

  try {
    const response = await axios.post(url, formData, { responseType: "blob" });
    console.log("TTS response", response);

    const blob = new Blob([response.data], { type: "audio/mp3" });
    const audioUrl = URL.createObjectURL(blob);
    console.log("audioUrl", audioUrl);

    cutVideo(); // 剪接影片

    // 取得 mp3 音訊秒數
    // audio.addEventListener("loadedmetadata", function () {
    //   audioDuration.value = audio.duration;
    //   cutVideo();
    // });

    // 暫停當前音訊
    if (currentAudio.value) {
      currentAudio.value.pause();
      currentAudio.value.currentTime = 0;
    }

    // 播放音檔
    currentAudio.value = new Audio(audioUrl);
  } catch (error) {
    console.log("error", error);
  }
}

let videoLoading = ref(false);
let isAudioPlaying = ref(false); // 音訊播放狀態

// 取得語音回覆 mp4
async function cutVideo() {
  videoSrc.value = videoSpeakSources.value[videoIndex.value - 1];
  video.value.load(); // 重新讀取影片

  // 影片和音訊加載完成後播放
  video.value.oncanplay = () => {
    setTimeout(() => {
      // 監聽音訊播放結束
      currentAudio.value.addEventListener("ended", onAudioEnded);

      // 監聽音訊播放狀態
      currentAudio.value.addEventListener("play", onAudioPlay);
      currentAudio.value.addEventListener("pause", onAudioPause);

      video.value.play(); // 播放影片
      currentAudio.value.currentTime = 0; // 重置時間
      currentAudio.value.play(); // 播放音訊
      isVideoPause.value = true;
      videoLoading.value = false;
    }, 500);
  };
}

// 音訊結束後暫停影片播放
const onAudioEnded = () => {
  video.value.pause();
};

// 判斷音訊是否為播放狀態
const onAudioPlay = () => {
  isAudioPlaying.value = true;
  console.log("isAudioPlaying.value", isAudioPlaying.value);
};

const onAudioPause = () => {
  isAudioPlaying.value = false;
  isVideoPause.value = false;
  console.log("isAudioPlaying.value", isAudioPlaying.value);
};

const audioURL = ref(null);
const audioFile = ref(null); // 音訊檔案

let recordTime = ref(0); // 錄音時間
let isRecording = ref(false); // 錄音狀態
let timer;

// 語音轉文字
async function handleAudioToText() {
  isRecording.value = false;
  let audioLang; // 音訊語言
  let lang = localStorage.getItem("lang");
  console.log("lang", lang);

  switch (lang) {
    case "zh-tw":
      audioLang = "cmn-Hant-TW";
      break;
    case "en-us":
      audioLang = "en-US";
      break;
    case "ja-jp":
      audioLang = "ja-JP";
      break;
    case "ko-kr":
      audioLang = "ko-KR";
      break;
    default:
      break;
  }

  let url = `https://cmm.ai:9001/gcp/speech-to-text?language_code=${audioLang}`;

  const formData = new FormData();
  formData.append("file", audioFile.value);

  try {
    console.log("audioFile.value", audioFile.value);
    const response = await axios.post(url, formData);
    console.log("語音轉文字 response", response);
    // showAnchor.value = false; // 關閉主播視窗

    userMessage.value = response.data[0];
    // handleTTS(userMessage.value); // 取得語音回覆

    if (response.data[0] && response.data[0] !== "") {
      sendMessage(); // 傳送使用者訊息
    } else {
      if (showAnchor.value) {
        alert("語音辨識有誤，請重新錄製。");
        videoLoading.value = false;
        return;
      } else {
        messages.value.push({
          label: "text",
          author: "user",
          body: "語音辨識有誤，請重新錄製。",
        });
      }
    }
  } catch (error) {
    console.log("error", error);
  }
}

// 語音轉文字 (使用 recorder-core 錄音)
let rec, wave;

// 調用 open 請求錄音權限
let recOpen = function (success) {
  rec = Recorder({
    type: "mp3",
    sampleRate: 16000,
    bitRate: 16,
    onProcess: function (
      buffers,
      powerLevel,
      bufferDuration,
      bufferSampleRate,
      newBufferIdx,
      asyncEnd
    ) {
      wave &&
        wave.input(buffers[buffers.length - 1], powerLevel, bufferSampleRate);
    },
  });

  rec.open(
    function () {
      if (Recorder.WaveView) wave = Recorder.WaveView({ elem: ".recwave" });
      success && success();
    },
    function (msg, isUserNotAllow) {
      // 使用者未授權或不支援
      console.log((isUserNotAllow ? "UserNotAllow，" : "") + "無法錄音:" + msg);
    }
  );
};

/** 開始錄音 **/
function recStart() {
  togglePause("pause"); // 暫停影片音訊
  // 需先呼叫 recOpen() 開啟錄音後才能調用 start、stop 方法
  console.log("開始錄音");

  recOpen(function () {
    isRecording.value = true;

    // 開始計時
    timer = setInterval(() => {
      recordTime.value += 1;
    }, 1000);

    rec.start();
  });
}

/** 結束錄音 **/
function recStop() {
  videoLoading.value = true;
  rec.stop(
    function (blob, duration) {
      // 利用 URL 產生本地檔案位址，不用時需要 revokeObjectURL
      let localUrl = (window.URL || webkitURL).createObjectURL(blob); // 該 url 只能本地端使用 (例如給 audio.src 進行播放，或是給 a.href download 進行下載)
      console.log(blob, localUrl, "時長:" + duration + "ms");
      // rec.close(); // 釋放錄音資源 (若不釋放系統或瀏覽器將持續提示在錄音中)
      // rec = null;

      // 將 Blob 轉換為 File 對象
      audioFile.value = new File([blob], "recording.mp3", {
        type: "audio/mp3",
      });

      console.log("audioFile", audioFile.value);

      rec.close(); // 釋放錄音資源 (若不釋放系統或瀏覽器將持續提示在錄音中)
      rec = null;

      if (recordTime.value !== 0) {
        handleAudioToText(); // 語音轉文字
      } else {
        isRecording.value = false;
      }

      clearInterval(timer); // 清空計時秒數
      recordTime.value = 0;
    },
    function (msg) {
      console.log("錄音失敗：" + msg);
      rec.close(); // 可以透過 stop 方法的第 3 個參數來自動呼叫 close
      rec = null;
    }
  );
}

let videoCacheData = ref({});

async function getVideoCache(messages) {
  let url = `https://cmm.ai:9101/video_cache?client_message=${messages}`;

  try {
    const response = await axios.post(url);
    console.log("response", response);
    console.log("response.status", response.status);
    if (response.data.state === 200) {
      videoCacheData.value = response.data.message[0];
      console.log("videoCacheData.value", videoCacheData.value);
      return true;
    } else {
      return false;
    }
  } catch (error) {
    console.log("error", error);
  }
}

// 播放 Video Cache
function handleVideoCache() {
  console.log("播放 Video Cache", videoCacheData.value);
  // AI 客服回傳訊息
  messages.value.push({
    label: "text",
    author: "ai",
    body: videoCacheData.value.answer,
  });

  // 播放 Cache 影片
  videoSrc.value = `https://cmm.ai:9101${videoCacheData.value.video_url}`;
  video.value.load();

  // 清空音訊
  if (currentAudio.value) {
    currentAudio.value.pause();
    currentAudio.value.currentTime = 0;
    currentAudio.value = null;
  }

  video.value.play();
  isVideoPause.value = true;

  setTimeout(() => {
    videoLoading.value = false;
  }, 1000);
}

// 關閉主播視窗 (結束錄音)
function closeRec() {
  video.value.pause();
  isVideoPause.value = true;
  recordTime.value = 0;
  clearInterval(timer); // 清空計時秒數
  if (isRecording.value) {
    recStop();
  }
  showAnchor.value = false;
}

let isVideoPause = ref(true);

// AI 主播影片播放 & 暫停
function togglePause(val) {
  if (val === "pause") {
    // video.value.pause();
    isVideoPause.value = false;

    if (video.value) {
      video.value.pause();
    }
    if (currentAudio.value) {
      currentAudio.value.pause(); // 暫停音訊
    }
  } else {
    isVideoPause.value = true;

    if (video.value) {
      video.value.play();
    }
    if (currentAudio.value) {
      currentAudio.value.play(); // 播放音訊
      currentAudio.value.addEventListener("ended", onAudioEnded);
    }
  }
}

// 回傳未收錄問題
async function messageNotInCache(question, answer) {
  let url = `https://cmm.ai:9101/message_not_in_cache?question=${question}&answer=${answer}&client_id=0`;

  try {
    const response = await axios.post(url);
    console.log("messageNotInCache response", response);
  } catch (error) {
    console.log("error", error);
  }
}
</script>

<template>
  <Navbar />
  <main>
    <div v-if="isLanguagePage" class="lang-content">
      <button
        v-for="(item, index) in langList"
        :key="index"
        @click="chooseLang(item.value)"
        class="main-btn"
      >
        {{ item.lang }}
      </button>
    </div>
    <div v-else class="main-containar">
      <div class="video-content">
        <video ref="video" preload playsinline>
          <source :src="videoSrc" type="video/mp4" />
          <!-- <source src="../assets/video/start_1.mp4" type="video/mp4" /> -->
          Your browser does not support the video tag.
        </video>

        <div v-if="videoLoading" class="video-progress">
          <v-progress-circular
            color="primary"
            indeterminate
          ></v-progress-circular>
        </div>

        <button
          v-if="isVideoPause"
          @click="togglePause('pause')"
          class="control-btn"
        >
          <img src="../assets/img/pause-button.png" alt="" />
        </button>

        <button v-else @click="togglePause('play')" class="control-btn">
          <img src="../assets/img/play-button.png" alt="" />
        </button>

        <div class="control-item">
          <div class="d-flex flex-column align-center">
            <audio v-if="audioURL" :src="audioURL" controls></audio>
            <!-- <p
              v-if="!isRecording"
              class="mb-3 text-center"
              v-html="t('system_construction')"
            ></p> -->
            <p v-if="!isRecording" class="mb-3">
              {{ t("tap_to_record") }}
            </p>
            <p v-else class="mb-3">錄音中：{{ recordTime }} 秒</p>
            <!-- 錄音按鈕 -->
            <v-btn
              v-if="!isRecording"
              @click="recStart"
              icon="mdi-circle"
              size="large"
            >
              <v-icon icon="mdi-circle" color="red" size="large"></v-icon>
            </v-btn>

            <v-btn
              v-else
              @click="recStop"
              icon="mdi-circle"
              size="large"
              color="success"
            >
              <v-icon icon="mdi-square" size="large"></v-icon>
            </v-btn>
          </div>
        </div>
      </div>

      <div class="chat-content">
        <div class="headline">
          <h1>{{ t("title") }}</h1>
          <!-- <button @click="isRotate = !isRotate">
          <img
            :class="{ rotate: isRotate }"
            src="../assets/img/angles-up-solid.svg"
            alt=""
          />
        </button> -->
        </div>
        <section
          ref="chatArea"
          class="chat-area"
          :class="{ 'area-open': isRotate, 'hide-menu': hideMenu }"
          :style="{
            paddingBottom: !hideMenu ? menuHeight + 20 + 'px' : '70px',
          }"
        >
          <div v-for="message in messages" class="message-content">
            <p
              v-if="message.label === 'text'"
              class="message animate__animated"
              :class="{
                'message-out': message.author === 'user',
                'message-in': message.author !== 'user',
                animate__fadeInRight: message.author === 'user',
                animate__fadeInLeft: message.author !== 'user',
              }"
              v-html="message.body"
            ></p>

            <div v-if="message.label === 'line'" class="line-item">
              <img
                src="../assets/img/line_oa_qrcode.png"
                alt="Line OA Qrcode"
              />
            </div>
          </div>
        </section>

        <!-- 底部選單 -->
        <div ref="menu" class="menu">
          <div class="d-flex align-center position-relative">
            <div class="w-100 d-flex align-center justify-center">
              <button
                v-if="!showInput"
                @click="
                  hideMenu = true;
                  showInput = true;
                "
                class="d-flex align-center question-btn"
              >
                <img
                  class="me-2"
                  src="../assets/img/icon/素材-03.png"
                  alt=""
                  width="45"
                />
                {{ t("question") }}
              </button>

              <!-- 對話輸入框 -->
              <form
                v-else
                @submit.prevent="sendMessage()"
                class="chat-inputs"
                :class="{ 'd-none': !showInput }"
              >
                <input
                  v-model="userMessage"
                  type="text"
                  placeholder="Type a message..."
                />
                <button type="submit" class="submit">
                  <img
                    width="20"
                    src="../assets/img/paper-plane-solid.svg"
                    alt=""
                  />
                </button>
              </form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </main>
</template>

<style scoped lang="scss">
main {
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  /* background-color: var(--sub-color); */
  background-color: rgba(0, 0, 0, 0.6);
  background-blend-mode: multiply;
  background-image: url("@/assets/img/banner.jpg");
  background-size: cover;
  background-position: center center;
}

.main-btn {
  padding: 16px 70px;
  font-size: 22px;
  font-weight: 600;
  border: none;
  border-radius: 100px;
  letter-spacing: 2px;
  color: white;
  background-color: var(--main-color);
  cursor: pointer;
  transition: all 0.3s;
  &:hover {
    opacity: 0.7;
  }
}

.lang-content {
  display: flex;
  flex-direction: column;
  .main-btn {
    margin-bottom: 40px;
    &:last-child {
      margin-bottom: 0;
    }
  }
}

.main-containar {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;

  .video-content {
    height: 73vh;
    display: flex;
    justify-content: center;
    transform: scale(1);

    video {
      width: 100%;
      height: 100%;
      position: fixed;
      top: 55px;
      left: 0;
      right: 0;
      z-index: 10;

      @media (max-width: 575px) {
        top: 15px;
      }

      @media (max-width: 375px) {
        top: 40px;
      }
    }

    .control-btn {
      width: 33px;
      height: 33px;
      display: flex;
      align-items: center;
      justify-content: center;
      position: absolute;
      z-index: 50;
      top: 80px;
      right: 7px;
      background: var(--main-color);
      border: none;
      border-radius: 100px;

      img {
        width: 25px;
        filter: invert(100%) sepia(0%) saturate(0%) hue-rotate(93deg)
          brightness(103%) contrast(103%);
      }
    }

    .control-item {
      display: flex;
      justify-content: end;
      flex-direction: column;
      z-index: 10;
      position: absolute;
      bottom: 30px;

      @media (max-width: 375px) {
        height: 29vh;
      }

      p {
        color: #fff;
        font-size: 0.875rem;
        letter-spacing: 1px;
        text-shadow: 1px 1px 2px #333;
      }
    }
  }

  .chat-content {
    width: 100%;
    height: 60vh;
    margin-top: -20px;
    position: relative;
    z-index: 100;
    letter-spacing: 1px;
    border-radius: 10px 10px 0 0;

    .headline {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 10px 25px;
      border-radius: 15px 15px 0 0;
      background-color: var(--main-color);

      h1 {
        font-size: 1.125rem;
        font-weight: 500;
        color: white;

        @media (max-width: 375px) {
          font-size: 0.875rem;
        }
      }

      button {
        padding-top: 3px;
        display: flex;
        align-items: center;
        border: none;
        background-color: transparent;
        cursor: pointer;

        img {
          width: 25px;
          height: 20px;
          transition: all 0.3s;
          transform: rotate(0deg);

          &.rotate {
            transform: rotate(180deg);
          }
        }
      }
    }

    .chat-area {
      display: flex;
      flex-direction: column;
      background: var(--sub-color);
      height: 40vh;
      padding: 0 1em 2em;
      overflow-x: hidden;
      overflow-y: auto;
      transition: all 0.3s;

      &.area-open {
        height: 75vh;

        @media (max-width: 400px) {
          height: 67vh;
        }
      }
    }

    .message-content {
      display: flex;
      flex-direction: column;

      .message {
        max-width: 45%;
        border-radius: 20px;
        padding: 0.5em 1.2em;
        white-space: pre-line;

        &:first-child {
          margin-top: 0;
        }

        @media (max-width: 600px) {
          max-width: 80%;
          font-size: 0.875rem;
        }

        &.message-out {
          margin-left: auto;
          background: var(--bg-grey);
          color: white;
        }

        &.message-in {
          margin-right: auto;
          background: #f1f0f0;
          color: black;
        }

        &.message-in,
        &.message-out {
          margin-top: 20px;
        }
      }

      .line-item {
        margin-top: 20px;

        img {
          max-width: 300px;
        }
      }
    }

    .chat-inputs {
      width: 100%;
      display: flex;
      padding: 13px 20px;
      background-color: white;

      input {
        width: 100%;
        border: none;

        &:focus-visible {
          outline: none;
        }
      }

      button {
        border: none;
        background: white;
        cursor: pointer;
        &:hover {
          img {
            opacity: 0.8;
          }
        }

        img {
          padding-top: 5px;
          transition: all 0.3s;
          filter: invert(16%) sepia(40%) saturate(4127%) hue-rotate(286deg)
            brightness(108%) contrast(122%);
        }
      }

      ::placeholder {
        font-size: 1rem;
        font-weight: 500;
        color: var(--main-color);
        opacity: 1; /* Firefox */
      }

      ::-ms-input-placeholder {
        /* Edge 12 -18 */
        color: var(--sub-color);
      }
    }
  }
}

.video-progress {
  position: absolute;
  left: 50%;
  top: 40%;
  z-index: 100;
  transform: translate(-50%, -50%);
}

.v-expansion-panel-text {
  font-size: 0.875rem;
  line-height: 1.7;
}

/* 底部選單 */
.menu {
  position: fixed;
  z-index: 300;
  left: 0;
  bottom: 0px;
  right: 0;
  color: var(--text-color);
  background-color: white;

  &.hide-menu table {
    height: 0;
    z-index: -1;
  }

  .icon {
    width: 80px;

    @media (max-width: 767px) {
      width: 50px;
    }
  }

  .question-btn {
    color: var(--main-color);
    font-size: 1.25rem;
    font-weight: 500;
    letter-spacing: 0.1rem;

    @media (max-width: 414px) {
      font-size: 1rem;
    }
  }
}
</style>
