:root {
  --bg: #eef2f6;
  --surface: #ffffff;
  --surface-2: #f6f8fa;
  --line: #e2e8f0;
  --text: #0f172a;
  --text-2: #64748b;
  --accent: #0d9488;
  --accent-strong: #0f766e;
  --accent-soft: #ccfbf1;
  --mine: #0d9488;
  --mine-text: #ffffff;
  --theirs: #ffffff;
  --theirs-text: #0f172a;
  --pin-bg: #fffbeb;
  --pin-line: #fde68a;
  --pin-text: #92400e;
  --danger: #dc2626;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.06), 0 1px 1px rgba(15, 23, 42, 0.04);
  --radius: 18px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --surface: #111a2b;
    --surface-2: #172236;
    --line: #233149;
    --text: #e2e8f0;
    --text-2: #94a3b8;
    --accent: #14b8a6;
    --accent-strong: #2dd4bf;
    --accent-soft: rgba(20, 184, 166, 0.16);
    --mine: #0f8f84;
    --mine-text: #ffffff;
    --theirs: #1c283d;
    --theirs-text: #e2e8f0;
    --pin-bg: rgba(251, 191, 36, 0.1);
    --pin-line: rgba(251, 191, 36, 0.3);
    --pin-text: #fcd34d;
    --danger: #f87171;
    --shadow: none;
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family:
    'Pretendard', -apple-system, BlinkMacSystemFont, 'Apple SD Gothic Neo',
    'Malgun Gothic', 'Noto Sans KR', sans-serif;
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}

svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex-shrink: 0;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

.hidden {
  display: none !important;
}

/* ---------- 전체 틀: 화면 높이에 맞춘 세로 배치 ---------- */
.app {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 820px;
  margin: 0 auto;
  background: var(--bg);
}

@media (min-width: 860px) {
  .app {
    border-left: 1px solid var(--line);
    border-right: 1px solid var(--line);
  }
}

/* ---------- 상단 바 ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 10px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  z-index: 3;
}

.peer {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
}

.peer-text {
  min-width: 0;
}

.peer-name {
  font-weight: 700;
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.peer-status {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  color: var(--text-2);
}

.peer-status::before {
  content: '';
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #94a3b8;
}

.peer-status.online::before {
  background: #22c55e;
}

.peer-status.typing {
  color: var(--accent);
}

.peer-status.typing::before {
  background: var(--accent);
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 12px;
  background: transparent;
  color: var(--text-2);
  transition: background 0.15s, color 0.15s;
}

.icon-btn:hover {
  background: var(--surface-2);
  color: var(--text);
}

.icon-btn-sm {
  width: 32px;
  height: 32px;
  border-radius: 10px;
}

.icon-btn-sm svg {
  width: 18px;
  height: 18px;
}

.text-btn {
  border: none;
  background: none;
  color: var(--accent);
  font-weight: 600;
  font-size: 14px;
  padding: 6px 8px;
}

/* ---------- 아바타 ---------- */
.avatar {
  position: relative;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  font-weight: 700;
  font-size: 14px;
  color: #fff;
  background: var(--avatar-color, #64748b);
}

.avatar img {
  /* 사진이 있으면 이니셜 위를 덮음 (사진이 안 열리면 이니셜만 남음) */
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.avatar-lg {
  width: 40px;
  height: 40px;
  font-size: 16px;
}

/* ---------- 검색 / 고정 / 연결 상태 ---------- */
.search-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  color: var(--text-2);
}

.search-bar svg {
  width: 18px;
  height: 18px;
}

.search-bar input {
  flex: 1;
  min-width: 0;
  border: none;
  outline: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  padding: 6px 0;
}

.search-count {
  font-size: 12px;
  white-space: nowrap;
}

.pinned-bar {
  display: flex;
  align-items: center;
  gap: 4px;
  margin: 8px 10px 0;
  padding: 4px 4px 4px 0;
  background: var(--pin-bg);
  border: 1px solid var(--pin-line);
  border-radius: 14px;
  color: var(--pin-text);
  z-index: 2;
}

.pinned-body {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
  padding: 6px 4px 6px 12px;
  border: none;
  background: none;
  color: inherit;
  text-align: left;
}

.pinned-body svg {
  width: 18px;
  height: 18px;
}

.pinned-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
  font-size: 13px;
}

.pinned-text strong {
  font-size: 11px;
  opacity: 0.8;
}

#pinned-text {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text);
}

.pinned-bar .icon-btn {
  color: inherit;
}

