:root {
  --bg-color: #121212;
  --card-bg: #1e1e1e;
  --primary: #bb86fc;
  --secondary: #03dac6;
  --text-main: #e0e0e0;
  --text-sub: #a0a0a0;
  --bubble-self: #6200ee;
  --bubble-other: #2c2c2c;
  --input-bg: #2d2d2d;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background-color: var(--bg-color);
  color: var(--text-main);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* --- 登录页 --- */
#loginScreen {
  position: fixed;
  top: 0; left: 0; width: 100%; height: 100%;
  background: 
    linear-gradient(135deg, rgba(31, 28, 44, 0.9), rgba(146, 141, 171, 0.8)),
    url('https://images.unsplash.com/photo-1536440136628-849c177e76a1?ixlib=rb-1.2.1&auto=format&fit=crop&w=1950&q=80');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  padding: 20px;
}
.login-box {
  background: rgba(255,255,255,0.1);
  backdrop-filter: blur(10px);
  padding: 30px;
  border-radius: 16px;
  width: 100%;
  max-width: 400px;
  text-align: center;
  box-shadow: 0 10px 30px rgba(0,0,0,0.5);
}
.login-input {
  width: 100%;
  padding: 15px;
  margin: 15px 0;
  border: none;
  border-radius: 8px;
  background: rgba(0,0,0,0.3);
  color: white;
  font-size: 18px;
  text-align: center;
}
.btn-primary {
  width: 100%;
  padding: 15px;
  border: none;
  border-radius: 8px;
  background: var(--primary);
  color: white;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  transition: transform 0.1s;
}
.btn-primary:active { transform: scale(0.98); }

/* --- 主界面 --- */
#appScreen { display: none; height: 100%; flex-direction: column; position: relative; }

