/* Merphi mobile-first structural repair
   Breakpoints: mobile <=767 | tablet 768–1023 | desktop >=1024
   Loaded after ui-v3.css — wins over fixed-header / padding bugs. */

:root {
  --mr-header: 64px;
  --mr-bottom-nav: 64px;
  --mr-page-pad: 16px;
  --mr-control-min: 44px;
  --mr-touch-font: 16px;
}

html.ui-v2.mr-keyboard-open .mobile-bottom-nav {
  display: none !important;
}

/* —— Overlay singleton —— */
body.mr-overlay-open {
  overflow: hidden;
  overscroll-behavior: none;
}

/* —— Shell: mobile + tablet (no clipped headings) —— */
@media (max-width: 1023px) {
  html.ui-v2 {
    --ui-v2-header: var(--mr-header);
  }

  /* Sticky header in document flow → no h1 clipping */
  html.ui-v2 .global-header {
    position: sticky !important;
    top: 0 !important;
    left: auto !important;
    right: auto !important;
    width: 100% !important;
    height: calc(var(--mr-header) + env(safe-area-inset-top, 0px)) !important;
    min-height: calc(var(--mr-header) + env(safe-area-inset-top, 0px)) !important;
    padding-top: env(safe-area-inset-top, 0px) !important;
    z-index: 60 !important;
    background: var(--ink, #0b0f14) !important;
  }

  html.ui-v2 .app-shell-main {
    margin-left: 0 !important;
    min-width: 0;
    width: 100%;
  }

  html.ui-v2 .workspace {
    padding-top: 0 !important;
  }
}

@media (max-width: 767px) {
  html.ui-v2 .global-header {
    padding-left: 10px !important;
    padding-right: 10px !important;
  }

  html.ui-v2 .workspace {
    padding-left: var(--mr-page-pad) !important;
    padding-right: var(--mr-page-pad) !important;
    padding-bottom: calc(var(--mr-bottom-nav) + env(safe-area-inset-bottom, 0px) + 16px) !important;
    width: 100% !important;
    max-width: 100% !important;
    box-sizing: border-box;
    overflow-x: clip;
  }

  html.ui-v2 .page-shell,
  html.ui-v2 .catalog-v2,
  html.ui-v2 .workspace-page {
    width: 100% !important;
    max-width: 100% !important;
  }

  html.ui-v2 .page-header h1,
  html.ui-v2 .catalog-page-header h1,
  html.ui-v2 .page-shell > header h1 {
    font-size: 1.625rem !important; /* ~26px, not desktop scale */
    line-height: 1.2 !important;
    margin: 0 0 6px !important;
    overflow-wrap: anywhere;
  }

  html.ui-v2 .page-header,
  html.ui-v2 .catalog-page-header {
    padding-top: 12px !important;
    margin-top: 0 !important;
  }

  html.ui-v2 .brand-wordmark-header {
    width: clamp(118px, 32vw, 132px) !important;
    height: auto !important;
  }

  html.ui-v2 .global-header .icon-button,
  html.ui-v2 .global-header .button-create-mobile,
  html.ui-v2 .header-create-compact {
    min-width: var(--mr-control-min);
    min-height: var(--mr-control-min);
  }

  html.ui-v2 .account-copy {
    display: none !important;
  }

  html.ui-v2 .mobile-bottom-nav {
    display: grid !important;
    position: fixed !important;
    left: 0;
    right: 0;
    bottom: 0;
    height: calc(var(--mr-bottom-nav) + env(safe-area-inset-bottom, 0px)) !important;
    padding: 0 4px env(safe-area-inset-bottom, 0px) !important;
    z-index: 55 !important;
    box-sizing: border-box;
  }

  html.ui-v2 .mobile-bottom-nav > [data-mobile-nav-primary],
  html.ui-v2 .mobile-bottom-nav > .mobile-nav-more-toggle,
  html.ui-v2 .mobile-bottom-nav a,
  html.ui-v2 .mobile-bottom-nav button {
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
    text-align: center !important;
    width: 100%;
    min-width: 0;
    max-width: 100%;
    padding: 6px 2px 4px;
    gap: 3px;
    box-sizing: border-box;
  }

  html.ui-v2 .mobile-bottom-nav > [data-mobile-nav-primary] > span,
  html.ui-v2 .mobile-bottom-nav > .mobile-nav-more-toggle > span,
  html.ui-v2 .mobile-bottom-nav a > span,
  html.ui-v2 .mobile-bottom-nav button > span {
    display: block !important;
    width: 100%;
    min-width: 0;
    text-align: center !important;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    line-height: 1.15;
  }

  html.ui-v2 .mobile-bottom-nav svg {
    display: block;
    margin: 0 auto;
    flex: 0 0 auto;
  }

  html.ui-v2 .mobile-bottom-nav.editor-hidden {
    display: none !important;
  }

  html.ui-v2 .control,
  html.ui-v2 .search-control .control,
  html.ui-v2 button.button {
    min-height: var(--mr-control-min);
    font-size: var(--mr-touch-font);
  }

  html.ui-v2 .button.button-small {
    min-height: 40px;
    font-size: 15px;
  }

  /* —— Forbid desktop split panes —— */
  html.ui-v2 .two-pane,
  html.ui-v2 .partners-desk,
  html.ui-v2 .moderation-desk,
  html.ui-v2 .document-grid {
    display: flex !important;
    flex-direction: column !important;
    grid-template-columns: 1fr !important;
    height: auto !important;
    max-height: none !important;
    min-height: 0 !important;
  }

  html.ui-v2 .partners-desk.mr-list-only .pane-detail,
  html.ui-v2 .partners-desk.mr-list-only #partner-detail {
    display: none !important;
  }

  html.ui-v2 .partners-desk.mr-detail-only .pane-list {
    display: none !important;
  }

  html.ui-v2 .partners-desk.mr-detail-only .pane-detail,
  html.ui-v2 .partners-desk.mr-detail-only #partner-detail {
    display: block !important;
    width: 100%;
  }

  html.ui-v2 .pane-list,
  html.ui-v2 .pane-detail {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
  }

  /* Operations: hide wrapping subnav pills on hub; show list cards */
  html.ui-v2 .operations-subnav {
    display: flex !important;
    flex-wrap: nowrap !important;
    gap: 8px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 4px;
  }

  html.ui-v2 .operations-subnav::-webkit-scrollbar {
    display: none;
  }

  html.ui-v2 .operations-hub {
    display: flex !important;
    flex-direction: column !important;
    gap: 10px !important;
  }

  html.ui-v2 .operations-card {
    width: 100%;
    text-align: left;
    min-height: 72px;
    padding: 16px !important;
  }

  html.ui-v2 .settings-tabs,
  html.ui-v2 .partners-subnav {
    display: flex !important;
    flex-wrap: nowrap !important;
    overflow-x: auto;
    gap: 8px;
    -webkit-overflow-scrolling: touch;
  }

  html.ui-v2 .settings-tabs .button,
  html.ui-v2 .partners-subnav .button {
    flex: 0 0 auto;
    white-space: nowrap;
  }

  html.ui-v2 .document-grid > aside.side-surface {
    width: 100%;
  }

  /* Moderation: single detail; queue/checks as drawers */
  html.ui-v2 .moderation-desk .moderation-queue,
  html.ui-v2 .moderation-desk .moderation-side {
    /* drawers handled below when .drawer-open */
  }

  html.ui-v2 .moderation-actions.sticky-action-bar {
    position: sticky;
    bottom: calc(var(--mr-bottom-nav) + env(safe-area-inset-bottom, 0px));
    z-index: 40;
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 8px;
    padding: 10px 0 calc(10px + env(safe-area-inset-bottom, 0px));
    background: linear-gradient(180deg, transparent, var(--surface-1, #f3f4f6) 28%);
  }

  html.ui-v2 .moderation-actions .mr-mod-primary {
    grid-column: 1 / -1;
  }

  html.ui-v2 .moderation-actions .mr-mod-more {
    min-width: var(--mr-control-min);
  }

  /* Create release wizard (full-screen modal) */
  html.ui-v2 .modal-backdrop:not(.hidden) {
    align-items: stretch !important;
    padding: 0 !important;
  }

  html.ui-v2 .modal-backdrop .modal,
  html.ui-v2 #modal {
    width: 100% !important;
    max-width: 100% !important;
    min-height: 100dvh !important;
    height: 100dvh !important;
    border-radius: 0 !important;
    display: flex !important;
    flex-direction: column !important;
    margin: 0 !important;
  }

  html.ui-v2 #modal .modal-head,
  html.ui-v2 .modal .modal-head {
    position: sticky;
    top: 0;
    z-index: 2;
    flex: 0 0 auto;
    padding-top: calc(12px + env(safe-area-inset-top, 0px));
    background: #fff;
  }

  html.ui-v2 #modal-body,
  html.ui-v2 .modal-body {
    flex: 1 1 auto;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 8px;
  }

  html.ui-v2 .mr-wizard-footer {
    position: sticky;
    bottom: 0;
    z-index: 2;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    padding: 12px 16px calc(12px + env(safe-area-inset-bottom, 0px));
    background: #fff;
    border-top: 1px solid rgba(15, 23, 42, 0.08);
  }

  html.ui-v2 .mr-wizard-step[hidden] {
    display: none !important;
  }

  html.ui-v2 .mr-wizard-progress {
    display: flex;
    gap: 6px;
    margin: 0 0 14px;
  }

  html.ui-v2 .mr-wizard-progress span {
    flex: 1;
    height: 4px;
    border-radius: 999px;
    background: #e5e7eb;
  }

  html.ui-v2 .mr-wizard-progress span.active {
    background: #f96302;
  }

  /* Editor: one column, drawers, sticky bottom */
  html.ui-v2 .editor-layout {
    grid-template-columns: minmax(0, 1fr) !important;
    display: flex !important;
    flex-direction: column !important;
  }

  html.ui-v2 .project-rail,
  html.ui-v2 .editor-layout > .editor-context-panel,
  html.ui-v2 .editor-readiness-panel:not(.drawer-open):not(.open) {
    display: none !important;
  }

  html.ui-v2 .editor-mobile-stepper {
    display: flex !important;
    gap: 8px;
    overflow-x: auto;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
    padding: 4px 0 12px;
  }

  html.ui-v2 .editor-mobile-stepper > * {
    scroll-snap-align: start;
    flex: 0 0 auto;
  }

  html.ui-v2 .editor-bottom-bar.sticky-action-bar {
    position: sticky;
    bottom: 0;
    z-index: 30;
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    background: #fff;
  }

  html.ui-v2 body.mr-overlay-open .editor-bottom-bar.sticky-action-bar,
  html.ui-v2 body.drawer-scroll-lock .editor-bottom-bar.sticky-action-bar {
    pointer-events: none;
    opacity: 0.35;
  }

  html.ui-v2 .dropzone,
  html.ui-v2 .artwork-drop,
  html.ui-v2 .cover-upload-zone {
    flex-direction: column !important;
    align-items: stretch !important;
  }

  /* Editor / upload previews only — never inflate catalog list covers. */
  html.ui-v2 .artwork-preview,
  html.ui-v2 .editor-cover-preview,
  html.ui-v2 .cover-upload-zone .cover-thumb-lg,
  html.ui-v2 .artwork-drop .cover-thumb-lg {
    width: clamp(112px, 36vw, 140px) !important;
    height: clamp(112px, 36vw, 140px) !important;
    max-width: 140px;
  }

  html.ui-v2 .catalog-list-cover,
  html.ui-v2 .catalog-list-cover .cover-thumb,
  html.ui-v2 .catalog-list-cover .cover-thumb-lg,
  html.ui-v2 .catalog-list-cover .catalog-cover,
  html.ui-v2 .catalog-list-cover .catalog-cover-art {
    width: var(--cover-list, 76px) !important;
    height: var(--cover-list, 76px) !important;
    max-width: 100% !important;
    max-height: 100% !important;
  }

  /* Drawers shared — single z stack above header (60) and bottom bar (30) */
  html.ui-v2 .track-rail.drawer-open,
  html.ui-v2 #editor-tracks-drawer.drawer-open,
  html.ui-v2 #editor-readiness-drawer.open,
  html.ui-v2 .editor-readiness-drawer.open,
  html.ui-v2 .staff-tools-sheet:not(.hidden),
  html.ui-v2 .moderation-queue.drawer-open,
  html.ui-v2 .moderation-side.drawer-open,
  html.ui-v2 .catalog-filters-panel.open,
  html.ui-v2 .catalog-menu-panel.open {
    position: fixed !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    top: auto !important;
    max-height: min(62dvh, calc(100dvh - var(--mr-header) - env(safe-area-inset-top, 0px) - 12px)) !important;
    border-radius: 16px 16px 0 0 !important;
    z-index: 70 !important;
    overflow: hidden;
    display: flex !important;
    flex-direction: column !important;
    padding-bottom: env(safe-area-inset-bottom, 0px) !important;
    background: #fff !important;
    pointer-events: auto !important;
    visibility: visible !important;
    opacity: 1 !important;
    height: auto !important;
    transform: translate3d(0, 0, 0) !important;
  }

  /* Closed readiness must fully disappear (not nudge ~72px from ui-v3 inset). */
  html.ui-v2 .editor-readiness-drawer:not(.open),
  html.ui-v2 #editor-readiness-drawer:not(.open) {
    display: none !important;
    visibility: hidden !important;
    pointer-events: none !important;
    opacity: 0 !important;
    transform: translate3d(0, 110%, 0) !important;
    max-height: 0 !important;
    height: 0 !important;
    overflow: hidden !important;
    bottom: 0 !important;
    inset: auto 0 0 0 !important;
  }

  html.ui-v2 .editor-page .track-rail:not(.drawer-open),
  html.ui-v2 .editor-page #editor-tracks-drawer:not(.drawer-open) {
    display: none !important;
  }

  html.ui-v2 .editor-readiness-drawer.open > header,
  html.ui-v2 #editor-readiness-drawer.open > header,
  html.ui-v2 .track-rail.drawer-open .track-rail-head,
  html.ui-v2 .staff-tools-sheet:not(.hidden) > header {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    position: relative;
    z-index: 2;
  }

  html.ui-v2 .editor-readiness-drawer.open > header .icon-button,
  html.ui-v2 #editor-readiness-drawer.open > header .icon-button,
  html.ui-v2 .track-rail.drawer-open .track-rail-head .icon-button,
  html.ui-v2 .staff-tools-sheet:not(.hidden) > header .icon-button,
  html.ui-v2 .moderation-queue.drawer-open .pane-head .icon-button,
  html.ui-v2 .moderation-side.drawer-open .moderation-side-drawer-head .icon-button {
    min-width: var(--mr-control-min);
    min-height: var(--mr-control-min);
    position: relative;
    z-index: 3;
    pointer-events: auto !important;
  }

  html.ui-v2 .editor-readiness-drawer.open .drawer-body,
  html.ui-v2 .editor-readiness-drawer.open .readiness-drawer-body,
  html.ui-v2 #editor-readiness-drawer.open .drawer-body,
  html.ui-v2 #editor-readiness-drawer.open .readiness-drawer-body {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    max-height: none !important;
  }

  html.ui-v2 .mr-drawer-scroll {
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    flex: 1 1 auto;
    min-height: 0;
  }

  html.ui-v2 .mr-drawer-handle {
    width: 36px;
    height: 4px;
    border-radius: 999px;
    background: #d1d5db;
    margin: 10px auto 6px;
    flex: 0 0 auto;
  }

  html.ui-v2 .editor-mobile-drawer-backdrop:not(.hidden),
  html.ui-v2 .moderation-drawer-backdrop:not(.hidden),
  html.ui-v2 #catalog-filters-backdrop:not(.hidden),
  html.ui-v2 body.catalog-filters-open #catalog-filters-backdrop {
    display: block !important;
    position: fixed !important;
    inset: 0 !important;
    z-index: 65 !important;
    background: rgba(20, 24, 28, 0.4);
    pointer-events: auto !important;
  }

  /* M2 catalog: reinforce scrollport if workspace clips */
  html.ui-v2 .m2-catalog-panel,
  html.ui-v2 .m2-catalog-filters,
  html.ui-v2 .m2-filter-group,
  html.ui-v2 .m2-filter-chips {
    min-width: 0 !important;
    max-width: 100% !important;
  }

  html.ui-v2 .m2-filter-chips {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-x;
  }

  html.ui-v2 .m2-page-header {
    flex-direction: column !important;
    align-items: stretch !important;
  }

  html.ui-v2 .m2-page-header .page-actions {
    width: 100% !important;
    display: grid !important;
    grid-template-columns: 1fr !important;
    gap: 8px !important;
  }

  /* Documents / agreements */
  html.ui-v2 .agreement-actions,
  html.ui-v2 .agreement-terms-compact {
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 8px !important;
    min-width: 0;
    max-width: 100%;
  }

  html.ui-v2 .agreement-actions .button {
    flex: 1 1 auto;
    min-width: min(100%, 140px);
  }

  html.ui-v2 .document-grid,
  html.ui-v2 .docs-layout {
    display: flex !important;
    flex-direction: column !important;
    gap: 12px !important;
  }

  html.ui-v2 .table-scroll {
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Moderation sticky actions above bottom nav, not fighting it */
  html.ui-v2 .moderation-actions.sticky-action-bar {
    bottom: calc(var(--mr-bottom-nav) + env(safe-area-inset-bottom, 0px)) !important;
    z-index: 40;
  }

  html.ui-v2 .moderation-queue.drawer-open,
  html.ui-v2 .moderation-side.drawer-open {
    bottom: 0 !important;
    z-index: 70 !important;
  }

  /* Settings / partners / ops tabs: horizontal scroll, no wrap clip */
  html.ui-v2 .settings-tabs,
  html.ui-v2 .partners-subnav,
  html.ui-v2 .operations-subnav,
  html.ui-v2 .docs-tabs,
  html.ui-v2 .page-shell > .tabs,
  html.ui-v2 .tabs:has([data-docs-tab]) {
    flex-wrap: nowrap !important;
  }

  html.ui-v2 .settings-tabs,
  html.ui-v2 .partners-subnav,
  html.ui-v2 .operations-subnav,
  html.ui-v2 .page-shell > .tabs,
  html.ui-v2 .tabs:has([data-docs-tab]) {
    display: flex !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-x;
    scrollbar-width: none;
    max-width: 100%;
    gap: 8px;
  }

  html.ui-v2 .settings-tabs::-webkit-scrollbar,
  html.ui-v2 .partners-subnav::-webkit-scrollbar,
  html.ui-v2 .operations-subnav::-webkit-scrollbar,
  html.ui-v2 .page-shell > .tabs::-webkit-scrollbar,
  html.ui-v2 .tabs:has([data-docs-tab])::-webkit-scrollbar {
    display: none;
  }

  html.ui-v2 .page-shell > .tabs .tab-button,
  html.ui-v2 .tabs:has([data-docs-tab]) .tab-button {
    flex: 0 0 auto;
    white-space: nowrap;
  }

  /* Keyboard: keep sticky bars above visual viewport when possible */
  html.ui-v2.mr-keyboard-open .editor-bottom-bar.sticky-action-bar,
  html.ui-v2.mr-keyboard-open .mr-wizard-footer {
    bottom: env(keyboard-inset-height, 0px);
  }
}

