:root {
  color-scheme: light;
  --bg: #f6f5f1;
  --surface: #fffdfa;
  --surface-2: #f0eee8;
  --surface-3: #e9f1eb;
  --ink: #1f2723;
  --muted: #69736d;
  --faint: #8b948e;
  --line: #dedbd2;
  --line-strong: #cbc6ba;
  --brand: #2f6b4f;
  --brand-strong: #1f563c;
  --brand-soft: #dfece4;
  --amber: #a86513;
  --amber-soft: #fff1d7;
  --danger: #b42318;
  --danger-soft: #fde7e4;
  --blue: #315f87;
  --blue-soft: #e5eef6;
  --shadow: 0 14px 34px rgba(38, 47, 42, 0.08);
  --radius: 8px;
  --sidebar: 248px;
  --font: Arial, "Microsoft YaHei", "PingFang SC", sans-serif;
}

* { box-sizing: border-box; }
html { min-width: 320px; }
body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: var(--bg);
  font-family: var(--font);
  display: grid;
  grid-template-columns: var(--sidebar) minmax(0, 1fr);
  letter-spacing: 0;
}

button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.62; }
.skip-link {
  position: fixed;
  left: 12px;
  top: 12px;
  z-index: 100;
  padding: 9px 12px;
  border-radius: var(--radius);
  color: #fff;
  background: var(--ink);
  transform: translateY(-160%);
}
.skip-link:focus { transform: translateY(0); }
input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  color: var(--ink);
  padding: 10px 12px;
  outline: none;
  min-height: 40px;
}
textarea { min-height: 144px; resize: vertical; line-height: 1.6; }
input:focus, select:focus, textarea:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(47, 107, 79, 0.14);
}
button:focus-visible, a:focus-visible {
  outline: 3px solid var(--blue);
  outline-offset: 3px;
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
}
label { display: grid; gap: 6px; color: var(--muted); font-size: 12px; font-weight: 700; }
h1, h2, h3, p { margin: 0; }
h2 { font-size: 26px; line-height: 1.18; }
h3 { font-size: 15px; line-height: 1.35; }

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  padding: 20px 16px;
  border-right: 1px solid var(--line);
  background: #ece8df;
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  gap: 18px;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
  width: 38px;
  height: 38px;
  border-radius: var(--radius);
  display: grid;
  place-items: center;
  background: var(--brand);
  color: #fff;
  font-weight: 800;
  box-shadow: inset 0 -8px 18px rgba(0,0,0,0.12);
}
.brand h1 { font-size: 18px; line-height: 1.2; }
.brand p { color: var(--muted); font-size: 12px; margin-top: 3px; }

.nav-metrics {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}
.nav-metrics .metric {
  min-height: 58px;
  padding: 10px 12px;
  box-shadow: none;
}
.nav { display: grid; align-content: start; gap: 8px; }
.nav button {
  width: 100%;
  min-height: 42px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: transparent;
  color: var(--ink);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  text-align: left;
}
.nav button:hover { background: rgba(255,255,255,0.55); border-color: var(--line); }
.nav button.active {
  background: var(--brand);
  color: #fff;
  border-color: var(--brand);
}
.nav-icon {
  width: 24px;
  height: 24px;
  border-radius: 6px;
  display: grid;
  place-items: center;
  background: rgba(255,255,255,0.55);
  color: var(--brand-strong);
  font-size: 12px;
  font-weight: 800;
}
.nav button.active .nav-icon { background: rgba(255,255,255,0.2); color: #fff; }
.sidebar-foot {
  border-top: 1px solid var(--line);
  padding-top: 12px;
  display: flex;
  justify-content: space-between;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
}
.sidebar-foot strong { color: var(--brand-strong); }

.workspace {
  min-width: 0;
  height: 100vh;
  overflow: auto;
  padding: 24px;
}
.view { display: none; }
.view.active { display: block; }
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}
.eyebrow {
  color: var(--brand);
  font-size: 12px;
  font-weight: 800;
  margin-bottom: 5px;
}
.toolbar, .actions, .token-box {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.button, button {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  min-height: 38px;
  padding: 8px 12px;
}
.button:hover, button:hover { border-color: var(--line-strong); }
.button.primary {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
}
.button.primary:hover { background: var(--brand-strong); }
.button.secondary { background: #fff; }
.button.danger, button.danger, .danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}
.button.ghost, button.ghost {
  background: transparent;
  border-color: transparent;
  color: var(--brand-strong);
}

.panel {
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.panel:hover { border-color: var(--line-strong); }
.panel-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}
.panel-meta { color: var(--muted); font-size: 12px; }
.divider { height: 1px; background: var(--line); margin: 14px 0; }

.metrics {
  display: grid;
  grid-template-columns: repeat(6, minmax(130px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}
.metric {
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}
.metric span, .row small {
  display: block;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}
.metric strong {
  display: block;
  margin-top: 5px;
  font-size: 25px;
  line-height: 1.1;
}
.metric .trend { margin-top: 8px; color: var(--faint); font-size: 12px; }

.overview-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 16px;
  margin-bottom: 16px;
}
.grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 16px;
  margin-bottom: 16px;
}
.grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }

