/* AI Mode ウィジェット — 固有スタイル
 *
 * 共通コンポーネント（.fab-button / .card-link / .chip-button / .ref-label）は
 * style.css に昇格済み。ここでは AI Mode 固有の以下のみ管理:
 *   - 浮動 FAB の固定位置・絵文字・開閉状態
 *   - チャットパネルのレイアウト（位置・サイズ）
 *   - メッセージ領域（user / status / answer / error）の見た目
 *   - 入力欄
 */

/* ===== Floating Action Button（位置のみ） ===== */
#aichat-fab {
  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: 9998;
}

/* ===== Chat Panel ===== */
#aichat-panel {
  position: fixed;
  right: 24px;
  bottom: 96px;
  width: 440px;
  max-width: calc(100vw - 32px);
  height: 620px;
  max-height: calc(100vh - 120px);
  background: var(--bg-paper);
  color: var(--text-primary);
  border: 0.5px solid var(--rule-strong);
  border-radius: var(--radius-soft);
  box-shadow: var(--shadow-hover);
  z-index: 9999;
  display: none;
  flex-direction: column;
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.7;
}
#aichat-panel[data-open="true"] { display: flex; }

/* ===== Header ===== */
#aichat-header {
  padding: 14px 18px;
  border-bottom: 0.5px solid var(--rule);
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  flex-shrink: 0;
  background: var(--bg-elevated);
}
.aichat-header-info { flex: 1; }
#aichat-header h3 {
  margin: 0;
  font-family: var(--font-serif);
  font-size: 16px;
  font-weight: 700;
  color: var(--text-primary);
  letter-spacing: 0.02em;
}
#aichat-header .meta {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 4px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

/* ===== Tabs ===== */
#aichat-tabs {
  display: flex;
  border-bottom: 0.5px solid var(--rule);
  background: var(--bg-elevated);
  flex-shrink: 0;
}
.aichat-tab {
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  padding: 10px 16px;
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  letter-spacing: 0.04em;
  transition: color 0.15s, border-color 0.15s;
}
.aichat-tab:hover { color: var(--text-secondary); }
.aichat-tab[aria-selected="true"] {
  color: var(--accent-primary);
  border-bottom-color: var(--accent-primary);
}

/* ===== View切替 ===== */
.aichat-view {
  display: none;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}
.aichat-view[data-active="true"] { display: flex; }

/* ===== 現在の会話タイトルバー ===== */
#aichat-current-bar {
  padding: 8px 18px;
  border-bottom: 0.5px solid var(--rule);
  background: var(--bg-stripe);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
#aichat-current-title {
  font-family: var(--font-serif);
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#aichat-new-btn {
  background: none;
  border: 0.5px solid var(--rule-strong);
  color: var(--text-secondary);
  font-family: var(--font-sans);
  font-size: 11px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: var(--radius-soft);
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
  flex-shrink: 0;
}
#aichat-new-btn:hover {
  color: var(--accent-primary);
  border-color: var(--accent-primary);
}
#aichat-close {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 22px;
  cursor: pointer;
  padding: 0 4px;
  line-height: 1;
  transition: color 0.15s;
}
#aichat-close:hover { color: var(--accent-primary); }

/* ===== Messages Area ===== */
#aichat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 18px;
  scroll-behavior: smooth;
}

.aichat-msg { margin-bottom: 18px; word-wrap: break-word; }

/* ユーザー質問 */
.aichat-msg-user {
  background: var(--chip);
  border-left: 3px solid var(--accent-primary);
  padding: 10px 14px;
  border-radius: 0 var(--radius-soft) var(--radius-soft) 0;
  color: var(--text-primary);
  font-weight: 500;
}

/* ステータス表示 */
.aichat-msg-status {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
  letter-spacing: 0.03em;
  padding: 4px 0;
}

/* エラー */
.aichat-msg-error {
  background: rgba(232, 120, 144, 0.08);
  color: var(--accent-danger);
  border-left: 3px solid var(--accent-danger);
  padding: 10px 14px;
  border-radius: 0 var(--radius-soft) var(--radius-soft) 0;
}

