:root {
  color-scheme: light;
  --bg: #d9e1e6;
  --panel: #eef3f6;
  --panel-muted: #d5dee4;
  --control: #f8fbfc;
  --control-muted: #e5edf2;
  --ink: #17242c;
  --muted: #536773;
  --line: #aebdc6;
  --line-strong: #748996;
  --accent: #087ea4;
  --accent-soft: #cfeaf3;
  --accent-line: #46aeca;
  --warn: #a64d12;
  --warn-soft: #ffe3cf;
  --indexed: #006ca8;
  --indexed-soft: #d7eefc;
  --write: #9c3d77;
  --write-soft: #f7d9eb;
  --shadow: 0 12px 30px rgba(23, 36, 44, 0.13);
  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(--ink);
  font-size: 14px;
}

::selection {
  background: var(--accent);
  color: #ffffff;
}

button,
input,
select {
  font: inherit;
}

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

button:disabled,
select:disabled,
input:disabled {
  cursor: not-allowed;
}

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

.controls {
  display: grid;
  grid-template-columns: minmax(200px, 0.72fr) minmax(260px, 1.55fr) minmax(260px, 1.55fr);
  gap: 12px;
  align-items: stretch;
}

.field,
.stage-card,
.event-panel,
.report-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.field {
  min-width: 0;
  padding: 12px;
}

.op-field {
  display: flex;
  flex-direction: column;
  gap: 12px;
  transition: border-color 160ms ease, background-color 160ms ease;
}

label {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 700;
  text-transform: uppercase;
}

select,
.cell-input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--control);
  color: var(--ink);
}

select {
  min-height: 36px;
  padding: 0 10px;
}

.operand-field {
  display: flex;
  min-height: 138px;
  flex-direction: column;
  transition: border-color 160ms ease, background-color 160ms ease, opacity 160ms ease;
}

.operand-field.inactive {
  background: var(--panel-muted);
  opacity: 0.68;
}

.operand-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.shape-text {
  color: var(--muted);
  font-size: 0.76rem;
  white-space: nowrap;
}

.operand-editor {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 10px;
}

.grid-wrap {
  overflow: auto;
  padding-bottom: 2px;
}

.input-grid {
  display: grid;
  gap: 6px;
  min-width: max-content;
}

.cell-input {
  width: 50px;
  height: 34px;
  padding: 0 6px;
  text-align: center;
  transition: border-color 140ms ease, box-shadow 140ms ease;
}

.cell-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
  outline: none;
}

.cell-input.invalid {
  border-color: var(--warn);
  background: var(--warn-soft);
}

.editor-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: auto;
}

.small-btn,
.step-btn,
.primary-btn {
  min-height: 34px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--control);
  color: var(--ink);
  font-weight: 700;
}

.small-btn {
  padding: 0 9px;
}

.small-btn.icon {
  width: 34px;
  padding: 0;
  font-size: 1.1rem;
  line-height: 1;
}

.primary-btn {
  width: 100%;
  border-color: var(--accent);
  background: var(--accent);
  color: #ffffff;
}

.primary-btn:disabled {
  border-color: var(--line);
  background: var(--panel-muted);
  color: var(--muted);
}

.toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  color: var(--ink);
  font-size: 0.9rem;
  font-weight: 700;
  text-transform: none;
}

.toggle input {
  width: 17px;
  height: 17px;
  accent-color: var(--accent);
}

.message {
  min-height: 20px;
  margin: 12px 0;
  color: var(--warn);
  font-weight: 700;
}

.workspace {
  min-height: 460px;
}

.workspace.empty {
  display: grid;
  place-items: center;
  border: 1px dashed var(--line-strong);
  border-radius: 8px;
  background: rgba(238, 243, 246, 0.5);
}

.empty-state {
  max-width: 420px;
  padding: 32px 18px;
  text-align: center;
}

.empty-state p {
  margin: 0;
  color: var(--muted);
  font-size: 1rem;
}

.visualiser {
  display: grid;
  gap: 12px;
  align-items: start;
}

.stage-card {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(150px, 0.15fr);
  gap: 16px;
  min-width: 0;
  padding: 14px;
}

.stage-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.stage-title h2,
.stage-steps h2,
.event-panel h2,
.report-panel h2 {
  margin: 0;
  font-size: 1rem;
}

.step-index {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
}

.matrix-zone {
  display: grid;
  grid-template-columns: max-content minmax(36px, 1fr) max-content minmax(36px, 1fr) max-content;
  column-gap: 0;
  row-gap: 12px;
  align-items: center;
  overflow-x: auto;
  padding: 4px 0 12px;
}

.operator {
  justify-self: center;
  color: var(--muted);
  font-size: 1.35rem;
  font-weight: 800;
}

.viz-group {
  min-width: max-content;
}

.viz-label {
  margin-bottom: 7px;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 800;
  text-transform: uppercase;
}

.viz-grid {
  display: grid;
  gap: 6px;
}

.viz-cell {
  display: grid;
  width: 48px;
  height: 36px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--control);
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  transition: background-color 150ms ease, border-color 150ms ease, transform 150ms ease;
}

.viz-cell.pending {
  color: #81929c;
  font-weight: 600;
}

.viz-cell.indexed {
  border-color: var(--indexed);
  background: var(--indexed-soft);
  transform: translateY(-1px);
}

.viz-cell.write {
  border-color: var(--write);
  background: var(--write-soft);
  transform: translateY(-1px);
}

.viz-cell.target {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.trace-dashboard {
  display: grid;
  gap: 12px;
}

.detail-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(260px, 0.45fr);
  gap: 12px;
  align-items: start;
}

.event-panel,
.report-panel {
  padding: 12px;
}

.stage-steps {
  min-width: 0;
  border-left: 1px solid var(--line);
  padding-left: 16px;
}

.step-controls {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  margin-top: 10px;
}

.step-btn {
  padding: 0 9px;
}

.step-btn.active {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
}

.event-panel {
  overflow: hidden;
  min-height: 360px;
}

.event-kind {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  margin: 8px 0;
  border-radius: 999px;
  padding: 0 10px;
  background: var(--panel-muted);
  color: var(--ink);
  font-weight: 800;
}

.event-copy {
  min-height: 34px;
  margin: 0 0 10px;
  color: var(--muted);
  line-height: 1.4;
}

.event-json {
  overflow: visible;
  max-height: none;
  min-height: 220px;
  margin: 0;
  border-radius: 6px;
  padding: 9px;
  border: 1px solid #16313c;
  background: #10232b;
  color: #eefbff;
  font-size: 0.74rem;
  white-space: pre-wrap;
  word-break: break-word;
}

.report-panel.complete {
  border-color: var(--accent-line);
  background: var(--accent-soft);
}

.report-list {
  display: grid;
  gap: 9px;
  margin: 12px 0 0;
  font-size: 1.08rem;
}

.report-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  line-height: 1.25;
}

.report-row strong {
  color: var(--ink);
  font-size: 1.08rem;
}

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

  .stage-card {
    grid-template-columns: 1fr;
  }

  .stage-steps {
    border-left: 0;
    border-top: 1px solid var(--line);
    padding-left: 0;
    padding-top: 14px;
  }
}

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

  .controls,
  .visualiser,
  .detail-row,
  .matrix-zone {
    grid-template-columns: 1fr;
  }

  .operator {
    display: none;
  }

  .operand-field {
    min-height: 0;
  }
}
