/* ============================================
   SCHOOL CHAT SYSTEM - CSS
   Group chat WhatsApp style
   ============================================ */

.chat-screen,
#screen-chat {
    display: flex !important;
    flex-direction: column !important;
    height: 100% !important;
    min-height: 0 !important;
    max-height: 100% !important;
    overflow: hidden !important;
    background: #f0f4f8;
}

/* ROOMS LIST */
.chat-rooms-header {
  padding: 16px;
  background: white;
  border-bottom: 1px solid #eee;
  display: flex;
  align-items: center;
  gap: 12px;
}

.chat-rooms-header h2 {
  font-size: 18px;
  font-weight: 800;
  flex: 1;
}

.chat-search-bar {
  padding: 10px 16px;
  background: white;
  border-bottom: 1px solid #f0f0f0;
}

.chat-search-bar input {
  width: 100%;
  padding: 9px 14px;
  border: 1px solid #e0e0e0;
  border-radius: 20px;
  font-size: 14px;
  font-family: inherit;
  background: #f8f8f8;
  outline: none;
}

/* ==================== CHAT ROOMS LIST - FIXED SCROLL ==================== */
.chat-rooms-list {
    flex: 1 1 0px !important;
    min-height: 0 !important;
    height: 100% !important;           /* fallback */
    max-height: 100% !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    display: flex !important;
    flex-direction: column !important;

    /* Remove debug styles */
    border: none !important;
    background: transparent !important;
}

/* Force scrollbar to be visible for testing */
.chat-rooms-list::-webkit-scrollbar {
  width: 8px;
}
.chat-rooms-list::-webkit-scrollbar-thumb {
  background: grey;
  border-radius: 20px;
}

.chat-room-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid #f5f5f5;
  cursor: pointer;
  transition: background 0.15s;
  flex-shrink: 0;
  min-height: 70px;
}

