:root {
  --bg: #f6f5f1;
  --panel: #ffffff;
  --ink: #1d1e22;
  --sub: #6b6c72;
  --line: #e2e0d8;
  --grid: #e9e6dc;
  --grid5: #d6d2c4;
  --accent: #2f6fe4;
  --accent-ink: #ffffff;
  --hi: #e5484d;
  --ok: #1f9d55;
  --sel: #2f6fe4;
  --wire: #2b2d33;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.12);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16171a;
    --panel: #212226;
    --ink: #e9e9ec;
    --sub: #9c9ca4;
    --line: #34353b;
    --grid: #232428;
    --grid5: #303137;
    --accent: #6b9bff;
    --accent-ink: #0d1424;
    --hi: #ff6369;
    --ok: #3fcf7f;
    --sel: #6b9bff;
    --wire: #d7d8de;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", "Malgun Gothic", sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }

.top {
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 6px) 12px 6px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 800; letter-spacing: -0.02em; font-size: 16px; }
.tabs { display: flex; background: var(--bg); border-radius: 10px; padding: 3px; }
.tab { padding: 6px 12px; border-radius: 8px; font-weight: 600; color: var(--sub); white-space: nowrap; }
.tab.on { background: var(--panel); color: var(--ink); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); }
.grow { flex: 1; }
.run { background: var(--accent); color: var(--accent-ink); padding: 7px 14px; border-radius: 10px; font-weight: 700; white-space: nowrap; }
.run.on { background: var(--hi); color: #fff; }
.icon { width: 36px; height: 36px; border-radius: 10px; font-size: 18px; }
.icon:hover { background: var(--bg); }

.body { flex: 1; display: flex; min-height: 0; }
.palette {
  width: 300px; display: flex; flex-direction: column; background: var(--panel); border-right: 1px solid var(--line); min-height: 0;
}
.psearch {
  margin: 8px 8px 6px; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--bg); color: var(--ink); font: inherit; font-size: 13px;
}
.pbody { flex: 1; display: flex; min-height: 0; }
.prail { width: 76px; flex: none; overflow-y: auto; border-right: 1px solid var(--line); padding: 4px 4px 8px; }
.pcat {
  display: flex; flex-direction: column; align-items: center; gap: 2px; width: 100%;
  padding: 7px 2px; border-radius: 10px; color: var(--sub); font-size: 11px; line-height: 1.2; text-align: center;
}
.pcat b { font-size: 18px; line-height: 22px; color: var(--ink); font-weight: 700; }
.pcat:hover { background: var(--bg); }
.pcat.on { background: color-mix(in srgb, var(--accent) 16%, transparent); color: var(--accent); }
.pcat.on b { color: var(--accent); }
.plist { flex: 1; overflow-y: auto; padding: 4px 6px 10px; min-width: 0; }
.palette h4 { margin: 6px 4px 6px; font-size: 12px; color: var(--sub); font-weight: 700; }
.pitem {
  display: flex; align-items: center; gap: 8px; width: 100%; text-align: left;
  padding: 5px 6px; border-radius: 8px; font-size: 13px;
}
.pitem:hover { background: var(--bg); }
.pitem.on { background: color-mix(in srgb, var(--accent) 16%, transparent); }
.pitem canvas { width: 44px; height: 28px; flex: none; }
.stage { flex: 1; position: relative; min-width: 0; }
#cv { position: absolute; left: 0; right: 0; bottom: 0; top: 38px; width: 100%; height: calc(100% - 38px); touch-action: none; display: block; }
.sheetbar {
  position: absolute; left: 0; right: 0; top: 0; height: 38px; display: flex; align-items: center; gap: 4px;
  padding: 0 10px; overflow-x: auto; border-bottom: 1px solid var(--line); background: var(--panel); scrollbar-width: none;
}
.sheetbar .sh { flex: none; padding: 5px 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); font-weight: 600; font-size: 13px; color: var(--sub); }
.sheetbar .sh.on { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.sheetbar .sh.add { border-style: dashed; }
.sheetbar .grow { flex: 1; }
.sheetbar .shlabel { font-size: 13px; font-weight: 700; color: var(--sub); white-space: nowrap; }
.sheetbar .sh.mode { border-color: var(--accent); color: var(--accent); }
.sheetbar .sh.mode.on { background: var(--accent); color: var(--accent-ink); }
.boardp { position: absolute; left: 0; right: 0; bottom: 0; top: 38px; display: flex; flex-direction: column; background: var(--bg); z-index: 6; }
.boardp[hidden] { display: none; }
.btools { display: flex; gap: 4px; padding: 6px 8px; overflow-x: auto; border-bottom: 1px solid var(--line); background: var(--panel); flex: none; scrollbar-width: none; }
.bt { flex: none; padding: 6px 10px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); font-weight: 600; font-size: 13px; color: var(--ink); }
.bt.on { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.bt[hidden] { display: none; }
.bstat { margin-left: auto; font-size: 12.5px; color: var(--sub); white-space: nowrap; align-self: center; padding: 0 6px; }
.bstat.ok { color: var(--ok); font-weight: 700; }
#bcv { flex: 1; width: 100%; min-height: 0; touch-action: none; display: block; }
body.boardmode .palette, body.boardmode .tools { display: none !important; }
.sheet-body ul { margin: 4px 0 8px; padding-left: 18px; font-size: 13px; }
.hint {
  position: absolute; left: 12px; top: 48px; max-width: calc(100% - 24px);
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 6px 10px;
  color: var(--sub); font-size: 12.5px; pointer-events: none; box-shadow: var(--shadow);
}
.hint:empty { display: none; }
.warn {
  position: absolute; left: 12px; right: 12px; bottom: 12px; max-height: 30%; overflow: auto;
  background: color-mix(in srgb, var(--hi) 12%, var(--panel)); border: 1px solid color-mix(in srgb, var(--hi) 40%, transparent);
  border-radius: 12px; padding: 8px 12px; font-size: 13px;
}
.warn div + div { margin-top: 4px; }
.props {
  position: absolute; right: 12px; top: 48px; width: 260px; max-width: calc(100% - 24px);
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 12px; box-shadow: var(--shadow);
}
.props h3 { margin: 0 0 8px; font-size: 15px; }
.props label { display: block; font-size: 12px; color: var(--sub); margin: 8px 0 3px; }
.props input, .props select {
  width: 100%; padding: 8px 10px; border-radius: 9px; border: 1px solid var(--line);
  background: var(--bg); color: var(--ink); font: inherit;
}
.props .row { display: flex; gap: 6px; margin-top: 10px; }
.props .row button, .btn {
  flex: 1; padding: 8px 10px; border-radius: 9px; background: var(--bg); border: 1px solid var(--line); font-weight: 600;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.props .meas { margin-top: 8px; font-variant-numeric: tabular-nums; font-weight: 700; }

.tools {
  display: flex; gap: 4px; justify-content: center;
  padding: 6px 8px calc(env(safe-area-inset-bottom) + 6px);
  background: var(--panel); border-top: 1px solid var(--line);
}
.tool { display: flex; flex-direction: column; align-items: center; gap: 1px; min-width: 56px; padding: 4px 6px; border-radius: 10px; font-size: 11.5px; color: var(--sub); }
.tool span { font-size: 18px; line-height: 22px; color: var(--ink); }
.tool.on { background: color-mix(in srgb, var(--accent) 16%, transparent); color: var(--accent); }
.mobile-only { display: none; }

.sheet { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.35); display: flex; align-items: center; justify-content: center; z-index: 20; padding: 16px; }
.sheet[hidden] { display: none; }
.sheet-card { background: var(--panel); border-radius: 16px; width: min(640px, 100%); max-height: 88vh; display: flex; flex-direction: column; box-shadow: var(--shadow); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 6px 16px; }
.sheet-body { overflow: auto; padding: 4px 16px 16px; }
.sheet-body table { border-collapse: collapse; width: 100%; font-size: 13px; }
.sheet-body th, .sheet-body td { border-bottom: 1px solid var(--line); padding: 6px 6px; text-align: left; vertical-align: top; }
.sheet-body th { color: var(--sub); font-weight: 600; font-size: 12px; }
.sheet-body h4 { margin: 14px 0 6px; }
.sheet-body .mlist button { display: block; width: 100%; text-align: left; padding: 10px 10px; border-radius: 10px; }
.sheet-body .mlist button:hover { background: var(--bg); }
.sheet-body .mlist small { display: block; color: var(--sub); }
.sheet-body .opts { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0 10px; }
.chip { padding: 6px 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--bg); font-weight: 600; }
.chip.on { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.note { color: var(--sub); font-size: 12.5px; margin: 6px 0; }
.sw { display: flex; flex-wrap: wrap; gap: 6px; margin: 4px 0 8px; }
.sw button, .sw .pick {
  width: 28px; height: 28px; border-radius: 50%; border: 2px solid var(--line); position: relative;
  display: inline-flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 800; color: var(--sub); cursor: pointer;
}
.sw button.auto { background: var(--bg); }
.sw button.on, .sw .pick.on { border-color: var(--accent); box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 35%, transparent); }
.sw .pick { background: conic-gradient(#e5484d, #f0a500, #1f9d55, #00a3b4, #2f6fe4, #8e4ec6, #e5484d); color: #fff; text-shadow: 0 0 2px #000; overflow: hidden; }
.sw .pick input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.chk { display: flex; align-items: flex-start; gap: 8px; padding: 6px 0; font-size: 14px; cursor: pointer; color: var(--ink); }
.chk input { margin-top: 3px; width: 16px; height: 16px; flex: none; }
.chk small { display: block; color: var(--sub); font-size: 12px; }
.props .chk { font-size: 13px; display: flex; color: var(--ink); margin: 6px 0 0; }
.sw .pick { margin: 0; }
.lk { margin-top: 6px; border-top: 1px solid var(--line); padding-top: 6px; }
.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 80px); transform: translateX(-50%);
  background: var(--ink); color: var(--bg); padding: 8px 14px; border-radius: 10px; z-index: 30; font-size: 13px; max-width: 90vw;
}
.tt td.h { color: var(--hi); font-weight: 800; }

