/* ── P2P Call — UI Styles (2026-09-16 重构：移动优先 / dvh / 安全区) ── */

:root {
  --bg:        #0b0d12;
  --bg-2:      #12161f;
  --card:      #161b25;
  --card-2:    #1b2130;
  --border:    #262d3b;
  --text:      #e9ecf3;
  --muted:     #8d96a8;
  --primary:   #3b82f6;
  --primary-2: #2563eb;
  --danger:    #ef4444;
  --ok:        #22c55e;
  --warn:      #f59e0b;
  --radius:    14px;
  --transition: .2s ease;
  /* 刘海屏/手势条安全区 */
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

*, *::before, *::after { box-sizing: border-box; }

html { height: 100%; }

body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
               "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

/* 关键修复：不再用 100vh + overflow:hidden 锁死页面。
   落地页允许纵向滚动，保证小屏/横屏都能滚到"加入"按钮。 */
.view { width: 100%; }

/* ══════════════════ 落地页 ══════════════════ */
.landing {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  min-height: 100dvh;          /* dvh 不含浏览器地址栏 —— 移动端关键 */
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: calc(20px + var(--safe-t)) calc(16px + var(--safe-r))
           calc(20px + var(--safe-b)) calc(16px + var(--safe-l));
}

.landing-card {
  width: 100%;
  max-width: 440px;
  margin: auto;                 /* 内容高于屏幕时仍可滚动 */
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 26px 20px 20px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, .45);
}

.logo { display: flex; flex-direction: column; align-items: center; gap: 10px; color: var(--primary); }
.logo h1 { margin: 0; font-size: 21px; font-weight: 700; color: var(--text); letter-spacing: .3px; }
.subtitle { margin: 8px 0 22px; text-align: center; font-size: 13px; line-height: 1.6; color: var(--muted); }

.conn-status-bar {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  margin-bottom: 20px; padding: 9px 12px;
  background: var(--bg-2); border: 1px solid var(--border); border-radius: 10px;
}
.conn-text { font-size: 13px; color: var(--muted); }

.conn-dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; flex: 0 0 auto; }
.conn-dot.connecting   { background: var(--warn); animation: pulse 1.2s ease-in-out infinite; }
.conn-dot.connected    { background: var(--ok); }
.conn-dot.disconnected { background: var(--danger); }
.conn-dot.error        { background: var(--danger); }
@keyframes pulse { 0%, 100% { opacity: 1 } 50% { opacity: .3 } }

.landing-actions { display: flex; flex-direction: column; gap: 14px; }

/* 关键修复：用 grid 代替单行 flex，窄屏自动换行，按钮永远在屏幕内 */
.join-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.join-row > #btn-join-room { grid-column: 1 / -1; }
@media (min-width: 480px) {
  .join-row { grid-template-columns: 1fr 112px auto; align-items: stretch; }
  .join-row > #btn-join-room { grid-column: auto; }
}

/* 分隔线（flex + 两侧渐变线） */
.divider {
  display: flex; align-items: center; gap: 12px;
  font-size: 12px; color: var(--muted);
}
.divider::before, .divider::after {
  content: ''; flex: 1; height: 1px; background: var(--border);
}

/* 剪贴板导入按钮（次级动作，不抢主按钮） */
.btn-ghost {
  background: rgba(59, 130, 246, .1);
  border: 1px dashed rgba(59, 130, 246, .45);
  color: #9dc1ff;
  font-size: 15px;
}
.btn-ghost:hover:not(:disabled) { background: rgba(59, 130, 246, .18); }

