/* ================================================================
   RESPONSIVE DESIGN
   Breakpoints:
     - 1120, 1040, 960, 860px: The top bar gives up labels before the tabs scroll
     - 1024px: Tablet landscape / small desktop
     - 768px:  Tablet portrait / iPad
     - 480px:  Mobile phones
   ================================================================ */

/* --- The top bar is one row. Its tabs tighten first, then it gives up Feedback's
   word, then the name, then the chip's trade count, and only then do the tabs
   scroll inside their row. --- */
@media (max-width: 1200px) {
  .cc-tabs { gap: 2px; }
  .cc-tab { padding: 0 12px; font-size: var(--text-sm); }
}
@media (max-width: 1040px) {
  .header-feedback-btn { width: 34px; padding: 0; }
  .header-feedback-btn__label { display: none; }
}
@media (max-width: 960px) {
  .header-title { display: none; }
}
@media (max-width: 860px) {
  .header-guardrail-chip .gr-sep,
  .header-guardrail-chip .gr-sep + .gr-val { display: none; }
}

/* The plan pill (the trial countdown, or Choose a plan) takes about 130 px. While
   it shows, the chip gives up its trade count at 1120 and hides at 860, so the
   tabs still fit. Row width needed with a 5-digit limit: 986 px, 919 without
   Feedback's word, 820 without the name, 655 without the chip, and 625 at the
   tighter padding from 768 down. Under 625 the tabs scroll, with an edge fade
   (layout-dashboard.css). The margin is for a longer chip, such as
   "P&L missing / $100,000". */
@media (max-width: 1120px) {
  .header-right.has-plan-pill .header-guardrail-chip .gr-sep,
  .header-right.has-plan-pill .header-guardrail-chip .gr-sep + .gr-val { display: none; }
}
@media (max-width: 860px) {
  .header-right.has-plan-pill .header-guardrail-chip { display: none; }
}

/* A lapsed member's workspace is inert except the header, and the tabs sit in
   the header now, so they hide at every width. */
html.mac-paywall-active .cc-tabs { display: none !important; }

/* --- 1024px: Tablet landscape --- */
@media (max-width: 1024px) {
  .center-under-tagline { left: 0 !important; }

  /* Overview: Market Snapshot 6-col → 3-col */
  .ov-snap-grid { grid-template-columns: repeat(3, 1fr) !important; }
  /* Analysis: stat cards 4-col → 2-col */
  /* Analysis: 2-col panels → 1-col */
  /* Scanner: scan results cards */
  }

