/* Reset & Base Styles */
* { box-sizing: border-box; }
html { scrollbar-gutter: stable; }
body {
    margin: 0; padding: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    font-size: 1.1rem;
    display: flex; flex-direction: column; align-items: center;
    min-height: 100vh; min-height: 100dvh; width: 100%;
    line-height: 1.6; position: relative; overflow-x: hidden;
}

/* Background Layer
 * SVG colour classes (witen-bg / witen-main / witen-accent) are defined in the
 * SVG files themselves. JS always prepares transparent-bg blob URLs and sets
 * --bg-logo-left / --bg-logo-right before the watermarks are shown, so the
 * fallback is intentionally `none` to prevent a blue-background flash on load. */
body::before {
    content: ""; position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: -1;
    pointer-events: none;
    background-image: var(--bg-logo-left, none), var(--bg-logo-right, none);
    background-repeat: no-repeat, no-repeat;
    background-position:
        calc(((100vw - 1024px) / 4) - (min(400px, (100vw - 1024px) / 2) / 2)) center,
        calc((100vw - ((100vw - 1024px) / 4)) - (min(400px, (100vw - 1024px) / 2) / 2)) center;
    background-size: min(400px, calc((100vw - 1024px) / 2)) auto; opacity: 0.1;
}

/* Header Layout */
#site-header { display: flex; align-items: center; justify-content: flex-start; gap: 1rem; width: 1024px; max-width: 100%; padding: 1rem; z-index: 1000; position: relative; margin: 0 auto; }
#menu-container { display: flex; align-items: center; height: 2.2rem; flex-shrink: 0; }
.logo-link-wrapper { display: flex; align-items: center; height: 2.2rem; text-decoration: none; flex-grow: 0; flex-shrink: 1; min-width: 0; }
#header-logo { height: 100%; width: auto; max-width: 100%; object-fit: contain; display: block; }
#hamburger { background: none; border: none; cursor: pointer; height: 2.2rem; width: 2.2rem; display: flex; align-items: center; justify-content: center; color: inherit; }
#hamburger svg { width: 1.8rem; height: 1.8rem; }

/* Download Link Fix */
#download-link {
    margin-left: auto;
    display: block !important;
    width: 2.2rem;
    height: 2.2rem;
    flex-shrink: 0;
    cursor: pointer;
    -webkit-mask: url('/assets/download.svg') no-repeat center / 2.2rem;
    mask: url('/assets/download.svg') no-repeat center / 2.2rem;
    background-color: currentColor; /* Inherits JS-assigned body color */
    overflow: visible;
}

#nav-menu { position: absolute; top: 100%; left: 1rem; border-radius: 8px; padding: 1.5rem; display: flex; flex-direction: column; box-shadow: 0 10px 25px rgba(0,0,0,0.5); min-width: 280px; }
#nav-menu.hidden, #download-link.hidden { display: none !important; }
#nav-menu a { color: #ffffff; text-decoration: none; font-weight: 500; padding: 0.8rem 0; border-bottom: 1px solid rgba(255,255,255,0.1); }
#nav-menu a:last-child { border-bottom: none; }

/* Main Content Frame */
#content-frame { flex: 1; width: 1024px; max-width: 100%; margin: 0 auto; padding: 4rem 7rem; transition: all 0.3s ease; position: relative; z-index: 1; min-height: 0; opacity: 0; }
@media (max-width: 860px) { #content-frame { padding: 3vw 3vw; } }
.homepage-frame { display: flex !important; flex-direction: column; justify-content: center; align-items: center; background-color: transparent !important; }
.homepage-frame #frame-image { width: 350px; max-width: 100%; object-fit: contain; margin: -2rem auto; display: block; }
.homepage-frame #markdown-body { text-align: justify; }

/* Homepage wrapper overrides */
.homepage-wrapper {
    flex: 1; /* Take up flexible space instead of content-frame */
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100%;
}

.homepage-wrapper #content-frame {
    flex: 0 0 auto; /* Don't grow, don't shrink, auto size */
    min-height: 0; /* Remove min-height constraint */
    padding-bottom: 0; /* Remove bottom padding on homepage only */
}

