/* --------------------------------------------------
  message-bubble.css

  TechF2F AI 聊天室樣式

  ▸ 品牌色彩變數
  ▸ 通用樣式
  ▸ AI 視窗容器
  ▸ 開關按鈕
  ▸ 聊天視窗
  ▸ 訊息顯示區塊
  ▸ Tags 樣式
  ▸ Card 樣式
  ▸ 輸入與錄音區
  ▸ 錄音動畫
  ▸ 滑動提示
  ▸ 載入動畫
  ▸ 參考資料列表
  ▸ Tooltip
  ▸ 響應式設定
-------------------------------------------------- */

/* ==================================================
   品牌色彩變數
================================================== */

:root {
  /* TechF2F 品牌主色 */
  --main-color: #651dda;
  --sub-color: #5415bd;

  /* 品牌點綴色 */
  --brand-cyan: #8ce4f2;
  --brand-blue: #b7dcfa;
  --brand-lilac: #e6c5ff;

  /* 文字顏色 */
  --text-color: #34323f;
  --muted-color: #85899a;
  --placeholder-color: #9a9daa;

  /* 背景顏色 */
  --bg-color: #ffffff;
  --soft-bg-color: #f8f7fc;
  --lighter-bg-color: #fbfaff;

  /* 邊框與陰影 */
  --border-color: #e8e4f0;
  --soft-border-color: #efecf5;
  --focus-color: rgba(101, 29, 218, 0.1);
  --shadow-color: rgba(54, 40, 82, 0.14);
}

/* ==================================================
   通用樣式
================================================== */

.hidden {
  display: none !important;
}

/*
  不使用全站 button 選擇器，
  避免影響網站原本的按鈕樣式。
*/
.ai-container button,
#toggle-button {
  padding: 0;
  font: inherit;
  background: transparent;
  border: 0;
  cursor: pointer;
}

.ai-container button:focus,
#toggle-button:focus {
  outline: none;
}

.ai-container button:focus-visible,
#toggle-button:focus-visible {
  outline: 3px solid rgba(140, 228, 242, 0.75);
  outline-offset: 3px;
}

.scroll {
  position: relative;
  overflow-y: auto;
  max-height: calc(100vh - 200px);

  scrollbar-width: none;
  -ms-overflow-style: none;
  overscroll-behavior: contain;
}

.scroll::-webkit-scrollbar,
.chat-input textarea::-webkit-scrollbar,
.message-container::-webkit-scrollbar {
  display: none;
}

/* ==================================================
   AI 視窗容器
================================================== */

.ai-container {
  position: fixed;
  inset: 0;
  z-index: 9000;

  display: none;
  align-items: flex-end;

  /*
    不使用黑色遮罩，也不使用模糊效果。
    使用者仍可看到並操作原本網站內容。
  */
  background: transparent;
  background-color: transparent;

  -webkit-backdrop-filter: none;
  backdrop-filter: none;

  opacity: 0;
  pointer-events: none;

  transition: opacity 0.25s ease;
  will-change: opacity;
}

/*
  外層不攔截頁面操作，
  聊天視窗本身仍可正常點擊。
*/
.message-container {
  pointer-events: auto;
}

/* ==================================================
   AI 開關按鈕
================================================== */

#toggle-button {
  position: fixed;
  right: 60px;
  bottom: 30px;
  z-index: 10001;

  display: flex;
  align-items: center;
  justify-content: center;

  border-radius: 50%;

  transition:
    transform 0.22s ease,
    filter 0.22s ease;
}

.tooltip.app-tooltip {
  z-index: 10002;
}

#toggle-button:hover {
  transform: translateY(-3px) scale(1.04);
  filter: drop-shadow(0 7px 12px rgba(74, 51, 108, 0.16));
}

#toggle-button:active {
  transform: translateY(-1px) scale(0.98);
}

#toggle-button.dragging {
  transform: none;
  filter: none;
  transition: none;
}

#toggle-button {
  touch-action: none;
}

