:root {
  --bg: #f3f6fb;
  --bg-2: #eef2f9;
  --ink: #1e293b;
  --muted: #7c8798;
  --dark: #0f172a;
  --dark-2: #1b2436;
  --accent: #4f46e5;
  --accent-2: #4338ca;
  --accent-soft: #eef2ff;
  --card: #ffffff;
  --border: #e3e8f2;
  --danger: #e11d48;
  --dot: #fb7185;
  --radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-size: 15px;
}

button, input, textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

.hidden { display: none !important; }

.view {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--app-height, 100dvh);
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

@media (min-width: 720px) {
  .view {
    width: 100%;
    max-width: 520px;
    left: 50%;
    right: auto;
    transform: translateX(-50%);
    box-shadow: 0 0 60px rgba(15, 23, 42, .12);
  }
}

.icon {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: 0 0 auto;
}

.icon-btn {
  width: 42px;
  height: 42px;
  border: 0;
  border-radius: 13px;
  background: transparent;
  color: inherit;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
}

.icon-btn:active { background: rgba(100, 116, 139, .14); }

/* ---------------- 登录 ---------------- */
.login-wrap {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 26px;
  background:
    radial-gradient(900px 420px at 50% -10%, #dfe5ff 0%, transparent 60%),
    radial-gradient(700px 380px at 100% 100%, #e6f4ff 0%, transparent 55%),
    var(--bg);
}

.brand {
  text-align: center;
  margin-bottom: 24px;
}

.logo {
  width: 76px;
  height: 76px;
  margin: 0 auto 14px;
  border-radius: 24px;
  background: linear-gradient(150deg, #6366f1, #4338ca);
  box-shadow: 0 16px 34px rgba(79, 70, 229, .32);
  position: relative;
}

.logo span {
  position: absolute;
  left: 20px;
  top: 23px;
  width: 36px;
  height: 25px;
  background: #fff;
  border-radius: 9px;
}

.logo span::after {
  content: "";
  position: absolute;
  left: 9px;
  top: 23px;
  width: 13px;
  height: 13px;
  background: #fff;
  transform: rotate(45deg);
  border-radius: 3px;
}

.brand h1 {
  margin: 0;
  font-size: 23px;
  letter-spacing: .2px;
}

.brand p {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.login-card {
  background: var(--card);
  border-radius: 24px;
  padding: 22px;
  box-shadow: 0 20px 50px rgba(15, 23, 42, .08);
}

.login-card label {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 8px;
}

.login-card input {
  width: 100%;
  height: 50px;
  border: 1px solid var(--border);
  border-radius: 15px;
  background: #f8fafc;
  padding: 0 15px;
  font-size: 16px;
  outline: none;
}

.login-card input:focus {
  border-color: var(--accent);
  background: #fff;
  box-shadow: 0 0 0 3px rgba(79, 70, 229, .12);
}

.primary {
  width: 100%;
  height: 50px;
  margin-top: 14px;
  border: 0;
  border-radius: 15px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  font-size: 16px;
}

.primary:active { background: var(--accent-2); }
.primary:disabled { opacity: .55; }

.error {
  min-height: 18px;
  margin: 10px 0 0;
  color: var(--danger);
  font-size: 13px;
}

.hint {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

/* ---------------- 会话列表 ---------------- */
.app-header {
  background: var(--dark);
  color: #fff;
  padding: calc(14px + var(--safe-top)) 14px 12px;
  flex: 0 0 auto;
}

.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.header-title h1 {
  margin: 0;
  font-size: 23px;
  font-weight: 700;
}

.header-title p {
  margin: 4px 0 0;
  color: #94a3b8;
  font-size: 12px;
  max-width: 290px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 2px;
}

.search-wrap { margin-top: 10px; }

.search-wrap input {
  width: 100%;
  height: 42px;
  border: 0;
  border-radius: 13px;
  background: var(--dark-2);
  color: #e5e7eb;
  padding: 0 14px;
  outline: none;
  font-size: 14px;
}

.search-wrap input::placeholder { color: #64748b; }

.conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px 10px calc(20px + var(--safe-bottom));
  -webkit-overflow-scrolling: touch;
}

.section-label {
  padding: 14px 10px 6px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}

.conv-item {
  width: 100%;
  border: 0;
  background: transparent;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px;
  border-radius: 17px;
  text-align: left;
}

.conv-item:active { background: #e9eef8; }

.avatar {
  width: 47px;
  height: 47px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: #fff;
  font-weight: 700;
  font-size: 18px;
  flex: 0 0 auto;
  position: relative;
  user-select: none;
}

.avatar.small {
  width: 34px;
  height: 34px;
  font-size: 13px;
}

.avatar .dot {
  position: absolute;
  top: -1px;
  right: -1px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--dot);
  border: 2px solid var(--bg);
}

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

.conv-name {
  display: block;
  font-size: 16px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv-meta {
  display: block;
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
}

/* ---------------- 聊天 ---------------- */
.chat-header {
  background: #fff;
  border-bottom: 1px solid var(--border);
  padding: calc(8px + var(--safe-top)) 8px 8px;
  display: flex;
  align-items: center;
  gap: 2px;
  flex: 0 0 auto;
}

.chat-title {
  flex: 1;
  min-width: 0;
  padding: 0 6px;
}

.chat-title h1 {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-title p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 11px;
  height: 13px;
  overflow: hidden;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  -webkit-overflow-scrolling: touch;
}

.empty {
  margin: auto;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
  padding: 30px;
}

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

.msg-row.mine {
  flex-direction: row-reverse;
}

.msg-content {
  max-width: calc(100% - 48px);
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.mine .msg-content { align-items: flex-end; }

.msg-name {
  font-size: 11px;
  color: var(--muted);
  margin: 0 5px 4px;
}

.mine .msg-name { text-align: right; }

.bubble {
  display: inline-block;
  max-width: 100%;
  padding: 10px 13px;
  border-radius: 18px 18px 18px 6px;
  background: var(--card);
  border: 1px solid var(--border);
  font-size: 15px;
  line-height: 1.48;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
  box-shadow: 0 1px 2px rgba(15, 23, 42, .03);
}

.mine .bubble {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  border-radius: 18px 18px 6px 18px;
}

.msg-time {
  margin: 4px 5px 0;
  font-size: 10px;
  color: var(--muted);
}

.sys-time {
  align-self: center;
  background: #e7edf7;
  color: var(--muted);
  font-size: 11px;
  padding: 4px 11px;
  border-radius: 999px;
}

.media-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 9px;
  max-width: 100%;
  box-shadow: 0 1px 2px rgba(15, 23, 42, .03);
}

.media-card img {
  display: block;
  max-width: 100%;
  max-height: 320px;
  min-width: 140px;
  min-height: 90px;
  border-radius: 11px;
  object-fit: contain;
  background: #f1f5f9;
  cursor: zoom-in;
}

.media-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  margin-top: 9px;
  font-size: 12px;
}

.media-actions a {
  color: var(--accent);
  text-decoration: none;
}

.file-card {
  display: flex;
  align-items: center;
  gap: 11px;
  min-width: 210px;
}

.file-icon {
  width: 42px;
  height: 48px;
  border-radius: 11px;
  background: var(--accent-soft);
  color: var(--accent);
  display: grid;
  place-items: center;
  font-size: 11px;
  font-weight: 700;
  flex: 0 0 auto;
}

.file-info { min-width: 0; }
.file-name {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 210px;
}
.file-size { margin-top: 3px; color: var(--muted); font-size: 11px; }

.composer {
  background: #fff;
  border-top: 1px solid var(--border);
  padding: 8px 10px calc(8px + var(--safe-bottom));
  display: flex;
  align-items: flex-end;
  gap: 8px;
  flex: 0 0 auto;
}

.composer textarea {
  flex: 1;
  min-height: 42px;
  max-height: 120px;
  border: 1px solid var(--border);
  border-radius: 17px;
  background: #f6f8fc;
  padding: 10px 14px;
  font-size: 15px;
  line-height: 1.4;
  resize: none;
  outline: none;
}

.composer textarea:focus {
  background: #fff;
  border-color: #c7d2fe;
}

.attach {
  background: #f1f5f9;
  border-radius: 14px;
}

.send-btn {
  height: 42px;
  border: 0;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  padding: 0 17px;
  flex: 0 0 auto;
}

.send-btn:disabled { opacity: .5; }

/* ---------------- 底部弹层 ---------------- */
.sheet {
  position: fixed;
  inset: 0;
  z-index: 50;
}

.sheet-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, .36);
  backdrop-filter: blur(2px);
}

.sheet-panel {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 82vh;
  background: #fff;
  border-radius: 24px 24px 0 0;
  display: flex;
  flex-direction: column;
  padding-bottom: var(--safe-bottom);
  box-shadow: 0 -10px 40px rgba(15, 23, 42, .16);
  animation: slideUp .18s ease-out;
}

@keyframes slideUp {
  from { transform: translateY(20px); opacity: .4; }
  to { transform: translateY(0); opacity: 1; }
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 14px 8px;
  flex: 0 0 auto;
}

.sheet-head h2 {
  margin: 0;
  font-size: 18px;
}

.sheet-body {
  overflow-y: auto;
  padding: 0 14px 22px;
}

.field {
  margin: 8px 0 12px;
}

.field label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 6px;
}

.field input {
  width: 100%;
  height: 44px;
  border: 1px solid var(--border);
  border-radius: 13px;
  background: #f8fafc;
  padding: 0 13px;
  outline: none;
  font-size: 15px;
}

.field input:focus { border-color: var(--accent); background: #fff; }

.row-btn {
  width: 100%;
  height: 46px;
  border: 0;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  margin-top: 6px;
}

.row-btn.secondary {
  background: #f1f5f9;
  color: var(--ink);
}

.sheet-actions {
  display: flex;
  gap: 8px;
  margin: 8px 0 12px;
}

.sheet-actions .row-btn {
  margin-top: 0;
}

.account-card {
  background: linear-gradient(135deg, #eef2ff, #f8fafc);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 14px;
  margin: 8px 0 14px;
}

.account-name {
  font-size: 18px;
  font-weight: 600;
}

.account-sub {
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.code-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.code-row code {
  flex: 1;
  min-width: 0;
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 11px 12px;
  font-family: Consolas, "SFMono-Regular", monospace;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.code-box {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 11px 12px;
  font-family: Consolas, "SFMono-Regular", monospace;
  font-size: 12px;
  line-height: 1.5;
  word-break: break-all;
  max-height: 120px;
  overflow-y: auto;
  margin-bottom: 8px;
}

.member-row, .result-row, .friend-row {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 10px 2px;
  border-bottom: 1px solid #f0f3f9;
}

.member-main, .result-main {
  flex: 1;
  min-width: 0;
}

.member-name, .result-name {
  font-size: 15px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.member-meta, .result-meta {
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.member-role {
  font-size: 11px;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 999px;
  padding: 3px 8px;
  flex: 0 0 auto;
}

.mini-btn {
  border: 0;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 12px;
  font-weight: 600;
  padding: 8px 12px;
  border-radius: 10px;
  flex: 0 0 auto;
}

.mini-btn:disabled {
  background: #f1f5f9;
  color: var(--muted);
}

.sheet-note {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.55;
  margin: 8px 2px;
}

.friend-row input[type="checkbox"] {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
}

/* ---------------- Toast ---------------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(86px + var(--safe-bottom));
  transform: translateX(-50%);
  background: rgba(15, 23, 42, .92);
  color: #fff;
  font-size: 13px;
  padding: 9px 15px;
  border-radius: 999px;
  z-index: 80;
  max-width: 80vw;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---------------- 加载 ---------------- */
.spinner {
  width: 26px;
  height: 26px;
  border: 3px solid #e2e8f0;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
  margin: 34px auto;
}

@keyframes spin { to { transform: rotate(360deg); } }

.loading-text {
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  padding: 26px;
}

/* ---------------- 搜索聊天记录 ---------------- */
.search-result {
  padding: 10px 2px;
  border-bottom: 1px solid #f0f3f9;
  cursor: pointer;
}

.search-result:active { background: #f8fafc; }

.search-result-time {
  font-size: 11px;
  color: var(--muted);
}

.search-result-text {
  margin-top: 3px;
  font-size: 14px;
  line-height: 1.45;
  word-break: break-word;
}

.msg-row.highlight .bubble,
.msg-row.highlight .media-card {
  box-shadow: 0 0 0 3px rgba(79, 70, 229, .35);
}

.member-online {
  font-size: 11px;
  color: #10b981;
  margin-left: 6px;
}

.member-online.off { color: #94a3b8; }

/* ---------------- 图片灯箱 ---------------- */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 90;
  background: rgba(8, 12, 22, .96);
  display: flex;
  flex-direction: column;
}

.lightbox-backdrop {
  position: absolute;
  inset: 0;
}

.lightbox-top {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: calc(10px + var(--safe-top)) 14px 10px;
}

.lightbox-name {
  color: #94a3b8;
  font-size: 12px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lightbox-close { color: #e2e8f0; }

.lightbox-img {
  position: relative;
  z-index: 1;
  flex: 1;
  min-height: 0;
  width: 100%;
  object-fit: contain;
  padding: 0 8px;
  box-sizing: border-box;
}

.lightbox-actions {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 22px;
  padding: 12px 14px calc(16px + var(--safe-bottom));
}

.lightbox-actions a,
.lightbox-actions button {
  color: #c7d2fe;
  background: transparent;
  border: 0;
  font-size: 14px;
  text-decoration: none;
  padding: 8px 4px;
}

/* ---------------- 电脑网页模式：双栏 ---------------- */
@media (min-width: 900px) {
  body.desktop.authed #list-view {
    display: flex !important;
    left: 0;
    right: auto;
    width: 360px;
    max-width: none;
    transform: none;
    border-right: 1px solid var(--border);
  }

  body.desktop.authed #chat-view {
    display: flex !important;
    left: 360px;
    right: 0;
    width: auto;
    max-width: none;
    transform: none;
  }

  body.desktop.authed #login-view {
    display: none !important;
  }

  body.desktop #back-btn { display: none; }

  body.desktop:not(.has-chat) .composer { display: none; }

  body.desktop:not(.has-chat) #members-btn,
  body.desktop:not(.has-chat) #more-btn,
  body.desktop:not(.has-chat) #chat-search-btn {
    visibility: hidden;
  }

  body.desktop .chat-header { padding-left: 18px; }
  body.desktop .messages { padding-bottom: 24px; }

  body.desktop .sheet-panel {
    left: 50%;
    right: auto;
    width: 520px;
    max-width: calc(100vw - 40px);
    transform: translateX(-50%);
    border-radius: 22px 22px 0 0;
  }
}

