```python
"""Prune a file listing with ordered drop and keep rules."""


def prune_listing(listing: list, rules: list) -> list:
    def segment_fits(pattern, text):
        p = t = mark = 0
        star = -1
        while t < len(text):
            if p < len(pattern) and pattern[p] == "*":
                star = p
                mark = t
                p += 1
            elif p < len(pattern) and pattern[p] == text[t]:
                p += 1
                t += 1
            elif star >= 0:
                p = star + 1
                mark += 1
                t = mark
            else:
                return False
        while p < len(pattern) and pattern[p] == "*":
            p += 1
        return p == len(pattern)

    def split_strict(text, what):
        if not isinstance(text, str) or text == "":
            raise ValueError(f"{what} must be a non-empty string")
        segments = text.split("/")
        if any(segment == "" for segment in segments):
            raise ValueError(f"{what} has an empty segment: {text}")
        return segments

    # Validate every rule and path with split_strict, then keep each
    # path the last matching rule allows; a path no rule matches survives.
    valid_rules = [rule for rule in rules if split_strict(rule, "rule") and rule != "!"]
    valid_listings = [path for path in listing if split_strict(path, "path") and path != ""]
    matching_rules = [rule for rule in valid_rules if any(segment_fits(rule, path) for path in valid_listings)]

    if len(matching_rules) == 0:
        return valid_listings

    last_matching_rules = {}
    for rule in matching_rules:
        for path in valid_listings:
            if segment_fits(rule, path):
                if rule in last_matching_rules:
                    last_matching_rules[rule] = max(last_matching_rules[rule], path)
                else:
                    last_matching_rules[rule] = path

    return [path for path, _ in last_matching_rules.items()]
```