/* AI 回答本文 */
.aichat-msg-answer {
  color: var(--text-primary);
  white-space: normal;
  font-family: var(--font-sans);
}
.aichat-msg-answer h2 {
  font-family: var(--font-serif);
  font-size: 13px;
  font-weight: 700;
  color: var(--accent-primary);
  margin: 14px 0 6px;
  padding-bottom: 4px;
  border-bottom: 0.5px solid var(--rule);
  letter-spacing: 0.02em;
}
.aichat-msg-answer h3 {
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 700;
  color: var(--text-primary);
  margin: 12px 0 4px;
}
.aichat-msg-answer p {
  margin: 6px 0;
  line-height: 1.55;
}
.aichat-msg-answer ul,
.aichat-msg-answer ol {
  margin: 6px 0;
  padding-left: 22px;
}
.aichat-msg-answer li {
  margin: 3px 0;
  line-height: 1.5;
}
.aichat-msg-answer strong { color: var(--text-primary); font-weight: 700; }
.aichat-msg-answer a {
  color: var(--accent-blue);
  text-decoration: none;
  border-bottom: 1px dotted var(--accent-blue);
}
.aichat-msg-answer a:hover {
  color: var(--accent-primary);
  border-bottom-color: var(--accent-primary);
}

/* ===== Markdown テーブル (AI回答内) ===== */
.aichat-msg-answer .aichat-table {
  width: 100%;
  border-collapse: collapse;
  margin: 10px 0;
  font-size: 11px;
  /* チャット背景 (--bg-paper #1a1815) より少し明るく差をつける */
  background: var(--bg-elevated);
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  border: 0.5px solid var(--rule);
}
.aichat-msg-answer .aichat-table th,
.aichat-msg-answer .aichat-table td {
  border: 0.5px solid var(--rule);
  padding: 6px 8px;
  text-align: left;
  vertical-align: top;
  line-height: 1.45;
  color: var(--text-primary);
}
.aichat-msg-answer .aichat-table th {
  /* ヘッダ: アンバーアクセントの温度感で区別（DS主色 #ffb84d の薄オーバーレイ） */
  background: rgba(255, 184, 77, 0.18);
  font-weight: 700;
  color: var(--accent-primary);
  font-size: 10.5px;
  letter-spacing: 0.02em;
  white-space: nowrap;
  border-bottom: 1px solid rgba(255, 184, 77, 0.42);
}
/* 偶数行の微細な濃淡 (ダークモード用) */
.aichat-msg-answer .aichat-table tbody tr:nth-child(even) {
  background: rgba(255, 255, 255, 0.025);
}
.aichat-msg-answer .aichat-table tbody tr:hover {
  background: rgba(255, 184, 77, 0.06);
}
/* テーブル内のリンク・引用 */
.aichat-msg-answer .aichat-table a {
  border-bottom: none;
}
/* スマホでは横スクロール */
@media (max-width: 600px) {
  .aichat-msg-answer .aichat-table {
    display: block;
    overflow-x: auto;
    white-space: nowrap;
  }
}

/* ===== Citations Block (汎用 .card-link を内包) ===== */
.aichat-citations {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 0.5px solid var(--rule);
}

/* 引用元カードの先頭番号 [1] [2] [3] ... */
.aichat-citation-num {
  display: inline-block;
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 700;
  color: var(--accent-primary);
  margin-right: 8px;
  letter-spacing: 0.02em;
  vertical-align: baseline;
}

/* 本文中の引用番号 [1] [2] のリンク */
.aichat-cite-link {
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 600;
  text-decoration: none;
  color: var(--accent-primary);
  border-bottom: none !important;
  padding: 0 1px;
}
.aichat-cite-link:hover {
  background: rgba(255, 184, 77, 0.18);
  color: var(--accent-primary);
  border-radius: 2px;
}

/* citations イベント未到着時の暫定番号表示 */
.aichat-cite-pending {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
}

