/* Shared small-screen refinements, loaded after each page's desktop styles. */
@media (max-width: 768px) {
    *, *::before, *::after { box-sizing: border-box; }
    body input:not([type="checkbox"]):not([type="radio"]),
    body select, body textarea { font-size: 16px; min-width: 0; max-width: 100%; }
    button, a, input, select, textarea { touch-action: manipulation; }
    body button, body .primary-btn, body .secondary-btn { min-height: 44px; }
    body :focus-visible { outline: 3px solid #238acb; outline-offset: 3px; }

    body:is(.home-page, .services-page, .profile-page) .home-nav {
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto;
        gap: 10px;
        align-items: center;
    }
    body:is(.home-page, .services-page, .profile-page) .home-brand { grid-column: 1 / -1; }
    body:is(.home-page, .services-page, .profile-page) .home-brand-title { font-size: 0.95rem; line-height: 1.35; }
    body:is(.home-page, .services-page, .profile-page) .home-brand-subtitle { font-size: 0.75rem; }
    body:is(.home-page, .services-page, .profile-page) .home-nav-links {
        display: grid;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        margin: 0;
        padding: 3px;
        gap: 2px;
        min-width: 0;
    }
    body:is(.home-page, .services-page, .profile-page) .home-nav-links a {
        display: flex;
        align-items: center;
        justify-content: center;
        min-height: 44px;
        padding: 8px 4px;
        font-size: 0.875rem;
    }
    body .home-nav-actions { width: auto; margin: 0; gap: 6px; }
    body .home-nav-actions button { width: 44px; height: 44px; flex-shrink: 0; padding: 0; }
    body.dark-mode:is(.home-page, .services-page, .profile-page) .home-nav-links {
        background: #17324f;
        border-color: rgba(145, 196, 246, 0.32);
    }
    body.dark-mode:is(.home-page, .services-page, .profile-page) .home-nav-links a {
        background: #294966;
        color: #f3f8ff;
    }
    body.home-page .copy { font-size: 1rem; line-height: 1.65; }
    body.home-page .hero-box h1 { line-height: 1.15; }
    body.home-page .home-story > *, body.profile-page .profile-dashboard > *,
    body.profile-page .profile-card, body.profile-page .profile-detail-chip { min-width: 0; overflow-wrap: anywhere; }
    body.services-page .card-container {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 16px;
    }
    body.services-page .promo-card, body.services-page .service-card {
        width: 100%; min-height: 0; padding: 22px 18px; height: auto;
    }
    body.services-page .promo-card h3, body.services-page .service-card h3 { font-size: 1.15rem; line-height: 1.35; }
    body.services-page .includes, body.services-page .service-info { font-size: 0.95rem; line-height: 1.6; }
    body.services-page .price { font-size: 1.15rem; }
    body .booking-form-grid { grid-template-columns: minmax(0, 1fr); }
    body .modal-content.large, body .service-modal-content, body .booking-modal-content, body .app-dialog {
        max-width: 100%; max-height: calc(100dvh - 32px); overflow-y: auto; overscroll-behavior: contain;
    }
    body .close-btn, body .app-dialog-close { min-width: 44px; min-height: 44px; display: grid; place-items: center; }
    body .message-fab { right: 16px; bottom: max(16px, env(safe-area-inset-bottom)); width: 52px; height: 52px; }
    body .user-message-drawer, body .user-notification-drawer {
        right: 12px; width: calc(100% - 24px); max-height: calc(100dvh - 150px);
        overflow-y: auto; overscroll-behavior: contain;
    }
    body .user-message-drawer { bottom: 80px; height: min(600px, calc(100dvh - 150px)); grid-template-rows: auto minmax(0, 1fr) auto; overflow: hidden; }
    body .user-message-drawer.is-composing { right: 8px; width: calc(100% - 16px); padding: 12px; gap: 8px; }
    body .user-message-drawer .user-drawer-messages-list { min-height: 0; max-height: none; overflow-y: auto; }
    body .user-message-drawer .user-drawer-composer { grid-template-columns: minmax(0, 1fr) 44px; }
    body.profile-page .profile-timeline-item { align-items: flex-start; }
    body.profile-page .profile-timeline-item > div { flex: 1 1 auto; min-width: 0; }
    body.profile-page .profile-timeline-item > span:last-child { flex: 0 0 auto; white-space: nowrap; overflow-wrap: normal; font-size: 0.82rem; }
    body .user-notification-drawer { top: 124px; grid-template-rows: auto auto minmax(0, 1fr); }

    /* Login, registration, password reset and policy pages. */
    body .content, body .right, body .right form { min-width: 0; }
    body nav a, body nav .login-btn { min-height: 44px; display: flex; align-items: center; justify-content: center; }
    body .policy-main { margin: 24px auto; }
    body .policy-document { padding: 24px 18px; overflow-wrap: anywhere; }
    body .policy-document h1 { font-size: clamp(1.6rem, 7vw, 2.2rem); line-height: 1.2; }
    body .policy-modal { padding: 12px; }
    body .policy-modal-dialog { height: calc(100dvh - 24px); }

    /* Keep the admin content within the screen; wide tables scroll locally. */
    body .main, body .analytics-grid > *, body .lab-detail-workspace > * { min-width: 0; }
    body .sidebar-menu { max-height: 240px; overflow-y: auto; overscroll-behavior: contain; }
    body .lab-detail-dialog { max-height: calc(100dvh - 28px); overflow-y: auto; }
}

/* Service details should scroll as one panel, without a light sticky footer in dark mode. */
@media (max-width: 768px) {
    body.services-page #serviceModal {
        height: 100dvh;
        padding: 12px 10px calc(16px + env(safe-area-inset-bottom));
        overflow-y: auto;
        overscroll-behavior: contain;
        box-sizing: border-box;
    }

    body.services-page #serviceModal .service-modal-content {
        position: relative;
        top: auto;
        transform: none;
        width: min(100%, 560px);
        max-height: none;
        margin: 0 auto;
        padding: 22px 16px calc(20px + env(safe-area-inset-bottom));
        overflow: visible;
        box-sizing: border-box;
    }

    body.services-page #serviceModal .service-booking-actions {
        position: static;
        bottom: auto;
        display: grid;
        grid-template-columns: minmax(0, 1fr);
        gap: 10px;
        margin: 20px 0 0;
        padding: 0;
        background: transparent;
    }

    body.services-page #serviceModal .service-booking-actions button {
        width: 100%;
        min-height: 48px;
        margin: 0;
        border-radius: 12px;
    }
}

