/* Phones and tablets. Every rule here sits in one of three media queries, so the desktop layouts (1366 px wide and up,
   taller than 500 px) are exactly app.css, story.css, try.css and watch.css (which keeps Watch's own phone rules):
     reflow  (max-width: 1100px), (max-height: 500px)  tablets, phones either way up: each view is a page that scrolls,
                                                       controls are at least 40 px for a finger
     narrow  (max-width: 599px)                        phones upright: one column, the header on two rows
     short   (max-height: 500px)                       phones on their side: a one-row header, the rest scrolls
   tests/test_ui.py (test_every_view_fits_every_screen) checks every view at eight sizes. */

/* ---- reflow -------------------------------------------------------------------------------------------------- */
@media (max-width: 1100px), (max-height: 500px) {
  :root { --st-pad: 16px; --tap: 40px; }

  /* the header: logo, the areas, About; Lab's controls wrap in their own row */
  .sbar, .app > .topbar { height: auto; min-height: 56px; padding: 8px var(--st-pad); gap: 8px 12px; flex-wrap: wrap; }
  .navseg a { height: var(--tap); padding: 0 12px; font-size: 14px; }
  .brand { min-height: var(--tap); }
  .team { font-size: 12px; }

  /* wide content scrolls inside its own box, with a shadow at the edge that has more */
  .hscroll {
    overflow-x: auto; max-width: 100%; -webkit-overflow-scrolling: touch;
    background:
      linear-gradient(90deg, var(--bg-1) 30%, transparent) left / 24px 100% no-repeat local,
      linear-gradient(270deg, var(--bg-1) 30%, transparent) right / 24px 100% no-repeat local,
      radial-gradient(farthest-side at 0 50%, rgba(234, 240, 248, 0.22), transparent) left / 12px 100% no-repeat scroll,
      radial-gradient(farthest-side at 100% 50%, rgba(234, 240, 248, 0.22), transparent) right / 12px 100% no-repeat scroll;
  }


  /* technical tables in their own scroll box */
  .techs { grid-template-columns: minmax(0, 1fr); }
  .ttbox { border-radius: 12px; }
  .ttab { min-width: 620px; }
  .ttab thead th { white-space: normal; vertical-align: bottom; }

  /* the measured sections' figures wrap, wide tables scroll inside their box */
  .eq { padding: 14px; }
  .eq-more summary, .tdetails summary { min-height: var(--tap); display: flex; align-items: center; }
  .eq-kv { white-space: normal; }
  .ev-gridbox { margin: 0 -4px; }
  .ev-grid { min-width: 680px; }
  .ev-grid td.rl, .ev-grid th.rl { position: sticky; left: 0; z-index: 1; background: var(--bg-1); }
  .ev-grid td.c a { min-height: var(--tap); }
  .ev-card a { display: inline-flex; align-items: center; min-height: var(--tap); }
  .ev-find { grid-template-columns: repeat(auto-fit, minmax(min(360px, 100%), 1fr)); }
  .tlegend { grid-template-columns: repeat(auto-fit, minmax(min(420px, 100%), 1fr)); }
  .tlegend div { flex-wrap: wrap; }

  /* Try it: the side column above the board, buttons for a finger */
  .try-acc, .try-btn, .try-orbit button, .seg.try-orbit button { height: var(--tap); }
  .try-orbit button { min-width: var(--tap); }
  .try-pal { min-height: var(--tap); justify-content: center; }
  .try-dur input { height: var(--tap); }
  .try-yamlbox summary { min-height: var(--tap); flex-wrap: wrap; }
  .try-btn.sm { height: var(--tap); padding: 0 14px; font-size: 13px; }
  .try-tl { min-width: 600px; }

  /* Lab: a page that scrolls; the panel goes under the lanes, every lane keeps its full height */
  .app { display: flex; flex-direction: column; height: 100vh; overflow-x: hidden; overflow-y: auto;
    --label-w: 300px; --pill-w: 112px; --col-gap: 62px; --col-out: 58px; --col-late: 44px; }
  .app > * { flex: none; }
  .app > .topbar { overflow: visible; min-height: 56px; }
  .rung-title { white-space: normal; }
  .name-line { flex-wrap: wrap; row-gap: 4px; }
  .ticker { white-space: normal; }
  .topbar .spacer { display: none; }
  .topbar .clock { margin-left: auto; }
  .tbtn.play { width: 46px; height: 46px; }
  .seg button, .pickbtn, .seg.views button, .seg.rowmode button { height: var(--tap); min-width: var(--tap); }
  .topbar .seg#speedSeg button:nth-child(n) { display: inline-block; }
  .topbar #helpBtn { display: inline-block; width: var(--tap); }
  .jbar { flex-wrap: wrap; row-gap: 8px; }
  .jbar .spacer { display: none; }
  .jb-tabs, .jb-group { flex-wrap: wrap; }
  .jb-tab, .jb-chip, .jb-seeds .seedchips button { height: var(--tap); }
  .jb-seeds .seedchips button { min-width: var(--tap); }
  .jb-more select { height: var(--tap); max-width: 100%; font-size: 14px; }
  .jb-more .jb-label { display: inline; }
  .app > .main { flex: none; }
  .ladder-view { display: flex; flex-direction: column; height: auto; padding-bottom: 16px; }
  .stage { flex: none; }
  .lanes { flex: none; }
  .row { flex: none !important; min-height: 84px; max-height: none; }
  .row.queued { min-height: 40px; }
  .row-track { min-height: 64px; }
  .row-cap { white-space: normal; }
  .panel { overflow: visible; display: grid; grid-template-columns: repeat(auto-fill, minmax(min(340px, 100%), 1fr)); align-items: start; }
  .panel .log { flex: none; min-height: 0; }
  .panel .log ol { max-height: 260px; flex: none; }
  .log li .lx { display: block; -webkit-line-clamp: none; }
  .p-blurb, .psec.settings { display: block; }
  .p-tags { display: flex; }
  .score-view { position: static; overflow: visible; grid-template-columns: minmax(0, 1fr); }
  .score-view > .sb-card { grid-column: 1 / -1 !important; }
  #sbLadder { min-width: 1440px; }
  #sbCost { min-width: 560px; }
  .tiles { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
  .legend { flex-wrap: wrap; gap: 8px 18px; padding: 10px var(--st-pad); }
  .legend .keys { display: none; }
}


/* ---- narrow ---------------------------------------------------------------------------------------------------- */
@media (max-width: 599px) {
  :root { --st-pad: 12px; }
  .sbar .navseg, .app > .topbar .navseg { order: 10; flex: 1 1 100%; display: flex; }
  .sbar > .spacer, .sbar > .team, .sbar > .chip { order: 1; }
  .app > .topbar > :not(.brand):not(.navseg) { order: 20; }
  .jb-more { flex: 1 1 100%; min-width: 0; }
  .jb-more select { flex: 1; min-width: 0; }
  .navseg a { flex: 1 1 auto; justify-content: center; padding: 0 6px; }


  .eq-fg { grid-template-columns: minmax(0, 1fr); }
  .eq-h { gap: 10px; }
  .ab-hero { flex-wrap: wrap; gap: 16px; }
  .ab-card { padding: 14px 16px; }

  /* Lab on a phone: each lane's name and numbers above its track; the play head runs behind the names */
  .app { --pill-w: 104px; --col-gap: 64px; --col-out: 60px; --col-late: 48px; }
  .journey { grid-template-columns: minmax(0, 1fr); }
  .j-left { position: relative; z-index: 6; background: var(--bg-2); border-radius: var(--radius) var(--radius) 0 0; padding-bottom: 8px; margin-top: -10px; padding-top: 10px; }
  .lanes-head { grid-template-columns: minmax(0, 1fr); height: auto; }
  .lh-cols { position: relative; z-index: 6; background: var(--bg-0); padding: 2px 16px; }
  .lh-track { padding-top: 4px; min-height: 18px; }
  .row { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(64px, 1fr); }
  .row-track { display: flex; flex-direction: column; }
  .row-track canvas { position: relative; inset: auto; height: 64px; flex: none; }
  .nstrip { position: static; flex-wrap: wrap; gap: 4px 10px; padding: 4px 8px 6px; }
  .notice-line { flex-wrap: wrap; flex: 1 1 100%; }
  .row-label { position: relative; z-index: 6; background: var(--bg-2); border-right: 0; border-bottom: 1px solid var(--line-1); border-radius: var(--radius) var(--radius) 0 0; }
}

/* ---- short ----------------------------------------------------------------------------------------------------- */
@media (max-height: 500px) {
  .sbar, .app > .topbar { min-height: 52px; padding-top: 6px; padding-bottom: 6px; }
}

/* ---- phones, either way up: no text under 12 px ---------------------------------------------------------------- */
@media (max-width: 599px), (max-height: 500px) {
  :root { --fs-2xs: 12px; --fs-xs: 12px; }
  .j-svg .tr-key text, .lanes.tight .pill, .lanes.tight .nchip, .lanes.tight .ticker, .wx-chip em, .lanes.tight .wx-chip,
  .wx-leg .sh, .phase-svg text, .chip.synth.small, .tdots i, .chip.small, .mk, .mc .mt, .st-vhud, .st-scrub .fz-key,
  .st-scrub .fz-l, .st-scrub .st-sl, .st-scrub .tt, .ev-grid td.c a small.mis, .th, .ttab thead th, .ttab thead th.tn small,
  .ev-grid td.c a small, .ev-f .area, .ev-f .src, .lanes.micro .pill, .ai-chip.sm, .eq-table th, .eq-table th small,
  .eq-table td small, .eq-chip small, .eq-tt, .try-k, .try-rl small, .try-axl, .try-ticks span { font-size: 12px; }
  .phase-svg text { font-family: var(--font-ui); }
  .clock-val .unit { font-size: 12px; }
  .ev-sub { font-size: max(12px, 0.62em); }
  .sb-svg text { font-size: 12px !important; }
  #sbTotals div[style*="font-size"] { font-size: 12px !important; }
}
