@layer tokens, base, components, pages;

/* ==========================================================================
   设计令牌：字面值只出现在这一层，其余样式全部引用变量
   ========================================================================== */

@layer tokens {
  :root {
    --bg:            #fbfbfa;
    --surface:       #ffffff;
    --surface-sunken:#f5f5f3;
    --text:          #23262b;
    --text-muted:    #6b7280;
    --text-faint:    #9aa0a8;
    --border:        #e6e6e2;
    --border-strong: #d6d6d1;
    --accent:        #0e7490;
    --accent-soft:   #ecf5f7;

    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;

    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 24px;
    --space-6: 32px;
    --space-7: 48px;
    --space-8: 72px;

    --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
                 "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", sans-serif;
    --font-mono: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;

    --container: 960px;
    --reading:   720px;

    --shadow-card: 0 1px 2px rgba(24, 28, 33, .04);
    --shadow-lift: 0 6px 20px -8px rgba(24, 28, 33, .18);
  }

  @media (prefers-color-scheme: dark) {
    :root {
      --bg:            #121417;
      --surface:       #181b1f;
      --surface-sunken:#1e2227;
      --text:          #e7eaee;
      --text-muted:    #9ba3ad;
      --text-faint:    #767e88;
      --border:        #272c32;
      --border-strong: #363c44;
      --accent:        #56c8dd;
      --accent-soft:   #15333b;

      --shadow-card: 0 1px 2px rgba(0, 0, 0, .3);
      --shadow-lift: 0 6px 20px -8px rgba(0, 0, 0, .6);
    }
  }
}

/* ==========================================================================
   基础排版
   ========================================================================== */

@layer base {
  *, *::before, *::after { box-sizing: border-box; }

  html {
    -webkit-text-size-adjust: 100%;
    scroll-behavior: smooth;
  }

  body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font-sans);
    font-size: 16px;
    line-height: 1.75;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
  }

  h1, h2, h3 {
    margin: 0;
    font-weight: 600;
    line-height: 1.35;
    letter-spacing: -.01em;
  }

  p { margin: 0; }

  a {
    color: var(--accent);
    text-decoration: none;
    text-underline-offset: 3px;
  }

  a:hover { text-decoration: underline; }

  img, svg { max-width: 100%; height: auto; }

  ul { margin: 0; padding: 0; list-style: none; }

  :focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
    border-radius: var(--radius-sm);
  }

  .wrap {
    width: 100%;
    max-width: var(--container);
    margin: 0 auto;
    padding: 0 var(--space-5);
  }

  .skip-link {
    position: absolute;
    left: -9999px;
    top: var(--space-3);
    z-index: 10;
    padding: var(--space-2) var(--space-4);
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
  }

  .skip-link:focus { left: var(--space-5); }
}

/* ==========================================================================
   组件
   ========================================================================== */

