/* ===== CSS Variables ===== */
:root {
  --color-bg: #ffffff;
  --color-surface: #f5f6fa;
  --color-sidebar: #f0f1f5;
  --color-primary: #1a1a2e;
  --color-primary-light: #2d2d4e;
  --color-accent: #4f8cff;
  --color-accent-hover: #3a7bf5;
  --color-accent-light: #eef3ff;
  --color-text: #1a1a2e;
  --color-text-secondary: #6b7280;
  --color-text-muted: #9ca3af;
  --color-border: #e5e7eb;
  --color-border-light: #f0f1f5;
  --color-self-bubble: #4f8cff;
  --color-self-bubble-text: #ffffff;
  --color-other-bubble: #f0f0f5;
  --color-other-bubble-text: #1a1a2e;
  --color-online: #22c55e;
  --color-offline: #9ca3af;
  --color-danger: #ef4444;
  --color-danger-hover: #dc2626;
  --color-badge: #ef4444;
  --color-input-bg: #f5f6fa;
  --color-overlay: rgba(0, 0, 0, 0.45);
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --radius-xl: 20px;
  --radius-full: 50%;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.08);
  --shadow-md: 0 4px 14px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 10px 40px rgba(0, 0, 0, 0.15);
  --sidebar-width: 280px;
  --transition: 0.2s ease;
  --transition-slow: 0.3s ease;
}

/* ===== Reset & Base ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 14px;
  line-height: 1.5;
  color: var(--color-text);
  background: var(--color-bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

input, textarea, button {
  font-family: inherit;
  font-size: inherit;
}

button {
  cursor: pointer;
  border: none;
  background: none;
}

a {
  color: var(--color-accent);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

input:focus, textarea:focus, button:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

.hidden {
  display: none !important;
}

/* ===== Page Transitions ===== */
.page {
  display: none;
  height: 100%;
}

.page.active {
  display: block;
}

/* ===== Login / Register ===== */
#page-login, #page-register {
  display: none;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: var(--color-surface);
  padding: 20px;
}

#page-login.active, #page-register.active {
  display: flex;
}

.auth-box {
  width: 100%;
  max-width: 380px;
  background: var(--color-bg);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 40px 32px;
}

.auth-logo {
  font-size: 28px;
  font-weight: 700;
  text-align: center;
  color: var(--color-primary);
  margin-bottom: 4px;
}

.auth-sub {
  text-align: center;
  color: var(--color-text-secondary);
  font-size: 13px;
  margin-bottom: 28px;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.auth-form input {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-input-bg);
  color: var(--color-text);
  transition: border-color var(--transition), box-shadow var(--transition);
}

.auth-form input:focus {
  border-color: var(--color-accent);
  box-shadow: 0 0 0 3px var(--color-accent-light);
  background: var(--color-bg);
}

.auth-form button {
  padding: 12px;
  background: var(--color-primary);
  color: #fff;
  border-radius: var(--radius-md);
  font-weight: 600;
  font-size: 15px;
  letter-spacing: 1px;
  transition: background var(--transition), transform var(--transition);
}

.auth-form button:hover {
  background: var(--color-primary-light);
}

.auth-form button:active {
  transform: scale(0.98);
}

.auth-switch {
  text-align: center;
  font-size: 13px;
  color: var(--color-text-secondary);
}

.auth-error {
  color: var(--color-danger);
  font-size: 13px;
  text-align: center;
  min-height: 20px;
}

/* ===== App Layout ===== */
#page-app {
  height: 100vh;
  overflow: hidden;
}

.app-layout {
  display: flex;
  height: 100vh;
}

/* ===== Sidebar ===== */
.sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  height: 100vh;
  background: var(--color-sidebar);
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--color-border);
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 16px 12px;
  border-bottom: 1px solid var(--color-border);
}

.sidebar-header h2 {
  font-size: 18px;
  font-weight: 700;
  color: var(--color-primary);
}

.sidebar-user {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--color-text-secondary);
  max-width: 120px;
}

.sidebar-user span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#btn-logout {
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  color: var(--color-text-secondary);
  transition: background var(--transition), color var(--transition);
}

#btn-logout:hover {
  background: var(--color-danger);
  color: #fff;
}

/* ===== Sidebar Search ===== */
.sidebar-search {
  padding: 10px 16px;
  position: relative;
}

#search-user {
  width: 100%;
  padding: 9px 12px;
  border: 1.5px solid transparent;
  border-radius: var(--radius-md);
  background: var(--color-bg);
  color: var(--color-text);
  font-size: 13px;
  transition: border-color var(--transition), box-shadow var(--transition);
}

