/* 聊天厅 + 私信 (#/chat, js/pages/chat.js) and the shell parts of chat: top-bar entry with the DM unread badge,
   side-nav badge / hall dot, and the site popup (app.js).
   The page root gets .chat-on-dm on the 私信 tab: below 1280 px the side cards give their width to the DM view. */

/* ---------- Shell ---------- */
.topbar-msg { position: relative; flex: none; }
/* 44 × 44 hit area around the 36 px button without changing the layout (neighbours are ≥ 10 px away). */
.topbar-msg::before { content: ''; position: absolute; inset: -5px; }
/* Badge red darker than --alert so white text keeps ≥ 4.5:1 (#D92D2D: 4.8:1); shared by top bar, side nav, tabs. */
.msg-badge {
  display: inline-flex; align-items: center; justify-content: center; min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px; background: #D92D2D; color: #fff; font-family: var(--mono); font-size: 11px; font-weight: 700;
  line-height: 1; font-style: normal;
}
.topbar-msg .msg-badge { position: absolute; top: -7px; right: -7px; box-shadow: 0 0 0 2px var(--bg); }
.nav-item .msg-badge { margin-left: auto; }
.chat-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); display: inline-block; flex: none; }
.topbar-msg .chat-dot { position: absolute; top: 5px; right: 5px; box-shadow: 0 0 0 2px var(--bg); }

/* Site popup (04 §11.3): text set with textContent, own /uploads/ image only. */
.modal.site-popup { width: min(460px, 100%); }
.site-popup .modal-body { display: flex; flex-direction: column; gap: 14px; }
.popup-img { display: block; max-width: 100%; max-height: 50vh; object-fit: contain; border-radius: var(--r-2); align-self: center; }
.popup-text { white-space: pre-wrap; overflow-wrap: anywhere; font-size: var(--fs-md); line-height: 1.75; color: var(--text); }
.popup-act { display: flex; justify-content: flex-end; }

/* ---------- Page ---------- */
.pg-chat .page { gap: var(--sp-3); }
.pg-chat .chat-head { display: flex; align-items: flex-end; justify-content: space-between; gap: var(--sp-4); flex-wrap: wrap; }
.pg-chat .eyebrow { font-size: 12px; color: var(--accent); letter-spacing: .1em; }
.pg-chat .chat-title-row { display: flex; align-items: center; gap: 10px; }
.pg-chat .chat-title h1 { font-size: var(--fs-2xl); font-weight: 700; line-height: 1.25; }
.pg-chat .chat-sample .tag { height: 22px; }
.pg-chat .chat-lead { font-size: var(--fs-sm); color: var(--text-2); margin-top: 4px; }
.pg-chat .chat-head-r { display: flex; align-items: center; gap: var(--sp-2); }
.pg-chat .chat-tabs a { position: relative; }
.pg-chat .chat-tabs [aria-current="page"] .msg-badge { background: var(--on-accent); color: var(--accent); }
.pg-chat .chat-tabs [aria-current="page"] .chat-dot { background: var(--on-accent); }
.pg-chat .chat-rules-btn { display: none; }

.pg-chat .chat-grid { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: var(--sp-4); align-items: start; }
.pg-chat .chat-main {
  display: flex; flex-direction: column; position: relative; overflow: hidden;
  height: clamp(460px, calc(100vh - var(--top-h) - 150px), 920px);
}
.pg-chat .chat-side { display: flex; flex-direction: column; gap: var(--sp-3); position: sticky; top: calc(var(--top-h) + 16px); }

