@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);    /* Tesla red */
    --accent-2: oklch(62% 0.18 250);   /* SpaceX blue */
    --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-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);
    --shadow-lg: 0 20px 50px rgb(0 0 0 / 0.08);
  }
}

@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; }
  }

  blockquote {
    font-family: var(--font-serif);
    font-style: italic;
    font-size: 1.05rem;
    color: var(--fg);
    padding-left: var(--s-3);
    border-left: 3px solid var(--accent);
    margin-block: var(--s-3);
  }

  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; }
}

@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.5rem, 6vw, 4.5rem);
    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.125rem;
    color: var(--muted);
    max-width: 36rem;
    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);
    }

    & p { color: var(--fg); }
  }

  .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;
  }

  .hint {
    margin-top: var(--s-2);
    padding: var(--s-2) var(--s-3);
    background: color-mix(in oklch, var(--accent-2), white 92%);
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    color: var(--fg);
  }

  /* ---- 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);
      vertical-align: top;
    }

    & 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; }
  }

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

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

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

  /* ---- 5-Step Algorithm ---- */
  .algorithm {
    display: grid;
    gap: var(--s-4);
    margin-top: var(--s-4);
    counter-reset: algo;
  }

  .step {
    --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);
    display: grid;
    grid-template-columns: auto 1fr;
    gap: var(--s-3) var(--s-4);
    transition: transform 200ms, box-shadow 200ms;

    &:hover {
      transform: translateY(-2px);
      box-shadow: var(--shadow);
    }

    & header {
      grid-column: 1 / -1;
      display: flex;
      align-items: center;
      gap: var(--s-3);
    }

    & .num {
      flex: 0 0 auto;
      width: 3rem;
      height: 3rem;
      border-radius: 50%;
      background: var(--c);
      color: white;
      display: grid;
      place-items: center;
      font-size: 1.5rem;
      font-weight: 800;
    }

    & h3 {
      font-size: 1.25rem;
      line-height: 1.3;

      & .ko {
        display: block;
        font-size: 0.95rem;
        color: var(--muted);
        font-weight: 500;
        margin-top: 2px;
      }
    }

    & .body {
      grid-column: 1 / -1;

      & p + p { margin-top: var(--s-2); }
    }

    & blockquote {
      border-left-color: var(--c);
    }
  }

  /* ---- Callout ---- */
  .callout {
    margin-top: var(--s-5);
    padding: var(--s-4);
    background: color-mix(in oklch, var(--accent), white 92%);
    border: 1px dashed var(--accent);
    border-radius: var(--radius);
    font-size: 1rem;
    text-align: center;
  }

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

  /* ---- Apply cases (details) ---- */
  .case {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 0;
    margin-block: var(--s-2);
    overflow: hidden;

    & summary {
      list-style: none;
      cursor: pointer;
      padding: var(--s-3) var(--s-4);
      font-weight: 600;
      font-size: 1.0625rem;
      display: flex;
      align-items: center;
      justify-content: space-between;
      transition: background 120ms;

      &::-webkit-details-marker { display: none; }

      &::after {
        content: "+";
        font-size: 1.5rem;
        color: var(--muted);
        font-weight: 300;
        transition: transform 200ms;
      }

      &:hover { background: color-mix(in oklch, var(--accent), white 96%); }
    }

    &[open] summary::after { transform: rotate(45deg); }

    & ol {
      counter-reset: case;
      padding: 0 var(--s-4) var(--s-4) var(--s-4);
      display: grid;
      gap: var(--s-2);
    }

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

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

  /* ---- 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);
  }
}

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

/* ---- Container queries for narrow viewports ---- */
@media (max-width: 640px) {
  .hero { padding-block: var(--s-5) var(--s-5); }
  .section { padding-block: var(--s-5); }
  .step { padding: var(--s-3); }
  .step .num { width: 2.5rem; height: 2.5rem; font-size: 1.25rem; }
}

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