*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "SF Pro Text",
    "Segoe UI", sans-serif;
  background: #101418;
  color: #f5f5f5;
}

body {
  -webkit-font-smoothing: antialiased;
}

.hidden {
  display: none !important;
}

#app {
  height: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: radial-gradient(circle at top left, #262c33, #101418 55%);
  overflow: hidden;
}

/* Header */

.chat-header {
  padding: 10px 14px 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  background: rgba(7, 9, 12, 0.9);
  backdrop-filter: blur(12px);
}

.chat-title {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: #f5f5f5;
}

.header-controls {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Players button in header */
.players-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: transparent;
  border: none;
  color: #cbd5e1;
  padding: 6px;
  border-radius: 8px;
  cursor: pointer;
}
.players-btn:hover {
  background: rgba(255,255,255,0.02);
  color: #e5e7eb;
}

.players-count {
  background: #ef4444;
  color: white;
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 999px;
  line-height: 1;
}

.players-panel {
  position: absolute;
  top: 56px;
  right: 12px;
  width: 260px;
  max-height: 60vh;
  background: rgba(12,14,16,0.98);
  border: 1px solid rgba(255,255,255,0.06);
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.6);
  z-index: 1200;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.players-panel.hidden {
  display: none;
}

.players-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  border-bottom: 1px solid rgba(255,255,255,0.03);
  color: #e5e7eb;
  font-weight: 600;
  font-size: 13px;
}

.players-close {
  background: transparent;
  border: none;
  color: #9ca3af;
  font-size: 14px;
  cursor: pointer;
}

.players-list {
  list-style: none;
  margin: 0;
  padding: 8px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.players-item {
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 6px;
  border-radius: 8px;
  cursor: pointer;
}
.players-item:hover {
  background: rgba(255,255,255,0.02);
}

.players-item-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
}

