:root {
  --bg: #f5f2ec;
  --panel: #ffffff;
  --ink: #23262b;
  --muted: #6b7280;
  --line: #e2ddd4;
  --brand: #2f6df6;
  --brand-dark: #1f4fc4;
  --ok: #12a150;
  --warn: #e0a20a;
  --danger: #d9453d;
  --scene-bg: #101724;
  --radius: 14px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
  font-size: 14px;
}

.app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  padding: 14px 16px 0;
  gap: 12px;
}

/* ---------- 顶栏 ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 18px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 16px;
}
.brand { display: flex; align-items: center; gap: 8px; }
.logo { color: var(--brand); font-size: 18px; }
.brandname { font-weight: 700; letter-spacing: .3px; }
.course-info { display: flex; align-items: center; gap: 10px; flex: 1; }
.chip {
  background: #e8f0ff; color: var(--brand-dark);
  border-radius: 20px; padding: 3px 10px; font-size: 12px; font-weight: 600;
}
.title { font-weight: 600; color: #303640; }
.hud { display: flex; gap: 14px; color: var(--muted); font-variant-numeric: tabular-nums; }

/* ---------- 主体 ---------- */
.grid {
  display: grid;
  grid-template-columns: 1.35fr 1fr 1fr;
  gap: 12px;
  flex: 1;
  min-height: 0;
}

.panel {
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  min-height: 0;
  overflow: hidden;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}
.panel-head h2 { margin: 0; font-size: 14px; font-weight: 700; color: #2b313b; }

.hint {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.6;
  min-height: 18px;
}
.hint.ok { color: var(--ok); }
.hint.err { color: var(--danger); }

/* ---------- 场景 ---------- */
canvas#scene {
  width: 100%;
  height: auto;
  border-radius: 10px;
  background: var(--scene-bg);
  display: block;
}

.tempbar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 10px;
}
.tempbar label { color: var(--muted); font-size: 12px; white-space: nowrap; }
.tempbar input[type=range] { flex: 1; accent-color: var(--brand); }
.tempbar output { font-weight: 700; min-width: 52px; text-align: right; }

/* ---------- 接线 ---------- */
.wiring-list { display: flex; flex-direction: column; gap: 10px; overflow: auto; }

