/* ============================================
   AI 對話(v6.1.0)
   - 全部使用主題變數,深淺色自動跟隨
   - 深色:互動元件金描邊(沿用全 App 規則)
   ============================================ */

/* ---------- 聊天頁版面 ---------- */
.ai-screen {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  padding-bottom: 0;            /* 自己管理底部(輸入列 + tabbar) */
  overflow: hidden;
}
.ai-chat-scroll {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px 16px 8px;
  display: flex;
  flex-direction: column;
}

/* ---------- 日期分隔 ---------- */
.ai-day-sep {
  align-self: center;
  font-size: 11px;
  color: var(--text-mute);
  background: var(--bg-soft);
  padding: 3px 12px;
  border-radius: var(--r-pill);
  margin: 12px 0;
}

/* ---------- 訊息 ---------- */
.ai-msg {
  display: flex;
  gap: 8px;
  margin: 6px 0;
  max-width: 100%;
}
.ai-msg.user { flex-direction: row-reverse; }
.ai-avatar {
  width: 30px; height: 30px;
  border-radius: 50%;
  flex: 0 0 auto;
  display: flex; align-items: center; justify-content: center;
  background: var(--accent-soft);
  color: var(--accent);
  align-self: flex-end;
}
.ai-avatar svg { width: 17px; height: 17px; }
.ai-msg.user .ai-avatar { background: var(--bg-soft); color: var(--text-soft); }
.ai-bubble-wrap {
  display: flex;
  flex-direction: column;
  max-width: 78%;
  min-width: 0;
}
.ai-msg.user .ai-bubble-wrap { align-items: flex-end; }
.ai-bubble {
  padding: 10px 13px;
  border-radius: 16px;
  font-size: 14.5px;
  line-height: 1.55;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.ai-msg.assistant .ai-bubble {
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-bottom-left-radius: 5px;
  color: var(--text);
}
.ai-msg.user .ai-bubble {
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 5px;
  font-weight: 500;
  white-space: pre-wrap;
}
[data-theme="dark"] .ai-msg.user .ai-bubble { color: var(--on-accent); }
.ai-bubble.err {
  background: var(--danger-soft);
  border: 1px solid var(--danger);
  color: var(--danger);
}
.ai-ts {
  font-size: 10.5px;
  color: var(--text-dim);
  margin: 3px 4px 0;
  font-variant-numeric: tabular-nums;
}

/* AI 氣泡內 Markdown */
.ai-bubble .md-h { font-weight: 700; margin: 8px 0 4px; font-size: 15px; }
.ai-bubble ul, .ai-bubble ol { margin: 6px 0; padding-left: 20px; }
.ai-bubble ul { list-style: disc; }
.ai-bubble ol { list-style: decimal; }
.ai-bubble li { margin: 3px 0; }
.ai-bubble pre {
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  overflow-x: auto;
  margin: 8px 0;
  white-space: pre;
}
.ai-bubble code {
  background: var(--bg-soft);
  padding: 1px 5px;
  border-radius: 5px;
  font-size: 12.5px;
}
.ai-bubble pre code { background: none; padding: 0; }

/* 打字中 */
.ai-typing { display: inline-flex; gap: 4px; align-items: center; padding: 3px 0; }
.ai-typing i {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--text-mute);
  animation: ai-blink 1.3s infinite both;
}
.ai-typing i:nth-child(2) { animation-delay: .2s; }
.ai-typing i:nth-child(3) { animation-delay: .4s; }
@keyframes ai-blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* ---------- 空狀態 + 快捷問題 ---------- */
.ai-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--text-mute);
  text-align: center;
  padding: 0 28px;
}
.ai-empty svg { width: 40px; height: 40px; opacity: .5; }
.ai-empty .t { font-size: 15px; color: var(--text-soft); }
.ai-empty .s { font-size: 13px; }
.ai-chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 14px; }
.ai-chip {
  border: 1px solid var(--line-strong);
  background: var(--bg-card);
  color: var(--text);
  font-size: 13px;
  padding: 8px 14px;
  border-radius: var(--r-pill);
  transition: background .15s, border-color .15s;
}
.ai-chip:active { background: var(--accent-soft); border-color: var(--accent); }
[data-theme="dark"] .ai-chip { border-color: var(--tap-border); }

