/* =========================================================
   Demo kit: shared building blocks for interactive illustrations.
   Everything is prefixed `dk-`. Each topic's demo is wrapped in
   <div class="demo d-{topicId}"> so topic CSS can be scoped.
   ========================================================= */

.demo {
  /* Mock-UI tokens: demos should use these so they work in light + dark */
  --dk-bg: var(--bg-sunken);
  --dk-surface: var(--surface);
  --dk-surface-2: var(--surface-2);
  --dk-ink: var(--text);
  --dk-ink-2: var(--text-2);
  --dk-ink-3: var(--text-3);
  --dk-line: var(--border);
  --dk-line-strong: var(--border-strong);
  --dk-accent: var(--accent);
  --dk-accent-soft: var(--accent-soft);
  --dk-on-accent: var(--on-accent);
  --dk-good: var(--success);
  --dk-bad: var(--danger);
  --dk-warn: var(--warning);
  --dk-placeholder: color-mix(in oklch, var(--text) 12%, transparent);
  --dk-placeholder-2: color-mix(in oklch, var(--text) 7%, transparent);
  position: relative;
  font-family: var(--font-sans);
  color: var(--dk-ink);
  line-height: 1.5;
}

/* ---------- Stage: the canvas where the illustration lives ---------- */
.dk-stage {
  position: relative;
  display: flex; align-items: center; justify-content: center; flex-wrap: wrap;
  gap: var(--s-6);
  min-height: 260px;
  padding: var(--s-8) var(--s-6);
  background-color: var(--dk-bg);
  background-image: radial-gradient(color-mix(in oklch, var(--text) 9%, transparent) 1px, transparent 1px);
  background-size: 16px 16px;
  overflow: hidden;
}
.dk-stage.plain { background-image: none; }
.dk-stage.tall { min-height: 380px; }
.dk-stage.col { flex-direction: column; }
.dk-stage.start { align-items: flex-start; justify-content: flex-start; }
@media (max-width: 640px) { .dk-stage { padding: var(--s-6) var(--s-4); gap: var(--s-4); } }

/* ---------- Controls bar ---------- */
.dk-controls {
  display: flex; flex-wrap: wrap; align-items: center;
  gap: var(--s-3) var(--s-6);
  padding: var(--s-4) var(--s-5);
  border-top: 1px solid var(--dk-line);
  background: var(--dk-surface);
}
.dk-control { display: flex; align-items: center; gap: var(--s-3); font-size: var(--t-sm); color: var(--dk-ink-2); min-width: 0; }
.dk-control > .dk-control-label { font-weight: 500; white-space: nowrap; }
.dk-control output, .dk-value { min-width: 4.5ch; font: 500 var(--t-xs)/1 var(--font-mono); color: var(--dk-ink); font-variant-numeric: tabular-nums; }
.dk-control input[type="range"] { width: 140px; accent-color: var(--dk-accent); }
@media (max-width: 640px) {
  .dk-controls { flex-direction: column; align-items: stretch; }
  .dk-control { justify-content: space-between; }
  .dk-control input[type="range"] { flex: 1; width: auto; }
}

/* Segmented control */
.dk-seg { display: inline-flex; flex-wrap: wrap; padding: 3px; gap: 2px; border-radius: var(--r-md); background: var(--dk-bg); }
.dk-seg button {
  min-height: 32px; padding: 0 var(--s-3);
  border: 0; border-radius: 7px; background: transparent;
  font-size: var(--t-xs); font-weight: 600; color: var(--dk-ink-2);
  transition: background var(--d-fast), color var(--d-fast);
}
.dk-seg button:hover { color: var(--dk-ink); }
.dk-seg button[aria-pressed="true"] { background: var(--dk-surface); color: var(--dk-ink); box-shadow: var(--shadow-1); }

/* Toggle switch */
.dk-switch { position: relative; display: inline-flex; align-items: center; gap: var(--s-2); cursor: pointer; font-size: var(--t-sm); color: var(--dk-ink-2); font-weight: 500; }
.dk-switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.dk-switch .track { width: 36px; height: 22px; border-radius: 999px; background: var(--dk-line-strong); position: relative; transition: background var(--d-base); flex: none; }
.dk-switch .track::after { content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: white; box-shadow: 0 1px 2px oklch(0 0 0 / 0.3); transition: transform var(--d-base) var(--ease-out); }
.dk-switch input:checked + .track { background: var(--dk-accent); }
.dk-switch input:checked + .track::after { transform: translateX(14px); }
.dk-switch input:focus-visible + .track { outline: 2px solid var(--focus); outline-offset: 2px; }

