/* WTR responsive shell — SPEC-S7 / ADR-0021 / Issue #206
 * Breakpoints: xs ≤480 · sm 481–768 · md 769–1024 · lg ≥1025
 * Compact shell (bottom nav · cards · stacked Robot) = xs+sm+md (≤1024), incl. tablets.
 * Full desktop chrome only at lg (≥1025).
 */

:root {
    --wtr-bnav-h: 60px;
    --wtr-touch-min: 44px;
}

.wtr-lang-switch {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    margin-left: 8px;
    margin-right: 8px;
    font-size: 12px;
    flex: 0 0 auto;
    position: relative;
    z-index: 60;
}
.wtr-lang-switch button {
    background: transparent;
    border: 0;
    color: var(--muted, #8b9bb0);
    cursor: pointer;
    padding: 2px 6px;
    font: inherit;
    min-height: auto;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    pointer-events: auto;
}
.wtr-lang-switch button.active {
    color: var(--accent, #f0b90b);
    font-weight: 700;
}
.wtr-lang-sep {
    color: var(--line, #26303f);
    pointer-events: none;
    user-select: none;
}
.wtr-lang-switch--me {
    display: flex;
    justify-content: center;
    margin: 8px 16px 4px;
    font-size: 14px;
}
.wtr-lang-switch--me button {
    min-height: var(--wtr-touch-min);
    padding: 8px 14px;
    font-size: 14px;
}

/* Compact shell header: stable single row on DevTools resize (#232) */
@media (max-width: 1024px) {
    .app-header {
        display: flex !important;
        flex-wrap: nowrap !important;
        align-items: center;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        gap: 8px;
        overflow: hidden;
    }
    .app-header h1 {
        flex: 0 0 auto;
    }
    .app-header .wtr-header-spacer,
    .app-header > span[style*="flex"] {
        flex: 1 1 auto !important;
        min-width: 4px;
        max-width: none;
    }
    .app-header .wtr-lang-switch {
        margin-left: 0;
        margin-right: 0;
        padding: 0;
        border-radius: 8px;
        background: rgba(16, 20, 28, 0.55);
        border: 1px solid var(--line, #26303f);
        z-index: 70;
        flex: 0 0 auto;
    }
    .app-header .wtr-lang-switch button {
        min-height: 40px;
        min-width: 40px;
        padding: 6px 10px;
        font-size: 13px;
        font-weight: 700;
    }
    .app-header .session-user {
        max-width: 26vw;
        flex: 0 1 auto;
        min-width: 0;
    }
}

/* Auth / login landing: pin switcher top-right (#224) */
html.wtr-shell-auth .wtr-lang-switch {
    position: fixed;
    top: 12px;
    right: 12px;
    z-index: 40;
    margin: 0;
    padding: 6px 10px;
    border-radius: 8px;
    background: rgba(16, 20, 28, 0.72);
    border: 1px solid var(--line, #26303f);
}

/* Issue status colors inside mobile cards (#223) */
.wtr-stack-row .issue-status-open {
    color: #ef5350;
    font-weight: 600;
}
.wtr-stack-row .issue-status-resolved,
.wtr-stack-row .issue-status-notified {
    color: var(--ok, #26a69a);
}
.wtr-stack-row .status-open { color: #26a69a; }
.wtr-stack-row .status-closed { color: var(--muted, #8b9bb0); }


/* —— utilities —— */
.desk-only { }
.mobile-only { display: none !important; }

@media (max-width: 1024px) {
    .desk-only { display: none !important; }
    .mobile-only { display: revert !important; }
}

/* —— bottom nav (injected) —— */
.wtr-bottom-nav {
    display: none;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1000;
    /* #271: no fixed height that clips pill corners — pad inside + safe-area */
    box-sizing: border-box;
    min-height: calc(var(--wtr-bnav-h) + env(safe-area-inset-bottom, 0px));
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom, 0px));
    grid-template-columns: repeat(7, 1fr);
    align-items: stretch;
    gap: 2px;
    background: #0e141c;
    border-top: 1px solid var(--line, #26303f);
    overflow: visible;
}
.wtr-bnav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    min-height: 0;
    width: 100%;
    box-sizing: border-box;
    color: var(--muted, #8b9bb0);
    text-decoration: none;
    text-align: center;
    background: none;
    border: 0;
    font: inherit;
    cursor: pointer;
    padding: 6px 4px;
    margin: 0;
    border-radius: 10px;
    -webkit-tap-highlight-color: transparent;
}
/* #268: pill like desktop site-nav.active — beat page-level `button`/`a` color rules */
.wtr-bottom-nav a.wtr-bnav-item.active,
.wtr-bottom-nav button.wtr-bnav-item.active {
    color: #10141c !important;
    background: var(--accent, #f0b90b);
    font-weight: 600;
}
.wtr-bottom-nav a.wtr-bnav-item.active .wtr-bnav-glyph,
.wtr-bottom-nav a.wtr-bnav-item.active .wtr-bnav-label,
.wtr-bottom-nav button.wtr-bnav-item.active .wtr-bnav-glyph,
.wtr-bottom-nav button.wtr-bnav-item.active .wtr-bnav-label {
    color: inherit;
}
.wtr-bnav-glyph {
    font-size: 18px;
    line-height: 1;
    display: block;
    text-align: center;
}
.wtr-bnav-label {
    font-size: 10px;
    line-height: 1.15;
    display: block;
    width: 100%;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.wtr-bnav-item .issue-badge {
    position: absolute;
    margin-left: 18px;
    margin-top: -28px;
    font-size: 9px;
    min-width: 14px;
    height: 14px;
    padding: 0 3px;
}

/* Phone + tablet (≤1024): compact shell */
@media (max-width: 1024px) {
    html.wtr-shell-app .wtr-bottom-nav { display: grid; }
    html.wtr-shell-app body.has-wtr-bottom-nav {
        padding-bottom: calc(var(--wtr-bnav-h) + env(safe-area-inset-bottom, 0px) + 8px);
    }
    html.wtr-shell-app .app-footer,
    html.wtr-shell-app .robot-footer,
    html.wtr-shell-app footer.app-footer {
        margin-bottom: 4px;
    }

    /* Compact header: brand + session; hide desktop site-nav */
    .app-header .site-nav { display: none !important; }
    .app-header .account,
    .app-header form.logout { display: none !important; }
    .app-header {
        padding: 10px 12px;
        gap: 8px;
        position: sticky;
        top: 0;
        z-index: 50;
        flex-wrap: nowrap !important;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        overflow: hidden;
    }
    .app-header h1 { font-size: 14px; }
    .session-user { max-width: 40vw; }

    /* Touch targets */
    button,
    .btn,
    .secondary,
    a.account,
    form.logout button,
    input[type="submit"],
    select,
    .chip {
        min-height: var(--wtr-touch-min);
    }
    input:not([type="hidden"]):not([type="checkbox"]):not([type="radio"]),
    select,
    textarea {
        min-height: var(--wtr-touch-min);
        font-size: 16px; /* iOS zoom guard */
    }

    /* Main padding — tablets get a bit more air than phones */
    .robot-main,
    .accounts-main,
    main {
        padding-left: 14px;
        padding-right: 14px;
        max-width: 960px;
        margin-left: auto;
        margin-right: auto;
    }

    /*
     * Period performance band (#259): root must stay a VERTICAL stack.
     * Never put .period-stats itself into a 2-col grid — that places .ps-head
     * beside .ps-hero (badge "Period in profit" shoves Total P&L sideways).
     * Only .ps-grid / .ps-side-metrics are KPI grids.
     */
    .period-stats,
    .period-stats-band,
    #period-stats {
        display: flex !important;
        flex-direction: column !important;
        grid-template-columns: none !important;
        align-items: stretch !important;
        width: 100% !important;
        max-width: 100% !important;
        gap: 10px !important;
        box-sizing: border-box;
    }
    .period-stats:not(.dash-ps-band) {
        text-align: center;
    }
    .period-stats:not(.dash-ps-band) .ps-head {
        display: flex !important;
        flex-direction: column !important;
        flex-wrap: nowrap !important;
        align-items: center !important;
        justify-content: center !important;
        text-align: center !important;
        width: 100%;
        gap: 8px !important;
    }
    .period-stats:not(.dash-ps-band) .ps-hero {
        display: flex !important;
        flex-direction: column !important;
        grid-template-columns: none !important;
        align-items: center !important;
        width: 100%;
        gap: 12px !important;
        text-align: center;
    }
    .period-stats:not(.dash-ps-band) .ps-hero > div {
        width: 100%;
    }
    .period-stats .ps-side-metrics {
        display: grid !important;
        grid-template-columns: 1fr 1fr !important;
        gap: 8px !important;
        width: 100%;
    }
    .period-stats .ps-grid,
    .period-stats-grid,
    .ps-grid {
        display: grid !important;
        grid-template-columns: 1fr 1fr !important;
        gap: 8px !important;
        width: 100%;
    }
    .period-stats:not(.dash-ps-band) .ps-chip,
    .period-stats:not(.dash-ps-band) .ps-cell,
    .period-stats:not(.dash-ps-band) .ps-pnl-label,
    .period-stats:not(.dash-ps-band) .ps-pnl-value,
    .period-stats:not(.dash-ps-band) .ps-pnl-sub,
    .period-stats:not(.dash-ps-band) .ps-title,
    .period-stats:not(.dash-ps-band) .ps-period,
    .period-stats:not(.dash-ps-band) .ps-badge {
        text-align: center;
    }
    .period-stats:not(.dash-ps-band) .ps-chip-k,
    .period-stats:not(.dash-ps-band) .ps-chip-v,
    .period-stats:not(.dash-ps-band) .ps-cell-k,
    .period-stats:not(.dash-ps-band) .ps-cell-v {
        display: block;
        text-align: center;
        width: 100%;
    }

    /* Charts full width + overscroll */
    .chart-host,
    .robot-chart-host,
    #robot-chart,
    #equity-chart,
    #live-equity-chart,
    #capital-chart,
    [data-chart-host] {
        width: 100% !important;
        max-width: 100% !important;
        overscroll-behavior: contain;
    }

    /* Tables: horizontal escape hatch */
    .table-wrap,
    .live-table-wrap,
    .ta-accounts-scroll {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        max-width: 100%;
    }
    /*
     * #286 — desktop max-heights (200–360px / 60vh) clip tall mobile cards.
     * JS sets inline max-height ≈ one .wtr-stack-card + pad; clear CSS caps in card mode.
     */
    .wtr-table-as-cards.table-wrap,
    .wtr-table-as-cards.live-table-wrap,
    .wtr-table-as-cards.ta-accounts-scroll,
    .wtr-table-as-cards.users-table-wrap,
    .wtr-table-as-cards.bt-table-wrap,
    .wtr-table-as-cards.trades-table,
    .robot-ledger .wtr-table-as-cards.table-wrap,
    .accounts-main .wtr-table-as-cards.table-wrap {
        max-height: none !important;
        overflow-x: hidden;
        overflow-y: visible;
        -webkit-overflow-scrolling: touch;
    }
    .wtr-stack-cards { display: none; }
    .wtr-table-as-cards > table,
    .wtr-table-as-cards table[data-mobile-cards="1"] {
        display: none !important;
    }
    .wtr-table-as-cards .wtr-stack-cards,
    .wtr-stack-cards.wtr-stack-viewport-fit {
        display: block;
    }
    .wtr-stack-cards.wtr-stack-viewport-fit {
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
        box-sizing: border-box;
    }
    .wtr-stack-card {
        border: 1px solid var(--line, #26303f);
        border-radius: 10px;
        padding: 10px 12px;
        background: #18202c;
        margin-bottom: 8px;
    }
    .wtr-table-as-cards .wtr-stack-card:last-child {
        margin-bottom: 0;
    }
    .wtr-stack-card.selected {
        outline: 2px solid var(--accent, #f0b90b);
        background: rgba(240, 185, 11, 0.10);
        box-shadow: inset 3px 0 0 var(--accent, #f0b90b);
    }
    .wtr-stack-card.muted { color: var(--muted, #8b9bb0); }
    .wtr-stack-row {
        display: flex;
        justify-content: space-between;
        gap: 10px;
        margin: 3px 0;
        font-size: 12px;
    }
    .wtr-stack-row .muted { color: var(--muted, #8b9bb0); flex: 0 0 42%; }

    /* Forms */
    .form-grid,
    .params-grid,
    .layout-a-params,
    .master-grid,
    .metric-grid {
        grid-template-columns: 1fr 1fr !important;
    }
    .field, label { max-width: 100%; }

    .layout-toggle-btn { display: none !important; }

    /* Robot: force stacked mid columns when Layout B still mounted */
    .robot-main.layout-b .robot-mid {
        display: grid !important;
        grid-template-columns: 1fr !important;
        gap: 12px !important;
    }
    .robot-main.layout-b .robot-col-left,
    .robot-main.layout-b .robot-col-center,
    .robot-main.layout-b .robot-col-right {
        width: 100% !important;
        min-width: 0 !important;
        max-width: none !important;
    }
    .layout-a #slot-a-hero .robot-control-block {
        flex-direction: column !important;
    }
    .control-start .btn-robot-wrap,
    #btn-robot {
        width: 100%;
    }
    .btn-row { gap: 8px; }
    .btn-row > * { flex: 1 1 auto; }

    .panel { padding: 12px; }
    .accounts-main .hint { font-size: 12px; }

    header.app-header,
    body > header {
        flex-wrap: nowrap !important;
    }
}

/* Tablet band: slightly roomier cards / KPI */
@media (min-width: 769px) and (max-width: 1024px) {
    .wtr-stack-card { padding: 12px 14px; font-size: 13px; }
    /* KPI cells only — not the period-stats root */
    .period-stats .ps-grid,
    .period-stats-grid,
    .ps-grid {
        grid-template-columns: repeat(3, 1fr) !important;
    }
    .period-stats .ps-side-metrics {
        grid-template-columns: repeat(2, 1fr) !important;
    }
    .wtr-bnav-label { font-size: 11px; }
    .wtr-bnav-glyph { font-size: 20px; }
}

@media (max-width: 1024px) {
    /* Dashboard hero must stay a vertical stack; left-aligned rows (mockup B). */
    .dash-ps-band.period-stats {
        display: flex !important;
        flex-direction: column !important;
        grid-template-columns: none !important;
        text-align: left !important;
        gap: 0 !important;
    }
    .dash-ps-band.period-stats .ps-head,
    .dash-ps-band.period-stats .ps-hero,
    .dash-ps-band.period-stats .ps-chip,
    .dash-ps-band.period-stats .ps-cell,
    .dash-ps-band.period-stats .ps-pnl-label,
    .dash-ps-band.period-stats .ps-pnl-value,
    .dash-ps-band.period-stats .ps-title,
    .dash-ps-band.period-stats .ps-period,
    .dash-ps-band.period-stats .ps-badge,
    .dash-ps-band.period-stats .ps-chip-k,
    .dash-ps-band.period-stats .ps-chip-v,
    .dash-ps-band.period-stats .ps-cell-k,
    .dash-ps-band.period-stats .ps-cell-v {
        text-align: left !important;
        align-items: flex-start;
    }
    .dash-ps-band .dash-metrics {
        display: grid !important;
        grid-template-columns: 1fr 1fr !important;
        width: 100%;
    }
}

@media (max-width: 480px) {
    .form-grid,
    .params-grid,
    .layout-a-params,
    .master-grid.compact,
    .master-grid {
        grid-template-columns: 1fr !important;
    }
    .kpi-row {
        grid-template-columns: 1fr 1fr !important;
    }
    .period-stats .ps-grid,
    .period-stats-grid,
    .ps-grid,
    .period-stats .ps-side-metrics {
        grid-template-columns: 1fr 1fr !important;
    }
    .robot-main,
    .accounts-main,
    main {
        padding-left: 12px;
        padding-right: 12px;
        max-width: none;
    }
}

/* —— Me sheet —— */
.wtr-me-sheet[hidden] { display: none !important; }
.wtr-me-sheet {
    position: fixed;
    inset: 0;
    z-index: 1100;
}
.wtr-me-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
}
.wtr-me-panel {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    max-width: 480px;
    margin: 0 auto;
    background: #121821;
    border-top: 1px solid var(--line, #26303f);
    border-radius: 16px 16px 0 0;
    padding: 10px 16px calc(16px + env(safe-area-inset-bottom, 0px));
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.wtr-me-handle {
    width: 36px;
    height: 4px;
    border-radius: 999px;
    background: #2a3545;
    margin: 4px auto 8px;
}
.wtr-me-user {
    margin: 0 0 4px;
    color: var(--muted, #8b9bb0);
    font-size: 13px;
}
.wtr-me-link,
.wtr-me-logout button,
.wtr-me-close {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 44px;
    border-radius: 10px;
    border: 1px solid var(--line, #26303f);
    background: #18202c;
    color: var(--text, #e6edf6);
    text-decoration: none;
    font: inherit;
    cursor: pointer;
}
.wtr-me-logout { margin: 0; }
.wtr-me-logout button {
    width: 100%;
    color: #ef5350;
}
.wtr-me-close { background: transparent; color: var(--muted, #8b9bb0); }

/* —— Auth pages (phone + tablet) —— */
@media (max-width: 1024px) {
    html.wtr-shell-auth .landing {
        display: flex !important;
        flex-direction: column !important;
        min-height: 100vh;
        padding: 16px !important;
        gap: 16px;
        max-width: 560px;
        margin: 0 auto;
    }
    html.wtr-shell-auth .landing .hero {
        padding: 12px 4px 0 !important;
        max-width: none !important;
    }
    html.wtr-shell-auth .landing .login-panel,
    html.wtr-shell-auth .landing .card {
        width: 100% !important;
        max-width: none !important;
    }
    html.wtr-shell-auth .card button,
    html.wtr-shell-auth #login-submit {
        width: 100%;
        min-height: 44px;
    }
}

/* —— lg desktop: full chrome, no compact shell —— */
@media (min-width: 1025px) {
    .wtr-bottom-nav { display: none !important; }
    .wtr-me-sheet { display: none !important; }
    body.has-wtr-bottom-nav { padding-bottom: 0 !important; }
    .wtr-stack-cards { display: none !important; }
    .wtr-table-as-cards > table { display: table !important; }
}