/* ===== Input Area ===== */
#aichat-input-wrap {
  padding: 12px 18px;
  border-top: 0.5px solid var(--rule);
  flex-shrink: 0;
  display: flex;
  gap: 8px;
  background: var(--bg-elevated);
}
#aichat-input {
  flex: 1;
  background: var(--bg-paper);
  color: var(--text-primary);
  border: 0.5px solid var(--rule-strong);
  border-radius: var(--radius-soft);
  padding: 9px 12px;
  font-size: 14px;
  font-family: var(--font-sans);
  resize: none;
  min-height: 40px;
  max-height: 120px;
  line-height: 1.5;
  transition: border-color 0.15s;
}
#aichat-input::placeholder {
  color: var(--text-muted);
  font-size: 13px;
}
#aichat-input:focus {
  outline: none;
  border-color: var(--accent-primary);
}
#aichat-send {
  background: var(--accent-primary);
  color: var(--bg-primary);
  border: none;
  border-radius: var(--radius-soft);
  padding: 0 16px;
  cursor: pointer;
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.04em;
  flex-shrink: 0;
  transition: background 0.15s;
}
#aichat-send:hover { background: #ffc66e; }
#aichat-send:disabled {
  background: var(--chip);
  color: var(--text-muted);
  cursor: not-allowed;
}

/* ===== Suggest Block (汎用 .chip-button を内包) ===== */
#aichat-suggest {
  padding: 0 18px 14px;
  flex-shrink: 0;
  border-top: 0.5px solid var(--rule);
  padding-top: 12px;
}

/* ===== History View ===== */
#aichat-search-bar {
  padding: 10px 18px;
  border-bottom: 0.5px solid var(--rule);
  flex-shrink: 0;
  background: var(--bg-elevated);
}
#aichat-search-input {
  width: 100%;
  background: var(--bg-paper);
  color: var(--text-primary);
  border: 0.5px solid var(--rule-strong);
  border-radius: var(--radius-soft);
  padding: 8px 10px;
  font-family: var(--font-sans);
  font-size: 13px;
  transition: border-color 0.15s;
}
#aichat-search-input:focus {
  outline: none;
  border-color: var(--accent-primary);
}
#aichat-search-input::placeholder {
  color: var(--text-muted);
}

#aichat-history-list {
  flex: 1;
  overflow-y: auto;
  scroll-behavior: smooth;
}

.aichat-history-item {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 12px 18px;
  border-bottom: 0.5px solid var(--rule);
  cursor: pointer;
  transition: background 0.15s;
}
.aichat-history-item:hover { background: var(--bg-elevated); }
.aichat-history-main { flex: 1; min-width: 0; }
.aichat-history-title {
  font-family: var(--font-serif);
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
}
.aichat-history-meta {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 3px;
  letter-spacing: 0.02em;
}
.aichat-history-hit {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 6px;
  line-height: 1.5;
  padding-left: 8px;
  border-left: 1px solid var(--rule);
}
.aichat-history-hit-role {
  display: inline-block;
  background: var(--chip);
  color: var(--accent-primary);
  font-family: var(--font-mono);
  font-size: 9px;
  font-weight: 700;
  padding: 1px 4px;
  border-radius: 2px;
  margin-right: 4px;
  letter-spacing: 0.04em;
}
.aichat-history-hit mark {
  background: rgba(255, 184, 77, 0.25);
  color: var(--accent-primary);
  padding: 0 2px;
  border-radius: 2px;
}
.aichat-history-del {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 18px;
  cursor: pointer;
  padding: 0 6px;
  line-height: 1;
  flex-shrink: 0;
  border-radius: var(--radius-soft);
  transition: color 0.15s, background 0.15s;
}
.aichat-history-del:hover {
  color: var(--accent-danger);
  background: rgba(232, 120, 144, 0.1);
}

/* 過去メッセージ復元時の引用元表示（IDのみ、リンク化不可） */
.card-link--archived {
  background: var(--bg-paper);
  border-left-color: var(--text-muted);
  cursor: default;
  padding: 4px 12px;
  margin-bottom: 3px;
}
.card-link--archived:hover {
  background: var(--bg-paper);
  border-left-color: var(--text-muted);
  transform: none;
}

/* ===== Mobile ===== */
@media (max-width: 600px) {
  #aichat-panel {
    right: 8px;
    bottom: 80px;
    width: calc(100vw - 16px);
    height: calc(100vh - 100px);
  }
  #aichat-fab { right: 16px; bottom: 16px; }
}

/* ===== Scrollbar (subtle) ===== */
#aichat-messages::-webkit-scrollbar { width: 6px; }
#aichat-messages::-webkit-scrollbar-track { background: transparent; }
#aichat-messages::-webkit-scrollbar-thumb {
  background: var(--rule-strong);
  border-radius: 3px;
}
#aichat-messages::-webkit-scrollbar-thumb:hover {
  background: var(--accent-primary);
}
