html, body {
  height: var(--vh, 100%);
  max-height: var(--vh, 100%);
  overflow: hidden;
  width: 100%;
  margin: 0;
  padding: 0;
  overscroll-behavior: none;
  -webkit-overflow-scrolling: touch;
  position: fixed;
  top: 0;
  left: 0;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background: #f5f5f5;
}

/* ==================== Animations ==================== */
.explosion-particle {
  position: fixed; pointer-events: none; z-index: 9999; font-size: 20px;
  animation: explode 0.6s ease-out forwards;
}
@keyframes explode {
  0% { transform: translate(0, 0) scale(1); opacity: 1; }
  100% { transform: translate(var(--tx, 50px), var(--ty, -50px)) scale(0.2); opacity: 0; }
}
.message-dissolve { animation: dissolve 0.5s ease-out forwards; }
@keyframes dissolve {
  0% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.5; transform: scale(1.05); }
  100% { opacity: 0; transform: scale(0.8); }
}
@keyframes slideIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
@keyframes badge-pop { 0% { transform: scale(0); } 60% { transform: scale(1.3); } 100% { transform: scale(1); } }
@keyframes pulse-notify { 0%, 100% { background: #fff3e0; } 50% { background: #ffe0b2; } }

/* ==================== Auth ==================== */
.auth-container {
  width: 100%; height: var(--vh, 100dvh); display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); overflow-y: auto;
}
.auth-form-container {
  background: white; padding: 40px; border-radius: 10px; box-shadow: 0 10px 40px rgba(0,0,0,0.2);
  max-width: 400px; width: 90%;
}
.auth-form-container h1 { text-align: center; margin-bottom: 30px; color: #333; font-size: 28px; }
.auth-tabs { display: flex; margin-bottom: 30px; border-bottom: 2px solid #eee; }
.tab-button {
  flex: 1; padding: 12px; background: none; border: none; cursor: pointer; font-size: 14px;
  font-weight: 600; color: #999; border-bottom: 3px solid transparent; transition: all 0.3s ease;
}
.tab-button.active { color: #667eea; border-bottom-color: #667eea; }
.auth-form { display: none; }
.auth-form.active { display: flex; flex-direction: column; gap: 15px; }
.auth-form input[type="text"], .auth-form input[type="password"] {
  padding: 12px; border: 1px solid #ddd; border-radius: 5px; font-size: 14px; transition: border-color 0.3s ease;
}
.auth-form input:focus { outline: none; border-color: #667eea; box-shadow: 0 0 5px rgba(102,126,234,0.3); }
.password-wrapper { position: relative; display: flex; align-items: center; }
.password-wrapper input { flex: 1; padding-right: 40px; }
.pw-toggle { position: absolute; right: 8px; background: none; border: none; font-size: 18px; cursor: pointer; padding: 4px; opacity: 0.7; transition: opacity 0.2s; }
.pw-toggle:hover { opacity: 1; }
.invite-info { text-align: center; color: #667eea; font-size: 13px; }
.btn-primary {
  padding: 12px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white;
  border: none; border-radius: 5px; font-size: 16px; font-weight: 600; cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.btn-primary:hover { transform: translateY(-2px); box-shadow: 0 5px 15px rgba(102,126,234,0.4); }
.error-message { color: #d32f2f; font-size: 13px; text-align: center; min-height: 20px; }

/* ==================== Chat Container ==================== */
.chat-container {
  width: 100%; height: var(--vh, 100dvh); display: flex; flex-direction: column;
  background: #f5f5f5; overflow: hidden; position: relative;
}
.screen { display: none; flex-direction: column; flex: 1; min-height: 0; height: 100%; }
.screen.active-screen { display: flex; }

/* ==================== Header ==================== */
.chat-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white;
  padding: 10px 15px; display: flex; justify-content: space-between; align-items: center;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1); flex-shrink: 0; gap: 10px;
}
.chat-header h1 { font-size: 20px; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.header-info { display: flex; align-items: center; gap: 12px; flex-shrink: 0; }
.header-user-small { font-size: 11px; opacity: 0.8; }
#currentUser { font-weight: 600; }

/* ==================== User Menu Dropdown ==================== */
.user-menu-wrapper { position: relative; }
.btn-user-menu {
  display: flex; align-items: center; gap: 6px; background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.4); color: white; padding: 5px 12px 5px 6px;
  border-radius: 20px; cursor: pointer; font-size: 13px; font-weight: 600;
  transition: all 0.2s; white-space: nowrap;
}
.btn-user-menu:hover { background: rgba(255,255,255,0.25); }
.header-avatar-canvas {
  width: 24px; height: 24px; border-radius: 50%;
  image-rendering: pixelated; image-rendering: crisp-edges;
  flex-shrink: 0;
}
.user-dropdown {
  position: absolute; top: calc(100% + 6px); right: 0; background: white;
  border-radius: 10px; box-shadow: 0 6px 24px rgba(0,0,0,0.18); min-width: 200px;
  z-index: 200; overflow: hidden; display: flex; flex-direction: column;
}
.dropdown-item {
  display: flex; align-items: center; gap: 8px; padding: 12px 16px; border: none;
  background: none; font-size: 14px; color: #333; cursor: pointer; text-align: left;
  transition: background 0.15s; width: 100%;
}
.dropdown-item:hover { background: #f0eeff; }
.dropdown-item.logout { color: #d32f2f; }
.dropdown-item.logout:hover { background: #ffebee; }
.dropdown-divider { height: 1px; background: #eee; margin: 0; }

.btn-logout {
  background: rgba(255,255,255,0.2); color: white; border: 1px solid rgba(255,255,255,0.5);
  padding: 6px 14px; border-radius: 5px; cursor: pointer; font-size: 13px; transition: all 0.3s ease; white-space: nowrap;
}
.btn-logout:hover { background: rgba(255,255,255,0.3); border-color: white; }
.btn-back {
  background: rgba(255,255,255,0.2); color: white; border: 1px solid rgba(255,255,255,0.4);
  padding: 6px 12px; border-radius: 8px; cursor: pointer; font-size: 16px; font-weight: 700;
  transition: all 0.2s ease; flex-shrink: 0; line-height: 1;
}
.btn-back:hover { background: rgba(255,255,255,0.3); border-color: white; }
.btn-back:active { transform: scale(0.95); }
.btn-settings {
  background: rgba(255,255,255,0.2); border: 1px solid rgba(255,255,255,0.4);
  color: white; padding: 6px 10px; border-radius: 8px; cursor: pointer; font-size: 16px;
  transition: all 0.2s; flex-shrink: 0;
}
.btn-settings:hover { background: rgba(255,255,255,0.3); }

/* ==================== Contacts Screen ==================== */
.contacts-body { flex: 1; overflow-y: auto; padding: 12px; -webkit-overflow-scrolling: touch; background: #f5f5f5 url('/bg-pattern-light.png') repeat; background-size: 300px 300px; }

.contacts-top-section {
  max-height: 600px; opacity: 1; overflow: hidden;
  transition: max-height 0.3s ease, opacity 0.25s ease;
}
.contacts-top-section.collapsed {
  max-height: 0; opacity: 0; pointer-events: none;
}
.contacts-btn-row {
  display: flex; gap: 6px; margin-bottom: 8px;
}

.contacts-group-btn {
  display: flex; align-items: center; justify-content: center; gap: 6px; flex: 1; min-width: 0; padding: 10px 8px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border: none;
  border-radius: 10px; font-size: 12px; font-weight: 600; cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s; box-shadow: 0 2px 8px rgba(102,126,234,0.3);
  position: relative;
}
.contacts-group-btn:hover { transform: translateY(-1px); box-shadow: 0 4px 14px rgba(102,126,234,0.4); }
.contacts-group-btn:active { transform: translateY(0); }
.contacts-group-btn { text-decoration: none; }
.group-btn-icon { font-size: 16px; }
.group-btn-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.btn-inline-create {
  width: 28px; height: 28px; border-radius: 50%; border: none;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white; font-size: 18px; font-weight: bold; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  line-height: 1; padding: 0 0 1px 0;
}
.create-group-form {
  display: flex; flex-direction: column; gap: 6px; margin-bottom: 8px; padding: 8px;
  background: rgba(230,230,235,0.7); border-radius: 10px;
}
.create-group-form input[type="text"] {
  padding: 8px 10px; border: 1px solid rgba(200,200,210,0.6); border-radius: 8px; font-size: 13px;
}
.create-group-form .checkbox-label { font-size: 12px; display: flex; align-items: center; gap: 6px; }
.create-group-form .btn-primary {
  padding: 6px 12px; background: linear-gradient(135deg, #667eea, #764ba2); color: white;
  border: none; border-radius: 8px; font-size: 13px; font-weight: 600; cursor: pointer;
}

/* ==================== Add Friend ==================== */
.contacts-add-friend { display: flex; gap: 8px; margin-bottom: 8px; }
.contacts-add-friend input {
  flex: 1; padding: 10px 12px; border: 1px solid rgba(200,200,210,0.6); border-radius: 10px;
  font-size: 14px; transition: border-color 0.2s; background: rgba(230, 230, 235, 0.7); backdrop-filter: blur(2px); -webkit-backdrop-filter: blur(2px);
}
.contacts-add-friend input:focus { outline: none; border-color: #667eea; box-shadow: 0 0 5px rgba(102,126,234,0.2); }
.btn-add-friend {
  padding: 8px 14px; background: linear-gradient(135deg, #667eea, #764ba2); color: white;
  border: none; border-radius: 10px; font-size: 13px; font-weight: bold; cursor: pointer;
  flex-shrink: 0; transition: transform 0.2s; white-space: nowrap;
}
.btn-add-friend:active { transform: scale(0.95); }

/* ==================== Invite Link ====================*/
.invite-link-wrapper {
  display: flex; gap: 8px; margin-bottom: 12px; align-items: stretch;
}
.btn-invite-link {
  flex: 1; padding: 10px; background: rgba(230, 230, 235, 0.7); border: 1px dashed rgba(150,150,160,0.6);
  border-radius: 10px; cursor: pointer; font-size: 14px; color: #333;
  transition: all 0.2s; text-align: center; backdrop-filter: blur(2px); -webkit-backdrop-filter: blur(2px);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.btn-invite-link.has-link {
  font-size: 11px; text-align: left; color: #667eea; border-color: #667eea;
  background: rgba(230, 230, 245, 0.7); cursor: default;
}
.btn-invite-link:hover { background: rgba(220, 220, 230, 0.8); border-color: #667eea; color: #667eea; }
.btn-share-link {
  width: 44px; min-width: 44px; background: #667eea; color: white; border: none;
  border-radius: 10px; cursor: pointer; font-size: 18px;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.2s;
}
.btn-share-link:hover { background: #5a6fd6; }
.btn-share-link:active { transform: scale(0.95); }
.btn-copy-link {
  width: 44px; min-width: 44px; background: #43a047; color: white; border: none;
  border-radius: 10px; cursor: pointer; font-size: 18px;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.2s;
}
.btn-copy-link:hover { background: #388e3c; }
.btn-copy-link:active { transform: scale(0.95); }
.btn-new-link {
  width: 44px; min-width: 44px; background: #ff9800; color: white; border: none;
  border-radius: 10px; cursor: pointer; font-size: 18px;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.2s;
}
.btn-new-link:hover { background: #f57c00; }
.btn-new-link:active { transform: scale(0.95); }

/* Install Guide Overlay */
.install-guide-overlay {
  position: fixed; top: 0; left: 0; width: 100%; height: 100%;
  background: rgba(0,0,0,0.5); z-index: 9999;
  display: flex; align-items: center; justify-content: center;
  padding: 20px; box-sizing: border-box;
}
.install-guide-content {
  background: #fff; border-radius: 16px; padding: 24px;
  max-width: 420px; width: 100%; max-height: 80vh; overflow-y: auto;
  box-shadow: 0 8px 32px rgba(0,0,0,0.2);
  font-size: 15px; line-height: 1.6; color: #333;
}
.install-guide-content h2 { margin: 0 0 12px; font-size: 20px; }
.install-guide-content ol, .install-guide-content ul { margin: 8px 0; padding-left: 24px; }
.install-guide-content li { margin-bottom: 6px; }
.install-guide-content p { margin: 6px 0; }

.invite-link-result {
  padding: 8px 12px; background: #e8f5e9; color: #2e7d32; border-radius: 8px;
  font-size: 13px; margin-bottom: 12px; word-break: break-all;
}

/* ==================== Friend Requests ==================== */
.friend-request-item { gap: 8px; }
.btn-accept {
  background: #4caf50; color: white; border: none; padding: 6px 12px; border-radius: 6px;
  cursor: pointer; font-weight: bold; font-size: 14px; transition: transform 0.2s;
}
.btn-accept:active { transform: scale(0.95); }
.btn-decline {
  background: #e53935; color: white; border: none; padding: 6px 12px; border-radius: 6px;
  cursor: pointer; font-weight: bold; font-size: 14px; transition: transform 0.2s;
}
.btn-decline:active { transform: scale(0.95); }

/* ==================== Group Invites ==================== */
.group-invite-item {
  flex-wrap: wrap; gap: 8px;
}
.group-invite-info {
  flex: 1; display: flex; flex-direction: column; gap: 2px; min-width: 0;
}
.group-invite-info strong { font-size: 14px; }
.group-invite-from { font-size: 12px; color: #999; }
.group-invite-actions { display: flex; gap: 6px; }

/* ==================== Contacts Sections ==================== */
.contacts-section { margin-bottom: 16px; }
.contacts-section h3 { font-size: 13px; color: #999; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 8px; padding: 0 4px; }
.contacts-list { list-style: none; padding: 0; margin: 0; }

.contact-item {
  display: flex; align-items: center; gap: 10px; padding: 12px 14px; background: rgba(230, 230, 235, 0.7);
  border-radius: 10px; margin-bottom: 6px; cursor: pointer; backdrop-filter: blur(2px); -webkit-backdrop-filter: blur(2px);
  transition: background 0.2s, transform 0.15s; box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}
.contact-item:hover { background: rgba(224, 222, 243, 0.8); transform: translateX(2px); }
.contact-item:active { transform: scale(0.98); }
.contact-item.has-notification { background: rgba(255, 243, 224, 0.8); animation: pulse-notify 1.5s infinite; }
.contact-status-dot { font-size: 14px; flex-shrink: 0; width: 18px; text-align: center; }
.contact-status-dot.online { color: #4caf50; }
.contact-status-dot.offline { color: #ccc; }
.contact-name { flex: 1; font-size: 15px; font-weight: 500; color: #333; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.unread-badge {
  background: #e91e63; color: white; font-size: 12px; font-weight: bold; padding: 3px 8px;
  border-radius: 12px; flex-shrink: 0; animation: badge-pop 0.3s ease; white-space: nowrap;
}
.btn-remove-friend {
  background: none; border: none; cursor: pointer; font-size: 14px; padding: 4px 6px;
  border-radius: 4px; color: #ccc; opacity: 0; transition: all 0.2s; flex-shrink: 0;
}
.contact-item:hover .btn-remove-friend { opacity: 1; }
.btn-remove-friend:hover { background: #ffcdd2; color: #d32f2f; }

/* ==================== Channels List ==================== */
.channels-list { display: flex; flex-direction: column; gap: 8px; }
.channel-item {
  display: flex; justify-content: space-between; align-items: center; padding: 16px;
  background: rgba(230, 230, 235, 0.7); border-radius: 12px; cursor: pointer; transition: all 0.2s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05); backdrop-filter: blur(2px); -webkit-backdrop-filter: blur(2px);
}
.channel-item:hover { background: rgba(224, 222, 243, 0.8); transform: translateX(2px); }
.channel-item:active { transform: scale(0.98); }
.channel-info { display: flex; flex-direction: column; gap: 4px; }
.channel-name { font-size: 16px; font-weight: 600; color: #333; }
.channel-desc { font-size: 12px; color: #999; }
.channel-meta { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.channel-active { font-size: 12px; color: #4caf50; font-weight: 500; }

/* ==================== Groups ==================== */
.btn-create-group {
  width: 100%; padding: 12px; background: linear-gradient(135deg, #43a047, #2e7d32); color: white;
  border: none; border-radius: 10px; font-size: 15px; font-weight: 600; cursor: pointer;
  margin-bottom: 12px; transition: transform 0.2s;
}
.btn-create-group:hover { transform: translateY(-1px); }
.btn-create-group:active { transform: translateY(0); }
.create-group-form {
  background: rgba(230, 230, 235, 0.7); padding: 16px; border-radius: 12px; margin-bottom: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1); display: flex; flex-direction: column; gap: 10px;
  backdrop-filter: blur(2px); -webkit-backdrop-filter: blur(2px);
}
.create-group-form input[type="text"],
.create-group-form input[type="date"],
.create-group-form input[type="time"] {
  padding: 10px; border: 1px solid #ddd; border-radius: 8px; font-size: 14px;
}
.create-group-form input:focus { outline: none; border-color: #667eea; }
.checkbox-label {
  display: flex; align-items: center; gap: 8px; font-size: 14px; color: #555; cursor: pointer;
}
.checkbox-label input[type="checkbox"] { width: 18px; height: 18px; accent-color: #667eea; }

.settings-section { margin-bottom: 20px; }
.settings-section h3 { font-size: 14px; color: #667eea; font-weight: 600; margin-bottom: 10px; }
.add-member-select {
  flex: 1; padding: 10px; border: 1px solid #ddd; border-radius: 10px; font-size: 14px; background: white;
}

/* ==================== Events ==================== */
.events-list, .polls-list { display: flex; flex-direction: column; gap: 10px; margin-top: 10px; }
.event-card, .poll-card {
  background: white; padding: 14px; border-radius: 12px; box-shadow: 0 1px 4px rgba(0,0,0,0.08);
}
.event-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; flex-wrap: wrap; gap: 6px; }
.event-date { font-size: 12px; color: #999; }
.vote-bars { display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; }
.vote-bar-row { display: flex; align-items: center; gap: 8px; }
.vote-label { font-size: 12px; color: #555; min-width: 70px; }
.vote-bar-bg { flex: 1; height: 18px; background: #f0f0f0; border-radius: 9px; overflow: hidden; }
.vote-bar { height: 100%; border-radius: 9px; transition: width 0.5s ease; min-width: 0; }
.vote-bar.yes { background: linear-gradient(90deg, #4caf50, #66bb6a); }
.vote-bar.no { background: linear-gradient(90deg, #e53935, #ef5350); }
.vote-bar.none { background: linear-gradient(90deg, #bdbdbd, #ccc); }
.event-actions { display: flex; gap: 8px; }
.btn-vote-yes, .btn-vote-no {
  flex: 1; padding: 8px; border: none; border-radius: 8px; font-size: 14px; cursor: pointer;
  font-weight: 600; transition: transform 0.2s;
}
.btn-vote-yes { background: #e8f5e9; color: #2e7d32; }
.btn-vote-yes:hover { background: #c8e6c9; }
.btn-vote-no { background: #ffebee; color: #c62828; }
.btn-vote-no:hover { background: #ffcdd2; }

/* ==================== Polls ==================== */
.poll-question { margin-bottom: 10px; font-size: 15px; }
.poll-option {
  position: relative; padding: 10px 14px; background: #f5f5f5; border-radius: 8px;
  margin-bottom: 6px; cursor: pointer; overflow: hidden; transition: all 0.2s;
  display: flex; justify-content: space-between; align-items: center;
}
.poll-option:hover { background: #e8e8e8; }
.poll-option-bar {
  position: absolute; left: 0; top: 0; bottom: 0; background: rgba(102,126,234,0.15);
  border-radius: 8px; transition: width 0.5s ease;
}
.poll-option-text { position: relative; z-index: 1; font-size: 14px; color: #333; }
.poll-option-count { position: relative; z-index: 1; font-size: 12px; color: #999; flex-shrink: 0; }
.poll-option-input {
  width: 100%; padding: 8px; border: 1px solid #ddd; border-radius: 6px; font-size: 13px; margin-bottom: 6px;
}
.poll-option-input:focus { outline: none; border-color: #667eea; }
.btn-small {
  padding: 6px 12px; background: #f0f0f0; border: 1px solid #ddd; border-radius: 6px;
  cursor: pointer; font-size: 13px; transition: background 0.2s;
}
.btn-small:hover { background: #e0e0e0; }
.btn-danger {
  width: 100%; padding: 12px; background: #e53935; color: white; border: none; border-radius: 10px;
  font-size: 15px; font-weight: 600; cursor: pointer; transition: transform 0.2s;
}
.btn-danger:hover { transform: translateY(-1px); }
.empty-text { color: #999; font-size: 13px; padding: 8px 0; }

/* ==================== Avatar Circles ==================== */
.avatar-circle {
  border-radius: 50%; image-rendering: pixelated; image-rendering: crisp-edges;
  object-fit: contain; flex-shrink: 0; background: #f0f0f0;
}
.message-avatar {
  align-self: flex-end; margin-right: 6px; flex-shrink: 0;
}

/* ==================== Avatar Editor ==================== */
.avatar-editor-body { display: flex; flex-direction: column; align-items: center; gap: 20px; padding: 24px 16px; }
.avatar-preview-large { display: flex; justify-content: center; padding: 16px; }
.avatar-preview-large canvas {
  width: 144px; height: 144px; image-rendering: pixelated; image-rendering: crisp-edges;
  border-radius: 16px; border: 3px solid #667eea; box-shadow: 0 4px 16px rgba(102,126,234,0.25);
}
.avatar-controls { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 10px; }
.avatar-controls-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.btn-avatar-control {
  padding: 12px 16px; background: white; border: 1px solid #ddd; border-radius: 10px;
  font-size: 14px; font-weight: 500; cursor: pointer; transition: all 0.2s; color: #333;
}
.btn-avatar-control:hover { border-color: #667eea; background: #f0eeff; color: #667eea; }
.btn-avatar-control:active { transform: scale(0.97); }
.btn-avatar-control.primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white;
  border: none; font-size: 16px; font-weight: 600;
}
.btn-avatar-control.primary:hover { transform: translateY(-1px); box-shadow: 0 4px 12px rgba(102,126,234,0.4); }
.btn-save-avatar {
  background: rgba(255,255,255,0.2); color: white; border: 1px solid rgba(255,255,255,0.5);
  padding: 6px 14px; border-radius: 8px; cursor: pointer; font-size: 13px; font-weight: 600;
  transition: all 0.2s; white-space: nowrap; flex-shrink: 0;
}
.btn-save-avatar:hover { background: rgba(255,255,255,0.3); border-color: white; }

/* ==================== Friends Management ==================== */
.manage-friends-info { font-size: 13px; color: #888; padding: 8px 4px 12px; line-height: 1.4; }
.manage-friend-item { display: flex; align-items: center; gap: 10px; padding: 12px 14px; }
.friend-status-text { font-size: 12px; font-weight: 500; flex-shrink: 0; }
.btn-remove-friend-manage {
  background: #ffebee; color: #d32f2f; border: 1px solid #ffcdd2; padding: 6px 12px;
  border-radius: 8px; font-size: 12px; font-weight: 600; cursor: pointer;
  transition: all 0.2s; flex-shrink: 0; white-space: nowrap;
}
.btn-remove-friend-manage:hover { background: #ffcdd2; border-color: #ef9a9a; }
.btn-remove-friend-manage:active { transform: scale(0.95); }

/* ==================== Invite Links Management ==================== */
.invite-link-count { font-size: 14px; font-weight: 600; color: #667eea; padding: 4px 4px 12px; }
.manage-invite-item { display: flex; align-items: center; gap: 10px; padding: 12px 14px; }
.invite-item-info { display: flex; flex-direction: column; gap: 2px; flex: 1; min-width: 0; }
.invite-item-code { font-size: 14px; font-weight: 600; color: #333; font-family: monospace; }
.invite-item-meta { font-size: 12px; color: #888; }

/* ==================== DM Partner Info ==================== */
.dm-partner-info { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.dm-avatar-canvas {
  width: 32px; height: 32px; border-radius: 50%; flex-shrink: 0;
  image-rendering: pixelated; image-rendering: crisp-edges;
}
.e2e-badge { font-size: 14px; flex-shrink: 0; cursor: help; }
.e2e-warning { background: #ff980020; color: #ff9800; font-size: 11px; text-align: center; padding: 4px 8px; border-bottom: 1px solid #ff980040; }

/* ==================== Messages ==================== */
.messages-container {
  flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; background: white url('/bg-pattern-light.png') repeat; background-size: 300px 300px;
}
.message { display: flex; align-items: flex-end; margin-bottom: 6px; animation: slideIn 0.3s ease; }
.message.own { justify-content: flex-end; }
.message.other { justify-content: flex-start; }
.message-content { max-width: 65%; padding: 10px 14px; border-radius: 12px; word-wrap: break-word; position: relative; }
.message.own .message-content { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; }
.message.other .message-content { background: #f0f0f0; color: #333; }
.message-username { font-size: 12px; font-weight: 600; margin-bottom: 3px; opacity: 0.7; display: flex; align-items: center; gap: 5px; }
.message-name { white-space: nowrap; }
.message-text { font-size: 14px; line-height: 1.4; }
.message-ttl { font-size: 11px; opacity: 0.7; }
.message-time { font-size: 10px; opacity: 0.5; text-align: right; margin-top: 4px; }
.message.own .message-time { color: rgba(255,255,255,0.7); }

/* ==================== Message Input ==================== */
.message-input-area {
  padding: 8px 12px; padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px));
  background: white; border-top: 1px solid #eee; flex-shrink: 0; position: relative;
}
.input-group { display: flex; gap: 6px; align-items: center; }
#messageInput, #dmInput, #publicInput, #groupInput {
  flex: 1; padding: 10px 14px; border: 1px solid #ddd; border-radius: 20px; font-size: 14px;
  transition: border-color 0.3s ease; min-width: 0;
}
#messageInput:focus, #dmInput:focus, #publicInput:focus, #groupInput:focus {
  outline: none; border-color: #667eea; box-shadow: 0 0 5px rgba(102,126,234,0.3);
}
.btn-send {
  padding: 10px 18px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white;
  border: none; border-radius: 20px; font-weight: 600; cursor: pointer; font-size: 14px;
  transition: transform 0.2s ease, box-shadow 0.2s ease; white-space: nowrap;
}
.btn-send:hover { transform: translateY(-1px); box-shadow: 0 4px 12px rgba(102,126,234,0.4); }
.btn-send:active { transform: translateY(0); }
.ttl-selector { display: flex; align-items: center; gap: 6px; margin-bottom: 6px; font-size: 13px; color: #666; }
.ttl-selector select {
  padding: 4px 8px; border: 1px solid #ddd; border-radius: 6px; font-size: 13px; background: white; color: #333; cursor: pointer;
}

/* ==================== Emoji Picker ==================== */
.emoji-picker {
  display: grid; grid-template-columns: repeat(10, 1fr); gap: 4px; padding: 10px;
  background: white; border: 1px solid #ddd; border-radius: 10px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.15); max-height: 200px; overflow-y: auto;
  position: absolute; bottom: 100%; left: 0; right: 0; margin-bottom: 5px; z-index: 100;
}
.emoji-item {
  font-size: 22px; cursor: pointer; text-align: center; padding: 4px; border-radius: 6px;
  transition: background 0.15s; user-select: none;
}
.emoji-item:hover { background: #f0f0f0; transform: scale(1.2); }

/* ==================== Image ==================== */
.btn-icon {
  background: none; border: none; font-size: 20px; cursor: pointer; padding: 6px; border-radius: 6px;
  transition: background 0.2s; flex-shrink: 0; line-height: 1; display: flex; align-items: center;
}
.btn-icon:hover { background: #f0f0f0; }
.image-preview {
  display: flex; align-items: center; gap: 10px; padding: 8px; background: #f9f9f9;
  border-radius: 8px; margin-bottom: 6px; border: 1px solid #ddd;
}
.image-preview img { max-height: 60px; max-width: 100px; border-radius: 6px; object-fit: cover; }
.btn-cancel-img {
  background: #d32f2f; color: white; border: none; border-radius: 50%; width: 24px; height: 24px;
  font-size: 16px; cursor: pointer; display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.message-image { max-width: 100%; max-height: 300px; border-radius: 8px; margin: 6px 0; cursor: pointer; transition: opacity 0.2s; }
.message-image:hover { opacity: 0.9; }

/* ==================== Scrollbar ==================== */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #ccc; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #999; }

/* ==================== Mobile ==================== */
@media (max-width: 768px) {
  .chat-header { padding: 6px 10px; gap: 8px; }
  .chat-header h1 { font-size: 16px; }
  .header-info { gap: 8px; }
  #currentUser { font-size: 11px; }
  .btn-user-menu { padding: 4px 8px 4px 4px; font-size: 11px; }
  .header-avatar-canvas { width: 20px; height: 20px; }
  .user-dropdown { min-width: 180px; }
  .dropdown-item { padding: 10px 14px; font-size: 13px; }
  .btn-logout { padding: 4px 8px; font-size: 11px; }
  .btn-back { padding: 5px 10px; font-size: 14px; }
  .contacts-body { padding: 8px; }
  .contacts-group-btn { padding: 12px 14px; font-size: 15px; margin-bottom: 6px; }
  .contacts-add-friend input { padding: 8px 10px; font-size: 13px; }
  .contact-item { padding: 10px 12px; gap: 8px; }
  .contact-name { font-size: 14px; }
  .messages-container { padding: 8px; gap: 6px; }
  .message-content { max-width: 85%; padding: 8px 10px; }
  .message-username { font-size: 11px; margin-bottom: 2px; }
  .message-text { font-size: 13px; line-height: 1.3; }
  .message-ttl { font-size: 10px; }
  .message-input-area { padding: 6px 8px; padding-bottom: max(6px, env(safe-area-inset-bottom)); }
  .ttl-selector { margin-bottom: 4px; font-size: 12px; }
  .ttl-selector select { font-size: 12px; padding: 2px 4px; }
  .input-group { gap: 4px; }
  #messageInput, #dmInput, #publicInput, #groupInput { padding: 8px 10px; font-size: 14px; }
  .btn-send { padding: 8px 12px; font-size: 13px; }
  .emoji-picker { grid-template-columns: repeat(8, 1fr); }
  .btn-icon { padding: 4px; font-size: 18px; }
  .image-preview { padding: 4px; margin-bottom: 4px; }
  .image-preview img { max-height: 40px; max-width: 60px; }
  .btn-cancel-img { width: 20px; height: 20px; font-size: 14px; }
  .btn-remove-friend { opacity: 1; }
  .auth-form-container { padding: 24px 20px; border-radius: 8px; width: 92%; }
  .auth-form-container h1 { font-size: 22px; margin-bottom: 20px; }
  .auth-tabs { margin-bottom: 20px; }
  .tab-button { padding: 10px; font-size: 13px; }
  .auth-form input { padding: 10px; font-size: 14px; }
  .btn-primary { padding: 10px; font-size: 15px; }
  .message-image { max-height: 200px; }
  .channel-item { padding: 12px; }
  .channel-name { font-size: 14px; }
  .event-card, .poll-card { padding: 10px; }
  .btn-settings { padding: 5px 8px; font-size: 14px; }
  .avatar-preview-large canvas { width: 120px; height: 120px; }
  .avatar-controls-grid { gap: 6px; }
  .btn-avatar-control { padding: 10px 12px; font-size: 13px; }
  .dm-avatar-canvas { width: 28px; height: 28px; }
}

@media (max-width: 400px) {
  .chat-header h1 { font-size: 14px; }
  .btn-logout { padding: 3px 6px; font-size: 10px; }
  .btn-user-menu { padding: 3px 6px 3px 3px; font-size: 10px; gap: 4px; }
  .header-avatar-canvas { width: 18px; height: 18px; }
  #currentUser { font-size: 10px; }
  .btn-send { padding: 8px 10px; font-size: 12px; }
  .btn-icon { padding: 3px 2px; font-size: 16px; }
  #messageInput, #dmInput, #publicInput, #groupInput { font-size: 13px; padding: 7px 8px; }
  .contacts-group-btn { padding: 10px 12px; font-size: 14px; }
  .contact-item { padding: 9px 10px; }
  .contact-name { font-size: 13px; }
  .avatar-preview-large canvas { width: 100px; height: 100px; }
}

/* ==================== Bottom Bar ==================== */
.mobile-bottom-bar {
  display: flex; position: fixed; bottom: 0; left: 0; right: 0; padding: 8px 12px;
  background: linear-gradient(135deg, #667eea, #764ba2); border-top: 1px solid rgba(255,255,255,0.2);
  justify-content: center; gap: 10px; z-index: 1000; box-shadow: 0 -2px 10px rgba(0,0,0,0.2);
  opacity: 0; pointer-events: none;
  transition: opacity 0.3s ease;
}
.mobile-bottom-bar.visible {
  opacity: 1; pointer-events: auto;
}
.mobile-bottom-bar button {
  background: white; color: #667eea; border: none; padding: 9px 14px; border-radius: 20px;
  font-size: 13px; font-weight: 600; cursor: pointer; box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  transition: transform 0.2s, box-shadow 0.2s, opacity 0.2s; white-space: nowrap;
}
.mobile-bottom-bar button:active { transform: scale(0.95); box-shadow: 0 1px 4px rgba(0,0,0,0.1); }
.mobile-bottom-bar button.done { background: rgba(255,255,255,0.3); color: white; cursor: default; opacity: 0.8; }
@media (max-width: 400px) { .mobile-bottom-bar button { padding: 8px 10px; font-size: 12px; } .mobile-bottom-bar { gap: 6px; padding: 6px 8px; } }

/* ==================== Safe Area ==================== */
@supports (padding: max(0px)) {
  .chat-header { padding-top: max(6px, env(safe-area-inset-top)); padding-left: max(10px, env(safe-area-inset-left)); padding-right: max(10px, env(safe-area-inset-right)); }
  .message-input-area { padding-bottom: max(6px, env(safe-area-inset-bottom)); }
  .mobile-bottom-bar { padding-bottom: max(8px, env(safe-area-inset-bottom)); }
}

@media (max-height: 500px) and (orientation: landscape) {
  .chat-header { padding: 4px 10px; }
  .chat-header h1 { font-size: 14px; }
  .messages-container { padding: 4px; }
  .message-input-area { padding: 4px 8px; }
  .contacts-body { padding: 4px 8px; }
  .contact-item { padding: 6px 8px; }
}

/* ==================== Games ==================== */
.games-create-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
  color: white !important; font-weight: 600;
}
.game-form-label { font-size: 13px; color: #666; margin-bottom: 8px; }
.game-invite-row { display: flex; gap: 8px; padding: 8px 12px; }
.game-invite-row select { flex: 1; }
.game-invite-row .btn-add-friend { padding: 8px 14px; }
.game-lobby-options { display: flex; flex-direction: column; gap: 8px; }
.game-option-btn { width: 100%; }
.game-option-divider { text-align: center; font-size: 12px; color: #999; padding: 4px 0; }
.game-invite-section { display: flex; gap: 8px; }
.game-invite-section select { flex: 1; }
.game-lobby-item { display: flex; align-items: center; gap: 10px; }
.game-lobby-info { flex: 1; display: flex; flex-direction: column; }
.game-lobby-info strong { font-size: 14px; }
.game-lobby-creator { font-size: 12px; color: #999; }
.game-join-btn { padding: 6px 14px !important; font-size: 13px !important; }
.game-invite-item { flex-wrap: wrap; }
.no-lobbies-msg { font-size: 13px; color: #999; padding: 12px 4px; }

/* Game Overlay */
.game-overlay {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0; z-index: 10000;
  background: #000; display: flex; align-items: center; justify-content: center;
}
.game-iframe {
  width: 100%; height: 100%; border: none;
}


/* ==================== Dark Mode Toggle ==================== */
.btn-dark-mode {
  background: rgba(255,255,255,0.2); border: 1px solid rgba(255,255,255,0.4); color: white;
  padding: 4px 8px; border-radius: 6px; cursor: pointer; font-size: 16px; line-height: 1;
  transition: all 0.2s; flex-shrink: 0;
}
.btn-dark-mode:hover { background: rgba(255,255,255,0.3); }
.dm-status-dot { font-size: 14px; flex-shrink: 0; margin-right: 2px; }
.dm-status-dot.online { color: #4caf50; }
.dm-status-dot.offline { color: rgba(255,255,255,0.3); }

/* ==================== Dark Mode ==================== */
body.dark-mode { background: #2a2a2a; color: #e0e0e0; }
body.dark-mode .chat-container { background: #2a2a2a; }
body.dark-mode .messages-container { background: #1e1e1e url('/bg-pattern-dark.png') repeat; background-size: 300px 300px; }
body.dark-mode .message.other .message-content { background: #3a3a3a; color: #e0e0e0; }
body.dark-mode .message-username { opacity: 0.8; }
body.dark-mode .message-time { opacity: 0.4; }
body.dark-mode .contacts-body { background: #2a2a2a url('/bg-pattern-dark.png') repeat; background-size: 300px 300px; }
body.dark-mode .contact-item { background: #1e1e1e; box-shadow: 0 1px 3px rgba(0,0,0,0.2); }
body.dark-mode .contact-item:hover { background: #3a3a3a; }
body.dark-mode .contact-name { color: #e0e0e0; }
body.dark-mode .contact-status-dot.offline { color: #555; }
body.dark-mode .contact-item.has-notification { background: #3a2d4a; animation: pulse-notify-dark 1.5s infinite; }
@keyframes pulse-notify-dark { 0%, 100% { background: #3a2d4a; } 50% { background: #4a3560; } }
body.dark-mode .message-input-area { background: #1e1e1e; border-top-color: #3a3a3a; }
body.dark-mode #messageInput, body.dark-mode #dmInput, body.dark-mode #publicInput, body.dark-mode #groupInput {
  background: #2a2a2a; color: #e0e0e0; border-color: #444;
}
body.dark-mode #messageInput::placeholder, body.dark-mode #dmInput::placeholder,
body.dark-mode #publicInput::placeholder, body.dark-mode #groupInput::placeholder { color: #777; }
body.dark-mode .contacts-add-friend input { background: #1e1e1e; color: #e0e0e0; border-color: #444; }
body.dark-mode .contacts-add-friend input::placeholder { color: #777; }
body.dark-mode .contacts-section h3 { color: #888; }
body.dark-mode .ttl-selector { color: #aaa; }
body.dark-mode .ttl-selector select { background: #2a2a2a; color: #e0e0e0; border-color: #444; }
body.dark-mode .emoji-picker { background: #1e1e1e; border-color: #444; }
body.dark-mode .emoji-item:hover { background: #3a3a3a; }
body.dark-mode .image-preview { background: #1e1e1e; border-color: #444; }
body.dark-mode .auth-form-container { background: #1e1e1e; box-shadow: 0 10px 40px rgba(0,0,0,0.4); }
body.dark-mode .auth-form-container h1 { color: #e0e0e0; }
body.dark-mode .auth-form input { background: #2a2a2a; color: #e0e0e0; border-color: #444; }
body.dark-mode .auth-form input::placeholder { color: #777; }
body.dark-mode .auth-tabs { border-bottom-color: #444; }
body.dark-mode .tab-button { color: #888; }
body.dark-mode .tab-button.active { color: #667eea; }
body.dark-mode ::-webkit-scrollbar-thumb { background: #555; }
body.dark-mode ::-webkit-scrollbar-thumb:hover { background: #777; }
body.dark-mode .channel-item { background: #1e1e1e; box-shadow: 0 1px 3px rgba(0,0,0,0.2); }
body.dark-mode .channel-item:hover { background: #3a3a3a; }
body.dark-mode .channel-name { color: #e0e0e0; }
body.dark-mode .channel-desc { color: #777; }
body.dark-mode .event-card, body.dark-mode .poll-card { background: #1e1e1e; box-shadow: 0 1px 4px rgba(0,0,0,0.2); }
body.dark-mode .vote-bar-bg { background: #3a3a3a; }
body.dark-mode .vote-label { color: #aaa; }
body.dark-mode .poll-option { background: #2a2a2a; }
body.dark-mode .poll-option:hover { background: #3a3a3a; }
body.dark-mode .poll-option-text { color: #e0e0e0; }
body.dark-mode .poll-option-bar { background: rgba(102,126,234,0.25); }
body.dark-mode .create-group-form { background: #1e1e1e; box-shadow: 0 2px 8px rgba(0,0,0,0.3); }
body.dark-mode .create-group-form input { background: #2a2a2a; color: #e0e0e0; border-color: #444; }
body.dark-mode .poll-option-input { background: #2a2a2a; color: #e0e0e0; border-color: #444; }
body.dark-mode .btn-invite-link { background: #1e1e1e; color: #aaa; border-color: #555; }
body.dark-mode .btn-invite-link.has-link { background: #1a1a2e; color: #8b9cf7; border-color: #667eea; }
body.dark-mode .btn-invite-link:hover { color: #667eea; border-color: #667eea; }
body.dark-mode .btn-share-link { background: #667eea; }
body.dark-mode .invite-link-result { background: #1b3a1b; color: #4caf50; }
body.dark-mode .add-member-select { background: #2a2a2a; color: #e0e0e0; border-color: #444; }
body.dark-mode .checkbox-label { color: #aaa; }
body.dark-mode .btn-small { background: #2a2a2a; color: #e0e0e0; border-color: #444; }
body.dark-mode .settings-section h3 { color: #667eea; }
body.dark-mode .event-header strong { color: #e0e0e0; }
body.dark-mode .btn-vote-yes { background: #1b3a1b; color: #66bb6a; }
body.dark-mode .btn-vote-no { background: #3a1b1b; color: #ef5350; }
body.dark-mode .poll-question { color: #e0e0e0; }
body.dark-mode .empty-text { color: #777; }

/* Dark Mode: User Dropdown */
body.dark-mode .user-dropdown { background: #2a2a2a; box-shadow: 0 6px 24px rgba(0,0,0,0.4); }
body.dark-mode .dropdown-item { color: #e0e0e0; }
body.dark-mode .dropdown-item:hover { background: #3a3a3a; }
body.dark-mode .dropdown-item.logout { color: #ef5350; }
body.dark-mode .dropdown-item.logout:hover { background: #3a1b1b; }
body.dark-mode .dropdown-divider { background: #444; }

/* Dark Mode: Avatar Editor */
body.dark-mode .btn-avatar-control { background: #1e1e1e; border-color: #444; color: #e0e0e0; }
body.dark-mode .btn-avatar-control:hover { border-color: #667eea; background: #2a2a3a; color: #8b9cf7; }
body.dark-mode .avatar-preview-large canvas { border-color: #8b9cf7; box-shadow: 0 4px 16px rgba(102,126,234,0.15); }

/* Dark Mode: Friends Management */
body.dark-mode .manage-friends-info { color: #888; }
body.dark-mode .btn-remove-friend-manage { background: #3a1b1b; color: #ef5350; border-color: #5a2b2b; }
body.dark-mode .btn-remove-friend-manage:hover { background: #4a2020; border-color: #ef5350; }
body.dark-mode .friend-status-text { color: #aaa; }
body.dark-mode .invite-item-code { color: #e0e0e0; }
body.dark-mode .invite-item-meta { color: #888; }
body.dark-mode .invite-link-count { color: #8b9cf7; }
body.dark-mode .btn-copy-link { background: #2a6a2e; }
body.dark-mode .btn-copy-link:hover { background: #388e3c; }
body.dark-mode .btn-new-link { background: #e68a00; }
body.dark-mode .btn-new-link:hover { background: #ff9800; }
body.dark-mode .install-guide-overlay { background: rgba(0,0,0,0.7); }
body.dark-mode .install-guide-content { background: #1e1e1e; color: #ddd; box-shadow: 0 8px 32px rgba(0,0,0,0.5); }
body.dark-mode .install-guide-content h2 { color: #fff; }

/* Dark Mode: Gradient Buttons (muted) */
body.dark-mode .contacts-group-btn {
  background: linear-gradient(135deg, #3a4a8a 0%, #4a2d6a 100%);
  box-shadow: 0 2px 8px rgba(58,74,138,0.3);
}
body.dark-mode .contacts-group-btn:hover { box-shadow: 0 4px 14px rgba(58,74,138,0.4); }
body.dark-mode .contacts-group-btn.groups-btn {
  background: linear-gradient(135deg, #2a6a2e 0%, #1e4a20 100%);
  box-shadow: 0 2px 8px rgba(42,106,46,0.3);
}
body.dark-mode .contacts-group-btn.groups-btn:hover { box-shadow: 0 4px 14px rgba(42,106,46,0.4); }
body.dark-mode .contacts-group-btn.games-btn {
  background: linear-gradient(135deg, #3a4a8a 0%, #4a2d6a 100%);
  box-shadow: 0 2px 8px rgba(58,74,138,0.3);
}
body.dark-mode .contacts-group-btn.games-btn:hover { box-shadow: 0 4px 14px rgba(58,74,138,0.4); }
body.dark-mode .chat-header {
  background: linear-gradient(135deg, #3a4a8a 0%, #4a2d6a 100%);
  box-shadow: 0 2px 10px rgba(0,0,0,0.3);
}
body.dark-mode .btn-add-friend {
  background: linear-gradient(135deg, #3a4a8a 0%, #4a2d6a 100%);
}
body.dark-mode .btn-inline-create {
  background: linear-gradient(135deg, #3a4a8a 0%, #4a2d6a 100%);
}
body.dark-mode .create-group-form .btn-primary {
  background: linear-gradient(135deg, #3a4a8a 0%, #4a2d6a 100%);
}
body.dark-mode .game-form-label { color: #aaa; }
body.dark-mode .game-mode-btn { border-color: rgba(255,255,255,0.1); background: rgba(255,255,255,0.03); }
body.dark-mode .game-mode-btn:hover { border-color: #ff8f20; background: rgba(255,143,32,0.08); }
body.dark-mode .gm-name { color: #eee; }
body.dark-mode .gm-desc { color: #888; }
body.dark-mode .btn-back-small { color: #999; }
body.dark-mode .btn-back-small:hover { color: #ddd; }
body.dark-mode .game-option-divider { color: #666; }
body.dark-mode .game-lobby-creator { color: #888; }
body.dark-mode .game-lobby-info strong { color: #e0e0e0; }
body.dark-mode .no-lobbies-msg { color: #666; }

/* ==================== Lightbox ==================== */
.lightbox-overlay {
  position: fixed; top: 0; left: 0; width: 100%; height: 100%;
  background: rgba(0,0,0,0.92); z-index: 10000;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
}
.lightbox-img {
  max-width: 92vw; max-height: 88vh; border-radius: 8px;
  box-shadow: 0 8px 40px rgba(0,0,0,0.6); cursor: default;
}
.lightbox-close {
  position: absolute; top: 16px; right: 22px;
  font-size: 36px; color: #fff; cursor: pointer;
  background: none; border: none; line-height: 1;
  text-shadow: 0 2px 8px rgba(0,0,0,0.5);
}

/* ==================== Reply Preview ==================== */
.reply-preview {
  background: rgba(0,0,0,0.06); border-left: 3px solid #4a9fe5;
  padding: 4px 8px; margin-bottom: 4px; border-radius: 4px;
  font-size: 12px; color: #666; cursor: pointer;
  max-height: 40px; overflow: hidden; text-overflow: ellipsis;
  white-space: nowrap;
}
.reply-preview strong { color: #4a9fe5; margin-right: 4px; }
body.dark-mode .reply-preview { background: rgba(255,255,255,0.06); color: #999; }

/* ==================== Reply & Edit Banners ==================== */
.reply-banner, .edit-banner {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 12px; font-size: 13px;
  border-left: 3px solid #4a9fe5;
  background: rgba(74,159,229,0.08);
  border-radius: 4px 4px 0 0;
  margin: 0 8px;
}
.edit-banner { border-left-color: #f5a623; background: rgba(245,166,35,0.08); }
.reply-banner span, .edit-banner span,
.reply-banner-content, .edit-banner-content { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cancel-reply-btn, .cancel-edit-btn,
.reply-banner-close, .edit-banner-close {
  background: none; border: none; cursor: pointer;
  font-size: 18px; color: #999; padding: 0 4px; line-height: 1;
}
.cancel-reply-btn:hover, .cancel-edit-btn:hover,
.reply-banner-close:hover, .edit-banner-close:hover { color: #e74c3c; }

/* ==================== Message Footer ==================== */
.message-footer {
  display: flex; align-items: center; gap: 4px;
  margin-top: 2px; font-size: 11px; color: #999;
  justify-content: flex-end;
}
.message-edited { font-style: italic; color: #999; font-size: 10px; }

/* ==================== Read Receipts ==================== */
.read-receipt { font-size: 10px; margin-left: 4px; letter-spacing: -1px; }
.receipt-sent { color: rgba(255,255,255,0.4); }
.receipt-delivered { color: rgba(255,255,255,0.7); }
.receipt-read { color: #00e5ff; }

/* ==================== Deleted Message ==================== */
.message-deleted-text {
  font-style: italic; color: #999 !important; opacity: 0.7;
}

/* ==================== Context Menu ==================== */
.msg-menu-btn {
  position: absolute; top: 4px; right: 4px;
  background: none; border: none; cursor: pointer;
  font-size: 16px; color: #bbb; padding: 2px 4px;
  border-radius: 4px; line-height: 1; opacity: 0;
  transition: opacity 0.15s;
}
.message:hover .msg-menu-btn,
.message.touch-active .msg-menu-btn { opacity: 1; }
.msg-menu-btn:hover { background: rgba(0,0,0,0.06); color: #666; }
body.dark-mode .msg-menu-btn:hover { background: rgba(255,255,255,0.08); color: #ccc; }

.msg-context-menu {
  position: fixed; z-index: 9999;
  background: #fff; border-radius: 10px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.18);
  min-width: 150px; overflow: hidden;
  animation: ctxFadeIn 0.12s ease-out;
}
body.dark-mode .msg-context-menu {
  background: #2a2a2a;
  box-shadow: 0 4px 20px rgba(0,0,0,0.5);
}
@keyframes ctxFadeIn { from { opacity: 0; transform: scale(0.95); } to { opacity: 1; transform: scale(1); } }
.msg-context-item {
  display: block; width: 100%; padding: 10px 16px;
  border: none; background: none; cursor: pointer;
  font-size: 14px; text-align: left; color: #333;
  transition: background 0.1s;
}
.msg-context-item:hover { background: rgba(0,0,0,0.05); }
body.dark-mode .msg-context-item { color: #ddd; }
body.dark-mode .msg-context-item:hover { background: rgba(255,255,255,0.07); }
.msg-context-item.msg-context-danger { color: #e74c3c; }
.msg-context-item.msg-context-danger:hover { background: rgba(231,76,60,0.08); }

/* ==================== Message Highlight Animation ==================== */
@keyframes msgHighlight {
  0% { background: rgba(74,159,229,0.25); }
  100% { background: transparent; }
}
.message-highlight {
  animation: msgHighlight 1.5s ease-out;
}

/* ==================== Message bubble relative positioning ==================== */
.message { position: relative; }
.message-bubble { position: relative; }

/* ==================== Modal Overlay (Safety Number, Key Backup) ==================== */
.modal-overlay {
  position: fixed; top: 0; left: 0; width: 100%; height: 100%;
  background: rgba(0,0,0,0.5); z-index: 10000;
  display: flex; align-items: center; justify-content: center;
  padding: 20px; box-sizing: border-box;
}
.modal-content.safety-modal {
  background: #fff; border-radius: 16px; padding: 0;
  max-width: 380px; width: 100%;
  box-shadow: 0 8px 32px rgba(0,0,0,0.2);
  overflow: hidden;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid #eee;
}
.modal-header h2 { margin: 0; font-size: 17px; }
.btn-close-modal {
  background: none; border: none; font-size: 22px; color: #999;
  cursor: pointer; padding: 0; line-height: 1;
}
.btn-close-modal:hover { color: #333; }
.modal-body { padding: 20px; }
.safety-info { font-size: 13px; color: #666; margin: 0 0 16px; line-height: 1.5; }
.safety-number-display {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 18px; letter-spacing: 2px;
  text-align: center; padding: 16px;
  background: #f5f5f5; border-radius: 12px;
  color: #333; word-break: break-all; line-height: 1.8;
  user-select: all;
}
.safety-hint { font-size: 12px; color: #999; margin: 12px 0 0; text-align: center; }
.btn-verify-safety {
  background: none; border: none; font-size: 16px; cursor: pointer;
  padding: 2px 4px; flex-shrink: 0; opacity: 0.7;
}
.btn-verify-safety:hover { opacity: 1; }
.key-backup-status {
  font-size: 13px; padding: 10px; border-radius: 8px; margin-bottom: 12px;
}
.key-backup-status.has-backup { background: #e8f5e9; color: #2e7d32; }
.key-backup-status.no-backup { background: #fff3e0; color: #e65100; }
.input-field {
  width: 100%; padding: 10px 12px; border: 1px solid #ddd; border-radius: 8px;
  font-size: 14px; box-sizing: border-box;
}
.btn-primary {
  padding: 10px 16px; border-radius: 8px; border: none;
  background: #4a9fe5; color: #fff; font-size: 14px; cursor: pointer;
}
.btn-primary:hover { background: #3d8fd4; }
.btn-secondary {
  padding: 10px 16px; border-radius: 8px; border: 1px solid #ddd;
  background: #fff; color: #333; font-size: 14px; cursor: pointer;
}
.btn-secondary:hover { background: #f5f5f5; }

/* Dark mode for modals */
body.dark-mode .modal-content.safety-modal { background: #1e1e1e; }
body.dark-mode .modal-header { border-bottom-color: #333; }
body.dark-mode .modal-header h2 { color: #fff; }
body.dark-mode .btn-close-modal { color: #888; }
body.dark-mode .btn-close-modal:hover { color: #fff; }
body.dark-mode .safety-info { color: #aaa; }
body.dark-mode .safety-number-display { background: #2a2a2a; color: #eee; }
body.dark-mode .safety-hint { color: #777; }
body.dark-mode .input-field { background: #2a2a2a; border-color: #444; color: #eee; }
body.dark-mode .btn-secondary { background: #2a2a2a; border-color: #444; color: #ddd; }
body.dark-mode .key-backup-status.has-backup { background: #1b3a1b; color: #66bb6a; }
body.dark-mode .key-backup-status.no-backup { background: #3e2200; color: #ffb74d; }
