/*
 * Final mobile parity layer.
 * Keep this file last so phones use the current MyMappi product instead of
 * older responsive experiments still present in the main stylesheet.
 */

@media (max-width: 720px), (hover: none) and (pointer: coarse) and (max-width: 1024px) {
  :root {
    --mobile-page-gutter: 12px;
    --mobile-topbar-height: 58px;
    --bottom-nav-height: 78px;
  }

  html,
  body {
    width: 100% !important;
    max-width: 100% !important;
    min-height: 100% !important;
    overflow: hidden !important;
    overscroll-behavior: none;
  }

  html body main.app-shell,
  html body main.app-shell > .map-stage {
    box-sizing: border-box !important;
    width: 100vw !important;
    max-width: 100vw !important;
    height: 100dvh !important;
    min-height: 100dvh !important;
    overflow-x: hidden !important;
  }

  html body main.app-shell > .map-stage.home-focus {
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    scroll-padding-bottom: calc(var(--bottom-nav-height) + env(safe-area-inset-bottom) + 22px);
  }

  /* One header geometry across Home, Explore, and Memory Lane. */
  html body .app-shell .topbar {
    position: fixed !important;
    top: max(8px, env(safe-area-inset-top)) !important;
    right: var(--mobile-page-gutter) !important;
    left: var(--mobile-page-gutter) !important;
    z-index: 1500 !important;
    box-sizing: border-box !important;
    display: grid !important;
    grid-template-columns: 86px minmax(0, 1fr) 86px !important;
    align-items: center !important;
    width: auto !important;
    min-height: 50px !important;
    margin: 0 !important;
    padding: 0 !important;
    pointer-events: none !important;
  }

  html body main.app-shell section.map-stage .topbar {
    top: max(8px, env(safe-area-inset-top)) !important;
    right: var(--mobile-page-gutter) !important;
    left: var(--mobile-page-gutter) !important;
    width: auto !important;
    height: 50px !important;
    min-height: 50px !important;
    max-height: 50px !important;
    margin: 0 !important;
    padding: 0 !important;
    transform: none !important;
  }

  html body main.app-shell section.map-stage #map ~ header.topbar {
    top: max(8px, env(safe-area-inset-top)) !important;
    right: auto !important;
    left: var(--mobile-page-gutter) !important;
    width: calc(100vw - (var(--mobile-page-gutter) * 2)) !important;
    height: 50px !important;
    min-height: 50px !important;
    max-height: 50px !important;
    padding: 0 !important;
    transform: none !important;
  }

  html body .app-shell .topbar-left,
  html body .app-shell .topbar-actions {
    position: relative !important;
    inset: auto !important;
    box-sizing: border-box !important;
    display: grid !important;
    grid-template-columns: repeat(2, 40px) !important;
    align-items: center !important;
    gap: 4px !important;
    width: 86px !important;
    min-width: 86px !important;
    height: 46px !important;
    margin: 0 !important;
    padding: 3px !important;
    pointer-events: auto !important;
  }

  html body .app-shell .topbar-actions {
    justify-self: end !important;
  }

  html body .app-shell .topbar-left > .icon-button,
  html body .app-shell .topbar-left > .avatar-button,
  html body .app-shell .topbar-actions > .icon-button:not(.hidden-control) {
    box-sizing: border-box !important;
    display: grid !important;
    place-items: center !important;
    width: 40px !important;
    height: 40px !important;
    min-width: 40px !important;
    min-height: 40px !important;
    margin: 0 !important;
    padding: 0 !important;
    border-radius: 50% !important;
    box-shadow: none !important;
  }

  html body .app-shell .topbar .brand {
    position: relative !important;
    inset: auto !important;
    justify-self: center !important;
    align-self: center !important;
    width: max-content !important;
    max-width: 100% !important;
    margin: 0 !important;
    pointer-events: auto !important;
    transform: none !important;
  }

  html body .app-shell .topbar .brand h1 {
    max-width: 100% !important;
    margin: 0 !important;
    font-size: clamp(1.6rem, 8vw, 2rem) !important;
    line-height: 1 !important;
    white-space: nowrap !important;
  }

  /* Never let unloaded tiles flash as a black rectangle on mobile. */
  html body .app-shell #map,
  html body .app-shell #map .maplibregl-canvas-container,
  html body .app-shell #map .maplibregl-canvas {
    background: #e7eee4 !important;
  }

  html body .app-shell #map,
  html body .app-shell .map-backdrop {
    width: 100% !important;
    height: 100% !important;
  }

  html body .app-shell .pin-search-panel,
  html body .app-shell .notification-hub {
    position: fixed !important;
    top: calc(max(8px, env(safe-area-inset-top)) + 58px) !important;
    right: var(--mobile-page-gutter) !important;
    left: var(--mobile-page-gutter) !important;
    z-index: 1700 !important;
    box-sizing: border-box !important;
    width: auto !important;
    max-width: none !important;
    max-height: calc(100dvh - 160px - env(safe-area-inset-bottom)) !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  /* Home keeps every section available and uses one shared content edge. */
  html body .app-shell .hero-copy,
  html body .app-shell .home-dashboard {
    box-sizing: border-box !important;
    width: calc(100% - (var(--mobile-page-gutter) * 2)) !important;
    max-width: 720px !important;
    margin-right: auto !important;
    margin-left: auto !important;
  }

  html body .app-shell .home-dashboard {
    display: grid !important;
    gap: 24px !important;
    padding-bottom: calc(var(--bottom-nav-height) + env(safe-area-inset-bottom) + 34px) !important;
  }

  html body .app-shell #homeMemoryOfDay,
  html body .app-shell .home-action-section,
  html body .app-shell #homeOnThisDay,
  html body .app-shell #homeJourney,
  html body .app-shell #homeTimelineStrip {
    box-sizing: border-box !important;
    display: block !important;
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    margin: 0 !important;
  }

  html body .app-shell .home-quick-actions {
    display: grid !important;
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
    width: 100% !important;
    gap: 8px !important;
  }

  html body .app-shell .home-action-card {
    box-sizing: border-box !important;
    min-width: 0 !important;
    min-height: 104px !important;
    padding: 14px 8px !important;
    text-align: center !important;
  }

  html body .app-shell .home-action-card strong {
    max-width: 100% !important;
    font-size: clamp(0.72rem, 3.3vw, 0.86rem) !important;
    line-height: 1.22 !important;
    overflow-wrap: anywhere !important;
  }

  /* Mobile media uses the same real memory assets and framing as desktop. */
  html body .app-shell .today-memory-photo img,
  html body .app-shell .today-memory-media img,
  html body .app-shell .memory-echo-thumb img,
  html body .app-shell .memory-echo-photo img,
  html body .app-shell .life-chapter-cover img,
  html body .app-shell .life-chapter-preview img,
  html body .app-shell .recent-memory-art img,
  html body .app-shell .recent-memory-media img,
  html body .app-shell .memory-lane-cover-image,
  html body .app-shell .memory-lane-photo-tile img,
  html body .app-shell .sheet-photo-gallery__photo {
    display: block !important;
    width: 100% !important;
    height: 100% !important;
    min-width: 100% !important;
    min-height: 100% !important;
    max-width: none !important;
    max-height: none !important;
    object-fit: cover !important;
    object-position: center center !important;
    opacity: 1 !important;
    visibility: visible !important;
  }

  html body .app-shell video,
  html body .app-shell .memory-capsule-video,
  html body .app-shell .memory-lane-video {
    display: block !important;
    width: 100% !important;
    max-width: 100% !important;
    height: auto !important;
    max-height: min(62dvh, 560px) !important;
    object-fit: contain !important;
    object-position: center !important;
  }

  /* Capture Memory opens at Place and leaves only a compact save dock. */
  html body .app-shell .memory-editor.open {
    position: fixed !important;
    top: calc(max(8px, env(safe-area-inset-top)) + 58px) !important;
    right: 8px !important;
    bottom: calc(var(--bottom-nav-height) + env(safe-area-inset-bottom) + 10px) !important;
    left: 8px !important;
    z-index: 1800 !important;
    box-sizing: border-box !important;
    width: auto !important;
    min-width: 0 !important;
    max-width: none !important;
    max-height: none !important;
    margin: 0 !important;
    padding-bottom: 0 !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    border-radius: 24px !important;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    scroll-padding-top: 112px;
    scroll-padding-bottom: 82px;
  }

  html body .app-shell .memory-editor .editor-header {
    position: sticky !important;
    top: 0 !important;
    z-index: 12 !important;
    box-sizing: border-box !important;
    min-height: 92px !important;
    margin: 0 !important;
    padding: 14px 16px 10px !important;
  }

  html body .app-shell .memory-editor .memory-editor-progress {
    box-sizing: border-box !important;
    width: calc(100% - 24px) !important;
    margin: 10px 12px 14px !important;
    padding: 10px 8px !important;
    overflow: hidden !important;
  }

  html body .app-shell .memory-editor .memory-editor-progress span {
    min-width: 0 !important;
    font-size: 0.63rem !important;
  }

  html body .app-shell .memory-editor .memory-flow-panel {
    box-sizing: border-box !important;
    width: calc(100% - 24px) !important;
    margin: 0 12px 14px !important;
    padding: 16px !important;
    scroll-margin-top: 106px;
  }

  html body .app-shell .memory-editor .two-col,
  html body .app-shell .memory-editor .memory-meta-fields {
    grid-template-columns: 1fr !important;
  }

  html body .app-shell .memory-editor .memory-media-dropzone {
    box-sizing: border-box !important;
    width: 100% !important;
    padding: 18px 14px !important;
  }

  html body .app-shell .memory-editor .memory-media-dropzone-copy {
    text-align: center !important;
  }

  html body .app-shell .memory-editor .memory-media-dropzone-actions {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    width: 100% !important;
    gap: 8px !important;
  }

  html body .app-shell .memory-editor .memory-media-dropzone-actions button {
    min-width: 0 !important;
    padding-right: 8px !important;
    padding-left: 8px !important;
    white-space: normal !important;
  }

  html body .app-shell .memory-editor .editor-actions {
    position: sticky !important;
    bottom: 0 !important;
    z-index: 14 !important;
    box-sizing: border-box !important;
    display: grid !important;
    grid-template-columns: auto minmax(0, 1fr) !important;
    align-items: center !important;
    width: 100% !important;
    min-height: 72px !important;
    margin: 0 !important;
    padding: 9px 12px calc(9px + env(safe-area-inset-bottom)) !important;
    gap: 8px !important;
    border-radius: 0 !important;
  }

  html body .app-shell .memory-editor .editor-actions .save-step,
  html body .app-shell .memory-editor .editor-actions .save-memory-note {
    display: none !important;
  }

  html body .app-shell .memory-editor .editor-actions #deleteButton[hidden] {
    display: none !important;
  }

  html body .app-shell .memory-editor .editor-actions #deleteButton {
    min-width: 46px !important;
    min-height: 50px !important;
    padding: 0 11px !important;
  }

  html body .app-shell .memory-editor .editor-actions .primary-button {
    width: 100% !important;
    min-width: 0 !important;
    min-height: 52px !important;
    margin: 0 !important;
  }

  html body .app-shell .memory-editor .editor-actions #deleteButton[hidden] + .primary-button {
    grid-column: 1 / -1 !important;
    justify-self: center !important;
    width: min(100%, 280px) !important;
  }

  /* Memory Lane remains a full, scrollable product view on phones. */
  html body .app-shell .memory-database-page {
    position: fixed !important;
    inset: 0 !important;
    z-index: 900 !important;
    box-sizing: border-box !important;
    width: 100vw !important;
    max-width: 100vw !important;
    height: 100dvh !important;
    max-height: 100dvh !important;
    padding: calc(max(8px, env(safe-area-inset-top)) + 70px) var(--mobile-page-gutter)
      calc(var(--bottom-nav-height) + env(safe-area-inset-bottom) + 24px) !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  html body .app-shell .memory-lane-toolbar,
  html body .app-shell #memoryList,
  html body .app-shell .drawer-view {
    box-sizing: border-box !important;
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
  }

  html body .app-shell .memory-lane-search input {
    min-width: 0 !important;
    font-size: 0.84rem !important;
  }

  html body .app-shell .memory-lane-card,
  html body .app-shell .memory-lane-entry {
    box-sizing: border-box !important;
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
  }

  /* The full capsule stays centered and all media remains below its header. */
  html body .app-shell .memory-bottom-sheet.open {
    position: fixed !important;
    inset: 0 !important;
    z-index: 2100 !important;
    box-sizing: border-box !important;
    display: grid !important;
    place-items: center !important;
    width: 100vw !important;
    height: 100dvh !important;
    padding: max(8px, env(safe-area-inset-top)) 8px
      calc(8px + env(safe-area-inset-bottom)) !important;
    overflow: hidden !important;
  }

  html body .app-shell .memory-bottom-sheet.open #memorySheetContent {
    box-sizing: border-box !important;
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
  }

  html body .app-shell .memory-bottom-sheet.open .full-memory-capsule {
    box-sizing: border-box !important;
    width: 100% !important;
    max-width: 100% !important;
    max-height: calc(100dvh - max(16px, env(safe-area-inset-top)) - env(safe-area-inset-bottom)) !important;
    margin: 0 auto !important;
    padding: 12px !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  html body .app-shell .full-memory-capsule .memory-capsule-hero {
    grid-template-columns: 1fr !important;
    align-items: start !important;
    padding: 20px !important;
  }

  html body .app-shell .full-memory-capsule .memory-capsule-date-block {
    justify-self: start !important;
    min-width: 0 !important;
    max-width: 100% !important;
  }

  html body .app-shell .full-memory-capsule .sheet-photo-gallery {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    width: 100% !important;
    gap: 8px !important;
  }

  html body .app-shell .full-memory-capsule .sheet-photo-gallery__item {
    position: relative !important;
    width: 100% !important;
    min-width: 0 !important;
    aspect-ratio: 1 / 1 !important;
    overflow: hidden !important;
  }

  html body .app-shell .full-memory-capsule .sheet-photo-gallery__item.is-cover {
    grid-column: 1 / -1 !important;
    aspect-ratio: 16 / 10 !important;
  }

  /* Account is a usable bottom sheet with logout still reachable. */
  html body .app-shell .account-menu {
    position: fixed !important;
    top: auto !important;
    right: var(--mobile-page-gutter) !important;
    bottom: calc(var(--bottom-nav-height) + env(safe-area-inset-bottom) + 10px) !important;
    left: var(--mobile-page-gutter) !important;
    z-index: 1800 !important;
    box-sizing: border-box !important;
    width: auto !important;
    max-width: none !important;
    max-height: calc(100dvh - var(--bottom-nav-height) - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 32px) !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  /* Five stable navigation slots, with no map control residue on Home. */
  html body .app-shell .bottom-nav {
    position: fixed !important;
    right: max(8px, env(safe-area-inset-right)) !important;
    bottom: max(6px, env(safe-area-inset-bottom)) !important;
    left: max(8px, env(safe-area-inset-left)) !important;
    z-index: 1600 !important;
    box-sizing: border-box !important;
    display: grid !important;
    grid-template-columns: repeat(5, minmax(0, 1fr)) !important;
    align-items: center !important;
    width: auto !important;
    height: var(--bottom-nav-height) !important;
    min-height: var(--bottom-nav-height) !important;
    margin: 0 !important;
    padding: 6px !important;
    gap: 0 !important;
  }

  html body .app-shell .bottom-nav > * {
    min-width: 0 !important;
  }

  html body .app-shell .bottom-nav-item {
    box-sizing: border-box !important;
    width: 100% !important;
    min-width: 0 !important;
    height: 60px !important;
    padding: 6px 2px !important;
    font-size: clamp(0.58rem, 2.6vw, 0.72rem) !important;
    line-height: 1.08 !important;
    white-space: normal !important;
  }

  html body .app-shell .add-memory-button {
    position: relative !important;
    inset: auto !important;
    justify-self: center !important;
    align-self: center !important;
    width: 58px !important;
    height: 58px !important;
    min-width: 58px !important;
    min-height: 58px !important;
    margin: 0 !important;
    transform: none !important;
  }

  html body .app-shell .map-stage:not(.map-focus) .map-navigation-gadget,
  html body .app-shell .map-stage:not(.map-focus) .living-memory-preview,
  html body .app-shell .map-stage:not(.map-focus) .map-coordinates {
    display: none !important;
  }
}

@media (max-width: 380px) {
  html body .app-shell .topbar {
    grid-template-columns: 82px minmax(0, 1fr) 82px !important;
  }

  html body .app-shell .topbar-left,
  html body .app-shell .topbar-actions {
    grid-template-columns: repeat(2, 38px) !important;
    width: 82px !important;
    min-width: 82px !important;
  }

  html body .app-shell .topbar-left > .icon-button,
  html body .app-shell .topbar-left > .avatar-button,
  html body .app-shell .topbar-actions > .icon-button:not(.hidden-control) {
    width: 38px !important;
    height: 38px !important;
    min-width: 38px !important;
    min-height: 38px !important;
  }
}

@media (prefers-reduced-motion: reduce) {
  html body .app-shell *,
  html body .app-shell *::before,
  html body .app-shell *::after {
    scroll-behavior: auto !important;
  }
}
