"""
Magic: The Gathering Rules Engine - Keyword Database

This module contains a hardcoded database of all Magic: The Gathering keywords,
their definitions, and rule references. This is the core data source for the
rules engine, validator, and game logic modules.

Keywords are organized into three categories:
1. Ability Words (no rules meaning, just flavor)
2. Keyword Actions (verbs that describe game actions)
3. Keyword Abilities (static, triggered, or activated abilities)

All keywords are derived from the official Magic rules (July 2024 rules, version 5.3.0+20260722).
"""

from typing import Any, Dict, List, Optional, Set, Tuple

# =============================================================================
# KEYWORD CATEGORIES
# =============================================================================

class KeywordType:
    """Enumeration of keyword categories."""
    ABILITY_WORD = "ability_word"
    KEYWORD_ACTION = "keyword_action"
    KEYWORD_ABILITY = "keyword_ability"
    KEYWORD_VARIANT = "keyword_variant"

class KeywordSource:
    """Enumeration of keyword sources."""
    STATIC = "static"
    TRIGGERED = "triggered"
    ACTIVATED = "activated"
    SPELL_ABILITY = "spell_ability"
    CHARACTERISTIC_DEFINING = "characteristic_defining"

# =============================================================================
# ABILITY WORDS (no rules meaning)
# =============================================================================

ABILITY_WORDS: Dict[str, Dict[str, Any]] = {
    # List of all ability words from the rules
    # These have NO rules meaning and are included for reference only
    "adamant": {"rules": "", "description": "Ability word with no rules meaning"},
    "addendum": {"rules": "", "description": "Ability word with no rules meaning"},
    "alliance": {"rules": "", "description": "Ability word with no rules meaning"},
    "battalion": {"rules": "", "description": "Ability word with no rules meaning"},
    "bloodrush": {"rules": "", "description": "Ability word with no rules meaning"},
    "celebration": {"rules": "", "description": "Ability word with no rules meaning"},
    "channel": {"rules": "", "description": "Ability word with no rules meaning"},
    "chroma": {"rules": "", "description": "Ability word with no rules meaning"},
    "cohort": {"rules": "", "description": "Ability word with no rules meaning"},
    "constellation": {"rules": "", "description": "Ability word with no rules meaning"},
    "converge": {"rules": "", "description": "Ability word with no rules meaning"},
    "corrupted": {"rules": "", "description": "Ability word with no rules meaning"},
    "council's dilemma": {"rules": "", "description": "Ability word with no rules meaning"},
    "coven": {"rules": "", "description": "Ability word with no rules meaning"},
    "covercast": {"rules": "", "description": "Ability word with no rules meaning"},
    "delirium": {"rules": "", "description": "Ability word with no rules meaning"},
    "descend": {"rules": "", "description": "Ability word with no rules meaning"},
    "disappear": {"rules": "", "description": "Ability word with no rules meaning"},
    "domain": {"rules": "", "description": "Ability word with no rules meaning"},
    "eerie": {"rules": "", "description": "Ability word with no rules meaning"},
    "eminence": {"rules": "", "description": "Ability word with no rules meaning"},
    "enrage": {"rules": "", "description": "Ability word with no rules meaning"},
    "fateful hour": {"rules": "", "description": "Ability word with no rules meaning"},
    "fathomless descent": {"rules": "", "description": "Ability word with no rules meaning"},
    "ferocious": {"rules": "", "description": "Ability word with no rules meaning"},
    "flurry": {"rules": "", "description": "Ability word with no rules meaning"},
    "formidable": {"rules": "", "description": "Ability word with no rules meaning"},
    "grandeur": {"rules": "", "description": "Ability word with no rules meaning"},
    "hellbent": {"rules": "", "description": "Ability word with no rules meaning"},
    "hero's reward": {"rules": "", "description": "Ability word with no rules meaning"},
    "heroic": {"rules": "", "description": "Ability word with no rules meaning"},
    "imprint": {"rules": "", "description": "Ability word with no rules meaning"},
    "infusion": {"rules": "", "description": "Ability word with no rules meaning"},
    "inspired": {"rules": "", "description": "Ability word with no rules meaning"},
    "join forces": {"rules": "", "description": "Ability word with no rules meaning"},
    "kinfall": {"rules": "", "description": "Ability word with no rules meaning"},
    "kinship": {"rules": "", "description": "Ability word with no rules meaning"},
    "landfall": {"rules": "", "description": "Ability word with no rules meaning"},
    "landship": {"rules": "", "description": "Ability word with no rules meaning"},
    "legacy": {"rules": "", "description": "Ability word with no rules meaning"},
    "lieutenant": {"rules": "", "description": "Ability word with no rules meaning"},
    "magecraft": {"rules": "", "description": "Ability word with no rules meaning"},
    "metalcraft": {"rules": "", "description": "Ability word with no rules meaning"},
    "morbid": {"rules": "", "description": "Ability word with no rules meaning"},
    "opus": {"rules": "", "description": "Ability word with no rules meaning"},
    "pack tactics": {"rules": "", "description": "Ability word with no rules meaning"},
    "paradox": {"rules": "", "description": "Ability word with no rules meaning"},
    "parley": {"rules": "", "description": "Ability word with no rules meaning"},
    "radiance": {"rules": "", "description": "Ability word with no rules meaning"},
    "raid": {"rules": "", "description": "Ability word with no rules meaning"},
    "rally": {"rules": "", "description": "Ability word with no rules meaning"},
    "renew": {"rules": "", "description": "Ability word with no rules meaning"},
    "repartee": {"rules": "", "description": "Ability word with no rules meaning"},
    "revolt": {"rules": "", "description": "Ability word with no rules meaning"},
    "secret council": {"rules": "", "description": "Ability word with no rules meaning"},
    "spell mastery": {"rules": "", "description": "Ability word with no rules meaning"},
    "start your engines": {"rules": "", "description": "Ability word with no rules meaning"},
    "strive": {"rules": "", "description": "Ability word with no rules meaning"},
    "survival": {"rules": "", "description": "Ability word with no rules meaning"},
    "sweep": {"rules": "", "description": "Ability word with no rules meaning"},
    "tempting offer": {"rules": "", "description": "Ability word with no rules meaning"},
    "threshold": {"rules": "", "description": "Ability word with no rules meaning"},
    "underdog": {"rules": "", "description": "Ability word with no rules meaning"},
    "undergrowth": {"rules": "", "description": "Ability word with no rules meaning"},
    "valiant": {"rules": "", "description": "Ability word with no rules meaning"},
    "vivid": {"rules": "", "description": "Ability word with no rules meaning"},
    "void": {"rules": "", "description": "Ability word with no rules meaning"},
    "will of the planeswalkers": {"rules": "", "description": "Ability word with no rules meaning"},
    "will of the council": {"rules": "", "description": "Ability word with no rules meaning"},
}

# =============================================================================
# KEYWORD ACTIONS
# =============================================================================

