*,*::before,*::after{box-sizing:border-box}
:root{font-family:system-ui,-apple-system,Segoe UI,Roboto,Arial,sans-serif}
body{margin:0;background:#20a7d9;color:#0b1220;transition:background .15s ease}
body.running{background:#19b36a}
.wrap{max-width:100%;margin:0 auto;padding:16px}
h1{margin:0 0 16px 0;padding:8px 0;font-size:27px;color:#fff;font-weight:700;text-align:center}
.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}

/* 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}
.fillPos{position:absolute;left:50%;top:0;bottom:0;width:0;background:#0f6fff}
.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{
  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;
}
