<script setup>
import { computed, onMounted, onUnmounted, provide, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useAuthStore } from './stores/auth'
import { toApiUrl } from './lib/runtime'
import { useToast } from './composables/useToast'
import iconDockToLeft from './assets/icons/dock_to_left.svg'
import iconDockToRight from './assets/icons/dock_to_right.svg'
import iconGridView from './assets/icons/grid_view.svg'
import iconLists from './assets/icons/lists.svg'

const route = useRoute()
const router = useRouter()
const auth = useAuthStore()
const { toasts, dismissToast } = useToast()

const menuOpen = ref(false)
const rightRailOpen = ref(true)
provide('rightRailOpen', rightRailOpen)

const isAdmin = computed(() => !!auth.user?.isAdmin)
const isPreviewMode = computed(() => route.query.preview === '1')
const usesLocalRightRail = computed(() => ['editEditor', 'newEditor', 'admin'].includes(String(route.name || '')))
const avatarUrl = computed(() => (auth.user?.avatarSrc ? toApiUrl(auth.user.avatarSrc) : ''))
const accountName = computed(() => {
  const nickname = (auth.user?.nickname || '').trim()
  if (nickname) return nickname
  const email = (auth.user?.email || '').trim()
  if (email) return email.split('@')[0] || email
  return 'Guest'
})
const accountEmail = computed(() => (auth.user?.email || '').trim() || '로그인 후 개인 메뉴를 사용할 수 있어요.')
const leftNavItems = computed(() => {
  const items = [
    { key: 'home', label: 'Games', path: '/', iconSrc: iconGridView },
    { key: 'me', label: '내 리스트', path: '/me', iconSrc: iconLists, requiresAuth: true },
    { key: 'favorites', label: '즐겨찾기', path: '/favorites', icon: 'M12 4.75l2.18 4.42 4.88.71-3.53 3.44.83 4.86L12 15.9 7.64 18.18l.83-4.86-3.53-3.44 4.88-.71z', requiresAuth: true },
    { key: 'profile', label: 'Settings', path: '/profile', icon: 'M12 4.75a2.2 2.2 0 0 1 2.08 1.5l.18.56.58.13a2.2 2.2 0 0 1 1.52 2.76l-.17.56.39.46a2.2 2.2 0 0 1 0 2.86l-.39.46.17.56a2.2 2.2 0 0 1-1.52 2.76l-.58.13-.18.56a2.2 2.2 0 0 1-4.16 0l-.18-.56-.58-.13a2.2 2.2 0 0 1-1.52-2.76l.17-.56-.39-.46a2.2 2.2 0 0 1 0-2.86l.39-.46-.17-.56a2.2 2.2 0 0 1 1.52-2.76l.58-.13.18-.56A2.2 2.2 0 0 1 12 4.75z M12 9.35a2.65 2.65 0 1 0 0 5.3 2.65 2.65 0 0 0 0-5.3z', requiresAuth: true },
  ]
  if (isAdmin.value) {
    items.push({ key: 'admin', label: 'Admin', path: '/admin', iconSrc: iconLists })
  }
  return items.filter((item) => !item.requiresAuth || auth.user)
})
const routeMeta = computed(() => {
  if (route.name === 'home') {
    return {
      title: 'Tier Maker',
      subtitle: '게임 템플릿 선택과 커스텀 보드 시작',
      contextTitle: '빠른 시작',
      contextText: auth.user ? '커스텀 티어표를 만들거나 원하는 게임을 바로 선택할 수 있어요.' : '로그인하면 커스텀 티어표 생성과 개인 목록 관리가 열립니다.',
      actionLabel: auth.user ? '커스텀 티어표 만들기' : '로그인하러 가기',
      action: () => {
        router.push(auth.user ? '/editor/freeform/new' : '/login')
      },
    }
  }
  if (route.name === 'gameHub') {
    return {
      title: 'Game Boards',
      subtitle: '게임별 공개 티어표 탐색',
      contextTitle: '작성 작업',
      contextText: auth.user ? '이 게임의 새 티어표를 만들거나 기존 공개 티어표를 확인할 수 있어요.' : '로그인 후 새 티어표를 만들 수 있어요.',
      actionLabel: auth.user ? '새 티어표 만들기' : '로그인하러 가기',
      action: () => {
        const target = `/editor/${route.params.gameId}/new`
        router.push(auth.user ? target : `/login?redirect=${target}`)
      },
    }
  }
  if (route.name === 'editEditor' || route.name === 'newEditor') {
    return {
      title: 'Deck Builder',
      subtitle: '티어표 편집 및 공유',
      contextTitle: '편집 패널',
      contextText: '현재 편집 옵션은 중앙 화면 안에 유지되어 있습니다. 다음 단계에서 우측 패널로 정리해갈게요.',
      actionLabel: '게임 목록으로',
      action: () => router.push('/'),
    }
  }
  if (route.name === 'admin') {
    return {
      title: 'Admin Workspace',
      subtitle: '게임·아이템·회원 관리',
      contextTitle: '운영 노트',
      contextText: '관리자 화면은 기능이 많아 우선 공통 셸 톤을 맞췄고, 세부 패널은 다음 단계에서 시안 방식으로 더 세밀하게 나눌 예정입니다.',
      actionLabel: '게임 목록으로',
      action: () => router.push('/'),
    }
  }
  if (route.name === 'me') {
    return {
      title: 'My Lists',
      subtitle: '내가 저장한 티어표',
      contextTitle: '작성 이력',
      contextText: '최근 저장한 티어표를 열람하고 정리할 수 있어요.',
      actionLabel: '즐겨찾기 보기',
      action: () => router.push('/favorites'),
    }
  }
  if (route.name === 'favorites') {
    return {
      title: 'Favorites',
      subtitle: '마음에 드는 티어표 모음',
      contextTitle: '정리 도구',
      contextText: '정렬과 검색으로 원하는 티어표를 빠르게 다시 찾을 수 있어요.',
      actionLabel: '내 티어표 보기',
      action: () => router.push('/me'),
    }
  }
  if (route.name === 'profile') {
    return {
      title: 'Profile',
      subtitle: '프로필 및 계정 설정',
      contextTitle: '계정 관리',
      contextText: '아바타와 닉네임을 관리하고 표시 정보를 확인할 수 있어요.',
      actionLabel: '내 티어표 보기',
      action: () => router.push('/me'),
    }
  }
  return {
    title: 'Tier Maker',
    subtitle: 'by zenn',
    contextTitle: 'Workspace',
    contextText: '현재 화면에 맞는 도구와 안내를 여기에 배치할 수 있습니다.',
    actionLabel: '홈으로',
    action: () => router.push('/'),
  }
})
const favoriteLinks = computed(() => [
  { label: 'Games', path: '/' },
  ...(auth.user ? [{ label: 'Favorites', path: '/favorites' }] : []),
  ...(auth.user ? [{ label: 'My Lists', path: '/me' }] : []),
])

