/* 첨부 인앱 미리보기 모달 */
#attachPreviewOverlay {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, .55);
  z-index: 2000;
  align-items: center; justify-content: center;
  padding: 24px;
  /* 가로 휠이 크롬 뒤로/앞으로(스와이프 내비)로 새는 것 차단 — 모달 안 스크롤은 정상 */
  overscroll-behavior: contain;
}
#attachPreviewOverlay.show { display: flex; }

#attachPreviewOverlay .apv-modal {
  background: #fff;
  border-radius: 12px;
  width: 100%; max-width: 1100px;
  height: 100%; max-height: calc(100vh - 48px);
  display: flex; flex-direction: column;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
}
#attachPreviewOverlay .apv-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 12px 18px;
  border-bottom: 1px solid #e5e7eb; background: #f9fafb;
}
#attachPreviewOverlay .apv-head h3 {
  font-size: 14px; margin: 0; color: #1f2937; font-weight: 600;
  flex: 1; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
#attachPreviewOverlay .apv-actions {
  display: flex; gap: 8px; align-items: center; flex-shrink: 0;
}
#attachPreviewOverlay .apv-btn {
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 12px; font-weight: 600;
  cursor: pointer;
  border: 1px solid transparent;
  text-decoration: none;
  display: inline-block;
  font-family: inherit;
}
#attachPreviewOverlay .apv-btn-ghost {
  background: #fff; color: #00583F; border-color: #00583F;
}
#attachPreviewOverlay .apv-btn-ghost:hover { background: #E6F1ED; }
#attachPreviewOverlay .apv-btn-close {
  background: transparent; color: #6b7280;
  font-size: 22px; line-height: 1;
  border: 1px solid transparent;
  padding: 2px 8px; cursor: pointer;
  font-family: inherit;
}
#attachPreviewOverlay .apv-btn-close:hover { color: #1f2937; }

#attachPreviewOverlay .apv-body {
  flex: 1; min-height: 0;
  background: #374151;
  display: flex; align-items: stretch; justify-content: center;
  overflow: hidden;
  overscroll-behavior: contain;
}
#attachPreviewOverlay .apv-iframe {
  width: 100%; height: 100%;
  border: 0; background: #fff;
}
#attachPreviewOverlay .apv-image {
  max-width: 100%; max-height: 100%;
  margin: auto; display: block; object-fit: contain;
}
#attachPreviewOverlay .apv-status {
  padding: 60px 24px; text-align: center;
  color: #d1d5db; font-size: 14px; margin: auto;
}
#attachPreviewOverlay .apv-status-error { color: #fecaca; }

/* PDF 모바일 인앱 렌더 (pdf.js) — 모달 안 캔버스 페이지를 세로 스크롤.
   설치형 PWA(standalone)에서 새 탭이 앱 밖으로 튀던 문제 해결용. */
#attachPreviewOverlay .apv-pdf-scroll {
  position: relative; /* slot.offsetTop 이 이 컨테이너 기준이 되도록 (가상 스크롤 계산) */
  width: 100%; height: 100%;
  overflow-y: auto; overflow-x: hidden;
  background: #374151;
  -webkit-overflow-scrolling: touch;
  padding: 12px 0 28px;
  text-align: center;
  overscroll-behavior: contain;
}
/* 페이지 슬롯(빈 div) — 실제 페이지 높이로 미리 잡아 스크롤바를 정확히 유지.
   화면 근처일 때만 안에 <canvas> 가 들어와 채운다(가상 스크롤). */
#attachPreviewOverlay .apv-pdf-page {
  display: block;
  margin: 0 auto 12px;
  max-width: calc(100% - 16px);
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .4);
}
#attachPreviewOverlay .apv-pdf-page canvas {
  display: block;
  width: 100%; height: 100%;
}
#attachPreviewOverlay .apv-pdf-status {
  color: #d1d5db; font-size: 13px;
  padding: 14px; text-align: center; line-height: 1.7;
}

#attachPreviewOverlay .apv-foot {
  padding: 8px 18px;
  border-top: 1px solid #e5e7eb;
  font-size: 11px; color: #6b7280;
  background: #f9fafb;
}

/* 첨부 리스트의 미리보기/다운로드 버튼 (공용) */
.attach-actions {
  display: inline-flex; gap: 4px; margin-left: 8px;
  vertical-align: middle;
}
.attach-actions button,
.attach-actions a {
  padding: 3px 8px;
  font-size: 11px;
  border: 1px solid #d1d5db;
  background: #fff;
  color: #374151;
  border-radius: 4px;
  cursor: pointer;
  text-decoration: none;
  font-family: inherit;
  line-height: 1.4;
}
.attach-actions button:hover,
.attach-actions a:hover {
  background: #f3f4f6;
  border-color: #00583F;
  color: #00583F;
}

@media (max-width: 640px) {
  #attachPreviewOverlay { padding: 0; }
  #attachPreviewOverlay .apv-modal {
    max-width: 100%; max-height: 100vh; border-radius: 0;
  }
  #attachPreviewOverlay .apv-head h3 { font-size: 13px; }
}

