:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --border: #e5e7eb;
  --text: #1f2937;
  --muted: #6b7280;
  --primary: #0a84ff;
  --primary-hover: #0070e0;
  --error-bg: #fef2f2;
  --error-border: #fecaca;
  --error-text: #b91c1c;
  --avatar-bg: #eef2ff;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}

* {
  box-sizing: border-box;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-size: 16px;
  line-height: 1.55;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

#app {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 820px;
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
}

#header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

#header h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
}

#clear-btn {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 14px;
  padding: 6px 10px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

#clear-btn:hover {
  background: #f0f1f4;
  color: var(--text);
}

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  -webkit-overflow-scrolling: touch;
}

.message {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 100%;
}

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

.message.user .message-avatar {
  display: none;
}

.message.bot,
.message.error {
  justify-content: flex-start;
}

.message-avatar {
  flex-shrink: 0;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--avatar-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  line-height: 1;
}

.message-content {
  max-width: 75%;
  padding: 10px 14px;
  border-radius: 16px;
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  font-size: 16px;
  line-height: 1.5;
}

.message.user .message-content {
  background: var(--primary);
  color: #ffffff;
  border-bottom-right-radius: 4px;
}

.message.bot .message-content {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  box-shadow: var(--shadow);
}

.message.error .message-content {
  background: var(--error-bg);
  color: var(--error-text);
  border: 1px solid var(--error-border);
  border-bottom-left-radius: 4px;
}

.typing-indicator {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 0;
}

.typing-indicator .dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  opacity: 0.4;
  animation: typing-bounce 1.2s infinite ease-in-out;
}

.typing-indicator .dot:nth-child(2) {
  animation-delay: 0.15s;
}

.typing-indicator .dot:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes typing-bounce {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.4;
  }
  30% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

#start-overlay {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
}

#start-btn {
  font-size: 17px;
  font-weight: 600;
  padding: 14px 32px;
  background: var(--primary);
  color: #ffffff;
  border: none;
  border-radius: 24px;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(10, 132, 255, 0.25);
  transition: background 0.15s ease, transform 0.05s ease;
}

#start-btn:hover {
  background: var(--primary-hover);
}

#start-btn:active {
  transform: scale(0.97);
}

#input-area {
  position: sticky;
  bottom: 0;
  z-index: 10;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 20px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}

#user-input {
  flex: 1;
  resize: none;
  font-family: inherit;
  font-size: 16px;
  line-height: 1.5;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--bg);
  color: var(--text);
  max-height: 120px;
  overflow-y: auto;
  outline: none;
  transition: border-color 0.15s ease, background 0.15s ease;
}

#user-input:focus {
  border-color: var(--primary);
  background: var(--surface);
}

#user-input:disabled {
  background: #eef0f3;
  color: var(--muted);
  cursor: not-allowed;
}

#send-btn {
  flex-shrink: 0;
  min-width: 64px;
  padding: 10px 18px;
  background: var(--primary);
  color: #ffffff;
  border: none;
  border-radius: 18px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.05s ease;
}

#send-btn:hover {
  background: var(--primary-hover);
}

#send-btn:active {
  transform: scale(0.97);
}

#send-btn:disabled {
  background: #b8c2cc;
  cursor: not-allowed;
}

@media (max-width: 768px) {
  #app {
    max-width: 100%;
    height: 100vh;
    height: 100dvh;
  }

  #header {
    padding: 10px 14px;
  }

  #messages {
    padding: 12px 14px;
  }

  .message-content {
    max-width: 85%;
  }

  #input-area {
    padding: 10px 14px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
  }
}

/* Debug Panel */
#debug-panel {
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 9999;
  font-family: "SF Mono", "Fira Code", monospace;
  font-size: 12px;
}

#debug-toggle {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #2d2d2d;
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
  position: relative;
}

#debug-toggle:hover {
  background: #3d3d3d;
}

#debug-count {
  position: absolute;
  top: -4px;
  right: -4px;
  background: #0a84ff;
  color: #fff;
  border-radius: 10px;
  padding: 1px 6px;
  font-size: 10px;
  min-width: 16px;
  text-align: center;
}

#debug-content {
  display: none;
  position: fixed;
  top: 52px;
  left: 12px;
  width: 380px;
  max-height: 60vh;
  background: #1e1e1e;
  color: #d4d4d4;
  border-radius: 12px;
  border: 1px solid #333;
  box-shadow: 0 4px 20px rgba(0,0,0,0.4);
  overflow-y: auto;
  padding: 12px 14px;
}

#debug-panel.open #debug-content {
  display: block;
}

.debug-entry {
  margin-bottom: 16px;
  border-bottom: 1px solid #333;
  padding-bottom: 12px;
}

.debug-entry:last-child {
  border-bottom: none;
}

.debug-time {
  color: #888;
  margin-bottom: 4px;
}

.debug-label {
  color: #0a84ff;
  font-weight: bold;
  margin: 6px 0 2px;
}

.debug-json {
  white-space: pre-wrap;
  word-break: break-all;
  background: #252525;
  padding: 6px 8px;
  border-radius: 4px;
  max-height: 200px;
  overflow-y: auto;
  margin: 2px 0;
}

@media (max-width: 768px) {
  #debug-content {
    width: calc(100vw - 32px);
    left: 16px;
  }
}
