:root {
  --bg: #f2f8f4;            /* 浅绿白背景 */
  --panel: #ffffff;
  --sidebar: #ffffff;       /* 白色侧边栏 */
  --sidebar-muted: #7c9a8c;
  --primary: #16a34a;       /* 主绿 */
  --primary-dark: #15803d;
  --primary-soft: rgba(22, 163, 74, 0.12);
  --text: #20352b;          /* 深绿灰文字 */
  --muted: #6f877a;
  --border: #e2efe8;
  --bubble-me: #d9f6e3;     /* 自己发的消息：浅绿气泡 */
  --bubble-them: #ffffff;
  --danger: #dc2626;
  --success: #16a34a;
  --radius: 12px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
}

button,
input,
textarea {
  font: inherit;
}

.hidden {
  display: none !important;
}

.muted {
  color: var(--sidebar-muted);
}

/* ---------------- auth ---------------- */

.auth-view {
  height: var(--vvh, 100vh);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.auth-card {
  width: min(92vw, 400px);
  background: var(--panel);
  border-radius: 18px;
  padding: 32px 28px;
  box-shadow: 0 20px 40px rgba(15, 23, 42, 0.12);
}

.auth-card h1 {
  margin: 0;
  text-align: center;
  font-size: 2rem;
  letter-spacing: 0.02em;
}

.subtitle {
  margin: 4px 0 22px;
  text-align: center;
  color: var(--muted);
}

.tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 18px;
  background: var(--bg);
  padding: 4px;
  border-radius: 10px;
}

.tab {
  border: none;
  background: transparent;
  padding: 9px;
  min-height: 44px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--muted);
  font-weight: 600;
}

