<script setup>
import { computed, onBeforeUnmount, onMounted, 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 iconFavorite from './assets/icons/favorite.svg'
import iconLists from './assets/icons/lists.svg'
import iconSearch from './assets/icons/search.svg'
import iconSettings from './assets/icons/settings.svg'
import RightRailAd from './components/RightRailAd.vue'
import SvgIcon from './components/SvgIcon.vue'

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

const leftRailCollapsed = ref(false)
const rightRailOpen = ref(true)
const searchQuery = ref('')
const searchPlaceholder = computed(() => (route.name === 'home' ? '게임 템플릿 검색' : '전체 티어표 검색'))
const isCollapsedSearchOpen = ref(false)
const viewportWidth = ref(typeof window !== 'undefined' ? window.innerWidth : 1440)
provide('rightRailOpen', rightRailOpen)
provide('localRightRailTarget', '#local-right-rail-root')

const isAdmin = computed(() => !!auth.user?.isAdmin)
const isPreviewMode = computed(() => route.query.preview === '1')
const usesLocalRightRail = computed(() => ['editEditor', 'newEditor', 'admin'].includes(String(route.name || '')))
const isRightRailOverlay = computed(() => viewportWidth.value <= 1200)
const isMobileLayout = computed(() => viewportWidth.value <= 860)
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 shellStyle = computed(() => ({
  '--left-rail-width': leftRailCollapsed.value ? '76px' : '248px',
  '--right-rail-width': !isRightRailOverlay.value && rightRailOpen.value ? '320px' : '0px',
}))
const leftNavItems = computed(() => {
  const items = [
    { key: 'home', label: 'Games', path: '/', iconSrc: iconGridView },
    { key: 'me', label: 'My Lists', path: '/me', iconSrc: iconLists, requiresAuth: true },
    { key: 'favorites', label: 'Favorites', path: '/favorites', iconSrc: iconFavorite, requiresAuth: true },
    { key: 'profile', label: 'Settings', path: '/profile', iconSrc: iconSettings, requiresAuth: true },
  ]
  return items.filter((item) => !item.requiresAuth || auth.user)
})
const showRightRailAction = computed(() => false)
const showGameHubViewToggle = computed(() => route.name === 'gameHub')
const gameHubViewMode = computed(() => (route.query.view === 'list' ? 'list' : 'grid'))
const leftBottomPrimaryAction = computed(() => {
  if (route.name === 'home' && auth.user) {
    return { label: '커스텀 티어표 만들기', to: '/editor/freeform/new' }
  }
  if (route.name === 'gameHub') {
    const target = `/editor/${route.params.gameId}/new`
    return { label: '새 티어표 만들기', to: auth.user ? target : `/login?redirect=${target}` }
  }
  return null
})

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'),
    }
  }
  if (route.name === 'search') {
    return {
      title: 'Search',
      subtitle: '전체 공개 티어표 검색 결과',
      contextTitle: '검색',
      contextText: '제목, 작성자 기준으로 공개 티어표를 전체 검색할 수 있어요.',
      actionLabel: '홈으로',
      action: () => router.push('/'),
    }
  }
  return {
    title: 'Tier Maker',
    subtitle: 'by zenn',
    contextTitle: 'Workspace',
    contextText: '현재 화면에 맞는 도구와 안내를 여기에 배치할 수 있습니다.',
    actionLabel: '홈으로',
    action: () => router.push('/'),
  }
})

function syncViewportWidth() {
  if (typeof window === 'undefined') return
  viewportWidth.value = window.innerWidth
}

onMounted(async () => {
  await auth.refresh()
  if (typeof window !== 'undefined') {
    syncViewportWidth()
    window.addEventListener('resize', syncViewportWidth)
    window.addEventListener('keydown', handleGlobalKeydown)
    const leftSaved = window.localStorage.getItem('tier-maker:left-rail-collapsed')
    if (leftSaved === '1') leftRailCollapsed.value = true
    const saved = window.localStorage.getItem('tier-maker:right-rail-open')
    if (saved === '0') rightRailOpen.value = false
  }
  searchQuery.value = typeof route.query.q === 'string' ? route.query.q : ''
})

function handleGlobalKeydown(event) {
  if (event.key === 'Escape' && isCollapsedSearchOpen.value) {
    closeCollapsedSearch()
  }
}

onBeforeUnmount(() => {
  if (typeof window !== 'undefined') {
    window.removeEventListener('resize', syncViewportWidth)
    window.removeEventListener('keydown', handleGlobalKeydown)
  }
})

