/* ══════════════════════════════════════
   인쇄 / PDF 내보내기 스타일
   - A4 세로 기본, 가로로 긴 다이어그램(.print-wide)은 A4 가로 페이지에 배치
   - 앱 UI(헤더·패널·버튼 등)는 모두 숨기고 문서 내용만 출력
   ══════════════════════════════════════ */

@media print {
  /* 모든 페이지 동일 방향 (A4 세로) — 방향 혼합 없음 */
  @page {
    size: A4;
    margin: 14mm 13mm 16mm;
  }

  /* 다크모드로 열려 있어도 지면은 항상 라이트 팔레트 */
  :root,
  :root.dark {
    --color-primary: #1d4ed8;
    --color-primary-hover: #1d4ed8;
    --color-background: #ffffff;
    --color-surface: #ffffff;
    --color-surface-hover: #f3f4f6;
    --color-text: #111827;
    --color-text-secondary: #4b5563;
    --color-border: #d1d5db;
  }

  html,
  body {
    background: #ffffff !important;
    color: #111827;
  }

  /* ── 앱 UI 숨김 ── */
  .header,
  .filter-bar,
  .theme-panel,
  .shape-panel,
  .theme-overlay,
  .toast-container,
  .fullscreen-modal,
  .drop-zone-wrapper,
  .editor-pane,
  .spinner-overlay,
  .empty-state,
  .diagram-card__actions,
  .diagram-card__editor,
  .diagram-card__badge,
  .fc-hint,
  .fc-popup {
    display: none !important;
  }

  /* ── 레이아웃: 스크롤 컨테이너 해제 (없으면 첫 페이지만 인쇄됨) ── */
  .workspace {
    margin: 0 !important;
    height: auto !important;
    overflow: visible !important;
    display: block !important;
  }

  .workspace:not(.visible) {
    display: none !important;
  }

  .content-pane {
    overflow: visible !important;
    height: auto !important;
  }

  .md-doc {
    max-width: none;
    padding: 0;
  }

  /* 템플릿 레터헤드: 첫 페이지 상단에 인쇄 */
  .tpl-letterhead {
    break-inside: avoid;
    page-break-inside: avoid;
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
  }

  .tpl-letterhead__logo {
    max-height: 16mm;
  }

  .diagram-container.visible {
    display: block !important;
    padding: 0;
  }

  .diagram-container .diagram-card {
    margin-bottom: 8mm;
  }

  /* ── 본문 타이포그래피 ── */
  .md-content {
    font-family: 'Noto Sans KR', 'Malgun Gothic', 'Apple SD Gothic Neo', -apple-system, 'Segoe UI', sans-serif;
    font-size: 10.5pt;
    line-height: 1.7;
    color: #111827;
  }

  .md-content h1 { font-size: 19pt; }
  .md-content h2 { font-size: 15pt; }
  .md-content h3 { font-size: 12.5pt; }
  .md-content h4 { font-size: 11pt; }

  .md-content h1,
  .md-content h2,
  .md-content h3,
  .md-content h4 {
    break-after: avoid-page;
    page-break-after: avoid;
  }

  .md-content a {
    color: #1d4ed8;
    text-decoration: none;
  }

  .md-content pre {
    background: #f8fafc !important;
    border: 0.75pt solid #e2e8f0;
    box-shadow: none;
    break-inside: avoid;
    page-break-inside: avoid;
    font-size: 8.5pt;
  }

  .md-content code {
    background: #f3f4f6;
    border-color: #e5e7eb;
  }

  .md-content table {
    break-inside: avoid;
    page-break-inside: avoid;
    box-shadow: none;
    font-size: 9.5pt;
  }

  .md-content blockquote {
    break-inside: avoid;
    background: #f8fafc;
  }

  .md-content img {
    box-shadow: none;
  }

  /* ── 다이어그램 카드 ── */
  .diagram-card {
    break-inside: avoid;
    page-break-inside: avoid;
    box-shadow: none !important;
    border: 0.75pt solid #d1d5db;
    border-radius: 8px;
    background: #ffffff !important;
  }

  /* 등장 애니메이션 해제 (transform은 회전 배치에서 사용하므로 건드리지 않음) */
  .diagram-card--enter {
    animation: none !important;
    opacity: 1 !important;
  }

  .diagram-card__header {
    background: none;
    border-bottom: 0.75pt solid #e5e7eb;
    padding: 5pt 10pt;
  }

  .diagram-card__title {
    font-size: 8pt;
    font-weight: 600;
    color: #6b7280;
  }

  .diagram-card__body {
    padding: 8pt;
    overflow: visible !important;
  }

  /* SVG 색상을 지면에 그대로 출력 */
  .diagram-card__body,
  .diagram-card__body svg,
  .fc-legend {
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
  }

  .md-doc__diagram {
    margin: 5mm 0;
  }

  .md-doc__diagram .diagram-card__body svg {
    max-height: 205mm; /* 세로 페이지 내용 높이 이내 */
    width: 100%;
    height: auto;
  }

  /* 범례는 컴팩트하게 유지 */
  .fc-legend {
    border-top: 0.75pt solid #e5e7eb;
    padding: 4pt 10pt 6pt;
  }

  .fc-legend__title { font-size: 6.5pt; }
  .fc-legend__item { font-size: 7.5pt; }

  /* ── 가로로 긴 다이어그램 → 세로 페이지 안에서 90° 회전 배치 ──
     용지 방향은 모든 페이지 동일(세로)하게 유지하면서,
     넓은 차트가 작게 축소되지 않도록 전용 페이지에 회전시켜 크게 싣습니다. */
  .md-doc__diagram.print-wide {
    break-before: page;
    break-after: page;
    break-inside: avoid;
    page-break-inside: avoid;
    height: 252mm;      /* 본문 영역 높이만큼 한 페이지 점유 */
    position: relative;
    overflow: visible;
    margin: 0;
  }

  .md-doc__diagram.print-wide .diagram-card {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 245mm;       /* 회전 후 세로 방향 길이 */
    transform: translate(-50%, -50%) rotate(90deg) !important;
    border: 0.75pt solid #e5e7eb;
  }

  .md-doc__diagram.print-wide .diagram-card__body svg {
    width: 100%;
    height: auto;
    max-height: 158mm;  /* 회전 후 가로 방향(지면 폭) 한계 */
  }

  /* 전체 가로 모드(export_pdf.py --landscape): 회전 없이 본문 폭에 맞춤 */
  .pdf-landscape .md-doc__diagram.print-wide {
    height: auto;
    break-before: auto;
    break-after: auto;
    margin: 5mm 0;
  }

  .pdf-landscape .md-doc__diagram.print-wide .diagram-card {
    position: static;
    width: auto;
    transform: none !important;
  }

  .pdf-landscape .md-doc__diagram.print-wide .diagram-card__body svg {
    max-height: 165mm;
  }
}
