/* ==========================================================
   kntu.kr.ua — стилі друку сторінки новини (v2)
   Підключення (вже є в <head>):
   <link rel="stylesheet" href="/assets/css/default/kntu-print.css" media="print">

   Примітка: bootstrap, style.css, justifiedGallery.css підключені з
   media="screen, projection", тому під час друку вони НЕ діють.
   Але inline-стилі, які виставляє justifiedGallery (JS), діють —
   саме вони й накладали фото на текст. Нижче вони перебиваються.
   ========================================================== */

@page {
  size: A4;
  margin: 18mm 15mm 20mm;
}

@media print {

  /* ---------- База ---------- */
  html, body {
    background: #fff !important;
    color: #000 !important;
    width: auto !important;
    min-width: 0 !important;
    height: auto !important;
  }

  body {
    display: block !important;
    margin: 0 !important;
    padding: 0 !important;
    font-family: "Open Sans", Helvetica, Arial, sans-serif;
    font-size: 11.5pt;
    line-height: 1.5;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  *, *::before, *::after {
    box-shadow: none !important;
    text-shadow: none !important;
    animation: none !important;
    transition: none !important;
  }

  /* ---------- Приховуємо все зайве ---------- */
  header,                         /* верхні посилання, логотип, меню, пошук, мови */
  footer,                         /* меню внизу, контакти, соцмережі, кнопки */
  nav,
  aside,
  form,
  button,
  input,
  select,
  textarea,
  iframe,
  video,
  audio,
  script,
  noscript,
  .top_menu,
  .breadcrumbs-wide,              /* хлібні крихти */
  .news-page .row > article ~ *,  /* права колонка «Актуальні новини» */
  .news-page .col-lg-4,
  .lang-select,
  .btn,
  .fa, .fas, .far, .fab,          /* іконки Font Awesome (календар тощо) */
  .jg-spinner,
  .jg-caption,
  #swipebox-overlay,              /* оверлеї галерей, що додаються скриптами */
  #lightbox,
  #lightboxOverlay,
  .lightbox,
  .lightboxOverlay,
  [role="navigation"],
  [role="banner"],
  [role="contentinfo"],
  [role="search"],
  .no-print {
    display: none !important;
  }

  /* ---------- Шапка для друку: логотип + назва сайту ---------- */
  body::before {
    content: "Центральноукраїнський національний технічний університет\A kntu.kr.ua";
    white-space: pre-line;
    display: block;
    min-height: 16mm;
    margin: 0 0 8mm;
    padding: 2mm 0 4mm 22mm;
    border-bottom: 1.5pt solid #000;
    background: url("/assets/images/cntu-logo.webp") no-repeat left center;
    background-size: auto 16mm;
    font-size: 12pt;
    font-weight: 700;
    line-height: 1.35;
  }

  /* ---------- Підвал для друку: джерело ---------- */
  body::after {
    content: "Джерело: kntu.kr.ua · © ЦНТУ";
    display: block;
    clear: both;
    margin-top: 10mm;
    padding-top: 3mm;
    border-top: 0.75pt solid #000;
    font-size: 9.5pt;
    color: #333;
  }
  /* Якщо JS проставив адресу сторінки — показуємо її:
     <script>document.body.dataset.printUrl = location.href;</script> */
  body[data-print-url]::after {
    content: "Джерело: " attr(data-print-url) "  ·  © ЦНТУ";
  }

  /* ---------- Контейнери новини (bootstrap у друці не працює) ---------- */
  .news-page,
  .news-page .container,
  .news-page .row,
  .news-page article {
    display: block;
    width: auto !important;
    max-width: none !important;
    margin: 0 !important;
    padding: 0 !important;
    float: none !important;
  }

  h1 {
    margin: 0 0 4mm;
    font-family: "Merriweather", "Times New Roman", Times, serif;
    font-size: 20pt;
    font-weight: 700;
    line-height: 1.25;
    color: #000;
    break-after: avoid;
  }

  hr.divider {
    border: 0;
    border-top: 0.75pt solid #000;
    margin: 0 0 5mm;
    width: 30mm;
    height: 0;
  }

  /* ---------- Верх новини: текст (дата + анонс) зліва, фото справа ---------- */
  .news-page article > .row.mb-3 {
    display: flex !important;
    flex-direction: row-reverse;      /* у DOM фото йде першим — виносимо вправо */
    align-items: flex-start;
    gap: 6mm;
    margin: 0 0 6mm !important;
    break-inside: avoid;
  }
  .news-page article > .row.mb-3 > .col-lg-5 {
    flex: 0 0 38%;
    margin: 0 !important;
  }
  .news-page article > .row.mb-3 > .col-lg-7 {
    flex: 1 1 auto;
    margin: 0 !important;
  }

  .news-date {
    margin: 0 0 3mm;
    font-size: 10pt;
    font-weight: 600;
    color: #333;
  }

  .news-brief {
    margin: 0 !important;
    font-style: italic;
  }

  /* Головне фото новини */
  .news-page article > .row.mb-3 img {
    display: block;
    max-width: 100% !important;
    max-height: 90mm;
    width: auto !important;
    height: auto !important;
    margin: 0 auto;
    object-fit: contain;
  }

  /* ---------- Текст ---------- */
  h2, h3, h4, h5, h6 {
    font-family: "Merriweather", "Times New Roman", Times, serif;
    color: #000;
    break-after: avoid;
  }

  p, li {
    orphans: 3;
    widows: 3;
  }

  p {
    margin: 0 0 3.5mm;
  }

  ul, ol {
    margin: 0 0 4mm;
    padding-left: 7mm;
  }

  li {
    break-inside: avoid;
  }

  table {
    width: 100% !important;
    border-collapse: collapse;
  }
  th, td {
    border: 0.5pt solid #000;
    padding: 1.5mm 2mm;
  }
  thead { display: table-header-group; }
  tr { break-inside: avoid; }

  /* Зображення всередині тексту (крім галереї) */
  .news-page article > p img,
  .news-page article > div:not(.row) img {
    display: block;
    max-width: 100% !important;
    max-height: 120mm;
    width: auto !important;
    height: auto !important;
    margin: 3mm auto;
    break-inside: avoid;
  }

  /* ==========================================================
     ГАЛЕРЕЯ ФОТО В КІНЦІ НОВИНИ
     justifiedGallery виставляє inline height/width/top/left та
     position:absolute — у друці це накладає фото на текст.
     Робимо звичайну сітку в потоці, окремо від тексту.
     ========================================================== */
  #gallery,
  .justified-gallery {
    position: static !important;
    display: grid !important;
    grid-template-columns: repeat(3, 1fr);
    gap: 3mm;
    clear: both;
    float: none !important;
    width: 100% !important;
    height: auto !important;          /* перебиваємо inline height від JS */
    min-height: 0 !important;
    overflow: visible !important;
    margin: 10mm 0 0 !important;
    padding: 6mm 0 0 !important;
    border-top: 0.75pt solid #000;
    break-before: auto;
  }

  /* Підпис блоку */
  #gallery::before,
  .justified-gallery::before {
    content: "Фотографії";
    grid-column: 1 / -1;
    font-family: "Merriweather", "Times New Roman", Times, serif;
    font-size: 13pt;
    font-weight: 700;
    break-after: avoid;
  }

  #gallery > a,
  #gallery > div,
  #gallery > figure,
  .justified-gallery > a,
  .justified-gallery > div,
  .justified-gallery > figure {
    position: static !important;
    display: block !important;
    width: auto !important;           /* перебиваємо inline width/height/top/left */
    height: auto !important;
    top: auto !important;
    left: auto !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: hidden !important;
    opacity: 1 !important;
    filter: none !important;
    background: none !important;
    break-inside: avoid;
  }

  #gallery > a > img,
  #gallery > div > img,
  #gallery > figure > img,
  .justified-gallery > a > img,
  .justified-gallery > div > img,
  .justified-gallery > figure > img {
    position: static !important;
    display: block !important;
    width: 100% !important;
    height: 45mm !important;
    max-width: 100% !important;
    max-height: none !important;
    margin: 0 !important;             /* перебиваємо від'ємні margin-top/left від JS */
    padding: 0 !important;
    transform: none !important;
    opacity: 1 !important;
    filter: none !important;
    object-fit: cover;
    border: 0.5pt solid #999 !important;
  }

  /* ---------- Посилання ---------- */
  a, a:visited {
    color: #000 !important;
    text-decoration: underline;
  }

  /* Адреса зовнішніх посилань у тексті (напр. Google Drive).
     Посилання-обгортки навколо зображень пропускаємо. */
  .news-page article a[href^="http"]:not(:has(img))::after {
    content: " (" attr(href) ")";
    font-size: 9pt;
    font-weight: 400;
    word-break: break-all;
    overflow-wrap: anywhere;
  }

  a, p, li {
    overflow-wrap: anywhere;
  }
}
