/* Progressive enhancement: the desktop menus and legacy fallback stay untouched. */
#mobile-map-tools,
#mobile-control-dialog,
.profile-sheet-handle[hidden],
.artist-profile-panel .profile-sheet-preview[hidden] { display: none; }

@media (max-width: 480px) {
  body[data-mobile-navigation="v2"] {
    --mobile-paper: #fbfaf7;
    --mobile-ink: #2b2825;
    --mobile-muted: #8d867e;
    --mobile-red: #e60012;
    --mobile-tools-offset: calc(66px + env(safe-area-inset-top));
    --mobile-profile-peek: calc(136px + env(safe-area-inset-bottom));
  }
  body[data-mobile-navigation="v2"] #top-bar,
  body[data-map-layout="rail"] :is(#top-bar, #map-home-panel) { display: none; }
  body[data-mobile-navigation="v2"] #map .leaflet-top.leaflet-left { top: var(--mobile-tools-offset); }
  /* Match the Leaflet hit area and anchor to the mobile avatar, not its desktop box. */
  #map .singer-custom-marker:not(.network-node) {
    width: calc(38px * var(--profile-avatar-scale, 1)) !important;
    height: calc(38px * var(--profile-avatar-scale, 1)) !important;
    margin-left: calc(-19px * var(--profile-avatar-scale, 1)) !important;
    margin-top: calc(-19px * var(--profile-avatar-scale, 1)) !important;
    pointer-events: none;
  }
  #map .singer-custom-marker:not(.network-node) .singer-marker-container {
    transform: scale(var(--profile-avatar-scale, 1));
  }
  #map .singer-custom-marker:not(.network-node) .singer-avatar-main { pointer-events: auto; }
  body[data-mobile-navigation="v2"] #mobile-map-tools,
  body[data-mobile-navigation="v2"] #mobile-map-tools *,
  body[data-mobile-navigation="v2"] #mobile-control-dialog,
  body[data-mobile-navigation="v2"] #mobile-control-dialog * { box-sizing: border-box; }

  body[data-mobile-navigation="v2"] #mobile-map-tools {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 1850;
    pointer-events: none;
    color: var(--mobile-ink);
  }
  #mobile-map-tools button {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    min-width: 0;
    margin: 0;
    border: 0;
    background: transparent;
    color: inherit;
    font: inherit;
    cursor: pointer;
    pointer-events: auto;
    -webkit-tap-highlight-color: transparent;
  }
  #mobile-map-tools button svg { display: block; width: 20px; height: 20px; }
  #mobile-map-tools .mobile-map-search, .mobile-map-tool-rail {
    position: absolute;
    border: 1px solid #fff;
    background: #fffdfa;
    box-shadow: 0 5px 22px #392b3122, 0 1px 4px #392b3112;
  }
  #mobile-map-tools .mobile-map-search {
    top: calc(8px + env(safe-area-inset-top));
    left: max(12px, env(safe-area-inset-left));
    right: max(12px, env(safe-area-inset-right));
    height: 48px;
    padding: 0 15px;
    justify-content: flex-start;
    gap: 10px;
    border-radius: 24px;
    font-size: 15px;
    font-weight: 650;
    color: #5d554f;
  }
  .mobile-map-search svg { color: var(--mobile-red); }
  .mobile-map-tool-rail {
    top: var(--mobile-tools-offset);
    right: max(10px, env(safe-area-inset-right));
    display: grid;
    gap: 2px;
    width: 54px;
    padding: 3px;
    border-radius: 17px;
  }
  #mobile-map-tools .mobile-map-tool-rail button { flex-direction: column; min-height: 45px; padding: 4px 0; border-radius: 12px; font-size: 10px; font-weight: 700; }
  #mobile-map-tools button[aria-expanded="true"], #mobile-map-tools button:active { color: var(--mobile-red); background-color: #fcece9; }
  body:has(#artist-profile-panel[data-profile-sheet="expanded"]:not([hidden])) .mobile-map-tool-rail { display: none; }
  #mobile-map-tools :focus-visible,
  #mobile-control-dialog :focus-visible { outline: 2px solid var(--mobile-red); outline-offset: 2px; }

  /* A native dialog owns the modal focus; visualViewport keeps it above keyboards. */
  body[data-mobile-navigation="v2"] #mobile-control-dialog[open] {
    display: block;
    position: fixed;
    top: var(--mobile-viewport-top, 0px);
    left: 0;
    right: auto;
    bottom: auto;
    width: 100%;
    height: var(--mobile-viewport-height, 100dvh);
    max-width: none;
    max-height: none;
    margin: 0;
    padding: 0;
    border: 0;
    overflow: hidden;
    background: transparent;
    color: var(--mobile-ink);
    overscroll-behavior: contain;
  }
  #mobile-control-dialog::backdrop { background: transparent; }
  .mobile-panel-scrim {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    border: 0;
    padding: 0;
    background: rgba(39,32,27,.17);
    opacity: 0;
    transition: opacity .24s ease;
  }
  .mobile-panel-scrim:focus-visible { outline-offset: -3px; }
  .mobile-panel-sheet {
    position: absolute;
    left: 12px;
    right: 12px;
    bottom: max(8px, env(safe-area-inset-bottom));
    display: flex;
    flex-direction: column;
    height: min(68dvh, 590px);
    max-height: calc(var(--mobile-viewport-height, 100dvh) - max(8px, env(safe-area-inset-bottom)) - 14px);
    border: 1px solid rgba(255,255,255,.95);
    border-radius: 28px;
    background: var(--mobile-paper);
    box-shadow: 0 14px 42px rgba(39,32,27,.15);
    overflow: hidden;
    opacity: 0;
    transform: translateY(calc(24px + var(--mobile-drag-y, 0px)));
    transition: transform .24s cubic-bezier(.2,.8,.2,1), opacity .24s ease, height .26s ease;
  }
  #mobile-control-dialog[data-panel="search"] .mobile-panel-sheet { height: min(54dvh, 470px); }
  #mobile-control-dialog[data-panel="recommend"] .mobile-panel-sheet { height: auto; }
  #mobile-view-recommend { padding: 0 12px 12px; overflow-y: auto; }
  #mobile-view-recommend .home-release-carousel { flex-shrink: 0; border-radius: 18px; }
  #mobile-control-dialog.is-open .mobile-panel-scrim { opacity: 1; }
  #mobile-control-dialog.is-open .mobile-panel-sheet { opacity: 1; transform: translateY(var(--mobile-drag-y, 0px)); }
  #mobile-control-dialog .mobile-panel-sheet.is-dragging { transition: none; }
  .mobile-panel-header { flex-shrink: 0; padding: 10px 20px 16px; touch-action: none; user-select: none; }
  .mobile-panel-handle { width: 34px; height: 4px; margin: 0 auto 14px; border-radius: 4px; background: #dcd6cf; }
  .mobile-panel-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
  .mobile-panel-brand { color: var(--mobile-muted); font-size: 10px; letter-spacing: 1px; }
  #mobile-panel-title { margin: 4px 0 0; color: var(--mobile-ink); font-size: 24px; line-height: 1.3; font-weight: 850; }
  #mobile-panel-close { width: 36px; height: 36px; padding: 0; border: 0; border-radius: 50%; background: #eeeae4; color: #7e766e; font-size: 25px; line-height: 1; cursor: pointer; }
  .mobile-panel-view { display: flex; flex-direction: column; flex: 1; min-height: 0; animation: mobile-view-enter .18s ease-out both; }
  #mobile-control-dialog [hidden] { display: none !important; }

  #mobile-view-search { --mobile-search-button-width: 40px; --mobile-search-gap: 8px; padding: 0 20px 12px; }
  #mobile-search-toolbar { display: grid; grid-template-columns: minmax(0, 1fr) var(--mobile-search-button-width); gap: var(--mobile-search-gap); align-items: center; flex-shrink: 0; padding-bottom: 10px; }
  #mobile-control-dialog #search-input-inner { flex: 1; min-width: 0; max-width: none; }
  #mobile-control-dialog #singer-search { width: 100%; min-width: 0; height: 46px; padding: 0 32px 0 42px; background: #eeeae4; color: var(--mobile-ink); border: 1px solid transparent; border-radius: 15px; box-shadow: none; font-size: 16px; }
  #mobile-control-dialog #singer-search:focus { border-color: #e9b1ab; }
  #mobile-control-dialog #singer-search::placeholder { color: #9b938b; }
  #mobile-control-dialog #search-input-inner::before { border-color: #a49b91; }
  #mobile-control-dialog #search-input-inner::after { background: #a49b91; }
  #mobile-control-dialog #search-clear-btn { background: #c6bdb3; }
  #mobile-control-dialog #search-btn { display: flex; width: var(--mobile-search-button-width); height: 40px; border-radius: 50%; box-shadow: none; transform: none; }
  #mobile-control-dialog #search-mode-switch { grid-column: 1 / -1; justify-self: end; width: clamp(112px, 35vw, 145px); height: 36px; }
  #mobile-control-dialog #search-mode-switch button { font-size: 11px; }
  #mobile-search-results { width: calc(100% - var(--mobile-search-button-width) - var(--mobile-search-gap)); flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
  #mobile-control-dialog #search-suggestions { position: static; width: 100%; max-height: none; margin: 0; border: 0; border-radius: 14px; background: #fff; box-shadow: none; overflow: visible; }
  #mobile-control-dialog .suggestion-item { min-height: 48px; padding: 14px 12px; color: var(--mobile-ink); border-bottom: 1px solid #f0ece7; }
  #mobile-control-dialog .suggestion-item:hover { background: #fcece9; }
  #mobile-control-dialog #mobile-history-section { display: block; margin: 10px 2px 0; }
  #mobile-control-dialog[data-search-query="true"] #mobile-history-section { display: none; }
  #mobile-control-dialog .mobile-sheet-title { color: var(--mobile-muted); font-size: 12px; font-weight: 500; }
  #mobile-control-dialog #mobile-history-clear { color: #a0968a; font-size: 12px; }
  #mobile-control-dialog .mobile-history-item { min-height: 46px; color: var(--mobile-ink); border-color: #eae4dd; font-size: 14px; }
  #mobile-control-dialog .mobile-history-empty,
  #mobile-search-empty { color: var(--mobile-muted); padding: 12px 0; margin: 0; font-size: 13px; line-height: 1.8; }

  /* Original segmented controls, thresholds and filters remain fully interactive. */
  #mobile-view-filter { padding: 0 18px 16px; overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin; scrollbar-color: #dcd6cf transparent; }
  .mobile-filter-heading { padding: 0 2px 8px; color: var(--mobile-muted); font-size: 12px; }
  #mobile-control-dialog #mode-filter-tabs { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); position: relative; width: 100%; min-width: 0; height: 44px; padding: 4px; border-radius: 14px; background: #eeeae4; z-index: auto; }
  #mobile-control-dialog #mode-filter-tabs::before,
  #mobile-control-dialog #mode-filter-tabs::after,
  #mobile-control-dialog .mode-filter-slider { display: none; }
  #mobile-control-dialog #mode-filter-tabs .filter-btn { display: flex; position: static; width: auto; height: 36px; margin: 0; padding: 0; border-radius: 11px; color: #8a8178; background: transparent; font-size: 13px; }
  #mobile-control-dialog #mode-filter-tabs .filter-btn.active { color: #fff; background: var(--mobile-red); }
  #mobile-filter-options { margin-top: 10px; }
  #mobile-control-dialog #more-options-dropdown { display: flex; position: static !important; inset: auto !important; width: 100% !important; max-height: none; margin: 0; padding: 0; opacity: 1; pointer-events: auto; transform: none; border-radius: 16px; background: #fff; box-shadow: none; overflow: visible; }
  #mobile-control-dialog .dropdown-category-label { padding: 16px 14px 6px; color: #a0968a; font-size: 11px; border: 0; }
  #mobile-control-dialog .dots-only-option { min-height: 48px; padding: 12px 14px; color: #514b44; border-color: #f0ece7; }
  #mobile-control-dialog .fan-threshold-control { padding: 15px 14px; color: #514b44; border-color: #f0ece7; }
  #mobile-control-dialog .fan-threshold-labels { color: #a0968a; }
  #mobile-control-dialog .dropdown-item { min-height: 46px; padding: 12px 14px; border: 0; border-bottom: 1px solid #f0ece7; color: #514b44; font-size: 12px; line-height: 1.6; white-space: normal; text-align: left; }
  #mobile-control-dialog .dropdown-item.active { background: #fcece9 !important; color: var(--mobile-red) !important; }

  #mobile-control-dialog #rank-panel,
  #mobile-control-dialog #index-panel { display: flex; position: static !important; inset: auto !important; width: 100% !important; height: 100%; max-height: none; min-height: 0; margin: 0; border-radius: 0; background: transparent; box-shadow: none; backdrop-filter: none; transform: none; opacity: 1; pointer-events: auto; }
  #mobile-control-dialog #rank-tab-bar,
  #mobile-control-dialog #index-tab-bar { margin: 0 18px 8px; background: #eeeae4; }
  #mobile-control-dialog .rank-tab,
  #mobile-control-dialog .index-tab { min-height: 36px; padding: 10px 2px; color: #8a8178; }
  #mobile-control-dialog .rank-tab.active,
  #mobile-control-dialog .index-tab.active { color: #fff; }
  #mobile-control-dialog #rank-panel-subtitle { padding: 0 18px 3px; color: #a0968a; }
  #mobile-control-dialog #rank-fan-month-bar { margin: 6px 18px 0; background: #f0ece7; }
  #mobile-control-dialog #rank-panel-body,
  #mobile-control-dialog #index-list { min-height: 0; flex: 1; padding: 8px 18px 16px; overscroll-behavior: contain; }
  #mobile-control-dialog .rank-row { min-height: 54px; border-color: #eee8e0; }
  #mobile-control-dialog .rank-name,
  #mobile-control-dialog .index-name { color: var(--mobile-ink); }
  #mobile-control-dialog .rank-val { color: var(--mobile-red); font-variant-numeric: tabular-nums; }
  #mobile-control-dialog .rank-avatar { width: 32px; height: 32px; }
  #mobile-control-dialog .rank-network-btn { width: 32px; height: 36px; }
  #mobile-control-dialog .index-letter-header { padding-top: 12px; color: #a0968a; }
  #mobile-control-dialog .index-row { min-height: 65px; }
  #mobile-control-dialog .index-avatar { border-color: #fff; }

  #artist-profile-panel[data-profile-sheet] {
    --profile-sheet-rest: var(--mobile-profile-peek);
    top: auto; bottom: 0; left: 0; right: 0; width: auto;
    height: var(--profile-drag-height, var(--profile-sheet-rest));
    max-height: calc(100dvh - var(--mobile-tools-offset) - 8px);
    padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    border: 0; border-radius: 24px 24px 0 0; z-index: 1860;
    background: #fff; box-shadow: 0 -4px 28px #392b3126;
    transition: height .25s cubic-bezier(.2,.8,.2,1);
  }
  #artist-profile-panel[data-profile-sheet="expanded"] { --profile-sheet-rest: 76dvh; }
  #artist-profile-panel.is-sheet-dragging { transition: none; }
  #artist-profile-panel[data-profile-sheet] .profile-toolbar { inset: 0 8px auto; height: 42px; padding: 0; align-items: center; }
  #artist-profile-panel[data-profile-sheet] :is(.profile-back, .profile-close) { width: 40px; height: 40px; color: #625950; background: transparent; backdrop-filter: none; text-shadow: none; }
  #artist-profile-panel[data-profile-sheet] .profile-back { margin-right: auto; }
  #artist-profile-panel[data-profile-sheet] .profile-back svg { filter: none; }
  #artist-profile-panel[data-profile-sheet] .profile-scroll { flex: 1; padding-bottom: 14px; }
  #artist-profile-panel[data-profile-sheet="peek"]:not(.is-sheet-dragging) .profile-scroll { display: none; }
  #artist-profile-panel[data-profile-sheet] .profile-scrollbar { display: none; }
  .profile-sheet-handle {
    align-self: center; display: grid; place-items: center; flex: 0 0 42px;
    width: calc(100% - 104px); min-width: 56px; padding: 0; border: 0; background: none;
    cursor: pointer; touch-action: none;
  }
  .profile-sheet-handle::before { content: ''; width: 38px; height: 4px; border-radius: 4px; background: #cbc2b8; }
  .artist-profile-panel .profile-sheet-preview {
    display: flex; align-items: center; gap: 13px; flex-shrink: 0; min-height: 78px; margin: 0 14px;
    padding: 0 5px; border: 0; color: var(--profile-ink); background: none; text-align: left; cursor: pointer; touch-action: none;
  }
  .profile-sheet-preview .profile-sheet-avatar, .profile-sheet-preview .profile-album-cover { flex: 0 0 54px; width: 54px; height: 54px; }
  .profile-sheet-preview .profile-album-cover, .profile-sheet-preview .is-group { border-radius: 11px; }
  .profile-sheet-copy { display: grid; flex: 1; min-width: 0; gap: 3px; }
  .profile-sheet-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 19px; line-height: 1.35; }
  .profile-sheet-meta { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; color: #6d645e; }
  .profile-sheet-hint { margin-left: auto; color: #6d645e; white-space: nowrap; font-size: 11px; }
  body:has(#artist-profile-panel[data-profile-sheet="peek"]:not([hidden])) #map .leaflet-bottom { bottom: var(--mobile-profile-peek); }
  body:has(#artist-profile-panel[data-profile-sheet="expanded"]:not([hidden])) #map .leaflet-bottom { bottom: min(76dvh, calc(100dvh - var(--mobile-tools-offset) - 8px)); }
  body[data-mobile-navigation="v2"] :is(#exit-network-btn, #network-switch-dock).network-dock { bottom: calc(20px + env(safe-area-inset-bottom)); }
  body[data-mobile-navigation="v2"]:has(#artist-profile-panel[data-profile-sheet="peek"]:not([hidden])) :is(#exit-network-btn, #network-switch-dock).network-dock { bottom: calc(var(--mobile-profile-peek) + 8px); }
  body:has(#artist-profile-panel[data-profile-sheet="expanded"]:not([hidden])) :is(#exit-network-btn, #network-switch-dock).network-dock { visibility: hidden; }
  @keyframes mobile-view-enter { from { opacity: .5; transform: translateY(5px); } to { opacity: 1; transform: translateY(0); } }
}

@media (max-width: 480px) and (prefers-reduced-motion: reduce) {
  #mobile-control-dialog *,
  #mobile-map-tools *,
  #artist-profile-panel[data-profile-sheet] { animation: none !important; transition: none !important; }
}
