* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", "Yu Gothic UI", sans-serif;
  background: #0e1117;
  color: #e6edf3;
  font-size: 14px;
}

header {
  padding: 12px 20px;
  background: #161b22;
  border-bottom: 1px solid #30363d;
  position: sticky;
  top: 0;
  z-index: 10;
}
header h1 {
  margin: 0 0 8px 0;
  font-size: 18px;
  font-weight: 600;
}
header .sub {
  color: #8b949e;
  font-weight: 400;
  font-size: 13px;
}
.conn-bar {
  display: flex;
  align-items: center;
  gap: 10px;
}
.conn-bar .hint {
  color: #8b949e;
  font-size: 12px;
  margin-left: auto;
}
.link-btn {
  color: #58a6ff;
  text-decoration: none;
  font-size: 13px;
  padding: 4px 10px;
  border: 1px solid #30363d;
  border-radius: 4px;
}
.link-btn:hover { background: #21262d; }
.pressure-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  cursor: pointer;
  user-select: none;
}
.pressure-toggle input:checked + span { color: #79c0ff; font-weight: 600; }

.pressure-curve {
  display: flex;
  gap: 10px;
  align-items: center;
  margin: 6px 0 0 0;
  padding: 6px;
  background: #0d1117;
  border: 1px solid #30363d;
  border-radius: 4px;
}
.pressure-curve .curve-controls {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.pressure-curve label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: #8b949e;
  margin: 0;
}
.pressure-curve input[type=range] {
  flex: 1;
  min-width: 80px;
}
.pressure-curve .num {
  color: #58a6ff;
  font-family: "SF Mono", Consolas, monospace;
  min-width: 36px;
  text-align: right;
  font-size: 11px;
}
#pressureCurvePreview, #drawCurvePreview {
  background: #161b22;
  border: 1px solid #30363d;
  border-radius: 3px;
}

.dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
}
.dot.off { background: #6e7681; }
.dot.connecting { background: #d29922; box-shadow: 0 0 8px #d29922; }
.dot.on { background: #3fb950; box-shadow: 0 0 8px #3fb950; }
.dot.err { background: #f85149; box-shadow: 0 0 8px #f85149; }

main {
  padding: 16px 20px;
  display: grid;
  gap: 14px;
  grid-template-columns: 1fr;
  max-width: 1400px;
  margin: 0 auto;
}

.panel {
  background: #161b22;
  border: 1px solid #30363d;
  border-radius: 6px;
  padding: 14px 16px;
}
.panel h2 {
  margin: 0 0 12px 0;
  font-size: 14px;
  font-weight: 600;
  color: #c9d1d9;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.row {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  align-items: flex-start;
}

/* Status grid */
.status-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 10px;
}
.status-grid > div {
  background: #0d1117;
  border: 1px solid #30363d;
  border-radius: 4px;
  padding: 6px 10px;
  font-family: "SF Mono", Consolas, monospace;
}
.status-grid label {
  display: block;
  font-size: 11px;
  color: #8b949e;
  text-transform: uppercase;
}
.status-grid span {
  font-size: 15px;
  color: #58a6ff;
}

/* Jog pad */
.jog-pad {
  min-width: 260px;
  padding: 8px;
  border-radius: 6px;
  border: 2px solid transparent;
  transition: border 0.15s, background 0.15s;
}
.jog-pad.setup-mode {
  border-color: #d29922;
  background: rgba(210, 153, 34, 0.08);
}
.setup-mode-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  margin-top: 8px;
  cursor: pointer;
  user-select: none;
}
.setup-mode-toggle .setup-mode-label {
  color: #8b949e;
  transition: color 0.15s;
}
.jog-pad.setup-mode .setup-mode-label {
  color: #ffa657;
  font-weight: 600;
}
.jog-grid {
  display: grid;
  grid-template-columns: repeat(3, 70px);
  grid-template-rows: repeat(3, 60px);
  gap: 4px;
  margin-bottom: 10px;
}
.jog-grid button {
  font-size: 13px;
  font-weight: 600;
  transition: opacity 0.12s, color 0.12s, border-color 0.12s, background 0.12s;
}
.jog-grid button.clamped {
  opacity: 0.45;
  color: #6e7681;
  background: #161b22;
  border-style: dashed;
  border-color: #484f58;
}
.jog-grid button.clamped:hover {
  opacity: 0.7;
  background: #21262d;
}
.jog-grid button.at-boundary {
  opacity: 0.35;
  color: #6e7681;
  background: #161b22;
  border-style: dashed;
  border-color: #6e2c2c;
  cursor: not-allowed;
}
.jog-grid button.at-boundary::after {
  content: " 端";
  font-size: 10px;
  opacity: 0.7;
}
.jog-controls { display: flex; gap: 12px; flex-wrap: wrap; }
.jog-controls label { display: flex; align-items: center; gap: 4px; font-size: 12px; }

/* Pen & light fieldsets */
.pen-light { display: flex; gap: 10px; flex-wrap: wrap; flex: 1; min-width: 360px; }
fieldset {
  border: 1px solid #30363d;
  border-radius: 4px;
  padding: 8px 12px 10px;
  margin: 0;
  background: #0d1117;
  min-width: 180px;
  flex: 1;
}
legend {
  font-size: 12px;
  color: #8b949e;
  padding: 0 6px;
  text-transform: uppercase;
}
fieldset label {
  display: flex;
  justify-content: space-between;
  gap: 6px;
  margin: 4px 0;
  font-size: 12px;
  align-items: center;
}
fieldset input {
  width: 70px;
}

.seg-toggle { display: flex; gap: 10px; font-size: 12px; margin-bottom: 6px; }

.hint-text {
  font-size: 11px;
  color: #8b949e;
  margin: 6px 0 0 0;
  line-height: 1.6;
}
button.inline {
  padding: 1px 6px;
  font-size: 11px;
  font-family: "SF Mono", Consolas, monospace;
  background: #21262d;
}

.btn-row { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.btn-row button { flex: 1; min-width: 80px; }

/* Buttons */
button {
  background: #21262d;
  color: #e6edf3;
  border: 1px solid #30363d;
  border-radius: 4px;
  padding: 6px 12px;
  cursor: pointer;
  font-size: 13px;
  font-family: inherit;
  transition: background 0.1s, border 0.1s;
}
button:hover:not(:disabled) { background: #30363d; border-color: #8b949e; }
button:active:not(:disabled) { background: #161b22; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.primary { background: #1f6feb; border-color: #1f6feb; }
button.primary:hover:not(:disabled) { background: #388bfd; }
button.danger { background: #6e2c2c; border-color: #8a3a3a; }
button.danger:hover:not(:disabled) { background: #8a3a3a; }

input, select {
  background: #0d1117;
  color: #e6edf3;
  border: 1px solid #30363d;
  border-radius: 4px;
  padding: 4px 6px;
  font-family: inherit;
  font-size: 12px;
}
input:focus, select:focus { outline: 1px solid #1f6feb; }

hr { border: none; border-top: 1px solid #30363d; margin: 8px 0; }

/* Canvas */
.canvas-wrap { position: relative; }
#canvas {
  background: #fff;
  border: 1px solid #30363d;
  border-radius: 4px;
  cursor: crosshair;
  display: block;
  max-width: 100%;
  height: auto;
}
.canvas-overlay {
  position: absolute;
  top: 6px;
  left: 8px;
  background: rgba(0,0,0,0.55);
  color: #fff;
  padding: 2px 8px;
  border-radius: 3px;
  font-size: 11px;
  pointer-events: none;
}
.canvas-coord {
  position: absolute;
  bottom: 6px;
  right: 8px;
  background: rgba(0,0,0,0.65);
  color: #58a6ff;
  padding: 2px 8px;
  border-radius: 3px;
  font-size: 11px;
  font-family: "SF Mono", Consolas, monospace;
  pointer-events: none;
  min-width: 120px;
  text-align: center;
}
.env-display {
  background: #0d1117;
  border: 1px solid #30363d;
  border-radius: 4px;
  padding: 6px 10px;
  font-family: "SF Mono", Consolas, monospace;
  font-size: 12px;
}
.env-display > div { padding: 2px 0; }
.env-display .hint-text { margin-top: 4px; font-family: inherit; }
.env-display span { color: #58a6ff; }

/* SVG transform panel */
.svg-transform { padding: 8px 12px; }
.svg-transform legend { color: #ffa657; font-size: 11px; }
.svg-transform label {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  margin: 4px 0;
}
.svg-transform input[type=range] { flex: 1; min-width: 100px; max-width: 180px; }
.svg-transform input[type=number] { width: 80px; }

/* G-code preview */
.preview-badge {
  top: 6px;
  right: 8px;
  left: auto;
  background: rgba(31, 111, 235, 0.85);
  font-weight: 600;
}
/* キャンバス:操作群 = 3:1 横配置 */
.canvas-row {
  display: flex;
  gap: 16px;
  align-items: flex-start;
}
.canvas-row > .canvas-wrap { flex: 3; min-width: 0; }
.canvas-row > .canvas-side { flex: 1; min-width: 200px; }

/* タイムラインプレビュー - コンパクト縦並び (canvas-side内) */
.preview-controls {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 6px 0;
  padding: 8px;
  background: #0d1117;
  border: 1px solid #1f6feb;
  border-radius: 4px;
}
.preview-controls .pc-row {
  display: flex;
  gap: 6px;
  align-items: center;
}
.preview-controls .pc-row > * { flex: 1; }
.preview-controls #previewPlayBtn { flex: 0 0 auto; }
.preview-controls #previewSlider { width: 100%; }
.preview-controls #previewTimeText {
  font-family: "SF Mono", Consolas, monospace;
  font-size: 11px;
  color: #58a6ff;
  text-align: right;
  flex: 1;
}
.preview-line {
  font-family: "SF Mono", Consolas, monospace;
  font-size: 10px;
  color: #8b949e;
  background: #161b22;
  padding: 3px 6px;
  border-radius: 3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.canvas-side {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 220px;
  flex: 1;
}
.canvas-side label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  gap: 6px;
}
.canvas-side input { width: 90px; }

.progress {
  background: #0d1117;
  border: 1px solid #30363d;
  border-radius: 3px;
  height: 18px;
  position: relative;
  margin-top: 6px;
  overflow: hidden;
}
#plotBar {
  background: #1f6feb;
  height: 100%;
  width: 0%;
  transition: width 0.15s linear;
}
#plotText {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 11px;
  line-height: 18px;
  font-family: "SF Mono", Consolas, monospace;
}

/* Device settings panel */
.device-settings-controls {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-bottom: 10px;
  flex-wrap: wrap;
}
#settingsFilter {
  flex: 1;
  min-width: 200px;
  font-family: "SF Mono", Consolas, monospace;
}
.settings-stat {
  font-size: 12px;
  color: #8b949e;
}
#settingsTable {
  width: 100%;
  border-collapse: collapse;
  font-family: "SF Mono", Consolas, monospace;
  font-size: 12px;
}
#settingsTable th {
  background: #0d1117;
  border-bottom: 1px solid #30363d;
  padding: 4px 8px;
  text-align: left;
  color: #8b949e;
  font-weight: 600;
  position: sticky;
  top: 0;
}
#settingsTable td {
  padding: 3px 8px;
  border-bottom: 1px solid #1c2128;
  vertical-align: middle;
}
#settingsTable tr.highlight td { background: rgba(31, 111, 235, 0.08); }
#settingsTable tr.tmc td { color: #ffa657; }
#settingsTable tr.travel td { color: #56d364; }
#settingsTable .key { color: #79c0ff; }
#settingsTable .sym { color: #d2a8ff; }
#settingsTable .val {
  color: #c9d1d9;
  min-width: 80px;
}
#settingsTable .val input {
  width: 90px;
  font-size: 11px;
}
#settingsTable .ops button {
  padding: 2px 8px;
  font-size: 11px;
  margin-right: 4px;
}
#settingsTableWrap {
  max-height: 360px;
  overflow-y: auto;
  border: 1px solid #30363d;
  border-radius: 4px;
}