.homepage-wrapper .homepage-frame {
    min-height: 0; /* Remove min-height from homepage-frame when in wrapper */
}

.homepage-wrapper #nav-frame {
    margin-top: 0; /* No gap on homepage */
    margin-bottom: 0;
}
.document-frame { display: block !important; background-color: #ebe0cb; color: #000000; border-radius: 4px; }
@media (prefers-color-scheme: dark) { .document-frame { background-color: rgba(16, 20, 24, 0.85); color: #f4f1ea; } }
.document-frame #frame-image { width: 60%; max-height: 20vh; margin: 0 auto 2rem auto; display: block; }

#markdown-body { width: 100%; overflow-wrap: break-word; }

.legislation-section { scroll-margin-top: var(--scroll-offset, 2em); }
.article-section { scroll-margin-top: var(--scroll-offset, 2em); }

/* Apply to both screen and print */
.legislation-section p { margin-top: 0 !important; margin-bottom: 0 !important; }
#markdown-body p:not(.legislation-section p) { margin-top: 1em; margin-bottom: 1em; }

#content-frame a { text-decoration: underline; transition: color 0.2s ease; color: inherit; }
#content-frame a:hover { color: #CC0000 !important; }

/* Pronunciation links - look like regular links but trigger audio */
.pronounce {
    cursor: pointer;
    text-decoration: underline;
    color: inherit;
    transition: color 0.2s ease;
}
.pronounce:hover {
    color: #CC0000 !important;
}

footer { text-align: center; padding: 1.5rem; font-size: 0.8rem; opacity: 0.8; width: 100%; position: relative; z-index: 1; }

/* Navigation Frame */
#nav-frame {
    width: 1024px;
    max-width: 100%;
    margin: 2rem auto 0 auto;
    padding: 0 0;
    background-color: transparent !important;
    position: relative;
    z-index: 1;
    color: inherit;
    opacity: 0;
    transition: opacity 0.3s ease;
}

#nav-frame a {
    text-decoration: underline;
    transition: color 0.2s ease;
    color: inherit;
}

#nav-frame a:hover {
    color: #CC0000 !important;
}

@media (max-width: 1024px) { body::before { display: none; } }

/* --- PRINT ENGINE 2026 --- */

/* Safari-specific: CSS-based image swapping to prevent print flash */
@supports (hanging-punctuation: first) {
    @media print {
        /* Hide original images when print-ready, use pseudo-elements instead */
        #header-logo[data-print-ready] {
            visibility: hidden !important;
            position: relative;
        }

        #header-logo[data-print-ready]::after {
            content: "";
            position: absolute;
            top: 0; left: 0;
            width: 100%; height: 100%;
            visibility: visible;
            background: none; /* overridden by JS-injected #witen-print-assets */
        }

        #frame-image[data-print-ready] {
            visibility: hidden !important;
            position: relative;
        }

        #frame-image[data-print-ready="logo"]::after {
            content: "";
            position: absolute;
            top: 0; left: 0;
            width: 100%; height: 100%;
            visibility: visible;
            background: none; /* overridden by JS-injected #witen-print-assets */
        }

        #frame-image[data-print-ready="abillfor"]::after {
            content: "";
            position: absolute;
            top: 0; left: 0;
            width: 100%; height: 100%;
            visibility: visible;
            background: url('/assets/abillfor-light.svg') no-repeat center;
            background-size: contain;
        }
    }
}

