/* ═══════════════════════════════════════════════════════════════════
   Portfolio Terminal — mobile app shell (2026-10-09)
   Loaded after theme.css on both pages. Everything that changes layout
   is scoped to ≤ 820px, so the desktop dashboard is untouched.
   body.dash  = Dashboard (index.html) only
   body.has-tabbar = any page that shows the bottom tab bar
   ═══════════════════════════════════════════════════════════════════ */

:root { --tabbar-h: 60px; --sat: env(safe-area-inset-top, 0px); --sab: env(safe-area-inset-bottom, 0px); }

/* bug fix (all sizes): .stat-value / .hero-side b set color after .pos/.neg in theme.css,
   so gains/losses rendered white even though the JS adds the class */
.stat-value.pos, .hero-side .row b.pos { color: var(--pos); }
.stat-value.neg, .hero-side .row b.neg { color: var(--neg); }

/* hidden on desktop */
.tabbar, .app-title, .hero-spark, .hlist, .m-sort, .ptr, .more-btn { display: none; }
.section-nav { top: calc(var(--topbar-h) + var(--sat)); }

/* ── bottom sheet (shared; also usable on desktop) ── */
.sheet-backdrop { position: fixed; inset: 0; z-index: 300; background: rgba(4, 8, 16, .5); opacity: 0; pointer-events: none; transition: opacity .25s ease; -webkit-tap-highlight-color: transparent; }
.sheet {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 301;
    max-height: 90vh; max-height: 90dvh; display: flex; flex-direction: column;
    background: var(--card); border-radius: 22px 22px 0 0; border: 1px solid var(--border); border-bottom: 0;
    box-shadow: 0 -20px 60px -20px rgba(0,0,0,.45);
    transform: translateY(105%); transition: transform .34s cubic-bezier(.2, .9, .25, 1);
    padding-bottom: var(--sab); visibility: hidden;
}
html.sheet-open .sheet-backdrop { opacity: 1; pointer-events: auto; }
html.sheet-open .sheet { transform: none; visibility: visible; }
html.sheet-open, html.sheet-open body { overflow: hidden; }
.sheet.dragging { transition: none; }
.sheet-grip { flex: none; height: 24px; display: grid; place-items: center; touch-action: none; cursor: grab; }
.sheet-grip::before { content: ''; width: 40px; height: 5px; border-radius: 3px; background: var(--border-2); }
.sheet-body { overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; padding: 2px 18px 22px; }
@media (min-width: 821px) {
    .sheet { left: 50%; right: auto; width: 480px; transform: translate(-50%, 105%); }
    html.sheet-open .sheet { transform: translate(-50%, 0); }
}

