/* ==========================================================================
   Media & Files — Telegram-style styles
   ========================================================================== */

/* --- Media message bubbles --- */
.message-bubble.media {
  padding: 6px;
  max-width: 100%;
  overflow: hidden;
}

.message-bubble.media .media-image,
.message-bubble.media .media-video,
.message-bubble.media .media-audio {
  position: relative;
  border-radius: 12px;
  overflow: hidden;
  max-width: 100%;
}

/* --- Lazy images --- */
.lazy-image {
  display: block;
  max-width: 100%;
  height: auto;
  border-radius: 10px;
  cursor: pointer;
  transition: opacity 0.3s ease;
  background: rgba(0,0,0,0.2);
  min-height: 80px;
  object-fit: contain;
}

.lazy-image[src=""] {
  opacity: 0;
  min-height: 80px;
}

/* --- Download button overlay on media --- */
.download-btn {
  position: absolute;
  bottom: 8px;
  right: 8px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(0,0,0,0.55);
  color: #fff;
  border: none;
  cursor: pointer;
  display: grid;
  place-items: center;
  font-size: 16px;
  backdrop-filter: blur(6px);
  transition: background 0.2s;
  z-index: 2;
}

.download-btn:hover {
  background: rgba(0,0,0,0.75);
}

/* --- Video --- */
.media-video {
  position: relative;
  background: #000;
  max-width: 100%;
}

.video-poster {
  display: block;
  width: 100%;
  max-height: 360px;
  object-fit: cover;
  border-radius: 10px;
}

.video-poster.empty {
  width: 100%;
  height: 180px;
  background: rgba(0,0,0,0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
}

.play-btn {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: rgba(0,0,0,0.55);
  color: #fff;
  border: 2px solid rgba(255,255,255,0.3);
  cursor: pointer;
  font-size: 24px;
  display: grid;
  place-items: center;
  backdrop-filter: blur(6px);
  transition: transform 0.15s, background 0.2s;
  z-index: 2;
}

.play-btn:hover {
  transform: translate(-50%, -50%) scale(1.1);
  background: rgba(0,0,0,0.7);
}

.media-video video {
  width: 100%;
  max-height: 70vh;
  border-radius: 10px;
  background: #000;
}

/* --- Audio --- */
.media-audio {
  padding: 8px 12px;
  min-width: 260px;
}

.media-audio audio {
  width: 100%;
  max-width: 360px;
  border-radius: 24px;
}

/* --- File message --- */
.message-bubble.file {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
}

.message-bubble.file .file-name {
  flex: 1;
  font-size: 14px;
  word-break: break-word;
  color: var(--accent);
}

.message-bubble.file .download-btn {
  position: static;
  width: 32px;
  height: 32px;
  font-size: 14px;
}

/* ==========================================================================
   Pending upload bubbles
   ========================================================================== */

.pending-bubble {
  position: relative;
  opacity: 0.85;
}

.pending-bubble .media-preview {
  position: relative;
  margin-bottom: 6px;
  border-radius: 10px;
  overflow: hidden;
  max-height: 200px;
  background: rgba(0,0,0,0.2);
}

.pending-bubble .media-preview .thumbnail {
  display: block;
  width: 100%;
  max-height: 200px;
  object-fit: cover;
  border-radius: 8px;
}

.pending-bubble .file-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.pending-bubble .filename {
  font-size: 13px;
  font-weight: 600;
  word-break: break-word;
}

.pending-bubble .filesize {
  font-size: 12px;
  color: var(--textMuted);
}

.pending-bubble .speed {
  font-size: 11px;
  color: var(--accent);
}

.pending-bubble .cancel-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: rgba(255,77,109,0.25);
  color: var(--danger);
  border: none;
  cursor: pointer;
  font-size: 14px;
  display: grid;
  place-items: center;
  transition: background 0.2s;
}

.pending-bubble .cancel-btn:hover {
  background: rgba(255,77,109,0.4);
}

/* Needs-file box */
.needs-file {
  margin-top: 8px;
  padding: 10px;
  border-radius: 10px;
  background: rgba(255,193,7,0.1);
  border: 1px solid rgba(255,193,7,0.3);
}

.needs-file.hidden {
  display: none;
}

.needs-file .needs-text {
  font-size: 12px;
  color: var(--textMuted);
  margin-bottom: 6px;
}

