:root {
  color-scheme: light;
  --paper: #edf0eb;
  --panel: #f8f9f5;
  --ink: #18211e;
  --muted: #727b76;
  --line: #d7dcd5;
  --line-strong: #bac3bb;
  --stage: #111816;
  --stage-text: #eff3eb;
  --stage-muted: #93a199;
  --mint: #78d9b2;
  --cyan: #64c7d8;
  --amber: #f1bd58;
  --coral: #ef7968;
  --radius: 6px;
  font-family: Inter, "Segoe UI", "Microsoft YaHei", sans-serif;
  font-synthesis: none;
}

* { box-sizing: border-box; }

html,
body {
  min-width: 320px;
  min-height: 100%;
  margin: 0;
  color: var(--ink);
  background: var(--paper);
}

button,
input { font: inherit; }

button,
input[type="range"],
input[type="checkbox"] { cursor: pointer; }

button:focus-visible,
input:focus-visible {
  outline: 2px solid var(--coral);
  outline-offset: 2px;
}

.lab-shell {
  display: grid;
  grid-template-columns: 330px minmax(0, 1fr);
  min-height: 100svh;
}

.control-panel {
  position: relative;
  z-index: 3;
  display: flex;
  flex-direction: column;
  height: 100svh;
  overflow-y: auto;
  background: var(--panel);
  border-right: 1px solid var(--line);
  scrollbar-color: var(--line-strong) transparent;
  scrollbar-width: thin;
}

.brand {
  display: flex;
  align-items: center;
  min-height: 104px;
  padding: 22px 28px;
  gap: 14px;
  border-bottom: 1px solid var(--line);
}

.brand-mark {
  position: relative;
  width: 42px;
  height: 42px;
  flex: 0 0 42px;
  background: var(--ink);
  border-radius: 50%;
}

.brand-mark i {
  position: absolute;
  width: 8px;
  height: 8px;
  border: 1px solid var(--mint);
}

.brand-mark i:nth-child(1) { top: 10px; left: 10px; }
.brand-mark i:nth-child(2) { top: 10px; right: 10px; }
.brand-mark i:nth-child(3) { right: 10px; bottom: 10px; }
.brand-mark i:nth-child(4) { bottom: 10px; left: 10px; }

.brand-mark::before,
.brand-mark::after {
  position: absolute;
  top: 20px;
  left: 12px;
  width: 18px;
  height: 1px;
  background: var(--mint);
  content: "";
}

.brand-mark::after { transform: rotate(90deg); }

.brand span,
.brand h1 {
  display: block;
  margin: 0;
  letter-spacing: 0;
}

.brand span {
  margin-bottom: 5px;
  color: var(--muted);
  font-size: 9px;
  font-weight: 700;
}

.brand h1 {
  font-size: 18px;
  font-weight: 650;
}

.control-section {
  padding: 23px 28px 25px;
  border-bottom: 1px solid var(--line);
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 24px;
  margin-bottom: 18px;
}

.section-title h2 {
  margin: 0;
  font-size: 13px;
  font-weight: 700;
}

.section-title small {
  color: var(--line-strong);
  font: 10px ui-monospace, SFMono-Regular, Consolas, monospace;
}

.tile-set {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 5px;
}

