/* ============================================================
   cld-toys — Guided Exercises Shared Stylesheet
   Inspired by Tufte CSS. Designed for readability and print.
   ============================================================ */

:root {
  --text: #1a1a1a;
  --muted: #555;
  --faint: #999;
  --border: #ddd;
  --bg: #fafaf8;
  --surface: #fff;
  --accent: #1a56db;
  --accent-bg: #eff6ff;
  --warn: #b45309;
  --warn-bg: #fffbeb;
  --success: #166534;
  --success-bg: #f0fdf4;
  --danger: #991b1b;
  --danger-bg: #fef2f2;
  --code-bg: #f4f4f0;
  --session-a: #1a56db;
  --session-a-bg: #eff6ff;
  --session-b: #7c3aed;
  --session-b-bg: #f5f3ff;
  --session-c: #047857;
  --session-c-bg: #ecfdf5;
  --max-width: 760px;
  --font-serif: Georgia, 'Times New Roman', serif;
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono: 'SF Mono', 'Fira Code', 'Consolas', monospace;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 18px; background: var(--bg); color: var(--text); }

body {
  font-family: var(--font-serif);
  line-height: 1.7;
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 3rem 1.5rem 6rem;
}

/* ---- Typography ---- */

h1 { font-size: 2rem; line-height: 1.2; font-weight: 700; margin-bottom: 0.25rem; }
h2 { font-size: 1.3rem; font-weight: 700; margin: 2.5rem 0 0.75rem; }
h3 { font-size: 1.05rem; font-weight: 700; font-family: var(--font-sans); margin: 1.75rem 0 0.5rem; }
p  { margin-bottom: 1rem; }
p:last-child { margin-bottom: 0; }

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

strong { font-weight: 700; }
em { font-style: italic; }

ul, ol { padding-left: 1.4rem; margin-bottom: 1rem; }
li { margin-bottom: 0.3rem; }

hr { border: none; border-top: 1px solid var(--border); margin: 2.5rem 0; }

/* ---- Page header ---- */

.exercise-meta {
  font-family: var(--font-sans);
  font-size: 0.8rem;
  color: var(--faint);
  letter-spacing: 0.05em;
  text-transform: uppercase;
  margin-bottom: 1rem;
}

.exercise-meta a { color: var(--faint); }

.exercise-title { margin-bottom: 0.5rem; }

.exercise-subtitle {
  font-size: 1.1rem;
  color: var(--muted);
  font-style: italic;
  margin-bottom: 2rem;
  font-family: var(--font-serif);
}

/* ---- Callout boxes ---- */

.callout {
  border-left: 3px solid;
  padding: 0.9rem 1.1rem;
  margin: 1.5rem 0;
  border-radius: 0 4px 4px 0;
  font-family: var(--font-sans);
  font-size: 0.92rem;
}

.callout-label {
  font-weight: 700;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  display: block;
  margin-bottom: 0.3rem;
}

