/* Authoritative sidebar layout contract. Loaded after restored/compat layers. */

:root {
  --app-sidebar-width: 280px;
  --app-sidebar-rail-width: 64px;
  --app-shell-header-height: 68px;
  --app-sidebar-drawer-width: min(88vw, 320px);
  --app-sidebar-drawer-width: min(88dvw, 320px);
  --app-sidebar-layer: 90;
  --app-sidebar-backdrop-layer: 80;
}

.layout.app-shell,
.layout.app-shell.sidebar-pinned {
  grid-template-columns: var(--app-sidebar-width) minmax(0, 1fr);
}

.layout.app-shell.sidebar-floating {
  grid-template-columns: var(--app-sidebar-rail-width) minmax(0, 1fr);
}

.layout.app-shell > #appSidebar.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  z-index: var(--app-sidebar-layer);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  box-sizing: border-box;
  width: var(--app-sidebar-width);
  min-width: var(--app-sidebar-width);
  max-width: var(--app-sidebar-width);
  height: 100vh;
  min-height: 100vh;
  max-height: 100vh;
  height: 100dvh;
  min-height: 100dvh;
  max-height: 100dvh;
  margin: 0;
  border-radius: 0;
  overflow: hidden;
  overscroll-behavior: contain;
  visibility: visible;
  pointer-events: auto;
  transform: none;
  box-shadow: none;
}

.layout.app-shell > .main-content.shell-main {
  grid-column: 2;
  min-width: 0;
  overflow-x: clip;
}

.layout.app-shell.sidebar-floating > .main-content.shell-main {
  grid-column: 2;
}

.layout.app-shell .app-sidebar-brand.sidebar-brand {
  position: relative;
  z-index: 1;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  box-sizing: border-box;
  min-width: 0;
  height: var(--app-shell-header-height);
  min-height: var(--app-shell-header-height);
  max-height: var(--app-shell-header-height);
  margin: 0;
  padding: 11px 14px;
}

.layout.app-shell .sidebar-brand-main,
.layout.app-shell .sidebar-brand .brand,
.layout.app-shell .sidebar-brand .meta {
  min-width: 0;
}

.layout.app-shell .sidebar-brand-main {
  gap: 3px;
}

.layout.app-shell .sidebar-brand .meta {
  margin-top: 0;
}

.layout.app-shell .topbar.topbar-unified {
  box-sizing: border-box;
  height: var(--app-shell-header-height);
  min-height: var(--app-shell-header-height);
  max-height: var(--app-shell-header-height);
}

.layout.app-shell .sidebar-brand .brand,
.layout.app-shell .sidebar-brand .meta,
.layout.app-shell .side-nav-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.layout.app-shell .app-sidebar-nav.side-nav {
  display: grid;
  grid-auto-rows: max-content;
  align-content: start;
  gap: 7px;
  min-width: 0;
  min-height: 0;
  padding: 14px 10px max(18px, env(safe-area-inset-bottom));
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
  -webkit-overflow-scrolling: touch;
}

.layout.app-shell .app-sidebar-support {
  display: grid;
  gap: 9px;
  min-width: 0;
  margin: 0 10px max(14px, env(safe-area-inset-bottom));
  padding: 14px;
  border: 1px solid var(--sidebar-line-polish, var(--line));
  border-radius: 12px;
  background: color-mix(in srgb, var(--panel) 78%, var(--bg));
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

.layout.app-shell .app-sidebar-support strong {
  font-size: 14px;
  line-height: 1.3;
}

.layout.app-shell .app-sidebar-support p {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.layout.app-shell .app-sidebar-support-link {
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 38px;
  padding: 8px 11px;
  border: 1px solid color-mix(in srgb, var(--accent) 32%, var(--line));
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--accent) 9%, var(--panel));
  color: var(--accent);
  font-size: 13px;
  font-weight: 800;
  line-height: 1.2;
  text-decoration: none;
}

.layout.app-shell .app-sidebar-support-link:hover,
.layout.app-shell .app-sidebar-support-link:focus-visible {
  border-color: color-mix(in srgb, var(--accent) 62%, var(--line));
  background: color-mix(in srgb, var(--accent) 15%, var(--panel));
  outline: none;
  box-shadow: var(--focus-ring);
}

