:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface2: #334155;
  --border: #475569;
  --text: #e2e8f0;
  --text-dim: #94a3b8;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --success: #22c55e;
  --warning: #eab308;
  --danger: #ef4444;
}
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans SC', sans-serif;
  background: var(--bg); color: var(--text); min-height: 100vh;
  display: flex; justify-content: center;
}
.container { width: 100%; max-width: 1200px; padding: 20px; }
header {
  display: flex; align-items: center; gap: 12px;
  padding: 20px 0; border-bottom: 1px solid var(--border); margin-bottom: 18px;
}
header h1 { font-size: 22px; font-weight: 600; flex: 1; }
.badge {
  background: var(--surface2); color: var(--text-dim);
  padding: 4px 10px; border-radius: 6px; font-size: 12px;
}

/* Tabs */
.tab-bar {
  display: flex; gap: 0; margin-bottom: 0;
  border-bottom: 2px solid var(--border);
}
.tab-btn {
  padding: 10px 20px; background: transparent; border: none;
  color: var(--text-dim); font-size: 14px; font-weight: 500;
  cursor: pointer; border-bottom: 2px solid transparent;
  margin-bottom: -2px; transition: all 0.2s;
}
.tab-btn:hover { color: var(--text); background: rgba(255,255,255,0.03); }
.tab-btn.active { color: var(--accent); border-bottom-color: var(--accent); }
.tab-content { display: none; }
.tab-content.active { display: block; }

/* Config bar */
.config-bar {
  display: flex; gap: 12px; align-items: center;
  margin: 16px 0; flex-wrap: wrap;
}
.config-bar label { font-size: 13px; color: var(--text-dim); }
.config-bar select, .config-bar input {
  background: var(--surface); border: 1px solid var(--border);
  color: var(--text); padding: 6px 10px; border-radius: 6px; font-size: 13px;
}
.config-bar select:focus, .config-bar input:focus { outline: none; border-color: var(--accent); }
.config-bar input[type="number"] { width: 70px; }
.config-bar .stat { margin-left: auto; font-size: 12px; color: var(--text-dim); }

/* Input */
.input-area { display: flex; gap: 10px; margin-bottom: 16px; }
.input-area input {
  flex: 1; background: var(--surface); border: 1px solid var(--border);
  color: var(--text); padding: 12px 16px; border-radius: 8px; font-size: 15px;
}
.input-area input:focus { outline: none; border-color: var(--accent); }
.input-area input::placeholder { color: var(--text-dim); }