@layer components {

  /* ---- 站点头部 ---- */
  .site-header {
    border-bottom: 1px solid var(--border);
    background: color-mix(in srgb, var(--bg) 88%, transparent);
    backdrop-filter: saturate(160%) blur(8px);
    position: sticky;
    top: 0;
    z-index: 5;
  }

  .site-header__inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-5);
    min-height: 64px;
    flex-wrap: wrap;
    padding-top: var(--space-2);
    padding-bottom: var(--space-2);
  }

  .brand {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    color: var(--text);
    font-weight: 600;
    letter-spacing: -.01em;
  }

  .brand:hover { text-decoration: none; }

  .brand__mark {
    width: 22px;
    height: 22px;
    border-radius: 50% 50% 50% 4px;
    background: linear-gradient(135deg, var(--accent), color-mix(in srgb, var(--accent) 45%, var(--surface)));
    flex: none;
  }

  .brand__name { font-size: 15.5px; }

  .site-nav {
    display: flex;
    align-items: center;
    gap: var(--space-1);
    flex-wrap: wrap;
  }

  .site-nav a {
    padding: 6px var(--space-3);
    border-radius: 999px;
    color: var(--text-muted);
    font-size: 14.5px;
    transition: background-color .15s ease, color .15s ease;
  }

  .site-nav a:hover {
    background: var(--surface-sunken);
    color: var(--text);
    text-decoration: none;
  }

  .site-nav a.is-active {
    background: var(--accent-soft);
    color: var(--accent);
    font-weight: 500;
  }

  /* ---- 页脚 ---- */
  .site-footer {
    margin-top: var(--space-8);
    border-top: 1px solid var(--border);
    padding: var(--space-6) 0 var(--space-7);
  }

  .site-footer__inner {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    font-size: 13.5px;
    color: var(--text-faint);
  }

  .site-footer__copy { color: var(--text-muted); }

  .site-footer__meta {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    flex-wrap: wrap;
  }

  .site-footer__meta a { color: var(--text-faint); }
  .site-footer__meta a:hover { color: var(--accent); }

  .site-footer__sep { color: var(--border-strong); }

  .site-footer__note { max-width: 46em; }

  /* ---- 区块标题 ---- */
  .section { margin-top: var(--space-8); }

  .section__head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: var(--space-4);
    padding-bottom: var(--space-3);
    border-bottom: 1px solid var(--border);
    margin-bottom: var(--space-5);
  }

  .section__head h2 { font-size: 18px; }

  .section__more {
    font-size: 13.5px;
    color: var(--text-muted);
    flex: none;
  }

  .section__more:hover { color: var(--accent); }

  /* ---- 笔记列表 ---- */
  .post-list { display: flex; flex-direction: column; }

  .post-item + .post-item { border-top: 1px solid var(--border); }

  .post-item__link {
    display: flex;
    align-items: baseline;
    gap: var(--space-4);
    padding: var(--space-4) var(--space-2);
    margin: 0 calc(var(--space-2) * -1);
    border-radius: var(--radius-md);
    color: var(--text);
    transition: background-color .15s ease;
  }

  .post-item__link:hover {
    background: var(--surface-sunken);
    text-decoration: none;
  }

  .post-item__link--stacked {
    flex-direction: column;
    align-items: stretch;
    gap: var(--space-2);
  }

  .post-item__row {
    display: flex;
    align-items: center;
    gap: var(--space-3);
  }

  .post-item__date {
    font-family: var(--font-mono);
    font-size: 12.5px;
    color: var(--text-faint);
    flex: none;
  }

  .post-item__title {
    font-size: 16px;
    font-weight: 500;
    flex: 1 1 auto;
  }

  .post-item__link:hover .post-item__title { color: var(--accent); }

  .post-item__tag {
    font-size: 12px;
    color: var(--text-muted);
    background: var(--surface-sunken);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 1px var(--space-3);
    flex: none;
  }

  .post-item__summary {
    font-size: 14.5px;
    line-height: 1.7;
    color: var(--text-muted);
  }

  /* ---- 卡片 ---- */
  .card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: var(--space-4);
  }

  .card-grid--wide { grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); }

  .card {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: var(--space-5);
    box-shadow: var(--shadow-card);
    transition: border-color .15s ease, box-shadow .15s ease, transform .15s ease;
  }

  .card:hover {
    border-color: var(--border-strong);
    box-shadow: var(--shadow-lift);
    transform: translateY(-2px);
  }

  .card__top {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: var(--space-3);
  }

  .card__kind {
    font-size: 12px;
    color: var(--accent);
    letter-spacing: .02em;
  }

  .card__year {
    font-family: var(--font-mono);
    font-size: 12px;
    color: var(--text-faint);
  }

  .card__title { font-size: 16.5px; }

  .card__desc {
    font-size: 14.5px;
    line-height: 1.72;
    color: var(--text-muted);
    flex: 1 1 auto;
  }

  .tags {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    padding-top: var(--space-1);
  }

  .tags li {
    font-size: 12px;
    color: var(--text-faint);
    background: var(--surface-sunken);
    border-radius: var(--radius-sm);
    padding: 2px var(--space-2);
  }

  /* ---- 正文排版 ---- */
  .prose {
    max-width: var(--reading);
    font-size: 16px;
    line-height: 1.85;
    color: var(--text);
  }

  .prose > * + * { margin-top: var(--space-5); }

  .prose h2 {
    font-size: 18px;
    margin-top: var(--space-6);
  }

  .prose ul {
    list-style: none;
    max-width: var(--reading);
  }

  .prose ul li {
    position: relative;
    padding-left: var(--space-5);
  }

  .prose ul li + li { margin-top: var(--space-2); }

  .prose ul li::before {
    content: "";
    position: absolute;
    left: 6px;
    top: .78em;
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background: var(--border-strong);
  }

  .prose pre {
    overflow-x: auto;
    background: var(--surface-sunken);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: var(--space-4) var(--space-5);
    font-size: 13.5px;
    line-height: 1.7;
  }

  .prose code {
    font-family: var(--font-mono);
    font-size: .92em;
  }

  .prose :not(pre) > code {
    background: var(--surface-sunken);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 1px 5px;
  }

  /* ---- 通用页头 ---- */
  .page-head {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    padding-bottom: var(--space-5);
    border-bottom: 1px solid var(--border);
    margin-top: var(--space-7);
  }

  .page-head h1 { font-size: clamp(26px, 4vw, 34px); }

  .page-head__lead {
    max-width: var(--reading);
    color: var(--text-muted);
    font-size: 15.5px;
  }

  /* ---- 联系列表 ---- */
  .contact-list {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
  }

  .contact-list li {
    display: flex;
    align-items: baseline;
    gap: var(--space-4);
  }

  .contact-list__label {
    width: 5em;
    flex: none;
    font-size: 13.5px;
    color: var(--text-faint);
  }
}

