:root {
  --bg: #0e1116;
  --panel: #151a21;
  --panel-2: #1b222b;
  --line: #262f3a;
  --text: #e6ebf2;
  --muted: #8a96a6;
  --accent: #3ddcb4;
  --accent-ink: #062a22;
  --warn: #f5b84a;
  --err: #ff6b6b;
  --raw: #5b6b80;
  --smooth: #3ddcb4;
  --cal: #f5b84a;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden; }
body { display: flex; }
button, select, input { font: inherit; color: inherit; }
kbd { font: 600 11px/1 ui-monospace, Consolas, monospace; padding: 2px 5px; border-radius: 4px; border: 1px solid var(--line); background: var(--panel-2); color: var(--muted); }

/* ---------- 3D viewport ---------- */
#viewport {
  position: relative; flex: 1; min-width: 0;
  background:
    radial-gradient(ellipse 70% 60% at 50% 42%, #2a3442 0%, #161c25 55%, #0b0e13 100%);
}
#viewport canvas { position: absolute; inset: 0; z-index: 0; width: 100%; height: 100%; display: block; outline: none; }
.overlay-msg { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: 15px; pointer-events: none; }
.overlay-msg.error { color: var(--err); padding: 32px; text-align: center; pointer-events: auto; }
#hint { position: absolute; left: 50%; bottom: 14px; transform: translateX(-50%); font-size: 12px; color: #6d7887; pointer-events: none; white-space: nowrap; }

#grip-hud { position: absolute; left: 24px; top: 20px; pointer-events: none; }
.hud-label { font-size: 11px; letter-spacing: .14em; text-transform: uppercase; color: var(--muted); }
.hud-value { font-size: 44px; font-weight: 300; line-height: 1.05; font-variant-numeric: tabular-nums; }
.hud-value small { font-size: 18px; color: var(--muted); margin-left: 2px; }

/* ---------- calibration overlay ---------- */
#calib { position: absolute; inset: 0; z-index: 5; display: grid; place-items: center; background: rgba(8, 10, 14, .55); backdrop-filter: blur(3px); }
#calib[hidden] { display: none; }
.calib-card { width: min(380px, calc(100% - 32px)); background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 26px 26px 20px; text-align: center; box-shadow: 0 20px 60px rgba(0,0,0,.45); }
.calib-step { font-size: 11px; letter-spacing: .14em; text-transform: uppercase; color: var(--accent); }
.calib-card h2 { margin: 8px 0 6px; font-size: 24px; font-weight: 600; }
.calib-card p { margin: 0 0 18px; color: var(--muted); font-size: 14px; line-height: 1.45; min-height: 40px; }
.calib-ring { position: relative; width: 120px; height: 120px; margin: 0 auto 18px; }
.calib-ring svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.calib-ring circle { fill: none; stroke-width: 8; }
.calib-ring .track { stroke: var(--line); }
.calib-ring .bar { stroke: var(--accent); stroke-linecap: round; stroke-dasharray: 326.7; stroke-dashoffset: 326.7; transition: stroke-dashoffset .05s linear; }
.calib-ring.clench .bar { stroke: var(--warn); }
#calib-count { position: absolute; inset: 0; display: grid; place-items: center; font-size: 38px; font-weight: 300; font-variant-numeric: tabular-nums; }
.calib-actions { display: flex; gap: 8px; justify-content: center; }

/* ---------- side panel ---------- */
#panel { width: 340px; flex: none; background: var(--panel); border-left: 1px solid var(--line); overflow-y: auto; }
#panel header { position: sticky; top: 0; z-index: 2; background: var(--panel); padding: 16px 18px 14px; border-bottom: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.brand { font-weight: 650; font-size: 16px; display: flex; align-items: center; gap: 9px; }
.logo { width: 18px; height: 18px; border-radius: 6px; background: linear-gradient(135deg, var(--accent), #2b8cff); }
.status { display: flex; align-items: center; gap: 7px; font-size: 12px; color: var(--muted); min-width: 0; }
.status #status-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #4a5462; flex: none; }
.status.connected .dot { background: var(--accent); box-shadow: 0 0 0 3px rgba(61,220,180,.18); }
.status.connecting .dot, .status.nodata .dot { background: var(--warn); }
.status.error .dot { background: var(--err); }
.status.demo .dot { background: #8b7bff; }

section { padding: 16px 18px; border-bottom: 1px solid var(--line); }
section h3 { margin: 0 0 12px; font-size: 11px; letter-spacing: .14em; text-transform: uppercase; color: var(--muted); font-weight: 600; }
.row { display: flex; gap: 8px; margin-top: 10px; }
.grow { flex: 1; min-width: 0; }
.full { width: 100%; margin-top: 12px; }
.field { display: flex; flex-direction: column; gap: 5px; font-size: 12px; color: var(--muted); }
.field + .field, section > .field { margin-top: 10px; }
section > .field:first-of-type { margin-top: 0; }
select { background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-size: 13px; width: 100%; }
select:focus, button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

button { border: 1px solid var(--line); border-radius: 8px; padding: 9px 14px; font-size: 13px; font-weight: 600; cursor: pointer; background: var(--panel-2); }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
button.primary:hover:not(:disabled) { filter: brightness(1.08); }
button.secondary:hover:not(:disabled), button.ghost:hover:not(:disabled) { background: #222b36; }
button.ghost { background: transparent; }

.message { margin: 10px 0 0; font-size: 12.5px; line-height: 1.45; color: var(--muted); min-height: 0; }
.message:empty { display: none; }
.message.error { color: var(--err); }
.message.warn { color: var(--warn); }
.toggle { display: flex; gap: 8px; align-items: flex-start; margin-top: 12px; font-size: 12.5px; color: var(--muted); cursor: pointer; line-height: 1.5; }
.toggle input { margin-top: 3px; accent-color: var(--accent); }

#chart { width: 100%; height: 120px; display: block; border-radius: 8px; background: #0f141a; border: 1px solid var(--line); }
.legend { display: flex; gap: 14px; margin-top: 7px; font-size: 11px; color: var(--muted); }
.legend .sw { display: inline-block; width: 10px; height: 3px; border-radius: 2px; margin-right: 5px; vertical-align: middle; }
.sw.raw { background: var(--raw); } .sw.smooth { background: var(--smooth); } .sw.cal { background: var(--cal); }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 12px; }
section > .stats:first-of-type { margin-top: 0; }
.stats div { background: var(--panel-2); border-radius: 8px; padding: 8px 10px; }
.stats small { display: block; font-size: 10.5px; color: var(--muted); }
.stats b { font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; }
.gripbar { height: 6px; background: var(--panel-2); border-radius: 3px; margin-top: 12px; overflow: hidden; }
#gripbar-fill { height: 100%; width: 0; background: linear-gradient(90deg, var(--accent), #2b8cff); border-radius: 3px; }

.slider { display: grid; grid-template-columns: 1fr auto; gap: 4px 8px; font-size: 12px; color: var(--muted); margin-top: 10px; }
.slider:first-of-type { margin-top: 0; }
.slider output { color: var(--text); font-variant-numeric: tabular-nums; }
.slider input { grid-column: 1 / -1; width: 100%; accent-color: var(--accent); }

.swatches { display: flex; gap: 8px; margin-top: 12px; }
.swatches button { width: 30px; height: 30px; padding: 0; border-radius: 50%; border: 2px solid transparent; box-shadow: inset 0 0 0 1px rgba(0,0,0,.25); }
.swatches button[aria-pressed="true"] { border-color: var(--text); }

@media (max-width: 760px) {
  body { flex-direction: column; overflow: auto; height: auto; }
  #viewport { flex: none; height: 62vh; }
  #panel { width: 100%; border-left: 0; border-top: 1px solid var(--line); overflow: visible; }
  #hint { display: none; }
}