/* Buttons */
.btn {
  padding: 10px 22px; border: none; border-radius: 8px;
  font-size: 14px; font-weight: 500; cursor: pointer;
  transition: all 0.2s; white-space: nowrap;
}
.btn-primary { background: var(--accent); color: white; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-outline { background: transparent; border: 1px solid var(--border); color: var(--text); }
.btn-small { padding: 5px 10px; font-size: 12px; }
.btn-sm { padding: 4px 10px; font-size: 12px; border-radius: 6px; border: 1px solid var(--border); background: transparent; color: var(--text-dim); cursor: pointer; }
.btn-sm:hover { background: var(--surface2); color: var(--text); }

/* Layout (Tab1 search) */
.layout { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
@media (max-width: 800px) { .layout { grid-template-columns: 1fr; } }

/* Panels */
.panel {
  background: var(--surface); border-radius: 12px;
  border: 1px solid var(--border); overflow: hidden;
}
.panel-header {
  padding: 12px 16px; border-bottom: 1px solid var(--border);
  font-size: 13px; font-weight: 500; color: var(--text-dim);
  display: flex; justify-content: space-between; align-items: center;
}
.panel-body { padding: 16px; min-height: 150px; }

/* Empty state */
.empty-state { color: var(--text-dim); text-align: center; padding: 40px 20px; font-size: 14px; }
.empty-state .icon { font-size: 40px; margin-bottom: 12px; }

/* Hit cards */
.hit-card {
  background: var(--bg); border-radius: 8px; padding: 12px;
  margin-bottom: 10px; border-left: 3px solid var(--accent);
}
.hit-card:last-child { margin-bottom: 0; }
.hit-card .score {
  display: inline-block; padding: 2px 6px; border-radius: 4px;
  font-size: 11px; font-weight: 600; margin-bottom: 6px;
}
.score-high { background: #166534; color: #4ade80; }
.score-mid { background: #854d0e; color: #facc15; }
.score-low { background: #7f1d1d; color: #fca5a5; }
.hit-card .meta { font-size: 11px; color: var(--text-dim); margin-bottom: 6px; word-break: break-all; }
.hit-card .meta span { display: inline-block; margin-right: 10px; }
.hit-card .doc {
  font-size: 13px; line-height: 1.6; color: var(--text);
  max-height: 80px; overflow-y: auto; word-break: break-word;
}
.hit-card .doc::-webkit-scrollbar { width: 4px; }
.hit-card .doc::-webkit-scrollbar-thumb { background: var(--surface2); border-radius: 2px; }

/* Answer */
.answer-area { font-size: 14px; line-height: 1.7; }
.answer-area p { margin-bottom: 8px; }
.answer-area ul, .answer-area ol { margin: 8px 0; padding-left: 20px; }
.answer-area li { margin-bottom: 4px; }
.answer-area strong { color: var(--accent); }
.answer-area code { background: var(--surface2); padding: 2px 6px; border-radius: 4px; font-size: 13px; font-family: 'SF Mono', 'Fira Code', monospace; }
.answer-area pre { background: var(--bg); padding: 12px; border-radius: 8px; overflow-x: auto; margin: 8px 0; border: 1px solid var(--border); }
.answer-area pre code { background: none; padding: 0; }
.answer-area h1, .answer-area h2, .answer-area h3 { margin: 12px 0 6px; }
.answer-area hr { border: none; border-top: 1px solid var(--border); margin: 12px 0; }
.answer-loading { color: var(--text-dim); animation: pulse 1.5s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: 0.4; } 50% { opacity: 1; } }

/* History */
.history-item { padding: 10px 12px; border-bottom: 1px solid var(--border); cursor: pointer; transition: background 0.15s; }
.history-item:hover { background: var(--surface2); }
.history-item:last-child { border-bottom: none; }
.history-item .q { font-size: 13px; margin-bottom: 2px; }
.history-item .info { font-size: 11px; color: var(--text-dim); display: flex; gap: 8px; }
.history-item .hits-count { color: var(--success); }
.tag { display: inline-block; padding: 2px 6px; border-radius: 4px; font-size: 10px; font-weight: 500; }
.tag-blog { background: #1e3a5f; color: #93c5fd; }
.tag-product { background: #3b0764; color: #d8b4fe; }
.tag-doc { background: #14532d; color: #86efac; }
.tag-guide { background: #422006; color: #fde68a; }

/* ====== Tab2: Data Browser ====== */
.browser-filters { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.browser-filters select, .browser-filters input {
  background: var(--surface); border: 1px solid var(--border);
  color: var(--text); padding: 5px 8px; border-radius: 6px; font-size: 13px;
}
.browser-filters select:focus, .browser-filters input:focus { outline: none; border-color: var(--accent); }
.browser-table-wrap { overflow-x: auto; }
.browser-table {
  width: 100%; border-collapse: collapse; font-size: 13px;
}
.browser-table th {
  text-align: left; padding: 8px 10px;
  border-bottom: 2px solid var(--border);
  color: var(--text-dim); font-weight: 500; white-space: nowrap;
  position: sticky; top: 0; background: var(--surface); z-index: 1;
}
.browser-table td { padding: 7px 10px; border-bottom: 1px solid var(--border); }
.browser-table tr:hover td { background: rgba(59,130,246,0.05); }
.browser-table tr { cursor: pointer; }
.browser-pagination {
  display: flex; gap: 6px; align-items: center;
  justify-content: center; padding: 12px 0;
}
.browser-pagination .page-info { font-size: 12px; color: var(--text-dim); }
.browser-pagination button { min-width: 32px; }
.type-badge { display: inline-block; padding: 1px 6px; border-radius: 4px; font-size: 11px; font-weight: 500; }
.type-parent { background: #1e3a5f; color: #93c5fd; }
.type-child { background: #14532d; color: #86efac; }

/* ====== Tab3: Doc Detail ====== */
.detail-toolbar { display: flex; gap: 8px; align-items: center; margin-bottom: 12px; flex-wrap: wrap; }
.detail-toolbar .back-btn { cursor: pointer; color: var(--accent); font-size: 14px; }
.detail-toolbar .back-btn:hover { text-decoration: underline; }
.detail-metadata { background: var(--surface); border-radius: 8px; border: 1px solid var(--border); padding: 12px; margin-bottom: 16px; }
.detail-metadata table { width: 100%; border-collapse: collapse; font-size: 13px; }
.detail-metadata td { padding: 4px 8px; color: var(--text); vertical-align: top; }
.detail-metadata td:first-child { color: var(--text-dim); white-space: nowrap; width: 100px; }
.detail-content {
  background: var(--surface); border-radius: 8px; border: 1px solid var(--border);
  padding: 16px; line-height: 1.7; font-size: 14px; white-space: pre-wrap; word-break: break-word;
  max-height: 500px; overflow-y: auto;
}
.detail-content::-webkit-scrollbar { width: 6px; }
.detail-content::-webkit-scrollbar-thumb { background: var(--surface2); border-radius: 3px; }

/* Parent-child tree */
.relation-tree { margin-top: 16px; }
.relation-tree h3 { font-size: 14px; color: var(--text-dim); margin-bottom: 8px; }
.relation-card {
  background: var(--surface); border-radius: 8px; border: 1px solid var(--border);
  padding: 10px 12px; margin-bottom: 6px; cursor: pointer; transition: all 0.15s;
}
.relation-card:hover { border-color: var(--accent); }
.relation-card.parent-card { border-left: 3px solid var(--accent); }
.relation-card.child-card { border-left: 3px solid var(--success); margin-left: 20px; }
.relation-card.current { border-color: var(--warning); }
.relation-card .rel-label { font-size: 11px; color: var(--text-dim); margin-bottom: 3px; }
.relation-card .rel-preview { font-size: 13px; color: var(--text); max-height: 40px; overflow: hidden; }

/* Loading spinner */
.spinner { text-align: center; padding: 40px; color: var(--text-dim); }
.spinner::after { content: '⏳'; font-size: 32px; animation: spin 1s linear infinite; display: block; margin-bottom: 8px; }
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }

/* ====== Tab 4: Article Writing ====== */

/* Top: Input area */
.write-input-area {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 16px; margin-bottom: 16px;
}
.write-input-grid {
  display: grid; grid-template-columns: 1fr 2fr 1fr; gap: 16px;
}
.write-section { }
.write-label { display: block; font-size: 13px; font-weight: 600; color: var(--text); margin-bottom: 6px; }
.write-label .required { color: var(--danger); }
.write-input, .write-textarea, .write-url-input, .write-select {
  width: 100%; box-sizing: border-box;
  background: var(--bg); border: 1px solid var(--border); border-radius: 6px;
  color: var(--text); font-size: 14px; padding: 8px 10px;
  transition: border-color 0.15s;
}
.write-input:focus, .write-textarea:focus, .write-url-input:focus, .write-select:focus {
  outline: none; border-color: var(--accent);
}
.write-textarea { resize: vertical; min-height: 60px; font-family: inherit; line-height: 1.5; }
.write-url-list { display: flex; flex-direction: column; gap: 6px; }
.write-url-row { display: flex; gap: 6px; align-items: center; }
.write-url-row .write-url-input { flex: 1; }
.write-url-row .btn { flex-shrink: 0; padding: 4px 8px; font-size: 14px; }
.write-config-row { display: flex; gap: 16px; }
.write-config-item { flex: 1; min-width: 180px; }
.write-config-item label { display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 4px; }

/* Actions row */
.write-input-actions {
  display: flex; align-items: center; gap: 16px; margin-top: 14px;
  padding-top: 14px; border-top: 1px solid var(--border);
}
.write-generate-btn { padding: 10px 32px; font-size: 15px; flex-shrink: 0; }
.write-status { font-size: 13px; min-height: 20px; }
.write-status.error { color: var(--danger); }
.write-status.success { color: var(--success); }

/* Bottom: Output area */
.write-output-area { }

/* Article output */
.article-content {
  line-height: 1.8; font-size: 14px; word-break: break-word;
  white-space: pre-wrap;
}
.article-content h1, .article-content h2, .article-content h3 {
  margin: 16px 0 8px; color: var(--accent);
}
.article-content h1 { font-size: 18px; }
.article-content h2 { font-size: 16px; border-bottom: 1px solid var(--border); padding-bottom: 4px; }
.article-content h3 { font-size: 14px; }
.article-content p { margin: 8px 0; }
.article-content ul, .article-content ol { margin: 6px 0; padding-left: 20px; }
.article-content li { margin: 3px 0; }
.article-content strong { color: var(--accent); }
.article-content code {
  background: var(--surface2); padding: 1px 5px; border-radius: 3px; font-size: 13px;
}

/* Meta row: 3 panels side by side */
.write-output-meta {
  display: flex; gap: 12px; margin-top: 12px;
}
.write-meta-panel { flex: 1; min-width: 0; }
.write-meta-panel .panel-body {
  max-height: 160px; overflow-y: auto; font-size: 13px;
}
.write-meta-panel .panel-body::-webkit-scrollbar { width: 5px; }
.write-meta-panel .panel-body::-webkit-scrollbar-thumb { background: var(--surface2); border-radius: 3px; }

/* Source item in meta panels */
.meta-source-item {
  padding: 5px 0; font-size: 12px; display: flex; align-items: center; gap: 8px;
  border-bottom: 1px solid var(--border);
}
.meta-source-item:last-child { border-bottom: none; }
.meta-source-item .tag { flex-shrink: 0; font-size: 11px; }
.meta-source-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.meta-source-cat { color: var(--text-dim); font-size: 11px; flex-shrink: 0; }
.ks-expand { cursor: pointer; font-size: 12px; opacity: 0.6; margin-left: auto; flex-shrink: 0; }
.ks-expand:hover { opacity: 1; }
.ks-doc-preview {
  font-size: 11px; line-height: 1.5; color: var(--text-dim);
  padding: 6px 8px; margin-top: 4px; background: var(--surface);
  border-radius: 4px; width: 100%; word-break: break-all;
}

/* Style reference in meta */
.style-ref-item {
  padding: 3px 0; font-size: 12px;
  border-bottom: 1px solid var(--border);
}
.style-ref-text {
  font-size: 11px; line-height: 1.4; color: var(--text-dim);
  padding: 4px 0 2px; font-style: italic;
}
.style-ref-item:last-child { border-bottom: none; }
.style-ref-type { font-weight: 600; color: var(--accent); margin-right: 6px; }
.style-ref-source { color: var(--text-dim); font-size: 11px; }

/* Fact check in meta */
.fact-check-summary { display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.fact-check-stat { font-size: 12px; padding: 4px 10px; background: var(--surface); border-radius: 6px; }
.fact-check-num { font-weight: 700; font-size: 15px; }
.fact-check-stat.confirmed { color: var(--success); }
.fact-check-stat.unconfirmed { color: var(--warning); }
.fact-check-item {
  padding: 3px 0; font-size: 11px; line-height: 1.4;
  border-bottom: 1px solid var(--border);
}
.fact-check-item:last-child { border-bottom: none; }

/* Reasoning panel */
.write-reasoning-panel { margin-top: 8px; }
.reasoning-content {
  font-size: 12px;
  line-height: 1.6;
  color: var(--text-dim);
  white-space: pre-wrap;
  font-family: 'SF Mono', 'Consolas', 'Monaco', monospace;
  padding: 12px;
  background: var(--surface);
  border-radius: 6px;
  max-height: 300px;
  overflow-y: auto;
}
.reasoning-content::-webkit-scrollbar { width: 6px; }
.reasoning-content::-webkit-scrollbar-thumb { background: var(--surface2); border-radius: 3px; }

/* Token panel */
.write-token-panel { margin-top: 8px; }
.write-prompt-panel { margin-top: 8px; }
.write-queries-panel { margin-top: 8px; }
.queries-list { padding: 8px 12px; }
.query-item {
  font-size: 12px; padding: 4px 0; color: var(--text);
  font-family: 'SF Mono', 'Consolas', monospace;
}
.token-info {
  display: flex; gap: 24px; padding: 8px 12px;
  font-size: 13px; color: var(--text);
}
.token-info span { white-space: nowrap; }

/* History */
.write-history-panel {
  margin-top: 12px;
}
.write-history-list {
  max-height: 400px;
  overflow-y: auto;
  padding: 4px 0;
}
.write-history-list::-webkit-scrollbar { width: 6px; }
.write-history-list::-webkit-scrollbar-thumb { background: var(--surface2); border-radius: 3px; }
.history-item {
  padding: 10px 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background 0.15s;
}
.history-item:last-child { border-bottom: none; }
.history-item:hover { background: var(--surface2); }
.history-item-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}
.history-topic {
  font-weight: 600;
  font-size: 13px;
  color: var(--text);
}
.history-meta {
  font-size: 11px;
  color: var(--text-dim);
}
.history-preview {
  font-size: 12px;
  color: var(--text-dim);
  line-height: 1.4;
  margin-bottom: 2px;
}
.history-time {
  font-size: 11px;
  color: var(--text-dim);
  opacity: 0.7;
}

/* Article body scroll */
.write-article-body { max-height: 400px; overflow-y: auto; }
.write-article-body::-webkit-scrollbar { width: 6px; }
.write-article-body::-webkit-scrollbar-thumb { background: var(--surface2); border-radius: 3px; }

/* ====== Timeline Progress Bar ====== */
.timeline-bar {
  padding: 20px 32px 12px;
  position: relative;
}
.timeline-track {
  display: flex;
  justify-content: space-between;
  position: relative;
  z-index: 1;
}
.timeline-step {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  flex: 1;
  position: relative;
}
.tl-dot {
  width: 28px; height: 28px;
  border-radius: 50%;
  background: var(--surface2);
  border: 2px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  font-size: 13px;
  transition: all 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
  position: relative;
}
/* Step states */
.timeline-step.done .tl-dot {
  background: var(--success);
  border-color: var(--success);
}
.timeline-step.done .tl-dot::after {
  content: "✓";
  color: #fff;
  font-weight: 700;
  font-size: 14px;
}
.timeline-step.active .tl-dot {
  background: var(--accent);
  border-color: var(--accent);
  box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.2);
  animation: tl-pulse 1.5s ease-in-out infinite;
}
.timeline-step.active .tl-dot::after {
  content: "";
  width: 10px; height: 10px;
  background: #fff;
  border-radius: 50%;
  animation: tl-breathe 1.5s ease-in-out infinite;
}
.timeline-step.pending .tl-dot {
  background: var(--surface2);
  border-color: var(--border);
  opacity: 0.4;
}

@keyframes tl-pulse {
  0%, 100% { box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.2); }
  50% { box-shadow: 0 0 0 8px rgba(59, 130, 246, 0.08); }
}
@keyframes tl-breathe {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(0.6); opacity: 0.5; }
}

/* Connector line (the bar connecting dots) */
.timeline-connector {
  position: absolute;
  top: 34px; left: 48px; right: 48px;
  height: 2px;
  background: var(--border);
  z-index: 0;
  overflow: hidden;
}
.timeline-connector::after {
  content: "";
  display: block;
  height: 100%;
  background: var(--accent);
  transition: width 0.5s ease;
  width: var(--connector-fill, 0%);
}
.timeline-bar.progress-1 .timeline-connector::after { width: 0%; }
.timeline-bar.progress-2 .timeline-connector::after { width: 50%; }
.timeline-bar.progress-3 .timeline-connector::after { width: 100%; }

/* Labels */
.tl-label {
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap;
  transition: color 0.3s ease;
}
.timeline-step.active .tl-label { color: var(--accent); font-weight: 600; }
.timeline-step.done .tl-label { color: var(--success); }

/* Status text under label */
.tl-status {
  font-size: 11px;
  color: var(--text-dim);
  min-height: 16px;
  text-align: center;
}
.timeline-step.active .tl-status { color: var(--accent); }
.timeline-step.done .tl-status { color: var(--success); }
