/* ==========================================
   聊天空间 - Butterfly 主题风格
   ========================================== */

/* ---- CSS 变量 —— 与 Butterfly 主题对齐 ---- */
:root {
  --global-bg: #f7f9fe;
  --font-color: #4c4948;
  --font-secondary: #858585;
  --card-bg: #ffffff;
  --card-hover: #f6f8fa;
  --theme-color: #49b1f5;
  --theme-color-rgb: 73, 177, 245;
  --border-color: #e3e8f0;
  --shadow: 0 3px 8px 6px rgba(7, 17, 27, 0.05);
  --shadow-hover: 0 4px 16px rgba(7, 17, 27, 0.1);
  --radius: 8px;
  --radius-lg: 12px;
  --code-bg: #f2f2f2;
  --blockquote-border: #49b1f5;
  --pre-bg: #263238;
  --pre-color: #eeffff;
  --msg-self-bg: rgba(var(--theme-color-rgb), 0.08);
  --msg-self-border: rgba(var(--theme-color-rgb), 0.2);
  --msg-other-bg: #f8f9fa;
  --msg-other-border: #e8eaed;
}

[data-theme="dark"] {
  --global-bg: #0d0d0d;
  --font-color: rgba(255, 255, 255, 0.8);
  --font-secondary: rgba(255, 255, 255, 0.5);
  --card-bg: #1a1a1a;
  --card-hover: #242424;
  --border-color: #2e2e2e;
  --shadow: 0 3px 8px 6px rgba(0, 0, 0, 0.15);
  --shadow-hover: 0 4px 16px rgba(0, 0, 0, 0.25);
  --code-bg: #2a2a2a;
  --msg-self-bg: rgba(73, 177, 245, 0.12);
  --msg-self-border: rgba(73, 177, 245, 0.25);
  --msg-other-bg: #222222;
  --msg-other-border: #333333;
}

/* ---- 基础重置 ---- */
* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans SC", sans-serif;
  font-size: 14px;
  color: var(--font-color);
  background: transparent;
  line-height: 1.6;
}

#chat-app {
  max-width: 800px;
  margin: 0 auto;
  min-height: 75vh;
  display: flex;
  flex-direction: column;
}

/* ---- 页面切换 ---- */
.screen { display: none; flex-direction: column; }
.screen.active { display: flex; }

/* ---- 登录界面 ---- */
.login-card {
  background: var(--card-bg);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 40px 32px;
  max-width: 380px;
  margin: 60px auto;
  text-align: center;
  border: 1px solid var(--border-color);
}
.login-icon { font-size: 48px; margin-bottom: 12px; }
.login-card h2 {
  font-size: 22px;
  font-weight: 700;
  color: var(--font-color);
  margin-bottom: 6px;
}
.login-desc {
  font-size: 13px;
  color: var(--font-secondary);
  margin-bottom: 20px;
}
#password-input {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  font-size: 14px;
  outline: none;
  background: var(--global-bg);
  color: var(--font-color);
  transition: border-color 0.2s;
}
#password-input:focus {
  border-color: var(--theme-color);
  box-shadow: 0 0 0 3px rgba(var(--theme-color-rgb), 0.12);
}
.error-msg {
  color: #e74c3c;
  font-size: 12px;
  margin-top: 8px;
  min-height: 18px;
}
#join-btn {
  width: 100%;
  margin-top: 14px;
}

/* ---- 聊天头部 ---- */
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--card-bg);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  border: 1px solid var(--border-color);
  border-bottom: none;
  box-shadow: 0 1px 4px rgba(0,0,0,0.04);
}
.chat-room-name {
  font-size: 16px;
  font-weight: 700;
}
.chat-header-right {
  display: flex;
  align-items: center;
  gap: 8px;
}
.visitor-badge {
  font-size: 12px;
  background: rgba(var(--theme-color-rgb), 0.12);
  color: var(--theme-color);
  padding: 2px 10px;
  border-radius: 20px;
  font-weight: 600;
}

/* ---- 消息列表 ---- */
.message-list {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
  background: var(--card-bg);
  border-left: 1px solid var(--border-color);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-height: 55vh;
  min-height: 300px;
}
.message-list::-webkit-scrollbar {
  width: 5px;
}
.message-list::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: 10px;
}

#messages-end {
  flex-shrink: 0;
  height: 1px;
}

/* ---- 单条消息 ---- */
.msg-wrapper {
  display: flex;
  flex-direction: column;
  max-width: 85%;
  animation: msgIn 0.2s ease;
}
@keyframes msgIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
.msg-wrapper.self { align-self: flex-end; }
.msg-wrapper.other { align-self: flex-start; }

.msg-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  color: var(--font-secondary);
  margin-bottom: 3px;
  padding: 0 4px;
}
.msg-wrapper.self .msg-meta { flex-direction: row-reverse; }
.msg-author { font-weight: 600; color: var(--theme-color); }
.msg-time { opacity: 0.7; }

.msg-bubble {
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 14px;
  line-height: 1.7;
  word-break: break-word;
}
.msg-wrapper.self .msg-bubble {
  background: var(--msg-self-bg);
  border: 1px solid var(--msg-self-border);
  border-radius: var(--radius) var(--radius) 0 var(--radius);
}
.msg-wrapper.other .msg-bubble {
  background: var(--msg-other-bg);
  border: 1px solid var(--msg-other-border);
  border-radius: var(--radius) var(--radius) var(--radius) 0;
}

