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

@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  html { color-scheme: light; scroll-behavior: smooth; }
  body, h1, h2, h3, p, ol, ul, dl, dd { margin: 0; }
  img, svg { display: block; max-width: 100%; }
  button, input, textarea, select { font: inherit; }
}

@layer tokens {
  :root {
    --gold: #FFD700;
    --green: #009739;
    --green-deep: #006B35;
    --sky: #87CEEB;
    --sky-deep: #006C84;
    --earth: #8B4513;
    --coral: #FF6F61;
    --orange: #CC5500;
    --purple: #8A2BE2;
    --white: #FFFFFF;
    --ink: #38271D;
    --paper: #FFFDF4;
    --soft-gold: #FFF5B8;
    --soft-green: #E6F5EB;
    --soft-sky: #E8F7FC;
    --soft-purple: #F3EAFE;
    --line: #D8C9B4;
    --shadow: 0 2px 12px rgb(85 60 35 / 0.11);
    --radius: 8px;
    --measure: 72ch;
    --section-gap: clamp(4rem, 8vw, 8rem);
    --mono: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
    --body: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    --display: Georgia, "Times New Roman", serif;
  }
}

@layer base {
  body {
    min-width: 0;
    background: var(--paper);
    color: var(--ink);
    font-family: var(--body);
    font-size: 1rem;
    line-height: 1.6;
  }

  a {
    color: var(--green-deep);
    text-decoration-color: var(--sky);
    text-decoration-thickness: 0.16em;
    text-underline-offset: 0.22em;
  }

  a:hover { color: var(--earth); }

  a:focus-visible,
  button:focus-visible,
  summary:focus-visible {
    outline: 2px solid var(--sky-deep);
    outline-offset: 2px;
    box-shadow: 0 0 0 4px var(--sky);
  }

  h1, h2, h3 {
    color: var(--ink);
    font-family: var(--display);
    font-weight: 700;
    line-height: 1.08;
    letter-spacing: -0.025em;
    text-wrap: balance;
  }

  h1 { font-size: clamp(2.5rem, 5vw, 4rem); }
  h2 { font-size: 1.5rem; }
  h3 { font-size: 1.125rem; }
  p, li, dd { max-width: var(--measure); }
  button { color: inherit; }
}

@layer layout {
  .l-shell {
    width: min(100% - 3rem, 1280px);
    margin-inline: auto;
  }

  .l-hero {
    display: grid;
    grid-template-columns: minmax(0, 1.15fr) minmax(19rem, 0.85fr);
    gap: clamp(2rem, 6vw, 6rem);
    align-items: center;
    min-height: 34rem;
    padding-block: clamp(3.5rem, 8vw, 7rem);
  }

  .l-hero--compact {
    min-height: 27rem;
    padding-block: clamp(3rem, 7vw, 5.5rem);
  }

  .l-section {
    position: relative;
    padding-block: var(--section-gap);
  }

  .l-section + .l-section::before {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    height: 1rem;
    background:
      linear-gradient(135deg, transparent 25%, var(--gold) 25% 50%, transparent 50% 75%, var(--green) 75%);
    background-size: 4rem 1rem;
    content: "";
    opacity: 0.68;
  }

  .l-zigzag {
    display: grid;
    grid-template-columns: minmax(0, 0.9fr) minmax(0, 1.1fr);
    gap: clamp(2rem, 6vw, 5rem);
    align-items: start;
  }

  .l-zigzag--reverse > :first-child { order: 2; }
  .l-stack > * + * { margin-top: 1.25rem; }
  .l-stack--tight > * + * { margin-top: 0.75rem; }

  @media (max-width: 767px) {
    .l-shell { width: min(100% - 2rem, 1280px); }
    .l-hero,
    .l-zigzag { grid-template-columns: minmax(0, 1fr); }
    .l-hero { min-height: 0; }
    .l-zigzag--reverse > :first-child { order: 0; }
  }
}

