/* Newsfolio — touch / narrow layout: max-width 1023px OR large iPad portrait (portrait iPad ≤1024 wide); landscape ≥1024 uses desktop shell */

/* Hide scrollbars globally but keep scroll functionality */
* {
  scrollbar-width: none; /* Firefox */
  -ms-overflow-style: none; /* IE/Edge */
}

*::-webkit-scrollbar {
  display: none; /* Chrome/Safari */
}

/* Pages that restyle scrollbars — keep bars hidden in light/dark */
.dash-market-pills,
.ticker-dropdown,
.nf-nav-search__dropdown {
  scrollbar-width: none !important;
  -ms-overflow-style: none !important;
}

.dash-market-pills::-webkit-scrollbar,
.ticker-dropdown::-webkit-scrollbar,
.nf-nav-search__dropdown::-webkit-scrollbar {
  display: none !important;
}

.nf-bottom-nav {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 110000;
  isolation: isolate;
  box-sizing: border-box;
  align-items: center;
  justify-content: space-around;
  gap: 0;
  background: var(--bg-card-solid, #0d1117);
  border-top: 1px solid var(--border-subtle, rgba(255, 255, 255, 0.14));
  padding-top: 0.3rem;
  padding-left: env(safe-area-inset-left, 0px);
  padding-right: env(safe-area-inset-right, 0px);
  /* Older iOS; then safe-area, else minimum inset so background reaches the physical bottom */
  padding-bottom: constant(safe-area-inset-bottom);
  padding-bottom: max(16px, env(safe-area-inset-bottom, 0px));
  min-height: 56px;
}

.nf-bottom-nav__link,
.nf-bottom-nav__link * {
  touch-action: manipulation;
  pointer-events: auto;
}

button,
a {
  touch-action: manipulation;
}

.nav-brand-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

.nav-brand-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
}

.nav-mobile-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
}