#toggle-button,
#toggle-button img {
  -webkit-user-drag: none;
  user-select: none;
}

#toggle-button.is-positioned {
  left: clamp(8px, var(--toggle-left), calc(100vw - 96px));
  top: clamp(8px, var(--toggle-top), calc(100vh - 96px));
  right: auto;
  bottom: auto;
}

#toggle-button img {
  display: block;
  width: 80px;
  height: 80px;
  object-fit: contain;
}

/* ==================================================
   聊天視窗
================================================== */

.message-container {
  position: fixed;
  right: 70px;
  bottom: 125px;

  width: 450px;
  max-width: calc(100vw - 40px);

  display: flex;
  flex-direction: column;

  overflow: hidden;
  isolation: isolate;

  background: linear-gradient(180deg, #ffffff 0%, #ffffff 72%, #fbfaff 100%);

  border: 1px solid var(--border-color);
  border-radius: 14px;

  box-shadow:
    0 18px 44px var(--shadow-color),
    0 4px 12px rgba(54, 40, 82, 0.06);
}

/* 頂部青藍至淡紫品牌漸層細線 */
.message-container::before {
  content: "";

  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  z-index: 20;

  height: 3px;

  background: linear-gradient(
    90deg,
    var(--brand-cyan) 0%,
    var(--brand-blue) 48%,
    var(--brand-lilac) 76%,
    var(--main-color) 100%
  );

  pointer-events: none;
}

/* ==================================================
   訊息顯示區塊
================================================== */

.message-bubble {
  min-height: 480px;
  margin: 0;
  padding: 25px 20px 104px;

  display: flex;
  flex-direction: column;
  gap: 8px;

  overflow-wrap: anywhere;
  word-break: break-word;
  white-space: normal;
}

/* AI 與使用者訊息共用樣式 */
.message-bubble .ai,
.message-bubble .user {
  position: relative;

  min-width: 4rem;
  min-height: 2rem;
  max-width: 84%;

  margin-bottom: 25px;
  padding: 11px 14px;

  border-radius: 10px;

  font-size: 1rem;
  line-height: 1.65;
  text-align: left;
  white-space: pre-line;
}

/* AI 訊息 */
.message-bubble .ai {
  align-self: flex-start;

  color: var(--text-color);
  background: var(--soft-bg-color);

  border: 1px solid var(--soft-border-color);

  box-shadow: none;
}

/* 使用者訊息 */
.message-bubble .user {
  align-self: flex-end;

  color: #ffffff;
  background: linear-gradient(135deg, var(--main-color) 0%, #7641dc 100%);

  border: 1px solid var(--main-color);

  box-shadow: 0 5px 14px rgba(101, 29, 218, 0.16);
}

/* 訊息時間 */
.message-bubble .time {
  position: absolute;
  bottom: -21px;

  color: var(--muted-color);

  font-size: 0.75rem;
  line-height: 1;
  white-space: nowrap;
}

.message-bubble .ai .time {
  left: 2px;
}

.message-bubble .user .time {
  right: 2px;
}

/* ==================================================
   訊息 Tags
================================================== */

/* ==================================================
   訊息 Card
================================================== */

.card-container {
  width: 100%;
}

.card-container .card:not(:last-child) {
  margin-bottom: 12px;
}

.message-container .card {
  overflow: hidden;

  color: var(--text-color);
  background: #ffffff;

  border: 1px solid var(--border-color) !important;
  border-radius: 10px;

  box-shadow: 0 7px 20px rgba(54, 40, 82, 0.07);

  min-width: auto;
  min-height: auto;
}

.message-container .card-body {
  padding: 15px;
}

.message-container .card-title {
  margin-bottom: 8px;

  color: var(--text-color);

  font-size: 1.05rem;
  font-weight: 700;
  line-height: 1.45;
}

.message-container .card-text {
  margin-bottom: 12px;

  color: #6f7382;

  font-size: 0.92rem;
  line-height: 1.6;
  white-space: pre-line;
}

.message-container .card.reference-button {
  cursor: pointer;
  transition:
    border-color 0.2s ease,
    box-shadow 0.2s ease;
}

.message-container .card.reference-button:hover,
.message-container .card.reference-button:focus-visible {
  border-color: var(--main-color) !important;
  box-shadow: 0 7px 20px rgba(54, 40, 82, 0.14);
  outline: none;
}

.chat-error-notice {
  position: absolute;
  right: 14px;
  bottom: 82px;
  left: 14px;
  z-index: 31;

  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;

  color: #69707d;
  background: #f5f6f8;
  border: 1px solid #e1e4e8;
  border-radius: 8px;
  box-shadow: 0 5px 14px rgba(54, 40, 82, 0.08);

  font-size: 0.84rem;
  line-height: 1.45;
}

.chat-error-notice span {
  flex: 1;
}

.chat-error-notice-close {
  flex: 0 0 auto;
  width: 20px;
  height: 20px;
  padding: 0;

  color: #8a919d;
  background: transparent;
  border: 0;
  border-radius: 50%;
  font-size: 1.5rem !important;
  line-height: 0 !important;
}

.chat-error-notice-close:hover {
  color: #69707d;
  background: #e9ebef;
}

/* ==================================================
   底部輸入與錄音區
================================================== */

.chat-toggle {
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 30;

  width: 100%;
  min-height: 68px;
  padding: 12px 14px;

  display: flex;
  align-items: flex-end;
  gap: 8px;

  background: rgba(255, 255, 255, 0.98);

  border-top: 1px solid var(--border-color);
  border-radius: 0 0 13px 13px;

  box-shadow: 0 -9px 24px rgba(72, 54, 101, 0.04);
}

/* 文字輸入框 */
.chat-input {
  position: relative;

  width: 100%;
  min-height: 42px;
  padding: 7px 8px 7px 13px;

  display: flex;
  align-items: flex-end;
  gap: 5px;

  background: var(--soft-bg-color);

  border: 1px solid #e5dff0;
  border-radius: 9px;

  transition:
    background-color 0.2s ease,
    border-color 0.2s ease,
    box-shadow 0.2s ease;
}

.chat-input:focus-within {
  background: #ffffff;

  border-color: rgba(101, 29, 218, 0.5);

  box-shadow: 0 0 0 3px var(--focus-color);
}

.chat-input textarea {
  display: block;

  width: 100%;
  height: 100%;
  min-height: 25px;
  max-height: 150px;
  padding: 1px 0;

  color: var(--text-color);
  background: transparent;

  border: 0;
  outline: 0;
  resize: none;

  font-family: inherit;
  font-size: 1rem;
  line-height: 1.5;

  transition: height 0.2s ease;
}

.chat-input textarea:disabled {
  color: var(--muted-color);
  cursor: not-allowed;
}

.chat-input textarea:focus {
  border: 0;
  outline: none;
  box-shadow: none;
}

.chat-input textarea::placeholder {
  color: var(--placeholder-color);

  font-size: 0.95rem;

  opacity: 0.78;
}

.recording-waveform {
  flex: 1 1 auto;
  min-width: 0;
  height: 30px;
  width: 100%;

  display: block;
  overflow: hidden;
}

.recording-timer {
  position: absolute;
  bottom: calc(100% + 12px);
  left: 50%;
  z-index: 1;

  color: #666666;
  font-size: 0.85rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  padding: 6px 12px 0 12px;

  background: #fff;
  /* background: var(--soft-bg-color); */
  border-style: solid;
  border-width: 1px 1px 0 1px;
  border-color: var(--border-color);
  border-radius: 6px 6px 0 0;
  pointer-events: none;

  transform: translateX(-50%);
}

/* ==================================================
   輸入區按鈕
================================================== */

.microphone-btn,
.keyboard-btn,
.submit-btn {
  flex: 0 0 auto;

  display: inline-flex;
  align-items: center;
  justify-content: center;

  color: var(--main-color);

  transition:
    color 0.2s ease,
    background-color 0.2s ease,
    transform 0.2s ease;
}

.microphone-btn {
  width: 30px;
  height: 30px;

  border-radius: 7px;
}

.submit-btn,
.keyboard-btn {
  width: 40px;
  height: 40px;

  margin-bottom: 1px;

  background: rgba(101, 29, 218, 0.07) !important;

  border-radius: 8px;
}

.microphone-btn:hover,
.keyboard-btn:hover,
.submit-btn:hover {
  color: var(--sub-color);
  background-color: rgba(101, 29, 218, 0.11) !important;

  transform: translateY(-1px);
}

.microphone-btn:active,
.keyboard-btn:active,
.submit-btn:active {
  transform: translateY(0);
}

.microphone-btn:disabled,
.keyboard-btn:disabled,
.submit-btn:disabled {
  color: #b4b1bc;
  background: #f0eef3 !important;

  cursor: not-allowed;
  transform: none;
}

/* ==================================================
   錄音模式
================================================== */

.chat-recording-box {
  align-items: center;
}

.rec-btn {
  position: relative;

  width: 70%;
  min-height: 40px;
  margin: 0 auto;
  padding: 7px 12px;

  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;

  color: #ffffff;
  background: linear-gradient(135deg, var(--main-color), #7641dc) !important;

  border-radius: 8px;

  font-size: 1rem;
  text-decoration: none;

  box-shadow: 0 5px 14px rgba(101, 29, 218, 0.16);

  transition:
    background-color 0.2s ease,
    box-shadow 0.2s ease,
    transform 0.2s ease;
}

.rec-btn:hover {
  box-shadow: 0 7px 17px rgba(101, 29, 218, 0.2);

  transform: translateY(-1px);
}

.rec-btn:disabled {
  color: #a8a5af;
  background: #e9e7ed !important;

  box-shadow: none;

  cursor: not-allowed;
  transform: none;
}

.chat-input .rec-btn {
  flex: 0 0 30px;
  width: 30px;
  min-height: 30px;
  margin: 0;
  padding: 0;

  gap: 0;
  border-radius: 7px;
  font-size: 0.9rem;
}

.chat-input .rec-btn .rec-start-label,
.chat-input .rec-btn .rec-stop-label {
  display: none;
}

/* 錄音中 */
.rec-btn.Rec {
  color: #ffffff;
  background: #d84562 !important;

  animation: pulse 1s infinite linear;
}

/* ==================================================
   錄音動畫
================================================== */

@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(216, 69, 98, 0.3);
  }

  65% {
    box-shadow: 0 0 0 11px rgba(216, 69, 98, 0.12);
  }

  100% {
    box-shadow: 0 0 0 11px rgba(216, 69, 98, 0);
  }
}

/* ==================================================
   滑動提示陰影
================================================== */

.more-shadow {
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  z-index: 10;

  width: 100%;
  height: 25px;

  background: linear-gradient(
    to bottom,
    rgba(101, 29, 218, 0.08),
    rgba(255, 255, 255, 0)
  );

  pointer-events: none;
}

/* ==================================================
   載入動畫
================================================== */

.loader {
  width: 45px;
  aspect-ratio: 2;

  --_g: no-repeat
    radial-gradient(
      circle closest-side,
      rgba(101, 29, 218, 0.52) 90%,
      transparent
    );

  background:
    var(--_g) 0% 50%,
    var(--_g) 50% 50%,
    var(--_g) 100% 50%;

  background-size: calc(100% / 3) 50%;

  animation: l3 1s infinite linear;
}

@keyframes l3 {
  20% {
    background-position:
      0% 0%,
      50% 50%,
      100% 50%;
  }

  40% {
    background-position:
      0% 100%,
      50% 0%,
      100% 50%;
  }

  60% {
    background-position:
      0% 50%,
      50% 100%,
      100% 0%;
  }

  80% {
    background-position:
      0% 50%,
      50% 50%,
      100% 100%;
  }
}

/* ==================================================
   參考資料列表
================================================== */

.reference-list {
  margin: 10px 0 0;
  padding: 0;

  display: flex;
  flex-direction: column;
  gap: 0;

  list-style: none;
}

.reference-list li {
  margin: 0;
  padding: 9px 2px;
}

.reference-list li:first-child {
  padding-top: 5px;
}

.reference-list li:last-child {
  padding-bottom: 3px;
}

.reference-list li:not(:last-child) {
  border-bottom: 1px solid #e4e0ed;
}

.reference-list a,
.reference-list span,
.reference-list button {
  color: var(--main-color);

  font-weight: 500;
  line-height: 1.5;
  text-decoration: none;
  background: transparent;
  border: 0;
  padding: 0;
  text-align: left;
  font: inherit;

  cursor: pointer;

  transition:
    color 0.2s ease,
    opacity 0.2s ease;
}

.reference-list .reference-content {
  margin: 4px 0 0;
  color: #6f7382;
  font-size: 0.9rem;
  line-height: 1.5;
  white-space: pre-line;
}

.reference-list a:hover,
.reference-list span:hover,
.reference-list button:hover {
  color: var(--sub-color);
}

/* ==================================================
   Tippy Tooltip
================================================== */

.tippy-box {
  text-align: center;
  background: #000;

  border-radius: 7px;

  box-shadow: 0 7px 20px rgba(45, 32, 64, 0.18);
}

.tippy-box[data-placement^="top"] > .tippy-arrow::before {
  border-top-color: #000;
}

.tippy-box[data-placement^="bottom"] > .tippy-arrow::before {
  border-bottom-color: #000;
}

.tippy-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;

  color: #ffffff;
}

