* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; font-family: -apple-system, "Segoe UI", "Noto Sans CJK TC", sans-serif; background: #1e1e26; color: #e4e4e7; }
#app { height: 100%; display: flex; flex-direction: column; }
header { display: flex; align-items: center; gap: 12px; padding: 8px 16px; background: #111118; border-bottom: 1px solid #2a2a35; }
header .brand { font-weight: 600; font-size: 19px; }
header #user-info { margin-left: auto; font-size: 16px; color: #a1a1aa; }
header button { background: #2a2a35; color: #e4e4e7; border: none; padding: 6px 12px; border-radius: 4px; cursor: pointer; }
header button:hover { background: #3a3a45; }
.conn { font-size: 14px; padding: 2px 8px; border-radius: 10px; }
.conn.online { background: #064e3b; color: #6ee7b7; }
.conn.offline { background: #4b1d1d; color: #fca5a5; }

#login-panel { max-width: 320px; margin: 80px auto; padding: 24px; background: #111118; border: 1px solid #2a2a35; border-radius: 8px; display: flex; flex-direction: column; gap: 10px; }
#login-panel input { padding: 8px; background: #1e1e26; color: #e4e4e7; border: 1px solid #2a2a35; border-radius: 4px; }
#login-panel button { padding: 8px; background: #4f46e5; color: #fff; border: none; border-radius: 4px; cursor: pointer; }
#login-panel .otp-row { display: flex; gap: 8px; }
#login-panel .otp-row input { flex: 1; }
#login-panel .otp-row button { flex-shrink: 0; font-size: 14px; padding: 8px 12px; white-space: nowrap; }
#login-panel .otp-row button:disabled { background: #3a3a45; cursor: not-allowed; }
#login-mode-switch { text-align: center; font-size: 14px; color: #a1a1aa; }
#login-mode-switch a { color: #818cf8; cursor: pointer; text-decoration: underline; }
#login-panel .err { color: #fca5a5; font-size: 14px; }
.hidden { display: none !important; }

#main { flex: 1; display: flex; min-height: 0; }
#sidebar { width: 280px; background: #111118; border-right: 1px solid #2a2a35; display: flex; flex-direction: column; }
.sidebar-head { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; border-bottom: 1px solid #2a2a35; font-weight: 600; }
.sidebar-head button { background: transparent; color: #a1a1aa; border: none; font-size: 19px; cursor: pointer; }
#worker-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
#worker-list li { padding: 10px 14px; border-bottom: 1px solid #1f1f29; cursor: pointer; display: flex; gap: 10px; align-items: flex-start; }
#worker-list li:hover { background: #1a1a23; }
#worker-list li.active { background: #232336; }
#worker-list .avatar { width: 36px; height: 36px; border-radius: 50%; background: #2a2a35; flex-shrink: 0; display: flex; align-items: center; justify-content: center; font-size: 17px; color: #a1a1aa; overflow: hidden; }
#worker-list .avatar img { width: 100%; height: 100%; object-fit: cover; }
#worker-list .meta { flex: 1; min-width: 0; }
#worker-list .name { font-size: 16px; font-weight: 600; display: flex; gap: 6px; align-items: center; }
#worker-list .preview { font-size: 13px; color: #a1a1aa; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#worker-list .badge { background: #dc2626; color: #fff; border-radius: 10px; padding: 1px 7px; font-size: 12px; font-weight: 600; margin-left: auto; }
#worker-list .warlord { font-size: 12px; color: #fbbf24; }
#worker-list .state-dot { width: 6px; height: 6px; border-radius: 50%; background: #6ee7b7; display: inline-block; }
#worker-list .state-dot.locked { background: #fbbf24; }
/* 2026-05-08: villager 三段可摺疊 — sidebar section header.
   套到 <li class="worker-section-header"> 上；裡面是 <button class="section-toggle">。
   為什麼用 <button>：a11y（鍵盤可達 + screen reader 自動讀 expanded 狀態）。
   舊樣式（純文字 header）保留，但 cursor: default → pointer 並加 hover。
   2026-05-08 後續修：明確 display: block 覆寫上面 #worker-list li 的 display: flex /
   gap / border-bottom — 否則 wrapper li 變 flex container，inner ul 變 flex item
   寬度被收成 max-content，worker 行渲染窄，且 #worker-list li:hover 在 wrapper
   也會匹配，間接讓 .row-actions hover 規則失靈（desktop 看不到 ⋯ 按鈕）。 */
#worker-list .worker-section-header {
  list-style: none;
  padding: 0;
  display: block;
  border-bottom: none;
  cursor: default;
  gap: 0;
}
#worker-list .worker-section-header .section-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  background: transparent;
  border: none;
  color: #52525b;
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 10px 14px 4px;
  cursor: pointer;
  text-align: left;
}
#worker-list .worker-section-header .section-toggle:hover {
  color: #a1a1aa;
}
#worker-list .worker-section-header .section-toggle:focus-visible {
  outline: 2px solid #6366f1;
  outline-offset: -2px;
  border-radius: 4px;
}
#worker-list .worker-section-header .section-caret {
  display: inline-block;
  font-size: 10px;
  color: #71717a;
  transition: transform 0.15s ease;
}
#worker-list .worker-section-header .section-toggle[aria-expanded="true"] .section-caret {
  transform: rotate(90deg);
}
#worker-list .worker-section-header .section-emoji {
  font-size: 14px;
  font-style: normal;
  text-transform: none; /* 避免 OS fallback 把 emoji 當文字大寫處理 */
}
#worker-list .worker-section-header .section-label {
  /* 文字 — 跟 emoji 拆開避免 fallback 顯示成方框 */
}
#worker-list .worker-section-header .section-count {
  margin-left: auto;
  font-size: 11px;
  color: #71717a;
  font-weight: 500;
}

