/*
 * ConfigureX on iPhone (and other phones): an app-style layout.
 *   - the bike fills the top of the screen and never scrolls away
 *   - the parts list is a sheet at the bottom: drag its handle (or tap it) to pull it up or tuck it down
 *   - the top bar keeps just the logo, the price and a menu button; everything else is in the menu
 * Everything here only applies at phone widths, so the desktop layout is untouched.
 * Loaded last (after the page's own styles) so it wins ties.
 */

/* The menu button and sheet handle only exist on phones */
.m-more, .m-menu, .m-sheet-handle, .m-hint { display: none; }

@media (max-width: 760px) {
  :root {
    --m-top: calc(52px + env(safe-area-inset-top));
    --m-peek: 44dvh;                                        /* parts sheet height when tucked down */
    --m-open: calc(100dvh - var(--m-top) - 56px);           /* ...and pulled all the way up */
    --m-sheet: var(--m-peek);
  }
  html, body { height: 100%; overflow: hidden; overscroll-behavior: none; }
  html { -webkit-text-size-adjust: 100%; touch-action: manipulation; }
  body { -webkit-tap-highlight-color: transparent; }
  button, [role="button"], .part-row, .part-section { -webkit-tap-highlight-color: transparent; }

  /* ---- top bar: logo · price · menu ---- */
  .app { height: 100dvh; min-height: 0; display: block; }
  .topbar { position: fixed; z-index: 30; top: 0; left: 0; right: 0; height: var(--m-top); padding: env(safe-area-inset-top) max(14px, env(safe-area-inset-right)) 0 max(14px, env(safe-area-inset-left)); }
  .brand-word { font-size: 21px; }
  .brand .brand-mark { transform: scale(.88); }
  .topbar-right { gap: 8px; }
  /* the real buttons stay on the page (their pop-ups live next to them) but are reached from the menu */
  #theme-toggle, #lab-button, #ctrl-lab-button, #share-button, #ride-button, #garage-button, #reset-build, #settings-button { display: none !important; }
  .px-button { height: 36px; padding: 0 12px; border-radius: 10px; }
  .px-button svg { display: inline-block; }
  .px-button-total { font-size: 19px; }
  .m-more { display: grid; place-items: center; width: 40px; height: 36px; padding: 0; border: 1px solid #35393f; border-radius: 10px; background: #1c1f22; color: #c9d0d8; cursor: pointer; }
  .m-more svg { width: 20px; height: 20px; }
  .m-more[aria-expanded="true"] { border-color: var(--lime); color: var(--lime); }
  .m-more:focus-visible { outline: 2px solid var(--lime); outline-offset: 2px; }

  .m-menu { position: fixed; z-index: 40; top: calc(var(--m-top) + 8px); right: max(10px, env(safe-area-inset-right)); width: min(300px, calc(100vw - 20px)); display: grid; grid-template-columns: 1fr 1fr; gap: 8px; padding: 10px; border: 1px solid #3d4248; border-radius: 16px; background: linear-gradient(145deg, #1f2225f5, #16181bf5); -webkit-backdrop-filter: blur(18px); backdrop-filter: blur(18px); box-shadow: 0 20px 50px #000b; transform-origin: top right; animation: m-pop .18s cubic-bezier(.2,.8,.3,1); }
  .m-menu[hidden] { display: none; }
  @keyframes m-pop { from { opacity: 0; transform: translateY(-6px) scale(.97); } }
  .m-menu button { display: flex; flex-direction: column; align-items: flex-start; justify-content: center; gap: 6px; min-height: 64px; padding: 10px 12px; border: 1px solid #30353b; border-radius: 12px; background: #1b1e21; color: #e3e8ee; font: inherit; font-size: 13px; font-weight: 650; text-align: left; cursor: pointer; }
  .m-menu button svg { width: 20px; height: 20px; color: var(--lime); }
  .m-menu button:active { background: #23272b; transform: scale(.98); }
  .m-menu button:focus-visible { outline: 2px solid var(--lime); outline-offset: 2px; }

  /* Pop-ups from the top bar sit under it as full-width cards */
  .garage-panel, .px-panel, .settings-menu { position: fixed !important; top: calc(var(--m-top) + 8px) !important; left: max(10px, env(safe-area-inset-left)) !important; right: max(10px, env(safe-area-inset-right)) !important; width: auto !important; max-height: calc(100dvh - var(--m-top) - 24px - env(safe-area-inset-bottom)); overflow-y: auto; overscroll-behavior: contain; }
  .settings-menu { z-index: 41; }

  /* ---- the bike: everything between the top bar and the parts sheet ---- */
  .workspace { display: block; }
  .stage { position: fixed; z-index: 1; top: var(--m-top); left: 0; right: 0; bottom: var(--m-peek); display: block; }
  .viewer-scroll-area { overflow: hidden; }
  .bike-view { width: 100%; }
  .stage-top { display: none; }
  .angle-count { top: auto; left: 12px; right: auto; bottom: 16px; padding: 5px 10px; font-size: 9px; }
  .stage-bottom { left: 12px; right: 12px; bottom: 8px; }
  .zoom-control { display: none; }                         /* pinch the bike to zoom instead */
  .view-controls { margin-left: auto; gap: 8px; }
  .view-button { width: 40px; height: 40px; }
  .toast { bottom: calc(var(--m-peek) + 16px); }

  /* specs strip: one slim row over the bike; tap it for the full specs */
  .sx-top { top: 8px !important; left: 10px !important; right: 10px !important; gap: 0; }
  .sx-stats { grid-template-columns: repeat(4, minmax(0, 1fr)) !important; border-radius: 12px; cursor: pointer; pointer-events: auto; }
  .sx-chip { padding: 6px 7px 7px !important; border-right: 1px solid #25282c !important; border-bottom: 0 !important; }
  .sx-chip:last-child { border-right: 0 !important; }
  .sx-chip-label { font-size: 7.5px !important; letter-spacing: .06em; }
  .sx-chip-value { margin-top: 3px !important; font-size: 13px !important; }
  .sx-chip-sub, .sx-see-specs, .sx-hud-toggle { display: none !important; }

  /* first-visit hint over the bike */
  .m-hint { position: absolute; z-index: 3; left: 50%; bottom: 60px; display: block; padding: 8px 14px; border: 1px solid #2e8bff55; border-radius: 99px; background: #111418d9; -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px); color: #cfe2fa; font-size: 12px; font-weight: 600; white-space: nowrap; pointer-events: none; transform: translateX(-50%); transition: opacity .5s ease; }
  .m-hint.is-gone { opacity: 0; }

  /* ---- parts sheet ---- */
  .sidebar { position: fixed; z-index: 10; left: 0; right: 0; bottom: 0; height: var(--m-sheet); padding-bottom: env(safe-area-inset-bottom); border: 1px solid #30353b; border-bottom: 0; border-radius: 20px 20px 0 0; overflow-x: hidden; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; box-shadow: 0 -14px 40px #0009; transition: height .32s cubic-bezier(.2,.8,.25,1); }
  .sidebar.m-dragging { transition: none; }
  body.m-sheet-open .sidebar { --m-sheet: var(--m-open); }
  .m-sheet-handle { position: sticky; z-index: 6; top: 0; display: grid; place-items: center; height: 30px; background: #191b1d; cursor: grab; touch-action: none; }
  .m-sheet-close { position: absolute; top: 4px; right: 10px; display: grid; place-items: center; width: 38px; height: 26px; padding: 0; border: 1px solid #35393f; border-radius: 9px; background: #1c1f22; color: #c9d0d8; cursor: pointer; }
  .m-sheet-close svg { width: 18px; height: 18px; transition: transform .25s ease; }
  body.m-sheet-closed .m-sheet-close svg { transform: rotate(180deg); }
  /* closed: a slim bar (handle + the build heading) and the bike fills the screen */
  body.m-sheet-closed { --m-peek: calc(68px + env(safe-area-inset-bottom)); --m-sheet: var(--m-peek); }
  body.m-sheet-closed .sidebar { overflow: hidden; }
  .stage { transition: bottom .32s cubic-bezier(.2,.8,.25,1); }
  body.m-sheet-closed .toast { bottom: calc(var(--m-peek) + 16px); }
  html[data-theme="light"] .m-sheet-close { border-color: #d0d5db; background: #f6f7f9; color: #16202c; }
  .m-sheet-handle::before { width: 40px; height: 5px; border-radius: 99px; background: #4a5058; content: ""; }
  /* one slim row: "Your LBX" on the left, top speed + mph/km/h on the right */
  .sidebar .side-heading { display: flex !important; align-items: center; justify-content: space-between; gap: 10px; padding: 2px 60px 6px 16px !important; touch-action: none; }   /* right: room for the close button */
  .side-heading h1 { margin: 0; font-size: 22px !important; line-height: 1; }
  .side-heading .sx-gauge { display: flex !important; align-items: center; gap: 10px; width: auto !important; }
  .side-heading .sx-gauge svg, .sx-speed-label, .sx-max { display: none !important; }
  .side-heading .sx-speed { margin: 0 !important; flex-wrap: nowrap; }
  .sx-speed-num { font-size: 26px !important; }
  .sx-speed-unit { font-size: 11px; }
  .side-heading .sx-gauge-meta { margin: 0 !important; }
  .sx-unit-toggle button { min-width: 42px; height: 30px; font-size: 10px; }
  .sidebar > .section { padding: 0 12px 14px; }
  /* search and the All / On bike switch share one row */
  .parts-toolbar { top: 22px; background: #191b1d !important; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 8px !important; margin: 0 -12px 8px !important; padding: 8px 12px !important; }
  .parts-toolbar-head, .parts-expand { display: none !important; }
  .parts-search input { height: 40px !important; }
  .parts-filter button { height: 32px !important; padding: 0 10px !important; }
  .sidebar-footer { display: none; }

  /* no zoom-on-focus: iOS zooms into any field under 16px */
  input, select, textarea, .battery-capacity, .px-currency select { font-size: 16px !important; }
  .battery-capacity { height: 38px; }

  /* bigger, easier taps in the list */
  .part-section { min-height: 60px; padding: 10px 12px; }
  .part-section-name { font-size: 22px; }
  .px-tag { font-size: 11.5px; padding: 4px 8px; }

  /* light theme */
  html[data-theme="light"] .m-more { border-color: #d2d6da; background: #f4f6f8; color: #2b323a; }
  html[data-theme="light"] .m-menu { border-color: #d2d6da; background: #fffffff2; box-shadow: 0 20px 50px #0e21382e; }
  html[data-theme="light"] .m-menu button { border-color: #e1e4e8; background: #f6f7f9; color: #16202c; }
  html[data-theme="light"] .sidebar { border-color: #d2d6da; box-shadow: 0 -14px 40px #0e21381f; }
  html[data-theme="light"] .m-sheet-handle::before { background: #c4c9cf; }
  html[data-theme="light"] .m-sheet-handle, html[data-theme="light"] .parts-toolbar { background: #ffffff !important; }
  html[data-theme="light"] .m-hint { border-color: #b9d3f5; background: #ffffffe0; color: #0b4fae; }
}

@media (max-width: 760px) and (prefers-reduced-motion: reduce) {
  .sidebar, .m-menu { transition: none; animation: none; }
}

/* hold-to-move button: touch screens only */
.m-move { display: none; }
@media (pointer: coarse), (max-width: 760px) {
  .view-controls .m-move { display: grid; place-items: center; touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
  .m-move svg { width: 18px; height: 18px; }
  .m-move.is-held { border-color: var(--lime, #2e8bff); background: #2e8bff33; color: var(--lime, #2e8bff); }
}

/* Touch screens wider than the phone layout (iPhone on its side, iPad): no zoom-in when a box is tapped, nothing under
   the notch or the home bar, and the page fits a short landscape screen instead of scrolling. */
@media (pointer: coarse) {
  input, select, textarea { font-size: max(16px, 1em); }
  .app { padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right); box-sizing: border-box; }
}
@media (pointer: coarse) and (max-height: 500px) and (min-width: 761px) {
  .app { min-height: 0; padding-bottom: env(safe-area-inset-bottom); }
}
