@layer reset, tokens, base, layout, components, utilities;

@layer reset {
  *, *::before, *::after { box-sizing: border-box; margin: 0; }
  img, picture, video { display: block; max-width: 100%; }
  input, button, textarea, select { font: inherit; }
  ol, ul { padding: 0; list-style: none; }
}

@layer tokens {
  :root {
    color-scheme: light;
    --bg:       #fafaf7;
    --surface:  #ffffff;
    --fg:       #18181b;
    --muted:    #6b7280;
    --line:     #e5e7eb;
    --accent:   oklch(58% 0.22 25);
    --accent-2: oklch(62% 0.18 250);
    --success:  oklch(60% 0.15 145);
    --kill-bg:  oklch(96% 0.04 25);
    --kill-line:oklch(75% 0.15 25);
    --warn-bg:  oklch(96% 0.05 60);
    --warn-line:oklch(75% 0.15 60);

    --radius-sm: 0.375rem;
    --radius:    0.75rem;
    --radius-lg: 1.25rem;

    --s-1: 0.25rem;
    --s-2: 0.5rem;
    --s-3: 1rem;
    --s-4: 1.5rem;
    --s-5: 2.5rem;
    --s-6: 4rem;
    --s-7: 6rem;

    --font-sans: -apple-system, BlinkMacSystemFont, "Pretendard", "Apple SD Gothic Neo", system-ui, sans-serif;
    --font-mono: "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
    --font-serif: "Iowan Old Style", "Apple Garamond", Baskerville, "Times New Roman", serif;

    --shadow-sm: 0 1px 2px rgb(0 0 0 / 0.04);
    --shadow:    0 4px 16px rgb(0 0 0 / 0.06), 0 1px 3px rgb(0 0 0 / 0.04);
  }
}

@layer base {
  html { scroll-behavior: smooth; }
  body {
    font-family: var(--font-sans);
    background: var(--bg);
    color: var(--fg);
    line-height: 1.65;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
  }

  h1, h2, h3 { line-height: 1.2; letter-spacing: -0.02em; font-weight: 700; }

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

  code {
    font-family: var(--font-mono);
    font-size: 0.875em;
    padding: 0.1em 0.35em;
    background: color-mix(in oklch, var(--accent-2), white 92%);
    border-radius: var(--radius-sm);
  }

  cite { display: block; margin-top: var(--s-2); color: var(--muted); font-size: 0.875rem; font-style: normal; }
  small { color: var(--muted); font-size: 0.8125rem; }
  em { font-style: italic; }
}

@layer layout {
  main { max-width: 56rem; margin-inline: auto; padding-inline: var(--s-4); }

  .hero {
    max-width: 56rem;
    margin-inline: auto;
    padding: var(--s-7) var(--s-4) var(--s-6);
    text-align: center;
    border-bottom: 1px solid var(--line);
  }

  .section {
    padding-block: var(--s-7);
    border-bottom: 1px solid var(--line);
    &:last-of-type { border-bottom: 0; }
  }

  .section h2 {
    font-size: clamp(1.75rem, 3.5vw, 2.5rem);
    margin-bottom: var(--s-4);
  }

  .section .prose {
    font-size: 1.0625rem;
    color: var(--fg);
    max-width: 42rem;
    margin-bottom: var(--s-5);
  }

  footer {
    max-width: 56rem;
    margin-inline: auto;
    padding: var(--s-5) var(--s-4) var(--s-6);
    color: var(--muted);
    font-size: 0.875rem;
    text-align: center;
    & .meta { margin-top: var(--s-2); font-size: 0.8125rem; }
  }
}

