/*
 * Print stylesheet for Al-Abdulgader Family Heritage Portal.
 * Optimized for A4 paper (210mm × 297mm), preserving Arabic typography.
 * Hides navigation/footer chrome, handles intelligent page breaks.
 */

@media print {
  /* ---- Page setup & reset ---- */
  * {
    box-shadow: none !important;
    text-shadow: none !important;
  }

  html {
    font-size: 12pt;
  }

  body {
    background: #fff !important;
    color: #000 !important;
    font-family: var(--font-primary);
    line-height: 1.5;
    margin: 0;
    padding: 0;
  }

  /* ---- Hide non-content elements ---- */
  .site-header,
  .site-footer,
  .primary-nav,
  .nav-toggle,
  .breadcrumb,
  .sidebar-first,
  .sidebar-second,
  [role="complementary"],
  .pager,
  .tabs,
  .action-links,
  .contextual-links,
  .messages,
  .skip-link,
  .search-form,
  .region-search-bar,
  .figure-profile__nav,
  .btn--secondary,
  .social-share,
  .back-link,
  [aria-label*="actions"],
  [aria-label*="Actions"],
  nav:not(.figure-profile__nav) {
    display: none !important;
  }

  /* ---- Layout & spacing ---- */
  .layout-container {
    max-inline-size: 100%;
    padding: 0;
    margin: 0;
  }

  .layout-main {
    padding: 0;
    margin: 0;
  }

  .layout-main--two-col {
    display: block;
  }

  article,
  .node {
    margin: 0;
    padding: 0;
  }

  /* ---- Typography ---- */
  h1 {
    font-size: 18pt;
    font-weight: 700;
    margin-block-end: 0.5em;
    color: #000;
    page-break-after: avoid;
    orphans: 3;
  }

  h2 {
    font-size: 14pt;
    font-weight: 700;
    margin-block-start: 1em;
    margin-block-end: 0.5em;
    color: #000;
    page-break-after: avoid;
    orphans: 3;
  }

  h3 {
    font-size: 12pt;
    font-weight: 700;
    margin-block-start: 0.75em;
    margin-block-end: 0.5em;
    color: #000;
    page-break-after: avoid;
  }

  p {
    margin-block-end: 0.75em;
    orphans: 3;
    widows: 3;
  }

  /* ---- Links ---- */
  a {
    color: #000;
    text-decoration: underline;
  }

  /* Show URLs after external links (except for images) */
  a[href^="http"]:not([href*="family.ddev.site"])::after {
    content: " (" attr(href) ")";
    font-size: 0.9em;
    color: #666;
    word-wrap: break-word;
  }

  a[href^="http"] img::after {
    content: none;
  }

  /* ---- Images & media ---- */
  img {
    max-inline-size: 100%;
    block-size: auto;
    page-break-inside: avoid;
    page-break-after: auto;
  }

  figure {
    margin: 1em 0;
    page-break-inside: avoid;
  }

  figcaption {
    font-size: 0.9em;
    color: #333;
    margin-block-start: 0.5em;
  }

  /* ---- Page breaks ---- */
  /* Avoid breaking inside these elements */
  blockquote,
  pre,
  table,
  .content-card,
  .alert,
  .figure-profile__header,
  .figure-profile__key-info,
  .field--name-field-poetry-text .field__item,
  .field--name-field-historical-account .field__item,
  .stanza {
    page-break-inside: avoid;
  }

  /* Always break before these */
  h1 {
    page-break-before: always;
  }

  h1:first-child,
  article > h1:first-of-type {
    page-break-before: avoid;
  }

  /* Avoid breaks after headings */
  h1, h2, h3, h4, h5, h6 {
    page-break-after: avoid;
  }

  /* ---- Content-specific styles ---- */

  /* Figure Profile / Biography header */
  .figure-profile__header,
  .detailed-biography__header {
    margin-block-end: 2em;
    padding-block-end: 1em;
    border-block-end: 2px solid #000;
  }

  .figure-profile__name,
  .detailed-biography__title {
    margin-block-end: 0.25em;
  }

  .figure-profile__categories .badge,
  .badge {
    display: inline-block;
    padding: 0.25em 0.5em;
    border: 1px solid #000;
    border-radius: 0;
    background: transparent;
    color: #000;
    font-size: 9pt;
    font-weight: 600;
    margin-inline-end: 0.5em;
  }

  /* Photos */
  .figure-profile__photos,
  .field--name-field-photos {
    margin-block-end: 2em;
    text-align: center;
  }

  .figure-profile__photos img,
  .field--name-field-photos img {
    max-inline-size: 60%;
    border: 1px solid #ddd;
  }

  /* Key info / Biography text */
  .figure-profile__key-info,
  .field--name-field-key-info,
  .field--name-field-biography-text {
    font-size: 11pt;
    line-height: 1.6;
  }

  /* Poetry & Literary works */
  .field--name-field-poetry-text {
    font-family: var(--font-primary);
    font-size: 11pt;
    line-height: 1.8;
    margin-block: 2em;
    padding-inline-start: 2em;
  }

  .field--name-field-poetry-text .field__item {
    margin-block-end: 1.5em;
    page-break-inside: avoid;
  }

  /* Stanza grouping (if marked up) */
  .stanza {
    margin-block-end: 1.5em;
    page-break-inside: avoid;
  }

  /* Historical testimony */
  .field--name-field-historical-account {
    font-size: 11pt;
    line-height: 1.6;
    border-inline-start: 3px solid #000;
    padding-inline-start: 1em;
    margin-block: 1.5em;
    page-break-inside: avoid;
  }

  /* Library items */
  .field--name-field-description {
    font-size: 11pt;
    line-height: 1.6;
    margin-block: 1em;
  }

  .field--name-field-author,
  .field--name-field-publication-year,
  .field--name-field-library-section {
    font-size: 10pt;
    margin-block-end: 0.5em;
  }

  .field--label {
    font-weight: 700;
    margin-inline-end: 0.5em;
  }

  .field--label::after {
    content: ":";
  }

  /* ---- Tables ---- */
  table {
    border-collapse: collapse;
    inline-size: 100%;
    margin-block: 1em;
    page-break-inside: avoid;
  }

  thead {
    display: table-header-group;
  }

  th,
  td {
    border: 1px solid #000;
    padding: 0.5em;
    text-align: start;
  }

  th {
    background: #f0f0f0;
    font-weight: 700;
  }

  /* ---- Lists ---- */
  ul, ol {
    margin-block: 0.75em;
    padding-inline-start: 2em;
  }

  li {
    margin-block-end: 0.5em;
  }

  /* ---- Blockquotes ---- */
  blockquote {
    margin: 1.5em 2em;
    padding-inline-start: 1em;
    border-inline-start: 3px solid #000;
    font-style: italic;
    page-break-inside: avoid;
  }

  /* ---- Code blocks ---- */
  pre,
  code {
    font-family: 'Courier New', monospace;
    font-size: 10pt;
    page-break-inside: avoid;
  }

  pre {
    border: 1px solid #000;
    padding: 0.5em;
    overflow: visible;
    white-space: pre-wrap;
    word-wrap: break-word;
  }

  /* ---- Buttons (keep primary, hide secondary) ---- */
  .btn--primary {
    display: none;
  }

  /* ---- Print-only elements ---- */
  .print-only {
    display: block !important;
  }

  /* ---- Page header (printed on every page) ---- */
  @page {
    size: A4;
    margin: 2cm 2cm 3cm 2cm;

    @top-left {
      content: "عائلة العبدالقادر | Al-Abdulgader Family";
      font-size: 9pt;
      color: #666;
    }

    @top-right {
      content: counter(page);
      font-size: 9pt;
      color: #666;
    }
  }

  /* First page (no header) */
  @page :first {
    margin-block-start: 2cm;

    @top-left {
      content: none;
    }

    @top-right {
      content: none;
    }
  }

  /* ---- RTL support (preserve for Arabic) ---- */
  [dir="rtl"],
  :lang(ar) {
    direction: rtl;
    text-align: start;
  }

  /* ---- Print QR code ---- */
  .print-qr-wrapper {
    display: block !important;
    text-align: center;
    margin-block-start: 1.5cm;
    padding-block-start: 0.5em;
    border-block-start: 1pt solid #ccc;
  }

  .print-qr-wrapper #print-qr-code,
  .print-qr-wrapper #print-qr-code canvas,
  .print-qr-wrapper #print-qr-code img {
    display: inline-block !important;
    inline-size: 100px;
    block-size: 100px;
  }

  .print-qr-label {
    font-size: 8pt;
    color: #555;
    margin-block-start: 0.25em;
  }

  /* ---- Print footer: digital source URL ---- */
  /* Drupal adds page-node-type-* classes to <body>; data-print-url is set  */
  /* via hook_preprocess_html() to the current canonical URL.               */
  body[data-print-url]::after {
    content: "المصدر الرقمي: " attr(data-print-url);
    display: block;
    margin-block-start: 2cm;
    padding-block-start: 0.5em;
    border-block-start: 1px solid #ccc;
    font-size: 9pt;
    color: #555;
    word-break: break-all;
  }

  /* ---- Utility: force page break ---- */
  .page-break-before {
    page-break-before: always;
  }

  .page-break-after {
    page-break-after: always;
  }

  .avoid-page-break {
    page-break-inside: avoid;
  }
}