KEYWORD_ACTIONS: Dict[str, Dict[str, Any]] = {
    # Format: {keyword: {rule, definition, examples, variants}}
    
    "activate": {
        "rule": "701.2",
        "definition": "To put an activated ability onto the stack and pay its costs, so that it will eventually resolve and have its effect. Only an object's controller (or its owner, if it doesn't have a controller) can activate its activated ability unless the object specifically says otherwise. A player may activate an ability if they have priority.",
        "variants": [],
        "is_action": True,
    },
    "attach": {
        "rule": "701.3",
        "definition": "To take an Aura, Equipment, or Fortification from where it currently is and put it onto that object or player. If something is attached to a permanent on the battlefield, it's customary to place it so that it's physically touching the permanent. An Aura, Equipment, or Fortification can't be attached to an object or player it can't enchant, equip, or fortify, respectively.",
        "variants": [],
        "is_action": True,
    },
    "behold": {
        "rule": "701.4",
        "definition": "\"Behold a [quality]\" means \"Reveal a [quality] card from your hand or choose a [quality] permanent you control on the battlefield.\"",
        "variants": [],
        "is_action": True,
    },
    "cast": {
        "rule": "701.5",
        "definition": "To cast a spell is to take it from the zone it's in (usually the hand), put it on the stack, and pay its costs, so that it will eventually resolve and have its effect. A player may cast a spell if they have priority.",
        "variants": [],
        "is_action": True,
    },
    "counter": {
        "rule": "701.6",
        "definition": "To counter a spell or ability means to cancel it, removing it from the stack. It doesn't resolve and none of its effects occur. A countered spell is put into its owner's graveyard.",
        "variants": [],
        "is_action": True,
    },
    "create": {
        "rule": "701.7",
        "definition": "To create one or more tokens with certain characteristics, put the specified number of tokens with the specified characteristics onto the battlefield.",
        "variants": [],
        "is_action": True,
    },
    "destroy": {
        "rule": "701.8",
        "definition": "To destroy a permanent, move it from the battlefield to its owner's graveyard. The only ways a permanent can be destroyed are as a result of an effect that uses the word 'destroy' or as a result of the state-based actions that check for lethal damage.",
        "variants": [],
        "is_action": True,
    },
    "discard": {
        "rule": "701.9",
        "definition": "To discard a card, move it from its owner's hand to that player's graveyard. By default, effects that cause a player to discard a card allow the affected player to choose which card to discard.",
        "variants": [],
        "is_action": True,
    },
    "double": {
        "rule": "701.10",
        "definition": "Doubling a creature's power and/or toughness creates a continuous effect. To double a creature's power, that creature gets +X/+0, where X is that creature's power as the spell or ability that doubles its power resolves.",
        "variants": [],
        "is_action": True,
    },
    "exchange": {
        "rule": "701.12",
        "definition": "A spell or ability may instruct players to exchange something (for example, life totals or control of two permanents) as part of its resolution. When such a spell or ability resolves, if the entire exchange can't be completed, no part of the exchange occurs.",
        "variants": [],
        "is_action": True,
    },
    "exile": {
        "rule": "701.13",
        "definition": "To exile an object, move it to the exile zone from wherever it is.",
        "variants": [],
        "is_action": True,
    },
    "fight": {
        "rule": "701.14",
        "definition": "A spell or ability may instruct a creature to fight another creature or it may instruct two creatures to fight each other. Each of those creatures deals damage equal to its power to the other creature.",
        "variants": [],
        "is_action": True,
    },
    "goad": {
        "rule": "701.15",
        "definition": "Certain spells and abilities can goad a creature. Until the next turn of the controller of that spell or ability, that creature is goaded. Goaded creatures attacks each combat if able and attacks a player other than the controller of the permanent, spell, or ability that caused it to be goaded if able.",
        "variants": [],
        "is_action": True,
    },
    "investigate": {
        "rule": "701.16",
        "definition": "\"Investigate\" means \"Create a Clue token.\"",
        "variants": [],
        "is_action": True,
    },
    "mill": {
        "rule": "701.17",
        "definition": "For a player to mill a number of cards, that player puts that many cards from the top of their library into their graveyard.",
        "variants": [],
        "is_action": True,
    },
    "play": {
        "rule": "701.18",
        "definition": "To play a land means to put it onto the battlefield from the zone it's in (usually the hand). To play a card means to play that card as a land or to cast that card as a spell, whichever is appropriate.",
        "variants": [],
        "is_action": True,
    },
    "regenerate": {
        "rule": "701.19",
        "definition": "If the effect of a resolving spell or ability regenerates a permanent, it creates a replacement effect that protects the permanent the next time it would be destroyed this turn. 'Regenerate [permanent]' means 'The next time [permanent] would be destroyed this turn, instead remove all damage marked on it and its controller taps it. If it's an attacking or blocking creature, remove it from combat.'",
        "variants": [],
        "is_action": True,
    },
    "reveal": {
        "rule": "701.20",
        "definition": "To reveal a card, show that card to all players for a brief time. If an effect causes a card to be revealed, it remains revealed for as long as necessary to complete the parts of the effect that card is relevant to.",
        "variants": [],
        "is_action": True,
    },
    "sacrifice": {
        "rule": "701.21",
        "definition": "To sacrifice a permanent, its controller moves it from the battlefield directly to its owner's graveyard. A player can't sacrifice something that isn't a permanent, or something that's a permanent they don't control. Sacrificing a permanent doesn't destroy it.",
        "variants": [],
        "is_action": True,
    },
    "scry": {
        "rule": "701.22",
        "definition": "To \"scry N\" means to look at the top N cards of your library, then put any number of them on the bottom of your library in any order and the rest on top of your library in any order.",
        "variants": [],
        "is_action": True,
    },
    "search": {
        "rule": "701.23",
        "definition": "To search for a card in a zone, look at all cards in that zone (even if it's a hidden zone) and find a card that matches the given description.",
        "variants": [],
        "is_action": True,
    },
    "shuffle": {
        "rule": "701.24",
        "definition": "To shuffle a library or a face-down pile of cards, randomize the cards within it so that no player knows their order.",
        "variants": [],
        "is_action": True,
    },
    "surveil": {
        "rule": "701.25",
        "definition": "To \"surveil N\" means to look at the top N cards of your library, then put any number of them into your graveyard and the rest on top of your library in any order.",
        "variants": [],
        "is_action": True,
    },
    "transform": {
        "rule": "701.27",
        "definition": "To transform a permanent, turn it over so that its other face is up. Only permanents represented by double-faced tokens and double-faced cards can transform.",
        "variants": ["convert"],
        "is_action": True,
    },
    "convert": {
        "rule": "701.28",
        "definition": "To convert a permanent, turn it so that its other face is up. This follows the same rules as transforming a permanent.",
        "variants": ["transform"],
        "is_action": True,
    },
    "fateseal": {
        "rule": "701.29",
        "definition": "To \"fateseal N\" means to look at the top N cards of an opponent's library, then put any number of them on the bottom of that library in any order and the rest on top of that library in any order.",
        "variants": [],
        "is_action": True,
    },
    "clash": {
        "rule": "701.30",
        "definition": "To clash, a player reveals the top card of their library. That player may then put that card on the bottom of their library.",
        "variants": [],
        "is_action": True,
    },
    "planeswalk": {
        "rule": "701.31",
        "definition": "To planeswalk is to put each face-up plane card and phenomenon card on the bottom of its owner's planar deck face down, then move the top card of your planar deck off that planar deck and turn it face up.",
        "variants": [],
        "is_action": True,
    },
    "set in motion": {
        "rule": "701.32",
        "definition": "To set a scheme in motion, move it off the top of your scheme deck if it's on top of your scheme deck and turn it face up if it isn't face up.",
        "variants": [],
        "is_action": True,
    },
    "abandon": {
        "rule": "701.33",
        "definition": "To abandon a scheme, turn it face down and put it on the bottom of its owner's scheme deck.",
        "variants": [],
        "is_action": True,
    },
    "proliferate": {
        "rule": "701.34",
        "definition": "To proliferate means to choose any number of permanents and/or players that have a counter, then give each one additional counter of each kind that permanent or player already has.",
        "variants": [],
        "is_action": True,
    },
    "detain": {
        "rule": "701.35",
        "definition": "Certain spells and abilities can detain a permanent. Until the next turn of the controller of that spell or ability, that permanent can't attack or block and its activated abilities can't be activated.",
        "variants": [],
        "is_action": True,
    },
    "populate": {
        "rule": "701.36",
        "definition": "To populate means to choose a creature token you control and create a token that's a copy of that creature token.",
        "variants": [],
        "is_action": True,
    },
    "monstrosity": {
        "rule": "701.37",
        "definition": "\"Monstrosity N\" means \"If this permanent isn't monstrous, put N +1/+1 counters on it and it becomes monstrous.\"",
        "variants": [],
        "is_action": True,
    },
    "vote": {
        "rule": "701.38",
        "definition": "Some spells and abilities instruct players to vote for one choice from a list of options to determine some aspect of the effect of that spell or ability.",
        "variants": [],
        "is_action": True,
    },
    "bolster": {
        "rule": "701.39",
        "definition": "\"Bolster N\" means \"Choose a creature you control with the least toughness or tied for least toughness among creatures you control. Put N +1/+1 counters on that creature.\"",
        "variants": [],
        "is_action": True,
    },
    "manifest": {
        "rule": "701.40",
        "definition": "To manifest a card, turn it face down. It becomes a 2/2 face-down creature card with ward {2}, no name, no subtypes, and no mana cost. Put that card onto the battlefield face down.",
        "variants": ["manifest dread"],
        "is_action": True,
    },
    "manifest dread": {
        "rule": "701.62",
        "definition": "\"Manifest dread\" means \"Look at the top two cards of your library. Manifest one of them, then put the cards you looked at that were not manifested this way into your graveyard.\"",
        "variants": [],
        "is_action": True,
    },
    "adapt": {
        "rule": "701.46",
        "definition": "\"Adapt N\" means \"If this permanent has no +1/+1 counters on it, put N +1/+1 counters on it.\"",
        "variants": [],
        "is_action": True,
    },
    "amass": {
        "rule": "701.47",
        "definition": "To amass [subtype] N means \"If you don't control an Army creature, create a 0/0 black [subtype] Army creature token. Choose an Army creature you control. Put N +1/+1 counters on that creature. If it isn't a [subtype], it becomes a [subtype] in addition to its other types.\"",
        "variants": [],
        "is_action": True,
    },
    "learn": {
        "rule": "701.48",
        "definition": "\"Learn\" means \"You may discard a card. If you do, draw a card. If you didn't discard a card, you may reveal a Lesson card you own from outside the game and put it into your hand.\"",
        "variants": [],
        "is_action": True,
    },
    "venture into the dungeon": {
        "rule": "701.49",
        "definition": "To venture into the dungeon means to choose an appropriate dungeon card you own from outside the game and put it into the command zone, or to move a player's venture marker to an adjacent room.",
        "variants": [],
        "is_action": True,
    },
    "connive": {
        "rule": "701.50",
        "definition": "To have a permanent connive means the permanent's controller draws a card, then discards a card. If a nonland card is discarded this way, that player puts a +1/+1 counter on the conniving permanent.",
        "variants": [],
        "is_action": True,
    },
    "open an attraction": {
        "rule": "701.51",
        "definition": "To open an Attraction, move the top card of your Attraction deck off the Attraction deck, turn it face up, and put it onto the battlefield under your control.",
        "variants": [],
        "is_action": True,
    },
    "roll to visit your attractions": {
        "rule": "701.52",
        "definition": "To roll to visit your Attractions, roll a six-sided die. Then if you control one or more Attractions with a number lit up that is equal to that result, each of those Attractions has been \"visited\" and its visit ability triggers.",
        "variants": [],
        "is_action": True,
    },
    "incubate": {
        "rule": "701.53",
        "definition": "To incubate N, create an Incubator token that enters the battlefield with N +1/+1 counters on it.",
        "variants": [],
        "is_action": True,
    },
    "the ring tempts you": {
        "rule": "701.54",
        "definition": "Each time the Ring tempts you, choose a creature you control. That creature becomes your Ring-bearer until another creature becomes your Ring-bearer or another player gains control of it.",
        "variants": [],
        "is_action": True,
    },
    "face a villainous choice": {
        "rule": "701.55",
        "definition": "\"[A player] faces a villainous choice — [option A], or [option B]\" means \"[A player] chooses [option A] or [option B]. Then all actions in the chosen option are performed.\"",
        "variants": [],
        "is_action": True,
    },
    "time travel": {
        "rule": "701.56",
        "definition": "To time travel means to choose any number of permanents you control with one or more time counters on them and/or suspended cards you own in exile with one or more time counters on them and, for each of those objects, put a time counter on it or remove a time counter from it.",
        "variants": [],
        "is_action": True,
    },
    "discover": {
        "rule": "701.57",
        "definition": "\"Discover N\" means \"Exile cards from the top of your library until you exile a nonland card with mana value N or less. You may cast that card without paying its mana cost if the resulting spell's mana value is less than or equal to N. If you don't cast it, put that card into your hand. Put the remaining exiled cards on the bottom of your library in a random order.\"",
        "variants": [],
        "is_action": True,
    },
    "cloak": {
        "rule": "701.58",
        "definition": "To cloak a card, turn it face down. It becomes a 2/2 face-down creature card with ward {2}, no name, no subtypes, and no mana cost. Put that card onto the battlefield face down.",
        "variants": [],
        "is_action": True,
    },
    "collect evidence": {
        "rule": "701.59",
        "definition": "To \"collect evidence N\" means to exile any number of cards from your graveyard with total mana value N or greater.",
        "variants": [],
        "is_action": True,
    },
    "suspect": {
        "rule": "701.60",
        "definition": "Certain spells and abilities instruct a player to suspect a creature. That creature becomes suspected until it leaves the battlefield or until a spell or ability causes it to no longer be suspected.",
        "variants": [],
        "is_action": True,
    },
    "forage": {
        "rule": "701.61",
        "definition": "To forage means \"Exile three cards from your graveyard or sacrifice a Food.\"",
        "variants": [],
        "is_action": True,
    },
    "endure": {
        "rule": "701.63",
        "definition": "To endure N means to create an N/N white Spirit creature token unless you put N +1/+1 counters on that permanent.",
        "variants": [],
        "is_action": True,
    },
    "harness": {
        "rule": "701.64",
        "definition": "\"Harness [this permanent]\" means \"If this permanent isn't harnessed, it becomes harnessed.\"",
        "variants": [],
        "is_action": True,
    },
    "airbend": {
        "rule": "701.65",
        "definition": "Certain spells and abilities instruct a player to airbend one or more permanents and/or spells. To do so, that player exiles those objects. For each card exiled this way, for as long as it remains exiled, its owner may cast it by paying {2} rather than paying its mana cost.",
        "variants": [],
        "is_action": True,
    },
    "earthbend": {
        "rule": "701.66",
        "definition": "\"Earthbend N\" means \"Target land you control becomes a 0/0 land creature with haste in addition to its other types. Put N +1/+1 counters on it. When that land dies or is put into exile, return it to the battlefield tapped under your control.\"",
        "variants": [],
        "is_action": True,
    },
    "waterbend": {
        "rule": "701.67",
        "definition": "\"Waterbend [cost]\" means \"Pay [cost]. For each generic mana in that cost, you may tap an untapped artifact or creature you control rather than pay that mana.\"",
        "variants": [],
        "is_action": True,
    },
    "blight": {
        "rule": "701.68",
        "definition": "To \"blight N\" means to put N -1/-1 counters on a creature you control.",
        "variants": [],
        "is_action": True,
    },
    "heal": {
        "rule": "701.69",
        "definition": "To heal damage already dealt to a permanent, remove that marked damage from that permanent. If an effect states that damage already dealt to a permanent \"is healed,\" that permanent's controller removes all marked damage from that permanent.",
        "variants": [],
        "is_action": True,
    },
    "untap": {
        "rule": "701.26",
        "definition": "To untap a permanent, rotate it back to the upright position from a sideways position. Only tapped permanents can be untapped.",
        "variants": [],
        "is_action": True,
    },
    "tap": {
        "rule": "701.26",
        "definition": "To tap a permanent, turn it sideways from an upright position. Only untapped permanents can be tapped.",
        "variants": [],
        "is_action": True,
    },
}