watch(
  () => route.fullPath,
  () => {
    searchQuery.value = typeof route.query.q === 'string' ? route.query.q : ''
    isCollapsedSearchOpen.value = false
  }
)

watch(
  isMobileLayout,
  (mobile) => {
    if (mobile) leftRailCollapsed.value = false
  },
  { immediate: true }
)

watch(
  usesLocalRightRail,
  (needed) => {
    if (!needed || rightRailOpen.value) return
    rightRailOpen.value = true
    if (typeof window !== 'undefined') {
      window.localStorage.setItem('tier-maker:right-rail-open', '1')
    }
  },
  { immediate: true }
)

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

function toggleLeftRail() {
  if (isMobileLayout.value) return
  leftRailCollapsed.value = !leftRailCollapsed.value
  if (typeof window !== 'undefined') {
    window.localStorage.setItem('tier-maker:left-rail-collapsed', leftRailCollapsed.value ? '1' : '0')
  }
}

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

function setGameHubViewMode(mode) {
  if (route.name !== 'gameHub') return
  const nextQuery = { ...route.query }
  if (mode === 'list') nextQuery.view = 'list'
  else delete nextQuery.view
  router.replace({ path: route.path, query: nextQuery })
}

function openCollapsedSearch() {
  if (!leftRailCollapsed.value || isMobileLayout.value) return
  isCollapsedSearchOpen.value = true
}

function closeCollapsedSearch() {
  isCollapsedSearchOpen.value = false
}

function handleLeftRailSearch() {
  if (leftRailCollapsed.value && !isMobileLayout.value) {
    openCollapsedSearch()
    return
  }
  submitGlobalSearch()
}

function submitGlobalSearch() {
  const query = (searchQuery.value || '').trim()
  isCollapsedSearchOpen.value = false
  if (route.name === 'home') {
    router.push(query ? `/?q=${encodeURIComponent(query)}` : '/')
    return
  }
  router.push(query ? `/search?q=${encodeURIComponent(query)}` : '/search')
}


</script>

