---
import SiteNav from '../../SiteNav.astro';
import { site } from '../../../lib/siteConfig';
import { tagSlug, type ArchiveYear } from '../../../lib/posts';

type Props = {
  archiveYears: ArchiveYear[];
};

const { archiveYears } = Astro.props;
---

<main class="yar-archive-page">
  <div class="yar-shell">
    <header class="yar-header">
      <div class="yar-header-inner">
        <div class="yar-title-box">
          <h1 class="yar-title">{site.title}</h1>
        </div>
        <SiteNav />
      </div>
    </header>

    <section class="yar-archive" aria-label="Archive">
      {archiveYears.length === 0 ? (
        <p class="yar-empty">No published posts yet.</p>
      ) : (
        <div class="yar-archive-list">
          {archiveYears.map((group) => (
            <section class="yar-archive-year" aria-label={group.year}>
              <div class="yar-archive-year-label">
                <div class="yar-archive-year-meta">
                  <h3>{group.year}</h3>
                  <p>共 {group.posts.length} 篇</p>
                </div>
              </div>
              <ol>
                {group.posts.map((post) => (
                  <li>
                    <time datetime={post.date}>
                      {post.date
                        ? new Date(post.date).toLocaleDateString(site.language, {
                            month: '2-digit',
                            day: '2-digit'
                          })
                        : '----'}
                    </time>
                    <a class="yar-archive-post" href={post.url}>{post.title}</a>
                    {post.tags.length > 0 && (
                      <ul class="yar-tags" aria-label="Tags">
                        {post.tags.map((tag) => (
                          <li>
                            <a href={`/tags/${tagSlug(tag)}/`}>{tag}</a>
                          </li>
                        ))}
                      </ul>
                    )}
                  </li>
                ))}
              </ol>
            </section>
          ))}
        </div>
      )}
    </section>

    <footer class="yar-footer">
      <p>由 osaet 构建</p>
      <p>© 2026 Osaet. All rights reserved.</p>
    </footer>
  </div>
</main>

<style>
  .yar-archive-page {
    min-height: 100vh;
    background: #ffffff;
    color: #1c211b;
  }

  .yar-shell {
    display: flex;
    min-height: 100vh;
    flex-direction: column;
    width: 100%;
    padding: 40px 0 5vh;
  }

  .yar-header {
    border-bottom: 1px solid rgba(28, 33, 27, 0.18);
  }

  .yar-header-inner {
    display: flex;
    justify-content: space-between;
    gap: 24px;
    align-items: center;
    width: 50vw;
    margin: 0 auto;
    padding-bottom: 28px;
  }

  .yar-shell :global(.site-nav) {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25em;
    justify-content: flex-end;
    margin: 0;
    border-radius: 0.75em;
    background: #ffffff;
    box-shadow: 0 0em 0.2em rgb(29 53 87 / 13%);
    padding: 0.5em 1.4em;
    color: #53605a;
    font-family:
      ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
      sans-serif;
    font-size: 0.86rem;
  }

  .yar-shell :global(.site-nav a) {
    border-radius: 0.65em;
    color: inherit;
    padding: 0.55em 0.8em;
    text-decoration: none;
  }

  .yar-shell :global(.site-nav a:hover) {
    background: #eef1ed;
    text-decoration: none;
  }

  .yar-title-box {
    border-radius: 0.7em;
    background: #1d3557;
    padding: 1em 1.4em;
  }

  .yar-title {
    margin: 0;
    color: #ffffff;
    font-family:
      "Heti Hei", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei",
      sans-serif;
    font-size: 1em;
    letter-spacing: 0.16em;
    line-height: 1.15;
  }

  .yar-archive {
    width: 100%;
    margin: 0;
    padding: 36px 0 0;
    color: #3d3d3f;
    font-family:
      "Heti Hei", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei",
      "Noto Sans CJK SC", "Source Han Sans SC", "Source Han Sans CN",
      sans-serif;
    font-size: 16px;
    letter-spacing: 1px;
    line-height: 1.7em;
  }

  .yar-archive-list {
    display: grid;
    gap: 3.2em 0;
  }

  .yar-archive-year {
    display: grid;
    grid-template-columns: 25% 50% 25%;
  }

  .yar-archive-year-label {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-direction: column;
  }

  .yar-archive-year-meta {
    display: grid;
    gap: 0.35em;
    align-content: start;
    justify-items: center;
  }

  .yar-archive-year h3 {
    margin: 0;
    color: #8a9089;
    font-size: 1em;
    font-weight: 600;
    letter-spacing: 0.04em;
    line-height: 1.7em;
  }

  .yar-archive-year-label p {
    margin: 0;
    color: #b0b5ae;
    font-family:
      ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
      sans-serif;
    font-size: 0.72rem;
    line-height: 1.4;
  }

  .yar-archive-year ol {
    display: grid;
    gap: 0.8em;
    grid-column: 2;
    padding: 0;
    margin: 0;
    list-style: none;
  }

  .yar-archive-year > ol > li {
    display: grid;
    grid-template-columns: 5.5em minmax(0, 1fr) auto;
    gap: 1em;
    align-items: flex-start;
    border-bottom: 0;
    padding: 0;
  }

  .yar-archive-year time {
    color: #7a8375;
    font-family:
      ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
      sans-serif;
    font-size: 0.78rem;
    line-height: 1.7em;
    white-space: nowrap;
  }

  .yar-archive-post {
    color: #1c211b;
    line-height: 1.5;
    text-decoration: none;
  }

  .yar-archive-post:hover {
    color: #1d3557;
    text-decoration: underline;
    text-decoration-color: rgb(29 53 87 / 32%);
    text-decoration-thickness: 0.12em;
    text-underline-offset: 0.2em;
  }

  .yar-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 0.45em;
    justify-content: flex-end;
    padding: 0;
    margin: 0;
    list-style: none;
  }

  .yar-tags li {
    display: inline-flex;
  }

  .yar-tags a {
    border-radius: 999em;
    background: #f3f6f4;
    box-shadow: inset 0 0 0 1px rgb(29 53 87 / 7%);
    color: #53605a;
    font-family:
      ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
      sans-serif;
    font-size: 0.72rem;
    line-height: 1;
    padding: 0.5em 0.75em;
    text-decoration: none;
  }

  .yar-tags a:hover {
    background: #e8eee9;
  }

  .yar-empty {
    margin: 0;
    color: #596254;
    text-align: center;
  }

  .yar-footer {
    width: 50vw;
    margin: 2em auto 0;
    color: #8a9089;
    font-family:
      ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
      sans-serif;
    font-size: 0.78rem;
    line-height: 1.8;
    text-align: center;
  }

  .yar-footer p {
    margin: 0;
  }

  @media (max-width: 760px) {
    .yar-shell {
      padding-top: 24px;
    }

    .yar-header-inner,
    .yar-footer {
      width: calc(100% - 28px);
    }

    .yar-header-inner {
      flex-direction: column;
      align-items: flex-start;
      gap: 16px;
    }

    .yar-archive {
      width: calc(100% - 28px);
      margin: 0 auto;
    }

    .yar-archive-year {
      grid-template-columns: 1fr;
      gap: 0.8em;
    }

    .yar-archive-year-label {
      justify-content: flex-start;
      padding-right: 0;
    }

    .yar-archive-year ol {
      grid-column: auto;
    }

    .yar-archive-year > ol > li {
      grid-template-columns: 1fr;
      gap: 0.55em;
    }

    .yar-tags {
      justify-content: flex-start;
    }
  }
</style>
