:root {
  color-scheme: light;
  --page: #eef1ed;
  --surface: #ffffff;
  --surface-muted: #f5f7f4;
  --ink: #1d2722;
  --muted: #69736d;
  --line: #dce2dd;
  --accent: #16734b;
  --accent-strong: #0f5d3c;
  --customer: #dff2e8;
  --danger: #a43a31;
  --shadow: 0 18px 50px rgba(25, 42, 33, 0.12);
  font-family: Inter, "Noto Sans SC", "PingFang SC", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  overflow-x: hidden;
}

button, textarea { font: inherit; letter-spacing: 0; }

button { cursor: pointer; }

.chat-shell {
  width: min(100vw, 780px);
  max-width: 100vw;
  min-width: 0;
  height: 100dvh;
  margin: 0 auto;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto auto;
  background: var(--surface);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.topbar {
  min-height: 76px;
  padding: max(14px, env(safe-area-inset-top)) 18px 14px;
  display: flex;
  align-items: center;
  gap: 12px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.96);
  min-width: 0;
}

.agent-mark {
  width: 42px;
  height: 42px;
  flex: 0 0 42px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
  font-size: 14px;
  font-weight: 750;
}

.agent-title { min-width: 0; flex: 1; }

.agent-title h1 {
  margin: 0;
  font-size: 17px;
  line-height: 1.35;
  letter-spacing: 0;
}

.agent-title p {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: 12px;
}

.status-dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  margin-right: 6px;
  border-radius: 50%;
  background: #27a567;
}

.end-button, .close-button {
  min-height: 36px;
  padding: 0 11px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--muted);
  font-size: 13px;
}

.end-button:hover, .end-button:focus-visible,
.close-button:hover, .close-button:focus-visible {
  border-color: var(--danger);
  color: var(--danger);
}

.close-button {
  display: none;
  width: 36px;
  padding: 0;
  font-size: 22px;
  line-height: 1;
}

body.embedded .chat-shell {
  width: 100vw;
  box-shadow: none;
}

body.embedded .topbar {
  min-height: 66px;
  padding: 11px 14px;
}

body.embedded .agent-mark {
  width: 38px;
  height: 38px;
  flex-basis: 38px;
}

body.embedded .close-button { display: block; }

.messages {
  overflow-y: auto;
  padding: 24px 18px 10px;
  background: var(--surface-muted);
  scroll-behavior: smooth;
}

.message {
  display: flex;
  margin: 0 0 14px;
}

.message.user { justify-content: flex-end; }

.bubble {
  max-width: min(82%, 560px);
  padding: 11px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  font-size: 15px;
  line-height: 1.65;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.message.user .bubble {
  border-color: #c4e5d3;
  background: var(--customer);
}

.message.pending .bubble { opacity: 0.72; }

.message.failed .bubble {
  border-color: #d9a7a2;
  opacity: 1;
}

.reply-indicator {
  width: 52px;
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.reply-indicator span {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--muted);
  animation: reply-pulse 1s ease-in-out infinite;
}

.reply-indicator span:nth-child(2) { animation-delay: 120ms; }
.reply-indicator span:nth-child(3) { animation-delay: 240ms; }

@keyframes reply-pulse {
  0%, 60%, 100% { opacity: 0.35; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-2px); }
}

.bubble img {
  display: block;
  width: min(100%, 320px);
  max-height: 320px;
  margin-bottom: 8px;
  border-radius: 6px;
  object-fit: cover;
}

.image-preview {
  position: relative;
  width: 82px;
  margin: 10px 18px 0;
}

.image-preview img {
  width: 82px;
  height: 62px;
  display: block;
  border: 1px solid var(--line);
  border-radius: 6px;
  object-fit: cover;
}

.image-preview button {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 24px;
  height: 24px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--surface);
  color: var(--ink);
  line-height: 20px;
}

.composer {
  min-height: 72px;
  padding: 10px 18px calc(10px + env(safe-area-inset-bottom));
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: end;
  gap: 9px;
  border-top: 1px solid var(--line);
  background: var(--surface);
  min-width: 0;
}

.image-button, .send-button {
  height: 42px;
  display: grid;
  place-items: center;
  border-radius: 6px;
  font-size: 13px;
}

.image-button {
  width: 42px;
  min-width: 42px;
  border: 1px solid var(--line);
  color: var(--muted);
  cursor: pointer;
  font-size: 26px;
  font-weight: 300;
  line-height: 1;
}

.composer textarea {
  width: 100%;
  min-height: 42px;
  max-height: 126px;
  padding: 10px 12px;
  resize: none;
  border: 1px solid var(--line);
  border-radius: 6px;
  outline: none;
  color: var(--ink);
  background: var(--surface-muted);
  line-height: 1.45;
}

.composer textarea:focus { border-color: var(--accent); }

.send-button {
  min-width: 58px;
  padding: 0 13px;
  border: 0;
  background: var(--accent);
  color: #fff;
  font-weight: 650;
}

.send-button:hover, .send-button:focus-visible { background: var(--accent-strong); }

button:disabled, textarea:disabled { cursor: not-allowed; opacity: 0.55; }

.ended-panel {
  padding: 12px 18px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--surface);
}

.ended-panel button {
  width: 100%;
  min-height: 44px;
  border: 0;
  border-radius: 6px;
  background: var(--ink);
  color: #fff;
}

.notice {
  position: fixed;
  left: 50%;
  bottom: 92px;
  transform: translateX(-50%);
  max-width: calc(100% - 36px);
  padding: 9px 13px;
  border-radius: 6px;
  background: var(--ink);
  color: #fff;
  font-size: 13px;
  box-shadow: var(--shadow);
}

button:focus-visible, label:focus-within {
  outline: 3px solid rgba(22, 115, 75, 0.22);
  outline-offset: 2px;
}

@media (max-width: 520px) {
  .chat-shell { box-shadow: none; }
  .messages { padding-inline: 14px; }
  .topbar, .composer { padding-left: 14px; padding-right: 14px; }
  .bubble { max-width: 88%; }
  body.embedded .topbar {
    padding-top: max(11px, env(safe-area-inset-top));
  }
}

@media (prefers-reduced-motion: reduce) {
  .messages { scroll-behavior: auto; }
  .reply-indicator span { animation: none; opacity: 0.65; }
}