.chat-room-item:hover, .chat-room-item:active { background: #f5f5ff; }

.chat-room-avatar {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  background: linear-gradient(135deg, #5E5CE6, #007AFF);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 700;
  flex-shrink: 0;
  overflow: hidden;
  position: relative;
}

.chat-room-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.chat-room-avatar.broadcast-avatar {
  background: linear-gradient(135deg, #FF9500, #FF6B6B);
  font-size: 20px;
}

.chat-room-avatar.principal-avatar {
  background: linear-gradient(135deg, #34C759, #007AFF);
}

.chat-room-info { flex: 1; min-width: 0; }

.chat-room-name {
  font-size: 15px;
  font-weight: 700;
  color: #1a1a2e;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-room-sub {
  font-size: 12px;
  color: #888;
  margin-top: 1px;
}

.chat-room-preview {
  font-size: 13px;
  color: #aaa;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.chat-room-meta {
  text-align: right;
  flex-shrink: 0;
}

.chat-room-time {
  font-size: 11px;
  color: #bbb;
  margin-bottom: 4px;
}

.chat-unread-badge {
  background: var(--accent, #5E5CE6);
  color: white;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 700;
  padding: 2px 7px;
  min-width: 20px;
  text-align: center;
  display: inline-block;
}

/* CHAT WINDOW */
.chat-window {
    display: none;
    flex-direction: column;
    height: 100%;
    min-height: 0;
    max-height: 100%;
    position: absolute;
    inset: 0;
    background: #ECE5DD;
    z-index: 10;
}
.chat-window.active { display: flex; }

.chat-window-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: var(--accent, #5E5CE6);
  color: white;
  flex-shrink: 0;
}

.chat-window-back {
  font-size: 20px;
  cursor: pointer;
  padding: 4px;
  line-height: 1;
}

.chat-window-avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: rgba(255,255,255,0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 700;
  flex-shrink: 0;
  overflow: hidden;
}

.chat-window-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.chat-window-title {
  flex: 1;
  min-width: 0;
}

.chat-window-title h3 {
  font-size: 15px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-window-title p {
  font-size: 11px;
  opacity: 0.8;
}

/* Messages */
.chat-messages-area {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 0;
}

.chat-date-divider {
  text-align: center;
  margin: 8px 0;
}

.chat-date-divider span {
  background: rgba(225,221,216,0.9);
  font-size: 11px;
  color: #777;
  padding: 3px 12px;
  border-radius: 10px;
  font-weight: 600;
}

.msg-row {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  max-width: 82%;
}

.msg-row.sent {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.msg-row.received {
  align-self: flex-start;
}

.msg-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: linear-gradient(135deg, #5E5CE6, #007AFF);
  color: white;
  font-size: 11px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  overflow: hidden;
  margin-bottom: 2px;
}

.msg-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.msg-bubble-wrap { display: flex; flex-direction: column; min-width: 0; }

.msg-sender-name {
  font-size: 11px;
  font-weight: 700;
  color: #5E5CE6;
  margin-bottom: 2px;
  padding-left: 10px;
}

.msg-bubble {
  background: white;
  border-radius: 14px 14px 14px 2px;
  padding: 7px 10px;
  font-size: 14px;
  line-height: 1.4;
  color: #1a1a2e;
  box-shadow: 0 1px 2px rgba(0,0,0,0.08);
  word-break: break-word;
  position: relative;
}

.msg-row.sent .msg-bubble {
  background: #DCF8C6;
  border-radius: 14px 14px 2px 14px;
}

.msg-time {
  font-size: 10px;
  color: #aaa;
  margin-top: 2px;
  padding: 0 4px;
  align-self: flex-end;
}

.msg-row.sent .msg-time { align-self: flex-end; }

/* Media in bubbles */
.msg-image-wrap {
  border-radius: 10px;
  overflow: hidden;
  max-width: 200px;
  cursor: pointer;
}

.msg-image-wrap img {
  width: 100%;
  display: block;
  border-radius: 10px;
}

.msg-video-wrap {
  border-radius: 10px;
  overflow: hidden;
  max-width: 200px;
  position: relative;
  cursor: pointer;
}

.msg-video-play {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.3);
  border-radius: 10px;
}

.msg-video-play span {
  font-size: 32px;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.4));
}

.msg-doc-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 0;
  cursor: pointer;
  text-decoration: none;
  color: inherit;
}

.msg-doc-icon {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: var(--accent, #5E5CE6);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
}

.msg-doc-info { flex: 1; min-width: 0; }

.msg-doc-name {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.msg-doc-ext { font-size: 11px; color: #888; text-transform: uppercase; }

/* Reply preview inside bubble */
.msg-reply-preview {
  border-left: 3px solid var(--accent, #5E5CE6);
  padding: 4px 8px;
  margin-bottom: 6px;
  border-radius: 4px;
  background: rgba(94,92,230,0.07);
}

.msg-reply-preview .reply-sender { font-size: 11px; font-weight: 700; color: #5E5CE6; }
.msg-reply-preview .reply-text { font-size: 12px; color: #666; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Deleted message */
.msg-bubble.deleted {
  font-style: italic;
  color: #aaa;
  font-size: 13px;
}

/* Reply bar above input */
.chat-reply-bar {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  background: #f0f0f0;
  border-top: 1px solid #e0e0e0;
}

.chat-reply-bar.active { display: flex; }

.chat-reply-bar-content {
  flex: 1;
  border-left: 3px solid var(--accent, #5E5CE6);
  padding-left: 8px;
  min-width: 0;
}

.chat-reply-bar-sender { font-size: 11px; font-weight: 700; color: #5E5CE6; }
.chat-reply-bar-text { font-size: 12px; color: #666; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.chat-reply-close {
  font-size: 18px;
  cursor: pointer;
  color: #888;
  padding: 4px;
  flex-shrink: 0;
}

/* Input area */
.chat-input-area {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 10px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  background: #ECE5DD;
  flex-shrink: 0;
  position: relative;
  z-index: 5;
}

.chat-attach-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: none;
  border: none;
  font-size: 22px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #555;
  flex-shrink: 0;
}

.chat-text-input {
  flex: 1;
  min-height: 38px;
  max-height: 100px;
  padding: 9px 14px;
  border: none;
  border-radius: 20px;
  background: white;
  font-size: 14px;
  font-family: inherit;
  outline: none;
  resize: none;
  overflow-y: auto;
  line-height: 1.4;
}

.chat-send-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--accent, #5E5CE6);
  color: white;
  border: none;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: transform 0.1s;
}

.chat-send-btn:active { transform: scale(0.9); }

/* Attach menu */
.chat-attach-menu {
  display: none;
  position: absolute;
  bottom: 60px;
  left: 10px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.15);
  padding: 8px;
  z-index: 100;
}

.chat-attach-menu.active { display: block; }

.attach-menu-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  cursor: pointer;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  transition: background 0.15s;
}

.attach-menu-item:hover { background: #f5f5f5; }

.attach-menu-item span:first-child { font-size: 20px; }

/* Upload progress */
.chat-upload-progress {
  padding: 6px 14px;
  background: rgba(94,92,230,0.1);
  font-size: 12px;
  color: #5E5CE6;
  font-weight: 600;
  display: none;
}
.chat-upload-progress.active { display: block; }

/* Media lightbox */
.chat-lightbox {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.9);
  z-index: 9999;
  align-items: center;
  justify-content: center;
}

.chat-lightbox.active { display: flex; }

.chat-lightbox img, .chat-lightbox video {
  max-width: 95vw;
  max-height: 90vh;
  border-radius: 8px;
}

.chat-lightbox-close {
  position: absolute;
  top: 16px;
  right: 16px;
  color: white;
  font-size: 28px;
  cursor: pointer;
  z-index: 10;
}

/* Long press context menu */
.chat-context-menu {
  display: none;
  position: fixed;
  background: white;
  border-radius: 14px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.2);
  z-index: 1000;
  overflow: hidden;
  min-width: 160px;
}

.chat-context-menu.active { display: block; }

.ctx-menu-item {
  padding: 12px 18px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  border-bottom: 1px solid #f0f0f0;
  transition: background 0.15s;
}

.ctx-menu-item:last-child { border-bottom: none; }
.ctx-menu-item:hover { background: #f5f5f5; }
.ctx-menu-item.danger { color: #FF3B30; }

/* Monitor banner */
.monitor-banner {
  background: linear-gradient(135deg, #FF9500, #FF6B6B);
  color: white;
  text-align: center;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 700;
}

/* Loading spinner */
.chat-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  color: #aaa;
  font-size: 14px;
  flex-direction: column;
  gap: 12px;
}

.chat-spinner {
  width: 28px;
  height: 28px;
  border: 3px solid #eee;
  border-top-color: var(--accent, #5E5CE6);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

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

