/* chat.css — 温暖烘焙风，移动端优先，微信浏览器适配 */
:root {
  --primary: #8B5E3C;
  --primary-dark: #6B3E26;
  --primary-light: #C4956A;
  --bg: #F7F3EE;
  --card-bg: #FFFFFF;
  --bubble-user: #E8D5C4;
  --bubble-ai: #FFFFFF;
  --accent: #D4A574;
  --text: #2C1810;
  --text-secondary: #8B7355;
  --text-muted: #B8A088;
  --border: #E5D5C8;
  --error-bg: #FDE8E8;
  --error-text: #C62828;
  --shadow-sm: 0 1px 3px rgba(44, 24, 16, 0.08);
  --shadow-md: 0 2px 8px rgba(44, 24, 16, 0.12);
  --radius: 16px;
  --radius-sm: 10px;
  --topbar-height: 52px;
  --input-height: 56px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
  overscroll-behavior: none;
  -webkit-overflow-scrolling: touch;
  user-select: none;
  -webkit-user-select: none;
}

/* ========== Topbar ========== */
.topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  height: var(--topbar-height);
  padding-top: var(--safe-top);
  background: var(--primary-dark);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.15);
}
.topbar .logo {
  font-size: 1.2rem;
  line-height: 1;
}
.topbar h1 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: 0.5px;
}

/* ========== Main content area ========== */
.main {
  padding-top: calc(var(--topbar-height) + var(--safe-top));
  padding-bottom: calc(var(--input-height) + var(--safe-bottom) + 12px);
  height: 100%;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

/* ========== Login page ========== */
.login-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100%;
  padding: 24px 16px;
  background: linear-gradient(135deg, var(--bg) 0%, #EDE0D4 100%);
}
.login {
  background: var(--card-bg);
  border-radius: 20px;
  padding: 32px 24px;
  box-shadow: 0 4px 20px rgba(44, 24, 16, 0.12);
  width: 100%;
  max-width: 360px;
}
.login .brand {
  text-align: center;
  margin-bottom: 24px;
}
.login .brand-icon {
  font-size: 2.5rem;
  display: block;
  margin-bottom: 8px;
}
.login .brand h2 {
  margin: 0;
  font-size: 1.25rem;
  color: var(--primary-dark);
  font-weight: 600;
}
.login .brand p {
  margin: 4px 0 0;
  font-size: 0.85rem;
  color: var(--text-secondary);
}
.login label {
  display: block;
  margin: 16px 0;
}
.login label span {
  display: block;
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-bottom: 4px;
  font-weight: 500;
}
.login input {
  width: 100%;
  padding: 14px 16px;
  font-size: 1rem;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  transition: border-color 0.2s, box-shadow 0.2s;
  -webkit-appearance: none;
  appearance: none;
}
.login input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(139, 94, 60, 0.15);
}
.login button {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
  color: #fff;
  border: 0;
  border-radius: var(--radius-sm);
  font-size: 1rem;
  font-weight: 600;
  margin-top: 8px;
  cursor: pointer;
  transition: opacity 0.2s, transform 0.15s;
  -webkit-appearance: none;
  appearance: none;
}
.login button:active {
  opacity: 0.85;
  transform: scale(0.98);
}
.error {
  color: var(--error-text);
  background: var(--error-bg);
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  margin: 0 0 8px;
  text-align: center;
}
.hint {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.8rem;
  margin: 16px 0 0;
}

/* ========== Chat page ========== */
.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 16px;
  font-size: 0.8rem;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border);
  background: var(--card-bg);
  position: sticky;
  top: 0;
  z-index: 10;
}
.toolbar .logout-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--primary);
  cursor: pointer;
  font-size: 0.8rem;
  padding: 4px 12px;
  border-radius: 20px;
  transition: background 0.2s;
  -webkit-appearance: none;
  appearance: none;
}
.toolbar .logout-btn:active {
  background: var(--border);
}

/* Bubbles container */
.bubbles {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px;
  padding-bottom: 4px;
  min-height: 100%;
}

