/* ChatApp — 邀请制小型聊天 */

:root {
  --bg: #ededed;
  --bg-side: #f7f7f7;
  --bg-card: #ffffff;
  --bg-hover: #ececec;
  --bg-active: #e2e2e2;
  --line: #e3e3e3;
  --text: #111111;
  --text-2: #5e5e5e;
  --text-3: #999999;
  --green: #07c160;
  --green-dark: #06a854;
  --bubble-other: #ffffff;
  --bubble-mine: #95ec69;
  --bubble-mine-text: #111111;
  --danger: #e64340;
  --shadow: 0 2px 12px rgba(0, 0, 0, 0.07);
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111111;
    --bg-side: #191919;
    --bg-card: #202020;
    --bg-hover: #262626;
    --bg-active: #2d2d2d;
    --line: #2a2a2a;
    --text: #e9e9e9;
    --text-2: #a8a8a8;
    --text-3: #6f6f6f;
    --bubble-other: #2a2a2a;
    --bubble-mine: #2f6b3a;
    --bubble-mine-text: #e9f5ec;
    --shadow: 0 2px 14px rgba(0, 0, 0, 0.45);
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

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

button { -webkit-tap-highlight-color: transparent; }

.hidden { display: none !important; }

.noscript {
  padding: 24px;
  text-align: center;
  color: var(--danger);
}

/* ------------------------------------------------------------------ 登录 */

.auth-view {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(circle at 15% 10%, rgba(7, 193, 96, 0.14), transparent 42%),
    radial-gradient(circle at 90% 90%, rgba(7, 193, 96, 0.08), transparent 40%),
    var(--bg);
}

.auth-card {
  width: min(410px, 100%);
  background: var(--bg-card);
  border-radius: 18px;
  padding: 28px 26px 20px;
  box-shadow: var(--shadow);
}

.brand {
  display: flex;
  align-items: center;
  gap: 13px;
  margin-bottom: 22px;
}

.brand-mark {
  width: 54px;
  height: 54px;
  border-radius: 15px;
  display: grid;
  place-items: center;
  background: linear-gradient(145deg, #2bd97f, #07c160);
  color: #ffffff;
  font-size: 25px;
  font-weight: 700;
  flex: none;
}

.brand-text h1 {
  margin: 0;
  font-size: 22px;
  letter-spacing: 0.01em;
}

.brand-text p {
  margin: 4px 0 0;
  font-size: 12px;
  color: var(--text-3);
}

.auth-tabs {
  display: flex;
  gap: 2px;
  background: var(--bg-side);
  border-radius: 10px;
  padding: 3px;
  margin-bottom: 18px;
}

.auth-tab {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--text-2);
  padding: 9px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
}

.auth-tab.active {
  background: var(--bg-card);
  color: var(--text);
  box-shadow: var(--shadow);
  font-weight: 600;
}

.auth-form { display: block; }

.field { display: block; margin-bottom: 13px; }

.field > span {
  display: block;
  margin-bottom: 5px;
  font-size: 12px;
  color: var(--text-2);
}

.field input {
  display: block;
  width: 100%;
  height: 41px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--bg-side);
  color: var(--text);
  outline: none;
}

.field input:focus { border-color: var(--green); }

.field small {
  display: block;
  margin-top: 4px;
  font-size: 11px;
  color: var(--text-3);
}

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

.btn {
  border: 0;
  border-radius: 9px;
  padding: 9px 15px;
  background: var(--bg-hover);
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s ease;
}

.btn:hover { background: var(--bg-active); }

.btn.primary {
  background: var(--green);
  color: #ffffff;
  font-weight: 600;
}

.btn.primary:hover { background: var(--green-dark); }

.btn.danger { color: var(--danger); }

.btn.wide {
  width: 100%;
  height: 43px;
}

.btn.small {
  padding: 7px 11px;
  font-size: 12px;
  flex: none;
}

.btn:disabled {
  opacity: 0.55;
  cursor: default;
}

.auth-note {
  margin: 16px 0 0;
  text-align: center;
  font-size: 11px;
  color: var(--text-3);
}

.auth-note a {
  color: var(--green);
  text-decoration: none;
}

