/* tin-nhan.css - Giao diện Chat UI */

.tin-nhan-container {
  height: calc(100vh - 80px); /* Trừ chiều cao header */
  max-height: calc(100vh - 80px);
  display: flex;
  flex-direction: column;
}

.chat-layout {
  display: flex;
  flex: 1;
  background: var(--card);
  border-radius: 16px;
  border: 1px solid var(--line);
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0,0,0,0.05);
}

/* SIDEBAR: Danh sách hội thoại */
.chat-sidebar {
  width: 350px;
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.chat-sidebar-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
  background: var(--card);
}

.chat-sidebar-header h2 {
  margin: 0;
  font-size: 20px;
  color: var(--ink);
  font-family: 'Fraunces', serif;
}

.conversation-list {
  flex: 1;
  overflow-y: auto;
}

/* Từng item hội thoại */
.conversation-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  transition: background 0.2s;
}

.conversation-item:hover {
  background: var(--primary-tint);
}

.conversation-item.active {
  background: var(--card);
  border-left: 4px solid var(--primary);
  padding-left: 16px;
}

.conversation-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--line);
  flex-shrink: 0;
}

.conversation-info {
  flex: 1;
  min-width: 0; /* để text-overflow hoạt động */
}

.conversation-info-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}

.conversation-name {
  font-weight: 600;
  color: var(--ink);
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conversation-time {
  font-size: 12px;
  color: var(--muted);
}

.conversation-info-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.conversation-preview {
  font-size: 14px;
  color: var(--ink-soft);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  padding-right: 8px;
}

.conversation-unread-badge {
  background: var(--danger, #B23B3B);
  color: white;
  font-size: 11px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 99px;
  min-width: 20px;
  text-align: center;
}

.chat-empty-state {
  padding: 32px 20px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
}


/* MAIN CHAT AREA */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--card);
  min-width: 0;
}

.chat-no-selection {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}

.chat-active {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
}

/* Chat Header */
.chat-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--card);
}

.back-to-list-btn {
  display: none; /* Chỉ hiện trên mobile */
  padding: 8px;
  margin-left: -8px;
  color: var(--ink);
  cursor: pointer;
}

.chat-header-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--line);
}

.chat-header-info {
  flex: 1;
}

.chat-header-name {
  margin: 0;
  font-size: 16px;
  color: var(--ink);
  font-weight: 600;
}

.chat-header-listing {
  font-size: 12px;
  color: var(--accent);
  text-decoration: none;
  font-weight: 500;
}

.chat-header-listing:hover {
  text-decoration: underline;
}

/* Khung tin nhắn cuộn */
.chat-messages-area {
  flex: 1;
  overflow-y: auto;
  padding: 24px 20px;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* BONG BÓNG TIN NHẮN (BUBBLES) */
.message-bubble {
  max-width: 75%;
  display: flex;
  flex-direction: column;
}

.message-mine {
  align-self: flex-end;
}

.message-theirs {
  align-self: flex-start;
}

.message-content {
  padding: 12px 16px;
  border-radius: 16px;
  font-size: 15px;
  line-height: 1.5;
  word-wrap: break-word;
}

/* Tin nhắn của mình */
.message-mine .message-content {
  background: var(--ink);
  color: var(--bg);
  border-bottom-right-radius: 4px; /* Làm góc vuông ở đuôi */
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  border: none;
}

/* Tin nhắn của đối phương */
.message-theirs .message-content {
  background: var(--bg);
  color: var(--ink);
  border: 1px solid var(--line);
  border-bottom-left-radius: 4px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.03);
}

.message-time {
  font-size: 11px;
  color: var(--muted);
  margin-top: 4px;
}

.message-mine .message-time {
  text-align: right;
}

.message-theirs .message-time {
  text-align: left;
}

/* Chat Input Form */
.chat-input-area {
  padding: 16px 20px;
  border-top: 1px solid var(--line);
  background: var(--card);
}

.chat-form {
  display: flex;
  gap: 12px;
}

.chat-input {
  flex: 1;
  padding: 12px 16px;
  border: 1px solid var(--line);
  border-radius: 24px;
  font-family: inherit;
  font-size: 15px;
  outline: none;
  transition: border-color 0.2s;
  background: var(--bg);
  color: var(--ink);
}