.connection-banner {
  margin: 8px 10px 0;
  padding: 8px 12px;
  border-radius: 12px;
  background: rgba(220, 38, 38, 0.1);
  color: var(--danger);
  font-size: 13px;
  text-align: center;
}

/* ---------- 메시지 목록 ---------- */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px 12px 8px;
  overscroll-behavior: contain;
}

.empty-state {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  color: var(--text-2);
  font-size: 14px;
  text-align: center;
}

.empty-state strong {
  color: var(--text);
  font-size: 16px;
}

.date-divider {
  display: flex;
  justify-content: center;
  margin: 18px 0 10px;
}

.date-divider span {
  padding: 4px 12px;
  border-radius: 999px;
  background: rgba(100, 116, 139, 0.14);
  color: var(--text-2);
  font-size: 12px;
}

.system-line {
  text-align: center;
  color: var(--text-2);
  font-size: 12px;
  margin: 10px 0;
}

.msg {
  display: flex;
  gap: 8px;
  margin-top: 2px;
  align-items: flex-end;
}

.msg.group-start {
  margin-top: 12px;
}

.msg.mine {
  flex-direction: row-reverse;
}

.msg .avatar-slot {
  width: 34px;
  flex-shrink: 0;
  align-self: flex-start;
}

.msg-body {
  position: relative;
  display: flex;
  flex-direction: column;
  max-width: min(72%, 520px);
  min-width: 0;
}

.msg.mine .msg-body {
  align-items: flex-end;
}

.msg-name {
  font-size: 12px;
  color: var(--text-2);
  margin: 0 0 4px 4px;
}

.bubble-row {
  display: flex;
  align-items: flex-end;
  gap: 6px;
}

.msg.mine .bubble-row {
  flex-direction: row-reverse;
}

.bubble {
  position: relative;
  padding: 9px 13px;
  border-radius: var(--radius);
  background: var(--theirs);
  color: var(--theirs-text);
  box-shadow: var(--shadow);
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
  min-width: 0;
}

.msg:not(.mine).group-start .bubble {
  border-top-left-radius: 6px;
}

.msg.mine .bubble {
  background: var(--mine);
  color: var(--mine-text);
}

.msg.mine.group-start .bubble {
  border-top-right-radius: 6px;
}

.bubble a {
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.bubble mark {
  background: #fde047;
  color: #0f172a;
  border-radius: 3px;
  padding: 0 1px;
}

.bubble.is-pinned {
  outline: 2px solid #f59e0b;
  outline-offset: 2px;
}

.bubble.flash {
  animation: flash 1.2s ease;
}

@keyframes flash {
  0%,
  100% {
    filter: none;
  }
  30% {
    filter: brightness(1.25) saturate(1.3);
    transform: scale(1.02);
  }
}

.meta {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
  font-size: 11px;
  color: var(--text-2);
  white-space: nowrap;
  padding-bottom: 2px;
}

.msg.mine .meta {
  align-items: flex-end;
}

.unread {
  color: var(--accent);
  font-weight: 700;
}

/* 첨부: 사진은 말풍선 없이, 파일은 카드 */
.bubble.media {
  padding: 0;
  overflow: hidden;
  background: transparent;
  box-shadow: none;
}

.bubble.media img {
  display: block;
  max-width: min(280px, 64vw);
  max-height: 320px;
  border-radius: 14px;
  cursor: zoom-in;
  background: var(--surface-2);
}

.file-card {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 200px;
  max-width: 280px;
  padding: 10px 12px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--text);
  text-decoration: none !important;
}

.file-card.expired {
  opacity: 0.6;
}

.msg.mine .file-card {
  background: rgba(255, 255, 255, 0.14);
  border-color: rgba(255, 255, 255, 0.25);
  color: #fff;
}

.file-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 10px;
  font-weight: 800;
  flex-shrink: 0;
}

.msg.mine .file-icon {
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
}

.file-info {
  display: flex;
  flex-direction: column;
  min-width: 0;
  flex: 1;
}

