/* ==========================================================================
   The ChatML Handbook - portal stylesheet
   Palette sampled directly from images/ChatML-Handbook-Front-Cover.jpg.
   One theme, matching the printed object: the cover is dark, so the page is dark.
   ========================================================================== */

:root {
  /* Ground - sampled from the cover's corners and centre glow */
  --ground:    #070C16;   /* page ground (cover corner #070A13 / #0B111F) */
  --ground-2:  #0C1524;   /* cards, section bands */
  --ground-3:  #102739;   /* code panel fill (cover code block) */
  --ground-4:  #103649;   /* the cover's centre glow */

  --ink:       #EEF4F8;   /* headings, the ChatML wordmark white */
  --ink-soft:  #9FB0C4;   /* cover subtitle grey, sampled */
  --ink-mono:  #B0C0D0;   /* cover code body text, sampled */
  --rule:      #1B2E42;   /* hairlines */

  --accent:    #22D3ED;   /* the cover's one emphasis colour: HANDBOOK cyan */

  /* The cover's series: the four role-token colours of its code specimen,
     in the order the specimen prints them. */
  --s1: #DDC183;   /* system    - amber */
  --s2: #82C982;   /* user      - green */
  --s3: #22D3ED;   /* assistant - cyan  */
  --s4: #BC7DD4;   /* the <|im_start|> / <|im_end|> markers - violet */

  --measure: 68ch;
  --page:    1120px;
  --gap:     clamp(1rem, 4vw, 2.5rem);

  --font-sans: "Space Grotesk", system-ui, -apple-system, "Segoe UI", Roboto,
               "Helvetica Neue", Arial, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas,
               "Liberation Mono", monospace;
}

/* --------------------------------------------------------------------------
   Reset / base
   -------------------------------------------------------------------------- */

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

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

body {
  margin: 0;
  background: var(--ground);
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: 1.0625rem;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

img { max-width: 100%; height: auto; display: block; }

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

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

h1, h2, h3 { line-height: 1.1; letter-spacing: -0.02em; margin: 0 0 0.5em; font-weight: 700; }

p { margin: 0 0 1em; max-width: var(--measure); }

.wrap { width: min(100% - 2rem, var(--page)); margin-inline: auto; }

/* min-width reset: without this a grid/flex item defaults to min-width:auto and
   stretches its track around any overflow-x wrapper inside it. */
.split > *, .hero__grid > *, .prose, .contents__col { min-width: 0; }

.eyebrow {
  font-family: var(--font-mono);
  font-weight: 600;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.18em;
  color: var(--ink-soft);
  margin: 0 0 1rem;
}

/* JetBrains Mono ligates "<|" and "|>" into arrow glyphs, which would misrender
   the book's canonical <|im_start|> / <|im_end|> tokens. Turn ligatures off
   everywhere the mono face is used. */
code, pre, .mono, .eyebrow, .specimen, .codeblock, .chips li, .rung__level,
.rung__hint, .rung__field h4, .buy__store, .stat span, th, .contents__col h3,
.contents__col .num, .footer__meta, .legend li, .btn, .hero__tag, .stickybuy .buy {
  font-variant-ligatures: none;
  font-feature-settings: "liga" 0, "calt" 0;
}

.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* --------------------------------------------------------------------------
   Masthead
   -------------------------------------------------------------------------- */

.series-bar { display: flex; height: 8px; }
.series-bar span { flex: 1; }
.series-bar span:nth-child(1) { background: var(--s1); }
.series-bar span:nth-child(2) { background: var(--s2); }
.series-bar span:nth-child(3) { background: var(--s3); }
.series-bar span:nth-child(4) { background: var(--s4); }

.masthead__line {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1rem;
  justify-content: space-between;
  align-items: baseline;
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--rule);
}
.masthead__line .eyebrow { margin: 0; }
.masthead__line .eyebrow--ink { color: var(--ink); }

/* --------------------------------------------------------------------------
   Sections
   -------------------------------------------------------------------------- */

section { padding: clamp(3rem, 8vw, 5.5rem) 0; border-bottom: 1px solid var(--rule); }
section.band { background: var(--ground-2); }

.section-head { margin-bottom: 2.5rem; }
.section-head h2 { font-size: clamp(1.6rem, 3.5vw, 2.4rem); margin: 0 0 0.6rem; }
.section-head p { color: var(--ink-soft); font-size: clamp(1rem, 1.6vw, 1.15rem); }

