/* ===== 小龙虾-α 知识引擎 — 搜索页面样式 v2 ===== */
/* 暗色主题 + 移动端优先适配 */

:root {
  --bg-primary: #f5f7fa;
  --bg-secondary: #ffffff;
  --bg-card: #ffffff;
  --bg-hover: #f0f4f8;
  --border-color: #e5e7eb;
  --text-primary: #1f2937;
  --text-secondary: #4b5563;
  --text-muted: #9ca3af;
  --accent: #2563eb;
  --accent-dim: rgba(37,99,235,0.08);
  --danger: #dc2626;
  --warning: #f59e0b;
  --success: #16a34a;
  --radius: 8px;
  --radius-sm: 4px;
  --shadow: 0 4px 12px rgba(0,0,0,0.08);
}

/* 亮色主题 + 移动端优先适配 */

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
               'Helvetica Neue', Arial, 'PingFang SC', 'Microsoft YaHei', sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ===== 容器 ===== */
.container {
  max-width: 800px;
  margin: 0 auto;
  padding: 16px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ===== 头部 ===== */
.header {
  text-align: center;
  padding: 24px 0 16px;
}
.header h1 {
  margin: 0 0 6px;
  font-size: 22px;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: 1px;
}
.header p {
  margin: 0;
  font-size: 13px;
  color: var(--text-secondary);
}

/* ===== 搜索框 ===== */
.search-box {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.input-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: stretch;
}

.input-group input[type="text"] {
  flex: 1 1 100%;
  min-width: 0;
  padding: 10px 14px;
  font-size: 15px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  background: var(--bg-secondary);
  color: var(--text-primary);
  outline: none;
  transition: border-color 0.2s;
}
.input-group input[type="text"]:focus {
  border-color: var(--accent);
}
.input-group input[type="text"]::placeholder {
  color: var(--text-muted);
}

.input-group select {
  flex: 1 1 auto;
  min-width: 140px;
  padding: 10px 12px;
  font-size: 14px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  background: var(--bg-secondary);
  color: var(--text-primary);
  outline: none;
  cursor: pointer;
}

.input-group button {
  flex: 0 0 auto;
  padding: 10px 22px;
  font-size: 15px;
  font-weight: 600;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: #000;
  cursor: pointer;
  transition: opacity 0.2s, transform 0.1s;
}
.input-group button:hover { opacity: 0.85; }
.input-group button:active { transform: scale(0.97); }
.input-group button:disabled {
  background: var(--text-muted);
  cursor: not-allowed;
  opacity: 0.5;
}

/* ===== 统计数字 ===== */
.stats {
  display: flex;
  justify-content: center;
  gap: 32px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border-color);
}
.stat-item { text-align: center; }
.stat-value {
  font-size: 20px;
  font-weight: 700;
  color: var(--accent);
}
.stat-label {
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* ===== 结果区域 ===== */
.results {
  flex: 1;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  min-height: 200px;
}
.results h3 {
  margin: 0 0 12px;
  font-size: 15px;
  color: var(--text-secondary);
  font-weight: 600;
}

/* ===== 加载状态 ===== */
.loading {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-secondary);
  font-size: 14px;
}
.loading-spinner {
  display: inline-block;
  width: 32px;
  height: 32px;
  border: 3px solid var(--border-color);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-bottom: 12px;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 8px;
}
.loading-dots::after {
  content: '';
  animation: dots 1.5s steps(4, end) infinite;
}
@keyframes dots {
  0% { content: ''; }
  25% { content: '.'; }
  50% { content: '..'; }
  75% { content: '...'; }
}
.loading-stage {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 8px;
  min-height: 18px;
}

/* ===== 错误提示 ===== */
.error {
  padding: 16px;
  color: var(--danger);
  background: rgba(220,38,38,0.05);
  border-radius: var(--radius);
  font-size: 13px;
}

/* ===== RAG 结果卡片 ===== */
.rag-result {
  animation: fadeIn 0.3s ease;
}
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