/* Mobile layout + large iPad portrait (e.g. 12.9" portrait at 1024px wide) */
@media (max-width: 1023px), (max-width: 1366px) and (orientation: portrait) {
  html {
    overflow-x: hidden;
  }

  body {
    overflow-x: hidden;
    font-size: max(14px, 100%);
    background-attachment: fixed;
  }

  /* app.html feed: fixed backgrounds cost compositor work while scrolling long lists (all feed tabs). */
  body.nf-app {
    background-attachment: scroll !important;
  }

  .nav-brand-left {
    flex: 1;
  }

  /* Bottom tab bar handles nav — hide top links + hamburger (mobile + iPad portrait). */
  .nav-links {
    display: none !important;
  }

  .nav-mobile-toggle {
    display: none !important;
  }

  .nav-mobile-toggle-icon {
    display: block;
    width: 22px;
    height: 2px;
    background: currentColor;
    border-radius: 1px;
    box-shadow: 0 7px 0 currentColor, 0 14px 0 currentColor;
  }

  html[data-theme="pro"] .nav-mobile-toggle {
    background: rgba(255, 255, 255, 0.85);
    color: var(--text, #152022);
    border-color: rgba(11, 58, 58, 0.22);
  }

  body.nf-app nav:not(.nf-bottom-nav) {
    position: relative;
    z-index: 200;
  }

  body.nf-app nav.nav-open .nav-links {
    background: var(--bg-card-solid, #0d1117);
    border: 1px solid var(--border-subtle, rgba(255, 255, 255, 0.12));
    border-radius: 0 0 12px 12px;
    padding: 0.65rem 0.85rem 0.85rem;
    box-shadow: 0 16px 40px rgba(0, 0, 0, 0.45);
  }

  html[data-theme="pro"] body.nf-app nav.nav-open .nav-links {
    background: var(--bg-card-solid, #f2f8f9);
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.12);
  }

  nav:not(.nf-bottom-nav) {
    flex-wrap: wrap;
    align-items: center;
    row-gap: 0;
  }

  .nav-brand {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
  }

  .nav-brand--auth {
    width: 100%;
  }

  .nav-links {
    width: 100%;
    flex-basis: 100%;
    flex-direction: column;
    align-items: stretch;
    align-content: flex-start;
    gap: 0;
    padding: 0.75rem 0 0.25rem;
    margin: 0;
    border-top: 1px solid var(--border-subtle, rgba(255, 255, 255, 0.08));
  }

  nav.nav-open .nav-links {
    display: none;
  }

  .nf-bottom-nav {
    display: flex !important;
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    z-index: 110000 !important;
    padding-left: max(0.35rem, env(safe-area-inset-left, 0px));
    padding-right: max(0.35rem, env(safe-area-inset-right, 0px));
    background: rgba(8, 14, 16, 0.97);
    border-top-color: rgba(255, 255, 255, 0.14);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
  }

  html[data-theme="pro"] .nf-bottom-nav {
    background: #ffffff;
    border-top: 1px solid rgba(45, 85, 90, 0.15);
  }

  html[data-theme="pro"] .nf-bottom-nav__label {
    color: #5a6d70;
  }

  html[data-theme="pro"] .nf-bottom-nav__link.is-active .nf-bottom-nav__label,
  html[data-theme="pro"] .nf-bottom-nav__link[aria-current="page"] .nf-bottom-nav__label {
    color: #0b3a3a;
  }

  html[data-theme="default"] .nf-bottom-nav {
    background: #ffffff !important;
    border-top: 0.5px solid #efefef !important;
  }

  html[data-theme="default"] .nf-bottom-nav__label {
    color: #6b7280;
  }

  html[data-theme="default"] .nf-bottom-nav__link.is-active,
  html[data-theme="default"] .nf-bottom-nav__link[aria-current="page"] {
    color: #5ba4cf;
  }

  html[data-theme="default"] .nf-bottom-nav__link.is-active .nf-bottom-nav__label,
  html[data-theme="default"] .nf-bottom-nav__link[aria-current="page"] .nf-bottom-nav__label {
    color: #5ba4cf;
  }

  .nf-bottom-nav__link {
    flex: 1 1 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.12rem;
    min-width: 0;
    padding: 0.2rem 0.25rem;
    font-size: 0.625rem;
    font-weight: 600;
    letter-spacing: 0.03em;
    color: rgba(200, 220, 222, 0.52);
    text-decoration: none;
    font-family: inherit;
    border-radius: 10px;
    transition: color 0.2s ease, background 0.2s ease;
    -webkit-tap-highlight-color: transparent;
  }

  .nf-bottom-nav__link.is-active,
  .nf-bottom-nav__link[aria-current="page"] {
    color: var(--teal-light, #5BA4CF);
  }

  .nf-bottom-nav__icon {
    font-size: 1.28rem;
    line-height: 1;
  }

  .nf-bottom-nav__label {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
  }

  @media (max-width: 390px) {
    .nf-bottom-nav__label,
    .bottom-nav .nav-label {
      font-size: 10px;
    }
  }

  /*
   * Footer + bottom tab bar: pad the main shell so the last content clears the fixed tab bar.
   * Extra bottom inset for the home indicator is on .nf-bottom-nav (padding).
   */
  html:has(body.nf-app),
  html:has(body.nf-stock) {
    height: auto !important;
    max-height: none !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
  }

  body.nf-app,
  body.nf-stock {
    height: auto !important;
    max-height: none !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
  }

  /* Primary content wrappers: dashboard / portfolio / social / communities (all use .page) + stock page.
     No min-height: 100dvh/+1px — that inflated the layout viewport on iOS and made fixed bottom:0
     tab bar sit above the visible screen. Content height + padding-bottom clears the nav. */
  body.nf-app .page,
  body.nf-stock .nf-stock-page {
    flex: 0 0 auto !important;
    min-height: unset !important;
    overflow: visible !important;
    padding-bottom: 5rem !important;
    box-sizing: border-box;
  }

  body.nf-app .page .site-footer {
    margin-top: 1.25rem !important;
  }

  body.nf-app #pulse-main,
  body.nf-app .dash-shell {
    flex: 0 0 auto !important;
  }

  body.nf-app #comm-app:not(.comm-mobile-chat-open) {
    flex: 0 0 auto !important;
  }

  .nav-links a {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    padding: 0.35rem 0.25rem;
    font-size: max(0.875rem, 14px);
  }

  .nav-links .live-badge {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    align-self: flex-start;
    margin-top: 0.35rem;
    font-size: max(0.75rem, 13px);
  }

  .profile-panel {
    width: 100% !important;
    max-width: 100% !important;
    left: 0;
    right: 0;
    border-left: none;
  }

  .profile-close-btn {
    min-width: 44px;
    min-height: 44px;
  }

  .profile-avatar-btn {
    min-width: 44px;
    min-height: 44px;
  }

  /* —— app.html News Feed: ticker chips replace mobile holdings drawer; sidebar stays on desktop —— */
  body.nf-app .app-main-grid {
    flex: 0 0 auto !important;
    min-height: 0 !important;
    grid-template-columns: 1fr !important;
    grid-template-rows: auto !important;
  }

  body.nf-app .feed-column {
    order: 1;
    min-height: 0 !important;
    flex: 0 0 auto !important;
    height: auto !important;
    overflow: visible !important;
  }

  /* Match app.html #feed-panel.news-feed — natural height; page scrolls (no inner flex slice on tablet / half-window). */
  body.nf-app #feed-panel.news-feed.feed-scroll {
    flex: none !important;
    min-height: 0 !important;
    max-height: none !important;
    height: auto !important;
    margin-top: 0.35rem !important;
    padding-top: 0.2rem !important;
    padding-bottom: calc(80px + env(safe-area-inset-bottom, 0px)) !important;
    overflow-x: hidden !important;
    overflow-y: visible !important;
    -webkit-overflow-scrolling: touch !important;
    overscroll-behavior-y: auto;
  }

  body.nf-app .feed-column #feed-panel .feed-wrap {
    flex: none !important;
    min-height: 0 !important;
    height: auto !important;
  }

  body.nf-app .holdings-column {
    display: none !important;
  }

  .feed-filter-row {
    flex-wrap: nowrap !important;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    gap: 0.45rem;
    padding-bottom: 0.35rem;
    margin-bottom: 0.65rem;
  }

  .feed-filter-pill {
    flex-shrink: 0;
    min-height: 44px;
    padding: 0.45rem 0.85rem;
    font-size: max(0.8125rem, 13px);
  }

  .feed-tab {
    min-height: 44px;
  }

  .card {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
  }

  .card-meta,
  .card-badge,
  .refresh-hint {
    font-size: max(0.875rem, 14px);
  }

  .card-headline {
    font-size: max(1.02rem, 16px);
  }

  .btn-ai-summary {
    min-height: 44px;
    font-size: max(0.72rem, 12px);
    padding: 0.45rem 0.85rem;
  }

  /* —— portfolio.html —— */
  .holdings-table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
  }

  .holdings-table {
    min-width: 620px;
  }

  .holdings-sort-bar {
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    gap: 0.45rem;
    padding-bottom: 0.4rem;
    margin-bottom: 0.65rem;
  }

  .holdings-sort-btn {
    flex-shrink: 0;
    min-height: 44px;
    padding: 0.45rem 0.75rem;
    font-size: max(0.8125rem, 14px);
  }

  .line-chart-toolbar {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    gap: 0.4rem;
    padding-bottom: 0.35rem;
  }

  .line-chart-period-btn {
    flex-shrink: 0;
    min-height: 44px;
    min-width: 44px;
    font-size: max(0.72rem, 13px);
  }

  .line-wrap {
    width: 100%;
    height: min(220px, 42vw);
    min-height: 180px;
  }

  .pie-wrap {
    width: min(260px, 88vw);
    height: min(260px, 88vw);
    max-width: 100%;
    margin-left: auto;
    margin-right: auto;
  }

  .main-grid .card {
    min-width: 0;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
  }

  .import-screenshot-card,
  .import-dropzone {
    max-width: 100%;
    box-sizing: border-box;
  }

  .import-preview-table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
  }

  .holdings-refresh-btn {
    min-width: 44px;
    min-height: 44px;
  }

  .btn-edit,
  .btn-remove {
    min-width: 44px;
    min-height: 44px;
    box-sizing: border-box;
  }

  /* —— grading.html —— */
  .grade-grid {
    grid-template-columns: 1fr !important;
  }

  .grade-hero {
    text-align: center;
    padding: 1.5rem 1rem 1.75rem;
  }

  .grade-hero-score {
    font-size: clamp(2.85rem, 12vw, 3.75rem);
    display: block;
    text-align: center;
  }

  .grade-ai-section {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
  }

  .grade-ai-cards-grid {
    grid-template-columns: 1fr !important;
  }

  /* —— social.html —— */
  .social-top-tabs.feed-tabs {
    width: 100%;
  }

  .social-top-tabs .feed-tab {
    min-height: 48px;
    min-width: 0;
    flex: 1 1 calc(50% - 0.35rem);
    font-size: max(0.88rem, 14px);
  }

  .social-tab-panel--feed {
    max-height: none;
  }

  .feed-composer-card.card {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
  }

  #panel-communities .social-shell {
    flex-direction: column;
    min-height: min(72vh, 720px);
  }

  .social-sidebar {
    display: none !important;
  }

  .social-room-tabs {
    display: flex !important;
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    gap: 0.45rem;
    width: 100%;
    max-width: 100%;
    margin: 0 0 0.5rem;
    padding: 0.15rem 0 0.45rem;
    order: 1;
  }

  .room-tab {
    flex-shrink: 0;
    min-height: 44px;
    padding: 0.5rem 0.85rem;
    font-size: max(0.8rem, 14px);
  }

  .social-main {
    order: 2;
    width: 100%;
    min-width: 0;
    min-height: 280px;
  }

  .btn-feed-post,
  .btn-feed-secondary,
  .btn-send {
    min-height: 44px;
    font-size: max(0.88rem, 14px);
  }

  .post-card.card {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
  }

  .feed-posts-scroll {
    width: 100%;
    min-width: 0;
  }

  /* —— auth.html —— */
  .auth-card {
    max-width: none;
    width: 100%;
    box-sizing: border-box;
    padding-left: max(1rem, env(safe-area-inset-left));
    padding-right: max(1rem, env(safe-area-inset-right));
  }

  .auth-tab {
    min-height: 44px;
    font-size: max(0.88rem, 14px);
  }

  .btn-submit {
    min-height: 44px;
    font-size: max(0.92rem, 15px);
  }

  .field input {
    font-size: max(0.9rem, 16px);
  }

  .field label {
    font-size: max(0.7rem, 12px);
  }

  /* —— dashboard.html (home) —— */
  .dashboard {
    grid-template-columns: 1fr !important;
    gap: 1.5rem !important;
    padding-bottom: 1.5rem;
  }

  .hero-cta {
    flex-direction: column;
    align-items: stretch;
    width: 100%;
  }

  .hero-cta .btn {
    width: 100%;
    min-height: 48px;
    justify-content: center;
    box-sizing: border-box;
  }

  .dashboard-card {
    min-width: 0;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
  }

  .pie-wrap-dashboard {
    width: min(240px, 75vw) !important;
    height: min(240px, 75vw) !important;
    margin: 0 auto;
  }

  .how-step h3 {
    font-size: max(1rem, 16px);
  }

  .how-step p {
    font-size: max(0.88rem, 14px);
  }

  /* —— communities.html —— native chat: locked viewport, internal message scroll (mobile bundle) —— */
  :root {
    --top-nav-h: 56px;
    --bottom-nav-h: 56px;
  }

  html:has(body.nf-app #comm-app.comm-mobile-chat-open) {
    overflow: hidden !important;
    height: 100dvh !important;
    max-height: 100dvh !important;
  }

  body.nf-app:has(#comm-app.comm-mobile-chat-open) {
    overflow: hidden !important;
  }

  /* Bottom padding reserves space for fixed .nf-bottom-nav; flex children fill the rest (keyboard: 100dvh shrinks) */
  body.nf-app .page:has(#comm-app.comm-mobile-chat-open) {
    flex: 1 1 0 !important;
    min-height: 100dvh !important;
    max-height: 100dvh !important;
    height: 100dvh !important;
    overflow: hidden !important;
    padding-bottom: var(--bottom-nav-h, 56px) !important;
    display: flex !important;
    flex-direction: column !important;
    box-sizing: border-box !important;
  }

  body.nf-app .page:has(#comm-app.comm-mobile-chat-open) .site-footer {
    display: none !important;
  }

  body.nf-app .page:has(#comm-app.comm-mobile-chat-open) > nav:not(.nf-bottom-nav) {
    flex-shrink: 0 !important;
  }

  body.nf-app .page:has(#comm-app.comm-mobile-chat-open) #comm-app.comm-shell {
    flex: 1 1 0 !important;
    min-height: 0 !important;
    width: 100% !important;
    max-width: 100% !important;
    margin-left: 0 !important;
    margin-right: 0 !important;
    border-radius: 0 !important;
    overflow: hidden !important;
  }

  body.nf-app .page:has(#comm-app.comm-mobile-chat-open) .comm-chat-panel {
    flex: 1 1 0 !important;
    min-height: 0 !important;
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden !important;
  }

  /*
   * Mobile chat shell: flex column fills #comm-app; .page uses 100dvh + bottom padding for tab bar.
   * No position:fixed on input — keyboard visibility follows 100dvh / visual viewport.
   */
  body.nf-app .page:has(#comm-app.comm-mobile-chat-open) .comm-chat-shell {
    display: flex !important;
    flex-direction: column !important;
    flex: 1 1 0 !important;
    min-height: 0 !important;
    overflow: hidden !important;
    box-sizing: border-box !important;
  }

  body.nf-app .page:has(#comm-app.comm-mobile-chat-open) #comm-messages.comm-messages-list {
    flex: 1 1 auto !important;
    min-height: min(56dvh, 720px) !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    overscroll-behavior: contain !important;
  }

  body.nf-app .page:has(#comm-app.comm-mobile-chat-open) .comm-input-bar {
    flex-shrink: 0 !important;
  }

  body.nf-app .page:has(#comm-app.comm-mobile-chat-open) .comm-composer.comm-input-bar {
    padding-bottom: 0.75rem !important;
    position: relative !important;
    bottom: auto !important;
    z-index: auto !important;
  }

  body.nf-app .page:has(#comm-app.comm-mobile-chat-open) .comm-chat-status {
    flex-shrink: 0 !important;
  }

  body.nf-app .page:has(#comm-app.comm-mobile-chat-open) .comm-username-gate {
    flex-shrink: 0 !important;
  }
}