.callout.key      { border-color: var(--accent); background: var(--accent-bg); }
.callout.key      .callout-label { color: var(--accent); }
.callout.warn     { border-color: var(--warn); background: var(--warn-bg); }
.callout.warn     .callout-label { color: var(--warn); }
.callout.example  { border-color: #6b7280; background: #f9fafb; }
.callout.example  .callout-label { color: #374151; }

/* ---- Code ---- */

code {
  font-family: var(--font-mono);
  font-size: 0.83em;
  background: var(--code-bg);
  padding: 0.1em 0.35em;
  border-radius: 3px;
  border: 1px solid var(--border);
}

pre {
  background: #1e1e2e;
  color: #cdd6f4;
  padding: 1.2rem 1.4rem;
  border-radius: 6px;
  overflow-x: auto;
  margin: 1.5rem 0;
  font-family: var(--font-mono);
  font-size: 0.82rem;
  line-height: 1.6;
}

pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: 1em;
  color: inherit;
}

/* SQL / shell syntax */
.kw  { color: #89b4fa; font-weight: 600; }
.fn  { color: #cba6f7; }
.str { color: #a6e3a1; }
.cmt { color: #6c7086; font-style: italic; }
.num { color: #fab387; }
.op  { color: #89dceb; }

/* ---- Tables ---- */

table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--font-sans);
  font-size: 0.88rem;
  margin: 1.5rem 0;
}

th {
  text-align: left;
  font-weight: 700;
  padding: 0.6rem 0.8rem;
  border-bottom: 2px solid var(--border);
  color: var(--muted);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

td {
  padding: 0.6rem 0.8rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

tr:last-child td { border-bottom: none; }
tr:hover td { background: #f8f8f6; }

/* ---- Session tags (which psql session a step belongs to) ---- */

.session-tag {
  display: inline-block;
  font-family: var(--font-sans);
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0.15em 0.6em;
  border-radius: 999px;
  margin-bottom: 0.5rem;
}

.session-tag.a { color: var(--session-a); background: var(--session-a-bg); }
.session-tag.b { color: var(--session-b); background: var(--session-b-bg); }
.session-tag.c { color: var(--session-c); background: var(--session-c-bg); }

/* Not a session — a plain shell on the machine you're sitting at, or a
   different vantage point entirely (macOS host vs the Linux VM vs the
   container). Deliberately grey so it doesn't compete with the coloured
   session tags, which by convention mean concurrent connections. */
.session-tag.host {
  color: var(--muted);
  background: #eeeeec;
  border: 1px solid var(--border);
}

/* ---- Comparing two runs ----
   Two shapes, one job: showing what changed between run A and run B.
   `table.compare` for a metric-by-metric breakdown (label | before | after);
   `.compare-pair` for two raw transcripts side by side. */

table.compare td:first-child {
  font-weight: 700;
  color: var(--muted);
  white-space: nowrap;
}

table.compare tbody td:nth-child(2) { background: var(--danger-bg); }
table.compare tbody td:nth-child(3) { background: var(--success-bg); }
table.compare tbody tr:hover td:nth-child(2) { background: #fde8e8; }
table.compare tbody tr:hover td:nth-child(3) { background: #dcfce7; }

table.compare thead th:nth-child(2) { color: var(--danger); }
table.compare thead th:nth-child(3) { color: var(--success); }

/* Opt out of the good/bad reading when the two columns are merely
   different rather than worse/better. */
table.compare.neutral tbody td:nth-child(2),
table.compare.neutral tbody td:nth-child(3),
table.compare.neutral tbody tr:hover td:nth-child(2),
table.compare.neutral tbody tr:hover td:nth-child(3) { background: transparent; }
table.compare.neutral thead th:nth-child(2),
table.compare.neutral thead th:nth-child(3) { color: var(--muted); }

/* A cell spanning both comparison columns — for rows where the two sides
   are identical and the point is that they're identical. */
table.compare td.span-both {
  text-align: center;
  font-style: italic;
  color: var(--muted);
}

table.compare tbody td.span-both,
table.compare tbody tr:hover td.span-both { background: var(--surface); }

.compare-pair {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin: 1.5rem 0;
  align-items: start;
}

.compare-pair .pane-label {
  display: block;
  font-family: var(--font-sans);
  font-size: 0.76rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--faint);
  margin-bottom: 0.35rem;
}

.compare-pair .terminal-output,
.compare-pair pre { margin: 0; }

/* At half width, wrapping mangles aligned psql output — scroll each pane
   independently instead. */
.compare-pair .terminal-output {
  white-space: pre;
  overflow-x: auto;
}

@media (max-width: 700px) {
  .compare-pair { grid-template-columns: 1fr; gap: 1.2rem; }
}

/* ---- Exercise step ---- */

.step {
  margin: 1.75rem 0;
  padding: 1.1rem 1.3rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
}

.step-number {
  font-family: var(--font-sans);
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--faint);
  margin-bottom: 0.4rem;
}

.step pre { margin: 0.75rem 0; }
.step p:last-child { margin-bottom: 0; }

/* ---- Predict/reveal (native <details>) ---- */

details.reveal {
  margin-top: 0.9rem;
  border-top: 1px dashed var(--border);
  padding-top: 0.9rem;
}

details.reveal summary {
  cursor: pointer;
  font-family: var(--font-sans);
  font-weight: 600;
  font-size: 0.88rem;
  color: var(--accent);
  list-style: none;
}

details.reveal summary::-webkit-details-marker { display: none; }
details.reveal summary::before {
  content: "▸ ";
  display: inline-block;
  transition: transform 0.15s;
}
details.reveal[open] summary::before { content: "▾ "; }

details.reveal .observed {
  margin-top: 0.8rem;
}

.terminal-output {
  background: #1e1e2e;
  color: #cdd6f4;
  padding: 0.9rem 1.1rem;
  border-radius: 6px;
  font-family: var(--font-mono);
  font-size: 0.8rem;
  line-height: 1.55;
  white-space: pre-wrap;
  margin: 0.6rem 0;
}

/* ---- A session that is just sitting there ----
   For steps whose correct observation is *no output*: a blocked COMMIT, a
   synchronous replica that never confirms, a statement waiting on a lock. */

.terminal-output.waiting {
  color: var(--faint);
  font-style: italic;
  border-left: 3px solid var(--warn);
  white-space: normal;
}

/* inline-block, not a flex item — the text must stay one flowing line */
.terminal-output.waiting::before {
  content: "";
  display: inline-block;
  width: 0.5rem;
  height: 0.9rem;
  background: var(--warn);
  margin-right: 0.5rem;
  vertical-align: -0.12rem;
  animation: cursor-blink 1.1s step-end infinite;
}

@keyframes cursor-blink {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .terminal-output.waiting::before { animation: none; }
}

/* ---- Click-to-copy (progressive enhancement; see assets/copy.js) ----
   The button is injected by JS, so pages render fine with JS disabled. */

pre { position: relative; }

.copy-btn {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  font-family: var(--font-sans);
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #cdd6f4;
  background: rgba(205, 214, 244, 0.12);
  border: 1px solid rgba(205, 214, 244, 0.25);
  border-radius: 4px;
  padding: 0.25em 0.6em;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s, background 0.15s;
}

pre:hover .copy-btn,
.copy-btn:focus-visible { opacity: 1; }

.copy-btn:hover { background: rgba(205, 214, 244, 0.22); }
.copy-btn.copied { color: #a6e3a1; border-color: #a6e3a1; opacity: 1; }

/* Touch devices have no hover — keep the button permanently visible. */
@media (hover: none) {
  .copy-btn { opacity: 0.75; }
}

/* An editorial cut inside a transcript — lines removed for length.
   Styled so it reads as *our* elision, not as something the program
   printed. Use as: <span class="elide">40 more lines</span> */
.terminal-output .elide {
  /* inline, not block: the parent is white-space: pre-wrap, so the real
     newlines around this span already break the line. display:block would
     add a second break and double-space every marker. */
  display: inline;
  color: var(--faint);
  font-style: italic;
  font-size: 0.9em;
  opacity: 0.75;
}

.terminal-output .elide::before { content: "⋮  "; font-style: normal; }

/* ---- Diagram / visual blocks ---- */

.diagram {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 6px;
  padding: 1.2rem;
  margin: 1.5rem 0;
  font-family: var(--font-mono);
  font-size: 0.82rem;
  line-height: 1.5;
  overflow-x: auto;
  white-space: pre;
}

/* ---- Citation ---- */

.cite {
  font-family: var(--font-sans);
  font-size: 0.78rem;
  color: var(--faint);
}

.cite a { color: var(--faint); }

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

.exercise-footer {
  margin-top: 3rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
  font-family: var(--font-sans);
  font-size: 0.82rem;
  color: var(--faint);
}

/* ---- Index / landing page ---- */

.index-card {
  display: block;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  padding: 1.1rem 1.3rem;
  margin: 1rem 0;
  text-decoration: none;
  color: inherit;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.index-card:hover {
  opacity: 1;
  border-color: var(--accent);
  box-shadow: 0 1px 6px rgba(26, 86, 219, 0.08);
}

.card-tag {
  display: inline-block;
  font-family: var(--font-sans);
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--accent);
  background: var(--accent-bg);
  padding: 0.15em 0.6em;
  border-radius: 999px;
  margin-bottom: 0.6rem;
}

.card-title {
  font-size: 1.15rem;
  font-weight: 700;
  margin-bottom: 0.35rem;
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.card-aha {
  color: var(--muted);
  font-size: 0.95rem;
  margin-bottom: 0.6rem;
}

.card-meta {
  font-family: var(--font-sans);
  font-size: 0.76rem;
  color: var(--faint);
}

.upcoming {
  font-family: var(--font-sans);
  font-size: 0.88rem;
  color: var(--muted);
  list-style: none;
  padding-left: 0;
}

.upcoming li {
  padding: 0.4rem 0;
  border-bottom: 1px solid var(--border);
}

.upcoming li:last-child { border-bottom: none; }

.upcoming .system {
  display: inline-block;
  min-width: 7.5rem;
  color: var(--faint);
  font-size: 0.76rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* ---- Print ---- */

@media print {
  body { padding: 0; max-width: 100%; }
  pre, .terminal-output { background: #f4f4f0; color: var(--text); border: 1px solid var(--border); }
  details.reveal summary { display: none; }
  details.reveal .observed { display: block !important; }
}