/* DICOM 미리보기 (cornerstone) — 좌 메타데이터 + 우 뷰어 + 하단 도움말 */
#attachPreviewOverlay .apv-dicom-wrap {
  display: grid;
  grid-template-columns: 280px 1fr;
  grid-template-rows: 1fr auto;
  width: 100%; height: 100%;
  background: #111;
}
#attachPreviewOverlay .apv-dicom-meta {
  grid-row: 1; grid-column: 1;
  background: #1f2937; color: #e5e7eb;
  padding: 14px 16px;
  overflow-y: auto;
  font-size: 12px;
  border-right: 1px solid #374151;
}
#attachPreviewOverlay .apv-dicom-viewer {
  grid-row: 1; grid-column: 2;
  position: relative;
  background: #000;
  overflow: hidden;
  cursor: crosshair;
  /* cornerstone 이 안에 canvas 동적 생성 */
}
#attachPreviewOverlay .apv-dicom-viewer:active { cursor: grabbing; }
#attachPreviewOverlay .apv-dicom-hint {
  grid-row: 2; grid-column: 1 / -1;
  background: #1f2937; color: #9ca3af;
  padding: 8px 16px;
  font-size: 11px;
  border-top: 1px solid #374151;
}
#attachPreviewOverlay .apv-dcm-loading {
  color: #9ca3af; font-style: italic; text-align: center; padding: 30px 0;
}
#attachPreviewOverlay .apv-dcm-meta-table {
  width: 100%; border-collapse: collapse;
}
#attachPreviewOverlay .apv-dcm-meta-table th {
  text-align: left; padding: 4px 8px 4px 0;
  color: #9ca3af; font-weight: 500; font-size: 11px;
  border: 0; background: transparent;
  width: 90px; vertical-align: top;
}
#attachPreviewOverlay .apv-dcm-meta-table td {
  padding: 4px 0;
  color: #e5e7eb; font-size: 12px;
  border: 0; word-break: break-all;
}
#attachPreviewOverlay .apv-dcm-hud {
  margin-top: 14px; padding: 8px 10px;
  background: #111827; border-radius: 4px;
  font-family: 'Consolas', 'Courier New', monospace;
  font-size: 11px; color: #34d399;
  border: 1px solid #374151;
}

/* 진행 패널 — 다운로드/디코딩 진척 표시 */
#attachPreviewOverlay .apv-dcm-progress { padding: 4px 0; }
#attachPreviewOverlay .apv-dcm-step {
  font-size: 13px; color: #e5e7eb;
  font-weight: 600; margin-bottom: 10px;
  word-break: break-all; line-height: 1.5;
}
#attachPreviewOverlay .apv-dcm-bar {
  height: 6px; background: #111827;
  border-radius: 3px; overflow: hidden;
  margin-bottom: 14px;
}
#attachPreviewOverlay .apv-dcm-bar-fill {
  height: 100%; width: 0%;
  background: linear-gradient(90deg, #34d399, #10b981);
  transition: width .3s ease;
}
#attachPreviewOverlay .apv-dcm-log {
  font-family: 'Consolas', 'Courier New', monospace;
  font-size: 11px; color: #9ca3af;
  background: #111827; padding: 8px 10px;
  border-radius: 4px; border: 1px solid #374151;
  max-height: 220px; overflow-y: auto;
  line-height: 1.5;
}
#attachPreviewOverlay .apv-dcm-log div + div { margin-top: 2px; }

/* 에러 안내 패널 (실패 시 메타 패널 교체) */
#attachPreviewOverlay .apv-dcm-error { padding: 4px 0; }
#attachPreviewOverlay .apv-dcm-error-title {
  font-size: 14px; font-weight: 700;
  color: #fca5a5; margin-bottom: 10px;
}
#attachPreviewOverlay .apv-dcm-error-msg {
  font-family: 'Consolas', 'Courier New', monospace;
  font-size: 12px; color: #fecaca;
  background: #7f1d1d; padding: 8px 10px;
  border-radius: 4px; word-break: break-all;
  margin-bottom: 14px; line-height: 1.5;
}
#attachPreviewOverlay .apv-dcm-error-hint {
  font-size: 11px; color: #d1d5db;
  background: #111827; padding: 10px 12px;
  border-radius: 4px; line-height: 1.7;
  border: 1px solid #374151;
}

@media (max-width: 900px) {
  /* 모바일 DICOM — 이미지 우선 (viewer 상단 1fr) + 메타 하단으로 이동 (22vh, 내부 스크롤).
     본 의도는 진료 영상 확인이므로 메타 표를 가리지 않게 viewer 가 화면 70% 차지 */
  #attachPreviewOverlay .apv-dicom-wrap {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr auto auto;
  }
  #attachPreviewOverlay .apv-dicom-viewer { grid-row: 1; grid-column: 1; }
  #attachPreviewOverlay .apv-dicom-meta {
    grid-row: 2; grid-column: 1;
    border-right: 0; border-bottom: 0;
    border-top: 1px solid #374151;
    max-height: 22vh;
    padding: 10px 14px;
  }
  #attachPreviewOverlay .apv-dicom-hint { grid-row: 3; }
}
