/* CSS Variables for Theming */
:root {
  /* Light Theme Colors */
  --bg-primary: #f0f2f5;
  --bg-secondary: #ffffff;
  --bg-tertiary: #e7f3ff;
  --bg-hover: #f0f2f5;
  
  --text-primary: #050505;
  --text-secondary: #65676b;
  --text-tertiary: #1c1e21;
  
  --border-color: var(--border-color);
  --border-light: var(--border-light);
  
  --accent-color: #667eea;
  --accent-hover: #5568d3;
  --blue: #0084ff;
  --blue-hover: #0073e6;
  
  --message-own-bg: #0084ff;
  --message-other-bg: #f0f0f0;
  --message-own-text: #ffffff;
  --message-other-text: #050505;
  
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  --shadow-hover: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* Dark Theme Colors */
body.dark-mode {
  --bg-primary: #18191a;
  --bg-secondary: #242526;
  --bg-tertiary: #3a3b3c;
  --bg-hover: #3a3b3c;
  
  --text-primary: #e4e6eb;
  --text-secondary: #b0b3b8;
  --text-tertiary: #e4e6eb;
  
  --border-color: #3a3b3c;
  --border-light: #3a3b3c;
  
  --accent-color: #8b9dc3;
  --accent-hover: #7a8db8;
  --blue: #0084ff;
  --blue-hover: #0073e6;
  
  --message-own-bg: #0084ff;
  --message-other-bg: #3a3b3c;
  --message-own-text: #ffffff;
  --message-other-text: #e4e6eb;
  
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
  --shadow-hover: 0 4px 12px rgba(0, 0, 0, 0.6);
}

/* Smooth transition for theme change */
body {
  transition: background-color 0.3s ease, color 0.3s ease;
}

* {
  transition: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease;
}

/* Reset & Base Styles */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
}

/* Login Page */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-container {
  width: 100%;
  max-width: 400px;
  padding: 20px;
}

.login-box {
  background: white;
  padding: 40px 30px;
  border-radius: 12px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.15);
  overflow: hidden;
}

.form-toggle {
  display: flex;
  border: 1px solid var(--border-light);
  border-radius: 8px;
  margin-bottom: 30px;
  overflow: hidden;
}

.toggle-btn {
  flex: 1;
  padding: 12px;
  border: none;
  background: var(--bg-primary);
  cursor: pointer;
  font-size: 15px;
  font-weight: 600;
  color: var(--text-secondary);
  transition: background 0.2s, color 0.2s;
}

.toggle-btn.active {
  background: white;
  color: var(--accent-color);
  box-shadow: 0 2px 4px rgba(0,0,0,0.05) inset;
}

.login-box h1 {
  text-align: center;
  color: var(--accent-color);
  font-size: 32px;
  margin-bottom: 8px;
}

.subtitle {
  text-align: center;
  color: var(--text-secondary);
  margin-bottom: 30px;
  font-size: 14px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
  color: var(--text-tertiary);
  font-size: 14px;
}

.form-group input {
  width: 100%;
  padding: 12px 16px;
  border: 1px solid var(--border-light);
  border-radius: 8px;
  font-size: 15px;
  transition: border-color 0.2s;
}

.form-group input:focus {
  outline: none;
  border-color: var(--accent-color);
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
}

.btn-login {
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: 8px;
  background: var(--accent-color);
  color: white;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-login:hover {
  background: var(--accent-hover);
}

.btn-login:disabled {
  background: #a3b3f3;
  cursor: not-allowed;
}

.hint {
  text-align: center;
  margin-top: 20px;
  font-size: 12px;
  color: var(--text-secondary);
}

/* Chat Page */
.chat-page {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

.chat-container {
  display: flex;
  width: 100%;
  height: 100%;
}

/* Sidebar */
.chat-sidebar {
  width: 320px;
  flex-shrink: 0;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  padding: 20px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.header-actions {
  display: flex;
  gap: 10px;
  align-items: center;
}

.btn-notification {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  transition: background 0.2s;
  position: relative;
}

.btn-notification:hover {
  background: var(--bg-primary);
}

.btn-notification.disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.notification-badge {
  position: absolute;
  top: 4px;
  right: 4px;
  background: #ff3b30;
  color: white;
  font-size: 10px;
  font-weight: bold;
  padding: 2px 5px;
  border-radius: 10px;
  min-width: 16px;
  text-align: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 15px;
}

.user-info .avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  object-fit: cover;
}

.user-info h3 {
  font-size: 18px;
  margin: 0;
}

.role-badge {
  font-size: 12px;
  padding: 3px 8px;
  border-radius: 12px;
  background: var(--bg-tertiary);
  color: #1877f2;
  font-weight: 600;
}

.btn-logout {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  opacity: 0.6;
  transition: opacity 0.2s;
}

.btn-logout:hover {
  opacity: 1;
}

.sidebar-search {
  padding: 15px;
  border-bottom: 1px solid var(--border-color);
  position: relative;
}

#searchInput {
  width: 100%;
  padding: 10px 15px;
  border-radius: 20px;
  border: 1px solid var(--border-light);
  background: var(--bg-hover);
  color: var(--text-primary);
  font-size: 14px;
}

#searchInput:focus {
  outline: none;
  border-color: var(--accent-color);
  background: var(--bg-secondary);
}

