/* ============================================================
   双人聊天 PWA — 样式（移动优先）
   版本：与 index.html 的 ?v=N 同步（发版必改）
   ============================================================ */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --primary: #4f46e5;
  --bg: #f3f4f6;
  --bubble-self: #4f46e5;
  --bubble-other: #ffffff;
  --border: #e5e7eb;
  --text: #1f2937;
  --muted: #6b7280;
}

html,
body {
  height: 100%;
  /* 禁止页面滚动：iOS 键盘弹起时无内容可被顶走（配合 .app 钉在可视区） */
  overflow: hidden;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue",
    "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
}

.hidden {
  display: none !important;
}

/* ===== 昵称入口 ===== */
.name-overlay {
  position: fixed;
  /* 钉在 visualViewport 可视区（JS 设置 --app-top/--app-height，键盘弹起时跟随） */
  top: var(--app-top, 0);
  left: 0;
  right: 0;
  height: var(--app-height, 100vh);
  height: var(--app-height, 100dvh);
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: var(--bg);
}

.name-card {
  width: 100%;
  max-width: 360px;
  padding: 28px 24px;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  text-align: center;
}

.name-card h2 {
  font-size: 1.25rem;
  margin-bottom: 6px;
}

.name-hint {
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 16px;
}

.name-card input,
.chat-form input {
  width: 100%;
  padding: 10px 12px;
  font-size: 1rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  outline: none;
}

.name-card input:focus,
.chat-form input:focus {
  border-color: var(--primary);
}

/* 房间号输入与昵称输入之间留间距 */
.name-card input + input {
  margin-top: 8px;
}

.name-card button,
.chat-form button {
  margin-top: 12px;
  padding: 10px 20px;
  font-size: 1rem;
  color: #fff;
  background: var(--primary);
  border: none;
  border-radius: 10px;
  cursor: pointer;
}

.name-card button:active,
.chat-form button:active {
  opacity: 0.85;
}

/* ===== 主界面布局（移动端全高 + iOS 安全区） ===== */
.app {
  /* 钉在 visualViewport 可视区：iOS 键盘弹起时布局视口不收缩、页面被整体上推，
     必须把容器 fixed 并跟随可视区（top=offsetTop, height=height）才能避免 */
  position: fixed;
  top: var(--app-top, 0);
  left: 0;
  right: 0;
  bottom: auto;
  display: flex;
  flex-direction: column;
  height: var(--app-height, 100vh); /* 老浏览器兜底 */
  height: var(--app-height, 100dvh); /* JS 键盘适配：弹起时收缩到可视高度 */
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 14px 10px;
  background: var(--primary);
  color: #fff;
}

.app-title {
  font-size: 1.1rem;
  font-weight: 600;
}

/* 版本徽标：普通小字灰色（防继承渐变色导致看不清） */
.app-version {
  font-size: 0.7rem;
  font-weight: 400;
  margin-left: 8px;
  color: rgba(255, 255, 255, 0.8);
  background: none;
  -webkit-background-clip: initial;
  background-clip: initial;
  -webkit-text-fill-color: rgba(255, 255, 255, 0.8);
  white-space: nowrap;
  user-select: none;
  vertical-align: middle;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.room-label {
  font-size: 0.75rem;
  opacity: 0.9;
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.icon-btn {
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
  font-size: 0.85rem;
  cursor: pointer;
}

/* ===== 状态栏 / 在线 ===== */
.status {
  text-align: center;
  font-size: 0.75rem;
  padding: 4px;
}

.status-connecting {
  color: #b45309;
  background: #fef3c7;
}

.status-online {
  color: #047857;
  background: #d1fae5;
}

.status-offline {
  color: #b91c1c;
  background: #fee2e2;
}

.presence {
  font-size: 0.75rem;
  color: var(--muted);
  padding: 4px 14px 0;
}

/* ===== 消息区 ===== */
.chatlog {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px 14px;
  -webkit-overflow-scrolling: touch;
}

.msg {
  max-width: 75%;
  padding: 8px 12px;
  border-radius: 12px;
  word-break: break-word;
  font-size: 0.95rem;
  line-height: 1.4;
}

.msg .meta {
  font-size: 0.7rem;
  opacity: 0.7;
  margin-bottom: 2px;
}

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

.msg.own .meta {
  text-align: right;
}

.msg.other {
  align-self: flex-start;
  background: var(--bubble-other);
  border-bottom-left-radius: 4px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
}

.msg.system {
  align-self: center;
  max-width: 90%;
  background: transparent;
  color: var(--muted);
  font-size: 0.8rem;
  padding: 2px 8px;
}

/* ===== 输入区 ===== */
.chat-form {
  display: flex;
  align-items: center;
  gap: 8px;
  /* 上下统一 8px：iPhone 15 Pro 的 safe-area-inset-bottom(~34px) 会让按钮下方
     多出一大截空白，这里不加底部安全区，保持与顶部一致（home 指示条浮在白色底上即可） */
  padding: 8px 12px;
  background: #fff;
  border-top: 1px solid var(--border);
}

.chat-form input {
  flex: 1;
  min-width: 0;
}

.chat-form button {
  margin-top: 0;
  white-space: nowrap;
}