/* Section content — collapsed 用 max-height + overflow:hidden，避免 height auto 跳動。
   2026-05-08 後續修：強制 display: block + border-bottom: none + cursor: default
   覆寫繼承自 #worker-list li 的 flex / border / cursor，否則：
   * wrapper li 變 flex 容器 → inner ul 寬度收成 max-content
   * #worker-list li:hover 多匹配到 wrapper li，使 row-actions 的 hover 顯示
     規則語意被稀釋（desktop ⋯ 按鈕看不到）
   * inherit 來的 border-bottom 變成多餘的分隔線 */
#worker-list .section-content {
  list-style: none;
  padding: 0;
  margin: 0;
  overflow: hidden;
  transition: max-height 0.18s ease;
  max-height: 4000px; /* expanded 時夠大；用大數字而非 none，才能 transition */
  display: block;
  border-bottom: none;
  cursor: default;
  gap: 0;
}
#worker-list .section-content.collapsed {
  max-height: 0;
}
#worker-list .section-content-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: block;
  width: 100%;
}

/* Per-row action menu (⋯ button → dropdown) */
.sidebar-head-btns { display: flex; gap: 4px; }
.sidebar-head-btns button { background: transparent; color: #a1a1aa; border: none; font-size: 19px; cursor: pointer; padding: 2px 6px; border-radius: 4px; }
.sidebar-head-btns button:hover { background: #232336; color: #e4e4e7; }

#worker-list li .row-actions {
  margin-left: auto;
  display: none;
  background: transparent;
  color: #a1a1aa;
  border: none;
  cursor: pointer;
  font-size: 22px;
  padding: 2px 6px;
  border-radius: 4px;
  align-self: center;
}
#worker-list li:hover .row-actions,
#worker-list li.menu-open .row-actions { display: inline-block; }
#worker-list li .row-actions:hover { background: #2a2a35; color: #e4e4e7; }

.row-menu {
  position: absolute;
  background: #1a1a23;
  border: 1px solid #353545;
  border-radius: 6px;
  padding: 4px 0;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.5);
  z-index: 1500;
  min-width: 140px;
  font-size: 16px;
}
.row-menu button {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  color: #e4e4e7;
  border: none;
  padding: 8px 14px;
  cursor: pointer;
}
.row-menu button:hover { background: #232336; }
.row-menu button.danger { color: #fca5a5; }
.row-menu button.danger:hover { background: #3a1f1f; }

/* Create-worker modal */
.cw-reload-btn {
  background: transparent; border: none; color: #52525b; font-size: 18px;
  cursor: pointer; padding: 0; vertical-align: middle;
  margin-left: 6px; transition: color .15s;
}
.cw-reload-btn:hover { color: #a1a1aa; }
.cw-reload-btn:disabled { opacity: .35; cursor: default; }
.cw-filter {
  width: 100%;
  padding: 8px 10px;
  background: #1e1e26;
  color: #e4e4e7;
  border: 1px solid #2a2a35;
  border-radius: 4px;
  font-size: 16px;
  margin-bottom: 10px;
}
.cw-profile-list { list-style: none; margin: 0; padding: 0; max-height: 360px; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; }
.cw-profile-list li {
  padding: 10px 12px;
  border-radius: 6px;
  background: #1a1a23;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.cw-profile-list li:hover { background: #232336; }
.cw-profile-list .cw-name { font-size: 16px; font-weight: 600; color: #e4e4e7; }
.cw-profile-list .cw-meta { font-size: 13px; color: #71717a; }
.cw-profile-list .cw-scope-shared { color: #6ee7b7; }
.cw-profile-list li.cw-disabled { opacity: 0.35; pointer-events: none; }
.cw-profile-list li.cw-loading { background: #2a2a3e; outline: 1px solid #4f46e5; }
.cw-spinner {
  margin-left: 8px;
  font-weight: 400;
  font-size: 14px;
  color: #a5b4fc;
  animation: cw-pulse 1.2s ease-in-out infinite;
}
@keyframes cw-pulse {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 1; }
}
#create-worker-modal.busy { cursor: progress; }

/* --- Profile list: section headers (2026-05-08 villager + collapse) --- */
/* 高 specificity（用 .cw-profile-list li.cw-section-header）覆寫上面
   .cw-profile-list li 的 padding/background/cursor — 我們的 wrapper li
   不應該長得像可點擊的 profile row。 */
.cw-profile-list li.cw-section-header,
.cw-profile-list li.cw-section-content {
  background: transparent;
  cursor: default;
  display: block;
  padding: 0;
  border-radius: 0;
  flex-direction: initial;
  gap: 0;
}
.cw-profile-list li.cw-section-header:hover,
.cw-profile-list li.cw-section-content:hover {
  background: transparent;
}
.cw-section-header {
  list-style: none;
  padding: 0;
  border-bottom: 1px solid #27272a;
}
.cw-section-header .section-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  background: transparent;
  border: none;
  color: #71717a;
  font: inherit;
  font-size: 14px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 8px 4px 4px;
  cursor: pointer;
  text-align: left;
}
.cw-section-header .section-toggle:hover {
  color: #a1a1aa;
}
.cw-section-header .section-toggle:focus-visible {
  outline: 2px solid #6366f1;
  outline-offset: -2px;
  border-radius: 4px;
}
.cw-section-header .section-caret {
  display: inline-block;
  font-size: 10px;
  transition: transform 0.15s ease;
}
.cw-section-header .section-toggle[aria-expanded="true"] .section-caret {
  transform: rotate(90deg);
}
.cw-section-header .section-emoji {
  font-size: 14px;
  font-style: normal;
  text-transform: none;
}
.cw-section-header .section-count {
  margin-left: auto;
  font-size: 11px;
  color: #71717a;
  font-weight: 500;
}

/* WARN: 外層 .cw-profile-list 是 flex column container（為了 gap）+ max-height: 360px。
   本元素含 overflow:hidden（collapse 動畫需要），會觸發 W3C Flexbox L1 §4.5 的
   min-height:auto=0 連鎖，讓 wrapper 被 squeeze 成 0 → 列表內容裁切、scrollbar
   不出現（使用者無法滾到 villager 段最後幾個 profile）。必須 flex-shrink: 0 鎖死。
   移除前先確認 .cw-profile-list 是否也改回 block。
   見 diagnoses/2026-05-08-cw-modal-no-scroll.md */
.cw-section-content {
  list-style: none;
  padding: 0;
  margin: 0;
  overflow: hidden;
  transition: max-height 0.18s ease;
  max-height: 4000px;
  flex-shrink: 0;
}
.cw-section-content.collapsed {
  max-height: 0;
}
.cw-section-content-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

/* --- Profile list: two-button layout --- */
.cw-profile-list li {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}
.cw-info { display: flex; flex-direction: column; gap: 2px; min-width: 0; flex: 1; }
.cw-actions { display: flex; gap: 6px; flex-shrink: 0; }
.cw-btn {
  padding: 4px 10px;
  border: 1px solid #3f3f5a;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  background: transparent;
  color: #a1a1aa;
  white-space: nowrap;
}
.cw-btn:hover { background: #2a2a3e; }
.cw-btn-enroll { border-color: #4f46e5; color: #a5b4fc; }
.cw-btn-enroll:hover { background: #4f46e5; color: #fff; }
.cw-btn-dismiss { border-color: #dc2626; color: #fca5a5; }
.cw-btn-dismiss:hover { background: #dc2626; color: #fff; }
.cw-btn-loadout { border-color: #0d9488; color: #5eead4; }
.cw-btn-loadout:hover:not(:disabled) { background: #0d9488; color: #fff; }
.cw-btn-loadout:disabled { border-color: #374151; color: #6b7280; cursor: not-allowed; opacity: .5; }

/* --- Loadout overview (inside profile list) --- */
.lo-overview { cursor: default !important; flex-direction: column !important; }
.lo-overview:hover { background: #1a1a23 !important; }
.loadout-overview-title { font-size: 17px; font-weight: 700; color: #e4e4e7; margin-bottom: 10px; }
.lo-section { margin-bottom: 10px; }
.lo-section-header { display: flex; justify-content: space-between; align-items: center; font-size: 16px; color: #a1a1aa; margin-bottom: 4px; }
.lo-items { display: flex; flex-direction: column; gap: 2px; padding-left: 4px; }
.lo-item { font-size: 14px; color: #d4d4d8; padding: 2px 0; }
.lo-item.global { color: #71717a; }
.lo-lock { font-size: 13px; margin-right: 2px; }
.lo-empty { font-size: 14px; color: #52525b; font-style: italic; }
.lo-footer { margin-top: 8px; text-align: right; }

/* --- Loadout edit modal --- */
.loadout-modal-box { max-width: 580px; width: 92vw; max-height: 88vh; display: flex; flex-direction: column; }
.loadout-modal-body { flex: 1; overflow-y: auto; margin: 10px 0; min-height: 0; }
.loadout-modal-footer { text-align: right; padding-top: 8px; border-top: 1px solid #27272a; }
.loadout-modal-footer .btn-primary {
  padding: 6px 20px;
  border: none;
  border-radius: 4px;
  background: #4f46e5;
  color: #fff;
  font-size: 16px;
  cursor: pointer;
}
.loadout-modal-footer .btn-primary:hover { background: #4338ca; }

.lo-edit-section { margin-bottom: 12px; }
.lo-edit-title { font-size: 14px; font-weight: 700; color: #71717a; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 6px; border-bottom: 1px solid #27272a; padding-bottom: 4px; }
.lo-edit-item { display: flex; justify-content: space-between; align-items: center; padding: 5px 8px; border-radius: 4px; }
.lo-edit-item:hover { background: #232336; }
.lo-edit-item.global { opacity: 0.6; }
.lo-edit-info { display: flex; align-items: center; gap: 6px; min-width: 0; flex: 1; }
.lo-edit-name { font-size: 16px; color: #e4e4e7; }
.lo-edit-cat { font-size: 13px; color: #52525b; }
.lo-edit-tag { font-size: 12px; color: #6ee7b7; border: 1px solid #065f46; border-radius: 3px; padding: 1px 4px; }

.lo-cat-group { margin-bottom: 4px; }
.lo-cat-header { font-size: 14px; color: #a1a1aa; cursor: pointer; padding: 4px 6px; border-radius: 4px; user-select: none; }
.lo-cat-header:hover { background: #232336; }
.lo-cat-chevron { display: inline-block; transition: transform 0.15s; font-size: 13px; }
.lo-cat-group.collapsed .lo-cat-chevron { transform: rotate(-90deg); }
.lo-cat-group.collapsed .lo-cat-items { display: none; }
.lo-cat-count { color: #52525b; font-size: 13px; }
.lo-cat-items { padding-left: 8px; }

/* --- Loadout AI 設定 --- */
.lo-ai-config { background: #1a1a23; border: 1px solid #2a2a35; border-radius: 6px; padding: 10px 12px; margin-bottom: 12px; }
.lo-ai-title { font-size: 14px; font-weight: 700; color: #a5b4fc; margin-bottom: 8px; }
.lo-ai-row { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }
.lo-ai-row label { width: 80px; font-size: 13px; color: #a1a1aa; }
.lo-ai-row select { flex: 1; padding: 6px 8px; background: #1e1e26; color: #e4e4e7; border: 1px solid #2a2a35; border-radius: 4px; font-size: 14px; }
.lo-ai-hint { font-size: 12px; color: #71717a; margin-top: 6px; font-style: italic; }

/* --- Loadout tab bar --- */
.lo-tabs {
  display: flex;
  gap: 0;
  border-bottom: 2px solid #27272a;
  margin-bottom: 10px;
}
.lo-tab {
  flex: 1;
  background: transparent;
  color: #71717a;
  border: none;
  padding: 8px 4px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: color 0.15s, border-color 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}
.lo-tab:hover { color: #a1a1aa; background: #1a1a23; }
.lo-tab.active {
  color: #e4e4e7;
  border-bottom-color: #4f46e5;
}
.lo-tab-count {
  font-size: 13px;
  color: #52525b;
  background: #1e1e26;
  border-radius: 8px;
  padding: 1px 6px;
  font-weight: 500;
}
.lo-tab.active .lo-tab-count {
  color: #a5b4fc;
  background: #2a2a4e;
}

/* Edit-worker modal */
#edit-worker-modal .modal-box { min-width: 360px; }
.ew-label { display: block; font-size: 14px; color: #a1a1aa; margin: 10px 0 4px; }
#edit-worker-modal input {
  width: 100%;
  padding: 8px 10px;
  background: #1e1e26;
  color: #e4e4e7;
  border: 1px solid #2a2a35;
  border-radius: 4px;
  font-size: 16px;
}
.ew-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }
.ew-actions button {
  padding: 8px 16px;
  background: #2a2a35;
  color: #e4e4e7;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}
.ew-actions button.primary { background: #4f46e5; color: #fff; }
.ew-actions button:hover { filter: brightness(1.15); }

/* M8: viewers stack */
#worker-list .viewers-mini { display: flex; align-items: center; gap: -2px; margin-left: 4px; }
#worker-list .viewers-mini .v-dot { width: 14px; height: 14px; border-radius: 50%; background: #4f46e5; color: #fff; font-size: 11px; font-weight: 700; display: flex; align-items: center; justify-content: center; border: 1.5px solid #111118; margin-left: -4px; }
#worker-list .viewers-mini .v-dot.overflow { background: #6b7280; }

.viewers { display: flex; align-items: center; gap: 6px; font-size: 14px; color: #a1a1aa; }
.viewers.hidden { display: none; }
.viewers .v-dot { width: 20px; height: 20px; border-radius: 50%; background: #4f46e5; color: #fff; font-size: 13px; font-weight: 700; display: flex; align-items: center; justify-content: center; border: 2px solid #111118; margin-left: -6px; }
.viewers .v-dot:first-child { margin-left: 0; }
.viewers .v-dot.overflow { background: #6b7280; }
.viewers .v-label { margin-left: 4px; }

#thread { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
#thread-head {
  padding: 12px 16px;
  border-bottom: 1px solid #353545;
  font-weight: 600;
  display: flex;
  gap: 16px;
  align-items: center;
  background: #161620;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
  position: relative;
  z-index: 2;
}
#thread-title { flex-shrink: 0; }

/* M6: Warlord card in thread header */
#warlord-card { display: flex; gap: 10px; align-items: center; margin-left: auto; padding: 6px 12px; background: #1a1a23; border: 1px solid #2a2a35; border-radius: 8px; cursor: pointer; transition: background 0.15s; }
#warlord-card:hover { background: #232336; }
#warlord-card .wl-meta { display: flex; flex-direction: column; gap: 2px; min-width: 120px; }
#warlord-card .wl-name { font-size: 14px; font-weight: 600; color: #e4e4e7; }
#warlord-card .wl-level { font-size: 12px; color: #fbbf24; font-weight: 500; }
#warlord-card .wl-might { font-size: 13px; color: #fca5a5; }
#warlord-card .wl-bar { width: 120px; height: 4px; background: #0b0b10; border-radius: 2px; overflow: hidden; }
#warlord-card .wl-bar > div { height: 100%; background: linear-gradient(90deg, #4f46e5, #f472b6); transition: width 0.4s ease; width: 0; }

/* Rarity border */
#warlord-card.rarity-C { border-color: #71717a; }
#warlord-card.rarity-B { border-color: #60a5fa; }
#warlord-card.rarity-A { border-color: #a78bfa; }
#warlord-card.rarity-S { border-color: #fbbf24; box-shadow: 0 0 8px rgba(251,191,36,0.3); }
#warlord-card.rarity-epic { border-color: #f472b6; box-shadow: 0 0 10px rgba(244,114,182,0.4); }
#warlord-card.rarity-legendary { border-color: #f59e0b; box-shadow: 0 0 14px rgba(245,158,11,0.5); }

/* Sidebar warlord mini avatar */
#worker-list .avatar.rarity-S, #worker-list .avatar.rarity-epic { box-shadow: 0 0 6px rgba(251,191,36,0.5); }
#worker-list .avatar.rarity-legendary { box-shadow: 0 0 8px rgba(245,158,11,0.6); }
#worker-list .wl-line { font-size: 12px; color: #fbbf24; }
#worker-list .wl-might { color: #fca5a5; }

/* Modal */
.modal { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; z-index: 1000; }
.modal.hidden { display: none; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0.6); }
.modal-box { position: relative; background: #18181f; border: 1px solid #2a2a35; border-radius: 10px; padding: 24px 28px; min-width: 320px; max-width: 520px; max-height: 90vh; overflow-y: auto; }
.modal-close { position: absolute; top: 8px; right: 12px; background: transparent; border: none; color: #a1a1aa; font-size: 29px; cursor: pointer; }
.modal-box h2 { margin: 0 0 12px; font-size: 22px; }
.wm-row { display: flex; justify-content: space-between; font-size: 16px; padding: 4px 0; border-bottom: 1px solid #222; }
.wm-label { color: #a1a1aa; }
#wm-radar { display: block; margin: 16px auto 8px; }
#wm-rubric { font-size: 14px; color: #a1a1aa; margin-top: 8px; }
#wm-rubric .rubric-row { display: flex; justify-content: space-between; padding: 2px 0; }
/* Toast */
#toast-stack { position: fixed; top: 60px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 2000; pointer-events: none; }
.toast { background: #1a1a23; color: #fbbf24; border: 1px solid #fbbf24; border-radius: 6px; padding: 10px 16px; font-size: 16px; font-weight: 600; box-shadow: 0 4px 12px rgba(0,0,0,0.4); animation: toast-in 1.5s ease forwards; }
.toast.might-up { color: #fca5a5; border-color: #fca5a5; }
.toast.title-unlock { color: #a78bfa; border-color: #a78bfa; }
@keyframes toast-in {
  0%   { opacity: 0; transform: translateX(30px) scale(0.9); }
  15%  { opacity: 1; transform: translateX(0) scale(1.05); }
  25%  { transform: translateX(0) scale(1); }
  85%  { opacity: 1; }
  100% { opacity: 0; transform: translateX(30px); }
}
/* --- Permission prompt quick-action buttons --- */
.cr-permission-prompt { margin: 8px 0; padding: 12px 14px; background: #2a2520; border: 1px solid #78580a; border-radius: 10px; }
.cr-prompt-question { font-weight: 600; color: #fbbf24; margin-bottom: 10px; font-size: 17px; line-height: 1.4; }
.cr-prompt-options { display: flex; flex-wrap: wrap; gap: 8px; }
.cr-prompt-btn { background: #1a2a3a; color: #93c5fd; border: 1px solid #334155; border-radius: 6px; padding: 8px 16px; font-size: 16px; cursor: pointer; transition: all .15s; }
.cr-prompt-btn:hover { background: #2563eb; color: #fff; border-color: #2563eb; }
.cr-prompt-btn.cr-prompt-selected { background: #1e3a5f; border-color: #3b82f6; color: #fff; }
.cr-prompt-btn.cr-prompt-cancel { background: #2a1a1a; color: #fca5a5; border-color: #7f1d1d; }
.cr-prompt-btn.cr-prompt-cancel:hover { background: #dc2626; color: #fff; border-color: #dc2626; }
.cr-prompt-answered { color: #4ade80; font-weight: 600; font-size: 16px; padding: 4px 0; }
@media (max-width: 600px) {
  .cr-prompt-options { flex-direction: column; }
  .cr-prompt-btn { width: 100%; text-align: left; }
}

/* Compact group members strip: first name + "+N" overflow badge. Clicking
   the strip opens the #members-modal with the full list. */
#thread-members {
  padding: 6px 16px;
  border-bottom: 1px solid #2a2a35;
  background: #15151c;
  font-size: 14px;
  color: #a1a1aa;
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  user-select: none;
  transition: background 0.15s;
}
#thread-members:hover { background: #1a1a23; }
#thread-members.hidden { display: none; }
#thread-members .mm-icon { color: #71717a; font-size: 16px; }
#thread-members .mm-first { color: #e4e4e7; font-weight: 500; }
#thread-members .mm-rest {
  color: #a1a1aa;
  background: #232336;
  padding: 1px 8px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 600;
}
#thread-members .mm-chev { color: #52525b; margin-left: auto; font-size: 17px; }

/* Members modal list */
#mm-list { list-style: none; margin: 12px 0 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
#mm-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 6px;
  background: #1a1a23;
}
#mm-list li.me { background: #2a2a3f; }
#mm-list .mm-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #4f46e5;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 17px;
  font-weight: 600;
  flex-shrink: 0;
}
#mm-list .mm-name { font-size: 16px; color: #e4e4e7; font-weight: 500; }
#mm-list .mm-me-tag { margin-left: auto; color: #a78bfa; font-size: 13px; }
#mm-count { font-size: 16px; color: #a1a1aa; font-weight: 400; margin-left: 6px; }

#thread-body { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; }
.bubble { max-width: 72%; padding: 8px 12px; border-radius: 10px; font-size: 16px; line-height: 1.5; word-wrap: break-word; }
.bubble.user { align-self: flex-end; background: #4f46e5; color: #fff; }
.bubble.assistant { align-self: flex-start; background: #353545; color: #e4e4e7; }
.bubble p { margin: 0; }
.bubble p + p { margin-top: 8px; }
.bubble.system { align-self: center; background: #1f2937; color: #9ca3af; font-size: 13px; font-style: italic; }
.bubble.tool_use { align-self: flex-start; background: #1f2937; color: #60a5fa; border-left: 3px solid #60a5fa; font-family: monospace; font-size: 14px; }
.bubble.tool_result { align-self: flex-start; background: #1f2937; color: #86efac; border-left: 3px solid #86efac; font-family: monospace; font-size: 14px; white-space: pre-wrap; }
.bubble pre { background: #0b0b10; padding: 8px; border-radius: 4px; overflow-x: auto; }
.bubble code { background: #0b0b10; padding: 1px 4px; border-radius: 3px; font-size: 14px; }
.bubble.user pre, .bubble.user code { background: rgba(0,0,0,0.25); }
.bubble .ts { display: block; font-size: 12px; opacity: 0.6; margin-top: 4px; }

/* MQ queue status bar */
#queue-status { padding: 6px 16px; background: #1a1a23; border-top: 1px solid #2a2a35; font-size: 14px; color: #a1a1aa; }
#queue-status.hidden { display: none; }
/* 置底按鈕 */
#scroll-to-bottom-btn {
  position: absolute;
  bottom: 80px;
  right: 16px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #4f46e5;
  color: #fff;
  font-size: 20px;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0,0,0,0.4);
  transition: opacity 0.2s;
  z-index: 10;
}
#scroll-to-bottom-btn:hover { background: #6366f1; }
#scroll-to-bottom-btn.hidden { display: none; }

/* Sender name in user bubbles */
.bubble .sender-name { font-size: 13px; font-weight: 600; color: #818cf8; margin-bottom: 2px; }

/* Reply tag in AI bubbles */
.bubble .reply-tag { font-size: 12px; color: #6ee7b7; margin-bottom: 4px; }

/* 2026-05-13 chatroom 三狀態 bubble 生命週期（diagnosis 2026-05-13-chatroom-debounce-autosend.md）
   State 1 .pending : 灰色虛線 + 倒計時 + 取消按鈕
   State 2 .queued  : user bubble 樣式 + 排隊位置 + 取消按鈕
   State 3 .delivered : 正常 user bubble（無 footer） */
.bubble.user.pending {
  background: #2a2a35;           /* 與 user 紫不同，灰色 */
  color: #c4c4cf;
  border: 1px dashed #818cf8;    /* 虛線輪廓 */
  opacity: 0.85;
  /* 2026-05-14 v1.2: PENDING bubble 移除時 fade-out（diagnosis P2 風險表）
     —— 還原路徑同步移除 DOM，這個 transition 在 bubble 還未實際 remove 之前的
     瞬間提供視覺平滑；不影響 JS 行為（DOM 仍 synchronous remove）。 */
  transition: opacity 150ms ease-out;
}
.bubble.user.queued {
  background: #4f46e5;            /* 同 .bubble.user 紫 */
  color: #fff;
  border: 1px solid #6366f1;
}
.bubble.user.delivered {
  background: #4f46e5;
  color: #fff;
}
.bubble .bubble-text { word-wrap: break-word; }
.bubble .bubble-footer {
  margin-top: 6px;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  opacity: 0.85;
}
.bubble .pending-countdown {
  font-weight: 600;
  color: #fbbf24;                 /* amber/yellow — 醒目 */
  min-width: 1.5em;
  text-align: right;
}
.bubble .queue-pos {
  color: #6ee7b7;                 /* green-200 */
}
.bubble .bubble-cancel-btn {
  margin-left: auto;
  background: transparent;
  border: 1px solid currentColor;
  color: inherit;
  border-radius: 3px;
  padding: 1px 8px;
  font-size: 12px;
  cursor: pointer;
  line-height: 1.4;
}
.bubble .bubble-cancel-btn:hover {
  background: rgba(255, 255, 255, 0.08);
}

#input-form { display: flex; gap: 0; padding: 10px 16px; border-top: 1px solid #2a2a35; background: #111118; align-items: center; }
#input-form > *:not(:first-child) { margin-left: 8px; }
#input-form.input-focused > .icon-btn { margin-left: 0; }
#input-form .icon-btn {
  background: #2a2a35;
  color: #e4e4e7;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 22px;
  padding: 6px 10px;
  height: 38px;
  flex-shrink: 0;
  overflow: hidden;
  transition: max-width .25s ease, padding .25s ease, opacity .2s ease, margin .25s ease;
  max-width: 50px;
}
#input-form.input-focused .icon-btn {
  max-width: 0 !important;
  padding: 0 !important;
  margin: 0 !important;
  opacity: 0;
  pointer-events: none;
  border: none;
}
#input-form .icon-btn:hover:not(:disabled) { background: #3a3a45; }
#input-form .icon-btn:disabled { opacity: 0.4; cursor: not-allowed; }
#input-form #mic-btn.recording { background: #dc2626; animation: mic-pulse 1.2s infinite; }
@keyframes mic-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(220, 38, 38, 0.6); }
  50% { box-shadow: 0 0 0 6px rgba(220, 38, 38, 0); }
}
#input-form textarea { flex: 1 1 0; min-width: 0; width: 0; resize: none; padding: 8px; background: #1e1e26; color: #e4e4e7; border: 1px solid #2a2a35; border-radius: 4px; font-family: inherit; font-size: 16px; height: 38px; }
#input-form button { padding: 0 20px; background: #4f46e5; color: #fff; border: none; border-radius: 4px; cursor: pointer; flex-shrink: 0; height: 38px; }
#input-form #send-btn { min-width: 72px; }
#input-form button:disabled { background: #3a3a45; cursor: not-allowed; }

/* ---------- Mobile (single-pane) layout ---------- */
.mobile-only { display: none; }

#thread-back-btn {
  background: transparent;
  color: #e4e4e7;
  border: none;
  cursor: pointer;
  font-size: 17px;
  padding: 4px 10px 4px 4px;
  display: none;
  align-items: center;
  gap: 2px;
  border-radius: 6px;
}
#thread-back-btn:hover { background: #232336; }
#thread-back-btn .back-arrow {
  font-size: 31px;
  line-height: 1;
  color: #a1a1aa;
  font-weight: 300;
}
#thread-back-btn #thread-back-label { font-size: 16px; }
#thread-back-btn .back-unread {
  background: #dc2626;
  color: #fff;
  border-radius: 10px;
  padding: 1px 7px;
  font-size: 12px;
  font-weight: 600;
  margin-left: 6px;
}

@media (max-width: 768px) {
  /* Hard constrain the whole app to the viewport — no horizontal scroll,
     and pin to dynamic viewport height so mobile URL bar doesn't cut the
     input box off the bottom of the screen. */
  html, body {
    overflow: hidden;
    width: 100%;
    max-width: 100vw;
    height: var(--vvh, 100dvh);
    max-height: var(--vvh, 100dvh);
    position: fixed;
    inset: 0;
  }
  #app {
    width: 100vw;
    max-width: 100vw;
    height: var(--vvh, 100dvh);
    max-height: var(--vvh, 100dvh);
    overflow: hidden;
  }
  #main {
    height: auto;
    min-height: 0;
    flex: 1 1 auto;
    width: 100vw;
    max-width: 100vw;
    min-width: 0;
  }
  #sidebar {
    width: 100vw;
    max-width: 100vw;
    min-width: 0;
    box-sizing: border-box;
  }
  #thread {
    width: 100vw;
    max-width: 100vw;
    min-width: 0;
    box-sizing: border-box;
    overflow: hidden;
  }
  #thread-head {
    flex-shrink: 0;
    min-width: 0;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
    flex-wrap: wrap;
    position: sticky;
    top: 0;
    z-index: 5;
    background: #161620;
  }
  #thread-head > * { min-width: 0; }
  #thread-members { flex-shrink: 0; width: 100%; max-width: 100%; box-sizing: border-box; }
  #thread-body {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
  }
  #thread-body .bubble { max-width: 88%; box-sizing: border-box; }
  #thread-body .bubble img { max-width: 100%; }
  #thread-body .bubble table { display: block; overflow-x: auto; max-width: 100%; }
  #input-form { flex-shrink: 0; }
  #main.show-thread #thread {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
    flex: 1 1 auto;
  }

  header { padding: 6px 10px; gap: 8px; }
  header .brand { font-size: 17px; }
  header #user-info { font-size: 13px; }
  header button { padding: 4px 8px; font-size: 14px; }

  #main {
    position: relative;
    width: 100%;
    min-width: 0;
    overflow-x: hidden;
  }
  #sidebar { width: 100%; min-width: 0; border-right: none; }
  #thread {
    display: none;
    width: 100%;
    min-width: 0;
    max-width: 100vw;
    overflow-x: hidden;
  }

  /* When a thread is selected, sidebar slides out and thread takes the
     whole screen. Back button returns to the list view. */
  #main.show-thread #sidebar { display: none; }
  #main.show-thread #thread { display: flex; }

  #thread-back-btn { display: inline-flex; }

  /* Warlord card + viewers take too much room on phones — compact. */
  #warlord-card { display: none !important; }
  #thread-head { padding: 10px 12px; gap: 8px; }
  #thread-title {
    flex: 1;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 17px;
  }

  #thread-members { padding: 6px 12px; }
  #thread-body {
    padding: 10px;
    min-width: 0;
    width: 100%;
    max-width: 100%;
  }
  .bubble {
    max-width: 88%;
    font-size: 17px;
  }
  /* Long code blocks inside a bubble scroll inside themselves. */
  .bubble pre {
    max-width: 100%;
    overflow-x: auto;
    white-space: pre;
    word-break: normal;
  }
  .bubble pre code { white-space: pre; }

  #input-form {
    padding: 8px 10px;
    width: 100%;
    max-width: 100%;
    min-width: 0;
  }
  #input-form textarea {
    /* iOS Safari auto-zooms when focusing inputs with font-size < 16px,
       which permanently shifts the viewport and breaks our layout widths.
       Keep this at 16px on mobile to suppress the zoom. */
    font-size: 19px;
    min-width: 0;
    max-width: 100%;
    height: 44px;
  }
  #input-form button { padding: 0 16px; flex-shrink: 0; font-size: 18px; height: 44px; }
  #input-form #send-btn { min-width: 64px; }

  /* Worker list rows a touch taller for finger taps. */
  #worker-list li { padding: 14px 14px; }
  #worker-list .name { font-size: 17px; }
  #worker-list .preview { font-size: 14px; }

  /* Loadout modal: near-fullscreen on mobile */
  .loadout-modal-box {
    width: 100vw;
    max-width: 100vw;
    max-height: var(--vvh, 100dvh);
    height: var(--vvh, 100dvh);
    border-radius: 0;
    padding: 16px;
    margin: 0;
  }
  .loadout-modal-body { max-height: none; }
  .lo-tab { font-size: 17px; padding: 10px 4px; }
  .lo-edit-item { padding: 8px 10px; }
  .lo-edit-name { font-size: 17px; }

  /* 2026-05-08: 一般 modal-box（含「登錄武將」、「編輯武將」、「群組成員」）
     在 mobile 的尺寸對齊 — 解兩個 mobile bug：
     (1) 既有 .modal-box { max-height: 90vh } 在 mobile Safari URL bar
         收合時會跟著跳動，其他元素已切 var(--vvh, 100dvh)，唯獨這個沒跟上。
     (2) iPhone SE 寬度 320px 時 min-width: 320px + padding 24+28=372px 會
         溢出畫面（因 modal-box 預設 min-width: 320px）。
     登錄武將 modal 的 .cw-profile-list 同步把 max-height 從 360px 提到
     60vh，讓 30+ profile 環境在小螢幕看得多一些。
     見 diagnoses/2026-05-08-cw-modal-no-scroll.md follow-up #3 */
  .modal-box {
    max-width: calc(100vw - 16px);
    max-height: var(--vvh, 100dvh);
    min-width: 0;
    width: 100%;
    box-sizing: border-box;
  }
  .cw-profile-list { max-height: 60vh; }
}

/* ---- Edge rate-limit badges (single footer below warrior list) ---- */
/* 5h/7d is per-edge (shared by all warriors), so it's shown once here. */
.server-metrics {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid #2a2a35;
  background: #111118;
  font-size: 14px;
  color: #cbd5e1;
}
.server-metrics .sm-label {
  color: #a1a1aa;
  font-size: 12px;
  margin-right: auto;
}
.server-metrics .sm-metric {
  background: #2a2a35;
  padding: 4px 10px;
  border-radius: 4px;
  font-variant-numeric: tabular-nums;
  font-weight: 500;
  border: 1px solid #3a3a45;
}
.server-metrics .sm-metric[data-pct^="5"],
.server-metrics .sm-metric[data-pct^="6"],
.server-metrics .sm-metric[data-pct^="7"] { color: #fbbf24; border-color: #d97706; }
.server-metrics .sm-metric[data-pct^="8"],
.server-metrics .sm-metric[data-pct^="9"],
.server-metrics .sm-metric[data-pct="100"] { color: #f87171; border-color: #dc2626; font-weight: 600; }
.server-metrics[data-stale="true"] { opacity: 0.4; }

/* ---- Per-session context badge (thread head) ---- */
button.ctx-pct,
.ctx-pct {
  font-size: 14px;
  margin-left: 10px;
  padding: 3px 10px;
  background: #eef2ff;
  color: #4f46e5;
  border-radius: 4px;
  font-variant-numeric: tabular-nums;
  font-weight: 500;
  vertical-align: middle;
  border: 1px solid transparent;
  cursor: pointer;
  font-family: inherit;
  line-height: 1.4;
  transition: filter 0.15s, border-color 0.15s;
}
button.ctx-pct:hover,
.ctx-pct:hover {
  filter: brightness(0.95);
  border-color: currentColor;
}
button.ctx-pct:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}
.ctx-pct[data-pct^="5"] { background: #fef3c7; color: #b45309; }
.ctx-pct[data-pct^="6"],
.ctx-pct[data-pct^="7"] { background: #fef3c7; color: #b45309; }
.ctx-pct[data-pct^="8"],
.ctx-pct[data-pct^="9"],
.ctx-pct[data-pct="100"] { background: #fee2e2; color: #b91c1c; font-weight: 600; }
.ctx-pct[data-stale="true"] { opacity: 0.4; }

/* 2026-05-06 W2-step5: chatroom Web Push permission banner */
.chatroom-push-banner {
  position: fixed;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-elev, #ffffff);
  color: var(--text, #111);
  border: 1px solid var(--border, #ddd);
  border-radius: 12px;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  box-shadow: 0 4px 14px rgba(0,0,0,0.15);
  z-index: 9999;
  max-width: calc(100vw - 32px);
}
.chatroom-push-banner .cta {
  background: var(--accent-primary, #4f8cff);
  color: white;
  border: none;
  padding: 4px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
}
.chatroom-push-banner .cta:hover { opacity: 0.85; }
.chatroom-push-banner .dismiss {
  background: transparent;
  color: var(--text-secondary, #666);
  border: none;
  cursor: pointer;
  font-size: 16px;
  padding: 0 4px;
}
.chatroom-push-banner .dismiss:hover { color: var(--text, #111); }