# =============================================================================
# KEYWORD ABILITIES
# =============================================================================

KEYWORD_ABILITIES: Dict[str, Dict[str, Any]] = {
    # Format: {keyword: {rule, type, definition, variants}}
    # type: "static", "triggered", "activated", "spell_ability", "characteristic_defining"
    
    "deathtouch": {
        "rule": "702.2",
        "type": "static",
        "definition": "A creature with toughness greater than 0 that's been dealt damage by a source with deathtouch since the last time state-based actions were checked is destroyed as a state-based action.",
        "variants": [],
    },
    "defender": {
        "rule": "702.3",
        "type": "static",
        "definition": "A creature with defender can't attack.",
        "variants": [],
    },
    "double strike": {
        "rule": "702.4",
        "type": "static",
        "definition": "Double strike is a static ability that modifies the rules for the combat damage step. If at least one attacking or blocking creature has first strike or double strike as the combat damage step begins, the only creatures that assign combat damage in that step are those with first strike or double strike.",
        "variants": [],
    },
    "enchant": {
        "rule": "702.5",
        "type": "static",
        "definition": "Enchant is a static ability, written 'Enchant [object or player].' The enchant ability restricts what an Aura spell can target and what an Aura can enchant.",
        "variants": [],
    },
    "equip": {
        "rule": "702.6",
        "type": "activated",
        "definition": "Equip is an activated ability of Equipment cards. 'Equip [cost]' means '[Cost]: Attach this permanent to target creature you control. Activate only as a sorcery.'",
        "variants": ["equip planeswalker"],
    },
    "first strike": {
        "rule": "702.7",
        "type": "static",
        "definition": "First strike is a static ability that modifies the rules for the combat damage step. If at least one attacking or blocking creature has first strike or double strike as the combat damage step begins, the only creatures that assign combat damage in that step are those with first strike or double strike.",
        "variants": [],
    },
    "flash": {
        "rule": "702.8",
        "type": "static",
        "definition": "Flash is a static ability that functions in any zone from which you could play the card it's on. 'Flash' means 'You may play this card any time you could cast an instant.'",
        "variants": [],
    },
    "flying": {
        "rule": "702.9",
        "type": "static",
        "definition": "Flying is an evasion ability. A creature with flying can't be blocked except by creatures with flying and/or reach. A creature with flying can block a creature with or without flying.",
        "variants": [],
    },
    "haste": {
        "rule": "702.10",
        "type": "static",
        "definition": "If a creature has haste, it can attack even if it hasn't been controlled by its controller continuously since their most recent turn began.",
        "variants": [],
    },
    "hexproof": {
        "rule": "702.11",
        "type": "static",
        "definition": "A permanent or player with hexproof can't be the target of spells or abilities your opponents control.",
        "variants": ["hexproof from"],
    },
    "indestructible": {
        "rule": "702.12",
        "type": "static",
        "definition": "A permanent with indestructible can't be destroyed. Such permanents aren't destroyed by lethal damage, and they ignore the state-based action that checks for lethal damage.",
        "variants": [],
    },
    "intimidate": {
        "rule": "702.13",
        "type": "static",
        "definition": "A creature with intimidate can't be blocked except by artifact creatures and/or creatures that share a color with it.",
        "variants": [],
    },
    "landwalk": {
        "rule": "702.14",
        "type": "static",
        "definition": "A creature with landwalk can't be blocked as long as the defending player controls at least one land with the specified land type.",
        "variants": ["islandwalk", "mountainwalk", "plainswalk", "swampwalk", "forestwalk", "nonbasic landwalk", "snowwalk"],
    },
    "lifelink": {
        "rule": "702.15",
        "type": "static",
        "definition": "Damage dealt by a source with lifelink causes that source's controller, or its owner if it has no controller, to gain that much life.",
        "variants": [],
    },
    "protection": {
        "rule": "702.16",
        "type": "static",
        "definition": "A permanent or player with protection can't be targeted by spells with the stated quality and can't be targeted by abilities from a source with the stated quality.",
        "variants": ["protection from everything", "protection from [a player]"],
    },
    "reach": {
        "rule": "702.17",
        "type": "static",
        "definition": "A creature with reach can block a creature with flying. A creature with flying can't be blocked except by creatures with flying and/or reach.",
        "variants": [],
    },
    "shroud": {
        "rule": "702.18",
        "type": "static",
        "definition": "Shroud is a static ability. 'Shroud' means 'This permanent or player can't be the target of spells or abilities.'",
        "variants": [],
    },
    "trample": {
        "rule": "702.19",
        "type": "static",
        "definition": "Trample is a static ability that modifies the rules for assigning an attacking creature's combat damage. The controller of an attacking creature with trample first assigns damage to the creature(s) blocking it. Once all those blocking creatures are assigned lethal damage, any excess damage is assigned as its controller chooses among those blocking creatures and the player, planeswalker, or battle the creature is attacking.",
        "variants": ["trample over planeswalkers"],
    },
    "vigilance": {
        "rule": "702.20",
        "type": "static",
        "definition": "Vigilance is a static ability that modifies the rules for the declare attackers step. Attacking doesn't cause creatures with vigilance to tap.",
        "variants": [],
    },
    "ward": {
        "rule": "702.21",
        "type": "triggered",
        "definition": "Ward [cost] means 'Whenever this permanent becomes the target of a spell or ability an opponent controls, counter that spell or ability unless that player pays [cost].'",
        "variants": [],
    },
    "banding": {
        "rule": "702.22",
        "type": "static",
        "definition": "Banding is a static ability that modifies the rules for combat. 'Bands with other' is a special form of banding.",
        "variants": ["bands with other"],
    },
    "rampage": {
        "rule": "702.23",
        "type": "triggered",
        "definition": "Rampage N means 'Whenever this creature becomes blocked, it gets +N/+N until end of turn for each creature blocking it beyond the first.'",
        "variants": [],
    },
    "cumulative upkeep": {
        "rule": "702.24",
        "type": "triggered",
        "definition": "Cumulative upkeep [cost] means 'At the beginning of your upkeep, if this permanent is on the battlefield, put an age counter on this permanent. Then you may pay [cost] for each age counter on it. If you don't, sacrifice it.'",
        "variants": [],
    },
    "flanking": {
        "rule": "702.25",
        "type": "triggered",
        "definition": "Flanking means 'Whenever this creature becomes blocked by a creature without flanking, the blocking creature gets -1/-1 until end of turn.'",
        "variants": [],
    },
    "phasing": {
        "rule": "702.26",
        "type": "static",
        "definition": "Phasing is a static ability that modifies the rules of the untap step. During each player's untap step, before the active player untaps permanents, all phased-in permanents with phasing that player controls phase out.",
        "variants": [],
    },
    "buyback": {
        "rule": "702.27",
        "type": "static",
        "definition": "Buyback appears on some instants and sorceries. It represents two static abilities that function while the spell with buyback is on the stack. 'Buyback [cost]' means 'You may pay an additional [cost] as you cast this spell' and 'If the buyback cost was paid, put this spell into its owner's hand instead of into that player's graveyard as it resolves.'",
        "variants": [],
    },
    "shadow": {
        "rule": "702.28",
        "type": "static",
        "definition": "A creature with shadow can't be blocked by creatures without shadow, and a creature without shadow can't be blocked by creatures with shadow.",
        "variants": [],
    },
    "cycling": {
        "rule": "702.29",
        "type": "activated",
        "definition": "Cycling is an activated ability that functions only while the card with cycling is in a player's hand. 'Cycling [cost]' means '[Cost], Discard this card: Draw a card.'",
        "variants": ["mountaincycling", "islandcycling", "forestcycling", "plainscycling", "swampcycling", "slivercycling", "basic landcycling", "typecycling"],
    },
    "echo": {
        "rule": "702.30",
        "type": "triggered",
        "definition": "Echo [cost] means 'At the beginning of your upkeep, if this permanent came under your control since the beginning of your last upkeep, sacrifice it unless you pay [cost].'",
        "variants": [],
    },
    "horsemanship": {
        "rule": "702.31",
        "type": "static",
        "definition": "A creature with horsemanship can't be blocked by creatures without horsemanship. A creature with horsemanship can block a creature with or without horsemanship.",
        "variants": [],
    },
    "fading": {
        "rule": "702.32",
        "type": "static",
        "definition": "Fading N means 'This permanent enters with N fade counters on it' and 'At the beginning of your upkeep, remove a fade counter from this permanent. If you can't, sacrifice the permanent.'",
        "variants": [],
    },
    "kicker": {
        "rule": "702.33",
        "type": "static",
        "definition": "Kicker is a static ability that functions while the spell with kicker is on the stack. 'Kicker [cost]' means 'You may pay an additional [cost] as you cast this spell.'",
        "variants": ["multikicker", "sticker kicker"],
    },
    "flashback": {
        "rule": "702.34",
        "type": "static",
        "definition": "Flashback appears on some instants and sorceries. It represents two static abilities: one that functions while the card with flashback is in a player's graveyard and another that functions while the card with flashback is on the stack. 'Flashback [cost]' means 'You may cast this card from your graveyard if the resulting spell is an instant or sorcery spell by paying [cost] rather than paying its mana cost' and 'If the flashback cost was paid, exile this card instead of putting it anywhere else any time it would leave the stack.'",
        "variants": [],
    },
    "madness": {
        "rule": "702.35",
        "type": "static",
        "definition": "Madness [cost] means 'If a player would discard this card, that player discards it, but exiles it instead of putting it into their graveyard' and 'When this card is exiled this way, its owner may cast it by paying [cost] rather than paying its mana cost. If that player doesn't, they put this card into their graveyard.'",
        "variants": [],
    },
    "fear": {
        "rule": "702.36",
        "type": "static",
        "definition": "A creature with fear can't be blocked except by artifact creatures and/or black creatures.",
        "variants": [],
    },
    "morph": {
        "rule": "702.37",
        "type": "static",
        "definition": "Morph is a static ability that functions in any zone from which you could play the card it's on. 'Morph [cost]' means 'You may cast this card as a 2/2 face-down creature with no text, no name, no subtypes, and no mana cost by paying {3} rather than paying its mana cost.'",
        "variants": ["megamorph"],
    },
    "amplify": {
        "rule": "702.38",
        "type": "static",
        "definition": "Amplify is a static ability. 'Amplify N' means 'As this object enters, reveal any number of cards from your hand that share a creature type with it. This permanent enters with N +1/+1 counters on it for each card revealed this way.'",
        "variants": [],
    },
    "provoke": {
        "rule": "702.39",
        "type": "triggered",
        "definition": "Provoke means 'Whenever this creature attacks, you may choose to have target creature defending player controls block this creature this combat if able. If you do, untap that creature.'",
        "variants": [],
    },
    "storm": {
        "rule": "702.40",
        "type": "triggered",
        "definition": "Storm is a triggered ability that functions on the stack. 'Storm' means 'When you cast this spell, copy it for each other spell that was cast before it this turn. If the spell has any targets, you may choose new targets for the copies.'",
        "variants": [],
    },
    "affinity": {
        "rule": "702.41",
        "type": "static",
        "definition": "Affinity is a static ability that functions while the spell with affinity is on the stack. 'Affinity for [text]' means 'This spell costs {1} less to cast for each [text] you control.'",
        "variants": [],
    },
    "entwine": {
        "rule": "702.42",
        "type": "static",
        "definition": "Entwine is a static ability of modal spells. 'Entwine [cost]' means 'You may choose all modes of this spell instead of just the number specified. If you do, you pay an additional [cost].'",
        "variants": [],
    },
    "modular": {
        "rule": "702.43",
        "type": "triggered",
        "definition": "Modular represents both a static ability and a triggered ability. 'Modular N' means 'This permanent enters with N +1/+1 counters on it' and 'When this permanent is put into a graveyard from the battlefield, you may put a +1/+1 counter on target artifact creature for each +1/+1 counter on this permanent.'",
        "variants": [],
    },
    "sunburst": {
        "rule": "702.44",
        "type": "static",
        "definition": "Sunburst is a static ability that functions as an object is entering the battlefield. 'Sunburst' means 'If this object is entering as a creature, ignoring any type-changing effects that would affect it, it enters with a +1/+1 counter on it for each color of mana spent to cast it. Otherwise, it enters with a charge counter on it for each color of mana spent to cast it.'",
        "variants": [],
    },
    "bushido": {
        "rule": "702.45",
        "type": "triggered",
        "definition": "Bushido N means 'Whenever this creature blocks or becomes blocked, it gets +N/+N until end of turn.'",
        "variants": [],
    },
    "soulshift": {
        "rule": "702.46",
        "type": "triggered",
        "definition": "Soulshift N means 'When this permanent is put into a graveyard from the battlefield, you may return target Spirit card with mana value N or less from your graveyard to your hand.'",
        "variants": [],
    },
    "splice": {
        "rule": "702.47",
        "type": "static",
        "definition": "Splice is a static ability that functions while a card is in your hand. 'Splice onto [quality] [cost]' means 'You may reveal this card from your hand as you cast a [quality] spell. If you do, that spell gains the text of this card's rules text and you pay [cost] as an additional cost to cast that spell.'",
        "variants": [],
    },
    "offering": {
        "rule": "702.48",
        "type": "static",
        "definition": "Offering is a static ability that functions while the spell with offering is on the stack. '[Quality] offering' means 'As an additional cost to cast this spell, you may sacrifice a [quality] permanent. If you chose to pay the additional cost, this spell's total cost is reduced by the sacrificed permanent's mana cost, and you may cast this spell any time you could cast an instant.'",
        "variants": [],
    },
    "ninjutsu": {
        "rule": "702.49",
        "type": "activated",
        "definition": "Ninjutsu is an activated ability that functions only while the card with ninjutsu is in a player's hand. 'Ninjutsu [cost]' means '[Cost], Reveal this card from your hand, Return an unblocked attacking creature you control to its owner's hand: Put this card onto the battlefield from your hand tapped and attacking.'",
        "variants": ["commander ninjutsu"],
    },
    "epic": {
        "rule": "702.50",
        "type": "static",
        "definition": "Epic represents two spell abilities, one of which creates a delayed triggered ability. 'Epic' means 'For the rest of the game, you can't cast spells,' and 'At the beginning of each of your upkeeps for the rest of the game, copy this spell except for its epic ability.'",
        "variants": [],
    },
    "convoke": {
        "rule": "702.51",
        "type": "static",
        "definition": "Convoke is a static ability that functions while the spell with convoke is on the stack. 'Convoke' means 'For each colored mana in this spell's total cost, you may tap an untapped creature of that color you control rather than pay that mana. For each generic mana in this spell's total cost, you may tap an untapped creature you control rather than pay that mana.'",
        "variants": [],
    },
    "dredge": {
        "rule": "702.52",
        "type": "static",
        "definition": "Dredge is a static ability that functions only while the card with dredge is in a player's graveyard. 'Dredge N' means 'As long as you have at least N cards in your library, if you would draw a card, you may instead mill N cards and return this card from your graveyard to your hand.'",
        "variants": [],
    },
    "transmute": {
        "rule": "702.53",
        "type": "activated",
        "definition": "Transmute is an activated ability that functions only while the card with transmute is in a player's hand. 'Transmute [cost]' means '[Cost], Discard this card: Search your library for a card with the same mana value as the discarded card, reveal that card, and put it into your hand. Then shuffle your library. Activate only as a sorcery.'",
        "variants": [],
    },
    "bloodthirst": {
        "rule": "702.54",
        "type": "static",
        "definition": "Bloodthirst N means 'If an opponent was dealt damage this turn, this permanent enters with N +1/+1 counters on it.'",
        "variants": ["bloodthirst X"],
    },
    "haunt": {
        "rule": "702.55",
        "type": "triggered",
        "definition": "Haunt on a permanent means 'When this permanent is put into a graveyard from the battlefield, exile it haunting target creature.' Haunt on an instant or sorcery spell means 'When this spell is put into a graveyard during its resolution, exile it haunting target creature.'",
        "variants": [],
    },
    "replicate": {
        "rule": "702.56",
        "type": "static",
        "definition": "Replicate represents two abilities. The first is a static ability that functions while the spell with replicate is on the stack. The second is a triggered ability that functions while the spell with replicate is on the stack. 'Replicate [cost]' means 'As an additional cost to cast this spell, you may pay [cost] any number of times' and 'When you cast this spell, if a replicate cost was paid for it, copy it for each time its replicate cost was paid.'",
        "variants": [],
    },
    "forecast": {
        "rule": "702.57",
        "type": "activated",
        "definition": "A forecast ability is an activated ability that can be activated only from a player's hand. It's written 'Forecast — [Activated ability].'",
        "variants": [],
    },
    "graft": {
        "rule": "702.58",
        "type": "triggered",
        "definition": "Graft represents both a static ability and a triggered ability. 'Graft N' means 'This permanent enters with N +1/+1 counters on it' and 'Whenever another creature enters, if this permanent has a +1/+1 counter on it, you may move a +1/+1 counter from this permanent onto that creature.'",
        "variants": [],
    },
    "recover": {
        "rule": "702.59",
        "type": "triggered",
        "definition": "Recover is a triggered ability that functions only while the card with recover is in a player's graveyard. 'Recover [cost]' means 'When a creature is put into your graveyard from the battlefield, you may pay [cost]. If you do, return this card from your graveyard to your hand. Otherwise, exile this card.'",
        "variants": [],
    },
    "ripple": {
        "rule": "702.60",
        "type": "triggered",
        "definition": "Ripple N means 'When you cast this spell, you may reveal the top N cards of your library, or, if there are fewer than N cards in your library, you may reveal all the cards in your library. If you reveal cards from your library this way, you may cast any of those cards with the same name as this spell without paying their mana costs.'",
        "variants": [],
    },
    "split second": {
        "rule": "702.61",
        "type": "static",
        "definition": "Split second is a static ability that functions only while the spell with split second is on the stack. 'Split second' means 'As long as this spell is on the stack, players can't cast other spells or activate abilities that aren't mana abilities.'",
        "variants": [],
    },
    "suspend": {
        "rule": "702.62",
        "type": "static",
        "definition": "Suspend is a keyword that represents three abilities. 'Suspend N—[cost]' means 'If you could begin to cast this card by putting it onto the stack from your hand, you may pay [cost] and exile it with N time counters on it. This action doesn't use the stack.'",
        "variants": [],
    },
    "vanishing": {
        "rule": "702.63",
        "type": "static",
        "definition": "Vanishing N means 'This permanent enters with N time counters on it', 'At the beginning of your upkeep, if this permanent has a time counter on it, remove a time counter from it', and 'When the last time counter is removed from this permanent, sacrifice it.'",
        "variants": [],
    },
    "absorb": {
        "rule": "702.64",
        "type": "static",
        "definition": "Absorb is a static ability. 'Absorb N' means 'If a source would deal damage to this creature, prevent N of that damage.'",
        "variants": [],
    },
    "aura swap": {
        "rule": "702.65",
        "type": "activated",
        "definition": "Aura swap is an activated ability of some Aura cards. 'Aura swap [cost]' means '[Cost]: You may exchange this permanent with an Aura card in your hand.'",
        "variants": [],
    },
    "delve": {
        "rule": "702.66",
        "type": "static",
        "definition": "Delve is a static ability that functions while the spell with delve is on the stack. 'Delve' means 'For each generic mana in this spell's total cost, you may exile a card from your graveyard rather than pay that mana.'",
        "variants": [],
    },
    "fortify": {
        "rule": "702.67",
        "type": "activated",
        "definition": "Fortify is an activated ability of Fortification cards. 'Fortify [cost]' means '[Cost]: Attach this Fortification to target land you control. Activate only as a sorcery.'",
        "variants": [],
    },
    "frenzy": {
        "rule": "702.68",
        "type": "triggered",
        "definition": "Frenzy N means 'Whenever this creature attacks and isn't blocked, it gets +N/+0 until end of turn.'",
        "variants": [],
    },
    "embrace": {
        "rule": "702.119",
        "type": "static",
        "definition": "Emerge represents two static abilities that function while the spell with emerge is on the stack. 'Emerge [cost]' means 'You may cast this card by paying [cost] and sacrificing a creature rather than paying its mana cost' and 'If you chose to pay this spell's emerge cost, its total cost is reduced by an amount of generic mana equal to the sacrificed creature's mana value.'",
        "variants": ["emerge from"],
    },
    "evolve": {
        "rule": "702.100",
        "type": "triggered",
        "definition": "Evolve means 'Whenever a creature you control enters, if that creature's power is greater than this creature's power and/or that creature's toughness is greater than this creature's toughness, put a +1/+1 counter on this creature.'",
        "variants": [],
    },
    "extort": {
        "rule": "702.101",
        "type": "triggered",
        "definition": "Extort means 'Whenever you cast a spell, you may pay {W/B}. If you do, each opponent loses 1 life and you gain life equal to the total life lost this way.'",
        "variants": [],
    },
    "fuse": {
        "rule": "702.102",
        "type": "static",
        "definition": "Fuse appears on some split cards. It represents two static abilities: one that functions while the card with fuse is on the stack and another that functions while the card with fuse is in a player's hand. If a player casts a split card with fuse from their hand, the player may choose to cast both halves of that split card rather than choose one half.",
        "variants": [],
    },
    "bestow": {
        "rule": "702.103",
        "type": "static",
        "definition": "Bestow [cost] means 'As you cast this spell, you may choose to cast it bestowed. If you do, you pay [cost] rather than its mana cost.'",
        "variants": [],
    },
    "tribute": {
        "rule": "702.104",
        "type": "static",
        "definition": "Tribute N means 'As this creature enters, choose an opponent. That player may put an additional N +1/+1 counters on it as it enters.'",
        "variants": [],
    },
    "dethrone": {
        "rule": "702.105",
        "type": "triggered",
        "definition": "Dethrone means 'Whenever this creature attacks the player with the most life or tied for most life, put a +1/+1 counter on this creature.'",
        "variants": [],
    },
    "hidden agenda": {
        "rule": "702.106",
        "type": "static",
        "definition": "Hidden agenda is a static ability that functions as a conspiracy card with hidden agenda is put into the command zone. 'Hidden agenda' means 'As you put this conspiracy card into the command zone, turn it face down and secretly choose a card name.'",
        "variants": ["double agenda"],
    },
    "outlast": {
        "rule": "702.107",
        "type": "activated",
        "definition": "Outlast [cost] means '[Cost], {T}: Put a +1/+1 counter on this creature. Activate only as a sorcery.'",
        "variants": [],
    },
    "prowess": {
        "rule": "702.108",
        "type": "triggered",
        "definition": "Prowess means 'Whenever you cast a noncreature spell, this creature gets +1/+1 until end of turn.'",
        "variants": [],
    },
    "dash": {
        "rule": "702.109",
        "type": "static",
        "definition": "Dash represents three abilities: two static abilities that function while the card with dash is on the stack, one of which may create a delayed triggered ability, and a static ability that functions while the object with dash is on the battlefield. 'Dash [cost]' means 'You may cast this card by paying [cost] rather than paying its mana cost'.",
        "variants": [],
    },
    "exploit": {
        "rule": "702.110",
        "type": "triggered",
        "definition": "Exploit means 'When this creature enters, you may sacrifice a creature.'",
        "variants": [],
    },
    "menace": {
        "rule": "702.111",
        "type": "static",
        "definition": "A creature with menace can't be blocked except by two or more creatures.",
        "variants": [],
    },
    "renown": {
        "rule": "702.112",
        "type": "triggered",
        "definition": "Renown N means 'Whenever this creature deals combat damage to a player, if it isn't renowned, put N +1/+1 counters on it and it becomes renowned.'",
        "variants": [],
    },
    "awaken": {
        "rule": "702.113",
        "type": "static",
        "definition": "Awaken N—[cost] means 'You may pay [cost] rather than pay this spell's mana cost as you cast this spell' and 'If this spell's awaken cost was paid, put N +1/+1 counters on target land you control. That land becomes a 0/0 Elemental creature with haste. It's still a land.'",
        "variants": [],
    },
    "devoid": {
        "rule": "702.114",
        "type": "characteristic_defining",
        "definition": "Devoid is a characteristic-defining ability. 'Devoid' means 'This object is colorless.' This ability functions everywhere, even outside the game.",
        "variants": [],
    },
    "ingest": {
        "rule": "702.115",
        "type": "triggered",
        "definition": "Ingest means 'Whenever this creature deals combat damage to a player, that player exiles the top card of their library.'",
        "variants": [],
    },
    "myriad": {
        "rule": "702.116",
        "type": "triggered",
        "definition": "Myriad means 'Whenever this creature attacks, for each opponent other than defending player, you may create a token that's a copy of this creature that's tapped and attacking that player or a planeswalker they control. If one or more tokens are created this way, exile the tokens at end of combat.'",
        "variants": [],
    },
    "surge": {
        "rule": "702.117",
        "type": "static",
        "definition": "Surge [cost] means 'You may pay [cost] rather than pay this spell's mana cost if you or one of your teammates has cast another spell this turn.'",
        "variants": [],
    },
    "skulk": {
        "rule": "702.118",
        "type": "static",
        "definition": "A creature with skulk can't be blocked by creatures with greater power.",
        "variants": [],
    },
    "endure": {
        "rule": "702.145",
        "type": "static",
        "definition": "Daybound and nightbound are found on opposite faces of some double-faced cards. Daybound is found on the front faces of some double-faced cards and represents three static abilities.",
        "variants": ["nightbound"],
    },
    "disturb": {
        "rule": "702.146",
        "type": "static",
        "definition": "Disturb [cost] means 'You may cast this card transformed from your graveyard by paying [cost] rather than its mana cost.'",
        "variants": [],
    },
    "decayed": {
        "rule": "702.147",
        "type": "static",
        "definition": "Decayed represents a static ability and a triggered ability. 'Decayed' means 'This creature can't block' and 'When this creature attacks, sacrifice it at end of combat.'",
        "variants": [],
    },
    "cleave": {
        "rule": "702.148",
        "type": "static",
        "definition": "Cleave [cost] means 'You may cast this spell by paying [cost] rather than paying its mana cost' and 'If this spell's cleave cost was paid, change its text by removing all text found within square brackets in the spell's rules text.'",
        "variants": [],
    },
    "training": {
        "rule": "702.149",
        "type": "triggered",
        "definition": "Training means 'Whenever this creature and at least one other creature with power greater than this creature's power attack, put a +1/+1 counter on this creature.'",
        "variants": [],
    },
    "completed": {
        "rule": "702.150",
        "type": "static",
        "definition": "Completed is a static ability found on some planeswalker cards. Completed means 'If this permanent would enter with one or more loyalty counters on it and the player who cast it chose to pay life for any part of its cost represented by Phyrexian mana symbols, it instead enters the battlefield with that many loyalty counters minus two for each of those mana symbols.'",
        "variants": [],
    },
    "reconfigure": {
        "rule": "702.151",
        "type": "activated",
        "definition": "Reconfigure represents two activated abilities. Reconfigure [cost] means '[Cost]: Attach this permanent to another target creature you control. Activate only as a sorcery' and '[Cost]: Unattach this permanent. Activate only if this permanent is attached to a creature and only as a sorcery.'",
        "variants": [],
    },
    "blitz": {
        "rule": "702.152",
        "type": "static",
        "definition": "Blitz represents three abilities: two static abilities that function while the card with blitz is on the stack, one of which may create a delayed triggered ability, and a static ability that functions while the object with blitz is on the battlefield. 'Blitz [cost]' means 'You may cast this card by paying [cost] rather than paying its mana cost.'",
        "variants": [],
    },
    "casualty": {
        "rule": "702.153",
        "type": "static",
        "definition": "Casualty N means 'As an additional cost to cast this spell, you may sacrifice a creature with power N or greater.'",
        "variants": [],
    },
    "enlist": {
        "rule": "702.154",
        "type": "static",
        "definition": "Enlist means 'As this creature attacks, you may tap up to one untapped creature you control that you didn't choose to attack with and that either has haste or has been under your control continuously since this turn began. When you do, this creature gets +X/+0 until end of turn, where X is the tapped creature's power.'",
        "variants": [],
    },
    "encore": {
        "rule": "702.141",
        "type": "activated",
        "definition": "Encore [cost] means '[Cost], Exile this card from your graveyard: For each opponent, create a token that's a copy of this card that attacks that opponent this turn if able. The tokens gain haste. Sacrifice them at the beginning of the next end step. Activate only as a sorcery.'",
        "variants": [],
    },
    "boast": {
        "rule": "702.142",
        "type": "activated",
        "definition": "Boast adds additional rules to the activated ability that follows it. 'Boast — [Cost]: [Effect]' means '[Cost]: [Effect]. Activate only if this creature attacked this turn and only once each turn.'",
        "variants": [],
    },
    "foretell": {
        "rule": "702.143",
        "type": "static",
        "definition": "Forecast is an activated ability that can be activated only from a player's hand. It's written 'Forecast — [Activated ability].'",
        "variants": [],
    },
    "rebound": {
        "rule": "702.88",
        "type": "static",
        "definition": "Rebound appears on some instants and sorceries. It represents a static ability that functions while the spell with rebound is on the stack and may create a delayed triggered ability. 'Rebound' means 'If this spell was cast from your hand, instead of putting it into your graveyard as it resolves, exile it and, at the beginning of your next upkeep, you may cast this card from exile without paying its mana cost.'",
        "variants": [],
    },
    "umbra armor": {
        "rule": "702.89",
        "type": "static",
        "definition": "Umbra armor is a static ability that appears on some Auras. 'Umbra armor' means 'If enchanted permanent would be destroyed, instead remove all damage marked on it and destroy this Aura.'",
        "variants": [],
    },
    "infect": {
        "rule": "702.90",
        "type": "static",
        "definition": "Infect is a static ability. Damage dealt to a player by a source with infect doesn't cause that player to lose life. Rather, it causes that source's controller to give the player that many poison counters.",
        "variants": [],
    },
    "battle cry": {
        "rule": "702.91",
        "type": "triggered",
        "definition": "Battle cry means 'Whenever this creature attacks, each other attacking creature gets +1/+0 until end of turn.'",
        "variants": [],
    },
    "living weapon": {
        "rule": "702.92",
        "type": "triggered",
        "definition": "Living weapon means 'When this Equipment enters, create a 0/0 black Phyrexian Germ creature token, then attach this Equipment to it.'",
        "variants": [],
    },
    "undying": {
        "rule": "702.93",
        "type": "triggered",
        "definition": "Undying means 'When this permanent is put into a graveyard from the battlefield, if it had no +1/+1 counters on it, return it to the battlefield under its owner's control with a +1/+1 counter on it.'",
        "variants": [],
    },
    "miracle": {
        "rule": "702.94",
        "type": "static",
        "definition": "Miracle represents a static ability that functions while the card with miracle is on the stack and a triggered ability that functions while the card with miracle is on the stack. 'Miracle [cost]' means 'You may reveal this card from your hand if it's the first card you've drawn this turn. When you reveal this card this way, you may cast it by paying [cost] rather than paying its mana cost.'",
        "variants": [],
    },
    "soulbond": {
        "rule": "702.95",
        "type": "triggered",
        "definition": "Soulbond represents two triggered abilities. 'Soulbond' means 'When this creature enters, if you control both this creature and another creature and both are unpaired, you may pair this creature with another unpaired creature you control for as long as both remain creatures on the battlefield under your control' and 'Whenever another creature you control enters, if you control both that creature and this one and both are unpaired, you may pair that creature with this creature for as long as both remain creatures on the battlefield under your control.'",
        "variants": [],
    },
    "overload": {
        "rule": "702.96",
        "type": "static",
        "definition": "Overload is a keyword that represents two static abilities that function while the spell with overload is on the stack. Overload [cost] means 'You may choose to pay [cost] rather than pay this spell's mana cost' and 'If you chose to pay this spell's overload cost, change its text by replacing all instances of the word target with the word each.'",
        "variants": [],
    },
    "scavenge": {
        "rule": "702.97",
        "type": "activated",
        "definition": "Scavenge [cost] means '[Cost], Exile this card from your graveyard: Put a number of +1/+1 counters equal to the power of the card you exiled on target creature. Activate only as a sorcery.'",
        "variants": [],
    },
    "unleash": {
        "rule": "702.98",
        "type": "static",
        "definition": "Unleash is a keyword that represents two static abilities. 'Unleash' means 'You may have this permanent enter with an additional +1/+1 counter on it' and 'This permanent can't block as long as it has a +1/+1 counter on it.'",
        "variants": [],
    },
    "retrace": {
        "rule": "702.81",
        "type": "static",
        "definition": "Retrace is a static ability that functions while the card with retrace is in a player's graveyard. 'Retrace' means 'You may cast this card from your graveyard by discarding a land card as an additional cost to cast it.'",
        "variants": [],
    },
    "devour": {
        "rule": "702.82",
        "type": "triggered",
        "definition": "Devour N means 'As this object enters, you may sacrifice any number of creatures. This permanent enters with N +1/+1 counters on it for each creature sacrificed this way.'",
        "variants": ["devour [quality]"],
    },
    "exalted": {
        "rule": "702.83",
        "type": "triggered",
        "definition": "Exalted means 'Whenever a creature you control attacks alone, that creature gets +1/+1 until end of turn.'",
        "variants": [],
    },
    "unearth": {
        "rule": "702.84",
        "type": "activated",
        "definition": "Unearth [cost] means '[Cost], Return this card from your graveyard to the battlefield. It gains haste. Exile it at the beginning of the next end step. If it would leave the battlefield, exile it instead of putting it anywhere else. Activate only as a sorcery.'",
        "variants": [],
    },
    "cascade": {
        "rule": "702.85",
        "type": "triggered",
        "definition": "Cascade is a triggered ability that functions only while the spell with cascade is on the stack. 'Cascade' means 'When you cast this spell, exile cards from the top of your library until you exile a nonland card with mana value less than this spell's mana value. You may cast that card without paying its mana cost if the resulting spell's mana value is less than this spell's mana value. Then put all cards exiled this way that weren't cast on the bottom of your library in a random order.'",
        "variants": [],
    },
    "annihilator": {
        "rule": "702.86",
        "type": "triggered",
        "definition": "Annihilator N means 'Whenever this creature attacks, defending player sacrifices N permanents.'",
        "variants": [],
    },
    "level up": {
        "rule": "702.87",
        "type": "activated",
        "definition": "Level up [cost] means '[Cost]: Put a level counter on this permanent. Activate only as a sorcery.'",
        "variants": [],
    },
    "squad": {
        "rule": "702.157",
        "type": "static",
        "definition": "Squad represents two linked abilities. The first is a static ability that functions while the creature spell with squad is on the stack. The second is a triggered ability that functions when the creature with squad enters the battlefield. 'Squad [cost]' means 'As an additional cost to cast this spell, you may pay [cost] any number of times' and 'When this creature enters, if its squad cost was paid, create a token that's a copy of it for each time its squad cost was paid.'",
        "variants": [],
    },
    "space sculptor": {
        "rule": "702.158",
        "type": "static",
        "definition": "Space sculptor is a keyword ability that causes creatures to gain sector designations.",
        "variants": [],
    },
    "visit": {
        "rule": "702.159",
        "type": "triggered",
        "definition": "Visit is a keyword ability found on Attraction cards. 'Visit — [Effect]' means 'Whenever you roll to visit your Attractions, if the result is equal to a number that is lit up on this Attraction, [effect].'",
        "variants": [],
    },
    "prototype": {
        "rule": "702.160",
        "type": "static",
        "definition": "Prototype is a static ability that appears on prototype cards that have a secondary set of power, toughness, and mana cost characteristics. A player who casts a spell with prototype can choose to cast that card 'prototyped'. If they do, the alternative set of its power, toughness, and mana cost characteristics are used.",
        "variants": [],
    },
    "living metal": {
        "rule": "702.161",
        "type": "static",
        "definition": "Living metal is a keyword ability found on some Vehicles. 'Living metal' means 'During your turn, this permanent is an artifact creature in addition to its other types.'",
        "variants": [],
    },
    "more than meets the eye": {
        "rule": "702.162",
        "type": "static",
        "definition": "More Than Meets the Eye [cost] means 'You may cast this card converted by paying [cost] rather than paying its mana cost.'",
        "variants": [],
    },
    "for mirrodin": {
        "rule": "702.163",
        "type": "triggered",
        "definition": "For Mirrodin! means 'When this Equipment enters, create a 2/2 red Rebel creature token, then attach this Equipment to it.'",
        "variants": [],
    },
    "toxic": {
        "rule": "702.164",
        "type": "static",
        "definition": "Toxic is a static ability. It is written 'toxic N,' where N is a number. Some rules and effects refer to a creature's total toxic value. A creature's total toxic value is the sum of all N values of toxic abilities that creature has.",
        "variants": [],
    },
    "backup": {
        "rule": "702.165",
        "type": "triggered",
        "definition": "Backup N means 'When this creature enters, put N +1/+1 counters on target creature. If that's another creature, it also gains the non-backup abilities of this creature until end of turn.'",
        "variants": [],
    },
    "bargain": {
        "rule": "702.166",
        "type": "static",
        "definition": "Bargain is a static ability that functions while the spell with bargain is on the stack. 'Bargain' means 'As an additional cost to cast this spell, you may sacrifice an artifact, enchantment, or token.'",
        "variants": [],
    },
    "craft": {
        "rule": "702.167",
        "type": "activated",
        "definition": "Craft represents an activated ability. It is written as 'Craft with [materials] [cost],' where [materials] is a description of one or more objects. It means '[Cost], Exile this permanent, Exile [materials] from among permanents you control and/or cards in your graveyard: Return this card to the battlefield transformed under its owner's control. Activate only as a sorcery.'",
        "variants": [],
    },
    "disguise": {
        "rule": "702.168",
        "type": "static",
        "definition": "Disguise is a static ability that functions in any zone from which you could play the card it's on, and the disguise effect works any time the card is face down. 'Disguise [cost]' means 'You may cast this card as a 2/2 face-down creature with ward {2}, no name, no subtypes, and no mana cost by paying {3} rather than paying its mana cost.'",
        "variants": [],
    },
    "solved": {
        "rule": "702.169",
        "type": "static",
        "definition": "Solved is a keyword ability found on Case cards. 'Solved' is followed by ability text. Together, they represent a static ability, a triggered ability, or an activated ability.",
        "variants": [],
    },
    "plot": {
        "rule": "702.170",
        "type": "static",
        "definition": "Plot [cost] means 'Any time you have priority during your main phase while the stack is empty, you may exile this card from your hand and pay [cost]. It becomes a plotted card.'",
        "variants": [],
    },
    "saddle": {
        "rule": "702.171",
        "type": "activated",
        "definition": "Saddle N means 'Tap any number of other untapped creatures you control with total power N or greater: This permanent becomes saddled until end of turn. Activate only as a sorcery.'",
        "variants": [],
    },
    "spree": {
        "rule": "702.172",
        "type": "static",
        "definition": "Spree is a static ability found on some modal spells. Spree means 'Choose one or more modes. As an additional cost to cast this spell, pay the costs associated with those modes.'",
        "variants": [],
    },
    "freerunning": {
        "rule": "702.173",
        "type": "static",
        "definition": "Freerunning [cost] means 'You may pay [cost] rather than pay this spell's mana cost if a player was dealt combat damage this turn by a creature that, at the time it dealt that damage, was an Assassin creature or a commander under your control.'",
        "variants": [],
    },
    "gift": {
        "rule": "702.174",
        "type": "static",
        "definition": "Gift represents two abilities. The first is a static ability that functions while the card with gift is on the stack, and the second is either a static ability or a triggered ability. The first ability is always 'As an additional cost to cast this spell, you may choose an opponent.'",
        "variants": [],
    },
    "offspring": {
        "rule": "702.175",
        "type": "static",
        "definition": "Offspring represents two abilities. 'Offspring [cost]' means 'You may pay an additional [cost] as you cast this spell' and 'When this permanent enters, if its offspring cost was paid, create a token that's a copy of it, except it's 1/1.'",
        "variants": [],
    },
    "impending": {
        "rule": "702.176",
        "type": "static",
        "definition": "Impending N—[cost] means 'You may choose to pay [cost] rather than pay this spell's mana cost', 'If you chose to pay this permanent's impending cost, it enters with N time counters on it', 'As long as this permanent's impending cost was paid and it has a time counter on it, it's not a creature', and 'At the beginning of your end step, if this permanent's impending cost was paid and it has a time counter on it, remove a time counter from it.'",
        "variants": [],
    },
    "exhaust": {
        "rule": "702.177",
        "type": "activated",
        "definition": "Exhaust is a keyword that adds additional rules to the activated ability that follows it. 'Exhaust — [Cost]: [Effect]' means '[Cost]: [Effect]. Activate only once.'",
        "variants": [],
    },
    "max speed": {
        "rule": "702.178",
        "type": "static",
        "definition": "A max speed ability is a static ability. 'Max speed — [Ability]' means 'As long as your speed is 4, this object has [Ability].' See rule 702.179, \"Start Your Engines!\"",
        "variants": [],
    },
    "start your engines": {
        "rule": "702.179",
        "type": "static",
        "definition": "Start your engines! is a static ability. If a player controls a permanent with start your engines! and that player has no speed, their speed becomes 1. This is a state-based action.",
        "variants": [],
    },
    "harmonize": {
        "rule": "702.180",
        "type": "static",
        "definition": "Harmonize represents three static abilities: one that functions while the card is in a player's graveyard and two that function while the spell with harmonize is on the stack. 'Harmonize [cost]' means 'You may cast this card from your graveyard by paying [cost] and tapping up to one untapped creature you control rather than paying this spell's mana cost', 'If you cast this spell using its harmonize ability, its total cost is reduced by an amount of generic mana equal to the tapped creature's power', and 'If the harmonize cost was paid, exile this card instead of putting it anywhere else any time it would leave the stack.'",
        "variants": [],
    },
    "mobilize": {
        "rule": "702.181",
        "type": "triggered",
        "definition": "Mobilize N means 'Whenever this creature attacks, create N 1/1 red Warrior creature tokens. Those tokens enter tapped and attacking. Sacrifice them at the beginning of the next end step.'",
        "variants": [],
    },
    "job select": {
        "rule": "702.182",
        "type": "triggered",
        "definition": "Job select means 'When this Equipment enters, create a 1/1 colorless Hero creature token, then attach this Equipment to it.'",
        "variants": [],
    },
    "tiered": {
        "rule": "702.183",
        "type": "static",
        "definition": "Tiered is a static ability found on some modal spells. Tiered means 'Choose one. As an additional cost to cast this spell, pay the cost associated with that mode.'",
        "variants": [],
    },
    "station": {
        "rule": "702.184",
        "type": "activated",
        "definition": "Station means 'Tap another untapped creature you control: Put a number of charge counters on this permanent equal to the tapped creature's power. Activate only as a sorcery.'",
        "variants": [],
    },
    "warp": {
        "rule": "702.185",
        "type": "static",
        "definition": "Warp represents two static abilities that function while the card with warp is on the stack, one of which may create a delayed triggered ability. 'Warp [cost]' means 'You may cast this card from your hand by paying [cost] rather than paying its mana cost' and 'If this spell's warp cost was paid, exile the permanent this spell becomes at the beginning of the next end step. Its owner may cast this card after the current turn has ended for as long as it remains exiled.'",
        "variants": [],
    },
    "infinity": {
        "rule": "702.186",
        "type": "static",
        "definition": "Infinity (the mathematical symbol for infinity) is a keyword found on Infinity cards. '∞' is followed by ability text. Together, they represent a static ability. '∞ — [Ability]' means 'As long as this permanent is harnessed, it has [ability].' See rule 701.64, \"Harness.'",
        "variants": [],
    },
    "mayhem": {
        "rule": "702.187",
        "type": "static",
        "definition": "Mayhem [cost] means 'As long as you discarded this card this turn, you may cast it from your graveyard by paying [cost] rather than paying its mana cost.'",
        "variants": [],
    },
    "web-slinging": {
        "rule": "702.188",
        "type": "static",
        "definition": "Web-slinging [cost] means 'You may cast this spell by paying [cost] and returning a tapped creature you control to its owner's hand rather than paying its mana cost.'",
        "variants": [],
    },
    "firebending": {
        "rule": "702.189",
        "type": "triggered",
        "definition": "Firebending N means 'Whenever this creature attacks, add N {R}. Until end of combat, you don't lose this mana as steps and phases end.'",
        "variants": [],
    },
    "sneak": {
        "rule": "702.190",
        "type": "static",
        "definition": "Sneak [cost] means 'Any time you could cast an instant during your declare blockers step, you may cast this spell by paying [cost] and returning an unblocked creature you control to its owner's hand rather than paying this spell's mana cost.'",
        "variants": [],
    },
    "increment": {
        "rule": "702.191",
        "type": "triggered",
        "definition": "Increment means 'Whenever you cast a spell, if this permanent is a creature and the amount of mana spent to cast that spell is greater than this creature's power or this creature's toughness, put a +1/+1 counter on this creature.'",
        "variants": [],
    },
    "paradigm": {
        "rule": "702.192",
        "type": "static",
        "definition": "Paradigm represents two spell abilities, one of which creates a delayed triggered ability. Paradigm means 'If this is the first time a spell you control with this spell's name has resolved this game, at the beginning of each of your precombat main phases for the rest of the game, create a copy of this object in exile. You may cast the copy without paying its mana cost' and 'Exile this spell.'",
        "variants": [],
    },
    "power-up": {
        "rule": "702.193",
        "type": "activated",
        "definition": "Power-up adds additional rules to the activated ability that follows it. 'Power-up — [Cost]: [Effect]' means '[Cost]: [Effect]. If this permanent entered this turn, this ability's cost is reduced by this permanent's mana cost. Activate this ability only once.'",
        "variants": [],
    },
    "teamwork": {
        "rule": "702.194",
        "type": "static",
        "definition": "Teamwork represents a static ability that functions while the spell with teamwork is on the stack. 'Teamwork N' means 'As an additional cost to cast this spell, you may tap any number of creatures you control with total power N or more.'",
        "variants": [],
    },
}

