:root {
  --bg: #15101b;
  --bg2: #1f1729;
  --panel: #2a1f38;
  --panel2: #342845;
  --text: #f3ecf7;
  --muted: #b3a3c4;
  --rose: #ff7eb6;
  --rose-dk: #e85fa0;
  --blue: #6cc8ff;
  --line: #3a2d4d;
  --ok: #6be0a0;
  --radius: 14px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, "PingFang SC", "Microsoft YaHei", "Segoe UI", system-ui, sans-serif;
  background: radial-gradient(1200px 600px at 70% -10%, #2a1b34 0%, var(--bg) 60%);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;            /* 杜绝横向溢出造成的左右空隙 */
}
.hidden { display: none !important; }
button { font-family: inherit; cursor: pointer; }

/* ---------- 按钮 ---------- */
.btn {
  border: 1px solid var(--line);
  background: var(--panel2);
  color: var(--text);
  padding: 9px 16px;
  border-radius: 10px;
  font-size: 14px;
  transition: transform .08s ease, background .15s ease, opacity .15s;
}
.btn:hover { background: #3e3056; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: linear-gradient(135deg, var(--rose), var(--rose-dk)); border-color: transparent; color: #fff; font-weight: 600; }
.btn.primary:hover { filter: brightness(1.07); background: linear-gradient(135deg, var(--rose), var(--rose-dk)); }
.btn.ghost { background: transparent; }
.btn.tiny { padding: 6px 11px; font-size: 13px; }
.btn.tiny.icon.off { opacity: .4; }

/* ---------- 进入房间 ---------- */
#join-screen {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
}
.join-card {
  width: min(420px, 100%);
  background: linear-gradient(180deg, var(--panel), var(--bg2));
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 32px 28px;
  box-shadow: 0 24px 60px rgba(0,0,0,.45);
}
.join-card h1 { margin: 0 0 4px; font-size: 30px; }
.join-card .heart { color: var(--rose); animation: beat 1.4s ease-in-out infinite; display: inline-block; }
@keyframes beat { 0%,100%{transform:scale(1)} 15%{transform:scale(1.25)} 30%{transform:scale(1)} }
.join-card .sub { margin: 0 0 22px; color: var(--muted); font-size: 14px; }
.join-card label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 14px; }
.join-card input {
  width: 100%;
  margin-top: 6px;
  padding: 12px 14px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--text);
  font-size: 15px;
}
.join-card input:focus { outline: none; border-color: var(--rose); }
.join-actions { display: flex; gap: 10px; margin-top: 6px; }
.join-actions .btn { flex: 1; }
.tip { color: var(--muted); font-size: 12.5px; line-height: 1.6; margin: 16px 0 0; }
.tip.small { font-size: 12px; margin-top: 8px; }

/* ---------- 顶栏 ---------- */
#app { height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 14px;
  padding: 10px 16px;
  background: rgba(31,23,41,.9);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(8px);
  flex-wrap: wrap;
}
.room-meta { display: flex; align-items: center; gap: 8px; }
.room-meta .label { color: var(--muted); font-size: 13px; }
.room-code { font-weight: 700; letter-spacing: 2px; color: var(--rose); font-size: 16px; }
.status-group { display: flex; gap: 12px; align-items: center; margin-left: auto; }
.partner { font-size: 13px; color: var(--muted); }
.partner.online { color: var(--rose); }
.conn { font-size: 12px; color: #c98b8b; }
.conn.ok { color: var(--ok); }
.top-actions { display: flex; gap: 8px; }

/* ---------- 内容布局 ---------- */
.content {
  flex: 1; min-height: 0;
  display: grid;
  grid-template-columns: 1fr 340px;
  gap: 14px;
  padding: 14px;
}
.stage-wrap { display: flex; flex-direction: column; gap: 12px; min-width: 0; }

/* ---------- 视频舞台 ---------- */
#stage {
  position: relative;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  aspect-ratio: 16 / 9;
  border: 1px solid var(--line);
}
#stage video {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: contain;
  background: #000;
}
#danmaku-layer {
  position: absolute; inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 5;
}
.danmaku-item {
  position: absolute;
  left: 100%;               /* 从右边缘起步 */
  top: 0;
  white-space: nowrap;
  font-size: 22px;
  font-weight: 600;
  color: #fff;
  text-shadow: 0 1px 4px rgba(0,0,0,.9), 0 0 2px rgba(0,0,0,.9);
  will-change: transform;
  animation-name: danmaku-fly;
  animation-timing-function: linear;
  animation-fill-mode: forwards;
}
@keyframes danmaku-fly {
  from { transform: translateX(0); }
  to   { transform: translateX(var(--dm-dist, -100vw)); }
}
.danmaku-item.self { font-style: normal; }
.float-emoji {
  position: absolute;
  bottom: 8%;
  font-size: 46px;
  pointer-events: none;
  z-index: 6;
  animation: floatUp 2.6s ease-out forwards;
}
@keyframes floatUp {
  0% { transform: translateY(0) scale(.6); opacity: 0; }
  15% { opacity: 1; transform: translateY(-10px) scale(1.1); }
  100% { transform: translateY(-220px) scale(1); opacity: 0; }
}
#stage-hint {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  z-index: 4;
  background: radial-gradient(circle at center, rgba(42,31,56,.6), rgba(0,0,0,.85));
  text-align: center;
}
.hint-inner { color: var(--muted); }
.hint-emoji { font-size: 52px; margin-bottom: 10px; }
.hint-text { white-space: pre-line; font-size: 14px; line-height: 1.6; }

