/* ATS Modal — <dialog> + showModal() tiered system */

/* Dialog reset — constrain to viewport so inner flex chain can scroll */
dialog.ats-modal {
    border: none;
    padding: 0;
    margin: auto;
    background: transparent;
    max-width: 95vw;
    max-height: 90vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

/* Backdrop */
dialog.ats-modal::backdrop {
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(2px);
}

/* Content container — flex column so header stays fixed and body scrolls.
   box-sizing: border-box so the border doesn't push past the dialog's 90vh.
   max-height inherits from the dialog constraint — no separate value needed. */
.ats-modal-content {
    box-sizing: border-box;
    background: var(--neutral-layer-2);
    color: var(--neutral-foreground-rest);
    border: 1px solid var(--neutral-stroke-rest);
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    max-height: inherit;
    min-height: 0;
    overflow: hidden;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
}

/* Size variants */
.ats-modal-sm .ats-modal-content { width: 400px; }
.ats-modal-md .ats-modal-content { width: 600px; }
.ats-modal-lg .ats-modal-content { width: 800px; }
.ats-modal-xl .ats-modal-content { width: 1100px; }
.ats-modal-full .ats-modal-content {
    width: 95vw;
    height: 95vh;
    max-height: 95vh;
}

/* Header */
.ats-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--neutral-stroke-divider-rest);
    flex-shrink: 0;
}

.ats-modal-header h2 {
    margin: 0;
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--neutral-foreground-rest);
}

.ats-modal-close {
    background: none;
    border: none;
    color: var(--neutral-foreground-hint);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0 0.25rem;
    line-height: 1;
    transition: color 0.15s;
}

.ats-modal-close:hover {
    color: var(--neutral-foreground-rest);
}

/* Body — the only scrollable region.
   flex-basis: auto lets the body size to its content; max-height caps it
   so the dialog never exceeds the viewport. Fullscreen modals have a
   definite height on .ats-modal-content, so flex-grow fills the rest. */
.ats-modal-body {
    box-sizing: border-box;
    padding: 1rem 0px 1rem 0px;
    margin: 0px;
    overflow-y: auto;
    flex: 1 1 auto;
    min-height: 0;
    max-height: calc(80vh - 6rem);
}

.ats-modal-full .ats-modal-body {
    max-height: calc(95vh - 6rem);
}

/* Prevent inner content from breaking out of the scroll container */
.ats-modal-body > * {
    max-width: 100%;
}

.ats-modal-body table {
    display: block;
    overflow-x: auto;
}

.ats-modal-body form {
    min-height: 0;
}

/* Open animation */
dialog.ats-modal[open] {
    animation: ats-modal-fade-in 0.15s ease-out;
}

dialog.ats-modal[open]::backdrop {
    animation: ats-modal-backdrop-in 0.15s ease-out;
}

