/* Device-responsive stability layer.
   This file is intentionally loaded last so compact screens, display scaling
   and vendor WebView font boosting cannot force content outside its cards. */
html {
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body,
.app-layout,
.main-content,
.tab-pane,
.card,
.settings-pane,
.settings-content-container {
    min-width: 0;
    max-width: 100%;
}

body {
    width: 100%;
    min-height: 100dvh;
}

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

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

.card-title,
.stat-card,
.stat-card h3,
.stat-box,
.stat-box h2,
.bal-amount,
#compSummary > div,
#compSummary > div > div,
#wishesManagerList,
#shoppingTab {
    min-width: 0;
    overflow-wrap: anywhere;
}

.currency-picker {
    position: relative;
}

.currency-help {
    display: block;
    margin-top: 5px;
    color: var(--text-muted);
    font-size: 0.68rem;
}

.currency-results {
    position: absolute;
    z-index: 30;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    max-height: min(260px, 38dvh);
    overflow-y: auto;
    overscroll-behavior: contain;
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 5px;
    background: var(--bg-card);
    box-shadow: 0 18px 40px rgba(15, 23, 42, 0.18);
}

.currency-results[hidden] {
    display: none;
}

.currency-result {
    width: 100%;
    display: grid;
    grid-template-columns: 46px minmax(0, 1fr);
    gap: 1px 8px;
    padding: 9px 10px;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--text-dark);
    text-align: left;
    cursor: pointer;
}

.currency-result:hover,
.currency-result:focus-visible {
    background: var(--primary-light);
    outline: none;
}

.currency-result strong {
    grid-row: 1 / span 2;
    align-self: center;
    color: var(--primary);
    font-size: 0.82rem;
}