.layout.app-shell .app-sidebar-support-icon {
  flex: 0 0 auto;
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.layout.app-shell .topbar .connection-indicator {
  position: static;
  inset: auto;
  z-index: auto;
  flex: 0 0 auto;
  box-sizing: border-box;
  min-height: 44px;
  max-width: min(230px, 28vw);
  padding: 8px 11px;
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--soft-panel) 72%, var(--panel));
  box-shadow: inset 0 1px 0 rgb(255 255 255 / 0.04);
}

.layout.app-shell .topbar .connection-indicator-label {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.layout.app-shell .app-sidebar-link.side-nav-button,
.layout.app-shell .app-sidebar-section > .side-nav-title {
  width: 100%;
  min-width: 0;
  max-width: 100%;
  min-height: 46px;
  margin: 0;
  padding: 10px 12px;
  overflow: hidden;
  text-align: left;
  touch-action: manipulation;
}

.layout.app-shell .app-sidebar-nav > .app-sidebar-link.side-nav-button {
  border-color: var(--sidebar-line-polish, var(--line));
  background: color-mix(in srgb, var(--panel) 62%, transparent);
}

.layout.app-shell .app-sidebar-nav > .app-sidebar-link.side-nav-button:not(.active):not(.is-active) {
  border-color: var(--sidebar-line-polish, var(--line));
  background: color-mix(in srgb, var(--panel) 62%, transparent);
}

.layout.app-shell .app-sidebar-section.side-nav-section {
  width: 100%;
  min-width: 0;
  max-width: 100%;
  height: auto;
  max-height: none;
  margin: 0;
  overflow: clip;
}

.layout.app-shell .app-sidebar-section > .side-nav-title::after {
  flex: 0 0 24px;
  margin-left: auto;
}

.layout.app-shell .side-nav-icon {
  display: inline-grid;
  flex: 0 0 28px;
  place-items: center;
  width: 28px;
  min-width: 28px;
  height: 28px;
  min-height: 28px;
  border: 1px solid color-mix(in srgb, var(--line) 72%, transparent);
  border-radius: 8px;
  background: color-mix(in srgb, var(--muted) 9%, transparent);
  color: color-mix(in srgb, var(--text) 88%, var(--muted));
  font-size: 15px;
  line-height: 1;
}

.layout.app-shell .app-sidebar-link:is(.active, .is-active) .side-nav-icon,
.layout.app-shell .app-sidebar-section.is-active > .side-nav-title .side-nav-icon {
  border-color: color-mix(in srgb, var(--accent) 52%, var(--line));
  background: color-mix(in srgb, var(--accent) 16%, transparent);
  color: var(--accent);
}

.layout.app-shell .app-sidebar-section .side-nav-list {
  display: grid;
  gap: 4px;
  min-width: 0;
  padding: 0 6px 7px;
}

.layout.app-shell .app-sidebar-link.side-nav-child {
  min-height: 42px;
  padding-inline: 14px 10px;
}

.layout.app-shell .app-sidebar-link.side-nav-child .side-nav-icon {
  flex-basis: 24px;
  width: 24px;
  min-width: 24px;
  height: 24px;
  min-height: 24px;
  border-color: transparent;
  background: transparent;
  font-size: 14px;
}

.layout.app-shell .app-sidebar-nav.side-nav::-webkit-scrollbar {
  width: 8px;
}

.layout.app-shell .app-sidebar-nav.side-nav::-webkit-scrollbar-track {
  background: transparent;
}

.layout.app-shell .app-sidebar-nav.side-nav::-webkit-scrollbar-thumb {
  border: 2px solid transparent;
  border-radius: var(--radius-pill);
  background: color-mix(in srgb, var(--muted) 34%, transparent);
  background-clip: padding-box;
}

.layout.app-shell .app-sidebar-nav.side-nav::-webkit-scrollbar-thumb:hover {
  background: color-mix(in srgb, var(--muted) 52%, transparent);
  background-clip: padding-box;
}

.layout.app-shell .app-sidebar-link.side-nav-button:focus-visible,
.layout.app-shell .app-sidebar-section > .side-nav-title:focus-visible,
.layout.app-shell .sidebar-pin:focus-visible,
.layout.app-shell .sidebar-close:focus-visible,
.layout.app-shell .topbar-menu-button:focus-visible {
  outline: 2px solid color-mix(in srgb, var(--accent) 78%, white);
  outline-offset: 2px;
}

.layout.app-shell .sidebar-close,
.layout.app-shell .sidebar-backdrop,
.layout.app-shell .topbar-menu-button {
  display: none;
}

@media (min-width: 1025px) {
  .layout.app-shell.sidebar-floating > #appSidebar.sidebar {
    width: var(--app-sidebar-rail-width);
    min-width: var(--app-sidebar-rail-width);
    max-width: var(--app-sidebar-rail-width);
    visibility: visible;
    pointer-events: auto;
    transform: none;
    box-shadow: none;
    transition:
      width 220ms cubic-bezier(0.22, 1, 0.36, 1),
      min-width 220ms cubic-bezier(0.22, 1, 0.36, 1),
      max-width 220ms cubic-bezier(0.22, 1, 0.36, 1),
      box-shadow 180ms ease;
    will-change: width;
  }

  .layout.app-shell.sidebar-floating > #appSidebar.sidebar:is(:hover, :focus-within) {
    width: var(--app-sidebar-width);
    min-width: var(--app-sidebar-width);
    max-width: var(--app-sidebar-width);
    box-shadow: 18px 0 44px rgb(0 0 0 / 0.34);
  }

  .layout.app-shell.sidebar-floating:not(.sidebar-hovered):not(.app-shell-no-sidebar):not(.work-position-mode) > #appSidebar.sidebar:not(:hover):not(:focus-within) .app-sidebar-brand.sidebar-brand {
    grid-template-columns: 1fr;
    justify-items: center;
    height: var(--app-shell-header-height);
    min-height: var(--app-shell-header-height);
    max-height: var(--app-shell-header-height);
    padding: 11px 8px;
  }

  .layout.app-shell.sidebar-floating > #appSidebar.sidebar:not(:hover):not(:focus-within) :is(.sidebar-brand-main, .side-nav-label, .side-nav-list) {
    display: none !important;
  }

  .layout.app-shell.sidebar-floating > #appSidebar.sidebar:not(:hover):not(:focus-within) .app-sidebar-support {
    display: block;
    width: 48px;
    min-width: 48px;
    height: 48px;
    min-height: 48px;
    margin: 0 auto max(14px, env(safe-area-inset-bottom));
    padding: 0;
    border: 0;
    background: transparent;
    box-shadow: none;
  }

  .layout.app-shell.sidebar-floating > #appSidebar.sidebar:not(:hover):not(:focus-within) .app-sidebar-support > :not(.app-sidebar-support-link) {
    display: none;
  }

  .layout.app-shell.sidebar-floating > #appSidebar.sidebar:not(:hover):not(:focus-within) .app-sidebar-support-link {
    justify-content: center;
    width: 48px;
    min-width: 48px;
    height: 48px;
    min-height: 48px;
    padding: 0;
  }

  .layout.app-shell.sidebar-floating > #appSidebar.sidebar:not(:hover):not(:focus-within) .app-sidebar-support-link > span:first-child {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
  }

  .layout.app-shell.sidebar-floating > #appSidebar.sidebar:not(:hover):not(:focus-within) .app-sidebar-support-icon {
    width: 14px;
    height: 14px;
    color: var(--accent);
  }

  .layout.app-shell.sidebar-floating > #appSidebar.sidebar:not(:hover):not(:focus-within) .side-nav-title::after {
    display: none !important;
  }

  .layout.app-shell.sidebar-floating > #appSidebar.sidebar:not(:hover):not(:focus-within) .sidebar-pin {
    width: 44px;
    min-width: 44px;
    height: 44px;
    min-height: 44px;
    margin: 0 auto;
    padding: 0;
  }

  .layout.app-shell.sidebar-floating > #appSidebar.sidebar:not(:hover):not(:focus-within) .app-sidebar-nav.side-nav {
    justify-items: center;
    gap: 8px;
    padding: 10px 8px 14px;
    scrollbar-gutter: auto;
  }

  .layout.app-shell.sidebar-floating > #appSidebar.sidebar:not(:hover):not(:focus-within) :is(.side-nav-section, .side-nav-section.is-active) {
    display: block;
    width: 48px;
    min-width: 48px;
    max-width: 48px;
    height: 48px;
    min-height: 48px;
    max-height: 48px;
    margin: 0 auto;
    padding: 0;
    overflow: visible;
  }

  .layout.app-shell.sidebar-floating > #appSidebar.sidebar:not(:hover):not(:focus-within) :is(.side-nav-button, .side-nav-title) {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0;
    box-sizing: border-box;
    width: 48px;
    min-width: 48px;
    max-width: 48px;
    height: 48px;
    min-height: 48px;
    max-height: 48px;
    margin: 0 auto;
    padding: 0;
    overflow: visible;
  }
}

