/* Keep desktop Marketplace card actions clear of the guide launcher.
   The guide's own collision resolver still controls inline positions when
   another floating control has been dragged over it. */
@media (min-width: 761px) {
  body.miki-pulse:has(.mk-project-shell) .miki-ai-guide {
    right: 0;
    bottom: calc(116px + env(safe-area-inset-bottom));
  }

  body.miki-pulse:has(.mk-project-shell) .miki-ai-guide__launcher {
    display: grid;
    place-items: center;
    width: 32px;
    min-height: 42px;
    padding: 0;
    border-radius: 10px 0 0 10px;
    font-size: 0 !important;
  }

  body.miki-pulse:has(.mk-project-shell) .miki-ai-guide__launcher::before {
    content: '✦';
    font-size: 18px;
    line-height: 1;
  }
}

/* M1 public mobile Marketplace prototype. The actual music and guide nodes
   sit in the filter row; no fixed control or permanent rail covers cards. */
@media (max-width: 640px) {
  body.miki-pulse.miki-page-public .mk-app:not(.is-embedded):not(.is-detail-view)
    .mk-layout.mk-mobile-tools-mounted > .mk-filters {
    grid-column: 1;
    grid-row: 2;
  }

  /* Closed summary and the two 44px launchers occupy separate hit areas.
     The open filter regains the full row; its tools move below it. */
  body.miki-pulse.miki-page-public .mk-app:not(.is-embedded):not(.is-detail-view)
    .mk-layout.mk-mobile-tools-mounted:not(:has(> .mk-filters[open])) > .mk-filters {
    width: calc(100% - 102px);
    min-width: 0;
    justify-self: start;
  }

  body.miki-pulse.miki-page-public .mk-app:not(.is-embedded):not(.is-detail-view)
    .mk-layout.mk-mobile-tools-mounted > .mk-main-column {
    grid-column: 1;
    grid-row: 3;
  }

  body.miki-pulse.miki-page-public .mk-app:not(.is-embedded):not(.is-detail-view)
    .mk-layout.mk-mobile-tools-mounted > .mk-mobile-tools {
    display: flex;
    align-items: center;
    gap: 4px;
    grid-column: 1;
    grid-row: 2;
    justify-self: end;
    align-self: start;
    box-sizing: border-box;
    width: 92px;
    height: 44px;
    margin: 2px 0 0;
    position: relative;
    z-index: 3;
  }

  /* Expanded filters use the whole heading for their explanatory text.
     Move tools to their own in-flow row until filters are collapsed again. */
  body.miki-pulse.miki-page-public .mk-app:not(.is-embedded):not(.is-detail-view)
    .mk-layout.mk-mobile-tools-mounted:has(> .mk-filters[open]) > .mk-mobile-tools {
    grid-row: 3;
    justify-self: stretch;
    width: auto;
    height: 44px;
    margin: 0;
    padding: 0 8px;
    justify-content: flex-end;
    border: 1px solid rgba(146, 166, 194, .16);
    border-radius: 9px;
    background: #0b1019;
  }

  body.miki-pulse.miki-page-public .mk-app:not(.is-embedded):not(.is-detail-view)
    .mk-layout.mk-mobile-tools-mounted:has(> .mk-filters[open]) > .mk-mobile-tools::before {
    content: 'Инструменты';
    margin-right: auto;
    align-self: center;
    color: #a6b3ca;
    font-size: 12px;
  }

  body.miki-pulse.miki-page-public .mk-app:not(.is-embedded):not(.is-detail-view)
    .mk-layout.mk-mobile-tools-mounted:has(> .mk-filters[open]) > .mk-main-column {
    grid-row: 4;
  }

  body.miki-pulse.miki-page-public .mk-app:not(.is-embedded):not(.is-detail-view)
    .mk-layout.mk-mobile-tools-mounted .mk-grid,
  body.miki-pulse.miki-page-public .mk-app:not(.is-embedded):not(.is-detail-view)
    .mk-layout.mk-mobile-tools-mounted .mk-grid:has(> .mk-unified-card) {
    box-sizing: border-box;
    grid-template-columns: minmax(0, 1fr);
    padding-right: 0;
  }

  body.miki-pulse.miki-page-public .mk-app:not(.is-embedded):not(.is-detail-view)
    .mk-layout.mk-mobile-tools-mounted .mk-card.mk-unified-card[data-market-kind="vehicles"] {
    grid-template-rows: clamp(170px, 52vw, 205px) minmax(0, 1fr) auto !important;
  }

  body.miki-pulse.miki-page-public .mk-app:not(.is-embedded):not(.is-detail-view)
    .mk-layout.mk-mobile-tools-mounted .mk-card.mk-unified-card[data-market-kind="vehicles"] .mk-unified-card__media {
    height: clamp(170px, 52vw, 205px) !important;
    min-height: clamp(170px, 52vw, 205px) !important;
    max-height: clamp(170px, 52vw, 205px) !important;
  }

  body.miki-pulse.miki-page-public:has(.mk-app:not(.is-embedded):not(.is-detail-view) .mk-layout.mk-mobile-tools-mounted)
    .mk-mobile-tools > .miki-music-widget,
  body.miki-pulse.miki-page-public:has(.mk-app:not(.is-embedded):not(.is-detail-view) .mk-layout.mk-mobile-tools-mounted)
    .mk-mobile-tools > .miki-ai-guide {
    position: relative !important;
    inset: auto !important;
    top: auto !important;
    right: auto !important;
    bottom: auto !important;
    left: auto !important;
    display: block !important;
    flex: 0 0 44px;
    width: 44px !important;
    height: 44px !important;
    margin: 0 !important;
    transform: none !important;
  }

  /* The guide normally stacks above music. While music is expanded its fixed
     dialog, including Close, must stay above a guide launcher scrolling past. */
  body.miki-pulse.miki-page-public:has(.mk-app:not(.is-embedded):not(.is-detail-view) .mk-layout.mk-mobile-tools-mounted)
    .mk-mobile-tools > .miki-music-widget:has(> .miki-music-widget__launcher[aria-expanded="true"]) {
    z-index: 1701 !important;
  }

  body.miki-pulse.miki-page-public:has(.mk-app:not(.is-embedded):not(.is-detail-view) .mk-layout.mk-mobile-tools-mounted)
    .mk-mobile-tools > .miki-ai-guide .miki-ai-guide__panel {
    position: fixed !important;
    inset: auto max(8px, env(safe-area-inset-right)) calc(12px + env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left)) !important;
    width: auto;
    max-height: min(440px, calc(100dvh - 80px - env(safe-area-inset-bottom)));
  }

  /* The short-viewport music dialog must clear the sticky login/header row;
     the inherited 64px bottom offset puts its close button underneath it. */
  body.miki-pulse.miki-page-public:has(.mk-app:not(.is-embedded):not(.is-detail-view) .mk-layout.mk-mobile-tools-mounted)
    .mk-mobile-tools > .miki-music-widget .miki-music-widget__panel {
    top: calc(76px + env(safe-area-inset-top)) !important;
    bottom: auto !important;
    height: min(556px, calc(100dvh - 84px - env(safe-area-inset-top) - env(safe-area-inset-bottom))) !important;
  }

  body.miki-pulse.miki-page-public:has(.mk-app:not(.is-embedded):not(.is-detail-view) .mk-layout.mk-mobile-tools-mounted)
    .site-counter-floating.site-counter-floating {
    display: none !important;
  }
}