.auth-note a:hover { text-decoration: underline; }

/* ------------------------------------------------------------------ 主布局 */

.app-view {
  height: 100vh;
  height: 100dvh;
  display: flex;
  overflow: hidden;
}

.sidebar {
  width: 320px;
  flex: none;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--bg-side);
  border-right: 1px solid var(--line);
}

.sidebar-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 14px 12px 10px;
}

.me-card {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  text-align: left;
  cursor: pointer;
}

.me-card:hover { background: var(--bg-hover); }

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  flex: none;
  background: #6d8ac9;
  color: #ffffff;
  font-size: 16px;
  font-weight: 600;
  user-select: none;
}

.avatar.small {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  font-size: 13px;
}

.me-text {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.me-text strong {
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.me-text small {
  font-size: 11px;
  color: var(--text-3);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.icon-btn {
  width: 32px;
  height: 32px;
  flex: none;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text-2);
  cursor: pointer;
  font-size: 17px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.icon-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.text-btn {
  flex: none;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text-2);
  font-size: 12px;
  padding: 7px 9px;
  cursor: pointer;
}

.text-btn:hover {
  background: var(--bg-hover);
  color: var(--danger);
}

.side-tabs {
  display: flex;
  gap: 4px;
  padding: 0 12px 8px;
}

.side-tab {
  flex: 1;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text-2);
  padding: 8px;
  font-size: 13px;
  cursor: pointer;
}

.side-tab.active {
  background: var(--bg-card);
  color: var(--text);
  box-shadow: var(--shadow);
  font-weight: 600;
}

.side-panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.search-box { padding: 0 12px 8px; }

.search-box.with-button {
  display: flex;
  gap: 6px;
}

.search-box input {
  width: 100%;
  height: 34px;
  padding: 0 11px;
  border: 0;
  border-radius: 8px;
  background: var(--bg-card);
  color: var(--text);
  outline: none;
  font-size: 13px;
}

.search-box.with-button input { flex: 1; min-width: 0; }

.conversation-list,
.contact-list,
.search-results {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 0 8px 8px;
}

.search-results { flex: none; max-height: 45%; }

.conv-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: 10px;
  cursor: pointer;
}

.conv-item:hover,
.conv-item.active { background: var(--bg-hover); }

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

.conv-top {
  display: flex;
  align-items: center;
  gap: 6px;
}

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

.conv-time {
  margin-left: auto;
  flex: none;
  font-size: 10px;
  color: var(--text-3);
}

.conv-preview {
  margin-top: 2px;
  font-size: 12px;
  color: var(--text-3);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.unread {
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--green);
  color: #ffffff;
  font-size: 10px;
  display: grid;
  place-items: center;
  flex: none;
}

.conversation-list:empty,
.contact-list:empty {
  flex: 0 0 auto;
}

.side-empty {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px 20px;
  text-align: center;
  color: var(--text-3);
  font-size: 13px;
  line-height: 1.8;
}

.side-empty span { font-size: 11px; }

.sidebar-foot {
  flex: none;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 9px 14px calc(9px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  font-size: 11px;
  color: var(--text-3);
}

.conn-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-3);
  flex: none;
}

.conn-dot.online { background: var(--green); }

.foot-spacer { flex: 1; }

/* ------------------------------------------------------------------ 联系人 */

.section-title {
  padding: 10px 10px 5px;
  font-size: 11px;
  color: var(--text-3);
}

.contact-item,
.pending-item,
.search-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: 9px;
}

.contact-item:hover,
.search-item:hover { background: var(--bg-hover); }

.contact-main {
  flex: 1;
  min-width: 0;
}

.contact-name {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.contact-sub {
  margin-top: 1px;
  font-size: 11px;
  color: var(--text-3);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.online-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-3);
  flex: none;
}

.online-dot.online { background: var(--green); }

.contact-actions {
  display: flex;
  gap: 5px;
  flex: none;
}

.mini-btn {
  border: 0;
  border-radius: 7px;
  background: var(--bg-card);
  color: var(--text-2);
  padding: 5px 9px;
  font-size: 11px;
  cursor: pointer;
}

.mini-btn:hover { background: var(--bg-hover); }

