:root {
  --orange: #ff7a1a;
  --orange-soft: #fff1e6;
  --ink: #1c1c1e;
  --ink-soft: #555;
  --bg: #f7f7f9;
  --line: #e5e5ea;
  --green: #22c55e;
  --red: #ef4444;
  --amber: #f59e0b;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", "PingFang TC",
               "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  background: var(--bg);
  color: var(--ink);
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 24px;
  background: #fff;
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; gap: 12px; align-items: center; }
.brand .logo { font-size: 32px; }
.brand h1 { margin: 0; font-size: 18px; }
.brand p { margin: 0; color: var(--ink-soft); font-size: 12px; }

.status { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--ink-soft); }
.status .dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--amber);
}
.status.ok .dot { background: var(--green); }
.status.err .dot { background: var(--red); }

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(320px, 1fr);
  gap: 0;
  min-height: 0;
}

.chat-panel {
  display: flex;
  flex-direction: column;
  background: #fff;
  border-right: 1px solid var(--line);
  min-height: 0;
}

.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 20px;
  border-bottom: 1px solid var(--line);
}
.chat-header h2 { margin: 0; font-size: 15px; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg {
  max-width: 78%;
  padding: 10px 14px;
  border-radius: 14px;
  white-space: pre-wrap;
  line-height: 1.55;
  font-size: 14px;
  word-wrap: break-word;
}
.msg.user { align-self: flex-end; background: var(--orange); color: #fff; border-bottom-right-radius: 4px; }
.msg.bot  { align-self: flex-start; background: var(--orange-soft); color: var(--ink); border-bottom-left-radius: 4px; }
.msg.bot.blocked { background: #fdecea; }
.msg.bot.off_topic { background: #f1f1f4; color: var(--ink-soft); }
.msg.bot a { color: #c2410c; text-decoration: underline; }
.msg.system { align-self: center; background: transparent; color: var(--ink-soft); font-size: 12px; }
.msg.error  { align-self: center; background: #fdecea; color: var(--red); font-size: 12px; border-radius: 8px; }

.msg .meta {
  display: block;
  font-size: 11px;
  color: var(--ink-soft);
  margin-top: 4px;
  opacity: 0.8;
}
.msg.user .meta { color: rgba(255,255,255,0.85); }

.chat-input {
  display: flex;
  gap: 8px;
  padding: 12px 20px;
  border-top: 1px solid var(--line);
}
.chat-input input {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 24px;
  font-size: 14px;
  outline: none;
}
.chat-input input:focus { border-color: var(--orange); }
.chat-input button, .ghost {
  padding: 10px 18px;
  background: var(--orange);
  color: #fff;
  border: none;
  border-radius: 24px;
  cursor: pointer;
  font-size: 14px;
}
.chat-input button:disabled { opacity: 0.5; cursor: not-allowed; }
.ghost {
  background: transparent;
  color: var(--ink-soft);
  border: 1px solid var(--line);
  padding: 6px 12px;
  font-size: 12px;
}
.ghost:hover { color: var(--ink); }

.suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0 20px 14px;
  align-items: center;
  font-size: 12px;
  color: var(--ink-soft);
}
.chip {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
}
.chip:hover { border-color: var(--orange); color: var(--orange); }

.debug-panel {
  background: #fafbfd;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-height: 0;
}
.debug-section {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
}
.debug-section h3 { margin: 0 0 10px; font-size: 13px; }
.debug-section h4 { margin: 12px 0 6px; font-size: 12px; color: var(--ink-soft); }

.kv { display: grid; grid-template-columns: 80px 1fr; gap: 4px 10px; margin: 0; font-size: 12px; }
.kv dt { color: var(--ink-soft); }
.kv dd { margin: 0; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }

.flow { list-style: none; padding: 0; margin: 0; font-size: 12px; }
.flow li {
  padding: 4px 8px;
  border-left: 2px solid var(--orange);
  margin-bottom: 4px;
  background: #fff8f1;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

#kb-search {
  width: 100%;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 12px;
  margin-bottom: 8px;
}
.kb-list {
  list-style: none;
  padding: 0;
  margin: 0;
  max-height: 360px;
  overflow-y: auto;
  font-size: 12px;
}
.kb-list li {
  padding: 6px 8px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}
.kb-list li:hover { background: var(--orange-soft); }
.kb-list .qid { color: var(--orange); font-weight: 600; margin-right: 4px; }
.kb-list .cat { color: var(--ink-soft); font-size: 11px; }

@media (max-width: 900px) {
  body { height: auto; min-height: 100vh; }
  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: 85vh auto;
  }
  .chat-panel {
    height: 85vh;
    border-right: none;
    border-bottom: 1px solid var(--line);
  }
  .messages { flex: 1 1 0; min-height: 0; overflow-y: auto; }
  .debug-panel {
    max-height: none;
    overflow-y: visible;
  }
  .kb-list { max-height: 240px; }
}