/* ---------- 離線橫幅 ---------- */
.ai-offline {
  flex: 0 0 auto;
  display: none;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  background: var(--danger-soft);
  border-top: 1px solid var(--danger);
  color: var(--danger);
  font-size: 13px;
}
.ai-offline svg { width: 16px; height: 16px; flex: 0 0 auto; }
.ai-screen.offline .ai-offline { display: flex; }
.ai-screen.offline .ai-inputbar { opacity: .5; pointer-events: none; }

/* ---------- 輸入列(貼在 tabbar 上方) ---------- */
.ai-inputbar {
  flex: 0 0 auto;
  display: flex;
  gap: 8px;
  align-items: flex-end;
  padding: 10px 12px calc(10px + var(--tabbar-h) + var(--safe-bottom));
  border-top: 1px solid var(--line);
  background: var(--bg-elev);
}
.ai-inputbar textarea {
  flex: 1;
  resize: none;
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 10px 14px;
  color: var(--text);
  font-size: 15px;
  line-height: 1.4;
  max-height: 120px;
  transition: border-color .15s, box-shadow .15s;
}
.ai-inputbar textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(200, 137, 58, 0.12);
}
.ai-inputbar textarea::placeholder { color: var(--text-dim); }
[data-theme="dark"] .ai-inputbar textarea { border-color: var(--tap-border); }
[data-theme="dark"] .ai-inputbar textarea:focus { border-color: var(--tap-border-strong); }
.ai-send {
  width: 42px; height: 42px;
  border-radius: 50%;
  flex: 0 0 auto;
  background: var(--accent);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  transition: opacity .15s, transform .1s;
}
[data-theme="dark"] .ai-send { color: var(--on-accent); }
.ai-send:active { transform: scale(.92); }
.ai-send:disabled { opacity: .35; }
.ai-send svg { width: 20px; height: 20px; }

/* ---------- 設定:AI 群組 ---------- */
.ai-status-line { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text-soft); }
.ai-status-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--text-dim);
  flex: 0 0 auto;
}
.ai-status-dot.on { background: var(--success); box-shadow: 0 0 0 3px var(--success-soft); }
.ai-key-note {
  font-size: 11px;
  color: var(--text-mute);
  line-height: 1.5;
  padding: 0 14px 12px;
}

/* 連線彈窗:金鑰眼睛 */
.ai-key-wrap { position: relative; }
.ai-key-wrap input { padding-right: 46px !important; }
.ai-eye {
  position: absolute;
  right: 6px; top: 50%;
  transform: translateY(-50%);
  width: 36px; height: 36px;
  border-radius: var(--r-sm);
  display: flex; align-items: center; justify-content: center;
  color: var(--text-mute);
}
.ai-eye svg { width: 19px; height: 19px; }

/* ============================================
   AI 助手設定:展開區 + 多連線卡片(v6.1.0)
   ============================================ */

/* 展開/收合(沿用更新設定的 details 手法) */
.ai-details { border-top: 1px solid var(--line); margin-top: 4px; }
.ai-details > summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 14px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-soft);
}
.ai-details > summary::-webkit-details-marker { display: none; }
.ai-details > summary .chevron { display: inline-flex; transition: transform 0.2s; color: var(--text-mute); }
.ai-details[open] > summary .chevron { transform: rotate(180deg); }
.ai-details-body { padding: 0 14px 14px; }

.ai-sub-label {
  font-size: 12px;
  color: var(--text-mute);
  letter-spacing: 0.04em;
  margin: 4px 2px 8px;
}
.ai-empty-hint {
  font-size: 13px;
  color: var(--text-mute);
  padding: 10px 12px;
  background: var(--bg-soft);
  border-radius: var(--r-md);
  margin-bottom: 8px;
}

/* 連線卡片列:[單選+主體] [編輯] [刪除] */
.ai-prof-row {
  display: flex;
  align-items: stretch;
  gap: 6px;
  margin-bottom: 8px;
}
.ai-prof-main {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 11px;
  text-align: left;
  padding: 11px 13px;
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  transition: border-color 0.15s, background 0.15s;
}
.ai-prof-row.active .ai-prof-main {
  border-color: var(--accent);
  background: var(--accent-soft);
}
[data-theme="dark"] .ai-prof-main { border-color: var(--tap-border); }
[data-theme="dark"] .ai-prof-row.active .ai-prof-main { border-color: var(--tap-border-strong); }