/* Console */
.console-controls {
  display: flex;
  gap: 6px;
  align-items: center;
  margin-bottom: 8px;
  flex-wrap: wrap;
}
#cmdInput {
  flex: 1;
  min-width: 280px;
  font-family: "SF Mono", Consolas, monospace;
  font-size: 13px;
  padding: 6px 8px;
}
.console-controls label {
  font-size: 12px;
  color: #8b949e;
  display: flex;
  gap: 4px;
  align-items: center;
}

.log {
  background: #0d1117;
  border: 1px solid #30363d;
  border-radius: 4px;
  padding: 8px 10px;
  height: 260px;
  overflow-y: auto;
  font-family: "SF Mono", Consolas, monospace;
  font-size: 12px;
  line-height: 1.5;
}
.log .line { padding: 1px 0; word-break: break-all; }
.log .ts { color: #6e7681; margin-right: 8px; }
.log .tx { color: #79c0ff; }
.log .rx { color: #a5d6ff; }
.log .ok { color: #56d364; }
.log .err { color: #ff7b72; font-weight: 600; }
.log .info { color: #8b949e; font-style: italic; }
.log .alarm { color: #ffa657; font-weight: 600; }
.log .status { color: #6e7681; }

footer {
  text-align: center;
  color: #8b949e;
  padding: 12px;
  font-size: 12px;
  border-top: 1px solid #30363d;
}
footer code {
  background: #0d1117;
  padding: 1px 4px;
  border-radius: 3px;
}

.settings-panel .settings-row {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}
.settings-status {
  font-size: 12px;
  color: #8b949e;
  display: flex;
  gap: 6px;
  align-items: center;
}
.settings-status code {
  background: #0d1117;
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 11px;
}
.settings-spacer { flex: 1; }
.origin-display {
  font-family: "SF Mono", Consolas, monospace;
  font-size: 12px;
  color: #58a6ff;
  background: #0d1117;
  border: 1px solid #30363d;
  padding: 2px 8px;
  border-radius: 3px;
  min-width: 140px;
  text-align: center;
}
.origin-display.empty { color: #6e7681; }

dialog {
  background: #161b22;
  color: #e6edf3;
  border: 1px solid #30363d;
  border-radius: 6px;
  padding: 16px;
  max-width: 70vw;
  max-height: 80vh;
}
dialog::backdrop { background: rgba(0,0,0,0.6); }
dialog pre {
  background: #0d1117;
  padding: 10px;
  border-radius: 4px;
  max-height: 50vh;
  overflow: auto;
  font-size: 12px;
}
.dialog-actions { text-align: right; margin-top: 8px; }