#search-user:focus {
  border-color: var(--color-accent);
  box-shadow: 0 0 0 3px var(--color-accent-light);
}

#search-user::placeholder {
  color: var(--color-text-muted);
}

/* ===== Search Results Dropdown ===== */
.search-results {
  position: absolute;
  top: 100%;
  left: 16px;
  right: 16px;
  z-index: 100;
  background: var(--color-bg);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  max-height: 240px;
  overflow-y: auto;
  padding: 6px 0;
}

.search-results:not(.hidden) {
  display: block;
}

.search-results .search-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  cursor: pointer;
  transition: background var(--transition);
}

.search-results .search-item:hover {
  background: var(--color-accent-light);
}

.search-results .search-item .avatar {
  --av-size: 32px;
  --av-font: 13px;
}
.invite-item .avatar {
  --av-size: 32px;
  --av-font: 13px;
  background: var(--color-accent-light);
  color: var(--color-accent);
}

.search-results .search-item .name {
  font-size: 13px;
  font-weight: 500;
}

.search-results .search-item .hint {
  font-size: 12px;
  color: var(--color-text-secondary);
  margin-left: auto;
}

/* ===== Sidebar Tabs ===== */
.sidebar-tabs {
  display: flex;
  border-bottom: 1px solid var(--color-border);
  padding: 0 4px;
}

.tab {
  flex: 1;
  padding: 10px 8px;
  font-size: 13px;
  font-weight: 500;
  color: var(--color-text-secondary);
  position: relative;
  text-align: center;
  transition: color var(--transition);
  white-space: nowrap;
}

.tab::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 2.5px;
  background: var(--color-accent);
  border-radius: 2px;
  transition: width var(--transition);
}

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

.tab.active {
  color: var(--color-accent);
}

.tab.active::after {
  width: 60%;
}

/* ===== Tab Content ===== */
.tab-content {
  display: none;
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.tab-content.active {
  display: block;
}

.tab-actions {
  padding: 10px 16px;
}

#btn-create-group {
  width: 100%;
  padding: 9px;
  border: 1.5px dashed var(--color-border);
  border-radius: var(--radius-md);
  font-size: 13px;
  font-weight: 500;
  color: var(--color-accent);
  transition: background var(--transition), border-color var(--transition);
}

#btn-create-group:hover {
  background: var(--color-accent-light);
  border-color: var(--color-accent);
}

/* ===== Contact List ===== */
.contact-list {
  padding: 4px 0;
}

.contact-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  cursor: pointer;
  transition: background var(--transition);
}

.contact-item:hover {
  background: rgba(0, 0, 0, 0.04);
}

.contact-item.active {
  background: var(--color-accent-light);
}

.contact-item .avatar {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-full);
  background: var(--color-accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  font-weight: 600;
  flex-shrink: 0;
  position: relative;
}

.contact-item .avatar .status-dot {
  position: absolute;
  bottom: 1px;
  right: 1px;
  width: 10px;
  height: 10px;
  border-radius: var(--radius-full);
  border: 2px solid var(--color-sidebar);
}

.contact-item .avatar .status-dot.online {
  background: var(--color-online);
}

.contact-item .avatar .status-dot.offline {
  background: var(--color-offline);
}

.contact-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

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

.contact-status {
  font-size: 12px;
  color: var(--color-text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.contact-status.online {
  color: var(--color-online);
}

/* ===== Badge ===== */
.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--color-badge);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  line-height: 1;
  vertical-align: middle;
}

/* ===== Chat Area ===== */
.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100vh;
  min-width: 0;
  background: var(--color-bg);
}

/* ===== Empty State ===== */
.chat-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--color-text-muted);
  font-size: 15px;
}

#chat-active {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

#chat-active.hidden { display: none !important; }

.chat-empty p {
  padding: 40px;
  text-align: center;
}

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

#chat-title {
  font-size: 16px;
  font-weight: 600;
}

.chat-subtitle {
  font-size: 12px;
  color: var(--color-text-secondary);
  margin-left: auto;
}

/* ===== Chat Messages ===== */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overscroll-behavior: contain;
  background: var(--color-bg);
}

.msg {
  display: flex;
  flex-direction: column;
  max-width: 70%;
  animation: msgFadeIn 0.2s ease;
}

@keyframes msgFadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.msg-self {
  align-self: flex-end;
  align-items: flex-end;
}

.msg-other {
  align-self: flex-start;
  align-items: flex-start;
}

.msg-sender {
  font-size: 12px;
  font-weight: 500;
  color: var(--color-accent);
  margin-bottom: 2px;
  padding: 0 4px;
}

