/* ============================================
   RITHM - Floating Chat Panel
   Direct messaging UI (driven by profile-chat.js).
   Uses stage palette variables from variables.css.
   ============================================ */

/* --- Panel shell --- */
.rp-messaging {
  position: fixed;
  right: 1.25rem;
  bottom: 1.25rem;
  width: 370px;
  max-width: calc(100vw - 2.5rem);
  height: 520px;
  max-height: calc(100vh - 6rem);
  display: flex;
  flex-direction: column;
  background: var(--bg-elevated);
  border: 1px solid var(--border-hover);
  border-radius: var(--radius-lg);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.55);
  z-index: 5000;
  overflow: hidden;
  font-size: 0.9rem;
}

/* --- Header --- */
.rp-msg-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.65rem 0.75rem;
  background: var(--bg-base);
  border-bottom: 1px solid var(--border-hover);
  flex: 0 0 auto;
}

#rp-msg-title {
  flex: 1;
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.rp-msg-icon-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 1rem;
  padding: 0.35rem 0.45rem;
  border-radius: var(--radius-sm);
  line-height: 1;;
  min-width: 44px;
  min-height: 44px;
}

.rp-msg-icon-btn:hover {
  color: var(--color-primary);
  background: var(--surface-primary);
}

.rp-msg-icon-btn:disabled {
  opacity: 0.4;
  cursor: default;
}

/* --- Conversation list --- */
.rp-msg-conversations {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
}

.rp-msg-empty {
  text-align: center;
  color: var(--text-muted);
  padding: 2.5rem 1rem;
}

.rp-msg-empty i {
  font-size: 2rem;
  margin-bottom: 0.75rem;
  color: var(--text-muted);
  display: block;
}

.rp-msg-conv-item,
.rp-msg-friend-item {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.6rem 0.65rem;
  border-radius: var(--radius-md);
  cursor: pointer;
}

.rp-msg-conv-item:hover,
.rp-msg-friend-item:hover {
  background: var(--surface-primary);
}

.rp-msg-conv-avatar {
  width: 40px;
  height: 40px;
  flex: 0 0 40px;
  border-radius: 50%;
  background: var(--bg-base);
  border: 1px solid var(--border-hover);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  overflow: hidden;
}

.rp-msg-conv-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.rp-msg-conv-info {
  flex: 1;
  min-width: 0;
}

.rp-msg-conv-name {
  font-weight: 600;
  color: var(--text-primary);
}

.rp-msg-conv-preview {
  color: var(--text-secondary);
  font-size: 0.82rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.rp-msg-conv-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.25rem;
  flex: 0 0 auto;
}

.rp-msg-conv-time {
  font-size: 0.72rem;
  color: var(--text-muted);
}

.rp-msg-unread {
  background: var(--color-primary);
  color: #1a1714;
  font-size: 0.72rem;
  font-weight: 700;
  min-width: 1.35rem;
  height: 1.35rem;
  line-height: 1.35rem;
  text-align: center;
  border-radius: 999px;
  padding: 0 0.3rem;
}