/* ==========================================================================
   页面级样式
   ========================================================================== */

@layer pages {

  main.wrap { padding-top: var(--space-2); }

  /* ---- 首页 ---- */
  .hero {
    max-width: var(--reading);
    padding: var(--space-8) 0 var(--space-6);
  }

  .hero__name {
    font-size: clamp(30px, 5.2vw, 44px);
    letter-spacing: -.02em;
  }

  .hero__tagline {
    margin-top: var(--space-3);
    font-size: 17px;
    color: var(--accent);
  }

  .hero__intro {
    margin-top: var(--space-5);
    font-size: 16.5px;
    line-height: 1.85;
    color: var(--text-muted);
  }

  /* ---- 文章页 ---- */
  .post { padding-top: var(--space-7); }

  .post__head {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    padding-bottom: var(--space-6);
    border-bottom: 1px solid var(--border);
  }

  .post__meta {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-family: var(--font-mono);
    font-size: 12.5px;
    color: var(--text-faint);
  }

  .post__dot { color: var(--border-strong); }

  .post__title { font-size: clamp(24px, 4.2vw, 32px); }

  .post__summary {
    max-width: var(--reading);
    color: var(--text-muted);
    font-size: 15.5px;
  }

  .post .prose { margin-top: var(--space-6); }

  .post__foot {
    margin-top: var(--space-7);
    padding-top: var(--space-5);
    border-top: 1px solid var(--border);
    font-size: 14px;
  }

  /* ---- 空页 ---- */
  .page--empty {
    padding: var(--space-8) 0;
    max-width: var(--reading);
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
  }

  .page--empty h1 { font-size: 28px; }

  /* ---- 窄屏 ---- */
  @media (max-width: 640px) {
    .site-header__inner { min-height: auto; padding: var(--space-3) 0; }
    .site-nav { gap: 0; }
    .site-nav a { padding: 6px var(--space-2); font-size: 14px; }
    .post-item__link { flex-wrap: wrap; gap: var(--space-2); }
    .post-item__title { flex-basis: 100%; order: 3; }
    .hero { padding-top: var(--space-7); }
    .card { padding: var(--space-4); }
    .contact-list li { flex-direction: column; gap: var(--space-1); }
  }

  @media (prefers-reduced-motion: reduce) {
    html { scroll-behavior: auto; }
    .card, .site-nav a, .post-item__link { transition: none; }
    .card:hover { transform: none; }
  }
}