.search-results-container {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-top: none;
  max-height: 300px;
  overflow-y: auto;
  z-index: 100;
  box-shadow: var(--shadow-hover);
}

.search-result-item {
  display: flex;
  align-items: center;
  padding: 10px 15px;
  gap: 10px;
  cursor: pointer;
  transition: background 0.2s;
}

.search-result-item:hover {
  background: var(--bg-primary);
}

.search-result-item .avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

.search-result-item h5 {
  margin: 0;
  font-size: 15px;
}

.conversations-list {
  padding: 10px 0;
  flex-grow: 1;
  overflow-y: auto;
}

.conversations-list h4 {
  padding: 16px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
}

.conversation-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  cursor: pointer;
  transition: background 0.2s;
  border-left: 3px solid transparent;
}

.conversation-item:hover {
  background: var(--bg-primary);
}

.conversation-item.active {
  background: var(--bg-tertiary);
  border-left-color: var(--blue);
}

.conversation-item .avatar {
  width: 56px;
  height: 56px;
}

.conversation-info {
  flex: 1;
  min-width: 0;
}

.conversation-info h5 {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 4px;
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Unread badge in conversation list */
.unread-badge {
  background: #ff3b30;
  color: white;
  font-size: 11px;
  font-weight: bold;
  padding: 2px 6px;
  border-radius: 10px;
  min-width: 20px;
  text-align: center;
  display: inline-block;
}

.conversation-item.has-unread {
  background: var(--bg-tertiary);
}

.conversation-item.has-unread .conversation-info h5 {
  color: var(--blue);
}

.conversation-item.has-unread .conversation-info p {
  font-weight: 600;
  color: var(--text-tertiary);
}

.conversation-info p {
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Main Chat Area */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: white;
}

.chat-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--message-other-bg);
  background: white;
  box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}

.chat-header-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.chat-header-info h3 {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 2px;
}

.status-text {
  font-size: 12px;
  color: var(--text-secondary);
}

.empty-state {
  text-align: center;
  color: var(--text-secondary);
  padding: 20px;
}

/* Messages Container */
.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  background: var(--bg-primary);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.message-wrapper {
  display: flex;
  gap: 8px;
  margin-bottom: 4px;
}

.message-wrapper.own {
  flex-direction: row-reverse;
}

.message-wrapper .avatar {
  width: 32px;
  height: 32px;
  flex-shrink: 0;
}

.message-content {
  max-width: 60%;
}

.message-bubble {
  padding: 10px 14px;
  border-radius: 18px;
  word-wrap: break-word;
  position: relative;
}

.message-wrapper.own .message-bubble {
  background: var(--blue);
  color: white;
}

.message-wrapper:not(.own) .message-bubble {
  background: var(--message-other-bg);
  color: var(--text-primary);
}

.message-image {
  max-width: 100%;
  border-radius: 12px;
  cursor: pointer;
}

.message-time {
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 4px;
  padding: 0 4px;
}

/* Chat Input */
.chat-input-area {
  padding: 16px 20px;
  background: white;
  border-top: 1px solid var(--message-other-bg);
}

#messageForm {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-primary);
  padding: 8px;
  border-radius: 24px;
}

.btn-upload {
  background: transparent;
  border: none;
  font-size: 24px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 50%;
  transition: background 0.2s;
}

.btn-upload:hover {
  background: var(--message-other-bg);
}

#messageInput {
  flex: 1;
  border: none;
  background: transparent;
  padding: 8px 12px;
  font-size: 15px;
  outline: none;
}

#messageInput:disabled {
  cursor: not-allowed;
  opacity: 0.6;
}