/* 單選圓鈕 */
.ai-radio {
  flex: 0 0 auto;
  width: 19px;
  height: 19px;
  border-radius: 50%;
  border: 2px solid var(--text-dim);
  position: relative;
  transition: border-color 0.15s;
}
.ai-prof-row.active .ai-radio { border-color: var(--accent); }
.ai-radio.on::after {
  content: "";
  position: absolute;
  inset: 3px;
  border-radius: 50%;
  background: var(--accent);
}

.ai-prof-text { min-width: 0; flex: 1; }
.ai-prof-name {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ai-prof-model {
  font-size: 11.5px;
  color: var(--text-mute);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 編輯 / 刪除 小圖示鈕 */
.ai-prof-icon {
  flex: 0 0 auto;
  width: 42px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  color: var(--text-soft);
  transition: background 0.15s, color 0.15s;
}
.ai-prof-icon:active { background: var(--bg-soft); transform: scale(0.95); }
.ai-prof-icon.danger:active { background: var(--danger-soft); color: var(--danger); }
[data-theme="dark"] .ai-prof-icon { border-color: var(--tap-border); }

/* 新增連線按鈕 */
.ai-add-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 11px;
  background: transparent;
  border: 1px dashed var(--line-strong);
  border-radius: var(--r-md);
  color: var(--accent-deep);
  font-size: 14px;
  font-weight: 500;
  transition: background 0.15s, border-color 0.15s;
}
.ai-add-btn:active { background: var(--accent-soft); }
.ai-add-btn .plus { font-size: 17px; line-height: 1; }
[data-theme="dark"] .ai-add-btn { border-color: var(--tap-border); color: var(--accent); }

/* 從資料庫還原按鈕 */
.ai-restore-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 11px;
  background: var(--bg-card);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-md);
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
  transition: background 0.15s, border-color 0.15s;
}
.ai-restore-btn:active { background: var(--bg-soft); }
.ai-restore-btn:disabled { opacity: 0.55; }
[data-theme="dark"] .ai-restore-btn { border-color: var(--tap-border); }

/* 還原:覆蓋/保留 選擇彈窗 */
.ai-restore-warn {
  font-size: 12.5px;
  line-height: 1.6;
  color: var(--warn, #b5791a);
  background: var(--warn-soft, rgba(212,163,86,0.12));
  border: 1px solid var(--warn-border, rgba(212,163,86,0.3));
  border-radius: var(--r-md);
  padding: 10px 12px;
  margin: 4px 0 16px;
}
.ai-choice-btn {
  width: 100%;
  text-align: left;
  padding: 13px 15px;
  margin-bottom: 10px;
  background: var(--bg-card);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-md);
  transition: border-color 0.15s, background 0.15s;
}
.ai-choice-btn:active { background: var(--bg-soft); }
.ai-choice-btn.primary { border-color: var(--accent); background: var(--accent-soft); }
[data-theme="dark"] .ai-choice-btn { border-color: var(--tap-border); }
[data-theme="dark"] .ai-choice-btn.primary { border-color: var(--tap-border-strong); }
.ai-choice-title { font-size: 14.5px; font-weight: 600; color: var(--text); margin-bottom: 3px; }
.ai-choice-sub { font-size: 12.5px; color: var(--text-soft); line-height: 1.5; }

/* ============================================
   BETA 標示(v6.2.1)
   ============================================ */

/* 聊天頁頂部紅底警告條 */
.ai-beta-banner {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  background: var(--danger-soft);
  border-bottom: 1px solid var(--danger);
  color: var(--danger);
  font-size: 12.5px;
  line-height: 1.4;
}
.ai-beta-tag {
  flex: 0 0 auto;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.05em;
  padding: 2px 6px;
  border-radius: 5px;
  background: var(--danger);
  color: #fff;
}

/* 設定群組標題的 BETA 小卡 */
.setting-group-title { display: flex; align-items: center; gap: 8px; }
.ai-beta-chip {
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.05em;
  padding: 2px 7px;
  border-radius: 6px;
  background: var(--accent-soft);
  color: var(--accent-deep, var(--accent));
  border: 1px solid var(--accent);
}
[data-theme="dark"] .ai-beta-chip { color: var(--accent); border-color: var(--tap-border-strong); }

/* 底部 AI 分頁角標 */
.tab-btn.has-beta { position: relative; }
.tab-beta {
  position: absolute;
  top: 4px;
  left: 50%;
  margin-left: 6px;
  font-size: 7.5px;
  font-weight: 800;
  letter-spacing: 0.03em;
  line-height: 1;
  padding: 1.5px 3px;
  border-radius: 4px;
  background: var(--danger);
  color: #fff;
}