.chat-input:focus {
  border-color: var(--primary);
}

.btn-send-chat {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.btn-send-chat:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* RESPONSIVE MOBILE */
@media (max-width: 768px) {
  .tin-nhan-container {
    height: calc(100vh - 64px);
    max-height: calc(100vh - 64px);
    padding: 0; /* Tràn viền trên mobile */
  }
  
  .chat-layout {
    border-radius: 0;
    border: none;
    border-top: 1px solid var(--line);
  }

  .chat-sidebar {
    width: 100%;
    border-right: none;
  }
  
  .chat-main {
    display: none; /* Mặc định ẩn chat main */
    width: 100%;
  }
  
  /* Lớp utility dùng JS để chuyển đổi trạng thái trên mobile */
  .chat-layout.mobile-view-chat .chat-sidebar {
    display: none;
  }
  
  .chat-layout.mobile-view-chat .chat-main {
    display: flex;
  }
  
  .back-to-list-btn {
    display: block; /* Hiện nút back */
  }
}

/* CHAT CONTEXTUAL CARD */
.chat-context-card {
  background: var(--primary-tint);
  border-bottom: 1px solid var(--primary);
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.chat-context-card-info {
  display: flex;
  flex-direction: column;
}
.chat-context-card-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--ink);
  margin: 0 0 4px 0;
}
.chat-context-card-link {
  font-size: 12px;
  color: var(--primary);
  text-decoration: none;
  font-weight: 500;
}
.chat-context-card-link:hover {
  text-decoration: underline;
}

/* CHAT ATTACHMENT PREVIEW */
.chat-attachment-preview {
  padding: 12px 20px;
  background: var(--bg);
  border-top: 1px solid var(--line);
  display: flex;
}
.chat-attachment-list {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.chat-attachment-image-wrap {
  position: relative;
  width: 60px;
  height: 60px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--line);
}
.chat-attachment-image-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.chat-attachment-remove {
  position: absolute;
  top: 4px;
  right: 4px;
  background: rgba(0,0,0,0.6);
  color: white;
  border: none;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  font-size: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.chat-attachment-remove:hover {
  background: rgba(0,0,0,0.8);
}

/* CHAT INPUT AREA MODS */
.chat-input-wrap {
  position: relative;
  flex: 1;
  display: flex;
  align-items: center;
}
.chat-input {
  width: 100%;
  padding-right: 40px; /* space for emoji button */
}
.btn-chat-emoji {
  position: absolute;
  right: 8px;
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  padding: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
}
.btn-chat-emoji:hover {
  transform: scale(1.1);
}
.btn-chat-attach {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}
.btn-chat-attach:hover {
  background: var(--line);
  color: var(--ink);
}

/* EMOJI PICKER POPUP */
.emoji-picker-popup {
  position: absolute;
  bottom: 50px;
  right: 0;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.1);
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 8px;
  z-index: 100;
}
.emoji-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  border-radius: 8px;
  padding: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.emoji-btn:hover {
  background: var(--bg);
}

/* CHAT IMAGE IN BUBBLE */
.chat-bubble-image-grid {
  display: grid;
  gap: 4px;
  margin-top: 8px;
}
.chat-bubble-image-grid[data-count="1"] { grid-template-columns: 1fr; }
.chat-bubble-image-grid[data-count="2"] { grid-template-columns: 1fr 1fr; }
.chat-bubble-image-grid[data-count="3"] { grid-template-columns: 1fr 1fr 1fr; }
.chat-bubble-image-grid[data-count="4"] { grid-template-columns: 1fr 1fr; }
.chat-bubble-image-grid[data-count="5"],
.chat-bubble-image-grid[data-count="6"] { grid-template-columns: 1fr 1fr 1fr; }

.chat-bubble-image {
  width: 100%;
  height: auto;
  max-height: 200px;
  border-radius: 8px;
  cursor: pointer;
  object-fit: cover;
  background: var(--bg);
  display: block;
}
.chat-bubble-image-grid[data-count="1"] .chat-bubble-image {
  max-height: 300px;
  object-fit: contain;
}

/* Lightbox CSS moved to components.css */

/* CHAT AUTO LINK */
.chat-link {
  color: var(--primary);
  text-decoration: underline;
  word-break: break-all;
}
.message-mine .chat-link {
  color: white;
}
