/*
 * Final mobile geometry guarantees.
 * Loaded after every feature stylesheet so iOS/WKWebView cannot fall back to
 * desktop modal widths when a bottom sheet opens.
 */

html.page-scroll-locked,
html.page-scroll-locked body {
  overflow: hidden !important;
  overscroll-behavior: none !important;
}

@media (max-width: 1024px), (max-width: 1366px) and (any-pointer: coarse) {
  /*
   * Keep the primary page headers on one shared geometry. Feature layers used
   * to restyle the dashboard bell after the foundation layer, which made it
   * look different and nudged both actions when changing tabs.
   */
  html[data-theme^="glass-"] body :is(
    .dashboard,
    .community-view,
    .settings-view
  ) > .topbar.topbar-with-notification {
    min-height: 60px;
    box-sizing: border-box;
    align-items: center;
    justify-content: space-between;
    flex-direction: row;
    gap: 12px;
    padding: 4px 2px 2px;
  }

  html[data-theme^="glass-"] body :is(
    .dashboard,
    .community-view,
    .settings-view
  ) > .topbar > :first-child {
    min-width: 0;
    flex: 1 1 auto;
  }

  html[data-theme^="glass-"] body :is(
    .dashboard,
    .community-view,
    .settings-view
  ) > .topbar > .topbar-actions {
    width: 81px;
    min-width: 81px;
    height: 40px;
    flex: 0 0 81px;
    display: grid;
    grid-template-columns: repeat(2, 40px);
    align-items: center;
    justify-content: end;
    gap: 1px;
    margin: 0;
  }

  html[data-theme^="glass-"] body :is(
    .dashboard,
    .community-view,
    .settings-view
  ) > .topbar > .topbar-actions :is(
    .topbar-icon-button,
    .notification-bell
  ),
  html[data-theme^="glass-"] body :is(
    .dashboard,
    .community-view,
    .settings-view
  ) > .topbar > .topbar-actions :is(
    .topbar-icon-button,
    .notification-bell
  ):hover:not(:disabled),
  html[data-theme^="glass-"] body :is(
    .dashboard,
    .community-view,
    .settings-view
  ) > .topbar > .topbar-actions :is(
    .topbar-icon-button,
    .notification-bell
  ):focus-visible {
    width: 40px;
    min-width: 40px;
    height: 40px;
    min-height: 40px;
    margin: 0;
    border: 0;
    border-radius: 50%;
    padding: 0;
    background: transparent;
    color: var(--mobile-ink-soft, #344956);
    box-shadow: none;
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
    transform: none;
  }

  html[data-theme^="glass-"] body :is(
    .consent-backdrop,
    .policy-backdrop,
    .notification-backdrop,
    .ai-summary-backdrop,
    .community-detail-backdrop,
    .community-comments-backdrop
  ) {
    position: fixed;
    inset: 0;
    width: 100dvw;
    min-width: 100dvw;
    max-width: none;
    box-sizing: border-box;
    align-items: end !important;
    justify-items: stretch;
    margin: 0;
    padding-right: env(safe-area-inset-right, 0px) !important;
    padding-left: env(safe-area-inset-left, 0px) !important;
    overscroll-behavior: none;
    touch-action: pan-y;
  }

  html[data-theme^="glass-"] body :is(
    .notification-content,
    .community-detail-content,
    .community-comments-content,
    .ai-summary-content,
    .policy-content,
    .feedback-dialog,
    .profile-edit-dialog,
    .food-selection-dialog,
    .manual-food-dialog,
    .quick-weight-dialog
  ) {
    overscroll-behavior-y: contain;
    touch-action: pan-y;
    -webkit-overflow-scrolling: touch;
  }

  html[data-theme^="glass-"] body .community-comment-action-sheet-backdrop {
    right: 0;
    left: 0;
    width: 100dvw;
    min-width: 100dvw;
    max-width: none;
    box-sizing: border-box;
    padding-right: env(safe-area-inset-right, 0px) !important;
    padding-bottom: 0 !important;
    padding-left: env(safe-area-inset-left, 0px) !important;
  }

  html[data-theme^="glass-"] body :is(
    .consent-dialog,
    .profile-edit-dialog,
    .confirmation-dialog,
    .action-confirm-dialog,
    .feedback-dialog,
    .food-selection-dialog,
    .manual-food-dialog,
    .quick-weight-dialog,
    .policy-panel,
    .notification-panel,
    .ai-summary-panel,
    .community-detail-panel,
    .community-comments-panel,
    .community-comment-action-sheet
  ) {
    width: calc(100dvw - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px)) !important;
    min-width: 0 !important;
    max-width: none !important;
    box-sizing: border-box;
    justify-self: stretch;
    margin-right: 0 !important;
    margin-left: 0 !important;
  }

  html[data-theme^="glass-"] body .community-comment-action-sheet {
    border-bottom: 0;
    border-radius: 22px 22px 0 0;
    padding-bottom: max(12px, calc(env(safe-area-inset-bottom, 0px) + 12px));
  }

  /* Member profiles use all usable vertical space without covering the OS UI. */
  html[data-theme^="glass-"] body .community-detail-panel {
    height: calc(100dvh - var(--modal-safe-top-gap));
    max-height: calc(100dvh - var(--modal-safe-top-gap));
  }

  /* The composer itself is not a card; only its input and send button are. */
  html[data-theme^="glass-"] body .community-comments-panel .community-comment-form {
    border: 0;
    border-radius: 0;
    padding: 0;
    background: transparent;
    box-shadow: none;
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }

  /* Food editor controls opt out of the global 46px filled-button treatment. */
  html[data-theme^="glass-"] body .food-selection-dialog .food-selection-data {
    flex-wrap: wrap;
    gap: 4px 14px;
    overflow: visible;
    padding-bottom: 0;
  }

  html[data-theme^="glass-"] body .food-selection-dialog :is(
    .food-selection-edit-button,
    .food-selection-ai-button
  ),
  html[data-theme^="glass-"] body .food-selection-dialog :is(
    .food-selection-edit-button,
    .food-selection-ai-button
  ):hover:not(:disabled),
  html[data-theme^="glass-"] body .food-selection-dialog :is(
    .food-selection-edit-button,
    .food-selection-ai-button
  ):focus-visible,
  html[data-theme^="glass-"] body .food-selection-dialog :is(
    .food-selection-edit-button,
    .food-selection-ai-button
  ):active:not(:disabled) {
    width: auto;
    min-width: 0;
    height: 30px;
    min-height: 30px;
    border: 0;
    border-radius: 0;
    padding: 0 2px;
    background: transparent;
    color: var(--mobile-tracking-primary-strong, #1f6979);
    box-shadow: none;
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
    font-size: 12px;
    font-weight: 700;
    line-height: 1;
    white-space: nowrap;
    transform: none;
  }

  html[data-theme^="glass-"] body .food-selection-dialog .food-selection-ai-button,
  html[data-theme^="glass-"] body .food-selection-dialog .food-selection-ai-button:hover:not(:disabled),
  html[data-theme^="glass-"] body .food-selection-dialog .food-selection-ai-button:focus-visible,
  html[data-theme^="glass-"] body .food-selection-dialog .food-selection-ai-button:active:not(:disabled) {
    color: #5265a5;
  }

  html[data-theme^="glass-"] body .food-selection-dialog .food-portion-control {
    grid-template-columns: 34px minmax(74px, 86px) 34px;
    gap: 6px;
  }

  html[data-theme^="glass-"] body .food-selection-dialog .food-portion-step,
  html[data-theme^="glass-"] body .food-selection-dialog .food-portion-step:hover:not(:disabled),
  html[data-theme^="glass-"] body .food-selection-dialog .food-portion-step:focus-visible,
  html[data-theme^="glass-"] body .food-selection-dialog .food-portion-step:active:not(:disabled) {
    width: 34px;
    min-width: 34px;
    height: 34px;
    min-height: 34px;
    display: grid;
    place-items: center;
    border: 1px solid var(--mobile-tracking-line, rgba(48, 78, 94, 0.13));
    border-radius: 50%;
    padding: 0;
    background: var(--mobile-tracking-surface-soft, #f1f6f9);
    color: var(--mobile-tracking-primary-strong, #1f6979);
    box-shadow: none;
    line-height: 0;
    transform: none;
  }

  html[data-theme^="glass-"] body .food-selection-dialog .food-portion-step svg {
    width: 16px;
    height: 16px;
    display: block;
  }

  html[data-theme^="glass-"] body .food-selection-dialog .food-portion-value {
    min-width: 0;
    min-height: 36px;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 3px;
    border: 1px solid var(--mobile-tracking-line, rgba(48, 78, 94, 0.13));
    border-radius: 12px;
    padding: 0 9px;
    background: var(--mobile-tracking-surface-raised, #fff);
    box-shadow: 0 3px 10px rgba(42, 65, 78, 0.06);
  }

  html[data-theme^="glass-"] body .food-selection-dialog .food-portion-value input,
  html[data-theme^="glass-"] body .food-selection-dialog .food-portion-value input:focus {
    min-width: 0;
    height: 34px !important;
    min-height: 34px !important;
    border: 0 !important;
    border-radius: 0 !important;
    padding: 0 !important;
    background: transparent !important;
    color: var(--mobile-tracking-ink, #13222c);
    box-shadow: none !important;
    font-size: 16px;
    font-weight: 720;
    line-height: 1;
    text-align: right;
  }

  html[data-theme^="glass-"] body .food-selection-dialog .food-portion-value small {
    display: flex;
    align-items: center;
    color: var(--mobile-tracking-muted, #657681);
    font-size: 11px;
    font-weight: 650;
    line-height: 1;
  }

  html[data-theme^="glass-"] body #closeFoodSelectionButton,
  html[data-theme^="glass-"] body #closeFoodSelectionButton:hover:not(:disabled),
  html[data-theme^="glass-"] body #closeFoodSelectionButton:focus-visible,
  html[data-theme^="glass-"] body #closeFoodSelectionButton:active:not(:disabled) {
    width: 44px;
    min-width: 44px;
    height: 44px;
    min-height: 44px;
    display: grid;
    place-items: center;
    border: 1px solid var(--mobile-tracking-line, rgba(48, 78, 94, 0.13));
    border-radius: 14px;
    padding: 0;
    background: var(--mobile-tracking-surface-soft, #f1f6f9);
    color: var(--mobile-tracking-primary-strong, #1f6979);
    box-shadow: none;
    transform: none;
  }

  html[data-theme^="glass-"] body #closeFoodSelectionButton svg {
    width: 18px;
    height: 18px;
    display: block;
  }

  /*
   * Notification rows are touch controls, not desktop buttons. Neutralise the
   * global sticky hover/focus treatment and keep only a restrained press state.
   */
  html[data-theme^="glass-"] body .notification-panel .notification-item,
  html[data-theme^="glass-"] body .notification-panel .notification-item:hover:not(:disabled),
  html[data-theme^="glass-"] body .notification-panel .notification-item:focus,
  html[data-theme^="glass-"] body .notification-panel .notification-item:focus-visible {
    border-color: transparent;
    border-radius: 0;
    background: var(--mobile-ui-surface-raised, #fff);
    color: var(--mobile-ui-text, #17231d);
    box-shadow: none;
    outline: none;
    -webkit-tap-highlight-color: transparent;
  }

  html[data-theme^="glass-"] body .notification-panel .notification-item:active:not(:disabled) {
    border-color: transparent;
    background: var(--mobile-ui-surface-soft, #f2f5f6);
    box-shadow: none;
    outline: none;
  }

  html[data-theme^="glass-"] body .notification-panel .notification-swipe-row:not(.is-delete-open) .notification-item,
  html[data-theme^="glass-"] body .notification-panel .notification-swipe-row:not(.is-delete-open) .notification-item:active:not(:disabled) {
    transform: none;
  }

  html[data-theme^="glass-"] body .notification-panel .notification-swipe-row.is-delete-open .notification-item,
  html[data-theme^="glass-"] body .notification-panel .notification-swipe-row.is-delete-open .notification-item:active:not(:disabled) {
    transform: translate3d(-72px, 0, 0);
  }

  /* A single full-height WeChat-style action: no secondary hide action. */
  html[data-theme^="glass-"] body .notification-panel .notification-delete-action,
  html[data-theme^="glass-"] body .notification-panel .notification-delete-action:hover:not(:disabled),
  html[data-theme^="glass-"] body .notification-panel .notification-delete-action:focus,
  html[data-theme^="glass-"] body .notification-panel .notification-delete-action:focus-visible {
    width: 72px;
    min-width: 72px;
    height: 100%;
    min-height: 0;
    display: grid;
    place-items: center;
    border: 0;
    border-radius: 0;
    padding: 0;
    background: #fa5151;
    color: #fff;
    box-shadow: none;
    outline: none;
    transform: none;
  }

  html[data-theme^="glass-"] body .notification-panel .notification-delete-action:active:not(:disabled) {
    background: #e64340;
    transform: none;
  }
}

/*
 * Tablet composition keeps the phone interaction model while preventing
 * cards, navigation and sheets from stretching to desktop proportions.
 */
@media (min-width: 761px) and (max-width: 1024px),
  (min-width: 1025px) and (max-width: 1366px) and (any-pointer: coarse) {
  html[data-theme^="glass-"] {
    --mobile-page-inline: clamp(24px, 4vw, 40px);
  }

  html[data-theme^="glass-"] .app-shell {
    width: min(100%, 920px);
    max-width: 920px;
    margin-right: auto;
    margin-left: auto;
  }

  html[data-theme^="glass-"] .capture-view .capture-flow {
    width: min(100%, 560px, 64dvh);
    max-width: 560px;
    margin-right: auto;
    margin-left: auto;
  }

  html[data-theme^="glass-"] .weight-chart.is-native-large-card {
    height: clamp(440px, 58dvh, 560px);
    min-height: 440px;
    max-height: 560px;
    aspect-ratio: auto;
  }

  html[data-theme^="glass-"] .bottom-tabs {
    right: auto;
    left: 50%;
    width: min(520px, calc(100dvw - 48px));
    max-width: 520px;
    transform: translateX(-50%);
  }

  html[data-theme^="glass-"] body :is(
    .consent-dialog,
    .profile-edit-dialog,
    .confirmation-dialog,
    .action-confirm-dialog,
    .feedback-dialog,
    .food-selection-dialog,
    .manual-food-dialog,
    .quick-weight-dialog,
    .community-comment-action-sheet
  ) {
    width: min(640px, calc(100dvw - 48px)) !important;
    max-width: 640px !important;
    justify-self: center;
    margin-right: auto !important;
    margin-left: auto !important;
  }

  html[data-theme^="glass-"] body :is(
    .policy-panel,
    .notification-panel,
    .ai-summary-panel,
    .community-comments-panel
  ) {
    width: min(820px, calc(100dvw - 48px)) !important;
    max-width: 820px !important;
    justify-self: center;
    margin-right: auto !important;
    margin-left: auto !important;
  }

  html[data-theme^="glass-"] body .community-detail-panel {
    width: min(920px, calc(100dvw - 48px)) !important;
    max-width: 920px !important;
    justify-self: center;
    margin-right: auto !important;
    margin-left: auto !important;
  }

  html[data-theme^="glass-"] body .ai-summary-panel {
    height: min(88dvh, 760px) !important;
    max-height: min(88dvh, 760px) !important;
  }
}
