/* The look of the book (docs/dsssl/scaly-book.dsl writes the pages): the
   colours of scaly.io -- stone and emerald --, light and dark, the theme
   chosen by the reader (theme.js, data-theme on the root) or by the system. */

:root {
  --bg: #fafaf9;
  --fg: #1c1917;
  --muted: #78716c;
  --accent: #059669;
  --accent-strong: #047857;
  --accent-soft: #d1fae5;
  --accent-faint: #ecfdf5;
  --border: #e7e5e4;
  --sidebar-bg: #f1f5f2;
  --sidebar-fg: #44403c;
  --code-bg: #f3f4f1;
  --code-inline: #e9efeb;
  --screen-bg: #1c1917;
  --screen-fg: #e7e5e4;
  --mark: #fde68a;
  --bad: #b91c1c;
  --shadow: 0 1px 2px rgba(28, 25, 23, 0.06), 0 4px 16px rgba(28, 25, 23, 0.06);
  --hl-keyword: #047857;
  --hl-string: #b45309;
  --hl-comment: #8a837c;
  --hl-number: #7c3aed;
  --hl-type: #0369a1;
  --hl-value: #c2410c;
  --hl-sigil: #be185d;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #131715;
    --fg: #e7e5e4;
    --muted: #a8a29e;
    --accent: #34d399;
    --accent-strong: #6ee7b7;
    --accent-soft: #064e3b;
    --accent-faint: #0f241c;
    --border: #2a302d;
    --sidebar-bg: #0f1311;
    --sidebar-fg: #d6d3d1;
    --code-bg: #1a1f1c;
    --code-inline: #1f2723;
    --screen-bg: #0a0c0b;
    --screen-fg: #e7e5e4;
    --mark: #854d0e;
  --bad: #f87171;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 16px rgba(0, 0, 0, 0.3);
    --hl-keyword: #34d399;
    --hl-string: #fbbf24;
    --hl-comment: #8b9490;
    --hl-number: #c4b5fd;
    --hl-type: #7dd3fc;
    --hl-value: #fdba74;
    --hl-sigil: #f9a8d4;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #131715;
  --fg: #e7e5e4;
  --muted: #a8a29e;
  --accent: #34d399;
  --accent-strong: #6ee7b7;
  --accent-soft: #064e3b;
  --accent-faint: #0f241c;
  --border: #2a302d;
  --sidebar-bg: #0f1311;
  --sidebar-fg: #d6d3d1;
  --code-bg: #1a1f1c;
  --code-inline: #1f2723;
  --screen-bg: #0a0c0b;
  --screen-fg: #e7e5e4;
  --mark: #854d0e;
  --bad: #f87171;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 16px rgba(0, 0, 0, 0.3);
  --hl-keyword: #34d399;
  --hl-string: #fbbf24;
  --hl-comment: #8b9490;
  --hl-number: #c4b5fd;
  --hl-type: #7dd3fc;
  --hl-value: #fdba74;
  --hl-sigil: #f9a8d4;
  color-scheme: dark;
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 17px/1.7 'DM Sans', system-ui, -apple-system, 'Segoe UI', sans-serif;
  font-optical-sizing: auto;
}

a { color: var(--accent-strong); text-decoration: none; }
a:hover { text-decoration: underline; text-underline-offset: 3px; }

code, pre, kbd { font-family: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace; font-variant-ligatures: none; }

/* ------------------------------------------------------------ the sidebar */

.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  width: 300px;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  padding: 20px 12px 40px;
  font-size: 15px;
  line-height: 1.45;
  z-index: 30;
  transition: transform 0.2s ease;
}

.sidebar-title {
  display: block;
  padding: 4px 10px 16px;
  color: var(--fg);
  font-weight: 600;
  font-size: 16px;
  letter-spacing: -0.01em;
}
.sidebar-title:hover { text-decoration: none; color: var(--accent-strong); }

.sidebar ol { list-style: none; margin: 0; padding: 0; }

.sidebar .part-row { display: flex; align-items: center; }
.sidebar .part-row a { flex: 1; }

.sidebar a {
  display: block;
  padding: 5px 10px;
  border-radius: 6px;
  color: var(--sidebar-fg);
}
.sidebar a:hover { background: var(--accent-faint); color: var(--accent-strong); text-decoration: none; }
.sidebar a[aria-current="page"] { color: var(--accent-strong); font-weight: 600; }
.sidebar .sections a.active { background: var(--accent-soft); color: var(--accent-strong); }

.sidebar .num {
  display: inline-block;
  min-width: 2.2em;
  color: var(--muted);
  font-family: 'JetBrains Mono', ui-monospace, monospace;
  font-size: 12px;
  font-weight: 400;
}