<template>
  <div
    class="appShell"
    :class="{
      'appShell--preview': isPreviewMode,
      'appShell--leftCollapsed': leftRailCollapsed,
      'appShell--rightClosed': !rightRailOpen,
      'appShell--rightOverlay': isRightRailOverlay,
    }"
    :style="shellStyle"
  >
    <template v-if="isPreviewMode">
      <main class="appMain appMain--preview">
        <RouterView />
      </main>
    </template>
    <template v-else>
      <aside class="leftRail">
        <div class="leftRail__top railHeader">
          <button v-if="!isMobileLayout" class="ghostIcon ghostIcon--iconOnly" type="button" aria-label="왼쪽 패널 토글" @click="toggleLeftRail">
            <SvgIcon :src="iconDockToRight" :size="24" />
          </button>
        </div>

        <div class="leftRail__body">
        <div class="leftRail__content">
        <div v-if="auth.user" class="appUserCard">
          <div class="appUserCard__button">
            <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>
          </div>
        </div>

        <form class="searchStub" @submit.prevent="submitGlobalSearch">
          <button class="searchStub__iconButton" type="button" :aria-label="leftRailCollapsed ? '검색 열기' : '검색'" @click="handleLeftRailSearch">
            <span class="searchStub__icon">
              <SvgIcon :src="iconSearch" :size="24" />
            </span>
          </button>
          <input v-model="searchQuery" class="searchStub__input" type="search" :placeholder="leftRailCollapsed ? '' : searchPlaceholder" />
        </form>

        <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) }"
            :title="leftRailCollapsed ? item.label : ''"
            :aria-label="leftRailCollapsed ? item.label : undefined"
          >
            <span class="leftNav__glyph">
              <SvgIcon v-if="item.iconSrc" :src="item.iconSrc" :size="24" />
              <svg v-else viewBox="0 0 24 24" aria-hidden="true"><path :d="item.icon" /></svg>
            </span>
            <span class="leftNav__label">{{ item.label }}</span>
          </RouterLink>
        </nav>

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

      <main class="appMain">
        <section class="workspace" :class="{ 'workspace--localRail': usesLocalRightRail }">
          <header class="workspaceHead railHeader">
            <div class="workspaceHead__brand" @click="$router.push('/')">
              <span class="workspaceHead__brandTitle">Tier Maker</span>
              <span class="workspaceHead__brandSub">by zenn</span>
            </div>
            <div class="workspaceHead__actions">
              <div v-if="showGameHubViewToggle" class="viewToggle" role="group" aria-label="티어표 보기 방식">
                <button class="ghostIcon ghostIcon--iconOnly" :class="{ 'ghostIcon--active': gameHubViewMode === 'grid' }" type="button" aria-label="그리드 보기" @click="setGameHubViewMode('grid')">
                  <SvgIcon :src="iconGridView" :size="24" />
                </button>
                <button class="ghostIcon ghostIcon--iconOnly" :class="{ 'ghostIcon--active': gameHubViewMode === 'list' }" type="button" aria-label="리스트 보기" @click="setGameHubViewMode('list')">
                  <SvgIcon :src="iconLists" :size="24" />
                </button>
              </div>
              <button v-if="!rightRailOpen" class="ghostIcon ghostIcon--iconOnly" type="button" aria-label="패널 열기" @click="toggleRightRail">
                <SvgIcon :src="iconDockToLeft" :size="24" />
              </button>
            </div>
          </header>
          <div class="workspaceBody" :class="{ 'workspaceBody--localRail': usesLocalRightRail }">
            <RouterView />
          </div>
        </section>
      </main>

      <div v-if="isCollapsedSearchOpen" class="collapsedSearchModal" role="dialog" aria-modal="true" :aria-label="searchPlaceholder" @click.self="closeCollapsedSearch">
        <form class="collapsedSearchBar" @submit.prevent="submitGlobalSearch">
          <span class="collapsedSearchBar__icon">
            <SvgIcon :src="iconSearch" :size="24" />
          </span>
          <input v-model="searchQuery" class="collapsedSearchBar__input" type="search" :placeholder="searchPlaceholder" autofocus />
        </form>
      </div>

      <button v-if="rightRailOpen && isRightRailOverlay" class="rightRailBackdrop" type="button" aria-label="오른쪽 패널 닫기" @click="toggleRightRail"></button>

      <aside class="rightRail" :class="{ 'rightRail--closed': !rightRailOpen, 'rightRail--overlay': isRightRailOverlay }" :aria-hidden="!rightRailOpen">
        <div class="rightRail__top railHeader">
          <button v-if="rightRailOpen" class="ghostIcon ghostIcon--iconOnly" type="button" aria-label="패널 닫기" @click="toggleRightRail">
            <SvgIcon :src="iconDockToLeft" :size="24" />
          </button>
        </div>
        <div class="rightRail__body">
          <div class="rightRail__content">
            <div v-if="usesLocalRightRail" id="local-right-rail-root" class="localRightRailRoot"></div>
            <template v-else>
              <RightRailAd />
            </template>
          </div>
          <div class="rightRail__bottom">
            <template v-if="showRightRailAction">
              <section class="rightRailAction">
                <button class="rightRailAction__button" type="button" @click="routeMeta.action">
                  {{ routeMeta.actionLabel }}
                </button>
              </section>
            </template>
          </div>
        </div>
      </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: 100dvh;
  display: grid;
  grid-template-columns: var(--left-rail-width, 248px) minmax(0, 1fr) var(--right-rail-width, 320px);
  background: rgba(14, 14, 14, 0.96);
  color: rgba(255, 255, 255, 0.92);
  transition: grid-template-columns 220ms ease;
}

.appShell--preview {
  display: block;
}

.leftRail,
.rightRail {
  min-height: 100dvh;
  border-right: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(14, 14, 14, 0.92);
  box-sizing: border-box;
  min-width: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.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 .rightRail {
  opacity: 0;
  transform: translateX(18px);
  pointer-events: none;
  overflow: hidden;
  padding-left: 0;
  padding-right: 0;
  border-left-color: transparent;
}

.railHeader {
  height: 56px;
  min-height: 56px;
  display: flex;
  align-items: center;
  padding: 0 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  box-sizing: border-box;
}


.leftRail__top,
.rightRail__top {
  gap: 12px;
}

.leftRail__top {
  justify-content: flex-start;
}

.rightRail__top {
  justify-content: flex-end;
}

.leftRail__body,
.rightRail__body {
  flex: 1;
  min-height: 0;
  padding: 14px 12px;
  box-sizing: border-box;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.leftRail__body {
  max-height: calc(100dvh - 56px);
}

.rightRail__body {
  max-height: none;
}

.leftRail__content,
.rightRail__content {
  flex: 1;
  min-height: 0;
  overflow: auto;
}

.rightRail__body {
  overflow-y: auto;
  overflow-x: hidden;
}

.rightRail__content {
  flex: 0 0 auto;
  overflow: visible;
}

.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: 28px;
  height: 28px;
  stroke: currentColor;
  stroke-width: 1.8;
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.ghostIcon img,
.leftNav__glyph img,
.searchStub__icon img {
  width: 24px;
  height: 24px;
  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;
  border: 0;
  background: transparent;
  box-shadow: none;
}

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

.appUserCard__button,
.appUserCard__guest {
  width: 100%;
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 10px;
  border-radius: 16px;
  color: inherit;
  text-align: left;
  cursor: default;
  box-sizing: border-box;
  transition: padding 220ms ease, justify-content 220ms ease;
}

.appUserCard__avatar {
  width: 42px;
  height: 42px;
  border-radius: 999px;
  object-fit: cover;
  flex: 0 0 auto;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.08);
}

.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;
  max-width: 180px;
  overflow: hidden;
  transition: opacity 180ms ease, max-width 220ms ease, transform 220ms ease;
}