/* holding sheet */
.hs-head { display: grid; grid-template-columns: 48px 1fr auto; gap: 12px; align-items: center; padding: 4px 0 14px; }
.hs-head .hl-logo { width: 48px; height: 48px; border-radius: 13px; }
.hs-sym { font-family: var(--mono); font-weight: 800; font-size: 20px; letter-spacing: -.02em; display: flex; align-items: center; gap: 8px; }
.hs-name { font-size: 12px; color: var(--ink-3); margin-top: 1px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 56vw; }
.hs-x { width: 34px; height: 34px; border-radius: 50%; background: var(--card-2); border: 1px solid var(--border); display: grid; place-items: center; color: var(--ink-2); }
.hs-x svg { width: 16px; height: 16px; }
.hs-price { display: flex; align-items: baseline; flex-wrap: wrap; gap: 4px 10px; padding: 14px 16px; border-radius: 16px; background: linear-gradient(145deg, color-mix(in srgb, var(--accent) 12%, var(--card-2)), var(--card-2) 70%); border: 1px solid var(--border); }
.hs-price .p { font-family: var(--mono); font-size: 28px; font-weight: 800; letter-spacing: -.03em; line-height: 1.1; }
.hs-price .c { font-size: 12px; color: var(--ink-3); width: 100%; }
.hs-sec { font-size: 11px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--ink-3); margin: 18px 2px 8px; }
.hs-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.hs-grid > div { background: var(--card-2); border: 1px solid var(--border); border-radius: 12px; padding: 9px 12px; min-width: 0; }
.hs-grid .k { font-size: 11px; color: var(--ink-3); font-weight: 600; }
.hs-grid .v { font-family: var(--mono); font-size: 14px; font-weight: 700; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hs-grid .v small { font-weight: 500; color: var(--ink-3); font-size: 11px; }
.hs-grid .full { grid-column: 1 / -1; }
.hs-range { background: var(--card-2); border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px 9px; }
.hs-range .k { font-size: 11px; color: var(--ink-3); font-weight: 600; display: flex; justify-content: space-between; }
.hs-range .k b { font-family: var(--mono); color: var(--ink); }
.hs-pe { background: var(--card-2); border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; }

/* more sheet (settings-style list) */
.ms-title { font-size: 20px; font-weight: 800; letter-spacing: -.02em; padding: 2px 2px 4px; }
.ms-sub { font-size: 12px; color: var(--ink-3); padding: 0 2px 4px; }
.ms-group { background: var(--card-2); border: 1px solid var(--border); border-radius: 14px; overflow: hidden; }
.ms-row { display: flex; align-items: center; gap: 12px; min-height: 50px; padding: 8px 14px; border-bottom: 1px solid var(--border); font-size: 14px; font-weight: 600; color: var(--ink); width: 100%; text-align: left; -webkit-tap-highlight-color: transparent; cursor: pointer; }
.ms-row:last-child { border-bottom: 0; }
.ms-row:active { background: var(--hover); }
.ms-row .ic { width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center; flex: none; color: #fff; }
.ms-row .ic svg { width: 16px; height: 16px; }
.ms-row .lb { flex: 1; min-width: 0; }
.ms-row .lb small { display: block; font-size: 11.5px; font-weight: 500; color: var(--ink-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ms-row .chev { color: var(--ink-3); font-size: 18px; }
.ms-seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; padding: 4px; background: var(--card-2); border: 1px solid var(--border); border-radius: 12px; }
.ms-seg button { height: 38px; border-radius: 9px; font-size: 13px; font-weight: 700; color: var(--ink-2); }
.ms-seg button.on { background: var(--card); color: var(--ink); box-shadow: var(--shadow); }
.ms-note { font-size: 12px; color: var(--ink-3); line-height: 1.6; padding: 10px 4px 0; }

/* logo avatar (list + sheet) */
.hl-logo { position: relative; width: 40px; height: 40px; border-radius: 11px; flex: none; overflow: hidden; display: grid; place-items: center; background: var(--accent-dim); color: var(--accent-ink); }
.hl-logo em { font-style: normal; font-family: var(--mono); font-weight: 800; font-size: 10.5px; letter-spacing: -.02em; }
.hl-logo img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; padding: 6px; background: #fff; }
.hl-logo.t-passive { background: rgba(74, 58, 167, .14); color: #8a7ff0; }
.hl-logo.t-bond { background: var(--warn-dim); color: var(--warn); }
.hl-logo.t-cash { background: var(--pos-dim); color: var(--pos); }

/* ═══════════════════════ PHONE / APP MODE ═══════════════════════ */
@media (max-width: 820px) {
    html { -webkit-tap-highlight-color: transparent; }
    html.app-mode, html.app-mode body { overscroll-behavior-y: none; }
    button, a, .chip, .legend-item, .tb, .hl-row, .perf-row, .tm-cell, label { touch-action: manipulation; }

    /* ── top app bar ── */
    .topbar { height: calc(var(--topbar-h) + var(--sat)); padding-top: var(--sat); padding-left: max(14px, env(safe-area-inset-left)); padding-right: max(10px, env(safe-area-inset-right)); }
    .section { scroll-margin-top: calc(var(--topbar-h) + var(--sat) + 10px); }
    .btn { height: 40px; min-width: 40px; border-radius: 12px; }
    .btn-icon { width: 40px; }

    /* ── bottom tab bar ── */
    body.has-tabbar { padding-bottom: calc(var(--tabbar-h) + var(--sab)); }
    .tabbar {
        display: grid; grid-template-columns: repeat(5, 1fr);
        position: fixed; left: 0; right: 0; bottom: 0; z-index: 120;
        height: calc(var(--tabbar-h) + var(--sab));
        padding: 5px max(4px, env(safe-area-inset-left)) var(--sab) max(4px, env(safe-area-inset-right));
        background: color-mix(in srgb, var(--card) 86%, transparent);
        backdrop-filter: saturate(180%) blur(18px); -webkit-backdrop-filter: saturate(180%) blur(18px);
        border-top: 1px solid var(--border);
        user-select: none; -webkit-user-select: none;
    }
    .tb { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 10.5px; font-weight: 700; letter-spacing: .02em; color: var(--ink-3); }
    .tb i { display: grid; place-items: center; width: 54px; height: 30px; border-radius: 15px; transition: background .2s ease, transform .15s ease; }
    .tb svg { width: 21px; height: 21px; }
    .tb.active { color: var(--accent-ink); }
    .tb.active i { background: var(--accent-dim); }
    .tb:active i { transform: scale(.88); }

    .fab { right: 14px; bottom: calc(var(--tabbar-h) + var(--sab) + 14px); width: 42px; height: 42px; }
    .toast { bottom: calc(var(--tabbar-h) + var(--sab) + 18px); max-width: calc(100vw - 32px); }
    .page { padding: 14px 12px 28px; padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); }

    /* ── pull to refresh ── */
    .ptr { display: grid; position: fixed; left: 50%; top: calc(var(--topbar-h) + var(--sat) + 4px); z-index: 95; width: 38px; height: 38px; margin-left: -19px; border-radius: 50%; place-items: center; background: var(--card); border: 1px solid var(--border); box-shadow: var(--shadow-lg); color: var(--accent); transform: translateY(-70px); opacity: 0; pointer-events: none; }
    .ptr svg { width: 18px; height: 18px; }
    .ptr.ready { color: var(--ink-inv); background: var(--accent); border-color: var(--accent); }
    .ptr.spin svg { animation: spin .8s linear infinite; }
    .ptr.settle { transition: transform .3s ease, opacity .3s ease; }

    /* ── generic mobile polish (both pages) ── */
    .card { border-radius: 16px; }
    .card:hover, .stat:hover { border-color: var(--border); transform: none; }
    .card-head { padding: 12px 14px; }
    .card-title { font-size: 13.5px; }
    .chip { height: 34px; padding: 0 12px; font-size: 12.5px; }
    .seg .chip { height: 32px; }
    .legend { gap: 2px 4px; padding: 0 12px 12px; }
    .legend-item { padding: 7px 9px; font-size: 12px; }
    .toggle { min-height: 34px; font-size: 12.5px; }
    .search input, .cape-manual input { height: 42px; font-size: 16px; border-radius: 12px; }   /* 16px stops iOS zoom-on-focus */
    .search svg { top: 14px; }
    .source-note { position: relative; cursor: pointer; padding-left: 18px; display: -webkit-box; -webkit-line-clamp: 1; -webkit-box-orient: vertical; overflow: hidden; }
    .source-note::before { content: 'ⓘ'; position: absolute; left: 0; top: 0; color: var(--ink-3); }
    .source-note.open { display: block; -webkit-line-clamp: unset; }
    .exhibit-sub { cursor: pointer; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; font-size: 12.5px; }
    .exhibit-sub.open { display: block; -webkit-line-clamp: unset; }
    .chart-tip { max-width: 220px; }
}

/* ═══════════════════════ DASHBOARD-ONLY APP MODE ═══════════════════════ */
@media (max-width: 820px) {
    body.dash .section-nav { display: none; }
    body.dash .page { padding-top: 12px; }

    /* app bar content */
    body.dash .brand > div:not(.brand-mark):not(.app-title), body.dash .status-pill, body.dash .update-time,
    body.dash #insightsMobile, body.dash #themeBtn, body.dash .topbar label[for="fileInput"] { display: none !important; }
    body.dash .brand-mark { width: 34px; height: 34px; border-radius: 10px; }
    .app-title { display: flex; flex-direction: column; min-width: 0; line-height: 1.2; }
    .app-title b { font-size: 18px; font-weight: 800; letter-spacing: -.02em; }
    .app-title span { font-size: 11px; color: var(--ink-3); font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; align-items: center; gap: 5px; }
    .app-title span i { width: 6px; height: 6px; border-radius: 50%; background: var(--ink-3); flex: none; }
    .app-title span i.live { background: var(--st-good); }
    .app-title span i.err { background: var(--st-critical); }
    body.dash #refreshBtn { background: var(--card-2); border-color: var(--border); color: var(--ink-2); width: 40px; padding: 0; justify-content: center; }
    body.dash #refreshBtn svg { width: 17px; height: 17px; }
    body.refreshing #refreshBtn svg { animation: spin .8s linear infinite; }
    .more-btn { display: inline-flex; }

    /* tab views — only the active view's sections are shown */
    body.dash[data-view="overview"]  [data-view]:not([data-view="overview"]),
    body.dash[data-view="holdings"]  [data-view]:not([data-view="holdings"]),
    body.dash[data-view="analysis"]  [data-view]:not([data-view="analysis"]),
    body.dash[data-view="valuation"] [data-view]:not([data-view="valuation"]) { display: none !important; }
    body.dash .reveal { opacity: 1; transform: none; }
    .view-in { animation: viewIn .3s cubic-bezier(.2, .8, .2, 1) both; }
    @keyframes viewIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }

    /* section heads: report chrome → app section titles */
    body.dash .section { margin-bottom: 22px; }
    body.dash .exhibit-head { margin-bottom: 10px; padding: 0 2px; }
    body.dash .exhibit-label, body.dash .exhibit-rule { display: none; }
    body.dash .action-title { font-size: 16.5px; margin-top: 0; line-height: 1.35; }
    body.dash .source-note { margin: 8px 2px 0; }

    /* ── Overview: balance card ── */
    body.dash .hero { margin-bottom: 12px; gap: 0; }
    body.dash .hero-text { display: none; }
    body.dash .hero-side { border-radius: 22px; padding: 18px 18px 14px; gap: 8px;
        background: radial-gradient(120% 90% at 100% 0%, color-mix(in srgb, var(--accent) 30%, transparent), transparent 60%), linear-gradient(160deg, color-mix(in srgb, var(--accent) 16%, var(--card)), var(--card) 75%); }
    body.dash .hero-side .lbl { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
    .hero-asof { font-family: var(--mono); font-size: 10.5px; letter-spacing: 0; text-transform: none; color: var(--ink-3); background: color-mix(in srgb, var(--card) 70%, transparent); border: 1px solid var(--border); padding: 2px 8px; border-radius: 999px; }
    body.dash .hero-side .big { font-size: 31px; margin-top: 6px; }
    body.dash .hero-side .row { flex-direction: row; flex-wrap: wrap; align-items: center; column-gap: 10px; row-gap: 2px; padding-top: 8px; font-size: 12.5px; }
    body.dash .hero-side .row span { white-space: nowrap; }
    body.dash .hero-side .row b { margin-left: auto; white-space: nowrap; }
    body.dash .hero-side .row b { font-size: 13px; text-align: right; }
    body.dash .hero-side .row b.pos { color: var(--pos); }
    body.dash .hero-side .row b.neg { color: var(--neg); }
    body.dash .hero-spark { display: block; position: relative; height: 70px; margin: 2px -6px 4px; }
    body.dash .hero-spark canvas { width: 100% !important; height: 100% !important; }

    /* KPI tiles */
    body.dash #sec-kpi .exhibit-head, body.dash #cardTotalAssets { display: none; }
    body.dash .stats { grid-template-columns: 1fr 1fr; gap: 8px; }
    body.dash .stat { padding: 11px 12px; min-height: 0; border-radius: 14px; }
    body.dash .stat-label { font-size: 10px; letter-spacing: .05em; }
    body.dash .stat-value { font-size: 16px; margin-top: 5px; }
    body.dash .stat-value.sm { font-size: 15px; }
    body.dash .stat-sub { font-size: 11px; margin-top: 4px; gap: 2px 8px; }
    body.dash #cardAlloc { grid-column: 1 / -1; }

    /* history card */
    body.dash #sec-history .card-head { flex-direction: column; align-items: stretch; }
    body.dash #sec-history .card-head > div:last-child { width: 100%; justify-content: space-between; }
    body.dash #rangeSeg { display: flex; width: 100%; }
    body.dash #rangeSeg .chip, body.dash #tmMode .chip, body.dash #bubbleMode .chip { flex: 1; justify-content: center; padding: 0 4px; }
    body.dash .chart-readout { display: grid; grid-template-columns: 1fr 1fr; gap: 2px 10px; padding: 10px 14px 0; font-size: 11.5px; min-height: 64px; }
    body.dash .chart-readout > span:first-child { grid-column: 1 / -1; font-family: var(--mono); color: var(--ink-3); }
    body.dash .grid-hist { gap: 12px; }

    /* analysis */
    body.dash #sec-map .card-head, body.dash #sec-pe .card-head { flex-direction: column; align-items: stretch; }
    body.dash #tmMode, body.dash #bubbleMode { display: flex; width: 100%; }
    body.dash .treemap { height: 380px; }
    body.dash .treemap-wrap { padding: 8px; }
    body.dash .perf-list { padding: 4px 6px; }
    body.dash .perf-row { padding: 10px 8px; }
    body.dash .perf-row:active, body.dash .pe-card:active { background: var(--hover); }
    body.dash .grid-2 { gap: 12px; }

    /* holdings: table → app list */
    body.dash #sec-holdings .exhibit-sub { display: none; }
    body.dash .table-wrap { display: none; }
    body.dash .table-tools { display: grid; grid-template-columns: 1fr auto; gap: 8px; padding: 10px 12px; }
    body.dash .table-tools .search { max-width: none; grid-column: 1 / -1; }
    body.dash .table-tools .chips, body.dash .table-tools .seg { overflow-x: auto; flex-wrap: nowrap; scrollbar-width: none; }
    body.dash #typeFilter { grid-column: 1 / -1; }
    body.dash #ccyFilter { justify-self: start; }
    .m-sort { display: flex; align-items: center; gap: 4px; justify-self: end; }
    .m-sort select { appearance: none; -webkit-appearance: none; height: 34px; padding: 0 26px 0 10px; border-radius: 9px; border: 1px solid var(--border); background: var(--card-2) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%238a96a8' stroke-width='2.4' stroke-linecap='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E") no-repeat right 8px center / 12px; font-size: 13px; font-weight: 700; color: var(--ink); }
    .m-sort button { width: 34px; height: 34px; border-radius: 9px; border: 1px solid var(--border); background: var(--card-2); display: grid; place-items: center; color: var(--ink-2); }
    .m-sort button svg { width: 15px; height: 15px; transition: transform .2s; }
    .m-sort button.asc svg { transform: rotate(180deg); }
    .hlist { display: block; }
    .hl-row { display: grid; grid-template-columns: 40px minmax(0, 1fr) auto; gap: 12px; align-items: center; padding: 10px 14px; min-height: 64px; border-bottom: 1px solid var(--border); cursor: pointer; transition: background .12s; }
    .hl-row:last-child { border-bottom: 0; }
    .hl-row:active { background: var(--hover); }
    .hl-row.cash { cursor: default; }
    .hl-sym { font-family: var(--mono); font-weight: 800; font-size: 15px; letter-spacing: -.01em; display: flex; align-items: center; gap: 6px; }
    .hl-sym .pill { font-size: 9.5px; padding: 1px 6px; }
    .hl-sub { font-size: 11.5px; color: var(--ink-3); font-family: var(--mono); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .hl-wt { height: 3px; border-radius: 2px; background: var(--card-2); margin-top: 6px; overflow: hidden; max-width: 140px; }
    .hl-wt i { display: block; height: 100%; background: var(--accent); border-radius: 2px; opacity: .7; }
    .hl-right { text-align: right; }
    .hl-mv { font-family: var(--mono); font-weight: 700; font-size: 14.5px; }
    .hl-chg { display: inline-block; min-width: 72px; text-align: center; margin-top: 4px; padding: 3px 7px; border-radius: 7px; font-family: var(--mono); font-size: 12px; font-weight: 700; }
    .hl-chg.up { background: var(--pos-dim); color: var(--pos); }
    .hl-chg.dn { background: var(--neg-dim); color: var(--neg); }
    .hl-chg.flat { background: var(--card-2); color: var(--ink-3); }
    .hl-empty { padding: 36px 16px; text-align: center; color: var(--ink-3); font-size: 13px; }

    /* valuation */
    body.dash .cape-hero .n { font-size: 52px; }
    body.dash .cape-legend { gap: 6px 12px; font-size: 11px; }
    body.dash .pe-grid { padding: 12px; gap: 10px; }
    body.dash .pe-card { cursor: pointer; }
    body.dash #peFilter { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
}
@media (max-width: 360px) {
    body.dash .hero-side .big { font-size: 27px; }
    body.dash .stat-value { font-size: 14.5px; }
    .tb i { width: 46px; }
}
@media (prefers-reduced-motion: reduce) { .view-in { animation: none; } }
@media print { .tabbar, .ptr, .sheet, .sheet-backdrop, .more-btn { display: none !important; } body.has-tabbar { padding-bottom: 0 !important; } }