/* Hall */
.pg-chat .chat-ph { justify-content: space-between; flex: none; flex-wrap: nowrap; }
.pg-chat .chat-ph .meta { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pg-chat .chat-rule {
  flex: none; display: flex; align-items: center; gap: 8px; padding: 8px 14px;
  font-size: 12px; line-height: 1.5; color: var(--text-2); background: var(--panel-3); border-bottom: 1px solid var(--line-soft);
}
.pg-chat .chat-rule .ico { color: var(--accent); }
.pg-chat .chat-line { flex: none; padding: 8px 14px; font-size: var(--fs-sm); line-height: 1.5; border-bottom: 1px solid var(--line-soft); }
.pg-chat .chat-line.is-notice { color: var(--accent); background: var(--accent-soft); }
.pg-chat .chat-line.is-mute { color: var(--alert-soft); background: var(--alert-bg); font-family: var(--font); font-variant-numeric: tabular-nums; }
.pg-chat .chat-line.is-info { color: var(--text-2); background: var(--panel-3); }
.pg-chat .chat-scroll { flex: 1; min-height: 0; overflow-y: auto; padding: 10px 14px 16px; overscroll-behavior: contain; }
.pg-chat .chat-more-wrap { display: flex; justify-content: center; }
.pg-chat .chat-more { margin: 2px 0 10px; }
.pg-chat .chat-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 12px; }
.chat-msg { display: flex; gap: 10px; align-items: flex-start; }
.chat-av {
  flex: none; width: 32px; height: 32px; border-radius: var(--r-2); display: inline-flex; align-items: center; justify-content: center;
  font-size: 14px; font-weight: 700; color: hsl(var(--av-h, 36) 85% 72%); background: hsl(var(--av-h, 36) 32% 17%);
}
.chat-bubble-wrap { min-width: 0; max-width: min(78%, 640px); display: flex; flex-direction: column; gap: 4px; }
.chat-msg-head { display: flex; align-items: baseline; gap: 8px; font-size: 12px; min-width: 0; }
.chat-name { color: var(--text-2); font-weight: 500; max-width: 16em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-time { color: var(--text-3); font-size: 11px; flex: none; }
.chat-text {
  padding: 8px 12px; border-radius: 2px var(--r-3) var(--r-3) var(--r-3); background: var(--panel-2); border: 1px solid var(--line);
  font-size: var(--fs-md); line-height: 1.6; color: var(--text); white-space: pre-wrap; overflow-wrap: anywhere;
}
.chat-msg.is-mine { flex-direction: row-reverse; }
.chat-msg.is-mine .chat-bubble-wrap { align-items: flex-end; }
.chat-msg.is-mine .chat-msg-head { flex-direction: row-reverse; }
.chat-msg.is-mine .chat-name { color: var(--accent); }
.chat-msg.is-mine .chat-text { background: var(--accent-soft); border-color: var(--accent-line); border-radius: var(--r-3) 2px var(--r-3) var(--r-3); }
.chat-day { display: flex; align-items: center; gap: 10px; font-size: 12px; color: var(--text-3); }
.chat-day::before, .chat-day::after { content: ''; flex: 1; height: 1px; background: var(--line-soft); }
.pg-chat .chat-ph-box { display: flex; justify-content: center; padding: 36px 0; color: var(--text-3); font-size: var(--fs-sm); }
.chat-ph-t { display: inline-flex; align-items: center; gap: 6px; }
.pg-chat .chat-newpill {
  position: absolute; left: 50%; bottom: 78px; transform: translateX(-50%); z-index: 2;
  display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 14px; border-radius: 16px;
  border: 1px solid var(--accent); background: var(--panel); color: var(--accent); font-size: 12px; font-weight: 700;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .45);
}
.pg-chat .chat-compose {
  flex: none; display: flex; align-items: flex-end; gap: 8px; padding: 10px 14px;
  border-top: 1px solid var(--line); background: var(--panel-3);
}
.pg-chat .chat-compose .input { flex: 1; min-width: 0; }
/* One line fits exactly (20 + 2 × 11 + 2 × 1 = 44); chat.js grows it to 132 px and only then lets it scroll. */
.pg-chat .chat-compose textarea.input {
  height: 44px; min-height: 44px; max-height: 132px; padding: 11px 12px; line-height: 20px;
  overflow-y: hidden; resize: none; font-family: inherit;
}
.pg-chat .chat-compose .input:disabled { opacity: .65; cursor: not-allowed; }
.pg-chat .chat-count { flex: none; align-self: center; min-width: 52px; text-align: right; font-size: 11px; color: var(--text-3); }
.pg-chat .chat-count.is-over { color: var(--alert-soft); }
.pg-chat .chat-send { flex: none; height: 44px; }

