/* ---- PWA standalone (installed app) — respect the notch/status bar ----
   Only applies when launched from the home screen (display:standalone). We opted
   into black-translucent + viewport-fit=cover, so content would otherwise render
   under the iOS status bar; pad the header and the fixed hamburger to clear it. */
@media (display-mode: standalone) {
  header { padding-top: calc(18px + env(safe-area-inset-top)); }
  .mobile-nav-toggle { top: calc(12px + env(safe-area-inset-top)); }
  .topbar-right { top: calc(44px + env(safe-area-inset-top)); }
}

/* ---- Responsive ---- */
@media (max-width: 900px) {
  #cards-grid .card { grid-template-columns: 38px 1fr auto; }
  #cards-grid .card canvas.sparkline { display: none; }
  #cards-grid .card .prices { display: none; }
}
@media (max-width: 768px) {
  .sidebar {
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    z-index: 1000;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
  }
  .sidebar.open {
    transform: translateX(0);
  }
  /* On mobile the sidebar is a slide-in overlay; desktop collapse doesn't apply */
  .sidebar.collapsed {
    width: 220px;
  }
  .sidebar.collapsed .sidebar-title,
  .sidebar.collapsed .nav-label {
    display: inline;
  }
  .sidebar.collapsed .nav-item {
    justify-content: flex-start;
    gap: 10px;
    padding: 10px 14px;
  }
  .sidebar-collapse-btn {
    display: none;
  }
  .sidebar-toggle {
    display: none;
  }
  /* On-screen hamburger + backdrop (the in-sidebar toggle slides off-screen) */
  .mobile-nav-toggle {
    display: flex;
  }
  .sidebar.open ~ .mobile-nav-toggle {
    display: none;
  }
  .sidebar-backdrop {
    display: block;
  }
  .main-wrapper {
    width: 100%;
    /* Scroll is handled by the base .main-wrapper rule (universal safety-net
       scroller) at every breakpoint — no per-breakpoint override needed. */
  }
  /* Shrink the fixed top-right bar so it doesn't crowd longer page titles
     (e.g. "Trade History"): drop the username text, keep the avatar + logout. */
  .sidebar-user-name { display: none; }
  /* Anchor the top-right controls to the very top of the viewport. At the
     desktop default (top:44px) the fixed bar hangs DOWN over the header's
     second row (Dip Finder tabs) and over the first content card on
     single-row-header pages (Trades' source box). Pinning it to the top keeps
     the Sign-in button beside the title instead of on top of content below.
     env(safe-area-inset-top) clears the notch in standalone/PWA and is 0 in a
     normal mobile browser; this rule comes after the display-mode:standalone
     block above, so it wins for mobile. */
  /* !important beats the inline `style.top = '0'` the auth handler sets on this
     bar (a desktop-only tweak to close the gap when the demo banner hides). On
     mobile the bar always sits at the safe-area offset so it clears the notch. */
  /* Center a 44px-tall control inside the ~56px app bar (below the notch):
     top = safe-area + (56 − 44)/2 = safe-area + 6. Both the top-right cluster
     and the hamburger now sit fully INSIDE the header bar instead of hanging
     below its bottom edge. */
  /* !important on height/padding too: the base .topbar-right rule sits LATER in
     the file, so (media queries adding no specificity) it wins on source order
     otherwise — the same reason `top` needs !important here. Without this the
     bar keeps its 56px desktop height and hangs below the 56px app bar. */
  .topbar-right { top: calc(env(safe-area-inset-top) + 6px) !important; gap: 6px !important; padding: 4px 8px !important; height: 44px !important; }
  .mobile-nav-toggle { top: calc(env(safe-area-inset-top) + 6px); }
  /* Trim the Sign-in button so it can't sprawl into the title on narrow phones. */
  .auth-btn { padding: 6px 10px; font-size: 0.78rem; }
  /* Home (landing) page has no <header> app-bar — its hero sits directly under
     the fixed burger + sign-in (top: safe-area + 6px, 44px tall → reaches
     ~safe-area + 50px). Pad the content down so the logo/title start below that
     control strip instead of hiding under the burger. */
  .landing-wrapper {
    padding-top: calc(64px + env(safe-area-inset-top));
  }
  /* Mobile app bar: the header is a sticky top bar that the fixed hamburger
     (top-left) and top-right controls (theme/auth) visually sit inside — so
     they read as part of the bar instead of floating over page content.
     padding-top carries env(safe-area-inset-top): 0 in a normal browser, the
     notch height in an installed PWA (so the title/controls clear the iOS
     status bar). padding-left/right reserve room for the two fixed controls. */
  header {
    position: sticky;
    top: 0;
    z-index: 900;
    padding-top: calc(14px + env(safe-area-inset-top));
    padding-bottom: 14px;
    padding-left: 60px;
    padding-right: 96px;
    /* The fixed hamburger + top-right controls (44px tall, pinned at top:6px)
       reach y≈50px. The bar must extend past that so they sit INSIDE it, not
       hanging below its bottom edge. Center the title against the control row. */
    min-height: calc(56px + env(safe-area-inset-top));
    display: flex;
    align-items: center;
    box-sizing: border-box;
  }
  /* Watch List / Dip Finder headers carry a second child (.dip-tab-bar). The
     app-bar flex above lays children in a ROW, so without this the tabs sit
     BESIDE the title on the same line (works on Holdings, which has a single
     child). Stack the two children in a column so the title row still centers
     against the burger + sign-in, and the tabs drop underneath — matching
     Holdings. Keep the title row itself the min-height of the control row so
     the title stays vertically aligned with the burger/sign-in. */
  header:has(.dip-tab-bar) {
    flex-direction: column;
    align-items: stretch;
    /* Anchor the stack to the top (not centered): otherwise the tab bar's height
       shoves the title row upward, off the burger/sign-in line. Match the fixed
       control strip's top offset (top: safe-area + 6px) so the title row sits in
       the same band as the burger + sign-in. */
    justify-content: flex-start;
    padding-top: calc(6px + env(safe-area-inset-top));
    /* padding-bottom:0 (from the desktop :has rule) lets the tab bar's own
       border act as the track; re-add a little breathing room above it. */
    padding-bottom: 0;
  }
  /* Title row = the 44px-tall control strip band; centering the title in it lines
     it up vertically with the burger (left) and sign-in (right), matching the
     single-child Holdings/Trades headers. */
  header:has(.dip-tab-bar) .header-inner {
    min-height: 44px;
    align-items: center;
  }
  /* Tabs go edge-to-edge and can scroll horizontally if they don't fit. */
  header:has(.dip-tab-bar) .dip-tab-bar {
    margin-top: 0.5rem;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  /* Holdings: stack summary column above the charts instead of side-by-side */
  .portfolio-top { grid-template-columns: 1fr; }
  .summary-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  /* Table cards must NOT try to fill the viewport with an inner scroll on
     mobile. The base rule (flex:1; min-height:0) works on desktop where the
     table is the main content, but on Holdings the tall chart card above it
     leaves ~0 height, collapsing the Holdings table to an unreachable sliver
     (the "table doesn't load at the bottom" bug). Let the card size to its
     content and the page (#main-scroll) own the vertical scroll; the wrapper
     keeps only horizontal scroll for wide tables. */
  .card:has(.data-table-wrapper) {
    flex: none;
    min-height: 0;
  }
  .card:has(.data-table-wrapper) .data-table-wrapper {
    flex: none;
    min-height: 0;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  /* Holdings table on mobile: too many columns to fit, so show only the
     essentials — Ticker (1) plus Gain/Loss (7), Return % (8), Weight (9).
     Hide the middle columns (Shares, Current Price, Current Value, Avg Cost,
     Cost Basis = 2–6). Cells carry no per-column class, so target by position.
     This lets the table fit the screen without horizontal scrolling. */
  .holdings-table th:nth-child(n+2):nth-child(-n+6),
  .holdings-table td:nth-child(n+2):nth-child(-n+6) {
    display: none;
  }
  /* Ticker popup goes full-screen on mobile: edge-to-edge, no rounded corners,
     and the modal chrome (backdrop centering) is irrelevant since it fills the
     viewport. The header stays pinned (flex-shrink:0) and .ticker-info-body
     owns the internal scroll — combined with the body scroll-lock added in
     ticker-modal.js, the page behind no longer scrolls. Uses dvh so the iOS
     browser chrome doesn't clip the bottom. */
  #ticker-modal.modal {
    align-items: stretch;
    justify-content: stretch;
  }
  .ticker-info-modal {
    width: 100%;
    max-width: none;
    height: 100vh;
    height: 100dvh;
    max-height: none;
    border-radius: 0;
    border: none;
    padding: 1.25rem;
    padding-top: calc(1rem + env(safe-area-inset-top));
    padding-bottom: calc(1rem + env(safe-area-inset-bottom));
  }
  /* Full-screen mobile popup: pin the close (×) to the top-right corner so it's
     always reachable, and let the header anchor it. The title truncates with an
     ellipsis so a long "TICKER — Company Name" never pushes the × off-screen or
     collides with it. */
  .ticker-info-modal .modal-header {
    position: relative;
    padding-right: 48px;
  }
  .ticker-info-modal .modal-header h2 {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-width: 0;
  }
  .ticker-info-modal .modal-close {
    position: fixed;
    top: calc(0.75rem + env(safe-area-inset-top));
    right: 0.75rem;
    z-index: 1;
  }
  /* Watch List: collapse the two-pane layout into a single column.
     The base .wl-layout/.wl-sidebar rules appear LATER in the file with equal
     specificity, so they'd win over these overrides on source order alone.
     Scoping to #wl-tab-detail/#wl-tab-compare (id) raises specificity so the
     mobile stack reliably wins. overflow:visible lets the page (not each pane)
     own the scroll, and the sidebar border moves to the bottom edge. */
  #wl-tab-detail.wl-layout,
  #wl-tab-compare.wl-layout {
    flex-direction: column;
    overflow: visible;
    /* Watch List has no #main-scroll gutter, so its panes would otherwise touch
       the screen edges. Add a small horizontal inset so content breathes from
       the sides on mobile (matches the ~12px gutter other pages get). */
    padding: 0 12px;
    box-sizing: border-box;
  }
  #wl-tab-detail .wl-sidebar,
  #wl-tab-compare .wl-sidebar {
    width: 100%;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
  /* Phone ticker list: a grid of compact logo+ticker cards instead of a tall
     one-per-row list. This keeps the list short so the detail pane below it is
     reachable (the old full-height list pushed detail ~1000px down and crushed
     it). flex:none + visible overflow lets the whole page scroll naturally. */
  .wl-ticker-list {
    flex: none;
    overflow-y: visible;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(76px, 1fr));
    gap: 8px;
    padding: 12px;
  }
  /* Section dividers ("Holdings" / "Watching") span the full grid row. */
  .wl-ticker-list .wl-section-label {
    grid-column: 1 / -1;
    border-top: none;
    margin-top: 0;
    padding: 4px 2px 0;
  }
  /* Each ticker becomes a centered card: logo on top, ticker below. */
  .wl-ticker-list .wl-item {
    display: flex;
    flex-direction: column;
    grid-template-columns: none;
    gap: 6px;
    padding: 10px 6px;
    border: 1px solid var(--border);
    border-left: 1px solid var(--border);
    border-radius: 10px;
    background: var(--surface);
    position: relative;
  }
  .wl-ticker-list .wl-item-active {
    border-color: var(--accent);
    background: var(--accent-soft);
    box-shadow: 0 0 0 1px var(--accent);
  }
  .wl-ticker-list .wl-item-logo { width: 30px; height: 30px; }
  .wl-ticker-list .wl-item-ticker {
    flex: none;
    text-align: center;
    font-size: 0.95rem;
    line-height: 1.1;
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .wl-ticker-list .wl-item-value {
    flex: none;
    width: auto;
    justify-self: auto;
    text-align: center;
    font-size: 0.66rem;
    line-height: 1;
  }
  .wl-ticker-list .wl-item-value-spacer,
  .wl-list-header .wl-item-value-spacer { display: none; }
  /* Remove (×) tucks into the card's top-right corner. */
  .wl-ticker-list .wl-item-remove {
    position: absolute;
    top: 2px;
    right: 4px;
    opacity: 0.5;
    padding: 2px 4px;
  }
  /* Detail/compare panes must size to their content and let the whole page
     scroll — NOT stay a flex:1 pane with its own inner scroll. The base rule
     (flex:1; overflow-y:auto) crushed the pane to a ~48px sliver on mobile so
     the chart/metrics were hidden inside it. flex:none + height:auto + visible
     overflow lets it grow naturally beneath the ticker grid.
     Scoped to #wl-tab-* (id) so it beats the base .wl-detail/.wl-compare rules,
     which appear LATER in the file and would otherwise win on source order. */
  #wl-tab-detail .wl-detail,
  #wl-tab-compare .wl-compare {
    flex: none;
    height: auto;
    overflow: visible;
    padding: 16px;
  }
  .wl-detail-placeholder { height: auto; padding: 32px 0; }
  .wl-autocomplete { width: 100%; max-width: calc(100vw - 24px); }
  /* Period selectors wrap instead of overflowing the row */
  .period-selector { flex-wrap: wrap; justify-content: center; }
}
@media (max-width: 600px) {
  #main-scroll { padding: 12px; }
  /* Keep the hamburger/topbar clearance AND the safe-area padding-top from the
     768px block — only shrink the bottom padding here. (A bare
     `padding: 10px 14px` would reset padding-top and slide the title back under
     the notch, and reset left/right and slide it under the hamburger.) */
  header { padding-bottom: 10px; }
  .controls { flex-direction: column; }
  .ctrl-group { width: 100%; }
  .data-table { font-size: 0.75rem; }
  .data-table th, .data-table td { padding: 8px; }
  /* Wide tables scroll horizontally with momentum instead of clipping */
  .data-table-wrapper { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  /* Trades table: pack it into the narrow screen so it needs little/no
     horizontal scroll. The biggest offenders were the date wrapping to three
     lines ("10-/Jun-/2026") and generous cell padding. Keep dates on one line,
     tighten padding + font, and shrink the emoji action buttons. */
  #trades-table { font-size: 0.72rem; }
  #trades-table th, #trades-table td { padding: 6px 6px; }
  #trades-table td:first-child, #trades-table th:first-child { white-space: nowrap; }
  #trades-table .btn-icon { padding: 2px 3px; font-size: 0.85rem; }
  /* Trades add-form: let the 7-field row wrap onto multiple lines */
  .trade-form-inline { flex-wrap: wrap; }
  .form-group-inline { flex: 1 1 45%; }
  /* Bigger touch targets for the small icon buttons */
  .theme-toggle, .modal-close { min-width: 40px; min-height: 40px; }
  .period-btn, .ticker-period-btn { min-height: 36px; }
  /* Sign-in prompt must fit a narrow viewport */
  .signin-prompt { right: 8px; left: 8px; width: auto; }
  /* Watch List remove (×) is hover-only — reveal it on touch devices */
  .wl-item-remove { opacity: 0.6; }
   .summary-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
   .summary-value { font-size: 0.95rem; }
  .summary-value { overflow: visible; text-overflow: clip; }
  /* Info icon must never wrap onto its own line, away from the label text. */
  .summary-label { white-space: nowrap; }
  .summary-label .info-icon { vertical-align: baseline; }
  /* Period selector: 11 buttons wrapped leaving a lone "ALL" centered on its
     own line. Tighten spacing and let the duration hint take the full width. */
  .period-selector { gap: 4px; }
  .period-btn { padding: 4px 8px; font-size: 0.65rem; }
  #period-duration { flex-basis: 100%; text-align: center; margin-left: 0; margin-top: 4px; }
}