.mini-btn.primary {
  background: var(--green);
  color: #ffffff;
}

.mini-btn.danger { color: var(--danger); }

/* ------------------------------------------------------------------ 聊天区 */

.chat-pane {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.chat-head {
  flex: none;
  height: 58px;
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}

.back-btn { display: none; }

.peer-text {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.peer-text strong {
  font-size: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.peer-text small {
  font-size: 11px;
  color: var(--text-3);
}

.typing-indicator {
  margin-left: auto;
  font-size: 11px;
  color: var(--green);
  white-space: nowrap;
}

.message-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 16px 18px 10px;
  display: flex;
  flex-direction: column;
}

.message-list {
  list-style: none;
  margin: auto 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
}

.load-more {
  align-self: center;
  margin-bottom: 10px;
  border: 0;
  border-radius: 8px;
  background: var(--bg-card);
  color: var(--text-2);
  padding: 6px 12px;
  font-size: 12px;
  cursor: pointer;
  box-shadow: var(--shadow);
}

.chat-empty {
  margin: auto;
  padding: 30px;
  text-align: center;
  color: var(--text-3);
}

.chat-empty-mark { font-size: 40px; }

.chat-empty p {
  margin: 9px 0 4px;
  color: var(--text-2);
  font-size: 14px;
}

.chat-empty span { font-size: 11px; }

.day-divider {
  align-self: center;
  margin: 12px 0 8px;
  font-size: 10px;
  color: var(--text-3);
  user-select: none;
}

.msg-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 3px 0;
  max-width: 100%;
  animation: msg-in 0.15s ease;
}

@keyframes msg-in {
  from { opacity: 0; transform: translateY(2px); }
  to { opacity: 1; transform: translateY(0); }
}

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

.msg-body {
  min-width: 0;
  max-width: min(70%, 560px);
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

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

.msg-sender {
  margin: 0 3px 3px;
  font-size: 10px;
  color: var(--text-3);
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.msg-bubble {
  padding: 8px 11px;
  border-radius: 10px;
  background: var(--bubble-other);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.03);
}

.msg-row.mine .msg-bubble {
  background: var(--bubble-mine);
  color: var(--bubble-mine-text);
}

.msg-time {
  margin-top: 3px;
  font-size: 10px;
  color: var(--text-3);
}

.msg-row.pending .msg-bubble { opacity: 0.6; }

.msg-row.failed .msg-bubble {
  outline: 1px solid var(--danger);
  opacity: 0.8;
}

.msg-failed-note {
  margin-top: 3px;
  font-size: 10px;
  color: var(--danger);
  cursor: pointer;
}

/* 多媒体消息 */

.msg-bubble.media {
  padding: 3px;
  background: var(--bubble-other);
}

.msg-row.mine .msg-bubble.media {
  background: var(--bubble-mine);
}

/* 图片/视频直接展示，不要绿色气泡边框 */
.msg-row .msg-bubble.media-image,
.msg-row .msg-bubble.media-video,
.msg-row.mine .msg-bubble.media-image,
.msg-row.mine .msg-bubble.media-video {
  background: transparent;
  padding: 0;
  box-shadow: none;
}

.msg-row .msg-bubble.media-image::before,
.msg-row .msg-bubble.media-video::before {
  display: none;
}

.msg-image {
  display: block;
  max-width: min(260px, 62vw);
  max-height: 320px;
  border-radius: 9px;
  cursor: zoom-in;
}

.msg-video {
  display: block;
  width: min(320px, 68vw);
  max-height: 360px;
  border-radius: 9px;
  background: #000000;
}

.msg-media-download {
  display: block;
  margin: 4px 3px 2px;
  font-size: 10px;
  color: var(--text-3);
  text-decoration: none;
}

.msg-media-download:hover { text-decoration: underline; }

.msg-audio-wrap {
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 3px 5px 4px;
}

.msg-audio-label {
  font-size: 11px;
  color: var(--text-2);
}

.msg-row.mine .msg-audio-label {
  color: var(--bubble-mine-text);
  opacity: 0.78;
}

.msg-audio {
  width: min(240px, 62vw);
  height: 34px;
}

.modal.lightbox {
  width: min(92vw, 900px);
  padding: 8px;
  background: #000000;
}

.modal.lightbox img {
  display: block;
  width: 100%;
  height: auto;
  max-height: 86vh;
  object-fit: contain;
  border-radius: 6px;
}

.composer {
  flex: none;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
  padding: 8px 14px calc(9px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--bg);
}

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  min-width: 0;
}

.composer-tools {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0 2px;
}

.tool-btn {
  border: 0;
  border-radius: 8px;
  padding: 6px 11px;
  background: var(--bg-card);
  color: var(--text-2);
  font-size: 12px;
  cursor: pointer;
  box-shadow: var(--shadow);
}

.tool-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.tool-btn.recording {
  background: var(--danger);
  color: #ffffff;
}

.record-status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-left: 2px;
  font-size: 11px;
  color: var(--danger);
}

