* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --bg: #0a0c10;
  --panel: #141820;
  --panel2: #1e2430;
  --border: #2c3444;
  --text: #f4f6fa;
  --muted: #8b95a5;
  --blue: #3b82f6;
  --green: #10b981;
  --amber: #f59e0b;
  --red: #ef4444;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
}

button {
  color: var(--text);
  cursor: pointer;
  font: inherit;
}

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

.app {
  width: 100%;
  height: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
}

.topbar {
  flex: 0 0 56px;
  padding: 8px 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brandIcon {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  background: #1b2538;
  border: 1px solid #354768;
  border-radius: 9px;
  font-size: 17px;
}

.brand h1 {
  font-size: 15px;
  font-weight: 700;
}

.brand p {
  font-size: 10px;
  color: var(--muted);
}

.topBadges {
  display: flex;
  gap: 6px;
}

.statusPill {
  padding: 4px 8px;
  border-radius: 20px;
  background: #2b2314;
  color: #fbbf24;
  font-size: 10px;
  font-weight: 600;
  white-space: nowrap;
}

.statusPill.ready {
  background: #112e24;
  color: #34d399;
}

.statusPill.disconnected {
  background: #28191d;
  color: #f87171;
}

.sourceToolbar {
  flex: 0 0 50px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  overflow-x: auto;
  background: #0f1218;
  border-bottom: 1px solid #222834;
  scrollbar-width: none;
}

.sourceToolbar::-webkit-scrollbar {
  display: none;
}

.sourceButton {
  height: 35px;
  padding: 0 11px;
  display: flex;
  align-items: center;
  gap: 5px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel2);
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}

.primaryBtn {
  background: #1d4ed8;
  border-color: #3b82f6;
}

.bleBtn {
  background: #0f3a4a;
  border-color: #0284c7;
  color: #7dd3fc;
}

.bleBtn.connected {
  background: #064e3b;
  border-color: #10b981;
  color: #6ee7b7;
}

.dangerBtn {
  background: #3b181d;
  border-color: #7f1d1d;
}

.previewArea {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 8px;
  gap: 7px;
}

.stage {
  flex: 1;
  min-height: 0;
  width: 100%;
  position: relative;
  overflow: hidden;
  background: #050608;
  border: 1px solid #222834;
  border-radius: 10px;
}

.stage video {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.stage canvas {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
  touch-action: manipulation;
}

.telemetryHud {
  position: absolute;
  top: 10px;
  left: 10px;
  right: 10px;
  display: flex;
  justify-content: space-between;
  gap: 6px;
  pointer-events: none;
  z-index: 5;
}

.hudItem {
  flex: 1;
  background: rgba(10, 12, 16, 0.78);
  backdrop-filter: blur(5px);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  padding: 5px 6px;
  text-align: center;
}

.hudLabel {
  display: block;
  font-size: 9px;
  color: var(--muted);
}

.hudItem b {
  font-size: 12px;
}

.hudItem b.activeMove {
  color: #38bdf8;
}

.hudItem b.stop {
  color: #94a3b8;
}

.hudItem b.tracking {
  color: #34d399;
}

.hudItem b.searching {
  color: #fbbf24;
}

.hudItem b.idle {
  color: #94a3b8;
}

#empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 20px;
  gap: 8px;
  color: var(--muted);
  pointer-events: none;
}

.emptyIcon {
  font-size: 44px;
}

#empty h2 {
  font-size: 16px;
  color: var(--text);
}

#empty p {
  font-size: 12px;
  max-width: 260px;
  line-height: 1.4;
}

.liveStatusBanner {
  flex: 0 0 auto;
  padding: 7px 10px;
  border-radius: 8px;
  background: #131924;
  border: 1px solid #263245;
  color: #93c5fd;
  font-size: 11px;
  line-height: 1.35;
  text-align: center;
  overflow-wrap: anywhere;
}

.liveStatusBanner.error {
  background: #2b1419;
  border-color: #7f1d1d;
  color: #fca5a5;
}

.quickActionBar {
  flex: 0 0 42px;
  display: flex;
  gap: 8px;
}

.actionBtn {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel2);
  font-size: 11px;
  font-weight: 700;
}

.startTrackBtn {
  background: #059669;
  border-color: #10b981;
}

.stopMotorsBtn {
  background: #7f1d1d;
  border-color: #ef4444;
}

.bottomToolbar {
  flex: 0 0 60px;
  display: flex;
  align-items: center;
  justify-content: space-around;
  padding: 4px 6px;
  padding-bottom: max(4px, env(safe-area-inset-bottom));
  background: var(--panel);
  border-top: 1px solid var(--border);
  z-index: 30;
}

.navTool {
  flex: 1;
  height: 50px;
  border: none;
  border-radius: 8px;
  background: transparent;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  color: var(--muted);
  font-size: 10px;
}

.toolIcon {
  font-size: 18px;
}

.navTool.active {
  background: #1e293b;
  color: #60a5fa;
}

.sheetBackdrop {
  position: fixed;
  inset: 0;
  z-index: 39;
  background: rgba(0, 0, 0, 0.6);
  opacity: 0;
  visibility: hidden;
  transition: 0.2s ease;
}

.sheetBackdrop.show {
  opacity: 1;
  visibility: visible;
}

.bottomSheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 40;
  max-height: 70dvh;
  padding: 8px 16px 20px;
  overflow-y: auto;
  background: #141820;
  border-radius: 18px 18px 0 0;
  border-top: 1px solid var(--border);
  transform: translateY(105%);
  visibility: hidden;
  transition: transform 0.25s ease, visibility 0.25s;
}

.bottomSheet.open {
  transform: translateY(0);
  visibility: visible;
}

.sheetHandle {
  width: 100%;
  height: 16px;
  display: flex;
  justify-content: center;
}

.sheetHandle span {
  width: 36px;
  height: 4px;
  border-radius: 4px;
  background: #475569;
}

.sheetHeader {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 14px;
}

.sheetHeader h2 {
  font-size: 16px;
}

.closeSheet {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--panel2);
}

.sheetPanel {
  display: none;
}

.sheetPanel.visible {
  display: block;
}

.sheetPanel h3 {
  font-size: 13px;
  color: #93c5fd;
  margin-bottom: 14px;
}

.sliderControl {
  margin-bottom: 16px;
}

.sliderControl label {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  margin-bottom: 8px;
}

.sliderControl output {
  color: #60a5fa;
  font-weight: bold;
}

.sliderControl input {
  width: 100%;
  accent-color: var(--blue);
}

.toggleRow {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid #222834;
  font-size: 12px;
}

.toggleRow input {
  width: 18px;
  height: 18px;
  accent-color: var(--blue);
}

.templatePreviewBox {
  margin-top: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #0d1016;
  padding: 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  font-size: 11px;
  color: var(--muted);
}

#templateCanvas {
  width: 60px;
  height: 60px;
  border-radius: 6px;
  border: 1px solid #475569;
  background: #000;
}

.dpadGrid {
  display: grid;
  grid-template-columns: repeat(3, 68px);
  grid-template-rows: repeat(3, 68px);
  gap: 8px;
  justify-content: center;
  margin: 10px 0;
}

.dpadBtn {
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--panel2);
  font-size: 18px;
  font-weight: bold;
  user-select: none;
}

.dpadBtn:active {
  background: var(--blue);
}

.stopDpad {
  background: #7f1d1d;
  font-size: 12px;
}

.statusSection {
  margin-top: 12px;
}

.statusItem {
  display: flex;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid #222834;
  font-size: 12px;
  color: var(--muted);
}

#message {
  margin-top: 14px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: #93c5fd;
}
