/*
 * Design tokens. Every color in the app comes from here; components never hard-code colors.
 * data-theme is always resolved to "light" or "dark" by the inline bootstrap script in index.html
 * (System mode follows prefers-color-scheme live via src/theme/theme.ts).
 * Contrast targets: text >= 4.5:1 and lines >= 3:1 against their surfaces in both themes.
 */
:root {
  color-scheme: light;

  /* App chrome */
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --surface-3: #e4e7ec;
  --border: #d0d5dd;
  --border-strong: #8a94a6;
  --text: #101828;
  --text-muted: #475467;
  --accent: #1d4ed8;
  --accent-hover: #1e40af;
  --accent-fg: #ffffff;
  --accent-soft: #dbe7ff;
  --danger: #b42318;
  --danger-soft: #fee4e2;
  --warning: #9a3f07;
  --warning-soft: #fef0c7;
  --info: #175cd3;
  --info-soft: #d1e9ff;
  --success: #067647;
  --success-soft: #dcfae6;
  --focus: #1d4ed8;
  --overlay: rgb(16 24 40 / 0.45);
  --shadow-color: rgb(16 24 40 / 0.16);

  /* Diagram */
  --canvas: #ffffff;
  --grid: #d9dde4;
  --ink: #101828;
  --ink-muted: #475467;
  --step-active: #d3f8df;
  --token: #067647;
  --selection: #1d4ed8;
  --selection-fill: rgb(29 78 216 / 0.08);
  --link-target: #067647;
  --enabled: #9a3f07;
  --fired: #f79009;
  --comment-bg: #fffaeb;
  --comment-border: #b5891a;
}

:root[data-theme='dark'] {
  color-scheme: dark;

  --bg: #0b0d12;
  --surface: #12151c;
  --surface-2: #1a1e27;
  --surface-3: #252a36;
  --border: #2e3442;
  --border-strong: #6b7488;
  --text: #e6e9ef;
  --text-muted: #a3acbd;
  --accent: #7aa2ff;
  --accent-hover: #9bb8ff;
  --accent-fg: #0b0d12;
  --accent-soft: #1b2a4d;
  --danger: #ff8a80;
  --danger-soft: #3d1715;
  --warning: #fdb022;
  --warning-soft: #3a2a0c;
  --info: #84caff;
  --info-soft: #102a43;
  --success: #6ce9a6;
  --success-soft: #0f2e20;
  --focus: #7aa2ff;
  --overlay: rgb(0 0 0 / 0.6);
  --shadow-color: rgb(0 0 0 / 0.5);

  --canvas: #0f1218;
  --grid: #2a303d;
  --ink: #e6e9ef;
  --ink-muted: #a3acbd;
  --step-active: #133d27;
  --token: #6ce9a6;
  --selection: #7aa2ff;
  --selection-fill: rgb(122 162 255 / 0.12);
  --link-target: #6ce9a6;
  --enabled: #fdb022;
  --fired: #fdb022;
  --comment-bg: #2a2410;
  --comment-border: #c9a43a;
}

/*
 * Website guide pages (src/site/render.ts). Published after src/theme/tokens.css, whose tokens give
 * every color here; data-theme is set by the same inline bootstrap as the app.
 */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  scroll-padding-top: 16px;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font:
    17px/1.65 system-ui,
    -apple-system,
    'Segoe UI',
    Roboto,
    'Helvetica Neue',
    Arial,
    'Noto Sans',
    sans-serif;
}

a {
  color: var(--accent);
  text-underline-offset: 2px;
}

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

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

.skip {
  position: absolute;
  left: 16px;
  top: -48px;
  padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  z-index: 1;
}

.skip:focus {
  top: 8px;
}

/* Header */

.top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 16px;
  padding: 10px max(16px, calc((100% - 1200px) / 2));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  font-weight: 600;
  text-decoration: none;
}

.brand svg {
  color: var(--accent);
}

.langs {
  display: flex;
  gap: 2px;
  margin-left: auto;
}

.langs a {
  padding: 4px 8px;
  border-radius: 6px;
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 500;
  text-decoration: none;
}

.langs a:hover {
  background: var(--surface-2);
  color: var(--text);
}

.langs a[aria-current='page'] {
  background: var(--accent-soft);
  color: var(--text);
}

.button {
  display: inline-block;
  padding: 6px 14px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font-size: 15px;
  font-weight: 500;
  line-height: 1.5;
  text-decoration: none;
  white-space: nowrap;
}

