:root {
  color-scheme: light;
  --bg: #f4f1ea;
  --ink: #202124;
  --muted: #6c6f73;
  --line: #d8d3c8;
  --panel: #ffffff;
  --accent: #0f7b6c;
  --accent-strong: #075f54;
  --danger: #bd2f2f;
  --warn: #a56600;
  --ok: #137333;
  --canvas: #fbfaf7;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 1040px;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
label.file-button,
select {
  font: inherit;
}

button,
label.file-button {
  border: 1px solid #bdb7aa;
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  min-width: 0;
  min-height: 36px;
  padding: 7px 10px;
  cursor: pointer;
  overflow-wrap: anywhere;
}

button:hover,
label.file-button:hover {
  border-color: #8f887b;
}

button:disabled {
  cursor: not-allowed;
  color: #8d8f92;
  background: #ece9e2;
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

button.primary:hover {
  background: var(--accent-strong);
}

button.danger {
  background: #fff7f7;
  border-color: #de9a9a;
  color: var(--danger);
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(720px, 1fr) 390px;
  min-height: 100vh;
}

.stage {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  padding: 20px 22px 14px;
  border-bottom: 1px solid var(--line);
}

.title-block {
  min-width: 240px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 22px;
  line-height: 1.2;
}

h2 {
  font-size: 14px;
  line-height: 1.2;
  margin-bottom: 10px;
}

.topbar p {
  color: var(--muted);
  font-size: 13px;
  margin-top: 5px;
}

.topbar-controls {
  display: grid;
  justify-items: end;
  gap: 7px;
  min-width: 0;
}

.toio-status-strip,
.toio-connect-actions {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
  flex-wrap: wrap;
}

.toio-status-item {
  display: inline-grid;
  grid-template-columns: auto auto minmax(58px, auto);
  align-items: center;
  min-height: 28px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  font-size: 12px;
}

.toio-status-item[hidden] {
  display: none;
}

.toio-connect-button {
  display: inline-grid;
  grid-template-columns: auto auto minmax(58px, auto);
  align-items: center;
  min-height: 28px;
  padding: 3px 0;
  border-radius: 999px;
  font-size: 12px;
}

.toio-connect-button .toio-status-label {
  padding-left: 5px;
}

.toio-connect-button .toio-status-value {
  padding-right: 9px;
}

.toio-status-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  margin-left: 2px;
  border-radius: 50%;
  color: #fff;
  background: var(--danger);
  font-size: 13px;
  font-weight: 800;
  line-height: 1;
}

.toio-status-mark.connected {
  background: var(--ok);
}

.toio-status-mark.connecting {
  color: var(--ink);
  background: #e4c27f;
}

.toio-status-label {
  padding: 4px 7px 4px 9px;
  color: var(--muted);
}

.toio-status-value {
  min-width: 0;
  max-width: 148px;
  overflow: hidden;
  padding: 4px 9px 4px 7px;
  color: var(--muted);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toio-status-value.connected {
  color: var(--ok);
  background: #f1fbf3;
}

.toio-status-value.connecting {
  color: var(--warn);
  background: #fff8e6;
}

.toio-status-value.disconnected {
  color: var(--danger);
  background: #fff4f4;
  font-weight: 700;
}

.toio-connect-actions button {
  min-height: 28px;
  padding: 4px 9px;
  font-size: 12px;
  line-height: 1;
}

.status-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 28px;
  padding: 4px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.status-pill.ok {
  color: var(--ok);
  border-color: #a8d6b5;
  background: #f1fbf3;
}

.status-pill.warn {
  color: var(--warn);
  border-color: #e4c27f;
  background: #fff8e6;
}

.status-pill.error {
  color: var(--danger);
  border-color: #e8adad;
  background: #fff4f4;
}

.canvas-wrap {
  position: relative;
  flex: 1;
  min-height: 680px;
  padding: 18px 22px 22px;
}

#plotCanvas {
  display: block;
  width: 100%;
  height: calc(100vh - 120px);
  min-height: 620px;
  border: 1px solid #cfc8bc;
  background: var(--canvas);
  cursor: crosshair;
  touch-action: none;
}

.canvas-hint {
  position: absolute;
  left: 214px;
  right: 230px;
  top: 30px;
  min-height: 30px;
  padding: 5px 8px;
  background: rgba(255, 255, 255, 0.9);
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 12px;
  pointer-events: none;
  display: flex;
  align-items: center;
}

.canvas-hint.warn {
  background: rgba(255, 248, 230, 0.96);
  border-color: #e4c27f;
  color: var(--warn);
  font-weight: 600;
}

.canvas-tools {
  position: absolute;
  top: 30px;
  left: 34px;
  display: flex;
  align-items: center;
  gap: 12px;
  z-index: 1;
}

.canvas-tool-group {
  display: flex;
  gap: 6px;
}

.canvas-tools button {
  min-height: 30px;
  padding: 4px 10px;
  background: rgba(255, 255, 255, 0.94);
  border-color: var(--line);
  font-size: 12px;
  font-weight: 600;
  line-height: 1;
}

.canvas-tools button.danger {
  background: rgba(255, 247, 247, 0.95);
  border-color: #de9a9a;
}

.legend-toggle {
  position: absolute;
  top: 30px;
  right: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 178px;
  min-height: 30px;
  padding: 4px 10px;
  background: rgba(255, 255, 255, 0.94);
  color: var(--ink);
  border-color: var(--line);
  font-size: 12px;
  font-weight: 600;
  line-height: 1;
  z-index: 1;
}

.legend-toggle::after {
  content: "";
  width: 7px;
  height: 7px;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: translateY(-2px) rotate(45deg);
  transition: transform 120ms ease;
}

.legend-toggle[aria-expanded="true"] {
  color: var(--ink);
}

.legend-toggle[aria-expanded="false"] {
  color: var(--muted);
}

.legend-toggle[aria-expanded="false"]::after {
  transform: translateY(2px) rotate(-135deg);
}

.control-panel {
  overflow: auto;
  max-height: 100vh;
  padding: 14px;
  border-left: 1px solid var(--line);
  background: #f7f5ef;
}

.panel-section {
  margin-bottom: 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.button-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.import-grid {
  grid-template-columns: 1fr;
}

.simulation-actions {
  grid-template-columns: 1fr;
  margin-bottom: 10px;
}

.position-status-item .toio-status-value {
  max-width: 190px;
}

.role-swap-button {
  min-height: 24px;
  margin-right: 3px;
  padding: 3px 7px;
  border-radius: 999px;
  font-size: 11px;
  line-height: 1;
}

.pen-check-button {
  grid-column: 1 / -1;
}

.sample-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 86px auto;
  gap: 8px;
  margin-bottom: 8px;
}

.sample-row select {
  min-width: 0;
  min-height: 36px;
  padding: 6px 8px;
  border: 1px solid #c9c2b6;
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
}

.file-button {
  text-align: center;
}

.file-button input {
  display: none;
}

.icon-file-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  justify-self: start;
  width: 42px;
  min-height: 36px;
  padding: 7px 10px;
}

