/* ==========================================================================
   Responsive app shell
   Mobile navigation, safe areas and trading layouts. Shared surfaces and
   controls live in design-system.css; this file only shapes the app shell.
   ========================================================================== */

/* Keep scroll gestures inside the app instead of chaining out to the browser's
   own overscroll actions. Required now that pull-to-refresh runs on iOS
   standalone (see initPullToRefresh in main.js). */
html,
body {
    overscroll-behavior-y: contain;
}

/* Scrim behind bottom sheets. Without it the list stays visible and tappable,
   which reads as a floating card rather than a modal sheet. */
.sheet-backdrop {
    position: fixed;
    inset: 0;
    z-index: 1040;
    background: rgba(0, 0, 0, 0.4);
    opacity: 0;
    transition: opacity 0.2s ease;
}

.sheet-backdrop.visible {
    opacity: 1;
}

.portfolio-slots-empty {
    display: flex;
    align-items: center;
    gap: 1rem;
    color: var(--text-muted);
    font-size: var(--fs-base);
}

.portfolio-slots-empty > i {
    font-size: var(--fs-2xl);
}

.portfolio-slots-empty strong {
    display: block;
    color: var(--text-primary);
    margin-bottom: 0.25rem;
}

.portfolio-back-link {
    display: inline-flex;
    align-items: center;
    min-height: 44px;
}