.rec-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--danger);
  animation: rec-pulse 1s ease-in-out infinite;
}

@keyframes rec-pulse {
  50% { opacity: 0.3; }
}

.composer textarea {
  flex: 1;
  min-width: 0;
  min-height: 40px;
  max-height: 120px;
  resize: none;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg-card);
  color: var(--text);
  outline: none;
  font-size: 14px;
  line-height: 1.45;
}

.composer textarea:focus { border-color: var(--green); }

.composer .btn {
  height: 40px;
  flex: none;
}

/* ------------------------------------------------------------------ 弹窗 */

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(0, 0, 0, 0.45);
}

.modal {
  width: min(380px, 100%);
  max-height: calc(100dvh - 40px);
  overflow-y: auto;
  padding: 21px;
  border-radius: 16px;
  background: var(--bg-card);
  box-shadow: var(--shadow);
}

.modal h3 {
  margin: 0 0 14px;
  font-size: 16px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 16px;
}

.security-row {
  margin: 2px 0 6px;
}

.security-status {
  margin: 0 0 6px;
  font-weight: 600;
  color: var(--text-2);
}

.security-status.on { color: var(--green); }

.security-hint {
  margin: 0 0 13px;
  color: var(--text-3);
  font-size: 12px;
  line-height: 1.55;
}

.totp-qr {
  display: block;
  width: 200px;
  max-width: 100%;
  margin: 2px auto 16px;
  padding: 9px;
  border-radius: 12px;
  background: #ffffff;
}

.secret-box {
  display: block;
  padding: 9px 11px;
  border-radius: 8px;
  background: var(--bg-side);
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  line-height: 1.5;
  letter-spacing: 0.5px;
  word-break: break-all;
  user-select: all;
}

.swatches {
  display: flex;
  flex-wrap: wrap;
  gap: 9px;
  margin-top: 7px;
}

.swatch {
  width: 28px;
  height: 28px;
  border: 2px solid transparent;
  border-radius: 50%;
  cursor: pointer;
  padding: 0;
}

.swatch.active {
  border-color: var(--text);
  box-shadow: 0 0 0 2px var(--bg-card) inset;
}

/* ------------------------------------------------------------------ 提示 */

.toasts {
  position: fixed;
  left: 50%;
  bottom: calc(84px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 60;
  display: flex;
  flex-direction: column;
  gap: 6px;
  pointer-events: none;
}

.toast {
  max-width: min(80vw, 420px);
  padding: 8px 14px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.82);
  color: #ffffff;
  font-size: 12px;
  line-height: 1.4;
  text-align: center;
  animation: toast-in 0.18s ease;
}

.toast.error { background: rgba(230, 67, 64, 0.94); }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ------------------------------------------------------------------ 手机 */

@media (max-width: 760px) {
  .sidebar {
    width: 100%;
    border-right: 0;
  }

  .chat-pane { display: none; }

  body.in-chat .sidebar { display: none; }
  body.in-chat .chat-pane { display: flex; }

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

  .message-scroll { padding: 10px 10px 8px; }

  .msg-body { max-width: 78%; }

  .composer { padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); }

  .chat-head { padding: 0 10px; }

  .search-results { max-height: 38%; }
}

@media (max-width: 420px) {
  .msg-body { max-width: 82%; }
  .auth-card { padding: 22px 18px 16px; }
}