/* --- 768px: Tablet portrait / iPad --- */
@media (max-width: 768px) {
  :root { --mac-header-h: 52px; }

  .header {
    padding: env(safe-area-inset-top) 12px 0;
    gap: 8px;
  }
  .header-left { gap: 8px; }
  .header-right .last-updated { display: none; }
  /* Hide text labels on header buttons, keep icons */
  .header-right .refresh-btn {
    padding: 8px 10px;
    font-size: 0; /* hide text */
    min-height: 36px;
  }
  .header-right .refresh-btn svg { font-size: initial; }
  /* Keep Sign Out visible */
  .header-right .refresh-btn[onclick="logoutUser()"] { font-size: var(--text-sm); padding: 8px 12px; }
  /* #user-email lives inside the avatar dropdown now — keep it visible on
     phones; the avatar button itself is already compact. */

  /* The workspace rail is the primary mobile navigation. Keep its compact
     horizontal rhythm, but meet the 44px touch target and snap each tab into
     a stable reading position while the rail scrolls. */
  .cc-tabs { scroll-snap-type: x proximity; }
  .cc-tabs { padding: 2px; }
  .cc-tab {
    min-height: 44px;
    padding: 0 10px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    scroll-snap-align: start;
    touch-action: manipulation;
  }

  /* iOS Safari zooms the page when a focused form control is below 16px.
     Keep the terminal compact everywhere else, but never make a member fight
     an unexpected viewport zoom while entering a ticker, plan, or trade. */
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]),
  select,
  textarea {
    font-size: 16px !important;
  }

  .card { border-radius:var(--radius-md); padding: 16px; }
  .card-header-bar { font-size: var(--text-2xl) !important; white-space: normal !important; }
  .toggle-label { display: none !important; }

  /* Overview: Market Snapshot 3-col → 2-col */
  .ov-snap-grid { grid-template-columns: repeat(2, 1fr) !important; }
  /* Overview: Econ calendar horizontal → vertical stack */
  .ov-econ-grid { grid-template-columns: 1fr !important; }
  /* Overview: Mindset rules 2-col → 1-col */
  .ov-mindset-cols { columns: 1 !important; }
  /* Overview: Heatmap table — horizontal scroll */
  /* Scanner: Strategy grid → stacked on mobile */
  .scanner-strategy-grid { grid-template-columns: 1fr !important; }
  /* Scanner: Top 100 table → horizontal scroll */
  .sc-table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .sc-table-row { min-width: 560px; }

  /* Trade Journal: 2-col → stacked */
  .tj-day-grid { grid-template-columns: 1fr !important; }
  /* Trade Journal: keep the five trading days in-view. Weekly totals remain
     available on desktop; hiding that sixth summary column on phones avoids
     a mystery horizontal swipe just to reach Friday. */
  #cal-grid { overflow-x: clip; max-width: 100%; min-width: 0; }
  #cal-grid > div { min-width: 0; }
  .journal-calendar-row { grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 4px; }
  .journal-calendar-week-header,
  .journal-calendar-week-total { display: none; }
  /* Trade Journal: Calendar stat cells */
  .rv-stat-grid { grid-template-columns: repeat(4, 1fr); }
  /* Trade Journal: Recap results 2-col → stacked */
  /* minmax(0, 1fr): a plain 1fr column grows to its widest content, and the
     P&L card ran 108px past a 375px screen (site review, Code 21). */
  .recap-cols { grid-template-columns: minmax(0, 1fr) !important; }
  .recap-cols > *, .recap-section-card { min-width: 0; }
  .recap-section-header { overflow-wrap: anywhere; }
  /* Trade Journal: stay inside the real device viewport and keep every core
     action at the 44px mobile interaction baseline. */
  #view-journal {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    margin: 0 auto;
    padding: 10px;
    overflow-x: clip;
  }
  #view-journal .refresh-btn,
  #view-journal .app-nav-btn {
    min-height: 44px;
    touch-action: manipulation;
  }
  #view-journal .app-nav-btn { min-width: 44px; padding-inline: 12px; }
  #view-journal .recap-analyze-btn { min-height: 48px; }
  #view-journal .recap-dropzone[role="button"] { touch-action: manipulation; }
  .journal-trade-dialog {
    width: calc(100vw - 16px);
    max-height: calc(100dvh - 16px);
    border-radius: 18px;
  }
  .journal-trade-form { padding: 22px 18px calc(18px + env(safe-area-inset-bottom)); }
  .journal-trade-form__grid,
  .journal-trade-details__grid { grid-template-columns: 1fr; }
  .journal-trade-setup-plan { grid-template-columns: 1fr; }
  .journal-trade-setup-plan__levels { text-align: left; }
  .journal-trade-form__actions { grid-template-columns: 1fr; }
  .journal-trade-form__save { order: -1; }

  /* Analysis: stat grid → 2-col */
  /* Analysis: sub-nav pills scroll horizontally */
  #analysis-subnav {
    flex-wrap: nowrap !important;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 4px;
  }
  #view-analysis .app-nav-btn,
  #view-analysis .app-nav-label--interactive,
  #view-analysis .refresh-btn,
  #view-analysis .preview-day-tab,
  #view-analysis .an-pill {
    min-height: 44px;
    touch-action: manipulation;
  }
  #view-analysis .app-nav-btn { min-width: 44px; }

  /* Settings modal */
  .settings-shell {
    width: 95vw !important;
    padding: 20px !important;
    border-radius: 12px !important;
  }
  .settings-grid--2 { grid-template-columns: 1fr !important; }

  /* Feedback becomes a safe-area-aware bottom sheet. Do not autofocus its
     textarea on coarse pointers; feedback.js focuses the close control. */
  .feedback-overlay {
    align-items: flex-end !important;
    padding: 0 !important;
  }
  .feedback-modal {
    width: 100vw !important;
    max-width: 100vw !important;
    max-height: calc(100dvh - max(12px, env(safe-area-inset-top))) !important;
    padding: 20px 16px calc(18px + env(safe-area-inset-bottom)) !important;
    border-right: 0;
    border-bottom: 0;
    border-left: 0;
    border-radius: 20px 20px 0 0 !important;
  }
  .feedback-modal__actions { grid-template-columns: 1fr; }
  .feedback-modal__submit { order: -1; width: 100%; }
  .feedback-modal .feedback-modal__cancel { width: 100%; min-height: 48px; }

  .header-feedback-btn {
    width: 44px;
    min-width: 44px;
    height: 44px;
    padding: 0;
  }
  .header-feedback-btn__label { display: none; }

  /* Theme toggle */
  }