.hint { margin: 10px 0 0; font-size: 12px; line-height: 1.6; color: var(--muted); text-align: center; }
.hint strong { color: #9dc1ff; font-weight: 600; }

/* ── 表单控件：16px 字号（否则 iOS 聚焦时会自动放大页面）── */
.input-room {
  width: 100%; min-width: 0;
  min-height: 50px;
  padding: 13px 14px;
  font-size: 16px;
  font-family: inherit;
  color: var(--text);
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  transition: border-color var(--transition), box-shadow var(--transition);
  appearance: none;
}
.input-room::placeholder { color: #5d6678; }
.input-room:focus {
  outline: none; border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, .18);
}
.input-pass { text-align: center; letter-spacing: 3px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 13px 18px;
  font-size: 16px; font-weight: 600; font-family: inherit;
  border-radius: 12px; border: 1px solid transparent;
  cursor: pointer; touch-action: manipulation;
  transition: background var(--transition), opacity var(--transition), transform .08s ease;
}
.btn:active:not(:disabled) { transform: scale(.985); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-2); }
.btn-outline { background: transparent; border-color: var(--border); color: var(--text); }
.btn-outline:hover:not(:disabled) { background: var(--card-2); }
.btn-lg { min-height: 52px; font-size: 16.5px; }

.status-text { min-height: 20px; margin-top: 16px; text-align: center; font-size: 13px; color: var(--muted); }

/* ══════════════════ 通话页 ══════════════════ */
.call {
  position: relative;
  display: flex; flex-direction: column;
  height: 100vh;
  height: 100dvh;               /* 移动端关键 */
  overflow: hidden;
  background: #000;
}

.video-stage { position: relative; flex: 1 1 auto; min-height: 0; background: #05070c; }
.video-stage > video,
#remote-video {
  display: block; width: 100%; height: 100%;
  object-fit: contain; background: #05070c;
}
/* 远端共享屏幕时铺满（contain 已足够；留 max-height 兜底） */
#remote-video { max-height: 100%; }

.top-bar {
  position: absolute; z-index: 8;
  top: calc(10px + var(--safe-t));
  left: calc(10px + var(--safe-l));
  right: calc(10px + var(--safe-r));
  display: flex; align-items: flex-start; justify-content: space-between; gap: 10px;
  pointer-events: none;
}
.top-bar > * { pointer-events: auto; }

.video-overlay {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 7px 12px; border-radius: 999px;
  background: rgba(10, 13, 20, .68);
  border: 1px solid rgba(255, 255, 255, .1);
  backdrop-filter: blur(6px);
  max-width: 52%;
}
.call-status { font-size: 12.5px; color: #dfe5f0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.room-info {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 7px 10px; border-radius: 12px;
  background: rgba(10, 13, 20, .68);
  border: 1px solid rgba(255, 255, 255, .1);
  backdrop-filter: blur(6px);
  font-size: 12.5px; flex-wrap: wrap; justify-content: flex-end;
}
.room-label { color: var(--muted); font-size: 11.5px; }
.room-code { font-weight: 700; letter-spacing: 1.5px; font-variant-numeric: tabular-nums; }

.btn-icon {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 44px; min-height: 32px; padding: 4px 10px;
  font-size: 12px; font-family: inherit;
  color: var(--text); background: rgba(255, 255, 255, .08);
  border: 1px solid rgba(255, 255, 255, .12); border-radius: 9px;
  cursor: pointer; touch-action: manipulation;
}
.btn-icon:active { background: rgba(255, 255, 255, .16); }

/* 本地画中画（含共享屏幕时的提示） */
.local-pip {
  position: absolute; z-index: 7;
  right: calc(10px + var(--safe-r));
  bottom: calc(12px);
  width: 27%; max-width: 190px; min-width: 92px;
  aspect-ratio: 3 / 4;
  border-radius: 12px; overflow: hidden;
  border: 1px solid rgba(255, 255, 255, .16);
  box-shadow: 0 8px 26px rgba(0, 0, 0, .5);
  background: #000;
  transition: width var(--transition), aspect-ratio var(--transition);
}
.local-pip video { display: block; width: 100%; height: 100%; object-fit: cover; }
/* 自己正在共享屏幕：PIP 变成 16:9 便于看内容 */
.local-pip.sharing { aspect-ratio: 16 / 9; width: 34%; max-width: 240px; }
.local-pip::after {
  content: attr(data-label); position: absolute; left: 0; right: 0; bottom: 0;
  padding: 3px 6px; font-size: 10px; text-align: center; color: #fff;
  background: rgba(0, 0, 0, .55); opacity: 0; transition: opacity var(--transition);
}
.local-pip.sharing::after { opacity: 1; }