.split {
  display: grid;
  gap: var(--gap);
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 320px), 1fr));
}

/* --------------------------------------------------------------------------
   Hero
   -------------------------------------------------------------------------- */

.hero {
  padding-top: clamp(2.5rem, 6vw, 4rem);
  background:
    radial-gradient(120% 90% at 50% 55%, var(--ground-4) 0%, transparent 62%),
    var(--ground);
  border-bottom: 1px solid var(--rule);
}

.hero__grid {
  display: grid;
  gap: var(--gap);
  align-items: start;
  grid-template-columns: 1fr;
}

@media (min-width: 900px) {
  .hero__grid { grid-template-columns: minmax(280px, 400px) 1fr; gap: clamp(2rem, 5vw, 4rem); }
}

.hero__cover img {
  width: 100%;
  border: 1px solid var(--rule);
  border-radius: 6px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.55);
}

.hero h1 {
  font-size: clamp(2.5rem, 7vw, 4.5rem);
  line-height: 0.98;
  margin: 0.25rem 0 0.75rem;
}
.hero h1 .line { display: block; }
.hero h1 .line--accent { color: var(--accent); }

.hero__tag {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  letter-spacing: 0.08em;
  color: var(--accent);
  margin: 0 0 0.25rem;
}

.hero__deck {
  font-size: clamp(1.05rem, 2vw, 1.4rem);
  color: var(--ink-soft);
  font-weight: 400;
  max-width: 34ch;
  margin-bottom: 1.5rem;
}

.pullquote {
  border-left: 4px solid var(--accent);
  padding: 0.25rem 0 0.25rem 1.1rem;
  margin: 0 0 1.75rem;
  color: var(--ink);
  font-size: 1.0625rem;
  max-width: 56ch;
}

.hero__meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem 1.25rem;
  margin: 1.5rem 0 0;
  padding-top: 1.25rem;
  border-top: 1px solid var(--rule);
}
.hero__meta span {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--ink-soft);
}

/* --------------------------------------------------------------------------
   Buy row
   -------------------------------------------------------------------------- */

.buy-row { display: flex; flex-wrap: wrap; gap: 0.75rem; }

.buy {
  display: inline-flex;
  flex-direction: column;
  gap: 0.15rem;
  padding: 0.7rem 1.25rem;
  border-radius: 4px;
  border: 1px solid var(--accent);
  text-decoration: none;
  font-weight: 700;
  font-size: 1rem;
  line-height: 1.2;
  transition: transform 0.15s ease, background-color 0.15s ease;
}
.buy:hover { transform: translateY(-2px); }
.buy__store {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  font-weight: 500;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  opacity: 0.85;
}

.buy--primary { background: var(--accent); color: var(--ground); border-color: var(--accent); }
.buy--primary:hover { background: #4FE0F5; }
.buy--secondary { background: transparent; color: var(--ink); border-color: var(--rule); }
.buy--secondary:hover { border-color: var(--accent); color: var(--accent); }

/* Sticky buy bar */
.stickybuy {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 40;
  background: rgba(7, 12, 22, 0.94);
  backdrop-filter: blur(10px);
  border-top: 1px solid var(--rule);
  transform: translateY(110%);
  transition: transform 0.25s ease;
}
body.show-stickybuy .stickybuy { transform: translateY(0); }
.stickybuy__inner {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1rem;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 0;
}
.stickybuy__title { font-weight: 700; font-size: 0.95rem; }
.stickybuy .buy { padding: 0.45rem 0.9rem; font-size: 0.9rem; }
.stickybuy .buy__store { display: none; }

/* --------------------------------------------------------------------------
   ChatML specimen
   -------------------------------------------------------------------------- */

.specimen {
  background: var(--ground-3);
  border: 1px solid var(--rule);
  border-radius: 8px;
  overflow: hidden;
  font-family: var(--font-mono);
  font-size: clamp(0.78rem, 1.7vw, 0.9rem);
  line-height: 1.75;
}
.specimen__bar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.6rem 0.9rem;
  border-bottom: 1px solid var(--rule);
  color: var(--ink-soft);
  font-size: 0.75rem;
  letter-spacing: 0.06em;
}
.specimen__dot { width: 8px; height: 8px; border-radius: 50%; background: var(--rule); }
.specimen__dot:nth-of-type(3) { background: var(--accent); }
.specimen__code { margin: 0; padding: 1rem 0.9rem; overflow-x: auto; }
.specimen__code code { white-space: pre; }
.tok-mark { color: var(--s4); }
.tok-system { color: var(--s1); font-weight: 700; }
.tok-user { color: var(--s2); font-weight: 700; }
.tok-assistant { color: var(--s3); font-weight: 700; }
.tok-body { color: var(--ink-mono); }

