:root {
  --bg: #0f1218;
  --panel: #181c25;
  --panel-2: #20252f;
  --line: #2c3340;
  --text: #e9ecf1;
  --muted: #9aa3b2;
  --green: #2ecc71;
  --red: #ff4d4f;
  --yellow: #f5c542;
  --accent: #4f8cff;
  --radius: 14px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 18px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-user-select: none; user-select: none;
  touch-action: manipulation;
}
button { font: inherit; cursor: pointer; border: 0; border-radius: 10px; color: var(--text); background: var(--panel-2); padding: 12px 16px; }
[hidden] { display: none !important; }
button:hover { filter: brightness(1.15); }
button:disabled { opacity: .45; cursor: not-allowed; filter: none; }
button.primary { background: var(--accent); color: #fff; font-weight: 600; }
button.danger { background: #3a2226; color: #ffb3b5; }
input[type=text] { font: inherit; background: var(--panel-2); color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; }

/* ---------- header / status banner ---------- */
header {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 14px 20px; border-bottom: 1px solid var(--line);
}
header h1 { font-size: 26px; margin: 0; letter-spacing: .3px; display: flex; align-items: baseline; gap: 8px; }
.model { font-size: 16px; font-weight: 800; color: #061; background: var(--green); padding: 2px 10px; border-radius: 99px; align-self: center; }
.pagelink { color: var(--muted); font-size: 15px; text-decoration: none; padding: 8px 12px; border: 1px solid var(--line); border-radius: 10px; }
.pagelink:hover { color: var(--text); border-color: var(--muted); }
#version { font-size: 14px; font-weight: 600; color: var(--muted); letter-spacing: .5px; }
#banner {
  flex: 1; min-width: 240px;
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; border-radius: var(--radius);
  font-weight: 700; font-size: 21px;
  background: #3a1e21; color: #ffb3b5; border: 2px solid var(--red);
}
#banner.ok { background: #16311f; color: #b6f2c9; border-color: var(--green); }
#banner.warn { background: #3a3116; color: #ffe8a3; border-color: var(--yellow); }
#banner .dot { width: 16px; height: 16px; border-radius: 50%; background: var(--red); box-shadow: 0 0 0 0 rgba(255,77,79,.6); animation: pulse 1.6s infinite; flex: none; }
#banner.ok .dot { background: var(--green); box-shadow: 0 0 0 0 rgba(46,204,113,.6); }
#banner.warn .dot { background: var(--yellow); box-shadow: 0 0 0 0 rgba(245,197,66,.6); }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 currentColor; } 70% { box-shadow: 0 0 0 10px rgba(0,0,0,0); } 100% { box-shadow: 0 0 0 0 rgba(0,0,0,0); } }
.alert { max-width: 1200px; margin: 20px auto 0; padding: 18px 22px; border-radius: var(--radius); background: #3a1e21; border: 3px solid var(--red); }
#problemBox { width: calc(100% - 40px); }
#problemBox .detail { margin-top: 6px; font-size: 15px; color: #ffb3b5; }
#btHelp { width: calc(100% - 40px); font-size: 22px; }
#btHelp h3 { margin: 0 0 12px; font-size: 28px; }
#btHelp ol { margin: 0; padding-left: 32px; display: grid; gap: 12px; }
#btHelp code { font-family: ui-monospace, Consolas, monospace; background: #000; color: #fff; padding: 4px 10px; border-radius: 8px; user-select: all; -webkit-user-select: all; }
#btHelp button { font-size: 18px; padding: 8px 14px; margin-left: 6px; vertical-align: middle; }
#btHelp .small { margin: 12px 0 0; font-size: 16px; color: #ffb3b5; }

/* ---------- layout ---------- */
main { display: grid; grid-template-columns: 380px 1fr; gap: 20px; padding: 20px; max-width: 1200px; margin: 0 auto; }
@media (max-width: 800px) { main { grid-template-columns: 1fr; } }
section.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
h2 { font-size: 16px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); margin: 0 0 12px; }

/* ---------- connect ritual ---------- */
#connectBtn { width: 100%; font-size: 24px; padding: 18px; margin-bottom: 14px; }
ol.steps { margin: 0 0 16px; padding-left: 0; list-style: none; counter-reset: s; display: grid; gap: 10px; }
ol.steps li { counter-increment: s; display: flex; align-items: center; gap: 12px; font-size: 20px; }
ol.steps li::before { content: counter(s); flex: none; width: 32px; height: 32px; border-radius: 50%; background: var(--panel-2); color: var(--text); display: grid; place-items: center; font-weight: 700; font-size: 17px; }
ol.steps b { color: var(--text); }

/* ---------- robot cards ---------- */
#robots { display: grid; gap: 10px; }
.robot {
  position: relative; display: grid; gap: 8px;
  padding: 12px 12px 12px 16px; border-radius: 12px;
  background: var(--panel-2); border: 2px solid var(--line);
  border-left: 8px solid var(--robot-color, #888);
  cursor: pointer;
}
.robot.active { border-color: var(--robot-color); box-shadow: 0 0 0 3px color-mix(in srgb, var(--robot-color) 35%, transparent); }
.robot.dead { opacity: .75; border-color: var(--red); border-left-color: var(--robot-color); }
.robot .top { display: flex; align-items: center; gap: 10px; padding-right: 96px; }
.robot .swatch { width: 34px; height: 34px; border-radius: 50%; background: var(--robot-color); box-shadow: 0 0 12px var(--robot-color); flex: none; }
.robot .name { font-weight: 700; font-size: 22px; flex: 0 1 auto; min-width: 0; }
.robot .name input { width: 100%; font-weight: 700; font-size: 20px; }
.robot .id { color: var(--muted); font-size: 14px; font-family: ui-monospace, Menlo, monospace; }
.robot .status { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 19px; }
.robot .status .dot { width: 14px; height: 14px; border-radius: 50%; background: var(--red); }
.robot .status.ok .dot { background: var(--green); }
.robot .status.busy .dot { background: var(--yellow); }
.robot .status.ok { color: #b6f2c9; } .robot .status.busy { color: #ffe8a3; } .robot .status.bad { color: #ffb3b5; }
.robot .meta { color: var(--muted); font-size: 16px; }
.robot .actions { display: flex; flex-wrap: wrap; gap: 6px; }
.robot .actions button { padding: 9px 13px; font-size: 17px; }
.robot .drivetag { position: absolute; top: 10px; right: 10px; font-size: 13px; font-weight: 800; letter-spacing: .5px; text-transform: uppercase; color: #061; background: var(--green); padding: 3px 8px; border-radius: 99px; display: none; }
.robot.active .drivetag { display: block; }
.robot .rename { padding: 4px 8px; font-size: 16px; background: transparent; opacity: .6; }
.robot .rename:hover { opacity: 1; background: var(--panel); }
#swapKeys { display: block; margin-top: 8px; font-size: 16px; }
.robot .keys {
  justify-self: start; padding: 6px 12px; border-radius: 99px; font-weight: 800; font-size: 19px; letter-spacing: .3px;
  background: #2a3550; color: #cfe0ff; border: 2px solid #4f6fb0;
}
.robot .keys[data-set=arrows] { background: #3b2a50; color: #ead6ff; border-color: #8a5fc7; }
.robot .keys[data-set=none] { background: var(--panel); color: var(--muted); border-color: var(--line); font-weight: 600; font-size: 16px; }
#noRobots { color: var(--muted); font-size: 19px; text-align: center; padding: 20px 8px; border: 2px dashed var(--line); border-radius: 12px; }

/* ---------- controls ---------- */
#controls { display: grid; grid-template-columns: auto 1fr; gap: 24px; align-items: start; }
@media (max-width: 560px) { #controls { grid-template-columns: 1fr; justify-items: center; } }
#stickWrap { position: relative; }
#stick {
  width: 260px; height: 260px; border-radius: 50%;
  background: radial-gradient(circle at 50% 50%, #262c38 0, #1c212b 70%, #161a22 100%);
  border: 3px solid var(--line); position: relative; touch-action: none;
  box-shadow: inset 0 0 40px rgba(0,0,0,.5);
}
#stick::before { content: ""; position: absolute; inset: 50%; width: 2px; height: 100%; transform: translate(-50%, -50%); background: var(--line); }
#stick::after  { content: ""; position: absolute; inset: 50%; height: 2px; width: 100%; transform: translate(-50%, -50%); background: var(--line); }
#knob {
  position: absolute; left: 50%; top: 50%; width: 92px; height: 92px; border-radius: 50%;
  transform: translate(-50%, -50%);
  background: radial-gradient(circle at 35% 30%, #6d9bff, #2f63d6 70%);
  box-shadow: 0 8px 20px rgba(0,0,0,.5); z-index: 2;
  transition: background .2s;
}
#stick.dead #knob { background: radial-gradient(circle at 35% 30%, #555, #333 70%); }
#stickOverlay {
  position: absolute; inset: 0; display: grid; place-items: center; text-align: center;
  border-radius: 50%; background: rgba(15,18,24,.7); color: var(--text); font-weight: 700; font-size: 20px;
  padding: 30px; z-index: 3;
}
#stickOverlay[hidden] { display: none; }
.fwdLabel { position: absolute; top: 8px; left: 50%; transform: translateX(-50%); font-size: 13px; color: var(--muted); letter-spacing: 1px; }

.row { display: grid; gap: 10px; margin-bottom: 18px; }
.row label { display: flex; justify-content: space-between; color: var(--muted); font-size: 16px; text-transform: uppercase; letter-spacing: .8px; }
.row label output { color: var(--text); font-weight: 700; }
input[type=range] { width: 100%; accent-color: var(--accent); }
#swatches { display: flex; flex-wrap: wrap; gap: 8px; }
#swatches button { width: 46px; height: 46px; border-radius: 50%; padding: 0; border: 3px solid transparent; }
#swatches button.sel { border-color: #fff; }
#colorPick { width: 46px; height: 46px; padding: 0; border: 0; background: none; cursor: pointer; }
#aimBox { display: none; padding: 12px; border-radius: 10px; background: #2b2a1a; border: 1px solid #5a5220; }
#aimBox.on { display: grid; gap: 10px; }
#aimBox p { margin: 0; font-size: 19px; }
.keymap { display: grid; grid-template-columns: auto 1fr; gap: 12px 16px; align-items: center; font-size: 20px; }
.keymap .what { font-weight: 800; }
.keymap .or { color: var(--muted); margin: 0 6px; font-size: 17px; }
details.more { margin-top: 4px; color: var(--muted); font-size: 16px; }
details.more summary { cursor: pointer; font-weight: 700; color: var(--text); padding: 6px 0; }
details.more ul { margin: 8px 0 0; padding-left: 20px; display: grid; gap: 6px; }
.chip { display: inline-block; padding: 5px 10px; border-radius: 99px; background: var(--panel-2); color: var(--muted); font-size: 14px; font-weight: 600; }
.chip.on { background: #16311f; color: #b6f2c9; }
kbd { display: inline-block; min-width: 34px; text-align: center; padding: 3px 9px; margin: 2px 1px; border-radius: 7px; background: var(--panel-2); border: 1px solid #4a5366; border-bottom-width: 4px; font-family: inherit; font-weight: 700; font-size: 18px; color: var(--text); }

/* ---------- toasts ---------- */
#toasts { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); display: grid; gap: 8px; z-index: 50; width: min(520px, calc(100% - 32px)); }
.toast { padding: 12px 16px; border-radius: 10px; background: #2a3040; border-left: 6px solid var(--accent); box-shadow: 0 8px 30px rgba(0,0,0,.5); font-weight: 700; font-size: 19px; animation: rise .2s ease-out; }
.toast.bad { border-left-color: var(--red); background: #3a1e21; }
.toast.ok  { border-left-color: var(--green); background: #16311f; }
@keyframes rise { from { transform: translateY(10px); opacity: 0; } }

.toggle { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; color: var(--text); }
.toggle input { width: 20px; height: 20px; accent-color: var(--accent); }
