/**
 *  Print Stylesheet - GC Vizit Ultimate
 *  Professional Hospital Document Style
 *  Philosophy: Ink-Saving, Clean, Compact, Professional
 *  Version: 4.0 - Long Content & Smart Breaking Support
 *  
 *  CHANGELOG v4.0:
 *  - FIXED: Blank first page issue - Removed break-inside:avoid from .patient-card
 *  - FIXED: Narrow width issue - All sections now use 100% width
 *  - ADDED: Smart page breaking - Headers try to stay with content
 *  - ADDED: Orphan/widow control (3 lines minimum) on text blocks
 *  - IMPROVED: Long reports (2-3 pages) now flow naturally across pages
 *  - IMPROVED: Full A4 width utilization for clinical notes and reports
 */

@media print {
  /* === PAGE SETUP === */
  @page {
    margin: 1.5cm 1cm;
    size: A4 portrait;
  }

  /* === COLOR VARIABLES OVERRIDE === */
  :root {
    --primary: #000;
    --accent: #000;
    --text-main: #000;
    --text-light: #333;
    --border-color: #ccc;
  }

  /* === GLOBAL RESETS === */
  * {
    box-shadow: none !important;
    text-shadow: none !important;
    background: white !important;
    border-radius: 0 !important;
    animation: none !important;
    transition: none !important;
  }

  body {
    background: white !important;
    padding: 0 !important;
    margin: 0 !important;
    color: #000 !important;
    font-size: 10pt !important;
    line-height: 1.3 !important;
    font-family: "Inter", "Segoe UI", Arial, sans-serif !important;
    /* ENSURE NO CLIPPING */
    overflow: visible !important;
    height: auto !important;
  }

  /* === HIDE NON-PRINTABLE ELEMENTS === */
  .no-print,
  .no-print-card,
  .btn,
  .btn-quick-action,
  .btn-toolbar,
  .filter-bar,
  .modal-overlay,
  .editor-toolbar,
  .floating-editor-toolbar,
  .date-settings-wrapper,
  .header-actions,
  .header-actions-right,
  .location-selector,
  .location-selector-compact,
  .action-toolbar,
  .main-header,
  .trend-delete-col,
  .trend-delete-row,
  .trend-actions,
  .btn-trend-action,
  .report-delete-btn,
  .date-trigger,
  .date-popup,
  .card-collapse-indicator {
    display: none !important;
    visibility: hidden !important;
  }

  /* === PRINT DOCUMENT HEADER === */
  .print-document-header {
    display: block !important;
    text-align: center !important;
    margin-bottom: 20px !important;
    padding-bottom: 15px !important;
    border-bottom: 2px solid #000 !important;
  }

  body[data-print-compact="true"] .print-document-header {
    margin-bottom: 15px !important;
    padding-bottom: 10px !important;
    border-bottom: 1.5px solid #000 !important;
  }

  .print-doc-title {
    font-size: 14pt !important;
    font-weight: 700 !important;
    margin: 0 0 5px 0 !important;
    color: #000 !important;
    text-transform: uppercase !important;
    letter-spacing: 0.5px !important;
  }

  .print-doc-date {
    font-size: 9pt !important;
    font-weight: 500 !important;
    color: #000 !important;
  }

  /* === CONTAINER === */
  .container {
    max-width: 100% !important;
    width: 100% !important;
    margin: 0 !important;
    padding: 0 !important;
    /* ENSURE NO CLIPPING */
    overflow: visible !important;
    height: auto !important;
  }

  /* === PATIENT CARD === */
  .patient-card {
    border: none !important;
    border-radius: 0 !important;
    margin: 0 0 25px 0 !important;
    padding: 0 0 15px 0 !important;
    background: white !important;
    width: 100% !important;
    max-width: none !important;
    box-shadow: none !important;
    /* ALLOW BREAKING: Long reports can span multiple pages */
    break-inside: auto !important;
    page-break-inside: auto !important;
  }

  /* Simple separation line between patients */
  .patient-card::after {
    content: "" !important;
    display: block !important;
    height: 0 !important;
    border-bottom: 2px solid #000 !important;
    margin-top: 15px !important;
  }

  /* FORCE EXPAND: Always show card body regardless of collapsed state */
  .patient-card .patient-card-body,
  .patient-card.collapsed .patient-card-body {
    max-height: none !important;
    opacity: 1 !important;
    overflow: visible !important;
    display: block !important;
    height: auto !important;
  }

  .patient-card.is-preop {
    border: none !important;
    box-shadow: none !important;
  }

  .patient-card.is-preop::after {
    border-bottom: 2px solid #000 !important;
  }

  .patient-card:hover {
    transform: none !important;
  }

  /* CRITICAL: Force all card bodies to be visible in print */
  .patient-card .patient-card-body,
  .patient-card.collapsed .patient-card-body {
    max-height: none !important;
    opacity: 1 !important;
    overflow: visible !important;
    display: block !important;
    height: auto !important;
  }

  /* Compact mode - still visible separation */
  body[data-print-compact="true"] .patient-card {
    margin-bottom: 18px !important;
    padding: 0 0 12px 0 !important;
    border: none !important;
  }

  body[data-print-compact="true"] .patient-card::after {
    border-bottom: 1.5px solid #000 !important;
    margin-top: 12px !important;
  }

  /* === CARD HEADER (Patient Info) === */
  .card-header {
    padding: 0 !important;
    margin: 0 0 6px 0 !important;
    border-bottom: none !important;
    background: white !important;
    width: 100% !important;
    /* TRY to keep header with content, but allow breaking if needed */
    break-after: avoid !important;
    page-break-after: avoid !important;
    break-inside: avoid !important;
    page-break-inside: avoid !important;
  }

  body[data-print-compact="true"] .card-header {
    margin: 0 0 4px 0 !important;
  }

  .header-top-row,
  .header-second-row {
    display: block !important;
    gap: 0 !important;
  }

  .patient-name {
    font-size: 12pt !important;
    font-weight: 700 !important;
    color: #000 !important;
    margin: 0 0 2px 0 !important;
    line-height: 1.2 !important;
    display: inline !important;
  }

  body[data-print-compact="true"] .patient-name {
    font-size: 11pt !important;
  }

  /* Show patient meta info in print */
  .patient-meta-info {
    display: block !important;
    margin-top: 2px !important;
    font-size: 8pt !important;
    color: #000 !important;
    background: transparent !important;
    padding: 0 !important;
    line-height: 1.4 !important;
  }

  .meta-info-item {
    display: inline !important;
    background: transparent !important;
    border-radius: 0 !important;
    padding: 0 !important;
  }

  /* Remove screen-only styling */
  .meta-info-item::before {
    content: "" !important;
    display: none !important;
  }

  .meta-info-item:not(:first-child) {
    padding-left: 0 !important;
  }

  /* Add pipe separator between items */
  .meta-info-item:not(:first-child)::after {
    content: "" !important;
  }

  .meta-info-item:not(:last-child)::after {
    content: " | " !important;
    color: #999 !important;
    font-weight: 400 !important;
  }

  .meta-info-icon {
    display: none !important;
  }

  /* Add label prefix and format text */
  .meta-info-item[data-info="location"]::before {
    content: "Yer: " !important;
    display: inline !important;
    font-weight: 600 !important;
    color: #000 !important;
  }

  .meta-info-item[data-info="room"]::before {
    content: "Oda: " !important;
    display: inline !important;
    font-weight: 600 !important;
    color: #000 !important;
  }

  .meta-info-item[data-info="doctor"]::before {
    content: "Dr: " !important;
    display: inline !important;
    font-weight: 600 !important;
    color: #000 !important;
  }

  .meta-info-text {
    color: #000 !important;
    font-weight: 500 !important;
    overflow: visible !important;
    text-overflow: clip !important;
    white-space: normal !important;
    max-width: none !important;
    display: inline !important;
  }

  /* Hide empty meta items */
  .meta-info-text:empty {
    display: none !important;
  }

  .meta-info-item.meta-info-empty,
  .meta-info-item[data-info="room"]:has(.meta-info-text:empty),
  .meta-info-item[data-info="doctor"]:has(.meta-info-text:empty) {
    display: none !important;
  }

  /* Hide separator after empty items */
  .meta-info-item.meta-info-empty::after {
    display: none !important;
  }

  body[data-print-compact="true"] .patient-meta-info {
    font-size: 7.5pt !important;
    margin-top: 1px !important;
  }

  .diagnosis {
    font-size: 10pt !important;
    color: #333 !important;
    font-style: italic !important;
    margin: 2px 0 4px 0 !important;
    line-height: 1.2 !important;
    display: block !important;
  }

  body[data-print-compact="true"] .diagnosis {
    font-size: 9pt !important;
    margin: 1px 0 3px 0 !important;
  }

  /* === META BADGES (Dates) === */
  .meta-row {
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 4px !important;
    margin: 0 !important;
  }

  .meta-badge-container {
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 4px !important;
  }

  .meta-badge {
    font-size: 8pt !important;
    font-weight: 600 !important;
    color: #000 !important;
    background: white !important;
    padding: 0 6px 0 0 !important;
    border: none !important;
    white-space: nowrap !important;
  }

  .meta-badge::after {
    content: " |" !important;
    color: #999 !important;
    font-weight: 400 !important;
  }

  .meta-badge:last-child::after {
    content: "" !important;
  }

  body[data-print-compact="true"] .meta-badge {
    font-size: 7.5pt !important;
    padding: 0 5px 0 0 !important;
  }

  /* === TASK BAR === */
  .task-bar {
    padding: 0 !important;
    margin: 4px 0 8px 0 !important;
    border-bottom: none !important;
    background: white !important;
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 6px !important;
  }

  .task-item {
    border: none !important;
    color: #000 !important;
    background: white !important;
    padding: 0 !important;
    font-size: 9pt !important;
    font-weight: 500 !important;
    display: inline-flex !important;
    align-items: center !important;
    gap: 3px !important;
  }

  .task-item::before {
    content: "• " !important;
    font-weight: 700 !important;
  }

  .task-item.task-done,
  .task-item.status-2 {
    text-decoration: line-through !important;
    color: #666 !important;
  }

  .task-item i {
    display: none !important;
  }

  /* === TREND / LAB SECTION === */
  /* Force visibility regardless of toggle state */
  details,
  details.trend-details,
  details[open],
  .trend-details {
    display: block !important;
    visibility: visible !important;
    border: none !important;
    margin: 0 0 6px 0 !important;
    padding: 0 !important;
    background: white !important;
    /* FULL WIDTH */
    width: 100% !important;
    max-width: none !important;
  }

  /* Hide summary/toggle */
  details > summary,
  summary,
  .trend-summary {
    display: none !important;
  }

  /* Force content visibility */
  details > .trend-content,
  .trend-content {
    display: block !important;
    visibility: visible !important;
    opacity: 1 !important;
    height: auto !important;
    max-height: none !important;
    overflow: visible !important;
    padding: 0 !important;
    margin: 0 !important;
    /* FULL WIDTH */
    width: 100% !important;
    max-width: none !important;
  }

  /* Trend table styling */
  .trend-table {
    display: table !important;
    visibility: visible !important;
    width: 100% !important;
    table-layout: fixed !important;
    border-collapse: collapse !important;
    margin: 4px 0 !important;
    font-size: 9pt !important;
    background: white !important;
  }

  .trend-table thead,
  .trend-table tbody,
  .trend-table tr {
    display: table-row-group !important;
  }

  .trend-table tr {
    display: table-row !important;
  }

  .trend-table th,
  .trend-table td {
    display: table-cell !important;
    border: 0.5px solid #ccc !important;
    padding: 3px 6px !important;
    font-size: 9pt !important;
    line-height: 1.3 !important;
    white-space: normal !important;
    word-wrap: break-word !important;
    vertical-align: middle !important;
    background: white !important;
  }

  .trend-table th {
    font-weight: 700 !important;
    text-align: left !important;
    background: white !important;
    border-bottom: 1px solid #000 !important;
  }

  /* Hide older columns (keep last 3) - respects lab limit setting */
  body[data-print-lab-limit="true"] .no-print-col {
    display: none !important;
  }

  /* Show all columns if lab limit is off */
  body[data-print-lab-limit="false"] .no-print-col {
    display: table-cell !important;
  }

  /* Highlight abnormal values */
  .val-high {
    text-decoration: underline !important;
    font-weight: 700 !important;
    color: #000 !important;
  }

  /* Simplify header cells */
  .trend-header-cell,
  .trend-param-name-cell {
    display: block !important;
  }

  /* === CLINICAL NOTES === */
  /* Clinical Notes Container */
  details {
    display: block !important;
    break-inside: avoid !important;
    page-break-inside: avoid !important;
    margin-bottom: 8px !important;
  }

  details[open] {
    display: block !important;
  }

  /* Clinical Notes Header - Always Show */
  details summary {
    display: block !important;
    font-weight: 700 !important;
    font-size: 10pt !important;
    color: #000 !important;
    margin: 10px 0 5px 0 !important;
    padding: 0 !important;
    border-bottom: none !important;
    text-transform: uppercase !important;
    letter-spacing: 0.3px !important;
    list-style: none !important;
  }

  body[data-print-compact="true"] details summary {
    font-size: 9pt !important;
    margin: 8px 0 4px 0 !important;
    padding: 0 !important;
  }

  /* Hide the disclosure triangle */
  details summary::-webkit-details-marker {
    display: none !important;
  }

  details summary::marker {
    display: none !important;
  }

  /* Hide all icons in summaries */
  details summary i,
  .progress-notes-summary i,
  .reports-summary i,
  .trend-summary i {
    display: none !important;
  }

  .clinical-body {
    display: block !important;
    visibility: visible !important;
    padding: 4px 0 !important;
    margin: 0 0 8px 0 !important;
    min-height: 0 !important;
    font-size: 10pt !important;
    line-height: 1.4 !important;
    background: white !important;
    /* FULL WIDTH */
    width: 100% !important;
    max-width: none !important;
    /* ALLOW BREAKING: Long clinical notes can span pages */
    break-inside: auto !important;
    page-break-inside: auto !important;
    /* PREVENT ORPHANS/WIDOWS */
    orphans: 3 !important;
    widows: 3 !important;
  }

  body[data-print-compact="true"] .clinical-body {
    font-size: 9pt !important;
    line-height: 1.3 !important;
    margin: 0 0 5px 0 !important;
  }

  /* === PROGRESS NOTES === */
  .progress-notes-details,
  .progress-notes-details[open] {
    display: block !important;
    border-bottom: none !important;
    margin-bottom: 10px !important;
  }

  .progress-notes-summary {
    display: block !important;
    font-weight: 700 !important;
    font-size: 10pt !important;
    color: #000 !important;
    margin: 10px 0 5px 0 !important;
    padding: 0 !important;
    border-bottom: none !important;
    text-transform: uppercase !important;
    letter-spacing: 0.3px !important;
    list-style: none !important;
  }

  .progress-notes-summary i {
    display: none !important;
  }

  .progress-notes-content {
    display: block !important;
    max-height: none !important;
    opacity: 1 !important;
    overflow: visible !important;
  }

  .progress-note-item {
    margin-bottom: 8px !important;
    padding: 0 0 0 8px !important;
    border-left: 1.5px solid #666 !important;
    background: white !important;
    border-radius: 0 !important;
  }

  .progress-note-header {
    display: flex !important;
    margin-bottom: 3px !important;
  }

  .progress-note-date {
    font-weight: 700 !important;
    font-size: 9pt !important;
    color: #000 !important;
  }

  .progress-note-date i {
    display: none !important;
  }

  .progress-note-content {
    font-size: 9pt !important;
    line-height: 1.4 !important;
    color: #000 !important;
    white-space: pre-wrap !important;
  }

  body[data-print-compact="true"] .progress-note-item {
    margin-bottom: 6px !important;
    padding: 0 0 0 6px !important;
  }

  body[data-print-compact="true"] .progress-note-content {
    font-size: 8.5pt !important;
  }

  body[data-print-compact="true"] .progress-notes-summary {
    font-size: 9pt !important;
    margin: 8px 0 4px 0 !important;
  }

  /* === REPORTS SECTION === */
  details.reports-details,
  .reports-details {
    display: block !important;
    visibility: visible !important;
    border: none !important;
    margin: 0 0 6px 0 !important;
    padding: 0 !important;
    background: white !important;
    /* FULL WIDTH */
    width: 100% !important;
    max-width: none !important;
    /* ALLOW BREAKING: Long reports can span pages */
    break-inside: auto !important;
    page-break-inside: auto !important;
  }

  .reports-summary {
    display: none !important;
  }

  .reports-content {
    display: block !important;
    visibility: visible !important;
    padding: 0 !important;
    margin: 0 !important;
    width: 100% !important;
    max-width: none !important;
  }

  .reports-list {
    display: block !important;
    padding: 0 !important;
    margin: 0 !important;
    width: 100% !important;
  }

  .report-item {
    border-left: 1.5px solid #666 !important;
    padding-left: 8px !important;
    padding-right: 0 !important;
    margin-bottom: 10px !important;
    margin-right: 0 !important;
    background: white !important;
    /* FULL WIDTH */
    width: 100% !important;
    max-width: none !important;
    /* ALLOW BREAKING: Individual reports can span pages */
    break-inside: auto !important;
    page-break-inside: auto !important;
  }

  .report-header {
    display: block !important;
    margin-bottom: 3px !important;
    /* TRY to keep header with report text */
    break-after: avoid !important;
    page-break-after: avoid !important;
  }

  .report-date {
    font-weight: 700 !important;
    color: #000 !important;
    font-size: 9pt !important;
    display: block !important;
  }

  .report-text {
    white-space: pre-wrap !important;
    word-wrap: break-word !important;
    color: #000 !important;
    font-size: 9pt !important;
    line-height: 1.4 !important;
    background: white !important;
    /* FULL WIDTH */
    width: 100% !important;
    max-width: none !important;
    padding: 0 !important;
    margin: 0 !important;
    /* ALLOW BREAKING: Long report text can span pages */
    break-inside: auto !important;
    page-break-inside: auto !important;
    /* PREVENT ORPHANS/WIDOWS */
    orphans: 3 !important;
    widows: 3 !important;
  }

  /* === ORDER SECTION === */
  .order-section {
    border: none !important;
    margin: 10px 0 0 0 !important;
    padding: 0 !important;
    background: white !important;
    /* FULL WIDTH */
    width: 100% !important;
    max-width: none !important;
  }

  body[data-print-compact="true"] .order-section {
    border: none !important;
    margin: 8px 0 0 0 !important;
  }

  .order-title {
    background: white !important;
    color: #000 !important;
    padding: 0 0 3px 0 !important;
    font-size: 10pt !important;
    font-weight: 700 !important;
    text-transform: uppercase !important;
    letter-spacing: 0.3px !important;
    border-bottom: none !important;
  }

  body[data-print-compact="true"] .order-title {
    font-size: 9pt !important;
    padding: 0 0 2px 0 !important;
  }

  .order-title::before {
    display: none !important;
  }

  .order-input {
    background: white !important;
    min-height: 20px !important;
    padding: 0 0 0 8px !important;
    margin: 0 !important;
    font-size: 9pt !important;
    line-height: 1.4 !important;
    color: #000 !important;
    border: none !important;
    border-left: 1.5px solid #666 !important;
    word-wrap: break-word !important;
    /* FULL WIDTH */
    width: 100% !important;
    max-width: none !important;
    /* ALLOW BREAKING for long orders */
    break-inside: auto !important;
    page-break-inside: auto !important;
  }

  .order-input:empty::before {
    display: none !important;
  }

  /* === PAGE BREAK CONTROL === */
  /* SMART BREAKING STRATEGY:
   * - Allow patient cards to break (for long reports)
   * - Keep small elements together (headers, tasks, meta)
   * - Allow breaking in long text blocks
   * - Prevent orphans/widows
   */

  /* Small elements: try to keep together */
  .task-bar,
  .meta-row,
  .meta-badge-container {
    break-inside: avoid !important;
    page-break-inside: avoid !important;
  }

  /* Order section: try to keep together if possible, but allow if too long */
  .order-section {
    break-inside: avoid !important;
    page-break-inside: avoid !important;
  }

  /* Trend table: avoid breaking if possible */
  .trend-table {
    break-inside: avoid !important;
    page-break-inside: avoid !important;
  }

  /* Allow breaking between major sections */
  .patient-card + .patient-card {
    page-break-before: auto !important;
  }

  /* Text blocks: allow breaking with orphan/widow control */
  p,
  .clinical-body,
  .report-text,
  .order-input {
    orphans: 3 !important;
    widows: 3 !important;
  }

  /* === TYPOGRAPHY & SPACING OPTIMIZATION === */
  h1,
  h2,
  h3,
  h4,
  h5,
  h6 {
    color: #000 !important;
    background: white !important;
  }

  p,
  span,
  div,
  td,
  th,
  li,
  label {
    color: #000 !important;
    background: white !important;
  }

  a {
    text-decoration: none !important;
    color: #000 !important;
  }

  /* Remove all floating/sticky positioning */
  .floating,
  .sticky,
  .fixed {
    position: static !important;
  }

  /* === BROWSER COMPATIBILITY === */
  * {
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }

  /* === COMPACT SPACING BETWEEN SECTIONS === */
  .task-bar + .trend-details,
  .trend-details + details,
  .reports-details + .order-section {
    margin-top: 0 !important;
  }

  /* Ensure tables don't overflow */
  table {
    max-width: 100% !important;
  }

  /* === PROFESSIONAL TOUCHES === */
  /* Slightly increase spacing for better readability */
  .patient-card:not(:last-child) {
    margin-bottom: 15px !important;
  }

  /* Ensure first patient doesn't have extra space */
  .patient-card:first-child {
    margin-top: 0 !important;
  }
}