.module-card {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 9px 10px;
  background: #fbfaf8;
}
.module-card .mname { font-weight: 700; margin-bottom: 6px; font-size: 13px; }
.pin-row { display: flex; align-items: center; gap: 8px; margin: 4px 0; }
.pin-row .pinname { width: 74px; color: var(--muted); font-size: 12px; }
.pin-row select {
  flex: 1;
  padding: 5px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  font-size: 12px;
  color: var(--ink);
}
.pin-row select.bad { border-color: var(--danger); background: #fff5f4; }
.pin-row select.good { border-color: var(--ok); background: #f3fdf7; }

/* ---------- 积木 ---------- */
.palette {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 10px;
  max-height: 108px;
  overflow: auto;
}
.palette .blk {
  border: none;
  border-radius: 8px;
  padding: 7px 10px;
  font-size: 12px;
  font-weight: 600;
  color: #fff;
  cursor: pointer;
  background: var(--brand);
}
.palette .blk.ctl { background: #7a5af8; }
.palette .blk.act { background: #ef8b3c; }
.palette .blk:hover { filter: brightness(1.06); }

.script-wrap { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.script-label { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.script {
  flex: 1;
  overflow: auto;
  border: 1px dashed var(--line);
  border-radius: 10px;
  padding: 8px;
  min-height: 120px;
  background: #fcfbf9;
}
.script .empty { color: #a8a29a; font-size: 12px; padding: 14px 4px; }

.block-item {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #eef3ff;
  border-left: 4px solid var(--brand);
  border-radius: 8px;
  padding: 7px 9px;
  margin-bottom: 6px;
  font-size: 12px;
}
.block-item.ctl { background: #f2eeff; border-left-color: #7a5af8; }
.block-item.act { background: #fff1e6; border-left-color: #ef8b3c; }
.block-item .txt { flex: 1; }
.block-item input[type=number] {
  width: 56px;
  padding: 3px 6px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 12px;
}
.block-item .del {
  border: none; background: transparent; color: var(--danger);
  cursor: pointer; font-size: 14px; line-height: 1;
}
.block-item .body {
  margin: 6px 0 0 14px;
  padding-left: 10px;
  border-left: 3px solid #dcd3f7;
}
.block-item .body .block-item { margin-bottom: 5px; }
.block-item .addsub {
  border: 1px dashed #b9a9f0; background: #fff;
  color: #6a4fe0; border-radius: 6px; font-size: 11px;
  padding: 3px 8px; cursor: pointer; margin-top: 4px;
}

/* ---------- 目标运行 ---------- */
.panel-target { grid-column: 1 / -1; }

.target-modes { display: flex; gap: 6px; }
.target-modes .mode {
  border: 1px solid var(--line);
  background: #f7f5f0;
  color: #555b66;
  border-radius: 20px;
  padding: 5px 14px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}
.target-modes .mode.active {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
}

.target-body {
  display: grid;
  grid-template-columns: 340px 1fr;
  gap: 14px;
  min-height: 0;
}
.target-left { display: flex; flex-direction: column; }

.conn { display: flex; align-items: center; gap: 8px; font-size: 12px; color: #4b515c; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: #b9b4aa; flex: none; }
.dot.ok { background: var(--ok); box-shadow: 0 0 0 3px rgba(18, 161, 80, .15); }
.dot.warn { background: var(--warn); box-shadow: 0 0 0 3px rgba(224, 162, 10, .15); }
.dot.err { background: var(--danger); box-shadow: 0 0 0 3px rgba(217, 69, 61, .15); }

.target-actions { display: flex; gap: 8px; margin: 12px 0 4px; }

.sub-title { margin: 12px 0 6px; font-size: 12px; color: var(--muted); font-weight: 600; }

.scenario-row { display: flex; flex-wrap: wrap; gap: 6px; }
.scenario-row button {
  border: 1px solid #cfe0ff;
  background: #f2f7ff;
  color: var(--brand-dark);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 12px;
  cursor: pointer;
}
.scenario-row button:hover { background: #e6f0ff; }
.scenario-row button:disabled { opacity: .5; cursor: not-allowed; }

.cmd-row { display: flex; gap: 8px; }
.cmd-row input {
  flex: 1;
  min-width: 0;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 12px;
  color: var(--ink);
  background: #fff;
}
.cmd-row input:disabled { background: #f4f2ee; color: #a5a09a; }

.target-right { display: flex; flex-direction: column; min-height: 0; }
.log-head {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 6px;
}
.log-head .auto { display: flex; align-items: center; gap: 4px; margin-left: auto; cursor: pointer; }
.log-head .auto input { accent-color: var(--brand); }

.serial-log {
  margin: 0;
  flex: 1;
  min-height: 200px;
  max-height: 280px;
  overflow: auto;
  background: var(--scene-bg);
  color: #c9d7ea;
  border-radius: 10px;
  padding: 10px 12px;
  font-family: "JetBrains Mono", Consolas, "Courier New", monospace;
  font-size: 12px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-all;
}

.flash-box {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px dashed var(--line);
}
.flash-info { font-size: 12px; color: var(--muted); margin-bottom: 8px; line-height: 1.6; }
.flash-log {
  margin: 8px 0 0;
  max-height: 160px;
  overflow: auto;
  background: #fcfbf9;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  font-family: "JetBrains Mono", Consolas, "Courier New", monospace;
  font-size: 11px;
  line-height: 1.55;
  white-space: pre-wrap;
  color: #4b515c;
}

/* ---------- C 代码 ---------- */
.panel-code { grid-column: 1 / -1; }
.code {
  margin: 0;
  max-height: 300px;
  overflow: auto;
  background: var(--scene-bg);
  color: #d7e3f4;
  border-radius: 10px;
  padding: 12px 14px;
  font-family: "JetBrains Mono", Consolas, "Courier New", monospace;
  font-size: 12px;
  line-height: 1.65;
  white-space: pre;
  tab-size: 4;
}

/* ---------- 控制栏 ---------- */
.controlbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 16px;
  margin-bottom: 14px;
}
.tip { color: var(--muted); font-size: 12px; }
.tip.err { color: var(--danger); }
.tip.ok { color: var(--ok); }
.actions { display: flex; gap: 8px; }

button.primary, button.secondary, button.ghost {
  border-radius: 9px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid transparent;
  padding: 8px 16px;
}
button.primary { background: var(--brand); color: #fff; }
button.primary:hover { background: var(--brand-dark); }
button.primary:disabled { background: #9db6f3; cursor: not-allowed; }
button.secondary { background: #f1efe9; color: #43474f; border-color: var(--line); }
button.secondary:hover { background: #e9e6de; }
button.ghost {
  background: transparent;
  color: var(--brand);
  border: 1px solid #cfe0ff;
  padding: 5px 10px;
  font-size: 12px;
}
button.ghost:hover { background: #f2f7ff; }

/* ---------- 弹窗 ---------- */
dialog {
  border: none;
  border-radius: 14px;
  padding: 22px 24px;
  max-width: 460px;
  box-shadow: 0 18px 50px rgba(20, 26, 40, .22);
  color: var(--ink);
}
dialog::backdrop { background: rgba(16, 24, 40, .45); }
dialog h3 { margin: 0 0 10px; font-size: 16px; }
dialog p { line-height: 1.75; color: #3c424c; margin: 8px 0; }
dialog .muted { color: var(--muted); font-size: 12px; }
dialog form { margin-top: 14px; text-align: right; }

@media (max-width: 1180px) {
  .grid { grid-template-columns: 1fr 1fr; }
  .panel-scene { grid-column: span 2; }
}
@media (max-width: 900px) {
  .target-body { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .grid { grid-template-columns: 1fr; }
  .panel-scene { grid-column: auto; }
}

/* ---------- 登录门禁 ---------- */
body.auth-locked { overflow: hidden; }
.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(800px 360px at 12% -8%, rgba(47, 109, 246, 0.12), transparent 62%),
    radial-gradient(700px 340px at 92% 0%, rgba(18, 161, 80, 0.1), transparent 62%),
    var(--bg);
}
.auth-card {
  width: 100%;
  max-width: 380px;
  padding: 26px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 24px 60px -30px rgba(35, 38, 43, 0.45);
}
.auth-brand { display: flex; align-items: center; gap: 10px; margin-bottom: 18px; }
.auth-logo {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  background: #eef4ff;
  color: var(--brand);
  font-size: 20px;
}
.auth-card h1 { margin: 0; font-size: 16px; }
.auth-card p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }
.auth-card label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 12px; }
.auth-card input,
.auth-card select {
  width: 100%;
  margin-top: 6px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 14px;
  background: #fff;
  color: var(--ink);
}
.auth-card button.primary { width: 100%; padding: 10px; border-radius: 8px; font-size: 14px; }
.auth-error {
  background: #fdecea;
  color: var(--danger);
  border: 1px solid #f5c2be;
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 12px;
  margin-bottom: 12px;
}
.auth-hint {
  margin-top: 14px;
  padding: 10px 12px;
  background: #f4f8ff;
  border: 1px solid #dbe7ff;
  border-radius: 8px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.7;
}
.auth-hint code { color: var(--brand); }
.auth-tip { margin-top: 10px; font-size: 11px; color: var(--muted); }
.auth-user { font-size: 12px; padding: 4px 10px; white-space: nowrap; }