.sidebar .sections { display: none; margin: 2px 0 8px 14px; padding-left: 8px; border-left: 1px solid var(--border); }
.sidebar .open > .sections { display: block; }
.sidebar .sections a { font-size: 14px; padding: 4px 10px; }
.sidebar .sections .num { min-width: 3.4em; }

.sidebar .fold {
  flex: none;
  width: 28px;
  height: 28px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  position: relative;
}
.sidebar .fold:hover { background: var(--accent-faint); color: var(--accent-strong); }
.sidebar .fold::before {
  content: '';
  position: absolute;
  left: 11px;
  top: 10px;
  width: 6px;
  height: 6px;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: rotate(-45deg);
  transition: transform 0.15s ease;
}
.sidebar .open > .part-row .fold::before { transform: rotate(45deg); top: 9px; }

.scrim { display: none; }

/* ----------------------------------------------------- the page and its bar */

.page { margin-left: 300px; transition: margin-left 0.2s ease; min-height: 100vh; }

body.sidebar-closed .sidebar { transform: translateX(-100%); }
body.sidebar-closed .page { margin-left: 0; }
/* closed on the page before (theme.js): closed from the first frame */
html.sidebar-closed-early .sidebar { transform: translateX(-100%); transition: none; }
html.sidebar-closed-early .page { margin-left: 0; transition: none; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  height: 56px;
  padding: 0 16px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}

.topbar-tools { display: flex; gap: 2px; }