.legend { display: flex; flex-wrap: wrap; gap: 0.5rem 1.25rem; margin: 1.25rem 0 0; padding: 0; list-style: none; }
.legend li {
  display: flex; align-items: center; gap: 0.5rem;
  font-family: var(--font-mono); font-size: 0.75rem; letter-spacing: 0.1em;
  text-transform: uppercase; color: var(--ink-soft);
}
.legend .swatch { width: 10px; height: 10px; border-radius: 2px; background: var(--c, var(--rule)); }

/* --------------------------------------------------------------------------
   Centerpiece - the chapter ladder
   -------------------------------------------------------------------------- */

.ladder { margin: 0; }

.ladder__part {
  margin: 2.5rem 0 1rem;
  padding-bottom: 0.6rem;
  border-bottom: 1px solid var(--rule);
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.5rem 1rem;
}
.ladder__part:first-child { margin-top: 0; }
.ladder__part h3 {
  margin: 0;
  font-size: 1.1rem;
  color: var(--c, var(--ink));
  letter-spacing: 0.02em;
}
.ladder__part .ladder__purpose {
  margin: 0;
  color: var(--ink-soft);
  font-size: 0.9rem;
  max-width: 62ch;
}

.rung {
  border-left: 6px solid var(--c);
  background: var(--ground-2);
  border-radius: 0 6px 6px 0;
  margin: 0 0 0.5rem;
  margin-left: var(--indent, 0);
}
.rung + .ladder__part { margin-left: 0; }

.rung summary {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.35rem 0.9rem;
  padding: 0.95rem 2.5rem 0.95rem 1.1rem;
  cursor: pointer;
  position: relative;
  list-style: none;
}
.rung summary::-webkit-details-marker { display: none; }
.rung summary::marker { content: ""; }
.rung summary::after {
  content: "+";
  position: absolute;
  right: 1rem;
  top: 50%;
  transform: translateY(-50%);
  font-family: var(--font-mono);
  font-size: 1.2rem;
  color: var(--c);
  line-height: 1;
}
.rung[open] summary::after { content: "\2013"; }
.rung summary:hover { background: rgba(255, 255, 255, 0.03); }

.rung__level {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--c);
  flex: 0 0 auto;
}
.rung__name { font-weight: 700; font-size: 1.05rem; }
.rung__hint {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--ink-soft);
  flex-basis: 100%;
}

.rung__body { padding: 0 1.1rem 1.25rem; }
.rung__field { margin: 0 0 1.1rem; }
.rung__field:last-child { margin-bottom: 0; }
.rung__field h4 {
  margin: 0 0 0.35rem;
  font-family: var(--font-mono);
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--ink-soft);
}
.rung__field p { margin: 0; max-width: 72ch; }
.rung__field ul { margin: 0; padding-left: 1.1rem; max-width: 72ch; }
.rung__field li { margin-bottom: 0.35rem; }
.rung__field li::marker { color: var(--c); }

.chips { display: flex; flex-wrap: wrap; gap: 0.4rem; margin: 0; padding: 0; list-style: none; }
.chips li {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.04em;
  padding: 0.2rem 0.55rem;
  border: 1px solid var(--rule);
  border-radius: 3px;
  color: var(--ink-mono);
  background: var(--ground);
}

.ladder__controls { display: flex; gap: 0.75rem; margin-bottom: 1.5rem; }
.btn {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  padding: 0.5rem 0.9rem;
  background: transparent;
  color: var(--ink-soft);
  border: 1px solid var(--rule);
  border-radius: 3px;
  cursor: pointer;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }

@media (max-width: 640px) {
  .rung { margin-left: 0 !important; }
}

/* --------------------------------------------------------------------------
   Concept cards
   -------------------------------------------------------------------------- */

