*,*::before,*::after{box-sizing:border-box}
:root{font-family:system-ui,-apple-system,Segoe UI,Roboto,Arial,sans-serif}
body{margin:0;background:#9ca3af;color:#0b1220;transition:background .3s ease}
body.bg-idle{background:#9ca3af}
body.bg-connected{background:#20a7d9}
body.running{background:#19b36a}
body.bg-error{background:#f87171}
.wrap{max-width:100%;margin:0 auto;padding:16px}
h1{margin:0;padding:8px 0;font-size:27px;color:#fff;font-weight:700;text-align:center;flex:1}

/* Build instructions link */
.build-instructions-link {
  color: rgba(255,255,255,0.55);
  font-size: 14px;
  font-weight: 650;
  text-decoration: underline;
  line-height: 1.3;
  white-space: nowrap;
  flex-shrink: 0;
  margin-left: 8px;
}
.build-instructions-link:hover { color: rgba(255,255,255,0.85); }
body.fullscreen-mode .build-instructions-link { display: none; }

/* Reset all settings link */
.reset-all-row {
  text-align: center;
  margin-top: 18px;
  margin-bottom: 10px;
}
.reset-all-link {
  color: rgba(255,255,255,0.6);
  font-size: 15px;
  font-weight: 650;
  text-decoration: underline;
  cursor: pointer;
}
.reset-all-link:hover { color: #fff; }
body.fullscreen-mode .reset-all-row { display: none; }

/* ========================================
   NOT CONNECTED STATE — grey out everything except Connect Board
   ======================================== */

body.not-connected .activateBtn,
body.not-connected .meters,
body.not-connected .calibrateLink,
body.not-connected .calibrateLink + .small,
body.not-connected .topRow,
body.not-connected .settingsTitle,
body.not-connected .settings,
body.not-connected .calibToolBtn,
body.not-connected .calibToolBtn + .small,
body.not-connected .status,
body.not-connected .layout-bottom {
  opacity: 0.25;
  pointer-events: none;
  user-select: none;
}

/* Header row with fullscreen button */
.header-row{display:flex;align-items:center;margin-bottom:16px}
.card{background:rgba(255,255,255,.92);border-radius:14px;padding:14px;box-shadow:0 10px 28px rgba(0,0,0,.12);overflow:hidden;min-width:0}

/* Top row: video + controls centered on screen, same height */
.layout-top{display:flex;justify-content:center;gap:14px;align-items:stretch}

/* Video card: shrink-wrap tightly around the canvas */
.videoCard{flex-shrink:0;width:fit-content;position:relative}

/* Settings-group is a pure passthrough on desktop — no visual change.
   On mobile it becomes a real card (see mobile @media block). */
.settings-group { display: contents; }

/* Flip-camera button: hidden on desktop, shown on mobile (top-right of video).
   The default camera is the front (selfie) cam — this lets users swap to rear. */
.flip-camera-btn {
  display: none;
}
@media (max-width: 820px) {
  .flip-camera-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    top: 22px;
    right: 22px;
    width: 44px;
    height: 44px;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.55);
    color: #fff;
    cursor: pointer;
    z-index: 5;
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
    transition: transform 0.18s ease, background 0.18s ease;
  }
  .flip-camera-btn:active {
    transform: scale(0.92);
    background: rgba(0, 0, 0, 0.75);
  }
  .flip-camera-btn.flipping svg {
    animation: flip-cam-spin 0.5s cubic-bezier(0.4, 0, 0.2, 1);
  }
  @keyframes flip-cam-spin {
    from { transform: rotate(0deg);   }
    to   { transform: rotate(180deg); }
  }
}

/* Control card: 15% larger than 380 = 437 */
.controlCard{width:437px;flex-shrink:0;display:flex}
.controls{display:flex;flex-direction:column;gap:10px;width:100%}
button{background:#0f6fff;border:0;color:#fff;padding:10px;border-radius:10px;font-weight:700;cursor:pointer;position:relative;font-size:14px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
button:disabled{opacity:.55;cursor:not-allowed}
button.stopBtn{background:#e43d3d}

/* Activate Robot button: big, green, prominent — same font as Connect Board, just larger */
.activateBtn{background:#1db954;font-size:20px;padding:18px 10px;border-radius:14px;font-weight:700}
.activateBtn:disabled{background:#1db954}
.activateBtn.stopBtn{background:#e43d3d;font-size:20px;padding:18px 10px}

/* Continuous jiggle until clicked */
@keyframes jiggle{
  0%{transform:rotate(0)}
  15%{transform:rotate(-1.5deg)}
  30%{transform:rotate(1.5deg)}
  45%{transform:rotate(-1deg)}
  60%{transform:rotate(1deg)}
  75%{transform:rotate(-0.5deg)}
  100%{transform:rotate(0)}
}
button.jiggle{animation:jiggle .6s ease-in-out infinite}

/* Calibrate neutral: underlined greyed-out text link */
.calibrateLink{
  display:inline-block;
  color:#7a8aa3;
  font-size:13px;
  font-weight:650;
  text-decoration:underline;
  cursor:pointer;
  background:none;
  border:none;
  padding:2px 0;
}
.calibrateLink:hover{color:#4a5a73}
.calibrateLink .tip{vertical-align:middle}

.status{font-size:13px;font-weight:700}
.small{font-size:12px;color:#8a96a8;line-height:1.3;margin-top:-4px}

/* Video canvas: proper aspect ratio, never stretched */
canvas{width:100%;height:auto;border-radius:14px;display:block}

.meters{display:grid;gap:8px;margin-top:4px}
.meter{display:grid;grid-template-columns:auto 1fr 30px;gap:6px;align-items:center;min-width:0}
.meterLabel{font-size:13px;font-weight:750;color:#1b2a44;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.meterBar{position:relative;height:14px;border-radius:999px;background:#dbe6f7;overflow:hidden;min-width:40px}
.centerTick{position:absolute;left:50%;top:0;bottom:0;width:2px;background:rgba(27,42,68,.35)}
.fillNeg{position:absolute;right:50%;top:0;bottom:0;width:0;background:#0f6fff;transition:width .35s cubic-bezier(0.25,0.1,0.25,1)}
.fillPos{position:absolute;left:50%;top:0;bottom:0;width:0;background:#0f6fff;transition:width .35s cubic-bezier(0.25,0.1,0.25,1)}
.meterVal{font-size:13px;font-weight:800;color:#1b2a44;text-align:right}

/* Bottom row: 3 settings cards, centered */
.layout-bottom{display:grid;grid-template-columns:1fr 1fr 1fr;gap:14px;margin-top:14px;max-width:1200px;margin-left:auto;margin-right:auto}

.settingsTitle{margin:0 0 8px 0;padding:0;font-size:14px;font-weight:800;color:#1b2a44}
.settings{display:grid;gap:8px}
.row{display:grid;grid-template-columns:110px 1fr;gap:6px;align-items:center;min-width:0}
.row.hasVal{grid-template-columns:110px 1fr 30px}
.row label{font-size:12px;font-weight:650;color:#1b2a44}
.row input[type="range"]{width:100%;min-width:0}
.row input[type="number"]{width:100%;padding:5px;border-radius:8px;border:1px solid rgba(27,42,68,.2);font-size:13px}
.row select{width:100%;padding:6px;border-radius:8px;border:1px solid rgba(27,42,68,.2);background:#fff;font-weight:650;color:#1b2a44;font-size:13px}
.row input[type="checkbox"]{width:auto;justify-self:start;margin:0}
.sliderVal{font-size:12px;font-weight:700;color:#1b2a44;text-align:right;min-width:24px}
.tip{display:inline-block;width:15px;height:15px;margin-left:3px;border-radius:999px;background:#b8c0cc;vertical-align:middle;position:relative;cursor:help;flex-shrink:0}
.tip::before{content:"?";display:block;width:100%;height:100%;text-align:center;line-height:15px;font-size:9px;font-weight:900;color:#fff}
.tip:hover::after,
.tip.tip-open::after{
  content:attr(data-tip);
  position:absolute;
  left:0;
  top:calc(100% + 6px);
  z-index:10;
  width:220px;
  background:#0b1220;
  color:#fff;
  padding:8px 10px;
  border-radius:10px;
  font-size:11px;
  font-weight:650;
  box-shadow:0 10px 24px rgba(0,0,0,.25);
}
.meterLabel .tip:hover::after{left:0}

/* Top controls row for servo mode */
.topRow{display:grid;grid-template-columns:110px 1fr;gap:6px;align-items:center;min-width:0}
.topRow label{font-size:13px;font-weight:650;color:#1b2a44}
.topRow select{width:100%;padding:6px;border-radius:8px;border:1px solid rgba(27,42,68,.2);background:#fff;font-weight:650;color:#1b2a44;font-size:13px}

/* Servo Calibration Tool button */
.calibToolBtn{
  background:#f97316;
  color:#fff;
  font-size:14px;
  font-weight:700;
  padding:10px;
  border-radius:10px;
  border:none;
  cursor:pointer;
  transition:transform 0.1s, background 0.15s;
}
.calibToolBtn:hover{background:#ea580c;transform:scale(1.02)}
.calibToolBtn:active{transform:scale(0.98)}

/* ========================================
   SERVO CALIBRATION MODAL
   ======================================== */

.hidden { display: none !important; }

.calib-modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  z-index: 1000;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.calib-modal {
  background: #fff;
  border-radius: 18px;
  padding: 28px 32px;
  width: 75vw;
  max-width: 75vw;
  height: 75vh;
  max-height: 75vh;
  overflow-y: auto;
  position: relative;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
  text-align: center;
}

.calib-modal-close {
  position: absolute;
  top: 12px;
  right: 16px;
  background: none;
  border: none;
  font-size: 28px;
  color: #888;
  cursor: pointer;
  padding: 4px 8px;
  line-height: 1;
}
.calib-modal-close:hover { color: #333; }

.calib-modal-title {
  font-size: 1.4rem;
  color: #111;
  margin: 0 0 2px 0;
}

.calib-subtitle {
  color: #888;
  margin-bottom: 16px;
  font-size: 0.9rem;
}

.calib-step { margin-bottom: 12px; }

/* Calibration buttons */
.calib-btn {
  border: none;
  border-radius: 10px;
  padding: 12px 32px;
  font-size: 1.05rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s, opacity 0.15s;
}
.calib-btn:hover { transform: scale(1.03); }
.calib-btn:active { transform: scale(0.97); }
.calib-btn:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }

.calib-btn-connect { background: #2563eb; color: #fff; }
.calib-btn-start { background: #16a34a; color: #fff; }
.calib-btn-confirm { background: #16a34a; color: #fff; }
.calib-btn-danger { background: #ef4444; color: #fff; }
.calib-btn-apply {
  background: #1db954;
  color: #fff;
  font-size: 1.4rem;
  padding: 20px 50px;
  border-radius: 14px;
  font-weight: 700;
}
.calib-btn-apply:hover { background: #16a34a; }
.calib-btn-apply.jiggle { animation: calib-jiggle .6s ease-in-out infinite; }

@keyframes calib-jiggle {
  0%   { transform: rotate(0); }
  15%  { transform: rotate(-1.5deg); }
  30%  { transform: rotate(1.5deg); }
  45%  { transform: rotate(-1deg); }
  60%  { transform: rotate(1deg); }
  75%  { transform: rotate(-0.5deg); }
  100% { transform: rotate(0); }
}

.calib-btn-row {
  display: flex;
  gap: 12px;
  align-items: center;
}

.calib-status {
  margin-top: 12px;
  color: #888;
  font-size: 0.9rem;
}

/* Servo header */
.calib-servo-header { margin-bottom: 8px; }
.calib-servo-header h3 {
  font-size: 1.3rem;
  color: #111;
  margin: 0 0 2px 0;
}
.calib-instruction {
  color: #666;
  font-size: 0.95rem;
  margin-bottom: 8px;
}

/* Phase sections */
.calib-phase { margin-top: 8px; }

/* Moving indicator */
.calib-moving-indicator {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 24px;
}
.calib-moving-indicator p {
  color: #16a34a;
  font-weight: 600;
  font-size: 1.1rem;
}
.calib-pulse-ring {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 4px solid #16a34a;
  animation: calib-pulse 1s ease-in-out infinite;
}
@keyframes calib-pulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.3); opacity: 0.4; }
}

/* Compass layout — wheel left, controls right */
.calib-compass-layout {
  display: flex;
  align-items: center;
  gap: 40px;
  justify-content: center;
}

/* Ring sized to fit inside the modal */
.calib-chopstick-ring {
  --ring-size: min(42vh, 350px);
  width: var(--ring-size);
  height: var(--ring-size);
  flex-shrink: 0;
  border-radius: 50%;
  border: 2px solid #ddd;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle, #f8f8f8 0%, #f0f0f0 100%);
}

/* Tick marks */
.calib-chopstick-ring::before {
  content: "";
  position: absolute;
  top: 8px;
  width: 3px;
  height: 20px;
  background: #ccc;
  border-radius: 1px;
}
.calib-chopstick-ring::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 8px;
  width: 20px;
  height: 3px;
  background: #ddd;
  border-radius: 1px;
  transform: translateY(-50%);
}

.calib-chopstick-graphic {
  position: absolute;
  width: 14px;
  height: calc(var(--ring-size, 400px) * 0.65);
  display: flex;
  flex-direction: column;
  align-items: center;
  transform-origin: center center;
  transition: transform 0.08s ease-out;
}

.calib-chopstick-stick {
  width: 14px;
  flex: 1;
  background: linear-gradient(to bottom, #d4a056, #c4883c, #b8722e);
  border-radius: 6px 6px 2px 2px;
  box-shadow: 1px 0 3px rgba(0,0,0,0.15);
}

.calib-chopstick-tip {
  width: 9px;
  height: 30px;
  flex-shrink: 0;
  background: linear-gradient(to bottom, #b8722e, #8b5a2b);
  border-radius: 0 0 4px 4px;
  margin-top: -1px;
}

/* Controls panel to the right of the wheel */
.calib-compass-controls {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 16px;
  text-align: left;
  max-width: 300px;
}

.calib-compass-controls .calib-ask-label {
  font-size: 1.1rem;
  color: #222;
  margin-bottom: 0;
}

/* Rotation slider */
.calib-rotation-slider {
  width: 260px;
  height: 6px;
  -webkit-appearance: none;
  appearance: none;
  background: #ddd;
  border-radius: 3px;
  outline: none;
}
.calib-rotation-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #f97316;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(249, 115, 22, 0.4);
}

/* Back button */
.calib-btn-back {
  background: none;
  border: none;
  color: #ef4444;
  font-size: 0.95rem;
  cursor: pointer;
  padding: 4px 0;
}
.calib-btn-back:hover { color: #dc2626; }

/* Done message */
.calib-done-msg {
  font-size: 1.05rem;
  color: #16a34a;
  margin-bottom: 16px;
}

/* Results */
.calib-results-grid {
  display: flex;
  flex-direction: row;
  gap: 20px;
  margin-bottom: 24px;
  justify-content: center;
}

.calib-result-card {
  background: #f5f5f5;
  border: 1px solid #e0e0e0;
  border-radius: 16px;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 10px;
  width: 190px;
}

.calib-result-card h4 {
  font-size: 0.95rem;
  font-weight: 600;
  color: #444;
  margin: 0;
}

.calib-trim-value {
  font-size: 3rem;
  font-weight: 800;
  color: #111;
  line-height: 1;
  margin: 0;
}

.calib-btn-test {
  background: #16a34a;
  color: #fff;
  padding: 10px 24px;
  font-size: 0.95rem;
  font-weight: 600;
  border-radius: 8px;
  display: block;
  margin: 0 auto;
}

.calib-finetune-link {
  display: block;
  text-align: center;
  color: #7a8aa3;
  font-size: 12px;
  font-weight: 650;
  text-decoration: underline;
  cursor: pointer;
  margin-top: 4px;
}
.calib-finetune-link:hover { color: #4a5a73; }

.calib-btn-recalibrate {
  background: #ef4444;
  color: #fff;
  font-size: 0.95rem;
  padding: 10px 28px;
  border-radius: 10px;
}

/* ========================================
   FULLSCREEN BUTTON
   ======================================== */

.fullscreen-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(255,255,255,0.18);
  border: 1px solid rgba(255,255,255,0.3);
  color: #fff;
  padding: 6px 14px 6px 10px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 650;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s;
  flex-shrink: 0;
}
.fullscreen-btn:hover { background: rgba(255,255,255,0.28); }
.fullscreen-btn svg { flex-shrink: 0; }

/* ========================================
   RESET TRIMS BUTTON
   ======================================== */

.reset-trims-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  color: #b8c0cc;
  padding: 2px;
  margin-left: 4px;
  cursor: pointer;
  vertical-align: middle;
  border-radius: 4px;
  transition: color 0.15s;
  width: 20px;
  height: 20px;
}
.reset-trims-btn:hover { color: #e43d3d; }
.reset-trims-btn svg { display: block; }

/* ========================================
   FULLSCREEN MODE
   ======================================== */

body.fullscreen-mode {
  overflow: hidden;
}
body.fullscreen-mode.bg-idle {
  background: #9ca3af !important;
}
body.fullscreen-mode.bg-connected {
  background: #20a7d9 !important;
}
body.fullscreen-mode.running {
  background: #19b36a !important;
}
body.fullscreen-mode.bg-error {
  background: #f87171 !important;
}

body.fullscreen-mode .wrap {
  display: flex;
  flex-direction: column;
  height: 100vh;
  padding: 8px 16px;
  max-width: 100%;
}

body.fullscreen-mode .header-row {
  margin-bottom: 4px;
  justify-content: center;
  position: relative;
}
body.fullscreen-mode .header-row h1 {
  font-size: 20px;
  text-align: center;
}
body.fullscreen-mode .fullscreen-btn {
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
}

/* Hide the normal layout in fullscreen */
body.fullscreen-mode .layout-top,
body.fullscreen-mode .layout-bottom {
  display: none !important;
}

/* Show the fullscreen layout */
.fs-layout { display: none; }
body.fullscreen-mode .fs-layout {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
  min-height: 0;
  gap: 6px;
}

/* ---- Head meter (horizontal, above video) ---- */
.fs-head-meter {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  width: 500px;
  max-width: 80vw;
  flex-shrink: 0;
}

.fs-head-bar-row {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
}

.fs-label {
  font-size: 13px;
  font-weight: 750;
  color: #fff;
  white-space: nowrap;
}

.fs-label-head {
  font-size: 18px;
  font-weight: 800;
  text-align: center;
}

.fs-label-arm {
  font-size: 18px;
  font-weight: 800;
  text-align: center;
}

.fs-val {
  font-size: 13px;
  font-weight: 800;
  color: #fff;
  text-align: right;
  min-width: 26px;
}

.fs-val-arm {
  font-size: 16px;
}

.fs-bar-h {
  position: relative;
  flex: 1;
  height: 14px;
  border-radius: 999px;
  background: rgba(255,255,255,0.2);
  overflow: hidden;
}
.fs-tick-h {
  position: absolute;
  left: 50%;
  top: 0; bottom: 0;
  width: 2px;
  background: rgba(255,255,255,0.35);
}
.fs-fill-h-neg {
  position: absolute;
  right: 50%;
  top: 0; bottom: 0;
  width: 0;
  background: #fff;
  border-radius: 999px 0 0 999px;
  transition: width .35s cubic-bezier(0.25,0.1,0.25,1);
}
.fs-fill-h-pos {
  position: absolute;
  left: 50%;
  top: 0; bottom: 0;
  width: 0;
  background: #fff;
  border-radius: 0 999px 999px 0;
  transition: width .35s cubic-bezier(0.25,0.1,0.25,1);
}

/* ---- Center row: left arm | video | right arm ---- */
.fs-center {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
  min-height: 0;
  justify-content: center;
}

/* Arm meters (vertical) */
.fs-arm-meter {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
  height: 100%;
  max-height: 400px;
}

.fs-bar-v {
  position: relative;
  width: 14px;
  flex: 1;
  min-height: 60px;
  border-radius: 999px;
  background: rgba(255,255,255,0.2);
  overflow: hidden;
}
.fs-tick-v {
  position: absolute;
  top: 50%;
  left: 0; right: 0;
  height: 2px;
  background: rgba(255,255,255,0.35);
}
.fs-fill-v-neg {
  position: absolute;
  bottom: 50%;
  left: 0; right: 0;
  height: 0;
  background: #fff;
  border-radius: 999px 999px 0 0;
  transition: height .35s cubic-bezier(0.25,0.1,0.25,1);
}
.fs-fill-v-pos {
  position: absolute;
  top: 50%;
  left: 0; right: 0;
  height: 0;
  background: #fff;
  border-radius: 0 0 999px 999px;
  transition: height .35s cubic-bezier(0.25,0.1,0.25,1);
}

/* Video slot */
.fs-video-slot {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.fs-video-slot canvas {
  max-height: calc(100vh - 240px);
  width: auto;
  border-radius: 10px;
}

/* ---- Activate button top-left in fullscreen ---- */
.fs-activate-btn {
  display: none;
}
body.fullscreen-mode .fs-activate-btn {
  display: block;
  position: fixed;
  top: 12px;
  left: 16px;
  z-index: 100;
  font-size: 15px;
  padding: 12px 20px;
  border-radius: 12px;
}

/* Fullscreen guide */
.fullscreen-guide { display: none; }
body.fullscreen-mode .fullscreen-guide {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 60px;
  padding: 6px 0 4px 0;
  flex-shrink: 0;
}
.guide-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.guide-item-row {
  flex-direction: row;
  align-items: center;
  gap: 14px;
}
.guide-label-big {
  font-family: 'Baloo 2', cursive, sans-serif;
  color: #fff;
  font-size: 34px;
  font-weight: 800;
  line-height: 1.1;
  white-space: nowrap;
  letter-spacing: 0.5px;
}

/* ========================================
   DISCONNECT POPUP
   ======================================== */

.disconnect-popup-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 2000;
  display: flex;
  justify-content: center;
  align-items: center;
}

.disconnect-popup {
  background: #fff;
  border-radius: 16px;
  padding: 28px 36px;
  text-align: center;
  box-shadow: 0 16px 48px rgba(0,0,0,0.25);
}

.disconnect-msg {
  font-size: 1.2rem;
  font-weight: 700;
  color: #e43d3d;
  margin: 0 0 16px 0;
}

.disconnect-reconnect-btn {
  background: #0f6fff;
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 10px 28px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
}
.disconnect-reconnect-btn:hover { background: #0b5cd9; }

/* ========================================
   iOS UNSUPPORTED WARNING POPUP
   ======================================== */
.ios-warning-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  z-index: 3000;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}
.ios-warning {
  background: #fff;
  border-radius: 16px;
  padding: 26px 30px;
  max-width: 420px;
  width: 100%;
  text-align: center;
  box-shadow: 0 16px 48px rgba(0,0,0,0.25);
}
.ios-warning-title {
  font-size: 1.35rem;
  font-weight: 800;
  color: #e43d3d;
  margin: 0 0 10px 0;
}
.ios-warning-msg {
  font-size: 1rem;
  font-weight: 700;
  color: #1b2a44;
  margin: 0 0 8px 0;
}
.ios-warning-sub {
  font-size: 0.9rem;
  color: #555;
  margin: 0 0 18px 0;
}
.ios-warning-close-btn {
  background: #0f6fff;
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 10px 32px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
}
.ios-warning-close-btn:hover { background: #0b5cd9; }

/* ========================================
   MODEL LOADING OVERLAY (only shown on Activate-before-ready)
   Animated progress ring + smoothly-eased percentage counter.
   ======================================== */
.ml-overlay {
  position: fixed;
  inset: 0;
  z-index: 2500;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  background:
    radial-gradient(circle at 30% 20%, rgba(59,130,246,0.35), transparent 55%),
    radial-gradient(circle at 75% 80%, rgba(29,185,84,0.30), transparent 55%),
    rgba(11, 18, 32, 0.55);
  backdrop-filter: blur(14px) saturate(160%);
  -webkit-backdrop-filter: blur(14px) saturate(160%);
  animation: ml-overlay-in 0.35s cubic-bezier(0.16, 1, 0.3, 1);
}
.ml-overlay.ml-fadeout {
  animation: ml-overlay-out 0.32s ease forwards;
}
@keyframes ml-overlay-in  { from { opacity: 0; } to { opacity: 1; } }
@keyframes ml-overlay-out { to   { opacity: 0; transform: scale(1.01); } }

.ml-card {
  background: rgba(255,255,255,0.96);
  border-radius: 24px;
  padding: 34px 38px 30px 38px;
  width: min(420px, 92vw);
  text-align: center;
  box-shadow:
    0 30px 80px rgba(0,0,0,0.35),
    0 0 0 1px rgba(255,255,255,0.6) inset;
  animation: ml-card-in 0.5s cubic-bezier(0.16, 1, 0.3, 1);
}
@keyframes ml-card-in {
  from { transform: translateY(16px) scale(0.95); opacity: 0; }
  to   { transform: translateY(0)    scale(1);    opacity: 1; }
}

/* Progress ring */
.ml-ring-wrap {
  position: relative;
  width: 156px;
  height: 156px;
  margin: 0 auto 18px auto;
}
.ml-ring {
  width: 100%;
  height: 100%;
  /* Rotate so the arc starts from 12 o'clock */
  transform: rotate(-90deg);
  overflow: visible;
}
.ml-ring-track {
  fill: none;
  stroke: #e8eef9;
  stroke-width: 7;
}
.ml-ring-progress {
  fill: none;
  stroke-width: 7;
  stroke-linecap: round;
  /* Smooth glide between progress updates */
  transition: stroke-dashoffset 0.45s cubic-bezier(0.4, 0, 0.2, 1);
  filter: drop-shadow(0 0 6px rgba(59,130,246,0.45));
  animation: ml-ring-glow 2.4s ease-in-out infinite;
}
@keyframes ml-ring-glow {
  0%, 100% { filter: drop-shadow(0 0 4px  rgba(59,130,246,0.40)); }
  50%      { filter: drop-shadow(0 0 12px rgba(29,185,84,0.55)); }
}

/* Robot emoji bobbing in the center of the ring */
.ml-ring-icon {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 58px;
  animation: ml-bob 2.6s ease-in-out infinite;
  user-select: none;
  pointer-events: none;
  filter: drop-shadow(0 4px 8px rgba(0,0,0,0.15));
}
@keyframes ml-bob {
  0%, 100% { transform: translateY(0)    rotate(-4deg); }
  50%      { transform: translateY(-7px) rotate(4deg);  }
}

/* Percentage readout */
.ml-percent {
  font-size: 46px;
  font-weight: 800;
  color: #1b2a44;
  line-height: 1;
  margin: 4px 0 4px 0;
  font-variant-numeric: tabular-nums;
  letter-spacing: -1.5px;
}
.ml-percent-sym {
  font-size: 24px;
  color: #6b7a90;
  margin-left: 3px;
  font-weight: 700;
  letter-spacing: 0;
}

.ml-title {
  font-size: 17px;
  font-weight: 800;
  color: #1b2a44;
  margin: 8px 0 4px 0;
  letter-spacing: 0.2px;
}
.ml-stage {
  font-size: 13px;
  color: #6b7a90;
  font-weight: 600;
  min-height: 18px;
  font-variant-numeric: tabular-nums;
  transition: opacity 0.18s ease;
}

/* Three bouncing dots under the stage line */
.ml-dots {
  display: flex;
  justify-content: center;
  gap: 6px;
  margin-top: 10px;
}
.ml-dots span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: linear-gradient(135deg, #3b82f6, #1db954);
  animation: ml-dot 1.2s ease-in-out infinite;
}
.ml-dots span:nth-child(2) { animation-delay: 0.18s; }
.ml-dots span:nth-child(3) { animation-delay: 0.36s; }
@keyframes ml-dot {
  0%, 80%, 100% { transform: translateY(0)    scale(0.85); opacity: 0.55; }
  40%           { transform: translateY(-5px) scale(1);    opacity: 1;    }
}

/* ========================================
   MOBILE LAYOUT
   Immediate view = video + meter sliders.
   Everything else (buttons, mode, calibration, settings cards) lives below, reached by scrolling.
   ======================================== */
@media (max-width: 820px) {
  .wrap { padding: 10px; }
  .header-row {
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 10px;
  }
  h1 {
    font-size: 18px;
    padding: 4px 0;
    order: 1;
    flex-basis: 100%;
    text-align: center;
  }
  .build-instructions-link {
    order: 2;
    margin-left: 0;
    font-size: 12px;
  }
  .fullscreen-btn {
    order: 3;
    margin-left: auto;
  }

  /* Flatten the control card hierarchy so we can interleave buttons +
     video + meters in a single column layout. */
  .layout-top {
    flex-direction: column;
    gap: 8px;
    align-items: stretch;
  }
  .controlCard { display: contents; }
  .controls    { display: contents; }

  /* Order on mobile: Connect → Activate → Video → Meters → Settings card */
  #connectBtn                  { order: 1; }
  .controls > .activateBtn     { order: 2; }
  .videoCard                   { order: 3; width: 100%; padding: 8px;
                                  display: flex; justify-content: center; }
  .controls > .meters          { order: 4; }
  .settings-group              { order: 5; }

  /* The meters get their own white card backdrop now that they're floating */
  .controls > .meters {
    display: grid;
    gap: 8px;
    background: rgba(255,255,255,0.95);
    border-radius: 14px;
    padding: 12px 14px;
    box-shadow: 0 10px 28px rgba(0,0,0,.12);
    margin: 0;
  }
  .meter { grid-template-columns: minmax(130px, auto) 1fr 32px; }
  .meterLabel { font-size: 13px; }

  /* Settings group becomes a real card on mobile */
  .settings-group {
    display: flex;
    flex-direction: column;
    gap: 10px;
    background: rgba(255,255,255,0.95);
    border-radius: 14px;
    padding: 14px;
    box-shadow: 0 10px 28px rgba(0,0,0,.12);
  }

  /* Make the video noticeably smaller so it doesn't dominate the screen */
  .videoCard canvas {
    max-height: 40vh;
    width: auto !important;
    max-width: 100%;
    height: auto;
    margin: 0 auto;
    display: block;
  }

  /* Settings cards stack in a single column */
  .layout-bottom {
    grid-template-columns: 1fr;
    gap: 10px;
    margin-top: 12px;
  }

  /* Slightly tighter rows on phones */
  .row { grid-template-columns: 110px 1fr; }
  .row.hasVal { grid-template-columns: 110px 1fr 30px; }
  .topRow { grid-template-columns: 110px 1fr; }

  /* Bigger touch target for the tooltip ?-buttons */
  .tip { width: 20px; height: 20px; }
  .tip::before { line-height: 20px; font-size: 11px; }
  /* Tooltip popup is wider/easier to read on phones */
  .tip:hover::after,
  .tip.tip-open::after { width: 240px; font-size: 12px; }

  /* Fullscreen IS available on mobile — see the fullscreen-mode mobile
     overrides further down for sizing. */

  /* Bigger / friendlier sliders for fingertips */
  .row input[type="range"] { height: 28px; }
  .row input[type="number"] { font-size: 14px; padding: 7px; }
  .row select { font-size: 14px; padding: 8px; }
  .topRow select { font-size: 14px; padding: 8px; }

  /* Tap-friendly main buttons */
  .activateBtn { font-size: 22px; padding: 20px 10px; }
  #connectBtn { padding: 14px 10px; font-size: 15px; }

  /* Calibration modal: full-screen-ish on mobile */
  .calib-modal { width: 95vw; max-width: 95vw; height: 90vh; max-height: 90vh; padding: 18px; }
}

/* Prevent accidental browser-level double-tap zoom on controls — keeps
   tap → click latency near zero and feels more app-like. */
@media (max-width: 820px) {
  body { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
}

/* ========================================
   FULLSCREEN MODE — mobile overrides
   Mobile fullscreen restructures the layout so the Activate button is
   a normal element at the top (not floating/fixed, which collided with
   the head meter). Arm meters are tightened so labels don't clip off
   the viewport. Video gets a reasonable cap.
   ======================================== */
@media (max-width: 820px) {
  body.fullscreen-mode .wrap { padding: 4px 6px; }
  body.fullscreen-mode .header-row { margin-bottom: 0; min-height: 30px; }
  body.fullscreen-mode .header-row h1 { display: none; } /* save vertical space */
  body.fullscreen-mode .fullscreen-btn {
    padding: 6px 10px; font-size: 12px;
    position: absolute !important;
    top: 4px; right: 6px;
    transform: none;
  }

  body.fullscreen-mode .fs-layout { gap: 4px; }

  /* Activate button — in normal flow at the top, NOT fixed */
  body.fullscreen-mode .fs-activate-btn {
    display: inline-block !important;
    position: static !important;
    order: -1;
    align-self: center;
    width: auto;
    margin: 2px auto 4px auto !important;
    padding: 8px 22px !important;
    font-size: 14px !important;
    border-radius: 12px !important;
  }

  /* Head meter — full width, compact */
  body.fullscreen-mode .fs-head-meter {
    width: 100%;
    max-width: 100%;
    padding: 0 6px;
    box-sizing: border-box;
    gap: 1px;
  }
  body.fullscreen-mode .fs-head-bar-row { gap: 6px; }
  body.fullscreen-mode .fs-label-head { font-size: 13px; }
  body.fullscreen-mode .fs-bar-h { height: 10px; }
  body.fullscreen-mode .fs-val { font-size: 11px; min-width: 20px; }

  /* Center row — pad and constrain so arm labels never clip */
  body.fullscreen-mode .fs-center {
    gap: 6px;
    padding: 0 6px;
    width: 100%;
    box-sizing: border-box;
  }
  body.fullscreen-mode .fs-arm-meter {
    max-height: none;
    gap: 2px;
    flex-shrink: 1;
    min-width: 0;
  }
  body.fullscreen-mode .fs-bar-v { width: 8px; min-height: 40px; }
  body.fullscreen-mode .fs-label-arm {
    font-size: 11px;
    white-space: nowrap;
    writing-mode: horizontal-tb;
  }
  body.fullscreen-mode .fs-val-arm { font-size: 11px; }

  /* Video — keep it well within the viewport, leave room for arm meters and guide */
  body.fullscreen-mode .fs-video-slot {
    flex-shrink: 1;
    min-width: 0;
  }
  body.fullscreen-mode .fs-video-slot canvas {
    max-height: calc(100vh - 230px);
    max-width: calc(100vw - 90px);
    width: auto;
    height: auto;
  }

  /* Motion guide — compact so it doesn't overflow */
  body.fullscreen-mode .fullscreen-guide {
    gap: 14px;
    padding: 2px 4px 2px 4px;
    flex-wrap: wrap;
    justify-content: center;
  }
  body.fullscreen-mode .guide-label-big {
    font-size: 14px;
    white-space: normal;
    line-height: 1.05;
  }
  body.fullscreen-mode .guide-item-row { gap: 4px; }
  body.fullscreen-mode .guide-item-row svg { width: 70px; height: 54px; }
}

/* Even tighter on very narrow phones (≤480px) */
@media (max-width: 480px) {
  body.fullscreen-mode .fs-bar-v { width: 7px; }
  body.fullscreen-mode .fs-label-arm { font-size: 10px; }
  body.fullscreen-mode .fs-val-arm { font-size: 10px; }
  body.fullscreen-mode .guide-label-big { font-size: 12px; }
  body.fullscreen-mode .guide-item-row svg { width: 56px; height: 44px; }
  body.fullscreen-mode .fs-video-slot canvas {
    max-width: calc(100vw - 70px);
    max-height: calc(100vh - 215px);
  }
  body.fullscreen-mode .fs-activate-btn {
    font-size: 13px !important;
    padding: 7px 18px !important;
  }
}
