Repository: Textualize/textual
Issue category: Bug Report

last-of-type (and maybe others) not applied all the time
```markdown
# Textual Diagnostics

## Versions

| Name    | Value  |
|---------|--------|
| Textual | 1.0.0  |
| Rich    | 13.9.4 |

## Python

| Name           | Value                                                        |
|----------------|--------------------------------------------------------------|
| Version        | 3.12.3                                                       |
| Implementation | CPython                                                      |
| Compiler       | Clang 13.0.0 (clang-1300.0.29.30)                            |
| Executable     | /Users/davew/code/flux/flux_config_frontend/.venv/bin/python |

## Operating System

| Name    | Value                                                                                               |
|---------|-----------------------------------------------------------------------------------------------------|
| System  | Darwin                                                                                              |
| Release | 24.2.0                                                                                              |
| Version | Darwin Kernel Version 24.2.0: Sun Nov  3 20:54:52 PST 2024; root:xnu-11215.60.405~54/RELEASE_X86_64 |

## Terminal

| Name                 | Value              |
|----------------------|--------------------|
| Terminal Application | iTerm.app (3.5.10) |
| TERM                 | xterm-256color     |
| COLORTERM            | truecolor          |
| FORCE_COLOR          | *Not set*          |
| NO_COLOR             | *Not set*          |

## Rich Console options

| Name           | Value                |
|----------------|----------------------|
| size           | width=129, height=38 |
| legacy_windows | False                |
| min_width      | 1                    |
| max_width      | 129                  |
| is_terminal    | True                 |
| encoding       | utf-8                |
| max_height     | 38                   |
| justify        | None                 |
| overflow       | None                 |
| no_wrap        | False                |
| highlight      | None                 |
| markup         | None                 |
| height         | None                 |
```

Here is an MRE:

```python
from textual.app import App, ComposeResult
from textual.containers import Horizontal
from textual.widgets import Label


class MyApp(App):
    DEFAULT_CSS = """
    Screen {
        &> Horizontal {
            width: 1fr;
            align: center middle;

            &> Label {
                width: 10;
                height: 5;
                }

            &> Label:last-of-type {
                background: red;
            }
        }
    }
    """

    def compose(self) -> ComposeResult:
        with Horizontal():
            yield Label("Start")
            yield Label("Middle")
            yield Label("End")


app = MyApp()
app.run()
```

When the app loads, it is as expected:

![Screenshot 2024-12-12 at 1 40 28 PM](https://github.com/user-attachments/assets/43626141-d6c6-463f-9a40-0954466882bf)

However, when the app loses focus, the styling is no longer applied, and when the app regains focus it is also not applied.

![Screenshot 2024-12-12 at 1 41 21 PM](https://github.com/user-attachments/assets/61d634e0-0e47-4182-81ad-10a3cc3aeaac)

I beieve there is also an issue with hot reload with these styles, as they wern't being applied when I was running with `textual --dev` until I restarted the app.

On a side note, I'm not sure if this is Iterm doing this but I think it's textual... lately when I ctrl +c some of my key combinations no longer work. I am an avid ctrl + r, ctrl + w, ctrl + a etc etc user.

However, when I ctrl + c textual apps, the ctrl + <action> combos are no longer interpreted until I `reset` the terminal. See here:

![Screenshot 2024-12-12 at 1 38 56 PM](https://github.com/user-attachments/assets/f69f20f6-7712-4c5b-9bff-b1a33c56f47f)





Functions to edit:
src/textual/css/stylesheet.py:Stylesheet
Functions added: src/textual/css/stylesheet.py:Stylesheet

Patch:
diff --git a/src/textual/css/stylesheet.py b/src/textual/css/stylesheet.py
index 82f4ffae77..0bea5504e2 100644
--- a/src/textual/css/stylesheet.py
+++ b/src/textual/css/stylesheet.py
@@ -437,7 +437,7 @@ def _check_rule(
     # These shouldn't be used in a cache key
     _EXCLUDE_PSEUDO_CLASSES_FROM_CACHE: Final[set[str]] = {
         "first-of-type",
-        "last-of_type",
+        "last-of-type",
         "odd",
         "even",
         "focus-within",