/* App navigation is additive: desktop layout and all destinations stay intact. */
.hb-core-app-nav,
.hb-core-app-menu-links { display: none; }
.hb-core-app-footer-links { display: contents; }
.hb-core-app-footer-links > summary { display: none; }

/* Removed legacy modules can leave empty Theme Builder sections with negative
 * row margins. Those empty columns pull the whole document above the viewport.
 * Only collapse structural shells beside our native navigation; authored
 * modules and semantic content (including custom headers) remain untouched. */
body.miitbook-page:not(.et-fb) .et-l--header:has(.hb-core-public-bar, .hb-core-action-bar) .et_pb_section:has(.et_pb_row):not(:has(:not(.et_pb_row, .et_pb_column))) {
  display: none !important;
}

/* Saved Divi navigation wrappers include a -44px text-module offset as well
 * as row spacing. Native bars supply their own spacing. Reset only their
 * ancestors, including the marker for a redundant onboarding menu, so that
 * empty wrappers collapse naturally and authored siblings remain visible. */
body.miitbook-page:not(.et-fb) .et-l--header :is(.et_pb_section, .et_pb_row, .et_pb_column, .et_pb_text, .et_pb_text_inner, .et_pb_code, .et_pb_code_inner):has(.hb-core-public-bar, .hb-core-action-bar, .hb-core-role-menu, .hb-core-role-menu-placeholder) {
  width: 100% !important;
  max-width: none !important;
  margin: 0 !important;
  padding: 0 !important;
  min-height: 0;
}

/* The saved homepage catalogue section reserves 1725px even when its native
 * cards are shorter. Let this exact section follow its content on every screen. */
body.miitbook-page:not(.et-fb) .et_pb_section.hb-home-page.hb-divi-layout-section-professional_book_showcase:has(.hb-divi-professional-books) {
  min-height: 0 !important;
}

/* Logical full-width alignment also works after a language switch to RTL. */
body.miitbook-page :is(.hb-core-public-bar, .hb-core-action-bar, .hb-core-role-menu) {
  margin-left: 0;
  margin-right: 0;
  margin-inline-start: calc(50% - 50vw);
}
body.miitbook-page :is(.hb-core-public-bar, .hb-core-action-bar) > * { min-width: 0; }
/* Native account details can open on desktop outside the mobile panel state.
 * Lift only their header branch above later Divi sections, without raising
 * the shared builder root (dialogs and app navigation keep their priority). */
body.miitbook-page:not(.et-fb) :is(.et_pb_section, .et_pb_row, .et_pb_column, .et_pb_module, .et_pb_text_inner, .et_pb_code_inner):has(:is(.hb-core-public-bar, .hb-core-action-bar) .hb-core-account-menu[open]),
body.miitbook-page:not(.et-fb) :is(.hb-core-public-bar, .hb-core-action-bar):has(.hb-core-account-menu[open]) {
  z-index: 90 !important;
  overflow: visible !important;
}
/* The inline language dropdown must escape the same Divi stacking contexts
 * as the account menu; its own z-index cannot rise above sibling header rows. */
