/* ============================================================
   AI 查询 (聊天界面) — 跟随 app.css 设计令牌
   ============================================================ */

.tab-panel#tabAi {
  display: none;
}

.tab-panel#tabAi.active {
  display: block;
}

.ai-wrap {
  display: flex;
  flex-direction: column;
  height: calc(100dvh - 56px - 84px - env(safe-area-inset-bottom));
  padding: 10px 12px 0;
  box-sizing: border-box;
}

/* ---- 聊天记录区 ---- */
.ai-chat {
  flex: 1;
  overflow-y: auto;
  padding: 4px 2px 12px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
}

.ai-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.ai-msg {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  animation: ai-msg-in var(--dur) var(--ease);
}

@keyframes ai-msg-in {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.ai-msg.user {
  flex-direction: row-reverse;
}

.ai-avatar {
  flex: none;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  background: var(--accent-soft);
  color: var(--accent);
  border: 1px solid var(--border);
  user-select: none;
}

.ai-bubble {
  max-width: 82%;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  background: var(--card);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-xs);
  font-size: 14px;
  line-height: 1.65;
  color: var(--ink);
  overflow-wrap: break-word;
  word-break: break-word;
}

.ai-msg.user .ai-bubble {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: transparent;
  border-bottom-right-radius: 4px;
}

.ai-msg.assistant .ai-bubble {
  border-bottom-left-radius: 4px;
}

/* ---- 消息文本 (极简 markdown) ---- */
.ai-text > :first-child {
  margin-top: 0;
}
.ai-text > :last-child {
  margin-bottom: 0;
}
.ai-text p,
.ai-text .ai-p {
  margin: 6px 0;
}
.ai-text .ai-head {
  margin: 10px 0 4px;
  font-weight: 600;
}
.ai-text code {
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 12.5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.ai-msg.user .ai-text code {
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
}
.ai-text pre.ai-code {
  background: var(--accent-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px;
  overflow-x: auto;
  font-size: 12.5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre-wrap;
  word-break: break-word;
}
.ai-msg.user .ai-text pre.ai-code {
  background: rgba(255, 255, 255, 0.12);
}
.ai-text .ai-ul,
.ai-text .ai-ol {
  margin: 6px 0;
  padding-left: 20px;
}
.ai-text .ai-li {
  margin: 3px 0;
}
.ai-text strong {
  font-weight: 600;
}

/* ---- 消息内图片 ---- */
.ai-msg-img {
  margin-bottom: 8px;
}
.ai-msg-img img {
  max-width: 220px;
  max-height: 220px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  cursor: zoom-in;
  display: block;
}

/* ---- 命中猪卡片 ---- */
.ai-hits {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 8px;
  margin-top: 10px;
}

.ai-hit {
  background: var(--card-solid);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  box-shadow: var(--shadow-xs);
}

.ai-hit-img {
  width: 100%;
  aspect-ratio: 1;
  background: linear-gradient(var(--card-img-start), var(--card-img-end));
  border-radius: 6px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.ai-hit-img img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  cursor: pointer;
  transition: transform var(--dur) var(--ease);
}

.ai-hit-img img:hover {
  transform: scale(1.06);
}

.ai-hit-name {
  font-size: 12px;
  font-weight: 600;
  text-align: center;
  line-height: 1.3;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.ai-hit-pos {
  font-size: 10.5px;
  color: var(--muted);
  text-align: center;
  line-height: 1.3;
  min-height: 14px;
}

.ai-hit-btn {
  width: 100%;
  border: 1px solid var(--border-strong);
  background: transparent;
  color: var(--accent);
  font-size: 11.5px;
  padding: 4px 0;
  border-radius: var(--radius-pill);
  cursor: pointer;
  transition: background var(--dur) var(--ease), color var(--dur) var(--ease);
}

.ai-hit-btn:hover {
  background: var(--accent-soft);
}

.ai-hit-btn.owned {
  background: var(--accent);
  border-color: transparent;
  color: var(--accent-fg);
}

/* ---- 打字指示器 ---- */
.ai-typing {
  display: inline-flex;
  gap: 4px;
  padding: 4px 2px;
}

.ai-typing i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--muted);
  animation: ai-blink 1.2s infinite ease-in-out;
}

.ai-typing i:nth-child(2) {
  animation-delay: 0.15s;
}

.ai-typing i:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes ai-blink {
  0%, 80%, 100% {
    opacity: 0.25;
    transform: scale(0.85);
  }
  40% {
    opacity: 1;
    transform: scale(1);
  }
}

/* ---- 输入区 ---- */
.ai-composer {
  flex: none;
  padding: 8px 0 12px;
}

.ai-composer-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 6px 6px 6px 8px;
  box-shadow: var(--shadow);
  -webkit-backdrop-filter: blur(var(--glass-blur));
  backdrop-filter: blur(var(--glass-blur));
}

.ai-composer-row textarea {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--ink);
  font-size: 14px;
  line-height: 1.5;
  padding: 8px 4px;
  max-height: 120px;
  min-height: 22px;
  font-family: inherit;
}

.ai-composer-row textarea::placeholder {
  color: var(--faint);
}

.ai-attach-btn {
  flex: none;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: transparent;
  font-size: 17px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--dur) var(--ease);
}

.ai-attach-btn:hover {
  background: var(--accent-soft);
}

.ai-send-btn {
  flex: none;
  border: none;
  background: var(--accent);
  color: var(--accent-fg);
  font-size: 14px;
  font-weight: 600;
  padding: 9px 18px;
  border-radius: var(--radius-pill);
  cursor: pointer;
  transition: background var(--dur) var(--ease), transform var(--dur) var(--ease);
}

.ai-send-btn:hover {
  background: var(--accent-hover);
}

.ai-send-btn:active {
  transform: scale(0.96);
}

.ai-composer-foot {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 8px;
  padding: 6px 4px 0;
}

.ai-clear-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 11.5px;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: var(--radius-sm);
}

.ai-clear-btn:hover {
  color: var(--danger);
  background: var(--accent-soft);
}