function railGlyph(type) {
  if (type === 'menu') return 'M4 6.5h16M4 12h16M4 17.5h16'
  if (type === 'search') return 'M10.2 6.2a4 4 0 1 1 0 8 4 4 0 0 1 0-8z M13.6 13.6l3.2 3.2'
  if (type === 'link') return 'M8 12h8 M12 8l4 4-4 4'
  return 'M4 12h16'
}

onMounted(async () => {
  await auth.refresh()
  if (typeof window !== 'undefined') {
    const saved = window.localStorage.getItem('tier-maker:right-rail-open')
    if (saved === '0') rightRailOpen.value = false
  }
  document.addEventListener('click', onDocumentClick)
})

onUnmounted(() => {
  document.removeEventListener('click', onDocumentClick)
})

watch(
  () => route.fullPath,
  () => {
    menuOpen.value = false
  }
)

function onDocumentClick(event) {
  if (!event.target.closest('.appUserCard')) {
    menuOpen.value = false
  }
}

function isRouteActive(path) {
  if (path === '/') return route.path === '/'
  return route.path.startsWith(path)
}

function toggleMenu() {
  menuOpen.value = !menuOpen.value
}

function toggleRightRail() {
  rightRailOpen.value = !rightRailOpen.value
  if (typeof window !== 'undefined') {
    window.localStorage.setItem('tier-maker:right-rail-open', rightRailOpen.value ? '1' : '0')
  }
}

