/* Functional off-white panels. Palette reference:
   GOV.UK functional colors and USWDS blue-warm palette (blue-warm-60). */
:root {
  color-scheme: light;
  color: #262925;
  background: #555954;
  font: 14px/1.5 "Segoe UI", "Microsoft YaHei UI", "PingFang SC", "Noto Sans CJK SC", system-ui, sans-serif;
  --ink: #262925;
  --soft-ink: #5b605c;
  --panel: #f7f6f2;
  --panel-alt: #eeede8;
  --line: #d8d8d0;
  --accent: #345d96;
  --accent-dark: #2f4668;
  --accent-light: #e1e7f1;
  --dark: #14263a;
  --dark-line: #526679;
  --dark-text: #f0f6fa;
  --dark-muted: #bacbda;
  --top: 62px;
  --bottom: 61px;
  --margin: clamp(10px, 1.3vw, 22px);
  --side-gap: 14px;
  --side-top: calc(var(--top) + var(--side-gap));
  --side-bottom: calc(var(--bottom) + var(--side-gap));
  --bench-sidebar-width: clamp(208px, 15.6vw, 236px);
  --shadow: 0 2px 8px rgb(22 29 26 / 13%);
}

*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; overflow: hidden; }
[hidden] { display: none !important; }
#scene { position: fixed; inset: 0; }
#scene canvas { display: block; touch-action: none; }
#scene canvas.looking { cursor: grabbing; }
h1, h2, h3, p { margin: 0; }

.icon-library { position: absolute; width: 0; height: 0; overflow: hidden; }
.ui-icon {
  flex: none;
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
}

button, select {
  cursor: pointer;
  color: inherit;
  font: inherit;
  min-height: 32px;
  padding: 5px 10px;
  border: 1px solid var(--line);
  border-radius: 3px;
  background: transparent;
  white-space: nowrap;
}

