:root {
  --bg: #eef1f6;
  --panel: #ffffff;
  --header: #1f2937;
  --header-text: #ffffff;
  --user-bubble: #2f7cf6;
  --user-text: #ffffff;
  --agent-bubble: #ffffff;
  --agent-text: #1f2937;
  --muted: #8a94a6;
  --border: #e3e7ee;
  --danger: #e5484d;
  --ok: #22c55e;
  --warn: #f59e0b;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--agent-text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
}

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

.chat-header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(env(safe-area-inset-top) + 14px) 16px 14px;
  background: var(--header);
  color: var(--header-text);
}

.chat-header__title {
  font-size: 17px;
  font-weight: 600;
}

.chat-header__subtitle {
  margin-top: 2px;
  font-size: 12px;
  color: rgba(255, 255, 255, 0.7);
}

.chat-header__status {
  flex: 0 0 auto;
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.14);
  color: rgba(255, 255, 255, 0.9);
  white-space: nowrap;
}

.chat-header__status[data-state="online"] {
  background: rgba(34, 197, 94, 0.22);
  color: #b9f6ce;
}

.chat-header__status[data-state="offline"],
.chat-header__status[data-state="error"] {
  background: rgba(229, 72, 77, 0.22);
  color: #ffc9ca;
}

.chat-body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 16px 14px 8px;
  -webkit-overflow-scrolling: touch;
}

.chat-placeholder {
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  padding: 24px 12px;
  line-height: 1.6;
}

.fatal-title {
  color: var(--danger);
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 10px;
}

.fatal-detail {
  margin-top: 4px;
  word-break: break-all;
}

.fatal-retry {
  margin-top: 16px;
  border: 1px solid var(--border);
  background: #ffffff;
  color: var(--agent-text);
  border-radius: 999px;
  padding: 6px 22px;
  font-size: 14px;
  cursor: pointer;
}

.msg {
  display: flex;
  flex-direction: column;
  margin-bottom: 14px;
  max-width: 100%;
}

.msg-user {
  align-items: flex-end;
}

.msg-agent {
  align-items: flex-start;
}

.msg-system {
  align-items: center;
}

.bubble {
  max-width: 78%;
  padding: 9px 12px;
  border-radius: 14px;
  font-size: 15px;
  line-height: 1.5;
  word-break: break-word;
  white-space: pre-wrap;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.06);
}

.msg-user .bubble {
  background: var(--user-bubble);
  color: var(--user-text);
  border-bottom-right-radius: 4px;
}

.msg-agent .bubble {
  background: var(--agent-bubble);
  color: var(--agent-text);
  border-bottom-left-radius: 4px;
}

.msg-system .bubble {
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  box-shadow: none;
  padding: 4px 8px;
}

.meta {
  margin-top: 4px;
  font-size: 11px;
  color: var(--muted);
  padding: 0 4px;
}

.msg-user .meta {
  text-align: right;
}

.msg.is-failed .bubble {
  outline: 1px solid var(--danger);
}

.msg.is-failed .meta {
  color: var(--danger);
}

.chat-closed {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 10px 16px;
  background: #fff7ed;
  color: #9a3412;
  font-size: 13px;
  border-top: 1px solid #fed7aa;
}

.chat-closed button {
  border: 1px solid #fdba74;
  background: #ffffff;
  color: #9a3412;
  border-radius: 999px;
  padding: 4px 14px;
  font-size: 13px;
  cursor: pointer;
}

.chat-input {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px calc(env(safe-area-inset-bottom) + 10px);
  background: var(--panel);
  border-top: 1px solid var(--border);
}

.chat-input textarea {
  flex: 1 1 auto;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 9px 12px;
  font-size: 15px;
  line-height: 1.4;
  max-height: 120px;
  font-family: inherit;
  outline: none;
  background: #f7f9fc;
}

.chat-input textarea:focus {
  border-color: var(--user-bubble);
  background: #ffffff;
}

.chat-input button {
  flex: 0 0 auto;
  border: none;
  border-radius: 12px;
  background: var(--user-bubble);
  color: #ffffff;
  font-size: 15px;
  font-weight: 600;
  padding: 10px 18px;
  cursor: pointer;
}

.chat-input button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.emoji-toggle {
  flex: 0 0 auto;
  border: none;
  background: transparent;
  font-size: 24px;
  line-height: 1;
  padding: 8px 4px;
  cursor: pointer;
}

.emoji-panel {
  flex: 0 0 auto;
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  padding: 8px 10px;
  background: var(--panel);
  border-top: 1px solid var(--border);
  max-height: 168px;
  overflow-y: auto;
}

.emoji-panel button {
  border: none;
  background: transparent;
  font-size: 22px;
  line-height: 1;
  padding: 4px;
  cursor: pointer;
  border-radius: 6px;
}

.emoji-panel button:active {
  background: var(--bg);
}

.chat-input.is-disabled textarea,
.chat-input.is-disabled button {
  opacity: 0.5;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 96px;
  transform: translateX(-50%);
  background: rgba(31, 41, 55, 0.92);
  color: #ffffff;
  font-size: 13px;
  padding: 8px 14px;
  border-radius: 8px;
  max-width: 80%;
  text-align: center;
  z-index: 20;
}