@media (max-width: 440px) {
    body.profile-page .profile-timeline-item { flex-direction: column; gap: 8px; align-items: stretch; }
    body.profile-page .profile-timeline-item > span:last-child { align-self: flex-start; }
}

@media (max-width: 560px) {
    body.services-page .card-container,
    body.services-page .section:first-of-type .card-container {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 8px;
    }
    body.services-page .promo-card,
    body.services-page .service-card {
        min-height: 174px;
        padding: 11px 9px;
        gap: 5px;
        border-radius: 15px;
    }
    body.services-page .promo-card .icon,
    body.services-page .service-card .icon {
        width: 42px;
        height: 42px;
        margin: 0 0 5px;
        border-radius: 12px;
        font-size: 1rem;
    }
    body.services-page .promo-card h3,
    body.services-page .service-card h3 {
        margin: 2px 0;
        font-size: 0.78rem;
        line-height: 1.2;
    }
    body.services-page .service-focus {
        width: 100%;
        margin: 1px 0;
        padding: 4px 6px;
        font-size: 0.56rem;
        letter-spacing: 0.05em;
    }
    body.services-page .includes,
    body.services-page .service-info {
        display: -webkit-box;
        overflow: hidden;
        margin: 2px 0;
        font-size: 0.66rem;
        line-height: 1.3;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
    }
    body.services-page .price { margin-top: auto; font-size: 0.82rem; }
    body.home-page .hero-box .primary-btn { display: flex; justify-content: center; width: 100%; }
    body.profile-page .profile-stats, body.profile-page .profile-detail-grid { grid-template-columns: minmax(0, 1fr); }
    body .right form { padding: 24px 18px; }
}

@media (max-width: 768px) {
    body.home-page .lab-carousel-dots button {
        min-height: 7px;
    }

    body.home-page .lab-carousel-dots button.is-active {
        min-height: 7px;
    }
}

/* Authoritative phone layout loaded after desktop styles. */
@media (max-width: 640px) {
    body .content.centered .right form,
    body .right form {
        padding: 20px 16px;
    }

    body .login-role-chooser > .role-choice-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 10px;
    }

    body .role-login-btn,
    body .role-login-btn[data-role="user"] {
        min-height: 104px;
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        justify-content: center;
        gap: 9px;
        padding: 13px;
    }
}