.tab.active {
  background: var(--panel);
  color: var(--primary);
  box-shadow: 0 2px 6px rgba(15, 23, 42, 0.08);
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.auth-form input {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 13px;
  background: white;
}

.auth-form button,
.composer button,
.new-chat {
  border: none;
  border-radius: 10px;
  background: var(--primary);
  color: white;
  padding: 11px 16px;
  font-weight: 600;
  cursor: pointer;
}

.auth-form button:hover,
.composer button:hover,
.new-chat:hover {
  background: var(--primary-dark);
}

.auth-msg {
  min-height: 20px;
  margin-top: 14px;
  font-size: 0.9rem;
}

.auth-msg.error {
  color: var(--danger);
}

.auth-msg.success {
  color: var(--success);
}

/* 登录卡片下方「忘记密码？」入口 */
.forgot-pw {
  align-self: center;
  font-size: 0.85rem;
  color: var(--muted);
  text-decoration: none;
  padding: 4px 8px;
  min-height: 30px;
}

.forgot-pw:hover {
  color: var(--primary);
}

/* ---------------- main layout ---------------- */

.main-view {
  height: var(--vvh, 100vh);
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
}

/* 手机浏览器地址栏伸缩时使用动态视口高度，避免底部被遮挡。
   --vvh 由 app.js 根据 visualViewport 实时设置：软键盘弹出时视口被压缩，
   把高度收进可见区域，输入框才不会跑到键盘下面。 */
@supports (height: 100dvh) {
  .main-view {
    height: var(--vvh, 100dvh);
  }
}

.sidebar {
  background: var(--sidebar);
  color: var(--text);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(16px + env(safe-area-inset-top)) 16px 16px;
  border-bottom: 1px solid var(--border);
}

.me-info {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.me-info strong {
  font-size: 1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.me-info span {
  font-size: 0.8rem;
}

.ghost {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  border-radius: 8px;
  padding: 6px 12px;
  min-height: 44px;
  cursor: pointer;
}

.ghost:hover {
  background: #f0f8f3;
}

/* 侧栏头部「管理后台 / 改密 / 退出」一组按钮：紧凑排版，避免换行 */
.sidebar-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.sidebar-actions .ghost {
  min-height: 0;
  padding: 5px 9px;
  font-size: 0.8rem;
  white-space: nowrap;
  text-decoration: none;
  display: inline-block;
}

.new-chat {
  margin: 12px 16px;
  background: var(--primary);
  text-align: center;
}

.conversation-list {
  list-style: none;
  margin: 0;
  padding: 0 8px 12px;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

.conversation {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 10px;
  border-radius: 10px;
  cursor: pointer;
}

.conversation:hover {
  background: #f2f9f5;
}

.conversation.active {
  background: var(--primary-soft);
}

.conv-body {
  flex: 1;
  min-width: 0;
}

.conv-top,
.conv-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.conv-name {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-time {
  font-size: 0.72rem;
  color: var(--sidebar-muted);
  flex-shrink: 0;
}

.conv-preview {
  font-size: 0.8rem;
  color: var(--sidebar-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.badge {
  background: var(--primary);
  color: white;
  font-size: 0.7rem;
  border-radius: 999px;
  padding: 1px 8px;
  flex-shrink: 0;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #c9d6cf;
  flex-shrink: 0;
}

.dot.online {
  background: #22c55e;
}

.empty {
  padding: 18px 10px;
  color: var(--sidebar-muted);
  font-size: 0.85rem;
  text-align: center;
}

/* ---------------- chat ---------------- */

.chat {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--panel);
}

.chat-header {
  padding: calc(15px + env(safe-area-inset-top)) 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  min-height: 60px;
}

.chat-header-content {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
}

/* 返回按钮仅手机端显示 */
.back-btn {
  display: none;
  border: none;
  background: transparent;
  font-size: 1.5rem;
  line-height: 1;
  color: var(--text);
  padding: 8px 12px 8px 4px;
  min-width: 44px;
  min-height: 44px;
  cursor: pointer;
}

.chat-title {
  font-weight: 700;
  font-size: 1.05rem;
  margin-right: 10px;
}

.chat-status {
  font-size: 0.8rem;
  color: var(--muted);
}

.chat-status.online {
  color: var(--success);
}

.placeholder {
  color: var(--muted);
}

.message-list {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.msg {
  max-width: 68%;
  padding: 9px 13px;
  border-radius: 14px;
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
  word-break: break-word;
}

.msg.me {
  align-self: flex-end;
  background: var(--bubble-me);
  border-bottom-right-radius: 4px;
}

.msg.them {
  align-self: flex-start;
  background: var(--bubble-them);
  border-bottom-left-radius: 4px;
}

.msg-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-size: 0.72rem;
  color: var(--muted);
  margin-bottom: 3px;
}

.msg-who {
  font-weight: 600;
}

.msg-text {
  white-space: pre-wrap;
  line-height: 1.5;
}

.composer {
  border-top: 1px solid var(--border);
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  display: flex;
  gap: 10px;
  align-items: flex-end;
}

.composer textarea {
  flex: 1;
  resize: vertical;
  min-height: 44px;
  max-height: 160px;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
}

.composer button {
  min-width: 88px;
  align-self: flex-end;
}

/* ---------------- user picker overlay ---------------- */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}

.overlay-card {
  width: min(92vw, 420px);
  max-height: var(--overlay-h, 80vh);
  background: var(--panel);
  border-radius: 16px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.overlay-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.overlay-head h3 {
  margin: 0;
}

.overlay-card input {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
}

/* 弹窗里的提示信息（忘记密码 / 修改密码） */
.modal-msg {
  min-height: 20px;
  font-size: 0.9rem;
}

.modal-msg.error {
  color: var(--danger);
}

.modal-msg.success {
  color: var(--success);
}

.user-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  min-height: 60px;
}

/* 网页内消息通知横幅（手机浏览器 / 无原生通知时的兜底） */
.toast-banner {
  position: fixed;
  top: calc(12px + env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 60;
  max-width: min(92vw, 420px);
  background: rgba(32, 53, 43, 0.96);
  color: #fff;
  border-radius: 12px;
  padding: 10px 16px;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.28);
  font-size: 0.9rem;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
  animation: toast-in 0.25s ease-out;
}

.toast-banner .toast-title {
  font-weight: 600;
}

.toast-banner .toast-body {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

@keyframes toast-in {
  from {
    transform: translate(-50%, -10px);
    opacity: 0;
  }
  to {
    transform: translate(-50%, 0);
    opacity: 1;
  }
}

.user-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 8px;
  border-radius: 10px;
  cursor: pointer;
}

.user-list li:hover {
  background: var(--bg);
}

/* ---------------- responsive ---------------- */

/* ---------------- mobile: single-panel toggle ---------------- */
/* 手机上一次只显示一个面板：默认会话列表全屏，点进对话进入聊天全屏，
   聊天页顶部「←」返回会话列表。断点取 1024 以便横屏 iPhone（宽达 ~932px）
   也能拿到单栏布局，而不退回桌面双栏。 */
@media (max-width: 1024px) {
  .main-view {
    display: block;
    position: relative;
  }

  .sidebar,
  .chat {
    width: 100%;
    height: 100%;
  }

  .main-view.show-chat .sidebar {
    display: none;
  }

  .main-view:not(.show-chat) .chat {
    display: none;
  }

  .back-btn {
    display: inline-block;
  }

  .msg {
    max-width: 85%;
  }

  /* 16px 字号可避免 iOS 聚焦输入框时自动放大页面 */
  .composer textarea {
    font-size: 16px;
  }

  .chat-header {
    padding: calc(12px + env(safe-area-inset-top)) 14px 12px;
    min-height: 52px;
  }

  .message-list {
    padding: 14px;
  }
}
