:root {
  --key-size: 50px;
  --key-gap: 6px;
  --key-radius: 6px;
  --key-bg: #fafafa;
  --key-border: #aaa;
  --key-shadow: rgba(0,0,0,0.15);
  --analog-blue: #0d6efd;
  --ghost-gray: #868e96;
}

body {
  font-family: sans-serif;
  text-align: center;
  margin: 0;
  padding: 1rem;
}

h1 {
  margin-bottom: 0.25rem;
}

.subtitle {
  margin: 0 auto 1.5rem;
  max-width: 640px;
  color: #555;
  font-size: 0.95rem;
  line-height: 1.5;
}

/* 연결 컨트롤 */
.connection-controls {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1.5rem;
  justify-content: center;
}

.btn {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1rem;
  transition: background-color 0.2s;
}

.btn-primary {
  background-color: #007bff;
  color: white;
}

.btn-primary:hover {
  background-color: #0056b3;
}

.btn-primary:disabled {
  background-color: #6c757d;
  cursor: not-allowed;
}

.btn-secondary {
  background-color: #e9ecef;
  color: #333;
}

.btn-secondary:hover {
  background-color: #d3d9df;
}

.connection-status {
  padding: 0.5rem 1rem;
  border-radius: 4px;
  background-color: #f8f9fa;
  border: 1px solid #dee2e6;
  color: #6c757d;
}

.connection-status.connected {
  background-color: #d4edda;
  border-color: #c3e6cb;
  color: #155724;
}

/* Live HID report-rate readout (shown only while a device is connected) */
.sampling-rate {
  padding: 0.5rem 1rem;
  border-radius: 4px;
  background-color: #e7f1ff;
  border: 1px solid #b6d4fe;
  color: #084298;
  font-variant-numeric: tabular-nums;
}

.sampling-rate[hidden] {
  display: none;
}

/* 메인 레이아웃: 입력 패널 | 플레이필드 */
.demo-grid {
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 1.5rem;
  max-width: 1000px;
  margin: 0 auto;
  padding: 0 1rem;
  text-align: left;
  /* 두 컬럼이 같은 높이로 늘어나야 플레이필드가 입력 패널 바닥까지 확장됨 */
  align-items: stretch;
}

.demo-grid h2 {
  margin: 0 0 0.75rem;
  font-size: 1.1rem;
}

.field-panel {
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.field-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 0.75rem;
}

.field-header h2 {
  margin: 0;
  flex: 1;
}

.field-legend {
  display: flex;
  gap: 0.9rem;
  font-size: 0.85rem;
  color: #555;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  cursor: pointer;
  user-select: none;
}

.legend-item input[type="checkbox"] {
  accent-color: var(--analog-blue);
  margin: 0;
  cursor: pointer;
}

/* disconnected 상태: analog 토글 잠금 표시 */
.legend-item:has(input:disabled) {
  opacity: 0.45;
  cursor: not-allowed;
}

.legend-item input[type="checkbox"]:disabled {
  cursor: not-allowed;
}

.legend-dot {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 50%;
}

.legend-dot.analog {
  background: var(--analog-blue);
}

.legend-dot.ghost {
  border: 2px dashed var(--ghost-gray);
  box-sizing: border-box;
}

#fieldCanvas {
  display: block;
  width: 100%;
  /* 입력 패널 높이까지 채움 (field-panel이 flex column) */
  flex: 1 1 auto;
  min-height: 460px;
  border: 1px solid #ddd;
  border-radius: 8px;
  background: #fff;
}

/* 입력 패널: WASD 키 + 벡터 게이지 + 슬라이더 */
.input-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.input-panel h2 {
  align-self: flex-start;
}

.wasd-cluster {
  display: inline-block;
  background: #ddd;
  padding: 8px;
  border-radius: calc(var(--key-radius) * 1.5);
  margin-bottom: 1rem;
}

.row {
  display: flex;
  justify-content: center;
  gap: var(--key-gap);
  margin-bottom: var(--key-gap);
}

.row:last-child {
  margin-bottom: 0;
}

.key {
  display: flex;
  align-items: center;
  justify-content: center;
  width: var(--key-size);
  height: var(--key-size);
  background: var(--key-bg);
  border: 1px solid var(--key-border);
  border-radius: var(--key-radius);
  box-shadow: 0 2px 4px var(--key-shadow);
  user-select: none;
  text-align: center;
  font-size: 0.95rem;
  line-height: 1.2;
  cursor: default;
  transition: background 0.1s, box-shadow 0.1s;
  will-change: background-color, box-shadow;
}

/* 아날로그 활성 키: depth 뱃지 표시 */
.key.analog-active {
  position: relative;
  box-shadow: 0 0 10px rgba(13, 110, 253, 0.5);
}

.key.analog-active::after {
  content: attr(data-depth);
  position: absolute;
  bottom: 2px;
  right: 2px;
  font-size: 0.6rem;
  background: rgba(0, 0, 0, 0.7);
  color: white;
  padding: 1px 3px;
  border-radius: 2px;
  line-height: 1;
  pointer-events: none;
}

/* actuation 임계 통과 = 디지털 키보드라면 ON인 상태 */
.key.actuated {
  outline: 3px solid var(--ghost-gray);
  outline-offset: 2px;
}

#gaugeCanvas {
  display: block;
  width: 220px;
  height: 220px;
  margin-bottom: 1rem;
}

/* 튜너블 슬라이더 */
.tunables {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  width: 100%;
  padding: 0.75rem;
  background: #f8f9fa;
  border-radius: 8px;
  box-sizing: border-box;
  font-size: 0.85rem;
  color: #333;
}

.tunables label {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  text-align: left;
}

.tunables label span {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

.tunables input[type="range"] {
  width: 100%;
}

/* 실시간 depth 차트 */
.charts-container {
  width: 100%;
  max-width: 1000px;
  margin: 1.5rem auto;
  text-align: left;
  padding: 0 1rem;
  box-sizing: border-box;
}

.charts-container h2 {
  margin: 0 0 0.75rem;
  font-size: 1.1rem;
}

.chart-container {
  height: 260px;
  border: 1px solid #ddd;
  border-radius: 8px;
  background: #fff;
  padding: 1rem;
  box-sizing: border-box;
}

#depthChart {
  width: 100%;
  height: 100%;
  display: block;
}

/* 반응형 */
@media (max-width: 768px) {
  .demo-grid {
    grid-template-columns: 1fr;
  }

  #fieldCanvas {
    flex: none;
    min-height: 0;
    height: 340px;
  }

  .connection-controls {
    flex-direction: column;
    gap: 0.5rem;
  }
}
