/*
 * Shared phone & tablet fixes, loaded after each layout's own styles.
 * Keeps every page usable from ~320px (small Android) up to tablets,
 * including iPhone notches and the iOS "zoom on input focus" behaviour.
 */

html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

img, video, canvas, svg, iframe {
    max-width: 100%;
}

/* Wide tables scroll sideways inside their box instead of stretching the page */
.table-responsive {
    -webkit-overflow-scrolling: touch;
}

/* ---------- Tablets and phones ---------- */
@media (max-width: 768px) {
    /* Last-resort guard against sideways wobble; "clip" keeps sticky headers working */
    html, body {
        overflow-x: clip;
    }

    /* iOS zooms the page when a field under 16px gets focus */
    input:not([type=checkbox]):not([type=radio]):not([type=range]),
    select,
    textarea,
    .form-control,
    .form-select {
        font-size: 16px !important;
    }

    /* Top header (app layout): title shrinks, icons always stay on screen */
    .top-header {
        gap: 0.5rem;
        padding-left: max(0.75rem, env(safe-area-inset-left)) !important;
        padding-right: max(0.75rem, env(safe-area-inset-right)) !important;
    }
    .top-header .page-title {
        flex: 1 1 auto;
        min-width: 0;
        gap: 0.5rem;
    }
    .top-header .page-title h1 {
        flex: 1 1 auto;
        min-width: 0;
        max-width: none;
        font-size: 1rem;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    .top-header .hamburger-btn {
        margin-right: 0 !important;
    }
    .top-header .back-to-dashboard {
        width: 36px;
        height: 36px;
        flex-shrink: 0;
        border-radius: 10px;
    }
    .top-header .page-title .badge {
        flex-shrink: 0;
        white-space: nowrap;
        padding: 0.2rem 0.55rem;
        font-size: 0.7rem;
    }
    .top-header .header-actions {
        flex-shrink: 0;
        gap: 0.4rem;
    }
    .top-header .notification-bell {
        margin-right: 0;
        padding: 8px 10px;
    }
    .top-header .user-dropdown {
        padding: 0.25rem;
        gap: 0;
        background: transparent;
    }
    .top-header .user-avatar {
        width: 34px;
        height: 34px;
    }

    /* Announcements panel becomes a full-width sheet under the header */
    .notification-dropdown {
        position: fixed !important;
        top: var(--header-height, 64px) !important;
        left: 0 !important;
        right: 0 !important;
        width: 100% !important;
        max-height: 70vh;
        border-radius: 0 0 16px 16px !important;
    }

    /* Modals: use the full width of the phone */
    .modal-dialog {
        margin: 0.5rem;
    }

    /* Buttons and links easier to tap */
    .btn {
        min-height: 40px;
    }
    .btn-sm {
        min-height: 34px;
    }

    /* Group / item pick lists (Estimate, Workslip, Temporary Works, AMC):
       bigger text and touch targets */
    .groups-list .group-link,
    .items-list .item-label {
        font-size: 0.85rem;
        padding: 0.55rem 0.6rem;
        min-height: 40px;
    }
    .groups-list .group-link *,
    .items-list .item-name {
        font-size: inherit;
    }
    .items-list .item-checkbox {
        width: 18px;
        height: 18px;
    }

    /* "Select type" popup for grouped items: readable and easy to tap */
    .subtypes-popup-header {
        font-size: 0.85rem !important;
        padding: 0.55rem 0.75rem !important;
    }
    .subtypes-popup-close {
        width: 28px !important;
        height: 28px !important;
        font-size: 1.2rem !important;
    }
    .subtypes-popup,
    .subtypes-popup-body {
        max-height: 60vh !important;
    }
    .subtypes-popup-body {
        max-height: calc(60vh - 44px) !important;
    }
    .subtype-option {
        padding: 0.6rem 0.65rem !important;
        min-height: 42px;
    }
    .subtype-option .subtype-name {
        font-size: 0.85rem !important;
    }

    /* SOR rates bar above the pick lists */
    .backend-selection-bar > .d-flex {
        gap: 0.5rem !important;
    }
    .backend-selection-bar #backend-selector {
        max-width: 100% !important;
        flex: 1 1 100%;
    }
    .backend-selection-bar .text-muted.small {
        flex-basis: 100%;
    }
    .backend-selection-bar .btn-ribbon-upload {
        flex: 1 1 140px;
        margin-left: 0 !important;
        justify-content: center;
    }

    .content-footer {
        flex-direction: column;
        gap: 0.5rem;
        text-align: center;
        padding: 1rem;
    }
}

/* ---------- Small phones ---------- */
@media (max-width: 480px) {
    /* The title and menu already explain where you are */
    .top-header .page-title .badge {
        display: none;
    }

    .content-area {
        padding: 0.75rem !important;
    }

    .modal-dialog:not(.modal-sm) {
        margin: 0.25rem;
    }

    /* Step progress bars (1 → 2 → 3) fit on one line */
    .step-indicator {
        gap: 0.25rem !important;
    }
    .step-indicator .step {
        padding: 0.4rem 0.6rem;
        gap: 0.3rem;
        font-size: 0.75rem;
        flex-shrink: 1;
        min-width: 0;
        white-space: nowrap;
    }
    .step-indicator .step-connector {
        flex: 1 1 8px;
        width: auto !important;
        min-width: 6px;
        max-width: 40px;
        margin: 0 !important;
    }

    h1, .h1 { font-size: 1.5rem; }
    h2, .h2 { font-size: 1.3rem; }
    h3, .h3 { font-size: 1.15rem; }
}

/* Room for the iPhone home bar at the bottom of the page */
@supports (padding: env(safe-area-inset-bottom)) {
    body {
        padding-bottom: env(safe-area-inset-bottom);
    }
}
