{% extends "base.html" %}

{% block title %}{% if query %}「{{ query }}」の検索結果{% else %}検索{% endif %} - {{ site_name }}{% endblock %}

{% block content %}
<div class="search-page">
    <header class="search-header">
        <h1>検索</h1>
        <form class="search-form" action="/search" method="GET">
            <input type="text" name="q" value="{{ query }}" placeholder="キーワードを入力..." autofocus>
            <button type="submit">検索</button>
        </form>
    </header>

    {% if query %}
    <div class="search-results">
        <p class="result-count">「{{ query }}」の検索結果: {{ total }}件</p>

        {% if posts %}
        <div class="posts-list">
            {% for post in posts %}
            <article class="post-item">
                <h2><a href="/post/{{ post.slug }}">{{ post.title }}</a></h2>
                <p class="post-excerpt">{{ post.excerpt }}</p>
                <time class="post-date">{{ post.created_at[:10] }}</time>
            </article>
            {% endfor %}
        </div>

        {% if total_pages > 1 %}
        <nav class="pagination">
            {% if page > 1 %}
            <a href="/search?q={{ query }}&page={{ page - 1 }}" class="prev">前へ</a>
            {% endif %}
            <span class="current">{{ page }} / {{ total_pages }}</span>
            {% if page < total_pages %}
            <a href="/search?q={{ query }}&page={{ page + 1 }}" class="next">次へ</a>
            {% endif %}
        </nav>
        {% endif %}
        {% else %}
        <p class="no-results">該当する記事が見つかりませんでした。</p>
        {% endif %}
    </div>
    {% endif %}
</div>

<style>
.search-page {
    max-width: 800px;
    margin: 0 auto;
    padding: 2rem;
}

.search-header {
    text-align: center;
    margin-bottom: 3rem;
}

.search-header h1 {
    margin-bottom: 1.5rem;
}

.search-form {
    display: flex;
    gap: 0.5rem;
    max-width: 500px;
    margin: 0 auto;
}

.search-form input {
    flex: 1;
    padding: 0.75rem 1rem;
    border: 2px solid var(--color-border);
    border-radius: 0.5rem;
    font-size: 1rem;
}

.search-form input:focus {
    outline: none;
    border-color: var(--color-primary);
}

.search-form button {
    padding: 0.75rem 1.5rem;
    background: var(--color-primary);
    color: white;
    border: none;
    border-radius: 0.5rem;
    font-size: 1rem;
    cursor: pointer;
}

.search-form button:hover {
    opacity: 0.9;
}

.result-count {
    color: var(--color-text-muted);
    margin-bottom: 2rem;
}

.posts-list {
    display: flex;
    flex-direction: column;
    gap: 2rem;
}

.post-item {
    padding-bottom: 2rem;
    border-bottom: 1px solid var(--color-border);
}

.post-item h2 {
    margin-bottom: 0.5rem;
}

.post-item h2 a {
    color: inherit;
    text-decoration: none;
}

.post-item h2 a:hover {
    color: var(--color-primary);
}

.post-excerpt {
    color: var(--color-text-muted);
    line-height: 1.6;
    margin-bottom: 0.5rem;
}

.post-date {
    font-size: 0.875rem;
    color: var(--color-text-muted);
}

.pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 1rem;
    margin-top: 3rem;
}

.pagination a {
    padding: 0.5rem 1rem;
    background: var(--color-primary);
    color: white;
    text-decoration: none;
    border-radius: 0.25rem;
}

.no-results {
    text-align: center;
    color: var(--color-text-muted);
    padding: 3rem;
}
</style>
{% endblock %}
