@media (max-width: 640px) {
    :root {
        --inset-top: env(safe-area-inset-top, 0px);
        --inset-bottom: env(safe-area-inset-bottom, 0px);
        --header-pad: calc(8px + var(--inset-top));
        --header-block: calc(var(--header-pad) + 44px + 8px);
    }

    html[data-sim-safe-area] {
        --inset-top: var(--sim-safe-top, env(safe-area-inset-top, 0px));
        --inset-bottom: var(--sim-safe-bottom, env(safe-area-inset-bottom, 0px));
    }

    html, body { overflow-x: hidden; }
    body {
        padding-left: max(0px, env(safe-area-inset-left));
        padding-right: max(0px, env(safe-area-inset-right));
    }

    .toolbar {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        height: auto !important;
        min-height: var(--header-block);
        padding: var(--header-pad) 12px 8px !important;
        box-sizing: border-box;
        z-index: 1000;
        background: #f5f5f5;
        border-bottom: 1px solid #ddd;
        display: flex;
        align-items: center;
    }

    body.mobile-vv-pinned .toolbar {
        left: var(--mobile-vv-left, 0);
        width: var(--mobile-vv-width, 100%);
    }

    .toolbar-left .toggle-sidebar,
    .toolbar-center,
    .toolbar-right .user-badge,
    .toolbar-right form[action="/logout"],
    #builder-presets,
    .results-toolbar,
    .sidebar,
    .resizer,
    #autowrap-toggle { display: none !important; }

    .main-container {
        flex: 1;
        min-height: 0;
        margin-top: var(--header-block);
    }

    .logo {
        font-size: 15px;
        margin-left: 0;
        max-width: 42vw;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .toolbar-right { gap: 8px; flex-shrink: 0; }
    .toolbar-right:not(:has(#mobile-menu-btn))::before { content: ''; width: 44px; height: 44px; }

    #mobile-menu-btn {
        display: flex;
        align-items: center;
        justify-content: center;
        min-width: 44px;
        min-height: 44px;
        padding: 0 10px;
        border: 1px solid #ddd;
        border-radius: 8px;
        background: #fff;
        font-size: 22px;
        line-height: 1;
    }

    #execute-btn {
        width: 6.75em;
        min-height: 44px;
        padding: 8px 0;
        font-size: 15px;
        border-radius: 8px;
        white-space: nowrap;
        overflow: hidden;
    }
    .toolbar-right:not(:has(#mobile-menu-btn)) #execute-btn { color: transparent; }

    #builder-panel:not(:has(#mobile-quick-search))::before { content: ''; flex: none; height: 214px; }

    .mobile-quick-search { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }

    .mobile-preset-tabs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }

    .mobile-preset-tab {
        min-height: 44px;
        border: 1px solid #cfd8e6;
        border-radius: 8px;
        background: #eef3fa;
        font-size: 14px;
        font-weight: 600;
    }

    .mobile-preset-tab-active { background: #0078d7; border-color: #0078d7; color: #fff; }

    .mobile-quick-label { font-size: 13px; color: #555; font-weight: 600; }

    #mobile-quick-input {
        width: 100%;
        box-sizing: border-box;
        min-height: 48px;
        font-size: 16px;
        padding: 10px 12px;
        border: 1px solid #ccc;
        border-radius: 8px;
    }

    .mobile-recent-chips { display: flex; flex-wrap: wrap; gap: 6px; }

    .mobile-recent-chip {
        font-size: 13px;
        padding: 6px 10px;
        border-radius: 999px;
        border: 1px solid #cfd8e6;
        background: #fff;
        max-width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .mobile-advanced-details {
        border: 1px solid #d7dee8;
        border-radius: 8px;
        padding: 8px 10px;
        background: #fafbfd;
        margin-bottom: 8px;
    }

    .mobile-advanced-details > summary {
        font-weight: 600;
        font-size: 14px;
        list-style: none;
        display: flex;
        align-items: center;
        gap: 6px;
        cursor: pointer;
    }

    .mobile-advanced-details > summary::-webkit-details-marker { display: none; }

    .mobile-advanced-sub { font-weight: normal; font-size: 12px; color: #666; flex: 1; }

    #builder-panel:not([style*="none"]) ~ #sql-preview-label,
    #builder-panel:not([style*="none"]) ~ #sql-preview { display: none !important; }

    .mode-tab { min-height: 44px; font-size: 15px; flex: 1; }

    #expert-panel:not([style*="none"]) ~ #sql-preview,
    #mobile-advanced-details[open] + #builder-root select,
    #mobile-advanced-details[open] + #builder-root input { font-size: 16px; min-height: 44px; }

    #mobile-advanced-details[open] + #builder-root .builder-condition { grid-template-columns: 1fr auto; }
    #mobile-advanced-details[open] + #builder-root .builder-condition > :not(:first-child):not(.builder-btn-danger) { grid-column: 1 / -1; }
    #mobile-advanced-details[open] + #builder-root .builder-condition > .builder-btn-danger { grid-column: 2; grid-row: 1; }

    #builder-root { display: none; }
    #mobile-advanced-details[open] + #builder-root { display: block; }

    #builder-panel:not(:has(#mobile-advanced-details[open])) .builder-toolbar { display: none; }
    .builder-toolbar select,
    .builder-toolbar input { font-size: 16px; min-height: 44px; }

    .query-container { min-height: 0 !important; height: auto !important; }
    .query-container:has(#mobile-advanced-details[open]) { max-height: 38vh; }

    body.mobile-has-results .query-container { max-height: 18vh !important; }
    body.mobile-has-results .mobile-recent-chips { display: none; }
    body.mobile-has-results .mobile-preset-tab { min-height: 36px; font-size: 12px; padding: 4px 2px; }
    body.mobile-has-results #mobile-quick-input { min-height: 40px; padding: 8px 10px; }
    body.mobile-has-results .mobile-advanced-details > summary { font-size: 13px; }

    .table-container .results-table { display: none; }

    .mobile-results-list { display: flex; flex-direction: column; gap: 8px; }

    .mobile-result-card { border: 1px solid #ddd; border-radius: 8px; background: #fff; overflow: hidden; }
    .mobile-result-card-expanded { border-color: #0078d7; box-shadow: 0 0 0 1px #0078d7; }

    .mobile-result-card-head {
        display: block;
        width: 100%;
        text-align: left;
        border: none;
        background: transparent;
        padding: 12px;
        font: inherit;
        color: inherit;
    }

    .mobile-result-line1 {
        display: flex;
        justify-content: space-between;
        gap: 8px;
        align-items: flex-start;
        margin-bottom: 4px;
    }

    .mobile-result-desc {
        font-weight: 700;
        font-size: 14px;
        line-height: 1.3;
        flex: 1;
        overflow: hidden;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
    }

    .mobile-result-fob { font-weight: 700; font-size: 14px; white-space: nowrap; }
    .mobile-result-fob-unit { display: block; font-size: 11px; color: #666; font-weight: 500; text-align: right; }
    .mobile-result-line2 { font-size: 12px; color: #666; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

    .mobile-result-details { display: none; padding: 0 12px 12px; border-top: 1px dashed #ddd; }
    .mobile-result-card-expanded .mobile-result-details {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 10px;
        padding-top: 10px;
    }

    .mobile-result-field-wide { grid-column: 1 / -1; }
    .mobile-result-field-label { font-size: 10px; text-transform: uppercase; color: #888; }
    .mobile-result-field-value { font-size: 14px; word-break: break-word; }
    .mobile-result-less { grid-column: 1 / -1; color: #0078d7; font-size: 14px; padding-top: 4px; text-align: center; }

    .mobile-sort-bar { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; font-size: 13px; }
    .mobile-sort-bar > span { flex: 1; }
    #mobile-sort-select,
    #mobile-sort-dir {
        font-size: 16px;
        min-height: 44px;
        border-radius: 8px;
        border: 1px solid #ccc;
        background: #fff;
    }
    #mobile-sort-select { padding: 6px 10px; max-width: 55%; }
    #mobile-sort-dir { min-width: 44px; }

    #mobile-menu[hidden] { display: none; }

    .mobile-menu-backdrop {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        top: var(--header-block);
        background: rgba(0, 0, 0, 0.35);
        z-index: 1100;
    }
    .mobile-menu-sheet {
        position: fixed;
        top: var(--header-block);
        right: 12px;
        width: min(320px, calc(100vw - 24px));
        max-height: calc(100vh - 80px - var(--inset-top) - var(--inset-bottom));
        overflow: auto;
        background: #fff;
        border-radius: 12px;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
        z-index: 1101;
        padding: 8px 0;
    }

    .mobile-menu-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        width: 100%;
        text-align: left;
        border: none;
        background: transparent;
        padding: 12px 16px;
        font-size: 16px;
        min-height: 44px;
        box-sizing: border-box;
    }

    .mobile-menu-item:disabled { opacity: 0.45; }
    .mobile-menu-item-sub { font-size: 12px; color: #888; }
    .mobile-menu-section { padding: 8px 16px 4px; font-size: 11px; text-transform: uppercase; color: #888; font-weight: 600; }
    .mobile-menu-export { color: #2e7d32; }
    .mobile-menu-divider { height: 1px; background: #eee; margin: 4px 0; }

    .status-bar { padding-bottom: calc(4px + var(--inset-bottom)); }

    .login-card { margin: 16px; padding: 24px; max-width: none; }
    .login-card input[type="text"], .login-card input[type="password"] {
        font-size: 16px !important;
        min-height: 48px;
        padding: 12px;
    }
    .login-card .button { min-height: 50px; font-size: 16px; }
}