# =============================================================================
# KEYWORD VARIANTS
# =============================================================================

KEYWORD_VARIANTS: Dict[str, Dict[str, Any]] = {
    "hexproof from": {
        "base": "hexproof",
        "rule": "702.11d",
        "definition": "'Hexproof from [quality]' on a permanent means 'This permanent can't be the target of [quality] spells your opponents control or abilities your opponents control from [quality] sources.'",
        "variants": [],
    },
    "equivalent landwalk": {
        "base": "landwalk",
        "variants": ["islandwalk", "mountainwalk", "plainswalk", "swampwalk", "forestwalk", "nonbasic landwalk", "snowwalk"],
    },
    "trample over planeswalkers": {
        "base": "trample",
        "rule": "702.19c",
        "definition": "The controller of a creature with trample over planeswalkers assigns that creature's combat damage as described in rule 702.19b, with one exception. If that creature is attacking a planeswalker, after lethal damage is assigned to all blocking creatures and damage at least equal to the loyalty of the planeswalker the creature is attacking is assigned to that planeswalker, further excess damage may be assigned as the attacking creature's controller chooses among those blocking creatures, that planeswalker, and that planeswalker's controller.",
        "variants": [],
    },
    "emerge from": {
        "base": "emerge",
        "rule": "702.119",
        "definition": "Emerge from [quality] [cost] means 'You may cast this spell by paying [cost] and sacrificing a [quality] permanent rather than paying its mana cost' and 'If you chose to pay this spell's emerge cost, its total cost is reduced by an amount of generic mana equal to the sacrificed permanent's mana value.'",
        "variants": [],
    },
    "megamorph": {
        "base": "morph",
        "rule": "702.37b",
        "definition": "Megamorph is a variant of the morph ability. 'Megamorph [cost]' means 'You may cast this card as a 2/2 face-down creature with no text, no name, no subtypes, and no mana cost by paying {3} rather than paying its mana cost' and 'As this permanent is turned face up, put a +1/+1 counter on it if its megamorph cost was paid to turn it face up.'",
        "variants": [],
    },
    "double agenda": {
        "base": "hidden agenda",
        "rule": "702.106f",
        "definition": "Double agenda is a variant of the hidden agenda ability. As you put a conspiracy card with double agenda into the command zone, you secretly name two different cards rather than one. You don't reveal that more than one name was secretly chosen until you reveal the chosen names.",
        "variants": [],
    },
}