.file-name {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.file-size {
  font-size: 12px;
  opacity: 0.75;
}

.file-card svg {
  width: 18px;
  height: 18px;
  opacity: 0.8;
}

.bubble.with-file {
  padding: 4px;
}

.bubble.with-file .caption {
  padding: 6px 9px 4px;
}

/* 메시지 동작 (고정, 복사): 말풍선 위에 떠서 나타남 (자리를 차지하지 않음) */
.msg-actions {
  position: absolute;
  top: -16px;
  left: 0;
  z-index: 2;
  display: flex;
  gap: 4px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.12s;
}

.msg:hover .msg-actions,
.msg.show-actions .msg-actions {
  opacity: 1;
  pointer-events: auto;
}

@media (hover: none) {
  .msg:hover .msg-actions {
    opacity: 0;
    pointer-events: none;
  }
  .msg.show-actions .msg-actions {
    opacity: 1;
    pointer-events: auto;
  }
}

.msg.mine .msg-actions {
  left: auto;
  right: 0;
}

.msg.group-start:not(.mine) .msg-actions {
  top: 2px;
  left: auto;
  right: 0;
  transform: translateX(calc(100% + 6px));
}

.chip-btn {
  box-shadow: 0 2px 6px rgba(15, 23, 42, 0.12);
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 9px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text-2);
  font-size: 12px;
}

.chip-btn:hover {
  color: var(--text);
}

.chip-btn svg {
  width: 13px;
  height: 13px;
}

/* 입력 중 */
.typing-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  margin-top: 12px;
}

.typing-bubble {
  display: inline-flex;
  gap: 4px;
  padding: 12px 14px;
  border-radius: var(--radius);
  border-top-left-radius: 6px;
  background: var(--theirs);
  box-shadow: var(--shadow);
}

.typing-bubble i {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-2);
  animation: bounce 1.2s infinite ease-in-out;
}

.typing-bubble i:nth-child(2) {
  animation-delay: 0.15s;
}

.typing-bubble i:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes bounce {
  0%,
  60%,
  100% {
    transform: translateY(0);
    opacity: 0.5;
  }
  30% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

.new-message-pill {
  position: absolute;
  left: 50%;
  bottom: 86px;
  transform: translateX(-50%);
  padding: 8px 14px;
  border: none;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 6px 16px rgba(13, 148, 136, 0.35);
  z-index: 4;
}

/* ---------- 입력창 ---------- */
.composer {
  padding: 8px 10px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--line);
}

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 6px;
}

#message-input {
  flex: 1;
  min-width: 0;
  max-height: 140px;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  line-height: 1.4;
  resize: none;
  outline: none;
  transition: border-color 0.15s, background 0.15s;
}

#message-input:focus {
  border-color: var(--accent);
  background: var(--surface);
}

.send-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  flex-shrink: 0;
  transition: transform 0.12s, opacity 0.15s, background 0.15s;
}

.send-btn:hover:not(:disabled) {
  background: var(--accent-strong);
}

.send-btn:active:not(:disabled) {
  transform: scale(0.94);
}

.send-btn:disabled {
  opacity: 0.35;
  cursor: default;
}

.send-btn.sending svg {
  animation: spin 0.9s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* 첨부 미리보기 */
.attachment-preview {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 0 8px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--surface-2);
}

.attachment-preview .thumb {
  width: 56px;
  height: 56px;
  border-radius: 12px;
  object-fit: cover;
  flex-shrink: 0;
}

.attachment-preview .file-icon {
  width: 56px;
  height: 56px;
  border-radius: 12px;
  font-size: 12px;
}

.attachment-preview .file-info {
  flex: 1;
}

.drop-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(13, 148, 136, 0.12);
  border: 3px dashed var(--accent);
  border-radius: 16px;
  margin: 8px;
  z-index: 10;
  pointer-events: none;
  color: var(--accent);
  font-weight: 700;
  text-align: center;
  backdrop-filter: blur(2px);
}

.drop-overlay svg {
  width: 40px;
  height: 40px;
}

/* ---------- 안내 화면 / 사진 크게 보기 / 토스트 ---------- */
.gate {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--bg);
  z-index: 20;
}

.gate-card {
  max-width: 360px;
  padding: 28px 24px;
  border-radius: 20px;
  background: var(--surface);
  border: 1px solid var(--line);
  text-align: center;
  line-height: 1.6;
}

.gate-icon {
  display: inline-flex;
  padding: 12px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
}

.gate-icon svg {
  width: 28px;
  height: 28px;
}

.primary-btn {
  margin-top: 8px;
  padding: 10px 18px;
  border: none;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}

.lightbox {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(2, 6, 23, 0.88);
  z-index: 30;
  cursor: zoom-out;
}

.lightbox img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 8px;
}

.lightbox-download {
  position: absolute;
  top: max(16px, env(safe-area-inset-top));
  right: 16px;
  padding: 8px 14px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  font-size: 14px;
  text-decoration: none;
}

.toast {
  position: absolute;
  left: 50%;
  bottom: 90px;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 12px;
  background: rgba(15, 23, 42, 0.92);
  color: #fff;
  font-size: 14px;
  z-index: 40;
  text-align: center;
}