.chat-layout {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr) 340px;
  gap: 16px;
  min-height: calc(100vh - 116px);
}
.session-panel, .context-panel { align-self: stretch; }
.session-list { display: grid; gap: 8px; }
.filter-stack {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  margin-bottom: 12px;
}
.session-card {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: var(--radius);
  padding: 11px;
  display: grid;
  gap: 6px;
  text-align: left;
}
.session-card.active { border-color: var(--brand); background: var(--brand-soft); }
.session-card strong, .row strong {
  min-width: 0;
  overflow-wrap: anywhere;
  line-height: 1.45;
}
.session-card p { color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }
.conversation {
  min-width: 0;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  gap: 10px;
}
.messages {
  min-height: 560px;
  max-height: calc(100vh - 188px);
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  box-shadow: var(--shadow);
}
.msg {
  max-width: min(76%, 720px);
  padding: 11px 13px;
  border-radius: var(--radius);
  background: var(--surface-2);
  white-space: pre-wrap;
  line-height: 1.58;
  overflow-wrap: anywhere;
}
.msg small { display: block; color: var(--muted); font-size: 11px; margin-bottom: 5px; }
.msg.user { align-self: flex-end; background: var(--brand-soft); }
.msg.agent { background: var(--amber-soft); }
.msg.system { background: var(--blue-soft); }
.composer {
  display: grid;
  grid-template-columns: 132px minmax(0, 1fr) auto;
  gap: 8px;
}

.context-panel .row { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; margin-bottom: 8px; }
.source-list { display: grid; gap: 8px; }

.knowledge-layout {
  display: grid;
  grid-template-columns: 360px minmax(0, 1fr);
  gap: 16px;
  margin-bottom: 16px;
}
.kb-editor form { display: grid; gap: 12px; }
.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.table-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}
.search-input { max-width: 300px; }
.table-toolbar select { width: auto; min-width: 132px; }
.kb-list {
  display: grid;
  gap: 8px;
  max-height: 680px;
  overflow: auto;
  padding-right: 4px;
}
.kb-row {
  display: grid;
  grid-template-columns: minmax(220px, 1.2fr) 100px 88px 112px minmax(140px, 0.7fr);
  gap: 10px;
  align-items: start;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 11px;
  background: #fff;
}
.kb-question { font-weight: 800; overflow-wrap: anywhere; }
.kb-answer { color: var(--muted); font-size: 12px; line-height: 1.55; margin-top: 5px; }

