:root {
  --bg: #0d1117;
  --panel: #151b23;
  --panel-2: #1c242e;
  --line: #2a3542;
  --text: #e6edf3;
  --muted: #8b98a5;
  --accent: #00e5ff;
  --primary: #2f81f7;
  --danger: #ff4d6d;
  --ok: #3fb950;
  --warn: #d29922;
  /* How much of the bottom sheet peeks above the fold on a phone. */
  --sheet-peek: 92px;
}

* { box-sizing: border-box; }

/*
 * min-height, not height. With `height: 100%` the html element's scrolling box
 * is clamped to the viewport while the body overflows past it, so any layout
 * that is taller than one screen simply cannot be scrolled. That silently hid
 * everything below the fold between 620px and 900px wide — which includes a
 * phone held in landscape.
 */
html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; flex-wrap: wrap; }
.brand strong { font-size: 16px; letter-spacing: .2px; }
.brand .sub { color: var(--muted); margin-left: 8px; font-size: 12px; }
.brand-org {
  margin-left: 10px;
  padding: 3px 8px;
  font-size: 11px;
  font-weight: 600;
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 999px;
  text-decoration: none;
  white-space: nowrap;
}
.brand-org:hover { background: color-mix(in srgb, var(--accent) 15%, transparent); }

.topbar-right { display: flex; align-items: center; gap: 10px; }
.brand-toggle {
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 999px;
  padding: 6px 11px;
  font: 600 11px/1 ui-monospace, Consolas, monospace;
  cursor: pointer;
}
.brand-toggle[aria-pressed="true"] { color: var(--accent); border-color: var(--accent); }

