/* Mobile overrides: active only on small screens via media query */
@media (max-width: 768px) {
    /* Prevent horizontal scrolling on mobile */
    html, body {
        max-width: 100%;
        overflow-x: hidden !important;
        box-sizing: border-box;
    }
    :root {
        --header-height: 64px;
    }

    /* Make page scroll vertically on mobile */
    body {
        display: block;
        overflow-x: hidden;
        width: 100%;
        height: auto;
    }

    /* Header becomes a top bar */
    header {
        width: 100% !important;
        height: var(--header-height) !important;
        position: fixed !important;
        top: 0;
        left: 0;
        right: 0;
        border-right: none !important;
        display: flex !important;
        flex-direction: row !important;
        align-items: center !important;
        justify-content: space-between !important;
        padding: 0 12px !important;
        background-color: rgba(255,255,255,0.96) !important;
        box-shadow: 0 2px 6px rgba(0,0,0,0.12);
        z-index: 200;
    }

    /* Reset rotated vertical menu to horizontal */
    nav ul {
        transform: none !important;
        flex-direction: row !important;
        writing-mode: horizontal-tb !important;
        align-items: center !important;
        gap: 8px;
    }

    nav ul li {
        margin: 0 6px !important;
    }

    nav ul li a {
        font-size: 0.95em !important;
        text-shadow: none !important;
        color: var(--text-gray) !important;
        writing-mode: horizontal-tb !important;
        display: inline-block;
        transform: none !important;
        padding: 6px 8px;
    }

    /* Keep main content below header */
    main {
        margin-left: 0;
        padding-top: calc(var(--header-height) + 8px);
        display: block !important;
    }

    .content {
        width: 100% !important;
        height: auto !important;
        display: block !important;
    }

    .page {
        width: 100% !important;
        height: auto !important;
        min-height: calc(100vh - var(--header-height));
        padding: 18px 12px 18px 12px; /* reduced padding to tighten stacked pages */
        box-sizing: border-box;
    }

    .page-header,
    .subjects-row {
        max-width: 100% !important;
        margin: 0 auto !important;
        box-sizing: border-box;
    }

    .subjects-row {
        flex-direction: column !important;
        gap: 16px !important;
        padding: 0 4px;
    }

    .subject-layout {
        width: 100% !important;
        gap: 12px !important;
        padding: 0 !important;
        display: flex !important;
        flex-direction: column !important;
        align-items: center !important;
        justify-content: flex-start !important;
    }

    /* Use full image height to avoid vertical crop, adjust container heights to balance visual size */
    .picture1 {
        width: 98vw !important;
        height: 80vw !important; /* base height for picture1 */
        max-width: 1100px;
        max-height: 80vh;
        margin: 0 auto 6px auto !important;
        background-repeat: no-repeat !important;
        background-size: auto 100% !important; /* fit image height to container to avoid vertical cropping */
        background-position: center top !important;
        box-sizing: border-box;
    }

    .picture2 {
        width: 98vw !important;
        height: 80vw !important; /* match picture1 height */
        max-width: 1100px;
        max-height: 80vh;
        margin: 0 auto 6px auto !important;
        background-repeat: no-repeat !important;
        background-size: auto 100% !important; /* fit image height to container to avoid vertical cropping */
        background-position: center 40% !important;
        box-sizing: border-box;
    }

    /* Prevent picture from being overlapped by text: ensure block flow */
    .picture1, .picture2 {
        display: block !important;
    }

    .intro {
        font-size: 1.05em !important;
        max-width: 100% !important;
        padding: 8px 6px !important;
    }

    .highlight, .highlight-dark, .highlight-gray {
        font-size: 1.4em !important;
    }

    .subject {
        max-width: 100% !important;
        font-size: 1.05em !important;
        padding: 8px 6px !important;
    }

    .page-buttons-row {
        position: static !important;
        top: auto !important;
        transform: none !important;
        display: flex !important;
        justify-content: center !important;
        gap: 12px !important;
        margin: 12px 0 0 0;
    }

    /* Remove page navigation arrows on mobile (incompatible) */
    .page-buttons-row { display: none !important; }

    .page-buttons-row#next-page { right: auto !important; }
    .page-buttons-row#prev-page { left: auto !important; }

    .page-button {
        width: 44px !important;
        height: 44px !important;
    }

    .professional-button {
        width: 100% !important;
        max-width: 420px;
        height: 48px !important;
        margin: 8px auto !important;
        display: block;
    }

    .text-professional-button, .text-page-button {
        font-size: 1em !important;
    }

    footer {
        position: static !important;
        padding: 18px 8px !important;
        background: var(--background-white);
        color: var(--text-gray);
        box-shadow: 0 -1px 4px rgba(0,0,0,0.04);
        z-index: 10;
    }

    /* Header hide/show on scroll: transition */
    header {
        transition: transform 200ms ease-in-out;
        will-change: transform;
        border-bottom: 1px solid var(--text-lightgray); /* thin delimiting line for mobile */
    }

    header.hide-header {
        transform: translateY(-110%);
    }

    /* Mobile top container styling to align hamburger correctly */
    .mobile-top {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 24px 0 12px; /* leave extra space on right so hamburger isn't flush to edge */
        box-sizing: border-box;
    }

    .mobile-hamburger { margin: 0; }

    /* Keep nav dropdown within viewport */
    body.mobile-menu-open nav ul {
        right: 12px;
        left: auto;
        min-width: 160px;
    }

    /* Ensure professional button stays centered */
    .professional-button {
        width: 100% !important;
        max-width: 420px;
        height: 48px !important;
        margin: 8px auto !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        text-decoration: none;
    }

    .professional-button .text-professional-button {
        margin: 0 !important;
        text-align: center !important;
        width: 100%;
    }

    /* Reduce vertical gaps between picture and text to avoid large empty space */
    .subject-layout { gap: 8px !important; }
    .subject { margin-top: 0 !important; padding-top: 6px !important; }

    /* Hide redundant page title on second page for professional view */
    .page#second-page .page-header { display: none !important; }

    /* Reduce spacing between stacked pages: let pages size to content except first page */
    .page { min-height: auto !important; margin-bottom: 4px !important; }
    .page:not(:first-of-type) { margin-top: 2px !important; }
    .page:first-of-type { min-height: calc(100vh - var(--header-height)) !important; position: relative; }

    /* Ensure footer is visible and placed after content */
    footer {
        position: static !important;
        display: block !important;
        margin-top: 12px !important;
        padding: 18px 8px !important;
        background: var(--background-white);
        color: var(--text-gray);
        box-shadow: 0 -1px 4px rgba(0,0,0,0.04);
        z-index: 10;
    }

    /* Footer positioned inside first page should stick to its bottom and be transparent */
    footer.mobile-in-first {
        position: absolute !important;
        bottom: 0 !important;
        left: 0 !important;
        width: 100% !important;
        background: transparent !important;
        color: var(--text-white) !important;
        box-shadow: none !important;
        padding: 18px 12px !important;
        z-index: 50;
    }

    /* Adjust intro positioning on the first page */
    .page#first-page {
        padding-top: calc(var(--header-height) + 12px) !important;
        padding-bottom: calc(18px + 64px) !important; /* leave space for footer inside first page */
        display: flex !important;
        flex-direction: column !important;
        justify-content: center !important;
        align-items: center !important;
        box-sizing: border-box;
    }

    .page#first-page .intro {
        font-size: 1.12em !important; /* slightly larger */
        max-width: 92% !important;
        margin: 6px auto !important;
        line-height: 1.45 !important;
        text-align: center !important; /* center the intro text again */
    }

    /* Make images, icons and other fixed-size items responsive */
    #preloader img { width: 48px; height: 48px; }

    /* Shift the page background image slightly to the right on mobile for better composition */
    body.mainbody::before {
        background-position: right center !important;
        background-size: cover !important;
    }

    /* Small tweaks for very small screens */
    @media (max-width: 360px) {
        .picture1, .picture2 { height: 60vw !important; }
        .intro { font-size: 1em !important; }
    }
}