.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;
}

.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);
  margin-bottom: 14px;
  box-sizing: border-box;
  transition: padding 220ms ease, justify-content 220ms ease;
}

.searchStub__input {
  min-width: 0;
  flex: 1;
  max-width: 100%;
  border: 0;
  background: transparent;
  color: rgba(255, 255, 255, 0.92);
  outline: none;
  font: inherit;
  overflow: hidden;
  transition: opacity 180ms ease, max-width 220ms ease, transform 220ms ease;
}

.searchStub__input::placeholder {
  color: rgba(255, 255, 255, 0.42);
}

.searchStub__iconButton {
  border: 0;
  padding: 0;
  background: transparent;
  color: inherit;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.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__label {
  min-width: 0;
  max-width: 140px;
  white-space: nowrap;
  overflow: hidden;
  transition: opacity 180ms ease, max-width 220ms ease, transform 220ms 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;
}

.appShell--leftCollapsed .leftRail__top {
  justify-content: center;
}

.appShell--leftCollapsed .leftRail__body {
  padding-left: 10px;
  padding-right: 10px;
}

.appShell--leftCollapsed .appUserCard {
  margin-bottom: 10px;
}

.appShell--leftCollapsed .appUserCard__button,
.appShell--leftCollapsed .appUserCard__guest {
  justify-content: center;
}

.appShell--leftCollapsed .appUserCard__meta,
.appShell--leftCollapsed .leftNav__label,
.appShell--leftCollapsed .searchStub__input {
  opacity: 0;
  max-width: 0;
  transform: translateX(-4px);
  pointer-events: none;
}

.appShell--leftCollapsed .appUserCard__avatar {
  width: 44px;
  height: 44px;
}

.appShell--leftCollapsed .searchStub {
  justify-content: center;
}

.appShell--leftCollapsed .searchStub__iconButton {
  width: auto;
}

.appShell--leftCollapsed .leftNav {
  gap: 10px;
}

.appShell--leftCollapsed .leftNav__item {
  justify-content: center;
}

.appShell--leftCollapsed .leftRail__bottom {
  display: none;
}

.appShell--leftCollapsed .leftRail__content {
  overflow: hidden;
}

.leftRail__bottom {
  display: grid;
  gap: 10px;
  justify-content: stretch;
  align-items: flex-end;
  padding-top: 12px;
}

.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;
}

.appMain {
  min-width: 0;
  min-height: 0;
  box-sizing: border-box;
  background: rgba(18, 18, 18, 0.98);
  border-left: 1px solid rgba(255, 255, 255, 0.08);
  border-right: 1px solid rgba(255, 255, 255, 0.08);
}

.appMain--preview {
  padding: 0;
}

.workspace {
  display: grid;
  grid-template-rows: 56px minmax(0, 1fr);
  gap: 0;
  min-height: 100dvh;
}

.workspace--localRail {
  gap: 0;
}

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

.workspaceHead__brand {
  display: inline-flex;
  align-items: baseline;
  gap: 8px;
  cursor: pointer;
}

.workspaceHead__brandTitle {
  font-size: 28px;
  font-weight: 900;
  letter-spacing: -0.05em;
}

.workspaceHead__brandSub {
  font-size: 13px;
  font-weight: 700;
  color: rgba(255, 255, 255, 0.58);
}

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

.viewToggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.04);
}

.viewToggle .ghostIcon--iconOnly {
  width: 36px;
  height: 36px;
  min-width: 36px;
  border-radius: 10px;
}

.ghostIcon--active {
  background: rgba(255, 255, 255, 0.08);
}