.msg-bubble {
  padding: 10px 14px;
  border-radius: 18px;
  word-break: break-word;
  white-space: pre-wrap;
  line-height: 1.45;
  position: relative;
}

.msg-self .msg-bubble {
  background: var(--color-self-bubble);
  color: var(--color-self-bubble-text);
  border-bottom-right-radius: 4px;
}

.msg-other .msg-bubble {
  background: var(--color-other-bubble);
  color: var(--color-other-bubble-text);
  border-bottom-left-radius: 4px;
}

.msg-text { font-size: 14px; }
.msg-time { font-size: 11px; color: var(--color-text-muted); margin-top: 2px; padding: 0 4px; }
.msg-self .msg-time { text-align: right; }

.msg-image { max-width: 280px; max-height: 320px; border-radius: var(--radius-md); cursor: pointer; display: block; transition: opacity var(--transition); }
.msg-image:hover { opacity: 0.9; }

.msg-audio { width: 240px; height: 40px; outline: none; border-radius: var(--radius-sm); }
.msg-audio::-webkit-media-controls-panel { background: var(--color-surface); }

.msg-file-link {
  display: flex; align-items: center; gap: 6px; font-size: 13px;
  padding: 6px 10px; border-radius: var(--radius-sm); transition: background var(--transition);
}
.msg-self .msg-file-link { color: var(--color-self-bubble-text); background: rgba(255,255,255,0.15); }
.msg-self .msg-file-link:hover { background: rgba(255,255,255,0.25); }
.msg-other .msg-file-link { color: var(--color-accent); background: var(--color-accent-light); }

.msg-loading { text-align: center; color: var(--color-text-muted); padding: 20px; font-size: 13px; }

/* ===== Chat Input Area ===== */
.chat-input-area {
  border-top: 1px solid var(--color-border);
  background: var(--color-bg);
  padding: 0 16px 12px;
}

/* ===== Toolbar ===== */
.chat-toolbar {
  display: flex;
  gap: 4px;
  padding: 8px 0;
}

.tool-btn {
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  font-size: 18px;
  cursor: pointer;
  transition: background var(--transition);
}

.tool-btn:hover {
  background: var(--color-surface);
}

.tool-btn:active {
  background: var(--color-border);
}

/* ===== Voice Recording Bar ===== */
.voice-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
  font-size: 13px;
}

.voice-bar:not(.hidden) {
  display: flex;
}

.voice-bar span:first-child {
  color: var(--color-danger);
  font-weight: 500;
}

#voice-timer {
  font-variant-numeric: tabular-nums;
  color: var(--color-text-secondary);
  min-width: 32px;
}

.voice-bar button {
  padding: 6px 16px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  transition: background var(--transition);
}

#btn-send-voice {
  background: var(--color-accent);
  color: #fff;
}

#btn-send-voice:hover {
  background: var(--color-accent-hover);
}

#btn-cancel-voice {
  background: var(--color-surface);
  color: var(--color-text-secondary);
}

#btn-cancel-voice:hover {
  background: var(--color-border);
  color: var(--color-text);
}

/* ===== Input Row ===== */
.chat-input-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}

#chat-input {
  flex: 1;
  padding: 10px 14px;
  border: 1.5px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-input-bg);
  color: var(--color-text);
  resize: none;
  min-height: 40px;
  max-height: 120px;
  line-height: 1.45;
  transition: border-color var(--transition);
}

#chat-input:focus {
  border-color: var(--color-accent);
  background: var(--color-bg);
}

#chat-input::placeholder {
  color: var(--color-text-muted);
}

#btn-send {
  padding: 10px 22px;
  background: var(--color-accent);
  color: #fff;
  border-radius: var(--radius-md);
  font-weight: 600;
  font-size: 14px;
  white-space: nowrap;
  transition: background var(--transition), transform var(--transition);
}

#btn-send:hover {
  background: var(--color-accent-hover);
}

#btn-send:active {
  transform: scale(0.97);
}

/* ===== Modal Overlay ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: var(--color-overlay);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  animation: fadeIn 0.2s ease;
}

.modal-overlay:not(.hidden) {
  display: flex;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal {
  background: var(--color-bg);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 480px;
  max-height: 80vh;
  overflow-y: auto;
  padding: 28px;
  animation: modalSlide 0.25s ease;
}

@keyframes modalSlide {
  from {
    opacity: 0;
    transform: translateY(12px) scale(0.97);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* ===== Scrollbar Styling ===== */
::-webkit-scrollbar {
  width: 5px;
  height: 5px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--color-border);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--color-text-muted);
}