.rag-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border-color);
  flex-wrap: wrap;
}
.rag-header h3 {
  margin: 0;
  font-size: 16px;
  color: var(--text-primary);
  font-weight: 600;
  word-break: break-all;
}
.rag-header > div {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.level-badge {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 3px;
  background: var(--accent-dim);
  color: var(--accent);
  white-space: nowrap;
  font-weight: 600;
}
.conf-badge {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 3px;
  background: rgba(255,159,67,0.1);
  color: var(--warning);
  white-space: nowrap;
}

/* ===== AI 回答正文 ===== */
.rag-answer-text {
  font-size: 14px;
  line-height: 1.8;
  color: var(--text-primary);
  word-break: break-word;
  overflow-wrap: break-word;
}
.rag-answer-text p { margin: 0 0 10px; }
.rag-answer-text p:last-child { margin-bottom: 0; }

/* ===== 关键要点 ===== */
.rag-keypoints {
  margin-top: 16px;
  padding: 12px;
  background: var(--bg-secondary);
  border-radius: var(--radius-sm);
  border-left: 3px solid var(--accent);
}
.rag-keypoints h4 {
  margin: 0 0 8px;
  font-size: 13px;
  color: var(--accent);
}
.rag-keypoints ol {
  margin: 0;
  padding-left: 20px;
  font-size: 13px;
  color: var(--text-secondary);
}
.rag-keypoints li { margin: 4px 0; }

/* ===== 参考来源 ===== */
.rag-sources {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid var(--border-color);
}
.rag-sources h4 {
  margin: 0 0 10px;
  font-size: 13px;
  color: var(--text-secondary);
}
.source-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.source-tags span {
  font-size: 11px;
  padding: 3px 8px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ===== 来源图片展示 ===== */
.source-images-section {
  margin-top: 14px;
  padding: 12px;
  background: var(--bg-secondary);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-color);
}
.source-images-title {
  font-size: 12px;
  color: var(--accent);
  font-weight: 600;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.source-images-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 10px;
}
.source-image-card {
  background: var(--bg-primary);
  border-radius: var(--radius-sm);
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
  border: 1px solid var(--border-color);
}
.source-image-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.12);
  border-color: rgba(37,99,235,0.2);
}
.source-image-card img {
  width: 100%;
  height: 120px;
  object-fit: cover;
  display: block;
}
.source-image-caption {
  padding: 6px 8px;
  font-size: 11px;
  color: var(--text-secondary);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ===== 原始片段 ===== */
.raw-chunks {
  margin-top: 10px;
  padding: 12px;
  background: var(--bg-primary);
  border-radius: var(--radius-sm);
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.6;
  display: none; /* 默认折叠 */
}
.raw-chunks-hint {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 10px;
  font-style: italic;
}
.raw-chunk-item {
  margin-bottom: 10px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border-color);
}
.raw-chunk-item:last-child {
  margin-bottom: 0;
  padding-bottom: 0;
  border-bottom: none;
}
.chunk-title {
  font-size: 12px;
  color: var(--accent);
  margin-bottom: 4px;
  font-weight: 600;
}
.chunk-title span {
  color: var(--text-muted);
  font-weight: normal;
  font-size: 11px;
}
.chunk-text {
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.6;
}

/* ===== 延伸推荐 ===== */
.rag-suggestions {
  margin-top: 16px;
  padding: 12px;
  background: var(--bg-secondary);
  border-radius: var(--radius-sm);
}
.rag-suggestions h4 {
  margin: 0 0 8px;
  font-size: 13px;
  color: var(--warning);
}
.rag-suggestions ul {
  margin: 0;
  padding-left: 20px;
  font-size: 13px;
  color: var(--text-secondary);
}
.rag-suggestions li { margin: 4px 0; }

/* ===== Markdown 渲染 ===== */
.md-h1, .md-h2, .md-h3, .md-h4, .md-h5, .md-h6 {
  margin: 14px 0 8px;
  color: var(--accent);
  font-weight: 600;
}
.md-h1 { font-size: 18px; }
.md-h2 { font-size: 16px; }
.md-h3 { font-size: 15px; }
.md-h4 { font-size: 14px; }
.md-h5 { font-size: 13px; }
.md-h6 { font-size: 12px; }

.md-strong { color: var(--accent); font-weight: 600; }
.md-em { color: var(--text-secondary); font-style: italic; }
.md-del { color: var(--text-muted); text-decoration: line-through; }

.md-pre {
  background: var(--bg-primary);
  padding: 12px;
  border-radius: var(--radius-sm);
  overflow-x: auto;
  margin: 10px 0;
}
.md-code-block {
  font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
  font-size: 12px;
  color: #c2410c;
  line-height: 1.5;
}
.md-code-inline {
  background: rgba(0,0,0,0.05);
  padding: 1px 5px;
  border-radius: 3px;
  font-family: 'SF Mono', Monaco, monospace;
  font-size: 12px;
  color: #c2410c;
}

.md-table-wrap {
  overflow-x: auto;
  margin: 10px 0;
}
.md-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.md-table th, .md-table td {
  border: 1px solid var(--border-color);
  padding: 6px 10px;
  text-align: left;
}
.md-table th {
  background: var(--bg-hover);
  color: var(--accent);
  font-weight: 600;
}
.md-table td { color: var(--text-secondary); }

.md-blockquote {
  margin: 10px 0;
  padding: 8px 14px;
  border-left: 3px solid var(--accent);
  background: var(--bg-secondary);
  color: var(--text-secondary);
  font-size: 13px;
}

.md-ul, .md-ol {
  margin: 8px 0;
  padding-left: 20px;
}
.md-li { margin: 4px 0; color: var(--text-secondary); }

.md-link {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px solid rgba(37,99,235,0.3);
}
.md-link:hover { border-bottom-color: var(--accent); }