@media print {
    @page {
        margin: 2cm 2cm 1.5cm 2cm;
    }

    /* Safari-specific: Larger bottom margin to make page ~2 lines shorter */
    @supports (hanging-punctuation: first) {
        @page {
            margin: 2cm 2cm 3.0cm 2cm;
        }
    }

    /* Firefox-specific: Smaller bottom margin to lower footer position */
    @supports (-moz-appearance: none) {
        @page {
            margin: 2cm 2cm 0.5cm 2cm;
        }
    }

    /* Chrome-specific: @page margin footer (excludes Safari via hanging-punctuation) */
    @supports selector(::-webkit-scrollbar) and (not (hanging-punctuation: first)) {
        @page {
            margin: 2cm 1cm 2cm 1cm;
            @bottom-left {
                content: "";
                padding-top: 8pt;
                padding-left: 1cm;
                margin-top: -2.2em;
            }
            @bottom-center {
                content: var(--print-title);
                padding-top: 8pt;
                font-style: italic;
                font-family: serif;
                font-size: 11pt;
                margin-top: -2.2em;
            }
            @bottom-right {
                content: counter(page);
                padding-top: 8pt;
                padding-right: 1cm;
                font-family: serif;
                font-size: 11pt;
                margin-top: -2.2em;
            }
        }
    }

    body {
        background: white !important;
        color: black !important;
        display: block !important;
        font-family: "Times New Roman", Times, serif !important;
        font-size: 11pt !important;
    }

    /* Safari: Drop font size by 0.5pt */
    .browser-safari body {
        font-size: 10.27pt !important;
    }

    /* Firefox: Drop font size by 0.1pt */
    .browser-firefox body {
        font-size: 10.96pt !important;
    }

    #content-frame, .document-frame, .homepage-frame { background: white !important; color: black !important; display: block !important; min-height: 0 !important; width: 100% !important; margin: 0 !important; padding: 0 !important; opacity: 1 !important; }
    body::before, footer, #nav-menu, .screen-content, #frame-image, .print-footer-fixed, #nav-frame { display: none !important; }

    /* Header Layout - Right Aligned */
    #site-header {
        width: 100% !important;
        margin: 0 !important;
        padding: 0 !important;
        display: flex !important;
        align-items: center !important;
        justify-content: flex-end !important;
        gap: 0 !important;
    }

    /* Chrome: Add side padding to header to match content */
    .browser-chrome #site-header {
        padding-left: 1cm !important;
        padding-right: 1cm !important;
    }

    /* Hide hamburger completely - aggressive targeting for iPad Safari */
    #hamburger,
    #hamburger *,
    #hamburger img,
    #hamburger svg {
        display: none !important;
        visibility: hidden !important;
        width: 0 !important;
        height: 0 !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    #menu-container, .logo-link-wrapper { display: flex !important; margin: 0 !important; padding: 0 !important; align-items: center !important; }
    #header-logo { height: 1.76rem !important; width: auto !important; margin: 0 !important; display: block !important; }

    /* Hide download link completely - aggressive targeting for iPad Safari */
    #download-link,
    #download-link * {
        display: none !important;
        visibility: hidden !important;
        width: 0 !important;
        height: 0 !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    /* Table Engine */
    .print-table {
        display: table !important;
        width: 100% !important;
        border-collapse: collapse !important;
        table-layout: fixed !important;
    }
    .print-table tfoot { display: table-footer-group !important; }

    .print-content-wrapper {
        padding-top: 0 !important;
        display: block !important;
    }
    .browser-firefox .showcase-print .print-content-wrapper { padding-top: 20vh !important; }
    .browser-safari .showcase-print .print-content-wrapper { padding-top: 20vh !important; }

    /* Chrome: Set consistent margins */
    .browser-chrome .print-content-wrapper {
        padding-left: 1cm !important;
        padding-right: 1cm !important;
    }

    .browser-chrome .showcase-print .print-content-wrapper {
        padding-top: 20vh !important;
    }

    .print-page-break {
        display: block !important;
        break-before: page !important;
        page-break-before: always !important;
        height: 1px !important;
        margin: 0 !important;
        border: none !important;
        visibility: hidden !important;
    }

    /* Firefox fix: ensure content before page breaks can break */
    .print-content-wrapper > * {
        break-inside: auto !important;
        page-break-inside: auto !important;
    }

    /* Prevent page breaks inside legislation sections */
    .legislation-section {
        break-inside: avoid !important;
        page-break-inside: avoid !important;
        margin-bottom: 1em !important;
    }

        /* Prevent page breaks inside article sections */
    .article-section {
        break-inside: avoid !important;
        page-break-inside: avoid !important;
        margin-bottom: 1em !important;
    }

    /* Firefox: First page spacer for showcase documents */
    .firefox-first-page-spacer {
        display: block !important;
        break-inside: avoid !important;
        page-break-inside: avoid !important;
    }

    .print-footer-row {
        height: 1.5cm;
        border-top: 1.5pt solid black;
        font-family: serif;
        font-size: 11pt;
        vertical-align: middle;
        page-break-after: avoid;
        break-after: avoid;
    }


    .browser-firefox .print-content-wrapper {
        margin-bottom: 0 !important;
    }

    /* When print-bottom-logo-wrapper exists, remove any bottom margin */
    .print-content-wrapper:has(.print-bottom-logo-wrapper) {
        margin-bottom: 0 !important;
        padding-bottom: 0 !important;
        overflow: hidden !important;
    }

    /* Hide the spacer div after print-content-wrapper */
    .print-table tbody tr td > div:nth-child(2) {
        display: none !important;
        height: 0 !important;
        overflow: hidden !important;
    }

    /* Ensure table cell clips at bottom logo */
    .print-table tbody tr td {
        overflow: hidden !important;
        padding-bottom: 0 !important;
    }

    .print-footer-flex {
        display: flex !important;
        justify-content: space-between;
        align-items: center;
        width: 100%;
        padding-top: 8pt;
    }

    /* Firefox: Center title when page number is hidden */
    @supports (-moz-appearance: none) {
        .print-footer-flex {
            justify-content: center !important;
        }
    }

    .print-footer-title { font-style: italic; }

    /* Chrome: Hide table footer content, keep border line only */
    .browser-chrome .print-footer-flex {
        display: none !important;
    }

    .browser-chrome .print-footer-row {
        margin-top: 0 !important;
        height: 0 !important;
        min-height: 0 !important;
        padding-left: 1cm !important;
        padding-right: 1cm !important;
    }

    .browser-chrome .print-table tfoot td {
        padding-left: 1cm !important;
        padding-right: 1cm !important;
    }

    /* Safari: Hide table footer completely since Safari can't repeat it */
    .browser-safari .print-table tfoot {
        display: none !important;
    }

    /* Bottom logo on last page */
    .print-bottom-logo-wrapper {
        display: flex !important;
        flex-direction: column !important;
        justify-content: space-between !important;
        align-items: center !important;
        min-height: calc(100vh - 2cm) !important;
        page-break-before: avoid !important;
        break-before: avoid !important;
        overflow: hidden !important;
    }

    /* Chrome: Use empirically determined value that fills page without overflow */
    .browser-chrome .print-bottom-logo-wrapper {
        min-height: calc(100vh - 0.2cm) !important;
    }

    /* Clip everything after the bottom logo wrapper */
    .print-bottom-logo-wrapper ~ * {
        display: none !important;
        visibility: hidden !important;
        height: 0 !important;
        overflow: hidden !important;
    }

    .print-bottom-logo {
        display: block !important;
        width: 50% !important;
        height: auto !important;
        margin: auto 0 !important;
    }

    .print-bottom-footer {
        margin-top: 0 !important;
        margin-bottom: calc(1.6em * 2) !important;
        padding-bottom: calc(1.6em * 2) !important;
        width: 60% !important;
        text-align: center !important;
        font-size: 11pt !important;
        line-height: 1.6 !important;
    }

    /* Chrome: Remove bottom margin since @page margins handle footer spacing */
    .browser-chrome .print-bottom-footer {
        margin-bottom: 0 !important;
    }

    .browser-safari .print-bottom-footer {
        font-size: 10.27pt !important;
    }

    .browser-firefox .print-bottom-footer {
        font-size: 10.96pt !important;
    }

    /* Remove underlines from hyperlinks in print */
    a {
        text-decoration: none !important;
    }

    /* Remove underlines from pronunciation links in print */
    .pronounce {
        text-decoration: none !important;
    }

    /* Hide screen-only elements (e.g., emojis) in print */
    .screen-only {
        display: none !important;
    }

}

@media screen {
    .print-table, #hamburger img, .firefox-first-page-spacer, .print-bottom-logo, .print-bottom-logo-wrapper, .print-bottom-footer { display: none !important; }
    /* Hide header content until the transparent-bg witenagemot blob URL is ready.
     * visibility:hidden keeps layout intact (no reflow) and the DOM remains
     * readable by crawlers. The class is added/removed by JS only, so this rule
     * has no effect when JS is disabled. */
    #site-header.header-loading { visibility: hidden; }
}