@keyframes ats-modal-fade-in {
    from {
        opacity: 0;
        transform: scale(0.95) translateY(-10px);
    }
    to {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
}

@keyframes ats-modal-backdrop-in {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* Sticky action bar — sits at the top of the modal body, stays visible while scrolling.
   Bleeds into the modal-body padding so the border spans the full width.
   scrollbar-gutter on the body means we only need to cancel the 1rem padding. */
.ats-modal-body:has(> .dialog-action-bar) {
    scrollbar-gutter: stable;
}

.dialog-action-bar {
    position: sticky;
    top: -1rem;
    z-index: 10;
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 0.5rem;
    padding: 0.375rem 1rem;
    margin: -1rem 0 2px 0;
    background: var(--neutral-layer-1, #fff);
    border-bottom: 1px solid var(--neutral-stroke-rest, #e0e0e0);
}

.dialog-action-bar .action-spacer {
    flex: 1;
}

/* Header actions container */
.ats-modal-header-actions {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

/* Responsive — collapse to full-width on small screens */
@media (max-width: 640px) {
    .ats-modal-sm .ats-modal-content,
    .ats-modal-md .ats-modal-content,
    .ats-modal-lg .ats-modal-content,
    .ats-modal-xl .ats-modal-content {
        width: 95vw;
    }
}

/* ── Print styles ─────────────────────────────────────────────────── */
@page {
    margin: 0.5in;
}

@media print {
    /* Hide everything that isn't the printing dialog or an ancestor of it */
    body.ats-printing > *:not([data-print-ancestor]):not(dialog.ats-modal-printing) {
        display: none !important;
    }

    body.ats-printing [data-print-ancestor] > *:not([data-print-ancestor]):not(dialog.ats-modal-printing) {
        display: none !important;
    }

    /* Ancestors must be visible and unconstrained */
    body.ats-printing [data-print-ancestor] {
        display: block !important;
        overflow: visible !important;
        max-height: none !important;
        height: auto !important;
        position: static !important;
        visibility: visible !important;
    }

    /* Reset the printing dialog to a normal static block so content
       flows across pages. dialog.close() removes it from the top-layer;
       the open attribute keeps it visible as a regular DOM element. */
    dialog.ats-modal.ats-modal-printing {
        display: block !important;
        position: static !important;
        max-width: none !important;
        max-height: none !important;
        width: 100% !important;
        height: auto !important;
        overflow: visible !important;
        margin: 0 !important;
        padding: 0 !important;
        border: none !important;
        background: #fff !important;
    }

    dialog.ats-modal.ats-modal-printing::backdrop {
        display: none !important;
    }

    /* Content container — light mode, no constraints, no flex column */
    .ats-modal-printing .ats-modal-content {
        display: block !important;
        background: #fff !important;
        color: #000 !important;
        border: none !important;
        border-radius: 0 !important;
        box-shadow: none !important;
        max-height: none !important;
        width: 100% !important;
        height: auto !important;
        overflow: visible !important;
    }

    /* Header — light mode, hide action buttons */
    .ats-modal-printing .ats-modal-header {
        background: #fff !important;
        color: #000 !important;
        border-bottom: 1px solid #ccc !important;
    }

    .ats-modal-printing .ats-modal-header h2 {
        color: #000 !important;
    }

    .ats-modal-printing .ats-modal-header-actions {
        display: none !important;
    }

    /* Body — remove scroll constraints so content flows across pages */
    .ats-modal-printing .ats-modal-body {
        display: block !important;
        max-height: none !important;
        height: auto !important;
        overflow: visible !important;
        padding: 0.5rem 0 !important;
        flex: none !important;
    }

    /* Hide interactive elements */
    .ats-modal-printing .dialog-action-bar {
        display: none !important;
    }

    /* Tables — light mode, proper borders */
    .ats-modal-printing table {
        display: table !important;
        overflow: visible !important;
        border-collapse: collapse !important;
    }

    .ats-modal-printing th,
    .ats-modal-printing td {
        border: 1px solid #ccc !important;
        padding: 0.25rem 0.5rem !important;
        color: #000 !important;
        background: #fff !important;
    }

    .ats-modal-printing th {
        background: #f0f0f0 !important;
        font-weight: 600 !important;
    }

    .ats-modal-printing a {
        color: #000 !important;
        text-decoration: underline !important;
    }

    /* Force light mode on all descendants — override Fluent dark-theme variables */
    .ats-modal-printing,
    .ats-modal-printing * {
        --neutral-layer-1: #fff;
        --neutral-layer-2: #f5f5f5;
        --neutral-layer-3: #ebebeb;
        --neutral-layer-4: #e0e0e0;
        --neutral-layer-card-container: #fff;
        --neutral-foreground-rest: #1a1a1a;
        --neutral-foreground-hint: #6b6b6b;
        --neutral-stroke-rest: #ccc;
        --neutral-stroke-layer-rest: #ccc;
        --neutral-stroke-divider-rest: #ddd;
        --neutral-fill-rest: #f0f0f0;
        --neutral-fill-stealth-rest: transparent;
        --accent-foreground-rest: #005a9e;
        color: #1a1a1a !important;
        color-adjust: exact !important;
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }

    /* Ensure body background is white for page margins */
    body.ats-printing {
        background: #fff !important;
    }
}