/* 同步狀態列 */
.ai-sync-status {
  font-size: 12px;
  line-height: 1.5;
  padding: 9px 12px;
  margin-top: 8px;
  border-radius: var(--r-md);
  background: var(--bg-soft);
  color: var(--text-soft);
  display: flex;
  align-items: center;
  gap: 7px;
}
.ai-sync-status::before {
  content: "";
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--text-dim);
  flex: 0 0 auto;
}
.ai-sync-status.saving { color: var(--accent-deep, var(--accent)); }
.ai-sync-status.saving::before { background: var(--accent); animation: ai-sync-pulse 0.9s ease-in-out infinite; }
.ai-sync-status.ok { color: var(--success); background: var(--success-soft); }
.ai-sync-status.ok::before { background: var(--success); }
.ai-sync-status.err { color: var(--danger); background: var(--danger-soft); }
.ai-sync-status.err::before { background: var(--danger); }
.ai-sync-status.warn { color: var(--warn, #b5791a); background: var(--warn-soft, rgba(212,163,86,0.12)); }
.ai-sync-status.warn::before { background: var(--warn, #b5791a); }
@keyframes ai-sync-pulse { 0%,100% { opacity: 0.3; } 50% { opacity: 1; } }

/* ============================================
   打字時:輸入框貼齊鍵盤(導航列已收合成浮球)(v6.5.0)
   ============================================ */
/* 鍵盤升起 → 輸入列固定貼底(iOS 會自動把 fixed bottom:0 元素頂到鍵盤上方) */
body.kb-open .ai-inputbar {
  position: fixed;
  left: 0; right: 0;
  bottom: 0;
  padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px));
  z-index: 55;
}
/* 聊天捲動區預留輸入列高度,避免被遮 */
body.kb-open .ai-chat-scroll {
  padding-bottom: 80px;
}

/* ============================================
   頂部模型切換器(v6.6.0)
   ============================================ */
.ai-header { position: relative; }
.ai-model-switch { position: relative; flex: 1; display: flex; justify-content: center; }
.ai-model-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  max-width: 70vw;
  padding: 5px 10px;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: var(--r-pill);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
}
.ai-model-btn:active { background: var(--bg-card); }
.ai-model-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ai-model-caret { display: inline-flex; color: var(--text-mute); transition: transform 0.18s; }
.ai-model-btn.open .ai-model-caret { transform: rotate(180deg); }
[data-theme="dark"] .ai-model-btn { border-color: var(--tap-border); }

/* 下拉選單 */
.ai-model-menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  min-width: 220px;
  max-width: 86vw;
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: 0 8px 28px rgba(0,0,0,0.18);
  padding: 6px;
  z-index: 70;
  max-height: 60vh;
  overflow-y: auto;
}
[data-theme="dark"] .ai-model-menu { border-color: var(--tap-border); }
.ai-model-empty { padding: 14px; text-align: center; color: var(--text-mute); font-size: 13px; }
.ai-model-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 10px;
  background: none;
  border: none;
  border-radius: var(--r-md);
  text-align: left;
}
.ai-model-item:active { background: var(--bg-soft); }
.ai-model-item.active { background: var(--accent-soft); }
.ai-model-radio {
  flex: 0 0 auto;
  width: 17px; height: 17px;
  border-radius: 50%;
  border: 2px solid var(--text-dim);
  position: relative;
}
.ai-model-item.active .ai-model-radio { border-color: var(--accent); }
.ai-model-radio.on::after {
  content: ""; position: absolute; inset: 3px;
  border-radius: 50%; background: var(--accent);
}
.ai-model-item-text { min-width: 0; flex: 1; }
.ai-model-item-name {
  font-size: 14px; font-weight: 600; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.ai-model-item-model {
  font-size: 11.5px; color: var(--text-mute); margin-top: 1px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.ai-model-add {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  margin-top: 4px;
  padding: 10px;
  background: none;
  border: none;
  border-top: 1px solid var(--line-soft);
  border-radius: 0 0 var(--r-md) var(--r-md);
  color: var(--accent-deep);
  font-size: 13.5px;
  font-weight: 500;
}
.ai-model-add:active { background: var(--accent-soft); }
.ai-model-add .plus { font-size: 16px; line-height: 1; }
[data-theme="dark"] .ai-model-add { color: var(--accent); }
