/* The existing menu stays in its original shape. JS moves that same node into
   the active dialog's top layer; only the space around the task changes here. */
html.main-menu-ready {
  --main-menu-left: 0px;
  --main-menu-bottom: var(--menu-bottom, calc(68px + env(safe-area-inset-bottom)));
  --main-menu-available-width: calc(100dvw - var(--main-menu-left));
  --main-menu-available-height: calc(100dvh - var(--main-menu-bottom));
}

/* These defaults also work before the first ResizeObserver measurement. */
@media (min-width: 761px) {
  html.main-menu-ready {
    --main-menu-left: 84px;
    --main-menu-bottom: 0px;
  }
}

html.main-menu-ready .tabbar {
  z-index: 14000;
}
.main-menu-message {
  position: absolute;
  inset: auto 12px calc(100% + 8px);
  margin: 0;
  padding: 12px 16px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  color: var(--color-text);
  background: var(--color-surface);
  font: 0.875rem/1.5 var(--font);
  white-space: normal;
}
@media (min-width: 761px) {
  .main-menu-message {
    position: fixed;
    inset: 16px auto auto calc(var(--main-menu-left) + 12px);
    width: min(340px, calc(100dvw - 116px));
  }
}

/* Reset only the popover UA box. Link visibility, columns, outline/filled icons,
   selected states, the account and theme colours keep their incumbent rules. */
html.main-menu-ready .tabbar.main-menu-portal {
  position: fixed;
  margin: 0;
  max-width: none;
  max-height: none;
  box-sizing: border-box;
  border: 0;
  overflow: visible;
  font-family: var(--font);
  font-size: 1rem;
  font-weight: 400;
  font-style: normal;
  line-height: 1.5;
  letter-spacing: normal;
  text-align: start;
  text-transform: none;
  color: var(--color-text);
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transform: none;
  translate: none;
  scale: none;
  filter: none;
  animation: none;
}
html.main-menu-ready .tabbar.main-menu-portal::backdrop {
  background: transparent;
  backdrop-filter: none;
  pointer-events: none;
}

@media (max-width: 760px) {
  html.main-menu-ready .tabbar.main-menu-portal {
    inset: auto 0 0;
    width: 100%;
    height: calc(60px + env(safe-area-inset-bottom));
    padding: 0 4px env(safe-area-inset-bottom);
    border-top: 1px solid var(--color-border);
    background: var(--color-bg);
  }
  html.main-menu-ready[data-look='thread'] .tabbar.main-menu-portal {
    height: calc(68px + env(safe-area-inset-bottom));
    border-top: 0;
  }
}
@media (min-width: 761px) {
  html.main-menu-ready .tabbar.main-menu-portal {
    inset: 0 auto 0 0;
    width: 84px;
    height: 100dvh;
    padding: 14px 8px 16px;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-width: thin;
    scrollbar-color: var(--color-border) var(--color-bg);
    background: var(--color-bg);
  }
}

/* Native small dialogs keep their existing width caps. Auto margins centre them
   inside the usable viewport, including to the right of the desktop rail. */
html.main-menu-ready dialog.with-main-menu {
  --main-menu-dialog-gap: 12px;
  position: fixed;
  inset: var(--main-menu-dialog-gap) 0 calc(var(--main-menu-bottom) + var(--main-menu-dialog-gap))
    var(--main-menu-left);
  height: fit-content;
  max-width: calc(var(--main-menu-available-width) - 2 * var(--main-menu-dialog-gap));
  max-height: calc(var(--main-menu-available-height) - 2 * var(--main-menu-dialog-gap));
  margin: auto;
  box-sizing: border-box;
}
html.main-menu-ready dialog.with-main-menu.contribution-authoring {
  --main-menu-dialog-gap: 16px;
  height: min(860px, calc(var(--main-menu-available-height) - 32px));
}
html.main-menu-ready dialog.with-main-menu.contribution-authoring-compact {
  height: fit-content;
}
html.main-menu-ready dialog.with-main-menu.photo-editor.cover-crop {
  height: min(820px, calc(var(--main-menu-available-height) - 32px));
}
html.main-menu-ready dialog.with-main-menu.guided-popup {
  max-height: min(
    calc(90dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom)),
    calc(var(--main-menu-available-height) - 24px)
  );
}
html.main-menu-ready dialog.with-main-menu.share-dialog {
  max-height: min(92dvh, calc(var(--main-menu-available-height) - 24px));
}

/* In the non-popover fallback a fixed menu is still a dialog descendant. A
   transformed dialog would turn it into a local fixed box and clip the rail. */
html.main-menu-ready.main-menu-fallback dialog.with-main-menu {
  transform: none;
  translate: none;
  scale: none;
  filter: none;
  perspective: none;
  contain: none;
  animation: none;
}

/* Photo viewing fills the usable area, rather than hiding any menu item. */
html.main-menu-ready dialog.with-main-menu:is(.media-fullscreen, .lightbox) {
  inset: 0 0 var(--main-menu-bottom) var(--main-menu-left);
  width: var(--main-menu-available-width);
  height: var(--main-menu-available-height);
  max-width: var(--main-menu-available-width);
  max-height: var(--main-menu-available-height);
  margin: 0;
}
html.main-menu-ready .with-main-menu.media-fullscreen .card-gallery {
  /* The menu owns the device's bottom safe area. */
  padding-bottom: 0;
}
html.main-menu-ready .with-main-menu.lightbox img {
  max-height: calc(var(--main-menu-available-height) - 90px);
}

/* The notification drawer keeps its familiar rail-side / bottom-sheet anchor. */
html.main-menu-ready dialog.with-main-menu.notes-pop {
  inset: 16px auto 16px calc(var(--main-menu-left) + 12px);
  width: min(400px, calc(var(--main-menu-available-width) - 28px));
  max-height: calc(var(--main-menu-available-height) - 32px);
  margin: 0;
}

/* This fixed page already reserves menu insets. Its old width:100% ignored the
   right inset and extended the page 84px past the desktop viewport. */
html.main-menu-ready .panel.full {
  width: auto;
}

@media (max-width: 760px) {
  html.main-menu-ready dialog.with-main-menu.contribution-authoring,
  html.main-menu-ready dialog.with-main-menu.photo-editor.cover-crop,
  html.main-menu-ready dialog.with-main-menu.eat-dialog {
    --main-menu-dialog-gap: 0px;
    inset: 0 0 var(--main-menu-bottom) 0;
    width: 100%;
    height: var(--main-menu-available-height);
    max-width: 100%;
    max-height: var(--main-menu-available-height);
    margin: 0;
  }
  html.main-menu-ready dialog.with-main-menu.contribution-authoring.contribution-authoring-compact {
    inset: 12px 0 calc(var(--main-menu-bottom) + 12px);
    width: calc(100% - 24px);
    height: fit-content;
    max-height: calc(var(--main-menu-available-height) - 24px);
    margin: auto;
  }
  html.main-menu-ready dialog.with-main-menu.notes-pop {
    inset: auto 0 var(--main-menu-bottom);
    width: 100%;
    max-width: 100%;
    max-height: min(80dvh, calc(var(--main-menu-available-height) - 12px));
  }
}