.cards {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 260px), 1fr));
  margin: 0;
  padding: 0;
  list-style: none;
}
.card {
  background: var(--ground-2);
  border: 1px solid var(--rule);
  border-top: 3px solid var(--c, var(--accent));
  border-radius: 0 0 6px 6px;
  padding: 1.1rem;
}
.card h3 { font-size: 1rem; margin: 0 0 0.4rem; }
.card p { margin: 0; font-size: 0.94rem; color: var(--ink-soft); max-width: none; }
.card .where {
  display: block;
  margin-top: 0.7rem;
  font-family: var(--font-mono);
  font-size: 0.68rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--c, var(--accent));
}

/* --------------------------------------------------------------------------
   Stats + table
   -------------------------------------------------------------------------- */

.stats {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 150px), 1fr));
  margin: 0 0 2rem;
  padding: 0;
  list-style: none;
}
.stat { border-top: 1px solid var(--rule); padding-top: 0.75rem; }
.stat b {
  display: block;
  font-size: clamp(2rem, 5vw, 3rem);
  line-height: 1;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}
.stat span {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; min-width: 30rem; font-size: 0.94rem; }
th, td { text-align: left; padding: 0.6rem 0.9rem; border-bottom: 1px solid var(--rule); }
th {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-soft);
}
td code, .mono { font-family: var(--font-mono); font-size: 0.88em; color: var(--ink-mono); }

/* --------------------------------------------------------------------------
   Contents
   -------------------------------------------------------------------------- */

.contents {
  display: grid;
  gap: var(--gap);
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr));
}
.contents__col h3 {
  font-size: 0.75rem;
  font-family: var(--font-mono);
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--c, var(--ink-soft));
  padding-bottom: 0.5rem;
  border-bottom: 2px solid var(--c, var(--rule));
  margin-bottom: 0.9rem;
}
.contents__col ol, .contents__col ul { margin: 0; padding: 0; list-style: none; }
.contents__col li { display: flex; gap: 0.7rem; padding: 0.35rem 0; font-size: 0.95rem; }
.contents__col .num {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  color: var(--ink-soft);
  flex: 0 0 1.9rem;
}

.appendix-strip { margin-top: 2rem; padding-top: 1.5rem; border-top: 1px solid var(--rule); }
.appendix-strip ul { display: grid; gap: 0 var(--gap); grid-template-columns: 1fr; }
.appendix-strip li { min-width: 0; align-items: baseline; }
@media (min-width: 640px) { .appendix-strip ul { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (min-width: 980px) { .appendix-strip ul { grid-template-columns: repeat(5, minmax(0, 1fr)); } }

/* --------------------------------------------------------------------------
   Audience / code / author
   -------------------------------------------------------------------------- */

.checklist { margin: 0; padding: 0; list-style: none; }
.checklist li {
  position: relative;
  padding-left: 1.6rem;
  margin-bottom: 0.7rem;
  max-width: var(--measure);
}
.checklist li::before {
  content: "\203A";
  position: absolute;
  left: 0.3rem;
  color: var(--accent);
  font-weight: 700;
}

.codeblock {
  background: var(--ground-3);
  border: 1px solid var(--rule);
  border-radius: 6px;
  padding: 1rem;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 0.85rem;
  line-height: 1.7;
  color: var(--ink-mono);
  margin: 0 0 1.25rem;
}
.codeblock .c { color: var(--ink-soft); }

.author { display: grid; gap: var(--gap); grid-template-columns: 1fr; }
@media (min-width: 760px) { .author { grid-template-columns: 1fr 1fr; } }

/* --------------------------------------------------------------------------
   Footer
   -------------------------------------------------------------------------- */

footer { padding: clamp(2.5rem, 6vw, 4rem) 0 6rem; }
footer .buy-row { margin-bottom: 2rem; }
.footer__meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1.5rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--rule);
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.1em;
  color: var(--ink-soft);
}

/* --------------------------------------------------------------------------
   Reveal on scroll - content is visible by default; JS opts in.
   -------------------------------------------------------------------------- */

.js-reveal.reveal-armed { opacity: 0; transform: translateY(14px); }
.js-reveal.is-in { opacity: 1; transform: none; transition: opacity 0.5s ease, transform 0.5s ease; }

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
  .js-reveal.reveal-armed { opacity: 1; transform: none; }
}
