/* FR-CHAT-01~05: 모바일 메신저 관례 — 상단 내 대화명, 기록, 한 줄 입력줄만 남긴다.
   디자이너 채팅 바 메모에 따라 패널 내부는 평면으로, 색과 간격은 mobile.css 토큰을 쓴다. */
.chat-panel {
  position: fixed;
  inset: auto;
  top: calc(var(--chat-viewport-top, 0px) + var(--space-2) + env(safe-area-inset-top, 0px));
  left: var(--space-2);
  width: calc(100% - var(--space-4));
  height: calc(var(--chat-viewport-height, 100dvh) - var(--space-4) - env(safe-area-inset-top, 0px));
  max-width: none;
  max-height: none;
  margin: 0;
  padding: 0;
  color: var(--text);
  background: var(--bg);
  border: 0;
  border-radius: var(--radius-lg);
  overflow: hidden;
}
.chat-panel[open] {
  display: flex;
  flex-direction: column;
  animation: chat-panel-in 0.46s cubic-bezier(0.22, 1, 0.36, 1);
}
.chat-panel::backdrop {
  background: var(--scrim);
  animation: chat-backdrop-in 0.3s ease both;
  transition: opacity 0.34s ease;
}
.chat-panel.chat-closing::backdrop { opacity: 0; }
/* 등장 애니메이션은 transform만 움직이고 fill을 두지 않는다. opacity 키프레임과 fill both를
   함께 쓰면 백그라운드 탭의 애니메이션 클럭이 멈출 때 요소가 보이지 않는 상태에 갇히고
   (2026-10-04 채팅 QA에서 적발), fill이 남으면 끌어서 닫기의 인라인 transform을 덮는다. */
@keyframes chat-panel-in {
  from { transform: translateY(100%); }
  to { transform: none; }
}
@keyframes chat-backdrop-in {
  from { opacity: 0; }
}
@keyframes chat-in {
  from { transform: translateY(6px); }
}
@keyframes chat-bubble-in {
  from { transform: translateX(-8px) scale(0.96); }
}

/* 상단 바: 손잡이 + 내 대화명. 바 전체를 끌어내려 닫는다. */
.chat-header {
  position: relative;
  z-index: 1;
  flex: none;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-2) var(--space-2) var(--space-5);
  background: var(--surface);
  touch-action: none;
  user-select: none;
  cursor: grab;
}
.chat-grabber {
  position: absolute;
  top: 6px;
  left: 50%;
  width: 36px;
  height: 4px;
  margin-left: -18px;
  border-radius: 2px;
  background: var(--faint);
}
.chat-title-sr {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}
.chat-me {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
}
.chat-me-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 1.05rem;
  font-weight: 500;
}
.chat-header.chat-admin .chat-me-name { color: var(--accent-ink); }
.chat-close {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--faint);
  cursor: pointer;
  transition: color 0.12s ease-out;
}
.chat-close svg { width: 22px; height: 22px; }
.chat-close:hover,
.chat-close:active { color: var(--muted); }

.chat-body {
  flex: 1;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  /* 기존 말풍선 배치와 좌우 여백을 유지한다. */
  padding: var(--space-4) var(--space-5) var(--space-5);
}
.chat-history {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}
.chat-history > li {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  max-width: min(78%, 32rem);
}
.chat-history > li.chat-own {
  align-items: flex-end;
  margin-left: auto;
}
.chat-history > li.chat-in { animation: chat-in 0.16s ease; }
.chat-message {
  padding: var(--space-2) var(--space-3);
  color: var(--text);
  background: var(--surface-bright);
  border-radius: var(--radius);
  border-bottom-left-radius: var(--radius-xs);
  text-align: left;
}
li.chat-own .chat-message {
  background: var(--accent);
  color: var(--on-accent);
  border-bottom-left-radius: var(--radius);
  border-bottom-right-radius: var(--radius-xs);
}
button.chat-message { min-height: 44px; }
button.chat-message:hover { background: var(--accent-soft); }
li.chat-own button.chat-message:hover { background: var(--accent); filter: brightness(1.05); }
.chat-nickname { margin-right: 0.35em; font-weight: 500; }
.chat-nickname, .chat-text { font-size: 1rem; line-height: 1.5; }
.chat-message time {
  display: block;
  margin-top: 2px;
  font-size: 0.7rem;
  color: var(--muted);
  line-height: 1.3;
  text-align: right;
}
li.chat-own .chat-message time { color: var(--on-accent); opacity: 0.8; }
.chat-text { overflow-wrap: anywhere; white-space: pre-wrap; }
.chat-hidden .chat-text { color: var(--muted); font-style: italic; }
.chat-hidden .chat-nickname,
.chat-hidden .chat-bubble-nick { opacity: 0.7; }
.chat-hidden .chat-bubble-text { color: var(--muted); }
li.chat-own .chat-hidden .chat-text { color: var(--on-accent); }

