/* WCSAM responsive hardening — loaded after the original dashboard styles. */
*,
*::before,
*::after {
    box-sizing: border-box;
}

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

body {
    min-width: 0;
    min-height: 100vh;
    min-height: 100dvh;
    overflow-x: hidden;
}

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

button,
input,
select,
textarea {
    max-width: 100%;
}

button,
.btn,
.icon-btn,
.nav-item {
    touch-action: manipulation;
}

/* Login */
.login-screen {
    min-height: 100vh;
    min-height: 100dvh;
    display: grid;
    place-items: center;
    padding: clamp(1rem, 4vw, 3rem);
}

.login-shell {
    width: min(100%, 1180px);
    min-width: 0;
    display: grid;
    grid-template-columns: minmax(0, 1.05fr) minmax(320px, .95fr);
    align-items: stretch;
    gap: clamp(1rem, 3vw, 2rem);
}

.login-brand-panel,
.login-card,
.login-card .card-body {
    min-width: 0;
}

.login-brand-panel h1,
.login-card h2,
.panel-heading h3,
.topbar h2 {
    overflow-wrap: anywhere;
}

.security-points {
    display: grid;
    gap: .75rem;
}

.input-group > .form-control,
.input-group > .form-select {
    min-width: 0;
}

/* App frame */
.app-shell:not([hidden]) {
    min-height: 100vh;
    min-height: 100dvh;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 280px;
    grid-template-areas: "main sidebar";
    align-items: stretch;
}

.sidebar {
    grid-area: sidebar;
    min-width: 0;
    max-width: 100%;
}

.main-content {
    grid-area: main;
    min-width: 0;
    width: 100%;
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 30;
    min-width: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding-inline: clamp(1rem, 2.5vw, 2rem);
}

.topbar > div:first-of-type,
.topbar-actions {
    min-width: 0;
}

.topbar-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: .75rem;
}

.content-wrap {
    width: 100%;
    min-width: 0;
    padding: clamp(1rem, 2.5vw, 2rem);
}

.view-section,
.panel-card,
.panel-card > *,
.row > * {
    min-width: 0;
}

/* Adaptive grids */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 1rem;
}

.stat-card {
    min-width: 0;
}

.stat-card strong,
.wallet-hero strong,
.price-summary strong {
    overflow-wrap: anywhere;
}

.permission-grid,
.product-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
    gap: 1rem;
}

.panel-heading {
    min-width: 0;
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 1rem;
}

.panel-heading > div {
    min-width: 0;
}

.filter-row {
    min-width: min(100%, 520px);
    display: flex;
    align-items: center;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: .75rem;
}

.filter-row > * {
    flex: 1 1 190px;
    min-width: 0;
}

.profile-list {
    display: grid;
    grid-template-columns: minmax(100px, auto) minmax(0, 1fr);
    gap: .75rem 1rem;
}

.profile-list dt,
.profile-list dd {
    min-width: 0;
    margin: 0;
    overflow-wrap: anywhere;
}

/* Generated data areas: never let a wide table break the page. */
#recentOrdersContent,
#accountsContent,
#walletTransactions,
#ordersContent,
#logsContent,
.table-responsive,
.responsive-table-wrap {
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    -webkit-overflow-scrolling: touch;
}

#recentOrdersContent table,
#accountsContent table,
#walletTransactions table,
#ordersContent table,
#logsContent table,
.table-responsive table {
    width: 100%;
    min-width: 720px;
    margin-bottom: 0;
}

th,
td {
    vertical-align: middle;
}

/* Long technical values */
.account-line,
.server-line,
.file-line,
.order-result,
pre,
code {
    max-width: 100%;
    overflow-wrap: anywhere;
    word-break: break-word;
}

pre {
    overflow-x: auto;
    white-space: pre-wrap;
}

/* Dialog */
.action-dialog {
    width: min(calc(100% - 2rem), 620px);
    max-height: min(90vh, 760px);
    max-height: min(90dvh, 760px);
    padding: 0;
    border: 0;
    overflow: visible;
}

.action-dialog::backdrop {
    background: rgb(15 23 42 / .62);
    backdrop-filter: blur(3px);
}

