/*
 * Screen-size fixes shared by every page that includes partials/nav.php.
 * Loaded after the page's own stylesheets, with a modification-time
 * version, so changes here reach visitors without re-versioning style.css
 * on every page.
 */

/* ---------- Navigation: hamburger menu up to 1120px ----------
 * The full menu needs about 1120px. Below that the right-hand items were
 * clipped on tablets and small laptops, so the phone menu (copied from the
 * max-width: 768px rules in style.css) now applies up to 1120px. */
@media (min-width: 769px) and (max-width: 1120px) {
    nav {
        padding: 0.75rem 1rem;
        height: var(--header-height);
    }

    .logo {
        font-size: 1.3rem;
    }

    .menu-icon {
        display: block;
        margin-left: auto;
    }

    nav ul {
        position: fixed;
        top: var(--header-height);
        left: 0;
        width: 100%;
        flex-direction: column;
        background: var(--white);
        display: none;
        padding: 0 0 1rem 0;
        box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        z-index: 1002;
        max-height: calc(100vh - var(--header-height));
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    nav ul.active {
        display: flex;
    }

    nav ul li {
        margin: 0;
        width: 100%;
        height: auto;
        border-bottom: 1px solid #f0f0f0;
        display: block;
    }

    nav ul li a {
        padding: 1rem 1.5rem;
        width: 100%;
        font-size: 1.1rem;
        justify-content: flex-start;
        height: auto;
    }

    nav ul li.cta-menu-item {
        margin-left: 0;
        padding-left: 0;
        border-bottom: none;
    }

    nav ul li.cta-menu-item a {
        margin: 1rem 1.5rem;
        width: calc(100% - 3rem);
        text-align: center;
        border-radius: 5px;
        padding: 1rem;
    }

    nav ul li a::after {
        display: none;
    }

    nav ul li a:active {
        background: var(--light-gray);
    }

    .dropdown {
        position: relative;
    }

    .dropdown > a {
        font-weight: 700;
        color: var(--primary-color);
        background: var(--light-gray);
        text-transform: uppercase;
        font-size: 0.9rem;
        letter-spacing: 0.5px;
        position: relative;
        cursor: default;
        pointer-events: none;
    }

    .dropdown > a:hover {
        color: var(--primary-color);
    }

    .dropdown {
        margin-top: 1rem;
        padding-top: 1rem;
        border-top: 2px solid #e0e0e0;
    }

    .dropdown:first-of-type {
        margin-top: 0;
        padding-top: 0;
        border-top: none;
    }

    .dropdown:hover .dropdown-menu {
        display: block; /* Always show on mobile */
    }

    .dropdown-menu {
        position: static;
        display: block !important; /* Always visible on mobile */
        width: 100%;
        margin: 0;
        padding: 0;
        background: transparent;
        box-shadow: none;
        border-radius: 0;
        margin-left: 1rem;
    }

    .dropdown-menu li {
        background: var(--white);
        width: calc(100% - 1rem);
        height: auto;
        margin: 0;
        border-bottom: 1px solid #f0f0f0;
    }

    .dropdown-menu li:last-child {
        border-bottom: none;
    }

    .dropdown-menu a {
        padding: 0.875rem 1.5rem !important;
        font-size: 0.95rem;
        background: transparent;
        position: relative;
        color: var(--text-color);
    }

    .dropdown-menu a::after {
        display: none;
    }

    .dropdown-menu a:active {
        background: rgba(52, 152, 219, 0.1);
    }

    .dropdown-menu li:last-child a {
        font-weight: 600;
        color: var(--secondary-color);
        border-top: 1px solid #e0e0e0;
        margin-top: 0.5rem;
        padding-top: 1rem !important;
    }

    nav ul > li:last-child {
        border-bottom: none;
        margin-bottom: 0;
    }

    nav ul {
        padding-bottom: 4rem !important;
    }

    nav {
        flex-wrap: wrap;
    }

    nav ul {
        position: fixed;
        top: var(--header-height);
        left: 0;
        width: 100%;
        flex-direction: column;
        background: var(--white);
        display: none;
        padding: 1rem 0;
        box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        z-index: 1002;
        max-height: calc(100vh - var(--header-height));
        overflow-y: auto;
    }
}

/* ---------- Phones: nothing wider than the screen ---------- */
@media (max-width: 600px) {
    /* Card grids asked for 350px columns, wider than a 360px phone. */
    .kb-grid,
    .blog-grid,
    .project-grid {
        grid-template-columns: minmax(0, 1fr);
    }

    /* KB articles: less side padding so the text gets the width. */
    .main-container {
        padding-left: 0.75rem;
        padding-right: 0.75rem;
    }
    .kb-article {
        padding: 1.25rem 1rem;
    }

    /* Severity chips on service pages. */
    .lm-sev {
        font-size: 0.75rem;
    }
}

/* Floating share buttons sat on top of the floating email button. */
@media (max-width: 768px) {
    .social-share.floating {
        right: auto;
        left: 16px;
    }
}

/* Long inline formulas scroll inside their own box instead of the page. */
mjx-container:not([display="true"]) {
    display: inline-block;
    max-width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    vertical-align: middle;
}
mjx-container[display="true"] {
    max-width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
}

/* Downloads: feature list beside the download box, stacked on small screens. */
.dl-features-grid {
    display: grid;
    grid-template-columns: 2fr 1fr;
    gap: 2rem;
    margin-top: 1.5rem;
    align-items: start;
}
@media (max-width: 768px) {
    .dl-features-grid {
        grid-template-columns: minmax(0, 1fr);
    }
}

/* ---------- Calculators on phones: readable text, tappable buttons ---------- */
@media (max-width: 600px) {
    body[data-sveltekit-preload-data] .panel-title,
    body[data-sveltekit-preload-data] .field label,
    body[data-sveltekit-preload-data] .unit,
    body[data-sveltekit-preload-data] .hint,
    body[data-sveltekit-preload-data] .model-note,
    body[data-sveltekit-preload-data] .t-label,
    body[data-sveltekit-preload-data] .t-value i,
    body[data-sveltekit-preload-data] .results .note,
    body[data-sveltekit-preload-data] button.copy {
        font-size: 0.8125rem !important;
    }
    #root.tool-page-container .field-unit,
    #root.tool-page-container .result-label,
    #root.tool-page-container .model-label,
    #root.tool-page-container .model-value,
    #root.tool-page-container .assumptions-title {
        font-size: 0.8125rem !important;
    }
}
@media (pointer: coarse) {
    body[data-sveltekit-preload-data] .seg button,
    body[data-sveltekit-preload-data] .presets button,
    #root.tool-page-container .toggle-buttons button {
        min-height: 40px;
    }

    /* Homepage slideshow dots stay small but get a finger-sized hit area. */
    .carousel-dot {
        position: relative;
    }
    .carousel-dot::after {
        content: '';
        position: absolute;
        inset: -16px -8px;
    }
}