function goProfile() {
  menuOpen.value = false
  router.push('/profile')
}

async function logout() {
  menuOpen.value = false
  await auth.logout()
  router.push('/')
}
</script>

<template>
  <div
    class="appShell"
    :class="{
      'appShell--preview': isPreviewMode,
      'appShell--rightClosed': !rightRailOpen,
      'appShell--localRail': usesLocalRightRail,
    }"
  >
    <template v-if="isPreviewMode">
      <main class="appMain appMain--preview">
        <RouterView />
      </main>
    </template>
    <template v-else>
      <aside class="leftRail">
        <div class="leftRail__top">
          <button class="ghostIcon ghostIcon--iconOnly" type="button" aria-label="메뉴">
            <svg viewBox="0 0 24 24" aria-hidden="true"><path :d="railGlyph('menu')" /></svg>
          </button>
          <div class="brandBlock" @click="$router.push('/')">
            <div class="brandBlock__title">Tier Maker</div>
            <div class="brandBlock__sub">by zenn</div>
          </div>
        </div>

        <div v-if="auth.user" class="appUserCard">
          <button v-if="auth.user" class="appUserCard__button" type="button" @click.stop="toggleMenu">
            <img v-if="avatarUrl" :src="avatarUrl" class="appUserCard__avatar" alt="avatar" />
            <div v-else class="appUserCard__avatar appUserCard__avatar--fallback">{{ accountName[0]?.toUpperCase() || 'U' }}</div>
            <div class="appUserCard__meta">
              <div class="appUserCard__name">{{ accountName }}</div>
              <div class="appUserCard__email">{{ accountEmail }}</div>
            </div>
          </button>
          <div v-if="menuOpen" class="appUserMenu">
            <button class="appUserMenu__item" type="button" @click="goProfile">프로필</button>
            <button class="appUserMenu__item" type="button" @click="logout">로그아웃</button>
          </div>
        </div>

        <button class="searchStub" type="button" @click="$router.push('/favorites')">
          <span class="searchStub__icon">
            <svg viewBox="0 0 24 24" aria-hidden="true"><path :d="railGlyph('search')" /></svg>
          </span>
          <span>Quick Search</span>
        </button>

        <nav class="leftNav">
          <RouterLink
            v-for="item in leftNavItems"
            :key="item.key"
            :to="item.path"
            class="leftNav__item"
            :class="{ 'leftNav__item--active': isRouteActive(item.path) }"
          >
            <span class="leftNav__glyph">
              <img v-if="item.iconSrc" :src="item.iconSrc" alt="" />
              <svg v-else viewBox="0 0 24 24" aria-hidden="true"><path :d="item.icon" /></svg>
            </span>
            <span>{{ item.label }}</span>
          </RouterLink>
        </nav>

        <div class="leftRail__section">
          <div class="leftRail__sectionTitle">Favorites</div>
          <RouterLink v-for="item in favoriteLinks" :key="item.path" :to="item.path" class="favoriteLink">
            <span class="favoriteLink__dot"></span>
            <span>{{ item.label }}</span>
          </RouterLink>
        </div>

        <div class="leftRail__bottom">
          <RouterLink v-if="isAdmin" to="/admin" class="adminButton">관리자 메뉴</RouterLink>
          <RouterLink v-else-if="!auth.user" to="/login" class="adminButton">로그인</RouterLink>
        </div>
      </aside>

      <main class="appMain">
        <section class="workspace" :class="{ 'workspace--localRail': usesLocalRightRail }">
          <header class="workspaceHead">
            <div>
              <div class="workspaceHead__title">{{ routeMeta.title }}</div>
              <div class="workspaceHead__subtitle">{{ routeMeta.subtitle }}</div>
            </div>
            <div class="workspaceHead__actions">
              <button class="ghostIcon ghostIcon--workspace" type="button" :aria-pressed="rightRailOpen" @click="toggleRightRail">
                <img :src="rightRailOpen ? iconDockToRight : iconDockToLeft" alt="" />
                <span>{{ rightRailOpen ? '패널 숨기기' : '패널 보기' }}</span>
              </button>
            </div>
          </header>
          <div class="workspaceBody" :class="{ 'workspaceBody--localRail': usesLocalRightRail }">
            <RouterView />
          </div>
        </section>
      </main>

      <aside
        v-if="!usesLocalRightRail"
        class="rightRail"
        :class="{ 'rightRail--closed': !rightRailOpen }"
        :aria-hidden="!rightRailOpen"
      >
        <div class="rightRail__top">
          <button class="ghostIcon ghostIcon--iconOnly" type="button" aria-label="상태">
            <img :src="iconGridView" alt="" />
          </button>
        </div>
        <section class="contextCard">
          <div class="contextCard__label">Context</div>
          <h2 class="contextCard__title">{{ routeMeta.contextTitle }}</h2>
          <p class="contextCard__text">{{ routeMeta.contextText }}</p>
          <button class="contextCard__action" type="button" @click="routeMeta.action">
            {{ routeMeta.actionLabel }}
          </button>
        </section>
        <section class="contextCard">
          <div class="contextCard__label">Account</div>
          <div class="contextStat">
            <span class="contextStat__name">현재 사용자</span>
            <span class="contextStat__value">{{ accountName }}</span>
          </div>
          <div class="contextStat">
            <span class="contextStat__name">권한</span>
            <span class="contextStat__value">{{ isAdmin ? 'Admin' : auth.user ? 'Member' : 'Guest' }}</span>
          </div>
        </section>
        <section class="contextCard contextCard--links">
          <div class="contextCard__label">Jump</div>
          <button class="contextLink" type="button" @click="$router.push('/')">
            <svg viewBox="0 0 24 24" aria-hidden="true"><path :d="railGlyph('link')" /></svg>
            <span>게임 목록으로</span>
          </button>
          <button v-if="auth.user" class="contextLink" type="button" @click="$router.push('/me')">
            <svg viewBox="0 0 24 24" aria-hidden="true"><path :d="railGlyph('link')" /></svg>
            <span>내 티어표 열기</span>
          </button>
        </section>
      </aside>
    </template>

    <div class="toastStack" :class="{ 'toastStack--preview': isPreviewMode }" aria-live="polite" aria-atomic="true">
      <div v-for="item in toasts" :key="item.id" class="toast" :class="[`toast--${item.type}`, { 'toast--closing': item.isClosing }]">
        <div class="toast__body">
          <div class="toast__message">{{ item.message }}</div>
          <div v-if="item.count > 1" class="toast__count">x{{ item.count }}</div>
        </div>
        <button class="toast__close" type="button" @click="dismissToast(item.id)">닫기</button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.appShell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 248px minmax(0, 1fr) 320px;
  background:
    radial-gradient(circle at top left, rgba(255, 255, 255, 0.04), transparent 28%),
    linear-gradient(180deg, #1a1a1a 0%, #121212 100%);
  color: rgba(255, 255, 255, 0.92);
  transition: grid-template-columns 220ms ease;
}

