/* Вложения карточки объекта: карта (галерея, просмотрщик) и договор (чипы у поля «Номер договора»). */

.files-root {
  display: grid;
  gap: 14px;
}

.section-card:has([data-files-count]) .section-card__top {
  display: flex;
  align-items: center;
  gap: 12px;
}

.files-count {
  margin-left: auto;
  white-space: nowrap;
  font: 600 13px/1 "IBM Plex Mono", monospace;
  color: var(--text-soft);
  background: var(--surface-soft);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 7px 12px;
}

.files-note,
.files-empty {
  grid-column: 1 / -1;
  margin: 0;
  color: var(--text-soft);
  font-size: 15px;
}

/* Галерея карты */

.files-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 14px;
}

.files-tile {
  position: relative;
  display: grid;
  gap: 8px;
  min-width: 0;
  padding: 6px 6px 10px;
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-md);
  background: var(--surface-strong);
  box-shadow: var(--glass-shadow-sm);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.files-tile:hover {
  transform: translateY(-2px);
  box-shadow: var(--glass-shadow-md);
}

.files-tile__media {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  aspect-ratio: 4 / 3;
  padding: 0;
  border: 0;
  border-radius: calc(var(--radius-md) - 6px);
  overflow: hidden;
  background: var(--surface-soft);
  color: var(--steel);
  text-decoration: none;
  cursor: pointer;
}

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

.files-tile__media--file {
  flex-direction: column;
  gap: 6px;
  cursor: pointer;
}

.files-tile__media--file svg {
  width: 40px;
  height: 40px;
}

.files-tile__ext {
  font: 700 18px/1 "IBM Plex Mono", monospace;
  color: var(--text);
}

.files-tile__caption {
  display: grid;
  gap: 2px;
  min-width: 0;
  padding: 0 4px;
}

.files-tile__name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
}

.files-tile__meta {
  font-size: 13px;
  color: var(--text-muted);
}

.files-tile__remove {
  position: absolute;
  top: 12px;
  right: 12px;
}

/* Кнопки и иконки */

.files-icon-btn {
  display: inline-grid;
  place-items: center;
  flex: none;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  background: var(--surface-strong);
  color: var(--text-soft);
  cursor: pointer;
}

.files-btn,
.files-attach {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 36px;
  padding: 0 14px;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  background: var(--surface-soft);
  color: var(--primary);
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  text-decoration: none;
  cursor: pointer;
}

.files-btn--danger {
  border-color: var(--danger);
  background: var(--danger);
  color: var(--text-inverse);
}

.files-attach:hover,
.files-btn:hover {
  background: var(--primary-soft);
}

/* Зона загрузки (карта) */

.files-dropzone {
  display: grid;
  justify-items: center;
  gap: 6px;
  width: 100%;
  padding: 20px 16px;
  border: 1.5px dashed var(--line-strong);
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--text-soft);
  font-family: inherit;
  font-size: 15px;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.2s ease, background-color 0.2s ease;
}

.files-dropzone:hover,
.files-dropzone.is-dragover {
  border-color: var(--primary);
  background: var(--primary-soft);
}

.files-dropzone__link {
  color: var(--primary);
  font-weight: 700;
  text-decoration: underline;
}

.files-dropzone__hint {
  font-size: 13px;
  color: var(--text-muted);
}

/* Договор внутри поля «Номер договора» */

.files-contract {
  grid-column: 1 / -1;
  grid-row: 3;
  display: grid;
  gap: 8px;
  min-width: 0;
  margin-top: 4px;
  padding: 8px;
  border-radius: var(--radius-sm);
}

.files-contract.is-dragover {
  background: var(--primary-soft);
  box-shadow: inset 0 0 0 1.5px var(--primary);
}

#formSections .field:has(> .files-contract) > :is(input, select, textarea, .combo-field) {
  grid-column: 1;
  grid-row: 2;
}

#formSections .field:has(> .files-contract) > .copy-btn {
  grid-column: 2;
  grid-row: 2;
}

.files-chips:empty,
.files-uploads:empty {
  display: none;
}

.files-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.files-chip {
  display: inline-flex;
  align-items: center;
  max-width: 100%;
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-soft);
}

.files-chip__link {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  height: 36px;
  padding: 0 6px 0 12px;
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  text-decoration: none;
}

.files-chip__name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 240px;
}

.files-chip__size {
  font: 500 12px/1 "IBM Plex Mono", monospace;
  color: var(--text-muted);
  white-space: nowrap;
}

.files-chip__remove {
  display: inline-grid;
  place-items: center;
  flex: none;
  width: 32px;
  height: 32px;
  margin-right: 2px;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--text-soft);
  cursor: pointer;
}