.brand-credit {
  padding: 6px 16px;
  font-size: 11.5px;
  color: var(--muted);
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

/*
 * Build tag. Exists because a deploy that silently doesn't reach the device is
 * indistinguishable from a deploy that changed nothing — which happened, via
 * GitHub Pages' max-age=600 on unversioned asset URLs. Now the running build is
 * always visible, and tapping it forces a genuinely fresh copy.
 */
.build-tag {
  position: fixed;
  right: 4px;
  bottom: 2px;
  z-index: 20;
  padding: 2px 6px;
  border-radius: 4px;
  font: 500 9px/1.4 ui-monospace, Consolas, monospace;
  color: var(--muted);
  background: rgba(5, 8, 12, .55);
  opacity: .5;
  cursor: pointer;
  pointer-events: auto;
  user-select: none;
}
.build-tag:hover, .build-tag:focus { opacity: 1; }

.badge {
  font: 600 12px/1 ui-monospace, Consolas, monospace;
  padding: 7px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  white-space: nowrap;
}
.badge-cal0 { color: var(--danger); border-color: var(--danger); }
.badge-cal2 { color: var(--warn); border-color: var(--warn); }
.badge-cal3 { color: var(--ok); border-color: var(--ok); }

main {
  display: grid;
  grid-template-columns: 1fr 380px;
  /* Row 1 takes the space; row 2 is the tray, only as tall as it needs. */
  grid-template-rows: 1fr auto;
  gap: 12px;
  padding: 12px;
  /* Sized against the viewport rather than a percentage of an ancestor, so it
     no longer needs html/body to carry a definite height. dvh also tracks
     mobile browser chrome as it hides and shows. */
  height: calc(100vh - var(--topbar-h, 53px));
  height: calc(100dvh - var(--topbar-h, 53px));
}
.stage-wrap { grid-column: 1; grid-row: 1; }
.tray { grid-column: 1; grid-row: 2; }
.panel { grid-column: 2; grid-row: 1 / span 2; }
@media (max-width: 900px) {
  /* One column: drop the explicit placement or the panel creates a phantom
     second column and the page scrolls sideways. */
  main { grid-template-columns: 1fr; grid-template-rows: none; height: auto; }
  .stage-wrap, .tray, .panel { grid-column: auto; grid-row: auto; }
}

/* ------------------------------------------------------------- stage */
.stage-wrap { display: flex; flex-direction: column; gap: 10px; min-width: 0; }

.stage {
  position: relative;
  flex: 1;
  min-height: 340px;
  background: #05080c;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}
.stage video,
.stage canvas {
  position: absolute;
  inset: 0;
  margin: auto;
  max-width: 100%;
  max-height: 100%;
  display: block;
}
.stage video { object-fit: contain; }
#overlayCanvas { cursor: crosshair; }
.stage video.hidden, .stage canvas.hidden { display: none; }

/*
 * Pose readout. This lives in the sheet, not on the viewfinder.
 *
 * On the view it was actively harmful: it asked the operator to read and
 * interpret four numbers while trying to hold a phone steady, when the bubble
 * conveys the same thing pre-attentively. Here it is a record you can check.
 *
 * The layout still pins the label column and uses tabular figures, because a
 * value whose left edge moves as digits change width drags the eye and makes
 * the static labels appear to flash too.
 */
.pose-readout {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 2px 14px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: rgba(0, 0, 0, .18);
  font: 600 11.5px/1.7 ui-monospace, Consolas, monospace;
}
.pose-row {
  display: grid;
  grid-template-columns: 4.6em 4.2em;
  column-gap: 8px;
  align-items: baseline;
}
.pose-k { color: var(--muted); }
.pose-v {
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
  /* No transition: an animated number is harder to read than a static one. */
}

.level-canvas {
  position: absolute;
  inset: 0;
  margin: auto;
  pointer-events: none;
}

.level-status {
  position: absolute;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  padding: 8px 16px;
  border-radius: 999px;
  font: 700 13px/1 ui-monospace, Consolas, monospace;
  white-space: nowrap;
  pointer-events: none;
  border: 1.5px solid;
  background: rgba(5, 8, 12, .8);
  color: var(--muted);
  border-color: var(--line);
  /* Colour may animate; the text must not move. */
  transition: color .2s ease, border-color .2s ease;
}
.level-status.is-ready { color: var(--ok); border-color: var(--ok); }
.level-status.is-close { color: var(--warn); border-color: var(--warn); }
.level-status.is-off   { color: var(--danger); border-color: var(--danger); }

.steady-box {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 9px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: rgba(0, 0, 0, .18);
}

/* Used where a control does something surprising, like handing the whole
   display to an immersive AR session. Surprise is the thing to defuse. */
.warn-box {
  padding: 10px 11px;
  border: 1px solid var(--warn);
  border-left-width: 3px;
  border-radius: 7px;
  background: rgba(210, 153, 34, .09);
  color: var(--text);
  font: 12px/1.65 -apple-system, system-ui, sans-serif;
  margin: 10px 0;
}


/* ------------------------------------------------- camera dock (shutter) */
/*
 * The controls that matter live ON the viewfinder, the way every camera app
 * works. Nothing essential should require scrolling a panel.
 */
/*
 * The tray is the app's home row. On a phone it is fixed to the bottom edge and
 * the sheet slides up from directly above it, so the shutter is reachable in
 * every state. On a desktop it is a plain centred row under the stage.
 */
.tray {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
}

.tray-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  min-width: 58px;
  padding: 6px 8px;
  border: 1px solid transparent;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font: 600 10.5px/1 -apple-system, system-ui, sans-serif;
}
.tray-ico { font-size: 19px; line-height: 1; }
.tray-lab { white-space: nowrap; }
.tray-btn:disabled { opacity: .35; cursor: not-allowed; }
.tray-btn.is-on { color: var(--accent); border-color: var(--accent); }
.tray-btn:active:not(:disabled) { background: rgba(255, 255, 255, .07); }

.shutter {
  position: relative;
  width: 68px;
  height: 68px;
  border-radius: 50%;
  border: 0;
  background: rgba(255, 255, 255, .92);
  box-shadow: 0 2px 12px rgba(0, 0, 0, .5);
  cursor: pointer;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  transition: transform .1s ease, background .2s ease;
}
.shutter:active { transform: scale(.93); }
.shutter:disabled { opacity: .45; cursor: not-allowed; }
.shutter-ring {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: 3px solid #0d1117;
  display: block;
}
.shutter.is-busy { background: var(--accent); }
.shutter.is-armed { background: var(--ok); }
.shutter-count {
  position: absolute;
  font: 700 20px/1 ui-monospace, Consolas, monospace;
  color: #0d1117;
}

/* ------------------------------------------------------- bottom sheet */
.sheet-grip { display: none; }
.panel-scroll { display: contents; }

.level-warn {
  position: absolute;
  left: 50%;
  top: 12px;
  transform: translateX(-50%);
  background: var(--danger);
  color: #fff;
  font: 700 12px/1 ui-monospace, monospace;
  padding: 8px 12px;
  border-radius: 6px;
  pointer-events: none;
}

.empty {
  position: relative;
  z-index: 1;
  color: var(--muted);
  text-align: left;
  max-width: 420px;
  padding: 20px;
}
.empty p { margin: 6px 0; }
.empty .muted { margin-top: 14px; font-size: 12px; opacity: .75; }

.overlay-tools {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}
.chip, .chip-label {
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 12px;
  cursor: pointer;
}
.chip-label { display: inline-flex; gap: 6px; align-items: center; color: var(--muted); cursor: default; }
.chip-label select { background: transparent; color: var(--text); border: 0; font-size: 12px; }
.chip.chip-on { border-color: var(--accent); color: var(--accent); }
.chip:disabled { opacity: .4; cursor: not-allowed; }
.sep { width: 1px; height: 20px; background: var(--line); margin: 0 4px; }

/* ------------------------------------------------------------- panel */
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow-y: auto;
  min-height: 0;
}
details { border-bottom: 1px solid var(--line); }
summary {
  padding: 11px 14px;
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
  user-select: none;
}
summary::marker { color: var(--muted); }
.pad { padding: 0 14px 14px; display: flex; flex-direction: column; gap: 9px; }