.appShell--preview {
  display: block;
}

.appShell--localRail {
  grid-template-columns: 248px minmax(0, 1fr);
}

.appShell--localRail.appShell--rightClosed {
  grid-template-columns: 248px minmax(0, 1fr);
}

.leftRail,
.rightRail {
  min-height: 100vh;
  padding: 14px 12px;
  border-right: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(14, 14, 14, 0.92);
  box-sizing: border-box;
  min-width: 0;
}

.rightRail {
  border-right: 0;
  border-left: 1px solid rgba(255, 255, 255, 0.08);
  transition:
    opacity 220ms ease,
    transform 220ms ease,
    padding 220ms ease,
    border-color 220ms ease;
}

.appShell--rightClosed {
  grid-template-columns: 248px minmax(0, 1fr) 0px;
}

.appShell--rightClosed .rightRail {
  opacity: 0;
  transform: translateX(18px);
  pointer-events: none;
  overflow: hidden;
  padding-left: 0;
  padding-right: 0;
  border-left-color: transparent;
}

.leftRail__top,
.rightRail__top {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 18px;
}

.ghostIcon {
  min-width: 28px;
  height: 28px;
  padding: 0 10px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(255, 255, 255, 0.03);
  color: rgba(255, 255, 255, 0.72);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.ghostIcon svg,
.searchStub__icon svg,
.leftNav__glyph svg,
.contextLink svg {
  width: 16px;
  height: 16px;
  stroke: currentColor;
  stroke-width: 1.8;
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.ghostIcon img,
.leftNav__glyph img,
.ghostIcon--workspace img {
  width: 16px;
  height: 16px;
  display: block;
  filter: brightness(0) saturate(100%) invert(94%) sepia(6%) saturate(207%) hue-rotate(186deg) brightness(96%) contrast(92%);
}

.ghostIcon--iconOnly {
  min-width: 32px;
  width: 32px;
  padding: 0;
}

.ghostIcon--workspace {
  min-width: 132px;
  height: 38px;
  padding: 0 14px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.06);
  color: rgba(255, 255, 255, 0.88);
  font-size: 12px;
  font-weight: 800;
}

.brandBlock {
  display: grid;
  gap: 4px;
  cursor: pointer;
}

.brandBlock__title {
  font-size: 21px;
  font-weight: 900;
  letter-spacing: -0.04em;
}

.brandBlock__sub {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.56);
}