.workspaceBody {
  min-height: 0;
  padding: 18px 18px 32px;
  border: 0;
  border-radius: 0;
  background: rgba(24, 24, 24, 0.92);
  box-shadow: none;
  margin: 0;
}

.workspaceBody--localRail {
  min-height: 0;
  padding: 18px 18px 32px;
  border: 0;
  border-radius: 0;
  background: rgba(24, 24, 24, 0.92);
  box-shadow: none;
  margin: 0;
}

.rightRail {
  gap: 0;
}

.rightRailAction {
  display: grid;
  width: 100%;
}

.rightRail__bottom {
  display: flex;
  align-items: flex-end;
  padding-top: 12px;
}

.rightRailAction__button {
  width: 100%;
  padding: 12px 14px;
  border-radius: 14px;
  border: 1px solid rgba(77, 127, 233, 0.96);
  background: rgba(77, 127, 233, 0.88);
  color: #fff;
  font-weight: 800;
  cursor: pointer;
}

.rightRailBackdrop {
  display: none;
}

.collapsedSearchModal {
  position: fixed;
  inset: 0;
  z-index: 35;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 88px 20px 20px;
  background: rgba(0, 0, 0, 0.44);
  backdrop-filter: blur(6px);
}

.collapsedSearchBar {
  width: min(520px, calc(100vw - 32px));
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 18px 22px;
  border-radius: 24px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(26, 26, 26, 0.96);
  box-shadow: 0 28px 60px rgba(0, 0, 0, 0.34);
}

.collapsedSearchBar__icon {
  width: 28px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
}

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

.collapsedSearchBar__input {
  min-width: 0;
  flex: 1;
  border: 0;
  background: transparent;
  color: rgba(255, 255, 255, 0.92);
  font-size: 18px;
  font-weight: 700;
  outline: none;
}

.collapsedSearchBar__input::placeholder {
  color: rgba(255, 255, 255, 0.46);
}

.localRightRailRoot {
  min-height: auto;
  display: grid;
  align-content: start;
  gap: 14px;
}

.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: 1200px) {
  .appShell {
    grid-template-columns: var(--left-rail-width, 248px) minmax(0, 1fr);
  }

  .rightRailBackdrop {
    position: fixed;
    inset: 0;
    display: block;
    border: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 29;
  }

  .rightRail--overlay {
    position: fixed;
    top: 0;
    right: 0;
    width: min(360px, calc(100vw - 20px));
    height: 100dvh;
    z-index: 30;
    border-left: 1px solid rgba(255, 255, 255, 0.08);
    background: rgba(14, 14, 14, 0.96);
    box-shadow: -18px 0 36px rgba(0, 0, 0, 0.34);
  }


  .appShell--rightClosed .rightRail--overlay {
    transform: translateX(calc(100% + 24px));
    opacity: 0;
    pointer-events: none;
  }
}

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

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

  .leftRail__top {
    display: none;
  }

  .leftRail__body {
    max-height: none;
    padding: 12px 14px;
  }

  .appMain {
    min-height: auto;
    border-left: 0;
    border-right: 0;
  }

  .workspace,
  .workspaceBody,
  .workspaceBody--localRail {
    min-height: 0;
    height: auto;
  }

  .leftRail__content {
    overflow: visible;
  }

  .appShell--leftCollapsed .leftRail__top {
    display: none;
  }

  .appShell--leftCollapsed .appUserCard__meta,
  .appShell--leftCollapsed .leftNav__label,
  .appShell--leftCollapsed .searchStub__input {
    display: revert;
  }

  .appShell--leftCollapsed .appUserCard__button,
  .appShell--leftCollapsed .appUserCard__guest,
  .appShell--leftCollapsed .searchStub,
  .appShell--leftCollapsed .leftNav__item {
    justify-content: flex-start;
  }

  .appShell--leftCollapsed .appUserCard__button,
  .appShell--leftCollapsed .appUserCard__guest {
    padding: 10px;
  }

  .appShell--leftCollapsed .searchStub {
    padding: 11px 12px;
  }

  .appShell--leftCollapsed .leftNav__item {
    padding: 11px 12px;
  }

  .appShell--leftCollapsed .searchStub__iconButton {
    width: auto;
  }

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

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

  .collapsedSearchModal {
    padding: 72px 16px 16px;
  }

  .collapsedSearchBar {
    width: min(100%, calc(100vw - 24px));
    padding: 16px 18px;
    border-radius: 20px;
  }

  .collapsedSearchBar__input {
    font-size: 20px;
  }
}
</style>