.stats-bar {
  position: absolute; z-index: 6;
  left: calc(10px + var(--safe-l));
  /* 关键修复：统计栏锚在"视频区底部"，与下方的控制栏之间留出安全边距，
     并且绝不允许溢出到控制栏上（controls 在 .video-stage 之外，天然不重叠） */
  bottom: 10px;
  max-width: min(58%, 340px);
  padding: 4px 8px; border-radius: 8px;
  font-size: 10.5px; line-height: 1.5; color: #c6cfe0;
  background: rgba(0, 0, 0, .5);
  pointer-events: none;
  white-space: pre-wrap;
  display: flex; flex-wrap: wrap; gap: 2px 10px;
}
.stats-bar span { white-space: nowrap; }
/* 用户可在设置里关掉（手机默认关，避免遮挡画面/按钮） */
.stats-bar.hidden { display: none; }
.stats-bar[data-quality="good"] span:first-child { color: #4ade80; }
.stats-bar[data-quality="fair"] span:first-child { color: #fbbf24; }
.stats-bar[data-quality="poor"] span:first-child { color: #f87171; }

/* 窄屏：只显示"质量 + 帧率 + RTT"，避免一大坨文字盖住画面 */
@media (max-width: 560px) {
  .stats-bar { font-size: 10px; max-width: 66%; gap: 2px 8px; }
  .stats-bar span:nth-child(2),
  .stats-bar span:nth-child(3),
  .stats-bar span:nth-child(4) { display: none; }
}

/* ── 空闲回收倒计时提示 ── */
.ttl-notice {
  flex-basis: 100%;
  text-align: right;
  font-size: 10.5px;
  color: rgba(255, 255, 255, .55);
}
.ttl-notice:empty { display: none; }
.ttl-notice.warn { color: #fbbf24; animation: pulse 1.4s ease-in-out infinite; }

/* ── 控制栏 ── */
.controls {
  display: flex; align-items: center; justify-content: center;
  gap: 6px; flex-wrap: wrap;
  padding: 8px 10px calc(8px + var(--safe-b));
  background: var(--bg-2);
  border-top: 1px solid var(--border);
}
.ctrl-btn {
  position: relative;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  min-width: 66px; min-height: 58px; padding: 6px 8px;
  font-size: 11px; font-family: inherit;
  color: var(--text); background: transparent;
  border: none; border-radius: 12px;
  cursor: pointer; touch-action: manipulation;
  transition: background var(--transition), color var(--transition);
}
.ctrl-btn:active { background: var(--card-2); }
.ctrl-btn svg { width: 22px; height: 22px; }
.ctrl-btn.off { color: var(--danger); }
.ctrl-btn.active { color: var(--ok); }
.ctrl-btn .icon-mic-off, .ctrl-btn .icon-cam-off { display: none; }
.ctrl-btn.off .icon-mic-on,  .ctrl-btn.off .icon-cam-on  { display: none; }
.ctrl-btn.off .icon-mic-off, .ctrl-btn.off .icon-cam-off { display: inline; }
.ctrl-divider { width: 1px; height: 30px; background: var(--border); }

/* 麦克风电平条：让用户一眼确认"麦在工作" */
.mic-level {
  position: absolute; left: 16%; right: 16%; bottom: 5px; height: 3px;
  border-radius: 2px; background: rgba(255, 255, 255, .12); overflow: hidden;
}
.mic-level::after {
  content: ''; display: block; height: 100%; width: calc(var(--level, 0) * 100%);
  background: linear-gradient(90deg, #22c55e, #eab308, #ef4444);
  transition: width .08s linear;
}
.ctrl-btn.off .mic-level::after { background: #556; }
.btn-hangup { background: var(--danger); color: #fff; border-radius: 13px; min-width: 76px; }
.btn-hangup:active { background: #dc2626; }

@media (max-width: 380px) {
  .ctrl-btn { min-width: 58px; font-size: 10px; }
  .ctrl-btn svg { width: 20px; height: 20px; }
  .room-label { display: none; }
}

/* ── 设置面板（底部抽屉）── */
.settings-backdrop {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(0, 0, 0, .55);
  opacity: 0; pointer-events: none; transition: opacity .25s ease;
}
.settings-backdrop.open { opacity: 1; pointer-events: auto; }

.settings-panel {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
  display: flex; flex-direction: column;
  max-height: 82vh; max-height: 82dvh;
  background: var(--card);
  border-top: 1px solid var(--border);
  border-radius: 18px 18px 0 0;
  padding-bottom: var(--safe-b);
  transform: translateY(102%);
  transition: transform .25s ease;
  box-shadow: 0 -10px 40px rgba(0, 0, 0, .5);
}
.settings-panel.open { transform: translateY(0); }
.settings-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--border); flex: 0 0 auto;
}
.settings-header h3 { margin: 0; font-size: 16px; }
.settings-body {
  display: flex; flex-direction: column; gap: 14px;
  padding: 16px; overflow-y: auto; -webkit-overflow-scrolling: touch;
}
.setting-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  font-size: 14.5px; color: var(--text);
}
.setting-row > span { flex: 0 0 auto; color: var(--muted); }
.switch {
  appearance: none; width: 46px; height: 26px; border-radius: 999px;
  background: #333c4d; border: 1px solid var(--border); position: relative;
  cursor: pointer; transition: background var(--transition); flex: 0 0 auto;
}
.switch::after {
  content: ''; position: absolute; top: 2px; left: 2px; width: 20px; height: 20px;
  border-radius: 50%; background: #fff; transition: transform var(--transition);
}
.switch:checked { background: var(--primary); }
.switch:checked::after { transform: translateX(20px); }

.setting-row select {
  flex: 0 1 auto; min-width: 0; max-width: 64%;
  padding: 10px; font-size: 16px; font-family: inherit;
  color: var(--text); background: var(--bg-2);
  border: 1px solid var(--border); border-radius: 10px;
  appearance: none;
}

/* ── Toast ── */
.toast {
  position: fixed; left: 50%; z-index: 100;
  bottom: calc(100px + var(--safe-b));
  max-width: 88vw;
  padding: 12px 18px;
  font-size: 14px; text-align: center; color: #fff;
  background: #222a36; border: 1px solid rgba(255, 255, 255, .1);
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .5);
  opacity: 0; transform: translate(-50%, 12px); pointer-events: none;
  transition: opacity .25s ease, transform .25s ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* 桌面端：限制视频区高度，避免超宽屏拉得过扁 */
@media (min-width: 900px) and (orientation: landscape) {
  .local-pip { width: 22%; max-width: 210px; aspect-ratio: 3 / 4; }
  .stats-bar { max-width: 40%; }
}

/* ── 矮视口（手机横屏 / 小窗）紧凑模式 ──
   新增"剪贴板导入"按钮后卡片变高，844×390 横屏会挤到视口外。
   这里在高度不足时隐藏装饰性文案、压缩间距与控件高度，保证主按钮可见可点。 */
@media (max-height: 560px) {
  .landing { align-items: flex-start; padding-top: calc(8px + var(--safe-t)); }
  .landing-card { padding: 14px 14px 12px; border-radius: 14px; }
  .logo { gap: 4px; }
  .logo svg { width: 28px; height: 28px; }
  .logo h1 { font-size: 17px; }
  .subtitle, .hint { display: none; }
  .conn-status-bar { margin-bottom: 10px; padding: 6px 10px; }
  .landing-actions { gap: 8px; }
  .btn { min-height: 44px; font-size: 15px; }
  .btn-lg { min-height: 46px; }
  .btn-ghost { min-height: 40px; font-size: 14px; }
  .input-room { min-height: 44px; padding: 10px 12px; }
  .status-text { margin-top: 8px; min-height: 16px; font-size: 12px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