.tippy-content span {
  color: rgba(255, 255, 255, 0.78);

  font-size: 0.9em;
}

/* ==================================================
   平板尺寸
================================================== */

@media (max-width: 1024px) {
  .message-container {
    width: 370px;
  }
}

/* ==================================================
   手機尺寸
================================================== */

@media (max-width: 768px) {
  #toggle-button {
    right: 50px;
    bottom: 30px;
  }

  #toggle-button img {
    width: 70px;
    height: 70px;
  }

  .message-container {
    right: 16px;
    bottom: 135px;

    width: calc(100% - 32px);
    min-width: 280px;
    max-height: calc(100vh - 165px);

    border-radius: 12px;
  }

  .scroll {
    max-height: calc(100vh - 165px);
  }

  .message-bubble {
    min-height: min(480px, calc(100vh - 230px));
    padding: 22px 15px 100px;
  }

  .message-bubble .ai,
  .message-bubble .user {
    max-width: 90%;

    padding: 10px 12px;

    border-radius: 9px;

    font-size: 0.95rem;
  }

  .chat-toggle {
    padding: 11px 12px;

    border-radius: 0 0 11px 11px;
  }

  .chat-input {
    padding-left: 11px;
    border-radius: 8px;
  }

  .submit-btn,
  .keyboard-btn {
    width: 38px;
    height: 38px;

    border-radius: 7px;
  }

  .rec-btn {
    border-radius: 7px;
  }
}

/* ==================================================
   小尺寸手機
================================================== */

@media (max-width: 420px) {
  .message-container {
    right: 10px;
    bottom: 125px;

    width: calc(100% - 20px);
  }

  .message-bubble {
    padding-right: 13px;
    padding-left: 13px;
  }

  .message-bubble .ai,
  .message-bubble .user {
    max-width: 92%;
  }

  .chat-toggle {
    gap: 6px;
    padding-right: 10px;
    padding-left: 10px;
  }

  .chat-input textarea,
  .chat-input textarea::placeholder {
    font-size: 0.92rem;
  }
}

/* ==================================================
   減少動態效果偏好
================================================== */

@media (prefers-reduced-motion: reduce) {
  .ai-container,
  #toggle-button,
  .microphone-btn,
  .keyboard-btn,
  .submit-btn,
  .rec-btn {
    transition: none;
  }

  .rec-btn.Rec,
  .loader {
    animation-duration: 1.8s;
  }
}