/* Individual bubbles */
.bubble {
  padding: 12px 16px;
  border-radius: var(--radius);
  max-width: 85%;
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.5;
  animation: fadeInUp 0.3s ease;
}
.bubble.user {
  background: var(--bubble-user);
  color: var(--text);
  align-self: flex-end;
  border-radius: 18px 18px 4px 18px;
  box-shadow: var(--shadow-sm);
}
.bubble.assistant {
  background: var(--bubble-ai);
  color: var(--text);
  align-self: flex-start;
  border: 1px solid var(--border);
  border-radius: 18px 18px 18px 4px;
  box-shadow: var(--shadow-sm);
}
.bubble.tool {
  background: #F5F0EB;
  font-size: 0.82rem;
  color: var(--text-secondary);
  align-self: center;
  max-width: 90%;
  padding: 6px 14px;
  border-radius: 20px;
  text-align: center;
  animation: none;
}
.bubble.tool.ok {
  color: #5D8A3C;
}
.bubble.tool.err {
  color: var(--error-text);
}
.bubble.error {
  background: var(--error-bg);
  color: var(--error-text);
  align-self: center;
  max-width: 90%;
  text-align: center;
}

/* Bubble animation */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ========== Fixed bottom input bar ========== */
.bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 100;
  padding: 8px 12px;
  padding-bottom: calc(8px + var(--safe-bottom));
  background: var(--card-bg);
  border-top: 1px solid var(--border);
  box-shadow: 0 -2px 8px rgba(44, 24, 16, 0.06);
}
form.ask {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  max-width: 720px;
  margin: 0 auto;
}
form.ask textarea {
  flex: 1;
  min-height: 40px;
  max-height: 120px;
  padding: 10px 16px;
  font-size: 1rem;
  border: 1.5px solid var(--border);
  border-radius: 22px;
  background: var(--bg);
  color: var(--text);
  resize: none;
  font-family: inherit;
  line-height: 1.4;
  transition: border-color 0.2s, box-shadow 0.2s;
  -webkit-appearance: none;
  appearance: none;
}
form.ask textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(139, 94, 60, 0.12);
}
form.ask textarea::placeholder {
  color: var(--text-muted);
}
form.ask .send-btn {
  width: 44px;
  height: 44px;
  min-width: 44px;
  padding: 0;
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
  color: #fff;
  border: 0;
  border-radius: 50%;
  font-size: 1.2rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.2s, transform 0.15s;
  -webkit-appearance: none;
  appearance: none;
  flex-shrink: 0;
}
form.ask .send-btn:active {
  opacity: 0.85;
  transform: scale(0.92);
}
form.ask .send-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ========== Scrollbar ========== */
.bubbles::-webkit-scrollbar,
.main::-webkit-scrollbar {
  width: 4px;
}
.bubbles::-webkit-scrollbar-track,
.main::-webkit-scrollbar-track {
  background: transparent;
}
.bubbles::-webkit-scrollbar-thumb,
.main::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}

/* ========== Utility ========== */
.hidden { display: none !important; }

/* ========== WeChat browser fixes ========== */
/* Disable double-tap zoom */
* { touch-action: manipulation; }
/* Prevent iOS text size adjust on orientation change */
html { -webkit-text-size-adjust: 100%; }
/* Safe area for notch devices */
@supports (padding: env(safe-area-inset-top)) {
  .topbar { padding-top: var(--safe-top); }
}

/* ========== Dark mode (auto) ========== */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1A1410;
    --card-bg: #241E18;
    --bubble-user: #3D2E20;
    --bubble-ai: #2A221C;
    --text: #F0E8E0;
    --text-secondary: #B8A088;
    --text-muted: #7A6A58;
    --border: #3D3228;
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
    --shadow-md: 0 2px 8px rgba(0,0,0,0.4);
    --error-bg: #3D1A1A;
    --error-text: #EF9A9A;
  }
  .login { box-shadow: 0 4px 20px rgba(0,0,0,0.4); }
  .login-wrapper { background: linear-gradient(135deg, var(--bg) 0%, #1A1410 100%); }
  .topbar { box-shadow: 0 2px 4px rgba(0,0,0,0.3); }
  .bottom-bar { box-shadow: 0 -2px 8px rgba(0,0,0,0.2); }
  .bubble.assistant { border-color: var(--border); }
  .bubble.tool { background: #2A221C; }
  form.ask textarea { background: #1A1410; }
}

/* ========== Responsive ========== */
@media (max-width: 480px) {
  .bubble { max-width: 90%; }
  .login { padding: 24px 16px; }
  .bottom-bar { padding: 6px 10px; padding-bottom: calc(6px + var(--safe-bottom)); }
}