body.miitbook-page:not(.et-fb) :is(.et_pb_section, .et_pb_row, .et_pb_column, .et_pb_module, .et_pb_text_inner, .et_pb_code_inner):has(:is(.hb-core-public-bar, .hb-core-action-bar) .gtranslate_wrapper[data-hb-language-inline] .gt_options.gt-open),
body.miitbook-page:not(.et-fb) :is(.hb-core-public-bar, .hb-core-action-bar):has(.gtranslate_wrapper[data-hb-language-inline] .gt_options.gt-open) {
  z-index: 90 !important;
  overflow: visible !important;
}
body.miitbook-page .hb-core-brand small { padding-left: 0; padding-inline-start: 58px; font-size: 12px; }
body.miitbook-page #miitbook-footer ul,
body.miitbook-page #miitbook-footer li { list-style: none !important; }
body.miitbook-page #miitbook-footer ul { padding-inline-start: 0; }
body.miitbook-page #miitbook-footer :is(small, .mb-footer-copy) { font-size: 12px; }
body.miitbook-page .gtranslate_wrapper[data-hb-language-inline] {
  position: relative !important;
  inset: auto !important;
  z-index: 92 !important;
  flex: 0 0 auto;
}
body.miitbook-page .gtranslate_wrapper[data-hb-language-inline] #gt_float_wrapper {
  position: relative !important;
  inset: auto !important;
  z-index: auto !important;
}
body.miitbook-page .gtranslate_wrapper[data-hb-language-inline] .gt_float_switcher {
  /* Remove the inline baseline gap while still containing .gt-selected's float. */
  display: flow-root;
  overflow: visible;
  font-size: 12px !important;
  border: 1px solid var(--miitbook-line-strong, #c9b99f);
  border-radius: 10px;
  box-shadow: none;
}
body.miitbook-page #gt_float_wrapper .gt_float_switcher img { display: none !important; }
body.miitbook-page #gt_float_wrapper .gt-selected .gt-current-lang { min-height: 44px; padding: 10px; }
body.miitbook-page #gt_float_wrapper .gt-lang-code { text-transform: none; }
body.miitbook-page #gt_float_wrapper :is(.gt-selected, .gt_options a):focus-visible {
  outline: 3px solid var(--miitbook-focus, #082568);
  outline-offset: -3px;
}
body.miitbook-page .gtranslate_wrapper[data-hb-language-inline] .gt_options {
  position: absolute;
  inset-block-start: 100%;
  inset-inline-end: 0;
  min-width: 150px;
  width: max-content;
  max-width: min(300px, calc(100vw - 28px));
  max-height: min(300px, 60vh);
  /* The provider's saved right-opening mode uses one clipped horizontal row.
   * Our header places this panel below the control, so keep its links vertical. */
  float: none;
  white-space: normal;
  overflow-x: hidden;
  overflow-y: auto;
  background: var(--miitbook-surface, #fffdfa);
  border: 1px solid var(--miitbook-line-strong, #c9b99f);
  box-shadow: 0 8px 24px rgba(8, 37, 104, .12);
}
body.miitbook-page .gtranslate_wrapper[data-hb-language-inline] .gt_options a:not(.gt-current) {
  display: block;
  min-height: 44px;
  overflow-wrap: anywhere;
}
@media (max-width: 1100px) {
  body.miitbook-page :is(.hb-core-public-bar, .hb-core-action-bar):has([data-hb-language-inline]) {
    grid-template-columns: minmax(0, 1fr) auto auto;
  }
}

/* Decide the initial menu layout before deferred Core JS runs. Waiting for
 * .hb-menu-ready first painted every link in flow, then collapsed the header.
 * Browsers without this media feature retain the existing JS enhancement. */
@media (max-width: 1100px) and (scripting: enabled) {
  body.miitbook-page:not(.et-fb) :is(.hb-core-mobile-panel, .hb-core-role-links):not(.is-open) {
    display: none;
  }
}
@media (max-width: 1100px) and (scripting: none) {
  body.miitbook-page:not(.et-fb) .hb-core-menu-toggle { display: none; }
}

@media (min-width: 768px) {
  .hb-core-app-panel,
  .hb-core-app-panel-content { display: contents; }
  .hb-core-app-panel::details-content { display: contents; }
  .hb-core-app-panel > summary { display: none; }
}

@media (max-width: 767px) {
  body.miitbook-page {
    --hb-app-nav-height: calc(68px + env(safe-area-inset-bottom, 0px));
    padding-bottom: var(--hb-app-nav-height) !important;
  }
  html:has(body.miitbook-page) { scroll-padding-block: 72px 90px; }
  body.miitbook-page .et-l--header {
    position: sticky;
    top: 0;
    z-index: 90;
  }
  /* Some pages embed the header in the post instead of the Theme Builder.
   * Lift its Divi ancestors above sibling content while the menu is open:
   * the panel's own z-index cannot escape the column's stacking context.
   * Keep the shared builder root unchanged so app tabs/dialogs retain priority. */
  body.miitbook-page:not(.et-fb) :is(.et_pb_section, .et_pb_row, .et_pb_column, .et_pb_module):has(.hb-core-mobile-panel.is-open) {
    z-index: 90 !important;
    overflow: visible !important;
  }
  body.miitbook-page :is(.hb-core-public-bar, .hb-core-action-bar) {
    width: 100%;
    margin: 0;
    padding: 6px 14px !important;
    /* Reserve the language control's 44px target + 2px border, the bar's
     * 12px padding and 1px border before the asynchronous widget arrives. */
    min-height: 59px;
    gap: 0 12px !important;
    border-bottom: 1px solid var(--miitbook-line, #eadfce);
    background: var(--miitbook-surface, #fffdfa);
  }
  body.miitbook-page .hb-core-brand img { height: 30px; max-width: 145px; }
  body.miitbook-page .hb-core-brand small { display: none; }
  body.miitbook-page .hb-core-menu-toggle {
    width: 44px;
    height: 44px;
    min-height: 44px;
    padding: 10px;
    border-color: transparent;
    border-radius: 12px;
    background: transparent;
  }
  body.miitbook-page .hb-core-menu-label {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
  }
  body.miitbook-page .hb-core-mobile-panel {
    position: absolute;
    inset: 100% 0 auto;
    z-index: 91;
    max-height: calc(100dvh - 56px - var(--hb-app-nav-height));
    overflow-y: auto;
    overscroll-behavior: contain;
    box-sizing: border-box;
    padding: 12px 14px 18px;
    border: 0;
    border-bottom: 1px solid var(--miitbook-line, #eadfce);
    border-radius: 0 0 18px 18px;
    background: var(--miitbook-surface, #fffdfa);
    box-shadow: 0 16px 24px rgba(8, 37, 104, .12);
  }
  /* The menu's full-width link rules must not stretch the profile photo.
   * Reuse its existing localized accessible label for the visible link text. */
  body.miitbook-page :is(.hb-core-public-bar, .hb-core-action-bar) .hb-core-mobile-panel a.hb-core-avatar[aria-label] {
    display: flex;
    color: var(--miitbook-ink, #082568);
    align-items: center;
    gap: 10px;
    width: 100%;
    box-sizing: border-box;
    height: auto;
    min-height: 52px;
    padding: 8px 12px;
    overflow: visible;
    border-radius: 10px;
  }
  body.miitbook-page :is(.hb-core-public-bar, .hb-core-action-bar) .hb-core-mobile-panel a.hb-core-avatar[aria-label] > img {
    display: block;
    flex: 0 0 36px;
    width: 36px;
    height: 36px;
    min-width: 36px;
    max-width: 36px;
    min-height: 36px;
    max-height: 36px;
    object-fit: cover;
    border-radius: 50%;
  }
  body.miitbook-page :is(.hb-core-public-bar, .hb-core-action-bar) .hb-core-mobile-panel a.hb-core-avatar[aria-label]::after {
    content: attr(aria-label);
    flex: 1 1 auto;
    min-width: 0;
    font-size: 14px;
    line-height: 1.4;
    white-space: normal;
    text-align: start;
  }
  /* With JS disabled, menus remain in flow and fully usable. */
  html:not(.hb-menu-ready) .hb-core-mobile-panel { position: static; max-height: none; box-shadow: none; }
  body.miitbook-page:has(.hb-core-action-bar) .hb-core-role-menu { display: none !important; }
  body.miitbook-page .hb-core-app-menu-links {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px;
    padding-top: 12px;
    margin-top: 2px;
    border-top: 1px solid var(--miitbook-line, #eadfce);
  }
  body.miitbook-page .hb-core-app-menu-links a {
    display: flex;
    align-items: center;
    min-height: 44px;
    padding: 9px 10px;
    border-radius: 10px;
    color: var(--miitbook-ink, #082568);
    font-size: 13px;
    line-height: 1.35;
    font-weight: 600;
    text-decoration: none;
    white-space: normal;
  }
  body.miitbook-page .hb-core-app-nav {
    position: fixed;
    inset: auto 0 0;
    z-index: 80;
    display: flex;
    align-items: stretch;
    height: var(--hb-app-nav-height);
    box-sizing: border-box;
    padding: 6px max(8px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
    border-top: 1px solid var(--miitbook-line, #eadfce);
    background: var(--miitbook-surface, #fffdfa);
    box-shadow: 0 -4px 20px rgba(8, 37, 104, .04);
    font-family: var(--miitbook-font, "DM Sans", system-ui, sans-serif);
  }
  body.miitbook-page .hb-core-app-nav a {
    display: flex;
    flex: 1 1 0;
    margin: 0;
    min-width: 0;
    min-height: 48px;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    gap: 4px;
    padding: 4px 2px;
    border-radius: 12px;
    color: var(--miitbook-muted, #526486);
    font-size: 12px;
    font-weight: 600;
    line-height: 1.2;
    text-align: center;
    text-decoration: none;
  }
  body.miitbook-page .hb-core-app-nav svg {
    display: block;
    width: 22px;
    height: 22px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.7;
    stroke-linecap: round;
    stroke-linejoin: round;
  }
  body.miitbook-page .hb-core-app-nav a.is-active {
    color: var(--miitbook-accent, #c63800);
    background: var(--miitbook-soft, #f5ede0);
    font-weight: 800;
  }
  .hb-core-app-nav a:focus-visible,
  .hb-core-app-footer-links > summary:focus-visible {
    outline: 2px solid var(--miitbook-focus, #082568);
    outline-offset: -2px;
  }
  /* Replace the old floating homepage shortcut with the permanent tabs. */
  body.miitbook-page .hb-mobile-search-cta { display: none !important; }
  body.miitbook-page #gt_float_wrapper {
    bottom: calc(var(--hb-app-nav-height) + 8px) !important;
    left: auto !important;
    right: 10px !important;
    z-index: 70 !important;
  }
  body.miitbook-page .gt_float_switcher { font-size: 12px !important; border-radius: 10px; }
  body.miitbook-page .gt_float_switcher img { width: 22px !important; }
  body.miitbook-page #hb-bookchat { --hchat-viewport-height: calc(100dvh - var(--hb-app-nav-height)); }
  body.miitbook-page #miitbook-footer { margin-top: 24px; }
  body.miitbook-page .hb-core-app-footer-links { display: block; padding: 0 16px; }
  body.miitbook-page .hb-core-app-footer-links > summary {
    display: flex;
    min-height: 48px;
    align-items: center;
    justify-content: space-between;
    padding: 12px 0;
    color: var(--miitbook-ink, #082568);
    font-size: 14px;
    font-weight: 650;
    cursor: pointer;
    list-style: none;
  }
  body.miitbook-page .hb-core-app-footer-links > summary::after { content: "+"; font-size: 20px; }
  body.miitbook-page .hb-core-app-footer-links[open] > summary::after { content: "−"; }
  body.miitbook-page .hb-core-app-footer-links > summary::-webkit-details-marker { display: none; }
  body.miitbook-page #miitbook-footer .mb-footer-main { padding: 16px 0; gap: 20px; }
  body.miitbook-page #miitbook-footer .mb-footer-bottom-inner { padding: 12px 16px; gap: 8px; }
  body.miitbook-page #miitbook-footer .mb-footer-legal { gap: 2px 12px; }
  body.miitbook-page #miitbook-footer .mb-footer-legal a { font-size: 12px; }

  body.miitbook-page .hb-core-app-panel {
    width: 100%;
    min-width: 0;
    border: 1px solid var(--miitbook-line, #eadfce);
    border-radius: 14px;
    background: var(--miitbook-surface, #fffdfa);
  }
  body.miitbook-page .hb-core-app-panel > summary {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    min-height: 52px;
    padding: 14px;
    color: var(--miitbook-ink, #082568);
    font-size: 15px;
    font-weight: 700;
    line-height: 1.4;
    list-style: none;
    cursor: pointer;
  }
  body.miitbook-page .hb-core-app-panel > summary::after { content: "+"; flex: 0 0 auto; font-size: 20px; }
  body.miitbook-page .hb-core-app-panel[open] > summary::after { content: "−"; }
  body.miitbook-page .hb-core-app-panel > summary::-webkit-details-marker { display: none; }
  body.miitbook-page .hb-core-app-panel > summary:focus-visible {
    outline: 2px solid var(--miitbook-focus, #082568);
    outline-offset: 2px;
    border-radius: 12px;
  }
  body.miitbook-page .hb-core-app-panel-content { min-width: 0; padding: 0 10px 10px; }
}

/* Native locale links share a single touch-friendly row inside the mobile menu. */
@media (max-width: 767px) {
  body.miitbook-page header.hb-core-public-bar .hb-core-language-links {
    /* Legacy mobile_menu_css() emits display:grid and gap as !important on
     * every direct nav child. Override only this native locale selector. */
    display: flex !important;
    flex-flow: row nowrap;
    align-items: stretch;
    gap: 6px !important;
    width: 100%;
    min-width: 0;
  }
  body.miitbook-page header.hb-core-public-bar .hb-core-language-links > a {
    display: flex;
    flex: 1 1 0;
    align-items: center;
    justify-content: center;
    width: auto;
    min-width: 0;
    min-height: 44px;
    padding: 8px;
    border: 1px solid var(--miitbook-line, #eadfce);
    border-radius: 10px;
    white-space: nowrap !important;
  }
  body.miitbook-page header.hb-core-public-bar .hb-core-language-links > [aria-hidden="true"] { display: none; }
  /* Arabic multi-line hero actions need room for their glyphs and diacritics. */
  html[lang="ar"][dir="rtl"] body.miitbook-page .hb-divi-hero .hb-divi-actions .hb-divi-button {
    line-height: 1.4;
    min-height: 44px;
  }
}
