/* 聊天、跳出通知、表情、舞台右上角按鈕（Task 2.2） */
:root {
  --chat-dock-h: 112px; /* 聊天分頁底部：表情列 + 輸入列的高度（直式時讓畫圖 FAB 讓位） */
}

/* ---- 聊天分頁：紀錄可捲動，表情列與輸入列固定在底部 ---- */
/* .chat-active / .chat-tab 由 chat-ui.js 依聊天分頁是否開啟切換（不依賴 :has()） */
.panels.chat-active {
  display: flex; flex-direction: column; overflow: hidden; padding-bottom: 0;
}
.chat-panel { flex: 1; min-height: 0; display: flex; flex-direction: column; padding-top: 0; padding-bottom: 0; }
.chat-view { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.chat-scroll { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 8px 0; }
.chat-head { flex: none; display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 2px 2px; font-size: 13px; color: var(--fg-2); }
.chat-head .seg, .stage-chat-toasts .seg { flex: none; min-height: 32px; min-width: 76px; padding: 0 12px; }
.stage-chat-toasts .seg { min-height: var(--tap); }
.chat-head-label { display: inline-flex; align-items: center; gap: 6px; min-width: 0; }
.chat-head-label .icon { width: 16px; height: 16px; flex: none; }
.stage-chat-toasts { display: flex; align-items: center; gap: 8px; margin-left: auto; font-size: 13px; color: var(--fg-2); white-space: nowrap; }
.chat-empty { color: var(--fg-2); text-align: center; padding: 24px 0; font-size: 14px; }
.chat-log { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.chat-msg { padding: 0 2px; }
.chat-meta { display: flex; align-items: baseline; gap: 8px; font-size: 13px; }
.chat-name { font-weight: 600; color: var(--fg); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-time { color: var(--fg-2); font-size: 11px; flex: none; }
.chat-text { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 15px; }
.chat-msg[data-mine="true"] .chat-text { color: #cfe0ff; }
.chat-sys {
  display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 4px 8px;
  text-align: center; color: var(--fg-2); font-size: 13px; padding: 0 8px;
}
.chat-link {
  display: inline-flex; align-items: center; min-height: var(--tap); padding: 0 12px;
  color: var(--accent); text-decoration: underline;
}

.emoji-bar {
  flex: none; display: flex; gap: 2px; padding: 2px 0;
  overflow-x: auto; scrollbar-width: none; overscroll-behavior: contain;
}
.emoji-bar::-webkit-scrollbar { display: none; }
.emoji-btn {
  flex: 1 0 44px; min-width: 44px; height: 44px; padding: 0; border: 0; border-radius: 10px;
  background: transparent; font-size: 22px; line-height: 1; cursor: pointer; touch-action: manipulation;
  font-family: "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji", sans-serif;
}
.emoji-btn:active { background: var(--bg-3); transform: scale(.9); }
.chat-form { flex: none; display: flex; align-items: center; gap: 6px; padding: 4px 0 8px; }
.chat-input { flex: 1; min-width: 0; }
.chat-send { flex: none; color: var(--accent); }

/* 直式：聊天分頁開啟時，畫圖 FAB 移到輸入列上方 */
.room[data-layout="portrait"].chat-tab .fab-draw {
  bottom: calc(var(--tabbar-h) + var(--safe-b) + var(--chat-dock-h) + 8px);
}

/* ---- 聊天跳出通知（仿 Google Meet）----
   聊天頁（.floating）：固定在視窗角落、不依附版面容器 → 桌機右下、手機左下（右下是畫圖鍵）。
   錄影檢視頁（唯讀）：留在舞台內右下（recording.css）。 */
.chat-toasts {
  position: absolute; z-index: 13; right: 10px; bottom: 12px; width: min(320px, calc(100% - 20px));
  display: flex; flex-direction: column; align-items: stretch; gap: 6px; pointer-events: none;
}
/* 在抽屜（80/81）之上、一般 toast（90）之下；--toast-lift：桌機落在舞台上時避開播放器控制列（chat-ui.js） */
.chat-toasts.floating {
  position: fixed; z-index: 85; left: auto; top: auto;
  right: calc(16px + var(--safe-r)); bottom: calc(16px + var(--safe-b) + var(--toast-lift, 0px));
  width: min(320px, calc(100vw - 32px));
}
.chat-toasts.readonly .chat-toast { cursor: default; pointer-events: none; }
.chat-toast {
  display: flex; align-items: flex-start; gap: 10px; padding: 8px 12px 8px 8px; border-radius: 12px;
  background: rgba(32, 34, 40, .96); color: #fff; box-shadow: 0 4px 16px rgba(0, 0, 0, .45);
  cursor: pointer; pointer-events: auto; touch-action: manipulation;
  animation: toast-in .2s ease-out; transition: opacity .4s, transform .18s ease-out;
}
/* 可滑掉（朝通知欄所靠的側邊／向下）：拖曳由 chat-toasts.js 以 pointer 事件處理，不讓瀏覽器拿去捲動或選字 */
.chat-toasts:not(.readonly) .chat-toast { touch-action: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.chat-toast.dragging { transition: none; cursor: grabbing; }
.chat-toast.swiped { transition: opacity .18s ease-out, transform .18s ease-out; pointer-events: none; }
.chat-toast:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.chat-toast.fade { opacity: 0; }
.chat-toast.leaving { animation: ui-out-down .18s ease-in forwards; }
.toast-avatar {
  flex: none; display: inline-flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; border-radius: 50%; background: var(--accent); color: #fff;
  font-size: 15px; font-weight: 600; text-transform: uppercase;
}
.toast-body { min-width: 0; flex: 1; }
.toast-name { font-size: 12px; font-weight: 600; color: #cfd3dc; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.toast-text {
  font-size: 14px; line-height: 1.35; overflow: hidden; overflow-wrap: anywhere;
  display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2;
}
.toast-sys { padding: 5px 12px; background: rgba(60, 63, 72, .92); color: #c2c6d0; justify-content: center; text-align: center; }
.toast-sys .toast-text { font-size: 12px; }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ---- 表情：從舞台下方隨機 x 位置飄起 2.5 秒 ---- */
.reaction-layer { position: absolute; inset: 0; z-index: 11; pointer-events: none; overflow: hidden; }
.reaction {
  position: absolute; bottom: 0; font-size: 34px; line-height: 1; translate: -50% 0;
  font-family: "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji", sans-serif;
  animation: reaction-float 2.5s ease-out forwards; will-change: transform, opacity;
}
/* 只動 transform/opacity（合成層）；--rise = 舞台高度的 85%（由 reactions.js 依圖層高度設定） */
@keyframes reaction-float {
  0% { opacity: 0; transform: translateY(100%) scale(.6); }
  12% { opacity: 1; transform: translateY(calc(var(--rise, -200px) * .1)) scale(1.15); }
  70% { opacity: 1; }
  100% { opacity: 0; transform: translate(var(--drift, 0px), var(--rise, -200px)) scale(1); }
}
@keyframes reaction-fade {
  0% { opacity: 0; transform: scale(.9); }
  15% { opacity: 1; transform: scale(1); }
  75% { opacity: 1; }
  100% { opacity: 0; }
}

/* ---- 舞台右上角：表情、聊天紀錄、全螢幕 ---- */
.stage-controls { position: absolute; z-index: 12; top: 6px; right: 6px; display: flex; gap: 4px; }
.stage-btn {
  position: relative; display: inline-flex; align-items: center; justify-content: center;
  width: 44px; height: 44px; padding: 0; border: 0; border-radius: 50%;
  background: rgba(0, 0, 0, .45); color: #fff; cursor: pointer; touch-action: manipulation;
}
.stage-btn .icon { width: 22px; height: 22px; }
.stage-btn.on, .stage-btn[aria-pressed="true"] { background: var(--accent); }
.stage-btn:active { filter: brightness(1.3); }
.fs-icon { display: inline-flex; }
.stage-dot {
  position: absolute; top: 7px; right: 7px; width: 9px; height: 9px; border-radius: 50%;
  background: var(--danger); border: 1.5px solid #000;
}
.stage-emoji {
  position: absolute; z-index: 12; top: 56px; right: 6px; max-width: calc(100% - 12px);
  padding: 4px; border-radius: 14px; background: rgba(17, 19, 24, .82);
}
.stage-emoji { transform-origin: top right; animation: ui-pop-in .15s ease-out; }
.stage-emoji.closing { animation: ui-pop-out .15s ease-in forwards; }
.stage-emoji .emoji-bar { flex-wrap: wrap; justify-content: flex-end; overflow: visible; padding: 0; }
.stage-emoji .emoji-btn { flex: 0 0 44px; }

/* ---- 舞台容器內的完整聊天紀錄（全螢幕時仍可使用）---- */
.stage-chat {
  position: absolute; z-index: 40; top: 0; right: 0; bottom: 0; width: min(360px, 50%);
  display: flex; flex-direction: column;
  padding: 0 calc(10px + var(--safe-r)) calc(2px + var(--safe-b)) 12px;
  background: rgba(17, 19, 24, .9); border-left: 1px solid var(--line);
  -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px);
}
/* 開啟從右側滑入、關閉反向滑出（直式：從下方） */
.stage-chat { animation: stage-chat-in .22s ease-out; }
.stage-chat.closing { animation: stage-chat-out .2s ease-in forwards; }
@keyframes stage-chat-in { from { translate: 100% 0; } }
@keyframes stage-chat-out { to { translate: 100% 0; } }
.stage-chat-head { flex: none; display: flex; align-items: center; justify-content: space-between; min-height: 48px; }
.stage-chat-head h2 { font-size: 16px; }
@media (orientation: portrait) {
  .stage-chat {
    top: auto; left: 0; width: auto; height: 55%;
    padding-left: calc(12px + var(--safe-l));
    border-left: 0; border-top: 1px solid var(--line); border-radius: 16px 16px 0 0;
  }
  .stage-chat { animation-name: stage-chat-in-up; }
  .stage-chat.closing { animation-name: stage-chat-out-down; }
}
@keyframes stage-chat-in-up { from { translate: 0 100%; } }
@keyframes stage-chat-out-down { to { translate: 0 100%; } }

/* ---- 手機鍵盤（visualViewport）：輸入列留在鍵盤上方 ---- */
.room.kb-panel { position: fixed; left: 0; right: 0; top: var(--vvt, 0px); height: var(--vvh, 100dvh); }
/* 直式空間不夠：打字時收起頂部列、底部導覽、表情列與畫圖 FAB */
.room.kb-panel .topbar, .room.kb-panel .side > .tabbar, .room.kb-panel .fab-draw,
.room.kb-panel .chat-panel .emoji-bar, .room.kb-panel .chat-panel .chat-head { display: none; }
body.kb-panel-open .sheet-tall { bottom: var(--kb, 0px); height: var(--vvh, 92dvh); max-height: var(--vvh, 92dvh); }
.room.kb-stage .stage-chat { bottom: var(--kb, 0px); max-height: var(--vvh, 100%); }
.room.kb-stage .stage-chat .emoji-bar { display: none; }

/* ---- 桌機 ---- */
@media (min-width: 900px) and (min-height: 541px) {
  .emoji-btn { flex: 1 0 38px; min-width: 38px; height: 40px; font-size: 20px; }
  .stage-emoji .emoji-btn { flex: 0 0 40px; }
  .stage-chat { bottom: var(--boardbar-h); }
}

/* ---- 手機：通知在視窗左下。直式在底部導覽列之上（不蓋住分頁）；右側留給畫圖鍵 ---- */
@media not all and (min-width: 900px) and (min-height: 541px) {
  .chat-toasts { width: min(300px, calc(100% - 16px)); }
  .chat-toasts.floating {
    right: auto; left: calc(8px + var(--safe-l));
    bottom: calc(var(--tabbar-h) + var(--safe-b) + 8px);
    width: min(300px, calc(100vw - 96px - var(--safe-l) - var(--safe-r)));
  }
  .chat-head .seg { min-height: var(--tap); } /* 手機：可點區 ≥ 44px */
  .chat-toast { padding: 6px 10px 6px 6px; }
  .toast-avatar { width: 28px; height: 28px; font-size: 13px; }
  .toast-text { font-size: 13px; }
  /* 全螢幕（沒有底部導覽列）：貼近底邊；舞台置中，下方是空的 */
  .stage-wrap.fs > .chat-toasts.floating { bottom: calc(12px + var(--safe-b)); }
}
/* 手機橫式（含橫式全螢幕）：舞台佔滿畫面，底部 74px 是播放器的時間／齒輪／全螢幕與進度條（player.css .pc-mbottom），
   中央是上一部／播放／下一部 → 通知放在底部控制之上，寬度限制在中央按鈕左側 */
@media (orientation: landscape) and (max-height: 540px) {
  .chat-toasts.floating, .stage-wrap.fs > .chat-toasts.floating {
    bottom: calc(80px + var(--safe-b));
    width: min(240px, calc(50vw - 170px - var(--safe-l)));
  }
}

/* 戰術板窗格佔住通知的位置（chat-ui.js placeToasts）：改放在窗格步驟列的上方；.single：只顯示最新一則 */
.chat-toasts.floating.over-pane { bottom: var(--toast-bottom, 80px); }
.chat-toasts.floating.over-pane.pane-left { left: var(--toast-left, 8px); right: auto; }
.chat-toasts.floating.single .chat-toast:not(:last-child) { display: none; }
/* 手機橫式並排（placeToasts）：畫面左邊、影片窗格內 */
.chat-toasts.floating.ls-split { left: var(--toast-left, 8px); right: auto; bottom: var(--toast-bottom, 80px); width: var(--toast-w, 240px); }

/* ---- 減少動態效果：表情原地淡出、浮層不滑入 ---- */
@media (prefers-reduced-motion: reduce) {
  .reaction { bottom: 14%; animation-name: reaction-fade; }
  .chat-toast, .chat-toast.leaving { animation: none; transition: none; }
  .stage-emoji, .stage-emoji.closing, .stage-chat, .stage-chat.closing { animation: none; }
}

/* ---- 自動淡出（ui/idle.js 在 .room 加上 .ui-idle；桌機、直式、橫式相同）：
   有影片時（播放或暫停中）無操作 → 舞台按鈕、面板按鈕與播放器控制列一起淡出；聊天通知保留 ---- */
.stage-btn, .fab { transition: opacity .25s, visibility .25s; }
.room.ui-idle .stage-btn,
.room.ui-idle .fab {
  visibility: hidden; opacity: 0;
}
/* 滑鼠裝置：淡出後不可點，但仍可用 Tab 聚焦（聚焦後整組出現，與播放器控制列一致） */
@media (hover: hover) and (pointer: fine) {
  .room.ui-idle .stage-btn { visibility: visible; pointer-events: none; }
}

/* 橫式「開啟面板」浮動按鈕上的未讀紅點 */
.fab > .fab-dot { top: 8px; right: 8px; border-color: rgba(36, 40, 51, .85); }

/* 閒置時的點擊攔截層（ui/idle.js、chat-ui.js）：播放器、畫布（含畫圖模式 z 8）之上，浮層與按鈕之下。
   只給觸控：滑鼠移到舞台上就會叫出控制項，點擊照常傳到播放器 */
.idle-catcher {
  display: none; position: absolute; inset: 0; z-index: 9; background: transparent; touch-action: manipulation;
  /* 長按畫面＝2 倍速（player-controls.js）：不跳出系統選單、不選取文字 */
  -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent;
}
.room.ui-idle .idle-catcher, .idle-catcher.holding { display: block; }
@media (hover: hover) and (pointer: fine) {
  .room.ui-idle .idle-catcher, .idle-catcher.holding { display: none; }
}
/* 疑似 YouTube 廣告（播放器控制層穿透中）：攔截層也讓開，「略過廣告」第一下就點得到 */
.room.ui-idle .stage-wrap:not(.drawing) .stage.pc-pass .idle-catcher { display: none; }

/* 減少動態：閒置隱藏/出現的控制項不淡入淡出（只在 reduced-motion 時取消轉場） */
@media (prefers-reduced-motion: reduce) {
  .stage-btn, .fab, .board-toolbar, .fab-draw,
  .room.ui-idle .stage-btn, .room.ui-idle .fab,
  .room.ui-idle .board-toolbar, .room.ui-idle .fab-draw,
  .board-toolbar.idle, .fab-draw.idle { transition: none; }
}

/* 舞台聊天紀錄開著時不顯示「開啟面板」浮動按鈕（橫式時會蓋住送出鍵） */
.room.stage-log-open .fab { display: none; }