.icon-file-button svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 1.8;
}

.run-mode-control {
  display: block;
}

.run-mode-control select {
  width: 100%;
  min-height: 36px;
  padding: 6px 8px;
  border: 1px solid #c9c2b6;
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
}

.demo-mode-button {
  width: 100%;
  margin-top: 8px;
  border-color: #8c78d6;
  color: #5d46a8;
}

.demo-mode-button.active {
  border-color: #5d46a8;
  background: #eeeaff;
  color: #4c3698;
  font-weight: 700;
}

.demo-mode-note {
  margin-top: 7px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 9px;
}

.form-grid label {
  display: grid;
  gap: 4px;
  color: var(--muted);
  font-size: 12px;
}

.form-grid input {
  width: 100%;
  border: 1px solid #c9c2b6;
  border-radius: 6px;
  min-height: 32px;
  padding: 5px 7px;
  background: #fff;
  color: var(--ink);
}

.form-grid select {
  width: 100%;
  border: 1px solid #c9c2b6;
  border-radius: 6px;
  min-height: 32px;
  padding: 5px 7px;
  background: #fff;
  color: var(--ink);
}

.mode-grid {
  margin-bottom: 8px;
}

.selected-segment-panel {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
}

.selected-segment-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}

.selected-segment-header h3 {
  margin: 0;
  font-size: 13px;
  line-height: 1.2;
}

.segment-nav {
  display: flex;
  gap: 6px;
}

.segment-nav button {
  min-height: 28px;
  padding: 4px 8px;
  font-size: 12px;
}

.command-nav {
  position: sticky;
  top: 0;
  z-index: 2;
  margin: 0 -4px 8px;
  padding: 4px;
  background: var(--panel);
}

.segment-editor {
  display: block;
}

.segment-card {
  display: grid;
  gap: 8px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfaf7;
}

.segment-empty {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.segment-meta {
  color: var(--muted);
  font-size: 11px;
}

.segment-title {
  font-weight: 700;
  font-size: 13px;
}

.segment-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.segment-card label {
  display: grid;
  gap: 3px;
  color: var(--muted);
  font-size: 11px;
}

.segment-card input {
  width: 100%;
  border: 1px solid #c9c2b6;
  border-radius: 5px;
  min-height: 28px;
  padding: 4px 6px;
  background: #fff;
  color: var(--ink);
}

.toio-command-panel {
  margin-top: 10px;
}

.command-save-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 8px;
}

