/* Cloud Distance Lab (public page /technology/distance-lab), ported from ../Distances/index.html.
 * Loaded on that page only.
 *
 * Tokens: the lab's structural tokens map onto the site tokens (app.css), so the lab follows the site's palette, fonts
 * and dark mode; only the data colors (sets A/B and the eight metric series --s1…--s8) are the lab's own. The canvas
 * code reads these tokens from :root (readTokens in distance-lab.js), so they live on :root and must stay plain colors
 * or var() of the site's plain-color tokens — no color-mix(), which canvas color parsing does not accept.
 * Components: scoped to .dlab, because several class names (.row, .btn, .card, .check, .chart, .dot, .chip, .mono,
 * .eyebrow …) also exist in app.css / public/_style.html. Layout breakpoints are container queries on .dlab, so the
 * three-column bench adapts to the width of the site container rather than of the viewport. */

:root {
  --ink: var(--text);
  --ink-2: var(--text-2);
  --line: var(--border);
  --grid: var(--chart-grid);
  --grid-strong: #cfd7e4;
  --axis: var(--chart-axis);
  --control: var(--primary);
  --font-body: var(--font-sans);
  --font-display: var(--font-sans);
  --set-a: #2a78d6;
  --set-b: #eb6834;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948;
}
@media (prefers-color-scheme: dark) {
  :root {
    --grid-strong: #34425f;
    --set-a: #3987e5; --set-b: #d95926;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  }
}

/* ---------- lab root ---------- */
.dlab {
  container: dlab / inline-size;
  display: flex;
  flex-direction: column;
  gap: 14px;
  color: var(--ink);
  font: 14px/1.45 var(--font-body);
}
.dlab button, .dlab input, .dlab select { font: inherit; color: inherit; }
.dlab :focus-visible { outline: 2px solid var(--control); outline-offset: 2px; }

/* ---------- scenarios ---------- */
.dlab .dlab-top { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: center; justify-content: space-between; }
.dlab .presets { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.dlab .eyebrow {
  display: inline;
  margin: 0;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}
.dlab .presets .eyebrow { margin-right: 4px; }
.dlab .chip {
  display: inline-flex;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 13px;
  font-weight: 500;
  line-height: 1.3;
  cursor: pointer;
  color: var(--ink-2);
  transition: border-color .15s, color .15s, background .15s;
}
.dlab .chip:hover { border-color: var(--control); color: var(--ink); }
.dlab .chip[aria-pressed="true"] { background: var(--ink); color: var(--surface); border-color: var(--ink); }
.dlab .preset-note {
  margin: 0;
  color: var(--ink-2);
  font-size: 13.5px;
  min-height: 1.45em;
  max-width: 110ch;
}

/* ---------- bench layout ---------- */
.dlab .layout {
  display: grid;
  gap: 14px;
  grid-template-columns: 264px minmax(0, 1fr) 312px;
  grid-template-rows: auto 1fr;
  grid-template-areas:
    "ctrl plot metrics"
    "ctrl charts metrics";
  align-items: start;
}
.dlab .ctrl { grid-area: ctrl; }
.dlab .plot-wrap { grid-area: plot; }
.dlab .metrics { grid-area: metrics; }
.dlab .charts { grid-area: charts; }

@container dlab (max-width: 1099px) {
  .dlab .layout {
    grid-template-columns: 264px minmax(0, 1fr);
    grid-template-rows: auto 1fr auto;
    grid-template-areas:
      "ctrl plot"
      "ctrl charts"
      "metrics metrics";
  }
  .dlab .metrics { grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); }
}
@container dlab (max-width: 819px) {
  .dlab .layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: none;
    grid-template-areas: "plot" "metrics" "ctrl" "charts";
  }
}

.dlab .panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  min-width: 0;
}