/* ---------- Compare layouts: bad vs good, before vs after ---------- */
.dk-compare { display: grid; grid-template-columns: 1fr 1fr; gap: var(--s-4); width: 100%; }
.dk-compare.three { grid-template-columns: repeat(3, 1fr); }
@media (max-width: 720px) { .dk-compare, .dk-compare.three { grid-template-columns: 1fr; } }
.dk-panel {
  position: relative;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: var(--s-4);
  min-width: 0;
  padding: var(--s-10) var(--s-5) var(--s-6);
  border-radius: var(--r-lg);
  background: var(--dk-surface);
  border: 1px solid var(--dk-line);
}
.dk-panel > .dk-label { position: absolute; top: var(--s-3); left: var(--s-3); }
.dk-label {
  display: inline-flex; align-items: center; gap: 6px;
  height: 22px; padding: 0 8px; border-radius: 6px;
  font-size: 0.6875rem; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase;
  background: var(--dk-bg); color: var(--dk-ink-2);
}
.dk-label.bad, .dk-bad > .dk-label { background: color-mix(in oklch, var(--dk-bad) 14%, transparent); color: var(--dk-bad); }
.dk-label.good, .dk-good > .dk-label { background: color-mix(in oklch, var(--dk-good) 16%, transparent); color: var(--dk-good); }
.dk-label.bad::before, .dk-bad > .dk-label::before { content: "✕"; }
.dk-label.good::before, .dk-good > .dk-label::before { content: "✓"; }

/* ---------- Mock UI primitives ---------- */
.dk-card {
  background: var(--dk-surface);
  border: 1px solid var(--dk-line);
  border-radius: 14px;
  padding: var(--s-5);
  box-shadow: var(--shadow-1);
  color: var(--dk-ink);
  width: 100%; max-width: 320px;
}
.dk-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 38px; padding: 0 16px;
  border-radius: 9px; border: 1px solid transparent;
  font: 600 var(--t-sm)/1 var(--font-sans);
  background: var(--dk-surface); color: var(--dk-ink);
  border-color: var(--dk-line-strong);
  cursor: pointer;
  transition: background var(--d-fast), transform var(--d-fast) var(--ease-out), box-shadow var(--d-fast), border-color var(--d-fast);
}
.dk-btn:hover { background: var(--surface-hover); }
.dk-btn--primary { background: var(--dk-accent); color: var(--dk-on-accent); border-color: transparent; }
.dk-btn--primary:hover { background: var(--accent-hover); }
.dk-btn--ghost { background: transparent; border-color: transparent; color: var(--dk-ink-2); }
.dk-btn--ghost:hover { background: var(--dk-bg); color: var(--dk-ink); }
.dk-btn--danger { background: var(--dk-bad); color: white; border-color: transparent; }
.dk-btn--sm { min-height: 30px; padding: 0 10px; font-size: var(--t-xs); border-radius: 7px; }

.dk-input {
  display: block; width: 100%;
  height: 40px; padding: 0 12px;
  border-radius: 9px; border: 1px solid var(--dk-line-strong);
  background: var(--dk-surface); color: var(--dk-ink);
  font: 400 var(--t-sm)/1 var(--font-sans);
}
.dk-input:focus { outline: 2px solid var(--focus); outline-offset: 1px; }
.dk-field { display: grid; gap: 6px; width: 100%; }
.dk-field > label { font-size: var(--t-sm); font-weight: 500; color: var(--dk-ink); }
.dk-help { font-size: var(--t-xs); color: var(--dk-ink-3); }

.dk-avatar { display: grid; place-items: center; flex: none; width: 36px; height: 36px; border-radius: 50%; background: var(--dk-accent-soft); color: var(--accent-ink); font: 600 var(--t-xs)/1 var(--font-sans); overflow: hidden; }
.dk-chip { display: inline-flex; align-items: center; gap: 6px; height: 26px; padding: 0 10px; border-radius: 999px; background: var(--dk-bg); color: var(--dk-ink-2); font-size: var(--t-xs); font-weight: 500; }