/* ---------- 来源栏 ---------- */
#source-bar {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
}
.tabs { display: flex; gap: 8px; margin-bottom: 12px; }
.tab {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  padding: 8px 14px;
  border-radius: 10px;
  font-size: 14px;
}
.tab.active { background: var(--panel2); color: var(--text); border-color: var(--rose); }
.tab-panel { display: flex; flex-direction: column; gap: 10px; }
#url-input, .tab-panel input[type=text] {
  width: 100%;
  padding: 11px 13px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--text);
  font-size: 14px;
}
#url-input:focus { outline: none; border-color: var(--rose); }
.checkbox { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 8px; }
.file-row { display: flex; align-items: center; gap: 12px; }
.file-pick { position: relative; }
.file-name { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-actions { display: flex; gap: 10px; flex-wrap: wrap; }
#upload-progress { width: 100%; height: 8px; }
.share-status { font-size: 13px; color: var(--rose); min-height: 18px; }

/* ---------- 聊天 ---------- */
.chat-wrap {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  display: flex; flex-direction: column;
  min-height: 0;
}
.chat-head { padding: 12px 14px; border-bottom: 1px solid var(--line); font-weight: 600; display: flex; align-items: center; }
.chat-head .caret { display: none; margin-left: 8px; color: var(--muted); font-size: 12px; }
.chat-head .unread {
  display: none; min-width: 18px; height: 18px; line-height: 18px; text-align: center;
  background: var(--rose); color: #fff; border-radius: 9px; font-size: 11px; margin-left: 8px; padding: 0 5px;
}
.chat-head .unread.show { display: inline-block; }
#messages { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.msg { max-width: 85%; padding: 8px 11px; border-radius: 12px; font-size: 14px; line-height: 1.45; word-break: break-word; }
.msg .msg-name { display: block; font-size: 11px; color: var(--muted); margin-bottom: 2px; }
.msg:not(.self):not(.system) { align-self: flex-start; background: var(--panel2); border-bottom-left-radius: 4px; }
.msg.self { align-self: flex-end; background: linear-gradient(135deg, var(--rose), var(--rose-dk)); color: #fff; border-bottom-right-radius: 4px; }
.msg.self .msg-name { color: rgba(255,255,255,.8); text-align: right; }
.msg.system { align-self: center; background: transparent; color: var(--muted); font-size: 12px; text-align: center; max-width: 100%; }
.reactions { display: flex; gap: 6px; padding: 8px 12px 0; }
.reaction { border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 4px 9px; font-size: 18px; line-height: 1; }
.reaction:hover { background: var(--panel2); }
.chat-input-row { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--line); }
#chat-input {
  flex: 1; padding: 11px 13px;
  background: var(--bg); border: 1px solid var(--line);
  border-radius: 10px; color: var(--text); font-size: 14px;
}
#chat-input:focus { outline: none; border-color: var(--rose); }
.send { white-space: nowrap; }

