:root {
  --bg: #0d1117;
  --text: #c9d1d9;
  --blue: #58a6ff;
  --green: #3fb950;
  --orange: #ffa657;
  --red: #f85149;
  --purple: #d2a8ff;
  --link: #79c0ff;
  --mono: 'JetBrains Mono', ui-monospace, monospace;
  --sans: Inter, system-ui, -apple-system, sans-serif;
  --glass: rgba(22, 27, 34, 0.55);
  --line: rgba(201, 209, 217, 0.12);
  --pad: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --sheet-h: 190px; /* measured by main.js */
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

#stage, #stage canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

.glass {
  background: var(--glass);
  border: 1px solid var(--line);
  border-radius: 16px;
  backdrop-filter: blur(14px) saturate(140%);
  -webkit-backdrop-filter: blur(14px) saturate(140%);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.35);
}

button { font-family: inherit; }
.hud { position: fixed; z-index: 2; transition: opacity 0.8s ease; }
body.loading .hud, body.loading #chips { opacity: 0; pointer-events: none; }

/* ---------- Phone first: title and tiles on top, sentence and sheet at the bottom ---------- */

.hud-left { top: calc(var(--pad) + env(safe-area-inset-top, 0px)); left: var(--pad); right: var(--pad); pointer-events: none; }
.hud-left > * { pointer-events: auto; }
.eyebrow {
  margin: 0 0 4px;
  font: 500 9.5px/1 var(--mono);
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--orange);
}
h1 { margin: 0; line-height: 0.95; text-transform: uppercase; }
h1 span { font-weight: 800; font-size: 15px; letter-spacing: 0.02em; color: #f0f6fc; }
h1 strong {
  font-weight: 900;
  font-size: 27px;
  letter-spacing: -0.01em;
  background: linear-gradient(90deg, var(--link), var(--blue) 45%, var(--purple));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.hook { display: none; margin: 12px 0 16px; font-size: 13.5px; line-height: 1.5; color: #b7c0ca; }

.tiles { display: flex; gap: 6px; margin-top: 8px; }
.tile {
  flex: 1;
  min-width: 0;
  padding: 7px 8px 6px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(13, 17, 23, 0.55);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
.tile-label { display: block; font: 500 8.5px/1 var(--mono); letter-spacing: 0.1em; text-transform: uppercase; color: #8b949e; }
.tile-label small { font-size: inherit; letter-spacing: 0.04em; text-transform: none; color: #6e7681; }
/* Two values per tile: the engine in orange, the motor in blue. */
.tile-pair { display: flex; justify-content: space-between; gap: 6px; margin-top: 5px; font: 700 16px/1 var(--mono); font-variant-numeric: tabular-nums; white-space: nowrap; }
.tile-pair .e { color: var(--orange); }
.tile-pair .m { color: var(--blue); }

.sentence {
  position: fixed;
  left: var(--pad);
  right: var(--pad);
  bottom: calc(var(--sheet-h) + var(--safe-b) + 18px);
  margin: 0;
  padding: 10px 12px 10px 28px;
  max-height: 78px;
  overflow: hidden;
  font-size: 12.5px;
  line-height: 1.45;
  color: #d7dde4;
}
.sentence::before {
  content: '';
  position: absolute;
  left: 12px;
  top: 16px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 10px var(--green);
  animation: pulse 1.6s ease-in-out infinite;
}
@keyframes pulse { 50% { opacity: 0.35; } }

/* Bottom sheet within thumb reach. Every target is at least 44 px. */
.console {
  left: var(--pad);
  right: var(--pad);
  bottom: calc(10px + var(--safe-b));
  padding: 8px 10px 9px;
  border-radius: 20px;
}
.row-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin: 4px 4px 0;
  font: 500 10px/1 var(--mono);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: #8b949e;
}
.row-head .value { color: var(--orange); letter-spacing: 0.04em; font-size: 12px; font-weight: 700; font-variant-numeric: tabular-nums; }
.segmented {
  display: flex;
  gap: 2px;
  padding: 3px;
  border-radius: 12px;
  background: rgba(1, 4, 9, 0.45);
  border: 1px solid var(--line);
}
.segmented button {
  flex: 1;
  min-height: 44px;
  padding: 0 2px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: #b7c0ca;
  font: 500 12px/1 var(--sans);
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}
.segmented button.active { background: #e6edf3; color: #0d1117; font-weight: 600; }

input[type=range] {
  -webkit-appearance: none;
  appearance: none;
  display: block;
  width: 100%;
  height: 44px;
  margin: 0;
  background: transparent;
  cursor: pointer;
  --fill: 0%;
}
input[type=range]::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: 2px;
  background: linear-gradient(90deg, var(--blue), var(--orange) var(--fill), rgba(201, 209, 217, 0.15) var(--fill));
}
input[type=range]::-moz-range-track { height: 4px; border-radius: 2px; background: rgba(201, 209, 217, 0.15); }
input[type=range]::-moz-range-progress { height: 4px; border-radius: 2px; background: linear-gradient(90deg, var(--blue), var(--orange)); }
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 24px;
  height: 24px;
  margin-top: -10px;
  border-radius: 50%;
  background: #f0f6fc;
  box-shadow: 0 0 0 5px rgba(255, 166, 87, 0.25), 0 2px 8px rgba(0, 0, 0, 0.5);
}
input[type=range]::-moz-range-thumb { width: 24px; height: 24px; border: 0; border-radius: 50%; background: #f0f6fc; }

/* Torque against speed, both machines, with the current speed marked. */
#torque-chart { display: block; width: 100%; height: 46px; margin: 0 0 6px; }

.row-foot { display: flex; gap: 8px; align-items: center; }
.row-foot .round { margin-left: auto; }
.pill {
  flex: 0 0 auto;
  min-width: 64px;
  height: 44px;
  padding: 0 14px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: rgba(1, 4, 9, 0.45);
  color: #e6edf3;
  font: 600 12px/1 var(--mono);
  letter-spacing: 0.02em;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}
.pill[aria-pressed="true"] { background: #e6edf3; color: #0d1117; border-color: #e6edf3; }
.round {
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: rgba(1, 4, 9, 0.45);
  color: #e6edf3;
  font: 600 15px/1 var(--sans);
  cursor: pointer;
}

/* Spec card: on a phone it covers the sheet; the close button brings the sheet back. */
.card {
  position: fixed;
  z-index: 3;
  left: var(--pad);
  right: var(--pad);
  bottom: calc(10px + var(--safe-b));
  min-height: var(--sheet-h);
  padding: 16px 18px 14px;
  background: rgba(13, 17, 23, 0.86);
  transform: translateY(calc(100% + 40px));
  transition: transform 0.4s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.card.open { transform: none; }
.card .close { position: absolute; top: 8px; right: 8px; }
.card-kicker { margin: 0 0 4px; font: 500 9.5px/1 var(--mono); letter-spacing: 0.16em; text-transform: uppercase; color: var(--card-accent, var(--orange)); }
.card h2 { margin: 0 52px 10px 0; font-size: 19px; font-weight: 800; color: #f0f6fc; }
.card dl { display: grid; grid-template-columns: auto 1fr; gap: 5px 14px; margin: 0; font: 400 12px/1.3 var(--mono); }
.card dt { color: #8b949e; }
.card dd { margin: 0; color: #e6edf3; font-variant-numeric: tabular-nums; text-align: right; }
.card-note { margin: 10px 0 0; font-size: 12.5px; line-height: 1.45; color: #b7c0ca; }

/* Essay */
.essay {
  position: fixed;
  z-index: 4;
  top: 10px;
  right: 10px;
  bottom: 10px;
  width: calc(100vw - 20px);
  padding: 18px 20px;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: rgba(13, 17, 23, 0.9);
  transform: translateX(calc(100% + 40px));
  transition: transform 0.45s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.essay.open { transform: none; }
.essay .close { position: sticky; top: 0; float: right; }
.essay h2 { margin: 6px 0 6px; font-size: 22px; font-weight: 800; color: #f0f6fc; }
.essay h3 { margin: 20px 0 6px; font: 500 11px/1 var(--mono); letter-spacing: 0.14em; text-transform: uppercase; color: var(--orange); }
.essay p { margin: 0 0 8px; font-size: 13.5px; line-height: 1.62; color: #b7c0ca; }
.essay b { color: var(--link); font-family: var(--mono); font-weight: 500; }

/* "← Lab" and "Follow for more" (lab/lib/links.js): always visible. On a phone they sit above the title. */
#lab-links { top: calc(8px + env(safe-area-inset-top, 0px)); left: var(--pad); right: var(--pad); justify-content: space-between; }
.hud-left { top: calc(58px + env(safe-area-inset-top, 0px)); }
#perf { display: none; font: 400 11px/1 var(--mono); color: #6e7681; }

/* Labels pinned to 3D points (at most three) */
#chips { position: fixed; inset: 0; pointer-events: none; z-index: 1; transition: opacity 0.6s; }
body.card-open #chips { opacity: 0; }
.chip {
  position: absolute;
  left: 0;
  top: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 9px 4px 7px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: rgba(13, 17, 23, 0.6);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  font: 600 10.5px/1 var(--sans);
  color: #e6edf3;
  white-space: nowrap;
  transition: opacity 0.4s;
  will-change: transform;
}
.chip::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: var(--dot, var(--blue)); box-shadow: 0 0 8px var(--dot, var(--blue)); }
.chip small { font: 500 9.5px/1 var(--mono); color: #8b949e; }

/* Tooltip on the objects: hover on desktop, tap on a phone */
#object-tip {
  position: fixed;
  z-index: 5;
  left: 0;
  top: 0;
  max-width: min(280px, calc(100vw - 24px));
  pointer-events: none;
  opacity: 0;
  padding: 8px 11px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(13, 17, 23, 0.9);
  color: #e6edf3;
  font: 500 11px/1.4 var(--mono);
  transition: opacity 0.15s;
}
#object-tip.visible { opacity: 1; }
#object-tip b { color: #f0f6fc; font-weight: 700; }
#object-tip small { display: block; margin-top: 2px; color: #8b949e; font-size: 10px; }

/* ---------- Loader: the drawing hands off to the rendered assembly without a blank frame ---------- */
#stage canvas { opacity: 0; transition: opacity 0.6s ease; }
body.scene-ready #stage canvas { opacity: 1; }

/* ---------- Desktop and landscape tablets ---------- */
@media (min-width: 760px) and (min-height: 520px) {
  :root { --pad: 22px; }
  .hud-left { left: 26px; right: auto; width: 380px; }
  .eyebrow { font-size: 11px; margin-bottom: 6px; }
  h1 span { display: block; font-size: 22px; }
  h1 strong { display: block; font-size: 44px; }
  .hook { display: block; }
  .tiles { gap: 8px; margin-top: 0; }
  .tile { padding: 9px 11px 8px; }
  .tile-label { font-size: 9.5px; letter-spacing: 0.14em; }
  .tile-pair { font-size: 19px; margin-top: 7px; }
  .sentence { position: relative; left: auto; right: auto; bottom: auto; margin-top: 10px; max-height: none; min-height: 64px; padding: 12px 14px 12px 30px; font-size: 13px; }
  .sentence::before { left: 13px; top: 18px; }
  .console { top: 22px; right: 22px; left: auto; bottom: auto; width: 330px; padding: 12px 13px; border-radius: 16px; }
  .segmented button { min-height: 36px; font-size: 12.5px; }
  .segmented button:hover { color: #f0f6fc; background: rgba(201, 209, 217, 0.06); }
  .segmented button.active:hover { color: #0d1117; background: #e6edf3; }
  .row-head { margin-top: 12px; }
  input[type=range] { height: 32px; }
  #torque-chart { height: 64px; margin-top: 4px; }
  .round { width: 36px; height: 36px; }
  .pill { height: 36px; }
  .pill:hover { background: #e6edf3; color: #0d1117; }
  .round:hover { background: #e6edf3; color: #0d1117; }
  .card { left: auto; right: 22px; bottom: auto; top: 300px; width: 330px; min-height: 0; transform: translateX(calc(100% + 40px)); }
  .card.open { transform: none; }
  .essay { top: 22px; right: 22px; bottom: 22px; width: 430px; padding: 22px 26px; }
  .hud-left { top: 22px; }
  #lab-links { top: auto; right: auto; left: 26px; bottom: 16px; }
  #perf { display: block; right: 22px; bottom: 26px; }
}