/* Mobile-specific hamburger menu styles */
@media (max-width: 768px) {
    .mobile-hamburger {
        display: inline-flex;
        width: 38px;
        height: 28px;
        flex-direction: column;
        justify-content: space-between;
        align-items: center;
        background: transparent;
        border: none;
        padding: 4px;
        cursor: pointer;
    }

    .mobile-hamburger span {
        display: block;
        width: 100%;
        height: 3px;
        background: var(--text-gray);
        border-radius: 2px;
    }

    .mobile-section-name {
        display: inline-block;
        color: var(--text-gray);
        font-weight: bold;
        font-size: 0.95em;
        margin-left: 8px;
    }

    /* Hide original rotated menu until opened */
    nav ul { display: none !important; }

    /* When menu open, show vertical dropdown */
    body.mobile-menu-open nav ul {
        display: flex !important;
        position: absolute !important;
        top: calc(var(--header-height));
        right: 12px;
        background: rgba(255,255,255,0.98);
        border: 1px solid var(--text-lightgray);
        padding: 8px 12px;
        flex-direction: column !important;
        gap: 8px;
        box-shadow: 0 6px 18px rgba(0,0,0,0.12);
        z-index: 300;
    }

    body.mobile-menu-open nav ul li a {
        color: var(--text-gray) !important;
        writing-mode: horizontal-tb !important;
        transform: none !important;
        padding: 6px 8px;
    }

    /* Reset hamburger and section name to none on larger screens */
}
