.system-demo {
  --system-border: var(--table-border-color);
  --system-surface: var(--quote-bg);
  --system-active: color-mix(in srgb, var(--links) 19%, var(--bg));
  margin: 1.5rem 0 2.25rem;
  padding: 1rem;
  border: 1px solid var(--system-border);
  border-radius: 12px;
  background: var(--system-surface);
}

.system-demo-controls,
.system-demo-metrics,
.cache-contents {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  justify-content: center;
  gap: 0.65rem;
}

.system-demo-field {
  display: grid;
  gap: 0.25rem;
  color: var(--icons);
  font-size: 0.88em;
}

.system-demo select,
.system-demo button {
  min-height: 40px;
  padding: 0.48rem 0.75rem;
  border: 1px solid var(--system-border);
  border-radius: 7px;
  background: var(--bg);
  color: var(--fg);
  cursor: pointer;
  font: inherit;
}

.system-demo button:hover:not(:disabled) {
  background: var(--table-header-bg);
}

.system-demo button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.system-demo select:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.system-demo-metrics {
  margin: 1rem 0;
}

.system-demo-metric {
  min-width: 7rem;
  text-align: center;
  color: var(--icons);
  font-size: 0.82em;
}

.system-demo-metric strong {
  display: block;
  color: var(--fg);
  font-family: var(--mono-font);
  font-size: 1.25rem;
  font-variant-numeric: tabular-nums;
}

.memory-lines {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(8.5rem, 1fr));
  gap: 0.65rem;
}

.memory-lab-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.55fr) minmax(13rem, 0.45fr);
  gap: 0.8rem;
}

.memory-lab-panel {
  min-width: 0;
  padding: 0.7rem;
  border: 1px solid var(--system-border);
  border-radius: 9px;
  background: var(--bg);
}

.memory-lab-heading {
  display: flex;
  justify-content: space-between;
  gap: 0.7rem;
  margin-bottom: 0.65rem;
  color: var(--icons);
  font-size: 0.74em;
}

