/* ==========================================================================
   TBS — phone layout
   --------------------------------------------------------------------------
   Loaded last in <head> on every page. EVERYTHING here sits behind a
   max-width media query, so desktop rendering is byte-for-byte unchanged.

   Tailwind's Play CDN injects its own <style> into <head> at runtime, which
   can land after this file. Rules that must survive that carry !important;
   rules that only need to beat the stylesheet's own defaults do not.
   ========================================================================== */

@media (max-width: 767px) {

  /* ---- 1. Fields ---------------------------------------------------------
     iOS Safari zooms the viewport whenever a focused field is under 16px,
     and leaves it zoomed. Every field in TBS is 12-14px, so changing any
     strategy parameter on a phone meant zoom-in / edit / pinch-out, per
     field. 16px is the threshold, not a preference. Checkboxes and radios
     are excluded — they have no text and sizing them here misaligns rows. */
  input:not([type="checkbox"]):not([type="radio"]),
  select,
  textarea {
    font-size: 16px !important;
    min-height: 38px;
  }

  /* Tailwind's !-prefixed utilities (!text-xs) emit their own !important, so
     they tie with the rule above and win on source order — the Play CDN
     injects its <style> after this file. The [class] attribute selector
     raises specificity to (0,1,1), which beats a single utility class. */
  input[class]:not([type="checkbox"]):not([type="radio"]),
  select[class],
  textarea[class] {
    font-size: 16px !important;
  }

  /* Number inputs are the common case in the config panels and 16px makes
     them wide; let them shrink to their grid cell rather than push it. */
  input[type="number"], input[type="text"], input[type="time"], select {
    max-width: 100%;
    box-sizing: border-box;
  }

  /* Checkboxes stay small by default and are the hardest thing to tap in
     the whole app — the entry-day and Long/Short rows are all checkboxes. */
  input[type="checkbox"], input[type="radio"] {
    width: 18px !important;
    height: 18px !important;
  }

  /* ---- 2. Touch targets --------------------------------------------------
     Buttons inside table rows are deliberately left alone: those rows are
     already dense and growing them would push the widest tables wider. */
  button:not(table button):not(.mob-compact),
  a.btn, .btn:not(table .btn) {
    min-height: 34px;
  }

  /* ---- 3. Horizontal strips ---------------------------------------------
     The main tab strip is 1,408px wide inside a 390px screen. It already
     scrolls, but with no scrollbar and no clipped-edge cue there was
     nothing to tell you the other half of the app was there. Give it a
     visible rail and momentum scrolling. */
  .overflow-x-auto {
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
  }
  .overflow-x-auto::-webkit-scrollbar { height: 4px; }
  .overflow-x-auto::-webkit-scrollbar-track { background: rgba(255,255,255,.04); }
  .overflow-x-auto::-webkit-scrollbar-thumb {
    background: rgba(148,163,184,.45); border-radius: 2px;
  }

  /* The nav strip specifically: keep tabs on one line and let the row
     scroll, rather than letting a long label wrap and double its height. */
  nav .overflow-x-auto > *, .overflow-x-auto > .tab, .overflow-x-auto > button {
    flex-shrink: 0;
    white-space: nowrap;
  }

  /* ---- 4. Top bars -------------------------------------------------------
     Theta Algo and Intraday both put identity, ticks, MTM and the lifecycle
     controls in one row. On a phone the last group was clipped off-screen
     with no way to reach it. Let the bar wrap instead. */
  nav.tbar, nav.topbar, header nav {
    flex-wrap: wrap;
    row-gap: .4rem;
  }
  /* The bar wraps, but its inner groups were still single-line and pushed
     the last one (the cross-page links) off the right edge. */
  nav.tbar .tbar-group { flex-wrap: wrap; row-gap: .3rem; }
  nav.tbar .tbar-sep { display: none; }

  /* ---- 5. Squeezed headings ---------------------------------------------
     A heading sharing a flex row with controls gets crushed to its longest
     word and wraps to three lines ("Today's / MTM / Curve"). Give the row
     permission to stack instead. */
  .panel > .flex.items-center.justify-between,
  .panel-tight > .flex.items-center.justify-between {
    flex-wrap: wrap;
    row-gap: .4rem;
  }
  h1, h2, h3 { text-wrap: balance; }

  /* ---- 6. Wide tables ----------------------------------------------------
     The widest are 840-1050px against a 390px screen, so the identifying
     column scrolls away and the numbers lose their labels. Pin it. The
     explicit background is required: without it the scrolled columns show
     through the pinned cell. */
  table.mob-pin-first th:first-child,
  table.mob-pin-first td:first-child {
    position: sticky; left: 0; z-index: 2;
    background: #111827;
    box-shadow: 1px 0 0 rgba(148,163,184,.18);
  }

  /* ---- 7. Modals ---------------------------------------------------------
     Fixed max-widths (max-w-4xl and friends) plus mx-4 leave a phone modal
     with almost no usable width and a chart squeezed to nothing. */
  .fixed.inset-0 > .max-w-4xl,
  .fixed.inset-0 > .max-w-5xl,
  .fixed.inset-0 > .max-w-6xl,
  .fixed.inset-0 > .max-w-3xl,
  .fixed.inset-0 > .max-w-2xl {
    max-width: 100% !important;
    margin: .5rem !important;
    max-height: 92vh;
    overflow-y: auto;
  }

  /* ---- 8. Tables that nobody wrapped ------------------------------------
     24 tables across the 8 pages sit in no overflow container. Most are
     plain w-full and merely squeeze, but the ones carrying an explicit
     min-width (and the backtest report tables, whose cells are nowrap)
     cannot shrink, so on a phone their right-hand columns were clipped away
     by the overflow-x:hidden below — invisible and unreachable.

     The obvious fix, `table { display: block; overflow-x: auto }`, is wrong:
     a block table shrink-wraps its own anonymous table box, so any table
     NARROWER than its container stops stretching its columns to full width
     and renders with dead space on the right. Verified against real rows
     before rejecting it.

     Making the PARENT the scroller avoids that entirely — the table keeps
     display:table and its normal column stretching. Tables already inside
     an overflow-x-auto wrapper just match their own wrapper, which is a
     no-op. :has() is Safari 15.4+ / Chrome 105+; on anything older this
     rule is skipped and the behaviour is exactly today's. */
  *:has(> table[class*="min-w-["]),
  *:has(> table.rpt) {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* ---- 9. Backstop -------------------------------------------------------
     With the above, nothing should reach this. It stays so that one stray
     wide element can never make the whole page pan sideways. */
  body { overflow-x: hidden; }
}