.icon {
  display: inline-grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.icon:hover, .icon[aria-expanded="true"] { background: var(--accent-faint); color: var(--accent-strong); }
.icon svg { fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
#toggle-search svg { fill: currentColor; stroke: none; }

.topbar-title {
  flex: 1;
  text-align: center;
  color: var(--fg);
  font-weight: 500;
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar-title:hover { text-decoration: none; color: var(--accent-strong); }

.topbar-links { display: flex; gap: 18px; font-size: 14px; }
.topbar-links a { color: var(--muted); }
.topbar-links a:hover { color: var(--accent-strong); text-decoration: none; }

.theme-menu {
  position: absolute;
  top: 48px;
  left: 50px;
  margin: 0;
  padding: 6px;
  list-style: none;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  font-size: 14px;
  z-index: 40;
}
.theme-menu button {
  display: block;
  width: 100%;
  padding: 6px 14px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--fg);
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.theme-menu button:hover { background: var(--accent-faint); color: var(--accent-strong); }
.theme-menu button[aria-checked="true"] { color: var(--accent-strong); font-weight: 600; }

/* ------------------------------------------------------------ the search */

.search { max-width: 820px; margin: 0 auto; padding: 20px 24px 0; }
.search input {
  width: 100%;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--fg);
  font: inherit;
  font-size: 17px;
  box-shadow: var(--shadow);
}
.search input:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
.search-summary { color: var(--muted); font-size: 14px; margin: 10px 4px; }
.search-results { list-style: none; margin: 0 0 24px; padding: 0; border-bottom: 1px solid var(--border); }
.search-results li { border-top: 1px solid var(--border); }
.search-results a { display: block; padding: 12px 4px; color: var(--fg); }
.search-results a:hover, .search-results a.selected { background: var(--accent-faint); text-decoration: none; }
.search-results .result-title { color: var(--accent-strong); font-weight: 600; }
.search-results .result-title .num { color: var(--muted); font-weight: 400; font-size: 13px; margin-right: 8px; font-family: 'JetBrains Mono', monospace; }
.search-results .result-text { display: block; color: var(--muted); font-size: 15px; line-height: 1.55; margin-top: 2px; }
.search-results mark, .content mark { background: var(--mark); color: inherit; border-radius: 2px; padding: 0 1px; }

/* ------------------------------------------------------------ the text */

.content { max-width: 820px; margin: 0 auto; padding: 8px 32px 64px; }

.part-label {
  margin: 40px 0 0;
  color: var(--accent);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

h1, h2, h3, h4, h5, h6 { line-height: 1.25; letter-spacing: -0.015em; font-weight: 600; }
h1 { font-size: 2.4rem; margin: 6px 0 28px; letter-spacing: -0.025em; }
h2 { font-size: 1.65rem; margin: 2.2em 0 0.6em; padding-top: 0.2em; }
h3 { font-size: 1.3rem; margin: 1.9em 0 0.5em; }
h4 { font-size: 1.1rem; margin: 1.6em 0 0.4em; }
h5, h6 { font-size: 1rem; margin: 1.4em 0 0.4em; }

a.header { color: inherit; position: relative; }
a.header:hover { text-decoration: none; }
a.header::before {
  content: '#';
  position: absolute;
  left: -0.9em;
  color: var(--accent);
  opacity: 0;
  transition: opacity 0.15s ease;
}
a.header:hover::before { opacity: 1; }

p { margin: 0 0 1.1em; }

ul, ol { padding-left: 1.6em; margin: 0 0 1.1em; }
li { margin: 0.25em 0; }
li > p { margin: 0 0 0.5em; }
li::marker { color: var(--accent); }

dl { margin: 0 0 1.2em; }
dt { font-weight: 600; margin-top: 0.9em; }
dd { margin: 0.2em 0 0 1.4em; }
dd > p { margin-bottom: 0.5em; }

code {
  font-size: 0.86em;
  padding: 0.12em 0.36em;
  border-radius: 5px;
  background: var(--code-inline);
  white-space: nowrap;
}
a code { color: inherit; }
h1 code, h2 code, h3 code, h4 code { font-size: 0.85em; }

pre {
  position: relative;
  margin: 0 0 1.3em;
  padding: 16px 18px;
  border-radius: 10px;
  background: var(--code-bg);
  border: 1px solid var(--border);
  overflow-x: auto;
  font-size: 14px;
  line-height: 1.6;
  tab-size: 4;
}
pre code { padding: 0; background: none; font-size: inherit; border-radius: 0; overflow-wrap: normal; white-space: inherit; }

pre.screen { background: var(--screen-bg); color: var(--screen-fg); border-color: transparent; }

/* the tools of a listing: Copy on hover, Run (where the program runs) always */
.tools { position: absolute; top: 8px; right: 8px; display: flex; gap: 6px; }
.tools button {
  padding: 3px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--muted);
  font: 500 12px 'DM Sans', system-ui, sans-serif;
  cursor: pointer;
}
.tools button:hover { color: var(--accent-strong); border-color: var(--accent); }
.tools .copy { opacity: 0; transition: opacity 0.15s ease; }
pre:hover .copy, .tools .copy:focus { opacity: 1; }
@media (hover: none) { .tools .copy { opacity: 1; } }
.tools .run { background: var(--accent); border-color: var(--accent); color: #fff; }
.tools .run:hover { background: var(--accent-strong); border-color: var(--accent-strong); color: #fff; }
:root[data-theme="dark"] .tools .run, :root[data-theme="dark"] .tools .run:hover { color: #052e1f; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .tools .run, :root:not([data-theme="light"]) .tools .run:hover { color: #052e1f; }
}
pre.screen .tools button { background: #292524; border-color: #44403c; color: #d6d3d1; }

/* what a run printed, under its listing */
pre.runnable:has(+ .run-output) { margin-bottom: 0; border-radius: 10px 10px 0 0; }
.run-output {
  margin: 0 0 1.3em;
  padding: 10px 18px 12px;
  border: 1px solid var(--border);
  border-top: 1px dashed var(--border);
  border-radius: 0 0 10px 10px;
  background: var(--bg);
}
.run-status { color: var(--muted); font-size: 13px; }
.run-status.ok { color: var(--accent-strong); }
.run-status.bad, .run-output .stderr { color: var(--bad); }
.run-output pre { display: none; margin: 8px 0 0; padding: 0; border: 0; background: none; }
.run-output.has-output pre { display: block; }

.hl-keyword { color: var(--hl-keyword); font-weight: 500; }
.hl-string { color: var(--hl-string); }
.hl-comment { color: var(--hl-comment); font-style: italic; }
.hl-number { color: var(--hl-number); }
.hl-type { color: var(--hl-type); }
.hl-value { color: var(--hl-value); }
.hl-sigil { color: var(--hl-sigil); }
.hl-operator { color: var(--hl-keyword); }

kbd {
  display: inline-block;
  padding: 0.05em 0.45em;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: var(--bg);
  font-size: 0.82em;
  line-height: 1.4;
}

.gui { font-weight: 600; }
var { font-style: italic; color: var(--muted); }

q { quotes: '\201C' '\201D' '\2018' '\2019'; }

.table-wrap { overflow-x: auto; margin: 0 0 1.4em; border: 1px solid var(--border); border-radius: 10px; }
table { border-collapse: collapse; width: 100%; font-size: 15px; line-height: 1.5; }
th, td { padding: 9px 14px; text-align: left; vertical-align: top; border-bottom: 1px solid var(--border); }
th { background: var(--accent-faint); font-weight: 600; color: var(--fg); }
tr:last-child td { border-bottom: 0; }
td > p:last-child, th > p:last-child { margin-bottom: 0; }

aside.note {
  margin: 0 0 1.4em;
  padding: 14px 18px 4px;
  border-left: 4px solid var(--accent);
  border-radius: 0 10px 10px 0;
  background: var(--accent-faint);
}
.note-title { margin: 0 0 0.3em; color: var(--accent-strong); font-weight: 600; font-size: 14px; letter-spacing: 0.04em; text-transform: uppercase; }

figure.example { margin: 0 0 1.5em; }
figure.example figcaption { margin: 0 0 6px; color: var(--muted); font-size: 14px; font-weight: 500; }
figure.example figcaption::before { content: 'Example: '; color: var(--accent); font-weight: 600; }

.grammar {
  display: grid;
  grid-template-columns: max-content 2.4em 1fr;
  margin: 0 0 1.5em;
  font-family: 'JetBrains Mono', ui-monospace, monospace;
  font-size: 14px;
  line-height: 1.6;
}
/* one column for every left side of the grammar, so the ::= stand in a line */
.production {
  display: grid;
  grid-column: 1 / -1;
  grid-template-columns: subgrid;
  padding: 6px 10px;
  border-radius: 6px;
}
.production:nth-child(odd) { background: var(--code-bg); }
.production:target { background: var(--accent-soft); }
.production .lhs { color: var(--fg); font-weight: 500; }
.production .defines { color: var(--muted); text-align: center; }
.production .rhs { color: var(--hl-string); overflow-wrap: anywhere; }
.production .nonterminal { color: var(--accent-strong); }

/* ----------------------------------------------------------- the pager */

.pager { display: flex; gap: 16px; margin-top: 56px; padding-top: 24px; border-top: 1px solid var(--border); }
.pager a, .pager span { flex: 1; }
.pager a {
  display: block;
  padding: 14px 18px;
  border: 1px solid var(--border);
  border-radius: 10px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.pager a:hover { border-color: var(--accent); box-shadow: var(--shadow); text-decoration: none; }
.pager a.next { text-align: right; }
.pager-label { display: block; color: var(--muted); font-size: 13px; }
.pager a.prev .pager-label::before { content: '\2190  '; }
.pager a.next .pager-label::after { content: '  \2192'; }
.pager-title { display: block; color: var(--accent-strong); font-weight: 600; }

/* ------------------------------------------------------- the title page */

.title-page { padding: 56px 0 12px; }
.title-page h1 { font-size: 3rem; margin: 0 0 16px; }
.title-page .lead { font-size: 1.2rem; color: var(--muted); font-weight: 300; max-width: 36em; }
.actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 28px; }
.button {
  display: inline-block;
  padding: 9px 18px;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--fg);
  font-weight: 500;
  font-size: 15px;
}
.button:hover { border-color: var(--accent); color: var(--accent-strong); text-decoration: none; }
.button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.button.primary:hover { background: var(--accent-strong); color: #fff; }
:root[data-theme="dark"] .button.primary { color: #052e1f; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .button.primary { color: #052e1f; } }

ol.contents { list-style: none; padding: 0; }
ol.contents > li { margin: 0 0 18px; }
ol.contents > li > a { font-size: 1.15rem; font-weight: 600; color: var(--fg); }
ol.contents > li > a:hover { color: var(--accent-strong); }
ol.contents .num { display: inline-block; min-width: 2em; color: var(--accent); font-family: 'JetBrains Mono', monospace; font-size: 0.9em; }
ol.contents ol { list-style: none; padding-left: 2em; margin: 4px 0 0; columns: 2; column-gap: 24px; font-size: 15px; }
ol.contents ol li { margin: 0; padding: 2px 0; break-inside: avoid; }
ol.contents ol a { color: var(--sidebar-fg); }
ol.contents ol .num { color: var(--muted); font-size: 12px; min-width: 3em; }

/* ------------------------------------------------------- narrow screens */

@media (max-width: 1079px) {
  .page { margin-left: 0; }
  .sidebar { transform: translateX(-100%); box-shadow: none; }
  body.sidebar-open .sidebar { transform: none; box-shadow: var(--shadow); }
  body.sidebar-open .scrim {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 25;
    background: rgba(12, 10, 9, 0.35);
  }
  body.sidebar-closed .sidebar { transform: translateX(-100%); }
}

@media (max-width: 720px) {
  body { font-size: 16px; }
  code { white-space: normal; overflow-wrap: anywhere; }
  pre code { white-space: inherit; overflow-wrap: normal; }
  .topbar { padding: 0 8px; gap: 4px; }
  .topbar-links { display: none; }
  .content { padding: 4px 18px 48px; }
  h1 { font-size: 1.9rem; }
  h2 { font-size: 1.4rem; }
  .title-page h1 { font-size: 2.2rem; }
  pre { font-size: 13px; padding: 14px; margin-left: -4px; margin-right: -4px; }
  .grammar { grid-template-columns: 1fr; }
  .production { grid-template-columns: 1fr; }
  .production .defines { text-align: left; }
  ol.contents ol { columns: 1; }
  .pager { flex-direction: column; }
  a.header::before { display: none; }
}

@media print {
  .sidebar, .topbar, .search, .pager, .copy, .scrim { display: none !important; }
  .page { margin: 0; }
  pre { white-space: pre-wrap; }
}
