:root {
  --bg: #f6f7fb;
  --card: #ffffff;
  --ink: #1c2433;
  --muted: #6b7280;
  --border: #e5e7eb;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --success: #16a34a;
  --warn: #f59e0b;
  --danger: #dc2626;
  --rule-bg: #fff7ed;
  --rule-border: #fdba74;
  --customer-bg: #eef2ff;
  --cs-bg: #f0fdf4;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font: 15px/1.55 -apple-system, "Segoe UI", "Microsoft JhengHei", sans-serif;
  background: var(--bg); color: var(--ink);
}
.muted { color: var(--muted); }

/* ----- Topbar ----- */
.topbar {
  display: flex; align-items: center; gap: 24px;
  background: #fff; border-bottom: 1px solid var(--border);
  padding: 10px 20px;
}
.brand { font-size: 16px; }
.tabs { display: flex; gap: 4px; flex: 1; }
.tab {
  text-decoration: none; color: var(--muted);
  padding: 8px 16px; border-radius: 8px; font-weight: 500;
}
.tab:hover { background: #f3f4f6; }
.tab.active { background: #eff6ff; color: var(--primary); font-weight: 600; }
.status { display: flex; align-items: center; gap: 12px; font-size: 13px; }

main { padding: 16px 20px; max-width: 1400px; margin: 0 auto; }

/* ----- Tab panes ----- */
.tab-pane { display: none; }
.tab-pane.active { display: block; }

/* ----- Card ----- */
.card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: 12px; padding: 18px; margin-bottom: 16px;
}
.card h2 { font-size: 16px; margin: 0 0 8px; }

/* ----- Forms ----- */
textarea, input[type="text"], input[type="number"], select {
  border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 12px; font: inherit; background: #fff;
}
textarea { width: 100%; resize: vertical; min-height: 70px; }
input[type="text"] { width: 100%; }
label { font-size: 13px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-top: 10px; }

button {
  background: var(--primary); color: #fff; border: 0;
  padding: 9px 18px; border-radius: 8px; font: inherit; font-weight: 600;
  cursor: pointer;
}
button:hover { background: var(--primary-hover); }
button.ghost { background: transparent; color: var(--ink); border: 1px solid var(--border); }
button.ghost:hover { background: #f3f4f6; }
button.small { padding: 5px 10px; font-size: 13px; }
button.danger { background: var(--danger); }
button.danger:hover { background: #b91c1c; }

/* ----- Badges ----- */
.badge {
  display: inline-block; padding: 2px 9px; border-radius: 999px;
  font-size: 12px; font-weight: 600; line-height: 1.4;
  background: #f3f4f6; color: var(--muted);
}
.badge.high   { background: #dcfce7; color: var(--success); }
.badge.medium { background: #fef3c7; color: #b45309; }
.badge.low    { background: #fee2e2; color: var(--danger); }
.badge.year   { background: #dbeafe; color: var(--primary); }
.badge.warn   { background: #fef2f2; color: var(--danger); border: 1px solid #fecaca; }
.badge.on     { background: #dcfce7; color: var(--success); }
.badge.off    { background: #f3f4f6; color: var(--muted); }

/* ============== Chat layout ============== */
.chat-layout {
  display: grid;
  grid-template-columns: minmax(380px, 1fr) minmax(420px, 1.3fr);
  gap: 16px;
  align-items: start;
}
.chat-panel { display: flex; flex-direction: column; height: calc(100vh - 130px); }
.chat-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }

.chat-log {
  flex: 1; overflow-y: auto;
  background: #fafbfd; border: 1px solid var(--border);
  border-radius: 10px; padding: 12px;
  display: flex; flex-direction: column; gap: 10px;
}
.bubble {
  max-width: 85%; padding: 10px 14px; border-radius: 14px;
  white-space: pre-wrap; word-break: break-word; font-size: 14.5px;
}
.bubble.customer {
  background: var(--customer-bg); align-self: flex-start;
  border-bottom-left-radius: 4px;
}
.bubble.cs {
  background: var(--cs-bg); align-self: flex-end;
  border-bottom-right-radius: 4px;
}
.bubble .meta { font-size: 11px; color: var(--muted); margin-bottom: 4px; }

.chat-input { margin-top: 12px; }
.role-toggle { display: flex; gap: 6px; margin-bottom: 8px; }
.role-btn {
  background: #f3f4f6; color: var(--muted); border: 0;
  padding: 5px 12px; border-radius: 999px; font: inherit; font-size: 13px; font-weight: 500;
  cursor: pointer;
}
.role-btn.active[data-role="customer"] { background: var(--customer-bg); color: #4338ca; }
.role-btn.active[data-role="cs"]       { background: var(--cs-bg); color: var(--success); }

/* ----- Suggest panel ----- */
.suggest-panel { max-height: calc(100vh - 130px); overflow-y: auto; }
.suggest-header { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 10px; }

.draft-rule {
  background: var(--rule-bg); border: 1px solid var(--rule-border);
  border-radius: 10px; padding: 12px; margin-bottom: 10px;
}
.draft-rule .label { font-weight: 700; color: #c2410c; font-size: 13px; }
.draft-history {
  border-left: 4px solid var(--primary);
  padding: 10px 12px; background: #fafbfd;
  margin-bottom: 10px; border-radius: 0 8px 8px 0;
}
.draft-history.old { border-left-color: var(--muted); opacity: 0.85; }
.draft-history .meta { font-size: 11.5px; color: var(--muted); margin-bottom: 4px; }
.draft-history .meta .badge { margin-right: 3px; padding: 1px 7px; font-size: 11px; }
.draft-history .qa-q {
  color: var(--muted); font-size: 13px;
  padding-bottom: 5px; border-bottom: 1px dashed #e5e7eb; margin-bottom: 6px;
}
.draft-history .qa-q::before { content: "原問題: "; color: #9ca3af; }
.draft-actions { margin-top: 6px; display: flex; gap: 6px; }

.warn-banner {
  background: #fef2f2; border: 1px solid #fecaca; color: var(--danger);
  padding: 10px 14px; border-radius: 8px; margin-bottom: 12px; font-weight: 600;
  font-size: 13.5px;
}

.empty { text-align: center; color: var(--muted); padding: 40px; font-size: 14px; }

.section-title {
  font-weight: 700; color: var(--muted); font-size: 11px;
  text-transform: uppercase; letter-spacing: 0.05em; margin: 12px 0 6px;
}

/* ============== Rules layout ============== */
.rules-layout {
  display: grid;
  grid-template-columns: minmax(280px, 1fr) minmax(420px, 1.6fr);
  gap: 16px; align-items: start;
}

.rules-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }

#rules-table { display: flex; flex-direction: column; gap: 6px; max-height: 70vh; overflow-y: auto; }
.rule-row {
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px;
  cursor: pointer; background: #fafbfd;
}
.rule-row:hover { border-color: var(--primary); background: #fff; }
.rule-row.active { border-color: var(--primary); background: #eff6ff; }
.rule-row .id { font-weight: 600; font-size: 14px; }
.rule-row .scope { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.rule-row .badges { margin-top: 4px; display: flex; gap: 4px; flex-wrap: wrap; }

#rule-form .field { margin-bottom: 12px; }
#rule-form label { display: block; font-weight: 600; margin-bottom: 4px; font-size: 13px; }
#rule-form .help { color: var(--muted); font-size: 12px; margin-top: 2px; }
.checkbox-row { display: flex; gap: 16px; align-items: center; }
.checkbox-row label { display: flex; align-items: center; gap: 6px; font-weight: 400; }

.kw-tags { display: flex; gap: 6px; flex-wrap: wrap; padding: 6px; min-height: 40px; border: 1px solid var(--border); border-radius: 8px; background: #fff; }
.kw-tag {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 9px; background: #eff6ff; color: var(--primary);
  border-radius: 999px; font-size: 12.5px;
}
.kw-tag .x { cursor: pointer; color: var(--muted); font-weight: bold; }
.kw-tag .x:hover { color: var(--danger); }
.kw-input {
  border: 0; outline: 0; background: transparent;
  font: inherit; flex: 1; min-width: 100px;
}

.form-actions { display: flex; gap: 8px; margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--border); }

.test-result-rule {
  border: 1px solid var(--rule-border); background: var(--rule-bg);
  border-radius: 8px; padding: 10px 12px; margin-bottom: 8px;
}
.test-result-rule .id { font-weight: 700; }
.test-result-rule .kws { font-size: 13px; margin-top: 4px; }

/* ============== Query rewrite (Hybrid 區塊頂端) ============== */
.query-rewrite {
  background: #f0f9ff; border: 1px solid #bae6fd;
  border-radius: 8px; padding: 8px 10px; margin-bottom: 10px;
  font-size: 12.5px;
}
.qr-row { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.qr-tag {
  background: #0369a1; color: #fff;
  padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 700;
}
.qr-orig { color: var(--muted); }
.qr-arrow { color: #0369a1; font-weight: bold; }
.qr-rewritten {
  background: #fff; padding: 3px 9px; border-radius: 6px;
  border: 1px solid #bae6fd; color: #075985; font-weight: 600;
}
.qr-meta { margin-top: 4px; font-size: 12px; color: #374151; }
.qr-meta .qr-label {
  display: inline-block; min-width: 50px;
  color: var(--muted); font-weight: 600; margin-right: 6px;
}

/* ============== Context chips (對話已知) ============== */
.context-chips {
  background: #fff; border: 1px dashed #cbd5e1;
  border-radius: 10px; padding: 8px 10px 10px; margin-bottom: 12px;
}
.context-chips-label {
  font-size: 11px; font-weight: 700; color: var(--muted);
  text-transform: uppercase; letter-spacing: .05em; margin-bottom: 6px;
}
.context-chips-row { display: flex; flex-wrap: wrap; gap: 6px; }
.context-chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 4px 10px; border-radius: 999px; font-size: 12.5px;
  background: #eff6ff; color: #1e3a8a; border: 1px solid #c7d2fe;
}
.context-chip .chip-icon { font-size: 14px; }
.context-chip .chip-label { font-weight: 600; }
.context-chip .chip-value { color: #1e40af; }
.context-chip.chip-identity   { background: #fef3c7; color: #92400e; border-color: #fde68a; }
.context-chip.chip-cs_said    { background: #dcfce7; color: #166534; border-color: #bbf7d0; }
.context-chip.chip-personal_info { background: #fae8ff; color: #86198f; border-color: #f5d0fe; }
.context-chip.chip-order      { background: #e0f2fe; color: #075985; border-color: #bae6fd; }
.context-chip.chip-pending    { background: #fee2e2; color: #991b1b; border-color: #fecaca; }

/* ============== DCI step preview (展開看實際結果) ============== */
.step-preview {
  margin-top: 6px; padding: 6px 8px;
  background: #fff; border: 1px solid #e0e7ff;
  border-radius: 6px; font-family: -apple-system, "Segoe UI", "Microsoft JhengHei", sans-serif;
  font-size: 12px;
}
.step-preview summary {
  cursor: pointer; color: var(--primary); font-weight: 600;
  list-style: none; user-select: none;
}
.step-preview summary::-webkit-details-marker { display: none; }
.step-preview summary::before { content: '▶ '; font-size: 9px; }
.step-preview[open] summary::before { content: '▼ '; }
.step-preview ul {
  list-style: none; padding: 6px 0 0; margin: 6px 0 0;
  border-top: 1px dashed #e5e7eb;
}
.step-preview li {
  padding: 6px 4px; border-bottom: 1px solid #f3f4f6;
  font-size: 12.5px;
}
.step-preview li:last-child { border-bottom: none; }
.step-preview-q { color: #4b5563; margin-top: 3px; }
.step-preview-a { color: #1f2937; margin-top: 2px; padding-left: 12px;
  border-left: 2px solid var(--success); }
.step-preview-inline {
  margin-top: 6px; padding: 4px 8px; background: #fff;
  border-radius: 6px; font-size: 12.5px; display: flex; gap: 8px; flex-wrap: wrap;
}

/* ============== Feedback Dashboard ============== */
.dash-bars { margin-bottom: 18px; }
.dash-row {
  display: grid;
  grid-template-columns: 110px 1fr 180px;
  align-items: center; gap: 10px;
  padding: 6px 0;
}
.dash-label { font-size: 13px; font-weight: 600; }
.dash-value { font-size: 13px; text-align: right; }
.bar-wrap {
  background: #f3f4f6; border-radius: 999px;
  height: 12px; overflow: hidden;
}
.bar { height: 100%; border-radius: 999px; transition: width .3s; }

.dash-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 14px; margin-bottom: 18px;
}
.dash-list {
  background: #fafbfd; border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 14px;
}
.rank-list { list-style: none; padding: 0; margin: 6px 0 0; }
.rank-list li {
  display: flex; justify-content: space-between; align-items: center;
  padding: 5px 0; border-bottom: 1px solid #f3f4f6; font-size: 13px;
}
.rank-list li:last-child { border-bottom: none; }

.recent-table {
  width: 100%; border-collapse: collapse; font-size: 13px;
  background: #fff; border: 1px solid var(--border); border-radius: 8px;
  overflow: hidden;
}
.recent-table th, .recent-table td {
  text-align: left; padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}
.recent-table th { background: #f9fafb; font-weight: 700; color: var(--muted); }
.recent-table tr:last-child td { border-bottom: none; }

/* ============== Synthesis (3 個綜合建議) ============== */
.synth-block {
  border: 2px solid var(--primary) !important;
  box-shadow: 0 4px 12px rgba(37, 99, 235, .08);
}
.synth-block .suggest-block-header {
  background: linear-gradient(135deg, #eff6ff, #dbeafe);
}
.synth-block .suggest-block-header h3 { color: #1e3a8a; }

.synth-assessment {
  background: #fff; border: 1px dashed #c7d2fe;
  border-radius: 8px; padding: 8px 12px; margin-bottom: 12px;
  font-size: 13px; color: #374151;
}

.synth-card {
  background: #fff; border: 1px solid var(--border);
  border-radius: 10px; padding: 12px; margin-bottom: 10px;
  border-left: 4px solid var(--primary);
}
.synth-card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.synth-label { display: flex; align-items: center; gap: 8px; }
.synth-num {
  display: inline-flex; align-items: center; justify-content: center;
  width: 24px; height: 24px; border-radius: 50%;
  background: var(--primary); color: #fff;
  font-weight: 700; font-size: 13px;
}
.synth-card textarea {
  width: 100%; resize: vertical; font: inherit;
  border: 1px solid var(--border); border-radius: 6px; padding: 8px 10px;
  background: #fafbfd; min-height: 80px;
}
.synth-rationale {
  font-size: 12.5px; color: var(--muted);
  margin-top: 8px; padding-top: 8px; border-top: 1px dashed #e5e7eb;
}
.synth-sources { margin-top: 4px; font-size: 12.5px; }

/* ============== Suggest blocks (Hybrid + DCI 並列) ============== */
.suggest-block {
  border: 1px solid var(--border); border-radius: 10px;
  margin-bottom: 14px; background: #fff;
}
.suggest-block-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 14px; border-bottom: 1px solid var(--border);
  background: #f9fafb; border-radius: 10px 10px 0 0;
}
.suggest-block-header h3 { margin: 0; font-size: 14px; font-weight: 700; }
.suggest-block > div:not(.suggest-block-header) { padding: 12px; }

/* ============== DCI 深度搜尋 ============== */
.dci-block {
  border: 1px solid #c7d2fe; background: #eef2ff;
  border-radius: 10px; padding: 12px; margin-bottom: 12px;
}
.dci-header {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 8px; font-weight: 600;
}
.dci-trace { font-size: 12.5px; max-height: 280px; overflow-y: auto; }
.dci-step {
  background: #fff; border: 1px solid #e0e7ff; border-radius: 6px;
  padding: 6px 9px; margin-bottom: 4px; font-family: ui-monospace, "SF Mono", Consolas, monospace;
  font-size: 11.5px;
}
.dci-step .tool { color: #4338ca; font-weight: 700; }
.dci-step .args { color: var(--muted); }
.dci-step .meta { color: #6b7280; float: right; }
.dci-thinking { color: var(--muted); font-style: italic; padding: 8px 0; font-size: 13px; }
.dci-draft {
  background: #fff; border: 2px solid var(--success);
  border-radius: 8px; padding: 12px; margin-top: 10px;
}
.dci-draft .label { color: var(--success); font-weight: 700; font-size: 13px; margin-bottom: 6px; }
.dci-reasoning { font-size: 12.5px; color: var(--muted); margin-top: 8px; padding-top: 8px; border-top: 1px dashed #e5e7eb; }
.dci-sources { margin-top: 4px; }
.dci-sources .badge { margin-right: 4px; }

/* Clickable badges */
.badge.clickable {
  cursor: pointer; user-select: none;
  transition: transform .08s ease, box-shadow .08s ease;
}
.badge.clickable:hover {
  transform: translateY(-1px);
  background: #c7d2fe; color: #3730a3;
  box-shadow: 0 2px 4px rgba(0,0,0,.08);
}
.dci-step .ref-link {
  color: var(--primary); text-decoration: underline; text-decoration-style: dotted;
  cursor: pointer;
}
.dci-step .ref-link:hover { color: var(--primary-hover); text-decoration-style: solid; }

/* ============== Modal Dialog ============== */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 9999; padding: 20px;
  animation: fadeIn .12s ease-out;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.modal {
  background: #fff; border-radius: 14px;
  width: min(720px, 96vw); max-height: 88vh;
  display: flex; flex-direction: column;
  box-shadow: 0 20px 50px rgba(0,0,0,.25);
  overflow: hidden;
}
.modal-header {
  padding: 14px 20px; border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center;
  background: #f9fafb;
}
.modal-header h3 { margin: 0; font-size: 16px; }
.modal-close {
  background: transparent; color: var(--muted);
  border: 0; padding: 4px 10px; cursor: pointer;
  font-size: 22px; line-height: 1; border-radius: 6px; font-weight: bold;
}
.modal-close:hover { background: #e5e7eb; color: var(--ink); }
.modal-body {
  flex: 1; overflow-y: auto;
  padding: 18px 20px; font-size: 14.5px;
}
.modal-meta {
  display: flex; flex-wrap: wrap; gap: 6px;
  padding-bottom: 12px; border-bottom: 1px solid var(--border);
  margin-bottom: 14px; font-size: 12.5px;
}
.modal-section { margin-bottom: 16px; }
.modal-section .label {
  font-weight: 700; color: var(--muted); font-size: 12px;
  text-transform: uppercase; letter-spacing: .05em; margin-bottom: 6px;
}
.modal-section .content {
  white-space: pre-wrap; word-break: break-word;
  background: #fafbfd; padding: 10px 12px; border-radius: 8px;
  border-left: 3px solid var(--primary);
}
.modal-section.q .content { border-left-color: #818cf8; background: var(--customer-bg); }
.modal-section.a .content { border-left-color: var(--success); background: var(--cs-bg); }
.modal-loading { text-align: center; color: var(--muted); padding: 40px; }
.modal-error { color: var(--danger); padding: 20px; text-align: center; }
.modal-kv { font-size: 13px; }
.modal-kv .k { color: var(--muted); display: inline-block; min-width: 80px; }
.modal-kv .v { color: var(--ink); }

/* ============== Setup Wizard ============== */
.setup-overlay {
  position: fixed; inset: 0; z-index: 10000;
  background: rgba(15, 23, 42, 0.7);
  display: none; align-items: center; justify-content: center;
  padding: 20px;
}
.setup-modal {
  background: #fff; border-radius: 14px;
  width: min(720px, 96vw); max-height: 92vh;
  display: flex; flex-direction: column;
  overflow-y: auto;
  box-shadow: 0 20px 50px rgba(0,0,0,.3);
}
.setup-header {
  padding: 18px 24px; border-bottom: 1px solid var(--border);
  background: linear-gradient(135deg, #eff6ff, #dbeafe);
  border-radius: 14px 14px 0 0;
}
.setup-header h2 { margin: 0 0 4px; }
#setup-body, #setup-inline { padding: 18px 24px 22px; }

.wiz-step {
  border: 1px solid var(--border); border-radius: 10px;
  margin-bottom: 12px; overflow: hidden;
  transition: all .2s;
}
.wiz-step.pending { opacity: .5; }
.wiz-step.active  { border-color: var(--primary); box-shadow: 0 2px 8px rgba(37,99,235,.1); }
.wiz-step.done    { border-color: #bbf7d0; background: #f0fdf4; }

.wiz-step-head {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px;
  background: #f9fafb;
  border-bottom: 1px solid var(--border);
}
.wiz-step.done .wiz-step-head { background: #dcfce7; }
.wiz-step-head h3 { margin: 0; font-size: 15px; flex: 1; }

.wiz-num {
  display: inline-flex; align-items: center; justify-content: center;
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--muted); color: #fff; font-weight: 700;
}
.wiz-step.active .wiz-num { background: var(--primary); }
.wiz-step.done   .wiz-num { background: var(--success); }

.wiz-step-body { padding: 14px 16px; font-size: 14px; }
.wiz-step.pending .wiz-step-body { display: none; }
.wiz-step-body p { margin: 6px 0; }
.wiz-step-body code {
  background: #f3f4f6; padding: 2px 6px; border-radius: 4px; font-size: 12.5px;
}
button:disabled {
  background: #cbd5e1 !important; cursor: not-allowed;
}