/* ---------- controls ---------- */
.dlab .ctrl { display: flex; flex-direction: column; }
.dlab .ctrl section { padding: 14px 14px 16px; display: flex; flex-direction: column; gap: 10px; }
.dlab .ctrl section + section { border-top: 1px solid var(--line); }
.dlab .sec-head { display: flex; align-items: center; gap: 8px; justify-content: space-between; }
.dlab .sec-title { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 14px; }
.dlab .dot { display: inline-block; width: 11px; height: 11px; border-radius: 50%; flex: none; }
.dlab .dot.a { background: var(--set-a); }
.dlab .dot.b { background: var(--set-b); }
.dlab .set-a { --acc: var(--set-a); accent-color: var(--set-a); }
.dlab .set-b { --acc: var(--set-b); accent-color: var(--set-b); }
.dlab .settings { --acc: var(--control); accent-color: var(--control); }
.dlab input[type="range"] {
  -webkit-appearance: none; appearance: none; background: transparent; cursor: pointer;
  padding: 0; border: 0; box-shadow: none;
}
.dlab input[type="range"]:focus { box-shadow: none; }
.dlab input[type="range"]::-webkit-slider-runnable-track {
  height: 4px; border-radius: 2px;
  background: linear-gradient(to right, var(--acc, var(--control)) var(--fill, 50%), var(--line) var(--fill, 50%));
}
.dlab input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 14px; height: 14px; margin-top: -5px; border-radius: 50%;
  background: var(--acc, var(--control)); border: 2px solid var(--surface);
  box-shadow: 0 0 0 1px var(--acc, var(--control));
}
.dlab input[type="range"]::-moz-range-track { height: 4px; border-radius: 2px; background: var(--line); }
.dlab input[type="range"]::-moz-range-progress { height: 4px; border-radius: 2px; background: var(--acc, var(--control)); }
.dlab input[type="range"]::-moz-range-thumb {
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--acc, var(--control)); border: 2px solid var(--surface);
  box-shadow: 0 0 0 1px var(--acc, var(--control));
}
.dlab input[type="range"]:disabled { opacity: .45; cursor: default; }
.dlab .row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 76px;
  gap: 0 8px;
  align-items: center;
}
.dlab .row label { font-size: 12.5px; color: var(--ink-2); }
.dlab .row input[type="range"] { grid-column: 1 / -1; width: 100%; margin: 2px 0 0; height: 18px; }
.dlab .row input[type="number"], .dlab .row select, .dlab .row output {
  width: 76px;
  font: 12.5px/1.2 var(--font-mono);
  font-variant-numeric: tabular-nums;
  text-align: right;
  padding: 3px 6px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--ink);
}
.dlab .row output { border-color: transparent; background: transparent; padding-right: 0; }
.dlab .row select { text-align: left; }
.dlab .check { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--ink-2); cursor: pointer; }
.dlab .check input { margin: 0; width: auto; height: auto; accent-color: inherit; }
.dlab .toggles { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 10px; }
.dlab .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.dlab .btn {
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--ink);
  border-radius: 8px;
  padding: 7px 12px;
  font-size: 13px;
  font-weight: 500;
  line-height: 1.3;
  cursor: pointer;
}
.dlab .btn:hover { border-color: var(--control); background: var(--surface-2); }
.dlab .hint { font-size: 12px; color: var(--muted); margin: 0; }

/* ---------- plot ---------- */
.dlab .plot-wrap { overflow: hidden; }
.dlab .plot-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 6px 16px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
  color: var(--ink-2);
}
.dlab .legend { display: flex; flex-wrap: wrap; gap: 6px 16px; align-items: center; }
.dlab .legend span { display: inline-flex; align-items: center; gap: 6px; }
.dlab .mono { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
.dlab .coords { margin-left: auto; font-size: 12px; color: var(--muted); min-width: 15ch; text-align: right; }
.dlab #plot {
  display: block;
  width: 100%;
  height: clamp(320px, 64vh, 660px);
  touch-action: none;
  cursor: crosshair;
  background: var(--surface);
}

