/*
 * The native-app shell on phones (Dr. Marketo panel, super admin, auth).
 *
 * Linked from layouts/base after every other sheet except light.css.
 * EVERY rule is inside a max-width query (767px for phones, 1023px for the
 * tablet drawer fix), so a desktop screen (>=1024px) reads none of it.
 * Presentation only: no element is added or removed here, nothing posts.
 *
 * Hook classes (see mobile-shell-guide.md):
 *   .m-tabbar / .m-tab        bottom tab bar (partials/mobile-tabbar)
 *   .m-sheet-close            close button of the full-screen menu sheet
 *   .m-back                   back button injected into .app-header by JS
 *   .app-scroll               the shell's content scroller
 *   .m-card                   edge-to-edge rounded card on phones
 *   .m-sheet (+ .m-sheet-panel) any custom overlay → bottom sheet on phones
 *   .m-stack-table            table → stacked cards (cells need data-label)
 *   .table-responsive / .m-scroll-x   horizontal scroll wrapper
 *   .m-hide-phone / .m-only-phone     visibility helpers
 *   .m-full                   full-width on phones (buttons, inputs)
 *   .m-sticky-actions         action row pinned above the tab bar
 */

/* Hidden everywhere unless a phone query below turns it on. */
.m-tabbar,
.m-sheet-close,
.m-back,
.m-only-phone,
.m-install {
  display: none;
}

/* ===================================================== tablet + phone === */
@media (max-width: 1023px) {

  /*
   * The drawer always opens EXPANDED below the desktop breakpoint.
   * The collapse cookie defaults to collapsed, and app.css applies that
   * at every width, so the phone drawer used to open as a 64px icon rail.
   */
  html.sidebar-collapsed .app-sidebar { width: 16rem; }
  html.sidebar-collapsed .app-sidebar .sidebar-label { display: inline; }
  html.sidebar-collapsed .app-sidebar span.sidebar-label.flex-1 { display: block; }
  html.sidebar-collapsed .app-sidebar .sidebar-item {
    justify-content: flex-start;
    padding-left: .75rem;
    padding-right: .75rem;
    gap: .75rem;
  }
  html.sidebar-collapsed .app-sidebar .sidebar-expand-only { display: block; }
  html.sidebar-collapsed .app-sidebar .sidebar-collapse-only { display: none; }
  html.sidebar-collapsed .app-sidebar .sidebar-tooltip { display: inline-flex; }
  html .app-sidebar .sidebar-tooltip > span[role=tooltip] { display: none !important; }
}