/* --- 480px: Mobile phones --- */
@media (max-width: 480px) {
  :root {
    --mac-mobile-nav-height: calc(64px + env(safe-area-inset-bottom));
    --mac-header-h: 48px;
  }

  body {
    padding-bottom: var(--mac-mobile-nav-height);
  }

  .header {
    padding: env(safe-area-inset-top) 8px 0;
  }
  /* The tabs have left for the rail, so the name has room again. It gives way
     on the narrowest phones rather than push the gear off the row. */
  .header-left { flex: 0 1 auto; min-width: 0; }
  .header-left > div:last-child { min-width: 0; }
  .header-title { display: block; overflow: hidden; font-size: var(--text-md); text-overflow: ellipsis; }

  /* The existing workspace router becomes a persistent phone rail. There is
     still only one set of buttons and one navigation state: desktop keeps the
     top rail, while phones move that exact landmark into thumb reach. */
  .cc-tabs {
    position: fixed;
    z-index: 110;
    right: 0;
    bottom: 0;
    left: 0;
    justify-content: space-between;
    gap: 4px;
    overflow: visible;
    padding: 6px max(8px, env(safe-area-inset-right)) calc(6px + env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
    background: var(--glass-top);
    box-shadow: inset 0 1px 0 var(--glass-edge), var(--shadow-rail);
    -webkit-backdrop-filter: blur(18px) saturate(1.4);
    backdrop-filter: blur(18px) saturate(1.4);
    isolation: isolate;
  }
  .cc-tab {
    flex: 1 1 0;
    min-width: 0;
    min-height: 48px;
    padding: 0 3px;
    border-radius: 8px;
    font-size: 10.5px;
    letter-spacing: -0.01em;
    white-space: nowrap;
  }

  /* The rail yields to focused controls and overlays, so the software
     keyboard, sheets, and dialogs always own the full phone viewport. */
  body:has(input:focus, select:focus, textarea:focus) .cc-tabs {
    opacity: 0;
    pointer-events: none;
    transform: translateY(110%);
  }
  body:has(dialog[open]) .cc-tabs,
  body:has(.feedback-overlay) .cc-tabs,
  body:has(.tv-chart-modal-overlay) .cc-tabs,
  body:has(#settings-overlay[style*="display: flex"]) .cc-tabs {
    visibility: hidden;
    opacity: 0;
    pointer-events: none;
  }
  .mac-activation-guide {
    bottom: calc(var(--mac-mobile-nav-height) + 8px);
  }

  .card { border-radius: 10px; padding: 12px; }
  .card-header-bar { font-size: var(--text-xl) !important; }

  /* Overview: Breadth stats */
  .ov-breadth-footer { flex-direction: column; align-items: flex-start; gap: 2px; }
  /* Overview: Market Snapshot → 2-col (already set) */

  /* Scanner: results → single column */
  /* Trade Journal spacing fix */
  #view-journal { margin: 0 auto; padding: 8px; }
  .journal-calendar-day { min-height: 72px; padding: 8px 6px; }
  .journal-calendar-day__number,
  .journal-calendar-day__pnl { font-size: var(--text-xs); }
  /* Trade Journal: stat grid → 2-col on smallest */
  .rv-stat-grid { grid-template-columns: repeat(2, 1fr); }
  .rv-stat-cell { padding: 8px 6px; }
  .rv-stat-label { font-size: var(--text-sm); }
  .rv-stat-value { font-size: var(--text-md); }

  /* Analysis: stat grid → 2-col */
  /* Analysis: sub-nav pills smaller */
  #analysis-subnav .an-pill { padding: 5px 10px !important; font-size: var(--text-sm) !important; }

  /* Buttons: touch-friendly minimum size */
  .refresh-btn { min-height: 40px; }
  }

/* Phones (site review, Code 21): the chart dialog's Position Planning panel
   squeezed three columns into 375px and broke its labels mid-word, and the
   price readout scrolled sideways with no visible scrollbar, so the open,
   high, low and close sat out of sight. One column, and a readout that wraps. */
@media (max-width: 560px) {
  .lw-plan-grid { grid-template-columns: minmax(0, 1fr) !important; }
  .lw-plan-head { flex-wrap: wrap; gap: 6px; }
  .lw-chart-readout { flex-wrap: wrap; row-gap: 2px; white-space: normal; overflow-x: visible; }
  .lw-chart-readout > span { white-space: nowrap; }
}