/* --- Thread view --- */
.rp-msg-thread {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.rp-msg-load-more {
  text-align: center;
  padding: 0.35rem;
  flex: 0 0 auto;
}

.rp-msg-load-more button {
  background: transparent;
  border: 1px solid var(--border-hover);
  color: var(--text-secondary);
  border-radius: 999px;
  padding: 0.25rem 0.9rem;
  font-size: 0.78rem;
  cursor: pointer;
}

.rp-msg-load-more button:hover {
  color: var(--color-primary);
  border-color: var(--color-primary);
}

.rp-msg-messages {
  flex: 1;
  overflow-y: auto;
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.rp-msg-date-sep {
  text-align: center;
  font-size: 0.72rem;
  color: var(--text-muted);
  margin: 0.4rem 0;
}

.rp-msg-bubble {
  max-width: 80%;
  padding: 0.5rem 0.75rem;
  border-radius: 1rem;
  word-wrap: break-word;
}

.rp-msg-sent {
  align-self: flex-end;
  background: var(--color-primary);
  color: #1a1714;
  border-bottom-right-radius: 0.25rem;
}

.rp-msg-received {
  align-self: flex-start;
  background: var(--bg-base);
  color: var(--text-primary);
  border: 1px solid var(--border-hover);
  border-bottom-left-radius: 0.25rem;
}

.rp-msg-time {
  font-size: 0.68rem;
  margin-top: 0.2rem;
  opacity: 0.65;
  text-align: right;
}

/* --- Composer --- */
.rp-msg-composer {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  padding: 0.6rem 0.75rem;
  border-top: 1px solid var(--border-hover);
  background: var(--bg-base);
  flex: 0 0 auto;
}

#rp-msg-input-text {
  flex: 1;
  resize: none;
  background: var(--bg-elevated);
  border: 1px solid var(--border-hover);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  padding: 0.55rem 0.75rem;
  font-family: inherit;
  font-size: 0.9rem;
  line-height: 1.4;
  max-height: 80px;
}

#rp-msg-input-text:focus {
  outline: none;
  border-color: var(--color-primary);
}

#rp-msg-send {
  color: var(--color-primary);
  font-size: 1.15rem;
  flex: 0 0 auto;
  margin-bottom: 0.15rem;
}

/* --- Global nav badge --- */
#chat-badge {
  margin-left: 0.4rem;
}

/* --- Small screens: full-width bottom sheet --- */
@media (max-width: 480px) {
  .rp-messaging {
    right: 0.5rem;
    left: 0.5rem;
    bottom: 0.5rem;
    width: auto;
    max-width: none;
    height: 70vh;
  }
}

@media (max-width: 768px) {
  /* Prevent iOS Safari auto-zoom in the chat composer */
  #rp-msg-input-text { font-size: 16px; }
}

/* Bard entry inside the Messages panel (tutor-enabled accounts) */
.rp-msg-bard-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  width: 100%;
  padding: 0.8rem 1rem;
  background: transparent;
  border: 0;
  border-bottom: 1px solid var(--border-hover);
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.rp-msg-bard-row:hover { background: rgba(255, 176, 32, 0.08); }
.rp-msg-bard-row__icon { color: var(--stage-amber, #ffb020); font-size: 1.05rem; }
.rp-msg-bard-row__text { flex: 1; display: flex; flex-direction: column; }
.rp-msg-bard-row__name { font-weight: 600; }
.rp-msg-bard-row__sub { font-size: 0.8rem; opacity: 0.65; }
.rp-msg-bard-row__chev { opacity: 0.4; font-size: 0.75rem; }
.rp-msg-bard { flex: 1; min-height: 0; flex-direction: column; }
.rp-msg-bard .tutor-panel {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}
.rp-msg-bard .tutor-panel__transcript {
  flex: 1;
  min-height: 0;
  max-height: none;
}

/* --- Full-page mode (/messages/) --- */
.rp-messaging--page {
  position: static;
  width: 100%;
  max-width: none;
  height: min(780px, calc(100dvh - 180px));
  min-height: 460px;
  max-height: none;
  box-shadow: none;
  z-index: auto;
}

.rp-messaging--page #rp-msg-close { display: none; }

@media (min-width: 900px) {
  .rp-messaging--page {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 320px;
    grid-template-rows: auto auto minmax(0, 1fr);
  }
  .rp-messaging--page .rp-msg-header { grid-column: 1 / -1; }
  .rp-messaging--page .rp-msg-bard-row {
    grid-column: 2;
    grid-row: 2;
    border-left: 1px solid var(--border-hover);
  }
  .rp-messaging--page .rp-msg-conversations {
    grid-column: 2;
    grid-row: 3;
    border-left: 1px solid var(--border-hover);
    min-height: 0;
  }
  .rp-messaging--page .rp-msg-thread {
    grid-column: 1;
    grid-row: 2 / 4;
    min-height: 0;
  }
  .rp-messaging--page .rp-msg-bard {
    grid-column: 1;
    grid-row: 2 / 4;
    min-height: 0;
  }
}