/* ---- Markdown 渲染内容（消息体内） ---- */
.msg-bubble h1, .msg-bubble h2, .msg-bubble h3, .msg-bubble h4 {
  margin: 8px 0 4px;
  font-weight: 700;
}
.msg-bubble h1 { font-size: 1.4em; border-bottom: 2px solid var(--border-color); padding-bottom: 4px; }
.msg-bubble h2 { font-size: 1.2em; border-bottom: 1px solid var(--border-color); padding-bottom: 3px; }
.msg-bubble h3 { font-size: 1.05em; }
.msg-bubble p { margin: 4px 0; }
.msg-bubble ul, .msg-bubble ol { padding-left: 20px; margin: 4px 0; }
.msg-bubble li { margin: 2px 0; }
.msg-bubble a { color: var(--theme-color); text-decoration: none; }
.msg-bubble a:hover { text-decoration: underline; }
.msg-bubble code {
  background: var(--code-bg);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.9em;
  font-family: "Cascadia Code", "Fira Code", "Consolas", monospace;
}
.msg-bubble pre {
  background: var(--pre-bg);
  color: var(--pre-color);
  padding: 12px 16px;
  border-radius: var(--radius);
  overflow-x: auto;
  margin: 8px 0;
  font-size: 0.85em;
}
.msg-bubble pre code {
  background: none;
  padding: 0;
  color: inherit;
}
.msg-bubble blockquote {
  border-left: 4px solid var(--blockquote-border);
  padding: 4px 12px;
  margin: 6px 0;
  color: var(--font-secondary);
  background: var(--code-bg);
  border-radius: 0 var(--radius) var(--radius) 0;
}
.msg-bubble img {
  max-width: 100%;
  border-radius: var(--radius);
  margin: 4px 0;
}
.msg-bubble table {
  border-collapse: collapse;
  width: 100%;
  margin: 6px 0;
  font-size: 0.9em;
}
.msg-bubble th, .msg-bubble td {
  border: 1px solid var(--border-color);
  padding: 6px 10px;
  text-align: left;
}
.msg-bubble th {
  background: var(--code-bg);
  font-weight: 600;
}
.msg-bubble hr {
  border: none;
  border-top: 1px solid var(--border-color);
  margin: 8px 0;
}
.msg-bubble del { opacity: 0.6; }

/* ---- 输入区域 ---- */
.chat-input-area {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  background: var(--card-bg);
  border-radius: 0 0 var(--radius-lg) var(--radius-lg);
  border: 1px solid var(--border-color);
  border-top: none;
  box-shadow: 0 -1px 4px rgba(0,0,0,0.04);
}
#msg-input {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  font-size: 14px;
  font-family: inherit;
  outline: none;
  resize: none;
  background: var(--global-bg);
  color: var(--font-color);
  transition: border-color 0.2s;
  line-height: 1.5;
}
#msg-input:focus {
  border-color: var(--theme-color);
  box-shadow: 0 0 0 3px rgba(var(--theme-color-rgb), 0.12);
}


/* ---- 聊天室选择 ---- */
.room-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin: 16px 0;
  text-align: left;
}
.room-card {
  padding: 14px 16px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  cursor: pointer;
  transition: all 0.15s;
}
.room-card:hover {
  border-color: var(--theme-color);
  background: rgba(var(--theme-color-rgb), 0.05);
  transform: translateY(-1px);
  box-shadow: var(--shadow-hover);
}
.room-card-name {
  font-size: 15px;
  font-weight: 600;
  color: var(--font-color);
}
.room-card-desc {
  font-size: 12px;
  color: var(--font-secondary);
  margin-top: 4px;
}
.loading-text {
  text-align: center;
  color: var(--font-secondary);
  font-size: 13px;
  padding: 20px;
}
.login-btns {
  display: flex;
  gap: 10px;
  margin-top: 14px;
}
.login-btns .btn-primary { flex: 1; }
.login-btns .btn-ghost {
  padding: 10px 16px;
  background: transparent;
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  color: var(--font-secondary);
  cursor: pointer;
  font-size: 14px;
}
.login-btns .btn-ghost:hover {
  background: var(--card-hover);
  color: var(--font-color);
}
/* ---- 按钮 ---- */
.btn-primary {
  padding: 10px 20px;
  background: var(--theme-color);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}
.btn-primary:hover {
  opacity: 0.9;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(var(--theme-color-rgb), 0.3);
}
.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}
.btn-icon {
  width: 34px;
  height: 34px;
  background: none;
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  font-size: 14px;
  cursor: pointer;
  transition: all 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
}
.btn-icon:hover {
  background: var(--card-hover);
  border-color: var(--theme-color);
}

/* ---- 响应式 ---- */
@media (max-width: 640px) {
  #chat-app { max-width: 100%; }
  .login-card { margin: 40px 16px; padding: 28px 20px; }
  .message-list { max-height: 50vh; padding: 12px; }
  .chat-header { padding: 10px 14px; }
  .chat-input-area { padding: 10px 12px; }
  .msg-wrapper { max-width: 92%; }
}

