/* ===== 全局 ===== */
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
  width: 100%; height: 100%;
  background: #1a1d22;
  color: #d8dde3;
  font-family: -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  overflow: hidden;
}
#app {
  display: flex;
  flex-direction: column;
  width: 100vw; height: 100vh;
}

/* ===== 顶栏 ===== */
#topBar {
  height: 44px;
  min-height: 44px;
  background: #22262c;
  border-bottom: 1px solid #33383f;
  display: flex;
  align-items: center;
  padding: 0 14px;
  gap: 16px;
}
#topBar .brand {
  font-size: 14px;
  font-weight: 600;
  color: #f0f3f7;
}
#topBar .tag {
  display: inline-block;
  background: #3a6ea8;
  color: #fff;
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 3px;
  vertical-align: middle;
  margin-left: 4px;
}
.mode-tabs {
  display: flex;
  gap: 4px;
}
.tab {
  background: transparent;
  border: 1px solid #3a4049;
  color: #9aa3ad;
  padding: 4px 14px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
}
.tab.active {
  background: #3a6ea8;
  color: #fff;
  border-color: #3a6ea8;
}
.top-actions {
  margin-left: auto;
  display: flex;
  gap: 6px;
}
.top-actions button {
  background: #2c3138;
  border: 1px solid #3a4049;
  color: #d8dde3;
  padding: 5px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
}
.top-actions button:hover { background: #343a42; }

/* ===== 主体三栏 ===== */
#app {
  flex-direction: column;
}
.body-row {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ===== 左侧元件库 ===== */
#library {
  width: 210px;
  min-width: 210px;
  background: #22262c;
  border-right: 1px solid #33383f;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
}
#library h2, #levelPanel h2 {
  font-size: 14px;
  color: #f0f3f7;
  font-weight: 600;
}
#library .hint {
  font-size: 11px;
  color: #8a929c;
  line-height: 1.5;
}
.lib-list {
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.lib-item {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #2c3138;
  border: 1px solid #3a4049;
  color: #d8dde3;
  padding: 6px 8px;
  border-radius: 5px;
  cursor: grab;
  font-size: 12px;
  text-align: left;
  transition: background .12s, border-color .12s;
}
/* 元件栏分组 */
.lib-group { display: flex; flex-direction: column; gap: 5px; margin-bottom: 4px; }
.lib-group-title {
  font-size: 11px;
  color: #7d8590;
  border-bottom: 1px dashed #333941;
  padding-bottom: 3px;
  margin: 2px 0 5px;
  letter-spacing: 1px;
}
.lib-item:hover { background: #343a42; border-color: #4a525c; }
.lib-item:active { cursor: grabbing; }

/* 需登录元件：未登录时灰色不可用 */
.lib-item.lib-locked {
  opacity: .45;
  filter: grayscale(1);
  cursor: not-allowed;
  border-style: dashed;
}
.lib-item.lib-locked:hover { background: #2c3138; border-color: #3a4049; }
.lib-item .lib-lock {
  margin-left: auto;
  font-size: 10px;
  color: #8a929c;
  border: 1px solid #4a525c;
  border-radius: 8px;
  padding: 0 5px;
  white-space: nowrap;
}

/* 元件小图标 */
.sw {
  display: inline-block;
  width: 22px; height: 14px;
  border-radius: 2px;
  flex-shrink: 0;
}
.sw-battery { background: linear-gradient(to right, #d9534f 50%, #5bc0de 50%); }

/*.sw-switch  { background: #6c757d; position: relative; }*/
.sw-switch {
  background: url('/uploads/icons/0942.png') center / contain no-repeat, #fff;
}
.sw-switch::after {
  content: ''; position: absolute; left: 2px; top: -3px;
  width: 16px; height: 2px; background: #d8dde3; transform: rotate(-20deg);
}
.sw-ign { background: radial-gradient(circle, #f39c12 30%, #6c757d 70%); }
.sw-fuse { background: repeating-linear-gradient(45deg, #888 0 3px, #ccc 3px 6px); }
.sw-relay { background: linear-gradient(to right, #9b59b6 50%, #16a085 50%); }
.sw-starter { background: radial-gradient(circle, #e67e22 30%, #6c757d 70%); border-radius: 50%; }
.sw-pump { background: radial-gradient(circle, #2ecc71 30%, #6c757d 70%); }
.sw-coil { background: repeating-radial-gradient(circle, #e74c3c 0 2px, #6c757d 2px 4px); }
.sw-lamp { background: radial-gradient(circle, #f1c40f 30%, #6c757d 70%); }
.sw-ground { background: linear-gradient(to bottom, #888 0 3px, transparent 3px); }
.sw-custom {
  background: repeating-linear-gradient(45deg, #7f8c99 0 3px, #4a5568 3px 6px);
}
/* 自定义元件小图标 */
.sw-diyen {
  background: url('/uploads/icons/en.png') center / contain no-repeat, #fff;
}
.sw-diyoil{
  background: url('/uploads/icons/oil.png') center / contain no-repeat, #fff;
}
.sw-diy1670{
  background: url('/uploads/icons/1670.png') center / contain no-repeat, #fff;
}
.sw-diy47{
  background: url('/uploads/icons/47.png') center / contain no-repeat, #fff;
}
.sw-diy510{
  background: url('/uploads/icons/510.png') center / contain no-repeat, #fff;
}
.sw-diy2228{
  background: url('/uploads/icons/2228.png') center / contain no-repeat, #fff;
}
.sw-diy2229{
  background: url('/uploads/icons/2229.png') center / contain no-repeat, #fff;
}
.sw-diyact{
  background: url('/uploads/icons/act.png') center / contain no-repeat, #fff;
}
.sw-diy2244{
  background: url('/uploads/icons/2244.png') center / contain no-repeat, #fff;
}
.sw-diyecu{
  background: url('/uploads/icons/ecu.png') center / contain no-repeat, #fff;
}
.sw-diy0927{
  background: url('/uploads/icons/0927.png') center / contain no-repeat, #fff;
}
.sw-diy0933{
  background: url('/uploads/icons/0933.png') center / contain no-repeat, #fff;
}
.sw-diy0934{
  background: url('/uploads/icons/0934.png') center / contain no-repeat, #fff;
}
.sw-diy0936{
  background: url('/uploads/icons/0936.png') center / contain no-repeat, #fff;
}
.sw-diy0939{
  background: url('/uploads/icons/0939.png') center / contain no-repeat, #fff;
}
.sw-diy0945{
  background: url('/uploads/icons/0945.png') center / contain no-repeat, #fff;
}
.sw-diy0947{
  background: url('/uploads/icons/0947.png') center / contain no-repeat, #fff;
}
.sw-diy0948{
  background: url('/uploads/icons/0948.png') center / contain no-repeat, #fff;
}
.help {
  margin-top: auto;
  font-size: 11px;
  color: #8a929c;
  background: #1c2025;
  border: 1px solid #2c3138;
  border-radius: 5px;
  padding: 8px 10px;
  line-height: 1.7;
}
.help h3 { color: #c8cdd4; font-size: 12px; margin-bottom: 4px; }
.help ul { padding-left: 16px; }
.help kbd {
  background: #333; border: 1px solid #555; border-radius: 3px;
  padding: 0 4px; font-size: 10px;
}

#statusBar {
  font-size: 11px;
  color: #9aa3ad;
  background: #1c2025;
  border-radius: 4px;
  padding: 6px 8px;
  min-height: 28px;
}

/* ===== 关卡面板 ===== */
#levelPanel {
  width: 260px;
  min-width: 260px;
  background: #22262c;
  border-right: 1px solid #33383f;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
}
#levelPanel.hidden { display: none; }
.hidden { display: none !important; }

.level-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}
.level-card {
  background: #2c3138;
  border: 1px solid #3a4049;
  border-radius: 5px;
  padding: 8px;
  cursor: pointer;
  font-size: 11px;
  text-align: center;
  transition: background .12s;
}
.level-card:hover { background: #343a42; }
.level-card.current { border-color: #3a6ea8; background: #2a3a52; }
.level-card.locked { opacity: 0.45; cursor: not-allowed; }
/* 未登录时的「需登录」关卡：仍然置灰，但可点击（点击弹出登录框） */
.level-card.login-locked { position: relative; cursor: pointer; }
.level-card.login-locked .lv-num { color: #8a9099; }
.level-card .lv-badge {
  position: absolute;
  top: 3px;
  right: 3px;
  font-size: 9px;
  line-height: 1;
  padding: 2px 4px;
  border-radius: 3px;
  background: #3a4049;
  color: #c8cdd4;
}
.level-card .lv-num { font-size: 14px; font-weight: 700; color: #7fd0ff; }
.level-card .lv-stars { color: #f1c40f; font-size: 10px; margin-top: 2px; }
.level-card .lv-name { color: #c8cdd4; margin-top: 2px; }
/* 难度（后台 difficulty 1~5）：用圆点，和上面的成绩星区分开 */
.level-card .lv-diff {
  display: flex; align-items: center; justify-content: center; gap: 2px;
  margin-top: 3px; font-size: 9px; line-height: 1; color: #6c757d;
}
.level-card .lv-diff b { font-weight: 400; color: #7fd0ff; letter-spacing: 1px; }

.level-info {
  background: #1c2025;
  border: 1px solid #2c3138;
  border-radius: 5px;
  padding: 10px;
  font-size: 12px;
  line-height: 1.6;
}
.level-info h3 { color: #f0f3f7; font-size: 13px; margin-bottom: 6px; }
.lv-diff-line { color: #9aa5b1; font-size: 11px; margin: 0 0 6px; }
.lv-diff-line b { color: #7fd0ff; font-weight: 400; letter-spacing: 1px; }
.lv-diff-line:empty { display: none; }
.lv-intro { color: #c8cdd4; margin-bottom: 6px; }
.lv-tip { color: #f39c12; font-size: 11px; margin-bottom: 8px; }
.lv-actions { display: flex; gap: 6px; margin-bottom: 8px; }
.lv-actions button {
  flex: 1;
  padding: 6px;
  border-radius: 4px;
  border: 1px solid #3a4049;
  background: #2c3138;
  color: #d8dde3;
  cursor: pointer;
  font-size: 12px;
}
.lv-actions button.primary {
  background: #27ae60;
  border-color: #27ae60;
  color: #fff;
}
.lv-actions button:hover { opacity: 0.9; }

.score-ring {
  text-align: center;
  margin: 8px 0;
}
#scoreNum {
  font-size: 36px;
  font-weight: 700;
  color: #2ecc71;
}
.score-unit { font-size: 14px; color: #8a929c; margin-left: 2px; }

.result-msg {
  font-size: 12px;
  color: #c8cdd4;
  margin-bottom: 8px;
}
.pin-feedback {
  font-size: 11px;
  max-height: 180px;
  overflow-y: auto;
  border-top: 1px solid #2c3138;
  padding-top: 6px;
}
.pin-feedback .pin-row {
  padding: 2px 0;
  display: flex;
  gap: 6px;
}
.pin-feedback .pin-row.ok { color: #2ecc71; }
.pin-feedback .pin-row.missing { color: #f39c12; }
.pin-feedback .pin-row.wrong { color: #e74c3c; }

.fault-book {
  background: #1c2025;
  border: 1px solid #2c3138;
  border-radius: 5px;
  padding: 8px;
  font-size: 11px;
}
.fault-book h3 {
  font-size: 12px;
  color: #c8cdd4;
  margin-bottom: 6px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.fault-book .mini {
  background: transparent;
  border: 1px solid #3a4049;
  color: #8a929c;
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 3px;
  cursor: pointer;
}
.fault-list .fault-item {
  padding: 4px 0;
  border-bottom: 1px solid #2c3138;
  color: #e74c3c;
}
.fault-list .muted { color: #6c757d; }

/* ===== 画布区 ===== */
#canvasWrap {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: #14171b;
}
#canvas {
  display: block;
  width: 100%; height: 100%;
  touch-action: none;
  cursor: default;
}

/* 主体行（顶栏下方） */
.body-row { display: flex; flex: 1; overflow: hidden; }

/* ===== H5 / 移动端适配（同一套页面，无需单独 H5 目录） ===== */
.m-only { display: none; }

.drawer-mask {
  position: fixed; inset: 0; z-index: 88;
  background: rgba(0, 0, 0, .5);
  display: none;
}
.drawer-mask.show { display: block; }

.m-toast {
  position: fixed; left: 50%; bottom: calc(64px + env(safe-area-inset-bottom));
  transform: translate(-50%, 10px);
  max-width: 86vw; padding: 8px 14px;
  background: rgba(24, 28, 33, .95); color: #e8edf3;
  border: 1px solid #3a4049; border-radius: 18px;
  font-size: 12px; line-height: 1.5; text-align: center;
  z-index: 96; opacity: 0; pointer-events: none;
  transition: opacity .18s ease, transform .18s ease;
}
.m-toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 860px) {
  .m-only { display: inline-block; }
  #app { height: 100dvh; }

  /* 顶栏改多行：第一行品牌 + 抽屉开关，操作按钮行可横向滑动不裁切 */
  #topBar {
    height: auto; min-height: 0;
    flex-wrap: wrap; padding: 6px 10px; gap: 6px 8px;
  }
  #topBar .brand { font-size: 13px; margin-right: auto; }
  #topBar .brand .tag { display: none; }
  #topBar .m-only {
    background: #2c3138; border: 1px solid #3a4049; color: #d8dde3;
    padding: 7px 11px; border-radius: 6px; font-size: 12px; cursor: pointer;
  }
  #topBar .m-only.active { background: #3a6ea8; border-color: #3a6ea8; color: #fff; }
  .mode-tabs { width: 100%; }
  .mode-tabs .tab { flex: 1; padding: 8px 0; font-size: 13px; }
  .top-actions {
    width: 100%; margin-left: 0; gap: 6px;
    overflow-x: auto; -webkit-overflow-scrolling: touch; padding-bottom: 2px;
  }
  .top-actions::-webkit-scrollbar { display: none; }
  .top-actions button { flex: 0 0 auto; padding: 7px 10px; font-size: 12px; }
  .top-actions .user-box { flex: 0 0 auto; margin-left: auto; }
  .user-box .user-name { max-width: 84px; }

  /* 侧栏改抽屉：position:fixed 后不再占据文档流，画布自动铺满 */
  #library, #levelPanel {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 90;
    width: 86vw; max-width: 340px; min-width: 0;
    padding-top: calc(12px + env(safe-area-inset-top));
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
    border-right: 1px solid #33383f;
    box-shadow: 8px 0 32px rgba(0, 0, 0, .6);
    transform: translateX(-102%);
    transition: transform .22s ease;
    overscroll-behavior: contain;
  }
  #library.open, #levelPanel.open { transform: translateX(0); }

  /* 抽屉里精简说明文字，把空间留给元件与关卡 */
  #library .hint, #library .help { display: none; }

  /* 画布铺满顶栏以下的全部空间 */
  #canvasWrap { flex: 1 1 auto; width: 100%; height: auto; }

  /* 触摸目标放大 */
  .lib-item { padding: 11px 12px; font-size: 14px; }
  .sw { width: 26px; height: 16px; }
  .level-card { padding: 12px 8px; }
  .lv-actions { flex-wrap: wrap; }
  .lv-actions button { flex: 1 1 auto; padding: 11px 12px; font-size: 14px; }
  #statusBar { font-size: 12px; }
}

/* 手机横屏（宽度够）：顶栏压成一行，进一步给画布让高度 */
@media (min-width: 600px) and (max-width: 900px) and (max-height: 520px) {
  #topBar { flex-wrap: nowrap; padding: 4px 8px; gap: 4px 8px; }
  #topBar .brand { font-size: 12px; }
  #btnLibToggle { margin-left: 0; padding: 5px 8px; }
  #btnPanelToggle { padding: 5px 8px; }
  .mode-tabs { width: auto; }
  .mode-tabs .tab { padding: 6px 10px; font-size: 12px; }
  .top-actions { width: auto; margin-left: auto; }
  .top-actions button { padding: 5px 8px; font-size: 11px; }
  #library .hint, #library .help { display: none; }
}

/* 抽屉关闭按钮（仅移动端） */
.drawer-close { display: none; }
@media (max-width: 860px) {
  .drawer-close {
    display: flex; align-items: center; justify-content: center;
    position: absolute; z-index: 2;
    top: calc(8px + env(safe-area-inset-top)); right: 8px;
    width: 34px; height: 34px; padding: 0;
    border-radius: 50%; font-size: 18px; line-height: 1;
    background: #2c3138; border: 1px solid #3a4049; color: #d8dde3;
  }
  #library h2, #levelPanel h2 { padding-right: 40px; }
}

/* 画布缩放按钮（PC + 移动端都显示；PC 另支持鼠标滚轮缩放） */
.zoom-bar {
  position: absolute; left: auto; right: 10px;
  bottom: calc(12px + env(safe-area-inset-bottom));
  display: flex; gap: 6px; z-index: 40;
  max-width: calc(100% - 20px);
}
.zoom-bar button {
  height: 32px; min-width: 32px; padding: 0 10px;
  border-radius: 8px; font-size: 14px; line-height: 1;
  background: rgba(44, 49, 56, .94);
  border: 1px solid #3a4049; color: #d8dde3;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .4);
  cursor: pointer;
}
.zoom-bar #btnZoomFit { font-size: 12px; }
.zoom-bar button:hover { border-color: #3a6ea8; }
.zoom-bar button:active { background: #3a6ea8; border-color: #3a6ea8; color: #fff; }
/* 移动端触摸目标放大 */
@media (max-width: 860px) {
  .zoom-bar button {
    height: 40px; min-width: 40px;
    border-radius: 10px; font-size: 16px;
  }
}

/* 删除选中对象：触屏没有 Delete 键，提供画布浮动删除按钮（有选中时显示） */
.del-fab {
  position: absolute; right: 10px;
  bottom: calc(62px + env(safe-area-inset-bottom)); /* 避开移动端缩放条 */
  z-index: 41;
  height: 40px; min-width: 56px; padding: 0 14px;
  border-radius: 10px; font-size: 13px; line-height: 1;
  background: rgba(167, 46, 46, .95);
  border: 1px solid #a72e2e; color: #fff;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .4);
  cursor: pointer;
}
.del-fab:active { background: #e05252; border-color: #e05252; }

/* 重命名选中元件：叠在「删除」按钮上方（仅本次会话，刷新恢复） */
.rename-fab {
  position: absolute; right: 10px;
  bottom: calc(110px + env(safe-area-inset-bottom));
  z-index: 41;
  height: 40px; min-width: 56px; padding: 0 14px;
  border-radius: 10px; font-size: 13px; line-height: 1;
  background: rgba(45, 85, 138, .95);
  border: 1px solid #2d558a; color: #fff;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .4);
  cursor: pointer;
}
.rename-fab:active { background: #3a6ea8; border-color: #3a6ea8; }

/* 桌面端：避开右下角缩放条 */
@media (min-width: 861px) {
  .del-fab { bottom: 54px; height: 32px; border-radius: 8px; }
  .rename-fab { bottom: 94px; height: 32px; border-radius: 8px; }
}