.files-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.files-actions:empty {
  display: none;
}

/* Загрузки */

.files-uploads {
  display: grid;
  gap: 8px;
}

.files-upload {
  display: grid;
  gap: 6px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface-soft);
}

.files-upload.is-error {
  border-color: var(--danger);
  background: var(--danger-soft);
}

.files-upload__head {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.files-upload__name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
  font-weight: 600;
}

.files-upload__state {
  font: 500 12px/1 "IBM Plex Mono", monospace;
  color: var(--text-muted);
}

.files-upload__error {
  margin: 0;
  color: var(--danger);
  font-size: 14px;
  font-weight: 600;
}

.files-progress {
  height: 6px;
  border-radius: 999px;
  background: var(--line);
  overflow: hidden;
}

.files-progress > span {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: var(--primary);
  transition: width 0.2s ease;
}

/* Просмотрщик */

.files-viewer {
  inset: 0;
  width: 100%;
  max-width: none;
  height: 100%;
  max-height: none;
  margin: 0;
  padding: 0;
  border: 0;
  background: rgba(10, 14, 20, 0.88);
  color: var(--text-inverse);
  flex-direction: column;
}

.files-viewer[open] {
  display: flex;
}

.files-viewer::backdrop {
  background: rgba(10, 14, 20, 0.88);
}

.files-viewer [hidden] {
  display: none !important;
}

.files-viewer__stage {
  position: relative;
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: 16px 64px;
  touch-action: pan-y;
}

.files-viewer__img {
  display: block;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: var(--radius-sm);
  user-select: none;
}

.files-viewer__nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  display: inline-grid;
  place-items: center;
  width: 48px;
  height: 48px;
  padding: 0;
  border: 1px solid rgba(244, 247, 251, 0.25);
  border-radius: 999px;
  background: var(--surface-dark);
  color: var(--text-inverse);
  cursor: pointer;
}

.files-viewer__nav--prev {
  left: 12px;
}

.files-viewer__nav--next {
  right: 12px;
}

.files-viewer__bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  background: var(--surface-dark-strong);
  border-top: 1px solid rgba(244, 247, 251, 0.14);
}

.files-viewer__info {
  min-width: 0;
}

.files-viewer__name {
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 15px;
  font-weight: 700;
}

.files-viewer__meta {
  margin: 2px 0 0;
  font-size: 13px;
  color: rgba(244, 247, 251, 0.72);
}

.files-viewer__actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.files-viewer .files-btn:not(.files-btn--danger) {
  border-color: rgba(244, 247, 251, 0.25);
  background: rgba(244, 247, 251, 0.1);
  color: var(--text-inverse);
}

/* Адаптив: колонки и мишени не меньше 44px */

@media (max-width: 900px) {
  .files-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .files-icon-btn,
  .files-chip__remove,
  .files-btn,
  .files-attach,
  .files-dropzone,
  .files-viewer__nav {
    min-width: 44px;
    min-height: 44px;
  }

  .files-icon-btn,
  .files-chip__remove,
  .files-viewer__nav {
    width: 44px;
    height: 44px;
  }

  .files-chip,
  .files-chip__link {
    min-height: 44px;
    height: 44px;
  }

  .files-chip__link {
    height: 44px;
  }

  .files-tile__remove {
    top: 14px;
    right: 14px;
  }
}

@media (max-width: 600px) {
  .files-viewer__stage {
    padding: 12px 8px;
  }

  .files-viewer__nav--prev {
    left: 4px;
  }

  .files-viewer__nav--next {
    right: 4px;
  }

  .files-chip__name {
    max-width: 180px;
  }
}

/* Доступность и движение */

.files-root :focus-visible,
.files-contract :focus-visible,
.files-viewer :focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  .files-tile,
  .files-tile:hover,
  .files-dropzone,
  .files-progress > span {
    transition: none;
    transform: none;
  }
}

/* Карта не обрезается: плитки крупнее, изображение целиком (contain). */
.files-grid {
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
}

.files-tile__media img {
  object-fit: contain;
}

/* Одна карта — во всю ширину раздела по своим пропорциям, но не крупнее
   собственного разрешения (маленькая картинка не растягивается) и не выше 80vh. */
.files-grid:has(> .files-tile:only-child) {
  grid-template-columns: minmax(0, 1fr);
}

.files-grid:has(> .files-tile:only-child) .files-tile__media:has(> img) {
  aspect-ratio: auto;
  height: auto;
}

.files-grid:has(> .files-tile:only-child) .files-tile__media > img {
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 80vh;
  margin-inline: auto;
}