@media (hover: hover) and (pointer: fine) {
  .layout.app-shell .app-sidebar-link.side-nav-button,
  .layout.app-shell .app-sidebar-section > .side-nav-title {
    transition:
      border-color 160ms ease,
      background-color 160ms ease,
      color 160ms ease,
      box-shadow 160ms ease;
  }
}

@media (max-width: 1024px) {
  .layout.app-shell,
  .layout.app-shell.sidebar-pinned,
  .layout.app-shell.sidebar-hidden,
  .layout.app-shell.sidebar-floating {
    display: block;
    grid-template-columns: minmax(0, 1fr);
  }

  .layout.app-shell > .main-content.shell-main {
    grid-column: 1 / -1;
    width: 100%;
    min-width: 0;
  }

  .layout.app-shell > #appSidebar.sidebar,
  .layout.app-shell.sidebar-pinned > #appSidebar.sidebar,
  .layout.app-shell.sidebar-hidden > #appSidebar.sidebar,
  .layout.app-shell.sidebar-floating > #appSidebar.sidebar {
    inset: 0 auto 0 0;
    width: var(--app-sidebar-drawer-width);
    min-width: 0;
    max-width: 320px;
    height: 100vh;
    min-height: 100vh;
    max-height: 100vh;
    height: 100dvh;
    min-height: 100dvh;
    max-height: 100dvh;
    border-right: 1px solid var(--line);
    border-bottom: 0;
    border-radius: 0;
    visibility: hidden;
    pointer-events: none;
    transform: translate3d(-105%, 0, 0);
    box-shadow: 18px 0 44px rgb(0 0 0 / 0.34);
    transition:
      transform 220ms cubic-bezier(0.22, 1, 0.36, 1),
      visibility 0s linear 220ms;
    will-change: transform;
  }

  .layout.app-shell.sidebar-open > #appSidebar.sidebar {
    visibility: visible;
    pointer-events: auto;
    transform: translate3d(0, 0, 0);
    transition-delay: 0s;
  }

  .layout.app-shell .app-sidebar-brand.sidebar-brand {
    grid-template-columns: minmax(0, 1fr) 42px;
    gap: 10px;
    padding: max(14px, env(safe-area-inset-top)) 12px 14px max(14px, env(safe-area-inset-left));
  }

  .layout.app-shell .sidebar-pin {
    display: none !important;
  }

  .layout.app-shell .sidebar-close {
    display: inline-grid;
    place-items: center;
    width: 42px;
    min-width: 42px;
    height: 42px;
    min-height: 42px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 0;
    background: var(--soft-panel);
    color: var(--text);
    cursor: pointer;
    font-size: 24px;
    line-height: 1;
  }

  .layout.app-shell .app-sidebar-nav.side-nav {
    scrollbar-gutter: auto;
  }

  .layout.app-shell .sidebar-backdrop {
    position: fixed;
    inset: 0;
    z-index: var(--app-sidebar-backdrop-layer);
    display: block;
    width: 100%;
    height: 100%;
    min-height: 0;
    border: 0;
    border-radius: 0;
    padding: 0;
    background: rgb(0 0 0 / 0.58);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition:
      opacity 180ms ease,
      visibility 0s linear 180ms;
  }

  .layout.app-shell.sidebar-open .sidebar-backdrop {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    transition-delay: 0s;
  }

  .layout.app-shell .topbar .topbar-menu-button {
    display: inline-grid;
    grid-area: menu;
    grid-template-columns: auto minmax(0, auto);
    place-items: center;
    gap: 6px;
    width: auto;
    min-width: 44px;
    min-height: 40px;
    padding: 7px 10px;
    white-space: nowrap;
  }

  body:has(.layout.app-shell.sidebar-open) {
    overflow: hidden;
    overscroll-behavior: none;
  }
}

