/*
 * Shared messaging CSS — WhatsApp-inspired chat bubbles.
 * Used by both admin-server (player communication) and main-server (coach's corner).
 *
 * Theme-aware: uses CSS vars (--accent, --bg-primary, --bg-secondary)
 * so slam themes (Wimbledon/French/US/Australian) carry through.
 *
 * Include via: <link rel="stylesheet" href="/shared/messaging/messaging.css">
 */

/* ── Day header (Today / Yesterday / date) ── */
.msg-day-header {
  text-align: center;
  margin: 1em 0 0.6em;
  position: relative;
}
.msg-day-header::before {
  content: '';
  position: absolute;
  left: 0; right: 0; top: 50%;
  border-top: 1px solid rgba(255,255,255,0.08);
}
.msg-day-header span {
  position: relative;
  display: inline-block;
  padding: 3px 14px;
  font-size: 0.75em;
  font-weight: 600;
  color: rgba(255,255,255,0.5);
  background: var(--bg-secondary, #1a3a1b);
  border-radius: 8px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* ── Message thread container ── */
.msg-thread {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px 0;
}

/* ── Message bubble ── */
.msg-bubble {
  max-width: 78%;
  padding: 0.55em 0.75em;
  border-radius: 12px;
  font-size: 1em;
  line-height: 1.5;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  position: relative;
  margin-bottom: 2px;
}

/* Self/sender — right aligned. Stronger accent fill so "my messages" pop. */
.msg-bubble--sent {
  align-self: flex-end;
  background: linear-gradient(180deg, rgba(var(--accent-rgb, 228,249,27), 0.28), rgba(var(--accent-rgb, 228,249,27), 0.18));
  border: 1px solid rgba(var(--accent-rgb, 228,249,27), 0.55);
  box-shadow: 0 1px 4px rgba(var(--accent-rgb, 228,249,27), 0.18);
  border-bottom-right-radius: 4px;
  color: #fff;
}

/* Counterparty — left aligned. Cool slate so it reads as "incoming". */
.msg-bubble--received {
  align-self: flex-start;
  background: rgba(64, 110, 142, 0.18);
  border: 1px solid rgba(120, 165, 200, 0.28);
  border-bottom-left-radius: 4px;
}

/* ── Sender name (shown on first message in a group) ── */
.msg-sender {
  font-size: 0.78em;
  font-weight: 700;
  color: var(--accent, #e4f91b);
  margin-bottom: 2px;
}
.msg-bubble--received .msg-sender {
  color: rgba(255,255,255,0.6);
}

/* ── Message text ── */
.msg-text {
  color: #e2e8f0;
  white-space: pre-wrap;
}

/* ── Timestamp + channel indicator (bottom-right of bubble) ── */
.msg-meta {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
  margin-top: 4px;
  font-size: 0.7em;
  color: rgba(255,255,255,0.35);
  line-height: 1;
}
.msg-channel {
  font-size: 0.9em;
  display: inline-flex;
  align-items: center;
}
/* Compact WhatsApp badge. The SVG is self-contained (brand-green disc with
 * a white glyph baked in), so CSS just sizes the icon — no extra background. */
.msg-channel--whatsapp {
  display: inline-flex;
  align-items: center;
  margin-left: 3px;
  vertical-align: middle;
  line-height: 0;
}
.msg-channel--whatsapp svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  filter: drop-shadow(0 1px 1px rgba(37, 211, 102, 0.35));
}

/* ── Reply-to quote ── */
.msg-quote {
  padding: 5px 8px;
  margin-bottom: 4px;
  border-radius: 6px;
  border-left: 3px solid var(--accent, #e4f91b);
  background: rgba(255,255,255,0.05);
  font-size: 0.82em;
  color: rgba(255,255,255,0.55);
  cursor: pointer;
  max-height: 3.2em;
  overflow: hidden;
  text-overflow: ellipsis;
}
.msg-quote:hover {
  background: rgba(255,255,255,0.08);
}
.msg-quote-author {
  font-weight: 700;
  color: var(--accent, #e4f91b);
  font-size: 0.9em;
}

/* ── Media previews ── */
.msg-media {
  margin-top: 6px;
  border-radius: 8px;
  overflow: hidden;
}
.msg-media img {
  display: block;
  max-width: 100%;
  max-height: 240px;
  border-radius: 8px;
  object-fit: cover;
  cursor: pointer;
}
.msg-media video {
  display: block;
  max-width: 100%;
  max-height: 240px;
  border-radius: 8px;
}
.msg-media audio {
  display: block;
  width: 100%;
  height: 36px;
  margin-top: 4px;
}
.msg-media-file {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  background: rgba(255,255,255,0.05);
  border: 1px solid rgba(255,255,255,0.08);
  color: #ccc;
  font-size: 0.85em;
  text-decoration: none;
  cursor: pointer;
}
.msg-media-file:hover {
  background: rgba(255,255,255,0.08);
}
.msg-media-file-icon {
  font-size: 1.4em;
  flex-shrink: 0;
}
.msg-media-file-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ── Load older button ── */
.msg-load-older {
  text-align: center;
  padding: 8px;
}
.msg-load-older button {
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.1);
  color: rgba(255,255,255,0.5);
  padding: 6px 18px;
  border-radius: 20px;
  font-size: 0.82em;
  cursor: pointer;
  font-family: inherit;
}
.msg-load-older button:hover {
  background: rgba(255,255,255,0.1);
  color: rgba(255,255,255,0.7);
}

/* ── Compose bar (pinned to bottom of conversation, always visible) ── */
.msg-compose {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 8px 12px;
  border-top: 1px solid rgba(255,255,255,0.1);
  background: var(--bg-secondary, #1a3a1b);
  flex-shrink: 0;
  z-index: 5;
  /* Shadow separates compose from scrolling messages above */
  box-shadow: 0 -2px 8px rgba(0,0,0,0.25);
}
.msg-compose-input {
  flex: 1;
  min-height: 36px;
  max-height: 120px;
  padding: 8px 12px;
  border-radius: 18px;
  border: 1px solid rgba(255,255,255,0.12);
  background: rgba(255,255,255,0.06);
  color: white;
  font-size: 1em;
  font-family: inherit;
  resize: none;
  line-height: 1.4;
}
.msg-compose-input:focus {
  outline: none;
  border-color: var(--accent, #e4f91b);
}
.msg-compose-input::placeholder {
  color: rgba(255,255,255,0.3);
}
.msg-compose-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--accent, #e4f91b);
  color: #1a1a1a;
  font-size: 1.1em;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.msg-compose-btn:hover {
  filter: brightness(1.1);
}
.msg-compose-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.msg-compose-attach {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid rgba(255,255,255,0.15);
  background: none;
  color: rgba(255,255,255,0.5);
  font-size: 1.1em;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.msg-compose-attach:hover {
  color: var(--accent, #e4f91b);
  border-color: var(--accent, #e4f91b);
}

/* ── Reply banner (shown above compose when replying to a message) ── */
.msg-reply-banner {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: rgba(var(--accent-rgb, 228,249,27), 0.06);
  border-left: 3px solid var(--accent, #e4f91b);
  border-bottom: 1px solid rgba(255,255,255,0.06);
  font-size: 0.82em;
  color: rgba(255,255,255,0.6);
}
.msg-reply-banner-text {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.msg-reply-banner-close {
  background: none;
  border: none;
  color: rgba(255,255,255,0.4);
  cursor: pointer;
  font-size: 1.2em;
  padding: 0 4px;
}

/* ── Thread list (stacked above conversation) ── */
/* ── Panel: flex column, fills parent height exactly ── */
.msg-panel {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  overflow: hidden;
}
.msg-panel-threads {
  flex-shrink: 0;
  max-height: 45%;
  overflow-y: auto;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  transition: max-height 0.2s ease;
}
.msg-panel-threads.collapsed {
  max-height: 44px;
  overflow: hidden;
}

/* ── Conversation: messages scroll, compose anchored at bottom ── */
.msg-panel-conversation {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0; /* critical: allows flex child to shrink below content size */
  overflow: hidden;
}
.msg-panel-conversation.visible {
  display: flex !important;
}

/* Messages scroll container — fills all space between header and compose.
   flex:1 + min-height:0 lets it shrink to fit. Parent MUST have overflow:hidden. */
.msg-scroll {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  min-height: 0;
  padding: 10px 14px;
  -webkit-overflow-scrolling: touch;
}

/* Compose anchored at bottom — never scrolls, always visible */

/* Thread item row */
.msg-thread-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  cursor: pointer;
  border-bottom: 1px solid rgba(255,255,255,0.04);
  transition: background 0.15s;
  position: relative;
}
.msg-thread-item:hover {
  background: rgba(255,255,255,0.04);
}
.msg-thread-item.active {
  background: rgba(var(--accent-rgb, 228,249,27), 0.06);
  border-left: 3px solid var(--accent, #e4f91b);
  padding-left: 11px;
}

/* Avatar circle */
.msg-thread-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(255,255,255,0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.82em;
  font-weight: 700;
  color: var(--accent, #e4f91b);
  flex-shrink: 0;
  text-transform: uppercase;
}

/* Thread info */
.msg-thread-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.msg-thread-name {
  font-size: 0.88em;
  font-weight: 600;
  color: #e2e8f0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.msg-thread-preview {
  font-size: 0.78em;
  color: rgba(255,255,255,0.4);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Time + unread */
.msg-thread-time {
  font-size: 0.7em;
  color: rgba(255,255,255,0.35);
  flex-shrink: 0;
  text-align: right;
}
.msg-thread-unread {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent, #e4f91b);
  position: absolute;
  right: 14px;
  top: 50%;
  transform: translateY(-50%);
}

/* Active thread header (shown when thread list is collapsed) */
.msg-active-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  cursor: pointer;
  background: rgba(0,0,0,0.2);
  border-bottom: 1px solid rgba(255,255,255,0.06);
}
.msg-active-header:hover {
  background: rgba(0,0,0,0.3);
}
.msg-active-header .msg-thread-avatar {
  width: 28px;
  height: 28px;
  font-size: 0.72em;
}
.msg-active-header .msg-thread-name {
  flex: 1;
  font-size: 0.9em;
}
.msg-active-header .msg-back-arrow {
  color: var(--accent, #e4f91b);
  font-size: 0.85em;
  margin-right: 4px;
}

/* Thread search at bottom of list */
.msg-thread-search {
  padding: 8px 12px;
  border-top: 1px solid rgba(255,255,255,0.06);
}
.msg-thread-search input {
  width: 100%;
  padding: 6px 10px;
  border-radius: 16px;
  border: 1px solid rgba(255,255,255,0.1);
  background: rgba(255,255,255,0.05);
  color: white;
  font-size: 0.82em;
  font-family: inherit;
}
.msg-thread-search input:focus {
  outline: none;
  border-color: var(--accent, #e4f91b);
}
.msg-thread-search input::placeholder {
  color: rgba(255,255,255,0.3);
}

/* ── Empty state ── */
.msg-empty {
  text-align: center;
  padding: 2em 1em;
  color: rgba(255,255,255,0.3);
  font-size: 0.95em;
}

/* ── Responsive (mobile-first priority) ── */

/* Tablet: stacked layout, messaging gets fixed viewport height */
@media (max-width: 1024px) {
  .msg-panel-threads { max-height: 35%; }
  .msg-thread-item { padding: 10px 12px; min-height: 48px; }
  .msg-thread-avatar { width: 34px; height: 34px; font-size: 0.78em; }
}

/* Mobile: touch-friendly sizes, prevent iOS zoom */
@media (max-width: 768px) {
  .msg-bubble { max-width: 88%; }
  .msg-compose { padding: 8px; gap: 6px; }
  .msg-compose-btn, .msg-compose-attach {
    width: 44px; height: 44px; min-width: 44px; min-height: 44px;
  }
  .msg-compose-input { font-size: 16px; min-height: 44px; padding: 10px 14px; }
  .msg-thread-item { padding: 12px 14px; min-height: 56px; }
  .msg-thread-avatar { width: 40px; height: 40px; font-size: 0.82em; }
  .msg-panel-threads { max-height: 40%; }
  .msg-thread-search input { font-size: 16px; min-height: 44px; padding: 10px 14px; }
  .msg-active-header { padding: 10px 12px; min-height: 48px; }
  .msg-day-header { margin: 0.8em 0 0.5em; }
}

/* Small phones */
@media (max-width: 480px) {
  .msg-bubble { max-width: 94%; padding: 0.5em 0.65em; }
  .msg-day-header span { font-size: 0.72em; }
  .msg-thread-preview { display: none; }
  .msg-compose-input { padding: 10px 12px; }
  .msg-scroll { padding: 8px 10px; }
  .msg-thread-item { padding: 10px; }
}
