:root {
  --brand: #2563eb;
  --brand-contrast: #ffffff;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #1f2328;
  --muted: #6b7280;
  --border: #e5e7eb;
  --bubble-in: #ffffff;
  --danger: #dc2626;
  --success: #16a34a;
  --star: #f59e0b;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #111318;
    --surface: #1b1e24;
    --text: #e6e8eb;
    --muted: #9aa1ab;
    --border: #2c3038;
    --bubble-in: #252931;
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
}

.chat {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 760px;
  margin: 0 auto;
  background: var(--bg);
}

body:not(.embedded) .chat {
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
}

/* ---- header ---- */

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 12px 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  background: var(--brand);
  color: var(--brand-contrast);
}

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

.chat-logo {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.18);
  flex: none;
}

.chat-name {
  font-weight: 600;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  opacity: 0.9;
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #9ca3af;
  flex: none;
}

.status-dot.online {
  background: #4ade80;
}

.chat-actions {
  display: flex;
  gap: 2px;
}

.chat-actions button {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: inherit;
  cursor: pointer;
}

.chat-actions .text-btn {
  width: auto;
  height: 28px;
  margin: 2px 4px 0 0;
  padding: 0 10px;
  border: 1px solid color-mix(in srgb, var(--brand-contrast) 55%, transparent);
  border-radius: 999px;
  font-size: 12px;
  white-space: nowrap;
}

.chat-actions button:hover,
.chat-actions button:focus-visible {
  background: rgba(255, 255, 255, 0.18);
  outline: none;
}

/* ---- profile form ---- */

.profile-panel {
  display: grid;
  gap: 8px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.profile-panel[hidden] {
  display: none;
}

.profile-tip {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
}

.profile-panel input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  outline: none;
}

.profile-panel input:focus {
  border-color: var(--brand);
}

.profile-buttons {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.btn-primary,
.btn-text {
  padding: 6px 14px;
  border-radius: 8px;
  border: 0;
  cursor: pointer;
}

.btn-primary {
  background: var(--brand);
  color: var(--brand-contrast);
}

.btn-primary:disabled {
  opacity: 0.6;
  cursor: default;
}

.btn-text {
  background: transparent;
  color: var(--muted);
}

/* ---- messages ---- */

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 12px 8px;
  overscroll-behavior: contain;
}

.load-more {
  display: block;
  margin: 0 auto 12px;
  padding: 4px 12px;
  border: 0;
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
}

.time-sep {
  margin: 12px 0 8px;
  text-align: center;
  color: var(--muted);
  font-size: 12px;
}

.msg {
  display: flex;
  gap: 8px;
  margin: 6px 0;
  align-items: flex-end;
}

.msg.mine {
  flex-direction: row-reverse;
}

.msg-avatar {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--brand);
  color: var(--brand-contrast);
  font-size: 13px;
  flex: none;
  align-self: flex-start;
}

.msg-body {
  display: flex;
  flex-direction: column;
  max-width: 75%;
  min-width: 0;
}

.msg.mine .msg-body {
  align-items: flex-end;
}

.msg-name {
  font-size: 12px;
  color: var(--muted);
  margin: 0 0 2px 2px;
}

.ai-tag {
  display: inline-block;
  margin-left: 4px;
  padding: 0 4px;
  border-radius: 4px;
  background: color-mix(in srgb, var(--brand) 14%, transparent);
  color: var(--brand);
  font-size: 10px;
  font-weight: 600;
  line-height: 15px;
  vertical-align: 1px;
}

.bubble {
  padding: 8px 12px;
  border-radius: 14px;
  background: var(--bubble-in);
  border: 1px solid var(--border);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  border-top-left-radius: 4px;
}

.msg.mine .bubble {
  background: var(--brand);
  color: var(--brand-contrast);
  border-color: transparent;
  border-top-left-radius: 14px;
  border-top-right-radius: 4px;
}

.msg.mine .bubble a {
  color: inherit;
}

.bubble.image {
  padding: 4px;
  background: var(--surface);
}

.msg.mine .bubble.image {
  background: var(--surface);
  border-color: var(--border);
}

.bubble img {
  display: block;
  max-width: 220px;
  max-height: 260px;
  border-radius: 10px;
  cursor: zoom-in;
}

.msg-state {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
}

.msg-state.error {
  color: var(--danger);
  cursor: pointer;
}

.msg.pending .bubble {
  opacity: 0.7;
}

.sys {
  margin: 10px auto;
  max-width: 85%;
  text-align: center;
  color: var(--muted);
  font-size: 12px;
}

.sys span {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  background: rgba(127, 127, 127, 0.1);
}

/* ---- typing ---- */

.typing {
  display: flex;
  align-items: center;
  gap: 3px;
  padding: 0 16px 6px;
  color: var(--muted);
  font-size: 12px;
}

.typing[hidden] {
  display: none;
}

.typing span {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--muted);
  animation: blink 1.2s infinite ease-in-out;
}

.typing span:nth-child(2) {
  animation-delay: 0.2s;
}

.typing span:nth-child(3) {
  animation-delay: 0.4s;
  margin-right: 4px;
}

@keyframes blink {
  0%,
  80%,
  100% {
    opacity: 0.25;
  }
  40% {
    opacity: 1;
  }
}

/* ---- rating ---- */

.rating {
  margin: 0 12px 8px;
  padding: 12px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  text-align: center;
}

.rating[hidden] {
  display: none;
}

.rating-title {
  font-weight: 600;
}

.stars {
  display: flex;
  justify-content: center;
  gap: 4px;
  margin: 8px 0;
}

.stars button {
  border: 0;
  background: transparent;
  padding: 2px;
  cursor: pointer;
  color: var(--border);
}

.stars button.on {
  color: var(--star);
}

.stars button.on svg {
  fill: currentColor;
}

.rating textarea {
  width: 100%;
  min-height: 56px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  resize: none;
  outline: none;
}

.rating .row {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 8px;
}

.rating-done {
  color: var(--success);
}

/* ---- composer ---- */

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.composer textarea {
  flex: 1;
  min-height: 38px;
  max-height: 120px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 19px;
  background: var(--bg);
  resize: none;
  outline: none;
}

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

.icon-btn,
.send-btn {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 0;
  cursor: pointer;
  flex: none;
}

.icon-btn {
  background: transparent;
  color: var(--muted);
}

.icon-btn:hover {
  color: var(--brand);
}

.send-btn {
  background: var(--brand);
  color: var(--brand-contrast);
}

.send-btn:disabled {
  opacity: 0.5;
  cursor: default;
}

.toast {
  position: fixed;
  left: 50%;
  top: 72px;
  transform: translateX(-50%);
  padding: 8px 14px;
  border-radius: 8px;
  background: rgba(17, 24, 39, 0.9);
  color: #fff;
  font-size: 13px;
  z-index: 10;
  max-width: 90%;
}

.drop-hint {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(37, 99, 235, 0.12);
  border: 2px dashed var(--brand);
  color: var(--brand);
  font-weight: 600;
  pointer-events: none;
}