@layer components {

  /* ---- Hero ---- */
  .eyebrow {
    text-transform: uppercase;
    letter-spacing: 0.18em;
    font-size: 0.75rem;
    color: var(--accent);
    font-weight: 600;
    margin-bottom: var(--s-3);
  }

  .hero h1 {
    font-size: clamp(2.25rem, 5vw, 3.75rem);
    margin-bottom: var(--s-4);
  }

  .hero .accent {
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
  }

  .hero .lead {
    font-size: 1.0625rem;
    color: var(--muted);
    max-width: 38rem;
    margin: 0 auto var(--s-5);
  }

  .toc {
    display: flex;
    flex-wrap: wrap;
    gap: var(--s-2);
    justify-content: center;

    & a {
      padding: var(--s-2) var(--s-3);
      border: 1px solid var(--line);
      border-radius: 999px;
      background: var(--surface);
      color: var(--fg);
      font-size: 0.875rem;
      transition: border-color 120ms, color 120ms;

      &:hover {
        border-color: var(--accent);
        color: var(--accent);
        text-decoration: none;
      }
    }
  }

  .section-tag {
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.2em;
    color: var(--accent);
    margin-bottom: var(--s-2);
  }

  /* ---- Card ---- */
  .card {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: var(--s-4);
    box-shadow: var(--shadow-sm);
    margin-block: var(--s-3);

    & h3 { font-size: 1.125rem; margin-bottom: var(--s-2); }
  }

  .card.highlight {
    border-color: color-mix(in oklch, var(--accent), white 60%);
    background: color-mix(in oklch, var(--accent), white 95%);
  }

  .card.warn { border-color: var(--warn-line); background: var(--warn-bg); }

  .caption { margin-top: var(--s-2); color: var(--muted); font-size: 0.875rem; }

  /* ---- Steps list ---- */
  .steps {
    counter-reset: step;
    display: grid;
    gap: var(--s-2);

    & li {
      counter-increment: step;
      padding-left: var(--s-5);
      position: relative;

      &::before {
        content: counter(step);
        position: absolute;
        left: 0;
        top: 2px;
        width: 1.5rem;
        height: 1.5rem;
        background: var(--accent);
        color: white;
        border-radius: 50%;
        display: grid;
        place-items: center;
        font-size: 0.8125rem;
        font-weight: 700;
      }
    }
  }

  /* ---- Timeline ---- */
  .timeline {
    display: grid;
    gap: var(--s-3);
    margin-bottom: var(--s-5);
  }

  .iter {
    --c: oklch(60% 0.18 var(--hue, 0));
    background: var(--surface);
    border: 1px solid var(--line);
    border-left: 6px solid var(--c);
    border-radius: var(--radius);
    padding: var(--s-4);
    box-shadow: var(--shadow-sm);

    & header {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      gap: var(--s-3);
      margin-bottom: var(--s-3);
    }

    & .vtag {
      font-family: var(--font-mono);
      font-size: 0.875rem;
      font-weight: 700;
      color: white;
      background: var(--c);
      padding: 0.2em 0.6em;
      border-radius: var(--radius-sm);
    }

    & h3 {
      font-size: 1.25rem;
      flex: 1;
    }

    & .metric {
      font-family: var(--font-mono);
      font-size: 0.8125rem;
      color: var(--muted);
      background: color-mix(in oklch, var(--c), white 92%);
      padding: 0.25em 0.6em;
      border-radius: var(--radius-sm);
    }

    & p + p { margin-top: var(--s-2); }
    & .weak { color: var(--muted); font-size: 0.9375rem; }
    & .strong { color: var(--success); font-weight: 500; }
  }

  /* ---- Data table ---- */
  table.data {
    width: 100%;
    border-collapse: collapse;
    margin-top: var(--s-3);
    font-size: 0.9375rem;

    & th, & td {
      padding: var(--s-3);
      text-align: left;
      border-bottom: 1px solid var(--line);
    }

    & th {
      font-weight: 600;
      color: var(--muted);
      background: color-mix(in oklch, var(--accent), white 96%);
      font-size: 0.8125rem;
      text-transform: uppercase;
      letter-spacing: 0.05em;
    }

    & tbody tr:last-child td { border-bottom: 0; }

    & tr.final {
      background: color-mix(in oklch, var(--success), white 92%);

      & td { font-weight: 600; }
    }
  }

  /* ---- Diff grid ---- */
  .diff-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(18rem, 1fr));
    gap: var(--s-3);
  }

  .diff {
    background: var(--kill-bg);
    border: 1px solid var(--kill-line);
    border-radius: var(--radius);
    padding: var(--s-4);

    & h3 {
      font-size: 1rem;
      margin-bottom: var(--s-3);
      font-family: var(--font-mono);
      color: var(--accent);
    }

    & .killed {
      display: grid;
      gap: var(--s-2);

      & li {
        padding-left: var(--s-4);
        position: relative;
        font-size: 0.9375rem;
        color: var(--fg);

        &::before {
          content: "−";
          position: absolute;
          left: 0;
          top: 0;
          color: var(--accent);
          font-weight: 700;
          font-size: 1.1em;
        }
      }
    }
  }

  /* ---- Grid of cards ---- */
  .grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
    gap: var(--s-3);
  }

  /* ---- Code block ---- */
  pre.code {
    background: #0f1419;
    color: #e6e8ee;
    padding: var(--s-4);
    border-radius: var(--radius);
    overflow-x: auto;
    font-size: 0.875rem;
    line-height: 1.55;
    box-shadow: var(--shadow);
    margin-top: var(--s-3);

    & code {
      font-family: var(--font-mono);
      background: transparent;
      padding: 0;
      color: inherit;
      font-size: inherit;
    }
  }

  /* ---- Closing ---- */
  .closing { text-align: center; }

  .quote {
    font-family: var(--font-serif);
    font-size: clamp(1.25rem, 3vw, 1.75rem);
    line-height: 1.5;
    max-width: 36rem;
    margin: var(--s-4) auto 0;
    color: var(--fg);
    font-style: italic;
  }
}

@layer utilities {
  .section.closing { border-bottom: 0; }
}

@media (max-width: 640px) {
  .hero { padding-block: var(--s-5) var(--s-5); }
  .section { padding-block: var(--s-5); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
