/* iPhone-first shell: desktop keeps the existing workspace, phones get a native-app rhythm. */
@media (max-width: 800px) {
  :root {
    --phone-edge: 16px;
    /* Keeps the final control comfortably above the fixed tab bar. */
    --bottom-nav-clearance: calc(118px + env(safe-area-inset-bottom));
  }

  html { background: #0b0d10; }
  body {
    min-height: 100dvh;
    background: radial-gradient(110% 45% at 50% -8%, #253752 0%, #14181e 49%, #0b0d10 100%);
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
  }
  button, input, textarea, select { touch-action: manipulation; }
  input, textarea, select { font-size: 16px !important; }

  .app-shell { min-height: 100dvh; display: block; }
  main {
    margin: 0;
    padding: 0 var(--phone-edge) var(--bottom-nav-clearance);
    width: 100%;
    scroll-padding-bottom: var(--bottom-nav-clearance);
  }
  .view { scroll-margin-bottom: var(--bottom-nav-clearance); }
  button, input, textarea, select { scroll-margin-bottom: var(--bottom-nav-clearance); }

  .topbar {
    height: calc(62px + env(safe-area-inset-top));
    padding-top: env(safe-area-inset-top);
    border: 0;
  }
  .topbar:after { display: none; }
  .menu-button { display: none; }
  .date-label {
    padding: 0;
    background: transparent;
    border: 0;
    color: #c8d2dd;
    font: 600 12px "DM Sans", sans-serif;
    letter-spacing: -.15px;
  }
  .profile { width: 34px; height: 34px; border-radius: 50%; }

  .hero {
    display: block;
    margin: 7px 0 17px;
    padding: 22px 18px 16px;
    background: linear-gradient(145deg, #1e2b3d, #171c23 60%);
    border: 1px solid #344352;
    border-radius: 18px;
  }
  .hero:after { display: none; }
  .eyebrow { font-size: 9px; letter-spacing: 1.25px; }
  .hero h1, .page-title h1 {
    color: #f8fbff;
    font: 700 31px/1.04 "DM Sans", sans-serif;
    letter-spacing: -1.4px;
  }
  .hero-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 9px;
    margin-top: 21px;
  }
  .hero-actions .primary-button { order: -1; grid-column: 1 / -1; }
  .hero-actions button { width: 100%; min-height: 48px; }
  .primary-button, .paste-button, .program-button {
    border-radius: 12px;
    font-size: 13px;
    font-weight: 700;
  }
  .primary-button { box-shadow: 0 7px 18px #1566d355; }
  .paste-button, .program-button { padding: 11px 8px; }

  .stat-grid { grid-template-columns: 1fr 1fr; gap: 9px; }
  .stat-card {
    min-height: 128px;
    padding: 15px;
    border-radius: 14px;
  }
  .stat-card:nth-child(3) { grid-column: 1 / -1; min-height: 108px; }
  .stat-card strong { font-size: 22px; }
  .stat-card small { font-size: 11px; }
  .stat-card .text-link { position: static; display: block; margin-top: 12px; }
  .accent-card { background: linear-gradient(135deg, #17427e, #1d2d42); }
  .profile-summary { margin: 13px 2px 0; }

  .week-plan { margin-top: 30px; }
  .section-head { margin: 34px 1px 14px; padding: 0; border: 0; }
  .section-head h2 { font-size: 20px; }
  .exercise-grid {
    display: flex;
    overflow-x: auto;
    gap: 11px;
    margin: 0 calc(var(--phone-edge) * -1);
    padding: 0 var(--phone-edge) 7px;
    scroll-snap-type: x mandatory;
    scrollbar-width: none;
  }
  .exercise-grid::-webkit-scrollbar { display: none; }
  .exercise-card { flex: 0 0 76%; min-height: 172px; scroll-snap-align: start; border-radius: 15px; padding: 17px; }
  .exercise-card h3 { font-size: 18px; margin-top: 27px; }

  .page-title { margin: 18px 1px 22px; }
  .history-list, .library-list { gap: 9px; }
  .history-item, .library-item { padding: 14px; border-radius: 13px; }
  .history-date { width: 66px; font-size: 10px; }
  .history-volume { font-size: 10px; }
  .search-wrap { border-radius: 13px; }

  .macro-grid { gap: 9px; margin-bottom: 20px; }
  .macro-card { min-height: 112px; padding: 14px; border-radius: 14px; }
  .macro-card strong { font-size: 24px; margin: 20px 0 4px; }
  .food-scan-card, .journal-capture { border-radius: 16px; padding: 18px; }
  .food-scan-card { display: block; }
  .food-scan-card .primary-button { width: 100%; margin-top: 15px; min-height: 48px; }
  .meal-panel, .recovery-panel { border-radius: 16px; padding: 17px; }
  .meal-panel form { gap: 8px; }
  .meal-panel input { min-height: 46px; border-radius: 10px; }
  .recovery-actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; }
  .recovery-actions button { padding: 12px 5px; border-radius: 10px; }

  .sidebar {
    display: block;
    position: fixed;
    z-index: 20;
    inset: auto 0 0;
    width: 100%;
    height: auto;
    padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
    background: #10141ad9;
    border: 1px solid #303944;
    border-bottom: 0;
    border-radius: 19px 19px 0 0;
    backdrop-filter: blur(18px);
    box-shadow: 0 -10px 28px #0005;
  }
  .brand, .sidebar-foot, .nav-group { display: none; }
  .nav { display: grid; grid-template-columns: repeat(4, 1fr); gap: 3px; margin: 0; }
  .nav-item, .nav-item.active, .nav-item:hover {
    display: grid;
    place-items: center;
    gap: 4px;
    min-height: 53px;
    padding: 5px 2px;
    border-radius: 12px;
    background: transparent;
    color: #8f9baa;
    text-align: center;
    font-size: 10px;
    line-height: 1;
  }
  .nav-item[data-view="exercises"] { display: none; }
  .nav-item .nav-symbol {
    display: grid;
    width: 21px;
    height: 21px;
    margin: 0;
    border: 0;
    border-radius: 0;
    color: currentColor;
    font-size: 11px;
  }
  .nav-item .nav-symbol svg { width: 19px; height: 19px; stroke-width: 1.9; }
  .nav-item.active { background: #20324a; color: #8fc0ff; }
  .nav-item.active .nav-symbol { background: transparent; color: #79b2ff; }

  .modal-backdrop {
    /* A popup is a temporary full-screen task, so it must sit above the tab bar. */
    z-index: 50;
    align-items: flex-end;
    padding: 0;
    background: #05070bbb;
  }
  body:has(.modal-backdrop.show) {
    overflow: hidden;
    overscroll-behavior: none;
  }
  body:has(.modal-backdrop.show) .sidebar {
    visibility: hidden;
    pointer-events: none;
  }
  .profile-modal, .import-modal, .plan-choice-modal, .journal-modal, .food-modal, .program-modal, .builder-choice-modal, .custom-workout-modal, .workout-modal {
    width: 100%;
    max-height: 100dvh;
    margin: 0;
    padding: 24px 18px calc(22px + env(safe-area-inset-bottom));
    border-radius: 22px 22px 0 0;
    box-shadow: 0 -16px 46px #0008;
  }
  .close-button { min-width: 42px; min-height: 42px; font-size: 30px; }
  .profile-modal h2, .import-modal h2, .journal-modal h2, .food-modal h2, .program-modal h2 { font-size: 30px; }
  .workout-modal { max-height: 100dvh; padding-inline: 15px; }
  .workout-modal header { position: sticky; top: -24px; z-index: 2; padding: 18px 0 11px; background: #191e24; }
  .workout-name { width: min(265px, 74vw); font-size: 21px; }
  .set-row { grid-template-columns: 28px 1fr 1fr 42px; gap: 7px; }
  .set-row input { min-height: 43px; padding: 8px; }
  .set-row button { min-height: 43px; }
  .workout-modal footer { position: sticky; bottom: -22px; padding: 12px 0 calc(3px + env(safe-area-inset-bottom)); background: #191e24; }
  .workout-modal footer .primary-button { min-height: 47px; }
  .toast { right: 16px; bottom: calc(85px + env(safe-area-inset-bottom)); max-width: calc(100% - 32px); border-radius: 12px; }

  /* Native-app polish: these rules only change the phone presentation. */
  body { background: #090c11; overscroll-behavior-y: none; }
  main { padding-inline: 18px; }
  .topbar {
    position: relative;
    display: block;
    height: calc(82px + env(safe-area-inset-top));
    padding: 0;
  }
  .topbar::before {
    content: "powertrack";
    position: absolute;
    top: calc(env(safe-area-inset-top) + 11px);
    left: 1px;
    color: #f7f9fc;
    font: 700 22px/1 "DM Sans", sans-serif;
    letter-spacing: -1.15px;
  }
  .date-label {
    position: absolute;
    top: calc(env(safe-area-inset-top) + 40px);
    left: 2px;
    color: #8d9baa;
    font-size: 11px;
  }
  .profile {
    position: absolute;
    top: calc(env(safe-area-inset-top) + 7px);
    right: 1px;
    width: 39px;
    height: 39px;
    border: 1px solid #3e4c5d;
    background: #1c2b3c;
    color: #cfe5ff;
    box-shadow: 0 4px 14px #0006;
  }
  .hero {
    margin: 0 0 20px;
    padding: 22px 19px 18px;
    border-color: #3a4a5e;
    border-radius: 22px;
    background: radial-gradient(115% 100% at 100% 0%, #244e83 0%, #172535 42%, #141a22 100%);
    box-shadow: 0 16px 32px #0005, inset 0 1px #ffffff12;
  }
  .hero h1 { font-size: 33px; letter-spacing: -1.65px; }
  .hero-actions { margin-top: 23px; gap: 8px; }
  .hero-actions .primary-button { min-height: 52px; }
  .primary-button { background: #3d8df5; box-shadow: 0 8px 20px #0d59bd66, inset 0 1px #ffffff3d; }
  .paste-button, .program-button { background: #202a35; border: 1px solid #3b4857; color: #d4e5f8; }
  .stat-grid { gap: 10px; }
  .stat-card {
    border-color: #303b47;
    border-radius: 18px;
    background: linear-gradient(145deg, #1a2028, #13181f);
    box-shadow: inset 0 1px #ffffff0a, 0 8px 18px #0003;
  }
  .stat-card:nth-child(3) { min-height: 116px; }
  .accent-card { background: linear-gradient(135deg, #1c528d, #182839); border-color: #356898; }
  .section-head { margin-top: 38px; }
  .section-head h2 { font-size: 21px; letter-spacing: -.8px; }
  .page-title { margin: 9px 1px 24px; }
  .page-title h1 { font-size: 32px; }
  .history-item, .library-item, .macro-card, .meal-panel, .recovery-panel, .food-scan-card, .journal-capture {
    border-color: #303b47;
    background-color: #151b22;
    box-shadow: inset 0 1px #ffffff09;
  }
  .sidebar {
    inset: auto 10px calc(8px + env(safe-area-inset-bottom));
    width: auto;
    padding: 7px;
    border: 1px solid #384554cc;
    border-radius: 22px;
    background: #10161ee8;
    box-shadow: 0 10px 30px #0009, inset 0 1px #ffffff12;
  }
  .nav { gap: 2px; }
  .nav-item, .nav-item.active, .nav-item:hover { min-height: 54px; border-radius: 16px; font-size: 10px; letter-spacing: .05px; }
  .nav-item.active { background: linear-gradient(145deg, #294f7c, #203b5b); box-shadow: inset 0 1px #ffffff20; }
  .nav-item .nav-symbol svg { width: 20px; height: 20px; }

  /* App screens stay inside the iPhone viewport. History intentionally remains
     a long-form log; every other screen uses contained areas instead of a
     browser-length page. */
  html, body { height: 100%; overflow: hidden; }
  .app-shell { height: 100dvh; overflow: hidden; }
  main {
    height: 100dvh;
    padding-bottom: calc(92px + env(safe-area-inset-bottom));
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }
  .topbar { flex: 0 0 auto; }
  .view.active {
    display: flex;
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
    flex-direction: column;
  }
  #history-view.active {
    display: block;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding-bottom: var(--bottom-nav-clearance);
  }

  /* Training is an at-a-glance home screen, not a scrolling landing page. */
  #today-view .hero { flex: 0 0 auto; margin-bottom: 11px; padding: 16px 17px 14px; }
  #today-view .hero h1 { font-size: 28px; }
  #today-view .hero-actions { grid-template-columns: 1fr 1fr; margin-top: 15px; gap: 7px; }
  #today-view .hero-actions .primary-button { min-height: 46px; }
  #today-view .hero-actions .paste-button, #today-view .hero-actions .program-button { min-height: 38px; padding: 8px 5px; }
  #today-view .stat-grid { flex: 0 0 auto; }
  #today-view .stat-card { min-height: 92px; padding: 12px; }
  #today-view .stat-card:nth-child(3) { min-height: 72px; }
  #today-view .stat-card .stat-label { margin-bottom: 8px; }
  #today-view .stat-card strong { font-size: 19px; }
  #today-view .stat-card small { display: none; }
  #today-view .stat-card .text-link { margin-top: 7px; font-size: 10px; }
  #today-view .profile-summary { display: none; }
  #today-view .week-plan { flex: 0 0 auto; margin-top: 14px; }
  #today-view .week-plan-heading { margin-bottom: 7px; }
  #today-view .week-plan-heading h2 { font-size: 17px; }
  #today-view .week-plan-days { padding-bottom: 3px; }
  #today-view .week-plan-day { min-height: 100px; padding: 10px; }
  #today-view .week-plan-day strong { margin: 6px 0 3px; font-size: 12px; }
  #today-view .week-plan-day small { font-size: 9px; }
  #today-view .week-plan-day button { min-height: 27px; padding: 5px 0 0; font-size: 10px; }
  #today-view > .section-head { flex: 0 0 auto; margin: 14px 1px 7px; }
  #today-view > .section-head h2 { font-size: 17px; }
  #today-view .exercise-grid { flex: 1 1 auto; min-height: 0; }
  #today-view .exercise-card { min-height: 108px; padding: 13px; }
  #today-view .exercise-card h3 { margin-top: 12px; font-size: 15px; }

  /* Lists are the only things that scroll on fixed utility screens. */
  #exercises-view .page-title { flex: 0 0 auto; }
  #exercises-view .search-wrap { flex: 0 0 auto; }
  #exercises-view .library-list { flex: 1 1 auto; min-height: 0; overflow-y: auto; padding: 1px 2px var(--bottom-nav-clearance); overscroll-behavior: contain; }

  #nutrition-view .page-title, #nutrition-view .macro-grid, #nutrition-view .food-scan-card, #nutrition-view .meal-panel, #nutrition-view .nutrition-archive { flex: 0 0 auto; }
  #nutrition-view .page-title { margin-bottom: 13px; }
  #nutrition-view .page-title h1 { font-size: 27px; }
  #nutrition-view .macro-grid { gap: 7px; margin-bottom: 10px; }
  #nutrition-view .macro-card { min-height: 76px; padding: 10px 12px; }
  #nutrition-view .macro-card strong { margin: 10px 0 0; font-size: 20px; }
  #nutrition-view .macro-card small { display: none; }
  #nutrition-view .food-scan-card { margin-bottom: 9px; padding: 12px 14px; }
  #nutrition-view .food-scan-card h2 { font-size: 16px; }
  #nutrition-view .food-scan-card p:not(.eyebrow) { display: none; }
  #nutrition-view .food-scan-card .primary-button { min-height: 42px; margin-top: 10px; }
  #nutrition-view .meal-panel { padding: 13px; }
  #nutrition-view .meal-panel h2 { font-size: 16px; }
  #nutrition-view .meal-panel form { margin: 11px 0 6px; gap: 6px; }
  #nutrition-view .meal-panel input { min-height: 38px; padding: 8px; }
  #nutrition-view .meal-panel .primary-button { min-height: 38px; }
  #nutrition-view .meal-list { max-height: 48px; overflow-y: auto; }
  #nutrition-view .meal-item { padding: 7px 0; }
  #nutrition-view .nutrition-archive { margin-top: 8px; padding: 0 13px; }
  #nutrition-view .nutrition-archive summary { display: flex; align-items: center; justify-content: space-between; cursor: pointer; list-style: none; padding: 10px 0; }
  #nutrition-view .nutrition-archive summary::-webkit-details-marker { display: none; }
  #nutrition-view .nutrition-archive h2 { margin: 2px 0 0; font-size: 15px; }
  #nutrition-view .nutrition-archive .eyebrow { margin: 0; }
  #nutrition-view .archive-disclosure::after { content: 'Show'; color: #9ec7ff; font: 700 11px "DM Sans", sans-serif; }
  #nutrition-view .nutrition-archive[open] .archive-disclosure::after { content: 'Hide'; }
  #nutrition-view .nutrition-archive[open] #nutrition-archive-list { max-height: 120px; overflow-y: auto; padding-bottom: 10px; }

  #recovery-view .page-title { margin-bottom: 14px; }
  #recovery-view .page-title h1 { font-size: 28px; }
  #recovery-view .journal-capture { margin: 0; padding: 16px; }
  #recovery-view .journal-capture h2 { font-size: 18px; }
  #recovery-view .journal-capture p:not(.eyebrow) { margin-top: 4px; }
  #recovery-view .journal-capture .primary-button { min-height: 46px; }
  #recovery-view .recovery-panel { margin-top: 10px; padding: 16px; }
  #recovery-view .recovery-panel h2 { font-size: 18px; }

  @media (max-height: 760px) {
    #today-view > .section-head, #today-view .exercise-grid { display: none; }
    #today-view .week-plan-day { min-height: 91px; }
    #today-view .hero { padding-top: 13px; padding-bottom: 11px; }
  }

  /* Sideways detail cards keep extra tools one swipe away while the screen
     itself can still scroll naturally when it has more content. */
  .view.active { overflow-y: auto; overscroll-behavior: contain; padding-bottom: var(--bottom-nav-clearance); }
  #history-view.active { padding-bottom: var(--bottom-nav-clearance); }
  .mobile-panel-carousel {
    display: flex;
    flex: 0 0 auto;
    gap: 10px;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scroll-snap-type: x mandatory;
    scrollbar-width: none;
    margin: 14px calc(var(--phone-edge) * -1) 0;
    padding: 0 var(--phone-edge) 8px;
  }
  .mobile-panel-carousel::-webkit-scrollbar { display: none; }
  .mobile-swipe-panel {
    display: block;
    flex: 0 0 calc(100% - 34px);
    min-width: 0;
    scroll-snap-align: start;
    scroll-snap-stop: always;
  }
  #today-view .mobile-panel-carousel { margin-top: 13px; }
  #today-view .mobile-swipe-panel { min-height: 142px; }
  #today-view .recent-panel .section-head { margin: 0 1px 9px; }
  #today-view .recent-panel .exercise-grid { margin: 0; padding: 0 0 5px; }
  #today-view .recent-panel .exercise-card { flex-basis: 88%; }

  /* The week strip is a seven-day reference, not a paging carousel. Forced
     snapping made the final Saturday/Sunday cards jump away on iPhone. */
  #today-view .week-plan-days {
    scroll-snap-type: none;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-x;
  }
  #today-view .week-plan-day { scroll-snap-align: none; }

  #nutrition-view .mobile-panel-carousel { margin-top: 9px; }
  #nutrition-view .mobile-swipe-panel { min-height: 196px; margin: 0; }
  #nutrition-view .meal-panel { padding: 15px; }
  #nutrition-view .nutrition-archive { padding: 15px; overflow: hidden; }
  #nutrition-view .nutrition-archive h2 { margin: 3px 0 11px; }
  #nutrition-view .nutrition-archive #nutrition-archive-list { max-height: 145px; overflow-y: auto; }
  #nutrition-view .meal-list { max-height: 62px; }

  #recovery-view .mobile-panel-carousel { margin-top: 0; }
  #recovery-view .mobile-swipe-panel { min-height: 208px; margin: 0; }
  #recovery-view .journal-capture { display: grid; align-content: space-between; }
  #recovery-view .recovery-panel { display: grid; align-content: start; }

  /* On short phones, keep the scheduler available and let the swipe cards
     take the place of the lower recent-lifts strip. */
  @media (max-height: 760px) {
    #today-view > .section-head, #today-view .exercise-grid { display: initial; }
    #today-view .recent-panel { display: none; }
  }
}

@media (max-width: 380px) {
  .hero h1, .page-title h1 { font-size: 29px; }
  .nav-item { font-size: 9px; }
  .stat-card { padding: 13px; }
}