/* The drawer is already active up to 1024px, so its launcher must remain in
   the viewport on tablet widths too. Sticky cannot follow document scrolling
   here because .shell-main establishes an overflow context. */
@media (min-width: 761px) and (max-width: 1024px) {
  .layout.app-shell {
    --mobile-fixed-topbar-offset: var(--app-shell-header-height);
    --sidebar-tablet-topbar-offset: var(--app-shell-header-height);
  }

  .layout.app-shell .topbar.topbar-unified {
    position: fixed;
    top: max(0px, env(safe-area-inset-top));
    right: 0;
    left: 0;
    z-index: 70;
    grid-template-columns: 44px minmax(0, 1fr) auto;
    grid-template-areas: "menu title actions";
    align-items: center;
    width: auto;
    margin: 0;
  }

  .layout.app-shell .topbar.topbar-unified .topbar-title {
    grid-area: title;
  }

  .layout.app-shell .topbar.topbar-unified .topbar-actions {
    display: flex;
    grid-area: actions;
    width: auto;
  }

  .layout.app-shell .topbar.topbar-unified + .content {
    padding-top: calc(var(--sidebar-tablet-topbar-offset) + 12px);
  }
}

@media (min-width: 761px) and (max-width: 900px) {
  .layout.app-shell .topbar .connection-indicator {
    justify-content: center;
    width: 44px;
    min-width: 44px;
    max-width: 44px;
    padding: 0;
  }

  .layout.app-shell .topbar .connection-indicator-label {
    display: none;
  }
}