.memory-lab-heading strong {
  color: var(--fg);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.memory-lines.is-node-layout {
  grid-template-columns: repeat(auto-fit, minmax(6.5rem, 1fr));
}

.memory-line {
  padding: 0.45rem;
  border: 1px solid var(--system-border);
  border-radius: 8px;
  background: var(--bg);
  transition: opacity 180ms ease, transform 180ms ease, background-color 180ms ease;
}

.memory-line.is-cached {
  background: var(--system-active);
}

.memory-line.is-current {
  transform: translateY(-3px);
  border-color: var(--links);
}

.memory-line-label {
  margin-bottom: 0.3rem;
  color: var(--icons);
  font-family: var(--mono-font);
  font-size: 0.72em;
  text-align: center;
}

.memory-records {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.25rem;
}

.memory-record {
  display: grid;
  min-height: 38px;
  place-items: center;
  border: 1px solid var(--system-border);
  border-radius: 5px;
  font-family: var(--mono-font);
  font-size: 0.82em;
  font-variant-numeric: tabular-nums;
}

.memory-record.is-current {
  border-color: var(--links);
  background: var(--bg);
  font-weight: 700;
}

.memory-record.is-used {
  background: color-mix(in srgb, var(--links) 13%, var(--bg));
}

.memory-record.is-next {
  box-shadow: inset 0 -2px 0 var(--links);
}

.memory-lines.is-node-layout .memory-records {
  grid-template-columns: 1fr;
}

.memory-unused {
  display: grid;
  min-height: 30px;
  place-items: center;
  border: 1px dashed var(--system-border);
  border-radius: 5px;
  color: var(--icons);
  font-size: 0.66em;
}

.cache-panel {
  margin-top: 0;
  color: var(--icons);
  text-align: center;
}

.cache-contents {
  margin-top: 0.35rem;
  align-items: stretch;
}

.cache-slot {
  display: grid;
  gap: 0.12rem;
  min-width: 5.2rem;
  padding: 0.3rem 0.55rem;
  border: 1px solid var(--system-border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--mono-font);
  font-size: 0.8em;
}

.cache-slot strong {
  color: var(--fg);
}

.cache-slot small {
  font-family: var(--content-font);
  font-size: 0.72em;
}

.cache-slot.is-empty {
  color: var(--icons);
}

.memory-lab-transfer {
  margin-top: 0.7rem;
  padding: 0.55rem;
  border-radius: 6px;
  background: var(--system-surface);
  color: var(--fg);
  font-size: 0.72em;
  line-height: 1.4;
}

.latency-ladder {
  margin: 1.2rem 0 1.6rem;
  padding: 0.8rem;
  border: 1px solid var(--table-border-color);
  border-radius: 10px;
  background: var(--quote-bg);
}

.latency-ladder__header {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.7rem;
  color: var(--icons);
  font-size: 0.76em;
}

.latency-ladder__header strong {
  color: var(--fg);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.latency-rung {
  display: grid;
  grid-template-columns: 11rem minmax(5rem, 1fr) 13rem;
  align-items: center;
  gap: 0.7rem;
  min-height: 2.35rem;
  border-top: 1px dashed color-mix(in srgb, var(--table-border-color) 70%, transparent);
}

.latency-rung__name {
  font-weight: 650;
  font-size: 0.9em;
}

.latency-rung__bar {
  width: var(--latency-width);
  min-width: 0.45rem;
  height: 0.72rem;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--links), color-mix(in srgb, var(--links) 42%, #f97316));
}

.latency-rung__value {
  color: var(--icons);
  font-family: var(--mono-font);
  font-size: 0.72em;
}

.system-demo-status {
  min-height: 2.8rem;
  margin: 0.75rem 0 0;
  text-align: center;
}

.system-demo-history {
  margin-top: 0.4rem;
  color: var(--icons);
  font-family: var(--mono-font);
  font-size: 0.8em;
  text-align: center;
}

.layout-memory-lines {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.65rem;
}

.layout-lab-heading {
  display: flex;
  justify-content: space-between;
  gap: 0.7rem;
  margin-bottom: 0.6rem;
  color: var(--icons);
  font-size: 0.76em;
}

.layout-lab-heading strong {
  color: var(--fg);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.layout-memory-line {
  padding: 0.45rem;
  border: 1px solid var(--system-border);
  border-radius: 8px;
  background: var(--bg);
  transition: transform 180ms ease, background-color 180ms ease;
}

.layout-memory-line.is-loaded {
  background: var(--system-active);
}

.layout-memory-line.is-current {
  transform: translateX(4px);
  border-color: var(--links);
}

.layout-line-label {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.3rem;
  color: var(--icons);
  font-family: var(--mono-font);
  font-size: 0.72em;
}

.layout-line-label small {
  overflow: hidden;
  font-family: var(--content-font);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.layout-fields {
  display: grid;
  grid-template-columns: repeat(8, minmax(2.1rem, 1fr));
  gap: 0.25rem;
}

.layout-field {
  display: grid;
  min-height: 40px;
  place-items: center;
  border: 1px solid var(--system-border);
  border-radius: 5px;
  color: var(--icons);
  font-family: var(--mono-font);
  font-size: 0.78em;
  font-variant-numeric: tabular-nums;
  transition: opacity 180ms ease, transform 180ms ease, background-color 180ms ease;
}

.layout-field.is-p { box-shadow: inset 0 -3px 0 #2563eb; }
.layout-field.is-q { box-shadow: inset 0 -3px 0 #0f766e; }
.layout-field.is-t { box-shadow: inset 0 -3px 0 #c2410c; }
.layout-field.is-i { box-shadow: inset 0 -3px 0 #7c3aed; }
.layout-field.is-m { box-shadow: inset 0 -3px 0 #64748b; }

.layout-field.is-padding {
  border-style: dashed;
  color: var(--icons);
  box-shadow: none;
  opacity: 0.62;
}

.layout-field.is-used {
  color: var(--fg);
  background: var(--bg);
}

.layout-field.is-current {
  transform: translateY(-3px);
  border-color: var(--links);
  color: var(--fg);
  font-weight: 700;
}

.layout-legend {
  margin: 0.65rem 0 0;
  color: var(--icons);
  font-size: 0.82em;
  text-align: center;
}

.layout-progress {
  margin-top: 0.55rem;
  color: var(--icons);
  font-family: var(--mono-font);
  font-size: 0.76em;
  text-align: center;
}

.branch-predictor-states {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.45rem;
  margin: 1.2rem 0 0.8rem;
}

.branch-state {
  display: grid;
  min-height: 68px;
  place-items: center;
  padding: 0.4rem;
  border: 1px solid var(--system-border);
  border-radius: 7px;
  background: var(--bg);
  color: var(--icons);
  font-family: var(--mono-font);
  font-size: 0.76em;
  text-align: center;
  transition: transform 180ms ease, background-color 180ms ease;
}

.branch-state.is-current {
  transform: translateY(-4px);
  border-color: var(--links);
  background: var(--system-active);
  color: var(--fg);
  font-weight: 700;
}

.branch-prediction {
  color: var(--icons);
  font-family: var(--mono-font);
  text-align: center;
}

.branch-outcomes {
  display: grid;
  grid-template-columns: repeat(12, minmax(1.8rem, 1fr));
  gap: 0.25rem;
  margin-top: 0.8rem;
}

.branch-outcome {
  display: grid;
  min-height: 34px;
  place-items: center;
  border: 1px solid var(--system-border);
  border-radius: 5px;
  background: var(--bg);
  color: var(--icons);
  font-family: var(--mono-font);
  font-size: 0.76em;
}

.branch-outcome.is-correct {
  color: var(--fg);
  background: var(--system-active);
}

.branch-outcome.is-miss {
  color: var(--fg);
  box-shadow: inset 0 -3px 0 var(--links);
}

.branch-outcome.is-next {
  border-color: var(--links);
  font-weight: 700;
}

.vm-translation {
  display: grid;
  grid-template-columns: 1fr auto 1fr auto 1fr;
  gap: 0.5rem;
  align-items: center;
  margin: 1.1rem 0;
}

.vm-translation-stage {
  display: grid;
  min-height: 76px;
  place-items: center;
  padding: 0.5rem;
  border: 1px solid var(--system-border);
  border-radius: 8px;
  background: var(--bg);
  text-align: center;
}

.vm-translation-stage strong {
  font-family: var(--mono-font);
  font-variant-numeric: tabular-nums;
}

.vm-stage-label {
  color: var(--icons);
  font-size: 0.75em;
}

.vm-arrow {
  color: var(--icons);
  font-size: 1.3rem;
}

.vm-tables {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.8rem;
}

.vm-table-panel {
  min-width: 0;
}

.vm-table-title {
  margin-bottom: 0.35rem;
  color: var(--icons);
  font-size: 0.8em;
  text-align: center;
}

.vm-table-entries {
  display: grid;
  gap: 0.3rem;
}

.vm-entry {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.35rem 0.55rem;
  border: 1px solid var(--system-border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--icons);
  font-family: var(--mono-font);
  font-size: 0.78em;
  transition: transform 180ms ease, background-color 180ms ease;
}

.vm-entry.is-present {
  color: var(--fg);
  background: var(--system-active);
}

.vm-entry.is-current {
  transform: translateX(4px);
  border-color: var(--links);
  color: var(--fg);
  font-weight: 700;
}

.vm-access-sequence {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.25rem;
  margin-top: 0.75rem;
}

.vm-access {
  min-width: 2.3rem;
  padding: 0.25rem 0.35rem;
  border: 1px solid var(--system-border);
  border-radius: 5px;
  background: var(--bg);
  color: var(--icons);
  font-family: var(--mono-font);
  font-size: 0.74em;
  text-align: center;
}

.vm-access.is-hit {
  color: var(--fg);
  background: var(--system-active);
}

.vm-access.is-walk {
  color: var(--fg);
  box-shadow: inset 0 -3px 0 var(--links);
}

.vm-access.is-next {
  border-color: var(--links);
  font-weight: 700;
}

@media (max-width: 900px) {
  .memory-lab-layout {
    grid-template-columns: 1fr;
  }

  .latency-rung {
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "name value"
      "bar bar";
    gap: 0.25rem 0.7rem;
    padding: 0.45rem 0;
  }

  .latency-rung__name { grid-area: name; }
  .latency-rung__bar { grid-area: bar; }
  .latency-rung__value { grid-area: value; text-align: right; }
}

@media (max-width: 680px) {
  .memory-lines {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .layout-memory-lines {
    grid-template-columns: 1fr;
  }

  .latency-ladder__header {
    display: grid;
  }

  .vm-translation {
    grid-template-columns: 1fr;
  }

  .vm-arrow {
    transform: rotate(90deg);
    text-align: center;
  }
}

@media (max-width: 440px) {
  .system-demo {
    padding: 0.75rem;
  }

  .memory-lines {
    grid-template-columns: 1fr;
  }

  .memory-lines.is-node-layout {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .latency-rung__value {
    font-size: 0.64em;
  }

  .system-demo-controls button {
    min-height: 44px;
  }

  .layout-fields {
    gap: 0.15rem;
    grid-template-columns: repeat(8, minmax(1.85rem, 1fr));
  }

  .layout-field {
    min-height: 36px;
    font-size: 0.68em;
  }

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

  .branch-outcomes {
    grid-template-columns: repeat(8, minmax(1.8rem, 1fr));
  }

  .vm-tables {
    grid-template-columns: 1fr;
  }
}

@media (prefers-reduced-motion: reduce) {
  .memory-line {
    transition: none;
  }

  .layout-memory-line,
  .layout-field,
  .branch-state,
  .vm-entry {
    transition: none;
  }
}