/* DMs */
.pg-chat .chat-dm { flex: 1; min-height: 0; display: grid; grid-template-columns: 280px minmax(0, 1fr); }
.pg-chat .dm-list-col { border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.pg-chat .dm-threads { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; min-height: 0; }
.pg-chat .dm-row { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--line-soft); color: var(--text); }
.pg-chat .dm-row:hover { background: var(--panel-2); color: var(--text); }
.pg-chat .dm-row.is-active { background: var(--accent-soft); box-shadow: inset 2px 0 0 var(--accent); }
.pg-chat .dm-row-mid { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.pg-chat .dm-row-top { display: flex; align-items: center; gap: 6px; min-width: 0; }
.pg-chat .dm-title { font-size: var(--fs-sm); flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pg-chat .dm-row-time { font-size: 11px; color: var(--text-3); flex: none; }
.pg-chat .dm-preview { font-size: 12px; color: var(--text-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pg-chat .dm-row.is-unread .dm-preview { color: var(--text-2); }
.pg-chat .dm-tag { height: 18px; padding: 0 5px; flex: none; }
.pg-chat .dm-ph { padding: 28px 14px; text-align: center; color: var(--text-3); font-size: var(--fs-sm); }
.pg-chat .dm-empty { display: flex; gap: 10px; padding: 18px 14px; color: var(--text-2); font-size: 12px; line-height: 1.6; }
.pg-chat .dm-empty strong { display: block; color: var(--text); font-size: var(--fs-sm); }
.pg-chat .dm-empty .ico { width: 24px; height: 24px; color: var(--text-3); }
.pg-chat .dm-thread-col { display: flex; flex-direction: column; min-height: 0; min-width: 0; container: dmthread / inline-size; }
/* Narrow thread column: the reply box keeps the room (maxlength still caps the text). */
@container dmthread (max-width: 440px) { .pg-chat .chat-compose.is-dm .chat-count { display: none; } }
.pg-chat .dm-thead { justify-content: flex-start; flex-wrap: nowrap; }
.pg-chat .dm-thead-t { font-size: var(--fs-md); font-weight: 700; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pg-chat .dm-back { display: none; width: 32px; height: 32px; flex: none; }
.pg-chat .dm-hint { flex: none; margin: 0; padding: 0 14px 8px; font-size: var(--fs-xs); color: var(--text-3); background: var(--panel-3); text-align: right; }

/* Side cards */
.pg-chat .chat-card .panel-head { min-height: 40px; }
.chat-status { display: flex; flex-direction: column; margin: 0; padding: 4px 14px 10px; }
.chat-status > div { display: flex; justify-content: space-between; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--line-soft); font-size: var(--fs-sm); }
.chat-status > div:last-child { border-bottom: 0; }
.chat-status dt { color: var(--text-3); flex: none; }
.chat-status dd { margin: 0; color: var(--text); text-align: right; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-status .chat-st-ok { color: var(--live); }
.chat-status .chat-st-bad { color: var(--alert-soft); font-variant-numeric: tabular-nums; }
.chat-status .chat-st-off { color: var(--text-2); }
.pg-chat .chat-card-body { padding: 10px 14px 14px; }
.chat-rules { margin: 0; padding-left: 18px; display: flex; flex-direction: column; gap: 8px; font-size: 12px; line-height: 1.7; color: var(--text-2); }
.chat-rules li::marker { color: var(--accent); font-family: var(--mono); }
.chat-rules-modal .chat-status { padding: 0 0 12px; margin-bottom: 14px; border-bottom: 1px solid var(--line); }
.chat-help { margin: 12px 0 0; font-size: 12px; color: var(--text-2); display: flex; align-items: center; gap: 4px; }
.chat-help .csw-link { display: inline-flex; align-items: center; min-height: 44px; padding: 0 4px; font-size: var(--fs-sm); }
@media (min-width: 768px) { .chat-help .csw-link { min-height: 28px; } }

/* Guest */
.pg-chat .chat-guest { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 12px; padding: 36px 24px; max-width: 640px; width: 100%; margin: 0 auto; }
.pg-chat .chat-guest-ico { width: 52px; height: 52px; border-radius: 14px; display: inline-flex; align-items: center; justify-content: center; background: var(--accent-soft); color: var(--accent); }
.pg-chat .chat-guest-ico .ico { width: 28px; height: 28px; }
.pg-chat .chat-guest h2 { font-size: var(--fs-xl); }
.pg-chat .chat-guest p { font-size: var(--fs-sm); color: var(--text-2); line-height: 1.7; max-width: 30em; }
.pg-chat .chat-guest-act { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
.pg-chat .chat-guest-rules { width: 100%; text-align: left; margin-top: 8px; padding: 14px 16px; border: 1px solid var(--line); border-radius: var(--r-2); background: var(--panel-3); }
.pg-chat .chat-guest-rules strong { display: block; font-size: var(--fs-sm); margin-bottom: 8px; }

@media (pointer: coarse) { .pg-chat .dm-hint { display: none; } }
@media (max-width: 1279px) { .pg-chat .chat-dm { grid-template-columns: 240px minmax(0, 1fr); } }
@media (max-width: 1199px) { .pg-chat .chat-grid { grid-template-columns: minmax(0, 1fr) 260px; } }
/* 私信 tab at 1024–1279 px: the side cards go (the 规则 button shows them in a dialog), the DM view takes the width. */
@media (min-width: 1024px) and (max-width: 1279px) {
  .pg-chat.chat-on-dm .chat-grid { grid-template-columns: minmax(0, 1fr); }
  .pg-chat.chat-on-dm .chat-side { display: none; }
  .pg-chat.chat-on-dm .chat-rules-btn { display: inline-flex; }
}
@media (max-width: 1023px) {
  .pg-chat .chat-grid { grid-template-columns: minmax(0, 1fr); }
  .pg-chat .chat-side { display: none; }
  .pg-chat .chat-rules-btn { display: inline-flex; }
  .pg-chat .chat-main { height: calc(100dvh - var(--top-h) - var(--tab-h) - var(--safe-b) - 150px); min-height: 380px; }
}
@media (max-width: 767px) {
  .pg-chat .chat-lead, .pg-chat .eyebrow { display: none; }
  .pg-chat .chat-head { align-items: center; flex-wrap: nowrap; gap: var(--sp-2); }
  .pg-chat .chat-title { min-width: 0; }
  /* The tabs name the view on the phone: the h1 stays for screen readers only, the hall panel head goes. */
  .pg-chat .chat-title h1 { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
  .pg-chat .chat-head-r { flex: 1; justify-content: flex-end; }
  .pg-chat .chat-tabs a { padding: 0 14px; }
  .pg-chat .chat-tabs .ico { display: none; }
  .pg-chat .chat-ph-hall { display: none; }
  .pg-chat .chat-rules-btn { width: 44px; height: 44px; }
  .pg-chat .chat-main {
    height: calc(100dvh - var(--top-h) - var(--tab-h) - var(--safe-b) - 74px); min-height: 340px;
    margin: 0 calc(var(--page-pad) * -1); border-radius: 0; border-left: 0; border-right: 0;
  }
  .pg-chat .chat-ph .meta { display: none; }
  .pg-chat .chat-dm { grid-template-columns: minmax(0, 1fr); }
  .pg-chat .dm-list-col { border-right: 0; }
  .pg-chat .chat-dm.has-peer .dm-list-col { display: none; }
  .pg-chat .chat-dm:not(.has-peer) .dm-thread-col { display: none; }
  .pg-chat .dm-thead { padding-top: 0; padding-bottom: 0; padding-left: 0; }
  .pg-chat .dm-thead > h2 { padding-left: 14px; } /* 「会话」 state (no back link yet): keep off the screen edge */
  .pg-chat .dm-back { display: inline-flex; width: 44px; height: 44px; border-color: transparent; }
  .chat-bubble-wrap { max-width: 82%; }
  .pg-chat .chat-scroll { padding: 8px 12px 12px; }
  .pg-chat .chat-compose { padding: 8px 12px; }
  /* 16 px text: iOS Safari does not zoom the page on focus; one textarea line still fits 44 px (22 + 2 × 10 + 2). */
  .pg-chat .chat-compose .input { font-size: 16px; }
  .pg-chat .chat-compose textarea.input { padding: 10px 12px; line-height: 22px; }
  .pg-chat .chat-count { display: none; }
  .pg-chat .chat-rule { padding: 6px 12px; }
  .pg-chat .chat-newpill { bottom: 70px; height: 44px; border-radius: 22px; }
  .pg-chat .chat-guest { padding: 28px 16px; }
}