.btn-send {
  background: var(--blue);
  color: white;
  border: none;
  padding: 8px 20px;
  border-radius: 18px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-send:hover:not(:disabled) {
  background: var(--blue-hover);
}

.btn-send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Emoji Button */
.btn-emoji {
  background: transparent;
  border: none;
  font-size: 24px;
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-emoji:hover {
  background: var(--bg-tertiary);
  transform: scale(1.1);
}

/* Emoji Picker */
.emoji-picker {
  position: absolute;
  bottom: 70px;
  left: 50px;
  width: 350px;
  height: 400px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  display: flex;
  flex-direction: column;
  z-index: 1000;
  animation: fadeInUp 0.2s ease;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.emoji-picker-header {
  padding: 12px;
  border-bottom: 1px solid var(--border-color);
  background: var(--bg-hover);
}

.emoji-categories {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
}

.emoji-categories::-webkit-scrollbar {
  display: none;
}

.emoji-category {
  background: transparent;
  border: none;
  font-size: 20px;
  cursor: pointer;
  padding: 8px 10px;
  border-radius: 8px;
  transition: all 0.2s;
  border-bottom: 2px solid transparent;
  flex-shrink: 0;
}

.emoji-category:hover {
  background: var(--bg-tertiary);
}

.emoji-category.active {
  background: var(--bg-tertiary);
  border-bottom-color: var(--accent-color);
}

.emoji-picker-body {
  flex: 1;
  padding: 12px;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 4px;
  align-content: start;
}

.emoji-item {
  background: transparent;
  border: none;
  font-size: 28px;
  cursor: pointer;
  padding: 8px;
  border-radius: 8px;
  transition: all 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 44px;
}

.emoji-item:hover {
  background: var(--bg-hover);
  transform: scale(1.3);
}

.emoji-item:active {
  transform: scale(1.1);
}

/* Message Reactions */
.message-reactions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 6px;
  padding: 4px 0;
}

.reaction-item {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-light);
  border-radius: 12px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s;
  user-select: none;
}

.reaction-item:hover {
  background: var(--bg-hover);
  border-color: var(--accent-color);
  transform: scale(1.05);
}

.reaction-item .emoji {
  font-size: 16px;
}

.reaction-item .count {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-primary);
  min-width: 12px;
  text-align: center;
}

.reaction-item.own {
  background: var(--accent-color);
  border-color: var(--accent-color);
}

.reaction-item.own .count {
  color: white;
}

.reaction-add-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 2px 8px;
  background: var(--bg-hover);
  border: 1px solid var(--border-light);
  border-radius: 12px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.2s;
  opacity: 0;
}

.message-wrapper:hover .reaction-add-btn {
  opacity: 1;
}

.reaction-add-btn:hover {
  background: var(--bg-tertiary);
  border-color: var(--accent-color);
  transform: scale(1.1);
}

/* Scrollbar Styling */
.conversations-list::-webkit-scrollbar,
.messages-container::-webkit-scrollbar {
  width: 8px;
}

.conversations-list::-webkit-scrollbar-track,
.messages-container::-webkit-scrollbar-track {
  background: transparent;
}

.conversations-list::-webkit-scrollbar-thumb,
.messages-container::-webkit-scrollbar-thumb {
  background: var(--border-light);
  border-radius: 4px;
}

.conversations-list::-webkit-scrollbar-thumb:hover,
.messages-container::-webkit-scrollbar-thumb:hover {
  background: var(--text-secondary);
}

/* Back Button */
.btn-back {
  display: none;
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  padding: 8px;
  margin-right: 10px;
  color: var(--accent-color);
  transition: transform 0.2s;
}

.btn-back:hover {
  transform: translateX(-3px);
}

/* Online/Offline Status Indicator */
.status-indicator {
  position: absolute;
  bottom: 2px;
  right: 2px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 2px solid white;
  background: var(--border-light);
  transition: background 0.3s;
}

.status-indicator.online {
  background: #44b700;
  box-shadow: 0 0 8px rgba(68, 183, 0, 0.5);
}

.status-indicator.offline {
  background: #95a5a6;
}

/* Avatar wrapper for status positioning */
.avatar-wrapper {
  position: relative;
  display: inline-block;
}

/* Typing indicator */
.typing-indicator {
  display: none;
  align-items: center;
  gap: 5px;
  color: var(--accent-color);
  font-size: 13px;
  font-style: italic;
  padding: 5px 0;
}

.typing-indicator.active {
  display: flex;
}

.typing-dots {
  display: flex;
  gap: 3px;
}

.typing-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent-color);
  animation: typing 1.4s infinite;
}

.typing-dots span:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-dots span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes typing {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.7;
  }
  30% {
    transform: translateY(-10px);
    opacity: 1;
  }
}

