/*
 * Typeset - Rhythm-driven HTML & rendered markdown typography system
 * Built on three controls (--typeset-size, --typeset-leading, --typeset-flow),
 * zero-specificity :where() selectors, append-safe margin-block-start flow,
 * theme inheritance, and .not-typeset / [data-not-typeset] escape hatches.
 */

.typeset {
  --typeset-font-body: inherit;
  --typeset-font-heading: var(--font-heading, inherit);
  --typeset-font-mono: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace);

  --typeset-size: 1em;
  --typeset-leading: 1.75;
  --typeset-flow: 1.25em;

  font-family: var(--typeset-font-body);
  font-size: var(--typeset-size);
  line-height: var(--typeset-leading);
  font-weight: 400;
  color: var(--text-body, inherit);
}

/* Presets */
.typeset-docs {
  --typeset-font-body: var(--font-sans, inherit);
  --typeset-font-heading: var(--font-display, inherit);
  --typeset-font-mono: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace);
  --typeset-size: 15px;
  --typeset-leading: 1.82;
  --typeset-flow: 1.3em;
}

.typeset-chat {
  --typeset-leading: 1.6;
  --typeset-flow: 1em;
}

.typeset-compact {
  --typeset-size: 14px;
  --typeset-leading: 1.6;
  --typeset-flow: 1em;
}

.typeset-large {
  --typeset-size: 16px;
  --typeset-leading: 2;
  --typeset-flow: 1.75em;
}

@media (max-width: 640px) {
  .typeset-docs {
    --typeset-size: 15.5px;
    --typeset-leading: 1.8;
  }
}

/* Reset margins inside typeset (excluding opted-out subtrees) */
:where(.typeset :is(h1, h2, h3, h4, p, ul, ol, blockquote, pre, hr, table, figure)):not(.not-typeset, [data-not-typeset], .not-typeset *, [data-not-typeset] *) {
  margin-block: 0;
}

/* One-directional vertical flow (append/streaming safe: margin-block-start only) */
:where(.typeset > * + *):not(.not-typeset, [data-not-typeset]) {
  margin-block-start: var(--typeset-flow);
}

:where(.typeset > :first-child):not(.not-typeset, [data-not-typeset]) {
  margin-block-start: 0;
}

/* Measure & Paragraphs */
:where(.typeset p):not(.not-typeset, [data-not-typeset], .not-typeset *, [data-not-typeset] *) {
  font-size: 1em;
  font-weight: 400;
  line-height: var(--typeset-leading);
  color: var(--text-body, inherit);
}

/* Headings derived from container size and flow */
:where(.typeset :is(h1, h2, h3, h4)):not(.not-typeset, [data-not-typeset], .not-typeset *, [data-not-typeset] *) {
  font-family: var(--typeset-font-heading);
  color: var(--text-primary, inherit);
  font-weight: 700;
  letter-spacing: -0.02em;
}

:where(.typeset h1):not(.not-typeset, [data-not-typeset], .not-typeset *, [data-not-typeset] *) {
  font-size: 1.9em;
  line-height: 1.2;
  letter-spacing: -0.025em;
  margin-block-start: calc(var(--typeset-flow) * 1.85);
}

:where(.typeset h2):not(.not-typeset, [data-not-typeset], .not-typeset *, [data-not-typeset] *) {
  font-size: 1.35em;
  line-height: 1.32;
  margin-block-start: calc(var(--typeset-flow) * 1.8);
}

:where(.typeset h3):not(.not-typeset, [data-not-typeset], .not-typeset *, [data-not-typeset] *) {
  font-size: 1.125em;
  line-height: 1.4;
  letter-spacing: -0.015em;
  margin-block-start: calc(var(--typeset-flow) * 1.45);
}

:where(.typeset h4):not(.not-typeset, [data-not-typeset], .not-typeset *, [data-not-typeset] *) {
  font-size: 1em;
  line-height: 1.45;
  margin-block-start: calc(var(--typeset-flow) * 1.2);
}

/* Tighter gap immediately below headings (proximity rhythm) */
:where(.typeset h1 + *):not(.not-typeset, [data-not-typeset]) {
  margin-block-start: calc(var(--typeset-flow) * 0.75);
}

:where(.typeset h2 + *):not(.not-typeset, [data-not-typeset]) {
  margin-block-start: calc(var(--typeset-flow) * 0.6);
}

:where(.typeset :is(h3, h4) + *):not(.not-typeset, [data-not-typeset]) {
  margin-block-start: calc(var(--typeset-flow) * 0.45);
}