.row {
  border-top: 1px solid var(--line);
  padding: 10px 0;
  display: grid;
  gap: 6px;
}
.stack-list {
  display: grid;
  gap: 8px;
  max-height: 680px;
  overflow: auto;
  padding-right: 4px;
}
.detail-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px 14px;
}
.detail-grid .row {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  padding: 10px;
}
.row:first-child { border-top: 0; }
.row p, .empty, .muted {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.55;
}
.empty {
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius);
  padding: 18px;
  background: rgba(255,255,255,0.55);
}
.tags { display: flex; flex-wrap: wrap; gap: 5px; }
.tag-sm, .badge {
  display: inline-flex;
  align-items: center;
  width: fit-content;
  max-width: 100%;
  min-height: 24px;
  border-radius: 999px;
  padding: 3px 8px;
  font-size: 12px;
  line-height: 1.2;
  color: var(--brand-strong);
  background: var(--brand-soft);
  overflow-wrap: anywhere;
}
.badge.medium, .tag-risk-medium { color: var(--amber); background: var(--amber-soft); }
.badge.high, .tag-risk-high, .badge.urgent { color: var(--danger); background: var(--danger-soft); }
.badge.low { color: var(--brand-strong); background: var(--brand-soft); }
.badge.status { color: var(--blue); background: var(--blue-soft); }

.health-summary {
  display: grid;
  grid-template-columns: repeat(5, minmax(110px, 1fr));
  gap: 8px;
  margin-bottom: 12px;
}
.health-list {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}
.channel-health-row {
  min-height: 126px;
  align-content: start;
}
.channel-health-row > div {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.agent-card, .queue-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  padding: 12px;
  display: grid;
  gap: 9px;
  margin-bottom: 10px;
}
.agent-head, .queue-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
}
.queue-controls {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.report-panel { margin-bottom: 16px; }
.report-output,
.json-box {
  width: 100%;
  min-height: 120px;
  max-height: 360px;
  margin: 0;
  overflow: auto;
  white-space: pre-wrap;
  color: #f7f7f4;
  background: #202823;
  border-radius: var(--radius);
  padding: 12px;
  line-height: 1.55;
}

.token-box {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
}
.notice {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--muted);
  padding: 10px 12px;
  margin-bottom: 10px;
  font-size: 13px;
  line-height: 1.5;
}
.notice.ready { background: var(--brand-soft); color: var(--brand-strong); border-color: rgba(47,107,79,0.24); }
.notice.warn { background: var(--amber-soft); color: var(--amber); border-color: rgba(168,101,19,0.24); }
.toast {
  position: fixed;
  right: 22px;
  top: 18px;
  z-index: 20;
  max-width: min(360px, calc(100vw - 32px));
  padding: 11px 13px;
  border-radius: var(--radius);
  background: var(--ink);
  color: #fff;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(-8px);
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
  font-size: 13px;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { background: var(--danger); }

@media (max-width: 1240px) {
  .metrics { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .overview-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .chat-layout { grid-template-columns: 260px minmax(0, 1fr); }
  .context-panel { grid-column: 1 / -1; }
  .kb-row { grid-template-columns: minmax(220px, 1fr) 90px 86px; }
  .kb-row .actions { grid-column: 1 / -1; }
}

@media (max-width: 900px) {
  body { grid-template-columns: 1fr; }
  .sidebar {
    position: static;
    height: auto;
    grid-template-rows: auto;
    padding: 14px;
  }
  .nav { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .nav button { justify-content: center; padding: 8px; }
  .nav button span:last-child { display: none; }
  .nav-metrics { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .sidebar-foot { display: none; }
  .workspace { height: auto; min-height: 100vh; padding: 16px; }
  .page-header { align-items: flex-start; flex-direction: column; }
  .chat-layout, .knowledge-layout, .grid, .grid.two, .overview-grid, .metrics, .health-summary, .health-list {
    grid-template-columns: 1fr;
  }
  .messages { min-height: 420px; max-height: none; }
  .composer, .token-box, .queue-controls { grid-template-columns: 1fr; }
  .search-input { max-width: none; }
  .kb-row { grid-template-columns: 1fr; }
}

@media (max-width: 520px) {
  h2 { font-size: 22px; }
  .brand h1 { font-size: 16px; }
  .nav-metrics .metric { padding: 8px; }
  .metric strong { font-size: 21px; }
  .toolbar, .actions { width: 100%; }
  .toolbar .button, .toolbar button, .actions button { flex: 1 1 auto; }
  .msg { max-width: 100%; }
  .form-grid { grid-template-columns: 1fr; }
  .detail-grid { grid-template-columns: 1fr; }
  .nav { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
