/* =========================================
   1. 基本變數與排版設定
   ========================================= */
:root {
  /* 🎨 背景色 */
  --bg: #5367A0;              /* 頁面背景色 */

  /* 🎨 面板底色 */
  --panel: #67A2FF;           /* 頂部與輸入區背景 */

  /* 🎨 邊框色 */
  --border: #e5e7eb;

  /* 🎨 主要文字色 */
  --text: #111827;

  /* 🎨 次要/灰色文字 */
  --muted: #6b7280;

  /* 🎨 主色（按鈕、強調） */
  --primary: #2563eb;         /* 主色（正常狀態） */
  --primary-600: #1d4ed8;     /* 主色（hover 狀態） */

  /* 🎨 使用者訊息泡泡色 */
  --bubble-user: #2563eb;      /* 使用者泡泡背景 */
  --bubble-user-text: #ffffff; /* 使用者泡泡文字 */

  /* 🎨 機器人訊息泡泡色 */
  --bubble-bot: #ffffff;       /* 機器人泡泡背景 */
  --bubble-bot-text: #111827;  /* 機器人泡泡文字 */
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, "PingFang TC", "Noto Sans CJK TC", "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", "Apple Color Emoji", "Segoe UI Emoji";
  display: flex; flex-direction: column;
}

/* 公用 */
.hidden { display: none !important; }

/* =========================================
   2. 頂部列 (Header)
   ========================================= */
.topbar {
  position: sticky; top: 0; z-index: 10;
  height: 64px; background: var(--panel); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 12px; padding: 0 16px;
}
.logo { width: 36px; height: 36px; border-radius: 6px; }
.brand .title { font-weight: 600; }
.brand .subtitle { font-size: 12px; color: var(--muted); }

/* =========================================
   3. 主體與訊息清單
   ========================================= */
.main {
  flex: 1;
  display: flex; flex-direction: column; align-items: center;
  padding: 12px;
}
.messages {
  width: 100%; max-width: 820px;
  height: calc(100vh - 64px - 72px);
  height: calc(100dvh - 64px - 72px);
  overflow-y: auto; padding: 16px;
  display: flex; flex-direction: column; gap: 12px;
}

/* 單則訊息容器 */
.msg { display: flex; align-items: flex-start; gap: 10px; }
.msg.user { flex-direction: row-reverse; }
.avatar {
  width: 36px; height: 36px; border-radius: 999px;
  background: #fff; border: 1px solid var(--border); flex-shrink: 0;
}

/* =========================================
   4. 對話泡泡 (Bubble) 核心樣式
   ========================================= */
.bubble {
  max-width: 85%; /*稍微加寬一點以容納表格*/
  border: 1px solid var(--border); border-radius: 16px;
  padding: 10px 12px;
  background: var(--bubble-bot); color: var(--bubble-bot-text);
  box-shadow: 0 1px 3px rgba(0,0,0,.05);
  font-size: 14px; line-height: 1.5;
  
  /* ★ 修改：為了正確顯示 HTML，預設使用 normal */
  white-space: normal; 
  word-break: break-word;
}

/* 使用者泡泡特別處理 */
.user .bubble {
  background: var(--bubble-user);
  color: var(--bubble-user-text);
  border-color: transparent;
  
  /* ★ 修改：使用者輸入仍為純文字，需要保留換行 */
  white-space: pre-wrap; 
}

/* =========================================
   5. n8n HTML 內容樣式優化 (表格、清單、連結)
   ========================================= */

/* 連結 */
.bubble a {
  color: #2563eb;
  text-decoration: underline;
  word-break: break-all;
}
.bubble a:hover {
  color: #1d4ed8;
}

/* 表格 */
.bubble table {
  border-collapse: collapse;
  width: 100%;
  margin: 8px 0;
  font-size: 0.9em;
  background-color: #fff; /* 確保表格背景是白的 */
  border-radius: 8px;
  overflow: hidden; /* 讓圓角生效 */
}
.bubble th, .bubble td {
  border: 1px solid #e5e7eb;
  padding: 8px 10px;
  text-align: left;
}
.bubble th {
  background-color: #f3f4f6;
  font-weight: 600;
  color: #374151;
}
.bubble tr:nth-child(even) {
  background-color: #f9fafb;
}

/* 清單 (ul, ol) */
.bubble ul, .bubble ol {
  margin: 6px 0 6px 24px;
  padding: 0;
}
.bubble li {
  margin-bottom: 4px;
}

/* 段落與標題 */
.bubble p {
  margin: 0 0 8px 0;
}
.bubble p:last-child {
  margin-bottom: 0;
}
.bubble h1, .bubble h2, .bubble h3 {
  margin: 10px 0 6px 0;
  font-size: 1.1em;
  font-weight: bold;
}

/* 圖片 */
.bubble img {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  margin-top: 8px;
  display: block;
}

/* =========================================
   6. 思考中動畫 (Thinking)
   ========================================= */
.thinking {
  width: 100%; max-width: 820px;
  display: flex; align-items: center; gap: .5rem;
  margin-top: 8px; padding: .6rem .8rem;
  border-radius: 12px; background: #f6f7fb; color: #4b5563;
  border: 1px dashed var(--border);
  box-shadow: 0 1px 3px rgba(0,0,0,.04);
  font-size: 14px;
}
.thinking .dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: currentColor; opacity: .35; display: inline-block;
  animation: dotBounce 1.05s infinite ease-in-out;
}
.thinking .dot:nth-child(1) { animation-delay: 0s; }
.thinking .dot:nth-child(2) { animation-delay: .15s; }
.thinking .dot:nth-child(3) { animation-delay: .3s; }
@keyframes dotBounce {
  0%, 80%, 100% { transform: scale(0.7); opacity: .35; }
  40% { transform: scale(1); opacity: 1; }
}

/* =========================================
   7. 輸入列 (Composer)
   ========================================= */
.composer {
  min-height: 72px;
  height: auto;
  background: var(--panel);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 8px 12px;
  justify-content: center;
}
.composer .input {
  width: 100%; max-width: 620px;
  min-height: 40px;
  max-height: 120px;
  height: auto;
  padding: 8px 12px;
  line-height: 1.4;
  border: 1px solid var(--border);
  border-radius: 12px;
  outline: none;
  overflow-y: auto;
  resize: none;
  white-space: pre-wrap;
  word-break: break-word;
}
.btn {
  height: 40px; padding: 0 14px; border-radius: 12px; border: 1px solid transparent; cursor: pointer;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-600); }
.btn-outline { background: #f3f4f6; border-color: var(--border); }
.btn-outline:hover { background: #e9ecf1; }

/* =========================================
   8. 行動裝置 RWD 微調
   ========================================= */
@media (max-width: 480px) {
  .bubble { max-width: 90%; }
  .messages { padding: 12px; }
  
  /* 手機上表格若太寬可橫向捲動 */
  .bubble table {
    display: block;
    overflow-x: auto;
    white-space: nowrap;
  }
}