.currency-result span,
.currency-result small {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.currency-result span {
    font-size: 0.75rem;
    font-weight: 700;
}

.currency-result small {
    color: var(--text-muted);
    font-size: 0.64rem;
}

.currency-result-empty {
    padding: 12px;
    color: var(--text-muted);
    font-size: 0.75rem;
    text-align: center;
}

@media (max-width: 768px) {
    :root {
        --mobile-side-padding: clamp(8px, 3.2vw, 14px);
        --nav-item-width: clamp(64px, 20vw, 84px);
    }

    .nav-brand-mobile {
        width: 100%;
        max-width: 100vw;
        padding-left: var(--mobile-side-padding) !important;
        padding-right: var(--mobile-side-padding) !important;
        overflow: hidden;
    }

    .nav-brand-mobile > div:first-child {
        min-width: 0;
        gap: clamp(4px, 1.4vw, 8px);
    }

    .nav-brand-mobile > div:first-child > div:first-child {
        min-width: 0;
        flex: 1 1 auto;
        font-size: clamp(1.05rem, 5.7vw, 1.5rem) !important;
        line-height: 1.1;
        white-space: nowrap;
    }

    .nav-brand-mobile > div:first-child > div:last-child {
        flex: 0 0 auto;
        min-width: 0;
        gap: clamp(3px, 1.4vw, 8px) !important;
    }

    #headerXpWidget {
        width: clamp(72px, 25vw, 100px) !important;
        padding: 4px 6px !important;
    }

    .nav-brand-mobile .fa-exchange-alt,
    .nav-brand-mobile .fa-sign-out-alt {
        font-size: 0.9rem !important;
    }

    .nav-brand-mobile i[onclick],
    .nav-brand-mobile div[onclick] {
        width: clamp(27px, 8.5vw, 32px) !important;
        height: clamp(27px, 8.5vw, 32px) !important;
    }

    .main-content {
        width: 100%;
        max-width: 100vw;
        padding-left: var(--mobile-side-padding) !important;
        padding-right: var(--mobile-side-padding) !important;
        overflow-x: clip;
    }

    .app-nav {
        width: 100vw;
        max-width: 100vw;
        padding-left: 0 !important;
        padding-right: 0 !important;
        overflow-x: auto !important;
        overflow-y: hidden !important;
        overscroll-behavior-x: contain;
        scroll-snap-type: x proximity;
    }

    .app-nav .nav-links {
        width: max-content !important;
        min-width: 100%;
        gap: 0 !important;
    }

    .app-nav .nav-btn {
        width: var(--nav-item-width);
        max-width: var(--nav-item-width);
        min-width: var(--nav-item-width);
        padding: 5px 3px !important;
        scroll-snap-align: start;
    }

    .app-nav .nav-btn span {
        display: block;
        width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        font-size: clamp(0.54rem, 2.4vw, 0.66rem);
    }

    .top-header,
    .header-title,
    .header-actions-mobile {
        min-width: 0;
        max-width: 100%;
    }

    .header-actions-mobile {
        display: grid !important;
        grid-template-columns: 42px 42px minmax(0, 1fr);
        width: 100%;
    }

    .header-actions-mobile .date-filters {
        width: 100%;
        min-width: 0;
        display: grid;
        grid-template-columns: minmax(0, 1fr) 1px minmax(0, 1fr) 1px 38px;
    }

    .date-filters select {
        width: 100%;
        min-width: 0;
        padding: 6px 4px !important;
        font-size: clamp(0.68rem, 3vw, 0.82rem) !important;
    }

    .stats-grid,
    .stat-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
        gap: clamp(8px, 2.6vw, 12px) !important;
    }

    .stat-card,
    .stat-box {
        padding: clamp(10px, 3.4vw, 15px) !important;
        overflow: hidden;
    }

    .stat-card p,
    .stat-box p {
        font-size: clamp(0.62rem, 2.7vw, 0.78rem) !important;
        line-height: 1.35;
        letter-spacing: 0.1px !important;
    }

    .stat-card h3,
    .stat-box h2 {
        max-width: 100%;
        font-size: clamp(0.9rem, 4.4vw, 1.3rem) !important;
        line-height: 1.18;
        letter-spacing: -0.3px;
        word-break: break-word;
    }

    #compSummary {
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
        gap: 9px !important;
    }

    #compSummary > div {
        padding: 10px !important;
        overflow: hidden;
    }

    #compSummary > div:nth-child(3) {
        grid-column: 1 / -1;
    }

    #compSummary > div > div:last-child {
        font-size: clamp(0.82rem, 4vw, 1.05rem) !important;
        line-height: 1.25;
        word-break: break-word;
    }

    #settingsTab > div:first-child {
        gap: 8px !important;
    }

    #settingsTab > div:first-child > div {
        display: grid !important;
        grid-template-columns: repeat(4, minmax(0, 1fr));
        gap: 2px !important;
        width: 100%;
        overflow: visible !important;
    }

    #settingsTab .s-tab-btn {
        min-width: 0;
        padding: 7px 2px !important;
        font-size: clamp(0.61rem, 2.7vw, 0.78rem) !important;
        line-height: 1.2;
        white-space: normal;
        overflow-wrap: anywhere;
    }

    #settingsTab > div:nth-child(2) {
        padding: clamp(12px, 3.8vw, 20px) !important;
    }

    #s-general > div:first-child > div,
    #s-general .grid,
    .settings-pane .grid {
        grid-template-columns: minmax(0, 1fr) !important;
    }

    #shoppingTab .sub-tabs {
        display: grid !important;
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }

    #shoppingTab .sub-tabs button {
        min-width: 0;
        padding: 10px 4px !important;
        font-size: clamp(0.68rem, 3vw, 0.84rem) !important;
        line-height: 1.3;
        overflow-wrap: anywhere;
    }

    #wishesTab .card > div > div:first-child,
    #wishesManagerList > div > div:last-child {
        flex-wrap: wrap;
    }

    #wishesManagerList > div > div:last-child {
        display: grid !important;
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }

    #wishesManagerList button {
        min-width: 0;
        padding: 8px 3px !important;
        font-size: clamp(0.58rem, 2.7vw, 0.74rem) !important;
        line-height: 1.25;
        white-space: normal;
    }

    .auth-wrapper {
        min-height: 100dvh;
        height: 100dvh;
        overflow-y: auto;
        align-items: flex-start;
        padding: max(12px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom));
    }

    .auth-card {
        margin: auto;
        padding: clamp(20px, 6vw, 30px) clamp(16px, 5vw, 24px) !important;
        border-radius: clamp(20px, 7vw, 30px);
    }

    .auth-header {
        margin-bottom: clamp(18px, 5dvh, 30px);
    }

    .auth-logo {
        width: clamp(50px, 15vw, 64px);
        height: clamp(50px, 15vw, 64px);
        margin-bottom: 12px;
    }
}

@media (max-width: 360px) {
    .nav-brand-mobile > div:first-child > div:first-child {
        font-size: 1.02rem !important;
    }

    #headerXpWidget {
        width: 70px !important;
    }

    #headerXpWidget > div:first-child {
        font-size: 0.48rem !important;
    }

    .card {
        padding: 12px !important;
    }

    .bal-amount {
        font-size: clamp(1.65rem, 10vw, 2.1rem) !important;
        overflow-wrap: anywhere;
    }
}

@media (max-height: 640px) and (max-width: 768px) {
    .auth-card {
        margin-top: 6px;
        margin-bottom: 6px;
    }

    .auth-header {
        margin-bottom: 14px;
    }

    .auth-logo {
        width: 46px;
        height: 46px;
        margin-bottom: 8px;
    }
}
