<template>
	<div v-if="instance" :class="{ 'flex h-full flex-col': isFixedRender }">
		<div
			:class="['p-6 pr-2 pb-4', { 'shrink-0': isFixedRender }]"
			@contextmenu.prevent.stop="(event) => handleRightClick(event)"
		>
			<ExportModal ref="exportModal" :instance="instance" />
			<InstanceSettingsModal
				:key="instance.id"
				ref="settingsModal"
				:instance="instance"
				:offline="offline"
				@unlinked="fetchInstance"
			/>
			<UpdateToPlayModal ref="updateToPlayModal" :instance="instance" />
			<PageHeader
				:header="instance.name"
				:leading="instanceHeaderLeading"
				:metadata="instanceHeaderMetadata"
				:actions="instanceHeaderActions"
			>
				<template #metadata-server-details>
					<div class="flex items-center flex-wrap gap-2">
						<template v-if="loadingServerPing">
							<ServerOnlinePlayers
								v-if="playersOnline !== undefined"
								:online="playersOnline"
								:status-online="statusOnline"
								hide-label
							/>
							<ServerRecentPlays :recent-plays="recentPlays ?? 0" hide-label />
							<div
								v-if="
									(playersOnline !== undefined || recentPlays !== undefined) &&
									(minecraftServer?.region || ping)
								"
								class="w-1.5 h-1.5 rounded-full bg-surface-5"
							></div>
							<ServerPing v-if="ping" :ping="ping" />
						</template>

						<ServerRegion v-if="minecraftServer?.region" :region="minecraftServer?.region" />

						<div
							v-if="minecraftServer?.region || ping"
							class="w-1.5 h-1.5 rounded-full bg-surface-5"
						></div>

						<div v-if="linkedProjectV3" class="flex gap-1.5 items-center font-medium text-primary">
							Linked to
							<Avatar
								:src="linkedProjectV3.icon_url"
								:alt="linkedProjectV3.name"
								:tint-by="instance.id"
								size="24px"
							/>
							<router-link
								:to="`/project/${linkedProjectV3.slug ?? linkedProjectV3.id}`"
								class="hover:underline text-primary truncate"
							>
								{{ linkedProjectV3.name }}
							</router-link>
						</div>
					</div>
				</template>
			</PageHeader>
		</div>
		<div :class="['px-6', { 'shrink-0': isFixedRender }]">
			<NavTabs :links="tabs" />
		</div>
		<div :class="['p-6 pt-4', { 'min-h-0 flex-1 overflow-y-auto': isFixedRender }]">
			<RouterView v-slot="{ Component }" :key="instance.id" :route="displayedInstanceRoute">
				<template v-if="Component">
					<Suspense
						:key="instance.id"
						@pending="subpagePending = true"
						@resolve="subpagePending = false"
					>
						<component
							:is="Component"
							:instance="instance"
							:options="options"
							:offline="offline"
							:playing="playing"
							:installed="instance.install_stage !== 'installed'"
							:is-server-instance="isServerInstance"
							:open-settings="() => settingsModal?.show(1)"
							v-bind="contentSubpageProps"
							@play="updatePlayState"
							@stop="() => stopInstance('InstanceSubpage')"
						></component>
					</Suspense>
				</template>
			</RouterView>
		</div>
		<ContextMenu ref="options" @option-clicked="handleOptionsClick">
			<template #play> <PlayIcon /> Play </template>
			<template #stop> <StopCircleIcon /> Stop </template>
			<template #add_content> <PlusIcon /> Add content </template>
			<template #edit> <EditIcon /> Edit </template>
			<template #copy_path> <ClipboardCopyIcon /> Copy path </template>
			<template #open_folder> <FolderOpenIcon /> Open folder </template>
			<template #copy_link> <ClipboardCopyIcon /> Copy link </template>
			<template #open_link> <GlobeIcon /> Open in Modrinth <ExternalIcon /> </template>
			<template #copy_names><EditIcon />Copy names</template>
			<template #copy_slugs><HashIcon />Copy slugs</template>
			<template #copy_links><GlobeIcon />Copy links</template>
			<template #toggle><EditIcon />Toggle selected</template>
			<template #disable><XIcon />Disable selected</template>
			<template #enable><CheckCircleIcon />Enable selected</template>
			<template #hide_show><EyeIcon />Show/Hide unselected</template>
			<template #update_all
				><UpdatedIcon />Update {{ selected.length > 0 ? 'selected' : 'all' }}</template
			>
			<template #filter_update><UpdatedIcon />Select Updatable</template>
		</ContextMenu>
	</div>
