html {
  overscroll-behavior: none;
}

body {
  --safe-area-inset-top: env(safe-area-inset-top, 0px);
  --safe-area-inset-bottom: env(safe-area-inset-bottom, 0px);
  --footer-height: calc((var(--block-space)) + 3.6em + var(--block-space));
  --navbar-height: calc(4.125em + var(--safe-area-inset-top));
  --sidebar-width: 0vw;
  --content-inline-offset-start: var(--sidebar-width);
  --content-inline-offset-end: 0vw;

  block-size: var(--app-height, 100dvh); /* fix height so grid and #main-content get definite size and main scrolls */
  display: grid;
  grid-template-areas:
    "sidebar nav"
    "sidebar main";
  grid-template-columns: var(--sidebar-width) 1fr;
  grid-template-rows: min-content 1fr;
  height: var(--app-height, 100dvh); /* Chrome/WebKit: force fixed height so grid row gets definite size (min-height alone lets body grow) */
  max-block-size: var(--app-height, 100dvh);
  max-height: var(--app-height, 100dvh);
  min-block-size: 0;
  min-height: 0; /* Chrome/WebKit: grid row must shrink so main scroll works */
  overflow: hidden; /* clip so only #main-content and #sidebar scroll, not the document */

  &.sidebar {
    @media (min-width: 67.5rem) {
      --sidebar-width: 26vw;
    }
  }

  &.sidebar.sidebar-collapsed {
    @media (min-width: 67.5rem) {
      --sidebar-width: 0vw;
    }
  }

  &.sidebar-right {
    --content-inline-offset-start: 0vw;
    --content-inline-offset-end: var(--sidebar-width);

    @media (min-width: 67.5rem) {
      grid-template-areas:
        "nav sidebar"
        "main sidebar";
      grid-template-columns: 1fr var(--sidebar-width);
    }
  }
}

/* Unauthenticated screens (login/signup) use document flow instead of
   the app-shell fixed grid to avoid iOS keyboard dead-space behavior. */
body.unauthenticated {
  block-size: auto;
  display: block;
  height: auto;
  max-block-size: none;
  max-height: none;
  min-block-size: var(--app-height, 100dvh);
  min-height: var(--app-height, 100dvh);
  overflow-x: hidden;
  overflow-y: auto;
}

body.unauthenticated #nav,
body.unauthenticated #sidebar,
body.unauthenticated #footer {
  display: none;
}

body.unauthenticated #main-content {
  display: block;
  min-block-size: auto;
  min-height: auto;
  overflow: visible;
}

body.unauthenticated .main-content__scroll {
  display: block;
  flex: initial;
  height: auto;
  min-block-size: auto;
  min-height: auto;
  overflow: visible;
  -webkit-overflow-scrolling: auto;
}

#app-logo {
  display: none;
}

#nav {
  grid-area: nav;
}

#main-content {
  align-content: start;
  background-color: var(--color-bg);
  display: flex;
  flex-direction: column;
  grid-area: main;
  min-block-size: 0; /* allow shrink so grid gives definite height */
  min-height: 0; /* Chrome/WebKit: flex child must shrink so #main-scroll gets bounded height */
  overflow: hidden; /* scroll happens in #main-scroll */
  position: relative;

  .sidebar & {
    justify-content: end;

    @media (min-width: 67.5rem) {
      border-inline: 1px solid var(--color-border-darker);
    }

    /* Mobile: main content hidden by default, sidebar is home */
    @media (max-width: 67.4375rem) {
      opacity: 0;
      pointer-events: none;
      position: fixed;
      visibility: hidden;
    }
  }

  /* Mobile: show main content when in a room */
  .sidebar.in-room & {
    @media (max-width: 67.4375rem) {
      background-color: var(--color-bg);
      inset: 0;
      opacity: 1;
      pointer-events: auto;
      position: fixed;
      visibility: visible;
      z-index: 4;
    }
  }
}

/* Explicit scroll container so long pages (profile, account/edit, etc.) scroll */
.main-content__scroll {
  display: flex;
  flex-direction: column;
  flex: 1 1 0;
  height: 0; /* Chrome/WebKit: force flex item to take remaining space only */
  min-block-size: 0;
  min-height: 0;
  overflow-y: auto; /* scroll here when content overflows (e.g. account/edit); room page scrolls in .messages */
  -webkit-overflow-scrolling: touch;
}

/* Room page: .messages is direct child and becomes the scroll container */
.main-content__scroll > .messages {
  flex: 1 1 0;
  min-block-size: 0;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

#sidebar {
  grid-area: sidebar;
  position: relative;

  .sidebar & {
    -webkit-backdrop-filter: blur(66px);
    backdrop-filter: blur(66px);
    background-color: oklch(var(--lch-white) / 0.66);
    block-size: var(--app-height, 100dvh);
    max-block-size: var(--app-height, 100dvh);
    min-block-size: 0; /* allow grid item to shrink so inner scroll works */
    display: flex;
    flex-direction: column;
    overflow: hidden;
    z-index: 3;

    /* Mobile: sidebar is HOME — full screen, solid background */
    @media (max-width: 67.4375rem) {
      background-color: var(--color-bg);
      block-size: var(--app-height, 100dvh);
      inline-size: 100vw;
      inset: 0;
      position: fixed;
    }
  }

  /* Constrain turbo-frame so .sidebar__container can scroll */
  .sidebar & > * {
    display: flex;
    flex-direction: column;
    flex: 1 1 0;
    min-block-size: 0;
    overflow: hidden;
  }
}

@media (min-width: 67.5rem) {
  body.sidebar.sidebar-collapsed #main-content {
    border-inline: 0;
  }

  body.sidebar.sidebar-collapsed #sidebar {
    opacity: 0;
    pointer-events: none;
  }
}

#footer {
  grid-area: footer;
  flex-shrink: 0;
  padding-bottom: var(--safe-area-inset-bottom);
}

body::after {
  background: #000;
  content: "";
  inset: auto 0 0;
  block-size: var(--safe-area-inset-bottom);
  pointer-events: none;
  position: fixed;
  z-index: 2147483647;
}

html.soft-keyboard-open #footer {
  padding-bottom: 0;
}

html.soft-keyboard-open body::after {
  block-size: 0;
}

/* Prevent keyboard-time viewport/focus adjustments from animating like "tracking". */
html.soft-keyboard-open,
html.soft-keyboard-open body,
html.soft-keyboard-open #main-content,
html.soft-keyboard-open .main-content__scroll,
html.soft-keyboard-open .messages {
  scroll-behavior: auto !important;
}

@media (min-width: 67.5rem) and (prefers-reduced-motion: no-preference) {
  body.sidebar #main-content,
  body.sidebar #sidebar,
  body.sidebar #nav {
    transition:
      margin-inline 180ms ease,
      inset-inline-start 180ms ease,
      inset-inline-end 180ms ease,
      padding-inline 180ms ease,
      opacity 180ms ease,
      transform 180ms ease;
  }

  body.sidebar.is-flipping-sidebar #main-content,
  body.sidebar.is-flipping-sidebar #nav {
    opacity: 0.96;
    transform: translateY(1px);
  }

  body.sidebar.is-flipping-sidebar #sidebar {
    opacity: 0.98;
  }
}