label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
label.check { flex-direction: row; align-items: center; gap: 8px; }
input, select {
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 6px;
  padding: 7px 9px;
  font-size: 13px;
  width: 100%;
}
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn {
  flex: 1;
  min-width: 120px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 7px;
  padding: 9px 10px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-ghost { background: transparent; }

.file-btn {
  display: block;
  text-align: center;
  background: var(--panel-2);
  border: 1px dashed var(--line);
  border-radius: 7px;
  padding: 11px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
}
.file-btn:hover { border-color: var(--accent); }

.seg { display: flex; border: 1px solid var(--line); border-radius: 7px; overflow: hidden; }
.seg-btn {
  flex: 1;
  background: var(--panel-2);
  border: 0;
  color: var(--muted);
  padding: 8px;
  font-size: 12px;
  cursor: pointer;
}
.seg-btn.seg-on { background: var(--primary); color: #fff; }

.hint { font-size: 11.5px; color: var(--muted); margin: 0; }
.hint code { background: var(--panel-2); padding: 1px 4px; border-radius: 3px; }
.warn {
  font-size: 11.5px;
  color: var(--warn);
  margin: 0;
  border-left: 2px solid var(--warn);
  padding-left: 8px;
}

.out {
  background: #05080c;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 9px;
  font: 11px/1.5 ui-monospace, Consolas, monospace;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 220px;
  overflow: auto;
  margin: 0;
}

.list { display: flex; flex-direction: column; gap: 5px; }
.item {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-left-width: 3px;
  border-radius: 6px;
  padding: 7px 9px;
  font: 11.5px/1.45 ui-monospace, Consolas, monospace;
}
.item.pass { border-left-color: var(--ok); }
.item.fail { border-left-color: var(--danger); }
.item .k { color: var(--muted); }

.status {
  text-align: center;
  font: 700 13px/1 ui-monospace, monospace;
  padding: 9px;
  border-radius: 6px;
  border: 1px solid;
}
.status-unverified { color: var(--muted); border-color: var(--line); }
.status-verified { color: var(--ok); border-color: var(--ok); }
.status-failed { color: var(--danger); border-color: var(--danger); }

.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
  background: var(--panel-2);
  border: 1px solid var(--accent);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 13px;
  z-index: 50;
  max-width: 90vw;
}

.hidden { display: none !important; }

/* ------------------------------------------------------------ mobile */
/*
 * Camera-first layout.
 *
 * Applies wherever the two-column desktop layout doesn't fit, plus any short
 * viewport: a phone in landscape is ~844x390, which is not narrow but is
 * emphatically a phone, and a camera app has no business turning into a
 * scrolling document because the device was rotated. Without the width arm,
 * a tablet-sized window left the shutter roughly 3000px down the page.
 */
@media (max-width: 900px), (max-height: 560px) {
  /* The tagline is nice on a laptop and wastes four lines on a phone. */
  .brand .sub { display: none; }
  .brand strong { font-size: 15px; }

  .topbar {
    padding: 8px 10px;
    gap: 8px;
    flex-wrap: nowrap;
  }
  .topbar-right { gap: 6px; min-width: 0; }

  /* Badge must shrink rather than push the page sideways. */
  .badge {
    font-size: 10.5px;
    padding: 6px 8px;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .brand-toggle { padding: 5px 8px; font-size: 10px; }
  .brand-org { margin-left: 6px; font-size: 10px; padding: 2px 6px; }

  /*
   * Camera-first layout. The viewfinder is the app; it stays put and fills the
   * screen. Below it a tray of the most-used controls is pinned to the bottom
   * edge and never moves. The settings sheet slides up from directly above the
   * tray rather than over it, so the shutter is reachable in every sheet state.
   */
  main {
    display: block;
    padding: 0;
    height: auto;
  }

  .stage-wrap {
    position: fixed;
    /* Measured at runtime — a hard-coded guess overlaps whenever the topbar
       wraps, the user has larger text, or the device has a notch. */
    top: var(--topbar-h, 46px);
    left: 0;
    right: 0;
    bottom: var(--tray-h, 74px);
    gap: 0;
    z-index: 1;
  }
  .stage {
    flex: 1;
    min-height: 0;
    border-radius: 0;
    border-left: 0;
    border-right: 0;
  }

  /* --- the tray: fixed, always visible, above everything --- */
  .tray {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    gap: 6px;
    justify-content: space-around;
    border-radius: 0;
    border-left: 0;
    border-right: 0;
    border-bottom: 0;
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom, 0px));
    box-shadow: 0 -2px 14px rgba(0, 0, 0, .5);
  }
  .shutter { width: 58px; height: 58px; }
  .shutter-ring { width: 47px; height: 47px; }
  .tray-btn { min-width: 52px; padding: 4px 2px; }

  .chip, .chip-label { padding: 7px 11px; font-size: 12px; }
  .sep { display: none; }

  /*
   * The sheet spans from just above the tray upward. `bottom` is the tray, and
   * the collapsed state is expressed as a translate of its own height minus the
   * peek, so the peek measurement stays in one place.
   */
  .panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: var(--tray-h, 74px);
    z-index: 10;
    height: calc(100vh - var(--topbar-h, 46px) - var(--tray-h, 74px) - 12px);
    max-height: 78vh;
    border-radius: 16px 16px 0 0;
    border-left: 0;
    border-right: 0;
    border-bottom: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    box-shadow: 0 -6px 24px rgba(0, 0, 0, .55);
    transform: translateY(calc(100% - var(--sheet-peek, 92px)));
    transition: transform .28s cubic-bezier(.32, .72, 0, 1);
    touch-action: pan-y;
  }
  .panel[data-snap="half"] { transform: translateY(42%); }
  .panel[data-snap="full"] { transform: translateY(0); }

  .sheet-grip {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    padding: 8px 12px 10px;
    cursor: grab;
    flex: 0 0 auto;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
  }
  .sheet-handle {
    width: 40px;
    height: 4px;
    border-radius: 999px;
    background: var(--muted);
    opacity: .55;
  }
  .sheet-title {
    font: 600 12.5px/1 -apple-system, system-ui, sans-serif;
    color: var(--muted);
  }

  .panel-scroll {
    display: block;
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

  /* Comfortable touch targets in the field, with gloves on. */
  .btn { padding: 11px 12px; font-size: 13.5px; min-width: 100px; }
  input, select { padding: 9px; font-size: 16px; } /* 16px stops iOS zoom-on-focus */
  summary { padding: 13px 14px; }

  .level-status { bottom: 14px; font-size: 12px; }
}