.tile-set button {
  display: grid;
  min-width: 0;
  height: 64px;
  place-items: center;
  align-content: center;
  gap: 7px;
  color: var(--muted);
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.tile-set button:hover { border-color: var(--line-strong); }

.tile-set button[aria-pressed="true"] {
  color: var(--stage-text);
  background: var(--ink);
  border-color: var(--ink);
}

.tile-set b {
  font-size: 10px;
  font-weight: 600;
}

.tile-swatch {
  position: relative;
  width: 26px;
  height: 20px;
}

.tile-swatch::before,
.tile-swatch::after {
  position: absolute;
  content: "";
}

.tile-swatch::before {
  top: 9px;
  left: 1px;
  width: 24px;
  height: 2px;
  background: currentColor;
}

.tile-swatch::after {
  top: 2px;
  left: 12px;
  width: 2px;
  height: 16px;
  background: currentColor;
}

.tile-swatch.circuit { color: var(--mint); }
.tile-swatch.river { color: var(--cyan); }
.tile-swatch.metro { color: var(--amber); }

.range-control { display: block; }
.range-control + .range-control { margin-top: 20px; }

.range-control > span {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 10px;
  gap: 12px;
}

.range-control b,
.switch-control b {
  font-size: 11px;
  font-weight: 600;
}

.range-control output {
  color: var(--muted);
  font: 11px ui-monospace, SFMono-Regular, Consolas, monospace;
  font-variant-numeric: tabular-nums;
}

input[type="range"] {
  width: 100%;
  height: 14px;
  margin: 0;
  appearance: none;
  background: transparent;
}

input[type="range"]::-webkit-slider-runnable-track {
  height: 2px;
  background: linear-gradient(to right, var(--ink) var(--progress, 50%), var(--line) var(--progress, 50%));
}

input[type="range"]::-webkit-slider-thumb {
  width: 13px;
  height: 13px;
  margin-top: -5.5px;
  appearance: none;
  background: var(--panel);
  border: 2px solid var(--ink);
  border-radius: 50%;
}

input[type="range"]::-moz-range-track { height: 2px; background: var(--line); }
input[type="range"]::-moz-range-progress { height: 2px; background: var(--ink); }
input[type="range"]::-moz-range-thumb {
  width: 10px;
  height: 10px;
  background: var(--panel);
  border: 2px solid var(--ink);
  border-radius: 50%;
}

.field-label {
  display: block;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 650;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 3px;
  margin-bottom: 20px;
  padding: 3px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.segmented button {
  min-width: 0;
  height: 32px;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: 3px;
  font-size: 11px;
  font-weight: 600;
}

.segmented button[aria-pressed="true"] {
  color: var(--stage-text);
  background: var(--ink);
}

.switch-control {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.switch-control input {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  opacity: 0;
}

.switch-control span {
  position: relative;
  width: 30px;
  height: 17px;
  background: var(--line-strong);
  border-radius: 10px;
  transition: background 160ms ease;
}

.switch-control span::after {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 11px;
  height: 11px;
  background: var(--panel);
  border-radius: 50%;
  content: "";
  transition: transform 160ms ease;
}

.switch-control input:checked + span { background: var(--ink); }
.switch-control input:checked + span::after { transform: translateX(13px); }
.switch-control input:focus-visible + span { outline: 2px solid var(--coral); outline-offset: 2px; }

.panel-footer {
  display: flex;
  justify-content: space-between;
  margin-top: auto;
  padding: 18px 28px;
  color: var(--line-strong);
  font: 9px ui-monospace, SFMono-Regular, Consolas, monospace;
}

.simulation-stage {
  position: relative;
  min-width: 0;
  min-height: 560px;
  overflow: hidden;
  background: var(--stage);
}

.simulation-stage::before {
  position: absolute;
  inset: 0;
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.025) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.025) 1px, transparent 1px);
  background-size: 64px 64px;
  content: "";
}

#wfc-canvas {
  position: absolute;
  inset: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.stage-header {
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  z-index: 3;
  display: grid;
  grid-template-columns: minmax(160px, 1fr) auto minmax(210px, 1fr);
  align-items: center;
  min-height: 92px;
  padding: 0 34px;
  color: var(--stage-text);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  pointer-events: none;
}

.live-status {
  display: flex;
  align-items: center;
  gap: 12px;
}

.live-status > i {
  width: 8px;
  height: 8px;
  background: var(--mint);
  border-radius: 50%;
  box-shadow: 0 0 0 5px rgba(120, 217, 178, 0.1);
}

.live-status > i.paused { background: var(--amber); box-shadow: 0 0 0 5px rgba(241, 189, 88, 0.1); }
.live-status > i.complete { background: var(--cyan); box-shadow: 0 0 0 5px rgba(100, 199, 216, 0.1); }

.live-status div { display: grid; gap: 4px; }
.live-status span,
.metrics span { color: var(--stage-muted); font-size: 9px; font-weight: 600; }
.live-status strong { font-size: 13px; font-weight: 600; }

.metrics { display: flex; }

.metrics > div {
  display: grid;
  min-width: 92px;
  gap: 5px;
  padding: 0 22px;
  border-left: 1px solid rgba(255, 255, 255, 0.11);
}

.metrics > div:last-child { border-right: 1px solid rgba(255, 255, 255, 0.11); }

.metrics strong {
  font: 500 15px ui-monospace, SFMono-Regular, Consolas, monospace;
  font-variant-numeric: tabular-nums;
}

.stage-actions {
  display: flex;
  justify-self: end;
  gap: 8px;
  pointer-events: auto;
}