@media (max-width: 767.98px) {
    body.market-page,
    body.item-page,
    body.portfolio-page {
        padding-top: env(safe-area-inset-top, 0px);
        padding-bottom: env(safe-area-inset-bottom, 0px);
        background: var(--bg-primary);
        color: var(--text-primary);
        -webkit-font-smoothing: antialiased;
    }

    .market-page > .container-fluid,
    .item-page > .container-fluid,
    .portfolio-page > .container {
        padding-left: max(12px, env(safe-area-inset-left, 0px));
        padding-right: max(12px, env(safe-area-inset-right, 0px));
    }

    .market-page .mobile-app-header {
        padding: 8px 0 12px;
    }

    .market-page .mobile-header-primary {
        gap: 6px;
    }

    .market-page .mobile-app-logo {
        width: 36px;
        height: 36px;
        flex-shrink: 0;
    }

    .market-page .mobile-app-title {
        overflow: visible;
        font-size: var(--fs-lg);
        line-height: 1.25;
        font-weight: 700;
        white-space: normal;
    }

    .market-page .mobile-app-title > span:not(.badge) {
        display: block;
        font-weight: 500;
        color: var(--text-primary);
    }

    .market-page .mobile-data-status {
        color: var(--text-muted);
        font-size: var(--fs-sm);
        line-height: 1.5;
        font-variant-numeric: tabular-nums;
    }

    .market-page .mobile-settings-dropdown {
        width: min(300px, calc(100vw - 24px));
        max-height: min(70dvh, 560px);
        overflow-y: auto;
    }

    .market-page .mobile-header-actions {
        gap: 0;
    }

    .market-page .mobile-auth-wrapper .dropdown-toggle {
        justify-content: center;
        min-width: 44px;
        min-height: 44px;
    }

    .market-page #mainTable th.col-orig-0 {
        min-height: 44px;
    }

    .market-page #mainTable th.col-orig-0 button {
        min-height: 44px;
    }

    .market-page #ge-widget:has(#ge-widget-slots > span:only-child) {
        display: flex;
        flex-direction: row;
        align-items: center;
    }

    .market-page #ge-widget:has(#ge-widget-slots > span:only-child) > .card-header {
        flex: 1;
        min-width: 0;
        border-bottom: 0;
        background: transparent;
    }

    .market-page #ge-widget:has(#ge-widget-slots > span:only-child) #ge-widget-body {
        flex-shrink: 0;
        padding-right: 0.5rem;
    }

    .item-page .item-page-header {
        gap: 12px;
        flex-wrap: wrap;
        margin-bottom: 16px !important;
    }

    .item-page .item-page-header > a {
        min-height: 44px;
    }

    .item-page .item-page-header #global-search-mount {
        flex: 1 0 100%;
        min-width: 0;
    }

    .item-page .item-page-header .global-search {
        width: 100%;
    }

    .item-page .item-identity {
        gap: 12px !important;
        margin-bottom: 16px !important;
    }

    .item-page .item-identity-name {
        min-width: 0;
        flex: 1;
    }

    .item-page .item-identity-name > .d-flex {
        flex-wrap: wrap;
    }

    .item-page .item-identity-name h1 {
        font-size: var(--fs-xl);
        overflow-wrap: anywhere;
    }

    .item-page .item-identity-actions {
        width: 100%;
        justify-content: space-between;
        padding: 8px 12px;
        background: var(--raise-1);
        border-radius: var(--radius-lg);
    }

    .item-page .item-identity-actions .btn {
        min-width: 44px;
        min-height: 44px;
    }

    .item-page .item-market-stats .row > .col-6 {
        flex: 0 0 50%;
        max-width: 50%;
        min-width: 0;
        padding: 12px !important;
        font-size: var(--fs-sm);
        border: 0 !important;
    }

    .item-page .item-market-stats .row > .col-6 > .d-flex {
        align-items: baseline;
        gap: 4px;
        flex-wrap: wrap;
    }

    .item-page .item-market-stats .row > .col-6 > .d-flex > :last-child {
        margin-left: auto;
    }

    .item-page .item-market-stats .row > .col-6 > .d-flex i {
        display: none;
    }

    .item-page .item-market-stats .section-header {
        letter-spacing: 0.04em;
        line-height: 1.5;
    }

    .item-page .chart-controls button {
        min-width: 44px;
        min-height: 44px;
    }

    .portfolio-page .portfolio-header {
        gap: 8px !important;
        margin-bottom: 20px !important;
    }

    .portfolio-page .card-header {
        padding: 12px;
        flex-wrap: wrap;
        row-gap: 4px;
        font-size: var(--fs-base);
    }

    .portfolio-page #sync-status {
        flex-basis: 100%;
        margin-left: 24px !important;
        font-size: var(--fs-sm);
    }

    .portfolio-page #stats-row {
        --bs-gutter-x: 12px;
        --bs-gutter-y: 12px;
    }

    .portfolio-page .stat-card .card-body {
        padding: 12px !important;
    }

    .portfolio-page .stat-card .fs-4 {
        font-size: var(--fs-xl) !important;
        font-variant-numeric: tabular-nums;
        overflow-wrap: anywhere;
    }

    .portfolio-page .empty-state {
        padding: 28px 16px;
        font-size: var(--fs-base);
    }

    .portfolio-page .empty-state i {
        font-size: var(--fs-2xl);
    }

    .portfolio-page .flip-table,
    .portfolio-page .flip-table tbody {
        display: block;
        width: 100%;
    }

    /* Keep the column headings in the accessibility tree for the real table. */
    .portfolio-page .flip-table thead {
        position: absolute;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip-path: inset(50%);
    }

    .portfolio-page .flip-table tbody tr {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 10px 16px;
        padding: 16px;
        border-bottom: 1px solid var(--border);
    }

    .portfolio-page .flip-table tbody td {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        border: 0;
        padding: 0;
        min-width: 0;
        white-space: normal;
        overflow-wrap: anywhere;
        text-align: left !important;
        font-size: var(--fs-base);
        font-variant-numeric: tabular-nums;
        box-shadow: none;
        background: transparent;
    }

    .portfolio-page .flip-table td[data-label]::before {
        content: attr(data-label);
        font-size: var(--fs-sm);
        font-weight: 400;
        color: var(--text-muted);
        margin-bottom: 2px;
    }

    .portfolio-page .flip-table td[data-label="Item"] {
        grid-column: 1 / -1;
        display: block;
        font-size: var(--fs-lg);
    }

    .portfolio-page .flip-table td[data-label="Item"]::before,
    .portfolio-page .flip-table td[data-label="Actions"]::before {
        display: none;
    }

    .portfolio-page .flip-table td[data-label="Actions"] {
        align-items: flex-end;
        justify-content: flex-end;
    }

    .portfolio-page .delete-trade-btn {
        min-width: 44px;
        min-height: 44px;
    }

    .market-page button:focus-visible,
    .item-page button:focus-visible,
    .portfolio-page button:focus-visible {
        outline: 2px solid var(--blue);
        outline-offset: 2px;
    }

    .portfolio-header > :first-child {
        flex: 1 1 0;
        min-width: 0;
    }

    .portfolio-header-search {
        order: 3;
        flex: 1 0 100%;
        min-width: 0;
    }

    .portfolio-header-search .global-search {
        width: 100%;
        max-width: none;
    }

    /* iOS zooms the viewport when a focused control is under 16px. Any control
       reachable from a primary flow has to opt out of that. */
    .global-search-input,
    .dropdown-menu-filter .form-control,
    .price-alert-target-group input,
    .input-group-sm > .form-control,
    .input-group-sm > .form-select {
        font-size: 16px !important;
    }

    body {
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }

    /* Notched devices inset the horizontal edges in landscape. */
    .container,
    .container-fluid {
        padding-left: max(8px, env(safe-area-inset-left, 0px));
        padding-right: max(8px, env(safe-area-inset-right, 0px));
    }

    /* Tap targets on pages that never loaded main.css. */
    .btn {
        -webkit-tap-highlight-color: transparent;
    }
}

@media (prefers-reduced-motion: reduce) {
    .sheet-backdrop {
        transition: none;
    }

    .market-page .mobile-icon-btn,
    .item-page .chart-controls button,
    .portfolio-page .ge-slot {
        transition: none;
    }

    .market-page .mobile-icon-btn:active {
        transform: none;
    }
}