/* ---------- Service pages: no lone card on the last row ---------- */
@media (min-width: 1100px) {
    .lm-bento:not(.lm-bento--split):has(> :nth-child(4):last-child),
    .lm-bento:not(.lm-bento--split):has(> :nth-child(7):last-child),
    .lm-bento:not(.lm-bento--split):has(> :nth-child(8):last-child) {
        grid-template-columns: repeat(4, 1fr);
    }
}
@media (min-width: 901px) and (max-width: 1099px) {
    .lm-bento:not(.lm-bento--split) > :nth-child(3n+1):last-child {
        grid-column: 2;
    }
}
@media (min-width: 621px) and (max-width: 900px) {
    .lm-bento:not(.lm-bento--split) > :nth-child(odd):last-child {
        grid-column: 1 / -1;
    }
}
/* Wide two-column service cards (services.html): a lone last card sits
 * centred under the pair at the same width. */
@media (min-width: 621px) {
    .lm-bento--split > :nth-child(odd):last-child {
        grid-column: 1 / -1;
        justify-self: center;
        width: calc(50% - 0.625rem);
    }
}

/* ---------- Wide screens ---------- */

/* Project pages: photo beside Challenge / Solution / Results. */
@media (min-width: 1100px) {
    .service-detail-content:has(> img:first-child) {
        max-width: 1200px;
        display: grid;
        grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr);
        /* Six text rows beside the photo; if the photo is taller, the
           extra height goes below the last paragraph, not between them. */
        grid-template-rows: repeat(5, auto) 1fr;
        column-gap: 3rem;
        align-items: start;
    }
    .service-detail-content:has(> img:first-child) > img:first-child {
        grid-column: 1;
        grid-row: 1 / span 6;
        margin-bottom: 0 !important;
        position: sticky;
        top: 90px;
    }
    .service-detail-content:has(> img:first-child) > :not(img) {
        grid-column: 2;
    }
    .service-detail-content:has(> img:first-child) > h2:nth-child(2) {
        margin-top: 0;
    }
    .service-detail-content:has(> img:first-child) > .service-cta {
        grid-column: 1 / -1;
        margin-top: 2.5rem;
    }
}

/* Index pages (projects, knowledge base): three columns divide their
 * card counts evenly, so on large screens the cards grow instead. */
@media (min-width: 1500px) {
    .featured:has(> .project-grid),
    .featured:has(.kb-grid) {
        max-width: 1360px;
    }
    .project-grid,
    .kb-grid {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }
}

/* KB articles: an "On this page" list beside the article
 * (built by js/site-layout.js when an article has enough sections). */