.md-img-wrap {
  margin: 10px 0;
  text-align: center;
}
.md-img {
  max-width: 100%;
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.md-img-err {
  display: none;
  color: var(--danger);
  font-size: 12px;
  padding: 10px;
}

.md-hr {
  border: none;
  border-top: 1px solid var(--border-color);
  margin: 16px 0;
}

/* ===== 折叠块 ===== */
.fold-block {
  margin: 10px 0;
  padding: 12px;
  background: var(--bg-secondary);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-color);
}
.fold-block[data-folded="true"] .fold-inner {
  max-height: 120px;
  overflow: hidden;
  position: relative;
}
.fold-block[data-folded="true"] .fold-inner::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 40px;
  background: linear-gradient(transparent, var(--bg-secondary));
}
.fold-btn {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  font-size: 12px;
  padding: 4px 0;
  margin-top: 4px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

/* ===== 流式输出 ===== */
#streamAnswer {
  font-size: 14px;
  line-height: 1.8;
  color: var(--text-primary);
  min-height: 40px;
}
.stream-token {
  animation: tokenFade 0.1s ease;
}
@keyframes tokenFade { from { opacity: 0; } to { opacity: 1; } }

/* ===== 页脚 ===== */
.footer {
  text-align: center;
  padding: 20px 0 8px;
  margin-top: auto;
  font-size: 12px;
  color: var(--text-muted);
}
.footer p { margin: 0 0 4px; }

/* ===== Lightbox ===== */
.img-lightbox {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.92);
  z-index: 9999;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  cursor: zoom-out;
}
.img-lightbox.active { display: flex; }
.img-lightbox img {
  max-width: 95vw;
  max-height: 85vh;
  object-fit: contain;
  border-radius: var(--radius-sm);
}
.lb-close {
  position: absolute;
  top: 16px;
  right: 20px;
  font-size: 32px;
  color: #fff;
  cursor: pointer;
  line-height: 1;
  opacity: 0.7;
  transition: opacity 0.2s;
}
.lb-close:hover { opacity: 1; }
.lb-hint {
  color: var(--text-muted);
  font-size: 12px;
  margin-top: 12px;
}

/* ===== 响应式 — 平板/大屏手机适配 ===== */
@media (max-width: 640px) {
  .container { padding: 12px; }
  .header { padding: 18px 0 12px; }
  .header h1 { font-size: 20px; }

  .search-box { padding: 12px; }
  .input-group input[type="text"] { font-size: 16px; }
  .input-group select { font-size: 14px; min-width: 120px; }
  .input-group button { padding: 10px 18px; font-size: 14px; }

  .stats { gap: 24px; }

  .md-table-wrap { -webkit-overflow-scrolling: touch; }
}

/* ===== 响应式 — 小屏手机适配 ===== */
@media (max-width: 480px) {
  .container { padding: 10px; }
  .header { padding: 14px 0 8px; }
  .header h1 { font-size: 18px; }
  .header p { font-size: 12px; }

  .search-box { padding: 10px; }
  .input-group {
    flex-direction: column;
    gap: 8px;
  }
  .input-group input[type="text"] {
    font-size: 16px; /* 防止 iOS 自动缩放 */
    width: 100%;
  }
  .input-group select {
    font-size: 14px;
    min-width: 0;
    width: 100%;
    flex: none;
  }
  .input-group button {
    width: 100%;
    padding: 10px 16px;
    font-size: 14px;
  }

  .stats { gap: 20px; }
  .stat-value { font-size: 18px; }

  .results { padding: 12px; }

  .rag-header {
    flex-direction: column;
    gap: 8px;
  }
  .rag-header > div {
    width: 100%;
  }
  .rag-header h3 { font-size: 14px; word-break: break-word; overflow-wrap: break-word; }

  .rag-answer-text { font-size: 13px; word-break: break-word; overflow-wrap: break-word; }
  .md-h1 { font-size: 16px; }
  .md-h2 { font-size: 15px; }
  .md-h3 { font-size: 14px; }
  .md-table-wrap { -webkit-overflow-scrolling: touch; }
  .md-table { font-size: 11px; }
  .md-table th, .md-table td { padding: 4px 6px; }

  .source-tags {
    gap: 6px;
  }
  .source-tags span {
    font-size: 10px;
    max-width: 100%;
    white-space: normal;
    word-break: break-word;
    overflow-wrap: break-word;
  }

  /* 移动端图片网格 */
  .source-images-grid {
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    gap: 6px;
  }
  .source-image-card img {
    height: 80px;
  }
  .source-image-caption {
    font-size: 10px;
    padding: 4px 6px;
  }

  .raw-chunks { padding: 8px; }
  .chunk-title { font-size: 11px; word-break: break-word; overflow-wrap: break-word; }
  .chunk-text { font-size: 11px; word-break: break-word; overflow-wrap: break-word; }

  .img-lightbox img {
    max-width: 95vw;
    max-height: 80vh;
  }
}
