/*
 * 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 tab bar, chips, menu and sheet handle only exist on phones */
.m-tabs, .m-chips, .m-more, .m-menu, .m-sheet-handle, .m-hint { display: none; }

@media (max-width: 760px), (max-height: 500px) and (max-width: 950px) {
  :root {
    --m-top: calc(52px + env(safe-area-inset-top));
    --m-tabs: calc(62px + env(safe-area-inset-bottom));     /* bottom tab bar */
    --m-peek: 40dvh;                                        /* parts sheet height when tucked down */
    --m-open: calc(100dvh - var(--m-top) - var(--m-tabs) - 40px);   /* ...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, #reset-build, #settings-button,
  #explore-button, #account-button, .tb-labs-toggle, #price-button { display: none !important; }   /* only the buttons: their pop-up panels (Garage, Share, Cart) still open from the tabs / More */
  /* the logo on the left, with Beta beside it (the top bar's other buttons live in the tab bar on phones) */
  .topbar { justify-content: flex-start; }
  .topbar-left { flex-direction: row !important; align-items: center !important; gap: 8px !important; margin: 0; }
  .topbar-right { position: absolute; right: max(14px, env(safe-area-inset-right)); top: env(safe-area-inset-top); height: 52px; }
  .brand { gap: 6px; font-size: 22px; }
  .brand-home { display: none; }
  .beta-tag { font-size: 8px; padding: 0 5px; }
  /* the Garage: an icon on the right of the top bar */
  .topbar-right { display: flex; align-items: center; }
  #garage-button { display: inline-flex !important; width: 40px; height: 36px; padding: 0; justify-content: center; }
  #garage-button .garage-button-label { display: none; }
  /* just the bike's name in the sheet: no build tier badge */
  .side-heading .cx-tier { display: none !important; }

  /* colours: a sheet over the parts panel, ✕ or swipe down to close */
  .stage .paint-panel { position: fixed !important; z-index: 20; left: 0 !important; right: 0 !important; top: auto !important; bottom: var(--m-tabs) !important; height: 40dvh;   /* the same height as the parts panel */ overflow-y: auto; overscroll-behavior: contain;
    padding: 30px 14px 16px !important; border-radius: 20px 20px 0 0 !important; box-shadow: 0 -16px 40px #000a !important; transform: translateY(105%); transition: transform .28s cubic-bezier(.2,.8,.25,1), visibility .28s; }
  .stage .paint-panel.open { transform: none; }
  /* the colours sheet lives in the bike area: lift that area above the parts panel while it's open (they never overlap) */
  body:has(.stage .paint-panel.open) .stage { z-index: 25; }
  .stage .paint-panel::before { content: ""; position: absolute; top: 10px; left: 50%; width: 40px; height: 5px; border-radius: 99px; background: #4a5058; transform: translateX(-50%); }
  .stage .paint-panel .paint-close { top: 10px; right: 10px; width: 40px; height: 40px; border: 1px solid #3d4248; border-radius: 99px; background: #1c1f22; color: #e3e8ee; }
  .stage .paint-panel .swatch { width: 48px; height: 48px; }

  /* first visit: pick light or dark */
  .m-look { position: fixed; z-index: 95; inset: 0; display: grid; place-items: center; padding: 24px; background: #0b0c0ef2; -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px); animation: m-pop .25s ease; }
  .m-look-card { width: min(360px, 100%); text-align: center; }
  .m-look h2 { margin: 0 0 6px; color: #f2f5f8; font-size: 28px; }
  .m-look p { margin: 0 0 18px; color: #8d97a3; }
  .m-look-pick { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
  .m-look-pick button { display: grid; gap: 10px; justify-items: center; padding: 18px 10px; border: 2px solid #30353b; border-radius: 18px; font: inherit; font-size: 16px; font-weight: 800; cursor: pointer; }
  .m-look-pick button:active { transform: scale(.96); }
  .m-look-dark { background: #16181b; color: #e8edf2; }
  .m-look-light { background: #f4f6f8; color: #16181b; border-color: #d2d6da !important; }
  .m-look-pick i { width: 56px; height: 56px; border-radius: 50%; }
  .m-look-dark i { background: radial-gradient(circle at 35% 35%, #3b4a63, #0e1013); }
  .m-look-light i { background: radial-gradient(circle at 35% 35%, #fff, #c9d3df); }
  .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: calc(var(--m-peek) + var(--m-tabs)); 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) + var(--m-tabs) + 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 { display: none !important; }
  /* Settings covers everything, tab bar included; light / dark is in it on phones too */
  html.settings-open .m-tabs { display: none; }
  .sidebar.lab-mode .side-heading { display: none !important; }   /* a Lab panel shows on its own */
  .st-theme #theme-toggle { display: grid !important; }
  /* the little tab under the spec bar hides it (and brings it back) */
  .sx-hud-toggle { display: grid !important; top: 8px; width: 46px; height: 22px; }
  .sx-hud-collapsed .sx-top { transform: translateY(calc(-100% - 20px)) !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: var(--m-tabs); height: var(--m-sheet); padding-bottom: 0; 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 .22s 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: the sheet tucks away completely and the bike gets the whole screen (the Parts tab brings it back) */
  body.m-sheet-closed { --m-peek: 0px; --m-sheet: 0px; }
  body.m-sheet-closed .sidebar { overflow: hidden; border-width: 0; box-shadow: none; }
  .stage { transition: bottom .22s cubic-bezier(.2,.8,.25,1); }
  body.m-sheet-closed .toast { bottom: calc(var(--m-tabs) + 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 { position: static !important; 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; }

  /* ---- bottom tab bar ---- */
  .m-tabs { position: fixed; z-index: 35; left: 0; right: 0; bottom: 0; height: var(--m-tabs); display: grid; grid-template-columns: repeat(5, 1fr); align-items: start;
    padding: 6px max(6px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(6px, env(safe-area-inset-left)); border-top: 1px solid #2a2e33;
    background: #121417f2; -webkit-backdrop-filter: blur(18px); backdrop-filter: blur(18px); }
  .m-tab { display: grid; justify-items: center; gap: 3px; height: 52px; padding: 4px 0 0; border: 0; border-radius: 14px; background: none; color: #8d97a3; font: inherit; font-size: 11px; font-weight: 700; cursor: pointer; transition: color .15s, transform .12s; }
  .m-tab svg { width: 24px; height: 24px; }
  .m-tab:active { transform: scale(.92); }
  .m-tab.is-on, .m-tab[aria-expanded="true"] { color: #e8f1ff; }
  .m-tab.is-on svg, .m-tab[aria-expanded="true"] svg { color: var(--lime, #2e8bff); }
  /* Ride: the big fun button in the middle */
  .m-tab-ride { position: relative; top: -16px; width: 64px; height: 64px; justify-self: center; align-content: center; padding: 0; border-radius: 50%;
    background: linear-gradient(135deg, #ff5a36, #ff2f8f 55%, #7a4dff); color: #fff; box-shadow: 0 8px 22px #ff3d6b66, 0 0 0 4px #121417; }
  .m-tab-ride svg { width: 26px; height: 26px; }
  .m-tab-ride span { font-size: 10px; }
  .m-menu { top: auto !important; bottom: calc(var(--m-tabs) + 10px); transform-origin: bottom right; }

  /* ---- category chips at the top of the parts sheet ---- */
  .m-chips { position: static; z-index: 5; display: flex; gap: 8px; margin: -6px -12px 8px; padding: 2px 12px 10px; overflow-x: auto; scrollbar-width: none; background: #191b1d; }
  .m-chips::-webkit-scrollbar { display: none; }
  .m-chip { flex: none; height: 36px; padding: 0 14px; border: 1px solid #30353b; border-radius: 99px; background: #1f2226; color: #cfd6de; font: inherit; font-size: 14px; font-weight: 700; white-space: nowrap; cursor: pointer; }
  .m-chip[aria-pressed="true"] { border-color: var(--lime, #2e8bff); background: #2e8bff26; color: #e8f1ff; }
  .m-chip:active { transform: scale(.95); }

  /* weight / range / charge live in the Specs tab on phones, so the parts come first in the sheet */
  .sidebar .cx-weight { display: none !important; }
  /* swipe turns the bike, so no angle arrows */
  #previous-angle, #next-angle, .panel-toggle { display: none !important; }   /* no desktop panel-collapse tab either */
  /* no fiddly hold-to-move button on phones: swipe spins, pinch zooms */
  .view-controls .m-move { display: none !important; }
  /* "things to sort" sits just above the bike controls */
  .stage .bt-fit { top: auto; bottom: 58px; left: 12px; }
  .stage .bt-fit-pop { top: auto; bottom: 104px; left: 12px; width: calc(100% - 24px); max-height: 55%; }

  /* light theme */
  html[data-theme="light"] .m-tabs { border-top-color: #dde1e5; background: #ffffffee; }
  html[data-theme="light"] .m-tab { color: #5a616b; }
  html[data-theme="light"] .m-tab.is-on, html[data-theme="light"] .m-tab[aria-expanded="true"] { color: #121417; }
  html[data-theme="light"] .m-tab-ride { color: #fff; box-shadow: 0 8px 22px #ff3d6b55, 0 0 0 4px #fff; }
  html[data-theme="light"] .m-chips { background: #fff; }
  html[data-theme="light"] .m-chip { border-color: #d6dade; background: #f4f6f8; color: #2b323a; }
  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), (max-height: 500px) and (max-width: 950px) 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); }
}

/* ---- phone on its side: the bike on the left, the parts (or colours) as a panel on the right, slim bars ---- */
@media (orientation: landscape) and (max-height: 500px) and (max-width: 950px) {
  :root { --m-top: calc(44px + env(safe-area-inset-top)); --m-tabs: calc(50px + env(safe-area-inset-bottom)); --m-side: min(44vw, 400px); }
  body.m-sheet-closed { --m-side: 0px; }
  .topbar { padding-left: max(14px, env(safe-area-inset-left)); padding-right: max(14px, env(safe-area-inset-right)); }
  .stage { bottom: var(--m-tabs) !important; right: var(--m-side); left: env(safe-area-inset-left); transition: right .22s cubic-bezier(.2,.8,.25,1); }
  .sidebar { top: var(--m-top); bottom: var(--m-tabs); left: auto; right: 0; width: var(--m-side); height: auto !important; padding-right: env(safe-area-inset-right);
    border-radius: 16px 0 0 0; border-bottom: 1px solid #30353b; transition: width .22s cubic-bezier(.2,.8,.25,1); }
  body.m-sheet-closed .sidebar { width: 0; }
  .m-sheet-handle { display: none !important; }            /* the Parts tab opens and closes it */
  .sidebar .side-heading { padding-right: 16px !important; }
  .toast, body.m-sheet-closed .toast { bottom: calc(var(--m-tabs) + 12px); }
  .m-tabs { padding-left: max(10px, env(safe-area-inset-left)); padding-right: max(10px, env(safe-area-inset-right)); }
  .m-tab { height: 44px; padding-top: 2px; font-size: 10px; }
  .m-tab svg { width: 20px; height: 20px; }
  .m-tab-ride { top: -10px; width: 54px; height: 54px; }
  .stage .paint-panel { left: auto !important; right: 0 !important; top: var(--m-top) !important; bottom: var(--m-tabs) !important; width: min(44vw, 400px); height: auto;
    border-radius: 16px 0 0 0 !important; transform: translateX(105%); }
  .stage .paint-panel.open { transform: none; }
  .sx-top { right: 10px !important; }
  /* More: four across and compact, so it fits the short screen (scrolls if it ever needs to) */
  .m-menu { width: min(600px, calc(100vw - 20px)); grid-template-columns: repeat(4, 1fr); gap: 6px; max-height: calc(100dvh - var(--m-top) - var(--m-tabs) - 16px); overflow-y: auto; }
  .m-menu button { min-height: 50px; padding: 8px 10px; flex-direction: row; align-items: center; gap: 8px; font-size: 12.5px; }
}
