:root {
  --bg: #0f1115;
  --panel: #181b22;
  --line: #2a2f3a;
  --text: #e7e9ee;
  --muted: #9aa1ad;
  --accent: #4c8dff;
  --stop: #d43a3a;
  --ok: #3ac47d;
}
* { box-sizing: border-box; min-width: 0; }
html, body { margin: 0; overflow-x: hidden; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, "Segoe UI", Roboto, sans-serif;
  padding: env(safe-area-inset-top) 12px calc(env(safe-area-inset-bottom) + 12px);
  max-width: 640px;
  margin: 0 auto;
  overflow-x: hidden;
}
input[type=range] { min-width: 0; }

header {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 10px 0;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 5;
}
h1 {
  font-size: 18px;
  margin: 0;
  margin-right: auto;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.status { color: var(--muted); font-size: 14px; width: 100%; }
.status.on { color: var(--ok); }

.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 15px;
  min-height: 48px;
  cursor: pointer;
}
.btn:active { background: #222734; }
.btn.ghost { background: transparent; color: var(--muted); font-size: 13px; min-height: 40px; padding: 8px 10px; }

.warn {
  background: #3a2b16;
  border: 1px solid #6b4f22;
  border-radius: 10px;
  padding: 12px;
}

/* Одноразовое напоминание о сборке (нулевое положение приводов) */
#zeroWarn {
  background: #3a1616;
  border: 2px solid #b3473f;
  margin: 10px 0;
}
.warn-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 10px; }

#controls.disabled { opacity: 0.45; pointer-events: none; }

.stop {
  width: 100%;
  border: none;
  background: var(--stop);
  color: #fff;
  font-size: 26px;
  font-weight: 800;
  letter-spacing: 2px;
  border-radius: 14px;
  padding: 22px;
  margin: 8px 0 14px;
  cursor: pointer;
}
.stop:active { background: #b02c2c; }

.joint {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  margin-bottom: 10px;
}
.joint .row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 6px;
}
.joint .name { font-weight: 600; }
.joint .val { font-variant-numeric: tabular-nums; color: var(--muted); }
.joint .slide {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
}
.joint input[type=range] {
  width: 100%;
  height: 40px;
  accent-color: var(--accent);
}
.nudge {
  width: 46px;
  min-height: 44px;
  font-size: 20px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
}
.nudge:active { background: #222734; }

.actions {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-top: 12px;
}
.hint { color: var(--muted); font-size: 13px; text-align: center; }
.hint.hidden { visibility: hidden; }

/* ---------- управление наклоном ---------- */
#tiltBox {
  margin-top: 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
}
#tiltBox > summary { padding: 12px; cursor: pointer; font-weight: 600; }
.tilt { padding: 0 12px 12px; }
.tilt-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 10px;
}
.tilt label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  color: var(--muted);
}
.tilt label:first-child { flex: 1 1 auto; }
.tilt select {
  flex: 1;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 8px;
  font-size: 15px;
  min-height: 44px;
}
.tilt #tiltToggle { flex: 0 0 auto; }
.tilt #tiltToggle.on { background: #1d3b6b; border-color: var(--accent); }
.tilt input[type=range] { flex: 1; min-width: 90px; height: 36px; accent-color: var(--accent); }
.tilt #tiltGainVal { font-variant-numeric: tabular-nums; }
#tiltReadout { text-align: left; margin-top: 10px; }
#tiltReadout.warn-line { color: var(--stop); }

/* ---------- программа движений ---------- */
#programBox {
  margin-top: 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
}
#programBox > summary {
  padding: 12px;
  cursor: pointer;
  font-weight: 600;
}
.prog { padding: 0 12px 12px; }
.steps {
  list-style: decimal inside;
  margin: 0 0 8px;
  padding: 0;
}
.steps li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 6px;
  border-bottom: 1px solid var(--line);
  font-size: 14px;
}
.steps li span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.steps li.active { background: #1c2740; border-radius: 6px; }
.steps li .x {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  border-radius: 8px;
  min-width: 34px;
  min-height: 34px;
  cursor: pointer;
}
.prog-add, .prog-coord, .prog-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}
.prog-add .btn, .prog-actions .btn { flex: 1 1 auto; }
.prog .inline {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1 1 auto;
}
.prog input[type=number] {
  width: 68px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 8px;
  font-size: 15px;
}
.xyz {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  width: 100%;
}
.xyz label {
  display: flex;
  flex-direction: column;
  font-size: 12px;
  color: var(--muted);
  gap: 3px;
}
.xyz input { width: 100%; }
.btn.play { background: #1d3b6b; border-color: var(--accent); }

/* ---------- программирование (JS-код) ---------- */
#codeBox {
  margin-top: 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
}
#codeBox > summary { padding: 12px; cursor: pointer; font-weight: 600; }
.code { padding: 0 12px 12px; }
.code-help { text-align: left; line-height: 1.5; }
.code-help code {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 1px 4px;
  font-size: 12px;
}
#codeArea {
  width: 100%;
  min-height: 220px;
  resize: vertical;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  font: 13px/1.45 ui-monospace, Menlo, Consolas, monospace;
  tab-size: 2;
}
.code-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}
.code-actions .btn { flex: 1 1 auto; }

/* ---------- оверлей Blockly ---------- */
#blocklyOverlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: var(--bg);
  flex-direction: column;
}
/* id-селектор перебивает [hidden] по специфичности — показываем только без атрибута */
#blocklyOverlay:not([hidden]) { display: flex; }
#blocklyOverlay .bl-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  font-weight: 600;
}
#blocklyOverlay .bl-bar .btn { flex: 0 0 auto; min-height: 40px; }
#blocklyFrame {
  flex: 1;
  width: 100%;
  border: 0;
  background: #fff;
}

#logBox {
  margin-top: 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
}
#logBox summary { padding: 10px 12px; cursor: pointer; color: var(--muted); }
#log {
  margin: 0;
  padding: 0 12px 12px;
  max-height: 40vh;
  overflow: auto;
  font: 12px/1.35 ui-monospace, Menlo, Consolas, monospace;
  color: var(--muted);
  white-space: pre-wrap;
}

/* ---------- широкий экран (ПК): две колонки ---------- */
@media (min-width: 900px) {
  body { max-width: 1160px; padding-left: 24px; padding-right: 24px; }
  h1 { font-size: 22px; }
  .status { width: auto; }
  .stop { font-size: 22px; padding: 16px; margin: 0; }

  #controls {
    display: grid;
    grid-template-columns: minmax(360px, 1fr) minmax(360px, 1.05fr);
    gap: 18px 24px;
    align-items: start;
  }
  #stopBtn { grid-column: 1 / -1; }
  .ctl-main { grid-column: 1; min-width: 0; }
  #programBox { grid-column: 2; margin-top: 0; }
  #codeBox { grid-column: 2; }
  #tiltBox { margin-top: 14px; }

  /* оверлей Blockly не на весь экран, а по центру */
  #blocklyOverlay:not([hidden]) {
    inset: 24px;
    border: 1px solid var(--line);
    border-radius: 12px;
    overflow: hidden;
  }
}