.stage-actions button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 76px;
  height: 38px;
  padding: 0 13px;
  gap: 8px;
  color: var(--stage-text);
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: var(--radius);
  font-size: 11px;
}

.stage-actions button:hover { background: rgba(255, 255, 255, 0.1); }
.stage-actions button b { font-weight: 600; }
.stage-actions .primary-action { color: var(--stage); background: var(--mint); border-color: var(--mint); }
.stage-actions .primary-action:hover { background: #a1e7ca; }

.pause-icon,
.step-icon,
.rebuild-icon {
  position: relative;
  display: inline-block;
  width: 12px;
  height: 12px;
  flex: 0 0 12px;
}

.pause-icon::before,
.pause-icon::after {
  position: absolute;
  top: 1px;
  width: 3px;
  height: 10px;
  background: currentColor;
  content: "";
}

.pause-icon::before { left: 1px; }
.pause-icon::after { right: 1px; }

.pause-icon.play::before {
  top: 0;
  left: 2px;
  width: 0;
  height: 0;
  background: transparent;
  border-top: 6px solid transparent;
  border-bottom: 6px solid transparent;
  border-left: 9px solid currentColor;
}

.pause-icon.play::after { display: none; }

.step-icon::before {
  position: absolute;
  top: 1px;
  left: 1px;
  border-top: 5px solid transparent;
  border-bottom: 5px solid transparent;
  border-left: 7px solid currentColor;
  content: "";
}

.step-icon::after {
  position: absolute;
  top: 1px;
  right: 1px;
  width: 2px;
  height: 10px;
  background: currentColor;
  content: "";
}

.rebuild-icon {
  border: 1.5px solid currentColor;
  border-left-color: transparent;
  border-radius: 50%;
}

.rebuild-icon::after {
  position: absolute;
  top: -2px;
  left: -1px;
  width: 0;
  height: 0;
  border-top: 3px solid transparent;
  border-bottom: 3px solid transparent;
  border-right: 4px solid currentColor;
  content: "";
  transform: rotate(25deg);
}

.stage-note,
.legend {
  position: absolute;
  bottom: 25px;
  z-index: 3;
  display: flex;
  align-items: center;
  color: var(--stage-muted);
  font: 9px ui-monospace, SFMono-Regular, Consolas, monospace;
  pointer-events: none;
}

.stage-note { left: 34px; gap: 10px; }
.stage-note > i { width: 42px; height: 1px; background: rgba(255, 255, 255, 0.2); }
.legend { right: 34px; gap: 14px; }
.legend span { display: flex; align-items: center; gap: 6px; }
.legend i { width: 7px; height: 7px; border: 1px solid rgba(255, 255, 255, 0.24); }
.legend .uncertain { background: #26322e; }
.legend .active { background: var(--amber); border-color: var(--amber); }
.legend .fixed { background: var(--mint); border-color: var(--mint); }

@media (max-width: 1120px) {
  .lab-shell { grid-template-columns: 292px minmax(0, 1fr); }
  .brand,
  .control-section { padding-right: 22px; padding-left: 22px; }
  .stage-header { grid-template-columns: 1fr auto; padding: 0 24px; }
  .metrics { display: none; }
}

@media (max-width: 760px) {
  .lab-shell { display: flex; flex-direction: column; }
  .simulation-stage { order: 1; min-height: 62svh; }
  .control-panel {
    order: 2;
    width: 100%;
    height: auto;
    overflow: visible;
    border-top: 1px solid var(--line);
    border-right: 0;
  }
  .brand { min-height: 84px; }
  .stage-header { min-height: 74px; padding: 0 16px; }
  .stage-actions button { width: 40px; min-width: 40px; padding: 0; }
  .stage-actions button b { display: none; }
  .stage-note { left: 16px; bottom: 18px; }
  .legend { right: 16px; bottom: 18px; }
  .legend span:not(:last-child) { display: none; }
  .panel-footer { margin-top: 0; }
}

@media (max-width: 410px) {
  .live-status strong {
    display: block;
    max-width: 90px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .stage-actions { gap: 5px; }
  .stage-actions button { width: 36px; min-width: 36px; height: 36px; }
  .stage-note > i,
  .legend { display: none; }
  .control-section,
  .brand { padding-right: 20px; padding-left: 20px; }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after { transition-duration: 0.01ms !important; }
}
