/* hr小助手：沿用工作台的深绿、琥珀与中性色。 */
.chat-toggle,
.chat-widget {
  --agent-accent: var(--accent, #0f766e);
  --agent-accent-strong: var(--accent-strong, #0b5f59);
  --agent-amber: var(--amber, #b7791f);
  --agent-green: var(--green, #2f855a);
  --agent-red: var(--red, #c2410c);
  --agent-ink: var(--ink, #2c3030);
  --agent-muted: var(--muted, #6b7068);
  --agent-line: var(--line, #dfe4dc);
  --agent-surface: var(--surface, #ffffff);
  font-family: inherit;
}

.chat-toggle[hidden],
.chat-widget[hidden],
.chat-widget [hidden] {
  display: none !important;
}

/* 悬浮入口 */
.chat-toggle {
  position: fixed;
  right: 22px;
  bottom: 22px;
  z-index: 9999;
  display: grid;
  width: 60px;
  height: 60px;
  padding: 0;
  place-items: center;
  border: 2px solid #f4b942;
  border-radius: 18px;
  background: #26312d;
  color: #ffffff;
  box-shadow: 0 14px 34px rgba(38, 49, 45, 0.26);
  cursor: pointer;
  transition: transform 160ms ease, box-shadow 160ms ease, background 160ms ease;
}

.chat-toggle:hover {
  transform: translateY(-2px);
  background: #34413c;
  box-shadow: 0 18px 38px rgba(38, 49, 45, 0.3);
}

.chat-toggle:focus-visible {
  outline: 3px solid rgba(244, 185, 66, 0.42);
  outline-offset: 3px;
}

.chat-toggle:active {
  transform: translateY(0) scale(0.97);
}

.chat-icon {
  font-size: 18px;
  font-weight: 900;
  letter-spacing: -0.04em;
}

.chat-toggle-dot {
  position: absolute;
  top: -4px;
  right: -4px;
  width: 13px;
  height: 13px;
  border: 3px solid #ffffff;
  border-radius: 50%;
  background: #d59a2b;
}

.chat-toggle-dot.is-ready {
  background: #45c486;
}

.chat-toggle-dot.is-degraded,
.chat-toggle-dot.is-checking {
  background: #f4b942;
}

.chat-toggle-dot.is-offline {
  background: #d45a34;
}

/* 窗口骨架 */
.chat-widget {
  position: fixed;
  right: 22px;
  bottom: 94px;
  z-index: 9998;
  display: flex;
  width: min(410px, calc(100vw - 44px));
  height: min(660px, calc(100vh - 126px));
  min-height: 430px;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid rgba(38, 49, 45, 0.14);
  border-radius: 14px;
  background: var(--agent-surface);
  color: var(--agent-ink);
  box-shadow: 0 22px 58px rgba(38, 49, 45, 0.24);
  animation: knowledgeAgentEnter 180ms ease-out;
}

@keyframes knowledgeAgentEnter {
  from {
    opacity: 0;
    transform: translateY(10px) scale(0.985);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.chat-widget button,
.chat-widget textarea {
  font: inherit;
}

.chat-widget button:focus-visible,
.chat-widget textarea:focus-visible {
  outline: 2px solid rgba(15, 118, 110, 0.38);
  outline-offset: 2px;
}

/* 标题与连接状态 */
.chat-widget .chat-header {
  display: flex;
  min-height: 72px;
  flex: 0 0 auto;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 13px 14px;
  background: #26312d;
  color: #ffffff;
}

.chat-widget .chat-title {
  display: flex;
  min-width: 0;
  align-items: center;
  gap: 10px;
}

.chat-widget .chat-avatar {
  display: grid;
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
  place-items: center;
  border: 1px solid rgba(244, 185, 66, 0.62);
  border-radius: 10px;
  background: #f4b942;
  color: #1f2926;
  font-size: 14px;
  font-weight: 900;
}

.chat-title-copy {
  min-width: 0;
}

.chat-title-copy > strong {
  display: block;
  overflow: hidden;
  font-size: 15px;
  line-height: 1.25;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.knowledge-agent-status {
  display: flex;
  min-width: 0;
  align-items: center;
  gap: 5px;
  margin-top: 5px;
  color: #bdc9c3;
  font-size: 11px;
  line-height: 1.2;
}

.knowledge-agent-status i {
  width: 7px;
  height: 7px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: #f4b942;
}

.knowledge-agent-status.is-ready {
  color: #d9f7e8;
}

.knowledge-agent-status.is-ready i {
  background: #45c486;
  box-shadow: 0 0 0 3px rgba(69, 196, 134, 0.14);
}

.knowledge-agent-status.is-degraded {
  color: #ffedc2;
}

.knowledge-agent-status.is-degraded i,
.knowledge-agent-status.is-checking i {
  background: #f4b942;
}

.knowledge-agent-status.is-checking i {
  animation: knowledgeAgentPulse 1.2s ease-in-out infinite;
}

.knowledge-agent-status.is-offline {
  color: #ffd5c8;
}

.knowledge-agent-status.is-offline i {
  background: #e86c45;
}

@keyframes knowledgeAgentPulse {
  0%, 100% { opacity: 0.45; }
  50% { opacity: 1; }
}

.chat-widget .chat-header-actions {
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  gap: 5px;
}

.chat-widget .chat-header-actions button {
  min-height: 32px;
  padding: 0 8px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 7px;
  background: #34413c;
  color: #ffffff;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
  transition: background 140ms ease, opacity 140ms ease;
}

.chat-widget .chat-header-actions button:last-child {
  width: 32px;
  padding: 0;
  font-size: 19px;
}

.chat-widget .chat-header-actions button:hover:not(:disabled) {
  background: #405048;
}

.chat-widget .chat-header-actions button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.knowledge-agent-scope-note {
  flex: 0 0 auto;
  padding: 8px 14px;
  border-bottom: 1px solid #f1dbac;
  background: #fff6df;
  color: #7b5211;
  font-size: 11px;
  font-weight: 600;
  line-height: 1.45;
}

/* 对话主体与错误条 */
.knowledge-agent-chat-body {
  display: flex;
  min-height: 0;
  flex: 1 1 auto;
  flex-direction: column;
}

.knowledge-agent-error {
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 9px 12px;
  border-bottom: 1px solid #ffd2c1;
  background: #fff7f3;
  color: var(--agent-red);
  font-size: 12px;
  line-height: 1.4;
}

.knowledge-agent-error span {
  min-width: 0;
  overflow-wrap: anywhere;
}

.knowledge-agent-error button {
  min-height: 30px;
  flex: 0 0 auto;
  padding: 0 11px;
  border: 1px solid #e5a789;
  border-radius: 7px;
  background: #ffffff;
  color: var(--agent-red);
  font-size: 12px;
  font-weight: 800;
  cursor: pointer;
}

.chat-widget .chat-messages {
  display: flex;
  min-height: 0;
  flex: 1 1 auto;
  flex-direction: column;
  gap: 14px;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 15px;
  background: #f6f7f4;
  scrollbar-color: #bdc9c3 transparent;
  scrollbar-width: thin;
}

.chat-widget .chat-messages::-webkit-scrollbar {
  width: 6px;
}

.chat-widget .chat-messages::-webkit-scrollbar-thumb {
  border-radius: 6px;
  background: #bdc9c3;
}

.chat-widget .chat-message {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  animation: knowledgeAgentMessageIn 150ms ease-out;
}

@keyframes knowledgeAgentMessageIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.chat-widget .chat-message-user {
  flex-direction: row-reverse;
}

.chat-widget .message-avatar {
  display: grid;
  width: 28px;
  height: 28px;
  flex: 0 0 auto;
  place-items: center;
  border-radius: 8px;
  background: #e6efea;
  color: var(--agent-accent);
  font-size: 11px;
  font-weight: 900;
}

.chat-widget .chat-message-user .message-avatar {
  background: #fff0c7;
  color: #7b5211;
}

.chat-widget .message-content {
  max-width: min(82%, 310px);
  min-width: 0;
}

.chat-widget .message-text {
  padding: 10px 12px;
  border: 1px solid var(--agent-line);
  border-radius: 5px 12px 12px 12px;
  background: #ffffff;
  color: var(--agent-ink);
  box-shadow: 0 3px 10px rgba(38, 45, 42, 0.05);
  font-size: 13px;
  line-height: 1.55;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.chat-widget .chat-message-user .message-text {
  border-color: var(--agent-accent);
  border-radius: 12px 5px 12px 12px;
  background: var(--agent-accent);
  color: #ffffff;
}

.message-mode {
  display: inline-flex;
  margin-top: 6px;
  padding: 2px 7px;
  border-radius: 999px;
  background: #e6efea;
  color: #345149;
  font-size: 10px;
  font-weight: 800;
}

.message-warning {
  margin-top: 6px;
  padding: 7px 8px;
  border: 1px solid #f1dbac;
  border-radius: 7px;
  background: #fff6df;
  color: #7b5211;
  font-size: 11px;
  line-height: 1.45;
}

.chat-widget .message-sources {
  margin-top: 7px;
}

.chat-widget .message-sources small {
  display: block;
  margin-bottom: 4px;
  color: var(--agent-muted);
  font-size: 10px;
  font-weight: 700;
}

.chat-widget .source-tag {
  display: inline-block;
  max-width: 100%;
  margin: 0 5px 5px 0;
  padding: 3px 7px;
  overflow: hidden;
  border: 1px solid var(--agent-line);
  border-radius: 6px;
  background: #ffffff;
  color: #4b5954;
  font-size: 10px;
  line-height: 1.3;
  text-overflow: ellipsis;
  vertical-align: top;
  white-space: nowrap;
}

.chat-widget .message-loading {
  display: flex;
  width: 58px;
  min-height: 38px;
  align-items: center;
  justify-content: center;
  gap: 4px;
}

.chat-widget .message-loading span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--agent-accent);
  animation: knowledgeAgentBounce 1.1s infinite ease-in-out both;
}

.chat-widget .message-loading span:nth-child(1) { animation-delay: -0.24s; }
.chat-widget .message-loading span:nth-child(2) { animation-delay: -0.12s; }

@keyframes knowledgeAgentBounce {
  0%, 80%, 100% { transform: scale(0.5); opacity: 0.42; }
  40% { transform: scale(1); opacity: 1; }
}

.chat-widget .chat-empty-state {
  display: flex;
  min-height: 100%;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: 30px 18px;
  color: var(--agent-muted);
  text-align: center;
}

.chat-empty-state-icon {
  display: grid;
  width: 48px;
  height: 48px;
  margin-bottom: 12px;
  place-items: center;
  border: 1px solid #c6d6ce;
  border-radius: 14px;
  background: #e6efea;
  color: var(--agent-accent);
  font-size: 17px;
  font-weight: 900;
}

.chat-widget .chat-empty-state strong {
  color: var(--agent-ink);
  font-size: 14px;
}

.chat-widget .chat-empty-state p {
  max-width: 285px;
  margin: 7px 0 0;
  font-size: 12px;
  line-height: 1.55;
}

/* 快捷问题与输入 */
.chat-widget .chat-suggestions {
  display: flex;
  flex: 0 0 auto;
  gap: 6px;
  overflow-x: auto;
  padding: 9px 12px;
  border-top: 1px solid var(--agent-line);
  background: #fbfcfa;
  scrollbar-width: none;
}

.chat-widget .chat-suggestions::-webkit-scrollbar {
  display: none;
}

.chat-widget .chat-suggestion {
  min-height: 30px;
  flex: 0 0 auto;
  padding: 5px 10px;
  border: 1px solid #c6d6ce;
  border-radius: 999px;
  background: #ffffff;
  color: #345149;
  font-size: 11px;
  cursor: pointer;
  transition: border-color 140ms ease, background 140ms ease;
}

.chat-widget .chat-suggestion:hover:not(:disabled) {
  border-color: var(--agent-accent);
  background: #eff7f3;
}

.chat-widget .chat-suggestion:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.chat-widget .chat-input-wrap {
  display: flex;
  flex: 0 0 auto;
  align-items: flex-end;
  gap: 8px;
  padding: 11px 12px 12px;
  border-top: 1px solid var(--agent-line);
  background: #ffffff;
}

.chat-widget .chat-input {
  min-width: 0;
  min-height: 40px;
  max-height: 108px;
  flex: 1 1 auto;
  resize: none;
  padding: 9px 11px;
  border: 1px solid #c9d1cc;
  border-radius: 8px;
  background: #ffffff;
  color: var(--agent-ink);
  font-size: 13px;
  line-height: 1.5;
  transition: border-color 140ms ease, box-shadow 140ms ease;
}

.chat-widget .chat-input:focus {
  border-color: var(--agent-accent);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.1);
  outline: none;
}

.chat-widget .chat-input:disabled {
  cursor: not-allowed;
  background: #f0f3ef;
  color: #858b85;
}

.chat-widget .chat-input::placeholder {
  color: #858b85;
}

.chat-widget .chat-send-btn {
  min-width: 70px;
  min-height: 40px;
  flex: 0 0 auto;
  padding: 0 13px;
  border: 0;
  border-radius: 8px;
  background: var(--agent-accent);
  color: #ffffff;
  font-size: 12px;
  font-weight: 800;
  cursor: pointer;
  transition: background 140ms ease, opacity 140ms ease;
}

.chat-widget .chat-send-btn:hover:not(:disabled) {
  background: var(--agent-accent-strong);
}

.chat-widget .chat-send-btn:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

/* 历史列表 */
.knowledge-agent-history {
  display: flex;
  min-height: 0;
  flex: 1 1 auto;
  flex-direction: column;
  background: #f6f7f4;
}

.knowledge-agent-history-head {
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--agent-line);
  background: #ffffff;
}

.knowledge-agent-history-head strong,
.knowledge-agent-history-head span {
  display: block;
}

.knowledge-agent-history-head strong {
  font-size: 14px;
}

.knowledge-agent-history-head span {
  margin-top: 3px;
  color: var(--agent-muted);
  font-size: 11px;
}

.knowledge-agent-history-head span.is-error {
  color: var(--agent-red);
}

.knowledge-agent-history-head button {
  min-height: 34px;
  padding: 0 11px;
  border: 1px solid var(--agent-line);
  border-radius: 7px;
  background: #ffffff;
  color: var(--agent-accent);
  font-size: 11px;
  font-weight: 800;
  cursor: pointer;
}

.knowledge-agent-history-list {
  min-height: 0;
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 10px;
}

.knowledge-agent-history-item {
  display: flex;
  align-items: stretch;
  gap: 7px;
  margin-bottom: 8px;
  padding: 8px;
  border: 1px solid var(--agent-line);
  border-radius: 9px;
  background: #ffffff;
}

.knowledge-agent-history-item.is-active {
  border-color: #87c99b;
  background: #f7fff9;
}

.knowledge-agent-history-open {
  min-width: 0;
  flex: 1 1 auto;
  padding: 2px;
  border: 0;
  background: transparent;
  color: var(--agent-ink);
  text-align: left;
  cursor: pointer;
}

.knowledge-agent-history-open strong,
.knowledge-agent-history-open span,
.knowledge-agent-history-open small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.knowledge-agent-history-open strong {
  font-size: 13px;
}

.knowledge-agent-history-open span {
  margin-top: 4px;
  color: var(--agent-muted);
  font-size: 11px;
}

.knowledge-agent-history-open small {
  margin-top: 5px;
  color: #8b918b;
  font-size: 10px;
}

.knowledge-agent-history-delete {
  min-width: 48px;
  flex: 0 0 auto;
  align-self: center;
  padding: 6px 8px;
  border: 1px solid #ffd2c1;
  border-radius: 7px;
  background: #fff7f3;
  color: var(--agent-red);
  font-size: 10px;
  font-weight: 800;
  cursor: pointer;
}

.knowledge-agent-history-empty {
  display: grid;
  min-height: 180px;
  place-items: center;
  color: var(--agent-muted);
  font-size: 12px;
}

/* 移动端：全屏时隐藏入口，避免覆盖发送按钮。 */
@media (max-width: 768px) {
  body.knowledge-agent-open .chat-toggle,
  .chat-toggle.is-widget-open {
    display: none;
  }

  .chat-toggle {
    right: 16px;
    bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    width: 56px;
    height: 56px;
    border-radius: 16px;
  }

  .chat-widget {
    inset: 0;
    width: auto;
    height: 100vh;
    height: 100dvh;
    min-height: 0;
    max-height: none;
    border: 0;
    border-radius: 0;
  }

  .chat-widget .chat-header {
    min-height: 68px;
    padding-top: calc(10px + env(safe-area-inset-top, 0px));
  }

  .chat-widget .chat-header-actions {
    gap: 4px;
  }

  .chat-widget .chat-header-actions button {
    min-height: 38px;
    padding: 0 7px;
  }

  .chat-widget .chat-header-actions button:last-child {
    width: 38px;
  }

  .chat-widget .chat-input-wrap {
    padding-bottom: calc(11px + env(safe-area-inset-bottom, 0px));
  }

  .chat-widget .message-content {
    max-width: 86%;
  }
}

@media (max-width: 390px) {
  .chat-widget .chat-avatar {
    width: 34px;
    height: 34px;
  }

  .chat-title-copy > strong {
    font-size: 14px;
  }

  .chat-widget .chat-header-actions button {
    font-size: 10px;
  }

  .knowledge-agent-scope-note {
    font-size: 10px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .chat-widget,
  .chat-widget .chat-message,
  .knowledge-agent-status.is-checking i,
  .chat-widget .message-loading span {
    animation: none;
  }
}