/* 顶部导航 - 苹果刘海屏适配 */
.top-bar {
  padding: 4px 15px;
  background: rgba(18, 18, 18, 0.9);
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
  z-index: 110;
  padding-top: max(4px, env(safe-area-inset-top));
  padding-left: max(15px, env(safe-area-inset-left));
  padding-right: max(15px, env(safe-area-inset-right));
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  width: 100%;
  flex-wrap: nowrap;
  gap: 8px;
  box-sizing: border-box;
  overflow: hidden;
}
.room-info {
  flex: 1 1 30%; /* 占30%宽度，可伸缩 */
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.room-info h2 { margin: 0; font-size: 16px; color: var(--primary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-count { font-size: 12px; color: var(--text-sub); display: flex; align-items: center; }

/* 模式切换栏 */
.mode-switch {
  flex: 1 1 40%; /* 占40%宽度，可伸缩 */
  display: flex;
  gap: 8px;
  justify-content: center;
  z-index: 20;
  position: relative;
  min-width: 0;
}
.mode-switch button {
  flex: 1 1 50%; /* 按钮各占50%宽度 */
  min-width: 0;
}
.dot-green { width: 8px; height: 8px; background: #0f0; border-radius: 50%; margin-right: 5px; display: inline-block; }
.exit-btn {
  flex: 1 1 30%; /* 占30%宽度，可伸缩 */
  max-width: 120px; /* 最大宽度限制 */
  background: #f44336;
  color: white;
  border: none;
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 模式切换按钮样式 */
.pill {
  padding: 6px 16px;
  border-radius: 20px;
  background: #333;
  color: #aaa;
  font-size: 13px;
  border: none;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  min-width: 0;
}
.pill:hover { background: #444; }
.pill.active { background: var(--primary); color: white; }
.pill {
  padding: 6px 16px;
  border-radius: 20px;
  background: #333;
  color: #aaa;
  font-size: 13px;
  border: none;
  cursor: pointer;
  transition: all 0.2s;
}
.pill:hover { background: #444; }
.pill.active { background: var(--primary); color: white; }

/* 视频播放器基础样式 */
.video-player {
  width: 100%;
  height: 100%;
  object-fit: contain; /* 使用contain保持画面比例 */
  display: block; /* 确保视频元素是块级元素 */
  z-index: 2; /* 提高层级，确保视频在容器内最上层 */
  background: black;
  position: relative; /* 确保z-index生效 */
  outline: none; /* 移除默认焦点轮廓 */
}

/* 观影模式播放器 */
.movie-player {
  display: block; /* 默认显示 */
}

/* 竖屏视频优化 */
.video-player.portrait-mode {
  max-height: 100%;
  max-width: 70%;
  margin: 0 auto;
  display: block;
  height: auto;
}

/* 响应式设计 - 中等屏幕适配 (平板/大屏手机) */
@media (max-width: 768px) {
  /* 顶部栏调整 */
  .top-bar {
    top: 0;
    left: 0;
    right: 0;
    padding: 4px 8px;
    z-index: 110;
    width: 100%;
    gap: 5px;
  }
  
  .top-bar h2 {
    font-size: 13px;
  }
  
  .user-count {
    font-size: 11px;
  }
  
  /* 模式切换按钮调整 */
  .mode-switch {
    gap: 4px;
  }
  
  .mode-switch button {
    font-size: 11px;
    padding: 5px 8px;
  }
  
  /* 退出按钮调整 */
  .exit-btn {
    padding: 6px 8px;
    font-size: 12px;
    max-width: 100px;
  }
  }

/* 响应式设计 - 小屏幕适配 (普通手机) */
@media (max-width: 576px) {
  .top-bar {
    padding: 3px 6px;
    gap: 4px;
  }
  
  .top-bar h2 {
    font-size: 12px;
  }
  
  .user-count {
    font-size: 10px;
  }
  
  .mode-switch button {
    font-size: 10px;
    padding: 4px 6px;
  }
  
  .exit-btn {
    padding: 5px 6px;
    font-size: 11px;
    max-width: 90px;
  }
  }

/* 响应式设计 - 超小屏幕适配 (小屏手机) */
@media (max-width: 375px) {
  .top-bar {
    padding: 3px 4px;
    gap: 3px;
  }
  
  .top-bar h2 {
    font-size: 11px;
  }
  
  .user-count {
    font-size: 9px;
  }
  
  .mode-switch button {
    font-size: 9px;
    padding: 3px 4px;
  }
  
  .exit-btn {
    padding: 4px 5px;
    font-size: 10px;
    max-width: 80px;
  }
  }

/* 视频容器调整 - 刘海屏适配 */
.video-container {
    position: absolute;
    top: 40px; /* 直接在顶部导航栏下方显示视频 */
    bottom: 120px; /* 为输入栏和底部安全区域留出空间 */
    left: 0;
    right: 0;
    background: black;
    overflow: hidden;
    z-index: 1;
  }
  
  /* 视频播放器调整 */
  .video-player {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
    z-index: 2;
  }
  
  /* 房主控制条调整 */
  .video-overlay {
    top: 58px; /* 将移动端房主控制栏向上移动 */
    left: 0;
    right: 0;
    width: 100%; /* 全屏宽度 */
    padding: 5px 15px;
    padding-left: max(15px, env(safe-area-inset-left)); /* 适配左侧安全区域 */
    padding-right: max(15px, env(safe-area-inset-right)); /* 适配右侧安全区域 */
    z-index: 100;
    background: transparent;
    box-shadow: none;
    display: none; /* 隐藏移动端房主控制栏 */
  }
  
  .host-controls input {
    font-size: 12px;
    padding: 8px 12px;
    width: 70%; /* 相对宽度，自适应屏幕 */
    max-width: 300px; /* 最大宽度限制 */
  }
  
  .host-controls button {
    font-size: 12px;
    padding: 8px 16px;
  }
  
  /* 弹幕区域调整 */
  .chat-area {
    position: absolute;
    bottom: 100px; /* 从底部开始 */
    left: 10px;
    right: 10px;
    height: 40%; /* 只显示视频底部40%的区域 */
    max-height: 200px;
    width: calc(100% - 20px);
    z-index: 25;
    pointer-events: auto;
    /* 保持弹幕左对齐 */
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    align-items: flex-start;
    gap: 10px;
    padding: 15px 15px 15px 0;
  }
  
  /* 输入栏调整 */
  .input-bar {
    bottom: 20px;
    left: 10px;
    right: 10px;
    padding: 10px;
    z-index: 30;
  }
  
  .chat-input {
    font-size: 14px;
    padding: 12px 16px;
  }
  
  .send-btn {
    padding: 12px;
    width: 44px;
    height: 44px;
  }
}

/* 横屏模式优化 */
@media (orientation: landscape) and (max-height: 500px) {
  /* 视频容器调整 */
  .video-container {
    top: 40px; /* 直接在顶部导航栏下方显示视频 */
    bottom: 80px;
  }
  
  /* 弹幕区域调整 */
  .chat-area {
    bottom: 80px; /* 从底部开始 */
    height: 40%; /* 只显示视频底部40%的区域 */
    max-height: 150px;
    /* 保持弹幕左对齐 */
    align-items: flex-start;
  }
  
  /* 输入栏调整 */
  .input-bar {
    bottom: 10px;
    padding: 10px;
  }
}

/* 竖屏模式优化 */
@media (orientation: portrait) {
  /* 竖屏时视频居中显示 */
  .video-player {
    object-fit: contain;
  }
  
  /* 弹幕区域高度调整 */
  .chat-area {
    height: 35%;
    /* 保持弹幕左对齐 */
    align-items: flex-start;
  }
}

/* 直播模式播放器 */
.live-player {
  display: none; /* 默认隐藏 */
}

/* 模式特定样式 */
body[data-mode="movie"] .movie-player {
  display: block;
}

body[data-mode="movie"] .live-player {
  display: none;
}

body[data-mode="live"] .movie-player {
  display: none;
}

body[data-mode="live"] .live-player {
  display: block;
}

/* 视频区域 */
.video-container {
  position: absolute;
  top: 40px; /* 直接在顶部导航栏下方显示视频 */
  left: 0;
  width: 100%;
  height: calc(100% - 40px); /* 减去顶部导航栏的高度 */
  background: black;
  overflow: hidden;
  z-index: 1;
}

/* 直播占位符 */
#liveArea {
  position: absolute;
  font-size: 14px;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  z-index: 0; /* 确保占位符在视频下方 */
  color: white;
  text-align: center;
  padding: 20px;
  box-sizing: border-box;
}

/* 视频覆盖层 (房主控制) */
.video-overlay {
  position: fixed;
  top: 48px; /* 将房主控制栏向上移动 */
  left: 0;
  right: 0;
  width: 100%; /* 恢复全屏宽度 */
  background: transparent; /* 完全透明背景 */
  padding: 10px 15px;
  padding-left: max(15px, env(safe-area-inset-left)); /* 适配左侧安全区域 */
  padding-right: max(15px, env(safe-area-inset-right)); /* 适配右侧安全区域 */
  display: none; /* 隐藏房主控制栏 */
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center; /* 内部元素居中对齐 */
  opacity: 1; /* 默认显示，不再依赖hover */
  transition: opacity 0.3s;
  z-index: 90; /* 确保在视频上方，但在顶部导航栏下方 */
  border-radius: 0; /* 无边框设计 */
  box-sizing: border-box;
  box-shadow: none; /* 移除阴影 */
  pointer-events: none; /* 房主控制栏不接收鼠标事件，不影响视频交互 */
}

.host-controls input {
  max-width: 300px; /* 限制最大宽度 */
  width: calc(70% - 10px); /* 相对宽度，减去按钮宽度的影响 */
  padding: 10px 15px;
  border-radius: 20px; /* 圆角输入框 */
  border: none; /* 移除边框 */
  background: rgba(30, 30, 30, 0.8); /* 半透明背景 */
  color: white;
  outline: none;
  font-size: 14px;
  pointer-events: auto; /* 输入框可以正常交互 */
  box-sizing: border-box;
}
.host-controls button {
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 50%; /* 圆形按钮 */
  border: none;
  background: transparent; /* 透明背景 */
  color: white;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: auto; /* 按钮可以正常交互 */
  box-sizing: border-box;
  z-index: 120; /* 确保按钮在最上层，能够被点击 */
}
.host-controls button:hover {
  background: rgba(3, 218, 198, 0.7); /* 悬停时稍不透明 */
  transform: scale(1.1); /* 放大效果 */
}
.host-controls button:active {
  transform: scale(0.95); /* 缩小效果 */
}
.host-controls .live-btn {
  border: none;
  background: transparent;
  color: #ff4444;
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 50%;
  font-size: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 120; /* 确保按钮在最上层，能够被点击 */
}
.host-controls .live-btn:hover {
  background: rgba(255, 68, 68, 0.2);
  transform: scale(1.05);
}
.host-controls .stop-btn {
  border: none;
  background: transparent;
  color: #aaaaaa;
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 50%;
  font-size: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 120; /* 确保按钮在最上层，能够被点击 */
}
.host-controls .stop-btn:hover {
  background: rgba(102, 102, 102, 0.2);
  transform: scale(1.05);
}

/* 聊天区域 */
.chat-area {
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 120px; /* 底部留出更多空间给输入框 */
  width: calc(100% - 20px);
  height: 35%; /* 调整弹幕区域高度 */
  max-height: 180px;
  overflow-y: hidden; /* 隐藏垂直滚动条 */
  background: transparent; /* 透明背景，让视频可见 */
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: flex-start;
  gap: 8px;
  padding: 10px 10px 10px 0;
  z-index: 25; /* 提高层级，确保在视频和控制按钮上方 */
  pointer-events: none; /* 弹幕区域不接收鼠标事件，不影响视频交互 */
}

/* 滚动弹幕容器 */
.rolling-chat {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

/* 滚动弹幕轨道 */
.rolling-track {
  position: absolute;
  right: -100%; /* 从右侧开始 */
  white-space: nowrap;
  animation: roll 10s linear infinite;
  pointer-events: none;
}

/* 滚动动画 */
@keyframes roll {
  0% { right: -100%; }
  100% { right: 100%; }
}

/* 多轨道配置 */
.rolling-track.track-0 { top: 0; }
.rolling-track.track-1 { top: 25%; }
.rolling-track.track-2 { top: 50%; }
.rolling-track.track-3 { top: 75%; }

/* 暂停动画 */
.rolling-chat.paused .rolling-track {
  animation-play-state: paused;
}
.system-msg {
  text-align: center;
  font-size: 12px;
  color: rgba(224, 224, 224, 0.7); /* 半透明白色 */
  margin: 5px 0;
  background: rgba(255,255,255,0.05); /* 更透明的背景 */
  padding: 4px 10px;
  border-radius: 10px;
  align-self: center;
}
.msg-row { 
  display: flex; 
  align-items: flex-start; 
  max-width: 100%;
  width: 100%;
  padding: 0;
  margin: 0;
  flex-wrap: nowrap;
}
.avatar {
  width: 26px; height: 26px;
  border-radius: 50%;
  margin: 0 10px 0 0;
  flex-shrink: 0;
  display: flex; 
  align-items: center; 
  justify-content: center;
  font-size: 13px;
  color: white;
  font-weight: bold;
  box-shadow: 0 0 4px rgba(0, 0, 0, 0.6);
  overflow: hidden;
  border: none;
  /* 背景色将由JavaScript动态设置 */
}
.msg-content {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  gap: 8px;
  max-width: calc(100% - 34px);
  flex-wrap: wrap;
}
.nickname {
  font-size: 13px;
  color: white;
  font-weight: 500;
  margin: 0;
  display: inline-block;
  text-shadow: 0 0 3px rgba(0, 0, 0, 0.8);
}
.msg-text {
  font-size: 13px;
  color: white;
  word-break: break-word;
  text-shadow: 0 0 3px rgba(0, 0, 0, 0.8);
  max-width: 150px; /* 限制最大宽度，大约10-15个中文字符 */
  line-height: 1.2;
  white-space: normal; /* 允许文本换行 */
}

/* 底部输入框 - 苹果刘海屏适配 */
.input-bar {
  position: absolute;
  bottom: 20px; /* 调整位置，避免挡住视频进度条 */
  left: 0;
  right: 0;
  background: transparent; /* 完全透明背景 */
  padding: 15px;
  padding-left: max(15px, env(safe-area-inset-left)); /* 适配左侧安全区域 */
  padding-right: max(15px, env(safe-area-inset-right)); /* 适配右侧安全区域 */
  padding-bottom: max(15px, env(safe-area-inset-bottom) + 10px); /* 适配底部安全区域，增加额外空间 */
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  box-shadow: none; /* 去除阴影 */
  z-index: 30; /* 确保在所有元素最上层 */
}
.chat-input {
  flex: 1;
  max-width: 80%;
  background: rgba(30, 30, 30, 0.7); /* 半透明深色背景 */
  border: none;
  padding: 14px 20px;
  border-radius: 25px; /* 胶囊形状 */
  color: rgba(255, 255, 255, 0.9);
  outline: none;
  font-size: 14px;
  backdrop-filter: blur(5px); /* 毛玻璃效果 */
}
.send-btn {
  background: rgba(187, 134, 252, 0.8); /* 半透明紫色 */
  color: white;
  border: none;
  width: 44px; height: 44px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  backdrop-filter: blur(5px); /* 毛玻璃效果 */
  transition: all 0.2s ease;
}
.send-btn:hover {
  background: rgba(187, 134, 252, 1);
  transform: scale(1.05);
}
.send-btn:active {
  transform: scale(0.95);
}

/* Toast 提示框 */
#toast-container {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2000;
  pointer-events: none;
}
.toast {
  background: rgba(50, 50, 50, 0.95);
  color: white;
  padding: 10px 20px;
  border-radius: 20px;
  margin-bottom: 10px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
  font-size: 14px;
  animation: fadeInOut 3s forwards;
  display: flex;
  align-items: center;
}
@keyframes fadeInOut {
  0% { opacity: 0; transform: translateY(-10px); }
  10% { opacity: 1; transform: translateY(0); }
  90% { opacity: 1; transform: translateY(0); }
  100% { opacity: 0; transform: translateY(-10px); }
}

/* 确保安全区域样式正确应用 */
@supports (padding: max(0px)) {
  .top-bar { padding-top: max(10px, env(safe-area-inset-top)); }
  .input-bar { padding-bottom: max(10px, env(safe-area-inset-bottom)); }
}