/* Mobile floating controls: keep public card actions readable without
   changing the home, family or farm layouts. Load after the existing widget
   styles; the original versioned assets remain unchanged for rollback. */
@media (max-width: 640px) {
  body.miki-pulse:not(.miki-farm-page):not(:has(.home-main)):not(:has(.family-hub.dark-family:not(.is-embedded)))
    .miki-ai-guide {
    left: max(10px, env(safe-area-inset-left));
    right: auto;
    bottom: calc(12px + env(safe-area-inset-bottom));
  }

  body.miki-pulse:not(.miki-farm-page):not(:has(.home-main)):not(:has(.family-hub.dark-family:not(.is-embedded)))
    .miki-ai-guide__panel {
    left: 0;
    right: auto;
    max-height: min(440px, calc(100dvh - 125px - env(safe-area-inset-bottom)));
  }

  /* A dragged music launcher has an inline position and keeps it. */
  body.miki-pulse:not(.miki-farm-page):not(:has(.home-main)):not(:has(.family-hub.dark-family:not(.is-embedded)))
    .miki-music-widget:not([style*="left:"]):not([style*="inset:"]) {
    top: auto;
    left: auto;
    right: max(10px, env(safe-area-inset-right));
    bottom: calc(12px + env(safe-area-inset-bottom));
  }

  body.miki-pulse:not(.miki-farm-page):not(:has(.home-main)):not(:has(.family-hub.dark-family:not(.is-embedded)))
    .miki-music-widget__panel:not([style*="left:"]):not([style*="inset:"]) {
    top: auto;
    left: auto;
    right: max(10px, env(safe-area-inset-right));
    bottom: calc(64px + env(safe-area-inset-bottom));
    width: min(614px, calc(100% - 20px));
    height: min(556px, calc(100dvh - 80px - env(safe-area-inset-bottom)));
    min-height: min(240px, calc(100dvh - 80px - env(safe-area-inset-bottom)));
    transform: none;
  }

  /* Keep the small visitor counter between the two launchers. */
  body.miki-pulse:not(.miki-farm-page):not(:has(.home-main)):not(:has(.family-hub.dark-family:not(.is-embedded)))
    .site-counter-floating {
    left: 50% !important;
    right: auto !important;
    transform: translateX(-50%) scale(.58) !important;
    transform-origin: center bottom !important;
  }

  /* Public Marketplace uses a narrow card-free rail for persistent controls.
     Keep a user's manually dragged music position; these are defaults only. */
  body.miki-pulse.miki-page-public:has(.mk-app:not(.is-embedded):not(.is-detail-view) .mk-grid)
    .miki-ai-guide {
    left: auto !important;
    right: max(8px, env(safe-area-inset-right)) !important;
    bottom: calc(64px + env(safe-area-inset-bottom)) !important;
  }

  body.miki-pulse.miki-page-public:has(.mk-app:not(.is-embedded):not(.is-detail-view) .mk-grid)
    .miki-ai-guide__launcher {
    width: 44px !important;
    min-height: 44px;
    padding: 8px 0 !important;
  }

  body.miki-pulse.miki-page-public:has(.mk-app:not(.is-embedded):not(.is-detail-view) .mk-grid)
    .miki-ai-guide__panel {
    left: auto !important;
    right: 0 !important;
  }

  body.miki-pulse.miki-page-public:has(.mk-app:not(.is-embedded):not(.is-detail-view) .mk-grid)
    .site-counter-floating.site-counter-floating {
    left: auto !important;
    right: max(12px, env(safe-area-inset-right)) !important;
    bottom: calc(116px + env(safe-area-inset-bottom)) !important;
    transform: scale(.58) !important;
    transform-origin: right bottom !important;
  }
}