button:hover, select:hover { color: var(--accent-dark); background: var(--accent-light); border-color: #aebcd0; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button:disabled { opacity: .52; cursor: default; }
select { background: var(--panel); padding-inline-end: 8px; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 4px;
  box-shadow: var(--shadow);
}

.hud-top, .hud-bottom {
  position: fixed;
  inset-inline: 0;
  border-radius: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 12px;
  padding-inline: var(--margin);
}

.hud-top {
  top: 0;
  z-index: 3;
  height: var(--top);
  isolation: isolate;
  overflow: clip;
  color: var(--dark-text);
  background-color: #123253;
  background-image: linear-gradient(100deg, #0e2e4f 6%, #15395f 52%, #0f2e4e 98%);
  border-bottom: 1px solid #5d7b95;
  box-shadow: 0 3px 10px #09182830;
}

.hud-top::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background-image: radial-gradient(ellipse 65% 240% at 6% -68%, #23658a75 0 66%, transparent 67%),
    radial-gradient(ellipse 50% 230% at 5% -96%, #256f9b85 0 69%, transparent 70%),
    radial-gradient(ellipse 46% 192% at 75% -54%, #1d638c62 0 56%, transparent 57%),
    radial-gradient(ellipse 30% 185% at 72% -68%, #28729b80 0 62%, transparent 63%);
  pointer-events: none;
}

.hud-bottom {
  bottom: 0;
  z-index: 3;
  height: var(--bottom);
  grid-template-columns: auto minmax(0, 1fr) auto;
  color: var(--dark-text);
  background: linear-gradient(102deg, #192d44, #112136 71%, #15273b);
  border-top: 1px solid #72899e;
  box-shadow: 0 -3px 10px #0a1b2c23;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  justify-self: start;
}

.brand-mark { display: block; width: 37px; height: auto; flex: 0 0 auto; filter: brightness(0) invert(1); }
.brand-text { display: flex; align-items: baseline; gap: 11px; min-width: 0; }
.brand strong { font-size: 18px; font-weight: 700; line-height: 1.35; white-space: nowrap; }
.brand small { border-inline-start: 1px solid #86a2b9; padding-inline-start: 11px; color: #d4e4f0; font-size: 13px; line-height: 1.3; white-space: nowrap; }

.experiment-title { display: flex; align-items: center; gap: 12px; white-space: nowrap; }
.experiment-title::before { content: ""; height: 24px; border-inline-start: 1px solid #7f9bb3; margin-inline-end: 4px; }
.experiment-title h1 { font-size: 20px; font-weight: 700; color: #f4f9fc; line-height: 1.35; }
.experiment-title span {
  padding: 4px 9px;
  border: 1px solid #4599b1;
  border-radius: 5px;
  color: #f0fbff;
  background: #226781;
  font-size: 11px;
  font-weight: 650;
  line-height: 1.3;
}

.header-actions, .footer-right, .camera-tools, .view-control, .walk-help, .keys {
  display: flex;
  align-items: center;
  gap: 6px;
}

.header-actions, .footer-right { justify-self: end; font-size: 13px; }
.header-actions { min-width: max-content; gap: 10px; }
.compact { display: none; }
.view-control { color: #e3eef7; gap: 10px; }
.view-label { display: flex; align-items: center; gap: 6px; }
.select-frame { position: relative; }
.select-frame::after {
  content: "";
  position: absolute;
  inset-inline-end: 11px;
  top: calc(50% - 4px);
  width: 7px;
  height: 7px;
  border-inline-end: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: rotate(45deg);
  pointer-events: none;
}
.hud-top :is(select, button) { color: var(--dark-text); min-height: 35px; }
.view-control select {
  appearance: none;
  padding: 5px 28px 5px 11px;
  border: 1px solid #7291ab;
  border-radius: 6px;
  background: #102d4a80;
}
.view-control option { background: #173553; color: #f0f6fa; }
.hud-top button { border-color: #67839b; }
.hud-top :is(button, select):hover { background: #2e5679; border-color: #9db3c6; }
.hud-top :is(button, select):focus-visible { outline-color: #c1e0ff; }
#help {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 0;
  border-inline-start: 1px solid #67839b;
  border-radius: 0;
  padding-inline: 12px 0;
}

.guide, .bench-sidebar {
  position: fixed;
  top: var(--side-top);
  z-index: 2;
  max-height: calc(100dvh - var(--side-top) - var(--side-bottom));
}

.guide {
  left: var(--margin);
  width: clamp(216px, 17.2vw, 268px);
  display: flex;
  flex-direction: column;
}

.guide-head {
  flex: 0 0 auto;
  padding: 11px 14px 10px;
  border-bottom: 1px solid var(--line);
}

.guide h2, .readings h2 { font-size: 14px; font-weight: 650; line-height: 1.4; }
#steps { min-height: 0; overflow: auto; scrollbar-width: thin; scrollbar-color: #acb0a8 transparent; padding-inline: 13px; }
#steps:focus-visible { outline-offset: -2px; }
.step {
  display: grid;
  grid-template-columns: 22px minmax(0, 1fr);
  align-items: start;
  gap: 9px;
  padding-block: 10px;
  min-width: 0;
}

.step + .step { border-top: 1px solid var(--line); }
.step i {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  width: 22px;
  min-height: 21px;
  border: 1px solid var(--line);
  border-radius: 2px;
  color: var(--soft-ink);
  background: var(--panel-alt);
  font: 600 11px/1 "Segoe UI", system-ui, sans-serif;
  font-variant-numeric: tabular-nums;
  font-style: normal;
}

.step-copy { min-width: 0; }
.step h3 { font-size: 13px; line-height: 21px; font-weight: 650; }
.step p {
  margin-block-start: 2px;
  color: var(--soft-ink);
  font-size: 12px;
  line-height: 1.55;
  white-space: normal;
  overflow-wrap: anywhere;
}

.bench-sidebar {
  right: var(--margin);
  display: flex;
  flex-direction: column;
  gap: var(--side-gap);
  width: var(--bench-sidebar-width);
  overflow: auto;
  scrollbar-width: thin;
  pointer-events: none;
}

.bench-sidebar > .panel { flex: none; pointer-events: auto; }
.readings {
  overflow: auto;
  padding: 12px 13px 11px;
}

.reading-head { display: flex; align-items: baseline; justify-content: space-between; gap: 5px; }
#runNo { font-size: 11px; color: var(--soft-ink); white-space: nowrap; }
.volume {
  display: flex;
  align-items: baseline;
  gap: 7px;
  margin-top: 7px;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

.volume b { font-size: clamp(30px, 2.65vw, 38px); font-weight: 600; line-height: 1.13; }
.volume span, .reading-sub { color: var(--soft-ink); font-size: 11px; line-height: 1.45; }
.reading-sub { margin-top: 3px; }
.readings dl { margin: 10px 0 0; padding-top: 6px; border-top: 1px solid var(--line); }
.readings dl div { display: flex; align-items: baseline; justify-content: space-between; gap: 5px; padding: 3px 0; font-size: 11px; }
.readings dt { color: var(--soft-ink); }
.readings dd { margin: 0; white-space: nowrap; font-variant-numeric: tabular-nums; }
#evaluation { display: none; margin-top: 7px; padding-top: 7px; border-top: 1px solid var(--line); font-size: 11px; }

.titration-controls {
  padding: 10px;
  color: var(--dark-text);
  background: var(--dark);
  border-color: var(--dark-line);
}

.titration-head { display: flex; align-items: baseline; justify-content: space-between; gap: 6px; }
.titration-head h2 { font-size: 14px; font-weight: 650; line-height: 1.35; }
#controlStatus { color: var(--dark-muted); font-size: 11px; white-space: nowrap; }
.dose-label { margin-block: 7px 4px; color: #dce8f2; font-size: 11px; }
.dose-label span { margin-inline-start: 3px; color: var(--dark-muted); }
.dose-choices { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 4px; }
.dose-choices button { min-width: 0; padding-inline: 2px; font-variant-numeric: tabular-nums; }
.titration-controls button {
  color: var(--dark-text);
  border-color: #60798f;
  background: #213950;
  font-size: 12px;
}

.titration-controls button:not(:disabled):hover { background: #2d4863; border-color: #a4bcd0; }
.titration-controls button:focus-visible { outline-color: #c1e0ff; outline-offset: 1px; }
.titration-controls #doseOptions button[aria-pressed="true"] {
  color: #10314d;
  background: #dceaf6;
  border-color: #92b9d8;
  font-weight: 700;
}

.titration-actions { display: grid; grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr); gap: 6px; margin-top: 7px; }
.titration-actions button { min-height: 36px; padding-inline: 5px; font-weight: 650; }
#shakeFlask {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  gap: 5px;
  touch-action: none;
  user-select: none;
  -webkit-touch-callout: none;
}

#shakeFlask kbd { width: auto; height: 18px; padding-inline: 4px; font-size: 10px; }
#shakeFlask[aria-pressed="true"], #shakeFlask:not(:disabled):active { color: #eef8ff; background: #315e82; border-color: #9cc4e2; }
#directDose { color: #fff; background: #067e75; border-color: #459d96; }
#directDose:not(:disabled):hover { background: #11695f; border-color: #8ed6c9; }

.scene-state {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  min-width: 0;
  justify-self: start;
  padding-inline-end: 15px;
  border-inline-end: 1px solid #546a80;
}
.location-mark {
  display: grid;
  place-items: center;
  width: 25px;
  height: 25px;
  flex: none;
  margin-top: 1px;
  border-radius: 50%;
  background: #28425b;
  color: #edf7fe;
}
.location-mark .ui-icon { width: 17px; height: 17px; }
.scene-caption { display: block; font-size: 12px; font-weight: 650; white-space: nowrap; }
.position {
  display: block;
  color: var(--dark-muted);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  line-height: 1.25;
  white-space: nowrap;
}

.camera-tools { justify-self: center; gap: clamp(7px, 1vw, 16px); }
.walk-help { color: #e1eaf2; gap: 8px; font-size: 12px; white-space: nowrap; }
.keys { gap: 3px; }
kbd {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 23px;
  height: 23px;
  border-radius: 4px;
  border: 1px solid #647b90;
  background: #2a4054;
  color: var(--dark-text);
  font: 600 11px/1 "Segoe UI", system-ui, sans-serif;
}

.mouse-keys { display: inline-flex; align-items: center; gap: 4px; margin-inline-start: 9px; }
.wheel-keys { color: #cfe4f2; }
.hud-bottom button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 35px;
  padding: 4px 10px;
  border: 1px solid #72889a;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 650;
}
.camera-tools button { padding-inline: 3px; border-color: transparent; }
.hud-bottom button:hover { background: #304b64; color: var(--dark-text); border-color: #98b2c7; }
.hud-bottom button:focus-visible { outline-color: #c1e0ff; }
.footer-right { gap: 8px; padding-inline-start: 14px; border-inline-start: 1px solid #546a80; }
#count { color: #d8e7f3; font-variant-numeric: tabular-nums; }
#restartExperiment { border-color: #849db4; background: #263c52; color: #fff; }
#restartExperiment:hover { border-color: #c2d5e4; background: #3b5974; }
#export { border-color: #459d96; background: #067e75; color: #fff; }
#export:hover { border-color: #8ed6c9; background: #11695f; }

#objectMenu {
  position: fixed;
  right: var(--margin);
  top: max(calc(var(--side-top) + 4px), 31dvh);
  z-index: 5;
  width: min(290px, calc(100vw - 2 * var(--margin)));
  max-height: calc(100dvh - max(calc(var(--side-top) + 4px), 31dvh) - var(--side-bottom));
  padding: 13px;
  overflow: auto;
}

body:has(#objectMenu:not([hidden])):not(:has(#titrationControls:not([hidden]))) .readings { visibility: hidden; }
body:has(#titrationControls:not([hidden])) #objectMenu {
  top: var(--side-top);
  right: calc(var(--margin) + var(--bench-sidebar-width) + var(--side-gap));
  max-height: calc(100dvh - var(--side-top) - var(--side-bottom));
}
#objectMenu.reading-console { width: min(272px, calc(100vw - 2 * var(--margin))); }
#objectMenu.measurement-console {
  top: var(--side-top);
  width: min(294px, calc(100vw - 2 * var(--margin)));
  max-height: calc(100dvh - var(--side-top) - var(--side-bottom));
}

.object-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding-bottom: 7px;
  border-bottom: 1px solid var(--line);
}

.object-head h2 { font-size: 15px; line-height: 1.4; font-weight: 650; }
.close {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  width: 29px;
  min-height: 29px;
  flex: none;
  padding: 0;
  font-size: 19px;
  line-height: 1;
}

#objectMenu p { margin: 8px 0; color: var(--soft-ink); font-size: 12px; line-height: 1.55; }
#contextControls label { display: block; margin: 10px 0 5px; font-size: 11px; color: var(--soft-ink); }
#contextControls .reading-height {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 12px;
  align-items: center;
  min-height: 38px;
  margin: 8px 0 4px;
  color: var(--ink);
  font-size: 13px;
}
.reading-height input {
  width: 100%;
  min-width: 0;
  min-height: 38px;
  margin: 0;
  accent-color: var(--accent);
  cursor: ew-resize;
  touch-action: pan-y;
}

.measurement-console #contextControls {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-areas: "source source" "tools tools" "status fine" "height height" "confirm confirm";
  align-items: center;
  gap: 5px 8px;
}
#contextControls .measurement-source {
  display: grid;
  grid-area: source;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  margin: 0;
  font-size: 12px;
}
.measurement-source select {
  min-height: 33px;
  width: 100%;
  padding: 4px 7px;
  font: inherit;
  border: 1px solid var(--line);
  border-radius: 4px;
  color: var(--ink);
  background: #fff;
}
.measurement-console .measurement-toolbar { grid-area: tools; }
.measurement-console .measurement-speed { grid-area: fine; }
.measurement-console .reading-height { grid-area: height; }
.measurement-console #contextControls .measurement-confirm { grid-area: confirm; }
.measurement-console #contextControls .measurement-extra { grid-column: 1 / -1; }
.measurement-toolbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) minmax(0, .74fr);
  gap: 5px;
}

.measurement-toolbar button,
.measurement-console [data-action="manual-fine"] {
  min-height: 36px;
  padding-inline: 3px;
  border-color: #a5b5c4;
  border-radius: 4px;
  background: #eef2f5;
  color: #23405a;
  font-size: 13px;
  font-weight: 600;
  touch-action: manipulation;
}

.measurement-speed { display: flex; align-items: center; justify-content: flex-end; gap: 6px; }
.measurement-console [data-action="manual-fine"] { min-height: 28px; padding: 3px 10px; font-size: 12px; }
.measurement-toolbar button[aria-pressed="true"],
.measurement-console [data-action="manual-fine"][aria-pressed="true"] {
  border-color: #24527b;
  background: #325f87;
  color: #fff;
}

.measurement-toolbar [data-action="manual-stop"]:not(:disabled) {
  border-color: #a17c61;
  background: #f2e5da;
  color: #563a29;
}

.measurement-toolbar button:not(:disabled):hover,
.measurement-console [data-action="manual-fine"]:not(:disabled):hover { border-color: var(--accent); }
.measurement-toolbar button:disabled:not([aria-pressed="true"]) { background: #f1f0eb; }
.measurement-toolbar button[aria-pressed="true"]:disabled { opacity: 1; }
#objectMenu p.measurement-status {
  grid-area: status;
  margin: 0;
  color: #405871;
  line-height: 1.4;
  overflow-wrap: anywhere;
}

.primary { color: #fff; background: var(--accent); border-color: var(--accent); font-weight: 600; }
.primary:hover { color: #fff; background: var(--accent-dark); border-color: var(--accent-dark); }
.wide { width: 100%; margin-top: 5px; white-space: normal; overflow-wrap: anywhere; }

#toast {
  position: fixed;
  z-index: 8;
  left: 50%;
  bottom: calc(var(--bottom) + 14px);
  transform: translateX(-50%);
  max-width: min(480px, 90vw);
  display: none;
  padding: 8px 12px;
  border: 1px solid var(--dark-line);
  border-radius: 4px;
  background: var(--dark);
  color: var(--dark-text);
  box-shadow: var(--shadow);
  font-size: 12px;
  pointer-events: none;
}

#loadError { position: fixed; z-index: 9; left: 50%; top: 37%; transform: translateX(-50%); width: min(360px, 86vw); padding: 18px; }

dialog {
  color: var(--ink);
  background: var(--panel);
  border: 1px solid #c3c5bc;
  border-radius: 4px;
  width: min(720px, calc(100vw - 2 * var(--margin)));
  max-height: 86dvh;
  padding: 22px;
  box-shadow: 0 8px 30px rgb(26 29 27 / 29%);
  overflow: auto;
}

dialog::backdrop { background: rgb(24 28 25 / 50%); }
dialog .close { position: absolute; top: 13px; right: 13px; }
dialog h2 { font-size: 19px; padding-right: 40px; margin-bottom: 11px; }
dialog h3 { font-size: 14px; margin: 12px 0 6px; }
dialog p { color: var(--soft-ink); line-height: 1.65; margin: 8px 0; }
dialog table { width: 100%; border-collapse: collapse; font-size: 12px; }
dialog :is(th, td) { padding: 7px 5px; text-align: left; border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
dialog th { background: var(--panel-alt); font-weight: 650; }
dialog summary { cursor: pointer; margin-top: 8px; }
dialog .log { background: var(--panel-alt); border: 1px solid var(--line); padding: 8px; margin-bottom: 7px; font-size: 12px; line-height: 1.6; max-height: 185px; overflow: auto; }

#characterSetup {
  width: min(618px, calc(100vw - 2 * var(--margin)));
  max-height: calc(100dvh - 24px);
  padding: 0;
  border: 1px solid #c9d3dc;
  border-radius: 12px;
  background: #f9fafb;
  color: #142b43;
  box-shadow: 0 18px 58px #06152773;
}

#characterSetup::backdrop { background: #091c32ab; backdrop-filter: blur(5px); }
.setup-layout { display: grid; grid-template-columns: 224px minmax(0, 1fr); min-width: 0; }
#characterPreview {
  height: 312px;
  overflow: hidden;
  align-self: stretch;
  background: radial-gradient(ellipse at 50% 37%, #e9f0f5 0, #d8e4ee 73%);
  border-inline-end: 1px solid #c6d5e3;
}
#characterPreview > canvas { display: block; width: 100%; height: 100%; }
#characterPreview[hidden] + .setup-content { grid-column: 1 / -1; }
.setup-content { min-width: 0; align-self: center; padding: 23px 28px; }
.setup-brand { display: flex; align-items: center; gap: 8px; color: #1a3e60; font-weight: 700; }
.setup-brand img { display: block; width: 29px; height: auto; }
#characterSetupTitle { margin: 10px 0 4px; padding: 0; font-size: 26px; line-height: 1.3; }
.setup-content p { margin: 0; color: #4d6074; }
.character-options { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; margin-top: 19px; }
.character-options button {
  min-height: 40px;
  border: 1px solid #b9c8d7;
  border-radius: 6px;
  background: #fff;
  color: #283e54;
  font-weight: 650;
}
.character-options button:hover { background: #e9f1f8; border-color: #64849e; }
.character-options button[aria-pressed="true"] { background: #e1edf7; border-color: #32628c; color: #143d62; }
#characterStatus { min-height: 26px; padding-block: 4px; font-size: 12px; }
#startExperiment {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  min-height: 43px;
  padding-inline: 13px;
  border: 1px solid #16466d;
  border-radius: 6px;
  color: #fff;
  background: #18486e;
  font-weight: 650;
}
#startExperiment:not(:disabled):hover { background: #103756; border-color: #103756; }