.appUserCard {
  position: relative;
  margin-bottom: 14px;
  min-height: 58px;
}

.appUserCard__button,
.appUserCard__guest {
  width: 100%;
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 10px;
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(255, 255, 255, 0.04);
  color: inherit;
  text-align: left;
  cursor: pointer;
  box-sizing: border-box;
}

.appUserCard__avatar {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  object-fit: cover;
  flex: 0 0 auto;
}

.appUserCard__avatar--fallback {
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, 0.12);
  font-weight: 900;
}

.appUserCard__meta {
  min-width: 0;
  display: grid;
  gap: 4px;
}

.appUserCard__name {
  font-size: 14px;
  font-weight: 800;
}

.appUserCard__email {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.56);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.appUserMenu {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  right: 0;
  display: grid;
  gap: 6px;
  padding: 8px;
  border-radius: 14px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(10, 10, 10, 0.98);
  z-index: 20;
}

.appUserMenu__item {
  padding: 10px 12px;
  border-radius: 10px;
  border: 0;
  background: rgba(255, 255, 255, 0.04);
  color: inherit;
  cursor: pointer;
  text-align: left;
}

.searchStub {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 12px;
  border-radius: 14px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(255, 255, 255, 0.03);
  color: rgba(255, 255, 255, 0.62);
  cursor: pointer;
  margin-bottom: 14px;
}

.searchStub__icon {
  width: 18px;
  height: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.leftNav {
  display: grid;
  gap: 8px;
}

.leftNav__item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 12px;
  border-radius: 14px;
  color: rgba(255, 255, 255, 0.76);
  text-decoration: none;
  transition: background 180ms ease, color 180ms ease, transform 180ms ease;
}

.leftNav__item--active,
.leftNav__item.router-link-active {
  background: rgba(255, 255, 255, 0.1);
  color: rgba(255, 255, 255, 0.96);
}

.leftNav__glyph {
  width: 28px;
  height: 28px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, 0.06);
  flex: 0 0 auto;
}

.leftRail__section {
  margin-top: 24px;
  display: grid;
  gap: 10px;
}

.leftRail__sectionTitle {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.4);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.favoriteLink {
  display: flex;
  gap: 10px;
  align-items: center;
  color: rgba(255, 255, 255, 0.7);
  text-decoration: none;
  font-size: 14px;
  padding: 4px 0;
}

.favoriteLink__dot {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.86);
}

.leftRail__bottom {
  margin-top: auto;
  padding-top: 20px;
}

.adminButton {
  width: 100%;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  padding: 12px 14px;
  border-radius: 14px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.05);
  color: rgba(255, 255, 255, 0.92);
  text-decoration: none;
  box-sizing: border-box;
  font-weight: 800;
}