/* Nothing should ever scroll the page sideways on a phone. */
html, body { max-width: 100%; overflow-x: hidden; }

/* ------------------------------------------------ platform + camera */
.platform {
  font: 600 11.5px/1.4 ui-monospace, Consolas, monospace;
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid var(--line);
  color: var(--muted);
}
.platform-primary { color: var(--ok); border-color: var(--ok); }
.platform-reduced { color: var(--warn); border-color: var(--warn); }

.cam-controls {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 9px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: rgba(0, 0, 0, .18);
}
.cam-state {
  font: 11px/1.5 ui-monospace, Consolas, monospace;
  color: var(--muted);
  word-break: break-word;
}
input[type="range"] { padding: 0; accent-color: var(--accent); }

/* ---------------------------------------------------- upload / network */
.net-status {
  font: 600 11.5px/1.45 ui-monospace, Consolas, monospace;
  padding: 9px 10px;
  border-radius: 6px;
  border: 1px solid var(--line);
  color: var(--muted);
}
.net-status.net-ok { color: var(--ok); border-color: var(--ok); }
.net-status.net-hold { color: var(--warn); border-color: var(--warn); }
.net-status.net-off { color: var(--danger); border-color: var(--danger); }

.queue-box {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  background: rgba(0, 0, 0, .18);
}
.queue-row {
  display: flex;
  justify-content: space-between;
  font: 11.5px/1.7 ui-monospace, Consolas, monospace;
}
.queue-row span { color: var(--muted); }