/* Responsive Design - Mobile First */
@media (max-width: 768px) {
  .chat-container {
    position: relative;
  }

  .chat-sidebar {
    width: 100%;
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    z-index: 10;
    transition: transform 0.3s ease;
  }
  
  .chat-sidebar.hidden {
    transform: translateX(-100%);
  }

  .chat-main {
    width: 100%;
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    transform: translateX(100%);
    transition: transform 0.3s ease;
  }

  .chat-main.visible {
    transform: translateX(0);
  }

  .btn-back {
    display: inline-block;
  }

  /* Hide empty state on mobile */
  #emptyHeader {
    display: none !important;
  }

  /* Fix avatar size - Giảm kích thước avatar trên mobile */
  .user-info .avatar {
    width: 40px;
    height: 40px;
  }

  .chat-header-info .avatar {
    width: 36px;
    height: 36px;
  }

  .conversation-item .avatar {
    width: 45px;
    height: 45px;
  }

  .user-info h3 {
    font-size: 16px;
  }

  .chat-header-info h3 {
    font-size: 15px;
  }

  /* Fix padding để input không bị che */
  .chat-header {
    padding: 12px 15px;
  }

  .messages-container {
    padding: 15px;
    padding-bottom: 10px;
  }

  .chat-input-area {
    padding: 10px 12px;
  }

  /* Fix emoji picker trên mobile */
  .emoji-picker {
    width: 90vw;
    max-width: 350px;
    bottom: 65px;
    left: 5vw;
  }

  /* Fix header actions buttons */
  .header-actions {
    gap: 8px;
  }

  .btn-notification,
  .btn-logout,
  .btn-upload,
  .btn-emoji {
    width: 36px;
    height: 36px;
    font-size: 18px;
  }

  /* Đảm bảo chat container fit viewport trên mobile */
  .chat-container {
    height: 100vh;
    height: 100dvh; /* Dynamic viewport height - better for mobile */
    overflow: hidden;
  }

  /* Message input không bị keyboard che */
  .chat-input-area form {
    display: flex;
    align-items: center;
    gap: 8px;
  }

  #messageInput {
    flex: 1;
    font-size: 16px; /* Prevent iOS zoom on focus */
  }

  /* Fix typing indicator trên mobile */
  .typing-indicator {
    padding: 8px 15px;
  }
}

/* Small phones (iPhone SE, etc.) */
@media (max-width: 375px) {
  .user-info .avatar {
    width: 36px;
    height: 36px;
  }

  .user-info h3 {
    font-size: 14px;
  }

  .chat-header {
    padding: 10px 12px;
  }

  .messages-container {
    padding: 10px;
  }

  .chat-input-area {
    padding: 8px 10px;
  }

  .btn-notification,
  .btn-logout,
  .btn-upload,
  .btn-emoji {
    width: 32px;
    height: 32px;
    font-size: 16px;
  }

  .message-bubble {
    max-width: 240px;
    font-size: 14px;
  }

  /* Nút Gửi lớn hơn trên mobile nhỏ */
  .btn-send {
    padding: 10px 18px;
    font-size: 14px;
    min-width: 60px;
  }
}

/* Mobile - Nút Gửi lớn hơn */
@media (max-width: 768px) {
  .btn-send {
    padding: 10px 24px;
    font-size: 15px;
    min-width: 70px;
    border-radius: 20px;
  }
}

/* Image Preview Modal */
.image-modal {
  display: none;
  position: fixed;
  z-index: 9999;
  padding-top: 50px;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: rgba(0, 0, 0, 0.95);
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.image-modal-content {
  margin: auto;
  display: block;
  max-width: 90%;
  max-height: 80vh;
  object-fit: contain;
  animation: zoomIn 0.3s ease;
}

@keyframes zoomIn {
  from { transform: scale(0.5); }
  to { transform: scale(1); }
}

.image-modal-close {
  position: absolute;
  top: 20px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
  z-index: 10000;
}

.image-modal-close:hover,
.image-modal-close:focus {
  color: #bbb;
  transform: scale(1.2);
}

.image-modal-caption {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
  text-align: center;
  color: #ccc;
  padding: 10px 0;
  font-size: 14px;
}

/* Mobile adjustments for image modal */
@media (max-width: 768px) {
  .image-modal {
    padding-top: 30px;
  }

  .image-modal-content {
    max-width: 95%;
    max-height: 70vh;
  }

  .image-modal-close {
    top: 10px;
    right: 20px;
    font-size: 35px;
  }

  .image-modal-caption {
    font-size: 12px;
    width: 90%;
  }
}

/* Tablet responsive */
@media (min-width: 769px) and (max-width: 1024px) {
  .chat-sidebar {
    width: 320px;
  }
}