</template>
<script setup lang="ts">
import type { Labrinth } from '@modrinth/api-client'
import {
	BoxesIcon,
	CheckCircleIcon,
	ClipboardCopyIcon,
	DownloadIcon,
	EditIcon,
	ExternalIcon,
	EyeIcon,
	FolderOpenIcon,
	GlobeIcon,
	HashIcon,
	MoreVerticalIcon,
	PackageIcon,
	PlayIcon,
	PlusIcon,
	SettingsIcon,
	StopCircleIcon,
	TagCategoryGamepad2Icon as Gamepad2Icon,
	TerminalSquareIcon,
	TimerIcon,
	UpdatedIcon,
	XIcon,
} from '@modrinth/assets'
import {
	Avatar,
	formatLoaderLabel,
	injectNotificationManager,
	LoaderIcon as ServerLoaderIcon,
	NavTabs,
	PageHeader,
	ServerOnlinePlayers,
	ServerPing,
	ServerRecentPlays,
	ServerRegion,
	useLoadingBarToken,
} from '@modrinth/ui'
import type { Loaders } from '@modrinth/utils'
import { useQueryClient } from '@tanstack/vue-query'
import { convertFileSrc } from '@tauri-apps/api/core'
import dayjs from 'dayjs'
import duration from 'dayjs/plugin/duration'
import relativeTime from 'dayjs/plugin/relativeTime'
import { computed, onUnmounted, ref, shallowRef, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'

import ContextMenu from '@/components/ui/ContextMenu.vue'
import ExportModal from '@/components/ui/ExportModal.vue'
import InstanceSettingsModal from '@/components/ui/modal/InstanceSettingsModal.vue'
import UpdateToPlayModal from '@/components/ui/modal/UpdateToPlayModal.vue'
import { useInstanceConsole } from '@/composables/useInstanceConsole'
import { trackEvent } from '@/helpers/analytics'
import { get_project_v3 } from '@/helpers/cache.js'
import { instance_listener, process_listener } from '@/helpers/events'
import { install_existing_instance, install_pack_to_existing_instance } from '@/helpers/install'
import { get, get_full_path, kill, run } from '@/helpers/instance'
import { type InstanceContentData, loadInstanceContentData } from '@/helpers/instance-content'
import { get_by_instance_id } from '@/helpers/process'
import type { GameInstance } from '@/helpers/types'
import { createInstanceShortcut, showInstanceInFolder } from '@/helpers/utils.js'
import { get_server_status, refreshWorlds } from '@/helpers/worlds'
import { injectServerInstall } from '@/providers/server-install'
import { handleSevereError } from '@/store/error.js'
import { useBreadcrumbs, useTheming } from '@/store/state'

dayjs.extend(duration)
dayjs.extend(relativeTime)

const { addNotification, handleError } = injectNotificationManager()
const { playServerProject } = injectServerInstall()
const queryClient = useQueryClient()
const route = useRoute()

const router = useRouter()
const displayedInstanceRoute = shallowRef(router.currentRoute.value)
const breadcrumbs = useBreadcrumbs()
const themeStore = useTheming()
const showInstancePlayTime = computed(() => themeStore.getFeatureFlag('show_instance_play_time'))
const contentSubpageRouteNames = new Set(['Mods', 'ModsFilter'])

const offline = ref(!navigator.onLine)
window.addEventListener('offline', () => {
	offline.value = true
})
window.addEventListener('online', () => {
	offline.value = false
})

const instance = ref<GameInstance>()
const preloadedContent = ref<InstanceContentData | null>(null)
const playing = ref(false)
const loading = ref(false)
const subpagePending = ref(false)
const stopping = ref(false)
const exportModal = ref<InstanceType<typeof ExportModal>>()
const updateToPlayModal = ref<InstanceType<typeof UpdateToPlayModal>>()

useLoadingBarToken(subpagePending)

const isServerInstance = ref(false)
const linkedProjectV3 = ref<Labrinth.Projects.v3.Project>()
const selected = ref<unknown[]>([])

const minecraftServer = computed(() => linkedProjectV3.value?.minecraft_server)
const javaServerPingData = computed(() => linkedProjectV3.value?.minecraft_java_server?.ping?.data)
const statusOnline = computed(() => !!javaServerPingData.value)
const recentPlays = computed(
	() => linkedProjectV3.value?.minecraft_java_server?.verified_plays_2w ?? undefined,
)
const playersOnline = ref<number | undefined>(undefined)
const ping = ref<number | undefined>(undefined)
const loadingServerPing = ref(false)

watch(
	() => router.currentRoute.value,
	(nextRoute) => {
		if (nextRoute.path.startsWith('/instance')) {
			displayedInstanceRoute.value = nextRoute
		}
	},
	{ immediate: true },
)

function isContentSubpageRoute(routeName = displayedInstanceRoute.value.name) {
	return typeof routeName === 'string' && contentSubpageRouteNames.has(routeName)
}

async function fetchInstance() {
	isServerInstance.value = false
	linkedProjectV3.value = undefined
	preloadedContent.value = null
	ping.value = undefined
	playersOnline.value = undefined
	loadingServerPing.value = false

	const nextInstance = await get(route.params.id as string).catch(handleError)
	let nextLinkedProjectV3: Labrinth.Projects.v3.Project | undefined
	let nextIsServerInstance = false

	const contentPreloadPromise =
		nextInstance && isContentSubpageRoute()
			? loadInstanceContentData(nextInstance.id, undefined, handleError)
			: Promise.resolve(null)

	if (!offline.value && nextInstance?.link && nextInstance.link.project_id) {
		try {
			nextLinkedProjectV3 = await get_project_v3(nextInstance.link.project_id, 'must_revalidate')

			if (nextLinkedProjectV3?.minecraft_server != null) {
				nextIsServerInstance = true
			}
		} catch (error) {
			handleError(error as Error)
		}
	}

	const nextPreloadedContent = await contentPreloadPromise

	instance.value = nextInstance ?? undefined
	linkedProjectV3.value = nextLinkedProjectV3
	isServerInstance.value = nextIsServerInstance
	preloadedContent.value = nextPreloadedContent

	fetchDeferredData()

	if (nextInstance) {
		queryClient.prefetchQuery({
			queryKey: ['worlds', nextInstance.id],
			queryFn: () => refreshWorlds(nextInstance.id),
			staleTime: 30_000,
		})
	}
}

function fetchDeferredData() {
	const serverAddress = linkedProjectV3.value?.minecraft_java_server?.address
	if (isServerInstance.value && serverAddress) {
		get_server_status(serverAddress)
			.then((status) => {
				playersOnline.value = status.players?.online
				ping.value = status.ping
			})
			.catch((error) => {
				console.error(`Failed to fetch server status for ${serverAddress}:`, error)
			})
			.finally(() => {
				loadingServerPing.value = true
			})
	} else {
		loadingServerPing.value = true
	}

	updatePlayState()
}

async function updatePlayState() {
	if (!route.params.id) return
	const runningProcesses = await get_by_instance_id(route.params.id as string).catch(handleError)

	playing.value = Array.isArray(runningProcesses) && runningProcesses.length > 0
}

await fetchInstance()
watch(
	() => route.params.id,
	async () => {
		if (route.params.id && route.path.startsWith('/instance')) {
			await fetchInstance()
		}
	},
)

const basePath = computed(
	() => `/instance/${encodeURIComponent(displayedInstanceRoute.value.params.id as string)}`,
)

/**
 * Per-route layout mode.
 * - `'scroll'` (default): the whole instance page scrolls inside `.app-viewport`. This lets
 *   `position: sticky` children (and the viewport-rooted `IntersectionObserver` used by
 *   `useStickyObserver`) work correctly.
 * - `'fixed'`: the header + tabs are pinned and only the tab body scrolls in its own container.
 *   Used by tabs whose content (e.g. the log console) needs a bounded height to resolve `h-full`.
 */
const renderMode = computed<'scroll' | 'fixed'>(() =>
	displayedInstanceRoute.value.meta.renderMode === 'fixed' ? 'fixed' : 'scroll',
)
const isFixedRender = computed(() => renderMode.value === 'fixed')
const contentSubpageProps = computed(() =>
	isContentSubpageRoute() ? { preloadedContent: preloadedContent.value } : {},
)

const tabs = computed(() => [
	{
		label: 'Content',
		href: `${basePath.value}`,
		icon: BoxesIcon,
	},
	{
		label: 'Files',
		href: `${basePath.value}/files`,
		icon: FolderOpenIcon,
	},
	{
		label: 'Worlds',
		href: `${basePath.value}/worlds`,
		icon: GlobeIcon,
	},
	{
		label: 'Logs',
		href: `${basePath.value}/logs`,
		icon: TerminalSquareIcon,
	},
])

if (instance.value) {
	breadcrumbs.setName(
		'Instance',
		instance.value.name.length > 40
			? instance.value.name.substring(0, 40) + '...'
			: instance.value.name,
	)
	breadcrumbs.setContext({
		name: instance.value.name,
		link: displayedInstanceRoute.value.path,
		query: displayedInstanceRoute.value.query,
	})
}

const options = ref<InstanceType<typeof ContextMenu> | null>(null)

const startInstance = async (context: string) => {
	if (!instance.value) return
	if (updateToPlayModal.value?.hasUpdate) {
		updateToPlayModal.value.show(instance.value)
		return
	}

	loading.value = true
	try {
		await run(route.params.id as string)
		playing.value = true
	} catch (err) {
		handleSevereError(err, { instanceId: route.params.id as string })
	}
	loading.value = false

	trackEvent('InstanceStart', {
		loader: instance.value.loader,
		game_version: instance.value.game_version,
		source: context,
	})
}

const stopInstance = async (context: string) => {
	stopping.value = true
	await kill(route.params.id as string).catch(handleError)
	stopping.value = false
	playing.value = false

	if (!instance.value) return
	trackEvent('InstanceStop', {
		loader: instance.value.loader,
		game_version: instance.value.game_version,
		source: context,
	})
}

const handlePlayServer = async () => {
	if (!instance.value?.link?.project_id) return
	loading.value = true
	try {
		await playServerProject(instance.value.link.project_id)
	} finally {
		await updatePlayState()
		loading.value = false
	}
}

const repairInstance = async () => {
	if (
		instance.value.install_stage !== 'pack_installed' &&
		(instance.value.link?.type === 'modrinth_modpack' ||
			instance.value.link?.type === 'server_project_modpack')
	) {
		await install_pack_to_existing_instance(instance.value.id, {
			type: 'fromVersionId',
			project_id: instance.value.link.project_id ?? instance.value.link.server_project_id ?? '',
			version_id: instance.value.link.version_id ?? instance.value.link.content_version_id ?? '',
			title: instance.value.name,
		}).catch(handleError)
	} else {
		await install_existing_instance(instance.value.id, false).catch(handleError)
	}
}

const createShortcut = async () => {
	if (!instance.value) return
	try {
		const shortcutPath = await createInstanceShortcut(instance.value.name, instance.value.id)
		if (!shortcutPath) return

		addNotification({
			type: 'success',
			title: 'Shortcut created',
		})
	} catch (error: unknown) {
		addNotification({
			type: 'error',
			title: `Error creating shortcut`,
			text: `${error}`,
		})
	}
}

const handleRightClick = (event: MouseEvent) => {
	const baseOptions = [
		{ name: 'add_content' },
		{ type: 'divider' },
		{ name: 'edit' },
		{ name: 'open_folder' },
		{ name: 'copy_path' },
	]

	options.value?.showMenu(
		event,
		instance.value,
		playing.value
			? [
					{
						name: 'stop',
						color: 'danger',
					},
					...baseOptions,
				]
			: [
					{
						name: 'play',
						color: 'primary',
					},
					...baseOptions,
				],
	)
}

const handleOptionsClick = async (args: { option: string; item: unknown }) => {
	switch (args.option) {
		case 'play':
			await startInstance('InstancePageContextMenu')
			break
		case 'stop':
			await stopInstance('InstancePageContextMenu')
			break
		case 'add_content':
			await router.push({
				path: `/browse/${instance.value?.loader === 'vanilla' ? 'datapack' : 'mod'}`,
				query: { i: route.params.id },
			})
			break
		case 'edit':
			await router.push({
				path: `/instance/${encodeURIComponent(route.params.id as string)}/options`,
			})
			break
		case 'open_folder':
			if (instance.value) await showInstanceInFolder(instance.value.id)
			break
		case 'copy_path': {
			if (instance.value) {
				const fullPath = await get_full_path(instance.value?.path)
				await navigator.clipboard.writeText(fullPath)
			}
			break
		}
	}
}

const unlistenInstances = await instance_listener(
	async (event: { instance_id: string; event: string }) => {
		if (event.instance_id !== route.params.id) return
		if (event.event === 'removed' || route.path === '/') {
			if (route.path !== '/') {
				await router.push({ path: '/' })
			}
			return
		}
		instance.value = await get(route.params.id as string).catch((err) => {
			if (String(err).includes('not managed')) {
				router.push({ path: '/' })
				return undefined
			}
			return handleError(err)
		})
		if (!instance.value?.link?.project_id) {
			linkedProjectV3.value = undefined
			isServerInstance.value = false
		}
	},
)

const unlistenProcesses = await process_listener((e: { event: string; instance_id: string }) => {
	if (e.event === 'finished' && e.instance_id === route.params.id) {
		playing.value = false
	}
})

const icon = computed(() =>
	instance.value?.icon_path ? convertFileSrc(instance.value.icon_path) : null,
)

const settingsModal = ref<InstanceType<typeof InstanceSettingsModal>>()

const timePlayed = computed(() => {
	return instance.value
		? instance.value.recent_time_played + instance.value.submitted_time_played
		: 0
})

const loaderDisplayName = computed(() =>
	instance.value ? (formatLoaderLabel(instance.value.loader) as Loaders) : null,
)

const loaderLabel = computed(() =>
	instance.value && loaderDisplayName.value
		? [loaderDisplayName.value, instance.value.loader_version].filter(Boolean).join(' ')
		: '',
)

const timePlayedHumanized = computed(() => {
	const duration = dayjs.duration(timePlayed.value, 'seconds')
	const hours = Math.floor(duration.asHours())
	if (hours >= 1) {
		return hours + ' hour' + (hours > 1 ? 's' : '')
	}

	const minutes = Math.floor(duration.asMinutes())
	if (minutes >= 1) {
		return minutes + ' minute' + (minutes > 1 ? 's' : '')
	}

	const seconds = Math.floor(duration.asSeconds())
	return seconds + ' second' + (seconds > 1 ? 's' : '')
})

const playtimeLabel = computed(() =>
	timePlayed.value > 0 ? timePlayedHumanized.value : 'Never played',
)

const instanceHeaderLeading = computed(() => ({
	type: 'avatar' as const,
	src: icon.value ? icon.value : undefined,
	alt: instance.value?.name,
	avatarSize: '64px',
	tintBy: instance.value?.id,
}))

const instanceHeaderMetadata = computed(() => {
	if (!instance.value) return []
	if (isServerInstance.value) {
		return [
			{
				id: 'server-details',
				type: 'custom' as const,
				class: 'contents',
			},
		]
	}

	return [
		{
			id: 'game-version',
			label: instance.value.game_version,
			icon: Gamepad2Icon,
		},
		{
			id: 'loader',
			label: loaderLabel.value,
			icon: ServerLoaderIcon,
			iconProps: {
				loader: loaderDisplayName.value,
			},
		},
		...(showInstancePlayTime.value
			? [
					{
						id: 'playtime',
						label: playtimeLabel.value,
						icon: TimerIcon,
					},
				]
			: []),
	]
})

const installingStages = [
	'installing',
	'pack_installing',
	'pack_installed',
	'not_installed',
	'minecraft_installing',
]

const primaryInstanceAction = computed(() => {
	if (!instance.value) return null

	if (installingStages.includes(instance.value.install_stage)) {
		return {
			id: 'installing',
			label: 'Installing...',
			color: 'brand' as const,
			disabled: true,
		}
	}

	if (instance.value.install_stage !== 'installed') {
		return {
			id: 'repair',
			label: 'Repair',
			icon: DownloadIcon,
			color: 'brand' as const,
			onClick: () => {
				void repairInstance()
			},
		}
	}

	if (playing.value === true) {
		return {
			id: 'stop',
			label: stopping.value ? 'Stopping...' : 'Stop',
			icon: StopCircleIcon,
			color: 'red' as const,
			disabled: stopping.value,
			onClick: () => {
				void stopInstance('InstancePage')
			},
		}
	}

	if (playing.value === false && loading.value === false && !isServerInstance.value) {
		return {
			id: 'play',
			label: 'Play',
			icon: PlayIcon,
			color: 'brand' as const,
			onClick: () => {
				void startInstance('InstancePage')
			},
		}
	}

	if (playing.value === false && loading.value === false && isServerInstance.value) {
		return {
			id: 'play',
			label: 'Play',
			color: 'brand' as const,
			joinedActions: [
				{
					id: 'join_server',
					label: 'Play',
					icon: PlayIcon,
					action: () => {
						void handlePlayServer()
					},
				},
				{
					id: 'launch_instance',
					label: 'Launch instance',
					icon: PlayIcon,
					action: () => {
						void startInstance('InstancePage')
					},
				},
			],
		}
	}

	if (loading.value === true && playing.value === false) {
		return {
			id: 'starting',
			label: 'Starting...',
			color: 'brand' as const,
			disabled: true,
		}
	}

	return null
})

const instanceHeaderActions = computed(() => [
	...(primaryInstanceAction.value ? [primaryInstanceAction.value] : []),
	{
		id: 'settings',
		label: 'Instance settings',
		icon: SettingsIcon,
		labelHidden: true,
		tooltip: 'Instance settings',
		onClick: () => settingsModal.value?.show(),
	},
	{
		id: 'more',
		label: 'More actions',
		icon: MoreVerticalIcon,
		labelHidden: true,
		type: 'transparent' as const,
		tooltip: 'More actions',
		menuActions: [
			{
				id: 'open-folder',
				label: 'Open folder',
				icon: FolderOpenIcon,
				action: () => {
					if (instance.value) void showInstanceInFolder(instance.value.id)
				},
			},
			{
				id: 'export-mrpack',
				label: 'Export modpack',
				icon: PackageIcon,
				action: () => exportModal.value?.show(),
			},
			{
				id: 'create-shortcut',
				label: 'Create shortcut',
				icon: ExternalIcon,
				action: () => {
					void createShortcut()
				},
			},
		],
	},
])

onUnmounted(() => {
	unlistenProcesses()
	unlistenInstances()
	const instanceId = displayedInstanceRoute.value.params.id
	if (instanceId) {
		const { destroy } = useInstanceConsole(instanceId)
		destroy()
	}
})
</script>

<style scoped lang="scss">
.instance-card {
	display: flex;
	flex-direction: column;
	gap: 1rem;
}

Button {
	width: 100%;
}

.button-group {
	display: flex;
	flex-direction: row;
	gap: 0.5rem;
}

.side-cards {
	position: fixed;
	width: 300px;
	display: flex;
	flex-direction: column;

	min-height: calc(100vh - 3.25rem);
	max-height: calc(100vh - 3.25rem);
	overflow-y: auto;
	-ms-overflow-style: none;
	scrollbar-width: none;

	&::-webkit-scrollbar {
		width: 0;
		background: transparent;
	}

	.card {
		min-height: unset;
		margin-bottom: 0;
	}
}

.instance-nav {
	display: flex;
	flex-direction: column;
	align-items: flex-start;
	justify-content: center;
	padding: 1rem;
	gap: 0.5rem;
	background: var(--color-raised-bg);
	height: 100%;
}

.name {
	font-size: 1.25rem;
	color: var(--color-contrast);
	overflow: hidden;
	text-overflow: ellipsis;
}

.metadata {
	text-transform: capitalize;
}

.instance-container {
	display: flex;
	flex-direction: row;
	overflow: auto;
	gap: 1rem;
	min-height: 100%;
	padding: 1rem;
}

.instance-info {
	display: flex;
	flex-direction: column;
	width: 100%;
}

.badge {
	display: flex;
	align-items: center;
	font-weight: bold;
	width: fit-content;
	color: var(--color-orange);
}

.pages-list {
	display: flex;
	flex-direction: column;
	gap: var(--gap-xs);

	.btn {
		font-size: 100%;
		font-weight: 400;
		background: inherit;
		transition: all ease-in-out 0.1s;
		width: 100%;
		color: var(--color-primary);
		box-shadow: none;

		&.router-link-exact-active {
			box-shadow: var(--shadow-inset-lg);
			background: var(--color-button-bg);
			color: var(--color-contrast);
		}

		&:hover {
			background-color: var(--color-button-bg);
			color: var(--color-contrast);
			box-shadow: var(--shadow-inset-lg);
			text-decoration: none;
		}

		svg {
			width: 1.3rem;
			height: 1.3rem;
		}
	}
}

.instance-nav {
	display: flex;
	flex-direction: row;
	align-items: flex-start;
	justify-content: left;
	padding: 1rem;
	gap: 0.5rem;
	height: min-content;
	width: 100%;
}

.instance-button {
	width: fit-content;
}

.actions {
	display: flex;
	flex-direction: column;
	justify-content: flex-start;
	gap: 0.5rem;
}

.content {
	margin: 0 1rem 0.5rem 20rem;
	width: calc(100% - 20rem);
	display: flex;
	flex-direction: column;
	overflow: auto;
}

.stats {
	grid-area: stats;
	display: flex;
	flex-direction: column;
	flex-wrap: wrap;
	gap: var(--gap-md);

	.stat {
		display: flex;
		flex-direction: row;
		align-items: center;
		width: fit-content;
		gap: var(--gap-xs);
		--stat-strong-size: 1.25rem;

		strong {
			font-size: var(--stat-strong-size);
		}

		p {
			margin: 0;
		}

		svg {
			height: var(--stat-strong-size);
			width: var(--stat-strong-size);
		}
	}

	.date {
		margin-top: auto;
	}

	@media screen and (max-width: 750px) {
		flex-direction: row;
		column-gap: var(--gap-md);
		margin-top: var(--gap-xs);
	}

	@media screen and (max-width: 600px) {
		margin-top: 0;

		.stat-label {
			display: none;
		}
	}
}
</style>