/* 悬浮聊天按钮 + 关闭按钮（默认隐藏，仅手机端显示） */
.chat-fab {
  display: none;
  position: fixed;
  right: 14px;
  bottom: calc(16px + env(safe-area-inset-bottom));
  z-index: 80;
  width: 54px; height: 54px;
  align-items: center; justify-content: center;
  border: none; border-radius: 50%;
  font-size: 24px;
  background: linear-gradient(135deg, var(--rose), var(--rose-dk));
  color: #fff;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .45);
}
.chat-fab:active { transform: scale(.94); }
.fab-unread {
  display: none;
  position: absolute; top: -2px; right: -2px;
  min-width: 20px; height: 20px; line-height: 20px;
  padding: 0 5px;
  background: var(--blue); color: #15101b;
  border-radius: 10px; font-size: 12px; font-weight: 700;
}
.fab-unread.show { display: block; }
.chat-close {
  display: none;
  margin-left: auto;
  width: 28px; height: 28px;
  align-items: center; justify-content: center;
  border: none; border-radius: 8px;
  background: transparent; color: var(--muted);
  font-size: 16px;
}
.chat-close:active { background: var(--panel2); }

/* ---------- toast ---------- */
#toast {
  position: fixed; left: 50%; bottom: 40px; transform: translateX(-50%) translateY(20px);
  background: rgba(0,0,0,.85); color: #fff; padding: 10px 18px; border-radius: 20px;
  font-size: 14px; opacity: 0; pointer-events: none; transition: all .25s ease; z-index: 100;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- 响应式：手机端固定 App 外壳，聊天常显 ---------- */
@media (max-width: 880px) {
  html, body {
    height: 100%;
    overflow: hidden;             /* 锁住整页：消除左右空隙与触摸回弹晃动 */
    overscroll-behavior: none;
  }
  /* 高度跟随可视视口（由 JS 写入 --app-h），键盘弹出时自动收缩、并随页面上移量平移，
     避免输入框上方露出深色背景（黑边） */
  #app {
    height: var(--app-h, 100dvh);
    transform: translateY(var(--app-top, 0px));
  }

  /* 进入页可内部滚动，避免键盘遮挡按钮 */
  #join-screen {
    height: var(--app-h, 100dvh);
    overflow-y: auto;
    place-items: start center;
    padding-top: 32px;
  }

  .topbar { gap: 6px 8px; padding: 8px 12px; }
  .status-group { width: 100%; order: 3; margin-left: 0; justify-content: space-between; }
  .top-actions { margin-left: auto; }

  .content {
    position: relative;          /* 作为悬浮聊天的定位上下文 */
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    padding: 8px;
    gap: 8px;
    overflow: hidden;
  }

  /* 视频铺满可用空间，居中等比缩放 */
  .stage-wrap { flex: 1 1 auto; min-height: 0; gap: 8px; }
  #stage { flex: 1; height: 100%; aspect-ratio: auto; }
  #source-bar { flex: 0 0 auto; }

  /* 聊天默认收起在屏幕外，点右下角悬浮按钮才滑出，点 ✕ 收回 */
  .chat-wrap {
    position: fixed;
    top: 8px; right: 0; bottom: 8px;
    width: min(82vw, 340px);
    z-index: 78;
    min-height: 0;
    transform: translateX(106%);
    transition: transform .26s ease;
    border: none;
    border-left: 1px solid var(--line);
    border-radius: 14px 0 0 14px;
    background: rgba(26, 19, 32, 0.97);
    backdrop-filter: blur(12px);
    box-shadow: -12px 0 32px rgba(0, 0, 0, .5);
  }
  body.chat-open .chat-wrap { transform: translateX(0); }

  .chat-head { padding: 10px 12px; }
  .chat-close { display: inline-flex; }
  #messages {
    flex: 1; min-height: 0;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }
  .reactions { padding: 6px 10px 0; flex-wrap: wrap; }
  .chat-input-row { padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); }

  /* 悬浮聊天按钮：收起时显示，展开时隐藏 */
  .chat-fab { display: flex; }
  body.chat-open .chat-fab { display: none; }

  /* 关键：输入框字号 ≥16px，避免 iOS Safari 聚焦时自动放大页面 */
  #chat-input,
  #url-input,
  .tab-panel input[type=text],
  .join-card input { font-size: 16px; }
}
