:root {
  --ink: #111827;
  --muted: #4b5563;
  --card: rgba(255, 255, 255, 0.94);
  --edge: rgba(17, 24, 39, 0.12);
  --accent: #ff6a1a;
  --tab-w: 34px;
  --panel-w: min(44vw, 360px);
  --joy: 132px;
  --joy-gap: 28px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #eef0f3;
  color: var(--ink);
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

#view {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  display: block;
  touch-action: none;
}

button { font: inherit; color: inherit; cursor: pointer; }

/* ---------- top bar ---------- */
.topbar {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  display: flex;
  gap: 8px;
  align-items: center;
  z-index: 5;
}
#reset {
  border: 1px solid var(--edge);
  background: var(--card);
  border-radius: 10px;
  padding: 8px 14px;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}
#reset:hover { background: #fff; }
#reset:active { transform: translateY(1px); }
.chip {
  font: 600 12px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  background: rgba(17, 24, 39, 0.72);
  color: #fff;
  padding: 6px 10px;
  border-radius: 8px;
  white-space: nowrap;
}

/* ---------- controller button + setup panel ---------- */
#pad-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border: 1px solid var(--edge);
  background: var(--card);
  border-radius: 10px;
  padding: 8px 12px;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}
#pad-btn:hover { background: #fff; }
.pad-dot { width: 8px; height: 8px; border-radius: 50%; background: #9ca3af; }
#pad-btn[data-status="setup"] .pad-dot { background: #f59e0b; }
#pad-btn[data-status="ready"] .pad-dot { background: #10b981; }

.pad-modal {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(17, 24, 39, 0.28);
}
.pad-modal[hidden] { display: none; }
.pad-card {
  width: min(420px, 100%);
  max-height: calc(100dvh - 32px);
  overflow: auto;
  background: #fff;
  border-radius: 14px;
  padding: 16px 18px 18px;
  box-shadow: 0 20px 50px rgba(17, 24, 39, 0.25);
}
.pad-head { display: flex; justify-content: space-between; align-items: center; }
.pad-head strong { font-size: 16px; }
#pad-close { border: 0; background: none; font-size: 16px; color: var(--muted); padding: 4px 6px; }
.pad-name { margin: 8px 0 2px; font-weight: 600; font-size: 13px; word-break: break-word; }
.pad-hint { margin: 0 0 10px; font-size: 12px; color: var(--muted); line-height: 1.45; }
.pad-section { margin-top: 12px; }
.pad-label {
  font: 700 10px system-ui, sans-serif;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 6px;
}
.pad-rows { display: grid; gap: 5px; }
.pad-row { display: grid; grid-template-columns: 74px 1fr 44px; gap: 8px; align-items: center; font-size: 12px; }
.pad-row span:last-child { text-align: right; font-family: ui-monospace, Menlo, Consolas, monospace; color: var(--muted); }
.pad-bar { position: relative; height: 8px; border-radius: 4px; background: #eef0f3; overflow: hidden; }
.pad-bar::after { content: ""; position: absolute; left: 50%; top: 0; bottom: 0; width: 1px; background: rgba(17, 24, 39, 0.25); }
.pad-bar i { position: absolute; top: 0; bottom: 0; background: #2563eb; border-radius: 4px; }
.pad-wizard {
  margin-top: 10px;
  padding: 14px;
  border-radius: 10px;
  background: #fff7ed;
  border: 1px solid #fed7aa;
}
.pad-wizard[hidden] { display: none; }
.pad-prompt { font-weight: 700; font-size: 15px; text-align: center; }
.pad-progress { margin-top: 10px; height: 5px; border-radius: 3px; background: #fde4c8; overflow: hidden; }
.pad-progress i { display: block; height: 100%; width: 0; background: var(--accent); transition: width 0.2s; }
.pad-actions { display: flex; gap: 8px; margin-top: 16px; }
.pad-actions button {
  flex: 1;
  border: 1px solid var(--edge);
  background: #f9fafb;
  border-radius: 9px;
  padding: 9px 12px;
  font-weight: 600;
}
.pad-actions button.primary { background: var(--ink); color: #fff; border-color: var(--ink); }
.pad-actions button:disabled { opacity: 0.4; cursor: not-allowed; }

/* ---------- out-of-sight warning ---------- */
/* Full-screen overlay: pulsing red edge vignette + a compact card with a countdown ring. */
.oos {
  position: fixed;
  inset: 0;
  z-index: 6;
  pointer-events: none;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: max(64px, 14vh) 16px 0;
  box-shadow: inset 0 0 90px 10px rgba(220, 38, 38, 0.38);
  animation: oos-pulse 1s ease-in-out infinite alternate;
}
.oos[hidden] { display: none; }
@keyframes oos-pulse {
  from { box-shadow: inset 0 0 70px 4px rgba(220, 38, 38, 0.22); }
  to { box-shadow: inset 0 0 110px 16px rgba(220, 38, 38, 0.45); }
}
.oos-card {
  display: flex;
  align-items: center;
  gap: 14px;
  max-width: 100%;
  padding: 12px 20px 12px 12px;
  border-radius: 16px;
  background: rgba(17, 24, 39, 0.86);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  color: #fff;
  border: 1px solid rgba(248, 113, 113, 0.55);
  box-shadow: 0 10px 30px rgba(17, 24, 39, 0.3);
  animation: oos-in 0.22s ease-out;
}
@keyframes oos-in {
  from { opacity: 0; transform: translateY(-8px) scale(0.97); }
  to { opacity: 1; transform: none; }
}
.oos-ring { position: relative; width: 52px; height: 52px; flex: none; }
.oos-ring svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.oos-track { fill: none; stroke: rgba(255, 255, 255, 0.15); stroke-width: 4; }
.oos-bar {
  fill: none;
  stroke: #f87171;
  stroke-width: 4;
  stroke-linecap: round;
  stroke-dasharray: 131.95;
  stroke-dashoffset: 0;
}
.oos-ring b {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font: 700 22px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.oos-text { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.oos-text strong {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.oos-icon {
  display: inline-grid;
  place-items: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #ef4444;
  font-size: 12px;
  font-weight: 800;
  flex: none;
}
#oos-hint { font-size: 14px; color: #fecaca; }
.oos-text small { font-size: 11px; color: rgba(255, 255, 255, 0.6); }

/* ---------- slide-out panels ---------- */
.panel {
  position: fixed;
  right: 0;
  z-index: 4;
  width: var(--panel-w);
  transform: translateX(100%);
  transition: transform 0.28s ease;
}
.panel.open { transform: translateX(0); }
.panel-top { top: max(12px, env(safe-area-inset-top)); }
.panel-cam { bottom: max(12px, env(safe-area-inset-bottom)); }

.tab {
  position: absolute;
  left: calc(-1 * var(--tab-w));
  top: 0;
  width: var(--tab-w);
  height: 76px;
  border: 2px solid var(--ink);
  border-right: none;
  border-radius: 10px 0 0 10px;
  background: var(--card);
  display: grid;
  place-items: center;
  font-size: 28px;
  line-height: 1;
  box-shadow: -2px 2px 8px rgba(0, 0, 0, 0.08);
}
.panel-cam .tab { top: auto; bottom: 0; }
.tab span { display: inline-block; transition: transform 0.28s ease; margin-top: -3px; }
.panel.open .tab span { transform: scaleX(-1); }

.panel-card {
  background: var(--card);
  border: 2px solid var(--ink);
  border-right: none;
  border-radius: 0 0 0 12px;
  padding: 6px 8px 8px;
  box-shadow: -4px 4px 16px rgba(0, 0, 0, 0.1);
}
.panel-title {
  font: 700 11px system-ui, sans-serif;
  letter-spacing: 0.12em;
  color: var(--muted);
  padding: 0 2px 5px;
}
#topview {
  display: block;
  width: 100%;
  aspect-ratio: 1 / 1;
  max-height: calc(50vh - 60px);
  max-height: calc(50dvh - 60px);
  border-radius: 6px;
  border: 1px solid var(--edge);
}

/* Camera panel: the feed is drawn by WebGL *behind* this window, so it must stay transparent. */
.cam-card { background: transparent; box-shadow: none; padding: 0; border-radius: 12px 0 0 0; overflow: hidden; }
.cam-card .panel-title { background: var(--card); padding: 6px 10px 5px; margin: 0; }
#cam-window {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  max-height: calc(50vh - 60px);
  max-height: calc(50dvh - 60px);
  background: transparent;
  border-top: 2px solid var(--ink);
}
.panel-cam:not(.open) #cam-window { background: #1b1e22; }
.hud {
  position: absolute;
  inset: 6px 6px auto 6px;
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  pointer-events: none;
}
.hud .chip { font-size: 11px; padding: 4px 8px; }

/* Center crosshair */
.crosshair {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 26px;
  height: 26px;
  transform: translate(-50%, -50%);
  pointer-events: none;
}
.crosshair::before,
.crosshair::after {
  content: "";
  position: absolute;
  background: #fff;
  box-shadow: 0 0 0 1px rgba(17, 24, 39, 0.55);
  border-radius: 1px;
}
.crosshair::before { left: 0; right: 0; top: 50%; height: 2px; margin-top: -1px; }
.crosshair::after { top: 0; bottom: 0; left: 50%; width: 2px; margin-left: -1px; }

/* ---------- keyboard help (desktop) ---------- */
.help {
  position: fixed;
  left: max(12px, env(safe-area-inset-left));
  bottom: max(12px, env(safe-area-inset-bottom));
  white-space: nowrap;
  font-size: 12px;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.75);
  border: 1px solid var(--edge);
  border-radius: 8px;
  padding: 6px 10px;
  line-height: 1.5;
  z-index: 3;
  pointer-events: none;
}
.help b { color: var(--ink); }
body.touch .help { display: none; }

/* ---------- virtual joysticks (always shown; on desktop they mirror the keys) ---------- */
.joy {
  display: block;
  position: fixed;
  bottom: max(18px, env(safe-area-inset-bottom));
  width: var(--joy);
  height: var(--joy);
  border-radius: 50%;
  background: rgba(17, 24, 39, 0.12);
  border: 2px solid rgba(17, 24, 39, 0.25);
  z-index: 3;
  touch-action: none;
}
.joy-left { left: calc(50% - var(--joy) - var(--joy-gap) / 2); }
.joy-right { left: calc(50% + var(--joy-gap) / 2); }
.joy.active,
.joy.keyed { background: rgba(17, 24, 39, 0.18); }
.nub {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 54px;
  height: 54px;
  margin: -27px 0 0 -27px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.85);
  border: 2px solid rgba(17, 24, 39, 0.35);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  pointer-events: none;
}
.joy:not(.active) .nub { transition: transform 0.12s ease-out; }
.joy-label {
  position: absolute;
  top: -18px;
  left: 0;
  right: 0;
  text-align: center;
  font: 700 10px system-ui, sans-serif;
  letter-spacing: 0.08em;
  color: rgba(17, 24, 39, 0.55);
  pointer-events: none;
}

/* On narrow screens the centered sticks reach under the camera panel, so lift it above them. */
/* Mouse/keyboard screens: smaller, lower sticks so they stay clear of the pad at bottom center. */
@media (pointer: fine) {
  :root { --joy: 92px; --joy-gap: 22px; }
  .joy { bottom: max(10px, env(safe-area-inset-bottom)); opacity: 0.85; }
  .nub { width: 38px; height: 38px; margin: -19px 0 0 -19px; }
}

@media (max-width: 760px) {
  .panel-cam,
  .help { bottom: calc(var(--joy) + 34px); }
}

@media (max-height: 500px) {
  :root { --joy: 112px; --panel-w: min(38vw, 300px); }
  .nub { width: 46px; height: 46px; margin: -23px 0 0 -23px; }
}
