/* =========================================================
   AUTO SHADING UPI - GLOBAL MOBILE HEADER / TOPBAR PATCH
   Desktop is untouched. Mobile <= 768px only.
   ========================================================= */

@media (max-width: 768px) {
    html, body {
        max-width: 100%;
        overflow-x: hidden;
    }

    /* Main application topbar */
    .topbar {
        min-height: 104px;
        height: auto;
        box-sizing: border-box;
        padding: 10px 14px 11px;
        display: grid;
        grid-template-columns: 44px minmax(0, 1fr) auto;
        grid-template-rows: auto auto;
        column-gap: 9px;
        row-gap: 8px;
        align-items: center;
    }

    /* Hamburger stays compact */
    .menu-toggle,
    .mobile-menu-btn,
    .sidebar-toggle {
        grid-column: 1;
        grid-row: 1 / span 2;
        align-self: start;
        margin-top: 3px;
        width: 40px;
        min-width: 40px;
        height: 40px;
        padding: 0;
    }

    /* Title area gets the full useful width */
    .topbar-title,
    .page-title-wrap,
    .topbar-heading {
        grid-column: 2 / 4;
        grid-row: 1;
        min-width: 0;
        width: 100%;
        overflow: hidden;
    }

    .topbar-title span,
    .page-title-wrap > span,
    .topbar-heading > span {
        display: block;
        margin: 0 0 2px;
        font-size: 7px;
        line-height: 1.2;
        letter-spacing: .75px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .topbar-title h1,
    .page-title-wrap h1,
    .topbar-heading h1 {
        margin: 0;
        max-width: 100%;
        font-size: 21px;
        line-height: 1.12;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    /* Hide current-time box on phones: realtime time is not worth
       sacrificing navigation/header space. */
    .clock-box {
        display: none !important;
    }

    /* User information lives on the second row */
    .user-chip {
        grid-column: 2;
        grid-row: 2;
        display: flex !important;
        min-width: 0;
        max-width: 100%;
        flex-direction: row;
        align-items: center;
        justify-content: flex-start;
        gap: 7px;
        padding: 0;
        overflow: hidden;
    }

    .user-chip span {
        min-width: 0;
        max-width: 150px;
        font-size: 9px;
        line-height: 1.2;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .user-chip b {
        flex: 0 0 auto;
        padding: 3px 6px;
        border-radius: 999px;
        background: #eef3f7;
        font-size: 7px;
        line-height: 1;
        letter-spacing: .5px;
    }

    .logout-form {
        grid-column: 3;
        grid-row: 2;
        justify-self: end;
        margin: 0;
    }

    .logout-form button {
        min-height: 30px;
        padding: 6px 9px;
        border-radius: 7px;
        font-size: 7px;
        line-height: 1;
        white-space: nowrap;
    }

    /* Main content must start cleanly below the topbar */
    .main-content,
    .content,
    .page-content {
        min-width: 0;
    }

    /* Common page header / panel safety */
    .page-header,
    .panel-head {
        min-width: 0;
    }

    .panel-head {
        gap: 10px;
    }

    .panel-head h1,
    .panel-head h2,
    .panel-head p {
        overflow-wrap: anywhere;
    }

    /* Prevent cards/tables/forms from forcing horizontal page overflow */
    .panel,
    .card,
    .table-wrap,
    .management-table,
    .form-grid {
        max-width: 100%;
        box-sizing: border-box;
    }

    .table-wrap {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
}

@media (max-width: 430px) {
    .topbar {
        grid-template-columns: 40px minmax(0, 1fr) auto;
        padding-left: 11px;
        padding-right: 11px;
        column-gap: 7px;
    }

    .topbar-title h1,
    .page-title-wrap h1,
    .topbar-heading h1 {
        font-size: 19px;
    }

    .user-chip span {
        max-width: 125px;
        font-size: 8px;
    }

    .logout-form button {
        padding-left: 7px;
        padding-right: 7px;
    }
}

@media (max-width: 360px) {
    .topbar {
        grid-template-columns: 38px minmax(0, 1fr) auto;
        min-height: 98px;
        padding: 9px 9px 10px;
        column-gap: 6px;
    }

    .topbar-title h1,
    .page-title-wrap h1,
    .topbar-heading h1 {
        font-size: 18px;
    }

    .user-chip span {
        max-width: 100px;
    }

    .user-chip b {
        padding-left: 5px;
        padding-right: 5px;
    }
}

/* Landscape phones: keep header compact but never overlap content */
@media (max-width: 900px) and (max-height: 500px) and (orientation: landscape) {
    .topbar {
        min-height: 82px;
        grid-template-rows: auto auto;
        padding-top: 7px;
        padding-bottom: 7px;
        row-gap: 4px;
    }

    .topbar-title h1,
    .page-title-wrap h1,
    .topbar-heading h1 {
        font-size: 18px;
    }
}
