:root {
  --bg: #edf0ec;
  --panel: #f8faf7;
  --line: #d9ded7;
  --text: #1f2923;
  --muted: #6d766f;
  --green: #2f8f57;
  --green-deep: #1f6f42;
  --bubble: #ffffff;
  --customer: #d8f3c7;
  --danger: #b04444;
  --shadow: 0 1px 2px rgba(30, 45, 35, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Microsoft YaHei", "PingFang SC", sans-serif;
  font-size: 14px;
}

button,
input,
textarea {
  font: inherit;
}

.app {
  display: grid;
  grid-template-columns: 320px minmax(420px, 1fr) 300px;
  height: 100vh;
  overflow: hidden;
}

.blurred {
  filter: blur(2px);
  pointer-events: none;
}

.login-screen {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  background: rgba(237, 240, 236, 0.92);
}

.login-screen[hidden] {
  display: none;
}

.login-card {
  width: min(380px, calc(100vw - 32px));
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 22px;
  box-shadow: 0 18px 50px rgba(30, 45, 35, 0.18);
}

.login-card .search {
  width: 100%;
  margin: 12px 0 0;
}

.login-card .send-btn {
  width: 100%;
  margin-top: 14px;
}

.login-error {
  color: var(--danger);
  font-size: 12px;
  margin-top: 10px;
  min-height: 18px;
}

.sidebar,
.inspector {
  background: var(--panel);
  border-right: 1px solid var(--line);
  min-width: 0;
}

.inspector {
  border-right: 0;
  border-left: 1px solid var(--line);
  padding: 16px;
  overflow: auto;
}

.topbar,
.chat-head {
  height: 64px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  background: rgba(248, 250, 247, 0.96);
  min-width: 0;
  overflow: hidden;
}

.title,
.head-title,
.dialog-title {
  font-weight: 700;
  font-size: 16px;
}

.subtitle,
.head-meta,
.dialog-subtitle {
  color: var(--muted);
  font-size: 12px;
  margin-top: 3px;
}

.logout-btn {
  margin-left: auto;
  height: 32px;
  padding: 0 12px;
  flex: 0 0 auto;
}

.head-title,
.head-meta {
  max-width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn,
.secondary-btn,
.send-btn,
.mini-btn,
.mode-select {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  height: 34px;
  padding: 0 12px;
  border-radius: 6px;
  cursor: pointer;
}

.icon-btn {
  margin-left: auto;
}

.secondary-btn:hover,
.icon-btn:hover {
  border-color: #aeb8af;
}

.send-btn {
  border-color: var(--green);
  background: var(--green);
  color: #fff;
  font-weight: 700;
}

.mini-btn {
  height: 28px;
  padding: 0 9px;
  font-size: 12px;
}

.plus-btn {
  width: 32px;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: #fff;
  color: var(--green-deep);
  font-size: 24px;
  line-height: 28px;
  cursor: pointer;
}

.tool-hint {
  color: var(--muted);
  font-size: 12px;
}

.mode-select {
  width: 92px;
}

.back-btn {
  display: none;
}

.send-btn:hover {
  background: var(--green-deep);
}

.search {
  width: calc(100% - 24px);
  margin: 12px;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 10px;
  background: #fff;
}

.contact-list {
  height: calc(100vh - 112px);
  overflow: auto;
}

.contact {
  display: grid;
  grid-template-columns: 42px 1fr;
  gap: 10px;
  padding: 12px 14px;
  cursor: pointer;
  border-bottom: 1px solid rgba(217, 222, 215, 0.7);
}

.contact:hover,
.contact.active {
  background: #e8eee8;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 6px;
  display: grid;
  place-items: center;
  background: #dbe8df;
  color: #2c573c;
  font-weight: 700;
  flex: 0 0 auto;
}

.contact-name {
  font-weight: 700;
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.contact-meta,
.contact-preview,
.tag,
.source-item {
  color: var(--muted);
  font-size: 12px;
}

.contact-preview {
  margin-top: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.contact-tags {
  display: flex;
  gap: 4px;
  margin-top: 5px;
  overflow: hidden;
  white-space: nowrap;
}

.contact-tags span {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 1px 6px;
  color: var(--muted);
  background: #fff;
  font-size: 11px;
}

.contact-tags .status-pill {
  color: #23613a;
  border-color: #b8d4bf;
  background: #eaf5ec;
}

.contact-tags .interest-pill,
.interest-text {
  font-weight: 700;
}

.interest-text {
  display: inline-flex;
  width: fit-content;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 8px;
}

.level-SS {
  color: #8a3b00 !important;
  border-color: #efb871 !important;
  background: #fff2d9 !important;
}

.level-S {
  color: #9d2f1f !important;
  border-color: #e5a096 !important;
  background: #fff0ed !important;
}

.level-A {
  color: #1f5f3a !important;
  border-color: #9fcfae !important;
  background: #edf8ef !important;
}

.level-B {
  color: #245b83 !important;
  border-color: #a8c9df !important;
  background: #edf6fb !important;
}

.level-C {
  color: #6a5a2a !important;
  border-color: #d8c98f !important;
  background: #fbf7df !important;
}

.level-D {
  color: #666 !important;
  border-color: #d2d2d2 !important;
  background: #f3f3f3 !important;
}

.chat {
  display: grid;
  grid-template-rows: 64px 1fr 132px;
  min-width: 0;
  min-height: 0;
}

.head-main {
  min-width: 0;
  flex: 1;
  overflow: hidden;
}

.messages {
  padding: 14px 18px;
  overflow: auto;
  min-height: 0;
  background:
    linear-gradient(90deg, rgba(255,255,255,0.28) 1px, transparent 1px),
    linear-gradient(rgba(255,255,255,0.28) 1px, transparent 1px);
  background-size: 28px 28px;
}

.message-row {
  display: flex;
  margin: 7px 0;
  align-items: flex-start;
  gap: 7px;
}

.message-row.ai {
  justify-content: flex-start;
}

.message-row.draft {
  justify-content: flex-start;
}

.message-row.customer {
  justify-content: flex-end;
}

.message-row.system {
  justify-content: center;
}

.message-avatar {
  width: 32px;
  height: 32px;
  border-radius: 5px;
  display: grid;
  place-items: center;
  flex: 0 0 32px;
  margin-top: 1px;
  font-size: 13px;
  font-weight: 700;
}

.seller-avatar {
  background: #e7efe8;
  color: #245d3c;
}

.customer-avatar {
  background: #dbe8df;
  color: #2c573c;
}

.bubble {
  display: inline-flex;
  flex-direction: column;
  width: fit-content;
  max-width: min(420px, 58%);
  min-width: 0;
  min-height: 0;
  padding: 6px 9px;
  border-radius: 8px;
  background: var(--bubble);
  box-shadow: var(--shadow);
  white-space: normal;
  line-height: 1.4;
  word-break: break-word;
  overflow-wrap: anywhere;
  gap: 4px;
}

.message-text {
  display: block;
  margin: 0;
  padding: 0;
  min-height: 0;
  white-space: pre-wrap;
}

.message-text:empty {
  display: none;
}

.draft-label {
  white-space: normal;
}

.customer .bubble {
  background: var(--customer);
  max-width: min(420px, 62%);
}

.system .bubble {
  background: #fff1f1;
  color: var(--danger);
  font-size: 12px;
}

.draft .bubble {
  border: 1px dashed #b8b05f;
  background: #fffbe8;
}

.draft-label {
  color: #8a6a12;
  font-size: 12px;
  margin-bottom: 6px;
}

.draft-actions {
  margin-top: 6px;
}

.draft-editor {
  width: min(480px, 100%);
  min-height: 44px;
  height: auto;
  resize: vertical;
  display: block;
  margin-bottom: 6px;
  background: #fffef4;
}

.chat-image {
  display: block;
  max-width: min(240px, 100%);
  max-height: 320px;
  border-radius: 6px;
  margin-top: 6px;
  border: 1px solid var(--line);
}

.composer {
  display: grid;
  grid-template-columns: 1fr 112px;
  grid-template-rows: 30px 1fr;
  gap: 12px;
  padding: 14px;
  border-top: 1px solid var(--line);
  background: var(--panel);
  min-height: 0;
}

.composer-tools {
  grid-column: 1 / -1;
  display: flex;
  gap: 8px;
  align-items: center;
  overflow-x: auto;
}

textarea {
  width: 100%;
  height: 100%;
  resize: none;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px;
  outline: none;
}

textarea:focus,
input:focus {
  border-color: var(--green);
}

.panel-title {
  font-weight: 700;
  margin: 4px 0 10px;
}

.profile-box,
.source-box {
  display: grid;
  gap: 8px;
  margin-bottom: 22px;
}

.profile-line {
  display: grid;
  grid-template-columns: 68px 1fr;
  gap: 8px;
}

.profile-line span:first-child {
  color: var(--muted);
}

.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.tag {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 999px;
  padding: 4px 8px;
}

.source-item {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px;
  word-break: break-word;
}

.followup-draft {
  line-height: 1.55;
}

.review-panel {
  margin-bottom: 22px;
}

.review-box {
  display: grid;
  gap: 8px;
}

.review-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.review-tag {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 6px;
  padding: 5px 8px;
  color: var(--text);
  font-size: 12px;
}

.review-tag.active {
  border-color: #2f7d57;
  background: #eaf6ef;
  color: #1e5f3f;
  font-weight: 700;
}

.review-note {
  min-height: 76px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px;
  line-height: 1.5;
}

.review-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
}

.settings {
  width: min(520px, calc(100vw - 32px));
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0;
}

.settings::backdrop {
  background: rgba(20, 30, 24, 0.35);
}

.settings form {
  padding: 16px;
}

.dialog-head {
  display: flex;
  align-items: start;
  gap: 12px;
  margin-bottom: 14px;
}

.settings label {
  display: grid;
  gap: 6px;
  margin: 12px 0;
  color: var(--muted);
}

.settings input {
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 10px;
  color: var(--text);
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 16px;
}

.loading .send-btn,
.loading #messageInput {
  opacity: 0.65;
  pointer-events: none;
}

@media (max-width: 1080px) {
  .app {
    grid-template-columns: 280px 1fr;
  }

  .inspector {
    display: none;
  }
}

@media (max-width: 760px) {
  .app {
    grid-template-columns: 1fr;
  }

  .sidebar {
    display: block;
    height: 100vh;
  }

  .chat,
  .inspector {
    display: none;
  }

  .app.mobile-chat-open .sidebar {
    display: none;
  }

  .app.mobile-chat-open .chat {
    display: grid;
  }

  .back-btn {
    display: inline-flex;
    align-items: center;
  }

  .chat-head {
    gap: 8px;
    padding: 10px;
    min-width: 0;
  }

  .chat-head .secondary-btn:not(.back-btn),
  .chat-head .mode-select {
    height: 30px;
    padding: 0 8px;
    font-size: 12px;
    flex: 0 0 auto;
  }

  .head-main {
    min-width: 0;
  }

  .head-meta {
    display: none;
  }

  #settingsBtn,
  #reloadKbBtn {
    display: none;
  }

  .chat {
    grid-template-rows: 64px 1fr 142px;
    height: 100vh;
  }

  .bubble {
    max-width: calc(100vw - 104px);
  }

  .customer .bubble {
    max-width: calc(100vw - 104px);
  }

  .message-avatar {
    width: 30px;
    height: 30px;
    flex-basis: 30px;
    font-size: 12px;
  }

  .composer {
    grid-template-columns: 1fr 86px;
    padding: 10px;
    gap: 8px;
  }

  .composer-tools {
    gap: 6px;
  }

  .mini-btn {
    white-space: nowrap;
  }
}