/* ===== Responsive: Mobile ===== */
@media (max-width: 768px) {
  .app-layout {
    flex-direction: column;
  }

  .sidebar {
    width: 100%;
    min-width: 0;
    height: auto;
    max-height: 50vh;
    border-right: none;
    border-bottom: 1px solid var(--color-border);
  }

  .sidebar-header {
    padding: 12px 14px;
  }

  .sidebar-header h2 {
    font-size: 16px;
  }

  .sidebar-search {
    padding: 8px 14px;
  }

  .contact-item {
    padding: 8px 14px;
  }

  .chat-area {
    height: calc(100vh - 50vh);
    min-height: 300px;
  }

  .chat-messages {
    padding: 12px 14px;
  }

  .message {
    max-width: 85%;
  }

  .message-image img {
    max-width: 220px;
    max-height: 260px;
  }

  .message-audio audio {
    width: 200px;
  }

  .auth-box {
    padding: 28px 20px;
  }

  .modal {
    padding: 20px;
    max-width: 100%;
    margin: 10px;
  }
}

@media (max-width: 480px) {
  .sidebar {
    max-height: 40vh;
  }

  .chat-area {
    height: calc(100vh - 40vh);
    min-height: 250px;
  }

  .chat-header {
    padding: 10px 14px;
  }

  #chat-title {
    font-size: 14px;
  }

  .chat-input-area {
    padding: 0 10px 10px;
  }

  #btn-send {
    padding: 10px 16px;
    font-size: 13px;
  }
}

/* ===== Small Buttons ===== */
.btn-sm {
  padding: 4px 12px;
  font-size: 12px;
  border-radius: var(--radius-sm);
  font-weight: 500;
  transition: background var(--transition), color var(--transition);
}
.btn-accept {
  background: var(--color-accent);
  color: #fff;
}
.btn-accept:hover {
  background: var(--color-accent-hover);
}
.btn-reject {
  background: var(--color-border);
  color: var(--color-text-secondary);
}
.btn-reject:hover {
  background: #d1d5db;
}
.btn-add-friend {
  padding: 4px 10px;
  font-size: 11px;
  border-radius: var(--radius-sm);
  background: var(--color-accent);
  color: #fff;
  white-space: nowrap;
  transition: background var(--transition);
}
.btn-add-friend:hover {
  background: var(--color-accent-hover);
}

/* ===== Contact List Extras ===== */
.contact-empty {
  text-align: center;
  color: var(--color-text-muted);
  padding: 30px 16px;
  font-size: 13px;
}
.search-empty {
  text-align: center;
  color: var(--color-text-muted);
  padding: 16px;
  font-size: 13px;
}
.group-avatar {
  background: var(--color-accent-light) !important;
  color: var(--color-accent) !important;
  font-weight: 700;
}
.request-item {
  cursor: default !important;
}
.request-item .contact-name {
  font-weight: 600;
}
.request-actions {
  display: flex;
  gap: 6px;
  margin-left: auto;
  flex-shrink: 0;
}

/* ===== Search Add Button ===== */
.search-add-btn {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-full);
  background: var(--color-accent);
  color: #fff;
  font-size: 18px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background var(--transition), transform var(--transition);
}
.search-add-btn:hover {
  background: var(--color-accent-hover);
  transform: scale(1.1);
}
.search-add-btn:active {
  transform: scale(0.95);
}

/* ===== Toast ===== */
.toast {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
  background: var(--color-primary);
  color: #fff;
  padding: 12px 24px;
  border-radius: var(--radius-md);
  font-size: 14px;
  box-shadow: var(--shadow-lg);
  animation: toastIn 0.3s ease;
  max-width: 90vw;
  text-align: center;
}
.toast.hidden { display: none !important; }

@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(-20px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ===== Group Invite ===== */
.btn-invite {
  background: var(--color-accent);
  color: #fff;
  border: none;
  padding: 2px 10px;
  border-radius: var(--radius-sm);
  font-size: 12px;
  cursor: pointer;
  margin-left: 8px;
  transition: background var(--transition);
}
.btn-invite:hover {
  background: var(--color-accent-hover);
}

.invite-list {
  margin-top: 12px;
  max-height: 300px;
  overflow-y: auto;
}
.invite-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 4px;
  border-bottom: 1px solid var(--color-border-light);
}
.invite-item:last-child {
  border-bottom: none;
}
.invite-item .avatar {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-full);
  background: var(--color-accent-light);
  color: var(--color-accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 600;
  flex-shrink: 0;
}
.invite-name {
  flex: 1;
  font-size: 14px;
}
