:root {
  --bg: #0f1419;
  --panel: #1b2330;
  --panel-2: #232d3d;
  --fg: #e6edf3;
  --muted: #8b95a3;
  --accent: #58a6ff;
  --accent-2: #56d364;
  --warn: #f0883e;
  --error: #f85149;
  --border: #30363d;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
  background: var(--bg);
  color: var(--fg);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

header {
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  padding: 16px 24px;
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}

header h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
  margin-left: auto;
}

.status .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
}

.status.connected .dot {
  background: var(--accent-2);
  box-shadow: 0 0 8px var(--accent-2);}

.status.connecting .dot {
  background: var(--warn);
}

button {
  background: var(--panel-2);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 14px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.15s;
  font-family: inherit;
}

button:hover:not(:disabled) {
  background: #2d3a4f;
  border-color: var(--accent);
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

button.primary {
  background: var(--accent);
  color: #0d1117;
  border-color: var(--accent);
  font-weight: 600;
}

button.primary:hover:not(:disabled) {
  background: #79b8ff;
}

button.danger {
  background: var(--error);
  color: white;
  border-color: var(--error);
}

main {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  padding: 16px 24px;
  max-width: 1400px;
  width: 100%;
  margin: 0 auto;
}

@media (max-width: 900px) {
  main {
    grid-template-columns: 1fr;
  }
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
}

.card h2 {
  margin: 0 0 12px;
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--muted);
}

.row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

.row + .row {
  margin-top: 10px;
}

input[type='text'] {
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 14px;
  flex: 1;
  font-family: inherit;
}

input[type='text']:focus {
  outline: none;
  border-color: var(--accent);
}

.led-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
  max-width: 220px;
  margin: 12px auto;
}

.led {
  aspect-ratio: 1;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.1s;
}

.led:hover {
  border-color: var(--accent);
}

.led.on {
  background: #ff4444;
  border-color: #ff4444;
  box-shadow: 0 0 8px #ff4444aa;
}

.stat {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--panel-2);
  padding: 4px 10px;  border-radius: 999px;
  font-size: 13px;
  font-family: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
}

.btn-state {
  display: inline-block;
  height: 24px;
  text-align: center;
  font-weight: 600;
  font-family: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  background: var(--panel-2);
  color: var(--muted);  padding: 4px 8px;
  border-radius: 6px;
}

.btn-state.pressed {
  background: var(--accent-2);
  color: #0d1117;
}

#log {
  background: #0d1117;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px;
  font-family: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  font-size: 12px;
  height: 280px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-all;
}

#log .tx {
  color: var(--accent);
}

#log .rx {
  color: var(--accent-2);
}

#log .err {
  color: var(--error);
}

#log .info {
  color: var(--muted);
}

label {
  font-size: 13px;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}

.hint {
  font-size: 12px;
  color: var(--muted);
  line-height: 1.5;
  margin-top: 6px;
}

.grid-2col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

/* ===== Sound Panel ===== */

.note-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  justify-content: center;
}

.note-btn {
  min-width: 44px;
  height: 36px;
  font-size: 11px;
  font-weight: 600;
  padding: 4px 6px;
  font-family: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  background: var(--panel-2);
  color: var(--fg);
  border-radius: 4px;
}

.note-btn:hover:not(:disabled) {
  background: var(--accent);
  color: #0d1117;
}

.note-btn.sharp {
  background: #2a1f3d;
  color: #c9b3ff;
  min-width: 38px;
}

.note-btn.sharp:hover:not(:disabled) {
  background: #8b5cf6;
  color: white;
}

.short-input {
  width: 100px !important;
  flex: none !important;
}

/* ===== Motion Sensor Panel ===== */

.sensor-section {
  margin-bottom: 14px;
}

.sensor-section:last-of-type {
  margin-bottom: 0;
}

.sensor-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.sensor-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--fg);
}

.toggle-label {
  font-size: 12px;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
}

.axis-grid {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.axis-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.axis-label {
  font-size: 12px;
  font-weight: 700;
  font-family: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  color: var(--muted);
  min-width: 14px;
  text-align: center;
}

.axis-bar-container {
  flex: 1;
  height: 8px;
  background: var(--bg);
  border-radius: 4px;
  overflow: hidden;
}

.axis-bar {
  height: 100%;
  border-radius: 4px;
  transition: width 0.15s ease;
  min-width: 2px;
}

.axis-bar.axis-x { background: var(--accent); }
.axis-bar.axis-y { background: var(--accent-2); }
.axis-bar.axis-z { background: var(--warn); }
.axis-bar.axis-mx { background: #c084fc; }
.axis-bar.axis-my { background: #f472b6; }
.axis-bar.axis-mz { background: #fbbf24; }

.axis-value {
  font-size: 11px;
  font-family: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  color: var(--muted);
  min-width: 60px;
  text-align: right;
}

.stat-row {
  margin-top: 6px;
  display: flex;
  gap: 8px;
}

/* ===== Compass ===== */

.compass-container {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-top: 10px;
  justify-content: center;
}

.compass {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: var(--bg);
  position: relative;
  transition: transform 0.3s ease;
}

.compass-needle {
  position: absolute;
  top: 6px;
  left: 50%;
  width: 3px;
  height: 24px;
  margin-left: -1.5px;
  background: var(--error);
  border-radius: 2px;
}

.compass-n {
  position: absolute;
  top: 2px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 9px;
  font-weight: 700;
  color: var(--error);
}

.compass-heading {
  font-size: 18px;
  font-weight: 700;
  font-family: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  color: var(--fg);
}

/* ===== LED Brightness ===== */

.brightness-slider {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 6px;
  background: var(--bg);
  border-radius: 3px;
  outline: none;
  border: 1px solid var(--border);
}

.brightness-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: none;
}

.brightness-slider::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: none;
}

.led.brightness-mode.on {
  background: #ff4444;
  border-color: #ff4444;
}

/* ===== Layout adjustment for more cards ===== */

@media (min-width: 1200px) {
  main {
    grid-template-columns: 1fr 1fr 1fr;
  }
}

/* ===== Touch Sensor ===== */

.touch-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-top: 10px;
}

.touch-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.touch-indicator {
  width: 100%;
  aspect-ratio: 1;
  max-width: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--panel-2);
  border: 2px solid var(--border);
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
  font-family: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  color: var(--muted);
  transition: all 0.15s;
  cursor: default;
}

.touch-indicator.touched {
  background: #2d1f5e;
  border-color: #8b5cf6;
  color: #c9b3ff;
  box-shadow: 0 0 10px #8b5cf6aa;
}

.touch-label {
  font-size: 16px;
  text-align: center;
  color: var(--muted);
  transition: color 0.15s;
}