/* Placeholder "skeleton" text lines & images for wireframe-style mockups */
.dk-line { display: block; height: 10px; border-radius: 5px; background: var(--dk-placeholder); }
.dk-line.short { width: 40%; } .dk-line.mid { width: 65%; } .dk-line.long { width: 90%; }
.dk-line.thick { height: 16px; border-radius: 6px; background: color-mix(in oklch, var(--text) 22%, transparent); }
.dk-lines { display: grid; gap: 8px; width: 100%; }
.dk-img {
  display: grid; place-items: center;
  width: 100%; aspect-ratio: 16 / 10;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--dk-placeholder-2), var(--dk-placeholder));
  color: var(--dk-ink-3);
}
.dk-img::after { content: ""; width: 28%; aspect-ratio: 1; max-width: 48px; background: currentColor; opacity: 0.35; -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='1.5'%3E%3Crect x='3' y='4' width='18' height='16' rx='2'/%3E%3Ccircle cx='9' cy='10' r='2'/%3E%3Cpath d='m21 16-5-5-9 9'/%3E%3C/svg%3E") center / contain no-repeat; mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='1.5'%3E%3Crect x='3' y='4' width='18' height='16' rx='2'/%3E%3Ccircle cx='9' cy='10' r='2'/%3E%3Cpath d='m21 16-5-5-9 9'/%3E%3C/svg%3E") center / contain no-repeat; }

/* Phone / browser frames */
.dk-phone {
  position: relative; flex: none;
  width: 220px; height: 440px;
  padding: 10px;
  border-radius: 36px;
  background: oklch(0.22 0.01 70);
  box-shadow: var(--shadow-2), inset 0 0 0 1.5px oklch(0.4 0.01 70);
}
.dk-phone > .dk-screen { position: relative; width: 100%; height: 100%; border-radius: 27px; overflow: hidden; background: var(--dk-surface); }
.dk-browser { width: 100%; max-width: 640px; border-radius: 12px; overflow: hidden; background: var(--dk-surface); border: 1px solid var(--dk-line-strong); box-shadow: var(--shadow-2); }
.dk-browser > .dk-browser-bar { display: flex; align-items: center; gap: 6px; height: 32px; padding: 0 12px; background: var(--dk-surface-2); border-bottom: 1px solid var(--dk-line); }
.dk-browser > .dk-browser-bar::before { content: ""; width: 10px; height: 10px; border-radius: 50%; background: oklch(0.7 0.16 25); box-shadow: 16px 0 oklch(0.8 0.15 85), 32px 0 oklch(0.72 0.15 145); }
.dk-browser > .dk-browser-body { padding: var(--s-5); }

/* Annotation helpers: measurement badges and callouts */
.dk-measure { font: 600 10px/1 var(--font-mono); color: oklch(0.6 0.22 350); background: oklch(0.6 0.22 350 / 0.12); padding: 3px 5px; border-radius: 4px; white-space: nowrap; }
.dk-guide { position: absolute; pointer-events: none; background: oklch(0.6 0.22 350 / 0.6); }
.dk-note { font-size: var(--t-xs); color: var(--dk-ink-3); text-align: center; max-width: 52ch; text-wrap: pretty; }
.dk-tag { display: inline-block; font: 600 10px/1 var(--font-mono); padding: 3px 6px; border-radius: 4px; background: var(--dk-ink); color: var(--dk-surface); letter-spacing: 0.02em; }

/* Generic helpers */
.dk-row { display: flex; align-items: center; gap: var(--s-3); flex-wrap: wrap; }
.dk-col { display: flex; flex-direction: column; gap: var(--s-3); }
.dk-center { display: grid; place-items: center; }
.dk-w-full { width: 100%; }
.dk-mono { font-family: var(--font-mono); }
.dk-muted { color: var(--dk-ink-3); }
.dk-small { font-size: var(--t-xs); }
.dk-swatch { width: 44px; height: 44px; border-radius: 10px; border: 1px solid var(--dk-line); flex: none; }
.dk-readout { font: 500 var(--t-xs)/1.4 var(--font-mono); color: var(--dk-ink-2); background: var(--dk-surface); border: 1px solid var(--dk-line); padding: 6px 10px; border-radius: 8px; font-variant-numeric: tabular-nums; }

/* Mock app “toast” used inside demos (not the global one) */
.dk-toast { display: inline-flex; align-items: center; gap: 10px; padding: 10px 14px; border-radius: 10px; background: var(--dk-ink); color: var(--dk-surface); font-size: var(--t-sm); box-shadow: var(--shadow-3); }

/* ---------- Shared fixes ---------- */
.demo [hidden] { display: none !important; }
.dk-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.dk-btn--danger { color: oklch(0.99 0 0); background: oklch(0.55 0.19 25); }
.dk-btn--danger:hover { background: oklch(0.5 0.19 25); }
.dk-sr-only { position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.dk-kbd { display: inline-grid; place-items: center; min-width: 1.6em; height: 1.6em; padding: 0 0.4em; font: 500 0.6875rem/1 var(--font-mono); color: var(--dk-ink-2); background: var(--dk-surface); border: 1px solid var(--dk-line-strong); border-bottom-width: 2px; border-radius: 5px; }