.leftRail {
  display: flex;
  flex-direction: column;
}

.appMain {
  min-width: 0;
  padding: 14px 18px 22px;
  box-sizing: border-box;
}

.appMain--preview {
  padding: 0;
}

.workspace {
  display: grid;
  gap: 16px;
}

.workspace--localRail {
  gap: 12px;
}

.workspaceHead {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}

.workspaceHead__actions {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

.workspaceHead__title {
  font-size: 30px;
  font-weight: 900;
  letter-spacing: -0.04em;
}

.workspaceHead__subtitle {
  margin-top: 6px;
  color: rgba(255, 255, 255, 0.58);
  font-size: 13px;
}

.workspaceBody {
  min-height: calc(100vh - 110px);
  padding: 20px;
  border-radius: 26px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: linear-gradient(180deg, #2d2d2d 0%, #2a2a2a 100%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
}

.workspaceBody--localRail {
  min-height: calc(100vh - 92px);
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
}

.rightRail {
  display: grid;
  align-content: start;
  gap: 18px;
}

.contextCard {
  display: grid;
  gap: 12px;
  padding: 16px;
  border-radius: 18px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(255, 255, 255, 0.03);
}

.contextCard__label {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.42);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.contextCard__title {
  margin: 0;
  font-size: 22px;
  line-height: 1.2;
}

.contextCard__text {
  margin: 0;
  font-size: 14px;
  line-height: 1.6;
  color: rgba(255, 255, 255, 0.66);
}

.contextCard__action {
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 0;
  background: #4b7fe9;
  color: #fff;
  font-weight: 800;
  cursor: pointer;
}

.contextCard--links {
  gap: 10px;
}

.contextLink {
  width: 100%;
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 11px 12px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(255, 255, 255, 0.03);
  color: rgba(255, 255, 255, 0.86);
  cursor: pointer;
}

.contextStat {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
}

.contextStat__name {
  color: rgba(255, 255, 255, 0.56);
  font-size: 13px;
}

.contextStat__value {
  font-size: 14px;
  font-weight: 700;
}

.toastStack {
  position: fixed;
  top: 18px;
  right: 20px;
  z-index: 40;
  display: grid;
  gap: 10px;
  width: min(360px, calc(100vw - 24px));
}

.toastStack--preview {
  top: 12px;
}

.toast {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  justify-content: space-between;
  padding: 12px 14px;
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(11, 18, 32, 0.94);
  backdrop-filter: blur(12px);
  box-shadow: 0 14px 30px rgba(0, 0, 0, 0.28);
  opacity: 1;
  transform: translateY(0);
  transition: opacity 220ms ease, transform 220ms ease;
}

.toast--closing {
  opacity: 0;
  transform: translateY(-6px);
}

.toast--success {
  border-color: rgba(52, 211, 153, 0.38);
}

.toast--error {
  border-color: rgba(239, 68, 68, 0.34);
}

.toast--info {
  border-color: rgba(96, 165, 250, 0.34);
}

.toast__message {
  line-height: 1.5;
  font-size: 14px;
}

.toast__count {
  margin-top: 6px;
  font-size: 12px;
  color: rgba(255, 255, 255, 0.56);
}

.toast__close {
  border: 0;
  background: transparent;
  color: rgba(255, 255, 255, 0.68);
  cursor: pointer;
  font-size: 12px;
}

@media (max-width: 1280px) {
  .appShell {
    grid-template-columns: 220px minmax(0, 1fr);
  }

  .appShell--localRail {
    grid-template-columns: 220px minmax(0, 1fr);
  }

  .appShell--localRail.appShell--rightClosed {
    grid-template-columns: 220px minmax(0, 1fr);
  }

  .rightRail {
    display: none;
  }
}

@media (max-width: 860px) {
  .appShell {
    grid-template-columns: 1fr;
  }

  .leftRail {
    min-height: auto;
    border-right: 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  }

  .workspaceBody {
    padding: 14px;
    border-radius: 20px;
  }

  .workspaceBody--localRail {
    padding: 0;
    border-radius: 0;
  }

  .workspaceHead__title {
    font-size: 26px;
  }
}
</style>
