Refactor the python 3.12 code. Do not split into several classes. Lets think step by step. Python Code: class Sidebar:
    """Handles all sidebar-related UI and interactions."""

    def __init__(self, root: tk.Tk, app: TableApp):
        self.root = root
        self.app = app
        self.colors = app.colors
        self.fonts = app.fonts
        self.dimensions = app.dimensions
        self.style = app.style

        # Initialize variables
        self.table_labels_frame: Optional[tk.Frame] = None
        self.table_labels: dict[str, tk.Label] = {}
        self.selected_table_label: Optional[tk.Label] = None
        self.rename_entry: Optional[tk.Entry] = None

        self.create_sidebar()

    def create_sidebar(self):
        """Create the sidebar UI components."""
        self.sidebar_frame = create_frame(
            parent=self.root,
            width=self.dimensions["sidebar_width"],
            bg=self.colors["sidebar"],
        )
        self.sidebar_frame.grid(row=0, column=0, rowspan=2, sticky="ns")
        self.sidebar_frame.grid_propagate(False)
        self.sidebar_frame.columnconfigure(0, weight=1)
        self.sidebar_frame.rowconfigure(0, weight=1)

        functional_sidebar = create_frame(
            parent=self.sidebar_frame,
            width=self.dimensions["sidebar_width"],
            bg=self.colors["sidebar"],
        )
        functional_sidebar.grid(row=0, column=0, sticky="nsew")
        functional_sidebar.columnconfigure(0, weight=1)
        functional_sidebar.rowconfigure(0, weight=0)
        functional_sidebar.rowconfigure(1, weight=1)
        functional_sidebar.rowconfigure(2, weight=0)

        self._add_normal_buttons(functional_sidebar)
        self._add_scrollable_canvas(functional_sidebar)
        self._add_delete_table_section(functional_sidebar)

    def _add_normal_buttons(self, sidebar: tk.Frame):
        """Add buttons to the sidebar."""
        normal_buttons_frame = create_frame(
            parent=sidebar,
            width=self.dimensions["sidebar_width"],
            bg=self.colors["sidebar"],
        )
        normal_buttons_frame.grid(row=0, column=0, sticky="ew", padx=10, pady=(10, 0))
        normal_buttons_frame.columnconfigure(0, weight=1)

        create_table_label = self._create_clickable_label(
            parent=normal_buttons_frame,
            text="Create New Table",
            command=self.app.table_manager.create_new_table,
            anchor="center",
        )
        create_table_label.grid(row=0, column=0, pady=10, sticky="ew")
        self.add_custom_hover(create_table_label)

        separator = tk.Frame(
            normal_buttons_frame,
            height=self.dimensions["separator_height"],
            bg=self.colors["separator"],
        )
        separator.grid(row=1, column=0, sticky="ew", pady=5)

    def _on_canvas_configure(self, event):
        """Update the table_labels_frame width to match the canvas width."""
        self.canvas.itemconfig(self.table_labels_window, width=event.width)

    def _add_scrollable_canvas(self, sidebar: tk.Frame):
        """Add scrollable canvas for table labels within a single frame."""
        scrollable_container = create_frame(
            parent=sidebar,
            width=self.dimensions["sidebar_width"],
            bg=self.colors["sidebar"],
        )
        scrollable_container.grid(row=1, column=0, sticky="nsew", padx=10, pady=10)
        scrollable_container.columnconfigure(0, weight=1)
        scrollable_container.rowconfigure(0, weight=1)

        self.canvas = tk.Canvas(
            scrollable_container,
            bg=self.colors["sidebar"],
            highlightthickness=0,
        )
        self.canvas.grid(row=0, column=0, sticky="nsew")

        custom_scrollbar_design(self.style, self.colors)

        self.scrollbar = ttk.Scrollbar(
            scrollable_container,
            orient="vertical",
            command=self.canvas.yview,
            style="Custom.Vertical.TScrollbar",
        )
        self.scrollbar.grid(row=0, column=1, sticky="ns")

        self.canvas.configure(yscrollcommand=self.scrollbar.set)

        self.table_labels_frame = tk.Frame(self.canvas, bg=self.colors["sidebar"])

        # Capture the window item ID
        self.table_labels_window = self.canvas.create_window(
            (0, 0),
            window=self.table_labels_frame,
            anchor="nw",
            width=self.dimensions["sidebar_width"],
        )

        self.canvas.bind("<MouseWheel>", self._on_mouse_wheel)
        self.table_labels_frame.bind("<MouseWheel>", self._on_mouse_wheel)

        self.table_labels_frame.bind(
            "<Configure>",
            lambda event: (
                self.canvas.configure(scrollregion=self.canvas.bbox("all")),
                self._update_scrollbar(
                    scrollable_container, self.canvas, self.scrollbar
                ),
            ),
        )
        scrollable_container.bind(
            "<Configure>",
            lambda event: self._update_scrollbar(
                scrollable_container, self.canvas, self.scrollbar
            ),
        )

        # Bind the canvas resize to update the table_labels_frame width
        self.canvas.bind("<Configure>", self._on_canvas_configure)

    def _on_mouse_wheel(self, event):
        """Handle mouse wheel scrolling only if scrollbar is visible."""
        if self.scrollbar.winfo_viewable():
            self.canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")

    def _update_scrollbar(self, scrollbar_frame, canvas, scrollbar):
        """Update scrollbar visibility based on content size."""
        canvas.update_idletasks()
        canvas_height = scrollbar_frame.winfo_height()
        content_height = self.table_labels_frame.winfo_reqheight()

        if content_height > canvas_height:
            scrollbar.grid()
            canvas.configure(yscrollcommand=scrollbar.set)
            canvas.bind(
                "<MouseWheel>",
                lambda event: canvas.yview_scroll(int(-1 * (event.delta / 120)), "units"),
            )
        else:
            scrollbar.grid_remove()
            canvas.configure(yscrollcommand=lambda *args: None)
            canvas.unbind("<MouseWheel>")

    def _add_delete_table_section(self, sidebar: tk.Frame):
        """Add delete table option to the sidebar."""
        delete_table_frame = create_frame(
            parent=sidebar,
            width=self.dimensions["sidebar_width"],
            bg=self.colors["sidebar"],
        )
        delete_table_frame.grid(
            row=2, column=0, sticky="ew", padx=10, pady=(0, 10)
        )
        delete_table_frame.columnconfigure(0, weight=1)
        delete_table_frame.rowconfigure(1, weight=1)

        separator = tk.Frame(
            delete_table_frame,
            height=self.dimensions["separator_height"],
            bg=self.colors["separator"],
        )
        separator.grid(row=0, column=0, sticky="ew", pady=5)

        delete_table_label = self._create_clickable_label(
            parent=delete_table_frame,
            text="🗑 Delete Selected Table",
            command=self.app.table_manager.delete_table,
            anchor="center",
        )
        delete_table_label.grid(row=1, column=0, pady=10, sticky="ew")
        self.add_custom_hover(delete_table_label)

    def _create_clickable_label(
        self,
        parent: tk.Widget,
        text: str,
        command,
        allow_rename: bool = False,
        anchor: str = "w",
    ) -> tk.Label:
        """Create a clickable label with optional rename functionality."""
        cursor = "arrow" if allow_rename else "hand2"
        label = tk.Label(
            parent,
            text=text,
            bg=self.colors["button"],
            fg=self.colors["text"],
            cursor=cursor,
            anchor=anchor,
            font=self.fonts["text"],
        )
        label.bind("<Button-1>", lambda _: command())

        if allow_rename:
            label.bind("<Double-Button-1>", lambda _: self.app.table_manager.begin_rename(text))

        label.bind("<MouseWheel>", self._on_mouse_wheel)

        return label

    def add_custom_hover(self, widget: tk.Widget):
        """Add custom hover effects to a widget."""
        widget.bind(
            "<Enter>",
            lambda e: widget.config(bg=self.colors["button_selected"]),
        )
        widget.bind(
            "<Leave>", lambda e: widget.config(bg=self.colors["button"])
        )

    def create_table_label(self, table_name: str):
        """Create label for table."""
        label = self._create_clickable_label(
            self.table_labels_frame,
            table_name,
            lambda: self.app.load_selected_table(table_name),
            allow_rename=True,
        )
        label.pack(fill="x", padx=0, pady=5, expand=True)
        self.table_labels[table_name] = label
        self.add_custom_hover(label)
        self.sort_table_labels()

    def sort_table_labels(self):
        """Sort table labels with numeric names first, then alphabetic, then alphanumeric."""

        def sort_key(name):
            # Category 1: Starts with digits (e.g., "1. Movies")
            m = re.match(r"^(\d+)", name)
            if m:
                return (1, int(m.group(1)))

            # Category 3: Starts with letters followed by digits (e.g., "T1", "T2")
            m = re.match(r"^([A-Za-z]+)(\d+)", name)
            if m:
                return (3, m.group(1).lower(), int(m.group(2)))

            # Category 2: Starts with letters only (e.g., "a", "b", "c")
            m = re.match(r"^([A-Za-z]+)", name)
            if m:
                return (2, m.group(1).lower())

            # Category 4: Any other names
            return (4, name.lower())

        # Sort the table names using the defined sort key
        sorted_tables = sorted(
            self.app.data_manager.tables_data.keys(), key=sort_key
        )

        # Re-pack the labels in the sorted order
        for table_name in sorted_tables:
            label = self.table_labels.get(table_name)
            if label:
                label.pack_forget()
                label.pack(fill="x", padx=0, pady=5, expand=True)

    def cancel_rename(self):
        """Cancel rename operation."""
        if self.rename_entry:
            self.rename_entry.destroy()
            self.rename_entry = None