.kb-toc {
    display: none;
}
@media (min-width: 1400px) {
    .main-container.has-toc {
        max-width: 1240px;
        display: grid;
        grid-template-columns: minmax(0, 1fr) 260px;
        column-gap: 2.5rem;
        align-items: start;
    }
    .main-container.has-toc > .kb-article {
        grid-column: 1;
        max-width: none;
        margin: 0;
    }
    .main-container.has-toc > .kb-toc {
        display: block;
        grid-column: 2;
        grid-row: 1;
        position: sticky;
        top: 90px;
        max-height: calc(100vh - 110px);
        overflow-y: auto;
        background: var(--white, #fff);
        border-radius: 10px;
        box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        padding: 1.25rem 1.25rem 1rem;
        font-size: 0.9rem;
    }
    .kb-toc-title {
        font-weight: 700;
        color: var(--primary-color, #1e2a3a);
        margin-bottom: 0.75rem;
    }
    .kb-toc ol {
        list-style: none;
        margin: 0;
        padding: 0;
    }
    .kb-toc li {
        margin: 0;
    }
    .kb-toc a {
        display: block;
        padding: 0.4rem 0 0.4rem 0.75rem;
        border-left: 2px solid #e5e7eb;
        color: var(--text-color, #4b5563);
        text-decoration: none;
        line-height: 1.35;
    }
    .kb-toc a:hover {
        color: var(--secondary-color, #3b82f6);
    }
    .kb-toc a.active {
        border-left-color: var(--secondary-color, #3b82f6);
        color: var(--secondary-color, #3b82f6);
        font-weight: 600;
    }
}

/* ---------- Floating promo cards (built by js/site-layout.js) ----------
 * Fixed in the bottom-left corner, away from the email button, so they
 * never move the page. The script only shows them while the corner is free
 * of page content, and hides them on small screens. */
.side-ads {
    position: fixed;
    left: 24px;
    bottom: 24px;
    z-index: 998;
    display: flex;
    flex-direction: column;
    gap: 12px;
    width: var(--side-ad-size, 150px);
    opacity: 0;
    visibility: hidden;
    transform: translateX(-16px);
    transition: opacity 0.25s ease, transform 0.25s ease, visibility 0s linear 0.25s;
}
@media (max-width: 1699px) {
    .side-ads {
        --side-ad-size: 120px;
        gap: 10px;
    }
    .side-ad-title { font-size: 0.8rem; }
    .side-ad-tag { font-size: 0.6rem; }
    .side-ad-prompt { font-size: 0.62rem; top: 10px; left: 10px; right: 8px; }
    .side-ad-text { left: 10px; right: 10px; bottom: 9px; }
}
.side-ads.show {
    opacity: 1;
    visibility: visible;
    transform: none;
    transition: opacity 0.25s ease, transform 0.25s ease;
}
.side-ad {
    position: relative;
    display: block;
    width: var(--side-ad-size, 150px);
    height: var(--side-ad-size, 150px);
    border-radius: 14px;
    overflow: hidden;
    background: #1e2a3a;
    color: #fff;
    text-decoration: none;
    box-shadow: 0 8px 22px rgba(15, 26, 42, 0.25);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.side-ad:hover {
    transform: translateY(-3px);
    box-shadow: 0 14px 30px rgba(15, 26, 42, 0.32);
}
.side-ad img {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
}
.side-ad::after {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(to bottom, rgba(10, 16, 26, 0) 35%, rgba(10, 16, 26, 0.88) 100%);
}
.side-ad-text {
    position: absolute;
    left: 12px;
    right: 12px;
    bottom: 11px;
    z-index: 1;
    line-height: 1.2;
}
.side-ad-tag {
    display: block;
    font-size: 0.68rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: #7cc4ff;
    margin-bottom: 3px;
}
.side-ad-title {
    display: block;
    font-size: 0.95rem;
    font-weight: 700;
}
.side-ad-prompt {
    position: absolute;
    left: 12px;
    right: 12px;
    top: 14px;
    font-family: Consolas, 'Courier New', monospace;
    font-size: 0.72rem;
    line-height: 1.45;
    color: #9fe6a0;
}
.side-ad-prompt::after {
    content: '\2588';
    animation: side-ad-blink 1s steps(1) infinite;
}
@keyframes side-ad-blink {
    50% { opacity: 0; }
}
.side-ad--eda {
    background: radial-gradient(circle at 80% 10%, #2b4a6f 0%, #142033 65%);
}
.side-ads-close {
    align-self: flex-start;
    width: 24px;
    height: 24px;
    margin-bottom: -6px;
    border: none;
    border-radius: 50%;
    background: rgba(30, 42, 58, 0.75);
    color: #fff;
    font-size: 15px;
    line-height: 24px;
    padding: 0;
    cursor: pointer;
}
.side-ads-close:hover {
    background: #1e2a3a;
}
@media (prefers-reduced-motion: reduce) {
    .side-ad-prompt::after { animation: none; }
}