/* Inline emphasis & links */
:where(.typeset strong):not(.not-typeset, [data-not-typeset], .not-typeset *, [data-not-typeset] *) {
  font-weight: 600;
  color: var(--text-primary, inherit);
}

:where(.typeset a):not(.not-typeset, [data-not-typeset], .not-typeset *, [data-not-typeset] *) {
  color: var(--text-primary, inherit);
  font-weight: 400;
  text-decoration: underline;
  text-decoration-color: rgba(235, 235, 238, 0.35);
  text-underline-offset: 3px;
  transition: text-decoration-color 0.15s ease;
}

:where(.typeset a:hover):not(.not-typeset, [data-not-typeset], .not-typeset *, [data-not-typeset] *) {
  text-decoration-color: var(--text-primary, currentColor);
}

/* Lists */
:where(.typeset :is(ul, ol)):not(.not-typeset, [data-not-typeset], .not-typeset *, [data-not-typeset] *) {
  padding-inline-start: 1.35em;
}

:where(.typeset ul):not(.not-typeset, [data-not-typeset], .not-typeset *, [data-not-typeset] *) {
  list-style-type: disc;
}

:where(.typeset ol):not(.not-typeset, [data-not-typeset], .not-typeset *, [data-not-typeset] *) {
  list-style-type: decimal;
}

:where(.typeset li):not(.not-typeset, [data-not-typeset], .not-typeset *, [data-not-typeset] *) {
  font-weight: 400;
  color: var(--text-body, inherit);
}

:where(.typeset li + li, .typeset li > :is(ul, ol)):not(.not-typeset, [data-not-typeset], .not-typeset *, [data-not-typeset] *) {
  margin-block-start: calc(var(--typeset-flow) * 0.45);
}

/* Blockquotes */
:where(.typeset blockquote):not(.not-typeset, [data-not-typeset], .not-typeset *, [data-not-typeset] *) {
  margin-block-start: calc(var(--typeset-flow) * 1.25);
  padding-block: 0.25em;
  padding-inline-start: 1.15em;
  border-inline-start: 2px solid rgba(235, 235, 238, 0.25);
  font-style: italic;
  font-weight: 400;
  color: var(--text-primary, inherit);
}

/* Horizontal rules */
:where(.typeset hr):not(.not-typeset, [data-not-typeset], .not-typeset *, [data-not-typeset] *) {
  border: 0;
  border-block-start: 1px solid var(--border, rgba(255, 255, 255, 0.1));
  margin-block-start: calc(var(--typeset-flow) * 1.6);
}

:where(.typeset hr + *):not(.not-typeset, [data-not-typeset]) {
  margin-block-start: calc(var(--typeset-flow) * 1.6);
}

/* Inline & block code */
:where(.typeset :not(pre) > code):not(.not-typeset, [data-not-typeset], .not-typeset *, [data-not-typeset] *) {
  font-family: var(--typeset-font-mono);
  font-size: 0.85em;
  font-weight: 400;
  padding: 0.14em 0.42em;
  border-radius: 5px;
  background: var(--surface-elevated, #1e1e20);
  color: var(--text-primary, #ebebee);
}

:where(.typeset pre):not(.not-typeset, [data-not-typeset], .not-typeset *, [data-not-typeset] *) {
  margin-block-start: calc(var(--typeset-flow) * 1.2);
  padding: 1em 1.15em;
  border-radius: 10px;
  background: var(--surface, #141414);
  border: 1px solid var(--border, #1f1f22);
  overflow-x: auto;
  font-family: var(--typeset-font-mono);
  font-size: 0.85em;
  line-height: 1.65;
  color: var(--text-body, #ebebee);
}

/* Tables with append-stable separators on incoming cells */
.typeset-scroll {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

:where(.typeset table):not(.not-typeset, [data-not-typeset], .not-typeset *, [data-not-typeset] *) {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875em;
  line-height: 1.6;
  text-align: left;
}

:where(.typeset th):not(.not-typeset, [data-not-typeset], .not-typeset *, [data-not-typeset] *) {
  padding: 0.65em 0.8em;
  font-weight: 600;
  color: var(--text-primary, inherit);
}

:where(.typeset td):not(.not-typeset, [data-not-typeset], .not-typeset *, [data-not-typeset] *) {
  padding: 0.7em 0.8em;
  vertical-align: top;
  border-block-start: 1px solid var(--border, #1f1f22);
  color: var(--text-body, inherit);
}