@media (max-width: 560px) {
    body.services-page section.service-photo-section .card-container,
    body.services-page section.service-photo-section:first-of-type .card-container,
    body.services-page section.affordable-packages .card-container {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 10px;
    }

    body.services-page section.service-photo-section .service-card {
        min-height: 286px;
        padding: 0 10px 12px;
        gap: 4px;
    }

    body.services-page section.service-photo-section .service-card::before {
        flex-basis: 104px;
        width: calc(100% + 20px);
        height: 104px;
        margin: 0 -10px 6px;
    }

    body.services-page section.service-photo-section .service-card .service-info,
    body.services-page section.service-photo-section .service-card .includes {
        display: -webkit-box;
        overflow: hidden;
        font-size: .66rem;
        line-height: 1.35;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 3;
    }

    body.services-page section.affordable-packages .promo-card {
        min-width: 0;
        min-height: 0;
        display: flex;
        flex-direction: column;
        padding: 0;
    }

    body.services-page section.affordable-packages .package-card-media {
        width: 100%;
        min-height: 106px;
        height: 106px;
    }

    body.services-page section.affordable-packages .package-card-content {
        padding: 11px 10px 12px;
    }
}

@media (max-width: 350px) {
    body:is(.home-page, .services-page, .profile-page) .home-nav { column-gap: 5px; }
    body .home-nav-actions { gap: 2px; }
    body:is(.home-page, .services-page, .profile-page) .home-nav-links a { font-size: 0.8rem; }
}

/* Final services layout: stable, readable photo cards on phones. */
@media (max-width: 560px) {
    html:has(body.services-page),
    body.services-page {
        width: 100%;
        min-width: 0;
        overflow-x: clip;
        overscroll-behavior-x: none;
        touch-action: pan-y pinch-zoom;
    }

    body.services-page section.service-photo-section .card-container,
    body.services-page section.service-photo-section:first-of-type .card-container {
        display: grid;
        grid-template-columns: minmax(0, 1fr);
        row-gap: 22px;
    }

    body.services-page section.service-photo-section .promo-card,
    body.services-page section.service-photo-section .service-card,
    body.services-page section.service-photo-section .promo-card:hover,
    body.services-page section.service-photo-section .service-card:hover,
    body.services-page section.service-photo-section .promo-card:focus-visible,
    body.services-page section.service-photo-section .service-card:focus-visible {
        width: 100%;
        transform: none;
        touch-action: manipulation;
        -webkit-user-drag: none;
        user-select: none;
    }

    body.services-page section.affordable-packages .promo-card {
        display: block;
        padding: 0;
    }

    body.services-page section.affordable-packages .package-card-media {
        width: 100%;
        min-height: 170px;
        height: 170px;
        flex-basis: auto;
    }

    body.services-page section.affordable-packages .package-card-content {
        padding: 18px 18px 20px;
    }

    body.services-page section.service-photo-section .service-card .service-info,
    body.services-page section.service-photo-section .service-card .includes {
        display: block;
        overflow: visible;
        color: #536b80;
        font-size: .82rem;
        line-height: 1.48;
        opacity: 1;
        -webkit-line-clamp: initial;
        -webkit-text-fill-color: #536b80;
    }

    body.services-page section.service-photo-section img {
        pointer-events: none;
        transform: none;
        -webkit-user-drag: none;
        user-select: none;
    }
}

/* Keep these last: final phone presentation overrides. */
@media (max-width: 640px) {
    body .content.centered .right form,
    body .right form {
        padding: 20px 16px;
    }

    body .login-role-chooser > .role-choice-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 10px;
    }

    body .role-login-btn,
    body .role-login-btn[data-role="user"] {
        min-height: 104px;
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        justify-content: center;
        gap: 9px;
        padding: 13px;
    }
}

@media (max-width: 560px) {
    body.services-page section.service-photo-section .card-container,
    body.services-page section.service-photo-section:first-of-type .card-container,
    body.services-page section.affordable-packages .card-container {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 10px;
    }

    body.services-page section.service-photo-section .service-card {
        min-height: 286px;
        padding: 0 10px 12px;
        gap: 4px;
    }

    body.services-page section.service-photo-section .service-card::before {
        flex-basis: 104px;
        width: calc(100% + 20px);
        height: 104px;
        margin: 0 -10px 6px;
    }

    body.services-page section.service-photo-section .service-card .service-info,
    body.services-page section.service-photo-section .service-card .includes {
        display: -webkit-box;
        overflow: hidden;
        font-size: .66rem;
        line-height: 1.35;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 3;
    }

    body.services-page section.affordable-packages .promo-card {
        min-width: 0;
        min-height: 0;
        display: flex;
        flex-direction: column;
        padding: 0;
    }

    body.services-page section.affordable-packages .package-card-media {
        width: 100%;
        min-height: 106px;
        height: 106px;
    }

    body.services-page section.affordable-packages .package-card-content {
        padding: 11px 10px 12px;
    }
}