/* ---------- metric cards ---------- */
.dlab .metrics { display: grid; gap: 8px; }
.dlab .card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: none;
  padding: 11px 13px 12px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
  transition: border-color .15s;
}
.dlab .card:hover, .dlab .card.hl { border-color: var(--ink-2); }
.dlab .card header { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 8px; }
.dlab .card .sw { width: 14px; height: 3px; border-radius: 2px; flex: none; align-self: center; }
.dlab .card h3 { margin: 0; font-size: 13.5px; font-weight: 600; line-height: 1.3; letter-spacing: 0; color: var(--ink); }
.dlab .card .sub { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; flex: 1 1 auto; }
.dlab .card .ms { margin-left: auto; font: 11px var(--font-mono); color: var(--muted); white-space: nowrap; }
.dlab .card .val {
  font: 500 24px/1.15 var(--font-mono);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  transition: opacity .2s;
  display: flex;
  align-items: center;
  gap: 8px;
}
.dlab .card .val .unit { font-size: 12px; color: var(--muted); letter-spacing: 0; font-weight: 400; }
.dlab .card .detail { font: 11.5px/1.4 var(--font-mono); color: var(--ink-2); font-variant-numeric: tabular-nums; }
.dlab .card .what { margin: 2px 0 0; font-size: 12.5px; color: var(--ink-2); }
.dlab .card .formula { font: 11.5px/1.4 var(--font-mono); color: var(--muted); overflow-wrap: anywhere; }
.dlab .card.busy .val { opacity: .5; }
.dlab .pulse { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); opacity: 0; }
.dlab .card.busy .pulse { opacity: 1; animation: dlab-blink 1s ease-in-out infinite; }
@keyframes dlab-blink { 50% { opacity: .2; } }

/* ---------- diagnostic charts ---------- */
.dlab .charts { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(min(100%, 330px), 1fr)); }
.dlab figure.panel { margin: 0; padding: 12px 14px; display: flex; flex-direction: column; gap: 8px; position: relative; }
.dlab figure h2 {
  margin: 0; font-size: 14px; font-weight: 600; line-height: 1.35; letter-spacing: 0;
  display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap;
}
.dlab figure h2 .mono { font-weight: 400; color: var(--ink-2); font-size: 13px; }
.dlab figcaption { font-size: 12px; color: var(--muted); }
.dlab .chart { display: block; width: 100%; height: 240px; min-height: 0; }
.dlab .series-legend { display: flex; flex-wrap: wrap; gap: 4px; }
.dlab .series-legend button {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--line); background: var(--surface);
  border-radius: 6px; padding: 2px 8px; font-size: 12px; cursor: pointer; color: var(--ink-2);
}
.dlab .series-legend button[aria-pressed="false"] { opacity: .45; }
.dlab .series-legend i { width: 12px; height: 3px; border-radius: 2px; display: inline-block; }
.dlab .tip {
  position: absolute;
  pointer-events: none;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 9px;
  font-size: 12px;
  box-shadow: 0 6px 20px rgba(10, 20, 30, .14);
  min-width: 190px;
  z-index: 3;
}
.dlab .tip .t-head { font-weight: 600; margin-bottom: 4px; }
.dlab .tip .t-row { display: grid; grid-template-columns: 12px 1fr auto; gap: 6px; align-items: center; color: var(--ink-2); }
.dlab .tip .t-row i { width: 10px; height: 3px; border-radius: 2px; }
.dlab .tip .t-row b { font: 500 11.5px var(--font-mono); color: var(--ink); font-variant-numeric: tabular-nums; }
.dlab details.table-view summary { cursor: pointer; font-size: 12px; color: var(--ink-2); }
.dlab .table-scroll { overflow-x: auto; margin-top: 6px; }
.dlab table { border-collapse: collapse; font: 11.5px var(--font-mono); font-variant-numeric: tabular-nums; }
.dlab th, .dlab td { padding: 3px 8px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
.dlab th { font-family: var(--font-body); font-weight: 600; color: var(--ink-2); }

@media (prefers-reduced-motion: reduce) {
  .dlab * { transition: none !important; animation: none !important; }
}

/* ---------- page around the lab (hero art, context cards) ---------- */
.dlab-hero { width: 100%; height: auto; display: block; }
.dlab-hero .pa { fill: #6b9bff; }
.dlab-hero .pb { fill: #f39a45; }
.dlab-hero .ln { stroke: #dfe7fb; stroke-width: 1; opacity: .3; }
.dlab-hero .pill-t { fill: #fff; font-family: var(--font-serif); font-style: italic; font-size: 16px; }
.dlab-hero .lbl { fill: #9fb3e3; font-family: var(--font-sans); font-size: 11px; font-weight: 700; letter-spacing: .1em; }
.dlab-section { padding-top: 40px; }
.dlab-context .feature { height: 100%; }
.dlab-note { margin: 22px 0 0; }