.attach-again-btn {
  padding: 6px 14px;
  border-radius: 999px;
  border: 1px solid var(--accent);
  background: rgba(124,92,255,0.15);
  color: var(--accent);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.attach-again-btn:hover {
  background: rgba(124,92,255,0.3);
}

.needs-file .hint {
  font-size: 11px;
  color: var(--textMuted);
  margin-top: 4px;
}

/* ==========================================================================
   Albums — grid layout
   ========================================================================== */

.album-grid {
  display: grid;
  gap: 3px;
  border-radius: 12px;
  overflow: hidden;
}

/* 2 items */
.album-grid.grid-2 {
  grid-template-columns: 1fr 1fr;
}

/* 3 items — first spans 2 rows */
.album-grid.grid-3 {
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
}

.album-grid.grid-3 .album-item:first-child {
  grid-row: 1 / 3;
}

/* 4+ items — 2x2 */
.album-grid.grid-4 {
  grid-template-columns: 1fr 1fr;
}

.album-item {
  position: relative;
  aspect-ratio: 1;
  overflow: hidden;
  cursor: pointer;
  background: rgba(0,0,0,0.2);
  transition: opacity 0.15s;
}

.album-item:hover {
  opacity: 0.85;
}

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

.album-item .thumb.empty {
  width: 100%;
  height: 100%;
  background: rgba(255,255,255,0.05);
}

.album-more {
  position: absolute;
  bottom: 8px;
  right: 8px;
  background: rgba(0,0,0,0.6);
  color: #fff;
  font-size: 14px;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 8px;
  backdrop-filter: blur(4px);
  z-index: 2;
  pointer-events: none;
}

.download-album-btn {
  margin-top: 6px;
  padding: 6px 14px;
  border-radius: 999px;
  border: 1px solid var(--accent);
  background: rgba(124,92,255,0.15);
  color: var(--accent);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
  align-self: flex-end;
}

.download-album-btn:hover {
  background: rgba(124,92,255,0.3);
}

/* ==========================================================================
   Media Viewer — full-screen overlay
   ========================================================================== */

.media-viewer {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(0,0,0,0.95);
  display: flex;
  flex-direction: column;
  animation: viewerIn 0.2s ease-out;
}

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

.viewer-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: rgba(0,0,0,0.5);
  backdrop-filter: blur(8px);
  z-index: 2;
}

.viewer-header .close-btn,
.viewer-header .viewer-download-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(255,255,255,0.1);
  color: #fff;
  border: none;
  cursor: pointer;
  font-size: 16px;
  display: grid;
  place-items: center;
  transition: background 0.2s;
}

.viewer-header .close-btn:hover,
.viewer-header .viewer-download-btn:hover {
  background: rgba(255,255,255,0.2);
}

.viewer-header .counter {
  flex: 1;
  text-align: center;
  color: #fff;
  font-size: 14px;
}

.viewer-content {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.viewer-media {
  display: flex;
  align-items: center;
  justify-content: center;
  max-width: 100%;
  max-height: 100%;
  padding: 12px;
}

.viewer-media img {
  max-width: 100%;
  max-height: 85vh;
  object-fit: contain;
  border-radius: 4px;
}

.viewer-media video {
  max-width: 100%;
  max-height: 85vh;
  border-radius: 4px;
}

.nav-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: rgba(255,255,255,0.1);
  color: #fff;
  border: none;
  cursor: pointer;
  font-size: 24px;
  display: grid;
  place-items: center;
  backdrop-filter: blur(6px);
  transition: background 0.2s;
  z-index: 2;
}

.nav-btn:hover {
  background: rgba(255,255,255,0.2);
}

.nav-btn.prev { left: 12px; }
.nav-btn.next { right: 12px; }

/* ==========================================================================
   Selection mode
   ========================================================================== */

.selection-panel {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 900;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: var(--bgSecondary);
  border-top: 1px solid var(--border);
  backdrop-filter: blur(12px);
  box-shadow: 0 -4px 20px rgba(0,0,0,0.3);
}

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

#selected-count {
  flex: 1;
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

.selection-panel button {
  padding: 8px 16px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: rgba(255,255,255,0.05);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
  white-space: nowrap;
}

.selection-panel button:hover {
  background: rgba(255,255,255,0.1);
}

#sel-delete {
  border-color: rgba(255,77,109,0.3);
  color: var(--danger);
}

#sel-delete:hover {
  background: rgba(255,77,109,0.15);
}

/* Selected message highlight */
body.selection-mode .message {
  cursor: pointer;
  transition: background 0.15s;
}

body.selection-mode .message.selected {
  background: rgba(124,92,255,0.15);
  outline: 2px solid var(--accent);
  outline-offset: -2px;
  border-radius: 12px;
}

/* ==========================================================================
   Attach menu
   ========================================================================== */

.attach-menu {
  position: absolute;
  bottom: 100%;
  left: 8px;
  z-index: 100;
  background: var(--bgSecondary);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 6px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(16px);
  min-width: 160px;
}

.attach-menu.hidden {
  display: none;
}

.attach-menu button {
  display: block;
  width: 100%;
  padding: 10px 14px;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 14px;
  text-align: left;
  cursor: pointer;
  border-radius: 10px;
  transition: background 0.15s;
}

.attach-menu button:hover {
  background: rgba(255,255,255,0.08);
}

/* ==========================================================================
   Responsive: mobile adjustments
   ========================================================================== */