.players-item-meta {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.players-item-name {
  font-size: 13px;
  color: #e5e7eb;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.players-item-id {
  font-size: 11px;
  color: #94a3b8;
}

/* Main / message list */

.chat-main {
  flex: 1;
  min-height: 0;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.loading-indicator {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: #64748b;
  font-size: 13px;
}

.loading-indicator .spinner {
  width: 16px;
  height: 16px;
  border-radius: 999px;
  border: 2px solid rgba(148, 163, 184, 0.4);
  border-top-color: #22c55e;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.pinned-panel {
  margin-bottom: 6px;
  border-radius: 10px;
  background: rgba(15, 23, 42, 0.9);
  border: 1px solid rgba(148, 163, 184, 0.3);
  overflow: hidden;
}

.pinned-header {
  width: 100%;
  background: rgba(15, 23, 42, 0.9);
  color: #e5e7eb;
  border: none;
  padding: 6px 10px;
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  font-size: 12px;
}

.pinned-title {
  font-weight: 600;
}

.pinned-count {
  font-size: 11px;
  color: #9ca3af;
}

.pinned-toggle-icon {
  margin-left: auto;
  font-size: 12px;
  color: #9ca3af;
  transition: transform 0.15s ease;
}

.pinned-panel--collapsed .pinned-list {
  display: none;
}

.pinned-panel--collapsed .pinned-toggle-icon {
  transform: rotate(-90deg);
}

.pinned-list {
  list-style: none;
  margin: 0;
  padding: 4px 8px 6px;
  max-height: 120px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.pinned-item {
  padding: 4px 6px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s ease;
}

.pinned-item:hover {
  background: rgba(15, 23, 42, 0.95);
}

.pinned-content {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.pinned-meta {
  display: flex;
  align-items: baseline;
  gap: 6px;
}

.pinned-username {
  font-size: 11px;
  font-weight: 600;
  color: #e5e7eb;
}

.pinned-timestamp {
  font-size: 10px;
  color: #9ca3af;
}

.pinned-text {
  font-size: 12px;
  color: #cbd5f5;
}

.messages {
  list-style: none;
  margin: 0;
  padding: 0;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column; /* DOM order = visual order; newest will be at the bottom */
  gap: 6px;
  overscroll-behavior-y: contain;
  -webkit-overflow-scrolling: touch;
}

/* Individual message */

.message {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  max-width: 100%;
}

.message-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  flex-shrink: 0;
  background: #1f252b;
  overflow: hidden;
}

.message-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.message-body {
  display: inline-flex;
  flex-direction: column;
  max-width: 80%;
  min-width: 0;
}

.message-meta {
  display: flex;
  align-items: baseline;
  gap: 0;
  margin-bottom: 2px;
}

.message-username {
  font-size: 12px;
  font-weight: 600;
  color: #e2e8f0;
}

.message-timestamp {
  font-size: 10px;
  color: #94a3b8;
  margin-left: 4px;
}

.message-text {
  font-size: 14px;
  line-height: 1.35;
  padding: 6px 9px;
  border-radius: 10px;
  background: rgba(15, 23, 42, 0.9);
  color: #e5e7eb;
  word-wrap: break-word;
  word-break: break-word;
}

.message-text a {
  color: inherit;
  text-decoration: underline;
}

/* Mentions (e.g. @username) shouldn't show an underline */
.message-text a.mention {
  text-decoration: none;
}

.message-image-wrapper {
  position: relative;
  margin-top: 4px;
  max-width: 220px;
}

.message-image {
  width: 100%;
  border-radius: 10px;
  display: block;
  opacity: 0;
  transition: opacity 0.2s ease;
}

/* While loading, keep the image hidden and show a centered spinner */
.image-loading .message-image {
  opacity: 0;
}

.message-image-wrapper:not(.image-loading) .message-image {
  opacity: 1;
}

.image-loading-spinner {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.image-loading-spinner::before {
  content: "";
  width: 18px;
  height: 18px;
  border-radius: 999px;
  border: 2px solid rgba(148, 163, 184, 0.4);
  border-top-color: #22c55e;
  animation: spin 0.7s linear infinite;
}

/* Own message styling */

.message--own {
  flex-direction: row-reverse;
}

.message--own .message-body {
  text-align: right;
}

.message--own .message-text {
  background: #15803d;
  color: #f9fafb;
}

.message--own .message-meta {
  justify-content: flex-end;
}

/* Delete button */

.message-pin-btn {
  border: none;
  background: transparent;
  color: #64748b; /* Match delete icon color */
  font-size: 12px;
  cursor: pointer;
  padding: 0 0.5px; /* tightened padding */
  margin-left: 2px; /* 2px gap from timestamp */
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: color 0.2s ease;
}

.message-pin-btn:hover {
  color: #facc15; /* restored yellow on hover */
}

.message-pin-btn svg {
  display: block;
}

.message-pin-indicator {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-left: 2px; /* 2px gap from timestamp for non-admin view */
  color: #64748b; /* Match the delete icon color */
  font-size: 12px;
}

.pin-icon {
  filter: drop-shadow(0 1px 1px rgba(0,0,0,0.3));
}

/* Delete button */

.message-delete-btn {
  border: none;
  background: transparent;
  color: #64748b;
  font-size: 14px;
  cursor: pointer;
  padding: 0 2px;
  margin-left: 0; /* remove extra left margin so icons sit tighter */
}

.message-delete-btn:hover {
  color: #fca5a5;
}

/* Input bar */

.message-form {
  border-top: 1px solid rgba(255, 255, 255, 0.06);
  background: rgba(7, 9, 12, 0.96);
  backdrop-filter: blur(12px);
  display: flex;
  flex-direction: column;
}

.attachment-preview {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: rgba(255, 255, 255, 0.04);
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.attachment-thumbnail {
  width: 36px;
  height: 36px;
  object-fit: cover;
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.attachment-name {
  font-size: 12px;
  color: #cbd5e1;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.remove-attachment {
  background: rgba(255, 255, 255, 0.08);
  border: none;
  color: #94a3b8;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 14px;
}

.remove-attachment:hover {
  background: rgba(239, 68, 68, 0.2);
  color: #ef4444;
}

.input-bar {
  display: flex;
  align-items: center;
  padding: 8px 10px 10px;
  gap: 8px;
}

.image-upload-label {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 999px;
  background: #020617;
  border: 1px solid rgba(148, 163, 184, 0.4);
  color: #e5e7eb;
  flex-shrink: 0;
  cursor: pointer;
  font-size: 18px;
}

.image-upload-icon {
  pointer-events: none;
}

.image-input {
  display: none;
}

.message-input {
  flex: 1;
  border-radius: 999px;
  border: 1px solid rgba(148, 163, 184, 0.4);
  padding: 8px 12px;
  font-size: 14px;
  background: #020617;
  color: #e5e7eb;
  outline: none;
}

.message-input::placeholder {
  color: #64748b;
}

.message-input:focus {
  border-color: #22c55e;
}

.send-btn {
  border-radius: 999px;
  border: none;
  padding: 8px 14px;
  font-size: 14px;
  font-weight: 500;
  background: #16a34a;
  color: #f9fafb;
  cursor: pointer;
  flex-shrink: 0;
}

.send-btn:active {
  transform: translateY(1px);
  background: #15803d;
}

.send-btn:disabled {
  opacity: 0.5;
  cursor: default;
  transform: none;
}

/* Small screens */

/* Fullscreen Image Overlay */

.image-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.9);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
  backdrop-filter: blur(8px);
  transition: opacity 0.2s ease;
}

.image-overlay.hidden {
  display: none;
  opacity: 0;
  pointer-events: none;
}

#overlay-img {
  max-width: 95%;
  max-height: 95%;
  object-fit: contain;
  box-shadow: 0 0 30px rgba(0, 0, 0, 0.5);
  border-radius: 4px;
}

.overlay-controls {
  position: absolute;
  top: 20px;
  right: 20px;
  display: flex;
  gap: 16px;
  z-index: 1001;
}

.overlay-btn {
  background: rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: white;
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  text-decoration: none;
}

.overlay-btn:hover {
  background: rgba(0, 0, 0, 0.8);
  border-color: rgba(255, 255, 255, 0.4);
}

.overlay-btn:active {
  transform: scale(0.95);
}

.overlay-btn svg {
  width: 22px;
  height: 22px;
}

/* User messages popup */

.user-popup {
  position: fixed;
  inset: 0;
  z-index: 1300;
  display: flex;
  align-items: center;
  justify-content: center;
}

.user-popup.hidden {
  display: none;
}

.user-popup-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.7);
  backdrop-filter: blur(8px);
}

.user-popup-content {
  position: relative;
  z-index: 1;
  width: min(360px, 92vw);
  max-height: 70vh;
  background: #020617;
  border-radius: 12px;
  border: 1px solid rgba(148, 163, 184, 0.5);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 18px 45px rgba(0, 0, 0, 0.8);
}

.user-popup-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-bottom: 1px solid rgba(148, 163, 184, 0.3);
  background: rgba(15, 23, 42, 0.96);
}

