.app-shell {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  width: 100%;
  max-width: calc(var(--rail-w) + 8px + var(--content-max));
  margin: 0 auto;
  padding: 0 var(--space-3);
  min-height: 100vh;
  box-sizing: border-box;
}

.app-shell:not(:has(.app-nav)) {
  display: contents;
}

.app-topbar {
  display: none;
}

.app-nav-backdrop {
  display: none;
}

.app-nav__drawer-head {
  display: none;
}

.app-nav__brand--drawer {
  display: none;
}

.app-nav__close {
  display: none;
}

.app-nav {
  position: sticky;
  top: 0;
  z-index: 900;
  display: flex;
  flex-direction: column;
  flex: 0 0 var(--rail-w);
  width: var(--rail-w);
  height: 100vh;
  align-self: flex-start;
  gap: 2px;
  padding: var(--space-2) var(--space-1);
  background: var(--bg);
  overflow: visible;
  box-sizing: border-box;
}

.app-nav__brand {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  height: 44px;
  padding: 0;
  border-radius: var(--radius-pill);
  color: var(--label-primary);
  flex-shrink: 0;
}

.app-nav__brand img {
  width: 32px;
  height: 32px;
  object-fit: contain;
}

.app-nav__wordmark {
  display: none !important;
}

.app-nav__list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-top: var(--space-1);
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  scrollbar-width: thin;
}

.app-nav__group {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.app-nav__chevron {
  margin-left: auto;
  width: 16px !important;
  height: 16px !important;
  opacity: 0.55;
  display: none;
}

.app-nav__sub {
  display: none;
  flex-direction: column;
  gap: 1px;
  padding: 2px 0 4px 0;
}

.app-nav__group.is-open .app-nav__sub {
  display: flex;
}

.app-nav__sublink {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: 34px;
  padding: 0 10px 0 14px;
  border: 0;
  border-radius: var(--radius-pill);
  background: transparent;
  color: var(--label-secondary);
  font: inherit;
  font-size: 12px;
  text-align: left;
  text-decoration: none;
  cursor: pointer;
  transition: background-color var(--motion-fast) var(--ease-standard), color var(--motion-fast) var(--ease-standard);
}

.app-nav__sublink:hover {
  background: var(--fill-primary);
  color: var(--label-primary);
}

.app-nav__sublink.is-active {
  background: var(--fill-secondary);
  color: var(--label-primary);
  font-weight: var(--text-weight-semibold);
}

.app-nav__sublink > span {
  display: inline;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.app-nav[data-studio-open="1"],
.app-nav[data-admin-open="1"] {
  overflow-y: auto;
}

.app-nav__sublink .app-badge {
  margin-left: auto;
}

/* Compact rail: widen slightly so Studio/Admin submenu labels fit */
@media (min-width: 861px) and (max-width: 1099px) {
  .app-nav[data-studio-open="1"],
  .app-nav[data-admin-open="1"] {
    flex-basis: 148px;
    width: 148px;
  }

  .app-nav[data-studio-open="1"] .app-nav__link > span:not(.app-badge):not(.app-nav__flag),
  .app-nav[data-studio-open="1"] .app-nav__wordmark,
  .app-nav[data-studio-open="1"] .app-nav__lang-code,
  .app-nav[data-studio-open="1"] .app-nav__chevron,
  .app-nav[data-studio-open="1"] .app-nav__account-name,
  .app-nav[data-admin-open="1"] .app-nav__link > span:not(.app-badge):not(.app-nav__flag),
  .app-nav[data-admin-open="1"] .app-nav__wordmark,
  .app-nav[data-admin-open="1"] .app-nav__lang-code,
  .app-nav[data-admin-open="1"] .app-nav__chevron,
  .app-nav[data-admin-open="1"] .app-nav__account-name {
    display: inline;
  }

  .app-nav[data-studio-open="1"] .app-nav__brand,
  .app-nav[data-studio-open="1"] .app-nav__link,
  .app-nav[data-admin-open="1"] .app-nav__brand,
  .app-nav[data-admin-open="1"] .app-nav__link {
    justify-content: flex-start;
    padding: 0 10px;
  }

  .app-nav[data-studio-open="1"] .app-nav__sublink,
  .app-nav[data-admin-open="1"] .app-nav__sublink {
    padding-left: 28px;
  }
}

.app-nav__link {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  width: 100%;
  height: 44px;
  padding: 0;
  border: 0;
  border-radius: var(--radius-pill);
  background: transparent;
  color: var(--label-primary);
  font: inherit;
  font-size: var(--text-size-subhead);
  font-weight: var(--text-weight-regular);
  white-space: nowrap;
  cursor: pointer;
  transition: background-color var(--motion-fast) var(--ease-standard);
  -webkit-tap-highlight-color: transparent;
}

.app-nav__link:hover {
  background: var(--fill-primary);
}

.app-nav__link.is-active {
  background: var(--fill-tertiary);
  font-weight: var(--text-weight-bold);
}

.app-nav__link--accent {
  color: var(--label-primary);
}

.app-nav__link > span:not(.app-badge):not(.app-nav__flag) {
  display: none;
}

.app-nav__link svg {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
}

.app-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: var(--radius-pill);
  background: var(--label-primary);
  color: var(--label-on-accent);
  font-size: 10px;
  font-weight: var(--text-weight-semibold);
  line-height: 1;
}

.app-nav__link .app-badge {
  position: absolute;
  top: 4px;
  right: 4px;
}

.app-nav__bottom {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-top: auto;
  padding-top: var(--space-2);
  overflow: visible;
}

.app-nav__menu {
  position: relative;
  z-index: 960;
  overflow: visible;
}

.app-nav__flag {
  display: inline-flex;
  align-items: center;
  flex-shrink: 0;
}

.flag-icon {
  display: block;
  border-radius: 3px;
}

.app-nav__lang-code {
  display: none;
  font-size: var(--text-size-caption1);
  font-weight: var(--text-weight-semibold);
  letter-spacing: 0.04em;
}

.app-nav__panel[hidden] {
  display: none !important;
}

.app-nav__panel {
  position: absolute;
  left: calc(100% + 8px);
  bottom: 0;
  z-index: 950;
  width: 240px;
  max-height: min(70vh, 520px);
  overflow-y: auto;
  padding: var(--space-3);
  border-radius: var(--radius-xl);
  background: var(--popup-bg);
  box-shadow: var(--shadow-md);
  overscroll-behavior: contain;
}

.app-nav__list .app-nav__panel {
  top: 0;
  bottom: auto;
}

.app-nav__panel--lang {
  width: 180px;
  max-height: none;
  padding: 6px;
}

.app-nav__option {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 10px;
  border: 0;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--label-secondary);
  font: inherit;
  font-size: var(--text-size-footnote);
  text-align: left;
  cursor: pointer;
}

