/* Narrow-screen contract: treat every layout below the desktop-nav breakpoint as app/mobile UI.
 * This layer intentionally comes last and keeps the app layout comfortable below the desktop-nav breakpoint.
 */

@media (max-width: 979px) {
  html,
  body {
    width: 100%;
    max-width: 100%;
    overflow-x: clip;
  }

  body[data-page] .app-shell {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    overflow-x: clip;
  }

  body[data-page] .page {
    width: calc(100% - 0.7rem) !important;
    max-width: calc(100% - 0.7rem) !important;
    min-width: 0;
    padding-inline: env(safe-area-inset-left, 0px) env(safe-area-inset-right, 0px);
    padding-top: 0.45rem;
    padding-bottom: calc(var(--fr-mobile-nav-height, 4.15rem) + 1.25rem + env(safe-area-inset-bottom, 0px));
  }

  body[data-page] .page > *,
  body[data-page] .panel,
  body[data-page] section,
  body[data-page] article {
    min-width: 0;
    max-width: 100%;
  }

  .brand-copy small {
    display: none !important;
  }

  /* Keep all six destinations visible in one viewport. */
  .bottom-nav {
    position: fixed !important;
    left: max(0.45rem, env(safe-area-inset-left, 0px)) !important;
    right: max(0.45rem, env(safe-area-inset-right, 0px)) !important;
    bottom: max(0.45rem, env(safe-area-inset-bottom, 0px)) !important;
    width: auto !important;
    max-width: 46rem !important;
    margin-inline: auto;
    transform: none !important;
    z-index: var(--z-navigation) !important;
    display: grid !important;
    grid-template-columns: repeat(6, minmax(0, 1fr)) !important;
    gap: 0.18rem !important;
    padding: 0.28rem !important;
    border-radius: 1.35rem !important;
    overflow: hidden;
    visibility: visible !important;
    opacity: 1 !important;
  }

  .bottom-nav a {
    min-width: 0 !important;
    min-height: 2.7rem;
    display: grid;
    place-items: center;
    padding: 0.4rem 0.08rem !important;
    font-size: clamp(0.64rem, 1.9vw, 0.78rem) !important;
    line-height: 1.05;
    white-space: nowrap;
  }

  /* Shared compact filters. */
  body[data-page="history"] .page,
  body[data-page="metrics"] .page {
    display: grid;
    gap: 0.6rem;
  }

  body[data-page="history"] .filter-panel,
  body[data-page="metrics"] .metrics-filter-panel {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) auto !important;
    gap: 0.45rem !important;
    padding: 0.68rem !important;
    border-radius: 18px !important;
  }

  body[data-page="history"] .filter-header,
  body[data-page="metrics"] .filter-header {
    display: contents !important;
  }

  body[data-page="history"] .filter-header > div,
  body[data-page="metrics"] .filter-header > div,
  body[data-page="metrics"] .metrics-filter-note {
    display: none !important;
  }

  body[data-page="history"] .filter-header > .btn,
  body[data-page="metrics"] .filter-header > .btn {
    grid-column: 2;
    grid-row: 1;
    width: auto !important;
    min-height: 2.35rem;
    padding: 0.42rem 0.65rem;
    font-size: 0.76rem;
  }

  body[data-page="history"] .active-range-chip,
  body[data-page="metrics"] .active-range-chip {
    grid-column: 1;
    grid-row: 1;
    min-height: 2.35rem;
    padding: 0.45rem 0.62rem;
    border-radius: 12px;
  }

  body[data-page="history"] .active-range-chip span,
  body[data-page="metrics"] .active-range-chip span {
    display: none;
  }

  body[data-page="history"] .quick-row,
  body[data-page="metrics"] .quick-row {
    grid-column: 1 / -1;
    display: grid !important;
    grid-template-columns: repeat(4, minmax(0, 1fr)) !important;
    gap: 0.35rem !important;
  }

  body[data-page="history"] .quick-row .btn,
  body[data-page="metrics"] .quick-row .btn {
    min-height: 2.35rem;
    padding: 0.38rem 0.18rem;
    font-size: 0.72rem;
  }

  body[data-page="history"] #historySummary {
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
    gap: 0.45rem;
  }

  body[data-page="history"] #historySummary .stat-card {
    min-height: 4.8rem;
    padding: 0.62rem;
  }

  /* Overview: current state, not a billboard. */
  body[data-page="metrics"] .metrics-today-panel {
    padding: 0.72rem !important;
    border-radius: 19px;
  }

  body[data-page="metrics"] .metrics-today-panel .panel-heading {
    flex-direction: row !important;
    align-items: center !important;
    margin-bottom: 0.45rem;
  }

  body[data-page="metrics"] .metrics-today-title {
    font-size: 1.55rem !important;
  }

  body[data-page="metrics"] #metricsTodayShortcut {
    display: none;
  }

  body[data-page="metrics"] .metrics-today-grid {
    grid-template-columns: minmax(0, 1.25fr) minmax(7rem, 0.75fr) !important;
    gap: 0.5rem;
    margin-bottom: 0.45rem;
  }

  body[data-page="metrics"] .goal-kcal-block strong {
    font-size: 2rem;
  }

  body[data-page="metrics"] .goal-progress-card {
    padding: 0.55rem 0.62rem;
    gap: 0.42rem;
  }

  body[data-page="metrics"] .metrics-today-meta {
    margin-bottom: 0.45rem;
  }

  body[data-page="metrics"] .metrics-mini-stat {
    max-width: none;
    min-height: 2.55rem;
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: center;
    padding: 0.45rem 0.62rem;
  }

  body[data-page="metrics"] .today-metrics-list {
    gap: 0.48rem;
  }

  body[data-page="metrics"] .today-metric-track {
    min-height: 0.5rem;
  }

  /* Health: one-column action-first app layout all the way to desktop nav. */
  body[data-page="health"] .health-page {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) !important;
    gap: 0.55rem !important;
  }

  body[data-page="health"] .health-hero-panel {
    grid-template-columns: minmax(0, 1fr) auto !important;
    align-items: center !important;
    gap: 0.4rem !important;
    padding: 0.56rem 0.65rem !important;
    border-radius: 17px !important;
  }

  body[data-page="health"] .health-hero-copy .eyebrow,
  body[data-page="health"] .health-hero-copy .panel-note,
  body[data-page="health"] .health-assistant-link {
    display: none !important;
  }

  body[data-page="health"] .health-hero-copy h1 {
    margin: 0 !important;
    font-size: 1.55rem !important;
    line-height: 1 !important;
  }

  body[data-page="health"] .health-hero-actions {
    display: flex !important;
    flex-direction: column !important;
    align-items: flex-end !important;
    gap: 0.24rem !important;
  }

  body[data-page="health"] .health-hero-actions .button-row {
    width: auto !important;
  }

  body[data-page="health"] #syncOuraButton,
  body[data-page="health"] #connectOuraButton {
    flex: 0 0 auto !important;
    min-width: 6.5rem !important;
    min-height: 2.25rem !important;
    padding: 0.4rem 0.58rem !important;
    font-size: 0.76rem !important;
  }

  body[data-page="health"] .health-connection-strip {
    display: flex !important;
    justify-content: flex-end !important;
    max-width: 11rem;
    overflow: hidden !important;
  }

  body[data-page="health"] .health-connection-item,
  body[data-page="health"] .health-connection-message {
    padding: 0.24rem 0.45rem !important;
    font-size: 0.68rem !important;
  }

  body[data-page="health"] .health-connection-item:nth-child(2),
  body[data-page="health"] .health-connection-item:nth-child(3) {
    display: none !important;
  }

  body[data-page="health"] .health-hero-status {
    grid-column: 1 / -1;
  }

  body[data-page="health"] .health-coach-priority {
    padding: 0.66rem !important;
    border-radius: 18px !important;
  }

  body[data-page="health"] .health-coach-heading {
    display: flex !important;
    flex-direction: row !important;
    align-items: center !important;
    justify-content: space-between !important;
    gap: 0.55rem !important;
    margin-bottom: 0.42rem !important;
  }

  body[data-page="health"] .health-coach-heading .panel-note {
    display: none !important;
  }

  body[data-page="health"] .health-coach-heading .eyebrow {
    font-size: 0.64rem !important;
  }

  body[data-page="health"] .health-coach-heading h2 {
    margin: 0.05rem 0 0 !important;
    max-width: none !important;
    font-size: 1.38rem !important;
    line-height: 1 !important;
    white-space: nowrap !important;
  }

  body[data-page="health"] .health-coach-heading .btn {
    flex: 0 0 auto;
    min-height: 2.15rem !important;
    padding: 0.36rem 0.52rem !important;
    font-size: 0.7rem !important;
  }

  body[data-page="health"] .health-coach-card {
    gap: 0.3rem !important;
    padding: 0.7rem 0.74rem !important;
    border-radius: 14px !important;
  }

  body[data-page="health"] .health-coach-card h3 {
    font-size: 1.3rem !important;
    line-height: 1.05 !important;
  }

  body[data-page="health"] .health-coach-card > p:not(.eyebrow) {
    font-size: 0.88rem !important;
    line-height: 1.34 !important;
  }

  body[data-page="health"] .health-coach-evidence {
    gap: 0.28rem !important;
  }

  body[data-page="health"] .health-coach-evidence li {
    padding: 0.28rem 0.4rem !important;
    font-size: 0.64rem !important;
  }

  body[data-page="health"] .health-coach-status[data-tone="success"] {
    display: none !important;
  }

  body[data-page="health"] .health-section-heading {
    margin-bottom: 0.38rem !important;
  }

  body[data-page="health"] .health-section-heading h2 {
    font-size: 1.34rem !important;
  }

  body[data-page="health"] #healthCoverageNote {
    font-size: 0.65rem !important;
  }

  body[data-page="health"] .health-stat-grid {
    display: grid !important;
    grid-template-columns: repeat(4, minmax(0, 1fr)) !important;
    gap: 0.42rem !important;
    overflow: visible !important;
  }

  body[data-page="health"] .health-stat-card {
    min-width: 0 !important;
    min-height: 5.35rem !important;
    padding: 0.55rem !important;
    border-radius: 14px !important;
  }

  body[data-page="health"] .health-stat-card span,
  body[data-page="health"] .health-stat-card > small {
    font-size: 0.64rem !important;
  }

  body[data-page="health"] .health-stat-card strong {
    margin-top: auto !important;
    font-size: 1.5rem !important;
  }

  body[data-page="health"] .health-filter-panel {
    grid-template-columns: minmax(0, 1fr) !important;
    gap: 0.42rem !important;
    padding: 0.6rem !important;
    border-radius: 16px !important;
  }

  body[data-page="health"] .health-filter-copy h2 {
    display: none !important;
  }

  body[data-page="health"] .health-quick-range {
    min-width: 0 !important;
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
    gap: 0.35rem !important;
  }

  body[data-page="health"] .health-quick-range .btn {
    min-height: 2.25rem !important;
    padding: 0.38rem 0.2rem !important;
    font-size: 0.72rem !important;
  }

  body[data-page="health"] .health-range-details,
  body[data-page="health"] .health-range-details[open] {
    grid-column: auto !important;
    width: 100% !important;
    border-top: 1px solid rgba(35, 49, 39, 0.08) !important;
    padding-top: 0.4rem !important;
  }

  body[data-page="health"] .health-range-details summary,
  body[data-page="health"] .health-range-details[open] summary {
    width: auto !important;
    margin: 0 !important;
    justify-content: flex-start !important;
    min-height: 2rem !important;
    padding: 0 !important;
    border: 0 !important;
    background: transparent !important;
  }

  body[data-page="health"] .health-charts-grid {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) !important;
    gap: 0.55rem !important;
  }

  body[data-page="health"] .health-chart-wide {
    grid-column: auto !important;
  }

  body[data-page="health"] .health-chart-card,
  body[data-page="health"] .health-insights-panel,
  body[data-page="health"] .health-daily-panel {
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    padding: 0.7rem !important;
  }

  body[data-page="health"] .health-chart,
  body[data-page="health"] .health-chart-empty {
    min-height: 9.5rem !important;
  }

  body[data-page="health"] .health-chart-svg {
    width: 100% !important;
    height: 9.7rem !important;
    overflow: hidden !important;
  }

  body[data-page="health"] .health-insights-panel .panel-heading {
    margin-bottom: 0.5rem !important;
  }

  body[data-page="health"] .health-insight-card {
    padding: 0.7rem 0.72rem 0.7rem 0.86rem !important;
  }

  body[data-page="health"] #correlationWarning {
    margin-bottom: 0 !important;
    font-size: 0.76rem !important;
    line-height: 1.35 !important;
  }

  body[data-page="health"] .health-daily-summary {
    min-height: 2.55rem !important;
  }

  /* Assistant: whole chat stays in the visual viewport up to desktop mode. */
  body[data-page="assistant"] {
    overflow: hidden !important;
    overscroll-behavior: none;
  }

  body[data-page="assistant"] .assistant-shell {
    position: fixed !important;
    left: 0 !important;
    right: 0 !important;
    top: var(--assistant-vv-top, 0px) !important;
    height: var(--assistant-vv-height, 100dvh) !important;
    min-height: 0 !important;
    display: grid !important;
    grid-template-rows: auto minmax(0, 1fr) !important;
    overflow: hidden !important;
  }

  body[data-page="assistant"] .topbar {
    position: relative !important;
    top: auto !important;
  }

  body[data-page="assistant"] .assistant-hero {
    display: none !important;
  }

  body[data-page="assistant"] .assistant-page {
    width: calc(100% - 0.7rem) !important;
    height: 100% !important;
    min-height: 0 !important;
    padding: 0.35rem 0 calc(var(--fr-mobile-nav-height, 4.15rem) + 0.8rem + env(safe-area-inset-bottom, 0px)) !important;
    overflow: hidden !important;
  }

  body[data-page="assistant"] .assistant-chat-card {
    height: 100% !important;
    min-height: 0 !important;
    max-height: none !important;
    grid-template-rows: auto auto minmax(0, 1fr) auto auto !important;
    gap: 0.38rem !important;
    padding: 0.52rem !important;
    border-radius: 17px !important;
  }

  body[data-page="assistant"] .assistant-messages {
    min-height: 0 !important;
    max-height: none !important;
    overflow-y: auto !important;
    overscroll-behavior: contain;
  }

  body[data-page="assistant"] .assistant-disclaimer,
  body[data-page="assistant"] #assistantStatus[data-tone="info"],
  body[data-page="assistant"] #assistantStatus[data-tone="success"] {
    display: none !important;
  }

  body[data-page="assistant"].assistant-input-focused .bottom-nav {
    display: none !important;
  }

  body[data-page="assistant"].assistant-input-focused .assistant-page {
    padding-bottom: 0.25rem !important;
  }
}

