:root {
  --bg: #0f0d17;
  --bg-soft: #171425;
  --bg-elev: #1f1b30;
  --line: #2b2540;
  --text: #ece9f6;
  --muted: #9a93b3;
  --accent: #7c5cff;
  --accent-2: #a78bfa;
  --mine: #5b45d6;
  --danger: #ff5d6c;
  --radius: 16px;
  --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 {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 15px;
  overscroll-behavior: none;
}

body {
  background-image: radial-gradient(1200px 600px at 50% -10%, rgba(124, 92, 255, 0.22), transparent 60%);
  background-repeat: no-repeat;
}

.hidden { display: none !important; }
.spacer { height: 10px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

button { font: inherit; color: inherit; }

.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---------- 登录 ---------- */
.auth-screen {
  align-items: center;
  justify-content: center;
  padding: 24px;
  padding-top: calc(24px + var(--safe-top));
}

.auth-card {
  width: 100%;
  max-width: 380px;
  background: rgba(31, 27, 48, 0.82);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 26px 22px 20px;
  backdrop-filter: blur(12px);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
  text-align: center;
}

.auth-logo { width: 68px; height: 68px; border-radius: 18px; }
.auth-card h1 { font-size: 22px; margin: 10px 0 4px; letter-spacing: 2px; }
.auth-card > p { margin: 0 0 18px; font-size: 12.5px; }

.tabs {
  display: flex;
  background: var(--bg-soft);
  border-radius: 12px;
  padding: 4px;
  margin-bottom: 16px;
}
.tab {
  flex: 1;
  border: 0;
  background: transparent;
  padding: 9px 0;
  border-radius: 9px;
  color: var(--muted);
  cursor: pointer;
}
.tab.active { background: var(--accent); color: #fff; font-weight: 600; }

.field { display: block; text-align: left; margin-bottom: 12px; }
.field > span { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 6px; }
.field input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  outline: none;
}
.field input:focus { border-color: var(--accent); }

.btn {
  border: 1px solid var(--line);
  background: var(--bg-soft);
  color: var(--text);
  padding: 11px 16px;
  border-radius: 12px;
  cursor: pointer;
  font-size: 15px;
}
.btn.primary { background: linear-gradient(135deg, var(--accent), #5b45d6); border-color: transparent; font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.block { width: 100%; margin-top: 6px; }
.btn.danger { color: var(--danger); border-color: rgba(255, 93, 108, 0.35); }
.btn:disabled { opacity: 0.5; }

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

/* ---------- 顶栏 ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(10px + var(--safe-top)) 12px 10px;
  background: rgba(15, 13, 23, 0.86);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
  z-index: 5;
}
.title-block { flex: 1; min-width: 0; }
.title-block h1 {
  font-size: 17px;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.title-block span { display: block; margin-top: 1px; }

.icon-btn {
  border: 0;
  background: transparent;
  width: 38px;
  height: 38px;
  border-radius: 12px;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  color: var(--text);
  display: grid;
  place-items: center;
  flex: 0 0 auto;
}
#btn-back { font-size: 30px; padding-bottom: 6px; }
.icon-btn:active { background: var(--bg-elev); }
.top-actions { display: flex; gap: 2px; }

/* ---------- 会话列表 ---------- */
#views { flex: 1; min-height: 0; position: relative; }
.view { position: absolute; inset: 0; display: flex; flex-direction: column; min-height: 0; }

.conv-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.conv-item {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 12px 14px;
  cursor: pointer;
  border-bottom: 1px solid rgba(43, 37, 64, 0.6);
}
.conv-item:active { background: var(--bg-soft); }
.avatar {
  width: 46px;
  height: 46px;
  border-radius: 15px;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 17px;
  color: #fff;
  background: linear-gradient(135deg, #7c5cff, #4b36b8);
  flex: 0 0 auto;
}
.conv-main { flex: 1; min-width: 0; }
.conv-row { display: flex; align-items: baseline; gap: 8px; }
.conv-name { font-weight: 600; font-size: 15.5px; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-time { font-size: 11.5px; color: var(--muted); flex: 0 0 auto; }
.conv-preview {
  margin-top: 3px;
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.badge {
  min-width: 19px;
  height: 19px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-size: 11.5px;
  display: grid;
  place-items: center;
  margin-left: 6px;
}

.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  padding-bottom: 10vh;
}

/* ---------- 聊天 ---------- */
.messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px 12px 6px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.day-divider { text-align: center; font-size: 11.5px; color: var(--muted); margin: 8px 0 2px; }

.msg { display: flex; flex-direction: column; max-width: 82%; }
.msg.them { align-self: flex-start; align-items: flex-start; }
.msg.me { align-self: flex-end; align-items: flex-end; }
.msg-sender { font-size: 11.5px; color: var(--muted); margin: 0 6px 3px; }

.bubble {
  padding: 9px 12px;
  border-radius: 16px;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  line-height: 1.5;
  word-break: break-word;
  white-space: pre-wrap;
  font-size: 15px;
}
.msg.me .bubble {
  background: linear-gradient(135deg, #6750e0, #5540c4);
  border-color: transparent;
  border-bottom-right-radius: 6px;
}
.msg.them .bubble { border-bottom-left-radius: 6px; }

.bubble img.msg-image {
  display: block;
  max-width: min(62vw, 320px);
  max-height: 320px;
  border-radius: 10px;
  cursor: zoom-in;
}
.bubble.media { padding: 4px; background: var(--bg-elev); }
.msg.me .bubble.media { background: #5843c8; }

.file-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: inherit;
  min-width: 150px;
}
.file-chip .file-icon {
  width: 36px; height: 36px; border-radius: 10px;
  background: rgba(255, 255, 255, 0.14);
  display: grid; place-items: center; font-size: 17px;
}
.file-meta { min-width: 0; }
.file-name { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 190px; }
.file-size { font-size: 11.5px; opacity: 0.7; }

.msg-time { font-size: 10.5px; color: var(--muted); margin: 3px 6px 0; }
.msg-failed { font-size: 11.5px; color: var(--danger); margin: 3px 6px 0; }

.chat-hint {
  padding: 8px 14px;
  font-size: 12px;
  color: #ffd479;
  background: rgba(255, 212, 121, 0.08);
  border-top: 1px solid rgba(255, 212, 121, 0.2);
}

.composer {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 10px calc(8px + var(--safe-bottom));
  border-top: 1px solid var(--line);
  background: rgba(15, 13, 23, 0.92);
  backdrop-filter: blur(14px);
}
.composer textarea {
  flex: 1;
  resize: none;
  max-height: 120px;
  min-height: 40px;
  padding: 10px 12px;
  border-radius: 20px;
  border: 1px solid var(--line);
  background: var(--bg-soft);
  color: var(--text);
  font-size: 16px;
  line-height: 1.35;
  outline: none;
}
.composer textarea:focus { border-color: rgba(124, 92, 255, 0.6); }

.send-btn {
  width: 40px; height: 40px;
  border-radius: 50%;
  border: 0;
  background: linear-gradient(135deg, var(--accent), #5540c4);
  color: #fff;
  display: grid;
  place-items: center;
  cursor: pointer;
  flex: 0 0 auto;
}
.send-btn:disabled { opacity: 0.45; }

/* ---------- 抽屉 ---------- */
.sheet {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: flex-end;
  z-index: 50;
}
.sheet-panel {
  width: 100%;
  max-height: 82vh;
  background: var(--bg-soft);
  border-radius: 20px 20px 0 0;
  border-top: 1px solid var(--line);
  padding-bottom: var(--safe-bottom);
  animation: slideUp 0.22s ease;
  display: flex;
  flex-direction: column;
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
.sheet-head {
  display: flex;
  align-items: center;
  padding: 14px 14px 8px 18px;
}
.sheet-head h2 { flex: 1; font-size: 16px; margin: 0; }
.sheet-body { padding: 4px 18px 18px; overflow-y: auto; }

.sheet-note { font-size: 12.5px; color: var(--muted); line-height: 1.6; margin: 6px 0 14px; }
.seg { display: flex; gap: 6px; background: var(--bg); border-radius: 12px; padding: 4px; margin-bottom: 14px; }
.seg button {
  flex: 1; border: 0; background: transparent; padding: 8px 0; border-radius: 9px;
  color: var(--muted); cursor: pointer;
}
.seg button.active { background: var(--accent); color: #fff; }

.user-pick {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 0; border-bottom: 1px solid var(--line);
}
.user-pick:last-child { border-bottom: 0; }
.user-pick .grow { flex: 1; }

.device-row { padding: 12px 0; border-bottom: 1px solid var(--line); }
.device-row:last-child { border-bottom: 0; }
.device-name { font-size: 14px; }
.device-fp {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  letter-spacing: 1px;
  color: var(--accent-2);
  margin-top: 4px;
  word-break: break-all;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(84px + var(--safe-bottom));
  transform: translateX(-50%);
  background: rgba(40, 34, 62, 0.96);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 9px 16px;
  border-radius: 20px;
  font-size: 13px;
  z-index: 80;
  max-width: 86vw;
  text-align: center;
}

.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.92);
  display: grid;
  place-items: center;
  z-index: 90;
  padding: 16px;
}
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; }

@media (min-width: 720px) {
  .conv-list, .messages { padding-left: max(12px, calc(50vw - 360px)); padding-right: max(12px, calc(50vw - 360px)); }
  .composer { padding-left: max(10px, calc(50vw - 360px)); padding-right: max(10px, calc(50vw - 360px)); }
}