/* ============================================================ phones ===== */
@media (max-width: 767px) {

  html, body {
    overflow-x: hidden;
    -webkit-tap-highlight-color: transparent;
    -webkit-text-size-adjust: 100%;
  }

  /* The shells are 100vh; on a phone that is taller than what is visible
     while the browser's own bars are showing. */
  html:has(.m-tabbar) body .h-screen { height: 100vh; height: 100dvh; }

  /* ------------------------------------------------------------ tab bar */
  .m-tabbar {
    display: flex;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 45;
    height: calc(58px + env(safe-area-inset-bottom, 0px));
    padding: 0 env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
    background: rgba(255, 255, 255, .96);
    border-top: 1px solid rgba(0, 0, 0, .08);
    -webkit-backdrop-filter: saturate(180%) blur(16px);
    backdrop-filter: saturate(180%) blur(16px);
    box-shadow: 0 -6px 24px rgba(0, 0, 0, .06);
  }
  html.dark .m-tabbar {
    background: rgba(9, 12, 24, .94);
    border-top-color: rgba(255, 255, 255, .10);
    box-shadow: 0 -6px 24px rgba(0, 0, 0, .45);
  }

  .m-tab {
    flex: 1 1 0;
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    padding: 6px 2px 4px;
    border: 0;
    background: none;
    color: #6b7280;
    font: 500 11px/1.1 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
    text-decoration: none;
    -webkit-tap-highlight-color: transparent;
    position: relative;
  }
  .m-tab:hover { color: inherit; }
  html.dark .m-tab { color: #a3acd2; }
  .m-tab .m-tab-icon { width: 24px; height: 24px; flex-shrink: 0; }
  .m-tab .m-tab-label {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .m-tab.is-active,
  .m-tab.is-active:hover { color: var(--primary-color, #4c6fff); }
  html.dark .m-tab.is-active { color: #ffc27a; }
  .m-tab.is-active::before {
    content: "";
    position: absolute;
    top: 0;
    left: 30%;
    right: 30%;
    height: 3px;
    border-radius: 0 0 3px 3px;
    background: currentColor;
  }
  .m-tab:active { transform: scale(.94); }

  /* Room for the tab bar under every page, and momentum scrolling. */
  .app-scroll {
    padding-bottom: calc(76px + env(safe-area-inset-bottom, 0px)) !important;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-y: contain;
    overflow-x: hidden !important;
  }
  /* Fixed things pages pin to the bottom (assistant bubble, quest dock,
     toasts) sit above the tab bar instead of under it. */
  html:has(.m-tabbar) .m-sticky-actions {
    position: sticky;
    /* Sticky insets inside .app-scroll count from its bottom padding
       (76px + safe area, the room kept for the tab bar), so -10px sits the
       bar 8px above the 58px tab bar. calc(66px + …) left it ~84px higher. */
    bottom: -10px;
    z-index: 5;
  }

  /* The old floating hamburger: the Menu tab replaces it. */
  html:has(.m-tabbar) button[data-sidebar-open]:not(.m-tab) { display: none !important; }

  /* ------------------------------------------------------- app top bar */
  html:has(.m-tabbar) .app-header {
    position: sticky;
    top: 0;
    z-index: 30;
    height: auto !important;
    min-height: 52px;
    padding: env(safe-area-inset-top, 0px) 8px 0 8px !important;
    gap: 4px;
  }
  /* Breadcrumbs never; the one-line page title always (it was sm:hidden). */
  html:has(.m-tabbar) .app-header > .hidden.sm\:block { display: none !important; }
  html:has(.m-tabbar) .app-header > .sm\:hidden {
    display: flex !important;
    flex: 1 1 auto;
    min-width: 0;
    padding-left: 4px !important;
    gap: 2px;
  }
  html:has(.m-tabbar) .app-header > .sm\:hidden > span {
    font-size: 17px !important;
    font-weight: 600 !important;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  /* Right-hand cluster: tight, never wider than it must be. */
  html:has(.m-tabbar) .app-header > .flex:last-child {
    flex-shrink: 0;
    gap: 2px;
  }
  html:has(.m-tabbar) .app-header > .flex:last-child > * { margin-left: 0 !important; margin-right: 0 !important; }
  /* Desktop-only header shortcuts: Refer & Earn lives in the Menu sheet. */
  html:has(.m-tabbar) .app-header a[href*="referral"] { display: none !important; }

  .m-back {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    margin-left: -6px;
    border: 0;
    border-radius: 999px;
    background: none;
    color: inherit;
    flex-shrink: 0;
  }
  .m-back:active { background: rgba(127, 127, 127, .18); }
  .m-back svg { width: 22px; height: 22px; }

  /* Banners above the page: compact. */
  html:has(.m-tabbar) .app-header ~ div:not(.app-scroll) { font-size: 13px; }

  /* ----------------------------------------- Menu: a full-screen sheet */
  html:has(.m-tabbar) aside.app-sidebar,
  html:has(.m-tabbar) .dk-root > aside.app-sidebar,
  html.sidebar-collapsed:has(.m-tabbar) aside.app-sidebar {
    width: 100vw !important;
    max-width: 100vw;
    height: 100vh;
    height: 100dvh;
    z-index: 60;
    padding-top: env(safe-area-inset-top, 0px);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    border-right: 0;
  }
  html:has(.m-tabbar) aside.app-sidebar nav {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding-bottom: 24px;
  }
  html:has(.m-tabbar) aside.app-sidebar .sidebar-item {
    min-height: 48px;
    font-size: 16px;
  }
  html:has(.m-tabbar) aside.app-sidebar .sidebar-item .sidebar-label { font-size: 16px; }

  html.sidebar-open .m-sheet-close {
    display: inline-flex;
    position: fixed;
    top: calc(14px + env(safe-area-inset-top, 0px));
    right: 14px;
    z-index: 61;
    width: 40px;
    height: 40px;
    align-items: center;
    justify-content: center;
    border-radius: 999px;
    border: 1px solid rgba(127, 127, 127, .3);
    background: rgba(127, 127, 127, .15);
    color: inherit;
  }
  html.dark.sidebar-open .m-sheet-close { color: #fff; }
  html.sidebar-open { overflow: hidden; }

  .m-install {
    display: none;
    width: calc(100% - 2rem);
    margin: 8px 1rem 0;
    min-height: 44px;
    border-radius: 12px;
    border: 1px dashed rgba(127, 127, 127, .45);
    background: none;
    color: inherit;
    font-size: 15px;
    font-weight: 600;
    align-items: center;
    justify-content: center;
    gap: 8px;
  }
  .m-install.is-ready { display: inline-flex; }

  /* ------------------------------------------------- generic page rules */
  /* Full-width content with 16px gutters. */
  .app-scroll > * { max-width: 100% !important; }
  .app-scroll .container,
  .app-scroll .max-w-7xl,
  .app-scroll .max-w-6xl,
  .app-scroll .max-w-5xl,
  .app-scroll .max-w-screen-xl,
  .app-scroll .max-w-screen-2xl {
    max-width: 100% !important;
    padding-left: 16px !important;
    padding-right: 16px !important;
  }
  .app-scroll img,
  .app-scroll video,
  .app-scroll iframe { max-width: 100%; }
  .app-scroll pre { white-space: pre-wrap; word-break: break-word; }

  /* Cards: edge-to-edge-ish, rounded. */
  .m-card,
  .app-scroll .dk-card {
    border-radius: 16px !important;
  }
  .m-card { margin-left: -4px; margin-right: -4px; }

  /* Every native table scrolls sideways inside its card instead of
     widening the page. The x-table component already wraps; this covers
     hand-written tables via their parent. */
  .table-responsive,
  .m-scroll-x {
    display: block;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .app-scroll :has(> table:not(.m-stack-table)) {
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .table-responsive > table,
  .m-scroll-x > table { min-width: 34rem; }

  /* Opt-in: a table that becomes one card per row. Put data-label="Name"
     on each <td> (mobile-app.js fills missing ones from the <th> row). */
  table.m-stack-table,
  table.m-stack-table tbody,
  table.m-stack-table tr,
  table.m-stack-table td { display: block; width: 100%; }
  table.m-stack-table thead { display: none; }
  table.m-stack-table tr {
    margin-bottom: 12px;
    border: 1px solid rgba(127, 127, 127, .25);
    border-radius: 14px;
    padding: 6px 12px;
  }
  table.m-stack-table td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 8px 0 !important;
    border: 0 !important;
    text-align: right;
    min-width: 0;
    word-break: break-word;
  }
  table.m-stack-table td + td { border-top: 1px solid rgba(127, 127, 127, .15) !important; }
  table.m-stack-table td[data-label]::before {
    content: attr(data-label);
    flex-shrink: 0;
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .04em;
    opacity: .6;
    text-align: left;
  }

  /* Touch targets and no iOS zoom-on-focus. */
  input:not([type=checkbox]):not([type=radio]):not([type=range]):not([type=color]):not([type=file]):not([type=hidden]),
  select,
  textarea {
    font-size: 16px !important;
  }
  input:not([type=checkbox]):not([type=radio]):not([type=range]):not([type=color]):not([type=file]):not([type=hidden]),
  select {
    min-height: 44px;
  }
  button[type=submit],
  .m-btn,
  button.ring-offset-background.whitespace-nowrap:not(.h-6):not(.h-7):not(.h-8):not(.h-9):not(.w-8):not(.w-9),
  a.ring-offset-background.whitespace-nowrap:not(.h-6):not(.h-7):not(.h-8):not(.h-9) {
    min-height: 44px;
  }
  .app-header button[type=submit] { min-height: 0; }
  .m-full { width: 100% !important; }

  /* ------------------------------------------ modals → bottom sheets */
  /* x-modal: [data-modal] > [data-modal-backdrop] > panel. */
  [data-modal-backdrop] {
    align-items: flex-end !important;
    justify-content: center !important;
    padding: 0 !important;
  }
  [data-modal-backdrop] > div,
  .m-sheet > .m-sheet-panel {
    width: 100% !important;
    max-width: 100% !important;
    max-height: 90vh !important;
    max-height: 90dvh !important;
    margin: 0 !important;
    border-radius: 20px 20px 0 0 !important;
    border-bottom: 0 !important;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    animation: m-sheet-up .26s cubic-bezier(.2, .8, .2, 1);
    position: relative;
  }
  [data-modal-backdrop] > div::before,
  .m-sheet > .m-sheet-panel::before {
    content: "";
    display: block;
    width: 40px;
    height: 5px;
    margin: 8px auto 0;
    border-radius: 3px;
    background: rgba(127, 127, 127, .45);
    flex-shrink: 0;
  }
  [data-modal-backdrop] > div .px-6 { padding-left: 16px !important; padding-right: 16px !important; }
  [data-modal-backdrop] > div .p-6 { padding: 16px !important; }

  .m-sheet {
    position: fixed !important;
    inset: 0;
    display: flex;
    align-items: flex-end !important;
    justify-content: center;
    padding: 0 !important;
  }

  /* Dropdown menus (x-dropdown) never overflow the screen sideways. */
  [data-dropdown] [data-dropdown-menu],
  [data-dropdown] > div[role=menu] {
    max-width: calc(100vw - 16px);
  }

  /* ------------------------------------------------------ visibility */
  .m-hide-phone { display: none !important; }
  .m-only-phone { display: revert; }

  /* ------------------------------------------------------- auth pages */
  body:not(:has(.m-tabbar)) .max-w-md { max-width: 100%; }
  body:not(:has(.m-tabbar)) form input:not([type=checkbox]):not([type=radio]):not([type=hidden]) { min-height: 48px; border-radius: 12px; }
  body:not(:has(.m-tabbar)) form button[type=submit] { min-height: 48px; width: 100%; border-radius: 12px; }
  body:not(:has(.m-tabbar)) h2.font-serif { font-size: 2rem; line-height: 1.15; }

  /* ---------------------------------------------- creator public pages */
  .cr.pub { padding: 16px 16px calc(32px + env(safe-area-inset-bottom, 0px)); }
  .cr.pub img { max-width: 100%; height: auto; }
}

@keyframes m-sheet-up {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
  [data-modal-backdrop] > div,
  .m-sheet > .m-sheet-panel { animation: none; }
}

/* Running as an installed app: no browser bars, so honour the notch. */
@media (display-mode: standalone) and (max-width: 767px) {
  body { overscroll-behavior-y: none; }
}

/* Account pages: stacked tables ignore their desktop inline min-width on phones. */
@media (max-width: 767px) {
  table.m-stack-table { min-width: 0 !important; }
  table.m-stack-table td[colspan] { text-align: center; }
}

/* .m-acct — account/settings/auth page roots. Phone-only layout pass:
   no sideways scroll, tighter gutters, single-column forms, full-width
   primary submit buttons, wrapping header rows. Desktop untouched. */
@media (max-width: 767px) {
  .m-acct { max-width: 100%; overflow-x: hidden; }
  .m-acct.p-6, .m-acct .p-6.flex-1 { padding: 12px !important; }
  .m-acct img, .m-acct video, .m-acct iframe { max-width: 100%; height: auto; }
  .m-acct pre, .m-acct code { white-space: pre-wrap; word-break: break-all; }
  .m-acct .break-all-phone, .m-acct td { overflow-wrap: anywhere; }
  .m-acct .flex.justify-between { flex-wrap: wrap; gap: 8px; }
  .m-acct [class*="max-w-"][class*="xl"] { max-width: 100%; }
  .m-acct [class*="min-w-["] { min-width: 0 !important; }
  /* Primary submit buttons: full width, centred, 44px. */
  .m-acct form button[type="submit"]:not([class*="h-6"]):not([class*="h-7"]):not([class*="h-8"]):not([name="action"]):not(table *) {
    width: 100%; min-height: 44px; justify-content: center;
  }
  .m-acct form .flex:has(> button[type="submit"]) { flex-wrap: wrap; }
  .m-acct .space-x-4 > button[type="submit"] { margin-left: 0 !important; }
  /* Stat rows of four collapse to two. */
  .m-acct .grid.grid-cols-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 480px) {
  .m-acct .m-name-row,
  .m-acct form .grid.grid-cols-2 { grid-template-columns: minmax(0, 1fr); }
}

/* ---------------------------------------- assistant bubble vs tab bar
   Phones with the tab bar: the floating Dr. Marketo button sits above the
   bar (58px + safe area + 12px) instead of covering the Menu tab, and higher
   still when the page pins its own action bar to the bottom (mc-dock,
   m-sticky-actions, the post wizard's .ps-foot). A dragged bubble
   (.cy-moved, positioned inline) is left where the user put it. */
@media (max-width: 767px) {
  html:has(.m-tabbar) .cy-root:not(.cy-moved) {
    bottom: calc(70px + env(safe-area-inset-bottom, 0px)) !important;
  }
  html:has(.m-tabbar):has(.mc-dock, .m-sticky-actions, .ps-screen:not([hidden]) .ps-foot, .inbox-composer-wrap, .gl-nav, .ads-m-wizard-nav, [data-w-foot], .ad-foot) .cy-root:not(.cy-moved):not(.cy-open) {
    bottom: calc(136px + env(safe-area-inset-bottom, 0px)) !important;
  }
  html:has(.m-tabbar) .cy-root.cy-open .cy-panel {
    max-height: calc(100dvh - 90px - env(safe-area-inset-bottom, 0px)) !important;
  }
  /* The Inbox reply box is ~150px tall and sits on the tab bar (58px + safe
     area), so the bubble goes above the whole box, not over Send. */
  html:has(.m-tabbar):has(.inbox-thread-pane .inbox-composer-wrap) .cy-root.cy-root:not(.cy-moved):not(.cy-open) {
    bottom: calc(224px + env(safe-area-inset-bottom, 0px)) !important;
  }
}

/* ---------------------------------- deck buttons and the story strip (W0-18)
   The Command Deck's buttons (deck.css, read-only here) are about 30px tall
   at .is-sm with 11.5px words — every Connect, "Fix it now — free" and
   "Unlock with a plan" on the scorecard, and the story strip's action. On a
   phone they become 44px targets with 14px words; the strip's actions share
   its row and wrap. Desktop untouched. */
@media (max-width: 767px) {
  .dk-btn,
  .dk-btn.is-sm {
    min-height: 44px;
    font-size: 14px;
    justify-content: center;
  }
  .dk-strip-side .dk-btn { flex: 1 1 auto; }

  /* A standalone text link that does a button's job ("Open →", "Change",
     "Compare every plan →"): a 44px tap target on a phone, same words and
     look. Opt-in — a link inside a sentence keeps the sentence's line. */
  .m-tap { display: inline-flex; align-items: center; min-height: 44px; }
}

/* ------------------------------------------- the header on phones (W2-07)
   SHL-05 / AB-30: the DM Credits, theme, bell and avatar buttons were 32px.
   Below 640px each gets a 44px tap area (the icons do not change), and the
   theme toggle moves into the account menu so three 44px buttons and the
   page title fit at 360px. AB-75: .m-hit44 gives any small control (a
   dismiss ✕, a filter chip, a tab) the same 44px on phones. */
@media (max-width: 639px) {
  html:has(.m-tabbar) .app-header .m-hit {
    min-width: 44px;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
  html:has(.m-tabbar) .app-header [data-theme-tip] { display: none !important; }
}
@media (max-width: 767px) {
  .m-hit44 {
    min-width: 44px;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
  /* W2-06: a menu row's plain sub-line wraps inside the Menu sheet. */
  html:has(.m-tabbar) aside.app-sidebar [data-menu-sub] { font-size: 12px; white-space: normal; }
}

/* W2-07 (ASST-03): no keyboard shortcut on a touch screen's assistant label. */
@media (hover: none) {
  #drm-assistant .cy-kbd { display: none; }
}