.app-nav__option span {
  line-height: 1;
}

.app-nav__option:hover,
.app-nav__option.is-active {
  background: var(--surface-raised);
  color: var(--label-primary);
}

.rail-help {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.rail-help__title {
  margin: 0;
  color: var(--label-tertiary);
  font-size: 11px;
  font-weight: var(--text-weight-semibold);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.rail-help__links {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.rail-help__links a {
  color: var(--label-secondary);
  font-size: var(--text-size-caption1);
  line-height: 1.6;
  text-decoration: none;
}

.rail-help__links a:hover {
  color: var(--label-primary);
}

.rail-help__social {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding-top: var(--space-2);
}

.rail-help__social a {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  border-radius: var(--radius-pill);
  background: var(--fill-primary);
  color: var(--label-secondary);
}

.rail-help__social a:hover {
  background: var(--fill-tertiary);
  color: var(--label-primary);
}

.rail-help__social .social-icon {
  display: block;
}

.rail-help__copy {
  margin: 0;
  color: var(--label-quaternary);
  font-size: 10px;
}

.app-nav__logout {
  margin: 0;
  flex: 0 0 auto;
}

.app-nav__account {
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: 44px;
  padding: 0 2px;
}

.app-nav__account-main {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 40px;
  padding: 0 6px;
  border-radius: var(--radius-pill);
  color: var(--label-primary);
  text-decoration: none;
  transition: background-color var(--motion-fast) var(--ease-standard);
}

.app-nav__account-main:hover {
  background: var(--fill-primary);
}

.app-nav__avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
  background: var(--fill-tertiary);
}

.app-nav__account-name {
  display: none;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: var(--text-size-footnote);
  font-weight: var(--text-weight-semibold);
}

.app-nav__logout-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: var(--radius-pill);
  background: transparent;
  color: var(--label-secondary);
  cursor: pointer;
  flex-shrink: 0;
  transition: background-color var(--motion-fast) var(--ease-standard), color var(--motion-fast) var(--ease-standard);
}

.app-nav__logout-btn:hover {
  background: var(--fill-primary);
  color: var(--label-primary);
}

.app-nav__logout-btn svg {
  width: 18px;
  height: 18px;
}

.app-shell:has(.app-nav) > .main-container {
  flex: 1 1 auto;
  min-width: 0;
  max-width: var(--content-max);
  margin: 0;
  padding: var(--space-4) 0 var(--space-7);
}

.app-shell:has(.bm-shell) {
  max-width: calc(var(--rail-w) + 8px + 900px);
}

.app-shell:has(.bm-shell) > .main-container {
  max-width: 900px;
}

@media (min-width: 1100px) {
  .app-shell {
    max-width: calc(var(--rail-w-wide) + 8px + var(--content-max));
  }

  .app-nav {
    flex-basis: var(--rail-w-wide);
    width: var(--rail-w-wide);
    padding: var(--space-2);
  }

  .app-nav__brand {
    justify-content: flex-start;
    padding: 0 var(--space-3);
  }

  .app-nav__brand img {
    width: 34px;
    height: 34px;
  }

  .app-nav__link {
    justify-content: flex-start;
    padding: 0 var(--space-3);
  }

  .app-nav__link > span:not(.app-badge):not(.app-nav__flag) {
    display: inline;
  }

  .app-nav__chevron {
    display: block;
  }

  .app-nav__sublink {
    padding-left: 44px;
    font-size: var(--text-size-footnote);
  }

  .app-nav__link .app-badge {
    position: static;
    margin-left: auto;
  }

  .app-nav__lang-code {
    display: inline;
  }

  .app-nav__account-name {
    display: inline;
  }

  .app-nav__account {
    padding: 0;
  }

  .app-nav__account-main {
    padding: 0 8px 0 6px;
  }

  .app-shell:has(.bm-shell) {
    max-width: calc(var(--rail-w-wide) + 8px + 900px);
  }
}

@media (max-width: 1099px) {
  .app-nav__panel {
    left: calc(100% + 6px);
  }
}

@media (max-width: 860px) {
  .app-shell {
    display: block;
    max-width: var(--content-max);
    padding: 0 var(--space-3);
  }

  .app-topbar {
    position: sticky;
    top: 0;
    z-index: 920;
    display: grid;
    grid-template-columns: 48px 1fr 48px;
    align-items: center;
    height: 56px;
    margin: 0 calc(var(--space-3) * -1);
    padding: 0 var(--space-2);
    background: color-mix(in srgb, var(--bg) 92%, transparent);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
  }

  .app-topbar__brand {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    justify-self: center;
    grid-column: 2;
    color: var(--label-primary);
    text-decoration: none;
  }

  .app-topbar__brand img {
    width: 36px;
    height: 36px;
    object-fit: contain;
  }

  .app-topbar__burger {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    justify-self: start;
    grid-column: 1;
    width: 48px;
    height: 48px;
    border: 0;
    border-radius: 0;
    background: transparent;
    color: var(--label-primary);
    cursor: pointer;
    padding: 0;
  }

  .app-topbar__burger svg {
    width: 28px;
    height: 28px;
  }

  .app-topbar__spacer {
    grid-column: 3;
    width: 48px;
    height: 48px;
  }

  .app-nav-backdrop {
    position: fixed;
    inset: 0;
    z-index: 980;
    display: block;
    background: rgba(0, 0, 0, 0.55);
    opacity: 0;
    pointer-events: none;
    transition: opacity 180ms ease;
  }

  .app-nav-backdrop.is-open {
    opacity: 1;
    pointer-events: auto;
  }

  .app-nav-backdrop[hidden] {
    display: none !important;
  }

  .app-nav {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 990;
    width: min(300px, 86vw);
    max-width: 300px;
    height: 100vh;
    height: 100dvh;
    padding: 12px;
    background: var(--bg);
    box-shadow: 8px 0 32px rgba(0, 0, 0, 0.35);
    transform: translateX(-105%);
    transition: transform 200ms ease;
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .app-nav.is-open {
    transform: translateX(0);
  }

  .app-nav__drawer-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 8px;
  }

  .app-nav__brand--rail {
    display: none;
  }

  .app-nav__brand--drawer {
    display: flex;
    justify-content: flex-start;
    padding: 0 4px;
  }

  .app-nav__brand--drawer img {
    width: 32px;
    height: 32px;
  }

  .app-nav__close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: 0;
    border-radius: 10px;
    background: var(--fill-quaternary);
    color: var(--label-primary);
    cursor: pointer;
  }

  .app-nav__close svg {
    width: 18px;
    height: 18px;
  }

  .app-nav__link {
    justify-content: flex-start;
    padding: 0 12px;
  }

  .app-nav__link > span:not(.app-badge):not(.app-nav__flag) {
    display: inline;
  }

  .app-nav__chevron {
    display: block;
  }

  .app-nav__sublink {
    padding-left: 44px;
    font-size: var(--text-size-footnote);
  }

  .app-nav__link .app-badge {
    position: static;
    margin-left: auto;
  }

  .app-nav__lang-code {
    display: inline;
  }

  .app-nav__account-name {
    display: inline;
  }

  .app-nav__panel {
    position: static;
    left: auto;
    right: auto;
    bottom: auto;
    width: 100%;
    margin-top: 6px;
    box-shadow: none;
  }

  .app-shell:has(.app-nav) > .main-container,
  .app-shell:has(.bm-shell) > .main-container {
    max-width: none;
    padding: 12px 0 80px;
  }

  body.nav-open {
    overflow: hidden;
  }
}

@media (max-width: 600px) {
  .app-shell {
    padding: 0 var(--space-2);
  }

  .app-topbar {
    margin: 0 calc(var(--space-2) * -1);
    padding: 0 var(--space-2);
  }
}