.button:hover {
  background: var(--surface-2);
  color: var(--text);
}

.button.primary {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--accent-fg);
}

.button.primary:hover {
  border-color: var(--accent-hover);
  background: var(--accent-hover);
  color: var(--accent-fg);
}

/* Layout: title, contents, text; the contents move beside them on wide screens */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 24px;
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px 16px 48px;
}

.intro,
.text {
  min-width: 0;
  max-width: 760px;
}

.toc {
  align-self: start;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  font-size: 15px;
}

.toc h2 {
  margin: 0 0 6px;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.toc ol {
  margin: 0;
  padding-left: 20px;
  columns: 2 220px;
  column-gap: 24px;
}

.toc li {
  break-inside: avoid;
  padding: 2px 0;
}

.toc a {
  color: var(--text);
  text-decoration: none;
}

.toc a:hover {
  color: var(--accent);
  text-decoration: underline;
}

@media (min-width: 1080px) {
  .layout {
    grid-template-columns: 240px minmax(0, 1fr);
    grid-template-areas:
      'toc intro'
      'toc text';
    gap: 0 48px;
    padding-top: 40px;
  }

  .intro {
    grid-area: intro;
  }

  .text {
    grid-area: text;
  }

  .toc {
    grid-area: toc;
    position: sticky;
    top: 16px;
    max-height: calc(100vh - 32px);
    overflow: auto;
    border: 0;
    background: none;
    padding: 0;
  }

  .toc ol {
    columns: auto;
    border-left: 1px solid var(--border);
    padding-left: 28px;
  }
}

/* Article */

h1,
h2,
h3 {
  line-height: 1.25;
  text-wrap: balance;
}

h1 {
  margin: 0 0 4px;
  font-size: clamp(30px, 5vw, 40px);
}

.text h2 {
  margin: 2.2em 0 0.6em;
  padding-top: 0.6em;
  border-top: 1px solid var(--border);
  font-size: 26px;
}

.text h3 {
  margin: 1.6em 0 0.4em;
  font-size: 20px;
}

.updated {
  margin: 0;
  color: var(--text-muted);
  font-size: 15px;
}

@media (min-width: 1080px) {
  .updated {
    margin-bottom: 24px;
  }
}

p,
ul,
ol,
blockquote,
pre,
.table-wrap,
.fig {
  margin: 0 0 1em;
}

li + li {
  margin-top: 0.3em;
}

code,
kbd,
pre {
  font-family: 'JetBrains Mono', ui-monospace, 'Cascadia Mono', Consolas, Menlo, monospace;
  font-size: 0.88em;
}

code {
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--surface-2);
}

pre {
  overflow-x: auto;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
}

pre code {
  padding: 0;
  background: none;
}

kbd {
  padding: 1px 6px;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 4px;
  background: var(--surface);
}

blockquote {
  padding: 12px 16px;
  border-left: 3px solid var(--accent);
  border-radius: 0 8px 8px 0;
  background: var(--accent-soft);
}

blockquote p:last-child {
  margin-bottom: 0;
}

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 15px;
}

th,
td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

th {
  background: var(--surface-2);
  font-weight: 600;
}

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

/* Diagrams: inline SVG whose sentinel colors (src/site/render.ts) map to the diagram tokens */

.fig {
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--canvas);
}

.fig-art {
  overflow-x: auto;
  color: var(--ink);
  text-align: center;
}

.fig svg {
  max-width: 100%;
  height: auto;
}

.fig [fill='#000005'] {
  fill: var(--canvas);
}

.fig [fill='#000002'] {
  fill: var(--ink-muted);
}

.fig [fill='#000003'] {
  fill: var(--comment-bg);
}

.fig [stroke='#000004'] {
  stroke: var(--comment-border);
}

figcaption {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px 16px;
  margin-top: 8px;
  color: var(--text-muted);
  font-size: 15px;
  text-align: center;
}

/* Footer */

.foot {
  padding: 24px max(16px, calc((100% - 1200px) / 2)) 40px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  font-size: 15px;
}

.foot p {
  max-width: 760px;
  margin: 0 0 8px;
}

.foot a + a {
  margin-left: 8px;
}

@media print {
  .top,
  .toc,
  .foot,
  .skip {
    display: none;
  }

  .layout {
    display: block;
    padding: 0;
  }
}