/* app.html — News Feed: same as ≤1023 shell above; kept for clarity / cascade tie-break. */
@media (max-width: 1023px) {
  body.nf-app #feed-panel.news-feed.feed-scroll {
    flex: none !important;
    height: auto !important;
    max-height: none !important;
    min-height: unset !important;
    overflow-y: visible !important;
    -webkit-overflow-scrolling: touch !important;
    overscroll-behavior-y: auto;
  }
  body.nf-app .feed-column #feed-panel .feed-wrap {
    flex: none !important;
    min-height: unset !important;
  }
  body.nf-app .feed-column {
    flex: 0 0 auto !important;
    min-height: unset !important;
    overflow: visible !important;
  }
}

/* portfolio.html — holdings table (narrow phones): ticker on one line, sort row scrolls */
@media (max-width: 767px) {
  .holdings-sort-bar {
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
  }

  .holdings-sort-btn {
    flex-shrink: 0;
  }

  .holdings-table .ticker-badge {
    white-space: nowrap;
    min-width: 52px;
    max-width: none;
    word-break: normal;
    overflow-wrap: normal;
  }

  .line-wrap,
  .chart-wrap {
    overflow: hidden;
    width: 100%;
  }
}

/* Wide landscape — desktop shell from 1024px (see max-width 1023 mobile bundle above) */
@media (min-width: 1024px) and (orientation: landscape) {
  .nf-bottom-nav {
    display: none !important;
  }

  .holdings-drawer-toggle {
    display: none !important;
  }

  .holdings-drawer-panel {
    display: contents;
  }
}
