/* ============================================================
   MISAC.view — View / Print module styles
   Scoped to .misac-view  |  print-ready via @media print
   ============================================================ */

/* ── Root container ─────────────────────────────────────────── */
.misac-view {
    font-family: var(--m-font, Arial, Helvetica, sans-serif);
    font-size: 13px;
    color: var(--m-text, #2d2d2d);
    line-height: 1.5;
    background: white;/*var(--m-bg-subtle, #f0f2f5);*/
    padding: 12px;
}

/* ── Toolbar (screen only) ──────────────────────────────────── */
.misac-view__toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
    padding: 8px 14px;
    background: var(--m-surface, #fff);
    border: 1px solid var(--m-border, #dee2e6);
    border-radius: var(--m-radius, 6px);
    box-shadow: var(--m-shadow-sm, 0 1px 3px rgba(0,0,0,.06));
    gap: 8px;
}

.misac-view__title {
    font-size: 15px;
    font-weight: 600;
    margin: 0;
    color: var(--m-text, #343a40);
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.misac-view__meta {
    font-size: 11px;
    color: #6c757d;
    white-space: nowrap;
}

.misac-view__actions {
    display: flex;
    gap: 6px;
    flex-shrink: 0;
}

/* ── Document card ──────────────────────────────────────────── */
.misac-view__document {
    background: var(--m-surface, #fff);
    border: 1px solid var(--m-border, #dee2e6);
    border-radius: var(--m-radius, 6px);
    box-shadow: var(--m-shadow-sm, 0 1px 4px rgba(0,0,0,.07));
    overflow: hidden;
}

/* ── Section ────────────────────────────────────────────────── */
.misac-view__section {
    padding: 12px 16px;
    border-bottom: 1px solid var(--m-border-light, #e9ecef);
}
.misac-view__section:last-child {
    border-bottom: none;
}

.misac-view__section-title {
    font-size: 10.5px;
    font-weight: 700;
    color: var(--m-text-secondary, #6c757d);
    text-transform: uppercase;
    letter-spacing: 0.6px;
    margin-bottom: 10px;
    padding-bottom: 6px;
    border-bottom: 1px solid var(--m-border-light, #e9ecef);
}

/* ── Field grid ─────────────────────────────────────────────── */
.misac-view__fields {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 10px 20px;
}

.misac-view__field {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0; /* prevent overflow in grid */
}

.misac-view__field--full {
    grid-column: 1 / -1;
}

.misac-view__label {
    font-size: 10px;
    font-weight: 700;
    color: var(--m-text-muted, #868e96);
    text-transform: uppercase;
    letter-spacing: 0.4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.misac-view__value {
    font-size: 13px;
    color: var(--m-text, #212529);
    word-break: break-word;
    min-height: 18px;
}

.misac-view__value--empty {
    color: var(--m-text-muted, #adb5bd);
}

.misac-view__value--multi {
    white-space: pre-wrap;
    line-height: 1.6;
    background: #f8f9fa;
    padding: 4px 6px;
    border-radius: 3px;
    font-size: 12px;
}

.misac-view__value--html {
    line-height: 1.6;
    background: #f8f9fa;
    padding: 4px 8px;
    border-radius: 3px;
    font-size: 12px;
}

.misac-view__value--html p:last-child { margin-bottom: 0; }

/* ── Attachments ────────────────────────────────────────────── */
.misac-view__thumb {
    max-width: 90px;
    max-height: 70px;
    border-radius: 4px;
    border: 1px solid #dee2e6;
    object-fit: cover;
    display: block;
    cursor: pointer;
}

.misac-view__file-link {
    font-size: 12px;
    color: var(--m-accent, #0d6efd);
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 4px;
}
.misac-view__file-link:hover { text-decoration: underline; }

.misac-view__files-list {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    align-items: center;
}
.misac-view__thumb--sm {
    max-width: 50px;
    max-height: 40px;
    cursor: pointer;
}

/* ── Grid / table sections ──────────────────────────────────── */
.misac-view__section--grid .misac-view__section-title {
    margin-bottom: 8px;
}

.misac-view__table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.misac-view__table {
    width: auto;
    min-width: 100%;
    border-collapse: collapse;
    font-size: 12px;
    margin-top: 2px;
}

.misac-view__table th,
.misac-view__table td {
    border: 1px solid #dee2e6;
    padding: 5px 8px;
    text-align: left;
    vertical-align: middle;
}

.misac-view__table th {
    background: var(--m-bg-muted, #f8f9fa);
    font-weight: 600;
    font-size: 11px;
    color: var(--m-text-secondary, #495057);
    white-space: nowrap;
}

.misac-view__table tbody tr:nth-child(even) {
    background: #fafafa;
}

.misac-view__table tbody tr:hover {
    background: var(--m-accent-light, #f0f4ff);
}

.misac-view__table tfoot td {
    font-weight: 700;
    background: #f0f4f8;
    border-top: 2px solid #adb5bd;
}

.misac-view__th--sn,
.misac-view__td--sn {
    width: 34px;
    min-width: 34px;
    text-align: center;
    color: #adb5bd;
    font-size: 11px;
}

/* ── PDF attachment embed ───────────────────────────────────── */
.misac-view__pdf-embed {
    width: 100%;
    border: 1px solid #dee2e6;
    border-radius: 4px;
    background: #f8f9fa;
    min-height: 120px;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

.misac-view__pdf-embed iframe {
    width: 100%;
    min-height: 480px;
    border: none;
    display: block;
}

/* ── When inside a modal panel ──────────────────────────────── */
.modal-body .misac-view,
.misac-panel-body .misac-view {
    background: #fff;
    padding: 0;
}

.modal-body .misac-view__toolbar,
.misac-panel-body .misac-view__toolbar {
    display: none;
}

.modal-body .misac-view__document,
.misac-panel-body .misac-view__document {
    border: none;
    box-shadow: none;
    border-radius: 0;
}

/* ── Template content wrapper (user-supplied templates) ─────── */
.misac-view__template-content {
    /* Templates are fully self-styled; no wrapper styles needed */
}

/* ── Loading state ──────────────────────────────────────────── */
.misac-view-loader {
    padding: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #6c757d;
}

/* ── Inline attachment section (below main content) ────────────── */
.misac-view-attachments {
    margin-top: 24px;
    border-top: 2px solid var(--m-border, #dee2e6);
    padding-top: 12px;
}
.misac-view-attachments__header {
    font-size: 13px;
    font-weight: 600;
    color: var(--m-text-secondary, #6c757d);
    margin-bottom: 12px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    display: flex;
    align-items: center;
    gap: 8px;
}

.misac-view-attachments__toggle {
    margin-left: auto;
    font-size: 11px;
    font-weight: 400;
    color: var(--m-accent, #0d6efd);
    background: none;
    border: 1px solid transparent;
    cursor: pointer;
    padding: 2px 6px;
    border-radius: 3px;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    line-height: 1.4;
    text-transform: none;
    letter-spacing: 0;
}
.misac-view-attachments__toggle:hover {
    border-color: var(--m-accent, #0d6efd);
    background: var(--m-accent-light, #f0f4ff);
}

/* Print-only URL caption — hidden on screen, shown in print instead of embeds */
.misac-view-attachments__print-link {
    display: none;
}
.misac-view-attachments__item {
    margin-bottom: 20px;
}
.misac-view-attachments__label {
    font-size: 12px;
    font-weight: 600;
    color: var(--m-text, #212529);
    margin-bottom: 6px;
    display: flex;
    align-items: center;
    gap: 6px;
}
.misac-view-attachments__open {
    margin-left: auto;
    font-size: 11px;
    font-weight: 400;
    color: var(--m-accent, #0d6efd);
    text-decoration: none;
}
.misac-view-attachments__open:hover { text-decoration: underline; }
.misac-view-attachments__share {
    font-size: 11px;
    font-weight: 400;
    color: var(--m-accent, #0d6efd);
    background: none;
    border: none;
    padding: 0;
    cursor: pointer;
    font-family: inherit;
}
.misac-view-attachments__share:hover { text-decoration: underline; }
.misac-view-attachments__pdf {
    width: 100%;
    border: 1px solid var(--m-border, #dee2e6);
    border-radius: 4px;
    overflow: hidden;
    background: #f8f9fa;
    min-height: 120px;
}
.misac-view-attachments__loading {
    padding: 32px;
    text-align: center;
    color: #6c757d;
    font-size: 13px;
}
.misac-view-attachments__img {
    max-width: 100%;
    height: auto;
    border: 1px solid var(--m-border, #dee2e6);
    border-radius: 4px;
    display: block;
}
.misac-view-attachments__file-link {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    border: 1px solid var(--m-border, #dee2e6);
    border-radius: 4px;
    font-size: 13px;
    color: var(--m-accent, #0d6efd);
    text-decoration: none;
}
.misac-view-attachments__file-link:hover {
    background: var(--m-bg-muted, #f8f9fa);
}
/* PDF canvas pages inside the holder */
.misac-view-attachments__pdf .pdf-print-wrapper { width: 100%; }
.misac-view-attachments__pdf .pdf-print-page { margin: 0; padding: 0; }
.misac-view-attachments__pdf canvas { width: 100% !important; height: auto !important; display: block; }

/* ── Sidebar mode (CSS-only toggle, no DOM movement) ─────────────────── */
.misac-view.attachments-sidebar-mode {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
}
.misac-view.attachments-sidebar-mode .misac-view__toolbar {
    flex: 0 0 100%;
}
.misac-view.attachments-sidebar-mode .misac-view__document,
.misac-view.attachments-sidebar-mode .misac-view__template-content {
    flex: 1;
    min-width: 0;
}
.misac-view.attachments-sidebar-mode .misac-view-attachments {
    width: 260px;
    flex-shrink: 0;
    align-self: flex-start;
    position: sticky;
    top: 0;
    max-height: calc(100vh - 60px);
    overflow-y: auto;
    margin-top: 0;
    border-top: none;
    padding-top: 8px;
    border-left: 1px solid var(--m-border, #dee2e6);
    padding-left: 10px;
    background: var(--m-bg-muted, #f8f9fa);
}
.misac-view.attachments-sidebar-mode .misac-view-attachments__header {
    margin-bottom: 8px;
}
.misac-view.attachments-sidebar-mode .misac-view-attachments__pdf {
    max-height: 240px;
    overflow-y: auto;
}

@media print {
    /* Attachments section: no page-break on the wrapper itself.
       Each __item manages its own break below. */
    .misac-view-attachments {
        margin-top: 0 !important;
        border-top: none !important;
        padding-top: 0 !important;
    }

    /* Every attachment starts on its own page.
       Use block layout — flex+height:100% is unreliable in print across
       browsers and can cause a blank page followed by a clipped canvas. */
    .misac-view-attachments__item {
        page-break-before: always;
        break-before: page;
        display: block;
        height: auto;
        overflow: visible;
        margin-bottom: 0 !important;
    }

    /* Hide interactive chrome only */
    .misac-view-attachments__header  { display: none; }
    .misac-view-attachments__toggle  { display: none; }
    .misac-view-attachments__open    { display: none; }
    .misac-view-attachments__loading { display: none !important; }

    /* Field label line — compact */
    .misac-view-attachments__label {
        font-size: 10px;
        font-weight: bold;
        padding-bottom: 2px;
    }

    /* Print-only ID + URL line */
    .misac-view-attachments__print-link {
        display: block !important;
        font-size: 10px;
        margin-bottom: 4px;
        padding-bottom: 3px;
        border-bottom: 1px solid #bbb;
    }
    .misac-view-attachments__print-link a {
        color: #333;
        word-break: break-all;
    }
    .misac-view-attachments__print-id {
        font-weight: 700;
        color: #222;
    }

    /* PDF holder: simple block, no overflow clipping */
    .misac-view-attachments__pdf {
        display: block;
        overflow: visible;
        border: none !important;
        background: none !important;
        min-height: 0;
    }
    /* Wrapper: restore inline width:100% from PdfPrintEmbed */
    .misac-view-attachments__pdf .pdf-print-wrapper {
        width: 100% !important;
        height: auto !important;
        overflow: visible;
    }
    /* Per-PDF-page breaks: suppress page-break-after from PdfPrintEmbed to
       avoid blank trailing pages. Allow break-inside so a canvas that is
       slightly taller than one print page can flow naturally. */
    .misac-view-attachments__pdf .pdf-print-page {
        break-after:        auto !important;
        page-break-after:   auto !important;
        break-inside:       auto !important;
        page-break-inside:  auto !important;
        height:             auto !important;
        margin-bottom:      0    !important;
    }
    /* Canvas: constrain to fit on one print page alongside the metadata header.
       Label (~5mm) + print-link (~10mm) + canvas (≤265mm) + buffer = ~280mm < A4 297mm.
       width:auto + max-width:100% + max-height:265mm scales the canvas to satisfy
       whichever axis is more restrictive while preserving aspect ratio — so a
       tall portrait PDF narrows slightly to fit in height, and a short/landscape
       PDF uses full width.  No height:100% — that requires a concrete parent height
       which print cannot reliably provide through an auto-height ancestor chain. */
    /* Canvas must fit on the same printed page as the label + print-link.
       A4 usable height ~265mm minus ~15mm (label + url line) leaves ~250mm.
       400mm overflowed and pushed the canvas to a separate page (third page
       gap when main view sat on page 1, label on page 2, canvas alone on 3). */
    .misac-view-attachments__pdf canvas {
        width:      100% !important;
        height:     auto !important;
        max-width:  100% !important;
        max-height: 350mm !important;
        display:    block !important;
    }
    /* Image attachments — same constraint */
    .misac-view-attachments__img {
        max-width:  100%;
        max-height: 350mm;
        width:      auto;
        height:     auto;
        display:    block;
        margin:     0 auto;
    }
    /* Keep label + print-link + canvas glued together so the browser can't
       split them across pages. */
    .misac-view-attachments__item {
        break-inside:      avoid !important;
        page-break-inside: avoid !important;
    }

    /* Sidebar mode: revert to block layout so attachments print correctly */
    .misac-view.attachments-sidebar-mode {
        display: block !important;
    }
    .misac-view.attachments-sidebar-mode .misac-view-attachments {
        position: static !important;
        width: 100% !important;
        max-height: none !important;
        border-left: none !important;
        padding-left: 0 !important;
        background: transparent !important;
    }
}

/* ── Attachment sidebar (panel mode) ──────────────────────────── */
.misac-view-panel__wrapper {
    display: flex;
    height: 100%;
    overflow: hidden;
}

.misac-view-panel__main {
    flex: 1;
    min-width: 0;
    overflow-y: auto;
}

.misac-view-sidebar {
    width: 260px;
    flex-shrink: 0;
    border-left: 1px solid var(--m-border, #dee2e6);
    background: var(--m-bg-muted, #f8f9fa);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

.misac-view-sidebar__header {
    position: sticky;
    top: 0;
    background: #f8f9fa;
    padding: 12px 14px;
    font-size: 12px;
    font-weight: 700;
    color: #495057;
    text-transform: uppercase;
    letter-spacing: 0.4px;
    border-bottom: 1px solid #dee2e6;
    z-index: 1;
}

.misac-view-sidebar__list {
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.misac-view-sidebar__item {
    border: 1px solid #dee2e6;
    border-radius: 6px;
    background: #fff;
    padding: 10px;
    cursor: pointer;
    transition: border-color 0.15s, box-shadow 0.15s;
}

.misac-view-sidebar__item:hover {
    border-color: var(--m-accent, #0d6efd);
    box-shadow: 0 1px 4px rgba(13,110,253,.15);
}

.misac-view-sidebar__thumb {
    max-width: 100%;
    max-height: 140px;
    border-radius: 4px;
    object-fit: cover;
    display: block;
    margin-bottom: 6px;
}

.misac-view-sidebar__file-icon {
    font-size: 28px;
    color: #6c757d;
    margin-bottom: 6px;
    text-align: center;
}

.misac-view-sidebar__name {
    font-size: 12px;
    font-weight: 600;
    color: #212529;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.misac-view-sidebar__label {
    font-size: 10px;
    color: #868e96;
    margin-top: 2px;
}

/* ──────────────────────────────────────────────────────────────
   PRINT STYLES
   ────────────────────────────────────────────────────────────── */
@media print {
    .misac-view {
        background: white;
        padding: 0;
    }

    .misac-view__toolbar,
    .no-print {
        display: none !important;
    }

    .misac-view__document {
        border: none;
        box-shadow: none;
        border-radius: 0;
    }

    .misac-view__section {
        page-break-inside: avoid;
        break-inside: avoid;
    }

    .misac-view__section--grid {
        page-break-inside: auto;
        break-inside: auto;
    }

    /* Increase font sizes for fallback (no-template) print */
    .misac-view__section-title {
        font-size: 11pt;
    }

    .misac-view__label {
        font-size: 9pt;
    }

    .misac-view__value {
        font-size: 11pt;
    }

    .misac-view__table {
        font-size: 10.5pt;
    }

    .misac-view__table th {
        font-size: 9.5pt;
    }

    .misac-view__table tbody tr:hover {
        background: none;
    }

    .misac-view__table tbody tr:nth-child(even) {
        background: none;
    }

    .misac-view__file-link {
        color: #000;
    }

    .misac-view-sidebar {
        display: none !important;
    }

    /* Keep tables from splitting weirdly */
    .misac-view__table {
        page-break-inside: auto;
    }
    .misac-view__table tr {
        page-break-inside: avoid;
        break-inside: avoid;
    }

    /* Repeat the header row on every printed page so a grid that spans a page
       break still shows its column headers (was: header + column heads on page 1,
       body rows orphaned on page 2). table-footer-group does the same for totals. */
    .misac-view__table thead,
    .misac-matrix thead {
        display: table-header-group;
    }
    .misac-view__table tfoot {
        display: table-footer-group;
    }

    /* Keep the landed-cost / matrix card whole so its header is never split from
       its body. (If the matrix is taller than a page the browser breaks it anyway,
       but the thead repeat + row rules above keep that break clean.) */
    .misac-view-landed {
        page-break-inside: avoid;
        break-inside: avoid;
    }
    .misac-matrix tr {
        page-break-inside: avoid;
        break-inside: avoid;
    }

    /* Never strand a section / card header alone at the bottom of a page. */
    .misac-view__section-title,
    .misac-view-landed__header {
        page-break-after: avoid;
        break-after: avoid;
    }
}
