<!-- TodoItem Template - Child Component -->
<div class="todo-item {completed|then:todo-item--completed} {is_editing|then:todo-item--editing}">
    <!-- Non-Editing View -->
    <if condition="is_editing|not">
        <div class="todo-item__view">
            <!-- Checkbox -->
            <button
                class="todo-item__checkbox"
                data-livecomponent-action="toggle"
                aria-label="Toggle todo"
            >
                <if condition="completed">
                    <span class="todo-item__check">✓</span>
                </if>
            </button>

            <!-- Title -->
            <div
                class="todo-item__title"
                data-livecomponent-action="startEdit"
                title="Click to edit"
            >
                {title}
            </div>

            <!-- Created Date -->
            <div class="todo-item__meta">
                <span class="todo-item__date">{formatted_date}</span>
            </div>

            <!-- Delete Button -->
            <button
                class="todo-item__delete"
                data-livecomponent-action="delete"
                aria-label="Delete todo"
                title="Delete todo"
            >
                ✕
            </button>
        </div>
    </if>

    <!-- Editing View -->
    <if condition="is_editing">
        <form class="todo-item__edit" data-livecomponent-action="saveEdit">
            <input
                type="text"
                name="newTitle"
                class="todo-item__edit-input"
                value="{title}"
                autofocus
                required
            />

            <div class="todo-item__edit-actions">
                <button
                    type="submit"
                    class="todo-item__save"
                    title="Save changes"
                >
                    Save
                </button>

                <button
                    type="button"
                    class="todo-item__cancel"
                    data-livecomponent-action="cancelEdit"
                    title="Cancel editing"
                >
                    Cancel
                </button>
            </div>
        </form>
    </if>
</div>

<style>
.todo-item {
    padding: 1rem;
    margin-bottom: 0.5rem;
    background: white;
    border: 1px solid #e0e0e0;
    border-radius: 4px;
    transition: all 0.2s;
}

.todo-item:hover {
    border-color: #ccc;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}

.todo-item--completed {
    background: #f9f9f9;
    opacity: 0.7;
}

.todo-item--editing {
    border-color: #4CAF50;
    background: #f0f8f0;
}

.todo-item__view {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.todo-item__checkbox {
    width: 24px;
    height: 24px;
    border: 2px solid #ddd;
    border-radius: 4px;
    background: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: all 0.2s;
}

.todo-item__checkbox:hover {
    border-color: #4CAF50;
    background: #f0f8f0;
}

.todo-item--completed .todo-item__checkbox {
    background: #4CAF50;
    border-color: #4CAF50;
}

.todo-item__check {
    color: white;
    font-size: 14px;
    font-weight: bold;
}

.todo-item__title {
    flex: 1;
    font-size: 1rem;
    color: #333;
    cursor: pointer;
    padding: 0.25rem;
    border-radius: 4px;
    transition: background 0.2s;
}

.todo-item__title:hover {
    background: #f5f5f5;
}

.todo-item--completed .todo-item__title {
    text-decoration: line-through;
    color: #999;
}

.todo-item__meta {
    display: flex;
    gap: 0.5rem;
    font-size: 0.85rem;
    color: #999;
}

.todo-item__date {
    white-space: nowrap;
}

.todo-item__delete {
    width: 28px;
    height: 28px;
    border: 1px solid #ddd;
    border-radius: 4px;
    background: white;
    color: #999;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: all 0.2s;
    font-size: 16px;
}

.todo-item__delete:hover {
    background: #ff5252;
    border-color: #ff5252;
    color: white;
}

/* Editing Mode Styles */

.todo-item__edit {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.todo-item__edit-input {
    width: 100%;
    padding: 0.5rem;
    border: 2px solid #4CAF50;
    border-radius: 4px;
    font-size: 1rem;
}

.todo-item__edit-input:focus {
    outline: none;
    border-color: #45a049;
}

.todo-item__edit-actions {
    display: flex;
    gap: 0.5rem;
}

.todo-item__save,
.todo-item__cancel {
    flex: 1;
    padding: 0.5rem;
    border: none;
    border-radius: 4px;
    font-size: 0.9rem;
    cursor: pointer;
    transition: background 0.2s;
}

.todo-item__save {
    background: #4CAF50;
    color: white;
}

.todo-item__save:hover {
    background: #45a049;
}

.todo-item__cancel {
    background: #f5f5f5;
    color: #666;
    border: 1px solid #ddd;
}

.todo-item__cancel:hover {
    background: #e8e8e8;
}
</style>