/* 관리자 말풍선: 테마 잉크 이름 + 인증 배지 + 옅은 청록 바탕과 외곽선. */
.chat-admin .chat-nickname,
.chat-bubble.chat-admin .chat-bubble-nick { color: var(--accent-ink); }
.chat-admin .chat-nickname::before,
.chat-bubble.chat-admin .chat-bubble-nick::before {
  content: '';
  display: inline-block;
  width: 1em;
  height: 1em;
  margin-right: 0.2em;
  vertical-align: -0.15em;
  background: currentColor;
  -webkit-mask: var(--chat-admin-badge) center / contain no-repeat;
  mask: var(--chat-admin-badge) center / contain no-repeat;
}
.chat-panel, .chat-bubble {
  --chat-admin-badge: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M3.85 8.62a4 4 0 0 1 4.78-4.77 4 4 0 0 1 6.74 0 4 4 0 0 1 4.78 4.78 4 4 0 0 1 0 6.74 4 4 0 0 1-4.77 4.78 4 4 0 0 1-6.75 0 4 4 0 0 1-4.78-4.77 4 4 0 0 1 0-6.76Z'/%3E%3Cpath d='m9 12 2 2 4-4'/%3E%3C/svg%3E");
}
.chat-message.chat-admin,
li.chat-own .chat-message.chat-admin {
  color: var(--text);
  background: var(--accent-soft);
}
.chat-message.chat-admin:not(:focus-visible) {
  outline: 1px solid var(--accent-border);
  outline-offset: -1px;
}
li.chat-own .chat-message.chat-admin time { color: var(--muted); }
li.chat-own button.chat-message.chat-admin:hover { filter: none; }
.chat-empty {
  color: var(--muted);
  text-align: center;
  padding: var(--space-8);
  line-height: 1.6;
  word-break: keep-all;
}

/* 입력줄 — 옅은 평면 필드와 원형 전송 버튼. 필드는 JS가 내용 높이로 키운다. */
.chat-composer {
  flex: none;
  padding: var(--space-3) var(--space-4) calc(var(--space-3) + env(safe-area-inset-bottom, 0px));
  background: var(--surface);
}
.chat-input-row { display: flex; gap: var(--space-3); align-items: flex-end; }
.chat-input-row textarea {
  flex: 1;
  min-width: 0;
  box-sizing: border-box;
  min-height: 44px;
  resize: none;
  overflow-y: hidden;
  padding: 11px var(--space-4);
  color: var(--text);
  background: var(--surface-bright);
  border: 0;
  border-radius: 22px;
  font: inherit;
  line-height: 1.4;
}
.chat-input-row textarea:focus {
  outline: 2px solid var(--accent-ink);
  outline-offset: -2px;
}
.chat-send {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: 50%;
  background: var(--accent);
  color: var(--on-accent);
  transition: background-color 0.12s ease-out;
}
.chat-send:active:not(:disabled) { background: var(--accent-soft); }
.chat-send svg { width: 20px; height: 20px; margin-left: -2px; }
.chat-send:disabled { background: var(--surface-sunken); color: var(--muted); box-shadow: none; cursor: default; }
.chat-error {
  margin: var(--space-2) var(--space-2) 0;
  font-size: 0.8rem;
  line-height: 1.4;
  color: var(--danger-ink);
}
.chat-error:empty { display: none; }

/* 관리자 숨김 메뉴 */
.chat-actions {
  flex: none;
  display: flex;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  background: var(--surface);
}
.chat-actions button {
  min-height: 44px;
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-sm);
  background: var(--surface-bright);
}
.chat-actions button:active { background: var(--surface-sunken); }
#chatHide { color: var(--danger-ink); }

/* 최신 메시지 팝업 — [닉네임] 내용… 한 줄. 세로 중심을 레일 채팅 버튼에 맞추고(JS) 꼬리는 왼쪽을 향한다. */
.chat-bubble {
  --chat-bubble-bg: var(--surface-bright);
  position: absolute;
  z-index: 35;
  bottom: calc(var(--playbar-h) + env(safe-area-inset-bottom, 0px) + var(--space-4));
  left: calc(var(--rail-w) + var(--safe-left) + var(--space-3));
  right: calc(var(--space-4) + var(--safe-right));
  display: flex;
  align-items: center;
  gap: 0.35em;
  min-height: 44px;
  padding: var(--space-2) var(--space-4);
  color: var(--text);
  text-align: left;
  white-space: nowrap;
  border: 0;
  border-radius: var(--radius);
  background: var(--chat-bubble-bg);
  box-shadow: var(--shadow-float);
  animation: chat-bubble-in 0.2s ease;
}
.chat-bubble-nick { flex: none; font-weight: 500; }
.chat-bubble-nick, .chat-bubble-text { font-size: 0.95rem; line-height: 1.4; }
.chat-bubble-text {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-bubble::after {
  content: '';
  position: absolute;
  left: -6px;
  top: 50%;
  width: 12px;
  height: 12px;
  transform: translateY(-50%) rotate(45deg);
  background: var(--chat-bubble-bg);
  box-shadow: -3px 3px 6px -2px rgba(13, 39, 80, 0.10);
}
.chat-bubble.chat-admin {
  --chat-bubble-bg: #E3F1FA;
  box-shadow: inset 0 0 0 1px var(--accent-border), var(--shadow-float);
}
.chat-bubble.chat-admin::after { border-left: 1px solid var(--accent-border); border-bottom: 1px solid var(--accent-border); }
.chat-bubble[hidden], .chat-actions[hidden], .chat-empty[hidden] { display: none; }

@media (min-width: 960px) {
  .chat-panel {
    left: var(--space-4);
    width: calc(100% - var(--space-8));
    top: calc(var(--chat-viewport-top, 0px) + var(--space-4));
    height: calc(var(--chat-viewport-height, 100dvh) - var(--space-8));
  }
  .chat-history, .chat-empty, .chat-composer .chat-input-row, .chat-error {
    width: 100%;
    max-width: 65ch;
    margin-inline: auto;
  }
  .chat-bubble { right: auto; width: min(65ch, calc(100% - var(--rail-w) - var(--space-8))); }
}

@media (prefers-contrast: more) {
  .chat-message, .chat-bubble, .chat-input-row textarea { border: 1px solid var(--faint); }
}

@media (prefers-reduced-motion: reduce) {
  .chat-panel[open],
  .chat-panel::backdrop,
  .chat-history > li.chat-in,
  .chat-bubble { animation: none; transition: none; }
}