@media (max-width: 560px) {
  body[data-page="health"] .health-stat-grid {
    display: flex !important;
    gap: 0.42rem !important;
    overflow-x: auto !important;
    scroll-snap-type: x proximity;
  }

  body[data-page="health"] .health-stat-card {
    flex: 0 0 7.25rem !important;
    min-width: 7.25rem !important;
    scroll-snap-align: start;
  }
}

/* 2026 interaction pass: comfortable touch targets and calmer mobile rhythm. */
@media (max-width: 979px) {
  .topbar-inner {
    min-height: 4rem;
    padding-top: max(0.45rem, env(safe-area-inset-top, 0px));
    padding-bottom: 0.45rem;
  }

  body[data-page] .page {
    width: min(100% - 1rem, 54rem) !important;
    max-width: min(100% - 1rem, 54rem) !important;
    padding-top: 0.65rem;
    padding-bottom: calc(6.35rem + env(safe-area-inset-bottom, 0px));
  }

  .panel {
    padding: clamp(0.9rem, 3.6vw, 1.2rem);
    border-radius: clamp(18px, 5vw, var(--radius));
    box-shadow: 0 10px 28px rgba(53, 38, 22, 0.08);
  }

  .panel-heading {
    margin-bottom: 0.8rem;
  }

  .panel-note,
  .capture-mode-helper {
    line-height: 1.4;
  }

  .field input,
  .field select,
  .field textarea {
    min-height: 3rem;
    font-size: 1rem;
  }

  .btn,
  .btn-small,
  .segmented-control button,
  .language-picker select,
  .topbar-actions [data-logout] {
    min-height: 2.75rem;
  }

  .primary-capture-panel .upload-card {
    padding: 0.75rem;
  }

  .primary-capture-panel .upload-preview {
    min-height: clamp(9.25rem, 42vw, 15rem);
  }

  .upload-preview-empty {
    padding: 1.35rem 1rem;
  }

  .inline-actions {
    align-items: center;
  }

  .inline-actions .panel-note {
    flex: 1 1 15rem;
    margin: 0;
  }

  body[data-page="history"] .quick-row .btn,
  body[data-page="metrics"] .quick-row .btn,
  body[data-page="history"] .filter-header > .btn,
  body[data-page="metrics"] .filter-header > .btn {
    min-height: 2.75rem;
  }

  body[data-page="history"] .filter-panel,
  body[data-page="metrics"] .metrics-filter-panel {
    padding: 0.8rem !important;
    gap: 0.6rem !important;
  }

  .modal {
    padding: 0.65rem 0.65rem max(0.65rem, env(safe-area-inset-bottom, 0px));
  }

  .modal-card {
    width: min(42rem, 100%);
    max-height: calc(100vh - 1.3rem - env(safe-area-inset-bottom, 0px));
    max-height: calc(100dvh - 1.3rem - env(safe-area-inset-bottom, 0px));
    overscroll-behavior: contain;
  }

  .modal-close {
    width: 2.75rem;
    height: 2.75rem;
  }

  .analysis-primary-actions .btn,
  .analysis-edit-actions .btn,
  .profile-save-bar .btn {
    min-height: 3rem;
  }
}

@media (max-width: 359px) {
  .brand-copy {
    display: none;
  }

  .bottom-nav {
    left: max(0.2rem, env(safe-area-inset-left, 0px)) !important;
    right: max(0.2rem, env(safe-area-inset-right, 0px)) !important;
    gap: 0 !important;
    padding: 0.18rem !important;
  }

}