@media (max-width: 760px) {
  .palette {
    position: fixed; left: 0; right: 0; bottom: calc(env(safe-area-inset-bottom) + 62px); top: auto; width: auto;
    height: 52vh; border-right: 0; border-top: 1px solid var(--line); z-index: 10; box-shadow: var(--shadow);
    display: none; border-radius: 16px 16px 0 0;
  }
  .palette.open { display: flex; }
  .pbody { flex-direction: column; }
  .prail { width: auto; display: flex; gap: 4px; overflow-x: auto; overflow-y: hidden; border-right: 0; border-bottom: 1px solid var(--line); padding: 2px 8px 6px; flex: none; }
  .pcat { flex-direction: row; width: auto; white-space: nowrap; padding: 6px 10px; border-radius: 999px; border: 1px solid var(--line); font-size: 12.5px; gap: 5px; }
  .pcat b { font-size: 14px; line-height: 16px; }
  .palette .grp { display: flex; flex-wrap: wrap; gap: 4px; }
  .palette .pitem { width: auto; flex-direction: column; gap: 2px; font-size: 11.5px; padding: 6px 4px; min-width: 76px; max-width: 92px; text-align: center; }
  .mobile-only { display: flex; }
  .brand { display: none; }
  .tab { padding: 6px 9px; font-size: 13px; }
  .run { padding: 7px 10px; }
  .props { top: auto; bottom: 10px; right: 10px; left: 10px; width: auto; max-height: 50%; overflow: auto; }
  .tool { min-width: 0; flex: 1; padding: 4px 2px; font-size: 10.5px; }
  .bstat { position: absolute; left: 10px; right: 10px; bottom: calc(env(safe-area-inset-bottom) + 10px); white-space: normal; text-align: center;
    background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 6px 10px; box-shadow: var(--shadow); z-index: 2; }
  .bstat:empty { display: none; }
}
@media print {
  body * { visibility: hidden !important; }
}