@media (max-width: 600px) {
  .album-grid.grid-3,
  .album-grid.grid-4 {
    grid-template-columns: 1fr 1fr;
  }

  .nav-btn {
    width: 36px;
    height: 36px;
    font-size: 20px;
  }

  .nav-btn.prev { left: 6px; }
  .nav-btn.next { right: 6px; }

  .selection-panel {
    padding: 10px 12px;
    gap: 6px;
    flex-wrap: wrap;
  }

  .selection-panel button {
    padding: 6px 12px;
    font-size: 12px;
  }

  .play-btn {
    width: 48px;
    height: 48px;
    font-size: 20px;
  }

  .download-btn {
    width: 32px;
    height: 32px;
    font-size: 14px;
  }
}

/* ==========================================================================
   Reduced motion
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
  .media-viewer {
    animation: none;
  }

  .album-item,
  .download-btn,
  .play-btn,
  .attach-menu button {
    transition: none;
  }
}

/* ===== Telegram-style Send Popup ===== */

.send-popup-overlay {
  position: fixed;
  inset: 0;
  z-index: 9000;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(8px);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.send-popup-overlay.active {
  display: flex;
}

.send-popup {
  background: var(--bgPrimary);
  border-radius: 20px;
  width: 100%;
  max-width: 480px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0,0,0,0.4);
  animation: sp-enter 200ms ease-out;
}

@keyframes sp-enter {
  from {
    opacity: 0;
    transform: scale(0.95);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.sp-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px 10px;
}
.sp-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--textPrimary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sp-close {
  background: none;
  border: none;
  font-size: 24px;
  color: var(--textMuted);
  cursor: pointer;
  padding: 0 4px;
  flex-shrink: 0;
}
.sp-close:hover {
  color: var(--textPrimary);
}

/* Thumbnail grid — CSS Grid, 3 columns */
.sp-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  padding: 12px 16px;
  overflow-y: auto;
  max-height: 50vh;
}
.sp-grid::-webkit-scrollbar { width: 4px; }
.sp-grid::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.15);
  border-radius: 999px;
}

.sp-card {
  aspect-ratio: 1;
  border-radius: 12px;
  overflow: hidden;
  position: relative;
  background: var(--bgSecondary);
}
.sp-preview {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 12px;
  overflow: hidden;
  background: var(--bgSecondary);
}
.sp-thumb {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.sp-file-emoji {
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  font-size: 36px;
}

/* Circular progress ring */
.sp-progress-ring {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}
.sp-ring-fill {
  transition: stroke-dashoffset 0.3s ease;
}

.sp-status {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 13px;
  font-weight: 700;
  color: #fff;
  text-shadow: 0 1px 3px rgba(0,0,0,0.6);
  pointer-events: none;
}
.sp-status.sp-done {
  color: #4caf50;
  font-size: 24px;
}
.sp-status.sp-failed {
  color: #f44336;
  font-size: 18px;
}

.sp-info {
  display: none;
}

/* Caption row */
.sp-caption-row {
  padding: 10px 16px;
  border-top: 1px solid var(--border);
}
.sp-caption-row textarea {
  width: 100%;
  resize: none;
  border-radius: 12px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  background: var(--inputBg);
  font-size: 15px;
  color: var(--textPrimary);
  font-family: inherit;
  max-height: 120px;
}
.sp-caption-row textarea:focus {
  outline: none;
  border-color: var(--accent);
}

/* Action buttons */
.sp-actions {
  display: flex;
  gap: 8px;
  padding: 10px 16px 16px;
}
.sp-cancel {
  flex: 1;
  padding: 12px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--textPrimary);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.sp-cancel:hover { background: var(--bgSecondary); }
.sp-send {
  flex: 2;
  padding: 12px;
  border-radius: 14px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
}
.sp-send:hover { opacity: 0.9; }
.sp-send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Mobile: bottom sheet */
@media (max-width: 480px) {
  .send-popup-overlay {
    align-items: flex-end;
    padding: 0;
  }
  .send-popup {
    max-height: 85vh;
    border-radius: 20px 20px 0 0;
    max-width: 100%;
    animation: sp-slide-up 250ms ease-out;
  }
  @keyframes sp-slide-up {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
  }
}

/* Lightbox */
.sp-lightbox {
  position: fixed;
  inset: 0;
  z-index: 9100;
  background: rgba(0,0,0,0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  animation: sp-fade-in 150ms ease-out;
}
@keyframes sp-fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}
.sp-lightbox-img {
  max-width: 90vw;
  max-height: 90vh;
  object-fit: contain;
  border-radius: 8px;
}
.sp-lightbox-close {
  position: absolute;
  top: 16px;
  right: 16px;
  background: none;
  border: none;
  font-size: 32px;
  color: #fff;
  cursor: pointer;
  z-index: 1;
  opacity: 0.7;
  padding: 0;
  line-height: 1;
}
.sp-lightbox-close:hover { opacity: 1; }

/* Drag reorder */
.sp-card {
  touch-action: none;
  user-select: none;
}
.sp-card.sp-dragging {
  opacity: 0.85;
  transform: scale(1.05);
  box-shadow: 0 12px 40px rgba(0,0,0,0.5);
  transition: none;
  z-index: 10;
}
