:root {
  --pink: #f472b6;
  --pink-deep: #ec4899;
  --purple: #a78bfa;
  --ink: #3b2f3a;
  --ink-soft: #8a7a86;
  --bg: #fdf6fb;
  --card: #ffffff;
  --line: #f1e3ee;
  --me: linear-gradient(135deg, #f9a8d4, #f472b6);
  --other: #ffffff;
  --radius: 18px;
  --shadow: 0 8px 24px rgba(190, 120, 160, 0.14);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  color: var(--ink);
  background:
    radial-gradient(1200px 600px at 10% -10%, #ffe4f1 0%, transparent 60%),
    radial-gradient(900px 500px at 100% 110%, #ede9fe 0%, transparent 55%),
    var(--bg);
  overscroll-behavior: none;
  overscroll-behavior-y: none;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: var(--app-height, 100dvh);
  max-width: 760px;
  margin: 0 auto;
  background: rgba(255, 255, 255, 0.55);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  border-left: 1px solid var(--line);
  border-right: 1px solid var(--line);
}

/* ---------- 顶栏 ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: rgba(255, 255, 255, 0.82);
  border-bottom: 1px solid var(--line);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
}

.brand { display: flex; align-items: center; gap: 10px; }

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 14px;
  display: grid;
  place-items: center;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(135deg, var(--pink), var(--purple));
  box-shadow: 0 6px 14px rgba(236, 72, 153, 0.3);
}

.titles .title { font-weight: 700; font-size: 16px; letter-spacing: .5px; }
.titles .subtitle { font-size: 12px; color: var(--ink-soft); margin-top: 2px; }

.name-btn {
  border: none;
  background: #fff;
  width: 38px;
  height: 38px;
  border-radius: 12px;
  font-size: 16px;
  cursor: pointer;
  box-shadow: var(--shadow);
}

/* ---------- 消息区 ---------- */

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

.msg {
  display: flex;
  gap: 8px;
  max-width: 100%;
  animation: pop .18s ease-out;
}

@keyframes pop {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

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

.msg .bubble-wrap { display: flex; flex-direction: column; gap: 4px; max-width: 74%; }
.msg.me .bubble-wrap { align-items: flex-end; }

.meta {
  font-size: 11px;
  color: var(--ink-soft);
  padding: 0 6px;
}

.bubble {
  position: relative;
  padding: 10px 14px;
  border-radius: var(--radius);
  background: var(--other);
  box-shadow: var(--shadow);
  font-size: 15px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg.me .bubble {
  background: var(--me);
  color: #fff;
  border-bottom-right-radius: 6px;
}

.msg.other .bubble { border-bottom-left-radius: 6px; }

.bubble.sticker {
  background: transparent;
  box-shadow: none;
  font-size: 64px;
  line-height: 1;
  padding: 2px 6px;
}

.msg.me .bubble.sticker { background: transparent; }

.bubble img.photo {
  display: block;
  max-width: 240px;
  max-height: 300px;
  border-radius: 14px;
  cursor: zoom-in;
}

.bubble.media { padding: 6px; background: #fff; }
.msg.me .bubble.media { background: #fff; }

.bubble audio {
  display: block;
  width: 220px;
  max-width: 60vw;
}

.bubble .duration {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--ink-soft);
  padding: 2px 4px 0;
}

/* 正在输入 */
.typing {
  padding: 0 20px 6px;
  font-size: 12px;
  color: var(--ink-soft);
  min-height: 18px;
}

/* ---------- 输入区 ---------- */

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: rgba(255, 255, 255, 0.9);
  border-top: 1px solid var(--line);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
}

.icon-btn {
  border: none;
  touch-action: manipulation;
  background: #fff;
  width: 40px;
  height: 40px;
  border-radius: 13px;
  font-size: 19px;
  cursor: pointer;
  box-shadow: var(--shadow);
  flex: none;
  transition: transform .1s ease;
}

.icon-btn:active { transform: scale(.94); }
.icon-btn.active { background: #ffe4f1; }

#input {
  flex: 1;
  resize: none;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 10px 14px;
  font-size: 16px;
  line-height: 1.4;
  max-height: 120px;
  outline: none;
  background: #fff;
  color: var(--ink);
  font-family: inherit;
}

#input:focus { border-color: #f9a8d4; box-shadow: 0 0 0 3px rgba(244, 114, 182, .14); }

.send-btn {
  border: none;
  touch-action: manipulation;
  border-radius: 14px;
  padding: 0 18px;
  height: 40px;
  font-size: 15px;
  font-weight: 600;
  color: #fff;
  background: linear-gradient(135deg, var(--pink), var(--pink-deep));
  box-shadow: 0 8px 18px rgba(236, 72, 153, .3);
  cursor: pointer;
  flex: none;
}

.send-btn:active { transform: scale(.97); }

/* ---------- 表情面板 ---------- */

.emoji-panel {
  position: fixed;
  left: 50%;
  bottom: 76px;
  transform: translateX(-50%);
  width: min(720px, calc(100vw - 24px));
  background: #fff;
  border-radius: 20px;
  box-shadow: 0 18px 50px rgba(120, 60, 100, .2);
  padding: 10px 12px 12px;
  z-index: 30;
}

.emoji-tabs {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 8px;
}

.tab {
  border: none;
  touch-action: manipulation;
  background: #fdf2f8;
  color: var(--ink-soft);
  border-radius: 10px;
  padding: 6px 14px;
  font-size: 13px;
  cursor: pointer;
}

.tab.active { background: #ffe4f1; color: var(--pink-deep); font-weight: 600; }
.tab.close { margin-left: auto; background: transparent; font-size: 14px; }

.emoji-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(40px, 1fr));
  gap: 4px;
  max-height: 232px;
  overflow-y: auto;
}

.emoji-grid button {
  border: none;
  background: transparent;
  font-size: 24px;
  height: 40px;
  border-radius: 10px;
  cursor: pointer;
  line-height: 1;
}

.emoji-grid button:hover { background: #fdf2f8; }
.emoji-grid button.sticker { font-size: 34px; }

/* ---------- 录音 ---------- */

.record-mask {
  position: fixed;
  inset: 0;
  background: rgba(59, 47, 58, .35);
  backdrop-filter: blur(4px);
  display: grid;
  place-items: center;
  z-index: 40;
}

.record-card {
  width: min(300px, 86vw);
  background: #fff;
  border-radius: 24px;
  padding: 24px 20px 18px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(80, 40, 70, .3);
}

.rec-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #ef4444;
  margin: 0 auto 10px;
  animation: blink 1s infinite;
}

@keyframes blink { 50% { opacity: .25; } }

.rec-time { font-size: 30px; font-weight: 700; font-variant-numeric: tabular-nums; }
.rec-tip { font-size: 12px; color: var(--ink-soft); margin: 8px 0 16px; }
.rec-actions { display: flex; gap: 10px; }

.rec-actions button {
  flex: 1;
  height: 42px;
  border-radius: 14px;
  border: none;
  font-size: 15px;
  cursor: pointer;
}

.ghost { background: #fdf2f8; color: var(--ink-soft); }

.primary {
  background: linear-gradient(135deg, var(--pink), var(--pink-deep));
  color: #fff;
  font-weight: 600;
  border: none;
  height: 44px;
  border-radius: 14px;
  cursor: pointer;
  font-size: 15px;
}

/* ---------- 昵称弹窗 ---------- */

dialog {
  border: none;
  border-radius: 24px;
  padding: 26px 22px 20px;
  width: min(340px, 88vw);
  box-shadow: 0 24px 70px rgba(90, 40, 80, .3);
}

dialog::backdrop { background: rgba(59, 47, 58, .35); -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px); }
dialog h2 { margin: 0 0 4px; font-size: 20px; }
dialog p { margin: 0 0 16px; font-size: 13px; color: var(--ink-soft); }

dialog input {
  width: 100%;
  height: 44px;
  border-radius: 14px;
  border: 1px solid var(--line);
  padding: 0 14px;
  font-size: 16px;
  margin-bottom: 14px;
  outline: none;
}

dialog input:focus { border-color: #f9a8d4; }

dialog .primary { width: 100%; }

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 96px;
  transform: translateX(-50%);
  background: rgba(59, 47, 58, .9);
  color: #fff;
  font-size: 13px;
  padding: 9px 16px;
  border-radius: 999px;
  z-index: 50;
  animation: pop .2s ease-out;
}

/* 桌面端微调 */
@media (min-width: 900px) {
  .app { border-radius: 0; height: 100dvh; }
}

/* 窄屏：压缩输入栏，给文字框留空间 */
@media (max-width: 430px) {
  .composer { gap: 6px; padding: 8px 8px calc(8px + env(safe-area-inset-bottom)); }
  .icon-btn { width: 36px; height: 36px; font-size: 17px; border-radius: 12px; }
  .send-btn { padding: 0 14px; height: 36px; font-size: 14px; }
  #input { padding: 8px 12px; }
}

/* ---------- 顶栏操作 ---------- */

.top-actions { display: flex; gap: 8px; }

/* ---------- 房间密码 ---------- */

.gate {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(900px 500px at 20% 0%, #ffe4f1 0%, transparent 60%),
    radial-gradient(800px 500px at 100% 100%, #ede9fe 0%, transparent 55%),
    var(--bg);
}

.gate-card {
  width: min(340px, 100%);
  background: #fff;
  border-radius: 24px;
  padding: 28px 22px 22px;
  box-shadow: 0 24px 70px rgba(90, 40, 80, .18);
  text-align: center;
}

.gate-lock {
  width: 56px;
  height: 56px;
  margin: 0 auto 12px;
  border-radius: 18px;
  display: grid;
  place-items: center;
  font-size: 26px;
  background: linear-gradient(135deg, #ffe4f1, #ede9fe);
}

.gate-card h1 { margin: 0; font-size: 22px; letter-spacing: 1px; }
.gate-card p { margin: 6px 0 18px; font-size: 13px; color: var(--ink-soft); }

.gate-card input {
  width: 100%;
  height: 46px;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0 14px;
  font-size: 16px; /* iOS：小于 16px 会自动放大 */
  outline: none;
  margin-bottom: 12px;
  text-align: center;
  letter-spacing: 1px;
}

.gate-card input:focus { border-color: #f9a8d4; box-shadow: 0 0 0 3px rgba(244, 114, 182, .14); }
.gate-card .primary { width: 100%; }
.gate-error { margin-top: 10px; font-size: 13px; color: #e11d48; }

/* ---------- 图片灯箱 ---------- */

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 55;
  background: rgba(0, 0, 0, .82);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}

.lightbox img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 12px;
  object-fit: contain;
}

/* 老版本 iOS 的 <dialog> 兜底 */
dialog.fallback {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  margin: 0;
  z-index: 70;
  display: block;
}
dialog.fallback:not([open]) { display: none; }