.dialog-card {
    max-height: min(90vh, 760px);
    max-height: min(90dvh, 760px);
    overflow-y: auto;
    overscroll-behavior: contain;
}

.dialog-actions {
    display: flex;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: .75rem;
}

.dialog-actions .btn {
    min-width: 110px;
}

/* Loading and toast overlays */
.loading-overlay {
    position: fixed;
    inset: 0;
    z-index: 2000;
    padding: 1rem;
}

.toast-container {
    max-width: min(100%, 430px);
    padding-bottom: max(1rem, env(safe-area-inset-bottom)) !important;
    padding-left: max(1rem, env(safe-area-inset-left)) !important;
}

/* Tablet */
@media (max-width: 1199.98px) {
    .stats-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 991.98px) {
    .login-shell {
        grid-template-columns: minmax(0, 1fr);
        max-width: 720px;
    }

    .login-brand-panel {
        order: 2;
    }

    .login-card {
        order: 1;
    }

    .app-shell:not([hidden]) {
        display: block;
    }

    .sidebar {
        position: fixed;
        z-index: 1050;
        top: 0;
        right: 0;
        bottom: 0;
        width: min(86vw, 320px);
        max-width: 100%;
        transform: translateX(105%);
        transition: transform .22s ease;
        overflow-y: auto;
        overscroll-behavior: contain;
        padding-bottom: env(safe-area-inset-bottom);
        box-shadow: -20px 0 45px rgb(15 23 42 / .24);
    }

    .sidebar.is-open,
    .sidebar.open,
    .sidebar.show,
    body.sidebar-open .sidebar,
    .app-shell.sidebar-open .sidebar {
        transform: translateX(0);
    }

    .sidebar-backdrop:not([hidden]) {
        position: fixed;
        z-index: 1040;
        inset: 0;
        display: block;
        background: rgb(15 23 42 / .56);
        backdrop-filter: blur(2px);
    }

    .main-content {
        width: 100%;
    }

    .sticky-panel {
        position: static !important;
        top: auto !important;
    }
}

/* Mobile */
@media (max-width: 575.98px) {
    .login-screen {
        place-items: start center;
        padding: .75rem;
    }

    .login-shell {
        gap: .75rem;
    }

    .login-brand-panel {
        padding: 1.25rem;
    }

    .login-card .card-body {
        padding: 1.25rem !important;
    }

    .topbar {
        align-items: flex-start;
        flex-wrap: wrap;
        padding: .85rem 1rem;
    }

    .topbar > div:first-of-type {
        flex: 1 1 calc(100% - 56px);
    }

    .topbar-actions {
        width: 100%;
        justify-content: space-between;
    }

    .connection-state {
        flex: 1 1 auto;
    }

    #refreshButton {
        flex: 0 0 auto;
    }

    .content-wrap {
        padding: .85rem;
    }

    .stats-grid {
        grid-template-columns: minmax(0, 1fr);
        gap: .75rem;
    }

    .panel-card {
        border-radius: 14px;
    }

    .panel-heading {
        align-items: stretch;
        flex-direction: column;
    }

    .panel-heading > i {
        display: none;
    }

    .filter-row {
        width: 100%;
        min-width: 0;
        flex-direction: column;
        align-items: stretch;
    }

    .filter-row > * {
        width: 100%;
        flex-basis: auto;
    }

    .profile-list {
        grid-template-columns: minmax(0, 1fr);
        gap: .25rem;
    }

    .profile-list dd {
        margin-bottom: .75rem;
    }

    .dialog-actions,
    .dialog-actions .btn {
        width: 100%;
    }

    .toast-container {
        right: 0 !important;
        left: 0 !important;
        width: 100%;
        max-width: none;
    }
}

/* Very narrow devices */
@media (max-width: 359.98px) {
    .login-card .card-body,
    .login-brand-panel,
    .content-wrap {
        padding-inline: .7rem !important;
    }

    .topbar {
        padding-inline: .7rem;
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        scroll-behavior: auto !important;
        animation-duration: .01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: .01ms !important;
    }
}
