/* SW1 · media-gallery · 2026-09-09
 * Галерея чата (mg-sheet), превью отправки (mg-preview), тумб-прогресс (mg-upbar),
 * заглушка недоступного медиа (mg-dead), кнопка «во весь экран» на видео (mg-vexpand).
 * Mobile 390px first; light/dark через токены --surface/--text; reduced-motion уважаем.
 */

/* ═══ Кнопка «Медиа» в шапке треда ═══ */
.btn-chat-media {
  color: var(--text, #0f172a);
}

#thread-header .btn-chat-media {
  flex-shrink: 0;
}

/* ═══ Шит галереи ═══ */
.mg-sheet {
  position: fixed;
  inset: 0;
  z-index: 120;
  display: grid;
  place-items: end center;
}

.mg-sheet[hidden] {
  display: none !important;
}

html.mg-gallery-open,
html.mg-gallery-open body {
  overflow: hidden;
}

.mg-sheet__scrim {
  position: absolute;
  inset: 0;
  border: 0;
  margin: 0;
  padding: 0;
  background: rgba(15, 23, 42, 0.46);
  cursor: default;
}

.mg-sheet__panel {
  position: relative;
  z-index: 2;
  width: min(640px, 100%);
  height: min(86dvh, 760px);
  display: flex;
  flex-direction: column;
  background: var(--surface, #fff);
  color: var(--text, #0f172a);
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -12px 40px rgba(15, 23, 42, 0.18);
  overflow: hidden;
  animation: mg-rise 0.26s ease;
}

@keyframes mg-rise {
  from {
    transform: translateY(40px);
    opacity: 0.6;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.mg-sheet__head {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: max(10px, env(safe-area-inset-top)) 16px 6px;
}

.mg-sheet__close {
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  border: 0;
  border-radius: 50%;
  background: var(--surface-2, #eef1f6);
  color: var(--text, #0f172a);
  display: grid;
  place-items: center;
  cursor: pointer;
}

.mg-sheet__titles {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.mg-sheet__titles h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.mg-sheet__sub {
  font-size: 13px;
  color: var(--text-muted, #64748b);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* чипы Фото/Видео/Файлы */
.mg-sheet__chips {
  flex-shrink: 0;
  display: flex;
  gap: 8px;
  padding: 8px 16px 10px;
  overflow-x: auto;
  scrollbar-width: none;
}

.mg-sheet__chips::-webkit-scrollbar {
  display: none;
}

.mg-chip {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 34px;
  padding: 0 14px;
  border: 1px solid var(--border, #e2e8f0);
  border-radius: 999px;
  background: var(--surface, #fff);
  color: var(--text, #0f172a);
  font-size: 13px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
}

.mg-chip.is-active {
  background: var(--accent, #1a56db);
  border-color: var(--accent, #1a56db);
  color: #fff;
}

.mg-chip__n {
  font-size: 11px;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--surface-2, #eef1f6);
  color: var(--text-muted, #64748b);
}

.mg-chip.is-active .mg-chip__n {
  background: rgba(255, 255, 255, 0.25);
  color: #fff;
}

/* сетка + месяцы */
.mg-sheet__grid {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 0 12px calc(16px + env(safe-area-inset-bottom, 0px));
}

.mg-month {
  position: sticky;
  top: 0;
  z-index: 3;
  padding: 10px 4px 8px;
  font-size: 13px;
  font-weight: 700;
  color: var(--text-muted, #64748b);
  background: var(--surface, #fff);
  text-transform: none;
}

.mg-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 3px;
}

@media (min-width: 520px) {
  .mg-grid {
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
  }
}

@media (min-width: 900px) {
  .mg-grid {
    grid-template-columns: repeat(6, 1fr);
  }
}

.mg-cell {
  position: relative;
  aspect-ratio: 1 / 1;
  padding: 0;
  border: 0;
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface-2, #eef1f6);
  cursor: zoom-in;
  -webkit-tap-highlight-color: transparent;
  min-width: 0;
}

.mg-cell img,
.mg-cell video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

/* shimmer, пока кадр не пришёл */
.mg-cell.is-wait::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 1;
  background: linear-gradient(100deg, transparent 30%, rgba(255, 255, 255, 0.35) 50%, transparent 70%) var(--surface-2, #eef1f6);
  background-size: 220% 100%;
  animation: mg-shimmer 1.3s linear infinite;
}

@keyframes mg-shimmer {
  from {
    background-position: 180% 0;
  }
  to {
    background-position: -80% 0;
  }
}

html[data-theme="dark"] .mg-cell.is-wait::before {
  background: linear-gradient(100deg, transparent 30%, rgba(255, 255, 255, 0.07) 50%, transparent 70%) var(--surface-2, #242c3c);
  background-size: 220% 100%;
}

.mg-cell__play {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 2;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.45);
  color: #fff;
  pointer-events: none;
}

.mg-cell__dur {
  position: absolute;
  right: 5px;
  bottom: 5px;
  z-index: 2;
  padding: 1px 6px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}

.mg-cell__dur:empty {
  display: none;
}

/* недоступное медиа — честная заглушка */
.mg-cell.is-dead {
  cursor: default;
  background: var(--surface-2, #eef1f6);
}

.mg-cell.is-dead::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 26px;
  height: 26px;
  background: var(--text-muted, #64748b);
  opacity: 0.5;
  -webkit-mask: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='1.8'><rect x='3' y='5' width='18' height='14' rx='2'/><path d='m8.5 10.5 1.5 1.5m0-1.5-1.5 1.5M14.5 10.5l1.5 1.5m0-1.5-1.5 1.5'/></svg>") center / contain no-repeat;
  mask: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='1.8'><rect x='3' y='5' width='18' height='14' rx='2'/><path d='m8.5 10.5 1.5 1.5m0-1.5-1.5 1.5M14.5 10.5l1.5 1.5m0-1.5-1.5 1.5'/></svg>") center / contain no-repeat;
}

/* вкладка Файлы */
.mg-files {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding-bottom: 8px;
}

.mg-file {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border, #e2e8f0);
  border-radius: 12px;
  background: var(--surface, #fff);
  color: var(--text, #0f172a);
  font: inherit;
  text-align: left;
  cursor: pointer;
  min-width: 0;
}

.mg-file__ext {
  flex-shrink: 0;
  min-width: 46px;
  height: 40px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  background: var(--surface-2, #eef1f6);
  color: var(--text-muted, #64748b);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.04em;
}

.mg-file__body {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.mg-file__name {
  font-size: 14px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mg-file__meta {
  font-size: 12px;
  color: var(--text-muted, #64748b);
}

/* пустые состояния и статус */
.mg-empty {
  display: grid;
  place-items: center;
  gap: 6px;
  padding: 48px 16px;
  color: var(--text-muted, #64748b);
  font-size: 14px;
  font-weight: 600;
  text-align: center;
}

.mg-empty__hint {
  font-size: 13px;
  font-weight: 400;
  max-width: 260px;
}

.mg-empty__ico {
  opacity: 0.5;
}

.mg-shimmer {
  pointer-events: none;
}

.mg-sheet__status {
  flex-shrink: 0;
  padding: 8px 16px calc(10px + env(safe-area-inset-bottom, 0px));
  font-size: 13px;
  color: var(--danger, #dc2626);
  text-align: center;
}

.mg-sheet__more {
  flex-shrink: 0;
  margin: 4px 16px calc(14px + env(safe-area-inset-bottom, 0px));
  height: 42px;
  border: 1px solid var(--border, #e2e8f0);
  border-radius: 12px;
  background: var(--surface-2, #eef1f6);
  color: var(--text, #0f172a);
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
}

.mg-sheet__more.is-busy {
  opacity: 0.6;
  pointer-events: none;
}

/* ═══ Превью вложений ДО отправки (album sheet) ═══ */
.mg-preview {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  background: var(--surface-2);
  border-radius: 14px;
  padding: 6px;
  max-height: 44dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
}

@media (min-width: 420px) {
  .mg-preview {
    grid-template-columns: repeat(4, 1fr);
  }
}

.mg-preview__cell {
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: 10px;
  overflow: hidden;
  background: var(--surface, #fff);
  min-width: 0;
}

.mg-preview__cell img,
.mg-preview__cell video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.mg-preview__cell.is-wait video {
  opacity: 0;
}

.mg-preview__cell.is-wait::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 1;
  background: linear-gradient(100deg, transparent 30%, rgba(255, 255, 255, 0.4) 50%, transparent 70%) var(--surface-2, #eef1f6);
  background-size: 220% 100%;
  animation: mg-shimmer 1.3s linear infinite;
}

.mg-preview__play {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.45);
  color: #fff;
  pointer-events: none;
}

.mg-preview__rm {
  position: absolute;
  right: 4px;
  top: 4px;
  z-index: 3;
  width: 26px;
  height: 26px;
  border: 0;
  border-radius: 50%;
  background: rgba(12, 14, 20, 0.66);
  color: #fff;
  display: grid;
  place-items: center;
  cursor: pointer;
}

.mg-preview__rm:active {
  background: rgba(12, 14, 20, 0.85);
}

.mg-preview__cell--doc {
  grid-column: 1 / -1;
  aspect-ratio: auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 34px 10px 10px;
  min-height: 56px;
}

.mg-preview__doc-icon {
  flex-shrink: 0;
}

.mg-preview__doc-name {
  flex: 1;
  min-width: 0;
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mg-preview__doc-size {
  flex-shrink: 0;
  font-size: 12px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

/* тумблер «оригинал» */
.mg-toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--surface-2);
  cursor: pointer;
  user-select: none;
}

.mg-toggle input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.mg-toggle__box {
  flex-shrink: 0;
  width: 40px;
  height: 24px;
  border-radius: 999px;
  background: var(--border, #e2e8f0);
  position: relative;
  transition: background var(--motion-base, 0.22s ease);
}

.mg-toggle__box::after {
  content: "";
  position: absolute;
  left: 3px;
  top: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
  transition: transform var(--motion-base, 0.22s ease);
}

.mg-toggle input:checked + .mg-toggle__box {
  background: var(--accent, #1a56db);
}

.mg-toggle input:checked + .mg-toggle__box::after {
  transform: translateX(16px);
}

.mg-toggle input:focus-visible + .mg-toggle__box {
  box-shadow: 0 0 0 2px var(--accent-soft-strong, rgba(26, 86, 219, 0.16));
}

.mg-toggle__label {
  display: flex;
  flex-direction: column;
  gap: 1px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

.mg-toggle__hint {
  font-size: 12px;
  font-weight: 400;
  color: var(--text-muted);
}

/* ═══ Видео-пузырь: кнопка «во весь экран» ═══ */
.mg-vwrap {
  position: relative;
  max-width: 100%;
}

.mg-vwrap .bubble__video {
  display: block;
  max-width: 100%;
}

.mg-vexpand {
  position: absolute;
  right: 10px;
  top: 10px;
  z-index: 4;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 50%;
  background: rgba(12, 14, 20, 0.55);
  color: #fff;
  display: grid;
  place-items: center;
  cursor: pointer;
  opacity: 0.92;
  transition: opacity var(--motion-fast, 0.14s ease);
}

.mg-vexpand:active {
  opacity: 1;
  background: rgba(12, 14, 20, 0.8);
}

.mg-vwrap.is-dead::after {
  content: "Видео недоступно";
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  padding: 6px 12px;
  border-radius: 10px;
  background: rgba(12, 14, 20, 0.7);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}

/* ═══ Заглушка мёртвого медиа в ленте ═══ */
.mg-dead {
  width: 100%;
  min-height: 140px;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 6px;
  border-radius: var(--radius-bubble, 18px);
  background: var(--surface-2, #eef1f6);
  color: var(--text-muted, #64748b);
  font-size: 13px;
  font-weight: 600;
}

.mg-dead__ico {
  opacity: 0.6;
}

/* ═══ Прогресс загрузки на превью-мозаике ═══ */
.vy-mosaic__cell {
  position: relative;
}

.mg-upbar {
  position: absolute;
  left: 6px;
  right: 6px;
  bottom: 6px;
  z-index: 5;
  height: 4px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.35);
  overflow: hidden;
  pointer-events: none;
}

.mg-upbar__fill {
  height: 100%;
  width: 0;
  border-radius: 999px;
  background: #fff;
  transition: width 0.2s ease;
}

.mg-upbar.is-done {
  opacity: 0;
  transition: opacity 0.4s ease 0.3s;
}

/* ═══ reduced-motion ═══ */
@media (prefers-reduced-motion: reduce) {
  .mg-sheet__panel {
    animation: none;
  }
  .mg-cell.is-wait::before,
  .mg-preview__cell.is-wait::before {
    animation: none;
    background: var(--surface-2, #eef1f6);
  }
  .mg-upbar__fill {
    transition: none;
  }
  .mg-toggle__box,
  .mg-toggle__box::after {
    transition: none;
  }
}