@layer components {
  .c-skip {
    position: fixed;
    z-index: 100;
    top: 0.5rem;
    left: 0.5rem;
    min-height: 44px;
    padding: 0.65rem 1rem;
    border-radius: var(--radius);
    background: var(--earth);
    color: var(--white);
    transform: translateY(-150%);
  }

  .c-skip:focus { transform: translateY(0); }

  .c-masthead {
    border-bottom: 1px solid var(--line);
    background: var(--white);
  }

  .c-masthead__top {
    display: flex;
    min-height: 4.75rem;
    gap: 1rem;
    align-items: center;
    justify-content: space-between;
  }

  .c-wordmark {
    display: inline-flex;
    min-height: 44px;
    align-items: center;
    color: var(--earth);
    font-family: var(--display);
    font-size: 1.05rem;
    font-weight: 700;
    line-height: 1.1;
    text-decoration: none;
  }

  .c-wordmark span {
    margin-right: 0.65rem;
    color: var(--green-deep);
    font-family: var(--mono);
    font-size: 1rem;
    letter-spacing: 0.08em;
  }

  .c-plain-toggle {
    min-height: 44px;
    padding: 0.65rem 0.9rem;
    border: 1px solid var(--green-deep);
    border-radius: var(--radius);
    background: var(--soft-green);
    color: var(--green-deep);
    cursor: pointer;
    font-family: var(--mono);
    font-size: 1rem;
    font-weight: 600;
  }

  .c-chapter-index {
    border-top: 1px solid var(--line);
  }

  .c-chapter-index ol {
    display: grid;
    grid-template-columns: repeat(8, minmax(0, 1fr));
    padding: 0;
    list-style: none;
  }

  .c-chapter-index a {
    display: flex;
    min-height: 56px;
    padding: 0.55rem 0.45rem;
    border-right: 1px solid var(--line);
    align-items: center;
    justify-content: center;
    color: var(--ink);
    font-family: var(--mono);
    font-size: 1rem;
    line-height: 1.2;
    text-align: center;
    text-decoration: none;
  }

  .c-chapter-index li:first-child a { border-left: 1px solid var(--line); }
  .c-chapter-index a[aria-current="page"] { background: var(--gold); color: var(--earth); }
  .c-chapter-index a:hover { background: var(--soft-sky); }

  .c-eyebrow,
  .c-label {
    color: var(--green-deep);
    font-family: var(--mono);
    font-size: 0.875rem;
    font-weight: 600;
    letter-spacing: 0.065em;
    line-height: 1.4;
    text-transform: uppercase;
  }

  .c-lede {
    max-width: 60ch;
    font-size: clamp(1.1rem, 2vw, 1.3rem);
    line-height: 1.55;
  }

  .c-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
  }

  .c-button {
    display: inline-flex;
    min-height: 48px;
    padding: 0.75rem 1rem;
    border: 1px solid var(--earth);
    border-radius: var(--radius);
    align-items: center;
    justify-content: center;
    background: var(--gold);
    color: var(--earth);
    font-size: 1rem;
    font-weight: 700;
    text-decoration: none;
  }

  .c-button--secondary {
    border: 3px solid var(--orange);
    background: var(--white);
    color: var(--earth);
  }
  .c-button:hover { transform: translateY(-2px); }

  .c-workbook-cover {
    position: relative;
    overflow: hidden;
    min-height: 25rem;
    padding: clamp(1.5rem, 4vw, 3rem);
    border: 1px solid var(--earth);
    border-radius: var(--radius);
    background:
      linear-gradient(90deg, transparent 48%, rgb(139 69 19 / 0.15) 49% 51%, transparent 52%),
      linear-gradient(0deg, transparent 48%, rgb(139 69 19 / 0.15) 49% 51%, transparent 52%),
      var(--gold);
    background-size: 5rem 5rem;
    box-shadow: var(--shadow);
  }

  .c-workbook-cover::after {
    position: absolute;
    right: -4rem;
    bottom: -5rem;
    width: 15rem;
    height: 15rem;
    border: 2.5rem double var(--green);
    border-radius: 50%;
    content: "";
    opacity: 0.82;
  }

  .c-workbook-cover__kicker {
    display: inline-block;
    padding: 0.35rem 0.55rem;
    background: var(--white);
    color: var(--earth);
    font-family: var(--mono);
    font-size: 0.875rem;
    font-weight: 600;
    letter-spacing: 0.05em;
  }

  .c-workbook-cover__words {
    position: relative;
    z-index: 1;
    margin-top: 4rem;
    font-family: var(--display);
    font-size: clamp(2.4rem, 5vw, 4.5rem);
    font-weight: 700;
    line-height: 0.92;
    letter-spacing: -0.05em;
  }

  .c-workbook-cover__words span {
    display: block;
    width: fit-content;
    padding: 0.04em 0.12em 0.08em;
    background: var(--gold);
    color: var(--ink);
  }
  .c-workbook-cover__words span:nth-child(2) { color: var(--green-deep); transform: translateX(1.25rem); }
  .c-workbook-cover__words span:nth-child(3) { color: var(--purple); transform: translateX(2.5rem); }

  .c-answer {
    padding: clamp(1.25rem, 3vw, 2rem);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--white);
    box-shadow: var(--shadow);
  }

  .c-answer + .c-answer { margin-top: 1rem; }
  .c-answer--green { border-left: 0.5rem solid var(--green); }
  .c-answer--gold { border-left: 0.5rem solid var(--gold); }
  .c-answer--purple { border-left: 0.5rem solid var(--purple); }
  .c-answer--orange { border-left: 0.5rem solid var(--orange); }
  .c-answer--stop { border-left: 0.5rem solid var(--coral); }
  .c-answer h2, .c-answer h3 { margin-bottom: 0.75rem; }
  .c-answer p + p, .c-answer ul + p, .c-answer p + ul { margin-top: 0.75rem; }

  .c-checklist {
    padding: 0;
    list-style: none;
  }

  .c-checklist li {
    position: relative;
    min-height: 44px;
    padding: 0.55rem 0 0.55rem 2.25rem;
  }

  .c-checklist li::before {
    position: absolute;
    top: 0.72rem;
    left: 0;
    display: grid;
    width: 1.45rem;
    height: 1.45rem;
    border: 2px solid var(--green);
    border-radius: 3px;
    color: var(--green-deep);
    content: "✓";
    font-family: var(--mono);
    font-size: 0.9rem;
    font-weight: 700;
    line-height: 1;
    place-items: center;
  }

  .c-question-list {
    padding: 0;
    counter-reset: workbook;
    list-style: none;
  }

  .c-question-list li {
    display: grid;
    grid-template-columns: 3.25rem minmax(0, 1fr);
    gap: 1rem;
    padding-block: 1.25rem;
    border-bottom: 1px solid var(--line);
    counter-increment: workbook;
  }

  .c-question-list li::before {
    display: grid;
    width: 3.25rem;
    height: 3.25rem;
    border-radius: 50%;
    background: var(--gold);
    color: var(--earth);
    content: counter(workbook, decimal-leading-zero);
    font-family: var(--mono);
    font-size: 0.875rem;
    font-weight: 700;
    place-items: center;
  }

  .c-note {
    padding: 1rem 1.25rem;
    border: 1px solid var(--sky-deep);
    border-radius: var(--radius);
    background: var(--soft-sky);
  }

  .c-note--purple { border-color: var(--purple); background: var(--soft-purple); }
  .c-note--warning { border-color: var(--coral); background: #FFF0ED; }

  .c-detail {
    display: none;
    padding: 0.85rem 1rem;
    border-left: 4px solid var(--sky);
    background: var(--soft-sky);
    color: var(--ink);
    font-size: 1rem;
  }

  .is-detail .c-detail { display: block; }

  .c-source {
    display: inline-flex;
    min-height: 44px;
    padding: 0.5rem 0;
    align-items: center;
    font-family: var(--mono);
    font-size: 1rem;
    font-weight: 600;
  }

  .c-ledger {
    display: grid;
    gap: 0.75rem;
    padding: 0;
    list-style: none;
  }

  .c-ledger li {
    display: grid;
    grid-template-columns: minmax(7rem, 0.3fr) minmax(0, 1fr);
    gap: 1rem;
    padding: 1rem;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--white);
  }

  .c-ledger strong { color: var(--green-deep); font-family: var(--mono); font-size: 0.875rem; }

  .c-next {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: 1rem;
    min-height: 72px;
    padding: 1rem 1.25rem;
    border: 1px solid var(--earth);
    border-radius: var(--radius);
    align-items: center;
    background: var(--gold);
    color: var(--earth);
    text-decoration: none;
  }

  .c-next__number { font-family: var(--mono); font-size: 1rem; font-weight: 700; }
  .c-next__label { font-family: var(--display); font-size: 1.25rem; font-weight: 700; }
  .c-next__arrow { font-size: 1.5rem; }

  .c-footer {
    margin-top: var(--section-gap);
    border-top: 0.75rem solid var(--green);
    background: var(--earth);
    color: var(--white);
  }

  .c-footer__inner { padding-block: 2rem; }
  .c-footer p { font-size: 1rem; }
  .c-footer nav { margin-top: 1rem; }
  .c-footer nav ul { display: flex; flex-wrap: wrap; gap: 0.35rem 1rem; padding: 0; list-style: none; }
  .c-footer a { display: inline-flex; min-width: 44px; min-height: 44px; padding-inline: 0.25rem; align-items: center; justify-content: center; color: var(--white); text-decoration-color: var(--sky); }

  @media (max-width: 900px) {
    .c-chapter-index ol { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  }

  @media (max-width: 560px) {
    .c-masthead__top { align-items: stretch; flex-direction: column; padding-block: 0.65rem; }
    .c-plain-toggle { width: 100%; }
    .c-chapter-index ol { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .c-chapter-index a { min-height: 50px; font-size: 1rem; }
    .c-question-list li { grid-template-columns: 2.75rem minmax(0, 1fr); }
    .c-question-list li::before { width: 2.75rem; height: 2.75rem; }
    .c-ledger li { grid-template-columns: minmax(0, 1fr); gap: 0.25rem; }
  }
}

@layer utilities {
  .u-measure { max-width: var(--measure); }
  .u-soft-green { background: var(--soft-green); }
  .u-soft-gold { background: var(--soft-gold); }
  .u-soft-purple { background: var(--soft-purple); }
  .u-white { background: var(--white); }
}

@layer motion {
  @keyframes c-rise {
    from { opacity: 0; transform: translateY(16px); }
    to { opacity: 1; transform: translateY(0); }
  }

  main > * { animation: c-rise 420ms ease both; }
  main > *:nth-child(2) { animation-delay: 80ms; }
  a, button { transition: color 200ms ease, background-color 200ms ease, transform 200ms ease, opacity 200ms ease; }

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