.user-popup-user {
  display: flex;
  align-items: center;
  gap: 8px;
}

.user-popup-avatar {
  width: 32px;
  height: 32px;
  border-radius: 999px;
  object-fit: cover;
}

.user-popup-meta {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.user-popup-title {
  font-size: 14px;
  font-weight: 600;
  color: #e5e7eb;
}

.user-popup-profile {
  font-size: 11px;
  color: #38bdf8;
  text-decoration: none;
}

.user-popup-profile:hover {
  text-decoration: underline;
}

.user-popup-close {
  border: none;
  background: transparent;
  color: #9ca3af;
  font-size: 16px;
  cursor: pointer;
  padding: 4px;
  border-radius: 999px;
}

.user-popup-close:hover {
  background: rgba(148, 163, 184, 0.15);
  color: #e5e7eb;
}

.user-popup-body {
  padding: 8px 0 8px 0;
  overflow-y: auto;
}

.user-popup-list {
  list-style: none;
  margin: 0;
  padding: 0 10px 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.user-popup-item {
  padding: 6px 8px;
  border-radius: 8px;
  background: rgba(15, 23, 42, 0.92);
}

.user-popup-item-meta {
  display: flex;
  align-items: baseline;
  gap: 6px;
  margin-bottom: 2px;
}

.user-popup-item-timestamp {
  font-size: 10px;
  color: #94a3b8;
}

.user-popup-item-label {
  font-size: 10px;
  color: #22c55e;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.user-popup-item-text {
  font-size: 13px;
  color: #e5e7eb;
  word-wrap: break-word;
  word-break: break-word;
}

.user-popup-item-image {
  margin-top: 4px;
  max-width: 100%;
  border-radius: 8px;
  display: block;
}

.user-popup-empty {
  padding: 12px 10px 14px;
  font-size: 12px;
  color: #9ca3af;
  text-align: center;
}

@media (max-width: 420px) {
  .chat-header {
    padding-inline: 10px;
  }
  .chat-title {
    font-size: 14px;
  }
  .input-bar {
    padding-inline: 8px;
  }
  .message-image-wrapper {
    max-width: 70vw;
  }
}