:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #dde3ea;
  --text: #17202b;
  --muted: #5d6b7a;
  --accent: #c8102e;
  --accent-ink: #ffffff;
  --passed: #1d7a46;
  --passed-bg: #e6f4ec;
  --blocked: #b42318;
  --blocked-bg: #fdecea;
  --skipped: #6b7785;
  --skipped-bg: #eef1f4;
  --error: #b45309;
  --error-bg: #fdf3e2;
  --pending: #1f5fbf;
  --pending-bg: #e8f0fc;
  --radius: 10px;
  font-family: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", system-ui, sans-serif;
  color: var(--text);
}

* { box-sizing: border-box; }

body { margin: 0; background: var(--bg); min-height: 100vh; }

.topbar {
  display: flex;
  align-items: center;
  padding: 14px 28px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 9px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 700;
}
.brand h1 { margin: 0; font-size: 18px; }
.brand p { margin: 2px 0 0; font-size: 13px; color: var(--muted); }

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 420px;
  gap: 20px;
  padding: 20px 28px;
  height: calc(100vh - 67px);
}

.chat, .trace {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  min-height: 0;
}

.chat { display: flex; flex-direction: column; }

.messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.empty { margin: auto; color: var(--muted); text-align: center; max-width: 360px; line-height: 1.6; }

.msg { max-width: 78%; padding: 12px 14px; border-radius: var(--radius); line-height: 1.65; }
.msg p { margin: 0; white-space: pre-wrap; word-break: break-word; }
.msg-user { align-self: flex-end; background: #1f2937; color: #fff; }
.msg-assistant { align-self: flex-start; background: var(--surface-2); border: 1px solid var(--border); }
.tag { display: inline-block; margin-bottom: 6px; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; }

.scenarios { display: flex; flex-wrap: wrap; gap: 8px; padding: 12px 20px; border-top: 1px solid var(--border); }
.scenario {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  font: inherit;
  cursor: pointer;
}
.scenario:hover:not(:disabled) { border-color: var(--accent); }
.scenario:disabled { opacity: .55; cursor: not-allowed; }
.scenario-title { font-size: 14px; font-weight: 600; }
.scenario-expected { font-size: 12px; color: var(--muted); }

.composer { display: flex; gap: 10px; padding: 12px 20px 18px; }
.composer textarea {
  flex: 1;
  resize: none;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
}
.composer textarea:focus, .login-card input:focus { outline: 2px solid var(--pending); outline-offset: 1px; }

button[type="submit"] {
  padding: 0 22px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-ink);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
button[type="submit"]:disabled { opacity: .55; cursor: not-allowed; }

.trace { padding: 20px; overflow-y: auto; }
.trace h2 { margin: 0 0 14px; font-size: 16px; }

.stages { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.stage { position: relative; padding: 12px 14px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-2); }
.stage:not(:last-child)::after {
  content: "";
  position: absolute;
  left: 22px;
  bottom: -11px;
  width: 2px;
  height: 10px;
  background: var(--border);
}
.stage-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.stage-names { display: flex; flex-direction: column; }
.stage-names strong { font-size: 14px; }
.stage-sub { font-size: 12px; color: var(--muted); }
.badge { padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.hits { margin: 8px 0 0; padding-left: 18px; font-size: 13px; }
.stage-detail { margin: 8px 0 0; font-size: 13px; }

.state-passed { border-color: var(--passed); }
.state-passed .badge, .status-allowed { background: var(--passed-bg); color: var(--passed); }
.state-blocked { border-color: var(--blocked); }
.state-blocked .badge, .status-blocked_input, .status-blocked_output, .status-waf { background: var(--blocked-bg); color: var(--blocked); }
.state-skipped { opacity: .7; }
.state-skipped .badge { background: var(--skipped-bg); color: var(--skipped); }
.state-error { border-color: var(--error); }
.state-error .badge, .status-error { background: var(--error-bg); color: var(--error); }
.state-pending { border-color: var(--pending); }
.state-pending .badge { background: var(--pending-bg); color: var(--pending); }
.state-idle .badge { background: var(--skipped-bg); color: var(--skipped); }

.meta { margin: 18px 0 0; display: grid; gap: 8px; font-size: 13px; }
.meta div { display: flex; justify-content: space-between; gap: 12px; }
.meta dt { color: var(--muted); }
.meta dd { margin: 0; text-align: right; word-break: break-all; }
#meta-status[class] { padding: 1px 8px; border-radius: 999px; font-weight: 600; }

.login { position: fixed; inset: 0; display: grid; place-items: center; background: rgba(15, 23, 32, .55); }
.login[hidden] { display: none; }
.login-card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 320px;
  padding: 24px;
  border-radius: var(--radius);
  background: var(--surface);
}
.login-card h2 { margin: 0 0 6px; font-size: 18px; }
.login-card input { padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; font: inherit; }
.login-card button { padding: 10px; }
.login-error { min-height: 1.2em; margin: 0; color: var(--blocked); font-size: 13px; }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; height: auto; }
  .chat { min-height: 70vh; }
}