@media (max-width: 760px) {
  .layout.app-shell .topbar.topbar-unified {
    grid-template-columns: 44px minmax(70px, 1fr) 42px minmax(50px, 1fr) minmax(50px, 0.78fr);
    grid-template-areas: "menu title status notify profile";
    height: auto;
    min-height: 0;
    max-height: none;
  }

  .layout.app-shell .topbar.topbar-unified.topbar-no-sidebar {
    grid-template-columns: minmax(70px, 1fr) 42px minmax(50px, 1fr) minmax(50px, 0.78fr);
    grid-template-areas: "title status notify profile";
  }

  .layout.app-shell .topbar .connection-indicator {
    grid-area: status;
    justify-self: stretch;
    justify-content: center;
    width: 100%;
    min-width: 0;
    max-width: none;
    height: 48px;
    min-height: 48px;
    padding: 0;
  }

  .layout.app-shell .topbar .connection-indicator-label {
    display: none;
  }
}

@media (max-width: 560px) {
  :root {
    --app-sidebar-drawer-width: min(
      calc(100vw - max(24px, env(safe-area-inset-right))),
      320px
    );
    --app-sidebar-drawer-width: min(
      calc(100dvw - max(24px, env(safe-area-inset-right))),
      320px
    );
  }

  .layout.app-shell > #appSidebar.sidebar,
  .layout.app-shell.sidebar-pinned > #appSidebar.sidebar,
  .layout.app-shell.sidebar-hidden > #appSidebar.sidebar,
  .layout.app-shell.sidebar-floating > #appSidebar.sidebar {
    inset: 0 auto 0 0;
    width: var(--app-sidebar-drawer-width);
    max-width: var(--app-sidebar-drawer-width);
    border-radius: 0 12px 12px 0;
  }

  .layout.app-shell .topbar .topbar-menu-label {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .layout.app-shell > #appSidebar.sidebar,
  .layout.app-shell .sidebar-backdrop,
  .layout.app-shell .app-sidebar-link.side-nav-button,
  .layout.app-shell .app-sidebar-section > .side-nav-title {
    scroll-behavior: auto;
    transition-duration: 0.01ms !important;
  }
}

@media (forced-colors: active) {
  .layout.app-shell > #appSidebar.sidebar,
  .layout.app-shell .sidebar-close,
  .layout.app-shell .app-sidebar-link.side-nav-button,
  .layout.app-shell .app-sidebar-section.side-nav-section {
    border-color: CanvasText;
  }

  .layout.app-shell .app-sidebar-support,
  .layout.app-shell .app-sidebar-support-link,
  .layout.app-shell .topbar .connection-indicator {
    border-color: CanvasText;
  }

  .layout.app-shell .sidebar-backdrop {
    background: Canvas;
    opacity: 0.72;
  }
}
