:root {
  color-scheme: light;
  --bg: #f7f8f4;
  --surface: #ffffff;
  --surface-2: #eef4ef;
  --line: #d7ddd5;
  --line-strong: #aeb8ad;
  --text: #172019;
  --muted: #667063;
  --primary: #1d6f5f;
  --primary-dark: #155447;
  --danger: #b3443e;
  --focus: #0b7fbc;
  --chart-force: #1d6f5f;
  --chart-tilt: #b96b2b;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--text);
}

button,
input,
select {
  font: inherit;
}

button {
  min-height: 38px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  padding: 0 14px;
  cursor: pointer;
}

button:hover:not(:disabled) {
  border-color: var(--primary);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

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

button.primary:hover:not(:disabled) {
  background: var(--primary-dark);
}

button.danger {
  border-color: var(--danger);
  color: var(--danger);
}

input,
select {
  width: 100%;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #ffffff;
  color: var(--text);
  padding: 7px 10px;
}

input:focus,
select:focus,
button:focus {
  outline: 2px solid color-mix(in srgb, var(--focus), transparent 55%);
  outline-offset: 2px;
}

.app-shell {
  width: min(1440px, 100%);
  margin: 0 auto;
  padding: 18px;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(260px, 1fr) minmax(380px, 0.95fr) auto;
  align-items: center;
  gap: 14px;
  padding: 4px 0 14px;
}

.topbar h1 {
  margin: 0;
  font-size: clamp(22px, 3vw, 34px);
  line-height: 1.18;
  letter-spacing: 0;
}

.topbar p {
  margin: 8px 0 0;
  color: var(--muted);
}

.connect-row,
.button-row,
.jog-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.status-strip {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  overflow: hidden;
}

.status-strip div {
  min-width: 0;
  padding: 8px 10px;
  border-right: 1px solid var(--line);
}

.status-strip div:last-child {
  border-right: 0;
}

.status-strip span,
label span,
.readout-grid span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.status-strip strong,
.readout-grid strong {
  display: block;
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 15px;
}

.panel,
.experiment,
.log-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 14px;
}

.panel {
  min-width: 0;
}

.control-panel {
  padding: 0;
  overflow: hidden;
}

.control-panel summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 52px;
  padding: 0 14px;
  cursor: pointer;
  list-style: none;
}

.control-panel summary::-webkit-details-marker {
  display: none;
}

.control-panel summary span {
  color: var(--text);
  font-size: 16px;
  font-weight: 700;
}

.control-panel summary strong {
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
}

.control-panel[open] summary {
  border-bottom: 1px solid var(--line);
}

.control-grid {
  display: grid;
  grid-template-columns: minmax(280px, 0.95fr) repeat(3, minmax(0, 1fr));
  gap: 14px;
  padding: 14px;
}

.setting-block {
  min-width: 0;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 38px;
  margin-bottom: 12px;
}

.panel-head h2 {
  margin: 0;
  font-size: 16px;
  line-height: 1.2;
  letter-spacing: 0;
}

.panel-head.compact {
  margin-bottom: 10px;
}

.jog-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  margin-bottom: 12px;
}

button.jog {
  height: 64px;
  font-size: 28px;
  font-weight: 700;
  line-height: 1;
  background: var(--surface-2);
}

.field-grid,
.readout-grid,
.experiment-controls {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.field-grid label,
.experiment-controls label {
  min-width: 0;
}

label span {
  margin-bottom: 4px;
}

.button-row {
  margin-top: 12px;
}

.button-row.tight {
  margin-top: 0;
}

.switch-line {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
}

.switch-line input {
  width: 18px;
  min-height: 18px;
}

.readout-grid div {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px;
  background: #fbfcfa;
}

.fine-text {
  min-height: 20px;
  margin: 12px 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.experiment,
.log-panel {
  margin-top: 14px;
}

.experiment-controls {
  grid-template-columns: repeat(6, minmax(0, 1fr));
  margin-bottom: 14px;
}

.chart-panel {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfa;
  padding: 10px;
}

.chart-panel.combined canvas {
  height: 420px;
}

.chart-title {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  color: var(--muted);
  font-size: 13px;
  margin-bottom: 8px;
}

.chart-title strong {
  color: var(--text);
}

canvas {
  display: block;
  width: 100%;
  height: 420px;
  border-radius: 6px;
  background: #ffffff;
  cursor: crosshair;
}

#logOutput {
  min-height: 120px;
  max-height: 220px;
  overflow: auto;
  margin: 0;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #111612;
  color: #dce9de;
  font-size: 12px;
  line-height: 1.5;
  white-space: pre-wrap;
}

@media (max-width: 1180px) {
  .topbar {
    grid-template-columns: 1fr;
  }

  .control-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .experiment-controls {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (max-width: 760px) {
  .app-shell {
    padding: 12px;
  }

  .topbar {
    align-items: stretch;
    flex-direction: column;
    display: flex;
  }

  .connect-row button {
    flex: 1;
  }

  .status-strip {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .status-strip div:nth-child(2) {
    border-right: 0;
  }

  .status-strip div:nth-child(-n + 2) {
    border-bottom: 1px solid var(--line);
  }

  .control-grid,
  .experiment-controls,
  .field-grid,
  .readout-grid {
    grid-template-columns: 1fr;
  }

  .chart-panel.combined canvas,
  canvas {
    height: 360px;
  }
}