/* Tablet: keep single column for partners/moderation, allow denser type */
@media (min-width: 768px) and (max-width: 1023px) {
  html.ui-v2 .two-pane.partners-desk,
  html.ui-v2 .moderation-desk {
    grid-template-columns: 1fr !important;
  }

  html.ui-v2 .workspace {
    padding-left: 20px;
    padding-right: 20px;
  }

  html.ui-v2 .m2-page-header .page-actions {
    grid-template-columns: 1fr 1fr !important;
  }

  html.ui-v2 .m2-page-header .page-actions .m2-count-chip {
    grid-column: 1 / -1;
    width: auto !important;
    justify-self: start;
  }

  html.ui-v2 .m2-filter-chips {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-x;
  }

  html.ui-v2 .agreement-terms-compact {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }
}

@media (max-width: 767px) {
  html.ui-v2 .catalog-agreement-center .agreement-identity {
    grid-template-columns: 1fr !important;
  }

  html.ui-v2 .agreement-terms-compact {
    grid-template-columns: 1fr !important;
  }

  html.ui-v2 .agreement-term-pill-wide {
    grid-column: auto !important;
  }

  html.ui-v2 .page-header {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 12px !important;
  }

  html.ui-v2 .page-header .page-actions {
    width: 100%;
    flex-wrap: wrap;
    gap: 8px;
  }
}

html.ui-v2 .mr-mod-menu {
  display: none;
  position: absolute;
  right: 0;
  bottom: calc(100% + 8px);
  min-width: 200px;
  padding: 8px;
  border-radius: 12px;
  background: #fff;
  border: 1px solid rgba(15, 23, 42, 0.1);
  box-shadow: 0 12px 32px rgba(15, 23, 42, 0.16);
  z-index: 45;
}

html.ui-v2 .mr-mod-menu:not(.hidden) {
  display: grid;
  gap: 6px;
}

html.ui-v2 .moderation-actions {
  position: relative;
}

html.ui-v2 .mr-mod-disabled-help {
  grid-column: 1 / -1;
  margin: 0;
}

.email-preview-frame.mobile {
  max-width: 390px;
  margin: 0 auto;
  min-height: 520px;
  width: 100%;
  border: 0;
  background: #fff;
}

.email-preview-frame.desktop {
  width: 100%;
  min-height: 640px;
  border: 0;
  background: #fff;
}