.command-save-row button {
  width: 100%;
}

.detail-panel,
.turn-calibration-panel {
  display: block;
}

.detail-panel summary,
.turn-calibration-panel summary {
  cursor: pointer;
  font-size: 14px;
  font-weight: 700;
  line-height: 1.2;
}

.detail-panel-body {
  display: grid;
  gap: 8px;
  margin-top: 10px;
}

.detail-toolbar {
  display: flex;
  justify-content: flex-end;
}

.detail-toolbar button {
  min-height: 28px;
  padding: 4px 8px;
  font-size: 12px;
}

.turn-calibration-output {
  display: grid;
  gap: 8px;
}

.turn-calibration-summary {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfaf7;
  color: #43464a;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 11px;
}

.turn-test-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 7px;
  padding: 7px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfaf7;
}

.turn-test-main,
.turn-test-result {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  align-items: center;
  color: #43464a;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 11px;
}

.turn-test-inputs {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

.turn-test-inputs label {
  display: grid;
  gap: 2px;
  color: var(--muted);
  font-size: 10px;
}

.turn-test-inputs input {
  width: 100%;
  min-width: 0;
  min-height: 26px;
  padding: 3px 5px;
  border: 1px solid #c9c2b6;
  border-radius: 5px;
  background: #fff;
  color: var(--ink);
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 11px;
}

.turn-test-row > button {
  align-self: end;
  min-height: 26px;
  padding: 3px 8px;
  font-size: 11px;
}

.toio-command-output {
  max-height: 220px;
  margin: 0;
  overflow: auto;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfaf7;
  color: #43464a;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 11px;
  line-height: 1.45;
}

.command-row {
  display: grid;
  grid-template-columns: 34px 1fr;
  gap: 8px;
  align-items: start;
  padding: 7px;
  border: 1px solid transparent;
  border-left: 4px solid transparent;
  border-radius: 6px;
}

.command-row + .command-row {
  margin-top: 6px;
}

.command-row.active {
  border-color: #f0c36d;
  border-left-color: #f59e0b;
  background: #fff8e6;
  color: #202124;
}

.command-step-button,
.command-step-static {
  align-self: start;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 30px;
  min-height: 26px;
  padding: 3px 6px;
  border: 1px solid #b9d5ce;
  border-radius: 999px;
  background: #e8f2ef;
  color: var(--accent-strong);
  font-size: 11px;
  font-weight: 700;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  line-height: 1;
}

.command-step-static {
  border-color: #d8d3c8;
  background: transparent;
  color: var(--muted);
  cursor: default;
  opacity: 0.72;
}

.command-step-button:hover {
  border-color: var(--accent);
  background: #dcece8;
}

.command-row.active .command-step-button {
  border-color: #f59e0b;
  background: #f59e0b;
  color: #fff;
}

.command-main {
  min-width: 0;
}

.command-run-button {
  min-height: 28px;
  margin-top: 6px;
  padding: 4px 10px;
  font-size: 12px;
}

.command-label {
  overflow-wrap: anywhere;
}

.command-fields {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
  margin-top: 5px;
}

.command-fields-wait {
  display: flex;
}

.command-fields label {
  display: grid;
  gap: 2px;
  color: var(--muted);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 10px;
}

.command-fields .command-unit-field {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.command-fields input {
  width: 100%;
  min-width: 0;
  min-height: 26px;
  padding: 3px 5px;
  border: 1px solid #c9c2b6;
  border-radius: 5px;
  background: #fff;
  color: var(--ink);
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 11px;
}

.command-unit-field input {
  width: 90px;
}

.mini-log {
  display: grid;
  gap: 5px;
  margin: 10px 0 0;
  font-size: 12px;
}

.mini-log div {
  display: grid;
  grid-template-columns: 82px 1fr;
  gap: 8px;
}

.mini-log dt {
  color: var(--muted);
}

.mini-log dd {
  margin: 0;
  min-height: 22px;
  padding: 2px 6px;
  border-radius: 5px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mini-log dd.connected {
  color: var(--ok);
  background: #f1fbf3;
}

.mini-log dd.connecting {
  color: var(--warn);
  background: #fff8e6;
}

.mini-log dd.disconnected {
  color: var(--danger);
  background: #fff4f4;
  font-weight: 700;
}

.message-log {
  min-height: 72px;
  margin-top: 10px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfaf7;
  color: #43464a;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 12px;
  line-height: 1.45;
  white-space: pre-wrap;
}