# =============================================================================
# VALIDATION HELPERS
# =============================================================================

def get_all_keywords() -> Set[str]:
    """Return a set of all keyword names."""
    keywords = set(ABILITY_WORDS.keys())
    keywords.update(KEYWORD_ACTIONS.keys())
    keywords.update(KEYWORD_ABILITIES.keys())
    # Add all variants
    for variants in KEYWORD_ABILITIES.values():
        keywords.update(variants)
    for variant_data in KEYWORD_VARIANTS.values():
        keywords.add(variant_data.get("base", ""))
    return keywords

def get_keyword_info(keyword: str) -> Optional[Dict[str, Any]]:
    """Get keyword information from the database."""
    if keyword in ABILITY_WORDS:
        return {**ABILITY_WORDS[keyword], "category": "ability_word"}
    if keyword in KEYWORD_ACTIONS:
        return {**KEYWORD_ACTIONS[keyword], "category": "keyword_action"}
    if keyword in KEYWORD_ABILITIES:
        return {**KEYWORD_ABILITIES[keyword], "category": "keyword_ability"}
    if keyword in KEYWORD_VARIANTS:
        return {**KEYWORD_VARIANTS[keyword], "category": "keyword_variant"}
    return None

def is_valid_keyword(keyword: str) -> bool:
    """Check if a keyword is valid."""
    return keyword in get_all_keywords()

def get_keyword_type(keyword: str) -> str:
    """Get the type of a keyword."""
    if keyword in ABILITY_WORDS:
        return "ability_word"
    if keyword in KEYWORD_ACTIONS:
        return "keyword_action"
    if keyword in KEYWORD_ABILITIES:
        return "keyword_ability"
    if keyword in KEYWORD_VARIANTS:
        return "keyword_variant"
    return "unknown"
